runtest.java

来自「Pegasus is an open-source implementation」· Java 代码 · 共 867 行 · 第 1/2 页

JAVA
867
字号
                  if (DEBUG)                  {                     System.err.println ("findTests: ZIPFILE: " + szName);                  }                  szMatchName = MyFilenameFilter.matches (szName,                                                          patternStart,                                                          patternEnd);                  if (szMatchName != null)                  {                     if (DEBUG)                     {                        System.err.println ("findTests: found zip class: "+szMatchName);                     }                     addTestEntry (h,                                   szMatchName,                                   new TestEntry (szMatchName,                                                  TestEntry.ZIPFILE,                                                  ze,                                                  patternEnd));                  }               }            }            catch (IOException e)            {               System.err.println ("findTests: Caught " + e);               e.printStackTrace ();            }         }         else if (szLowerItem.endsWith (".jar"))         {            if (DEBUG)            {               System.err.println ("findTests: JAR: " + szCurrentItem);            }            try            {               JarFile jf = new JarFile (szCurrentItem);               for (Enumeration e = jf.entries (); e.hasMoreElements (); )               {                  JarEntry je          = (JarEntry)e.nextElement ();                  String   szName      = je.getName ();                  String   szMatchName;                  if (DEBUG)                  {                     System.err.println ("findTests: JARFILE: " + szName);                  }                  szMatchName = MyFilenameFilter.matches (szName,                                                          patternStart,                                                          patternEnd);                  if (szMatchName != null)                  {                     if (DEBUG)                     {                        System.err.println ("findTests: found jar class: "+szMatchName);                     }                     addTestEntry (h,                                   szMatchName,                                   new TestEntry (szMatchName,                                                  TestEntry.JARFILE,                                                  je,                                                  patternEnd));                  }               }            }            catch (IOException e)            {               System.err.println ("findTests: Caught " + e);               e.printStackTrace ();            }         }         else         {            if (DEBUG)            {               System.err.println ("findTests: FILE: " + szCurrentItem);            }            // Search through the subdirectory for files matching MyFilenameFilter ()            recurseSubdirectories (new File (szCurrentItem), "", h);         }      } while (0 <= iSeparator);      return h;   }   private void recurseSubdirectories (File file, String baseName, Hashtable h)   {      if (file.isDirectory ())      {         String[] files = file.list ();         for (int i = 0; i < files.length; i++)         {            File subdir = new File (file.getPath () + File.separator + files[i]);            if (subdir.isDirectory ())            {               String newBaseName;               if (baseName.length () == 0)               {                  newBaseName = files[i];               }               else               {                  newBaseName = baseName + File.separator + files[i];               }               recurseSubdirectories (subdir, newBaseName, h);            }         }         files = file.list (new MyFilenameFilter (patternStart, patternEnd));         if (DEBUG)         {            System.err.print ("recurseSubdirectories: " + baseName + ", {");            for (int i = 0; i < files.length; i++)            {               System.err.print (files[i]);               if (i < files.length - 1)               {                  System.err.print (",");               }            }            System.err.println ("}");         }         for (int i = 0; i < files.length; i++)         {            // The submenu name will be the name of the java class file without the            // .class part            String szMatchName     = new String (files[i].substring (0,                                                                     files[i].length ()                                                                     - patternEnd.length ()));            String szFullClassName = baseName + "." + szMatchName;            if (DEBUG)            {               System.err.println ("recurseSubdirectories: found class: " + szFullClassName);            }            addTestEntry (h,                          szMatchName,                          new TestEntry (szMatchName,                                         TestEntry.FILE,                                         szFullClassName,                                         patternEnd));         }      }   }   /**    * This will add an individual test entry to the list of test    * entries.  It will do some validation that the class support all    * of the APIs that we need it to support.    *    * @param  ht      This is the hash table to add into.    * @param  szKey   This is the key to set for the hash table entry.    * @param  de      This is the value to set for the hash table entry.    * @return boolean The success return code.    */   private boolean addTestEntry (Hashtable ht, String szKey, TestEntry de)   {      String szClassName;      Class  loadedClass;      Object loadedObject;      Method method;      String szGroupName = null;      // See if it already exists in the hashtable      if (null != ht.get (szKey))         // It does...         return false;      szClassName = de.getClassName ();      // Try to load it      try      {         if (DEBUG)         {            System.err.println ("addTestEntry: Getting class '" + szClassName + "'");         }/////////loadedClass = loader_d.loadClass (szClassName);         loadedClass = Class.forName (szClassName);         // Success!  Try to instantiate it         try         {            loadedObject = loadedClass.newInstance ();            // Success!            // Now, try to see if it supports the required methods...            method = findMethod (loadedObject, "main");            if (null == method)            {               System.err.println ("addTestEntry: Object '" + szClassName + "' doest have method main");               return false;            }            method = findMethod (loadedObject, "getGroup");            if (null == method)            {               System.err.println ("addTestEntry: Object '" + szClassName + "' doest have method getGroup");               return false;            }            // No parameters            Object[] paramArgs = { };            // Try to dynamically call it            try            {               szGroupName = (String)method.invoke (loadedObject, paramArgs);               if (DEBUG)               {                  System.err.println ("addTestEntry: getGroup () returns " + szGroupName);               }            }            catch (Exception e)            {               System.err.println ("addTestEntry: tried invoke getGroup, caught " + e);               e.printStackTrace ();               return false;            }            // Update the group entry            de.setGroup (szGroupName);            // Update the instance            de.setInstance (loadedObject);            ht.put (szKey, de);            return true;         }         catch (Exception e)         {            System.err.println ("addTestEntry: caught " + e + " trying newInstance");            e.printStackTrace ();         }      }      catch (ClassNotFoundException e)      {         System.err.println ("addTestEntry: loading '" + szClassName + "' failed!");         e.printStackTrace ();      }      return false;   }   /**    * This is for debugging use.  It prints out the methods in a class.    *    * @param o  The object instance of the class to print out.    */   private static void showMethods (Object o)   {      Class c = o.getClass ();      Method[] theMethods = c.getMethods ();      for (int i = 0; i < theMethods.length; i++)      {         String methodString = theMethods[i].getName ();         System.out.println ("Name: " + methodString);         String returnString = theMethods[i].getReturnType().getName ();         System.out.println ("   Return Type: " + returnString);         Class[] parameterTypes = theMethods[i].getParameterTypes ();         System.out.print ("   Parameter Types:");         for (int k = 0; k < parameterTypes.length; k ++)         {            String parameterString = parameterTypes[k].getName ();            System.out.print (" " +  parameterString);         }         System.out.println ();      }   }   /**    * This will find a method entry for an object.    *    * @param  o            This is the object to search.    * @param  szMethodName This is the name of the method.    * @return Method       The method if found.    */   private Method findMethod (Object o, String szMethodName)   {      Method   method;      Class    c          = o.getClass ();      Method[] theMethods = c.getMethods ();      for (int i = 0; i < theMethods.length; i++)      {         method = theMethods[i];         String methodString = method.getName ();         if (methodString.equals (szMethodName))         {            // Success!            return method;         }      }      // Failure!      return null;   }////**/// * This is the class loader to use to load test classes./// *////private SimpleClassLoader   loader_d;   /**    * This is the list of test classes.    */   private Hashtable           htTests_d;   private String[]            args_d;   private boolean             DEBUG            = false;   private final String        nameSpaceInterOp = "test/PG_InterOp";   private final String        hostName         = "localhost";}/** * This is the filter class that returns .class entries that match patterns. * * @author  Mark Hamzy */class MyFilenameFilter implements FilenameFilter{   /**    * This is the class constructor.    *    * @param szStart This is the starting pattern that must match.    * @param szEnd   This is the ending pattern that must match.    */   MyFilenameFilter (String szStart, String szEnd)   {      szStart_d = szStart;      szEnd_d   = szEnd;   }   /**    * This is a static function that will implements the matching    * algorithm.    *    * @param  szName  This is the first string.    * @param  szStart This is the starting pattern.    * @param  szEnd   This is the ending pattern.    * @return String  If it matches, then the part left after the start    *                 and end are removed from the string.    */   public static String matches (String szName, String szStart, String szEnd)   {      int cbStart = szStart.length ();      int cbEnd   = szEnd.length ();      int cbName  = szName.length ();      // name at least long enough to match following string subexpressions      if (cbStart + cbEnd <= cbName)      {         int iSlash = szName.lastIndexOf ('/');         if (0 == iSlash)         {            iSlash = szName.lastIndexOf ('\\');         }         if (0 < iSlash)         {            szName = szName.substring (iSlash + 1, cbName);            cbName = szName.length ();         }         if (  szName.startsWith (szStart)            && szName.endsWith (szEnd)            && -1 == szName.indexOf ('$')            )            // it matches the "test*.class" pattern without $'s in it            return szName.substring (0, cbName - cbEnd);      }      return null;   }   /**    * This is required for the FilenameFilter interface.    *    * @param  dir     This is the directory entry.    * @param  szName  This is the name to match.    * @return boolean This is the return code of the function. true    *                 means it matches.    */   public boolean accept (File dir, String szName)   {      return matches (szName, szStart_d, szEnd_d) != null;   }   /**    * This is the starting pattern.    */   private String  szStart_d;   /**    * This is the ending pattern.    */   private String  szEnd_d;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?