sm testng: run parameterized test with concrete parameter (IDEA-57906)
authorAnna Kozlova <anna.kozlova@jetbrains.com>
Wed, 15 Apr 2015 10:53:42 +0000 (12:53 +0200)
committerAnna Kozlova <anna.kozlova@jetbrains.com>
Wed, 15 Apr 2015 11:11:30 +0000 (13:11 +0200)
java/execution/impl/src/com/intellij/execution/JavaTestFrameworkRunnableState.java
plugins/junit/src/com/intellij/execution/junit/TestObject.java
plugins/testng/src/com/theoryinpractice/testng/configuration/TestNGConfigurationProducer.java
plugins/testng/src/com/theoryinpractice/testng/configuration/TestNGInClassConfigurationProducer.java
plugins/testng_rt/src/org/testng/IDEARemoteTestNG.java
plugins/testng_rt/src/org/testng/IDEATestNGRemoteListener.java
plugins/testng_rt/src/org/testng/RemoteTestNGStarter.java

index bc75c4f1359d21c03d3c46befdb9afc155c5bfd2..f98778128f5d7282a21dddd009a3ef6a7938bbca 100644 (file)
@@ -45,6 +45,7 @@ import com.intellij.openapi.util.Disposer;
 import com.intellij.openapi.util.Getter;
 import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.util.registry.Registry;
+import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.util.text.StringUtilRt;
 import com.intellij.openapi.vfs.CharsetToolkit;
 import com.intellij.psi.JavaPsiFacade;
@@ -195,6 +196,11 @@ public abstract class JavaTestFrameworkRunnableState<T extends ModuleBasedConfig
     JavaSdkUtil.addRtJar(javaParameters.getClassPath());
 
     configureClasspath(javaParameters);
+
+    if (!StringUtil.isEmptyOrSpaces(parameters)) {
+      javaParameters.getProgramParametersList().add("@name" + parameters);
+    }
+
     return javaParameters;
   }
 
index 7a5f8b10f45a12621e26ac210783f7e1a9fcddd4..8dab606da0638459d9343f6080566e62ac343d8e 100644 (file)
@@ -150,11 +150,6 @@ public abstract class TestObject extends JavaTestFrameworkRunnableState<JUnitCon
 
     javaParameters.getClassPath().add(PathUtil.getJarPathForClass(JUnitStarter.class));
 
-    String parameters = getConfiguration().getProgramParameters();
-    if (!StringUtil.isEmptyOrSpaces(parameters)) {
-      javaParameters.getProgramParametersList().add("@name" + parameters);
-    }
-
     final StringBuilder buf = new StringBuilder();
     collectListeners(javaParameters, buf, IDEAJUnitListener.EP_NAME, "\n");
     if (buf.length() > 0) {
index ce777ae383bb1cc03c0671105c06ce24194aa58f..950138652b22b9ec4ce939f9af0e63b1c47ad426 100644 (file)
  */
 package com.theoryinpractice.testng.configuration;
 
-import com.intellij.execution.*;
+import com.intellij.execution.JavaExecutionUtil;
+import com.intellij.execution.Location;
+import com.intellij.execution.RunManager;
+import com.intellij.execution.RunnerAndConfigurationSettings;
 import com.intellij.execution.actions.ConfigurationContext;
 import com.intellij.execution.actions.RunConfigurationProducer;
 import com.intellij.execution.configurations.RunConfiguration;
-import com.intellij.execution.impl.RunManagerImpl;
-import com.intellij.execution.junit.JUnitUtil;
 import com.intellij.execution.junit.JavaRunConfigurationProducerBase;
-import com.intellij.execution.junit.JavaRuntimeConfigurationProducerBase;
+import com.intellij.execution.junit2.PsiMemberParameterizedLocation;
 import com.intellij.openapi.module.Module;
 import com.intellij.openapi.util.Comparing;
-import com.intellij.psi.*;
+import com.intellij.psi.PsiElement;
 import com.theoryinpractice.testng.model.TestData;
-import org.jetbrains.annotations.NotNull;
-
-import java.util.List;
+import org.testng.IDEATestNGRemoteListener;
 
 public abstract class TestNGConfigurationProducer extends JavaRunConfigurationProducerBase<TestNGConfiguration> implements Cloneable {
 
@@ -48,13 +47,17 @@ public abstract class TestNGConfigurationProducer extends JavaRunConfigurationPr
       return false;
     }
     final RunConfiguration predefinedConfiguration = context.getOriginalConfiguration(TestNGConfigurationType.getInstance());
-    Location location = JavaExecutionUtil.stepIntoSingleClass(context.getLocation());
+    final Location contextLocation = context.getLocation();
+    Location location = JavaExecutionUtil.stepIntoSingleClass(contextLocation);
     final PsiElement element = location.getPsiElement();
     RunnerAndConfigurationSettings template = RunManager.getInstance(location.getProject()).getConfigurationTemplate(getConfigurationFactory());
     final Module predefinedModule = ((TestNGConfiguration)template.getConfiguration()).getConfigurationModule().getModule();
     final String vmParameters =
       predefinedConfiguration instanceof TestNGConfiguration ? ((TestNGConfiguration)predefinedConfiguration).getVMParameters() : null;
     if (vmParameters != null && !Comparing.strEqual(vmParameters, testNGConfiguration.getVMParameters())) return false;
+    String paramSetName = contextLocation instanceof PsiMemberParameterizedLocation
+                          ? getInvocationNumber(((PsiMemberParameterizedLocation)contextLocation).getParamSetName()) : null;
+    if (paramSetName != null && !Comparing.strEqual(paramSetName, testNGConfiguration.getProgramParameters())) return false;
     TestData testobject = testNGConfiguration.getPersistantData();
     if (testobject != null) {
       if (testobject.isConfiguredByElement(element)) {
@@ -65,4 +68,15 @@ public abstract class TestNGConfigurationProducer extends JavaRunConfigurationPr
     }
     return false;
   }
+  
+  protected static String getInvocationNumber(String str) {
+    final int indexOf = str.indexOf(IDEATestNGRemoteListener.INVOCATION_NUMBER);
+    if (indexOf > 0) {
+      final int lastIdx = str.indexOf(")", indexOf);
+      if (lastIdx > 0) {
+        return str.substring(indexOf + IDEATestNGRemoteListener.INVOCATION_NUMBER.length(), lastIdx);
+      }
+    }
+    return null;
+  }
 }
\ No newline at end of file
index fdc1387f3c5785a806ed4ae2ecd56ac2176a67ab..497967e352cfb5a4a7fbfc2477f3f6f0645ce490 100644 (file)
  */
 package com.theoryinpractice.testng.configuration;
 
+import com.intellij.execution.Location;
 import com.intellij.execution.PsiLocation;
 import com.intellij.execution.RunnerAndConfigurationSettings;
 import com.intellij.execution.actions.ConfigurationContext;
 import com.intellij.execution.actions.ConfigurationFromContext;
 import com.intellij.execution.actions.RunConfigurationProducer;
 import com.intellij.execution.junit.InheritorChooser;
+import com.intellij.execution.junit2.PsiMemberParameterizedLocation;
 import com.intellij.execution.junit2.info.MethodLocation;
 import com.intellij.openapi.module.Module;
 import com.intellij.openapi.project.Project;
@@ -100,6 +102,14 @@ public class TestNGInClassConfigurationProducer extends TestNGConfigurationProdu
       return false;
     }
 
+    final Location contextLocation = context.getLocation();
+    if (contextLocation instanceof PsiMemberParameterizedLocation) {
+      final String paramSetName = getInvocationNumber(((PsiMemberParameterizedLocation)contextLocation).getParamSetName());
+      if (paramSetName != null) {
+        configuration.setProgramParameters(paramSetName);
+      }
+    }
+
     PsiClass psiClass = null;
     PsiElement element = context.getPsiLocation();
     while (element != null) {
index f0f8fd96c975d5d54841143edb13488d9a7d1a91..cb55ecda63aaff83e2a0a9e944caedd38a2cb435 100644 (file)
@@ -19,14 +19,21 @@ package org.testng;
 import jetbrains.buildServer.messages.serviceMessages.ServiceMessage;
 import org.testng.collections.Lists;
 import org.testng.xml.XmlClass;
+import org.testng.xml.XmlInclude;
 import org.testng.xml.XmlSuite;
 import org.testng.xml.XmlTest;
 
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 
 public class IDEARemoteTestNG extends TestNG {
 
+  private final String myParam;
+  public IDEARemoteTestNG(String param) {
+    myParam = param;
+  }
+
   private static void calculateAllSuites(List<XmlSuite> suites, List<XmlSuite> outSuites) {
     for (XmlSuite s : suites) {
       outSuites.add(s);
@@ -50,6 +57,12 @@ public class IDEARemoteTestNG extends TestNG {
           for (XmlTest test : tests) {
             for (XmlClass aClass : test.getXmlClasses()) {
               System.out.println("##teamcity[suiteTreeStarted name=\'" + aClass.getName() + "\' locationHint=\'java:suite://" + aClass.getName() +  "\']");
+              if (myParam != null) {
+                for (XmlInclude include : aClass.getIncludedMethods()) {
+                  aClass.setIncludedMethods(Arrays.asList(new XmlInclude(include.getName(), Arrays.asList(Integer.parseInt(myParam)), 0)));
+                }
+              }
+
               System.out.println("##teamcity[suiteTreeEnded name=\'" + aClass.getName() + "\']");
             }
             testCount += test.getClasses().size();
@@ -60,8 +73,8 @@ public class IDEARemoteTestNG extends TestNG {
         final HashMap<String, String> map = new HashMap<String, String>();
         map.put("count", String.valueOf(testCount));
         System.out.println(ServiceMessage.asString("testCount", map));
-        addListener((ISuiteListener) new IDEATestNGRemoteListener());
-        addListener((ITestListener)  new IDEATestNGRemoteListener());
+        addListener((ISuiteListener) new IDEATestNGRemoteListener(myParam));
+        addListener((ITestListener)  new IDEATestNGRemoteListener(myParam));
         super.run();
       }
       else {
index f8413d1c7943e5d8827051d2d47ec33369f0ce90..8aa1176d245ae8e5b4e63d0f6521e799e000f20e 100644 (file)
@@ -15,7 +15,15 @@ import java.util.Map;
  */
 public class IDEATestNGRemoteListener implements ISuiteListener, IResultListener{
 
+  public static final String INVOCATION_NUMBER = "invocation number: ";
+  private final String myParam;
   private String myCurrentClassName;
+  private String myMethodName;
+  private int    myInvocationCount = 0;
+
+  public IDEATestNGRemoteListener(String param) {
+    myParam = param;
+  }
 
   public void onConfigurationSuccess(ITestResult itr) {
     //won't be called
@@ -46,17 +54,31 @@ public class IDEATestNGRemoteListener implements ISuiteListener, IResultListener
       }
       System.out.println("##teamcity[testSuiteStarted name =\'" + className + "\']");
       myCurrentClassName = className;
+      myInvocationCount = 0;
     }
-    String methodName = getMethodName(result);
+    String methodName = getMethodName(result, false);
     System.out.println("##teamcity[testStarted name=\'" +
                        methodName + "\' locationHint=\'java:test://" + className + "." + methodName + "\']");
   }
 
-  private static String getMethodName(ITestResult result) {
+  private String getMethodName(ITestResult result) {
+    return getMethodName(result, true);
+  }
+
+  private String getMethodName(ITestResult result, boolean changeCount) {
     String methodName = result.getMethod().getMethodName();
     final Object[] parameters = result.getParameters();
+    if (changeCount) {
+      if (!methodName.equals(myMethodName)) {
+        myInvocationCount = 0;
+        myMethodName = methodName;
+      }
+    }
     if (parameters.length > 0) {
-      methodName += "[" + parameters[0].toString() + "]";
+      methodName += "[" + parameters[0].toString() + (myParam == null ? (" (" + INVOCATION_NUMBER + myInvocationCount + ")") : "") + "]";
+      if (changeCount) {
+        myInvocationCount++;
+      }
     }
     return methodName;
   }
@@ -77,13 +99,14 @@ public class IDEATestNGRemoteListener implements ISuiteListener, IResultListener
     final Throwable ex = result.getThrowable();
     final String trace = getTrace(ex);
     final Map<String, String> attrs = new HashMap<String, String>();
-    attrs.put("name", getMethodName(result));
+    final String methodName = getMethodName(result);
+    attrs.put("name", methodName);
     final String failureMessage = ex.getMessage();
     attrs.put("message", failureMessage != null ? failureMessage : "");
     attrs.put("details", trace);
     attrs.put("error", "true");
     System.out.println(ServiceMessage.asString(ServiceMessageTypes.TEST_FAILED, attrs));
-    System.out.println("##teamcity[testFinished name=\'" + getMethodName(result) + "\']");
+    System.out.println("##teamcity[testFinished name=\'" + methodName + "\']");
   }
 
   public void onTestSkipped(ITestResult result) {
index 3aa287ec66a1706251299c9619c8c9d2fe3c2a78..8068d099ee9900e1df9a04a178adf099a47dc41d 100644 (file)
@@ -38,10 +38,14 @@ public class RemoteTestNGStarter {
   private static final String SOCKET = "-socket";
   public static void main(String[] args) throws Exception {
     int i = 0;
+    String param = null;
     Vector resultArgs = new Vector();
     for (; i < args.length; i++) {
       String arg = args[i];
-      if (arg.startsWith(SOCKET)) {
+      if (arg.startsWith("@name")) {
+        param = arg.substring(5);
+        continue;
+      } else if (arg.startsWith(SOCKET)) {
         final int port = Integer.parseInt(arg.substring(SOCKET.length()));
         try {
           final Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), port);  //start collecting tests
@@ -95,7 +99,7 @@ public class RemoteTestNGStarter {
     }
 
     if (SM_RUNNER) {
-      final IDEARemoteTestNG testNG = new IDEARemoteTestNG();
+      final IDEARemoteTestNG testNG = new IDEARemoteTestNG(param);
       CommandLineArgs cla = new CommandLineArgs();
       RemoteArgs ra = new RemoteArgs();
       new JCommander(Arrays.asList(cla, ra), (String[])resultArgs.toArray(new String[resultArgs.size()]));