Search in sources :

Example 1 with UnitTest

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

the class TestExecuter method runTest.

public static boolean runTest(final String mainClass, final String testClass, boolean quiteMode) {
    try {
        if (quiteMode) {
            Display.init(new java.awt.Container());
        } else {
            Simulator.loadFXRuntime();
            System.setProperty("dskin", "/iphone3gs.skin");
            Display.init(null);
        }
        final Class mainCls = Class.forName(mainClass);
        Display.getInstance().callSeriallyAndWait(new Runnable() {

            public void run() {
                try {
                    Object main = mainCls.newInstance();
                    main.getClass().getMethod("init", Object.class).invoke(main, (Object) null);
                    main.getClass().getMethod("start").invoke(main);
                } catch (Exception err) {
                    failed = true;
                    TestReporting.getInstance().logException(err);
                }
            }
        });
        try {
            final UnitTest test = (UnitTest) Class.forName(testClass).newInstance();
            final int timeout = test.getTimeoutMillis();
            if (test.shouldExecuteOnEDT()) {
                Display.getInstance().callSeriallyAndWait(new Runnable() {

                    public void run() {
                        try {
                            TestReporting.getInstance().startingTestCase(test);
                            test.prepare();
                            TestReporting.getInstance().logMessage("Test prepared for execution on EDT");
                            failed = !test.runTest();
                            test.cleanup();
                        } catch (Exception err) {
                            failed = true;
                            TestReporting.getInstance().logException(err);
                        }
                    }
                }, timeout);
            } else {
                Timer timeoutKiller = new Timer();
                final Thread currentThread = Thread.currentThread();
                TimerTask timeoutTask = new TimerTask() {

                    public void run() {
                        TestReporting.getInstance().logMessage("Test timeout occured: " + timeout + " milliseconds");
                        failed = true;
                        currentThread.stop();
                    }
                };
                timeoutKiller.schedule(timeoutTask, timeout);
                TestReporting.getInstance().startingTestCase(test);
                test.prepare();
                TestReporting.getInstance().logMessage("Test prepared for execution off the EDT");
                failed = !test.runTest();
                test.cleanup();
                TestReporting.getInstance().finishedTestCase(test, !failed);
                timeoutTask.cancel();
            }
        } catch (Exception err) {
            failed = true;
            TestReporting.getInstance().logException(err);
        }
        Display.deinitialize();
        for (java.awt.Frame f : java.awt.Frame.getFrames()) {
            if (f != null && f.isShowing()) {
                f.dispose();
            }
        }
        return !failed;
    } catch (Throwable t) {
        t.printStackTrace();
    }
    return false;
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) UnitTest(com.codename1.testing.UnitTest)

Aggregations

UnitTest (com.codename1.testing.UnitTest)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1