001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: SLSBOperationLoggerAccess.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.callbacklogger;
025:
026: import static java.util.Arrays.sort;
027: import static org.ow2.easybeans.tests.common.helper.ListHelper.convertListType;
028: import static org.testng.Assert.assertTrue;
029:
030: import java.util.List;
031:
032: import javax.ejb.Remote;
033: import javax.ejb.Stateless;
034: import javax.interceptor.ExcludeDefaultInterceptors;
035: import javax.persistence.EntityManager;
036: import javax.persistence.EntityManagerFactory;
037: import javax.persistence.PersistenceUnit;
038: import javax.persistence.Query;
039:
040: import org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.CallbackLogger;
041: import org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.CallbackType;
042: import org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationLogger;
043: import org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType;
044:
045: /**
046: * Accesses the entity bean that is responsible for registering operations.
047: * @author Gisele Pinheiro Souza
048: * @author Eduardo Studzinski Estima de Castro
049: */
050: @Stateless(name="SLSBOperationLoggerAccess")
051: @Remote(ItfOperationLoggerAccess.class)
052: @ExcludeDefaultInterceptors
053: public class SLSBOperationLoggerAccess extends CallbackLoggerAccessBase
054: implements ItfOperationLoggerAccess {
055:
056: /**
057: * The Entity Manager Factory used during the tests.
058: */
059: @PersistenceUnit
060: private EntityManagerFactory entityManagerFactory;
061:
062: /**
063: * Constant.
064: */
065: private static final String UNDEFINED = "Undefined.";
066:
067: /**
068: * Creates an instance of OperationLogger with the parameters and the
069: * current time.
070: * @param className the bean class.
071: * @param event the event type.
072: * @param eventClassName the name of the class where the event is defined.
073: * @param operationType the operation type.
074: * @param description the operation description.
075: */
076: public void insertOperationLogger(final String className,
077: final CallbackType event, final String eventClassName,
078: final OperationType operationType, final String description) {
079: EntityManager entityManager = entityManagerFactory
080: .createEntityManager();
081:
082: OperationLogger logger = new OperationLogger();
083: logger.setCallbackClassName(eventClassName);
084: logger.setCallbackEvent(event);
085: logger.setClassName(className);
086: logger.setInsertionDate(getTime());
087: logger.setOperationType(operationType);
088: logger.setDescription(description);
089: entityManager.persist(logger);
090: entityManager.flush();
091: }
092:
093: /**
094: * Creates an instance of OperationLogger with the parameters and the
095: * current time.
096: * @param className the bean class.
097: * @param event the event type.
098: * @param eventClassName the name of the class where the event is defined.
099: * @param operationType the operation type.
100: */
101: public void insertOperationLogger(final String className,
102: final CallbackType event, final String eventClassName,
103: final OperationType operationType) {
104: this .insertOperationLogger(className, event, eventClassName,
105: operationType, UNDEFINED);
106: }
107:
108: /**
109: * Verifies if the events for an operation were called and if the invocation
110: * order is correct.
111: * @param className the bean class.
112: * @param event the event type.
113: * @param eventClassNames the list of where the event methods are defined in
114: * the correct invocation order.
115: * @param operationType the operation type.
116: */
117: public void verifyOperation(final String className,
118: final CallbackType event, final String[] eventClassNames,
119: final OperationType operationType) {
120:
121: this .verifyOperation(className, event, eventClassNames,
122: operationType,
123: getArrayUndefined(eventClassNames.length));
124: }
125:
126: /**
127: * Verifies if the events for an operation were called and if the invocation
128: * order is correct.
129: * @param className the bean class.
130: * @param event the event type.
131: * @param eventClassNames the list of where the event methods are defined in
132: * the correct invocation order.
133: * @param operationType the operation type.
134: */
135: public void verifyOperation(final Class className,
136: final CallbackType event, final String[] eventClassNames,
137: final OperationType operationType) {
138: this .verifyOperation(className.getName(), event,
139: eventClassNames, operationType,
140: getArrayUndefined(eventClassNames.length));
141: }
142:
143: /**
144: * Verifies if the events for an operation were called and if the invocation
145: * order is correct.
146: * @param className the bean class.
147: * @param event the event type.
148: * @param eventClassNames the list of where the event methods are defined in
149: * the correct invocation order.
150: * @param operationType the operation type.
151: * @param descriptions the operations description.
152: */
153: public void verifyOperation(final Class className,
154: final CallbackType event, final String[] eventClassNames,
155: final OperationType operationType,
156: final String[] descriptions) {
157: this .verifyOperation(className.getName(), event,
158: eventClassNames, operationType, descriptions);
159: }
160:
161: /**
162: * Verifies if the events for an operation were called and if the invocation
163: * order is correct.
164: * @param className the bean class.
165: * @param event the event type.
166: * @param eventClassNames the list of where the event methods are defined in
167: * the correct invocation order.
168: * @param operationType the operation type.
169: * @param descriptions the operations description.
170: */
171: public void verifyOperation(final String className,
172: final CallbackType event, final String[] eventClassNames,
173: final OperationType operationType,
174: final String[] descriptions) {
175:
176: try {
177: Thread.sleep(WAIT);
178: } catch (InterruptedException e1) {
179: e1.printStackTrace();
180: }
181:
182: // Checks if the size of descriptions is the same as eventClassNames.
183: assertTrue(descriptions.length == eventClassNames.length,
184: "The length of descriptions and event class names must be equal.");
185:
186: EntityManager entityManager = entityManagerFactory
187: .createEntityManager();
188: Query query = entityManager
189: .createQuery("SELECT e FROM OperationLogger "
190: + "e WHERE e.className = :className AND e.callbackEvent= :event AND e.operationType = :operationType");
191: query.setParameter("className", className);
192: query.setParameter("event", event);
193: query.setParameter("operationType", operationType);
194: List callbackList = query.getResultList();
195:
196: assertTrue(
197: callbackList.size() == eventClassNames.length,
198: "The length of operations expected is different from the length of operations found.");
199:
200: // Sorts the events by date.
201: if (callbackList.size() != 0) {
202: OperationLogger[] lstManager = new OperationLogger[callbackList
203: .size()];
204: try {
205: lstManager = convertListType(callbackList).toArray(
206: lstManager);
207: } catch (Exception e) {
208: throw new RuntimeException(e);
209: }
210: sort(lstManager,
211: new CallbackLoggerComparator<CallbackLogger>());
212: for (int i = 0; i < lstManager.length; i++) {
213: if (!(lstManager[i].getCallbackClassName().equals(
214: eventClassNames[i]) & lstManager[i]
215: .getDescription().equals(descriptions[i]))) {
216: // The event class name and the operation description must
217: // be equal.
218: throw new IllegalStateException(
219: "The operation was not called. Expected = "
220: + eventClassNames[i] + ", Found = "
221: + lstManager[i].toString());
222: }
223: }
224: }
225: }
226:
227: /**
228: * Builds an array, which all values are equals to UNDEFINED.
229: * @param length array's length
230: * @return the array.
231: */
232: private String[] getArrayUndefined(final int length) {
233: String[] list = new String[length];
234:
235: for (int i = 0; i < length; i++) {
236: list[i] = UNDEFINED;
237: }
238:
239: return list;
240: }
241: }
|