Example #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
          }
        }
      }
    }
Example #2
0
  @Nullable
  public static Class generateAndLoadEvaluatorClass(
      Project project,
      SModel modelDescriptor,
      String className,
      IOperationContext context,
      boolean developerMode,
      InMemoryJavaGenerationHandler handler,
      ClassLoader parentloader)
      throws EvaluationException {
    try {

      final String fullClassName =
          SNodeOperations.getModelLongName(modelDescriptor) + "." + className;
      GeneratorUtil.MyCompilationResultAdapter compilationResult =
          new GeneratorUtil.MyCompilationResultAdapter();
      handler.setCompilationListener(compilationResult);
      IMessageHandler messageHandler = new GeneratorUtil.MyMessageHandler(project, developerMode);
      ProgressWindow progressWindow =
          new ProgressWindow(false, ProjectHelper.toIdeaProject(project));
      boolean successful =
          GenerationFacade.generateModels(
              context.getProject(),
              Collections.singletonList(modelDescriptor),
              context,
              handler,
              new ProgressMonitorAdapter(progressWindow),
              messageHandler,
              GenerationOptions.getDefaults()
                  .incremental(new DefaultNonIncrementalStrategy())
                  .saveTransientModels(developerMode)
                  .rebuildAll(false)
                  .reporting(false, false, false, 0)
                  .create(),
              context.getProject().getComponent(TransientModelsComponent.class));

      Disposer.dispose(progressWindow);

      String source = handler.getSources().get(fullClassName);

      if (successful && (source != null && source.length() > 0)) {
        if (developerMode) {
          System.err.println("[Generated text]\n" + source + "\n[Generated text]");
        }
        return Class.forName(
            fullClassName, true, handler.getCompiler().getClassLoader(parentloader));
      } else if ((source != null && source.length() > 0) && !(successful)) {
        String text = "Errors during compilation";
        if (compilationResult.hasErrors()) {
          text += ":\n" + compilationResult.getMessage();
        } else {
          text += ".";
        }
        throw new EvaluationException(text);
      } else {
        throw new EvaluationException("Errors during generation.");
      }
    } catch (EvaluationException e) {
      throw e;
    } catch (ClassNotFoundException e) {
      throw new EvaluationException(e);
    }
  }