From 2980468caa848632f151bba8e17562067c687c98 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Fri, 15 Dec 2006 04:47:19 +0000 Subject: When a property evolves from a boxed primitive to an unboxed primitive, null is converted to zero or false instead of throwing a NullPointerException. --- .../com/amazon/carbonado/layout/TestLayout.java | 169 ++++++++++++++++++++- 1 file changed, 167 insertions(+), 2 deletions(-) (limited to 'src/test/java/com/amazon/carbonado') diff --git a/src/test/java/com/amazon/carbonado/layout/TestLayout.java b/src/test/java/com/amazon/carbonado/layout/TestLayout.java index 98df0ee..7af8758 100644 --- a/src/test/java/com/amazon/carbonado/layout/TestLayout.java +++ b/src/test/java/com/amazon/carbonado/layout/TestLayout.java @@ -28,6 +28,7 @@ import org.joda.time.DateTime; import org.cojen.classfile.ClassFile; import org.cojen.classfile.CodeBuilder; +import org.cojen.classfile.MethodInfo; import org.cojen.classfile.Modifiers; import org.cojen.classfile.TypeDesc; import org.cojen.classfile.attribute.Annotation; @@ -37,6 +38,7 @@ import org.cojen.util.ClassInjector; import com.amazon.carbonado.adapter.YesNoAdapter; import com.amazon.carbonado.CorruptEncodingException; +import com.amazon.carbonado.Nullable; import com.amazon.carbonado.PrimaryKey; import com.amazon.carbonado.Repository; import com.amazon.carbonado.Storable; @@ -503,15 +505,22 @@ public class TestLayout extends TestCase { // Define various generations. Class typeWithInt = defineStorable(TEST_STORABLE_NAME, 1, TypeDesc.INT); + Class typeWithStr = defineStorable(TEST_STORABLE_NAME, 1, TypeDesc.STRING); + Class typeWithLong = defineStorable(TEST_STORABLE_NAME, 1, TypeDesc.LONG); + Class typeWithDate = defineStorable(TEST_STORABLE_NAME, 1, TypeDesc.forClass(DateTime.class)); + Class typeWithIntAndStr = defineStorable(TEST_STORABLE_NAME, TypeDesc.INT, TypeDesc.STRING); + Class typeWithNullableInteger = + defineStorable(TEST_STORABLE_NAME, 1, TypeDesc.forClass(Integer.class), true); + DateTime date = new DateTime(); { @@ -566,6 +575,28 @@ public class TestLayout extends TestCase { stm.insert(); } + { + // Insert Nullable Integer prop0, value is non-null + Storage storage = + mRepository.storageFor(typeWithNullableInteger); + BeanPropertyAccessor bean = BeanPropertyAccessor.forClass(typeWithNullableInteger); + StorableTestMinimal stm = storage.prepare(); + stm.setId(6); + bean.setPropertyValue(stm, "prop0", new Integer(9876)); + stm.insert(); + } + + { + // Insert Nullable Integer prop0, value is null + Storage storage = + mRepository.storageFor(typeWithNullableInteger); + BeanPropertyAccessor bean = BeanPropertyAccessor.forClass(typeWithNullableInteger); + StorableTestMinimal stm = storage.prepare(); + stm.setId(7); + bean.setPropertyValue(stm, "prop0", null); + stm.insert(); + } + // Load using int property generation. { // Load against int prop0. @@ -600,6 +631,18 @@ public class TestLayout extends TestCase { stm.setId(5); stm.load(); assertEquals(500, bean.getPropertyValue(stm, "prop0")); + + // Load against Integer prop0, value non-null. + stm = storage.prepare(); + stm.setId(6); + stm.load(); + assertEquals(9876, bean.getPropertyValue(stm, "prop0")); + + // Load against Integer prop0, value null. + stm = storage.prepare(); + stm.setId(7); + stm.load(); + assertEquals(0, bean.getPropertyValue(stm, "prop0")); } // Load using String property generation. @@ -635,6 +678,18 @@ public class TestLayout extends TestCase { stm.setId(5); stm.load(); assertEquals(null, bean.getPropertyValue(stm, "prop0")); + + // Load against Integer prop0, value non-null. + stm = storage.prepare(); + stm.setId(6); + stm.load(); + assertEquals(null, bean.getPropertyValue(stm, "prop0")); + + // Load against Integer prop0, value null. + stm = storage.prepare(); + stm.setId(7); + stm.load(); + assertEquals(null, bean.getPropertyValue(stm, "prop0")); } // Load using long property generation. @@ -670,6 +725,18 @@ public class TestLayout extends TestCase { stm.setId(5); stm.load(); assertEquals(500L, bean.getPropertyValue(stm, "prop0")); + + // Load against Integer prop0, value non-null. + stm = storage.prepare(); + stm.setId(6); + stm.load(); + assertEquals(9876L, bean.getPropertyValue(stm, "prop0")); + + // Load against Integer prop0, value null. + stm = storage.prepare(); + stm.setId(7); + stm.load(); + assertEquals(0L, bean.getPropertyValue(stm, "prop0")); } // Load using date property generation. @@ -705,6 +772,18 @@ public class TestLayout extends TestCase { stm.setId(5); stm.load(); assertEquals(null, bean.getPropertyValue(stm, "prop0")); + + // Load against Integer prop0, value non-null. + stm = storage.prepare(); + stm.setId(6); + stm.load(); + assertEquals(null, bean.getPropertyValue(stm, "prop0")); + + // Load against Integer prop0, value null. + stm = storage.prepare(); + stm.setId(7); + stm.load(); + assertEquals(null, bean.getPropertyValue(stm, "prop0")); } // Load using int and String property generation. @@ -747,6 +826,69 @@ public class TestLayout extends TestCase { stm.load(); assertEquals(500, bean.getPropertyValue(stm, "prop0")); assertEquals("world", bean.getPropertyValue(stm, "prop1")); + + // Load against Integer prop0, value non-null. + stm = storage.prepare(); + stm.setId(6); + stm.load(); + assertEquals(9876, bean.getPropertyValue(stm, "prop0")); + assertEquals(null, bean.getPropertyValue(stm, "prop1")); + + // Load against Integer prop0, value null. + stm = storage.prepare(); + stm.setId(7); + stm.load(); + assertEquals(0, bean.getPropertyValue(stm, "prop0")); + assertEquals(null, bean.getPropertyValue(stm, "prop1")); + } + + // Load using Nullable Integer property generation. + { + // Load against int prop0. + Storage storage = + mRepository.storageFor(typeWithNullableInteger); + BeanPropertyAccessor bean = BeanPropertyAccessor.forClass(typeWithNullableInteger); + StorableTestMinimal stm = storage.prepare(); + stm.setId(1); + stm.load(); + assertEquals(100, bean.getPropertyValue(stm, "prop0")); + + // Load against String prop0. + stm = storage.prepare(); + stm.setId(2); + stm.load(); + assertEquals(null, bean.getPropertyValue(stm, "prop0")); + + // Load against long prop0. + stm = storage.prepare(); + stm.setId(3); + stm.load(); + // Cast of 0x100000001L to int yields 1. + assertEquals(1, bean.getPropertyValue(stm, "prop0")); + + // Load against date prop0. + stm = storage.prepare(); + stm.setId(4); + stm.load(); + assertEquals(null, bean.getPropertyValue(stm, "prop0")); + + // Load against int prop0, String prop1. + stm = storage.prepare(); + stm.setId(5); + stm.load(); + assertEquals(500, bean.getPropertyValue(stm, "prop0")); + + // Load against Integer prop0, value non-null. + stm = storage.prepare(); + stm.setId(6); + stm.load(); + assertEquals(9876, bean.getPropertyValue(stm, "prop0")); + + // Load against Integer prop0, value null. + stm = storage.prepare(); + stm.setId(7); + stm.load(); + assertEquals(null, bean.getPropertyValue(stm, "prop0")); } } @@ -758,7 +900,7 @@ public class TestLayout extends TestCase { mRepository.close(); BDBRepositoryBuilder builder = (BDBRepositoryBuilder) TestUtilities.newTempRepositoryBuilder(); - System.out.println(builder.getEnvironmentHome()); + //System.out.println(builder.getEnvironmentHome()); builder.setLogInMemory(false); mRepository = builder.build(); @@ -835,6 +977,23 @@ public class TestLayout extends TestCase { public static Class defineStorable(String name, int extraPropCount, TypeDesc propType) + { + return defineStorable(name, extraPropCount, propType, false); + } + + /** + * Defines a new Storable with a variable number of extra properties. + * + * @param name name of class to generate + * @param extraPropCount number of properties named "propN" to add, where N + * is the zero-based property number + * @param propType type of each extra property + * @param nullable properties should be annotated as Nullable + */ + public static Class defineStorable(String name, + int extraPropCount, + TypeDesc propType, + boolean nullable) { ClassInjector ci = ClassInjector.createExplicit(name, null); ClassFile cf = new ClassFile(ci.getClassName()); @@ -845,7 +1004,13 @@ public class TestLayout extends TestCase { definePrimaryKey(cf); for (int i=0; i