Package com.nexirius.util.unittest

Source Code of com.nexirius.util.unittest.UnitTester$UnitTestProgressImpl

//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.util.unittest;

import com.nexirius.util.StringVector;
import com.nexirius.util.XFile;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class UnitTester {

    java.util.Vector mUnitTests = new java.util.Vector();
    UnitTestFrame m_win = null;
    UnitTestProgressImpl m_progress = new UnitTestProgressImpl();

    class UnitTestProgressImpl implements UnitTestProgress {
        public void addClassToList(UnitTest c) {
            if (m_win != null) {
                m_win.addClassToList(c);
            }
        }

        public void setCurrentUnit(UnitTest c) {
            if (m_win == null)
                System.out.println("Now testing : " + c);
            else {
                m_win.setCurrentUnit(c);
            }
        }

        public void progress(String msg) {
            if (m_win == null)
                System.out.println(msg);
            else {
                m_win.progress(msg);
            }
        }

        public void reportFailure(String why) {
            if (m_win == null)
                System.out.println(why);
            else {
                m_win.reportFailure(why);
            }
        }


        public void passed(boolean res) {
            m_win.passed(res);
        }
    }


    public static void main(String[] args) {
        UnitTester ut = new UnitTester(args);
        ut.runTests();
    }

    public UnitTester(String[] args) {
        buildGui();
        m_win.enableExitButton(false);
        findAllUnitTests(args);
    }

    public void findAllUnitTests(String[] args) {
        StringVector allFiles = new StringVector();

        // Find all classes specified on the command line
        for (int i = 0; i < args.length; i++) {
            XFile files = new XFile(args[i]);
            if (files.isDirectory()) {
                if (!args[i].endsWith(java.io.File.separator))
                    files = new XFile(args[i] + java.io.File.separator);

                StringVector mostFiles = files.getAllFiles(true);

                String s = mostFiles.firstItem();

                while (s != null) {
                    m_win.tickle();
                    if (s.endsWith(".class")) {
                        s = files.getParent() + java.io.File.separator + s.substring(0, s.indexOf(".class"));
                        s = s.replace(java.io.File.separatorChar, '.');
                        allFiles.append(s);
                    }
                    s = mostFiles.nextItem();
                }
            } else {
                if (args[i].endsWith(".class")) {
                    String s1 = files.getName();
                    String s2 = files.getParent() + java.io.File.separator + s1.substring(0, s1.indexOf(".class"));
                    s2 = s2.replace(java.io.File.separatorChar, '.');
                    allFiles.append(s2);
                }
            }
        }
       
        // Now try guessing the packaging for these files
        // using the first file as a prototype
        int stripOptimiser = 0;

        String aFile = allFiles.firstItem();
        while (aFile != null) {
            // Try an initial strip optimisation
            String optFile = aFile;
            Class aClass = null;
            int nextPos = 0; // dodgy start value...

            if (stripOptimiser != 0) {
                try {
                    optFile = optFile.substring(stripOptimiser);
                    m_win.tickle();
                    aClass = Class.forName(optFile);
                } catch (Exception cnfe) {
                    stripOptimiser = 0;
                }
            }
            if (stripOptimiser == 0) {
                while (nextPos >= 0) {
                    try {
                        m_win.tickle();
                        aClass = Class.forName(aFile);
                        nextPos = -1;
                    } catch (Exception cnfe) {
                        nextPos = aFile.indexOf('.');
                        stripOptimiser += nextPos + 1;
                        aFile = aFile.substring(nextPos + 1);
                    }
                }
            }

            try {
                if (aClass != null && UnitTest.class.isAssignableFrom(aClass)) {
                    if (!aClass.equals(UnitTest.class) && !aClass.equals(UnitTestImpl.class)) {
                        mUnitTests.addElement(aClass.newInstance());
                    }
                } else if (aClass != null && UnitTestable.class.isAssignableFrom(aClass)) {
                    if (!aClass.equals(UnitTestable.class)) {
                        mUnitTests.addElement(new UnitTestImpl((UnitTestable) aClass.newInstance()));
                    }
                }
            } catch (Exception e) {
                System.out.println("aClass = " + aClass);
                e.printStackTrace()//TODO
            }

            nextPos = -1;
            aFile = allFiles.nextItem();
        }
    }

    public void runTests() {
        java.util.Enumeration test = mUnitTests.elements();
        java.util.Vector testInstances = new java.util.Vector();

        while (test.hasMoreElements()) {
            try {
                UnitTest unitTest = (UnitTest) test.nextElement();
                m_progress.addClassToList(unitTest);
                testInstances.addElement(unitTest);
            } catch (Exception e) {
                System.out.print(e);
                e.printStackTrace();
            }
        }

        test = testInstances.elements();
        while (test.hasMoreElements()) {
            try {
                UnitTest aTest =
                        (UnitTest) test.nextElement();
                m_progress.setCurrentUnit(aTest);
                aTest.setUnitTestProgress(m_progress);
                aTest.performUnitTest();
                m_progress.passed(aTest.passed());
                Runtime.getRuntime().gc();
                Runtime.getRuntime().runFinalization();
                Runtime.getRuntime().gc();
                Runtime.getRuntime().runFinalization();
            } catch (Exception e) {
                System.out.print(e);
                e.printStackTrace();
            }
        }
        m_win.enableExitButton(true);
    }

    public void buildGui() {
        m_win = new UnitTestFrame();
        m_win.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        m_win.show();
    }

}
TOP

Related Classes of com.nexirius.util.unittest.UnitTester$UnitTestProgressImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.