KickJava   Java API By Example, From Geeks To Geeks.

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


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 test.
38  *
39  * Every entity enterprise Bean class must define the findByPrimaryKey
40  * method.
41  *
42  */

43 public class HomeInterfaceFindByPrimaryKeyName extends EjbTest implements EjbCheck {
44     Result result = null;
45     ComponentNameConstructor compName = null;
46
47     /**
48      * Define findByPrimaryKey method test.
49      *
50      * Every entity enterprise Bean class must define the findByPrimaryKey
51      * method.
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 = getInitializedResult();
60     boolean oneFailed = false;
61     compName = getVerifierContext().getComponentNameConstructor();
62
63     if (descriptor instanceof EjbEntityDescriptor) {
64         if(descriptor.getLocalHomeClassName() != null && !"".equals(descriptor.getLocalHomeClassName()))
65         oneFailed = commonToBothInterfaces(descriptor.getLocalHomeClassName(),descriptor);
66         
67         if(oneFailed == false) {
68         if(descriptor.getHomeClassName() != null && !"".equals(descriptor.getHomeClassName()))
69             oneFailed = commonToBothInterfaces(descriptor.getHomeClassName(),descriptor);
70         }
71         if (oneFailed) {
72         result.setStatus(result.FAILED);
73         } else {
74         result.setStatus(result.PASSED);
75         }
76         return result;
77     } else {
78         result.addNaDetails(smh.getLocalString
79                 ("tests.componentNameConstructor",
80                  "For [ {0} ]",
81                  new Object JavaDoc[] {compName.toString()}));
82         result.notApplicable(smh.getLocalString
83                  (getClass().getName() + ".notApplicable",
84                   "[ {0} ] expected {1} bean, but called with {2} bean.",
85                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
86         return result;
87     }
88     }
89
90     /**
91      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
92      * @param descriptor the Enterprise Java Bean deployment descriptor
93      * @param home for the Home interface of the Ejb.
94      * @return boolean the results for this assertion i.e if a test has failed or not
95      */

96
97     private boolean commonToBothInterfaces(String JavaDoc home, EjbDescriptor descriptor) {
98     boolean findByPrimaryKeyMethodFound = false;
99     boolean oneFailed = false;
100     try {
101         // retrieve the home interface methods
102
Context context = getVerifierContext();
103         ClassLoader JavaDoc jcl = context.getClassLoader();
104         Class JavaDoc homeInterfaceClass = Class.forName(home, false, getVerifierContext().getClassLoader());
105         Method [] ejbFinderMethods = homeInterfaceClass.getDeclaredMethods();
106         for (int j = 0; j < ejbFinderMethods.length; j++) {
107         if (ejbFinderMethods[j].getName().equals("findByPrimaryKey")) {
108             // Every entity enterprise Bean class must define the
109
// findByPrimaryKey method.
110

111             findByPrimaryKeyMethodFound = true;
112                 
113             // report for this particular findByPrimaryKey(...)
114
result.addGoodDetails(smh.getLocalString
115                       ("tests.componentNameConstructor",
116                        "For [ {0} ]",
117                        new Object JavaDoc[] {compName.toString()}));
118             result.addGoodDetails(smh.getLocalString
119                       (getClass().getName() + ".debug1",
120                        "For Home interface [ {0} ] Finder Method [ {1} ]",
121                        new Object JavaDoc[] {homeInterfaceClass.getName(),ejbFinderMethods[j].getName()}));
122             result.addGoodDetails(smh.getLocalString
123                       (getClass().getName() + ".passed",
124                        "A findByPrimaryKey method was found."));
125             return oneFailed;
126         }
127         }
128         if (!findByPrimaryKeyMethodFound) {
129         oneFailed = true;
130         result.addErrorDetails(smh.getLocalString
131                        ("tests.componentNameConstructor",
132                     "For [ {0} ]",
133                     new Object JavaDoc[] {compName.toString()}));
134         result.addErrorDetails(smh.getLocalString
135                        (getClass().getName() + ".debug3",
136                     "For Home interface [ {0} ] ",
137                     new Object JavaDoc[] {homeInterfaceClass.getName()}));
138         result.addErrorDetails(smh.getLocalString
139                        (getClass().getName() + ".failed",
140                     "Error: No findByPrimaryKey method was found in home interface class [ {0} ].",
141                     new Object JavaDoc[] {homeInterfaceClass.getName()}));
142         }
143         return oneFailed;
144     } catch (ClassNotFoundException JavaDoc e) {
145         Verifier.debug(e);
146         result.addErrorDetails(smh.getLocalString
147                    ("tests.componentNameConstructor",
148                     "For [ {0} ]",
149                     new Object JavaDoc[] {compName.toString()}));
150         result.failed(smh.getLocalString
151               (getClass().getName() + ".failedException",
152                "Error: Home interface [ {0} ] does not exist or is not loadable.",
153                new Object JavaDoc[] {home}));
154         oneFailed = true;
155         return oneFailed;
156     }
157     }
158 }
159
Popular Tags