KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > primarykeyclass > PrimaryKeyClassMethodHashCode


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.primarykeyclass;
24
25 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
26 import java.util.*;
27 import java.lang.reflect.*;
28 import com.sun.enterprise.deployment.EjbEntityDescriptor;
29 import com.sun.enterprise.deployment.EjbDescriptor;
30 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
31 import com.sun.enterprise.tools.verifier.*;
32 import java.lang.ClassLoader JavaDoc;
33 import com.sun.enterprise.tools.verifier.tests.*;
34
35 /**
36  * Primary key class provide implementation of hashCode() methods test.
37  *
38  * Enterprise Bean's primary key class
39  * The class must provide suitable implementation of the hashCode()
40  * method to simplify the management of the primary keys by client code.
41  *
42  */

43 public class PrimaryKeyClassMethodHashCode extends EjbTest implements EjbCheck {
44
45
46     /**
47      * Primary key class provide implementation of hashCode() methods test.
48      *
49      * Enterprise Bean's primary key class
50      * The class must provide suitable implementation of the hashCode()
51      * method to simplify the management of the primary keys by client code.
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 transactionType = descriptor.getTransactionType();
64         if (EjbDescriptor.CONTAINER_TRANSACTION_TYPE.equals(transactionType))
65         {
66
67             boolean hasDefinedHashCodeMethod = false;
68             boolean oneFailed = false;
69             int lc = 0;
70
71             // RULE: Primary key class must defined HashCode() method
72
try {
73             Context context = getVerifierContext();
74             ClassLoader JavaDoc jcl = context.getClassLoader();
75             // retrieve the EJB primary key class
76
Class JavaDoc c = Class.forName(((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName(), false, getVerifierContext().getClassLoader());
77             Method methods[] = c.getDeclaredMethods();
78             for (int i=0; i< methods.length; i++)
79                 {
80                 if (methods[i].getName().equals("hashCode")){
81                     // this is the right primary key class method hashCode()
82
hasDefinedHashCodeMethod = true;
83                     // used in output below
84
lc = i;
85                     break;
86                 }
87                 }
88
89             if (hasDefinedHashCodeMethod)
90                 {
91                 result.addGoodDetails(smh.getLocalString
92                           ("tests.componentNameConstructor",
93                            "For [ {0} ]",
94                            new Object JavaDoc[] {compName.toString()}));
95                 result.addGoodDetails(smh.getLocalString
96                               (getClass().getName() + ".debug1",
97                                "For EJB primary key class [ {0} ]",
98                                new Object JavaDoc[] {((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName()}));
99                 result.addGoodDetails(smh.getLocalString
100                               (getClass().getName() + ".passed",
101                                "Primary key class method [ {0} ] was defined in the primary key class.",
102                                new Object JavaDoc[] {methods[lc].getName()}));
103                 } else if (!hasDefinedHashCodeMethod) {
104                 oneFailed = true;
105                 result.addErrorDetails(smh.getLocalString
106                           ("tests.componentNameConstructor",
107                            "For [ {0} ]",
108                            new Object JavaDoc[] {compName.toString()}));
109                 result.addErrorDetails(smh.getLocalString
110                               (getClass().getName() + ".debug1",
111                                "For EJB primary key class [ {0} ]",
112                                new Object JavaDoc[] {((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName()}));
113                 result.addErrorDetails(smh.getLocalString
114                                (getClass().getName() + ".failed",
115                             "Error: Primary key class method hashCode() was not defined in the primary key class."));
116                 }
117         
118             } catch (ClassNotFoundException JavaDoc e) {
119             Verifier.debug(e);
120             result.addErrorDetails(smh.getLocalString
121                            ("tests.componentNameConstructor",
122                         "For [ {0} ]",
123                         new Object JavaDoc[] {compName.toString()}));
124             result.failed(smh.getLocalString
125                       (getClass().getName() + ".failedException",
126                        "Error: Class [ {0} ] not found within bean [ {1} ]",
127                        new Object JavaDoc[] {((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName(), descriptor.getName()})
128                       );
129             }
130
131             if (oneFailed)
132             result.setStatus(result.FAILED);
133             else
134             result.setStatus(result.PASSED);
135
136         } else {
137             // not container managed, but is a entity bean
138
result.addNaDetails(smh.getLocalString
139                           ("tests.componentNameConstructor",
140                            "For [ {0} ]",
141                            new Object JavaDoc[] {compName.toString()}));
142             result.notApplicable(smh.getLocalString
143                      (getClass().getName() + ".notApplicable2",
144                       "Bean [ {0} ] is not {1} managed, it is [ {2} ] managed.",
145                       new Object JavaDoc[] {descriptor.getName(),EjbDescriptor.CONTAINER_TRANSACTION_TYPE,transactionType}));
146         }
147
148         return result;
149
150     } else {
151         result.addNaDetails(smh.getLocalString
152                           ("tests.componentNameConstructor",
153                            "For [ {0} ]",
154                            new Object JavaDoc[] {compName.toString()}));
155         result.notApplicable(smh.getLocalString
156                  (getClass().getName() + ".notApplicable",
157                   "[ {0} ] expected {1} bean, but called with {2} bean.",
158                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
159         return result;
160     }
161     }
162 }
163
Popular Tags