Esempio n. 1
0
    public static void invokeTests(
        @NotNull InMemoryJavaGenerationHandler generationHandler,
        List<SModel> outputModels,
        junit.framework.TestResult testResult,
        ClassLoader baseClassLoader) {
      Condition<SNode> cond =
          new Condition<SNode>() {
            public boolean met(SNode node) {
              return node.isInstanceOfConcept(BootstrapLanguages.concept_baseLanguage_ClassConcept);
            }
          };
      for (final SModel model : outputModels) {
        Iterable<SNode> iterable = new ConditionalIterable<SNode>(model.roots(), cond);
        for (final SNode outputRoot : iterable) {
          if (baseClassLoader == null) {
            baseClassLoader = model.getClass().getClassLoader();
          }
          ClassLoader classLoader = generationHandler.getCompiler().getClassLoader(baseClassLoader);
          try {
            String className =
                ModelAccess.instance()
                    .runReadAction(
                        new Computable<String>() {
                          public String compute() {
                            return model.getLongName() + "." + outputRoot.getName();
                          }
                        });
            final Class testClass = Class.forName(className, true, classLoader);
            if (Modifier.isAbstract(testClass.getModifiers())
                || Modifier.isInterface(testClass.getModifiers())) continue;
            if (Modifier.isPrivate(testClass.getModifiers())) continue;
            if (testClass.getAnnotation(
                    classLoader.loadClass("jetbrains.mps.baseLanguage.util.plugin.run.MPSLaunch"))
                != null) continue;

            List<Method> testMethods = new ArrayList<Method>();
            Class<TestCase> testCaseClass =
                (Class<TestCase>) classLoader.loadClass(TestCase.class.getName());
            boolean isTestCase = testCaseClass.isAssignableFrom(testClass);

            for (Method method : testClass.getMethods()) {
              if (method.getAnnotation(
                          (Class<Annotation>) classLoader.loadClass(org.junit.Test.class.getName()))
                      != null
                  || (method.getName().startsWith("test") && isTestCase)) {
                testMethods.add(method);
              }
            }

            for (Method testMethod : testMethods) {
              try {
                final Object instance = testClass.newInstance();
                Method setName = testCaseClass.getMethod("setName", String.class);
                setName.invoke(instance, testMethod.getName());
                Method runMethod =
                    testCaseClass.getMethod(
                        "run", classLoader.loadClass(junit.framework.TestResult.class.getName()));
                runMethod.invoke(instance, testResult);
              } catch (Throwable ignored) {
                // if one test fails, we still want to try to run the others
                System.err.println(testClass.getCanonicalName() + ":");
                ignored.printStackTrace();
              }
            }
          } catch (Throwable ignored) {
            ignored.printStackTrace(); // exceptions happen for a reason
          }
        }
      }
    }