runtest.java

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

JAVA
867
字号
//%2006//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation, The Open Group.// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; Symantec Corporation; The Open Group.//// Permission is hereby granted, free of charge, to any person obtaining a copy// of this software and associated documentation files (the "Software"), to// deal in the Software without restriction, including without limitation the// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or// sell copies of the Software, and to permit persons to whom the Software is// furnished to do so, subject to the following conditions://// THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.////%/////////////////////////////////////////////////////////////////////////////package Client;import java.io.FilenameFilter;import java.io.File;import java.io.IOException;import java.net.InetAddress;import java.net.UnknownHostException;import java.util.Enumeration;import java.util.Hashtable;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;import java.util.jar.JarEntry;import java.util.jar.JarFile;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import org.pegasus.jmpi.CIMClient;import org.pegasus.jmpi.CIMException;import org.pegasus.jmpi.CIMNameSpace;/** * */public class RunTest{   /**    * This is the java equivalent of #define.  It is the starting pattern to    * search for.    */   public static String       patternStart = "test";   /**    * This is the java equivalent of #define.  It is the ending pattern to    * search for.    */   public static final String patternEnd   = ".class";   /**    * This is the class constructor.    *    * @param args These are the command line arguments.    */   public RunTest (String[] args)   {   }   public void setDebug (boolean fDebug)   {       DEBUG = fDebug;   }   /**    * Class printing out method <code>System.out.println ()</code>    *    * @return String The human readable version.    */   public String toString ()   {      java.util.Hashtable h = new java.util.Hashtable ();      Class               c = getClass ();      Field[]             f = c.getDeclaredFields ();      for (int i = 0; i < f.length; i++)      {         if (Modifier.isFinal (f[i].getModifiers ()))            // Don't print #defines            continue;         try         {            Object o = f[i].get (this);            if (o == null)               o = "null";            h.put (f[i].getName (), o);         }         catch (IllegalAccessException e)         {            h.put (f[i].getName (), "???");         }      }      return c.getName () + "@" + this.hashCode () + h;   }   /**    * This is the required main entry point.    *    * @param args These are the command line arguments.    */   public void Main (String[] args)   {      args_d = args;      for (int i = 0; i < args.length; i++)      {          if (args[i].equalsIgnoreCase ("debug"))          {              setDebug (true);          }          else if (args[i].startsWith ("--patternStart="))          {             patternStart = args[i].substring (15);          }      }      htTests_d = findTests ();      if (DEBUG)      {         System.err.println ("Main: " + htTests_d);      }      if (htTests_d == null)      {         System.out.println ("Error: Could not find any testcases!");         System.exit (1);      }      Enumeration enm = htTests_d.elements ();      if (!enm.hasMoreElements ())      {         System.out.println ("Error: Could not find any testcases!");         System.exit (1);      }      while (enm.hasMoreElements ())      {         TestEntry te = (TestEntry)enm.nextElement ();         if (!runTest (te))         {            System.exit (1);         }      }   }   /**    * This is the required main entry point.    *    * @param args These are the command line arguments.    */   public static void main (String[] args)   {      RunTest p = new RunTest (args);      p.Main (args);   }   private static String getHostname ()   {      try      {         InetAddress addr = InetAddress.getLocalHost ();         // Get IP Address         byte[] ipAddr = addr.getAddress ();         // Get hostname         return addr.getHostName ();      }      catch (UnknownHostException e)      {      }      return "localhost";   }   private CIMClient connectToClient ()   {      CIMClient     cimClient        = null;      int           portNo           = 5988;      String        username         = "";      String        password         = "";      String        urlAttach        = "http://" + hostName + ":" + portNo;      CIMNameSpace  clientNameSpace  = null;      clientNameSpace = new CIMNameSpace (urlAttach, nameSpaceInterOp);      if (clientNameSpace == null)      {          System.out.println ("Error: Could not create a CIMNameSpace (url: '" + urlAttach + "', namespace: '" + nameSpaceInterOp + "');");          System.exit (1);      }      try      {          cimClient = new CIMClient (clientNameSpace, username, password);      }      catch (Exception e)      {          System.out.println ("Caught " + e);          e.printStackTrace ();      }      if (cimClient == null)      {          System.out.println ("Error: Could not create a CIMClient (namespace: '" + clientNameSpace + "', username: '" + username + "', password: '" + password + "');");          System.exit (1);      }      if (DEBUG)          System.err.println ("connectToClient: cimClient    = " + cimClient);      return cimClient;   }   /**    * This is called to notify a test class that it must execute    *    * @param szTestClassName This is the menu name of the test class.    */   private boolean runTest (TestEntry testEntry)   {      Object    currentTestObject = null;      Class     currentTestClass  = null;      String    szTestClassName   = testEntry.getClassName ();      CIMClient cimClient         = null;      System.out.println ("RUNNING: " + szTestClassName);      try      {         cimClient = connectToClient ();         // Try to load it/////////currentTestClass = loader_d.loadClass (szTestClassName);         currentTestClass = Class.forName (szTestClassName);         // Success!  Try to instantiate it         try         {            Class[]    paramTypes;            Object[]   paramArgs;            Method     method;            Object     retVal;            currentTestObject = currentTestClass.newInstance ();            // Try to get the method            try            {               method = findMethod (currentTestObject, "main");               if (DEBUG)               {                  System.err.println ("runTest: main = " + method);               }               if (null != method)               {                  paramArgs = new Object[] {                     args_d,                     cimClient                  };                  // Try to dynamically call it                  try                  {                     retVal = method.invoke (testEntry.getInstance (), paramArgs);                  }                  catch (Exception e)                  {                     System.out.println ("FAIL: " + szTestClassName + " 1: " + e + ", " + method);                     if (DEBUG)                     {                        System.err.println ("runTest: tried invoke, caught " + e);                        e.printStackTrace ();                     }                     return false;                  }                  Boolean bRet = (Boolean)retVal;                  return bRet.booleanValue ();               }            }            catch (Exception e)            {               System.out.println ("FAIL: " + szTestClassName + " 2: " + e);               if (DEBUG)               {                  System.err.println ("FAIL: tried invoke on \"" + szTestClassName + "\", caught " + e);                  e.printStackTrace ();               }               return false;            }         }         catch (Exception e)         {            System.out.println ("FAIL: " + szTestClassName + " 3: " + e);            if (DEBUG)            {               System.err.println ("runTest: caught " + e + " trying newInstance");               e.printStackTrace ();            }            return false;         }      }      catch (ClassNotFoundException e)      {         System.out.println ("FAIL: " + szTestClassName + " 4: " + e);         if (DEBUG)         {            System.err.println ("runTest: loading '" + szTestClassName + "' failed!");            e.printStackTrace ();         }         return false;      }      finally      {         if (cimClient != null)         {            try            {               cimClient.close ();            }            catch (CIMException e)            {               if (DEBUG)               {                  System.err.println ("runTest: Error: CIMException: " + e);                  e.printStackTrace ();               }            }         }      }      return true;   }   /**    * This is called to create a list of test classes that are found    * by searching the CLASSPATH.    *    * @return Hashtable The list of test classes that were found.    */   private Hashtable findTests ()   {      Hashtable h             = new Hashtable ();      String    szClassPath   = System.getProperty ("java.class.path");      String    szCurrentItem;      int       iSeparator    = -1;      if (DEBUG)      {         System.err.println ("findTests: " + szClassPath);      }      // Loop through the CLASSPATH      szCurrentItem = szClassPath;      do      {         // Is there a separator?         iSeparator = szClassPath.indexOf (File.pathSeparatorChar);         if (0 <= iSeparator)         {            // Yes. Split into first and rest.            szCurrentItem = szClassPath.substring (0, iSeparator);            szClassPath   = szClassPath.substring (iSeparator + 1, szClassPath.length ());         }         else         {            // Nope.            szCurrentItem = szClassPath;            szClassPath   = "";         }         if (szCurrentItem.equals (""))            continue;         String szLowerItem = szCurrentItem.toLowerCase ();         if (szLowerItem.endsWith (".zip"))         {            if (DEBUG)            {               System.err.println ("findTests: ZIP: " + szCurrentItem);            }            try            {               ZipFile  zf = new ZipFile (szCurrentItem);               for (Enumeration e = zf.entries (); e.hasMoreElements (); )               {                  ZipEntry ze        = (ZipEntry)e.nextElement ();                  String szName      = ze.getName ();                  String szMatchName;

⌨️ 快捷键说明

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