@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);
  }
Example #2
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);
  }
 private void addFields(ParameterSignature sig, List<PotentialAssignment> list) {
   for (final Field field : fClass.getJavaClass().getFields()) {
     if (Modifier.isStatic(field.getModifiers())) {
       Class<?> type = field.getType();
       if (sig.canAcceptArrayType(type) && field.getAnnotation(DataPoints.class) != null) {
         addArrayValues(field.getName(), list, getStaticFieldValue(field));
       } else if (sig.canAcceptType(type) && field.getAnnotation(DataPoint.class) != null) {
         list.add(PotentialAssignment.forValue(field.getName(), getStaticFieldValue(field)));
       }
     }
   }
 }
 /** 完成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());
   }
 }
Example #5
0
 @Override
 protected Statement classBlock(final RunNotifier notifier) {
   TestClass testClass = getTestClass();
   Method checkMethod = getMethod(testClass.getJavaClass(), CheckBefore.class);
   Exception exception = execute(newInstance(testClass.getJavaClass()), checkMethod);
   Statement statement;
   if (exception == null) {
     statement = super.classBlock(notifier);
   } else {
     LOG.warn(
         "skip test "
             + testClass.getJavaClass().getSimpleName()
             + " cause check faild: "
             + exception.getMessage());
     return new Statement() {
       @Override
       public void evaluate() throws Throwable {
         Description description = Description.createSuiteDescription(getClass());
         notifier.fireTestIgnored(description);
       }
     };
   }
   return statement;
 }
 private void initTest() {
   TestClass testClass = jUnit4.getTestClass();
   if (testClass != null) {
     TestEngine.initTest(testClass.getJavaClass());
   }
 }
 /**
  * Load the Data for the given class or method. This method will try to find {@link DataLoader} on
  * either the class level or the method level. In case the annotation is found, this method will
  * load the data using the specified loader class and then save it in the DataContext for further
  * use by the system. We also create another copy of the input test data that we store in the
  * {@link DataDrivenTestRunner#writableData} field. This is done in order to facilitate the
  * writing of the data that might be returned by the test method.
  *
  * @param testClass the class object, if any.
  * @param method current executing method, if any.
  * @param currentTestClass the currently executing test class. this is used to append in front of
  *     the method name to get unique method names as there could be methods in different classes
  *     with the same name and thus we want to avoid conflicts.
  * @param writableData The writable data that is used internally for reporting purposes
  */
 public static void loadData(
     Class<?> testClass,
     FrameworkMethod method,
     TestClass currentTestClass,
     Map<String, List<Map<String, Object>>> writableData) {
   if (testClass == null && method == null) {
     Assert.fail(
         "The framework should provide either the testClass parameter or the method parameter in order to load the test data.");
   }
   // We give priority to Class Loading and then to method loading
   DataLoader testData = null;
   if (testClass != null) {
     testData = testClass.getAnnotation(DataLoader.class);
   } else {
     testData = method.getAnnotation(DataLoader.class);
   }
   if (testData != null) {
     TestInfo testInfo = DataLoaderUtil.determineLoader(testData, currentTestClass);
     Loader dataLoader = testInfo.getDataLoader();
     if (testInfo.getDataLoader() == null) {
       Assert.fail(
           "The framework currently does not support the specified Loader type. "
               + "You can provide the custom Loader by choosing LoaderType.CUSTOM in TestData "
               + "annotation and providing your custom loader using DataLoader annotation.");
     } else {
       if (testInfo.getFilePaths() == null || testInfo.getFilePaths().length == 0) {
         // implies that there exists a CUSTOM loader that loads the data using Java classes
         Map<String, List<Map<String, Object>>> data = dataLoader.loadData(new EmptyResource());
         // We also maintain the copy of the actual data for our write functionality.
         writableData.putAll(data);
         DataContext.setData(DataConverter.appendClassName(data, currentTestClass.getJavaClass()));
         DataContext.setConvertedData(
             DataConverter.convert(data, currentTestClass.getJavaClass()));
       } else {
         ResourceLoader resourceLoader =
             new ResourceLoaderStrategy(currentTestClass.getJavaClass());
         for (String filePath : testInfo.getFilePaths()) {
           Resource resource = resourceLoader.getResource(filePath);
           try {
             if (resource.exists()) {
               Map<String, List<Map<String, Object>>> data = dataLoader.loadData(resource);
               // We also maintain the copy of the actual data for our write functionality.
               writableData.putAll(data);
               DataContext.setData(
                   DataConverter.appendClassName(data, currentTestClass.getJavaClass()));
               DataContext.setConvertedData(
                   DataConverter.convert(data, currentTestClass.getJavaClass()));
             } else {
               LOG.warn(
                   "Resource {} does not exists in the specified path. If it is a classpath resource, use 'classpath:' "
                       + "before the path name, else check the path.",
                   resource);
             }
           } catch (Exception e) {
             LOG.error(
                 "Exception occured while trying to load the data for resource {}", resource, e);
             throw new RuntimeException(e);
           }
         }
       }
     }
   }
 }