Search in sources :

Example 1 with ParameterisedTestCase

use of org.kanonizo.framework.objects.ParameterisedTestCase in project kanonizo by kanonizo.

the class Framework method loadClasses.

public void loadClasses() throws ClassNotFoundException {
    sut = new SystemUnderTest();
    List<File> sourceFiles = findClasses(sourceFolder);
    Premain.instrument = true;
    for (File file : sourceFiles) {
        Class<?> cl = loadClassFromFile(file);
        sut.addClass(new ClassUnderTest(cl));
        logger.info("Added class " + cl.getName());
    }
    Premain.instrument = false;
    List<File> testFiles = findClasses(testFolder);
    for (File file : testFiles) {
        Class<?> cl = loadClassFromFile(file);
        if (cl != null) {
            if (Util.isTestClass(cl)) {
                List<Method> testMethods = TestingUtils.getTestMethods(cl);
                logger.info("Adding " + testMethods.size() + " test methods from " + cl.getName());
                for (Method m : testMethods) {
                    if (TestingUtils.isParameterizedTest(cl, m)) {
                        Optional<Method> parameterMethod = Arrays.asList(cl.getMethods()).stream().filter(method -> method.getAnnotation(Parameters.class) != null).findFirst();
                        if (parameterMethod.isPresent()) {
                            try {
                                Iterable<Object[]> parameters = (Iterable<Object[]>) parameterMethod.get().invoke(null, new Object[] {});
                                for (Object[] inst : parameters) {
                                    ParameterisedTestCase ptc = new ParameterisedTestCase(cl, m, inst);
                                    sut.addTestCase(ptc);
                                }
                            } catch (IllegalAccessException e) {
                                logger.error(e);
                            } catch (InvocationTargetException e) {
                                logger.error(e);
                            }
                        } else {
                            logger.error("Trying to create parameterized test case that has no parameter method");
                        }
                    } else {
                        TestCase t = new TestCase(cl, m);
                        sut.addTestCase(t);
                    }
                }
            } else {
                sut.addExtraClass(cl);
                logger.info("Adding supporting test class " + cl.getName());
            }
        }
    }
    ClassUnderTest.resetCount();
    logger.info("Finished adding source and test files. Total " + sut.getClassesUnderTest().size() + " classes and " + sut.getTestSuite().size() + " test cases");
}
Also used : Arrays(java.util.Arrays) Reflections(org.reflections.Reflections) GsonBuilder(com.google.gson.GsonBuilder) TypeAdapter(com.google.gson.TypeAdapter) ClassUnderTest(org.kanonizo.framework.objects.ClassUnderTest) TestCase(org.kanonizo.framework.objects.TestCase) APLCFunction(org.kanonizo.algorithms.metaheuristics.fitness.APLCFunction) CoverageWriter(org.kanonizo.reporting.CoverageWriter) Gson(com.google.gson.Gson) Method(java.lang.reflect.Method) TestCaseSelectionListener(org.kanonizo.listeners.TestCaseSelectionListener) ParameterisedTestCase(org.kanonizo.framework.objects.ParameterisedTestCase) SearchAlgorithm(org.kanonizo.algorithms.SearchAlgorithm) TestingUtils(org.kanonizo.junit.TestingUtils) Expose(com.google.gson.annotations.Expose) Set(java.util.Set) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) Serializable(java.io.Serializable) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) Util(org.kanonizo.util.Util) Logger(org.apache.logging.log4j.Logger) PropertyChangeListener(java.beans.PropertyChangeListener) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) Display(org.kanonizo.display.Display) InstrumentedFitnessFunction(org.kanonizo.algorithms.metaheuristics.fitness.InstrumentedFitnessFunction) APBCFunction(org.kanonizo.algorithms.metaheuristics.fitness.APBCFunction) Parameters(org.junit.runners.Parameterized.Parameters) Parameter(com.scythe.instrumenter.InstrumentationProperties.Parameter) ClassParser(org.apache.bcel.classfile.ClassParser) JsonReader(com.google.gson.stream.JsonReader) APFDFunction(org.kanonizo.algorithms.metaheuristics.fitness.APFDFunction) ArrayList(java.util.ArrayList) CsvWriter(org.kanonizo.reporting.CsvWriter) Instrumenter(org.kanonizo.framework.instrumentation.Instrumenter) Prerequisite(org.kanonizo.annotations.Prerequisite) JsonWriter(com.google.gson.stream.JsonWriter) PropertyChangeEvent(java.beans.PropertyChangeEvent) MutationSearchAlgorithm(org.kanonizo.algorithms.MutationSearchAlgorithm) JavaClass(org.apache.bcel.classfile.JavaClass) FitnessFunction(org.kanonizo.algorithms.metaheuristics.fitness.FitnessFunction) MiscStatsWriter(org.kanonizo.reporting.MiscStatsWriter) Iterator(java.util.Iterator) NullInstrumenter(org.kanonizo.instrumenters.NullInstrumenter) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Algorithm(org.kanonizo.annotations.Algorithm) TestCaseOrderingWriter(org.kanonizo.reporting.TestCaseOrderingWriter) File(java.io.File) SystemUnderTest(org.kanonizo.framework.objects.SystemUnderTest) PropertyChangeSupport(java.beans.PropertyChangeSupport) FileReader(java.io.FileReader) NullDisplay(org.kanonizo.display.NullDisplay) Comparator(java.util.Comparator) LogManager(org.apache.logging.log4j.LogManager) Parameters(org.junit.runners.Parameterized.Parameters) ParameterisedTestCase(org.kanonizo.framework.objects.ParameterisedTestCase) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) TestCase(org.kanonizo.framework.objects.TestCase) ParameterisedTestCase(org.kanonizo.framework.objects.ParameterisedTestCase) SystemUnderTest(org.kanonizo.framework.objects.SystemUnderTest) File(java.io.File) ClassUnderTest(org.kanonizo.framework.objects.ClassUnderTest)

Example 2 with ParameterisedTestCase

use of org.kanonizo.framework.objects.ParameterisedTestCase in project kanonizo by kanonizo.

the class JUnit4TestRunner method getRequest.

private Request getRequest(TestCase tc) {
    Class<?> testClass = tc.getTestClass();
    Method testMethod = tc.getMethod();
    try {
        final RunWith runWith = testClass.getAnnotation(RunWith.class);
        if (runWith != null) {
            final Class<? extends Runner> runnerClass = runWith.value();
            if (runnerClass.isAssignableFrom(Parameterized.class)) {
                try {
                    if (tc instanceof ParameterisedTestCase) {
                        ParameterisedTestCase ptc = (ParameterisedTestCase) tc;
                        Class.forName(// ignore IgnoreIgnored for junit4.4 and <
                        "org.junit.runners.BlockJUnit4ClassRunner");
                        return Request.runner(new ParameterizedMethodRunner(testClass, testMethod.getName(), ptc.getParameters()));
                    }
                } catch (Throwable thrown) {
                    logger.error(thrown);
                }
            }
        } else {
            if (testMethod != null && testMethod.getAnnotation(Ignore.class) != null) {
                // override ignored case only
                final Request classRequest = new ClassRequest(testClass) {

                    public Runner getRunner() {
                        try {
                            return new IgnoreIgnoredTestJUnit4ClassRunner(testClass);
                        } catch (Exception ignored) {
                        }
                        return super.getRunner();
                    }
                };
                return classRequest.filterWith(Description.createTestDescription(testClass, testMethod.getName()));
            }
        }
    } catch (Exception ignored) {
        logger.error(ignored);
    }
    return Request.method(testClass, testMethod.getName());
}
Also used : ParameterisedTestCase(org.kanonizo.framework.objects.ParameterisedTestCase) Request(org.junit.runner.Request) ClassRequest(org.junit.internal.requests.ClassRequest) Method(java.lang.reflect.Method) FrameworkMethod(org.junit.runners.model.FrameworkMethod) RunWith(org.junit.runner.RunWith) ClassRequest(org.junit.internal.requests.ClassRequest) AssumptionViolatedException(org.junit.AssumptionViolatedException)

Aggregations

Method (java.lang.reflect.Method)2 ParameterisedTestCase (org.kanonizo.framework.objects.ParameterisedTestCase)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 TypeAdapter (com.google.gson.TypeAdapter)1 Expose (com.google.gson.annotations.Expose)1 JsonReader (com.google.gson.stream.JsonReader)1 JsonWriter (com.google.gson.stream.JsonWriter)1 Parameter (com.scythe.instrumenter.InstrumentationProperties.Parameter)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 PropertyChangeSupport (java.beans.PropertyChangeSupport)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Modifier (java.lang.reflect.Modifier)1