]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor.tests/src/no/uio/ifi/refaktor/tests/CompilationUnitTextSelectionTest.java
7e72ac3e505fd228f9273d693eefac2eeea06925
[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.Test;
21
22 public class CompilationUnitTextSelectionTest {
23
24         private CompilationUnitTextSelection textSelection;
25         private CompilationUnitTextSelection equalTextSelection;
26         private int offset;
27         private int length;
28
29         @Before
30         public void setUp() throws Exception {
31                 ExampleCodeManager.INSTANCE.loadExampleCode();
32                 createSelections();
33         }
34
35         private void createSelections() throws JavaModelException {
36                 textSelection = createTextSelection();
37                 IMethod method = getTestDataMethod("testing");
38                 offset = method.getSourceRange().getOffset() + 46;
39                 length = 31;
40                 ITextSelection otherSelection = new TextSelection(offset, length);
41                 equalTextSelection = new CompilationUnitTextSelection(method.getCompilationUnit(), otherSelection);
42         }
43
44         @Test
45         public void testEquals() throws JavaModelException {
46                 assertEquals(textSelection, equalTextSelection);
47                 assertTrue(textSelection.hashCode() == equalTextSelection.hashCode());
48                 assertTrue(textSelection.getOffset() == equalTextSelection.getOffset());
49                 assertTrue(textSelection.getLength() == equalTextSelection.getLength());
50                 assertTrue(textSelection.getEnd() == equalTextSelection.getEnd());
51         }
52
53         @Test
54         public void testOffsetLengthEnd() throws JavaModelException {
55                 assertTrue(textSelection.getOffset() == offset);
56                 assertTrue(textSelection.getLength() == length);
57                 assertTrue(textSelection.getEnd() == offset + length);
58         }
59
60         @Test
61         public void testCoveredNode() throws JavaModelException {
62                 assertTrue(textSelection.getCoveredNode().subtreeMatch(new ASTMatcher(), equalTextSelection.getCoveredNode()));
63         }
64
65         @Test
66         public void testCoveringNode() throws JavaModelException {
67                 assertTrue(textSelection.getCoveringNode().subtreeMatch(new ASTMatcher(), equalTextSelection.getCoveringNode()));
68         }
69
70         private CompilationUnitTextSelection createTextSelection()
71                         throws JavaModelException {
72                 IMethod method = getTestDataMethod("testing"); 
73                 CompilationUnitTextSelection textSelection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset() + 46, 31);
74                 return textSelection;
75         }
76         
77         @Test
78         public void testCoveringType() throws Exception {
79                 IProject project = ExampleCodeManager.INSTANCE.getProject();
80                 IMethod method = RefaktorHandleUtils.findMethodHandleChecked(project, "selection", "Main.InnerClass", "methodOfInnerClass");
81                 assertThat(method.exists());
82                 
83                 CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset(), method.getSourceRange().getLength());
84                 assertThat(selection.getFullyQualifiedNameOfSurroundingType(), equalTo("selection.Main.InnerClass"));
85         }
86         
87         @Test
88         public void testLastStatement() throws Exception {
89                 IMethod method = RefaktorHandleUtils.findMethodHandle(ExampleCodeManager.INSTANCE.getProject(), "searchBased", "Main", "iHaveAConditionalWithAReturnStatement", new String[] {});
90                 assertThat(method.exists());
91                 
92                 CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset() + 80, 46);
93                 assertThat(selection.getLastStatement(), instanceOf(IfStatement.class));
94         }
95
96         private IMethod getTestDataMethod(String methodName) throws JavaModelException {
97                 IMethod method = RefaktorHandleUtils.findMethodHandle(ExampleCodeManager.INSTANCE.getProject(), "selection", "Main", methodName, new String[] {});
98                 assertTrue(method.exists());
99                 return method;
100         }
101 }