]>
Commit | Line | Data |
---|---|---|
f7008f4f EK |
1 | package no.uio.ifi.refaktor.tests; |
2 | ||
b544609d EK |
3 | import static no.uio.ifi.refaktor.assertion.RefaktorAssert.assertThat; |
4 | import static org.hamcrest.CoreMatchers.equalTo; | |
5748b416 EK |
5 | import static org.hamcrest.CoreMatchers.instanceOf; |
6 | import static org.junit.Assert.assertEquals; | |
7 | import static org.junit.Assert.assertTrue; | |
f7008f4f EK |
8 | import no.uio.ifi.refaktor.examples.manager.ExampleCodeManager; |
9 | import no.uio.ifi.refaktor.utils.CompilationUnitTextSelection; | |
10 | import no.uio.ifi.refaktor.utils.RefaktorHandleUtils; | |
11 | ||
b544609d | 12 | import org.eclipse.core.resources.IProject; |
f7008f4f EK |
13 | import org.eclipse.jdt.core.IMethod; |
14 | import org.eclipse.jdt.core.JavaModelException; | |
93607c08 | 15 | import org.eclipse.jdt.core.dom.ASTMatcher; |
5748b416 | 16 | import org.eclipse.jdt.core.dom.IfStatement; |
f7008f4f EK |
17 | import org.eclipse.jface.text.ITextSelection; |
18 | import org.eclipse.jface.text.TextSelection; | |
19 | import org.junit.Before; | |
dc0c2626 | 20 | import org.junit.BeforeClass; |
f7008f4f EK |
21 | import org.junit.Test; |
22 | ||
23 | public class CompilationUnitTextSelectionTest { | |
dc0c2626 EK |
24 | // NOTE: if any tests shall alter any code, the example code |
25 | // must be loaded in setUp method | |
f7008f4f | 26 | |
93607c08 EK |
27 | private CompilationUnitTextSelection textSelection; |
28 | private CompilationUnitTextSelection equalTextSelection; | |
29 | private int offset; | |
30 | private int length; | |
31 | ||
dc0c2626 EK |
32 | @BeforeClass |
33 | public static void setUpClass() throws Exception { | |
34 | ExampleCodeManager.INSTANCE.loadExampleCode(); | |
35 | } | |
36 | ||
f7008f4f EK |
37 | @Before |
38 | public void setUp() throws Exception { | |
93607c08 | 39 | createSelections(); |
f7008f4f EK |
40 | } |
41 | ||
93607c08 EK |
42 | private void createSelections() throws JavaModelException { |
43 | textSelection = createTextSelection(); | |
f7008f4f | 44 | IMethod method = getTestDataMethod("testing"); |
93607c08 EK |
45 | offset = method.getSourceRange().getOffset() + 46; |
46 | length = 31; | |
47 | ITextSelection otherSelection = new TextSelection(offset, length); | |
48 | equalTextSelection = new CompilationUnitTextSelection(method.getCompilationUnit(), otherSelection); | |
49 | } | |
50 | ||
51 | @Test | |
52 | public void testEquals() throws JavaModelException { | |
f7008f4f EK |
53 | assertEquals(textSelection, equalTextSelection); |
54 | assertTrue(textSelection.hashCode() == equalTextSelection.hashCode()); | |
93607c08 EK |
55 | assertTrue(textSelection.getOffset() == equalTextSelection.getOffset()); |
56 | assertTrue(textSelection.getLength() == equalTextSelection.getLength()); | |
57 | assertTrue(textSelection.getEnd() == equalTextSelection.getEnd()); | |
58 | } | |
59 | ||
60 | @Test | |
61 | public void testOffsetLengthEnd() throws JavaModelException { | |
62 | assertTrue(textSelection.getOffset() == offset); | |
63 | assertTrue(textSelection.getLength() == length); | |
64 | assertTrue(textSelection.getEnd() == offset + length); | |
65 | } | |
66 | ||
67 | @Test | |
68 | public void testCoveredNode() throws JavaModelException { | |
69 | assertTrue(textSelection.getCoveredNode().subtreeMatch(new ASTMatcher(), equalTextSelection.getCoveredNode())); | |
70 | } | |
71 | ||
72 | @Test | |
73 | public void testCoveringNode() throws JavaModelException { | |
74 | assertTrue(textSelection.getCoveringNode().subtreeMatch(new ASTMatcher(), equalTextSelection.getCoveringNode())); | |
f7008f4f EK |
75 | } |
76 | ||
77 | private CompilationUnitTextSelection createTextSelection() | |
78 | throws JavaModelException { | |
79 | IMethod method = getTestDataMethod("testing"); | |
80 | CompilationUnitTextSelection textSelection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset() + 46, 31); | |
81 | return textSelection; | |
82 | } | |
b544609d EK |
83 | |
84 | @Test | |
85 | public void testCoveringType() throws Exception { | |
86 | IProject project = ExampleCodeManager.INSTANCE.getProject(); | |
5e8e86e8 | 87 | IMethod method = RefaktorHandleUtils.findMethodHandleChecked(project, "selection", "Main$InnerClass", "methodOfInnerClass"); |
b544609d EK |
88 | assertThat(method.exists()); |
89 | ||
90 | CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset(), method.getSourceRange().getLength()); | |
5e8e86e8 | 91 | assertThat(selection.getFullyQualifiedNameOfSurroundingType(), equalTo("selection.Main$InnerClass")); |
b544609d | 92 | } |
5748b416 | 93 | |
f82892ff EK |
94 | @Test |
95 | public void testCoveringTypeAnonymous() throws Exception { | |
96 | IProject project = ExampleCodeManager.INSTANCE.getProject(); | |
97 | IMethod method = RefaktorHandleUtils.findMethodHandleChecked(project, "selection", "Main$1", "foo", "QB;"); | |
98 | assertThat(method.exists()); | |
99 | ||
100 | CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset(), method.getSourceRange().getLength()); | |
101 | assertThat(selection.getFullyQualifiedNameOfSurroundingType(), equalTo("selection.Main$1")); | |
102 | } | |
103 | ||
25a3136b EK |
104 | @Test |
105 | public void testCoveringTypeLocal() throws Exception { | |
106 | IProject project = ExampleCodeManager.INSTANCE.getProject(); | |
107 | IMethod method = RefaktorHandleUtils.findMethodHandleChecked(project, "selection", "Main$ALocalClass", "foo", "QB;"); | |
108 | assertThat(method.exists()); | |
109 | ||
110 | CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset(), method.getSourceRange().getLength()); | |
111 | assertThat(selection.getFullyQualifiedNameOfSurroundingType(), equalTo("selection.Main$ALocalClass")); | |
112 | } | |
113 | ||
5748b416 EK |
114 | @Test |
115 | public void testLastStatement() throws Exception { | |
116 | IMethod method = RefaktorHandleUtils.findMethodHandle(ExampleCodeManager.INSTANCE.getProject(), "searchBased", "Main", "iHaveAConditionalWithAReturnStatement", new String[] {}); | |
117 | assertThat(method.exists()); | |
118 | ||
119 | CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset() + 80, 46); | |
120 | assertThat(selection.getLastStatement(), instanceOf(IfStatement.class)); | |
121 | } | |
f7008f4f EK |
122 | |
123 | private IMethod getTestDataMethod(String methodName) throws JavaModelException { | |
124 | IMethod method = RefaktorHandleUtils.findMethodHandle(ExampleCodeManager.INSTANCE.getProject(), "selection", "Main", methodName, new String[] {}); | |
125 | assertTrue(method.exists()); | |
126 | return method; | |
127 | } | |
128 | } |