1 package org.apache.ojb.broker.metadata; 2 3 import junit.framework.TestCase; 4 import org.apache.ojb.broker.PersistenceBroker; 5 import org.apache.ojb.broker.PBFactoryException; 6 import org.apache.ojb.broker.PersistenceBrokerFactory; 7 import org.apache.ojb.broker.PersistenceBrokerException; 8 import org.apache.ojb.broker.Identity; 9 import org.apache.ojb.junit.PBTestCase; 10 11 16 public class ReadonlyTest extends PBTestCase 17 { 18 public static void main(String [] args) 19 { 20 String [] arr = {ReadonlyTest.class.getName()}; 21 junit.textui.TestRunner.main(arr); 22 } 23 24 public void setUp() throws Exception 25 { 26 super.setUp(); 27 } 28 29 public void tearDown() throws Exception 30 { 31 super.tearDown(); 32 } 33 34 public void testReadonly() throws Exception 35 { 36 long timestamp = System.currentTimeMillis(); 37 TestObject obj = new TestObject(null, "testReadonly_"+timestamp, "should not persisted", new Long (timestamp)); 38 39 broker.beginTransaction(); 40 broker.store(obj); 41 broker.commitTransaction(); 42 broker.clearCache(); 43 44 broker.beginTransaction(); 45 Identity oid = broker.serviceIdentity().buildIdentity(obj); 46 TestObject ret_obj = (TestObject) broker.getObjectByIdentity(oid); 47 broker.commitTransaction(); 48 49 assertNotNull(ret_obj); 50 assertNotNull(ret_obj.getName()); 51 assertNull("Field should not be populated", ret_obj.getReadonlyLong()); 52 assertNull("Field should not be populated", ret_obj.getReadonlyString()); 53 } 54 55 public void testReadonlyAll() throws Exception 56 { 57 long timestamp = System.currentTimeMillis(); 58 TestObject obj = new TestObject(null, "testReadonlyAll_"+timestamp, "should not persisted", new Long (timestamp)); 59 ClassDescriptor cld = broker.getClassDescriptor(TestObject.class); 60 FieldDescriptor fld_id = cld.getFieldDescriptorByName("objId"); 61 FieldDescriptor fld_name = cld.getFieldDescriptorByName("name"); 62 63 try 64 { 65 broker.beginTransaction(); 66 broker.store(obj); 67 broker.commitTransaction(); 68 broker.clearCache(); 69 70 broker.beginTransaction(); 71 Identity oid = broker.serviceIdentity().buildIdentity(obj); 72 TestObject ret_obj = (TestObject) broker.getObjectByIdentity(oid); 73 broker.commitTransaction(); 74 75 fld_id.setAccess("readonly"); 76 fld_name.setAccess("readonly"); 77 78 assertNotNull(ret_obj); 79 assertNotNull(ret_obj.getName()); 80 assertNull("Field should not be populated", ret_obj.getReadonlyLong()); 81 assertNull("Field should not be populated", ret_obj.getReadonlyString()); 82 83 broker.beginTransaction(); 84 oid = broker.serviceIdentity().buildIdentity(obj); 85 ret_obj = (TestObject) broker.getObjectByIdentity(oid); 86 broker.store(ret_obj); 87 broker.commitTransaction(); 88 89 assertNotNull(ret_obj); 90 assertNotNull(ret_obj.getName()); 91 assertNull("Field should not be populated", ret_obj.getReadonlyLong()); 92 assertNull("Field should not be populated", ret_obj.getReadonlyString()); 93 94 broker.beginTransaction(); 95 oid = broker.serviceIdentity().buildIdentity(obj); 96 ret_obj = (TestObject) broker.getObjectByIdentity(oid); 97 broker.delete(ret_obj); 99 broker.commitTransaction(); 100 101 oid = broker.serviceIdentity().buildIdentity(obj); 102 ret_obj = (TestObject) broker.getObjectByIdentity(oid); 103 104 assertNull(ret_obj); 105 } 106 finally 107 { 108 if(fld_id != null) fld_id.setAccess("readwrite"); 109 if(fld_name != null) fld_name.setAccess("readwrite"); 110 } 111 } 112 113 public static class TestObject 114 { 115 private Long objId; 116 private String name; 117 private String readonlyString; 118 private Long readonlyLong; 119 120 public TestObject() 121 { 122 } 123 124 public TestObject(Long objId, String name, String readonlyString, Long readonlyLong) 125 { 126 this.objId = objId; 127 this.name = name; 128 this.readonlyString = readonlyString; 129 this.readonlyLong = readonlyLong; 130 } 131 132 public Long getObjId() 133 { 134 return objId; 135 } 136 137 public void setObjId(Long objId) 138 { 139 this.objId = objId; 140 } 141 142 public String getName() 143 { 144 return name; 145 } 146 147 public void setName(String name) 148 { 149 this.name = name; 150 } 151 152 public String getReadonlyString() 153 { 154 return readonlyString; 155 } 156 157 public void setReadonlyString(String readonlyString) 158 { 159 this.readonlyString = readonlyString; 160 } 161 162 public Long getReadonlyLong() 163 { 164 return readonlyLong; 165 } 166 167 public void setReadonlyLong(Long readonlyLong) 168 { 169 this.readonlyLong = readonlyLong; 170 } 171 } 172 } 173 | Popular Tags |