KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
33 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
34 import com.sun.enterprise.tools.verifier.tests.ejb.EjbUtils;
35
36 /**
37  * Enterprise Java Bean primary key class fields subset of the names of
38  * the container-managed fields test.
39  *
40  * The names of the fields in the primary key class must be a subset of the
41  * names of the container-managed fields. (This allows the container to
42  * extract the primary key fields from an instance's container-managed fields,
43  * and vice versa.)
44  */

45 public class PrimaryKeyClassFieldsCmp extends EjbTest implements EjbCheck {
46
47
48     /**
49      * Enterprise Java Bean primary key class fields subset of the names of
50      * the container-managed fields test.
51      *
52      * The names of the fields in the primary key class must be a subset of the
53      * names of the container-managed fields. (This allows the container to
54      * extract the primary key fields from an instance's container-managed fields,
55      * and vice versa.)
56      *
57      * @param descriptor the Enterprise Java Bean deployment descriptor
58      *
59      * @return <code>Result</code> the results for this assertion
60      */

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