예제 #1
0
 /** @return The parameters defined in this test tag and the tags above it. */
 public Map<String, String> getAllParameters() {
   Map<String, String> result = Maps.newHashMap();
   Map<String, String> parameters = m_xmlTest.getLocalParameters();
   result.putAll(parameters);
   result.putAll(m_parameters);
   return result;
 }
예제 #2
0
  private static Map<String, Object> findMembers(Object o, Predicate<Field> fieldPredicate)
      throws IllegalArgumentException, IllegalAccessException {
    Map<String, Object> result = Maps.newLinkedHashMap();
    Class<?> clazz = (o != null) ? o.getClass() : null;

    if (o instanceof Iterable) {
      int i = 0;
      for (Object member : (Iterable) o) {
        result.put("member" + (i++), member);
      }
    } else if (o instanceof Map) {
      int i = 0;
      Map<?, ?> m = (Map<?, ?>) o;
      for (Map.Entry<?, ?> entry : m.entrySet()) {
        result.put("member" + (i++), new Entry(entry.getKey(), entry.getValue()));
      }
    } else {
      for (Field field : FlagUtils.getAllFields(clazz, fieldPredicate)) {
        field.setAccessible(true);
        String fieldName = field.getName();
        Object fieldVal = field.get(o);
        result.put(fieldName, fieldVal);
      }
    }

    return result;
  }
예제 #3
0
  protected void generateFailureSuite(XmlSuite xmlSuite, ISuite suite, String outputDir) {
    XmlSuite failedSuite = (XmlSuite) xmlSuite.clone();
    failedSuite.setName("Failed suite [" + xmlSuite.getName() + "]");
    m_xmlSuite = failedSuite;

    Map<String, XmlTest> xmlTests = Maps.newHashMap();
    for (XmlTest xmlT : xmlSuite.getTests()) {
      xmlTests.put(xmlT.getName(), xmlT);
    }

    Map<String, ISuiteResult> results = suite.getResults();

    for (Map.Entry<String, ISuiteResult> entry : results.entrySet()) {
      ISuiteResult suiteResult = entry.getValue();
      ITestContext testContext = suiteResult.getTestContext();

      generateXmlTest(
          suite,
          xmlTests.get(testContext.getName()),
          testContext,
          testContext.getFailedTests().getAllResults(),
          testContext.getSkippedTests().getAllResults());
    }

    if (null != failedSuite.getTests() && failedSuite.getTests().size() > 0) {
      Utils.writeUtf8File(outputDir, TESTNG_FAILED_XML, failedSuite.toXml());
      Utils.writeUtf8File(suite.getOutputDirectory(), TESTNG_FAILED_XML, failedSuite.toXml());
    }
  }
예제 #4
0
 /** @return the parameters defined in this tag and the tags above it. */
 public Map<String, String> getAllParameters() {
   Map<String, String> result = Maps.newHashMap();
   if (m_xmlClass != null) {
     result.putAll(m_xmlClass.getAllParameters());
   }
   result.putAll(m_parameters);
   return result;
 }
예제 #5
0
  /**
   * @param methods The methods we want to represent
   * @param srcXmlTest
   * @return A list of XmlClass objects (each representing a <class> tag) based on the parameter
   *     methods
   */
  private List<XmlClass> createXmlClasses(List<ITestNGMethod> methods, XmlTest srcXmlTest) {
    List<XmlClass> result = Lists.newArrayList();
    Map<Class, Set<ITestNGMethod>> methodsMap = Maps.newHashMap();

    for (ITestNGMethod m : methods) {
      Object[] instances = m.getInstances();
      Class clazz =
          instances == null || instances.length == 0 || instances[0] == null
              ? m.getRealClass()
              : instances[0].getClass();
      Set<ITestNGMethod> methodList = methodsMap.get(clazz);
      if (null == methodList) {
        methodList = new HashSet<ITestNGMethod>();
        methodsMap.put(clazz, methodList);
      }
      methodList.add(m);
    }

    // Ideally, we should preserve each parameter in each class but putting them
    // all in the same bag for now
    Map<String, String> parameters = Maps.newHashMap();
    for (XmlClass c : srcXmlTest.getClasses()) {
      parameters.putAll(c.getLocalParameters());
    }

    int index = 0;
    for (Map.Entry<Class, Set<ITestNGMethod>> entry : methodsMap.entrySet()) {
      Class clazz = entry.getKey();
      Set<ITestNGMethod> methodList = entry.getValue();
      // @author Borojevic
      // Need to check all the methods, not just @Test ones.
      XmlClass xmlClass = new XmlClass(clazz.getName(), index++, false /* don't load classes */);
      List<XmlInclude> methodNames = Lists.newArrayList(methodList.size());
      int ind = 0;
      for (ITestNGMethod m : methodList) {
        methodNames.add(
            new XmlInclude(m.getMethod().getName(), m.getFailedInvocationNumbers(), ind++));
      }
      xmlClass.setIncludedMethods(methodNames);
      xmlClass.setParameters(parameters);
      result.add(xmlClass);
    }

    return result;
  }
예제 #6
0
  /**
   * Fix the methodsDependedUpon to make sure that @Configuration methods respect inheritance
   * (before methods are invoked in the order Base first and after methods are invoked in the order
   * Child first)
   *
   * @param methods the list of methods
   * @param before true if we are handling a before method (meaning, the methods need to be sorted
   *     base class first and subclass last). false otherwise (subclass methods first, base classes
   *     last).
   */
  public static void fixMethodInheritance(ITestNGMethod[] methods, boolean before) {
    // Map of classes -> List of methods that belong to this class or same hierarchy
    Map<Class, List<ITestNGMethod>> map = Maps.newHashMap();

    //
    // Put the list of methods in their hierarchy buckets
    //
    for (ITestNGMethod method : methods) {
      Class<? extends ITestNGMethod> methodClass = method.getRealClass();
      List<ITestNGMethod> l = findMethodListSuperClass(map, methodClass);
      if (null != l) {
        l.add(method);
      } else {
        Class subClass = findSubClass(map, methodClass);
        if (null != subClass) {
          l = map.get(subClass);
          l.add(method);
          map.remove(subClass);
          map.put(methodClass, l);
        } else {
          l = Lists.newArrayList();
          l.add(method);
          map.put(methodClass, l);
        }
      }
    }

    //
    // Each bucket that has a list bigger than one element gets sorted
    //
    for (List<ITestNGMethod> l : map.values()) {
      if (l.size() > 1) {
        // Sort them
        sortMethodsByInheritance(l, before);

        /*
         *  Set methodDependedUpon accordingly
         *  E.g. Base class can have multiple @BeforeClass methods. Need to ensure
         *  that @BeforeClass methods in derived class depend on all @BeforeClass methods
         *  of base class. Vice versa for @AfterXXX methods
         */
        for (int i = 0; i < l.size() - 1; i++) {
          ITestNGMethod m1 = l.get(i);
          for (int j = i + 1; j < l.size(); j++) {
            ITestNGMethod m2 = l.get(j);
            if (!equalsEffectiveClass(m1, m2) && !dependencyExists(m1, m2, methods)) {
              Utils.log("MethodInheritance", 4, m2 + " DEPENDS ON " + m1);
              m2.addMethodDependedUpon(MethodHelper.calculateMethodCanonicalName(m1));
            }
          }
        }
      }
    }
  }
예제 #7
0
파일: XmlTest.java 프로젝트: JIGD/testng
 /**
  * Returns a merge of the the test parameters and its parent suite parameters. Test parameters
  * have precedence over suite parameters.
  *
  * @return a merge of the the test parameters and its parent suite parameters.
  */
 public Map<String, String> getParameters() {
   Map<String, String> result = Maps.newHashMap();
   Map<String, String> parameters = getSuite().getParameters();
   for (Map.Entry<String, String> parameter : parameters.entrySet()) {
     result.put(parameter.getKey(), parameter.getValue());
   }
   for (String key : m_parameters.keySet()) {
     result.put(key, m_parameters.get(key));
   }
   return result;
 }
예제 #8
0
/**
 * This class maintains a pool of slaves (represented by sockets).
 *
 * @author cbeust
 */
public class SlavePool {
  private static SocketLinkedBlockingQueue m_hosts = new SocketLinkedBlockingQueue();
  private static Map<Socket, ConnectionInfo> m_connectionInfos = Maps.newHashMap();

  public void addSlaves(Socket[] slaves) throws IOException {
    for (Socket s : slaves) {
      addSlave(s);
    }
  }

  public void addSlave(Socket s) {
    if (s == null) {
      return;
    }
    ConnectionInfo ci = new ConnectionInfo();
    ci.setSocket(s);
    addSlave(s, ci);
  }

  private void addSlave(Socket s, ConnectionInfo ci) {
    m_hosts.add(s);
    m_connectionInfos.put(s, ci);
  }

  public ConnectionInfo getSlave() {
    ConnectionInfo result = null;
    Socket host = null;

    try {
      host = m_hosts.take();
      result = m_connectionInfos.get(host);
    } catch (InterruptedException handled) {
      handled.printStackTrace();
      Thread.currentThread().interrupt();
    }

    return result;
  }

  public void returnSlave(ConnectionInfo slave) throws IOException {
    m_hosts.add(slave.getSocket());
    //    ConnectionInfo ci = m_connectionInfos.remove(slave.socket);
    //    ci.oos.close();
    //    ci.ois.close();
    //    addSlave(slave.socket);
  }
}
예제 #9
0
/**
 * Simple implementation of IAttributes.
 *
 * @author [email protected] (Cedric Beust), March 16th, 2010
 */
public class Attributes implements IAttributes {
  private Map<String, Object> m_attributes = Maps.newHashMap();

  public Object getAttribute(String name) {
    return m_attributes.get(name);
  }

  public Set<String> getAttributeNames() {
    return m_attributes.keySet();
  }

  public void setAttribute(String name, Object value) {
    m_attributes.put(name, value);
  }

  public Object removeAttribute(String name) {
    return m_attributes.remove(name);
  }
}
예제 #10
0
  public static void logUnserializableChains(Object root)
      throws IllegalArgumentException, IllegalAccessException {
    final Map<List<Object>, Class<?>> unserializablePaths = Maps.newLinkedHashMap();

    Visitor visitor =
        new Visitor() {
          @Override
          public boolean visit(Object o, Iterable<Object> refChain) {
            try {
              Serializers.reconstitute(o);
              return true;
            } catch (Exception e) {
              // not serializable in some way: report
              ImmutableList<Object> refChainList = ImmutableList.copyOf(refChain);

              // First strip out any less specific paths
              for (Iterator<List<Object>> iter = unserializablePaths.keySet().iterator();
                  iter.hasNext(); ) {
                List<Object> existing = iter.next();
                if (refChainList.size() >= existing.size()
                    && refChainList.subList(0, existing.size()).equals(existing)) {
                  iter.remove();
                }
              }

              // Then add this list
              unserializablePaths.put(ImmutableList.copyOf(refChainList), o.getClass());
              return false;
            }
          }
        };
    deepVisitInternal(
        root, SERIALIZED_FIELD_PREDICATE, Lists.newArrayList(), new LinkedList<Object>(), visitor);

    StringBuilder msg = new StringBuilder("Not serializable: \n");
    for (Map.Entry<List<Object>, Class<?>> entry : unserializablePaths.entrySet()) {
      msg.append("\ttype=" + entry.getValue() + "; chain=" + entry.getKey() + "\n");
    }
    LOG.warn(msg.toString());
  }
예제 #11
0
파일: XmlTest.java 프로젝트: JIGD/testng
  /** Convenience method to cache the ordering numbers for methods. */
  public List<Integer> getInvocationNumbers(String method) {
    if (m_failedInvocationNumbers == null) {
      m_failedInvocationNumbers = Maps.newHashMap();
      for (XmlClass c : getXmlClasses()) {
        for (XmlInclude xi : c.getIncludedMethods()) {
          List<Integer> invocationNumbers = xi.getInvocationNumbers();
          if (invocationNumbers.size() > 0) {
            String methodName = c.getName() + "." + xi.getName();
            m_failedInvocationNumbers.put(methodName, invocationNumbers);
          }
        }
      }
    }

    List<Integer> result = m_failedInvocationNumbers.get(method);
    if (result == null) {
      // Don't use emptyList here since this list might end up receiving values if
      // the test run fails.
      return Lists.newArrayList();
    } else {
      return result;
    }
  }
예제 #12
0
  private void init() {
    IAnnotation a = AnnotationHelper.findConfiguration(m_annotationFinder, m_method.getMethod());
    IConfigurationAnnotation annotation = (IConfigurationAnnotation) a;
    if (a != null) {
      m_inheritGroupsFromTestClass = annotation.getInheritGroups();
      setEnabled(annotation.getEnabled());
      setDescription(annotation.getDescription());
    }

    if (annotation != null && annotation.isFakeConfiguration()) {
      if (annotation.getBeforeSuite()) {
        initGroups(IBeforeSuite.class);
      }
      if (annotation.getAfterSuite()) {
        initGroups(IAfterSuite.class);
      }
      if (annotation.getBeforeTest()) {
        initGroups(IBeforeTest.class);
      }
      if (annotation.getAfterTest()) {
        initGroups(IAfterTest.class);
      }
      if (annotation.getBeforeGroups().length != 0) {
        initGroups(IBeforeGroups.class);
      }
      if (annotation.getAfterGroups().length != 0) {
        initGroups(IAfterGroups.class);
      }
      if (annotation.getBeforeTestClass()) {
        initGroups(IBeforeClass.class);
      }
      if (annotation.getAfterTestClass()) {
        initGroups(IAfterClass.class);
      }
      if (annotation.getBeforeTestMethod()) {
        initGroups(IBeforeMethod.class);
      }
      if (annotation.getAfterTestMethod()) {
        initGroups(IAfterMethod.class);
      }
    } else {
      initGroups(IConfigurationAnnotation.class);
    }

    // If this configuration method has inherit-groups=true, add the groups
    // defined in the @Test class
    if (inheritGroupsFromTestClass()) {
      ITestAnnotation classAnnotation =
          (ITestAnnotation) m_annotationFinder.findAnnotation(m_methodClass, ITestAnnotation.class);
      if (classAnnotation != null) {
        String[] groups = classAnnotation.getGroups();
        Map<String, String> newGroups = Maps.newHashMap();
        for (String g : getGroups()) {
          newGroups.put(g, g);
        }
        if (groups != null) {
          for (String g : groups) {
            newGroups.put(g, g);
          }
          setGroups(newGroups.values().toArray(new String[newGroups.size()]));
        }
      }
    }

    if (annotation != null) {
      setTimeOut(annotation.getTimeOut());
    }
  }
예제 #13
0
/**
 * This class describes the tag <class> in testng.xml.
 *
 * @author <a href="mailto:[email protected]">Cedric Beust</a>
 */
public class XmlClass implements Serializable, Cloneable {

  private static final long serialVersionUID = 8885360896966149897L;
  private List<XmlInclude> m_includedMethods = Lists.newArrayList();
  private List<String> m_excludedMethods = Lists.newArrayList();
  private String m_name = null;
  private Class m_class = null;
  /** The index of this class in the <test> tag */
  private int m_index;
  /** True if the classes need to be loaded */
  private boolean m_loadClasses = true;

  private Map<String, String> m_parameters = Maps.newHashMap();
  private XmlTest m_xmlTest;

  public XmlClass() {
    init("", null, 0, false /* load classes */);
  }

  public XmlClass(String name) {
    init(name, null, 0);
  }

  public XmlClass(String name, boolean loadClasses) {
    init(name, null, 0, loadClasses);
  }

  public XmlClass(Class cls) {
    init(cls.getName(), cls, 0, true);
  }

  public XmlClass(Class cls, boolean loadClasses) {
    init(cls.getName(), cls, 0, loadClasses);
  }

  public XmlClass(String className, int index) {
    init(className, null, index, true /* load classes */);
  }

  public XmlClass(String className, int index, boolean loadClasses) {
    init(className, null, index, loadClasses);
  }

  private void init(String className, Class cls, int index) {
    init(className, cls, index, true /* load classes */);
  }

  private void init(String className, Class cls, int index, boolean resolveClass) {
    m_name = className;
    m_class = cls;
    m_index = index;

    if (null == m_class && resolveClass) {
      loadClass();
    }
  }

  private void loadClass() {
    m_class = ClassHelper.forName(m_name);

    if (null == m_class) {
      throw new TestNGException("Cannot find class in classpath: " + m_name);
    }
  }

  /** @return Returns the className. */
  public Class getSupportClass() {
    if (m_class == null) loadClass();
    return m_class;
  }

  /** @param className The className to set. */
  public void setClass(Class className) {
    m_class = className;
  }

  /** @return Returns the excludedMethods. */
  public List<String> getExcludedMethods() {
    return m_excludedMethods;
  }

  /** @param excludedMethods The excludedMethods to set. */
  public void setExcludedMethods(List<String> excludedMethods) {
    m_excludedMethods = excludedMethods;
  }

  /** @return Returns the includedMethods. */
  public List<XmlInclude> getIncludedMethods() {
    return m_includedMethods;
  }

  /** @param includedMethods The includedMethods to set. */
  public void setIncludedMethods(List<XmlInclude> includedMethods) {
    m_includedMethods = includedMethods;
  }

  /** @return Returns the name. */
  public String getName() {
    return m_name;
  }

  /** @param name The name to set. */
  public void setName(String name) {
    m_name = name;
  }

  /** @return true if the classes need to be loaded. */
  public boolean loadClasses() {
    return m_loadClasses;
  }

  @Override
  public String toString() {
    return Objects.toStringHelper(getClass()).add("class", m_name).toString();
  }

  public String toXml(String indent) {
    XMLStringBuffer xsb = new XMLStringBuffer(indent);
    Properties prop = new Properties();
    prop.setProperty("name", getName());

    boolean hasMethods = !m_includedMethods.isEmpty() || !m_excludedMethods.isEmpty();
    boolean hasParameters = !m_parameters.isEmpty();
    if (hasParameters || hasMethods) {
      xsb.push("class", prop);
      XmlUtils.dumpParameters(xsb, m_parameters);

      if (hasMethods) {
        xsb.push("methods");

        for (XmlInclude m : getIncludedMethods()) {
          xsb.getStringBuffer().append(m.toXml(indent + "    "));
        }

        for (String m : getExcludedMethods()) {
          Properties p = new Properties();
          p.setProperty("name", m);
          xsb.addEmptyElement("exclude", p);
        }

        xsb.pop("methods");
      }

      xsb.pop("class");
    } else {
      xsb.addEmptyElement("class", prop);
    }

    return xsb.toXML();
  }

  public static String listToString(List<Integer> invocationNumbers) {
    StringBuilder result = new StringBuilder();
    int i = 0;
    for (Integer n : invocationNumbers) {
      if (i++ > 0) {
        result.append(" ");
      }
      result.append(n);
    }
    return result.toString();
  }

  /** Clone an XmlClass by copying all its components. */
  @Override
  public Object clone() {
    XmlClass result = new XmlClass(getName(), getIndex(), loadClasses());
    result.setExcludedMethods(getExcludedMethods());
    result.setIncludedMethods(getIncludedMethods());

    return result;
  }

  /**
   * Note that this attribute does not come from the XML file, it's calculated internally and
   * represents the order in which this class was found in its &lt;test&gt; tag. It's used to
   * calculate the ordering of the classes when preserve-order is true.
   */
  public int getIndex() {
    return m_index;
  }

  public void setIndex(int index) {
    m_index = index;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((m_class == null) ? 0 : m_class.hashCode());
    result = prime * result + (m_loadClasses ? 1 : 0);
    result = prime * result + ((m_excludedMethods == null) ? 0 : m_excludedMethods.hashCode());
    result = prime * result + ((m_includedMethods == null) ? 0 : m_includedMethods.hashCode());
    result = prime * result + m_index;
    result = prime * result + ((m_name == null) ? 0 : m_name.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null) return XmlSuite.f();
    if (getClass() != obj.getClass()) return XmlSuite.f();
    XmlClass other = (XmlClass) obj;
    if (other.m_loadClasses != m_loadClasses
        || !m_excludedMethods.equals(other.m_excludedMethods)) {
      return XmlSuite.f();
    }
    if (m_includedMethods == null) {
      if (other.m_includedMethods != null) return XmlSuite.f();
    } else if (!m_includedMethods.equals(other.m_includedMethods)) return XmlSuite.f();
    //    if (m_index != other.m_index)
    //      return XmlSuite.f();
    if (m_name == null) {
      if (other.m_name != null) return XmlSuite.f();
    } else if (!m_name.equals(other.m_name)) return XmlSuite.f();

    return true;
  }

  public void setParameters(Map<String, String> parameters) {
    m_parameters.clear();
    m_parameters.putAll(parameters);
  }

  /** @return The parameters defined in this test tag and the tags above it. */
  public Map<String, String> getAllParameters() {
    Map<String, String> result = Maps.newHashMap();
    Map<String, String> parameters = m_xmlTest.getLocalParameters();
    result.putAll(parameters);
    result.putAll(m_parameters);
    return result;
  }

  /**
   * @return The parameters defined in this tag, and only this test tag. To retrieve the inherited
   *     parameters as well, call {@code getAllParameters()}.
   */
  public Map<String, String> getLocalParameters() {
    return m_parameters;
  }

  /** @deprecated Use {@code getLocalParameters()} or {@code getAllParameters()} */
  @Deprecated
  public Map<String, String> getParameters() {
    return getAllParameters();
  }

  public void setXmlTest(XmlTest test) {
    m_xmlTest = test;
  }
}
예제 #14
0
파일: XmlTest.java 프로젝트: JIGD/testng
/**
 * This class describes the tag &lt;test&gt; in testng.xml.
 *
 * @author <a href = "mailto:cedric&#64;beust.com">Cedric Beust</a>
 * @author <a href = 'mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
 */
public class XmlTest implements Serializable, Cloneable {
  /** */
  private static final long serialVersionUID = 6533504325942417606L;

  public static int DEFAULT_TIMEOUT_MS = Integer.MAX_VALUE;

  private XmlSuite m_suite;
  private String m_name;
  private Integer m_verbose = XmlSuite.DEFAULT_VERBOSE;
  private Boolean m_isJUnit = XmlSuite.DEFAULT_JUNIT;
  private int m_threadCount = -1;

  private List<XmlClass> m_xmlClasses = Lists.newArrayList();

  private List<String> m_includedGroups = Lists.newArrayList();
  private List<String> m_excludedGroups = Lists.newArrayList();

  private Map<String, List<String>> m_metaGroups = Maps.newHashMap();
  private Map<String, String> m_parameters = Maps.newHashMap();
  private String m_parallel;

  private List<XmlMethodSelector> m_methodSelectors = Lists.newArrayList();
  // test level packages
  private List<XmlPackage> m_xmlPackages = Lists.newArrayList();

  private String m_timeOut;
  private Boolean m_skipFailedInvocationCounts = XmlSuite.DEFAULT_SKIP_FAILED_INVOCATION_COUNTS;
  private Map<String, List<Integer>> m_failedInvocationNumbers = null; // lazily initialized

  private String m_preserveOrder = XmlSuite.DEFAULT_PRESERVE_ORDER;

  private int m_index;

  private Boolean m_groupByInstances;

  /**
   * Constructs a <code>XmlTest</code> and adds it to suite's list of tests.
   *
   * @param suite the parent suite.
   * @param index the index of this test tag in testng.xml
   */
  public XmlTest(XmlSuite suite, int index) {
    init(suite, index);
  }

  public XmlTest(XmlSuite suite) {
    init(suite, 0);
  }

  private void init(XmlSuite suite, int index) {
    m_suite = suite;
    m_suite.getTests().add(this);
    m_index = index;
    // no two tests in the same suite should have the same name.
    // so, make the default test name unique
    m_name = TestNG.DEFAULT_COMMAND_LINE_TEST_NAME + " " + UUID.randomUUID().toString();
  }

  // For YAML
  public XmlTest() {}

  public void setXmlPackages(List<XmlPackage> packages) {
    m_xmlPackages = packages;
  }

  public List<XmlPackage> getXmlPackages() {
    return m_xmlPackages;
  }

  // For YAML
  public List<XmlPackage> getPackages() {
    return getPackages();
  }

  // For YAML
  public void setPackages(List<XmlPackage> p) {
    setXmlPackages(p);
  }

  public List<XmlMethodSelector> getMethodSelectors() {
    return m_methodSelectors;
  }

  public void setMethodSelectors(List<XmlMethodSelector> methodSelectors) {
    m_methodSelectors = methodSelectors;
  }

  /**
   * Returns the suite this test is part of.
   *
   * @return the suite this test is part of.
   */
  public XmlSuite getSuite() {
    return m_suite;
  }

  /**
   * @return the includedGroups. Note: do not modify the returned value, use {@link
   *     #addIncludedGroup(String)}.
   */
  public List<String> getIncludedGroups() {
    List<String> result = new ArrayList(m_includedGroups);
    result.addAll(m_suite.getIncludedGroups());
    return result;
  }

  /**
   * Sets the XML Classes.
   *
   * @param classes The classes to set.
   * @deprecated use setXmlClasses
   */
  @Deprecated
  public void setClassNames(List<XmlClass> classes) {
    m_xmlClasses = classes;
  }

  /** @return Returns the classes. */
  public List<XmlClass> getXmlClasses() {
    return m_xmlClasses;
  }

  // For YAML
  public List<XmlClass> getClasses() {
    return getXmlClasses();
  }

  // For YAML
  public void setClasses(List<XmlClass> c) {
    setXmlClasses(c);
  }

  /**
   * Sets the XML Classes.
   *
   * @param classes The classes to set.
   */
  public void setXmlClasses(List<XmlClass> classes) {
    m_xmlClasses = classes;
  }

  /** @return Returns the name. */
  public String getName() {
    return m_name;
  }

  /** @param name The name to set. */
  public void setName(String name) {
    m_name = name;
  }

  /** @param v */
  public void setVerbose(int v) {
    m_verbose = Integer.valueOf(v);
  }

  public int getThreadCount() {
    return m_threadCount > 0 ? m_threadCount : getSuite().getThreadCount();
  }

  public void setThreadCount(int threadCount) {
    m_threadCount = threadCount;
  }

  /** @param g */
  public void setIncludedGroups(List<String> g) {
    m_includedGroups = g;
  }

  /** @param g The excludedGrousps to set. */
  public void setExcludedGroups(List<String> g) {
    m_excludedGroups = g;
  }

  /**
   * @return Returns the excludedGroups. Note: do not modify the returned value, use {@link
   *     #addExcludedGroup(String)}.
   */
  public List<String> getExcludedGroups() {
    List<String> result = new ArrayList(m_excludedGroups);
    result.addAll(m_suite.getExcludedGroups());
    return result;
  }

  public void addIncludedGroup(String g) {
    m_includedGroups.add(g);
  }

  public void addExcludedGroup(String g) {
    m_excludedGroups.add(g);
  }

  /** @return Returns the verbose. */
  public int getVerbose() {
    Integer result = m_verbose;
    if (null == result || XmlSuite.DEFAULT_VERBOSE.equals(m_verbose)) {
      result = m_suite.getVerbose();
    }

    if (null != result) {
      return result.intValue();
    } else {
      return 1;
    }
  }

  public boolean getGroupByInstances() {
    Boolean result = m_groupByInstances;
    if (result == null || XmlSuite.DEFAULT_GROUP_BY_INSTANCES.equals(m_groupByInstances)) {
      result = m_suite.getGroupByInstances();
    }
    if (result != null) {
      return result.booleanValue();
    } else {
      return XmlSuite.DEFAULT_GROUP_BY_INSTANCES;
    }
  }

  public void setGroupByInstances(boolean f) {
    m_groupByInstances = f;
  }

  /** @return Returns the isJUnit. */
  public boolean isJUnit() {
    Boolean result = m_isJUnit;
    if (null == result || XmlSuite.DEFAULT_JUNIT.equals(result)) {
      result = m_suite.isJUnit();
    }

    return result;
  }

  /** @param isJUnit The isJUnit to set. */
  public void setJUnit(boolean isJUnit) {
    m_isJUnit = isJUnit;
  }

  // For YAML
  public void setJunit(boolean isJUnit) {
    setJUnit(isJUnit);
  }

  public void setSkipFailedInvocationCounts(boolean skip) {
    m_skipFailedInvocationCounts = skip;
  }

  /** @return Returns the isJUnit. */
  public boolean skipFailedInvocationCounts() {
    Boolean result = m_skipFailedInvocationCounts;
    if (null == result) {
      result = m_suite.skipFailedInvocationCounts();
    }

    return result;
  }

  public void addMetaGroup(String name, List<String> metaGroup) {
    m_metaGroups.put(name, metaGroup);
  }

  // For YAML
  public void setMetaGroups(Map<String, List<String>> metaGroups) {
    m_metaGroups = metaGroups;
  }

  /** @return Returns the metaGroups. */
  public Map<String, List<String>> getMetaGroups() {
    return m_metaGroups;
  }

  /** @param parameters */
  public void setParameters(Map<String, String> parameters) {
    m_parameters = parameters;
  }

  public void addParameter(String key, String value) {
    m_parameters.put(key, value);
  }

  public String getParameter(String name) {
    String result = m_parameters.get(name);
    if (null == result) {
      result = m_suite.getParameter(name);
    }

    return result;
  }

  /**
   * Returns a merge of the the test parameters and its parent suite parameters. Test parameters
   * have precedence over suite parameters.
   *
   * @return a merge of the the test parameters and its parent suite parameters.
   */
  public Map<String, String> getParameters() {
    Map<String, String> result = Maps.newHashMap();
    Map<String, String> parameters = getSuite().getParameters();
    for (Map.Entry<String, String> parameter : parameters.entrySet()) {
      result.put(parameter.getKey(), parameter.getValue());
    }
    for (String key : m_parameters.keySet()) {
      result.put(key, m_parameters.get(key));
    }
    return result;
  }

  /** @return the parameters defined on this <test> tag only */
  public Map<String, String> getTestParameters() {
    return m_parameters;
  }

  public void setParallel(String parallel) {
    m_parallel = parallel;
  }

  public String getParallel() {
    String result = null;
    if (null != m_parallel || XmlSuite.DEFAULT_PARALLEL.equals(m_parallel)) {
      result = m_parallel;
    } else {
      result = m_suite.getParallel();
    }

    return result;
  }

  public String getTimeOut() {
    String result = null;
    if (null != m_timeOut) {
      result = m_timeOut;
    } else {
      result = m_suite.getTimeOut();
    }

    return result;
  }

  public long getTimeOut(long def) {
    long result = def;
    if (getTimeOut() != null) {
      result = new Long(getTimeOut()).longValue();
    }

    return result;
  }

  public void setTimeOut(long timeOut) {
    m_timeOut = Long.toString(timeOut);
  }

  public void setExpression(String expression) {
    setBeanShellExpression(expression);
  }

  public void setBeanShellExpression(String expression) {
    List<XmlMethodSelector> selectors = getMethodSelectors();
    if (selectors.size() > 0) {
      selectors.get(0).setExpression(expression);
    } else if (expression != null) {
      XmlMethodSelector xms = new XmlMethodSelector();
      xms.setExpression(expression);
      xms.setLanguage("BeanShell");
      getMethodSelectors().add(xms);
    }
  }

  public String getExpression() {
    List<XmlMethodSelector> selectors = getMethodSelectors();
    if (selectors.size() > 0) {
      return selectors.get(0).getExpression();
    } else {
      return null;
    }
  }

  public String toXml(String indent) {
    XMLStringBuffer xsb = new XMLStringBuffer(indent);
    Properties p = new Properties();
    p.setProperty("name", getName());
    if (m_isJUnit != null) {
      XmlUtils.setProperty(p, "junit", m_isJUnit.toString(), XmlSuite.DEFAULT_JUNIT.toString());
    }
    if (m_parallel != null) {
      XmlUtils.setProperty(p, "parallel", m_parallel, XmlSuite.DEFAULT_PARALLEL);
    }
    if (m_verbose != null) {
      XmlUtils.setProperty(p, "verbose", m_verbose.toString(), XmlSuite.DEFAULT_VERBOSE.toString());
    }
    if (null != m_timeOut) {
      p.setProperty("time-out", m_timeOut.toString());
    }
    if (m_preserveOrder != null) {
      p.setProperty("preserve-order", m_preserveOrder.toString());
    }
    if (m_threadCount != -1) {
      p.setProperty("thread-count", Integer.toString(m_threadCount));
    }

    xsb.push("test", p);

    if (null != getMethodSelectors() && !getMethodSelectors().isEmpty()) {
      xsb.push("method-selectors");
      for (XmlMethodSelector selector : getMethodSelectors()) {
        xsb.getStringBuffer().append(selector.toXml(indent + "    "));
      }

      xsb.pop("method-selectors");
    }

    // parameters
    if (!m_parameters.isEmpty()) {
      for (Map.Entry<String, String> para : m_parameters.entrySet()) {
        Properties paramProps = new Properties();
        paramProps.setProperty("name", para.getKey());
        paramProps.setProperty("value", para.getValue());
        xsb.addEmptyElement("parameter", paramProps); // BUGFIX: TESTNG-27
      }
    }

    // groups
    if (!m_metaGroups.isEmpty() || !m_includedGroups.isEmpty() || !m_excludedGroups.isEmpty()) {
      xsb.push("groups");

      // define
      for (String metaGroupName : m_metaGroups.keySet()) {
        Properties metaGroupProp = new Properties();
        metaGroupProp.setProperty("name", metaGroupName);

        xsb.push("define", metaGroupProp);

        for (String groupName : m_metaGroups.get(metaGroupName)) {
          Properties includeProps = new Properties();
          includeProps.setProperty("name", groupName);

          xsb.addEmptyElement("include", includeProps);
        }

        xsb.pop("define");
      }

      if (!m_includedGroups.isEmpty() || !m_excludedGroups.isEmpty()) {
        xsb.push("run");

        for (String includeGroupName : m_includedGroups) {
          Properties includeProps = new Properties();
          includeProps.setProperty("name", includeGroupName);

          xsb.addEmptyElement("include", includeProps);
        }

        for (String excludeGroupName : m_excludedGroups) {
          Properties excludeProps = new Properties();
          excludeProps.setProperty("name", excludeGroupName);

          xsb.addEmptyElement("exclude", excludeProps);
        }

        xsb.pop("run");
      }

      xsb.pop("groups");
    }

    if (null != m_xmlPackages && !m_xmlPackages.isEmpty()) {
      xsb.push("packages");

      for (XmlPackage pack : m_xmlPackages) {
        xsb.getStringBuffer().append(pack.toXml("      "));
      }

      xsb.pop("packages");
    }

    // classes
    if (null != getXmlClasses() && !getXmlClasses().isEmpty()) {
      xsb.push("classes");
      for (XmlClass cls : getXmlClasses()) {
        xsb.getStringBuffer().append(cls.toXml(indent + "    "));
      }
      xsb.pop("classes");
    }

    xsb.pop("test");

    return xsb.toXML();
  }

  @Override
  public String toString() {
    //    return toXml("");
    StringBuffer result =
        new StringBuffer("[Test: \"" + m_name + "\"").append(" verbose:" + m_verbose);

    result.append("[parameters:");
    for (String k : m_parameters.keySet()) {
      String v = m_parameters.get(k);
      result.append(k + "=>" + v);
    }

    result.append("]");
    result.append("[metagroups:");
    for (String g : m_metaGroups.keySet()) {
      List<String> mg = m_metaGroups.get(g);
      result.append(g).append("=");
      for (String n : mg) {
        result.append(n).append(",");
      }
    }
    result.append("] ");

    result.append("[included: ");
    for (String g : m_includedGroups) {
      result.append(g).append(" ");
    }
    result.append("]");

    result.append("[excluded: ");
    for (String g : m_excludedGroups) {
      result.append(g).append("");
    }
    result.append("] ");

    result.append(" classes:");
    for (XmlClass cl : m_xmlClasses) {
      result.append(cl).append(" ");
    }

    result.append("] ");

    return result.toString();
  }

  static void ppp(String s) {
    System.out.println("[XmlTest] " + s);
  }

  /**
   * Clone the <TT>source</TT> <CODE>XmlTest</CODE> by including: - test attributes - groups
   * definitions - parameters
   *
   * <p>The &lt;classes&gt; sub element is ignored for the moment.
   *
   * @return a clone of the current XmlTest
   */
  @Override
  public Object clone() {
    XmlTest result = new XmlTest(getSuite());

    result.setName(getName());
    result.setIncludedGroups(getIncludedGroups());
    result.setExcludedGroups(getExcludedGroups());
    result.setJUnit(isJUnit());
    result.setParallel(getParallel());
    result.setVerbose(getVerbose());
    result.setParameters(getParameters());
    result.setXmlPackages(getXmlPackages());

    Map<String, List<String>> metagroups = getMetaGroups();
    for (Map.Entry<String, List<String>> group : metagroups.entrySet()) {
      result.addMetaGroup(group.getKey(), group.getValue());
    }

    return result;
  }

  /** Convenience method to cache the ordering numbers for methods. */
  public List<Integer> getInvocationNumbers(String method) {
    if (m_failedInvocationNumbers == null) {
      m_failedInvocationNumbers = Maps.newHashMap();
      for (XmlClass c : getXmlClasses()) {
        for (XmlInclude xi : c.getIncludedMethods()) {
          List<Integer> invocationNumbers = xi.getInvocationNumbers();
          if (invocationNumbers.size() > 0) {
            String methodName = c.getName() + "." + xi.getName();
            m_failedInvocationNumbers.put(methodName, invocationNumbers);
          }
        }
      }
    }

    List<Integer> result = m_failedInvocationNumbers.get(method);
    if (result == null) {
      // Don't use emptyList here since this list might end up receiving values if
      // the test run fails.
      return Lists.newArrayList();
    } else {
      return result;
    }
  }

  public void setPreserveOrder(String preserveOrder) {
    m_preserveOrder = preserveOrder;
  }

  public String getPreserveOrder() {
    String result = m_preserveOrder;
    if (result == null || XmlSuite.DEFAULT_PRESERVE_ORDER.equals(m_verbose)) {
      result = m_suite.getPreserveOrder();
    }

    return result;
  }

  public void setSuite(XmlSuite result) {
    m_suite = result;
  }

  /**
   * Note that this attribute does not come from the XML file, it's calculated internally and
   * represents the order in which this test tag was found in its &lt;suite&gt; tag. It's used to
   * calculate the ordering of the tests when preserve-test-order is true.
   */
  public int getIndex() {
    return m_index;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((m_excludedGroups == null) ? 0 : m_excludedGroups.hashCode());
    result =
        prime * result
            + ((m_failedInvocationNumbers == null) ? 0 : m_failedInvocationNumbers.hashCode());
    result = prime * result + ((m_includedGroups == null) ? 0 : m_includedGroups.hashCode());
    result = prime * result + ((m_isJUnit == null) ? 0 : m_isJUnit.hashCode());
    result = prime * result + ((m_metaGroups == null) ? 0 : m_metaGroups.hashCode());
    result = prime * result + ((m_methodSelectors == null) ? 0 : m_methodSelectors.hashCode());
    result = prime * result + ((m_name == null) ? 0 : m_name.hashCode());
    result = prime * result + ((m_parallel == null) ? 0 : m_parallel.hashCode());
    result = prime * result + ((m_parameters == null) ? 0 : m_parameters.hashCode());
    result = prime * result + ((m_preserveOrder == null) ? 0 : m_preserveOrder.hashCode());
    result =
        prime * result
            + ((m_skipFailedInvocationCounts == null)
                ? 0
                : m_skipFailedInvocationCounts.hashCode());
    result = prime * result + m_threadCount;
    result = prime * result + ((m_timeOut == null) ? 0 : m_timeOut.hashCode());
    result = prime * result + ((m_verbose == null) ? 0 : m_verbose.hashCode());
    result = prime * result + ((m_xmlClasses == null) ? 0 : m_xmlClasses.hashCode());
    result = prime * result + ((m_xmlPackages == null) ? 0 : m_xmlPackages.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null) return XmlSuite.f();
    if (getClass() != obj.getClass()) return XmlSuite.f();
    XmlTest other = (XmlTest) obj;
    if (m_excludedGroups == null) {
      if (other.m_excludedGroups != null) return XmlSuite.f();
    } else if (!m_excludedGroups.equals(other.m_excludedGroups)) return XmlSuite.f();
    //    if (m_expression == null) {
    //      if (other.m_expression != null)
    //        return XmlSuite.f();
    //    } else if (!m_expression.equals(other.m_expression))
    //      return XmlSuite.f();
    if (m_failedInvocationNumbers == null) {
      if (other.m_failedInvocationNumbers != null) return XmlSuite.f();
    } else if (!m_failedInvocationNumbers.equals(other.m_failedInvocationNumbers))
      return XmlSuite.f();
    if (m_includedGroups == null) {
      if (other.m_includedGroups != null) return XmlSuite.f();
    } else if (!m_includedGroups.equals(other.m_includedGroups)) return XmlSuite.f();
    if (m_isJUnit == null) {
      if (other.m_isJUnit != null && !other.m_isJUnit.equals(XmlSuite.DEFAULT_JUNIT))
        return XmlSuite.f();
    } else if (!m_isJUnit.equals(other.m_isJUnit)) return XmlSuite.f();
    if (m_metaGroups == null) {
      if (other.m_metaGroups != null) return XmlSuite.f();
    } else if (!m_metaGroups.equals(other.m_metaGroups)) return XmlSuite.f();
    if (m_methodSelectors == null) {
      if (other.m_methodSelectors != null) return XmlSuite.f();
    } else if (!m_methodSelectors.equals(other.m_methodSelectors)) return XmlSuite.f();
    if (m_name == null) {
      if (other.m_name != null) return XmlSuite.f();
    } else if (!m_name.equals(other.m_name)) return XmlSuite.f();
    if (m_parallel == null) {
      if (other.m_parallel != null) return XmlSuite.f();
    } else if (!m_parallel.equals(other.m_parallel)) return XmlSuite.f();
    if (m_parameters == null) {
      if (other.m_parameters != null) return XmlSuite.f();
    } else if (!m_parameters.equals(other.m_parameters)) return XmlSuite.f();
    if (m_preserveOrder == null) {
      if (other.m_preserveOrder != null) return XmlSuite.f();
    } else if (!m_preserveOrder.equals(other.m_preserveOrder)) return XmlSuite.f();
    if (m_skipFailedInvocationCounts == null) {
      if (other.m_skipFailedInvocationCounts != null) return XmlSuite.f();
    } else if (!m_skipFailedInvocationCounts.equals(other.m_skipFailedInvocationCounts))
      return XmlSuite.f();
    if (m_threadCount != other.m_threadCount) return XmlSuite.f();
    if (m_timeOut == null) {
      if (other.m_timeOut != null) return XmlSuite.f();
    } else if (!m_timeOut.equals(other.m_timeOut)) return XmlSuite.f();
    if (m_verbose == null) {
      if (other.m_verbose != null) return XmlSuite.f();
    } else if (!m_verbose.equals(other.m_verbose)) return XmlSuite.f();
    if (m_xmlClasses == null) {
      if (other.m_xmlClasses != null) return XmlSuite.f();
    } else if (!m_xmlClasses.equals(other.m_xmlClasses)) return XmlSuite.f();
    if (m_xmlPackages == null) {
      if (other.m_xmlPackages != null) return XmlSuite.f();
    } else if (!m_xmlPackages.equals(other.m_xmlPackages)) return XmlSuite.f();

    return true;
  }
}
예제 #15
0
public class XmlInclude implements Serializable {
  private static final long serialVersionUID = 1L;

  private String m_name;
  private List<Integer> m_invocationNumbers = Lists.newArrayList();
  private int m_index;
  private String m_description;
  private Map<String, String> m_parameters = Maps.newHashMap();

  private XmlClass m_xmlClass;

  public XmlInclude() {}

  public XmlInclude(String n) {
    this(n, Collections.<Integer>emptyList(), 0);
  }

  public XmlInclude(String n, int index) {
    this(n, Collections.<Integer>emptyList(), index);
  }

  public XmlInclude(String n, List<Integer> list, int index) {
    m_name = n;
    m_invocationNumbers = list;
    m_index = index;
  }

  public void setDescription(String description) {
    m_description = description;
  }

  public void setParameters(Map<String, String> parameters) {
    m_parameters.clear();
    m_parameters.putAll(parameters);
  }

  public String getDescription() {
    return m_description;
  }

  public String getName() {
    return m_name;
  }

  public List<Integer> getInvocationNumbers() {
    return m_invocationNumbers;
  }

  public int getIndex() {
    return m_index;
  }

  public String toXml(String indent) {
    XMLStringBuffer xsb = new XMLStringBuffer(indent);
    Properties p = new Properties();
    p.setProperty("name", getName());
    List<Integer> invocationNumbers = getInvocationNumbers();
    if (invocationNumbers != null && invocationNumbers.size() > 0) {
      p.setProperty("invocation-numbers", XmlClass.listToString(invocationNumbers).toString());
    }

    if (!m_parameters.isEmpty()) {
      xsb.push("include", p);
      XmlUtils.dumpParameters(xsb, m_parameters);
      xsb.pop("include");
    } else {
      xsb.addEmptyElement("include", p);
    }

    return xsb.toXML();
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + m_index;
    result = prime * result + ((m_invocationNumbers == null) ? 0 : m_invocationNumbers.hashCode());
    result = prime * result + (m_parameters == null ? 0 : m_parameters.hashCode());
    result = prime * result + ((m_name == null) ? 0 : m_name.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) return true;
    if (obj == null) return XmlSuite.f();
    if (getClass() != obj.getClass()) return XmlSuite.f();
    XmlInclude other = (XmlInclude) obj;
    // if (m_index != other.m_index)
    // return XmlSuite.f();
    if (m_invocationNumbers == null) {
      if (other.m_invocationNumbers != null) return XmlSuite.f();
    } else if (!m_invocationNumbers.equals(other.m_invocationNumbers)) return XmlSuite.f();
    if (m_name == null) {
      if (other.m_name != null) return XmlSuite.f();
    } else if (!m_name.equals(other.m_name)) return XmlSuite.f();
    if (m_parameters == null) {
      if (other.m_parameters != null) {
        return XmlSuite.f();
      }
    } else if (!m_parameters.equals(other.m_parameters)) {
      return XmlSuite.f();
    }
    return true;
  }

  public void addParameter(String name, String value) {
    m_parameters.put(name, value);
  }

  /** @deprecated Use {@code getLocalParameters()} or {@code getAllParameters()} */
  @Deprecated
  public Map<String, String> getParameters() {
    return getAllParameters();
  }

  /**
   * @return the parameters defined in this test tag, and only this test tag. To retrieve the
   *     inherited parameters as well, call {@code getAllParameters()}.
   */
  public Map<String, String> getLocalParameters() {
    return m_parameters;
  }

  /** @return the parameters defined in this tag and the tags above it. */
  public Map<String, String> getAllParameters() {
    Map<String, String> result = Maps.newHashMap();
    if (m_xmlClass != null) {
      result.putAll(m_xmlClass.getAllParameters());
    }
    result.putAll(m_parameters);
    return result;
  }

  public void setXmlClass(XmlClass xmlClass) {
    m_xmlClass = xmlClass;
  }
}