|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| package org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.callbacklogger; |
|
26 |
| |
|
27 |
| import static java.util.Arrays.sort; |
|
28 |
| import static org.objectweb.easybeans.tests.common.helper.ListHelper.convertListType; |
|
29 |
| import static org.testng.Assert.assertEquals; |
|
30 |
| import static org.testng.Assert.assertTrue; |
|
31 |
| |
|
32 |
| import java.util.Date; |
|
33 |
| import java.util.List; |
|
34 |
| |
|
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.log.JLog; |
|
41 |
| import org.objectweb.easybeans.log.JLogFactory; |
|
42 |
| import org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.CallbackLogger; |
|
43 |
| import org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.CallbackType; |
|
44 |
| import org.objectweb.easybeans.tests.common.helper.ContextHelper; |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| public class CallbackLoggerAccessBase{ |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| private static JLog logger = JLogFactory.getLog(CallbackLoggerAccessBase.class); |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| @PersistenceUnit |
|
64 |
| private EntityManagerFactory entityManagerFactory; |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
162
| protected long getTime(){
|
|
71 |
162
| try {
|
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
162
| Thread.sleep(2);
|
|
76 |
| } catch (InterruptedException e) { |
|
77 |
0
| throw new RuntimeException(e);
|
|
78 |
| } |
|
79 |
| |
|
80 |
162
| Date date = new Date();
|
|
81 |
162
| return date.getTime();
|
|
82 |
| } |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
0
| public void deleteCallbackEvent(final int id) {
|
|
89 |
0
| EntityManager entityManager = entityManagerFactory.createEntityManager();
|
|
90 |
0
| CallbackLogger callbackLogger = entityManager.find(CallbackLogger.class, new Integer(id));
|
|
91 |
0
| if (callbackLogger != null) {
|
|
92 |
0
| entityManager.remove(callbackLogger);
|
|
93 |
| } |
|
94 |
| } |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
23
| public List findCallbackEvent(final String className, final CallbackType callbackEvent) {
|
|
103 |
23
| EntityManager entityManager = entityManagerFactory.createEntityManager();
|
|
104 |
23
| Query query = entityManager.createNamedQuery("findLifecycleEvent");
|
|
105 |
23
| query.setParameter("className", className);
|
|
106 |
23
| query.setParameter("event", callbackEvent);
|
|
107 |
23
| return query.getResultList();
|
|
108 |
| } |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
0
| public List findCallbackEvent(final String className) {
|
|
116 |
0
| EntityManager entityManager = entityManagerFactory.createEntityManager();
|
|
117 |
0
| Query query = entityManager.createNamedQuery("findLifecycleEventByClass");
|
|
118 |
0
| query.setParameter("className", className);
|
|
119 |
0
| return query.getResultList();
|
|
120 |
| } |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
26
| public List findCallbackEventByCallbackMethod(final String className, final CallbackType callbackEvent,
|
|
131 |
| final String callbackClassName) { |
|
132 |
26
| EntityManager entityManager = entityManagerFactory.createEntityManager();
|
|
133 |
26
| Query query = entityManager.createNamedQuery("findLifecycleEventByCallbackMethod");
|
|
134 |
26
| query.setParameter("className", className);
|
|
135 |
26
| query.setParameter("event", callbackEvent);
|
|
136 |
26
| query.setParameter("callbackClassName", callbackClassName);
|
|
137 |
26
| return query.getResultList();
|
|
138 |
| } |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
0
| public List findAll(){
|
|
145 |
0
| EntityManager entityManager = entityManagerFactory.createEntityManager();
|
|
146 |
0
| Query query = entityManager.createNamedQuery("findAll");
|
|
147 |
0
| return query.getResultList();
|
|
148 |
| } |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
65
| public void deleteAll() {
|
|
154 |
65
| EntityManager entityManager = entityManagerFactory.createEntityManager();
|
|
155 |
65
| Query query = entityManager.createNamedQuery("findAll");
|
|
156 |
65
| List lstEvent = query.getResultList();
|
|
157 |
65
| for (Object obj : lstEvent) {
|
|
158 |
178
| CallbackLogger callbackLogger = (CallbackLogger) obj;
|
|
159 |
178
| if (callbackLogger != null) {
|
|
160 |
178
| entityManager.remove(callbackLogger);
|
|
161 |
| } |
|
162 |
| } |
|
163 |
| } |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
8
| public void verifyCallbackOrder(final String className, final CallbackType callbackEvent,
|
|
174 |
| final String[] callbackClassNames) { |
|
175 |
| |
|
176 |
8
| List callbackList = findCallbackEvent(className, callbackEvent);
|
|
177 |
8
| logger.debug("Callback list: {0}", callbackList);
|
|
178 |
| |
|
179 |
8
| assertTrue(callbackList.size() == callbackClassNames.length, "There is an error in callback interceptor invocation.");
|
|
180 |
| |
|
181 |
| |
|
182 |
5
| if (callbackList.size() != 0) {
|
|
183 |
5
| CallbackLogger[] lstManager = new CallbackLogger[callbackList.size()];
|
|
184 |
5
| try {
|
|
185 |
5
| lstManager = convertListType(callbackList).toArray(lstManager);
|
|
186 |
| } catch (Exception e) { |
|
187 |
0
| throw new RuntimeException(e);
|
|
188 |
| } |
|
189 |
5
| sort(lstManager, new CallbackLoggerComparator<CallbackLogger>());
|
|
190 |
5
| for (int i = 0; i < lstManager.length; i++) {
|
|
191 |
5
| assertEquals(lstManager[i].getCallbackClassName(), callbackClassNames[i],
|
|
192 |
| "The callback methods were not called in the correct order. Expected = " |
|
193 |
| + callbackClassNames[i] + ", Found = " + lstManager[i].toString()); |
|
194 |
| } |
|
195 |
| } |
|
196 |
| } |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
8
| public void verifyCallbackOrder(final Class className, final CallbackType callbackEvent, final String[] callbackClassNames) {
|
|
207 |
8
| this.verifyCallbackOrder(className.getName(), callbackEvent, callbackClassNames);
|
|
208 |
| } |
|
209 |
| } |