Ejemplo n.º 1
0
  /**
   * Invoke the @AfterClass methods if not done already
   *
   * @param testClass
   * @param mi
   */
  protected void invokeAfterClassMethods(ITestClass testClass, IMethodInstance mi) {
    // if no BeforeClass than return immediately
    // used for parallel case when BeforeClass were already invoked
    if ((null == m_classMethodMap) || (null == m_classMethodMap.getInvokedAfterClassMethods())) {
      return;
    }
    ITestNGMethod[] afterClassMethods = testClass.getAfterClassMethods();

    if (null == afterClassMethods || afterClassMethods.length == 0) {
      return;
    }

    //
    // Invoke after class methods if this test method is the last one
    //
    List<Object> invokeInstances = Lists.newArrayList();
    ITestNGMethod tm = mi.getMethod();
    if (m_classMethodMap.removeAndCheckIfLast(tm, mi.getInstances()[0])) {
      Map<ITestClass, Set<Object>> invokedAfterClassMethods =
          m_classMethodMap.getInvokedAfterClassMethods();
      synchronized (invokedAfterClassMethods) {
        Set<Object> instances = invokedAfterClassMethods.get(testClass);
        if (null == instances) {
          instances = new HashSet<Object>();
          invokedAfterClassMethods.put(testClass, instances);
        }
        for (Object inst : mi.getInstances()) {
          if (!instances.contains(inst)) {
            invokeInstances.add(inst);
          }
        }
      }

      for (Object inst : invokeInstances) {
        m_invoker.invokeConfigurations(
            testClass,
            afterClassMethods,
            m_suite,
            m_parameters,
            null, /* no parameter values */
            inst);
      }
    }
  }
Ejemplo n.º 2
0
  /**
   * Invoke the @BeforeClass methods if not done already
   *
   * @param testClass
   * @param mi
   */
  protected void invokeBeforeClassMethods(ITestClass testClass, IMethodInstance mi) {
    // if no BeforeClass than return immediately
    // used for parallel case when BeforeClass were already invoked
    if ((null == m_classMethodMap) || (null == m_classMethodMap.getInvokedBeforeClassMethods())) {
      return;
    }
    ITestNGMethod[] classMethods = testClass.getBeforeClassMethods();
    if (null == classMethods || classMethods.length == 0) {
      return;
    }

    // the whole invocation must be synchronized as other threads must
    // get a full initialized test object (not the same for @After)
    Map<ITestClass, Set<Object>> invokedBeforeClassMethods =
        m_classMethodMap.getInvokedBeforeClassMethods();
    //    System.out.println("SYNCHRONIZING ON " + testClass
    //        + " thread:" + Thread.currentThread().getId()
    //        + " invokedMap:" + invokedBeforeClassMethods.hashCode() + " "
    //        + invokedBeforeClassMethods);
    synchronized (testClass) {
      Set<Object> instances = invokedBeforeClassMethods.get(testClass);
      if (null == instances) {
        instances = new HashSet<Object>();
        invokedBeforeClassMethods.put(testClass, instances);
      }
      for (Object instance : mi.getInstances()) {
        if (!instances.contains(instance)) {
          instances.add(instance);
          m_invoker.invokeConfigurations(
              testClass,
              testClass.getBeforeClassMethods(),
              m_suite,
              m_parameters,
              null, /* no parameter values */
              instance);
        }
      }
    }
  }