]>
Commit | Line | Data |
---|---|---|
1 | package no.uio.ifi.refaktor.tests; | |
2 | ||
3 | import static no.uio.ifi.refaktor.assertion.RefaktorAssert.assertThat; | |
4 | import static org.hamcrest.CoreMatchers.is; | |
5 | import no.uio.ifi.refaktor.analyze.analyzers.LegalStatementsChecker; | |
6 | import no.uio.ifi.refaktor.examples.manager.ExampleCodeManager; | |
7 | import no.uio.ifi.refaktor.utils.CompilationUnitTextSelection; | |
8 | import no.uio.ifi.refaktor.utils.RefaktorHandleUtils; | |
9 | ||
10 | import org.eclipse.jdt.core.IMethod; | |
11 | import org.eclipse.jdt.core.JavaModelException; | |
12 | import org.junit.BeforeClass; | |
13 | import org.junit.Test; | |
14 | ||
15 | public class LegalStatementsTest { | |
16 | // NOTE: if any tests shall alter any code, the example code | |
17 | // must be loaded in setUp method | |
18 | ||
19 | private static final boolean NOT_LEGAL = false; | |
20 | ||
21 | @BeforeClass | |
22 | public static void setUp() throws Exception { | |
23 | ExampleCodeManager.INSTANCE.loadExampleCode(); | |
24 | } | |
25 | ||
26 | @Test | |
27 | public void testCheckCallToSuperConstructor() throws Exception { | |
28 | testIfSelectionWithinMethodIsLegal("Main", 18, 8, NOT_LEGAL); | |
29 | } | |
30 | ||
31 | @Test | |
32 | public void testCheckCallToSuperMethod() throws Exception { | |
33 | testIfSelectionWithinMethodIsLegal("superMethod", 65, 20, NOT_LEGAL); | |
34 | } | |
35 | ||
36 | @Test | |
37 | public void testCheckAccessToSuperField() throws Exception { | |
38 | testIfSelectionWithinMethodIsLegal("iAccessSuperField", 58, 37, NOT_LEGAL); | |
39 | } | |
40 | ||
41 | private void testIfSelectionWithinMethodIsLegal(String methodName, int methodRelativeOffset, int length, boolean expectedLegalityOfSelection) throws JavaModelException { | |
42 | IMethod enclosingMethod = getTestDataMethod(methodName); | |
43 | assertThat(enclosingMethod.exists()); | |
44 | int methodOffset = enclosingMethod.getSourceRange().getOffset(); | |
45 | ||
46 | CompilationUnitTextSelection selection = new CompilationUnitTextSelection( | |
47 | enclosingMethod.getCompilationUnit(), methodOffset + methodRelativeOffset , length); | |
48 | LegalStatementsChecker legalStatementsChecker = new LegalStatementsChecker(selection); | |
49 | assertThat(legalStatementsChecker.allStatementsAreLegal(), is(expectedLegalityOfSelection)); | |
50 | } | |
51 | ||
52 | private IMethod getTestDataMethod(String methodName, String... parameters) { | |
53 | return RefaktorHandleUtils.findMethodHandleChecked(ExampleCodeManager.INSTANCE.getProject(), "legal", "Main", methodName, parameters); | |
54 | } | |
55 | ||
56 | } |