KickJava   Java API By Example, From Geeks To Geeks.

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


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 and equals() methods test.
37  *
38  * Enterprise Bean's primary key class
39  * The class must provide suitable implementation of the equals(Object other)
40  * methods to simplify the management of the primary keys by client code.
41  *
42  */

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