/** * Determine if there are generators * * @param type * @return */ public boolean hasGenerator(Type type) { return hasGenerator(new GenericClass(type)); }
private boolean hasFunctionalMocksForGenerableTypes(TestCase testCase) { for (Statement statement : testCase) { if (statement instanceof FunctionalMockStatement) { FunctionalMockStatement fm = (FunctionalMockStatement) statement; Class<?> target = fm.getTargetClass(); GenericClass gc = new GenericClass(target); if (TestCluster.getInstance().hasGenerator(gc)) { return true; } } } return false; }
cacheGenerators(clazz); if(!hasGenerator(clazz)) { throw new ConstructionFailedException("No generators of type " + clazz);
.filter(c -> TestCluster.getInstance().hasGenerator(c) || c.isString()) .collect(Collectors.toList()); classes.add(new GenericClass(Object.class)); if (!TestCluster.getInstance().hasGenerator(Object.class)) { logger.debug("We have no generator for Object.class ");
@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()))); }
/** * Get a list of all generator objects for the type * * @param clazz * @param resolve * @return * @throws ConstructionFailedException */ public Set<GenericAccessibleObject<?>> getGenerators(GenericClass clazz, boolean resolve) throws ConstructionFailedException { // Instantiate generic type if (clazz.hasWildcardOrTypeVariables()) { GenericClass concreteClass = clazz.getGenericInstantiation(); if (!concreteClass.equals(clazz)) return getGenerators(concreteClass, false); } if (isSpecialCase(clazz)) { return getGeneratorsForSpecialCase(clazz); } if (!hasGenerator(clazz)) throw new ConstructionFailedException("No generators of type " + clazz); return generatorCache.get(clazz); }
@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()))); } }
if (!hasGenerator(clazz)) throw new ConstructionFailedException("No generators of type " + clazz);
logger.debug("No mock solution found: {}, {}, {}, {}", canUseFunctionalMocks, Properties.MOCK_IF_NO_GENERATOR, FunctionalMockStatement.canBeFunctionalMocked(type), FunctionalMockStatement.canBeFunctionalMockedIncludingSUT(type)); if(!TestCluster.getInstance().hasGenerator(type)) { logger.debug("No generators found for {}, attempting to resolve dependencies", type); TestClusterGenerator clusterGenerator = TestGenerationContext.getInstance().getTestClusterGenerator(); if (TestCluster.getInstance().hasGenerator(type)) { logger.debug("Found new generators for {}", type); return createObject(test, type, position, recursionDepth + 1, generatorRefToExclude, allowNull, canUseFunctionalMocks, canReuseVariables);
clazz.isString() || clazz.isArray() || TestCluster.getInstance().hasGenerator(parameterType) || Properties.P_FUNCTIONAL_MOCKING > 0 || Properties.MOCK_IF_NO_GENERATOR) {