Added tests for lazy-loading pattern
This commit is contained in:
lazy-loading/src/test/java/com/iluwatar/lazy/loading
@ -1,45 +1,26 @@
|
||||
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
|
||||
* Date: 12/19/15 - 12:19 PM
|
||||
*
|
||||
* Created by jones on 11/10/2015.
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public class HolderThreadSafeTest {
|
||||
public class HolderThreadSafeTest extends AbstractHolderTest {
|
||||
|
||||
@Test
|
||||
public void test() throws IllegalAccessException {
|
||||
HolderThreadSafe hts = new HolderThreadSafe();
|
||||
private final HolderThreadSafe holder = new HolderThreadSafe();
|
||||
|
||||
{
|
||||
// first call is null
|
||||
Field[] ff = HolderThreadSafe.class.getDeclaredFields();
|
||||
for (Field f : ff) {
|
||||
f.setAccessible(true);
|
||||
}
|
||||
|
||||
assertNull(ff[0].get(hts));
|
||||
}
|
||||
|
||||
// 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
|
||||
Field[] ff = HolderThreadSafe.class.getDeclaredFields();
|
||||
for (Field f : ff) {
|
||||
f.setAccessible(true);
|
||||
}
|
||||
|
||||
assertNotNull(ff[0].get(hts));
|
||||
}
|
||||
@Override
|
||||
Heavy getInternalHeavyValue() throws Exception {
|
||||
final Field holderField = HolderThreadSafe.class.getDeclaredField("heavy");
|
||||
holderField.setAccessible(true);
|
||||
return (Heavy) holderField.get(this.holder);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
Heavy getHeavy() throws Exception {
|
||||
return this.holder.getHeavy();
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user