FileDocCategorySizeDatePackage
VerifierTest.javaAPI DocGlassfish v2 API21086Fri May 04 22:33:26 BST 2007com.sun.enterprise.tools.verifier.tests

VerifierTest

public abstract class VerifierTest extends Object
Superclass for all tests developped for the Verifier Harness Contains convenience methods and fields
author
Jerome Dochez
version

Fields Summary
private boolean
addedError
private boolean
addedGood
private boolean
addedNa
private boolean
addedWarning
protected Logger
logger
protected final boolean
debug

Are we in debug mode

protected static final com.sun.enterprise.util.LocalStringManagerImpl
smh

helper property to get to the localized strings

private com.sun.enterprise.tools.verifier.Context
context
Constructors Summary
Methods Summary
protected voidaddErrorDetails(com.sun.enterprise.tools.verifier.Result result, ComponentNameConstructor compName)

        // make sure that this message is added only once
        if(addedError) return;
        addedError = true;
	    result.addErrorDetails(smh.getLocalString("tests.componentNameConstructor",
				                                  "For [ {0} ]",
				                                  new Object[] {compName.toString()}));
    
protected voidaddGoodDetails(com.sun.enterprise.tools.verifier.Result result, ComponentNameConstructor compName)
These methods are used to add details to the result. Since this Class is the base class of all the Ejb Tests, all the tests can use these methods. Currently only runtime tests are using them.

        // make sure that this message is added only once
        if(addedGood) return;
        addedGood = true;
	    result.addGoodDetails(smh.getLocalString("tests.componentNameConstructor",
				                                 "For [ {0} ]",
				                                 new Object[] {compName.toString()}));
    
protected voidaddNaDetails(com.sun.enterprise.tools.verifier.Result result, ComponentNameConstructor compName)

        // make sure that this message is added only once
        if(addedNa) return;
        addedNa = true;
	    result.addNaDetails(smh.getLocalString("tests.componentNameConstructor",
				                               "For [ {0} ]",
				                               new Object[] {compName.toString()}));
    
protected voidaddWarningDetails(com.sun.enterprise.tools.verifier.Result result, ComponentNameConstructor compName)

        // make sure that this message is added only once
        if(addedWarning) return;
        addedWarning = true;
	    result.addWarningDetails(smh.getLocalString("tests.componentNameConstructor",
				                                    "For [ {0} ]",
				                                    new Object[] {compName.toString()}));
    
public java.lang.ClasscheckIfPrimitive(java.lang.String param)
If the param string is of primitive type this method return that class representation of the primitive type

param
param
return
Class representaion of the primitive type


        if (param.equals("int"))
            return int.class;
        if (param.equals("boolean"))
            return boolean.class;
        if (param.equals("float"))
            return float.class;
        if (param.equals("double"))
            return(double.class);
        if (param.equals("byte"))
            return byte.class;
        if (param.equals("long"))
            return long.class;
        if (param.equals("char"))
            return char.class;
        if (param.equals("short"))
            return short.class;
        if (param.equals("void"))
            return void.class;

        return null;
    
public intgetCountNodeSet(java.lang.String xpath)

        try{
//            int value = -1;
            Document d = getVerifierContext().getRuntimeDocument();
            if (d==null)
                return -1;
            NodeSet ns = new NodeSet(XPathAPI.selectNodeList(d, xpath));
            return ns.getLength();
        }catch(Exception ex){
            ex.printStackTrace();
            return -1;
        }
    
public static java.lang.reflect.MethodgetDeclaredMethod(java.lang.Class clazz, java.lang.String methodName, java.lang.Class[] parmTypes)

utility method to return a method if it is implemented by a class or one of its superclass irrespective of the method being public, private or protected

param
clazz the class used to look up the method
param
methodName the method name
param
parmTypes the parameters
return
instanceof the Method if implemented

        Method m=null;
        Class c = clazz;
        do {
            try {
                m = clazz.getDeclaredMethod(methodName, parmTypes);
            } catch(NoSuchMethodException nsme) {
            } catch(SecurityException se) {
            }
            c = c.getSuperclass();
        } while (m != null && c != null);            
        return m;
    
protected com.sun.enterprise.tools.verifier.ResultgetInitializedResult()

Common traces and initialization of the result object that will hold the result of the assertion tested by this verifier test

return
the initialized Result object


        logger.log(Level.FINE, "which.class.called.string",
                new Object[] {getClass()}) ;
        Result result = new Result();
        String version = "";
        // This is only needed because of ParseDD test which runs before
        // context is set in VerifierTest object, else we shall get a NPE.
        String compName = "";
        if (context!=null) {
            version = context.getSchemaVersion();
            compName = context.getComponentNameConstructor().toString();
        } else {
            logger.fine(getClass().getName() + " context is null.");
        }
        result.init(getClass(), version, compName);
        logger.log(Level.FINE, "test.string.assertion", new Object[] {result.getAssertion()});
        return result;
    
public static java.lang.reflect.MethodgetMethod(java.lang.Class clazz, java.lang.String methodName, java.lang.Class[] parmTypes)

utility method to return a method if ig is implemented by a class or one of its superclass and it is defined as public

param
clazz the class used to look up the method
param
methodName the method name
param
parmTypes the parameters
return
instanceof the Method if implemented

        Method m=null;
        Class c = clazz;
        do {
            try {
                m = clazz.getMethod(methodName, parmTypes);
            } catch(NoSuchMethodException nsme) {
            } catch(SecurityException se) {
            }
            c = c.getSuperclass();
        } while (m != null && c != null);            
        return m;
    
public intgetNonRuntimeCountNodeSet(java.lang.String xpath)

        try{
//            int value = -1;
            Document d = getVerifierContext().getDocument();
            if (d==null)
                return -1;
            XObject result = XPathAPI.eval(d, xpath,
                             (PrefixResolver)new XpathPrefixResolver (d));
            NodeList nl = result.nodelist();
            NodeSet ns = new NodeSet(nl);
            return ns.getLength();
        }catch(Exception ex){
            ex.printStackTrace();
            return -1;
        }
    
public java.lang.FloatgetRuntimeSpecVersion()

        String docType = null;
        String versionStr = null;
        Float versionFloat=null;
        try{
            DocumentType dt = getVerifierContext().getRuntimeDocument().getDoctype();
            if (dt==null) return null;
            docType = dt.getPublicId();
            StringTokenizer st = new StringTokenizer(docType, "//");
            while (st.hasMoreElements()) {
                String tmp = st.nextToken();
                if (tmp.startsWith("DTD")) {
                // this is the string we are interested in
                    StringTokenizer versionST = new StringTokenizer(tmp);
                    while (versionST.hasMoreElements()) {
                        versionStr = versionST.nextToken();
                        try {
                            versionFloat = Float.valueOf(versionStr);
                        } catch(NumberFormatException nfe) {
                        // ignore, this is just the other info of the publicID
                        }
                    }
                }
            }
            return versionFloat;
        }catch(Exception ex){
            //ex.printStackTrace();
            return null;
        }
    
public com.sun.enterprise.tools.verifier.ContextgetVerifierContext()
This method provides the Context object

	return context;
    
public java.lang.StringgetXPathValue(java.lang.String xpath)

        try{
            String value = null;
            Document d = getVerifierContext().getRuntimeDocument();
            if (d==null) return null;
            NodeList nl = XPathAPI.selectNodeList(d, xpath);
            for(int i=0; i<nl.getLength();i++){
                Node n = ((Node)nl.item(i)).getFirstChild();
                if (n==null) return null;
                value = n.getNodeValue();
            }
            return value;
        }catch(Exception ex){
            ex.printStackTrace();
            return null;
        }
    
public java.lang.StringgetXPathValueForNonRuntime(java.lang.String xpath)

        try{
            String value = null;
            Document d = getVerifierContext().getDocument();
            if (d==null) return null;
            XObject result = XPathAPI.eval(d, xpath, 
                             (PrefixResolver)new XpathPrefixResolver (d));
            NodeList nl = result.nodelist();
            for(int i=0; i<nl.getLength();i++){
                Node n = ((Node)nl.item(i)).getFirstChild();
                if (n==null) return null;
                value = n.getNodeValue();
            }
            return value;
        }catch(Exception ex){
            ex.printStackTrace();
            return null;
        }
    
public static booleanisImplementorOf(java.lang.Class c, java.lang.String interfaceName)

Test if a class or its superclasses implements an interface

param
c is the class to test
param
interfaceName is the interface we test for implementation
return
true if the class or superclasses implements the interface

        
        if (c==null || interfaceName==null) 
            return false;

        // try this first because the code in the rest of the method
        // is buggy 
        try {
          Class<?> intf = Class.forName(interfaceName);
          if (intf.isAssignableFrom(c)) 
             return true;
          else
             return false;
         }catch(Exception e) { }
        
	do {
	    if (isSubclassOf(c, interfaceName)) {
		return true;
	    }
	    // get the list of implemented interfaces
            Class[] interfaces = c.getInterfaces();
            for (int i=0; i<interfaces.length;i++) {
		if (isSubclassOf(interfaces[i], interfaceName)) {
		    return true;
                }
            }
            // we haven't found for this implementation, look in the superclass
            c = c.getSuperclass();
        } while (c != null);
	return false;        
    
public static booleanisSubclassOf(java.lang.Class subClass, java.lang.String superClassName)

check if the class is a sublcass or the class itself of the passed class name

param
subClass class object to test if it a subclass
param
superClassName class name for the superclass
return
true if the Class is a subclass or the class itself of the class name

        
        
        if (subClass==null || superClassName==null) {
            return false;
        }
        
        Class c = subClass;
        
	do {
            if (c.getName().equals(superClassName)) {
                return true;
            }
	    Class[] interfaces = c.getInterfaces();
            for (int i=0; i<interfaces.length;i++) {
                if (interfaces[i].getName().equals(superClassName)) {
		    return true;
                }
		else {
		    if (isSubclassOf(interfaces[i], superClassName)) {
			return true;
		    }
		}
            }
            c = c.getSuperclass(); 	             
	} while (c!=null);
        return false;
    
public static booleanmethodThrowException(java.lang.reflect.Method method, java.lang.String exception)

test if a method throws a particular exception

param
method the method to test
param
exception the exception we are looking for
return
true if the method actually throw the exception

        
        Class[] exceptions = method.getExceptionTypes();
        for (int i=0;i<exceptions.length;i++) {
            if (isSubclassOf(exceptions[i], exception))
                return true;            
        }
        return false;
    
public voidsetVerifierContext(com.sun.enterprise.tools.verifier.Context context)
This method sets the Context object


               
        
	this.context = context;
    
public static voidtestFileExistence(java.lang.String uri, java.lang.String fileName, java.lang.String fileID, com.sun.enterprise.tools.verifier.Result result)

Test for a file existence in the archive file

param
uri The archive to look in
param
fileName The file Name to look for
param
fileID The archive file name
param
result where to place the result

        
       FileArchive arch=null;
//       ZipEntry ze=null;
       JarFile jarFile=null;
       
       if (fileName == null || fileName.length()==0) {
	    result.notApplicable(smh.getLocalString
    	        ("com.sun.enterprise.tools.verifier.tests.VerifierTest.fileexistence.notApplicable",
                 "No {0} defined in deployment descriptors",
                 new Object[] {fileID}));                        
            return;
        }
//        if (file==null){
            try{
                arch = new FileArchive();
                arch.open(uri);
            }catch(Exception e){}   
//        }else{
//            try {
//                jarFile = new JarFile(file);
//            } catch (java.io.IOException ioe) {
//                Verifier.debug(ioe);
//                result.failed(smh.getLocalString
//                        ("com.sun.enterprise.tools.verifier.tests.VerifierTest.fileexistence.failed",
//                         "Error:  {0} [ {1} ] not found in the archive",
//                         new Object[] {fileID, fileName}));      
//                         return;
//             }
//         }
        try{
//            if (file!=null){
//            ze = jarFile.getEntry(fileName);
//            if (ze == null) {
//                result.failed(smh.getLocalString
//                    ("com.sun.enterprise.tools.verifier.tests.VerifierTest.fileexistence.failed",
//                    "Error: {0} [ {1} ] not found in the archive",
//                    new Object[] {fileID, fileName}));                           
//            }else{
//                result.passed(smh.getLocalString
//                ("com.sun.enterprise.tools.verifier.tests.VerifierTest.fileexistence.passed",
//                "{0} [ {1} ] found in the archive",
//                new Object[] {fileID, fileName}));                           
//            }
//        }
//        else{
            File urif = new File(arch.getArchiveUri()+File.separator+fileName);
            if(urif.exists()){
                result.passed(smh.getLocalString
                ("com.sun.enterprise.tools.verifier.tests.VerifierTest.fileexistence.passed",
                "{0} [ {1} ] found in the archive",
                new Object[] {fileID, fileName}));                           
            }else{
                result.warning(smh.getLocalString
                    ("com.sun.enterprise.tools.verifier.tests.VerifierTest.fileexistence.warning",
                    "{0} [ {1} ] not found in the archive",
                    new Object[] {fileID, fileName}));                           
            }
                urif = null;
//        }
        
        if (jarFile!=null)
                        jarFile.close();
        }catch(Exception ex){}
    
    
public static booleantestImplementationOf(java.lang.Class clazz, java.lang.String interfaceName, com.sun.enterprise.tools.verifier.Result result)

verify that a class or one of its superclass is implementing an interface

param
clazz the class to test for the implementation of the interface
param
interfaceName the name of the interface that should be implementad
param
result where to put the result

        
        if (isImplementorOf(clazz, interfaceName)) {
            result.passed(smh.getLocalString
	        ("com.sun.enterprise.tools.verifier.tests.VerifierTest.interfaceimplementation.passed", 
                "The class [ {0} ] implements the [ {1} ] interface",
                new Object[] {clazz.getName(), interfaceName}));    
            return true;
        } else {
            result.failed(smh.getLocalString
	        ("com.sun.enterprise.tools.verifier.tests.VerifierTest.interfaceimplementation.failed", 
                "Error: The class [ {0} ] does not implement the [ {1} ] interface",
                new Object[] {clazz.getName(), interfaceName}));        
            return false;
        }