KickJava   Java API By Example, From Geeks To Geeks.

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


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 constuctor test.
36  * The primary key class must have a public constructor that takes no
37  * parameters.
38  */

39 public class PrimaryKeyClassConstructor extends EjbTest implements EjbCheck {
40
41
42     /**
43      * Enterprise Java Bean primary key class constuctor test.
44      * The primary key class must have a public constructor that takes no
45      * parameters.
46      *
47      * @param descriptor the Enterprise Java Bean deployment descriptor
48      *
49      * @return <code>Result</code> the results for this assertion
50      */

51     public Result check(EjbDescriptor descriptor) {
52
53     Result result = getInitializedResult();
54     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
55
56     if (descriptor instanceof EjbEntityDescriptor) {
57         String JavaDoc persistence =
58         ((EjbEntityDescriptor)descriptor).getPersistenceType();
59         if (EjbEntityDescriptor.CONTAINER_PERSISTENCE.equals(persistence)) {
60         // do we have primekey that maps to single or multiple fields in entity // bean class? if primekey-field exist, then primekey maps to single
61
// field in entity bean class and this test in notApplicable
62
try {
63                     FieldDescriptor fd = ((EjbCMPEntityDescriptor)descriptor).getPrimaryKeyFieldDesc();
64                     if (fd != null) {
65                         String JavaDoc pkf = fd.getName();
66                         if (pkf.length() > 0) {
67                 // N/A case
68
result.addNaDetails(smh.getLocalString
69                        ("tests.componentNameConstructor",
70                     "For [ {0} ]",
71                     new Object JavaDoc[] {compName.toString()}));
72                 result.notApplicable(smh.getLocalString
73                              (getClass().getName() + ".notApplicable2",
74                               "Entity Bean [ {0} ] with primekey-field non-blank, test not applicable.",
75                               new Object JavaDoc[] {descriptor.getEjbClassName()}));
76                         }
77             } else {
78             try {
79                 Context context = getVerifierContext();
80                 ClassLoader JavaDoc jcl = context.getClassLoader();
81                 Class JavaDoc c = Class.forName(((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName(), false, getVerifierContext().getClassLoader());
82       
83                 boolean foundOne = false;
84                 Constructor [] constructors = c.getConstructors();
85                 for (int i = 0; i < constructors.length; i++) {
86                 int modifiers = constructors[i].getModifiers();
87                 if (Modifier.isPublic(modifiers)) {
88                     Class JavaDoc [] constructorParameterTypes;
89                     constructorParameterTypes = constructors[i].getParameterTypes();
90                     if (constructorParameterTypes.length > 0) {
91                     continue;
92                     } else {
93                     foundOne = true;
94                     break;
95                     }
96                 }
97                 }
98           
99                 if (foundOne) {
100                 result.addGoodDetails(smh.getLocalString
101                        ("tests.componentNameConstructor",
102                     "For [ {0} ]",
103                     new Object JavaDoc[] {compName.toString()}));
104                 result.passed(smh.getLocalString
105                           (getClass().getName() + ".passed",
106                            "This primary key class [ {0} ] has a public constructor method with no parameters.",
107                            new Object JavaDoc[] {((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName()}));
108                 } else {
109                 result.addErrorDetails(smh.getLocalString
110                        ("tests.componentNameConstructor",
111                     "For [ {0} ]",
112                     new Object JavaDoc[] {compName.toString()}));
113                 result.failed(smh.getLocalString
114                           (getClass().getName() + ".failed",
115                            "Error: A public constructor method with no parameters was not found in the primary key class [ {0} ].",
116                            new Object JavaDoc[] {((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName()}));
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: [ {0} ] class not found.",
127                        new Object JavaDoc[] {((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName()}));
128             }
129             }
130         } catch (NullPointerException JavaDoc e) {
131             result.addErrorDetails(smh.getLocalString
132                        ("tests.componentNameConstructor",
133                     "For [ {0} ]",
134                     new Object JavaDoc[] {compName.toString()}));
135             result.failed(smh.getLocalString
136                      (getClass().getName() + ".failedException2",
137                       "Error: Primkey field not defined within [ {0} ] bean.",
138                       new Object JavaDoc[] {descriptor.getName()}));
139         }
140         return result;
141  
142         } else { //if (BEAN_PERSISTENCE.equals(persistence)) {
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() + ".notApplicable1",
149                       "Expected [ {0} ] managed persistence, but [ {1} ] bean has [ {2} ] managed persistence.",
150                       new Object JavaDoc[] {EjbEntityDescriptor.CONTAINER_PERSISTENCE,descriptor.getName(),persistence}));
151         return result;
152         }
153
154     } else {
155         result.addNaDetails(smh.getLocalString
156                        ("tests.componentNameConstructor",
157                     "For [ {0} ]",
158                     new Object JavaDoc[] {compName.toString()}));
159         result.notApplicable(smh.getLocalString
160                  (getClass().getName() + ".notApplicable",
161                   "{0} expected {1} bean, but called with {2}.",
162                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
163         return result;
164     }
165     }
166 }
167
Popular Tags