KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > pksinglefield > PrimekeyFieldPrimaryKeyType


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.pksinglefield;
24
25 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
26 import java.lang.reflect.Field JavaDoc;
27 import java.util.*;
28 import java.lang.ClassLoader JavaDoc;
29 import com.sun.enterprise.tools.verifier.tests.*;
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  * The type of the primkey-field must be the same as the primary key type.
36  */

37 public class PrimekeyFieldPrimaryKeyType extends EjbTest implements EjbCheck {
38
39
40     /**
41      * The type of the primkey-field must be the same as the primary key type.
42      *
43      * @param descriptor the Enterprise Java Bean deployment descriptor
44      *
45      * @return <code>Result</code> the results for this assertion
46      */

47     public Result check(EjbDescriptor descriptor) {
48
49     Result result = getInitializedResult();
50     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
51
52     // The type of the primkey-field must be the same as the primary key type.
53
if (descriptor instanceof EjbEntityDescriptor) {
54         String JavaDoc persistence =
55         ((EjbEntityDescriptor)descriptor).getPersistenceType();
56
57         if (EjbEntityDescriptor.CONTAINER_PERSISTENCE.equals(persistence)) {
58         try {
59             Context context = getVerifierContext();
60         ClassLoader JavaDoc jcl = context.getClassLoader();
61             Class JavaDoc c = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
62             try {
63             if (((EjbCMPEntityDescriptor)descriptor).getPrimaryKeyFieldDesc() != null) {
64                 Field JavaDoc pkf = c.getDeclaredField(((EjbCMPEntityDescriptor)descriptor).getPrimaryKeyFieldDesc().getName());
65                 Class JavaDoc pkfType = pkf.getType();
66                 try {
67                 String JavaDoc primkey =
68                     ((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName();
69     
70                 boolean foundMatch = false;
71                 if (primkey.equals(pkfType.getName())) {
72                     foundMatch = true;
73                 } else {
74                     foundMatch = false;
75                 }
76       
77                 if (foundMatch) {
78                     result.addGoodDetails(smh.getLocalString
79                               ("tests.componentNameConstructor",
80                                "For [ {0} ]",
81                                new Object JavaDoc[] {compName.toString()}));
82                     result.passed(smh.getLocalString
83                           (getClass().getName() + ".passed",
84                            "The type of the primkey-field [ {0} ] is the same as the primary key type [ {1} ] for bean [ {2} ]",
85                            new Object JavaDoc[] {((EjbCMPEntityDescriptor)descriptor).getPrimaryKeyFieldDesc().getName(),primkey,descriptor.getName()}));
86                 } else {
87                     result.addErrorDetails(smh.getLocalString
88                                ("tests.componentNameConstructor",
89                                 "For [ {0} ]",
90                                 new Object JavaDoc[] {compName.toString()}));
91                     result.failed(smh.getLocalString
92                           (getClass().getName() + ".failed",
93                            "The type of the primkey-field [ {0} ] is not the same as the primary key type [ {1} ] for bean [ {2} ]",
94                            new Object JavaDoc[] {((EjbCMPEntityDescriptor)descriptor).getPrimaryKeyFieldDesc().getName(),primkey,descriptor.getName()}));
95                 }
96                 } catch (NullPointerException JavaDoc e) {
97                 result.addNaDetails(smh.getLocalString
98                             ("tests.componentNameConstructor",
99                              "For [ {0} ]",
100                              new Object JavaDoc[] {compName.toString()}));
101                 result.notApplicable(smh.getLocalString
102                              (getClass().getName() + ".notApplicable2",
103                               "Primkey field not defined for [ {0} ] bean.",
104                               new Object JavaDoc[] {descriptor.getName()}));
105                 }
106             } else {
107                 result.addNaDetails(smh.getLocalString
108                         ("tests.componentNameConstructor",
109                          "For [ {0} ]",
110                          new Object JavaDoc[] {compName.toString()}));
111                 result.notApplicable(smh.getLocalString
112                          (getClass().getName() + ".notApplicable2",
113                           "Primkey field not defined for [ {0} ] bean.",
114                           new Object JavaDoc[] {descriptor.getName()}));
115             }
116             } catch (NullPointerException JavaDoc e) {
117             result.addNaDetails(smh.getLocalString
118                         ("tests.componentNameConstructor",
119                          "For [ {0} ]",
120                          new Object JavaDoc[] {compName.toString()}));
121             result.notApplicable(smh.getLocalString
122                          (getClass().getName() + ".notApplicable3",
123                           "Primkey field not defined within [ {0} ] bean.",
124                           new Object JavaDoc[] {descriptor.getName()}));
125             } catch (NoSuchFieldException JavaDoc e) {
126             result.addNaDetails(smh.getLocalString
127                         ("tests.componentNameConstructor",
128                          "For [ {0} ]",
129                          new Object JavaDoc[] {compName.toString()}));
130             result.notApplicable(smh.getLocalString
131                          (getClass().getName() + ".notApplicable2",
132                           "Primkey field [ {0} ] not defined within [ {1} ] bean.",
133                           new Object JavaDoc[] {((EjbCMPEntityDescriptor)descriptor).getPrimaryKeyFieldDesc().getName(),descriptor.getName()}));
134             }
135         } catch (ClassNotFoundException JavaDoc e) {
136             Verifier.debug(e);
137             result.addErrorDetails(smh.getLocalString
138                        ("tests.componentNameConstructor",
139                         "For [ {0} ]",
140                         new Object JavaDoc[] {compName.toString()}));
141             result.failed(smh.getLocalString
142                   (getClass().getName() + ".failedException",
143                    "Error: EJB class [ {0} ] does not exist or is not loadable within bean [ {1} ]",
144                    new Object JavaDoc[] {descriptor.getEjbClassName(),descriptor.getName()}));
145         }
146         } else {
147         result.addNaDetails(smh.getLocalString
148                     ("tests.componentNameConstructor",
149                      "For [ {0} ]",
150                      new Object JavaDoc[] {compName.toString()}));
151         result.notApplicable(smh.getLocalString
152                      (getClass().getName() + ".notApplicable1",
153                       "Expected persistence type [ {0} ], but bean [ {1} ] has persistence type [ {2} ]",
154                       new Object JavaDoc[] {EjbEntityDescriptor.CONTAINER_PERSISTENCE,descriptor.getName(),persistence}));
155         }
156     } else {
157         result.addNaDetails(smh.getLocalString
158                 ("tests.componentNameConstructor",
159                  "For [ {0} ]",
160                  new Object JavaDoc[] {compName.toString()}));
161         result.notApplicable(smh.getLocalString
162                  (getClass().getName() + ".notApplicable",
163                   "{0} expected \n {1} bean, but called with {2} bean",
164                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
165     }
166
167     return result;
168     }
169 }
170
Popular Tags