]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor.tests/src/no/uio/ifi/refaktor/tests/CompilationUnitTextSelectionTest.java
CompilationUnitTextSelectionTest: optimizing
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor.tests / src / no / uio / ifi / refaktor / tests / CompilationUnitTextSelectionTest.java
1 package no.uio.ifi.refaktor.tests;
2
3 import static no.uio.ifi.refaktor.assertion.RefaktorAssert.assertThat;
4 import static org.hamcrest.CoreMatchers.equalTo;
5 import static org.hamcrest.CoreMatchers.instanceOf;
6 import static org.junit.Assert.assertEquals;
7 import static org.junit.Assert.assertTrue;
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
12 import org.eclipse.core.resources.IProject;
13 import org.eclipse.jdt.core.IMethod;
14 import org.eclipse.jdt.core.JavaModelException;
15 import org.eclipse.jdt.core.dom.ASTMatcher;
16 import org.eclipse.jdt.core.dom.IfStatement;
17 import org.eclipse.jface.text.ITextSelection;
18 import org.eclipse.jface.text.TextSelection;
19 import org.junit.Before;
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22
23 public class CompilationUnitTextSelectionTest {
24         // NOTE: if any tests shall alter any code, the example code
25         // must be loaded in setUp method
26
27         private CompilationUnitTextSelection textSelection;
28         private CompilationUnitTextSelection equalTextSelection;
29         private int offset;
30         private int length;
31
32         @BeforeClass
33         public static void setUpClass() throws Exception {
34                 ExampleCodeManager.INSTANCE.loadExampleCode();
35         }
36         
37         @Before
38         public void setUp() throws Exception {
39                 createSelections();
40         }
41
42         private void createSelections() throws JavaModelException {
43                 textSelection = createTextSelection();
44                 IMethod method = getTestDataMethod("testing");
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 {
53                 assertEquals(textSelection, equalTextSelection);
54                 assertTrue(textSelection.hashCode() == equalTextSelection.hashCode());
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()));
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         }
83         
84         @Test
85         public void testCoveringType() throws Exception {
86                 IProject project = ExampleCodeManager.INSTANCE.getProject();
87                 IMethod method = RefaktorHandleUtils.findMethodHandleChecked(project, "selection", "Main$InnerClass", "methodOfInnerClass");
88                 assertThat(method.exists());
89                 
90                 CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset(), method.getSourceRange().getLength());
91                 assertThat(selection.getFullyQualifiedNameOfSurroundingType(), equalTo("selection.Main$InnerClass"));
92         }
93         
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         
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         
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         }
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 }