Search in sources :

Example 1 with JUnitXMLReporting

use of com.codename1.testing.JUnitXMLReporting in project CodenameOne by codenameone.

the class TestRunner method init.

private void init(String[] argv) {
    try {
        if (argv[0].startsWith("-") || argv[0].startsWith("/")) {
            printUsage();
            return;
        }
        try {
            mainClass = argv[0];
            int pos = 1;
            while (pos < argv.length) {
                String s = argv[pos];
                if (s.equalsIgnoreCase("-testCases")) {
                    pos++;
                    testCases = argv[pos].split(",");
                    pos++;
                    continue;
                }
                if (s.equalsIgnoreCase("-skins")) {
                    pos++;
                    skins = argv[pos].split(",");
                    pos++;
                    continue;
                }
                if (s.equalsIgnoreCase("-quietMode")) {
                    quietMode = true;
                    pos++;
                    continue;
                }
                if (s.equalsIgnoreCase("-junitXML")) {
                    TestReporting.setInstance(new JUnitXMLReporting());
                }
                if (s.equalsIgnoreCase("-cleanMode")) {
                    cleanMode = true;
                    pos++;
                    continue;
                }
                System.out.println("Unrecognized argument: " + s);
                printUsage();
                System.exit(1);
                return;
            }
        } catch (Exception err) {
            err.printStackTrace();
            printUsage();
            return;
        }
        String[] tests;
        if (testCases == null || testCases.length == 0) {
            InputStream is = getClass().getResourceAsStream("/tests.dat");
            if (is == null) {
                System.err.println("Test data not found in the file, make sure the ant task was executed in full");
                System.exit(2);
                return;
            }
            DataInputStream di = new DataInputStream(is);
            int version = di.readInt();
            if (version > VERSION) {
                System.err.println("Tests were built with a new version of Codename One and can't be executed with this runner");
                System.exit(4);
                return;
            }
            tests = new String[di.readInt()];
            for (int iter = 0; iter < tests.length; iter++) {
                tests[iter] = di.readUTF();
            }
            di.close();
        } else {
            tests = testCases;
        }
        System.out.println("Preparing to execute " + tests.length + " tests");
        StringTokenizer t = new StringTokenizer(System.getProperty("java.class.path"), File.pathSeparator);
        File[] files = new File[t.countTokens()];
        for (int iter = 0; iter < files.length; iter++) {
            files[iter] = new File(t.nextToken());
        }
        int passedTests = 0;
        int failedTests = 0;
        if (cleanMode) {
            for (String currentTestClass : tests) {
                ClassLoader ldr = new ClassPathLoader(files);
                Class c = Class.forName("com.codename1.impl.javase.TestExecuter", true, ldr);
                Method m = c.getDeclaredMethod("runTest", String.class, String.class, Boolean.TYPE);
                Boolean passed = (Boolean) m.invoke(null, mainClass, currentTestClass, quietMode);
                if (passed.booleanValue()) {
                    passedTests++;
                } else {
                    failedTests++;
                    if (stopOnFail) {
                        System.exit(100);
                        return;
                    }
                }
            }
        } else {
            ClassLoader ldr = new ClassPathLoader(files);
            Class c = Class.forName("com.codename1.impl.javase.TestExecuter", true, ldr);
            for (String currentTestClass : tests) {
                Method m = c.getDeclaredMethod("runTest", String.class, String.class, Boolean.TYPE);
                Boolean passed = (Boolean) m.invoke(null, mainClass, currentTestClass, quietMode);
                if (passed.booleanValue()) {
                    System.out.println(currentTestClass + " passed!");
                    passedTests++;
                } else {
                    System.out.println(currentTestClass + " failed!");
                    failedTests++;
                    if (stopOnFail) {
                        System.exit(100);
                        return;
                    }
                }
            }
        }
        TestReporting.getInstance().testExecutionFinished();
        if (failedTests > 0) {
            System.out.println("Test execution finished, some failed tests occured. Passed: " + passedTests + " tests. Failed: " + failedTests + " tests.");
        } else {
            System.out.println("All tests passed. Total " + passedTests + " tests passed");
        }
        System.exit(0);
    } catch (Exception ex) {
        ex.printStackTrace();
        System.exit(3);
    }
}
Also used : DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) Method(java.lang.reflect.Method) DataInputStream(java.io.DataInputStream) StringTokenizer(java.util.StringTokenizer) JUnitXMLReporting(com.codename1.testing.JUnitXMLReporting) File(java.io.File)

Aggregations

JUnitXMLReporting (com.codename1.testing.JUnitXMLReporting)1 DataInputStream (java.io.DataInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 Method (java.lang.reflect.Method)1 StringTokenizer (java.util.StringTokenizer)1