KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > session > stateless > StatelessCreateOnlyOne


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.session.stateless;
24
25 import java.lang.reflect.Method JavaDoc;
26
27 import com.sun.enterprise.deployment.EjbDescriptor;
28 import com.sun.enterprise.deployment.EjbSessionDescriptor;
29 import com.sun.enterprise.tools.verifier.Result;
30 import com.sun.enterprise.tools.verifier.Verifier;
31 import com.sun.enterprise.tools.verifier.tests.ComponentNameConstructor;
32 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
33 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
34
35 /**
36  * Stateless session beans home interface create method test.
37  * The home interface of a stateless session Bean must have a create method.
38  * The home interface must not have any other create methods.
39  */

40 public class StatelessCreateOnlyOne extends EjbTest implements EjbCheck {
41     Result result = null;
42     ComponentNameConstructor compName = null;
43
44     /**
45      * Stateless session beans home interface create method test.
46      * The home interface of a stateless session Bean must have a create method.
47      * The home interface must not have any other create methods.
48      *
49      * @param descriptor the Enterprise Java Bean deployment descriptor
50      *
51      * @return <code>Result</code> the results for this assertion
52      */

53     public Result check(EjbDescriptor descriptor) {
54
55         result = getInitializedResult();
56         compName = getVerifierContext().getComponentNameConstructor();
57
58         if (descriptor instanceof EjbSessionDescriptor) {
59             String JavaDoc stateType = ((EjbSessionDescriptor)descriptor).getSessionType();
60             if (EjbSessionDescriptor.STATELESS.equals(stateType)) {
61                 // RULE: Stateless session are only allowed to have create
62
// methods with no arguments, and returns the session Bean's
63
// remote interface. The home interface must not have any
64
// other create methods.
65
if(descriptor.getHomeClassName() != null && !"".equals(descriptor.getHomeClassName()))
66                     commonToBothInterfaces(descriptor.getHomeClassName(),(EjbSessionDescriptor)descriptor);
67                 if(descriptor.getLocalHomeClassName() != null && !"".equals(descriptor.getLocalHomeClassName()))
68                     commonToBothInterfaces(descriptor.getLocalHomeClassName(),(EjbSessionDescriptor)descriptor);
69             }
70         }
71         if (result.getStatus() != Result.FAILED) {
72             addGoodDetails(result, compName);
73             result.passed(smh.getLocalString
74                     (getClass().getName() + ".passed",
75                             "The bean's Home Interface has exactly one create Method defined"));
76         }
77         return result;
78     }
79
80     /**
81      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
82      * @param descriptor the Enterprise Java Bean deployment descriptor
83      * @param home for the Home interface of the Ejb.
84      */

85
86     private void commonToBothInterfaces(String JavaDoc home, EjbSessionDescriptor descriptor) {
87         try {
88             int count = 0;
89             Class JavaDoc c = Class.forName(home, false, getVerifierContext().getClassLoader());
90             for(Method JavaDoc methods : c.getDeclaredMethods()) {
91                 if (methods.getName().equals("create"))
92                     count++;
93             }
94             if(count!=1) {
95                 addErrorDetails(result, compName);
96                 result.failed(smh.getLocalString
97                         (getClass().getName() + ".failed",
98                                 "Error: [ {0} ] Create methods exists within bean [ {1} ]. " +
99                         "The home interface must have only one create method for stateless session bean.",
100                                 new Object JavaDoc[] {new Integer JavaDoc(count),home}));
101             }
102         } catch (ClassNotFoundException JavaDoc e) {
103             Verifier.debug(e);
104             result.addErrorDetails(smh.getLocalString
105                     ("tests.componentNameConstructor",
106                             "For [ {0} ]",
107                             new Object JavaDoc[] {compName.toString()}));
108             result.failed(smh.getLocalString
109                     (getClass().getName() + ".failedException",
110                             "Error: Class [ {0} ] not found within bean [ {1} ]",
111                             new Object JavaDoc[] {home, descriptor.getName()}));
112         }
113     }
114 }
115
Popular Tags