public JavaOutputProcessor createOutputWriter() { /* * create without printer, because Launcher is not initialized yet * so we cannot setup correct printer validators at this time * The printer is created lazily using Environment */ JavaOutputProcessor outputProcessor = new JavaOutputProcessor(); outputProcessor.setFactory(this.getFactory()); return outputProcessor; }
@Override public void setBinaryOutputDirectory(String path) { getFactory().getEnvironment().setBinaryOutputDirectory(path); }
private static Factory createFactory(File spoonJavaSourcesDirectory) { final Launcher launcher = new Launcher(); launcher.getEnvironment().setNoClasspath(true); launcher.getEnvironment().setCommentEnabled(true); // // Spoon model interfaces Arrays.asList("spoon/reflect/code", "spoon/reflect/declaration", "spoon/reflect/reference", "spoon/support/reflect/declaration", "spoon/support/reflect/code", "spoon/support/reflect/reference").forEach(path -> launcher.addInputResource(new FileSystemFolder(new File(spoonJavaSourcesDirectory, path)))); launcher.buildModel(); return launcher.getFactory(); }
/** checks that the file `outputDirectoryFile` can be parsed with Spoon , given a compliance level and the noclasspath option. */ public static void canBeBuilt(File outputDirectoryFile, int complianceLevel, boolean noClasspath) { final Launcher launcher = new Launcher(); final Factory factory = launcher.getFactory(); factory.getEnvironment().setComplianceLevel(complianceLevel); factory.getEnvironment().setNoClasspath(noClasspath); final SpoonModelBuilder compiler = launcher.createCompiler(factory); compiler.addInputSource(outputDirectoryFile); try { compiler.build(); } catch (Exception e) { final AssertionError error = new AssertionError("Can't compile " + outputDirectoryFile.getName() + " because " + e.getMessage()); error.initCause(e); throw error; } }
@Override public void run(String[] args) { this.setArgs(args); if (args.length != 0) { this.run(); // display GUI if (this.jsapActualArgs.getBoolean("gui")) { new SpoonModelTree(getFactory()); } } else { this.printUsage(); } }
private DSpotCompiler(Launcher launcher, InputConfiguration configuration, String pathToDependencies) { super(launcher.getFactory()); this.dependencies = pathToDependencies; this.launcher = launcher; this.binaryOutputDirectory = new File(configuration.getAbsolutePathToTestClasses()); this.sourceOutputDirectory = new File(getPathToAmplifiedTestSrc()); if (!this.sourceOutputDirectory.exists()) { this.sourceOutputDirectory.mkdir(); } else { try { FileUtils.deleteDirectory(this.sourceOutputDirectory); } catch (IOException e) { throw new RuntimeException(e); } } }
LOGGER.info("Building Spoon model..."); launcher.buildModel(); final List<CtType<?>> testClasses = launcher.getFactory().Class().getAll() .stream() .filter(ctType -> ctType.getMethods().stream().anyMatch(TestFramework.get()::isTest))
/** * this method analyze all java class in generatedTestDir and return java paths composed of packages.class * @param generatedTestFolder * @param classPath * @return List<String> list of class ({package.package.classname}) */ public static List<String> getNewTestClasses(String generatedTestFolder, String classPath){ List<String> testsClasses = new ArrayList<String>(); logger.debug("--------------------------------------------------"); logger.debug(" ##### Search tests files path ##### "); Launcher spoon = new Launcher(); spoon.getEnvironment().setAutoImports(true); spoon.addInputResource(generatedTestFolder); spoon.getEnvironment().setSourceClasspath(classPath.split(File.pathSeparator)); spoon.buildModel(); //getannotatedMethod.. could be better for(CtType<?> clazz : spoon.getFactory().Class().getAll()){ String className = clazz.getPackage().toString()+"."+clazz.getSimpleName(); logger.debug("[FOUND] "+className); testsClasses.add(className); } return testsClasses; }
public DSpot(int numberOfIterations, List<Amplifier> amplifiers, TestSelector testSelector, BudgetizerEnum budgetizer) { String dependencies = InputConfiguration.get().getDependencies(); this.compiler = DSpotCompiler.createDSpotCompiler(InputConfiguration.get(), dependencies); InputConfiguration.get().setFactory(this.compiler.getLauncher().getFactory()); this.amplifiers = new ArrayList<>(amplifiers); this.numberOfIterations = numberOfIterations; this.testSelector = testSelector; this.testSelector.init(InputConfiguration.get()); String splitter = File.separator.equals("/") ? "/" : "\\\\"; final String[] splittedPath = InputConfiguration.get().getAbsolutePathToProjectRoot().split(splitter); final File projectJsonFile = new File(InputConfiguration.get().getOutputDirectory() + File.separator + splittedPath[splittedPath.length - 1] + ".json"); if (projectJsonFile.exists()) { Gson gson = new GsonBuilder().setPrettyPrinting().create(); try { this.projectTimeJSON = gson.fromJson(new FileReader(projectJsonFile), ProjectTimeJSON.class); } catch (FileNotFoundException e) { throw new RuntimeException(e); } } else { this.projectTimeJSON = new ProjectTimeJSON(splittedPath[splittedPath.length - 1]); } this.budgetizer = budgetizer.getBudgetizer(this.amplifiers); }
@Override public void run(String[] args) { this.setArgs(args); if (args.length != 0) { this.run(); // display GUI if (this.jsapActualArgs.getBoolean("gui")) { new SpoonModelTree(getFactory()); } } else { this.printUsage(); } }
return element.getVariable().getDeclaration().equals(catches.get(0).getParameter()) && super.matches(element); }).forEach(variableAccess -> variableAccess.replace(spoon.getFactory().createVariableRead(catchVariable.getReference(), false))); ) { ((CtLiteral<String>) failInvocation.get(0).getArguments().get(0)).replace( spoon.getFactory().createLiteral(PREFIX_MESSAGE_EXPECTED_EXCEPTION + exceptionName) );
private AssertFixerResult fixGivenTest(Launcher launcher, String failingClass, String failingTestMethod) { CtClass testClass = launcher.getFactory().Class().get(failingClass); Failure failure = TestRunner.runTest(this.configuration, launcher, failingClass, failingTestMethod).getFailingTests().get(0); LOGGER.info("Fixing: {}", failure.messageOfFailure); try { return AssertFixer.fixAssert( configuration, launcher, testClass, failingTestMethod, failure, this.configuration.getClasspath() ); } catch (Exception e) { AssertFixerResult fixerResult = new AssertFixerResult(failingClass, failingTestMethod); fixerResult.setFilePath(testClass.getPosition().getFile().getPath()); fixerResult.setExceptionMessage(e.getMessage()); fixerResult.setSuccess(false); return fixerResult; } } }
spoon.buildModel(); Factory factory = spoon.getFactory(); factory.getEnvironment().setAutoImports(true);
/** * this method analyze all java class in generatedTestDir and return a list of all Junit method * @param generatedTestFolder * @param classPath * @return List<CtMethod> list of methods */ public static List<CtMethod<?>> getNewTestsMethods(String generatedTestFolder, String classPath){ List<CtMethod<?>> testsMethods = new ArrayList<CtMethod<?>>(); logger.debug("--------------------------------------------------"); logger.debug(" ##### Search tests methods ##### "); Launcher spoon = new Launcher(); spoon.getEnvironment().setAutoImports(true); spoon.addInputResource(generatedTestFolder); spoon.getEnvironment().setSourceClasspath(classPath.split(File.pathSeparator)); spoon.buildModel(); //getannotatedMethod.. could be better for(CtType<?> clazz : spoon.getFactory().Class().getAll()){ methodLoop: for(CtMethod<?> method : clazz.getAllMethods()){ for(CtAnnotation<? extends Annotation> annotation : method.getAnnotations()){ if (annotation.getType().equals(method.getFactory().Annotation().createReference(org.junit.Test.class))) { logger.debug("[FOUND] "+method.getSignature()); testsMethods.add(method); continue methodLoop; } } } } return testsMethods; }
String fullQualifiedName, String testCaseName) { final Factory factory = spoon.getFactory(); final CtClass<?> testClass = factory.Class().get(fullQualifiedName); final CtElement addedElement = addSaveStatementInTearDownAfterClass(testClass);
private static CtClass<?> getExistingClass(CtType<?> type, String pathname) { Launcher launcher = new Launcher(); launcher.getEnvironment().setNoClasspath(true); launcher.addInputResource(pathname); launcher.buildModel(); return launcher.getFactory().Class().get(type.getQualifiedName()); }
private static CtClass<?> getNewModelCtClass(String pathToSrcFolder, String fullQualifiedName) { Launcher launcher = new Launcher(); launcher.getEnvironment().setNoClasspath(true); launcher.getEnvironment().setCommentEnabled(true); launcher.addInputResource(pathToSrcFolder); launcher.buildModel(); return launcher.getFactory().Class().get(fullQualifiedName); }
AssertionsFixer.fixAssertion(spoon.getFactory(), testCaseToBeFix, indexToLog); repairType = AssertFixerResult.RepairType.AssertRepair;
public boolean compile(String pathToAdditionalDependencies) { if (this.factory == null) { this.factory = this.launcher.getFactory();
static void addTryCatchFailAssertion(Launcher spoon, CtMethod<?> testCaseToBeFix, Failure failure) { final Factory factory = spoon.getFactory(); final CtTry aTry = factory.createTry(); final CtCatch aCatch = factory.createCatch(); aTry.addCatcher(aCatch); String fullQualifiedNameException = failure.fullQualifiedNameOfException; CtTypeReference reference = testCaseToBeFix.getFactory().Type().createReference(fullQualifiedNameException); final String[] splittedNameException = fullQualifiedNameException.split("\\."); String exceptionName = splittedNameException[splittedNameException.length - 1]; if (isAnonymous.test(failure.fullQualifiedNameOfException)) { reference = testCaseToBeFix.getFactory().Type().createReference(fullQualifiedNameException).getSuperclass(); } final CtCatchVariable<? extends Throwable> catchVariable = testCaseToBeFix.getFactory().createCatchVariable( reference, PREFIX_NAME_EXPECTED_EXCEPTION + exceptionName ); aCatch.setParameter(catchVariable); aCatch.setBody(factory.createCodeSnippetStatement( "org.junit.Assert.assertTrue(true)" )); aTry.setBody(testCaseToBeFix.getBody().getStatement(0)); for (int i = 1; i < testCaseToBeFix.getBody().getStatements().size(); i++) { aTry.getBody().addStatement(testCaseToBeFix.getBody().getStatement(i)); } aTry.getBody().addStatement(factory .createCodeSnippetStatement("org.junit.Assert." + NAME_FAIL_METHOD + "(\"" + PREFIX_MESSAGE_EXPECTED_EXCEPTION + exceptionName + "\")") ); testCaseToBeFix.setBody(aTry); }