]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor.tests/src/no/uio/ifi/refaktor/tests/SearchBasedExtractAndMoveMethodAnalyzerTest.java
Moving comparators to no.uio.ifi.refaktor.analyze.comparators
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor.tests / src / no / uio / ifi / refaktor / tests / SearchBasedExtractAndMoveMethodAnalyzerTest.java
1 package no.uio.ifi.refaktor.tests;
2
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;
14
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;
24
25 public class SearchBasedExtractAndMoveMethodAnalyzerTest {
26
27         private IProgressMonitor monitor;
28
29         @Before
30         public void setUp() throws Exception {
31                 monitor = new NullProgressMonitor();
32                 ExampleCodeManager.INSTANCE.loadExampleCode(monitor);
33         }
34
35         @Test(expected=NoTargetFoundException.class)
36         public void testNoTarget() {
37                 Analyzer analyzer = new SearchBasedExtractAndMoveMethodAnalyzer(getTestDataMethod("emptyMethod"));
38                 analyzer.analyze();
39         }
40
41         @Test
42         public void testSimpleMethod() throws JavaModelException {
43                 IMethod method = getTestDataMethod("simpleMethod");
44
45                 SearchBasedExtractAndMoveMethodAnalyzer analyzer = new SearchBasedExtractAndMoveMethodAnalyzer(method);
46                 analyzer.analyze();
47
48                 int methodOffset = method.getSourceRange().getOffset();
49                 CompilationUnitTextSelection textSelection = new CompilationUnitTextSelection(method.getCompilationUnit(), methodOffset + 69, 10);
50                 assertThat(textSelection.isEquivalentTo(analyzer.getResult().textSelection));
51
52                 ASTNode coveredNode = textSelection.getCoveredNode();
53                 assertThat(coveredNode, instanceOf(ExpressionStatement.class));
54
55                 ExpressionStatement expressionStatement = (ExpressionStatement) coveredNode;
56                 assertThat(expressionStatement.getExpression(), instanceOf(MethodInvocation.class));
57
58                 MethodInvocation methodInvocation = (MethodInvocation) expressionStatement.getExpression();
59                 Prefix prefix = new Prefix(methodInvocation);
60                 assertTrue(prefix.getVariableBindingOfFirstExpression().isEqualTo(analyzer.getResult().calculateOriginalTarget()));
61         }
62
63         @Test
64         public void testSearchableMethodFavorNoUnfixes() throws Exception {
65                 IMethod method = getTestDataMethod("searchableMethod");
66
67                 SearchBasedExtractAndMoveMethodAnalyzer analyzer = new SearchBasedExtractAndMoveMethodAnalyzer(method, new FavorNoUnfixesAnalysisResultComparator());
68                 analyzer.analyze();
69
70                 int methodOffset = method.getSourceRange().getOffset();
71                 CompilationUnitTextSelection textSelection = new CompilationUnitTextSelection(method.getCompilationUnit(), methodOffset + 100, 31);
72                 assertThat(textSelection.isEquivalentTo(analyzer.getResult().textSelection)); 
73
74                 ASTNode coveredNode = textSelection.getCoveredNode();
75                 assertThat(coveredNode, instanceOf(ExpressionStatement.class));
76
77                 ExpressionStatement expressionStatement = (ExpressionStatement) coveredNode;
78                 assertThat(expressionStatement.getExpression(), instanceOf(MethodInvocation.class));
79
80                 MethodInvocation methodInvocation = (MethodInvocation) expressionStatement.getExpression();
81                 Prefix prefix = new Prefix(methodInvocation);
82                 assertTrue(prefix.getVariableBindingOfFirstExpression().isEqualTo(analyzer.getResult().calculateOriginalTarget()));
83         }
84
85         @Test(expected=NoTargetFoundException.class)
86         public void testMethodWithPrefixedMultilineComment() {
87                 Analyzer analyzer = new SearchBasedExtractAndMoveMethodAnalyzer(getTestDataMethod("iAmPrefixedWithAMultilineComment"));
88                 analyzer.analyze();
89         }
90
91         @Test(expected=NoTargetFoundException.class)
92         public void testAbstractMethod() {
93                 Analyzer analyzer = new SearchBasedExtractAndMoveMethodAnalyzer(getTestDataMethod("iAmAbstract"));
94                 analyzer.analyze();
95         }
96
97         @Test(expected=NoTargetFoundException.class)
98         public void testMethodWithJavadocAttachedAndASingleLineCommentAbove() {
99                 Analyzer analyzer = new SearchBasedExtractAndMoveMethodAnalyzer(getTestDataMethod("iHaveJavadocAttachedButThereIsAlsoASingleLineCommentAboveMe"));
100                 analyzer.analyze();
101         }
102
103         @Test
104         public void testOverloadedMethod() {
105                 try {
106                         IMethod method = getTestDataMethod("iAmAnOverloadedMethod", "QA.DuplicateType;");
107                         Analyzer analyzer = new SearchBasedExtractAndMoveMethodAnalyzer(method);
108                         analyzer.analyze();
109                 } catch (NoTargetFoundException e) {
110                         // Expected. 
111                         // Catch needed for the test to display messages of assert errors.
112                 }
113         }
114
115         private IMethod getTestDataMethod(String methodName, String... parameters) {
116                 return RefaktorHandleUtils.findMethodHandleChecked(ExampleCodeManager.INSTANCE.getProject(), "searchBased", "Main", methodName, parameters);
117         }
118 }