KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > pkmultiplefield > PrimaryKeyClassFieldsPublic


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.pkmultiplefield;
24
25 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
26 import java.lang.ClassLoader JavaDoc;
27 import com.sun.enterprise.tools.verifier.tests.*;
28 import java.lang.reflect.*;
29 import javax.ejb.EntityBean JavaDoc;
30 import com.sun.enterprise.deployment.*;
31 import com.sun.enterprise.tools.verifier.*;
32 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
33
34 /**
35  * Enterprise Java Bean primary key class public fields test.
36  * The primary key class must declare all fields within the class as public.
37  */

38 public class PrimaryKeyClassFieldsPublic extends EjbTest implements EjbCheck {
39
40
41     /**
42      * Enterprise Java Bean primary key class public fields test.
43      * The primary key class must declare all fields within the class as public.
44      *
45      * @param descriptor the Enterprise Java Bean deployment descriptor
46      *
47      * @return <code>Result</code> the results for this assertion
48      */

49     public Result check(EjbDescriptor descriptor) {
50
51     Result result = getInitializedResult();
52     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
53
54     if (descriptor instanceof EjbEntityDescriptor) {
55         String JavaDoc persistence =
56         ((EjbEntityDescriptor)descriptor).getPersistenceType();
57         if (EjbEntityDescriptor.CONTAINER_PERSISTENCE.equals(persistence)) {
58         // do we have primekey that maps to single or multiple fields in entity // bean class? if primekey-field exist, then primekey maps to single
59
// field in entity bean class and this test in notApplicable
60
try {
61                     FieldDescriptor fd = ((EjbCMPEntityDescriptor)descriptor).getPrimaryKeyFieldDesc();
62                     if (fd != null) {
63                         String JavaDoc pkf = fd.getName();
64                         if (pkf.length() > 0) {
65                 // N/A case
66
result.addNaDetails(smh.getLocalString
67                        ("tests.componentNameConstructor",
68                     "For [ {0} ]",
69                     new Object JavaDoc[] {compName.toString()}));
70                 result.notApplicable(smh.getLocalString
71                              (getClass().getName() + ".notApplicable2",
72                               "Entity Bean [ {0} ] with primekey-field non-blank, test not applicable.",
73                               new Object JavaDoc[] {descriptor.getEjbClassName()}));
74                         }
75             } else {
76             try {
77                 Context context = getVerifierContext();
78                 ClassLoader JavaDoc jcl = context.getClassLoader();
79                 Class JavaDoc c = Class.forName(((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName(), false, getVerifierContext().getClassLoader());
80       
81                 boolean oneFailed = false;
82                 boolean badField = false;
83                 Field [] fields = c.getDeclaredFields();
84                 for (int i = 0; i < fields.length; i++) {
85                 badField = false;
86                 int modifiers = fields[i].getModifiers();
87                 if (Modifier.isPublic(modifiers)) {
88                     continue;
89                 } else {
90                     if (!oneFailed) {
91                     oneFailed = true;
92                     }
93                     badField = true;
94                 }
95           
96                 if (badField) {
97                     result.addErrorDetails(smh.getLocalString
98                        ("tests.componentNameConstructor",
99                     "For [ {0} ]",
100                     new Object JavaDoc[] {compName.toString()}));
101                     result.failed(smh.getLocalString
102                           (getClass().getName() + ".failed",
103                            "Error: Field [ {0} ] defined within primary key class [ {1} ] is not defined as public.",
104                            new Object JavaDoc[] {fields[i].getName(),((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName()}));
105                 }
106                 }
107                 if (!oneFailed) {
108                 result.addGoodDetails(smh.getLocalString
109                        ("tests.componentNameConstructor",
110                     "For [ {0} ]",
111                     new Object JavaDoc[] {compName.toString()}));
112                 result.passed(smh.getLocalString
113                           (getClass().getName() + ".passed",
114                            "This primary key class [ {0} ] has defined all fields as public.",
115                            new Object JavaDoc[] {((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName()}));
116                 }
117             } catch (ClassNotFoundException JavaDoc e) {
118                 Verifier.debug(e);
119                 result.addErrorDetails(smh.getLocalString
120                        ("tests.componentNameConstructor",
121                     "For [ {0} ]",
122                     new Object JavaDoc[] {compName.toString()}));
123                 result.failed(smh.getLocalString
124                       (getClass().getName() + ".failedException",
125                        "Error: [ {0} ] class not found.",
126                        new Object JavaDoc[] {((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName()}));
127                         } catch (Throwable JavaDoc t) {
128                 result.addWarningDetails(smh.getLocalString
129                        ("tests.componentNameConstructor",
130                     "For [ {0} ]",
131                     new Object JavaDoc[] {compName.toString()}));
132                             result.warning(smh.getLocalString
133                                 (getClass().getName() + ".warningException",
134                                  "Warning: [ {0} ] class encountered [ {1} ]. Cannot access fields of class [ {2} ] which is external to [ {3} ].",
135                                  new Object JavaDoc[] {(descriptor).getEjbClassName(),t.toString(), t.getMessage(), descriptor.getEjbBundleDescriptor().getModuleDescriptor().getArchiveUri()}));
136             }
137             }
138         } catch (NullPointerException JavaDoc e) {
139             result.addErrorDetails(smh.getLocalString
140                        ("tests.componentNameConstructor",
141                     "For [ {0} ]",
142                     new Object JavaDoc[] {compName.toString()}));
143             result.failed(smh.getLocalString
144                      (getClass().getName() + ".failedException2",
145                       "Error: Primkey field not defined within [ {0} ] bean.",
146                       new Object JavaDoc[] {descriptor.getName()}));
147         }
148         return result;
149
150         } else { //if (BEAN_PERSISTENCE.equals(persistence)
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() + ".notApplicable1",
157                       "Expected [ {0} ] managed persistence, but [ {1} ] bean has [ {2} ] managed persistence.",
158                       new Object JavaDoc[] {EjbEntityDescriptor.CONTAINER_PERSISTENCE,descriptor.getName(),persistence}));
159         return result;
160         }
161  
162     } else {
163         result.addNaDetails(smh.getLocalString
164                        ("tests.componentNameConstructor",
165                     "For [ {0} ]",
166                     new Object JavaDoc[] {compName.toString()}));
167         result.notApplicable(smh.getLocalString
168                  (getClass().getName() + ".notApplicable",
169                   "{0} expected {1} bean, but called with {2}.",
170                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
171         return result;
172     }
173     }
174 }
175
Popular Tags