Added tests for lazy-loading pattern
This commit is contained in:
parent
5948a82cf2
commit
2c82bd9450
@ -0,0 +1,41 @@
|
|||||||
|
package com.iluwatar.lazy.loading;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static junit.framework.Assert.assertNotNull;
|
||||||
|
import static junit.framework.Assert.assertSame;
|
||||||
|
import static junit.framework.TestCase.assertNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date: 12/19/15 - 11:58 AM
|
||||||
|
*
|
||||||
|
* @author Jeroen Meulemeester
|
||||||
|
*/
|
||||||
|
public abstract class AbstractHolderTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the internal state of the holder value
|
||||||
|
*
|
||||||
|
* @return The internal value
|
||||||
|
*/
|
||||||
|
abstract Heavy getInternalHeavyValue() throws Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Request a lazy loaded {@link Heavy} object from the holder.
|
||||||
|
*
|
||||||
|
* @return The lazy loaded {@link Heavy} object
|
||||||
|
*/
|
||||||
|
abstract Heavy getHeavy() throws Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This test shows that the heavy field is not instantiated until the method getHeavy is called
|
||||||
|
*/
|
||||||
|
@Test(timeout = 3000)
|
||||||
|
public void testGetHeavy() throws Exception {
|
||||||
|
assertNull(getInternalHeavyValue());
|
||||||
|
assertNotNull(getHeavy());
|
||||||
|
assertNotNull(getInternalHeavyValue());
|
||||||
|
assertSame(getHeavy(), getInternalHeavyValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.iluwatar.lazy.loading;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date: 12/19/15 - 12:05 PM
|
||||||
|
*
|
||||||
|
* @author Jeroen Meulemeester
|
||||||
|
*/
|
||||||
|
public class HolderNaiveTest extends AbstractHolderTest {
|
||||||
|
|
||||||
|
private final HolderNaive holder = new HolderNaive();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
Heavy getInternalHeavyValue() throws Exception {
|
||||||
|
final Field holderField = HolderNaive.class.getDeclaredField("heavy");
|
||||||
|
holderField.setAccessible(true);
|
||||||
|
return (Heavy) holderField.get(this.holder);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
Heavy getHeavy() {
|
||||||
|
return holder.getHeavy();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,45 +1,26 @@
|
|||||||
package com.iluwatar.lazy.loading;
|
package com.iluwatar.lazy.loading;
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
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
|
* Date: 12/19/15 - 12:19 PM
|
||||||
* getHeavy is called
|
|
||||||
*
|
*
|
||||||
* Created by jones on 11/10/2015.
|
* @author Jeroen Meulemeester
|
||||||
*/
|
*/
|
||||||
public class HolderThreadSafeTest {
|
public class HolderThreadSafeTest extends AbstractHolderTest {
|
||||||
|
|
||||||
@Test
|
private final HolderThreadSafe holder = new HolderThreadSafe();
|
||||||
public void test() throws IllegalAccessException {
|
|
||||||
HolderThreadSafe hts = new HolderThreadSafe();
|
|
||||||
|
|
||||||
{
|
@Override
|
||||||
// first call is null
|
Heavy getInternalHeavyValue() throws Exception {
|
||||||
Field[] ff = HolderThreadSafe.class.getDeclaredFields();
|
final Field holderField = HolderThreadSafe.class.getDeclaredField("heavy");
|
||||||
for (Field f : ff) {
|
holderField.setAccessible(true);
|
||||||
f.setAccessible(true);
|
return (Heavy) holderField.get(this.holder);
|
||||||
}
|
|
||||||
|
|
||||||
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 getHeavy() throws Exception {
|
||||||
|
return this.holder.getHeavy();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.iluwatar.lazy.loading;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date: 12/19/15 - 12:27 PM
|
||||||
|
*
|
||||||
|
* @author Jeroen Meulemeester
|
||||||
|
*/
|
||||||
|
public class Java8HolderTest extends AbstractHolderTest {
|
||||||
|
|
||||||
|
private final Java8Holder holder = new Java8Holder();
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
Heavy getInternalHeavyValue() throws Exception {
|
||||||
|
final Field holderField = Java8Holder.class.getDeclaredField("heavy");
|
||||||
|
holderField.setAccessible(true);
|
||||||
|
|
||||||
|
final Supplier<Heavy> supplier = (Supplier<Heavy>) holderField.get(this.holder);
|
||||||
|
final Class<? extends Supplier> supplierClass = supplier.getClass();
|
||||||
|
|
||||||
|
// This is a little fishy, but I don't know another way to test this:
|
||||||
|
// The lazy holder is at first a lambda, but gets replaced with a new supplier after loading ...
|
||||||
|
if (supplierClass.isLocalClass()) {
|
||||||
|
final Field instanceField = supplierClass.getDeclaredField("heavyInstance");
|
||||||
|
instanceField.setAccessible(true);
|
||||||
|
return (Heavy) instanceField.get(supplier);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
Heavy getHeavy() throws Exception {
|
||||||
|
return holder.getHeavy();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user