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.Test;
22 public class CompilationUnitTextSelectionTest {
24 private CompilationUnitTextSelection textSelection;
25 private CompilationUnitTextSelection equalTextSelection;
30 public void setUp() throws Exception {
31 ExampleCodeManager.INSTANCE.loadExampleCode();
35 private void createSelections() throws JavaModelException {
36 textSelection = createTextSelection();
37 IMethod method = getTestDataMethod("testing");
38 offset = method.getSourceRange().getOffset() + 46;
40 ITextSelection otherSelection = new TextSelection(offset, length);
41 equalTextSelection = new CompilationUnitTextSelection(method.getCompilationUnit(), otherSelection);
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());
54 public void testOffsetLengthEnd() throws JavaModelException {
55 assertTrue(textSelection.getOffset() == offset);
56 assertTrue(textSelection.getLength() == length);
57 assertTrue(textSelection.getEnd() == offset + length);
61 public void testCoveredNode() throws JavaModelException {
62 assertTrue(textSelection.getCoveredNode().subtreeMatch(new ASTMatcher(), equalTextSelection.getCoveredNode()));
66 public void testCoveringNode() throws JavaModelException {
67 assertTrue(textSelection.getCoveringNode().subtreeMatch(new ASTMatcher(), equalTextSelection.getCoveringNode()));
70 private CompilationUnitTextSelection createTextSelection()
71 throws JavaModelException {
72 IMethod method = getTestDataMethod("testing");
73 CompilationUnitTextSelection textSelection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset() + 46, 31);
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());
83 CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset(), method.getSourceRange().getLength());
84 assertThat(selection.getFullyQualifiedNameOfSurroundingType(), equalTo("selection.Main.InnerClass"));
88 public void testLastStatement() throws Exception {
89 IMethod method = RefaktorHandleUtils.findMethodHandle(ExampleCodeManager.INSTANCE.getProject(), "searchBased", "Main", "iHaveAConditionalWithAReturnStatement", new String[] {});
90 assertThat(method.exists());
92 CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset() + 80, 46);
93 assertThat(selection.getLastStatement(), instanceOf(IfStatement.class));
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());