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
 public static StringBuffer extractStackTrace(Throwable e) {
   StringWriter writer = new StringWriter();
   e.printStackTrace(new PrintWriter(writer));
   return writer.getBuffer();
 }
Example #3
0
  public static boolean testOnProjectCopy(
      final File source,
      final File destinationDir,
      final String projectName,
      ProjectRunnable pr,
      final String... plugins) {
    IdeMain.setTestMode(TestMode.CORE_TEST);
    Logger.setThreshold("WARN");
    org.apache.log4j.BasicConfigurator.configure();
    TestMain.configureMPS(plugins);

    if (destinationDir.exists()) {
      FileUtil.delete(destinationDir);
    }
    if (source.isDirectory()) {
      FileUtil.copyDir(source, destinationDir);
    } else { // it is allowed to have zipped directory here
      try {
        destinationDir.mkdir();
        UnzipUtil.unzip(source, destinationDir);
      } catch (IOException e) {
        e.printStackTrace();
        return false;
      }
    }

    final Project[] project = new MPSProject[] {null};
    try {
      // load a project
      ThreadUtils.runInUIThreadAndWait(
          new Runnable() {
            public void run() {
              try {
                project[0] = loadProject(new File(destinationDir, projectName));
                VirtualFile projectVirtualDir =
                    LocalFileSystem.getInstance().refreshAndFindFileByIoFile(destinationDir);
                assert projectVirtualDir != null;
                projectVirtualDir.refresh(false, true);
              } catch (Throwable t) {
                t.printStackTrace();
              }
            }
          });

      waitUntilAllEventsFlushed();

      // execute test
      return pr.execute(project[0]);
    } catch (Throwable t) {
      t.printStackTrace();
      return false;
    } finally {
      waitUntilAllEventsFlushed();

      // clean up
      ThreadUtils.runInUIThreadAndWait(
          new Runnable() {
            public void run() {
              if (project[0] != null) {
                project[0].dispose();
              }
              FileUtil.delete(destinationDir);
            }
          });
    }
  }