Skip to content
Browse files

Refactoring, extracted GroupInfo and deprecated DependencyVisitor.

  • Loading branch information...
1 parent 17a5104 commit ccfea65ca9fbcda33c82c4df50c567da627c5a7d @cbeust cbeust committed Nov 26, 2011
View
79 src/main/org/testng/eclipse/util/DependencyVisitor.java
@@ -0,0 +1,79 @@
+package org.testng.eclipse.util;
+
+import java.util.List;
+
+import com.google.common.collect.Lists;
+
+import org.eclipse.jdt.core.dom.ASTVisitor;
+import org.eclipse.jdt.core.dom.ArrayInitializer;
+import org.eclipse.jdt.core.dom.Expression;
+import org.eclipse.jdt.core.dom.MemberValuePair;
+import org.eclipse.jdt.core.dom.NormalAnnotation;
+import org.eclipse.jdt.core.dom.StringLiteral;
+
+/**
+ * An <code>ASTVisitor</code> that extracts the <tt>dependsOnMethods</tt> and
+ * <tt>dependsOnGroups</tt>. Note: this class is no longer used, finding these
+ * dependencies is now performed in GroupInfo. Using an ASTVisitor such as this
+ * one takes longer since it reparses all the source files, while GroupInfo
+ * simply walks all the types of the IJavaProject.
+ */
+public class DependencyVisitor extends ASTVisitor {
+ private static final String ANNOTATION_PACKAGE = "org.testng.annotations.";
+ private static final String TEST_ANNOTATION = "Test";
+ private static final String TEST_ANNOTATION_FQN = ANNOTATION_PACKAGE + TEST_ANNOTATION;
+ private static final String DEPENDS_ON_METHODS= "dependsOnMethods";
+ private static final String DEPENDS_ON_GROUPS= "dependsOnGroups";
+
+ List<String> m_dependsOnMethods = Lists.newArrayList();
+ List<String> m_dependsOnGroups= Lists.newArrayList();
+
+ @Override
+ public boolean visit(NormalAnnotation annotation) {
+ String typeName = annotation.getTypeName().getFullyQualifiedName();
+ if(!TEST_ANNOTATION.equals(typeName) && !TEST_ANNOTATION_FQN.equals(typeName)) {
+ return false;
+ }
+
+ List values= annotation.values();
+
+ if(null != values && !values.isEmpty()) {
+ for(int i= 0; i < values.size(); i++) {
+ MemberValuePair pair= (MemberValuePair) values.get(i);
+ String name = pair.getName().toString();
+ if(DEPENDS_ON_METHODS.equals(name)) {
+ m_dependsOnMethods.addAll(extractValues(pair.getValue()));
+ }
+ else if(DEPENDS_ON_GROUPS.equals(name)) {
+ m_dependsOnGroups.addAll(extractValues(pair.getValue()));
+ }
+ }
+ }
+
+ return false;
+ }
+
+ public List<String> getDependsOnGroups() {
+ return m_dependsOnGroups;
+ }
+
+ public List<String> getDependsOnMethods() {
+ return m_dependsOnMethods;
+ }
+
+ private List<String> extractValues(Expression paramAttr) {
+ List<String> values = Lists.newArrayList();
+ if(paramAttr instanceof ArrayInitializer) {
+ List<StringLiteral> literals= ((ArrayInitializer) paramAttr).expressions();
+// List paramNames= new ArrayList(literals.size());
+ for(int j= 0; j < literals.size(); j++) {
+ StringLiteral str= literals.get(j);
+ values.add(str.getLiteralValue());
+ }
+ } else if (paramAttr instanceof StringLiteral) {
+ values.add(((StringLiteral) paramAttr).getLiteralValue());
+ }
+
+ return values;
+ }
+}
View
108 src/main/org/testng/eclipse/util/GroupInfo.java
@@ -0,0 +1,108 @@
+package org.testng.eclipse.util;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.Set;
+
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Sets;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jdt.core.IAnnotation;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.IMemberValuePair;
+import org.eclipse.jdt.core.IMethod;
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jface.dialogs.ProgressMonitorDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.PlatformUI;
+import org.testng.eclipse.launch.components.Filters;
+
+/**
+ * A class that represents all the information about groups in the current project
+ * (which types depend on which groups, which types define groups, which methods
+ * define on which groups and which methods define groups.
+ */
+public class GroupInfo {
+ Multimap<String, IType> typesByGroups = ArrayListMultimap.create();
+ Multimap<IType, String> groupDependenciesByTypes = ArrayListMultimap.create();
+ Multimap<String, IMethod> methodsByGroups = ArrayListMultimap.create();
+ Multimap<IMethod, String> groupDependenciesByMethods = ArrayListMultimap.create();
+
+ public static GroupInfo createGroupInfo(final IJavaProject javaProject) {
+ final GroupInfo result = new GroupInfo();
+
+ final IRunnableWithProgress runnable = new IRunnableWithProgress() {
+
+ public void run(IProgressMonitor monitor)
+ throws InvocationTargetException, InterruptedException {
+ final Set<IType> allTypes = Sets.newHashSet();
+ try {
+ monitor.beginTask("Launching", 2000);
+ monitor.subTask("Calculating the groups to run");
+ TestSearchEngine.collectTypes(javaProject, monitor, allTypes, Filters.SINGLE_TEST,
+ "Parsing tests");
+ monitor.subTask("Collecting group information");
+ monitor.worked(1);
+ for (IType type : allTypes) {
+ for (IMethod method : type.getMethods()) {
+ for (IAnnotation annotation : method.getAnnotations()) {
+ monitor.worked(1);
+ IMemberValuePair[] pairs = annotation.getMemberValuePairs();
+ if ("Test".equals(annotation.getElementName()) && pairs.length > 0) {
+ for (IMemberValuePair pair : pairs) {
+
+ if ("groups".equals(pair.getMemberName())) {
+ Object groups = pair.getValue();
+ if (groups.getClass().isArray()) {
+ for (Object o : (Object[]) groups) {
+ result.typesByGroups.put(o.toString(), type);
+ result.methodsByGroups.put(o.toString(), method);
+ }
+ } else {
+ result.typesByGroups.put(groups.toString(), type);
+ result.methodsByGroups.put(groups.toString(), method);
+ }
+ } else if ("dependsOnGroups".equals(pair.getMemberName())) {
+ Object dependencies = pair.getValue();
+ if (dependencies.getClass().isArray()) {
+ for (Object o : (Object[]) dependencies) {
+ result.groupDependenciesByTypes.put(type, o.toString());
+ result.groupDependenciesByMethods.put(method, o.toString());
+ }
+ } else {
+ result.groupDependenciesByTypes.put(type, dependencies.toString());
+ result.groupDependenciesByMethods.put(method,dependencies.toString());
+ }
+
+ }
+ }
+ }
+ }
+ }
+ }
+ } catch (CoreException e) {
+ e.printStackTrace();
+ }
+ }
+
+ };
+
+ Shell shell = PlatformUI.getWorkbench().getDisplay().getActiveShell();
+ ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);
+
+ try {
+ dialog.run(true /* fork */, true /* cancelable */, runnable);
+ } catch (InvocationTargetException e) {
+ e.printStackTrace();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+
+ return result;
+ }
+
+}
+
View
79 src/main/org/testng/eclipse/util/JDTUtil.java
@@ -31,12 +31,6 @@
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
-import org.eclipse.jdt.core.dom.ASTVisitor;
-import org.eclipse.jdt.core.dom.ArrayInitializer;
-import org.eclipse.jdt.core.dom.Expression;
-import org.eclipse.jdt.core.dom.MemberValuePair;
-import org.eclipse.jdt.core.dom.NormalAnnotation;
-import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPage;
@@ -429,7 +423,7 @@ private static IMethod fuzzyFindMethodInTypeHierarchy(IType type, String methodN
Set<String> parsedMethods = Sets.newHashSet();
MethodDefinition md = new MethodDefinition(method);
parsedMethods.add(method.getElementName());
-
+
List<MethodDefinition> results = Lists.newArrayList();
results.add(md);
results.addAll(solveDependencies(md, parsedMethods));
@@ -447,11 +441,11 @@ private static IMethod fuzzyFindMethodInTypeHierarchy(IType type, String methodN
DependencyVisitor dv = parse(methodDef.getMethod());
List<MethodDefinition> results = Lists.newArrayList();
- List<String> dependesonmethods= dv.getDependsOnMethods();
+ List<String> dependsOnMethods = dv.getDependsOnMethods();
- if(!dependesonmethods.isEmpty()) {
- for(int i= 0; i < dependesonmethods.size(); i++) {
- String methodName= dependesonmethods.get(i);
+ if(!dependsOnMethods.isEmpty()) {
+ for(int i= 0; i < dependsOnMethods.size(); i++) {
+ String methodName= dependsOnMethods.get(i);
if(!parsedMethods.contains(methodName)) {
IMethod meth= solveMethod(methodDef.getMethod().getDeclaringType(), methodName);
if(null != meth) {
@@ -551,67 +545,4 @@ public String getKey() {
return m_dependsongroups;
}
}
-
- /**
- * An <code>ASTVisitor</code> that extracts the <tt>dependsOnMethods</tt> and <tt>dependsOnGroups</tt>.
- */
- private static class DependencyVisitor extends ASTVisitor {
- private static final String ANNOTATION_PACKAGE = "org.testng.annotations.";
- private static final String TEST_ANNOTATION = "Test";
- private static final String TEST_ANNOTATION_FQN = ANNOTATION_PACKAGE + TEST_ANNOTATION;
- private static final String DEPENDS_ON_METHODS= "dependsOnMethods";
- private static final String DEPENDS_ON_GROUPS= "dependsOnGroups";
-
- List<String> m_dependsOnMethods = Lists.newArrayList();
- List<String> m_dependsOnGroups= Lists.newArrayList();
-
- @Override
- public boolean visit(NormalAnnotation annotation) {
- String typeName = annotation.getTypeName().getFullyQualifiedName();
- if(!TEST_ANNOTATION.equals(typeName) && !TEST_ANNOTATION_FQN.equals(typeName)) {
- return false;
- }
-
- List values= annotation.values();
-
- if(null != values && !values.isEmpty()) {
- for(int i= 0; i < values.size(); i++) {
- MemberValuePair pair= (MemberValuePair) values.get(i);
- String name = pair.getName().toString();
- if(DEPENDS_ON_METHODS.equals(name)) {
- m_dependsOnMethods.addAll(extractValues(pair.getValue()));
- }
- else if(DEPENDS_ON_GROUPS.equals(name)) {
- m_dependsOnGroups.addAll(extractValues(pair.getValue()));
- }
- }
- }
-
- return false;
- }
-
- public List<String> getDependsOnGroups() {
- return m_dependsOnGroups;
- }
-
- public List<String> getDependsOnMethods() {
- return m_dependsOnMethods;
- }
-
- private List<String> extractValues(Expression paramAttr) {
- List<String> values = Lists.newArrayList();
- if(paramAttr instanceof ArrayInitializer) {
- List<StringLiteral> literals= ((ArrayInitializer) paramAttr).expressions();
-// List paramNames= new ArrayList(literals.size());
- for(int j= 0; j < literals.size(); j++) {
- StringLiteral str= literals.get(j);
- values.add(str.getLiteralValue());
- }
- } else if (paramAttr instanceof StringLiteral) {
- values.add(((StringLiteral) paramAttr).getLiteralValue());
- }
-
- return values;
- }
- }
}
View
11 src/main/org/testng/eclipse/util/LaunchUtil.java
@@ -52,7 +52,6 @@
import org.testng.eclipse.ui.RunInfo;
import org.testng.eclipse.ui.util.ConfigurationHelper;
import org.testng.eclipse.util.JDTUtil.MethodDefinition;
-import org.testng.eclipse.util.TestSearchEngine.GroupInfo;
import org.testng.eclipse.util.param.ParameterSolver;
import org.testng.reporters.FailedReporter;
@@ -260,7 +259,7 @@ public static void launchMethodConfiguration(IJavaProject javaProject,
try {
if (methodDependsOnGroups(iMethod)) {
- GroupInfo groupInfo = TestSearchEngine.findGroupInfo(javaProject);
+ GroupInfo groupInfo = GroupInfo.createGroupInfo(javaProject);
methods.addAll(findMethodTransitiveClosure(iMethod, groupInfo));
}
} catch (JavaModelException e) {
@@ -393,8 +392,8 @@ public static boolean useStringProtocol(ILaunchConfiguration configuration) {
return jvmArgs.indexOf("-Dtestng.eclipse.stringprotocol") >= 0;
}
- private static void launchTypeBasedConfiguration(IJavaProject ijp, String confName, IType[] types,
- String mode) {
+ private static void launchTypeBasedConfiguration(IJavaProject javaProject, String confName,
+ IType[] types, String mode) {
Multimap<String, String> classMethods = ArrayListMultimap.create();
List<String> typeNames = Lists.newArrayList();
Set<IType> allTypes = Sets.newHashSet();
@@ -405,7 +404,7 @@ private static void launchTypeBasedConfiguration(IJavaProject ijp, String confNa
// If we depend on groups, need to add all the necessary types
Object[] groupDependencies = findGroupDependencies(types);
if (groupDependencies.length > 0) {
- GroupInfo groupInfo = TestSearchEngine.findGroupInfo(ijp);
+ GroupInfo groupInfo = GroupInfo.createGroupInfo(javaProject);
Set<IType> closure = findTypeTransitiveClosure(types, groupInfo);
allTypes.addAll(closure);
Set<IMethod> methods = findMethodTransitiveClosure(types, groupInfo);
@@ -423,7 +422,7 @@ private static void launchTypeBasedConfiguration(IJavaProject ijp, String confNa
}
ILaunchConfigurationWorkingCopy workingCopy =
- createLaunchConfiguration(ijp.getProject(), confName, null);
+ createLaunchConfiguration(javaProject.getProject(), confName, null);
workingCopy.setAttribute(TestNGLaunchConfigurationConstants.TYPE,
LaunchType.CLASS.ordinal());
View
85 src/main/org/testng/eclipse/util/TestSearchEngine.java
@@ -7,8 +7,6 @@
import java.util.Map;
import java.util.Set;
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import org.eclipse.core.internal.resources.File;
@@ -18,12 +16,10 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
-import org.eclipse.jdt.core.IAnnotation;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
-import org.eclipse.jdt.core.IMemberValuePair;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageDeclaration;
import org.eclipse.jdt.core.IPackageFragment;
@@ -33,10 +29,8 @@
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
-import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.operation.IRunnableWithProgress;
-import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.testng.eclipse.TestNGPlugin;
import org.testng.eclipse.launch.components.Filters;
@@ -388,85 +382,6 @@ public static void findFile(IContainer container, String fileName, Set<String> o
}
}
- public static class GroupInfo {
- Multimap<String, IType> typesByGroups = ArrayListMultimap.create();
- Multimap<IType, String> groupDependenciesByTypes = ArrayListMultimap.create();
- Multimap<String, IMethod> methodsByGroups = ArrayListMultimap.create();
- Multimap<IMethod, String> groupDependenciesByMethods = ArrayListMultimap.create();
- }
-
- public static GroupInfo findGroupInfo(final IJavaProject ijp) {
- final GroupInfo result = new GroupInfo();
-
- final IRunnableWithProgress runnable = new IRunnableWithProgress() {
-
- public void run(IProgressMonitor monitor)
- throws InvocationTargetException, InterruptedException {
- final Set<IType> allTypes = Sets.newHashSet();
- try {
- monitor.beginTask("Launching", 2000);
- monitor.subTask("Calculating the groups to run");
- collectTypes(ijp, monitor, allTypes, Filters.SINGLE_TEST, "Parsing tests");
- monitor.subTask("Collecting group information");
- monitor.worked(1);
- for (IType type : allTypes) {
- for (IMethod method : type.getMethods()) {
- for (IAnnotation annotation : method.getAnnotations()) {
- monitor.worked(1);
- IMemberValuePair[] pairs = annotation.getMemberValuePairs();
- if ("Test".equals(annotation.getElementName()) && pairs.length > 0) {
- for (IMemberValuePair pair : pairs) {
-
- if ("groups".equals(pair.getMemberName())) {
- Object groups = pair.getValue();
- if (groups.getClass().isArray()) {
- for (Object o : (Object[]) groups) {
- result.typesByGroups.put(o.toString(), type);
- result.methodsByGroups.put(o.toString(), method);
- }
- } else {
- result.typesByGroups.put(groups.toString(), type);
- result.methodsByGroups.put(groups.toString(), method);
- }
- } else if ("dependsOnGroups".equals(pair.getMemberName())) {
- Object dependencies = pair.getValue();
- if (dependencies.getClass().isArray()) {
- for (Object o : (Object[]) dependencies) {
- result.groupDependenciesByTypes.put(type, o.toString());
- result.groupDependenciesByMethods.put(method, o.toString());
- }
- } else {
- result.groupDependenciesByTypes.put(type, dependencies.toString());
- result.groupDependenciesByMethods.put(method,dependencies.toString());
- }
-
- }
- }
- }
- }
- }
- }
- } catch (CoreException e) {
- e.printStackTrace();
- }
- }
-
- };
-
- Shell shell = PlatformUI.getWorkbench().getDisplay().getActiveShell();
- ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);
-
- try {
- dialog.run(true /* fork */, true /* cancelable */, runnable);
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
-
- return result;
- }
-
/**
* Collect all the types under the parameter element, which is expected to be either
* an IJavaProject, an IPackageFragmentRoot or a IPackageFragment.

0 comments on commit ccfea65

Please sign in to comment.
Something went wrong with that request. Please try again.