1 package no.uio.ifi.refaktor.tests;
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;
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;
23 public class CompilationUnitTextSelectionTest {
24 // NOTE: if any tests shall alter any code, the example code
25 // must be loaded in setUp method
27 private CompilationUnitTextSelection textSelection;
28 private CompilationUnitTextSelection equalTextSelection;
33 public static void setUpClass() throws Exception {
34 ExampleCodeManager.INSTANCE.loadExampleCode();
38 public void setUp() throws Exception {
42 private void createSelections() throws JavaModelException {
43 textSelection = createTextSelection();
44 IMethod method = getTestDataMethod("testing");
45 offset = method.getSourceRange().getOffset() + 46;
47 ITextSelection otherSelection = new TextSelection(offset, length);
48 equalTextSelection = new CompilationUnitTextSelection(method.getCompilationUnit(), otherSelection);
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());
61 public void testOffsetLengthEnd() throws JavaModelException {
62 assertTrue(textSelection.getOffset() == offset);
63 assertTrue(textSelection.getLength() == length);
64 assertTrue(textSelection.getEnd() == offset + length);
68 public void testCoveredNode() throws JavaModelException {
69 assertTrue(textSelection.getCoveredNode().subtreeMatch(new ASTMatcher(), equalTextSelection.getCoveredNode()));
73 public void testCoveringNode() throws JavaModelException {
74 assertTrue(textSelection.getCoveringNode().subtreeMatch(new ASTMatcher(), equalTextSelection.getCoveringNode()));
77 private CompilationUnitTextSelection createTextSelection()
78 throws JavaModelException {
79 IMethod method = getTestDataMethod("testing");
80 CompilationUnitTextSelection textSelection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset() + 46, 31);
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());
90 CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset(), method.getSourceRange().getLength());
91 assertThat(selection.getFullyQualifiedNameOfSurroundingType(), equalTo("selection.Main$InnerClass"));
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());
100 CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset(), method.getSourceRange().getLength());
101 assertThat(selection.getFullyQualifiedNameOfSurroundingType(), equalTo("selection.Main$1"));
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());
110 CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset(), method.getSourceRange().getLength());
111 assertThat(selection.getFullyQualifiedNameOfSurroundingType(), equalTo("selection.Main$ALocalClass"));
115 public void testLastStatement() throws Exception {
116 IMethod method = RefaktorHandleUtils.findMethodHandle(ExampleCodeManager.INSTANCE.getProject(), "searchBased", "Main", "iHaveAConditionalWithAReturnStatement", new String[] {});
117 assertThat(method.exists());
119 CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset() + 80, 46);
120 assertThat(selection.getLastStatement(), instanceOf(IfStatement.class));
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());