KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > ejbfindbyprimarykey > EjbFindByPrimaryKeyName


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.ejbfindbyprimarykey;
24
25 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
26 import com.sun.enterprise.deployment.EjbEntityDescriptor;
27 import com.sun.enterprise.deployment.EjbDescriptor;
28 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
29 import com.sun.enterprise.tools.verifier.*;
30 import java.lang.ClassLoader JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32
33 import com.sun.enterprise.tools.verifier.tests.*;
34
35 /**
36  * Define ejbFindByPrimaryKey method name test.
37  *
38  * Every entity enterprise Bean class must define the ejbFindByPrimaryKey
39  * method. The return type for this method must be the primary key type.
40  * (i.e. the ejbFindByPrimaryKey method must be a single-object finder).
41  *
42  */

43 public class EjbFindByPrimaryKeyName extends EjbTest implements EjbCheck {
44
45
46     /**
47      * Define ejbFindByPrimaryKey method name test.
48      *
49      * Every entity enterprise Bean class must define the ejbFindByPrimaryKey
50      * method. The return type for this method must be the primary key type.
51      * (i.e. the ejbFindByPrimaryKey method must be a single-object finder).
52      *
53      * @param descriptor the Enterprise Java Bean deployment descriptor
54      *
55      * @return <code>Result</code> the results for this assertion
56      */

57     public Result check(EjbDescriptor descriptor) {
58
59     Result result = getInitializedResult();
60     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
61
62     if (descriptor instanceof EjbEntityDescriptor) {
63         String JavaDoc persistence = ((EjbEntityDescriptor)descriptor).getPersistenceType();
64         boolean ejbFindByPrimaryKeyMethodFound = false;
65         boolean oneFailed = false;
66         try {
67             // retrieve the EJB Class Methods
68
Context context = getVerifierContext();
69             ClassLoader JavaDoc jcl = context.getClassLoader();
70             Class JavaDoc EJBClass = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
71             // start do while loop here....
72
do {
73                 Method JavaDoc [] ejbFinderMethods = EJBClass.getDeclaredMethods();
74                 for (int j = 0; j < ejbFinderMethods.length; ++j) {
75                     if (ejbFinderMethods[j].getName().equals("ejbFindByPrimaryKey")) {
76                         // Every entity enterprise Bean class must define the
77
// ejbFindByPrimaryKey method. The result type for this method must
78
// be the primary key type (i.e. the ejbFindByPrimaryKey method
79
// must be a single-object finder).
80
ejbFindByPrimaryKeyMethodFound = true;
81                         result.addGoodDetails(smh.getLocalString
82                                 ("tests.componentNameConstructor",
83                                         "For [ {0} ]",
84                                         new Object JavaDoc[] {compName.toString()}));
85                         result.addGoodDetails(smh.getLocalString
86                                 (getClass().getName() + ".debug1",
87                                         "For EJB Class [ {0} ] Finder Method [ {1} ]",
88                                         new Object JavaDoc[] {EJBClass.getName(),ejbFinderMethods[j].getName()}));
89                         result.addGoodDetails(smh.getLocalString
90                                 (getClass().getName() + ".passed",
91                                         "An [ {0} ] method was found.",
92                                         new Object JavaDoc[] {ejbFinderMethods[j].getName()}));
93                         break;
94                     }
95                 }
96             } while (((EJBClass = EJBClass.getSuperclass()) != null) && (!ejbFindByPrimaryKeyMethodFound));
97             if (EjbEntityDescriptor.BEAN_PERSISTENCE.equals(persistence)&&ejbFindByPrimaryKeyMethodFound) {
98                 result.setStatus(result.PASSED);
99                 return result;
100             } else {
101                 if(EjbEntityDescriptor.BEAN_PERSISTENCE.equals(persistence) && !ejbFindByPrimaryKeyMethodFound) {
102                     oneFailed = true;
103                     result.addErrorDetails(smh.getLocalString
104                             ("tests.componentNameConstructor",
105                                     "For [ {0} ]",
106                                     new Object JavaDoc[] {compName.toString()}));
107                     result.addErrorDetails(smh.getLocalString
108                             (getClass().getName() + ".debug3",
109                                     "For EJB Class [ {0} ]",
110                                     new Object JavaDoc[] {descriptor.getEjbClassName()}));
111                     result.addErrorDetails(smh.getLocalString
112                             (getClass().getName() + ".failed",
113                                     "Error: No ejbFindByPrimaryKey method was found in bean class."));
114                 } else if(EjbEntityDescriptor.CONTAINER_PERSISTENCE.equals(persistence) && ejbFindByPrimaryKeyMethodFound) {
115                     oneFailed = true;
116                     result.addErrorDetails(smh.getLocalString
117                             ("tests.componentNameConstructor",
118                                     "For [ {0} ]",
119                                     new Object JavaDoc[] {compName.toString()}));
120                     result.addErrorDetails(smh.getLocalString
121                             (getClass().getName() + ".debug3",
122                                     "For EJB Class [ {0} ]",
123                                     new Object JavaDoc[] {descriptor.getEjbClassName()}));
124                     result.addErrorDetails(smh.getLocalString
125                             (getClass().getName() + ".failed1",
126                                     "Error: ejbFindByPrimaryKey method was found for CMP bean class."));
127                 } else {//(CONTAINER_PERSISTENCE.equals(persistence))
128
result.addNaDetails(smh.getLocalString
129                             ("tests.componentNameConstructor",
130                                     "For [ {0} ]",
131                                     new Object JavaDoc[] {compName.toString()}));
132                     result.notApplicable(smh.getLocalString
133                             (getClass().getName() + ".notApplicable2",
134                                     "Expected persistence type [ {0} ], but bean [ {1} ] has persistence type [ {2} ]",
135                                     new Object JavaDoc[] {EjbEntityDescriptor.BEAN_PERSISTENCE,descriptor.getName(),persistence}));
136                     return result;
137                 }
138             }
139         } catch (ClassNotFoundException JavaDoc e) {
140             Verifier.debug(e);
141             result.addErrorDetails(smh.getLocalString
142                     ("tests.componentNameConstructor",
143                             "For [ {0} ]",
144                             new Object JavaDoc[] {compName.toString()}));
145             result.failed(smh.getLocalString
146                     (getClass().getName() + ".failedException",
147                             "Error: EJB Class [ {0} ] does not exist or is not loadable.",
148                             new Object JavaDoc[] {descriptor.getEjbClassName()}));
149             oneFailed = true;
150         }
151         if (oneFailed) {
152             result.setStatus(result.FAILED);
153         } else {
154             result.setStatus(result.PASSED);
155         }
156         return result;
157     } else {
158         result.addNaDetails(smh.getLocalString
159                 ("tests.componentNameConstructor",
160                         "For [ {0} ]",
161                         new Object JavaDoc[] {compName.toString()}));
162         result.notApplicable(smh.getLocalString
163                 (getClass().getName() + ".notApplicable",
164                         "[ {0} ] expected {1} bean, but called with {2} bean.",
165                         new Object JavaDoc[] {getClass(),"Entity","Session"}));
166         return result;
167     }
168     }
169 }
170
Popular Tags