1 package no.uio.ifi.refaktor.tests;
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;
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;
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;
21 public class TextSelectionsGeneratorTest {
23 private static class S {
25 private final int offset;
26 private final int length;
28 private S(int offset, int length) {
35 public static void beforeClass() throws Exception {
36 // Loading project only once since it shall not be altered between tests
37 ExampleCodeManager.INSTANCE.loadExampleCode();
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));
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));
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));
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));
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));
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));
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));
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));
81 private void assertThatMethodContainsSelections(String methodName, S... methodRelativeSelections) throws JavaModelException {
82 IMethod enclosingMethod = getTestDataMethod(methodName);
83 assertThat(enclosingMethod.exists());
85 ICompilationUnit compilationUnit = enclosingMethod.getCompilationUnit();
86 Set<CompilationUnitTextSelection> textSelections =
87 TextSelectionsGenerator.generateTextSelections(new StatementListsCreator(enclosingMethod), compilationUnit);
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);
96 assertThat(textSelections.size(), is(methodRelativeSelections.length));
97 assertThat(textSelections, hasItems(expectedSelections));
100 private IMethod getTestDataMethod(String methodName, String... parameters) {
101 return RefaktorHandleUtils.findMethodHandleChecked(ExampleCodeManager.INSTANCE.getProject(), "selectionsGenerator", "Main", methodName, parameters);