Search in sources :

Example 6 with GenericClass

use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.

the class TestCluster method toString.

/*
	 * (non-Javadoc)
	 *
	 * @see java.lang.Object#toString()
	 */
@Override
public String toString() {
    StringBuilder result = new StringBuilder();
    result.append("Analyzed classes:\n");
    for (Class<?> clazz : analyzedClasses) {
        result.append(clazz.getName());
        result.append("\n");
    }
    result.append("Generators:\n");
    for (GenericClass clazz : generators.keySet()) {
        result.append(" Generators for " + clazz.getTypeName() + ": " + generators.get(clazz).size() + "\n");
        for (GenericAccessibleObject<?> o : generators.get(clazz)) {
            result.append("  " + clazz.getTypeName() + " <- " + o + " " + "\n");
        }
    }
    result.append("Modifiers:\n");
    for (GenericClass clazz : modifiers.keySet()) {
        result.append(" Modifiers for " + clazz.getSimpleName() + ": " + modifiers.get(clazz).size() + "\n");
        for (GenericAccessibleObject<?> o : modifiers.get(clazz)) {
            result.append(" " + clazz.getSimpleName() + " <- " + o + "\n");
        }
    }
    result.append("Test calls\n");
    for (GenericAccessibleObject<?> testCall : testMethods) {
        result.append(" " + testCall + "\n");
    }
    result.append("Environment calls\n");
    for (GenericAccessibleObject<?> testCall : environmentMethods) {
        result.append(" " + testCall + "\n");
    }
    return result.toString();
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass)

Example 7 with GenericClass

use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.

the class EnvironmentTestClusterAugmenter method handleGUI.

private void handleGUI() {
    if (!hasAddedJOptionPaneInputsForStrings && JOptionPaneInputs.getInstance().hasDialog(GUIAction.STRING_INPUT)) {
        hasAddedJOptionPaneInputsForStrings = true;
        try {
            final Class<?> clazz = JOptionPaneInputs.class;
            final String ENQUEUE_INPUT_STRING = "enqueueInputString";
            final Method method_to_call = clazz.getMethod(ENQUEUE_INPUT_STRING, new Class<?>[] { String.class });
            final GenericClass genericClass = new GenericClass(clazz);
            final GenericMethod genericMethod = new GenericMethod(method_to_call, genericClass);
            // adds JOptionPaneInputs.enqueueString() to the palette of
            // methods that can be used
            TestCluster.getInstance().addEnvironmentTestCall(genericMethod);
        } catch (SecurityException e) {
            logger.error("Error while handling Random: " + e.getMessage(), e);
        } catch (NoSuchMethodException e) {
            logger.error("Error while handling Random: " + e.getMessage(), e);
        }
    }
    if (!hasAddedJOptionPaneYesNoCancelSelection && JOptionPaneInputs.getInstance().hasDialog(GUIAction.YES_NO_CANCEL_SELECTION)) {
        hasAddedJOptionPaneYesNoCancelSelection = true;
        try {
            final Class<?> clazz = JOptionPaneInputs.class;
            final String ENQUEUE_YES_NO_CANCEL_SELECTION = "enqueueYesNoCancelSelection";
            final Method method_to_call = clazz.getMethod(ENQUEUE_YES_NO_CANCEL_SELECTION, new Class<?>[] { int.class });
            final GenericClass genericClass = new GenericClass(clazz);
            final GenericMethod genericMethod = new GenericMethod(method_to_call, genericClass);
            // adds JOptionPaneInputs.enqueueString() to the palette of
            // methods that can be used
            TestCluster.getInstance().addEnvironmentTestCall(genericMethod);
        } catch (SecurityException e) {
            logger.error("Error while handling Random: " + e.getMessage(), e);
        } catch (NoSuchMethodException e) {
            logger.error("Error while handling Random: " + e.getMessage(), e);
        }
    }
    if (!hasAddedJOptionPaneYesNoSelection && JOptionPaneInputs.getInstance().hasDialog(GUIAction.YES_NO_SELECTION)) {
        hasAddedJOptionPaneYesNoSelection = true;
        try {
            final Class<?> clazz = JOptionPaneInputs.class;
            final String ENQUEUE_YES_NO_SELECTION = "enqueueYesNoSelection";
            final Method method_to_call = clazz.getMethod(ENQUEUE_YES_NO_SELECTION, new Class<?>[] { int.class });
            final GenericClass genericClass = new GenericClass(clazz);
            final GenericMethod genericMethod = new GenericMethod(method_to_call, genericClass);
            // adds JOptionPaneInputs.enqueueString() to the palette of
            // methods that can be used
            TestCluster.getInstance().addEnvironmentTestCall(genericMethod);
        } catch (SecurityException e) {
            logger.error("Error while handling Random: " + e.getMessage(), e);
        } catch (NoSuchMethodException e) {
            logger.error("Error while handling Random: " + e.getMessage(), e);
        }
    }
    if (!hasAddedJOptionPaneOkCancelSelection && JOptionPaneInputs.getInstance().hasDialog(GUIAction.OK_CANCEL_SELECTION)) {
        hasAddedJOptionPaneOkCancelSelection = true;
        try {
            final Class<?> clazz = JOptionPaneInputs.class;
            final String ENQUEUE_OK_CANCEL_SELECTION = "enqueueOkCancelSelection";
            final Method method_to_call = clazz.getMethod(ENQUEUE_OK_CANCEL_SELECTION, new Class<?>[] { int.class });
            final GenericClass genericClass = new GenericClass(clazz);
            final GenericMethod genericMethod = new GenericMethod(method_to_call, genericClass);
            // adds JOptionPaneInputs.enqueueString() to the palette of
            // methods that can be used
            TestCluster.getInstance().addEnvironmentTestCall(genericMethod);
        } catch (SecurityException e) {
            logger.error("Error while handling Random: " + e.getMessage(), e);
        } catch (NoSuchMethodException e) {
            logger.error("Error while handling Random: " + e.getMessage(), e);
        }
    }
    if (!hasAddedJOptionPaneOptionSelection && JOptionPaneInputs.getInstance().hasDialog(GUIAction.OPTION_SELECTION)) {
        hasAddedJOptionPaneOptionSelection = true;
        try {
            final Class<?> clazz = JOptionPaneInputs.class;
            final String ENQUEUE_OPTION_SELECTION = "enqueueOptionSelection";
            final Method method_to_call = clazz.getMethod(ENQUEUE_OPTION_SELECTION, new Class<?>[] { int.class });
            final GenericClass genericClass = new GenericClass(clazz);
            final GenericMethod genericMethod = new GenericMethod(method_to_call, genericClass);
            // adds JOptionPaneInputs.enqueueString() to the palette of
            // methods that can be used
            TestCluster.getInstance().addEnvironmentTestCall(genericMethod);
        } catch (SecurityException e) {
            logger.error("Error while handling Random: " + e.getMessage(), e);
        } catch (NoSuchMethodException e) {
            logger.error("Error while handling Random: " + e.getMessage(), e);
        }
    }
}
Also used : JOptionPaneInputs(org.evosuite.runtime.util.JOptionPaneInputs) GenericClass(org.evosuite.utils.generic.GenericClass) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod)

Example 8 with GenericClass

use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.

the class EnvironmentTestClusterAugmenter method addEnvironmentClassToCluster.

/**
 * Add the given klass to the test cluster. Also recursively add (as
 * modifiers) all the other EvoSuite classes for which the given class is a
 * generator
 *
 * @param klass
 */
private boolean addEnvironmentClassToCluster(Class<?> klass) {
    if (handledClasses.contains(klass.getCanonicalName()) || !TestClusterUtils.isEvoSuiteClass(klass)) {
        // already handled, or not valid
        return false;
    }
    handledClasses.add(klass.getCanonicalName());
    boolean excludeClass = klass.getAnnotation(EvoSuiteClassExclude.class) != null;
    for (Constructor c : klass.getConstructors()) {
        // first check if it should be skipped
        if (shouldSkip(excludeClass, c)) {
            continue;
        }
        GenericAccessibleObject gc = new GenericConstructor(c, klass);
        TestCluster.getInstance().addEnvironmentTestCall(gc);
        GenericClass genclass = new GenericClass(klass);
        TestCluster.getInstance().invalidateGeneratorCache(genclass);
        TestCluster.getInstance().addGenerator(genclass, gc);
        testClusterGenerator.addNewDependencies(Arrays.asList(c.getParameterTypes()));
    }
    for (Method m : klass.getMethods()) {
        if (shouldSkip(excludeClass, m)) {
            continue;
        }
        GenericAccessibleObject gm = new GenericMethod(m, klass);
        TestCluster.getInstance().addEnvironmentTestCall(gm);
        testClusterGenerator.addNewDependencies(Arrays.asList(m.getParameterTypes()));
        Class<?> returnType = m.getReturnType();
        if (!returnType.equals(Void.TYPE)) {
            GenericClass genclass = new GenericClass(returnType);
            TestCluster.getInstance().invalidateGeneratorCache(genclass);
            TestCluster.getInstance().addGenerator(genclass, gm);
            addEnvironmentDependency(returnType);
        }
    }
    return true;
}
Also used : GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) GenericClass(org.evosuite.utils.generic.GenericClass) Constructor(java.lang.reflect.Constructor) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod)

Example 9 with GenericClass

use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.

the class EnvironmentTestClusterAugmenter method handleNetwork.

private void handleNetwork(TestCase test) {
    /*
		 * there are several things that are mocked in the network. based on
		 * what the SUT used, we might only need a subset of methods used to
		 * manipulate the mocked network
		 */
    // TODO might need more stuff once we handle assertion generation
    test.getAccessedEnvironment().addLocalListeningPorts(VirtualNetwork.getInstance().getViewOfLocalListeningPorts());
    test.getAccessedEnvironment().addRemoteURLs(VirtualNetwork.getInstance().getViewOfRemoteAccessedFiles());
    test.getAccessedEnvironment().addRemoteContactedPorts(VirtualNetwork.getInstance().getViewOfRemoteContactedPorts());
    if (!hasAddedRemoteURLs && test.getAccessedEnvironment().getViewOfRemoteURLs().size() > 0) {
        hasAddedRemoteURLs = true;
        try {
            TestCluster.getInstance().addEnvironmentTestCall(new GenericMethod(NetworkHandling.class.getMethod("createRemoteTextFile", new Class<?>[] { EvoSuiteURL.class, String.class }), new GenericClass(NetworkHandling.class)));
        } catch (Exception e) {
            logger.error("Error while handling hasAddedRemoteURLs: " + e.getMessage(), e);
        }
    }
    boolean openedTCP = false;
    boolean openedUDP = false;
    for (EndPointInfo info : test.getAccessedEnvironment().getViewOfLocalListeningPorts()) {
        if (info.getType().equals(VirtualNetwork.ConnectionType.TCP)) {
            openedTCP = true;
        } else if (info.getType().equals(VirtualNetwork.ConnectionType.UDP)) {
            openedUDP = true;
        }
        if (openedTCP && openedUDP) {
            break;
        }
    }
    if (!hasAddedUdpSupport && openedUDP) {
        hasAddedUdpSupport = true;
        try {
            TestCluster.getInstance().addEnvironmentTestCall(new GenericMethod(NetworkHandling.class.getMethod("sendUdpPacket", new Class<?>[] { EvoSuiteLocalAddress.class, EvoSuiteRemoteAddress.class, byte[].class }), new GenericClass(NetworkHandling.class)));
            TestCluster.getInstance().addEnvironmentTestCall(new GenericMethod(NetworkHandling.class.getMethod("sendUdpPacket", new Class<?>[] { EvoSuiteLocalAddress.class, byte[].class }), new GenericClass(NetworkHandling.class)));
        } catch (Exception e) {
            logger.error("Error while handling hasAddedUdpSupport: " + e.getMessage(), e);
        }
    }
    if (!hasAddedTcpListeningSupport && openedTCP) {
        hasAddedTcpListeningSupport = true;
        try {
            TestCluster.getInstance().addEnvironmentTestCall(new GenericMethod(NetworkHandling.class.getMethod("sendDataOnTcp", new Class<?>[] { EvoSuiteLocalAddress.class, byte[].class }), new GenericClass(NetworkHandling.class)));
            TestCluster.getInstance().addEnvironmentTestCall(new GenericMethod(NetworkHandling.class.getMethod("sendMessageOnTcp", new Class<?>[] { EvoSuiteLocalAddress.class, String.class }), new GenericClass(NetworkHandling.class)));
        } catch (Exception e) {
            logger.error("Error while handling hasAddedTcpListeningSupport: " + e.getMessage(), e);
        }
    }
    if (!hasAddedTcpRemoteSupport && test.getAccessedEnvironment().getViewOfRemoteContactedPorts().size() > 0) {
        hasAddedTcpRemoteSupport = true;
        try {
            TestCluster.getInstance().addEnvironmentTestCall(new GenericMethod(NetworkHandling.class.getMethod("openRemoteTcpServer", new Class<?>[] { EvoSuiteRemoteAddress.class }), new GenericClass(NetworkHandling.class)));
        } catch (Exception e) {
            logger.error("Error while handling hasAddedTcpRemoteSupport: " + e.getMessage(), e);
        }
    }
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) EndPointInfo(org.evosuite.runtime.vnet.EndPointInfo) GenericMethod(org.evosuite.utils.generic.GenericMethod)

Example 10 with GenericClass

use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.

the class TestClusterGenerator method addCastClassDependencyIfAccessible.

private boolean addCastClassDependencyIfAccessible(String className, Set<String> blackList) {
    if (className.equals(java.lang.String.class.getName()))
        return true;
    if (blackList.contains(className)) {
        logger.info("Cast class in blacklist: " + className);
        return false;
    }
    try {
        Class<?> clazz = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(className);
        if (!TestUsageChecker.canUse(clazz)) {
            logger.debug("Cannot use cast class: " + className);
            return false;
        }
        // boolean added =
        addDependency(new GenericClass(clazz), 1);
        genericCastClasses.add(new GenericClass(clazz));
        concreteCastClasses.add(clazz);
        blackList.add(className);
        return true;
    } catch (ClassNotFoundException e) {
        logger.error("Problem for " + Properties.TARGET_CLASS + ". Class not found", e);
        blackList.add(className);
        return false;
    }
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass)

Aggregations

GenericClass (org.evosuite.utils.generic.GenericClass)144 Test (org.junit.Test)64 GenericMethod (org.evosuite.utils.generic.GenericMethod)40 Method (java.lang.reflect.Method)36 VariableReference (org.evosuite.testcase.variable.VariableReference)25 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)24 WildcardType (java.lang.reflect.WildcardType)22 TypeToken (com.googlecode.gentyref.TypeToken)20 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)17 Type (java.lang.reflect.Type)16 ArrayList (java.util.ArrayList)16 GenericAccessibleObject (org.evosuite.utils.generic.GenericAccessibleObject)16 MethodStatement (org.evosuite.testcase.statements.MethodStatement)15 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)15 ParameterizedType (java.lang.reflect.ParameterizedType)14 WildcardTypeImpl (org.evosuite.utils.generic.WildcardTypeImpl)10 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)9 AnnotatedType (java.lang.reflect.AnnotatedType)8 LinkedList (java.util.LinkedList)8 List (java.util.List)7