KickJava   Java API By Example, From Geeks To Geeks.

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


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 modifier test.
36  * The class must be defined as public
37  */

38 public class PrimaryKeyClassModifier extends EjbTest implements EjbCheck {
39
40
41     /**
42      * Enterprise Java Bean primary key class modifier test.
43      * The class must be defined 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 isPublic = false;
82                 int modifiers = c.getModifiers();
83                 if (Modifier.isPublic(modifiers)) {
84                 isPublic = true;
85                 }
86           
87                 if (isPublic) {
88                 result.addGoodDetails(smh.getLocalString
89                               ("tests.componentNameConstructor",
90                                "For [ {0} ]",
91                                new Object JavaDoc[] {compName.toString()}));
92                 result.passed(smh.getLocalString
93                           (getClass().getName() + ".passed",
94                            "[ {0} ] properly declares public class modifier.",
95                            new Object JavaDoc[] {((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName()}));
96                 } else if (!isPublic) {
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: Ejb primary key class [ {0} ] was found, but was not declared as public. The primary key class [ {1} ] must be defined as public.",
104                            new Object JavaDoc[] {((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName(),((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName()}));
105                 }
106             } catch (ClassNotFoundException JavaDoc e) {
107                 Verifier.debug(e);
108                 result.addErrorDetails(smh.getLocalString
109                        ("tests.componentNameConstructor",
110                     "For [ {0} ]",
111                     new Object JavaDoc[] {compName.toString()}));
112                 result.failed(smh.getLocalString
113                       (getClass().getName() + ".failedException",
114                        "Error: [ {0} ] class not found.",
115                        new Object JavaDoc[] {((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName()}));
116             }
117             }
118         } catch (NullPointerException JavaDoc 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() + ".failedException2",
125                       "Error: Primkey field not defined within [ {0} ] bean.",
126                       new Object JavaDoc[] {descriptor.getName()}));
127         }
128         return result;
129
130         } else { // if (BEAN_PERSISTENCE.equals(persistence)
131
result.addNaDetails(smh.getLocalString
132                        ("tests.componentNameConstructor",
133                     "For [ {0} ]",
134                     new Object JavaDoc[] {compName.toString()}));
135         result.notApplicable(smh.getLocalString
136                      (getClass().getName() + ".notApplicable1",
137                       "Expected [ {0} ] managed persistence, but [ {1} ] bean has [ {2} ] managed persistence.",
138                       new Object JavaDoc[] {EjbEntityDescriptor.CONTAINER_PERSISTENCE,descriptor.getName(),persistence}));
139         return result;
140         }
141  
142     } else {
143         result.addNaDetails(smh.getLocalString
144                        ("tests.componentNameConstructor",
145                     "For [ {0} ]",
146                     new Object JavaDoc[] {compName.toString()}));
147         result.notApplicable(smh.getLocalString
148                  (getClass().getName() + ".notApplicable",
149                   "{0} expected {1} bean, but called with {2}.",
150                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
151         return result;
152     }
153     }
154 }
155
Popular Tags