2015-10-11 21:58:13 -03:00
|
|
|
package com.iluwatar.lazy.loading;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
import static org.junit.Assert.assertNull;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Using reflection this test shows that the heavy field is not instantiated until the method getHeavy is called
|
|
|
|
*
|
|
|
|
* Created by jones on 11/10/2015.
|
|
|
|
*/
|
|
|
|
public class HolderThreadSafeTest {
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void test() throws IllegalAccessException {
|
|
|
|
HolderThreadSafe hts = new HolderThreadSafe();
|
|
|
|
|
|
|
|
{//first call is null
|
2015-10-11 22:16:51 -03:00
|
|
|
Field[] ff = HolderThreadSafe.class.getDeclaredFields();
|
|
|
|
for (Field f: ff) {
|
|
|
|
f.setAccessible(true);
|
|
|
|
}
|
2015-10-11 21:58:13 -03:00
|
|
|
|
2015-10-11 22:16:51 -03:00
|
|
|
assertNull(ff[0].get(hts));
|
2015-10-11 21:58:13 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// now it is lazily loaded
|
|
|
|
hts.getHeavy();
|
|
|
|
|
|
|
|
{//now it is not null - call via reflection so that the test is the same before and after
|
2015-10-11 22:16:51 -03:00
|
|
|
Field[] ff = HolderThreadSafe.class.getDeclaredFields();
|
|
|
|
for (Field f: ff) {
|
|
|
|
f.setAccessible(true);
|
|
|
|
}
|
2015-10-11 21:58:13 -03:00
|
|
|
|
2015-10-11 22:16:51 -03:00
|
|
|
assertNotNull(ff[0].get(hts));
|
2015-10-11 21:58:13 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|