--- /dev/null
+package no.uio.ifi.refaktor.tests;
+
+import static no.uio.ifi.refaktor.RefaktorAssert.assertThat;
+import static org.hamcrest.CoreMatchers.allOf;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.hasItem;
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.notNullValue;
+
+import no.uio.ifi.refaktor.analyze.CollectorManager;
+import no.uio.ifi.refaktor.analyze.collectors.PrefixesCollector;
+import no.uio.ifi.refaktor.examples.manager.ExampleCodeManager;
+import no.uio.ifi.refaktor.prefix.Prefix;
+import no.uio.ifi.refaktor.prefix.PrefixSet;
+import no.uio.ifi.refaktor.utils.CompilationUnitTextSelection;
+import no.uio.ifi.refaktor.utils.ParseUtils;
+import no.uio.ifi.refaktor.utils.RefaktorHandleUtils;
+
+import org.eclipse.jdt.core.IMethod;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.dom.ASTNode;
+import org.eclipse.jdt.core.dom.CompilationUnit;
+import org.eclipse.jdt.core.dom.Expression;
+import org.eclipse.jdt.internal.corext.refactoring.structure.ASTNodeSearchUtil;
+import org.junit.Before;
+import org.junit.Test;
+
+@SuppressWarnings("restriction")
+public class PrefixesCollectorTest {
+
+ @Before
+ public void setUp() throws Exception {
+ ExampleCodeManager.INSTANCE.loadExampleCode();
+ }
+
+ @Test
+ public void testBuild() throws Exception {
+ ExampleCodeManager.INSTANCE.buildProjectBeforeTest();
+ }
+
+ @Test
+ public void testWithoutSimpleExpressions_a() throws Exception {
+ testThatPrefixIsFound("withoutSimpleExpressions", 44, 1, 3);
+ }
+
+ @Test
+ public void testWithoutSimpleExpressions_a_b() throws Exception {
+ testThatPrefixIsFound("withoutSimpleExpressions", 61, 3, 2);
+ }
+
+ @Test
+ public void testWithoutSimpleExpressions_a_c() throws Exception {
+ testThatPrefixIsFound("withoutSimpleExpressions", 76, 3, 1);
+ }
+
+ @Test
+ public void testWithoutSimpleExpressionsCount() throws Exception {
+ PrefixSet prefixes = collectPrefixes(getTestDataMethod("withoutSimpleExpressions"));
+ assertThat(prefixes.size(), equalTo(3));
+ }
+
+ private void testThatPrefixIsFound(String methodName, int offset, int length, int count) throws JavaModelException {
+ IMethod method = getTestDataMethod(methodName);
+ PrefixSet prefixes = collectPrefixes(method);
+ CompilationUnit root = (CompilationUnit) ParseUtils.parse(method.getCompilationUnit()).getRoot();
+
+ ASTNode node = ASTNodeSearchUtil.getAstNode(root, method.getSourceRange().getOffset() + offset, length);
+ assertThat(node, allOf(notNullValue(), instanceOf(Expression.class)));
+ Prefix prefix = new Prefix((Expression) node);
+ assertThat(prefixes, hasItem(prefix));
+ assertThat(prefixes.get(prefix).getCount(), equalTo(count));
+ }
+
+ private PrefixSet collectPrefixes(IMethod method) throws JavaModelException {
+ CompilationUnitTextSelection selection = new CompilationUnitTextSelection(method.getCompilationUnit(),
+ method.getSourceRange().getOffset(), method.getSourceRange().getLength());
+ PrefixesCollector collector = new PrefixesCollector(selection);
+ CollectorManager.collectProperties(selection, collector);
+ return collector.getPrefixes();
+ }
+
+ private IMethod getTestDataMethod(String methodName, String... parameters) {
+ return RefaktorHandleUtils.findMethodHandleChecked(ExampleCodeManager.INSTANCE.getProject(), "prefixes", "Main", methodName, parameters);
+ }
+}