package no.uio.ifi.refaktor.tests; import static no.uio.ifi.refaktor.assertion.RefaktorAssert.assertThat; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.instanceOf; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import no.uio.ifi.refaktor.examples.manager.ExampleCodeManager; import no.uio.ifi.refaktor.utils.CompilationUnitTextSelection; import no.uio.ifi.refaktor.utils.RefaktorHandleUtils; import org.eclipse.core.resources.IProject; import org.eclipse.jdt.core.IMethod; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.dom.ASTMatcher; import org.eclipse.jdt.core.dom.IfStatement; import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.text.TextSelection; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class CompilationUnitTextSelectionTest { // NOTE: if any tests shall alter any code, the example code // must be loaded in setUp method private CompilationUnitTextSelection textSelection; private CompilationUnitTextSelection equalTextSelection; private int offset; private int length; @BeforeClass public static void setUpClass() throws Exception { ExampleCodeManager.INSTANCE.loadExampleCode(); } @Before public void setUp() throws Exception { createSelections(); } private void createSelections() throws JavaModelException { textSelection = createTextSelection(); IMethod method = getTestDataMethod("testing"); offset = method.getSourceRange().getOffset() + 46; length = 31; ITextSelection otherSelection = new TextSelection(offset, length); equalTextSelection = new CompilationUnitTextSelection(method.getCompilationUnit(), otherSelection); } @Test public void testEquals() throws JavaModelException { assertEquals(textSelection, equalTextSelection); assertTrue(textSelection.hashCode() == equalTextSelection.hashCode()); assertTrue(textSelection.getOffset() == equalTextSelection.getOffset()); assertTrue(textSelection.getLength() == equalTextSelection.getLength()); assertTrue(textSelection.getEnd() == equalTextSelection.getEnd()); } @Test public void testOffsetLengthEnd() throws JavaModelException { assertTrue(textSelection.getOffset() == offset); assertTrue(textSelection.getLength() == length); assertTrue(textSelection.getEnd() == offset + length); } @Test public void testCoveredNode() throws JavaModelException { assertTrue(textSelection.getCoveredNode().subtreeMatch(new ASTMatcher(), equalTextSelection.getCoveredNode())); } @Test public void testCoveringNode() throws JavaModelException { assertTrue(textSelection.getCoveringNode().subtreeMatch(new ASTMatcher(), equalTextSelection.getCoveringNode())); } private CompilationUnitTextSelection createTextSelection() throws JavaModelException { IMethod method = getTestDataMethod("testing"); CompilationUnitTextSelection textSelection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset() + 46, 31); return textSelection; } @Test public void testCoveringType() throws Exception { IProject project = ExampleCodeManager.INSTANCE.getProject(); IMethod method = RefaktorHandleUtils.findMethodHandleChecked(project, "selection", "Main$InnerClass", "methodOfInnerClass"); assertThat(method.exists()); CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset(), method.getSourceRange().getLength()); assertThat(selection.getFullyQualifiedNameOfSurroundingType(), equalTo("selection.Main$InnerClass")); } @Test public void testCoveringTypeAnonymous() throws Exception { IProject project = ExampleCodeManager.INSTANCE.getProject(); IMethod method = RefaktorHandleUtils.findMethodHandleChecked(project, "selection", "Main$1", "foo", "QB;"); assertThat(method.exists()); CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset(), method.getSourceRange().getLength()); assertThat(selection.getFullyQualifiedNameOfSurroundingType(), equalTo("selection.Main$1")); } @Test public void testCoveringTypeLocal() throws Exception { IProject project = ExampleCodeManager.INSTANCE.getProject(); IMethod method = RefaktorHandleUtils.findMethodHandleChecked(project, "selection", "Main$ALocalClass", "foo", "QB;"); assertThat(method.exists()); CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset(), method.getSourceRange().getLength()); assertThat(selection.getFullyQualifiedNameOfSurroundingType(), equalTo("selection.Main$ALocalClass")); } @Test public void testLastStatement() throws Exception { IMethod method = RefaktorHandleUtils.findMethodHandle(ExampleCodeManager.INSTANCE.getProject(), "searchBased", "Main", "iHaveAConditionalWithAReturnStatement", new String[] {}); assertThat(method.exists()); CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(), method.getSourceRange().getOffset() + 80, 46); assertThat(selection.getLastStatement(), instanceOf(IfStatement.class)); } private IMethod getTestDataMethod(String methodName) throws JavaModelException { IMethod method = RefaktorHandleUtils.findMethodHandle(ExampleCodeManager.INSTANCE.getProject(), "selection", "Main", methodName, new String[] {}); assertTrue(method.exists()); return method; } }