View Javadoc
1   package mockit;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertNull;
5   import static org.junit.jupiter.api.Assertions.assertSame;
6   import static org.junit.jupiter.api.Assertions.assertTrue;
7   
8   import java.io.ByteArrayInputStream;
9   import java.io.IOException;
10  import java.io.InputStream;
11  import java.net.URL;
12  import java.util.Scanner;
13  
14  import org.junit.jupiter.api.BeforeAll;
15  import org.junit.jupiter.api.BeforeEach;
16  import org.junit.jupiter.api.Test;
17  
18  /**
19   * The Class FakeClassInstantiationPerSetupTest.
20   */
21  final class FakeClassInstantiationPerSetupTest {
22  
23      /**
24       * The Class RealClass1.
25       */
26      public static final class RealClass1 {
27  
28          /**
29           * Do something.
30           */
31          public static void doSomething() {
32              throw new RuntimeException();
33          }
34  
35          /**
36           * Perform computation.
37           *
38           * @param a
39           *            the a
40           * @param b
41           *            the b
42           *
43           * @return the int
44           */
45          public int performComputation(int a, boolean b) {
46              return b ? a : -a;
47          }
48      }
49  
50      /**
51       * The Class RealClass2.
52       */
53      public static final class RealClass2 {
54  
55          /**
56           * Do something.
57           */
58          public static void doSomething() {
59              throw new RuntimeException();
60          }
61  
62          /**
63           * Perform computation.
64           *
65           * @param a
66           *            the a
67           * @param b
68           *            the b
69           *
70           * @return the int
71           */
72          public int performComputation(int a, boolean b) {
73              return b ? a : -a;
74          }
75      }
76  
77      /**
78       * The Class RealClass3.
79       */
80      public static final class RealClass3 {
81  
82          /**
83           * Do something.
84           */
85          public static void doSomething() {
86              throw new RuntimeException();
87          }
88  
89          /**
90           * Perform computation.
91           *
92           * @param a
93           *            the a
94           * @param b
95           *            the b
96           *
97           * @return the int
98           */
99          public int performComputation(int a, boolean b) {
100             return b ? a : -a;
101         }
102     }
103 
104     /**
105      * The Class RealClass4.
106      */
107     public static final class RealClass4 {
108 
109         /**
110          * Do something.
111          */
112         public static void doSomething() {
113             throw new RuntimeException();
114         }
115 
116         /**
117          * Perform computation.
118          *
119          * @param a
120          *            the a
121          * @param b
122          *            the b
123          *
124          * @return the int
125          */
126         public int performComputation(int a, boolean b) {
127             return b ? a : -a;
128         }
129     }
130 
131     /**
132      * The Class FakeClass1.
133      */
134     static final class FakeClass1 extends MockUp<RealClass1> {
135 
136         /** The single instance created. */
137         static Object singleInstanceCreated;
138 
139         /**
140          * Instantiates a new fake class 1.
141          */
142         FakeClass1() {
143             assertNull(singleInstanceCreated);
144             singleInstanceCreated = this;
145         }
146 
147         /**
148          * Do something.
149          */
150         @Mock
151         void doSomething() {
152             assertSame(singleInstanceCreated, this);
153         }
154 
155         /**
156          * Perform computation.
157          *
158          * @param a
159          *            the a
160          * @param b
161          *            the b
162          *
163          * @return the int
164          */
165         @Mock
166         int performComputation(int a, boolean b) {
167             assertSame(singleInstanceCreated, this);
168             assertTrue(a > 0);
169             assertTrue(b);
170             return 2;
171         }
172     }
173 
174     /**
175      * The Class FakeClass2.
176      */
177     static final class FakeClass2 extends MockUp<RealClass2> {
178 
179         /** The single instance created. */
180         static Object singleInstanceCreated;
181 
182         /**
183          * Instantiates a new fake class 2.
184          */
185         FakeClass2() {
186             assertNull(singleInstanceCreated);
187             singleInstanceCreated = this;
188         }
189 
190         /**
191          * Do something.
192          */
193         @Mock
194         void doSomething() {
195             assertSame(singleInstanceCreated, this);
196         }
197 
198         /**
199          * Perform computation.
200          *
201          * @param a
202          *            the a
203          * @param b
204          *            the b
205          *
206          * @return the int
207          */
208         @Mock
209         int performComputation(int a, boolean b) {
210             assertSame(singleInstanceCreated, this);
211             assertTrue(a > 0);
212             assertTrue(b);
213             return 2;
214         }
215     }
216 
217     /**
218      * The Class FakeClass3.
219      */
220     static final class FakeClass3 extends MockUp<RealClass3> {
221 
222         /** The single instance created. */
223         static Object singleInstanceCreated;
224 
225         /**
226          * Instantiates a new fake class 3.
227          */
228         FakeClass3() {
229             assertNull(singleInstanceCreated);
230             singleInstanceCreated = this;
231         }
232 
233         /**
234          * Do something.
235          */
236         @Mock
237         void doSomething() {
238             assertSame(singleInstanceCreated, this);
239         }
240 
241         /**
242          * Perform computation.
243          *
244          * @param a
245          *            the a
246          * @param b
247          *            the b
248          *
249          * @return the int
250          */
251         @Mock
252         int performComputation(int a, boolean b) {
253             assertSame(singleInstanceCreated, this);
254             assertTrue(a > 0);
255             assertTrue(b);
256             return 2;
257         }
258     }
259 
260     /**
261      * The Class FakeClass4.
262      */
263     static final class FakeClass4 extends MockUp<RealClass4> {
264 
265         /** The single instance created. */
266         static Object singleInstanceCreated;
267 
268         /**
269          * Instantiates a new fake class 4.
270          */
271         FakeClass4() {
272             assertNull(singleInstanceCreated);
273             singleInstanceCreated = this;
274         }
275 
276         /**
277          * Do something.
278          */
279         @Mock
280         void doSomething() {
281             assertSame(singleInstanceCreated, this);
282         }
283 
284         /**
285          * Perform computation.
286          *
287          * @param a
288          *            the a
289          * @param b
290          *            the b
291          *
292          * @return the int
293          */
294         @Mock
295         int performComputation(int a, boolean b) {
296             assertSame(singleInstanceCreated, this);
297             assertTrue(a > 0);
298             assertTrue(b);
299             return 2;
300         }
301     }
302 
303     /**
304      * Sets the up class level fakes.
305      */
306     @BeforeAll
307     static void setUpClassLevelFakes() {
308         new FakeClass1();
309     }
310 
311     /**
312      * Sets the up additional class level fakes.
313      */
314     @BeforeAll
315     static void setUpAdditionalClassLevelFakes() {
316         new FakeClass2();
317     }
318 
319     /**
320      * Sets the up method level fakes.
321      */
322     @BeforeEach
323     void setUpMethodLevelFakes() {
324         FakeClass3.singleInstanceCreated = null;
325         new FakeClass3();
326     }
327 
328     /**
329      * Fake instance per setup in class and fixture scopes.
330      */
331     @Test
332     void fakeInstancePerSetupInClassAndFixtureScopes() {
333         assertFakeClass1();
334         assertFakeClass2();
335         assertFakeClass3();
336         assertEquals(1, new RealClass4().performComputation(1, true));
337     }
338 
339     /**
340      * Assert fake class 1.
341      */
342     void assertFakeClass1() {
343         RealClass1.doSomething();
344         assertEquals(2, new RealClass1().performComputation(1, true));
345     }
346 
347     /**
348      * Assert fake class 2.
349      */
350     void assertFakeClass2() {
351         RealClass2.doSomething();
352         assertEquals(2, new RealClass2().performComputation(1, true));
353     }
354 
355     /**
356      * Assert fake class 3.
357      */
358     void assertFakeClass3() {
359         RealClass3.doSomething();
360         assertEquals(2, new RealClass3().performComputation(1, true));
361     }
362 
363     /**
364      * Assert fake class 4.
365      */
366     void assertFakeClass4() {
367         RealClass4.doSomething();
368         assertEquals(2, new RealClass4().performComputation(1, true));
369     }
370 
371     /**
372      * Fake instance per setup in all scopes.
373      */
374     @Test
375     void fakeInstancePerSetupInAllScopes() {
376         new FakeClass4();
377 
378         assertFakeClass1();
379         assertFakeClass2();
380         assertFakeClass3();
381         assertFakeClass4();
382     }
383 
384     /**
385      * The Class FakeURL.
386      */
387     public static final class FakeURL extends MockUp<URL> {
388 
389         /**
390          * Open stream.
391          *
392          * @param inv
393          *            the inv
394          *
395          * @return the input stream
396          *
397          * @throws IOException
398          *             Signals that an I/O exception has occurred.
399          */
400         @Mock
401         public InputStream openStream(Invocation inv) throws IOException {
402             URL it = inv.getInvokedInstance();
403 
404             if ("test".equals(it.getHost())) {
405                 return new ByteArrayInputStream("response".getBytes());
406             }
407 
408             return it.openStream();
409         }
410     }
411 
412     /**
413      * Reentrant fake for JRE class.
414      *
415      * @throws Exception
416      *             the exception
417      */
418     @Test
419     void reentrantFakeForJREClass() throws Exception {
420         new FakeURL();
421 
422         InputStream response = new URL("http://test").openStream();
423 
424         assertEquals("response", new Scanner(response).nextLine());
425     }
426 }