@Override
  protected Statement methodBlock(FrameworkMethod frameworkMethod) {
    Thread currentThread = Thread.currentThread();

    ClassLoader contextClassLoader = currentThread.getContextClassLoader();

    PortalClassLoaderUtil.setClassLoader(contextClassLoader);

    TestClass testClass = getTestClass();

    List<FrameworkMethod> beforeFrameworkMethods = testClass.getAnnotatedMethods(Before.class);

    List<FrameworkMethod> afterFrameworkMethods = testClass.getAnnotatedMethods(After.class);

    List<String> arguments = createArguments(frameworkMethod);

    Class<?> clazz = testClass.getJavaClass();

    return new RunInNewJVMStatment(
        _classPath,
        arguments,
        clazz,
        beforeFrameworkMethods,
        frameworkMethod,
        afterFrameworkMethods);
  }
Esempio n. 2
0
 /**
  * 判断是否需要执行MilouDbUnitTestExecutionListener
  *
  * @param testClass
  */
 private void switchMilouDBUnitTestExecutionListener(TestClass testClass) {
   List<FrameworkMethod> DBSituations = testClass.getAnnotatedMethods(DBSituations.class);
   List<FrameworkMethod> DBSetupSituation = testClass.getAnnotatedMethods(DBSetupSituation.class);
   List<FrameworkMethod> DBTeardownSituation =
       testClass.getAnnotatedMethods(DBTeardownSituation.class);
   if (!CollectionUtils.isEmpty(DBSituations)
       || !CollectionUtils.isEmpty(DBSetupSituation)
       || !CollectionUtils.isEmpty(DBTeardownSituation)) {
     MilouDBThreadLocal.setMilouDBListener(Boolean.valueOf(true));
   }
 }
 @SuppressWarnings("deprecation")
 private void addSinglePointMethods(ParameterSignature sig, List<PotentialAssignment> list) {
   for (FrameworkMethod dataPointMethod : fClass.getAnnotatedMethods(DataPoint.class)) {
     Class<?> type = sig.getType();
     if ((dataPointMethod.producesType(type))) list.add(new MethodParameterValue(dataPointMethod));
   }
 }
 private void addMultiPointMethods(List<PotentialAssignment> list) {
   for (FrameworkMethod dataPointsMethod : fClass.getAnnotatedMethods(DataPoints.class))
     try {
       addArrayValues(dataPointsMethod.getName(), list, dataPointsMethod.invokeExplosively(null));
     } catch (Throwable e) {
       // ignore and move on
     }
 }
Esempio n. 5
0
 /** @see Parameterized */
 private FrameworkMethod getParametersMethod(TestClass testClass) throws Exception {
   List<FrameworkMethod> methods = testClass.getAnnotatedMethods(Parameterized.Parameters.class);
   for (FrameworkMethod each : methods) {
     int modifiers = each.getMethod().getModifiers();
     if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers)) return each;
   }
   throw new Exception("No public static parameters method on class " + testClass.getName());
 }
Esempio n. 6
0
 private void setAllSituationIntoMap(TestClass testClass) {
   List<FrameworkMethod> frameworkMethods = testClass.getAnnotatedMethods(Test.class);
   for (FrameworkMethod method : frameworkMethods) {
     List<SituationInfo> list = getSituationInfos(method);
     if (!CollectionUtils.isEmpty(list)) {
       situationInfos.put(method, list);
     }
   }
 }
Esempio n. 7
0
  public TestClassAttributes(
      TestClass testClass, ClassResolver classResolver, DependencyCollector dependencyCollector) {
    beforeMethods = testClass.getAnnotatedMethods(Before.class);
    afterMethods = testClass.getAnnotatedMethods(After.class);
    stjsClass = classResolver.resolve(testClass.getName());

    htmlFixture = testClass.getJavaClass().getAnnotation(HTMLFixture.class);

    scripts =
        getScriptsPathes(
            testClass.getJavaClass(),
            Scripts.class,
            new Function1<Annotation, Collection<String>>() {
              @Override
              public Collection<String> $invoke(Annotation annotation) {
                return Arrays.asList(Scripts.class.cast(annotation).value());
              }
            });

    scriptsBefore =
        getScriptsPathes(
            testClass.getJavaClass(),
            ScriptsBefore.class,
            new Function1<Annotation, Collection<String>>() {
              @Override
              public Collection<String> $invoke(Annotation annotation) {
                return Arrays.asList(ScriptsBefore.class.cast(annotation).value());
              }
            });

    scriptsAfter =
        getScriptsPathes(
            testClass.getJavaClass(),
            ScriptsAfter.class,
            new Function1<Annotation, Collection<String>>() {
              @Override
              public Collection<String> $invoke(Annotation annotation) {
                return Arrays.asList(ScriptsAfter.class.cast(annotation).value());
              }
            });

    dependencies = dependencyCollector.orderAllDependencies(stjsClass);
  }
Esempio n. 8
0
 @Test
 public void providesAnnotatedMethodsSortedByName() {
   TestClass tc = new TestClass(MethodsAnnotated.class);
   List<FrameworkMethod> annotatedMethods = tc.getAnnotatedMethods();
   assertThat("Wrong number of annotated methods.", annotatedMethods.size(), is(3));
   assertThat(
       "First annotated method is wrong.",
       annotatedMethods.iterator().next().getName(),
       is("methodA"));
 }
  public InternalRemoteRunner(
      Class<?> testClass, String endpoint, Class<? extends Runner> remoteRunnerClass)
      throws InitializationError {
    super(testClass);
    this.testClass = testClass;
    this.remoteRunnerClass = remoteRunnerClass;
    TestClass tc = new TestClass(testClass);

    description = Description.createTestDescription(testClass, tc.getName(), tc.getAnnotations());

    for (FrameworkMethod method : tc.getAnnotatedMethods(Test.class)) {
      String methodName = method.getName();
      Description child =
          Description.createTestDescription(testClass, methodName, method.getAnnotations());

      methodNames.put(child, methodName);
      description.addChild(child);
    }

    if (executorService == null) {
      String ep = System.getProperty("junit.remote.endpoint");
      if (ep == null) {
        ep = endpoint;
      }

      for (String e : ep.split(",")) {
        if (e.trim().equals("")) {
          continue;
        }
        endpoints.add(e.trim());
      }
      executorService = Executors.newFixedThreadPool(endpoints.size());
    }

    setScheduler(
        new RunnerScheduler() {
          @Override
          public void schedule(final Runnable childStatement) {
            SEMAPHORE.reducePermits(1);
            executorService.submit(new SemaphoreDelegate(childStatement));
          }

          @Override
          public void finished() {
            try {
              SEMAPHORE.acquire();
              SEMAPHORE.release();
            } catch (InterruptedException ignore) {
              Thread.currentThread().interrupt();
            }
          }
        });
  }
  protected static List<MethodKey> getMethodKeys(
      Class<?> targetClass, Class<? extends Annotation> annotationClass) {

    TestClass testClass = new TestClass(targetClass);

    List<FrameworkMethod> frameworkMethods = testClass.getAnnotatedMethods(annotationClass);

    List<MethodKey> methodKeys = new ArrayList<MethodKey>(frameworkMethods.size());

    for (FrameworkMethod annotatedFrameworkMethod : frameworkMethods) {
      methodKeys.add(new MethodKey(annotatedFrameworkMethod.getMethod()));
    }

    return methodKeys;
  }
Esempio n. 11
0
 public int getMaxTestNameLen() {
   if (0 == maxMethodNameLen) {
     int ml = 0;
     final TestClass tc = new TestClass(getClass());
     final List<FrameworkMethod> testMethods = tc.getAnnotatedMethods(org.junit.Test.class);
     for (Iterator<FrameworkMethod> iter = testMethods.iterator(); iter.hasNext(); ) {
       final int l = iter.next().getName().length();
       if (ml < l) {
         ml = l;
       }
     }
     maxMethodNameLen = ml;
   }
   return maxMethodNameLen;
 }
  private void handleInitializationError(
      final boolean developmentMode,
      final List<TestClassResult> result,
      final String klassName,
      final Class<?> klass,
      final InitializationError e) {
    TestClass testClass = new TestClass(klass);

    boolean allMethods =
        (!developmentMode) || (klass.getAnnotation(TestDuringDevelopment.class) != null);

    List<Throwable> causes = e.getCauses();
    long now = System.currentTimeMillis();
    List<TestCaseResult> testCaseResults = new ArrayList<TestCaseResult>();
    List<FrameworkMethod> annotatedMethods = testClass.getAnnotatedMethods(Test.class);

    Exception wrapperException = new Exception("Error during initialization. See log for details");
    wrapperException.setStackTrace(new StackTraceElement[0]);

    for (FrameworkMethod frameworkMethod : annotatedMethods) {
      if (allMethods || (frameworkMethod.getAnnotation(TestDuringDevelopment.class) != null)) {
        TestCaseResult testCaseResult =
            new TestCaseResult(frameworkMethod.getName(), now, now, wrapperException);

        testCaseResults.add(testCaseResult);
      }
    }

    TestClassResult classResult =
        new TestClassResult(klassName, 0, testCaseResults.size(), 0, 0, now, now, testCaseResults);

    result.add(classResult);

    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    pw.write("Error during initialization of test class '" + klassName + "':\n");
    for (Throwable throwable : causes) {
      throwable.printStackTrace(pw);
    }
    LOGGER.log(Level.SEVERE, sw.toString());
  }
 private List<TestCaseResult> createErrorTestCaseResults(
     final Class<?> klass, final boolean developmentMode, final long now) {
   TestClass testClass = new TestClass(klass);
   boolean allMethods =
       (!developmentMode) || (klass.getAnnotation(TestDuringDevelopment.class) != null);
   List<FrameworkMethod> annotatedMethods = testClass.getAnnotatedMethods(Test.class);
   List<TestCaseResult> result = new ArrayList<TestCaseResult>();
   for (FrameworkMethod frameworkMethod : annotatedMethods) {
     if (allMethods || (frameworkMethod.getAnnotation(TestDuringDevelopment.class) != null)) {
       Exception exception =
           new Exception(
               "Service object behind the reference was null. We experienced this when the activate"
                   + " method of a DS component threw an exception but there can be other causes. Please"
                   + " check the log! Please note, that in Equinox sometimes such exceptions are"
                   + " written only into different files in the configuration folder.");
       exception.setStackTrace(new StackTraceElement[0]);
       result.add(new TestCaseResult(frameworkMethod.getName(), now, now, exception));
     }
   }
   return result;
 }
Esempio n. 14
0
 /** 完成Stub的元数据扫描,构建stub元数据仓库 */
 private void stubInfoRepoInit(TestClass testClass) {
   // 加载本地stub仓库
   List<FrameworkMethod> stubMethods = testClass.getAnnotatedMethods(Stub.class);
   if (!CollectionUtils.isEmpty(stubMethods)) {
     for (FrameworkMethod fm : stubMethods) {
       StubInfo info = new StubInfo();
       String stubId = fm.getAnnotation(Stub.class).value();
       info.setStubId(stubId);
       info.setStubClass(testClass.getJavaClass());
       info.setStubMethod(fm.getMethod());
       logger.info("[milou] scan stub,stubId={}", stubId);
       StubInfoRepo.setIntoStubInfoRepo(stubId, info);
     }
   }
   StubLocation locationAnno = testClass.getJavaClass().getAnnotation(StubLocation.class);
   if (locationAnno != null && !locationAnno.value().equals(StubLocation.LOCAL)) {
     // 根据注解加载指定路径下的stub仓库
     List<Class<?>> scanList = PackageScanner.getStubClass(locationAnno.value());
     StubInfoScanner.scanAllStubClass(scanList, StubInfoRepo.getStubInfoRepo());
   }
 }
Esempio n. 15
0
 /**
  * Returns a {@link Statement}: run all non-overridden {@code @AfterClass} methods on this class
  * and superclasses before executing {@code statement}; all AfterClass methods are always
  * executed: exceptions thrown by previous steps are combined, if necessary, with exceptions from
  * AfterClass methods into a {@link MultipleFailureException}.
  */
 protected Statement withAfterClasses(Statement statement) {
   List<FrameworkMethod> afters = fTestClass.getAnnotatedMethods(AfterClass.class);
   return afters.isEmpty() ? statement : new RunAfters(statement, afters, null);
 }
Esempio n. 16
0
 /**
  * Returns a {@link Statement}: run all non-overridden {@code @BeforeClass} methods on this class
  * and superclasses before executing {@code statement}; if any throws an Exception, stop execution
  * and pass the exception on.
  */
 protected Statement withBeforeClasses(Statement statement) {
   List<FrameworkMethod> befores = fTestClass.getAnnotatedMethods(BeforeClass.class);
   return befores.isEmpty() ? statement : new RunBefores(statement, befores, null);
 }