Search in sources :

Example 1 with LineCoverageFactory

use of org.evosuite.coverage.line.LineCoverageFactory in project evosuite by EvoSuite.

the class ClassStatisticsPrinter method printClassStatistics.

/**
 * Identify all JUnit tests starting with the given name prefix, instrument
 * and run tests
 */
public static void printClassStatistics() {
    ExecutionTracer.disable();
    ExecutionTracer.setCheckCallerThread(false);
    try {
        DependencyAnalysis.analyzeClass(Properties.TARGET_CLASS, Arrays.asList(ClassPathHandler.getInstance().getClassPathElementsForTargetProject()));
        Sandbox.goingToExecuteSUTCode();
        TestGenerationContext.getInstance().goingToExecuteSUTCode();
        Sandbox.goingToExecuteUnsafeCodeOnSameThread();
        // Load SUT without initialising it
        Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
        if (targetClass != null) {
            LoggingUtils.getEvoLogger().info("* Finished analyzing classpath");
        } else {
            LoggingUtils.getEvoLogger().info("* Error while initializing target class, not continuing");
            return;
        }
        int publicMethods = 0;
        int nonpublicMethods = 0;
        int staticMethods = 0;
        int staticFields = 0;
        for (Method method : targetClass.getDeclaredMethods()) {
            if (method.getName().equals(ClassResetter.STATIC_RESET))
                continue;
            if (Modifier.isPublic(method.getModifiers())) {
                publicMethods++;
            } else {
                nonpublicMethods++;
            }
            if (Modifier.isStatic(method.getModifiers())) {
                LoggingUtils.getEvoLogger().info("Static: " + method);
                staticMethods++;
            }
        }
        for (Constructor<?> constructor : targetClass.getDeclaredConstructors()) {
            if (Modifier.isPublic(constructor.getModifiers())) {
                publicMethods++;
            } else {
                nonpublicMethods++;
            }
        }
        for (Field field : targetClass.getDeclaredFields()) {
            if (Modifier.isStatic(field.getModifiers())) {
                staticFields++;
            }
        }
        LoggingUtils.getEvoLogger().info("* Abstract: " + Modifier.isAbstract(targetClass.getModifiers()));
        LoggingUtils.getEvoLogger().info("* Public methods/constructors: " + publicMethods);
        LoggingUtils.getEvoLogger().info("* Non-Public methods/constructors: " + nonpublicMethods);
        LoggingUtils.getEvoLogger().info("* Static methods: " + staticMethods);
        LoggingUtils.getEvoLogger().info("* Inner classes: " + targetClass.getDeclaredClasses().length);
        LoggingUtils.getEvoLogger().info("* Total fields: " + targetClass.getDeclaredFields().length);
        LoggingUtils.getEvoLogger().info("* Static fields: " + staticFields);
        LoggingUtils.getEvoLogger().info("* Type parameters: " + targetClass.getTypeParameters().length);
    } catch (Throwable e) {
        LoggingUtils.getEvoLogger().error("* Error while initializing target class: " + (e.getMessage() != null ? e.getMessage() : e.toString()));
        return;
    } finally {
        Sandbox.doneWithExecutingUnsafeCodeOnSameThread();
        Sandbox.doneWithExecutingSUTCode();
        TestGenerationContext.getInstance().doneWithExecutingSUTCode();
    }
    LoggingUtils.getEvoLogger().info("* Subclasses: " + (TestCluster.getInheritanceTree().getSubclasses(Properties.TARGET_CLASS).size() - 1));
    LoggingUtils.getEvoLogger().info("* Superclasses/interfaces: " + (TestCluster.getInheritanceTree().getSuperclasses(Properties.TARGET_CLASS).size() - 1));
    LoggingUtils.getEvoLogger().info("* Lines of code: " + LinePool.getNumLines());
    LoggingUtils.getEvoLogger().info("* Methods without branches: " + BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getNumBranchlessMethods());
    LoggingUtils.getEvoLogger().info("* Total branch predicates: " + BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getBranchCounter());
    double complexity = 0.0;
    int maxComplexity = 0;
    for (Entry<String, RawControlFlowGraph> entry : GraphPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getRawCFGs(Properties.TARGET_CLASS).entrySet()) {
        int c = entry.getValue().getCyclomaticComplexity();
        if (c > maxComplexity)
            maxComplexity = c;
        complexity += c;
    // LoggingUtils.getEvoLogger().info("* Complexity of method "+entry.getKey()+": "+entry.getValue().getCyclomaticComplexity());
    }
    LoggingUtils.getEvoLogger().info("* Average cyclomatic complexity: " + (complexity / CFGMethodAdapter.getNumMethods(TestGenerationContext.getInstance().getClassLoaderForSUT())));
    LoggingUtils.getEvoLogger().info("* Maximum cyclomatic complexity: " + maxComplexity);
    StringBuilder allGoals = new StringBuilder();
    List<TestFitnessFactory<?>> factories = TestGenerationStrategy.getFitnessFactories();
    int numCriterion = 0;
    for (TestFitnessFactory<?> factory : factories) {
        List<TestFitnessFunction> goals = (List<TestFitnessFunction>) factory.getCoverageGoals();
        LoggingUtils.getEvoLogger().info("* Criterion " + Properties.CRITERION[numCriterion++] + ": " + goals.size());
        if (Properties.PRINT_GOALS) {
            if (factory instanceof LineCoverageFactory) {
                Collections.sort(goals, new Comparator<TestFitnessFunction>() {

                    @Override
                    public int compare(TestFitnessFunction l1, TestFitnessFunction l2) {
                        return Integer.compare(((LineCoverageTestFitness) l1).getLine(), ((LineCoverageTestFitness) l2).getLine());
                    }
                });
            }
            for (TestFitnessFunction goal : goals) {
                allGoals.append(goal.toString() + java.lang.System.getProperty("line.separator"));
            }
        }
    }
    if (allGoals.length() > 0 && Properties.PRINT_GOALS) {
        if (Properties.WRITE_ALL_GOALS_FILE) {
            FileIOUtils.writeFile(allGoals.toString(), Properties.ALL_GOALS_FILE);
        } else {
            LoggingUtils.getEvoLogger().info(allGoals.toString());
        }
    }
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) Method(java.lang.reflect.Method) RawControlFlowGraph(org.evosuite.graphs.cfg.RawControlFlowGraph) Field(java.lang.reflect.Field) List(java.util.List) LineCoverageFactory(org.evosuite.coverage.line.LineCoverageFactory) LineCoverageTestFitness(org.evosuite.coverage.line.LineCoverageTestFitness)

Aggregations

Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 LineCoverageFactory (org.evosuite.coverage.line.LineCoverageFactory)1 LineCoverageTestFitness (org.evosuite.coverage.line.LineCoverageTestFitness)1 RawControlFlowGraph (org.evosuite.graphs.cfg.RawControlFlowGraph)1 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)1