|
2 | 2 |
|
3 | 3 | import org.junit.Test;
|
4 | 4 |
|
5 |
| -import java.lang.reflect.Constructor; |
6 |
| -import java.lang.reflect.Field; |
7 |
| -import java.lang.reflect.InvocationTargetException; |
8 | 5 | import java.util.ArrayList;
|
9 | 6 | import java.util.List;
|
10 | 7 | import java.util.concurrent.*;
|
11 | 8 |
|
12 | 9 | import static org.junit.Assert.assertEquals;
|
13 |
| -import static org.junit.Assert.assertNull; |
14 | 10 |
|
15 | 11 | /**
|
16 | 12 | * This class provides several test case that test singleton construction.
|
@@ -71,32 +67,6 @@ public void test_MultipleCallsReturnTheSameObjectInDifferentThreads() throws Int
|
71 | 67 | }
|
72 | 68 | }
|
73 | 69 |
|
74 |
| - @Test |
75 |
| - @SuppressWarnings("unchecked") |
76 |
| - public void test_HoleInSingletonCreationIfUsingReflection() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException { |
77 |
| - Field f = ThreadSafeLazyLoadedIvoryTower.class.getDeclaredField("instance"); |
78 |
| - f.setAccessible(true); |
79 |
| - |
80 |
| - {//reflectively create an object - the singleton field is null |
81 |
| - Class lazyIvoryTowerClazz = Class.forName("com.iluwatar.singleton.ThreadSafeLazyLoadedIvoryTower"); |
82 |
| - Constructor<ThreadSafeLazyLoadedIvoryTower> constructor = lazyIvoryTowerClazz.getDeclaredConstructor(); |
83 |
| - constructor.setAccessible(true); |
84 |
| - ThreadSafeLazyLoadedIvoryTower instance = constructor.newInstance(); |
85 |
| - assertNull(f.get(instance)); |
86 |
| - } |
87 |
| - |
88 |
| - //instantiate the singleton but when we do the below code we are creating a new object where it is set to null still |
89 |
| - IvoryTower.getInstance(); |
90 |
| - |
91 |
| - {//reflectively create an object - the singleton field is null as a new object is created |
92 |
| - Class lazyIvoryTowerClazz = Class.forName("com.iluwatar.singleton.ThreadSafeLazyLoadedIvoryTower"); |
93 |
| - Constructor<ThreadSafeLazyLoadedIvoryTower> constructor = lazyIvoryTowerClazz.getDeclaredConstructor(); |
94 |
| - constructor.setAccessible(true); |
95 |
| - ThreadSafeLazyLoadedIvoryTower instance = constructor.newInstance(); |
96 |
| - assertNull(f.get(instance)); |
97 |
| - } |
98 |
| - } |
99 |
| - |
100 | 70 | private class SingletonCreatingThread implements Callable<NullObject> {
|
101 | 71 | @Override
|
102 | 72 | public NullObject call() {
|
|
0 commit comments