(no commit message)
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Fri, 30 Apr 2010 10:06:04 +0000 (10:06 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Fri, 30 Apr 2010 10:06:04 +0000 (10:06 +0000)
support/general/src/test/java/org/wamblee/persistence/PersistentFactoryTest.java [new file with mode: 0644]

diff --git a/support/general/src/test/java/org/wamblee/persistence/PersistentFactoryTest.java b/support/general/src/test/java/org/wamblee/persistence/PersistentFactoryTest.java
new file mode 100644 (file)
index 0000000..9f254c6
--- /dev/null
@@ -0,0 +1,254 @@
+package org.wamblee.persistence;
+
+import static junit.framework.Assert.*;
+
+import javax.persistence.Id;
+import javax.persistence.Version;
+
+import org.junit.Test;
+import org.wamblee.persistence.PersistentFactory.Accessor;
+import org.wamblee.persistence.PersistentFactory.EntityAccessor;
+import org.wamblee.persistence.PersistentFactory.FieldAccessor;
+import org.wamblee.persistence.PersistentFactory.PropertyAccessor;
+
+public class PersistentFactoryTest {
+
+    public static class X1 {
+        @Id
+        private int pk;
+
+        public int getPk() {
+            return pk;
+        }
+
+        public void setPk(int aPk) {
+            pk = aPk;
+        }
+        
+        private void privateSetter(int aPk) { 
+            pk = aPk;
+        }
+        
+        private int privateGetter() { 
+            return pk; 
+        }
+    }
+
+    public static class X2 {
+        private int pk;
+
+        @Id
+        public int getPk() {
+            return pk;
+        }
+
+        public void setPk(int aPk) {
+            pk = aPk;
+        }
+    }
+    
+    public static class X3 {
+        private int pk;
+
+        @Id
+        private int getPk() {
+            return pk;
+        }
+
+        private void setPk(int aPk) {
+            pk = aPk;
+        }
+    }
+
+    public static class X4 {
+        @Version
+        private int version;
+
+        public int getVersion() {
+            return version;
+        }
+
+        public void setVersion(int aVersion) {
+            version = aVersion;
+        }
+        
+        private void privateSetter(int aVersion) { 
+            version = aVersion;
+        }
+        
+        private int privateGetter() { 
+            return version; 
+        }
+    }
+
+    public static class X5 {
+        private int version;
+
+        @Version
+        public int getVersion() {
+            return version;
+        }
+
+        public void setVersion(int aVersion) {
+            version = aVersion;
+        }
+    }
+    
+    public static class X6 {
+        private int version;
+
+        @Version
+        private int getVersion() {
+            return version;
+        }
+
+        private void setVersion(int aVersion) {
+            version = aVersion;
+        }
+    }
+    
+    public static class X7 { 
+        @Id
+        public int id; 
+        
+        @Version
+        public int version; 
+    }
+
+    // FieldAccessor test
+
+    @Test
+    public void testFieldAccessorSet() throws Exception {
+        Accessor<Integer> accessor = new FieldAccessor<Integer>(X1.class
+            .getDeclaredField("pk"));
+        X1 x1 = new X1();
+        assertEquals(0, x1.getPk());
+        accessor.set(x1, 10);
+        assertEquals(10, x1.getPk());
+    }
+
+    // PropertyAccessor test
+    @Test
+    public void testPropertyAccessorSet() throws Exception {
+        Accessor<Integer> accessor = new PropertyAccessor<Integer>(X1.class
+            .getDeclaredMethod("getPk"), X1.class.getDeclaredMethod("setPk",
+            int.class));
+        X1 x1 = new X1();
+        assertEquals(0, x1.getPk());
+        accessor.set(x1, 10);
+        assertEquals(10, x1.getPk());
+    }
+    
+    @Test
+    public void testPropertyAccessorPrivate() throws Exception { 
+        Accessor<Integer> accessor = new PropertyAccessor<Integer>(X1.class
+            .getDeclaredMethod("privateGetter"), X1.class.getDeclaredMethod("privateSetter",
+            int.class));
+        X1 x1 = new X1();
+        assertEquals(0, x1.getPk());
+        accessor.set(x1, 10);
+        assertEquals((Integer)10, accessor.get(x1));
+        
+    }
+
+    // EntityAccessor test
+
+    @Test
+    public void testEntityAccessorPkField() {
+        EntityAccessor accessor = PersistentFactory.createEntityAccessor(X1.class);
+        assertNotNull(accessor);
+        assertTrue(accessor.getPk() instanceof FieldAccessor);
+        assertNull(accessor.getVersion());
+        assertEquals("pk", ((FieldAccessor) accessor.getPk()).getField()
+            .getName());
+    }
+
+    @Test
+    public void testEntityAccessorPkProperty() {
+        EntityAccessor accessor = PersistentFactory.createEntityAccessor(X2.class);
+        assertNotNull(accessor);
+        assertNotNull(accessor.getPk());
+        assertTrue(accessor.getPk() instanceof PropertyAccessor);
+        assertNull(accessor.getVersion());
+        PropertyAccessor property = (PropertyAccessor) accessor.getPk();
+        assertEquals("getPk", property.getGetter().getName());
+        assertEquals("setPk", property.getSetter().getName());
+    }
+    
+    @Test
+    public void testEntityAccessorPkPropertyPrivate() {
+        EntityAccessor accessor = PersistentFactory.createEntityAccessor(X3.class);
+        assertNotNull(accessor);
+        assertNotNull(accessor.getPk());
+        assertTrue(accessor.getPk() instanceof PropertyAccessor);
+        assertNull(accessor.getVersion());
+        PropertyAccessor property = (PropertyAccessor) accessor.getPk();
+        assertEquals("getPk", property.getGetter().getName());
+        assertEquals("setPk", property.getSetter().getName());
+    }
+
+    @Test
+    public void testEntityAccessorVersionField() {
+        EntityAccessor accessor = PersistentFactory.createEntityAccessor(X4.class);
+        assertNotNull(accessor);
+        assertTrue(accessor.getVersion() instanceof FieldAccessor);
+        assertNull(accessor.getPk());
+        assertEquals("version", ((FieldAccessor) accessor.getVersion()).getField()
+            .getName());
+    }
+
+    @Test
+    public void testEntityAccessorVersionProperty() {
+        EntityAccessor accessor = PersistentFactory.createEntityAccessor(X5.class);
+        assertNotNull(accessor);
+        assertNotNull(accessor.getVersion());
+        assertTrue(accessor.getVersion() instanceof PropertyAccessor);
+        assertNull(accessor.getPk());
+        PropertyAccessor property = (PropertyAccessor) accessor.getVersion();
+        assertEquals("getVersion", property.getGetter().getName());
+        assertEquals("setVersion", property.getSetter().getName());
+    }
+    
+    @Test
+    public void testEntityAccessorVersionPropertyPrivate() {
+        EntityAccessor accessor = PersistentFactory.createEntityAccessor(X6.class);
+        assertNotNull(accessor);
+        assertNotNull(accessor.getVersion());
+        assertTrue(accessor.getVersion() instanceof PropertyAccessor);
+        assertNull(accessor.getPk());
+        PropertyAccessor property = (PropertyAccessor) accessor.getVersion();
+        assertEquals("getVersion", property.getGetter().getName());
+        assertEquals("setVersion", property.getSetter().getName());
+    }
+    
+    @Test
+    public void testNoVersionAndNoPk() { 
+        EntityAccessor accessor = PersistentFactory.createEntityAccessor(String.class);
+        assertNull(accessor);
+    }
+
+    @Test
+    public void testPkAndVersion() { 
+        EntityAccessor accessor = PersistentFactory.createEntityAccessor(X7.class);
+        assertNotNull(accessor.getPk());
+        assertNotNull(accessor.getVersion());
+        
+        X7 x = new X7();
+        assertEquals(0, x.id);
+        assertEquals(0, x.version);
+        
+        accessor.getPk().set(x, 10); 
+        accessor.getVersion().set(x, 20);
+        
+        assertEquals(10, x.id);
+        assertEquals(20, x.version);
+    }
+    
+    @Test 
+    public void testEntityAccessorCache() { 
+        EntityAccessor accessor = PersistentFactory.createEntityAccessor(X1.class);
+        EntityAccessor accessor2 = PersistentFactory.createEntityAccessor(X1.class);
+        
+        assertSame(accessor, accessor2);
+    }
+}