View Javadoc
1   package mockit;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertFalse;
5   import static org.junit.jupiter.api.Assertions.assertNotNull;
6   import static org.junit.jupiter.api.Assertions.assertNull;
7   import static org.junit.jupiter.api.Assertions.assertSame;
8   import static org.junit.jupiter.api.Assertions.assertTrue;
9   
10  import java.io.PrintWriter;
11  import java.sql.Connection;
12  import java.util.logging.Logger;
13  
14  import javax.annotation.Resource;
15  import javax.annotation.sql.DataSourceDefinition;
16  import javax.annotation.sql.DataSourceDefinitions;
17  import javax.ejb.EJB;
18  import javax.enterprise.context.Conversation;
19  import javax.inject.Inject;
20  import javax.sql.ConnectionPoolDataSource;
21  import javax.sql.DataSource;
22  import javax.sql.PooledConnection;
23  import javax.sql.XAConnection;
24  import javax.sql.XADataSource;
25  
26  import org.junit.jupiter.api.Test;
27  import org.springframework.beans.factory.annotation.Autowired;
28  
29  /**
30   * The Class TestedClassWithFullAnnotatedDITest.
31   */
32  @SuppressWarnings("ParameterHidesMemberVariable")
33  final class TestedClassWithFullAnnotatedDITest {
34  
35      /**
36       * The Class DummyDataSource.
37       */
38      public static class DummyDataSource implements DataSource {
39  
40          /** The url. */
41          private String url;
42  
43          /** The user. */
44          private String user;
45  
46          /** The password. */
47          String password;
48  
49          /**
50           * Gets the url.
51           *
52           * @return the url
53           */
54          public String getUrl() {
55              return url;
56          }
57  
58          /**
59           * Sets the url.
60           *
61           * @param url
62           *            the new url
63           */
64          @SuppressWarnings("unused")
65          public void setUrl(String url) {
66              this.url = url;
67          }
68  
69          /**
70           * Gets the user.
71           *
72           * @return the user
73           */
74          public String getUser() {
75              return user;
76          }
77  
78          /**
79           * Sets the user.
80           *
81           * @param user
82           *            the new user
83           */
84          @SuppressWarnings("unused")
85          public void setUser(String user) {
86              this.user = user;
87          }
88  
89          /**
90           * Sets the password.
91           *
92           * @param password
93           *            the new password
94           */
95          @SuppressWarnings("unused")
96          public void setPassword(String password) {
97              this.password = password;
98          }
99  
100         @Override
101         public Connection getConnection() {
102             return null;
103         }
104 
105         @Override
106         public Connection getConnection(String username, String password) {
107             return null;
108         }
109 
110         @Override
111         public <T> T unwrap(Class<T> iface) {
112             return null;
113         }
114 
115         @Override
116         public boolean isWrapperFor(Class<?> iface) {
117             return false;
118         }
119 
120         @Override
121         public PrintWriter getLogWriter() {
122             return null;
123         }
124 
125         @Override
126         public void setLogWriter(PrintWriter out) {
127         }
128 
129         @Override
130         public void setLoginTimeout(int seconds) {
131         }
132 
133         @Override
134         public int getLoginTimeout() {
135             return 0;
136         }
137 
138         @Override
139         public Logger getParentLogger() {
140             return null;
141         }
142     }
143 
144     /**
145      * The Class TestedClass.
146      */
147     @DataSourceDefinition(name = "java:global/jdbc/testDS", className = "mockit.TestedClassWithFullAnnotatedDITest$DummyDataSource", url = "jdbc:testDb:test", user = "tests", password = "test123")
148     static final class TestedClass {
149 
150         /** The action. */
151         @Inject
152         Runnable action;
153 
154         /** The dependency 1. */
155         @Autowired
156         ItfWithSingleImpl dependency1;
157 
158         /** The dependency 2. */
159         @Resource
160         ItfWithSingleImpl dependency2;
161 
162         /** The another dependency. */
163         @Inject
164         ItfWithTwoImpls anotherDependency;
165 
166         /** The log 1. */
167         @Inject
168         private Logger log1;
169 
170         /** The log 2. */
171         @Inject
172         private Logger log2;
173 
174         /** The collaborator. */
175         Collaborator collaborator;
176 
177         /** The conversation. */
178         @Inject
179         Conversation conversation;
180 
181         /** The ds. */
182         @Resource(lookup = "java:global/jdbc/testDS")
183         DataSource ds;
184     }
185 
186     /**
187      * The Class PooledDataSource.
188      */
189     public static final class PooledDataSource extends DummyDataSource implements ConnectionPoolDataSource {
190         @Override
191         public PooledConnection getPooledConnection() {
192             return null;
193         }
194 
195         @Override
196         public PooledConnection getPooledConnection(String user, String password) {
197             return null;
198         }
199     }
200 
201     /**
202      * The Class DistributedDataSource.
203      */
204     public static final class DistributedDataSource extends DummyDataSource implements XADataSource {
205         @Override
206         public XAConnection getXAConnection() {
207             return null;
208         }
209 
210         @Override
211         public XAConnection getXAConnection(String user, String password) {
212             return null;
213         }
214     }
215 
216     /**
217      * The Class AnotherTestedClass.
218      */
219     @DataSourceDefinitions({
220             @DataSourceDefinition(name = "regularDS", className = "mockit.TestedClassWithFullAnnotatedDITest$DummyDataSource", url = "jdbc:oracle:test", user = "tests", password = "test123"),
221             @DataSourceDefinition(name = "pooledDS", className = "mockit.TestedClassWithFullAnnotatedDITest$PooledDataSource", url = "jdbc:hsqldb:db", user = "pool", password = "test123"),
222             @DataSourceDefinition(name = "distributedDS", className = "mockit.TestedClassWithFullAnnotatedDITest$DistributedDataSource", url = "jdbc:postgresql:database", user = "xa", password = "test123") })
223     static class AnotherTestedClass {
224 
225         /** The ds 1. */
226         @Resource(lookup = "regularDS")
227         DataSource ds1;
228 
229         /** The ds 2. */
230         @Resource(lookup = "pooledDS")
231         ConnectionPoolDataSource ds2;
232 
233         /** The ds 3. */
234         @Resource(lookup = "distributedDS")
235         XADataSource ds3;
236 
237         /** The ds 4. */
238         @Resource(name = "regularDS")
239         DataSource ds4;
240     }
241 
242     /**
243      * The Class Collaborator.
244      */
245     static class Collaborator {
246     }
247 
248     /**
249      * The Interface ItfWithSingleImpl.
250      */
251     public interface ItfWithSingleImpl {
252     }
253 
254     /**
255      * The Class SingleImpl.
256      */
257     public static final class SingleImpl implements ItfWithSingleImpl {
258         /** The ejb. */
259         @EJB
260         ItfToBeMocked ejb;
261     }
262 
263     /**
264      * The Interface ItfWithTwoImpls.
265      */
266     public interface ItfWithTwoImpls {
267     }
268 
269     /**
270      * The Class Impl1.
271      */
272     @SuppressWarnings("unused")
273     public static final class Impl1 implements ItfWithTwoImpls {
274     }
275 
276     /**
277      * The Class Impl2.
278      */
279     public static final class Impl2 implements ItfWithTwoImpls {
280     }
281 
282     /**
283      * The Interface ItfToBeMocked.
284      */
285     public interface ItfToBeMocked {
286     }
287 
288     /** The dep 1. */
289     @Tested
290     SingleImpl dep1;
291 
292     /** The another dep. */
293     @Tested
294     Impl2 anotherDep;
295 
296     /** The collaborator. */
297     @Tested
298     Collaborator collaborator;
299 
300     /** The tested. */
301     @Tested(fullyInitialized = true)
302     TestedClass tested;
303 
304     /** The action. */
305     // Without these injectables, a "missing @Injectable" exception occurs for each unresolved field.
306     @Injectable
307     Runnable action;
308 
309     /** The ejb. */
310     @Injectable
311     ItfToBeMocked ejb;
312 
313     /**
314      * Inject initialized dependencies for interfaces having tested objects of implementation class types.
315      */
316     @Test
317     void injectInitializedDependenciesForInterfacesHavingTestedObjectsOfImplementationClassTypes() {
318         assertSame(action, tested.action);
319         assertNotNull(tested.dependency1);
320         assertSame(tested.dependency1, tested.dependency2);
321         assertTrue(tested.anotherDependency instanceof Impl2);
322         assertSame(ejb, ((SingleImpl) tested.dependency1).ejb);
323     }
324 
325     /**
326      * Inject logger fields with logger created with tested class name.
327      */
328     @Test
329     void injectLoggerFieldsWithLoggerCreatedWithTestedClassName() {
330         assertEquals(TestedClass.class.getName(), tested.log1.getName());
331         assertSame(tested.log2, tested.log1);
332     }
333 
334     /**
335      * Inject non annotated field from matching tested field.
336      */
337     @Test
338     void injectNonAnnotatedFieldFromMatchingTestedField() {
339         assertSame(collaborator, tested.collaborator);
340     }
341 
342     /** The conversation. */
343     @Tested
344     Conversation conversation;
345 
346     /**
347      * Manage conversation context.
348      */
349     @Test
350     void manageConversationContext() {
351         assertNotNull(conversation);
352         assertSame(tested.conversation, conversation);
353         assertTrue(conversation.isTransient());
354 
355         assertEquals(0, conversation.getTimeout());
356         conversation.setTimeout(1500);
357         assertEquals(1500, conversation.getTimeout());
358 
359         assertNull(conversation.getId());
360 
361         conversation.begin();
362         assertFalse(conversation.isTransient());
363         assertNotNull(conversation.getId());
364 
365         conversation.end();
366         assertTrue(conversation.isTransient());
367         assertNull(conversation.getId());
368 
369         conversation.begin("test");
370         assertFalse(conversation.isTransient());
371         assertEquals("test", conversation.getId());
372     }
373 
374     /**
375      * Inject data source configured from single data source definition.
376      */
377     @Test
378     void injectDataSourceConfiguredFromSingleDataSourceDefinition() {
379         assertTrue(tested.ds instanceof DummyDataSource);
380 
381         DummyDataSource ds = (DummyDataSource) tested.ds;
382         assertEquals("jdbc:testDb:test", ds.getUrl());
383         assertEquals("tests", ds.getUser());
384         assertEquals("test123", ds.password);
385     }
386 
387     /** The tested 2. */
388     @Tested(fullyInitialized = true)
389     AnotherTestedClass tested2;
390 
391     /**
392      * Inject multiple data sources configured from different data source definitions.
393      */
394     @Test
395     void injectMultipleDataSourcesConfiguredFromDifferentDataSourceDefinitions() {
396         assertTrue(tested2.ds1 instanceof DummyDataSource);
397         assertTrue(tested2.ds2 instanceof PooledDataSource);
398         assertTrue(tested2.ds3 instanceof DistributedDataSource);
399         assertSame(tested2.ds1, tested2.ds4);
400     }
401 }