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