public static boolean testRefactoringTestEnvironment(File projectDirectory) { IdeMain.setTestMode(TestMode.CORE_TEST); TestMain.configureMPS(); final Project project = loadProject(new File(projectDirectory, REFACTORING_PROJECT)); final boolean[] b = new boolean[] {true}; ModelAccess.instance() .runReadAction( new Runnable() { public void run() { b[0] = getModel(project, REFACTORING_SANDBOX[0]) != null && getModel(project, REFACTORING_SANDBOX[1]) != null && getLanguage(project, REFACTORING_LANGUAGE[0]) != null && getLanguage(project, REFACTORING_LANGUAGE[1]) != null; } }); ThreadUtils.runInUIThreadNoWait( new Runnable() { public void run() { project.dispose(); } }); return b[0]; }
private static void waitUntilAllEventsFlushed() { // Wait until last invokeLater() is executed ApplicationManager.getApplication() .invokeAndWait( new Runnable() { public void run() {} }, ModalityState.NON_MODAL); ModelAccess.instance().flushEventQueue(); }
@NotNull public static Project loadProject(File projectFile) { if (!projectFile.exists()) { throw new RuntimeException("Can't find project file " + projectFile.getAbsolutePath()); } final ProjectManagerEx projectManager = ProjectManagerEx.getInstanceEx(); final String filePath = projectFile.getAbsolutePath(); // this is a workaround for MPS-8840 final com.intellij.openapi.project.Project[] project = new com.intellij.openapi.project.Project[1]; ThreadUtils.runInUIThreadAndWait( new Runnable() { public void run() { try { project[0] = projectManager.loadAndOpenProject(filePath, false); } catch (IOException e) { throw new RuntimeException(e); } catch (JDOMException e) { throw new RuntimeException(e); } catch (InvalidDataException e) { throw new RuntimeException(e); } } }); if (project[0] == null) { // this actually happens throw new RuntimeException( "ProjectManager could not load project from " + projectFile.getAbsolutePath()); } ModelAccess.instance() .runReadAction( new Runnable() { public void run() { new ModuleMaker() .make( new LinkedHashSet<IModule>( MPSModuleRepository.getInstance().getAllModules()), new EmptyProgressMonitor()); } }); projectManager.openProject(project[0]); return project[0].getComponent(MPSProject.class); }
public TestResult testProject(final String[] configurationsGiven) { ClassLoaderManager.getInstance().reloadAll(new EmptyProgressMonitor()); final List<String> errors = new ArrayList<String>(); final List<String> warnings = new ArrayList<String>(); final List<String> compilationResults = new ArrayList<String>(); final List<TestFailure> failedTests = new ArrayList<TestFailure>(); final List<String> diffReports = new ArrayList<String>(); final IMessageHandler handler = new MyIMessageHandler(errors, warnings); final ILoggingHandler loggingHandler = new MyILoggingHandler(errors, warnings); try { Logger.addLoggingHandler(loggingHandler); final DiffGenerationHandler generationHandler = new DiffGenerationHandler(true); ModelAccess.instance() .runWriteAction( new Runnable() { public void run() { List<BaseTestConfiguration> configurations = new ArrayList<BaseTestConfiguration>( ((StandaloneMPSProject) myProject) .getProjectDescriptor() .getTestConfigurations()); if (configurations.isEmpty()) { throw new RuntimeException("tested project has no test configurations"); } for (BaseTestConfiguration t : configurations) { System.out.println( "completed : " + configurations.indexOf(t) + " / " + configurations.size()); if (configurationsGiven.length > 0) { boolean exists = false; for (String confName : configurationsGiven) { if (confName.equals(t.getName())) { exists = true; break; } } if (!exists) { continue; } } GenParameters parms; try { parms = t.getGenParams(myProject, true); } catch (IllegalGeneratorConfigurationException e) { errors.add("Can't create a generator configuration : " + e.getMessage()); return; } int numErrorsBeforeGeneration = errors.size(); GenerationFacade.generateModels( myProject.getComponent(MPSProject.class), parms.getModelDescriptors(), new ModuleContext(parms.getModule(), myProject), generationHandler, new EmptyProgressMonitor(), handler, GenerationOptions.getDefaults().create(), myProject.getComponent(TransientModelsComponent.class)); if (myIsRunnable) { diffReports.addAll(generationHandler.createDiffReports(null)); } List<SModel> outputModels = new ArrayList<SModel>(); outputModels.addAll(generationHandler.getOutputModels()); if (errors.size() > numErrorsBeforeGeneration) { System.out.println("There were generation errors, cancelling compilation"); } else { long start = System.currentTimeMillis(); final List<CompilationResult> compilationResultList = new ArrayList<CompilationResult>(); CompilationResultAdapter listener = new CompilationResultAdapter() { public void onCompilationResult(CompilationResult r) { compilationResultList.add(r); } }; generationHandler.compile(new EmptyProgressMonitor(), listener); System.out.println( "Compiled " + compilationResultList.size() + " compilation units in " + (System.currentTimeMillis() - start)); compilationResults.addAll( createCompilationProblemsList(compilationResultList)); if (compilationResults.isEmpty()) { System.out.println("Compilation ok"); } failedTests.addAll(createTestFailures(generationHandler, outputModels)); } System.out.println(""); System.out.println(""); System.out.println(""); } } }); } finally { Logger.removeLoggingHandler(loggingHandler); } return new TestResult(errors, warnings, compilationResults, failedTests, diffReports); }
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 } } } }