private void initializeTargetClass() throws Throwable { String cp = ClassPathHandler.getInstance().getTargetProjectClasspath(); // Here is where the <clinit> code should be invoked for the first time DefaultTestCase test = buildLoadTargetClassTestCase(Properties.TARGET_CLASS); ExecutionResult execResult = TestCaseExecutor.getInstance().execute(test, Integer.MAX_VALUE); if (hasThrownInitializerError(execResult)) { // create single test suite with Class.forName() writeJUnitTestSuiteForFailedInitialization(); ExceptionInInitializerError ex = getInitializerError(execResult); throw ex; } else if (!execResult.getAllThrownExceptions().isEmpty()) { // some other exception has been thrown during initialization Throwable t = execResult.getAllThrownExceptions().iterator().next(); throw t; } DependencyAnalysis.analyzeClass(Properties.TARGET_CLASS, Arrays.asList(cp.split(File.pathSeparator))); LoggingUtils.getEvoLogger().info("* " + ClientProcess.getPrettyPrintIdentifier() + "Finished analyzing classpath"); }
@Override public void run() { changeState(ClientState.STARTED); Sandbox.goingToExecuteSUTCode(); TestGenerationContext.getInstance().goingToExecuteSUTCode(); Sandbox.goingToExecuteUnsafeCodeOnSameThread(); try { LoggingUtils.getEvoLogger().info("* Analyzing classpath (dependency analysis)"); DependencyAnalysis.analyzeClass(Properties.TARGET_CLASS, Arrays.asList(ClassPathHandler.getInstance().getClassPathElementsForTargetProject())); StringBuffer fileNames = new StringBuffer(); for(Class<?> clazz : TestCluster.getInstance().getAnalyzedClasses()) { fileNames.append(clazz.getName()); fileNames.append("\n"); } LoggingUtils.getEvoLogger().info("* Writing class dependencies to file "+fileName); FileIOUtils.writeFile(fileNames.toString(), fileName); } catch (Throwable t) { logger.error("Error when analysing coverage for: " + Properties.TARGET_CLASS + " with seed " + Randomness.getSeed() + ". Configuration id : " + Properties.CONFIGURATION_ID, t); } finally { Sandbox.doneWithExecutingUnsafeCodeOnSameThread(); Sandbox.doneWithExecutingSUTCode(); TestGenerationContext.getInstance().doneWithExecutingSUTCode(); } changeState(ClientState.DONE); } });
@BeforeClass public static void initialize() { Properties.TARGET_CLASS = "com.examples.with.different.packagename.context.complex.EntryPointsClass"; Properties.CRITERION = new Criterion[1]; Properties.CRITERION[0]=Criterion.IBRANCH; List<String> classpath = new ArrayList<>(); String cp = System.getProperty("user.dir") + "/target/test-classes"; classpath.add(cp); ClassPathHandler.getInstance().addElementToTargetProjectClassPath(cp); try { DependencyAnalysis .analyzeClass( "com.examples.with.different.packagename.context.complex.EntryPointsClass", classpath); } catch (ClassNotFoundException | RuntimeException e) { Assert.fail(e.toString()); } }
@BeforeClass public static void initialize() { Properties.TARGET_CLASS = "com.examples.with.different.packagename.context.EntryPointsClass"; Properties.CRITERION = new Criterion[1]; Properties.CRITERION[0]=Criterion.IBRANCH; List<String> classpath = new ArrayList<>(); String cp = System.getProperty("user.dir") + "/target/test-classes"; classpath.add(cp); ClassPathHandler.getInstance().addElementToTargetProjectClassPath(cp); try { DependencyAnalysis .analyzeClass( "com.examples.with.different.packagename.context.EntryPointsClass", classpath); } catch (ClassNotFoundException | RuntimeException e) { Assert.fail(); } }
ExecutionTracer.setCheckCallerThread(false); try { DependencyAnalysis.analyzeClass(Properties.TARGET_CLASS, Arrays.asList(ClassPathHandler.getInstance().getClassPathElementsForTargetProject()));
@Test public void testCycle() throws Exception { String targetClass = ClassToCheckGetterOfInput.class.getName(); Properties.TARGET_CLASS = targetClass; List<String> classpath = new ArrayList<>(); String cp = System.getProperty("user.dir") + "/target/test-classes"; classpath.add(cp); ClassPathHandler.getInstance().addElementToTargetProjectClassPath(cp); ClassPathHandler.getInstance().changeTargetCPtoTheSameAsEvoSuite(); DependencyAnalysis.analyzeClass(targetClass, classpath); ClassLoader cl = TestGenerationContext.getInstance().getClassLoaderForSUT(); Assert.assertFalse(TestCluster.getInstance().hasGenerator(cl.loadClass(AnInterface.class.getName()))); } }
DependencyAnalysis.analyzeClass(Properties.TARGET_CLASS, Arrays.asList(cp.split(File.pathSeparator)));
@Test public void test() throws Exception { String targetClass = ClassToCheckGenerators.class.getName(); Properties.TARGET_CLASS = targetClass; List<String> classpath = new ArrayList<>(); String cp = System.getProperty("user.dir") + "/target/test-classes"; classpath.add(cp); ClassPathHandler.getInstance().addElementToTargetProjectClassPath(cp); ClassPathHandler.getInstance().changeTargetCPtoTheSameAsEvoSuite(); DependencyAnalysis.analyzeClass(targetClass, classpath); ClassLoader cl = TestGenerationContext.getInstance().getClassLoaderForSUT(); //WithGenerator has default constructor Assert.assertTrue(TestCluster.getInstance().hasGenerator(cl.loadClass(WithGenerator.class.getName()))); //Only generator for IX is GeneratorForX, but this latter has no generator itself Assert.assertFalse(TestCluster.getInstance().hasGenerator(cl.loadClass(IX.class.getName()))); //Special case of recursion Assert.assertFalse(TestCluster.getInstance().hasGenerator(cl.loadClass(IGeneratorForItself.class.getName()))); //same for abstract versions Assert.assertFalse(TestCluster.getInstance().hasGenerator(cl.loadClass(AX.class.getName()))); Assert.assertFalse(TestCluster.getInstance().hasGenerator(cl.loadClass(AGeneratorForItself.class.getName()))); //be sure it has no generator Assert.assertFalse(TestCluster.getInstance().hasGenerator(cl.loadClass(GeneratorForX.class.getName()))); //even for concrete versions it should not work, as they all have private constructors Assert.assertFalse(TestCluster.getInstance().hasGenerator(cl.loadClass(X.class.getName()))); Assert.assertFalse(TestCluster.getInstance().hasGenerator(cl.loadClass(GeneratorForItself.class.getName()))); }
/** * As RESET_STATIC_FINAL_FIELDS=true removes the <code>final</code> modifier * of static fields in the target class, the purpose of this test case is to * check that the TestClusterGenerator indeed does not include these fields. * * * @throws ClassNotFoundException * @throws RuntimeException */ @Test public void test() throws ClassNotFoundException, RuntimeException { Properties.TARGET_CLASS = FinalPrimitiveField.class.getCanonicalName(); Properties.RESET_STATIC_FINAL_FIELDS = true; ClassPathHandler.getInstance().changeTargetCPtoTheSameAsEvoSuite(); String cp = ClassPathHandler.getInstance().getTargetProjectClasspath(); DependencyAnalysis.analyzeClass(Properties.TARGET_CLASS, Arrays.asList(cp.split(File.pathSeparator))); InheritanceTree tree = DependencyAnalysis.getInheritanceTree(); TestClusterGenerator gen = new TestClusterGenerator(tree); assertNotNull(gen); TestCluster cluster = TestCluster.getInstance(); List<GenericAccessibleObject<?>> testCalls = cluster.getTestCalls(); assertEquals("Unexpected number of TestCalls", 2, testCalls.size()); }
/** * As RESET_STATIC_FINAL_FIELDS=true removes the <code>final</code> modifier * of static fields in the target class, the purpose of this test case is to * check that the TestClusterGenerator indeed does not include these fields. * * * @throws ClassNotFoundException * @throws RuntimeException */ @Test public void test() throws ClassNotFoundException, RuntimeException { Properties.TARGET_CLASS = FinalReferenceField.class.getCanonicalName(); Properties.RESET_STATIC_FINAL_FIELDS = true; ClassPathHandler.getInstance().changeTargetCPtoTheSameAsEvoSuite(); String cp = ClassPathHandler.getInstance().getTargetProjectClasspath(); DependencyAnalysis.analyzeClass(Properties.TARGET_CLASS, Arrays.asList(cp.split(File.pathSeparator))); InheritanceTree tree = DependencyAnalysis.getInheritanceTree(); TestClusterGenerator gen = new TestClusterGenerator(tree); assertNotNull(gen); TestCluster cluster = TestCluster.getInstance(); List<GenericAccessibleObject<?>> testCalls = cluster.getTestCalls(); assertEquals("Unexpected number of TestCalls", 2, testCalls.size()); }
DependencyAnalysis.analyzeClass(MIMEType.class.getName(), Arrays.asList(classPath));
classpath.add(cp); ClassPathHandler.getInstance().addElementToTargetProjectClassPath(cp); DependencyAnalysis.analyzeClass(Properties.TARGET_CLASS, classpath);
DependencyAnalysis.analyzeClass(Properties.TARGET_CLASS, Arrays.asList(cp.split(File.pathSeparator))); testClusterGenerator = new TestClusterGenerator( DependencyAnalysis.getInheritanceTree());