Print

Print


Commit in lcsim/sandbox on MAIN
CTest.java+341added 1.1
save a copy of this in sandbox in case it is needed

lcsim/sandbox
CTest.java added at 1.1
diff -N CTest.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ CTest.java	30 Nov 2010 22:21:33 -0000	1.1
@@ -0,0 +1,341 @@
+package ctbuild;
+import java.io.*;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.StringTokenizer;
+/** A program to perform Java class component tests in association with an ant build.
+ * The packages themselves should have been built and bundled into the distJar jar file.
+ * This program looks for a file called packages.txt which lists the packages
+ * to test. Each package (kept in the componentTests subdirectory) needs to have a COMPONENTS
+ * file, which lists the Java classes to be tested. The classes should be listed by
+ * name (ClassName without the .java * suffix) in dependency order, with one class
+ * per line. Leading # indicates a comment and the rest of the line is ignored.
+ * For each class in turn, the corresponding component test ClassName_t.java is compiled and
+ * run. It is expected that the test class return 0 upon succesful completion. If the
+ * System returns 0, the component test is assumed to have completed successfuly and
+ * the next class is then compiled and tested. Output from the test program can be found
+ * in the directory ctests/packagename/ClassName. The file is named ok.txt if the class
+ * passes, or fail.txt if there is an error.
+ *
+ *
+ *@author Norman A. Graf
+ *@version 1.0
+ *
+ */
+public class CTest
+{
+    public CTest(String userdir, String distJar)
+    {
+        _distJar = distJar;
+        
+        System.out.println("Testing build release "+distJar+". Please be patient...");
+        //        String userdir = System.getProperty("user.dir");
+        _basedir = userdir;
+        //        _jamaPath = "C:/Jama/dist/Jama-1.0.jar";
+        
+        System.out.println("User Directory="+_basedir);
+        System.out.println("Will process the following packages:");
+        List packages = readPackagesFile();
+        
+        Iterator it = packages.iterator();
+        // first create the directories for the test classes...
+        while(it.hasNext())
+        {
+            String myPackage = (String) it.next();
+            System.out.println("\n \n Testing "+myPackage+"...");
+            
+            String testDir = _basedir+"/ctests/"+myPackage;
+            createDir(testDir);
+            compileTests(myPackage);
+            runTests(myPackage);
+        }
+    }
+    
+    private String _basedir;
+    private String _distJar;
+    //    private String _jamaPath;
+    
+    private String createTemporaryDir()
+    {
+        // create new directory called tmp under package directory for results of testing
+        String strNewDir = System.getProperty("user.dir") +
+                java.io.File.separator + "tmp";
+        java.io.File dir = new java.io.File(strNewDir);
+        dir.mkdir();
+        return strNewDir;
+    }
+    
+    private boolean createDir(String dirname)
+    {
+        java.io.File dir = new java.io.File(dirname);
+        //        System.out.println("want to create directory: "+dir);
+        if(dir.exists())
+        {
+            return true;
+        }
+        else
+        {
+            // break up the directory we want to create into subdirectories
+            StringTokenizer st = new StringTokenizer(dirname,"/");
+            List dirs = new ArrayList();
+            while(st.hasMoreTokens())
+            {
+                String nextDir = st.nextToken();
+                //                System.out.println(nextDir);
+                dirs.add(nextDir);
+                //              System.out.println("Creating directory: "+dir);
+            }
+            // now create new directories as needed...
+            Iterator it = dirs.iterator();
+            String dirNameToCreate="";
+            while(it.hasNext())
+            {
+                dirNameToCreate += ((String)it.next()+dir.separator );
+                File dirToCreate = new File(dirNameToCreate);
+                //                System.out.println(dirNameToCreate+ " "+dirToCreate.exists());
+                if(!dirToCreate.exists())
+                {
+                    //                    System.out.println(dirToCreate+" does not exist. Creating it.");
+                    dirToCreate.mkdir();
+                }
+                
+            }
+            return true;
+            //        return dir.mkdir();
+        }
+    }
+    
+    private synchronized void deleteTemporaryDir(String strTempDir)
+    {
+        java.io.File dir = new java.io.File(strTempDir);
+        if (dir.exists())
+        {
+            dir.delete();
+        }
+    }
+    
+    private List readPackagesFile()
+    {
+        List list = new ArrayList();
+        String packages = _basedir+"/packages.txt";
+        String thisLine;
+        try
+        {
+            FileInputStream fin =  new FileInputStream(packages);
+            BufferedReader myInput = new BufferedReader
+                    (new InputStreamReader(fin));
+            while ((thisLine = myInput.readLine()) != null)
+            {
+                
+                if(!thisLine.startsWith("#"))
+                {
+                    System.out.println(thisLine);
+                    list.add(thisLine);
+                }
+            }
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+        return list;
+    }
+    
+    private List readComponentsFile(String packageDir)
+    {
+        List list = new ArrayList();
+        // Open the file COMPONENTS to get the list of files to process
+        FileInputStream fs=null;
+        String component;
+        try
+        {
+            fs = new FileInputStream(packageDir+java.io.File.separator+"COMPONENTS");
+        }
+        catch (Exception e)
+        {
+            System.out.println(e);
+            System.exit(1);
+        }
+        
+        BufferedReader ds = new BufferedReader(new InputStreamReader(fs));
+        reread:
+            while (true)
+            {
+            try
+            {
+                component = ds.readLine();        // read 1 line
+                if (component == null) break;
+                //Tokenize the line to check for leading blanks or more than one component per line
+                StringTokenizer st = new StringTokenizer(component);
+                int ntokens = st.countTokens();
+                if(ntokens==0 || ntokens>1) continue reread; //blank line or too many components
+                String name = st.nextToken();
+                if(name.startsWith("#")) continue reread; //Comment;
+                component = name; // name should now have no leading or internal spaces, should be a valid component name
+                list.add(component);
+                // loop over files in COMPONENTS
+            }
+            catch (IOException e)
+            {
+                System.out.println(e);
+                break;
+            }
+            }
+        return list;
+    }
+    
+    private void compileTests(String myPackage)
+    {
+        //        String packageDir = _basedir+"/src/"+myPackage;
+        String packageDir = _basedir+"/componentTests/"+myPackage;
+        System.out.println("Compiling tests for "+myPackage);
+        System.out.println(packageDir);
+        List components = readComponentsFile(packageDir);
+        Iterator it = components.iterator();
+        while(it.hasNext())
+        {
+            String component = (String) it.next();
+            System.out.println("Compiling "+component);
+            int returncode=0;
+            // Compile the test components here...
+            try
+            {
+                // classpath is the jtf distribution plus whatever test classes are needed in the package
+                
+                //                String cp = " -classpath " + " \"" +_jamaPath+";dist/"+_distJar+";src/"+myPackage+"\"";
+                // shouldn't need the mypackage subdirectory.
+                //                String cp = " -classpath " + " \" C:/j2sdk1.4.2_04/jre/lib/rt.jar;" +_jamaPath+";dist/"+_distJar+"\"";
+                String cp = " -classpath " + " \""+_distJar+"\"";
+                String compileLine ="javac -d "+"ctests/"+ myPackage + cp+ " "+_basedir+"/componentTests/"+myPackage+"/";
+                //                String compileLine ="jikes -d "+"tests/"+ myPackage + cp+ " src/"+myPackage+"/";
+                compileLine+=component+"_t.java";
+                System.out.println(compileLine);
+                
+                String line;
+                Process p = Runtime.getRuntime().exec(compileLine);
+                BufferedReader input =
+                        new BufferedReader
+                        (new InputStreamReader(p.getInputStream()));
+                while ((line = input.readLine()) != null)
+                {
+                    System.out.println(line);
+                }
+                p.waitFor();
+                returncode = p.exitValue();
+                System.out.println("Compilation return code: "+ p.exitValue());
+                input.close();
+                if (returncode != 0)
+                {
+                    System.out.println("Build stopped!!! \n Problem with compiling component "+component);
+                    //should really throw an exception here...
+                    System.exit(0);
+                }
+            }
+            catch (Exception err)
+            {
+                err.printStackTrace();
+            }
+        }
+        
+    }
+    
+    
+    private void runTests(String myPackage)
+    {
+        String packageDir = _basedir+"/componentTests/"+myPackage;
+        System.out.println("Running tests for "+myPackage);
+        String testDir = _basedir+"/ctests/"+myPackage;
+        
+        // get the list of components for this package...
+        List components = readComponentsFile(packageDir);
+        Iterator it = components.iterator();
+        // get a PrintStream so we can direct informational output to the screen
+        PrintStream screen = System.out;
+        
+        // iterate over the components
+        while(it.hasNext())
+        {
+            String component = (String) it.next();
+            // create a temporary subdirectory for this component
+            String componentdir = testDir+"/"+component;
+            String line;
+            boolean ok = createDir(componentdir);
+            
+            // now run the component test program...
+            try
+            {
+                // Create a log file for this component by redirecting System.out
+                String fail = componentdir+"/fail.txt";
+                String okfile = componentdir+"/ok.txt";
+                PrintStream out = new PrintStream(
+                        new BufferedOutputStream(
+                        new FileOutputStream(fail)));
+                System.out.println("Testing "+component);
+                //                String cp = " -classpath " + "\""+_jamaPath+";dist/"+_distJar+";tests/"+myPackage+"\"";
+                String cp = " -classpath " + "\""+_distJar+";ctests/"+myPackage+"\"";
+                String cmdline = "\njava "+cp + " " + component+"_t";
+                System.out.println(cmdline);
+                
+                screen.print("   Testing " + component);
+                
+                // send output to the file fail.txt...
+                System.setOut(out);
+                System.setErr(out);
+                
+                // run the test...
+                Process p = Runtime.getRuntime().exec(cmdline);
+                
+                // capture the output to the file fail.txt...
+                BufferedReader input =
+                        new BufferedReader
+                        (new InputStreamReader(p.getInputStream()));
+                while ((line = input.readLine()) != null)
+                {
+                    System.out.println(line);
+                }
+                p.waitFor();
+                int exitValue = p.exitValue();
+                System.out.println("Test return code: "+ exitValue);
+                input.close();
+                // If exitValue is zero, everything is OK, rename fail.txt to ok.txt
+                out.close();
+                File f = new File(fail);
+                File o = new File(okfile);
+                //Does ok.txt already exist? (Presumably from an earlier run)
+                // If so, delete.
+                if(o.exists()) o.delete();
+                if ( f.exists() && exitValue==0)
+                {
+                    System.out.println(f.renameTo(o));
+                    screen.println(". Output in "+okfile);
+                }
+                //
+                //out.close(); // Close the temporary log file for this Component
+                System.setOut(screen); // redirect output to the screen
+                if (exitValue != 0)
+                {
+                    System.out.println("\n*Build stopped!!! \n  Problem with component "+component);
+                    screen.println("\n*Build stopped!!! \n  Problem with component "+component);
+                    screen.println("   Output in "+fail);
+                    // should throw an excpetion here...
+                    System.exit(0);
+                }
+            }
+            catch (Exception err)
+            {
+                err.printStackTrace();
+            }
+        }
+    }
+    
+    public static void main(String argv[])
+    {
+        //String jarFile = "jtf-1.0.jar";
+        //if(argv.length>0) jarFile = argv[0];
+        String userdir = argv[0];
+        String jarFile = argv[1];
+        new CTest(userdir,jarFile);
+    }
+}
+
CVSspam 0.2.8