KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > findbyprimarykey > HomeInterfaceFindByPrimaryKeyReturn


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

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

59     public Result check(EjbDescriptor descriptor) {
60
61     result = getInitializedResult();
62     compName = getVerifierContext().getComponentNameConstructor();
63
64     boolean oneFailed = false;
65     if (descriptor instanceof EjbEntityDescriptor) {
66         if(descriptor.getHomeClassName() != null && !"".equals(descriptor.getHomeClassName())&&
67            descriptor.getRemoteClassName() != null && !"".equals(descriptor.getRemoteClassName()))
68         oneFailed = commonToBothInterfaces(descriptor.getHomeClassName(), descriptor.getRemoteClassName(),descriptor);
69         if(oneFailed == false) {
70         if(descriptor.getLocalHomeClassName() != null && !"".equals(descriptor.getLocalHomeClassName())&&
71            descriptor.getLocalClassName() != null && !"".equals(descriptor.getLocalClassName()))
72             oneFailed = commonToBothInterfaces(descriptor.getLocalHomeClassName(),descriptor.getLocalClassName(),descriptor);
73         }
74         if (oneFailed) {
75         result.setStatus(result.FAILED);
76         } else {
77         result.setStatus(result.PASSED);
78         }
79         return result;
80
81     } else {
82         result.addNaDetails(smh.getLocalString
83                 ("tests.componentNameConstructor",
84                  "For [ {0} ]",
85                  new Object JavaDoc[] {compName.toString()}));
86         result.notApplicable(smh.getLocalString
87                  (getClass().getName() + ".notApplicable",
88                   "[ {0} ] expected {1} bean, but called with {2} bean.",
89                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
90         return result;
91     }
92     }
93
94     /**
95      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
96      * @param descriptor the Enterprise Java Bean deployment descriptor
97      * @param home for the Home interface of the Ejb.
98      * @param remote for Remote/Local interface
99      * @return boolean the results for this assertion i.e if a test has failed or not
100      */

101
102
103     private boolean commonToBothInterfaces(String JavaDoc home, String JavaDoc remote, EjbDescriptor descriptor) {
104     boolean findByPrimaryKeyMethodFound = false;
105     boolean oneFailed = false;
106     boolean returnValueValid = false;
107     try {
108         // retrieve the home interface methods
109
Context context = getVerifierContext();
110         ClassLoader JavaDoc jcl = context.getClassLoader();
111         Class JavaDoc homeInterfaceClass = Class.forName(home, false, getVerifierContext().getClassLoader());
112         Method [] ejbFinderMethods = homeInterfaceClass.getDeclaredMethods();
113         Class JavaDoc rc = Class.forName(remote, false, getVerifierContext().getClassLoader());
114         
115         for (int j = 0; j < ejbFinderMethods.length; ++j) {
116         // reset all booleans for next method within the loop
117
returnValueValid = false;
118         
119         if (ejbFinderMethods[j].getName().equals("findByPrimaryKey")) {
120             // Every entity enterprise Bean class must define the
121
// findByPrimaryKey method. The return type for this method must
122
// be the primary key type (i.e. the findByPrimaryKey method
123
// must be a single-object finder).
124
if (!findByPrimaryKeyMethodFound) {
125             findByPrimaryKeyMethodFound = true;
126             }
127             Class JavaDoc returnByPrimaryKeyValue = ejbFinderMethods[j].getReturnType();
128             // as long as this returns a single object finder, then return type
129
// is valid
130
if (((returnByPrimaryKeyValue.getName().equals(rc.getName())) &&
131              (!((returnByPrimaryKeyValue.getName().equals("java.util.Collection")) ||
132                 (returnByPrimaryKeyValue.getName().equals("java.util.Enumeration")))))) {
133             returnValueValid = true;
134             }
135             
136             
137             // report for this particular findByPrimaryKey(...)
138
if (findByPrimaryKeyMethodFound && returnValueValid) {
139             result.addGoodDetails(smh.getLocalString
140                           ("tests.componentNameConstructor",
141                            "For [ {0} ]",
142                            new Object JavaDoc[] {compName.toString()}));
143             result.addGoodDetails(smh.getLocalString
144                           (getClass().getName() + ".debug1",
145                            "For Home interface [ {0} ] Finder Method [ {1} ]",
146                            new Object JavaDoc[] {homeInterfaceClass.getName(),ejbFinderMethods[j].getName()}));
147             result.addGoodDetails(smh.getLocalString
148                           (getClass().getName() + ".passed",
149                            "A findByPrimaryKey method was found with valid return type."));
150             } else if (findByPrimaryKeyMethodFound && !returnValueValid) {
151             oneFailed = true;
152             result.addErrorDetails(smh.getLocalString
153                            ("tests.componentNameConstructor",
154                         "For [ {0} ]",
155                         new Object JavaDoc[] {compName.toString()}));
156             result.addErrorDetails(smh.getLocalString
157                            (getClass().getName() + ".debug1",
158                         "For Home interface [ {0} ] Finder Method [ {1} ]",
159                         new Object JavaDoc[] {homeInterfaceClass.getName(),ejbFinderMethods[j].getName()}));
160             result.addErrorDetails(smh.getLocalString
161                            (getClass().getName() + ".failed",
162                         "Error: A findByPrimaryKey method was found, but with invalid return type."));
163             }
164             // found findByPrimaryKey, so break out of loop
165
break;
166         }
167         }
168         if (!findByPrimaryKeyMethodFound) {
169         oneFailed = true;
170         result.addErrorDetails(smh.getLocalString
171                        ("tests.componentNameConstructor",
172                     "For [ {0} ]",
173                     new Object JavaDoc[] {compName.toString()}));
174         result.addErrorDetails(smh.getLocalString
175                        (getClass().getName() + ".debug3",
176                     "For Home interface [ {0} ] ",
177                     new Object JavaDoc[] {homeInterfaceClass.getName()}));
178         result.addErrorDetails(smh.getLocalString
179                        (getClass().getName() + ".failed1",
180                     "Error: No findByPrimaryKey method was found in home interface class [ {0} ].",
181                     new Object JavaDoc[] {homeInterfaceClass.getName()}));
182         }
183         return oneFailed;
184     } catch (ClassNotFoundException JavaDoc e) {
185         Verifier.debug(e);
186         result.addErrorDetails(smh.getLocalString
187                    ("tests.componentNameConstructor",
188                     "For [ {0} ]",
189                     new Object JavaDoc[] {compName.toString()}));
190         result.failed(smh.getLocalString
191               (getClass().getName() + ".failedException",
192                "Error: Home interface [ {0} ] does not exist or is not loadable.",
193                new Object JavaDoc[] {home}));
194         oneFailed = true;
195         return oneFailed;
196     }
197     
198     }
199 }
200
Popular Tags