1 package no.uio.ifi.refaktor.tests;
3 import static no.uio.ifi.refaktor.RefaktorAssert.assertThat;
4 import static org.hamcrest.CoreMatchers.instanceOf;
5 import static org.junit.Assert.assertTrue;
6 import no.uio.ifi.refaktor.analyze.Analyzer;
7 import no.uio.ifi.refaktor.analyze.NoTargetFoundException;
8 import no.uio.ifi.refaktor.analyze.SearchBasedExtractAndMoveMethodAnalyzer;
9 import no.uio.ifi.refaktor.analyze.comparators.FavorNoUnfixesAnalysisResultComparator;
10 import no.uio.ifi.refaktor.examples.manager.ExampleCodeManager;
11 import no.uio.ifi.refaktor.prefix.Prefix;
12 import no.uio.ifi.refaktor.utils.CompilationUnitTextSelection;
13 import no.uio.ifi.refaktor.utils.RefaktorHandleUtils;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.NullProgressMonitor;
17 import org.eclipse.jdt.core.IMethod;
18 import org.eclipse.jdt.core.JavaModelException;
19 import org.eclipse.jdt.core.dom.ASTNode;
20 import org.eclipse.jdt.core.dom.ExpressionStatement;
21 import org.eclipse.jdt.core.dom.MethodInvocation;
22 import org.junit.Before;
23 import org.junit.Test;
25 public class SearchBasedExtractAndMoveMethodAnalyzerTest {
27 private IProgressMonitor monitor;
30 public void setUp() throws Exception {
31 monitor = new NullProgressMonitor();
32 ExampleCodeManager.INSTANCE.loadExampleCode(monitor);
35 @Test(expected=NoTargetFoundException.class)
36 public void testNoTarget() {
37 Analyzer analyzer = new SearchBasedExtractAndMoveMethodAnalyzer(getTestDataMethod("emptyMethod"));
42 public void testSimpleMethod() throws JavaModelException {
43 IMethod method = getTestDataMethod("simpleMethod");
45 SearchBasedExtractAndMoveMethodAnalyzer analyzer = new SearchBasedExtractAndMoveMethodAnalyzer(method);
48 int methodOffset = method.getSourceRange().getOffset();
49 CompilationUnitTextSelection textSelection = new CompilationUnitTextSelection(method.getCompilationUnit(), methodOffset + 69, 10);
50 assertThat(textSelection.isEquivalentTo(analyzer.getResult().textSelection));
52 ASTNode coveredNode = textSelection.getCoveredNode();
53 assertThat(coveredNode, instanceOf(ExpressionStatement.class));
55 ExpressionStatement expressionStatement = (ExpressionStatement) coveredNode;
56 assertThat(expressionStatement.getExpression(), instanceOf(MethodInvocation.class));
58 MethodInvocation methodInvocation = (MethodInvocation) expressionStatement.getExpression();
59 Prefix prefix = new Prefix(methodInvocation);
60 assertTrue(prefix.getVariableBindingOfFirstExpression().isEqualTo(analyzer.getResult().calculateOriginalTarget()));
64 public void testSearchableMethodFavorNoUnfixes() throws Exception {
65 IMethod method = getTestDataMethod("searchableMethod");
67 SearchBasedExtractAndMoveMethodAnalyzer analyzer = new SearchBasedExtractAndMoveMethodAnalyzer(method, new FavorNoUnfixesAnalysisResultComparator());
70 int methodOffset = method.getSourceRange().getOffset();
71 CompilationUnitTextSelection textSelection = new CompilationUnitTextSelection(method.getCompilationUnit(), methodOffset + 100, 31);
72 assertThat(textSelection.isEquivalentTo(analyzer.getResult().textSelection));
74 ASTNode coveredNode = textSelection.getCoveredNode();
75 assertThat(coveredNode, instanceOf(ExpressionStatement.class));
77 ExpressionStatement expressionStatement = (ExpressionStatement) coveredNode;
78 assertThat(expressionStatement.getExpression(), instanceOf(MethodInvocation.class));
80 MethodInvocation methodInvocation = (MethodInvocation) expressionStatement.getExpression();
81 Prefix prefix = new Prefix(methodInvocation);
82 assertTrue(prefix.getVariableBindingOfFirstExpression().isEqualTo(analyzer.getResult().calculateOriginalTarget()));
85 @Test(expected=NoTargetFoundException.class)
86 public void testMethodWithPrefixedMultilineComment() {
87 Analyzer analyzer = new SearchBasedExtractAndMoveMethodAnalyzer(getTestDataMethod("iAmPrefixedWithAMultilineComment"));
91 @Test(expected=NoTargetFoundException.class)
92 public void testAbstractMethod() {
93 Analyzer analyzer = new SearchBasedExtractAndMoveMethodAnalyzer(getTestDataMethod("iAmAbstract"));
97 @Test(expected=NoTargetFoundException.class)
98 public void testMethodWithJavadocAttachedAndASingleLineCommentAbove() {
99 Analyzer analyzer = new SearchBasedExtractAndMoveMethodAnalyzer(getTestDataMethod("iHaveJavadocAttachedButThereIsAlsoASingleLineCommentAboveMe"));
104 public void testOverloadedMethod() {
106 IMethod method = getTestDataMethod("iAmAnOverloadedMethod", "QA.DuplicateType;");
107 Analyzer analyzer = new SearchBasedExtractAndMoveMethodAnalyzer(method);
109 } catch (NoTargetFoundException e) {
111 // Catch needed for the test to display messages of assert errors.
115 private IMethod getTestDataMethod(String methodName, String... parameters) {
116 return RefaktorHandleUtils.findMethodHandleChecked(ExampleCodeManager.INSTANCE.getProject(), "searchBased", "Main", methodName, parameters);