aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKmunk2012-12-14 09:11:42 (EST)
committerKmunk2012-12-18 16:23:15 (EST)
commit8f059a0ee1337f671775b294f036f9c79389520e (patch)
tree89b2d48825500d707624c364ff285bc63b9319a7
parent6d4480a24349267ba0fe49ba10c0688bef77bcbc (diff)
downloadorg.eclipse.recommenders-8f059a0ee1337f671775b294f036f9c79389520e.zip
org.eclipse.recommenders-8f059a0ee1337f671775b294f036f9c79389520e.tar.gz
org.eclipse.recommenders-8f059a0ee1337f671775b294f036f9c79389520e.tar.bz2
[tests] Added a method for retrieving the package name from code.refs/changes/41/9241/4
The tests for this method are within the JavaProjectFixtureTest class. Inserted a static field that provides a regular expression group for Java identifiers as stated in the Java Language Specification. This field will not only be used in the new method but also in the other find* methods from the JavaProjectFixture class to match Java identifiers. Change-Id: I8f76599a627dfaa88b8ae37ac62ffc41a5a9b226
-rw-r--r--tests/org.eclipse.recommenders.tests/src/org/eclipse/recommenders/tests/jdt/JavaProjectFixture.java48
-rw-r--r--tests/org.eclipse.recommenders.tests/src/org/eclipse/recommenders/tests/jdt/JavaProjectFixtureTest.java124
2 files changed, 163 insertions, 9 deletions
diff --git a/tests/org.eclipse.recommenders.tests/src/org/eclipse/recommenders/tests/jdt/JavaProjectFixture.java b/tests/org.eclipse.recommenders.tests/src/org/eclipse/recommenders/tests/jdt/JavaProjectFixture.java
index 9fa792d..e13806f 100644
--- a/tests/org.eclipse.recommenders.tests/src/org/eclipse/recommenders/tests/jdt/JavaProjectFixture.java
+++ b/tests/org.eclipse.recommenders.tests/src/org/eclipse/recommenders/tests/jdt/JavaProjectFixture.java
@@ -1,5 +1,5 @@
/**
- * Copyright (c) 2010, 2011 Darmstadt University of Technology.
+ * Copyright (c) 2010, 2011, 2012 Darmstadt University of Technology.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,6 +7,7 @@
*
* Contributors:
* Marcel Bruch - initial API and implementation.
+ * Kevin Munk - Extension of method for finding package names and correct regular expression for Java identifier.
*/
package org.eclipse.recommenders.tests.jdt;
@@ -55,11 +56,18 @@ import com.google.common.collect.Sets;
public class JavaProjectFixture {
+ /**
+ * A regular expression group that can be used to match Java identifier. Java identifier can not start with a digit,
+ * but can contain underscore and dollar signs. Java identifiers can contain ASCII or Unicode letters and digits.
+ */
+ public static final String JAVA_IDENTIFIER_REGEX = "([a-zA-Z_$\\p{Lu}\\p{Ll}]{1}"
+ + "[a-zA-Z_$0-9\\p{Lu}\\p{Ll}\\p{Nl}]*)";
+
public static String findClassName(final CharSequence source) {
- Pattern p = Pattern.compile(".*?class\\s+(\\w+).*", Pattern.DOTALL);
+ Pattern p = Pattern.compile(".*?class\\s+" + JAVA_IDENTIFIER_REGEX + ".*", Pattern.DOTALL);
Matcher matcher = p.matcher(source);
if (!matcher.matches()) {
- p = Pattern.compile(".*interface\\s+(\\w+).*", Pattern.DOTALL);
+ p = Pattern.compile(".*interface\\s+" + JAVA_IDENTIFIER_REGEX + ".*", Pattern.DOTALL);
matcher = p.matcher(source);
}
assertTrue(matcher.matches());
@@ -71,7 +79,7 @@ public class JavaProjectFixture {
String declaringType = findClassName(source);
List<String> names = newArrayList();
- Pattern p = Pattern.compile("(class|interface)\\s+(\\w+)", Pattern.DOTALL);
+ Pattern p = Pattern.compile("(class|interface)\\s+" + JAVA_IDENTIFIER_REGEX, Pattern.DOTALL);
Matcher matcher = p.matcher(source);
while (matcher.find()) {
final String name = matcher.group(2);
@@ -88,7 +96,7 @@ public class JavaProjectFixture {
List<String> names = newArrayList();
// new <name> ( ... ) {
- Pattern p = Pattern.compile("new\\s*?(\\w+)\\s*?\\([^)]*?\\)\\s*?\\{", Pattern.DOTALL);
+ Pattern p = Pattern.compile("new\\s*?" + JAVA_IDENTIFIER_REGEX + "\\s*?\\([^)]*?\\)\\s*?\\{", Pattern.DOTALL);
Matcher matcher = p.matcher(source);
while (matcher.find()) {
final String name = matcher.group(1);
@@ -99,6 +107,33 @@ public class JavaProjectFixture {
return names;
}
+ /**
+ * Finds the package name from the package declaration inside the source code.
+ *
+ * @param source
+ * the source code
+ * @return the package name or "" if no package declaration was found
+ */
+ public static String findPackageName(final CharSequence source) {
+ Pattern p = Pattern.compile(".*" // any characters at the beginning
+ + "package\\s+" // package declaration
+ + "(" // beginning of the package name group
+ + JAVA_IDENTIFIER_REGEX // the first part of the package
+ + "{1}" // must occur one time
+ + "([.]{1}" // the following parts of the package must begin with a dot
+ + JAVA_IDENTIFIER_REGEX // followed by a java identifier
+ + ")*" // the (.identifier) group can occur multiple times or not at all
+ + ")" // closing of the package name group
+ + "[;]+.*", // the following ; and the rest of the source code
+ Pattern.DOTALL);
+ Matcher matcher = p.matcher(source);
+ if (matcher.matches()) {
+ final String group = matcher.group(1);
+ return group;
+ }
+ return "";
+ }
+
private IJavaProject javaProject;
private ASTParser parser;
@@ -199,7 +234,8 @@ public class JavaProjectFixture {
return cu;
}
- public Tuple<ICompilationUnit, Set<Integer>> createFileAndParseWithMarkers(final CharSequence contentWithMarkers) throws CoreException {
+ public Tuple<ICompilationUnit, Set<Integer>> createFileAndParseWithMarkers(final CharSequence contentWithMarkers)
+ throws CoreException {
final Tuple<String, Set<Integer>> content = findMarkers(contentWithMarkers);
final String fileName = findClassName(content.getFirst()) + ".java";
diff --git a/tests/org.eclipse.recommenders.tests/src/org/eclipse/recommenders/tests/jdt/JavaProjectFixtureTest.java b/tests/org.eclipse.recommenders.tests/src/org/eclipse/recommenders/tests/jdt/JavaProjectFixtureTest.java
index 4777638..61503e1 100644
--- a/tests/org.eclipse.recommenders.tests/src/org/eclipse/recommenders/tests/jdt/JavaProjectFixtureTest.java
+++ b/tests/org.eclipse.recommenders.tests/src/org/eclipse/recommenders/tests/jdt/JavaProjectFixtureTest.java
@@ -1,18 +1,20 @@
/**
- * Copyright (c) 2010, 2011 Darmstadt University of Technology.
+ * Copyright (c) 2010, 2011, 2012 Darmstadt University of Technology.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
- *
+ *
* Contributors:
- * Sebastian Proksch - initial API and implementation
+ * Sebastian Proksch - initial API and implementation
+ * Kevin Munk - Extension for more tests for extracting package names
*/
package org.eclipse.recommenders.tests.jdt;
import static org.eclipse.recommenders.tests.jdt.JavaProjectFixture.findAnonymousClassNames;
import static org.eclipse.recommenders.tests.jdt.JavaProjectFixture.findClassName;
import static org.eclipse.recommenders.tests.jdt.JavaProjectFixture.findInnerClassNames;
+import static org.eclipse.recommenders.tests.jdt.JavaProjectFixture.findPackageName;
import static org.junit.Assert.assertEquals;
import java.util.List;
@@ -171,4 +173,120 @@ public class JavaProjectFixtureTest {
List<String> expecteds = Lists.newArrayList("Class1$1");
assertEquals(expecteds, actuals);
}
+
+ @Test
+ public void extractNoPackageName() {
+ String actual = findPackageName(code);
+ String expected = "";
+ assertEquals(expected, actual);
+ }
+
+ @Test
+ public void extractNoPackageNameFaultyPackage1() {
+ StringBuilder sb = new StringBuilder();
+ sb.append("package ");
+ sb.append("public class Class1 {");
+ sb.append("}");
+
+ String actual = findPackageName(sb);
+ String expected = "";
+ assertEquals(expected, actual);
+ }
+
+ @Test
+ public void extractNoPackageNameFaultyPackage2() {
+ StringBuilder sb = new StringBuilder();
+ sb.append("package test.");
+ sb.append("public class Class1 {");
+ sb.append("}");
+
+ String actual = findPackageName(sb);
+ String expected = "";
+ assertEquals(expected, actual);
+ }
+
+ @Test
+ public void extractNoPackageNameFaultyPackage3() {
+ StringBuilder sb = new StringBuilder();
+ sb.append("package test.blub.");
+ sb.append("public class Class1 {");
+ sb.append("}");
+
+ String actual = findPackageName(sb);
+ String expected = "";
+ assertEquals(expected, actual);
+ }
+
+ @Test
+ public void extractNoPackageNameFaultyPackage4() {
+ StringBuilder sb = new StringBuilder();
+ sb.append("package test.;");
+ sb.append("public class Class1 {");
+ sb.append("}");
+
+ String actual = findPackageName(sb);
+ String expected = "";
+ assertEquals(expected, actual);
+ }
+
+ @Test
+ public void extractNoPackageNameFaultyPackage5() {
+ StringBuilder sb = new StringBuilder();
+ sb.append("package test.blub.;");
+ sb.append("public class Class1 {");
+ sb.append("}");
+
+ String actual = findPackageName(sb);
+ String expected = "";
+ assertEquals(expected, actual);
+ }
+
+ @Test
+ public void extractPackageName() {
+ StringBuilder sb = new StringBuilder();
+ sb.append("package test;");
+ sb.append("public class Class1 {");
+ sb.append("}");
+
+ String actual = findPackageName(sb);
+ String expected = "test";
+ assertEquals(expected, actual);
+ }
+
+ @Test
+ public void extractPackageName2() {
+ StringBuilder sb = new StringBuilder();
+ sb.append("@SomeAnnotation");
+ sb.append("package test.blub.bla;");
+ sb.append("public class Class1 {");
+ sb.append("}");
+
+ String actual = findPackageName(sb);
+ String expected = "test.blub.bla";
+ assertEquals(expected, actual);
+ }
+
+ @Test
+ public void extractPackageName3() {
+ StringBuilder sb = new StringBuilder();
+ sb.append("package a.b.c.d.e.f;");
+ sb.append("public class Class1 {");
+ sb.append("}");
+
+ String actual = findPackageName(sb);
+ String expected = "a.b.c.d.e.f";
+ assertEquals(expected, actual);
+ }
+
+ @Test
+ public void extractPackageNameUnicode() {
+ StringBuilder sb = new StringBuilder();
+ sb.append("package ñ.b.c.d.ê.f;");
+ sb.append("public class Class1 {");
+ sb.append("}");
+
+ String actual = findPackageName(sb);
+ String expected = "ñ.b.c.d.ê.f";
+ assertEquals(expected, actual);
+ }
} \ No newline at end of file