KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > ias > ASEjbIsReadOnlyBean


1 package com.sun.enterprise.tools.verifier.tests.ejb.ias;
2
3 /*
4  * The contents of this file are subject to the terms
5  * of the Common Development and Distribution License
6  * (the License). You may not use this file except in
7  * compliance with the License.
8  *
9  * You can obtain a copy of the license at
10  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
11  * glassfish/bootstrap/legal/CDDLv1.0.txt.
12  * See the License for the specific language governing
13  * permissions and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL
16  * Header Notice in each file and include the License file
17  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
18  * If applicable, add the following below the CDDL Header,
19  * with the fields enclosed by brackets [] replaced by
20  * you own identifying information:
21  * "Portions Copyrighted [year] [name of copyright owner]"
22  *
23  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24  */

25
26 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
27 import java.util.*;
28 import com.sun.enterprise.deployment.EjbDescriptor;
29 import com.sun.enterprise.deployment.EjbSessionDescriptor;
30 import com.sun.enterprise.tools.verifier.*;
31 import com.sun.enterprise.tools.verifier.tests.*;
32
33 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
34
35 import com.sun.enterprise.tools.common.dd.ejb.SunEjbJar;
36 import com.sun.enterprise.tools.common.dd.ejb.EnterpriseBeans;
37 import com.sun.enterprise.tools.common.dd.ejb.Ejb;
38 import com.sun.enterprise.deployment.EjbEntityDescriptor;
39 import com.sun.enterprise.deployment.EjbSessionDescriptor;
40 import com.sun.enterprise.deployment.EjbMessageBeanDescriptor;
41 import java.lang.reflect.*;
42 /** ejb [0,n]
43  * is-read-only-bean ? [String]
44  *
45  * The <B>is-read-only-bean</B> should be defined for entity beans only.
46  * Also the implementation should not define ant create and remove methods.
47  * This is recommended and not required
48  * @author Irfan Ahmed
49  */

50 public class ASEjbIsReadOnlyBean extends EjbTest implements EjbCheck {
51
52     boolean oneFailed= false;
53     boolean oneWarning = false;
54
55     /**
56      *
57      * @param descriptor the Enterprise Java Bean deployment descriptor
58      * @return <code>Result</code> the results for this assertion
59      */

60     public Result check(EjbDescriptor descriptor) {
61
62     Result result = getInitializedResult();
63     ComponentNameConstructor compName = new ComponentNameConstructor(descriptor);
64
65         SunEjbJar ejbJar = descriptor.getEjbBundleDescriptor().getIasEjbObject();
66         String JavaDoc ejbName = null;
67         Ejb testCase = null;
68         if(ejbJar!=null)
69         {
70             testCase = getEjb(descriptor.getName(),ejbJar);
71             String JavaDoc isReadOnlyBean = testCase.getIsReadOnlyBean();
72             if(isReadOnlyBean != null)
73             {
74                 if(isReadOnlyBean.length()==0)
75                 {
76                     oneFailed = true;
77                      result.failed(smh.getLocalString(getClass().getName()+".failed",
78                         "FAILED [AS-EJB ejb] : is-read-only-bean cannot be an empty string"));
79                 }
80                 else
81                 {
82                     if(!isReadOnlyBean.equals("true") && !isReadOnlyBean.equals("false"))
83                     {
84                         oneFailed = true;
85                         result.failed(smh.getLocalString(getClass().getName()+".failed1",
86                             "FAILED [AS-EJB ejb] : is-read-only-bean cannot be {0}. It can only be true or false",
87                             new Object JavaDoc[]{isReadOnlyBean}));
88                     }
89                     else
90                     {
91                         if(isReadOnlyBean.equals("true"))
92                             testROBSpecific(descriptor,testCase,result);
93                         else
94                         {
95                             result.passed(smh.getLocalString(getClass().getName()+".passed",
96                             "PASSED [AS-EJB ejb] is-read-only-bean is {0}",
97                             new Object JavaDoc[]{isReadOnlyBean}));
98                         }
99                     }
100                 }
101                 if(oneFailed)
102                     result.setStatus(Result.FAILED);
103                 else if(oneWarning)
104                     result.setStatus(Result.WARNING);
105             }
106             else
107             {
108                 result.notApplicable(smh.getLocalString(getClass().getName()+".notApplicable",
109                     "NOT APPLICABLE [AS-EJB ejb] is-read-only-bean Element not defined"));
110             }
111         }
112         else
113         {
114             result.addErrorDetails(smh.getLocalString
115                                    ("tests.componentNameConstructor",
116                                     "For [ {0} ]",
117                                     new Object JavaDoc[] {compName.toString()}));
118             result.addErrorDetails(smh.getLocalString
119                  (getClass().getName() + ".notRun",
120                   "NOT RUN [AS-EJB] : Could not create an SunEjbJar object"));
121         }
122         return result;
123     }
124     
125     public void testROBSpecific(EjbDescriptor descriptor, Ejb testCase,Result result)
126     {
127         //Read only Beans can only be Entity Beans
128
if(descriptor instanceof EjbEntityDescriptor)
129         {
130             result.passed(smh.getLocalString(getClass().getName()+".passed1",
131                                     "PASSED [AS-EJB ejb] : Read Only Beans can only be Entity Beans"));
132         }
133         else if(descriptor instanceof EjbSessionDescriptor)
134         {
135             oneFailed = true;
136             result.failed(smh.getLocalString(getClass().getName()+".failed2",
137                 "FAILED [AS-EJB ejb] : Read Only Beans cannot be Session Beans. They can only be Entity Beans"));
138             return;
139         }
140         else if(descriptor instanceof EjbMessageBeanDescriptor)
141         {
142             oneFailed = true;
143             result.failed(smh.getLocalString(getClass().getName()+".failed3",
144                 "FAILED [AS-EJB ejb] : Read Only Beans cannot be Message Driven Beans. They can only be Entity Beans"));
145             return;
146         }
147
148         //Only Container Managed Transactions are Allowed
149
String JavaDoc txnType = descriptor.getTransactionType();
150         if(txnType.equals(descriptor.CONTAINER_TRANSACTION_TYPE))
151         {
152             result.passed(smh.getLocalString(getClass().getName()+".passed2",
153                                     "PASSED [AS-EJB ejb] : Read Only Beans can only have Container Managed Transactions"));
154         }
155         else
156         {
157             oneFailed = true;
158             result.failed(smh.getLocalString(getClass().getName()+".failed4",
159                 "FAILED [AS-EJB ejb] : Read Only Beans cannot have Bean Managed Transactions"));
160         }
161
162         //Encourage not to have create/remove methods in Home Interface
163
String JavaDoc homeName = descriptor.getHomeClassName();
164         Class JavaDoc homeClass = null;
165         boolean foundCreateMethod = false;
166         boolean foundRemoveMethod = false;
167         try
168         {
169             homeClass = getVerifierContext().getClassLoader().loadClass(homeName);
170             Method methods[] = homeClass.getMethods();
171             for(int i=0;i<methods.length;i++)
172             {
173                 if(methods[i].getName().startsWith("create"))
174                 {
175                     foundCreateMethod = true;
176                     break;
177                 }
178             }
179
180             for(int i=0;i<methods.length;i++)
181             {
182                if(methods[i].getName().startsWith("remove"))
183                {
184                     foundRemoveMethod = true;
185                     break;
186                }
187             }
188             if(foundCreateMethod)
189             {
190                 oneWarning = true;
191                 result.addWarningDetails(smh.getLocalString(getClass().getName()+".warning1",
192                     "WARNING [AS-EJB ejb] : Read Only Beans should have zero create Methods"));
193             }
194             else
195                 result.passed(smh.getLocalString(getClass().getName()+".passed3",
196                     "PASSED [AS-EJB ejb] : Read Only Bean has zero create Methods"));
197
198             if(foundRemoveMethod)
199             {
200                 oneWarning = true;
201                 result.addWarningDetails(smh.getLocalString(getClass().getName()+".warning2",
202                     "WARNING [AS-EJB ejb] : Read Only Beans should have zero remove Methods"));
203             }
204             else
205             {
206                 result.passed(smh.getLocalString(getClass().getName()+".passed4",
207                     "PASSED [AS-EJB ejb] : Read Only Bean has zero remove Methods"));
208             }
209
210             //Refresh Period Test
211
String JavaDoc refreshPeriod = testCase.getRefreshPeriodInSeconds();
212             if(refreshPeriod!=null)
213             {
214                 if(refreshPeriod.length()==0)
215                 {
216                     oneFailed = true;
217                     result.failed(smh.getLocalString(getClass().getName()+".failed5",
218                         "FAILED [AS-EJB ejb] : refresh-period-in-seconds is empty. It should be between 0 and {0}",new Object JavaDoc[]{new Long JavaDoc( Long.MAX_VALUE)}));
219                 }
220                 else
221                 {
222                     try
223                     {
224                         long refValue = Long.valueOf(refreshPeriod).longValue();
225                         if(refValue <0 || refValue > Long.MAX_VALUE)
226                         {
227                             result.failed(smh.getLocalString(getClass().getName()+".failed6",
228                                 "FAILED [AS-EJB ejb] : refresh-period-in-seconds is invalid. It should be between 0 and {0}." ,new Object JavaDoc[]{new Long JavaDoc( Long.MAX_VALUE)}));
229                         }
230                         else
231                             result.passed(smh.getLocalString(getClass().getName()+".passed5",
232                                 "PASSED [AS-EJB ejb] : refresh-period-in-seconds is {0}",new Object JavaDoc[]{new Long JavaDoc(refValue)}));
233                     }
234                     catch(NumberFormatException JavaDoc nfex)
235                     {
236                         Verifier.debug(nfex);
237                         result.failed(smh.getLocalString(getClass().getName()+".failed6",
238                             "FAILED [AS-EJB ejb] : refresh-period-in-seconds is invalid. It should be between 0 and {0}", new Object JavaDoc[]{new Long JavaDoc(Long.MAX_VALUE)} ));
239                     }
240                 }
241             }
242
243             if(oneFailed)
244                 result.setStatus(Result.FAILED);
245         }
246         catch(ClassNotFoundException JavaDoc cfne)
247         {
248             Verifier.debug(cfne);
249             result.addErrorDetails(smh.getLocalString
250                            ("tests.componentNameConstructor",
251                             "For [ {0} ]",
252                             new Object JavaDoc[] {(new ComponentNameConstructor(descriptor)).toString()}));
253             result.failed(smh.getLocalString
254                   (getClass().getName() + ".failed7",
255                    "Error: Home interface [ {0} ] does not exist or is not loadable within bean [ {1} ]",
256                    new Object JavaDoc[] {homeName, descriptor.getName()}));
257         }
258     }
259 }
260
261
Popular Tags