KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > cmp2 > QueryMethodTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.tools.verifier.tests.ejb.entity.cmp2;
24
25 import java.lang.reflect.Method JavaDoc;
26 import com.sun.enterprise.deployment.*;
27 import com.sun.enterprise.tools.verifier.Result;
28 import com.sun.enterprise.tools.verifier.*;
29 import java.lang.ClassLoader JavaDoc;
30 import com.sun.enterprise.tools.verifier.tests.*;
31
32 /**
33  * Superclass for all finder method test
34  *
35  * @author Jerome Dochez
36  * @version
37  */

38 abstract public class QueryMethodTest extends CMPTest {
39     ComponentNameConstructor compName = null;
40     /**
41      * <p>
42      * Run an individual test against a finder method (single or multi)
43      * </p>
44      *
45      * @param method is the finder method reference
46      * @param descriptor is the entity bean descriptor
47      * @param targetClass is the class to apply to tests to
48      * @param result is where to place the result
49      *
50      * @return true if the test passes
51      */

52     protected abstract boolean runIndividualQueryTest(Method JavaDoc method, EjbCMPEntityDescriptor descriptor, Class JavaDoc targetClass, Result result);
53     
54      /**
55      * check if a field has been declared in a class
56      *
57      * @param fieldName the field name to look for declaration
58      * @param c the class to look into
59      * @param result where to place the test result
60      */

61     public Result check(EjbCMPEntityDescriptor descriptor) {
62         
63         boolean allIsWell = true;
64         Result result = getInitializedResult();
65     compName = getVerifierContext().getComponentNameConstructor();
66         
67     if (descriptor.getHomeClassName() != null && !((descriptor.getHomeClassName()).equals("")) &&
68         descriptor.getRemoteClassName() != null && !((descriptor.getRemoteClassName()).equals(""))) {
69         allIsWell = commonToBothInterfaces(descriptor.getHomeClassName(),descriptor.getRemoteClassName(),descriptor, result);
70     }
71     if(allIsWell == true) {
72         if (descriptor.getLocalHomeClassName() != null && !((descriptor.getLocalHomeClassName()).equals("")) &&
73         descriptor.getLocalClassName() != null && !((descriptor.getLocalClassName()).equals(""))) {
74         allIsWell = commonToBothInterfaces(descriptor.getLocalHomeClassName(),descriptor.getLocalClassName(),descriptor, result);
75         }
76     }
77      
78         if (allIsWell)
79             result.setStatus(Result.PASSED);
80         else
81             result.setStatus(Result.FAILED);
82             
83         return result;
84     }
85   /**
86      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
87      * @param descriptor the Enterprise Java Bean deployment descriptor
88      * @param ejbHome for the Home interface of the Ejb.
89      * @param result Result of the test
90      * @param remote Remote/Local interface
91      * @return boolean the results for this assertion i.e if a test has failed or not
92      */

93
94
95     private boolean commonToBothInterfaces(String JavaDoc ejbHome, String JavaDoc remote, EjbDescriptor descriptor, Result result) {
96     boolean allIsWell = true;
97     boolean found = false;
98     String JavaDoc ejbClassName = descriptor.getEjbClassName();
99     Context context = getVerifierContext();
100         ClassLoader JavaDoc jcl = context.getClassLoader();
101         try {
102             Class JavaDoc ejbClass = Class.forName(ejbClassName, false,
103                                 getVerifierContext().getClassLoader());
104             Method JavaDoc[] methods = Class.forName(ejbHome, false,
105                                 getVerifierContext().getClassLoader()).getMethods();
106             for (int i=0;i<methods.length;i++) {
107                 String JavaDoc methodName = methods[i].getName();
108                 // get the expected return type
109
String JavaDoc methodReturnType = methods[i].getReturnType().getName();
110                 if (methodName.startsWith("find")) {
111             found = true;
112                     if (methodReturnType.equals(remote) ||
113             isSubclassOf(Class.forName(methodReturnType, false,
114                     getVerifierContext().getClassLoader()), "java.util.Collection") ||
115             isImplementorOf(Class.forName(methodReturnType, false,
116                     getVerifierContext().getClassLoader()), "java.util.Collection")) {
117                         
118                         if (!runIndividualQueryTest(methods[i], (EjbCMPEntityDescriptor) descriptor, ejbClass, result))
119                             allIsWell=false;
120                     }
121                 }
122         }
123         if (found == false) {
124         result.addGoodDetails(smh.getLocalString
125               ("com.sun.enterprise.tools.verifier.tests.ejb.EjbTest.passed",
126                "Not Applicable : No find methods found",
127                 new Object JavaDoc[] {}));
128         }
129             
130         return allIsWell;
131         } catch (ClassNotFoundException JavaDoc e) {
132             Verifier.debug(e);
133             result.addErrorDetails(smh.getLocalString
134                        ("tests.componentNameConstructor",
135                     "For [ {0} ]",
136                     new Object JavaDoc[] {compName.toString()}));
137         result.failed(smh.getLocalString
138               ("com.sun.enterprise.tools.verifier.tests.ejb.EjbTest.failedException",
139                "Error: [ {0} ] class not found.",
140                 new Object JavaDoc[] {descriptor.getEjbClassName()}));
141             allIsWell= false;
142         return allIsWell;
143         }
144     }
145 }
146
Popular Tags