]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor.tests/src/no/uio/ifi/refaktor/tests/TextSelectionsGeneratorTest.java
0a992b3e665eb07b7d958c71bdde49776fe78680
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor.tests / src / no / uio / ifi / refaktor / tests / TextSelectionsGeneratorTest.java
1 package no.uio.ifi.refaktor.tests;
2
3 import static no.uio.ifi.refaktor.assertion.RefaktorAssert.assertThat;
4 import static no.uio.ifi.refaktor.assertion.RefaktorAssert.hasItems;
5 import static org.hamcrest.CoreMatchers.is;
6
7 import java.util.Set;
8
9 import no.uio.ifi.refaktor.analyze.analyzers.StatementListsCreator;
10 import no.uio.ifi.refaktor.analyze.analyzers.TextSelectionsGenerator;
11 import no.uio.ifi.refaktor.examples.manager.ExampleCodeManager;
12 import no.uio.ifi.refaktor.utils.CompilationUnitTextSelection;
13 import no.uio.ifi.refaktor.utils.RefaktorHandleUtils;
14
15 import org.eclipse.jdt.core.ICompilationUnit;
16 import org.eclipse.jdt.core.IMethod;
17 import org.eclipse.jdt.core.JavaModelException;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20
21 public class TextSelectionsGeneratorTest {
22
23         private static class S {
24
25                 private final int offset;
26                 private final int length;
27                 
28                 private S(int offset, int length) {
29                         this.offset = offset;
30                         this.length = length;
31                 }
32         }
33
34         @BeforeClass
35         public static void beforeClass() throws Exception {
36                 // Loading project only once since it shall not be altered between tests
37                 ExampleCodeManager.INSTANCE.loadExampleCode();
38         }
39         
40         @Test
41         public void testSimpleStatements() throws Exception {
42                 assertThatMethodContainsSelections("simpleStatements", new S(54, 14), new S(71, 14), new S(54, 31), new S(88, 14), new S(71, 31), new S(54, 48));
43         }
44
45         @Test
46         public void testContainingBlock() throws Exception {
47                 assertThatMethodContainsSelections("containingBlock", new S(53, 37), new S(58, 12), new S(74, 12), new S(58, 28));
48         }
49
50         @Test
51         public void testContainingDo() throws Exception {
52                 assertThatMethodContainsSelections("containingDo", new S(50, 38), new S(92, 34), new S(50, 76), new S(58, 12), new S(98, 12));
53         }
54
55         @Test
56         public void testContainingEnhancedFor() throws Exception {
57                 assertThatMethodContainsSelections("containingEnhancedFor", new S(59, 68), new S(131, 62), new S(59, 134), new S(109, 14), new S(179, 14));
58         }
59
60         @Test
61         public void testContainingFor() throws Exception {
62                 assertThatMethodContainsSelections("containingFor", new S(51, 51), new S(106, 45), new S(51, 100), new S(84, 14), new S(137, 14));
63         }
64
65         @Test
66         public void testContainingIfElse() throws Exception {
67                 assertThatMethodContainsSelections("containingIfElse", new S(54, 58), new S(116, 79), new S(54, 141), 
68                                 new S(69, 12), new S(96, 12), new S(129, 12), new S(163, 12), new S(187, 8), new S(149, 46));
69         }
70
71         @Test
72         public void testContainingLabel() throws Exception {
73                 assertThatMethodContainsSelections("containingLabel", new S(53, 28), new S(84, 16), new S(53, 47), new S(65, 12), new S(92, 8));
74         }
75
76         @Test
77         public void testContainingWhile() throws Exception {
78                 assertThatMethodContainsSelections("containingWhile", new S(53, 34), new S(91, 28), new S(53, 66), new S(71, 12), new S(107, 12));
79         }
80
81         private void assertThatMethodContainsSelections(String methodName, S... methodRelativeSelections) throws JavaModelException {
82                 IMethod enclosingMethod = getTestDataMethod(methodName);
83                 assertThat(enclosingMethod.exists());
84                 
85                 ICompilationUnit compilationUnit = enclosingMethod.getCompilationUnit();
86                 Set<CompilationUnitTextSelection> textSelections = 
87                                 TextSelectionsGenerator.generateTextSelections(new StatementListsCreator(enclosingMethod), compilationUnit);
88                 
89                 int methodOffset = enclosingMethod.getSourceRange().getOffset();
90                 CompilationUnitTextSelection[] expectedSelections = new CompilationUnitTextSelection[methodRelativeSelections.length];
91                 for (int i = 0; i < methodRelativeSelections.length; i++) {
92                         S s = methodRelativeSelections[i];
93                         expectedSelections[i] = new CompilationUnitTextSelection(compilationUnit, methodOffset + s.offset, s.length);
94                 }
95                         
96                 assertThat(textSelections.size(), is(methodRelativeSelections.length));
97                 assertThat(textSelections, hasItems(expectedSelections));
98         }
99
100         private IMethod getTestDataMethod(String methodName, String... parameters) {
101                 return RefaktorHandleUtils.findMethodHandleChecked(ExampleCodeManager.INSTANCE.getProject(), "selectionsGenerator", "Main", methodName, parameters);
102         }
103
104 }