KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > stateless > containermanaged > callbacklogger > SLSBOperationLoggerAccess


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: SLSBOperationLoggerAccess.java 823 2006-07-05 12:21:09Z studzine $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.callbacklogger;
26
27 import static java.util.Arrays.sort JavaDoc;
28 import static org.objectweb.easybeans.tests.common.helper.ListHelper.convertListType;
29 import static org.testng.Assert.assertTrue;
30
31 import java.util.List JavaDoc;
32
33 import javax.ejb.Remote JavaDoc;
34 import javax.ejb.Stateless JavaDoc;
35 import javax.persistence.EntityManager;
36 import javax.persistence.EntityManagerFactory;
37 import javax.persistence.PersistenceUnit;
38 import javax.persistence.Query;
39
40 import org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.CallbackLogger;
41 import org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.CallbackType;
42 import org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationLogger;
43 import org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType;
44
45 /**
46  * Accesses the entity bean that is responsible for registering operations.
47  * @author Gisele Pinheiro Souza
48  * @author Eduardo Studzinski Estima de Castro
49  */

50 @Stateless JavaDoc(name = "SLSBOperationLoggerAccess")
51 @Remote JavaDoc(ItfOperationLoggerAccess.class)
52 public class SLSBOperationLoggerAccess extends CallbackLoggerAccessBase implements ItfOperationLoggerAccess {
53
54     /**
55      * The Entity Manager Factory used during the tests.
56      */

57     @PersistenceUnit
58     private EntityManagerFactory entityManagerFactory;
59
60     /**
61      * Constant.
62      */

63     private static final String JavaDoc UNDEFINED = "Undefined.";
64
65     /**
66      * Creates an instance of OperationLogger with the parameters and the
67      * current time.
68      * @param className the bean class.
69      * @param event the event type.
70      * @param eventClassName the name of the class where the event is defined.
71      * @param operationType the operation type.
72      * @param description the operation description.
73      */

74     public void insertOperationLogger(final String JavaDoc className, final CallbackType event, final String JavaDoc eventClassName,
75             final OperationType operationType, final String JavaDoc description) {
76         EntityManager entityManager = entityManagerFactory.createEntityManager();
77
78         OperationLogger logger = new OperationLogger();
79         logger.setCallbackClassName(eventClassName);
80         logger.setCallbackEvent(event);
81         logger.setClassName(className);
82         logger.setInsertionDate(getTime());
83         logger.setOperationType(operationType);
84         logger.setDescription(description);
85         entityManager.persist(logger);
86         entityManager.flush();
87     }
88
89     /**
90      * Creates an instance of OperationLogger with the parameters and the
91      * current time.
92      * @param className the bean class.
93      * @param event the event type.
94      * @param eventClassName the name of the class where the event is defined.
95      * @param operationType the operation type.
96      */

97     public void insertOperationLogger(final String JavaDoc className, final CallbackType event, final String JavaDoc eventClassName,
98             final OperationType operationType) {
99         this.insertOperationLogger(className, event, eventClassName, operationType, UNDEFINED);
100     }
101
102     /**
103      * Verifies if the events for an operation were called and if the invocation
104      * order is correct.
105      * @param className the bean class.
106      * @param event the event type.
107      * @param eventClassNames the list of where the event methods are defined in
108      * the correct invocation order.
109      * @param operationType the operation type.
110      */

111     public void verifyOperation(final String JavaDoc className, final CallbackType event, final String JavaDoc[] eventClassNames,
112             final OperationType operationType) {
113
114         this.verifyOperation(className, event, eventClassNames, operationType,
115                 getArrayUndefined(eventClassNames.length));
116     }
117
118     /**
119      * Verifies if the events for an operation were called and if the invocation
120      * order is correct.
121      * @param className the bean class.
122      * @param event the event type.
123      * @param eventClassNames the list of where the event methods are defined in
124      * the correct invocation order.
125      * @param operationType the operation type.
126      */

127     public void verifyOperation(final Class JavaDoc className, final CallbackType event, final String JavaDoc[] eventClassNames,
128             final OperationType operationType) {
129         this.verifyOperation(className.getName(), event, eventClassNames, operationType,
130                 getArrayUndefined(eventClassNames.length));
131     }
132
133     /**
134      * Verifies if the events for an operation were called and if the invocation
135      * order is correct.
136      * @param className the bean class.
137      * @param event the event type.
138      * @param eventClassNames the list of where the event methods are defined in
139      * the correct invocation order.
140      * @param operationType the operation type.
141      * @param descriptions the operations description.
142      */

143     public void verifyOperation(final Class JavaDoc className, final CallbackType event, final String JavaDoc[] eventClassNames,
144             final OperationType operationType, final String JavaDoc[] descriptions) {
145         this.verifyOperation(className.getName(), event, eventClassNames, operationType, descriptions);
146     }
147
148     /**
149      * Verifies if the events for an operation were called and if the invocation
150      * order is correct.
151      * @param className the bean class.
152      * @param event the event type.
153      * @param eventClassNames the list of where the event methods are defined in
154      * the correct invocation order.
155      * @param operationType the operation type.
156      * @param descriptions the operations description.
157      */

158     public void verifyOperation(final String JavaDoc className, final CallbackType event, final String JavaDoc[] eventClassNames,
159             final OperationType operationType, final String JavaDoc[] descriptions) {
160
161         try {
162             Thread.sleep(WAIT);
163         } catch (InterruptedException JavaDoc e1) {
164             e1.printStackTrace();
165         }
166
167         // Checks if the size of descriptions is the same as eventClassNames.
168
assertTrue(descriptions.length == eventClassNames.length,
169                 "The length of descriptions and event class names must be equal.");
170
171         EntityManager entityManager = entityManagerFactory.createEntityManager();
172         Query query = entityManager.createQuery("SELECT e FROM OperationLogger "
173                 + "e WHERE e.className = :className AND e.callbackEvent= :event AND e.operationType = :operationType");
174         query.setParameter("className", className);
175         query.setParameter("event", event);
176         query.setParameter("operationType", operationType);
177         List JavaDoc callbackList = query.getResultList();
178
179         assertTrue(callbackList.size() == eventClassNames.length,
180                 "The length of operations expected is different from the length of operations found.");
181
182         // Sorts the events by date.
183
if (callbackList.size() != 0) {
184             OperationLogger[] lstManager = new OperationLogger[callbackList.size()];
185             try {
186                 lstManager = convertListType(callbackList).toArray(lstManager);
187             } catch (Exception JavaDoc e) {
188                 throw new RuntimeException JavaDoc(e);
189             }
190             sort(lstManager, new CallbackLoggerComparator<CallbackLogger>());
191             for (int i = 0; i < lstManager.length; i++) {
192                 if (!(lstManager[i].getCallbackClassName().equals(eventClassNames[i]) & lstManager[i].getDescription()
193                         .equals(descriptions[i]))) {
194                     // The event class name and the operation description must
195
// be equal.
196
throw new IllegalStateException JavaDoc("The operation was not called. Expected = " + eventClassNames[i]
197                             + ", Found = " + lstManager[i].toString());
198                 }
199             }
200         }
201     }
202
203     /**
204      * Builds an array, which all values are equals to UNDEFINED.
205      * @param length array's length
206      * @return the array.
207      */

208     private String JavaDoc[] getArrayUndefined(final int length) {
209         String JavaDoc[] list = new String JavaDoc[length];
210
211         for (int i = 0; i < length; i++) {
212             list[i] = UNDEFINED;
213         }
214
215         return list;
216     }
217 }
218
Popular Tags