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