Fix unit test by makinig getField use the field name directly
This commit is contained in:
parent
7cf5b32086
commit
0107b24976
@ -73,20 +73,16 @@ public class LazyLoadedSingletonThreadSafetyTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void test_HoleInSingletonCreationIfUsingReflection() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
|
public void test_HoleInSingletonCreationIfUsingReflection() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
|
||||||
Field[] f = ThreadSafeLazyLoadedIvoryTower.class.getDeclaredFields();
|
Field f = ThreadSafeLazyLoadedIvoryTower.class.getDeclaredField("instance");
|
||||||
for (Field ff : f) {
|
f.setAccessible(true);
|
||||||
System.out.println(ff.getDeclaringClass());
|
|
||||||
}
|
|
||||||
assertEquals("One field only in ThreadSafeLazyLoadedIvoryTower", 1, f.length);
|
|
||||||
f[0].setAccessible(true);
|
|
||||||
|
|
||||||
{//reflectively create an object - the singleton field is null
|
{//reflectively create an object - the singleton field is null
|
||||||
Class lazyIvoryTowerClazz = Class.forName("com.iluwatar.singleton.ThreadSafeLazyLoadedIvoryTower");
|
Class lazyIvoryTowerClazz = Class.forName("com.iluwatar.singleton.ThreadSafeLazyLoadedIvoryTower");
|
||||||
Constructor<ThreadSafeLazyLoadedIvoryTower> constructor = lazyIvoryTowerClazz.getDeclaredConstructor();
|
Constructor<ThreadSafeLazyLoadedIvoryTower> constructor = lazyIvoryTowerClazz.getDeclaredConstructor();
|
||||||
constructor.setAccessible(true);
|
constructor.setAccessible(true);
|
||||||
ThreadSafeLazyLoadedIvoryTower instance = constructor.newInstance();
|
ThreadSafeLazyLoadedIvoryTower instance = constructor.newInstance();
|
||||||
assertNull(f[0].get(instance));
|
assertNull(f.get(instance));
|
||||||
}
|
}
|
||||||
|
|
||||||
//instantiate the singleton but when we do the below code we are creating a new object where it is set to null still
|
//instantiate the singleton but when we do the below code we are creating a new object where it is set to null still
|
||||||
@ -97,7 +93,7 @@ public class LazyLoadedSingletonThreadSafetyTest {
|
|||||||
Constructor<ThreadSafeLazyLoadedIvoryTower> constructor = lazyIvoryTowerClazz.getDeclaredConstructor();
|
Constructor<ThreadSafeLazyLoadedIvoryTower> constructor = lazyIvoryTowerClazz.getDeclaredConstructor();
|
||||||
constructor.setAccessible(true);
|
constructor.setAccessible(true);
|
||||||
ThreadSafeLazyLoadedIvoryTower instance = constructor.newInstance();
|
ThreadSafeLazyLoadedIvoryTower instance = constructor.newInstance();
|
||||||
assertNull(f[0].get(instance));
|
assertNull(f.get(instance));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user