]> git.uio.no Git - ifi-stolz-refaktor.git/blame - software/no.uio.ifi.refaktor.tests/src/no/uio/ifi/refaktor/tests/CompilationUnitTextSelectionTest.java
LegalStatementsTest: refactoring
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor.tests / src / no / uio / ifi / refaktor / tests / CompilationUnitTextSelectionTest.java
CommitLineData
f7008f4f
EK
1package no.uio.ifi.refaktor.tests;
2
b544609d
EK
3import static no.uio.ifi.refaktor.assertion.RefaktorAssert.assertThat;
4import static org.hamcrest.CoreMatchers.equalTo;
5748b416
EK
5import static org.hamcrest.CoreMatchers.instanceOf;
6import static org.junit.Assert.assertEquals;
7import static org.junit.Assert.assertTrue;
f7008f4f
EK
8import no.uio.ifi.refaktor.examples.manager.ExampleCodeManager;
9import no.uio.ifi.refaktor.utils.CompilationUnitTextSelection;
10import no.uio.ifi.refaktor.utils.RefaktorHandleUtils;
11
b544609d 12import org.eclipse.core.resources.IProject;
f7008f4f
EK
13import org.eclipse.jdt.core.IMethod;
14import org.eclipse.jdt.core.JavaModelException;
93607c08 15import org.eclipse.jdt.core.dom.ASTMatcher;
5748b416 16import org.eclipse.jdt.core.dom.IfStatement;
f7008f4f
EK
17import org.eclipse.jface.text.ITextSelection;
18import org.eclipse.jface.text.TextSelection;
19import org.junit.Before;
20import org.junit.Test;
21
22public class CompilationUnitTextSelectionTest {
23
93607c08
EK
24 private CompilationUnitTextSelection textSelection;
25 private CompilationUnitTextSelection equalTextSelection;
26 private int offset;
27 private int length;
28
f7008f4f
EK
29 @Before
30 public void setUp() throws Exception {
31 ExampleCodeManager.INSTANCE.loadExampleCode();
93607c08 32 createSelections();
f7008f4f
EK
33 }
34
93607c08
EK
35 private void createSelections() throws JavaModelException {
36 textSelection = createTextSelection();
f7008f4f 37 IMethod method = getTestDataMethod("testing");
93607c08
EK
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 {
f7008f4f
EK
46 assertEquals(textSelection, equalTextSelection);
47 assertTrue(textSelection.hashCode() == equalTextSelection.hashCode());
93607c08
EK
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()));
f7008f4f
EK
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 }
b544609d
EK
76
77 @Test
78 public void testCoveringType() throws Exception {
79 IProject project = ExampleCodeManager.INSTANCE.getProject();
5e8e86e8 80 IMethod method = RefaktorHandleUtils.findMethodHandleChecked(project, "selection", "Main$InnerClass", "methodOfInnerClass");
b544609d
EK
81 assertThat(method.exists());
82
83 CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset(), method.getSourceRange().getLength());
5e8e86e8 84 assertThat(selection.getFullyQualifiedNameOfSurroundingType(), equalTo("selection.Main$InnerClass"));
b544609d 85 }
5748b416 86
f82892ff
EK
87 @Test
88 public void testCoveringTypeAnonymous() throws Exception {
89 IProject project = ExampleCodeManager.INSTANCE.getProject();
90 IMethod method = RefaktorHandleUtils.findMethodHandleChecked(project, "selection", "Main$1", "foo", "QB;");
91 assertThat(method.exists());
92
93 CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset(), method.getSourceRange().getLength());
94 assertThat(selection.getFullyQualifiedNameOfSurroundingType(), equalTo("selection.Main$1"));
95 }
96
25a3136b
EK
97 @Test
98 public void testCoveringTypeLocal() throws Exception {
99 IProject project = ExampleCodeManager.INSTANCE.getProject();
100 IMethod method = RefaktorHandleUtils.findMethodHandleChecked(project, "selection", "Main$ALocalClass", "foo", "QB;");
101 assertThat(method.exists());
102
103 CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset(), method.getSourceRange().getLength());
104 assertThat(selection.getFullyQualifiedNameOfSurroundingType(), equalTo("selection.Main$ALocalClass"));
105 }
106
5748b416
EK
107 @Test
108 public void testLastStatement() throws Exception {
109 IMethod method = RefaktorHandleUtils.findMethodHandle(ExampleCodeManager.INSTANCE.getProject(), "searchBased", "Main", "iHaveAConditionalWithAReturnStatement", new String[] {});
110 assertThat(method.exists());
111
112 CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset() + 80, 46);
113 assertThat(selection.getLastStatement(), instanceOf(IfStatement.class));
114 }
f7008f4f
EK
115
116 private IMethod getTestDataMethod(String methodName) throws JavaModelException {
117 IMethod method = RefaktorHandleUtils.findMethodHandle(ExampleCodeManager.INSTANCE.getProject(), "selection", "Main", methodName, new String[] {});
118 assertTrue(method.exists());
119 return method;
120 }
121}