001: package org.apache.ojb.broker.metadata;
002:
003: import junit.framework.TestCase;
004: import org.apache.ojb.broker.PersistenceBroker;
005: import org.apache.ojb.broker.PBFactoryException;
006: import org.apache.ojb.broker.PersistenceBrokerFactory;
007: import org.apache.ojb.broker.PersistenceBrokerException;
008: import org.apache.ojb.broker.Identity;
009: import org.apache.ojb.junit.PBTestCase;
010:
011: /**
012: *
013: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
014: * @version $Id: ReadonlyTest.java,v 1.1.4.1 2005/12/05 15:06:32 arminw Exp $
015: */
016: public class ReadonlyTest extends PBTestCase {
017: public static void main(String[] args) {
018: String[] arr = { ReadonlyTest.class.getName() };
019: junit.textui.TestRunner.main(arr);
020: }
021:
022: public void setUp() throws Exception {
023: super .setUp();
024: }
025:
026: public void tearDown() throws Exception {
027: super .tearDown();
028: }
029:
030: public void testReadonly() throws Exception {
031: long timestamp = System.currentTimeMillis();
032: TestObject obj = new TestObject(null, "testReadonly_"
033: + timestamp, "should not persisted",
034: new Long(timestamp));
035:
036: broker.beginTransaction();
037: broker.store(obj);
038: broker.commitTransaction();
039: broker.clearCache();
040:
041: broker.beginTransaction();
042: Identity oid = broker.serviceIdentity().buildIdentity(obj);
043: TestObject ret_obj = (TestObject) broker
044: .getObjectByIdentity(oid);
045: broker.commitTransaction();
046:
047: assertNotNull(ret_obj);
048: assertNotNull(ret_obj.getName());
049: assertNull("Field should not be populated", ret_obj
050: .getReadonlyLong());
051: assertNull("Field should not be populated", ret_obj
052: .getReadonlyString());
053: }
054:
055: public void testReadonlyAll() throws Exception {
056: long timestamp = System.currentTimeMillis();
057: TestObject obj = new TestObject(null, "testReadonlyAll_"
058: + timestamp, "should not persisted",
059: new Long(timestamp));
060: ClassDescriptor cld = broker
061: .getClassDescriptor(TestObject.class);
062: FieldDescriptor fld_id = cld.getFieldDescriptorByName("objId");
063: FieldDescriptor fld_name = cld.getFieldDescriptorByName("name");
064:
065: try {
066: broker.beginTransaction();
067: broker.store(obj);
068: broker.commitTransaction();
069: broker.clearCache();
070:
071: broker.beginTransaction();
072: Identity oid = broker.serviceIdentity().buildIdentity(obj);
073: TestObject ret_obj = (TestObject) broker
074: .getObjectByIdentity(oid);
075: broker.commitTransaction();
076:
077: fld_id.setAccess("readonly");
078: fld_name.setAccess("readonly");
079:
080: assertNotNull(ret_obj);
081: assertNotNull(ret_obj.getName());
082: assertNull("Field should not be populated", ret_obj
083: .getReadonlyLong());
084: assertNull("Field should not be populated", ret_obj
085: .getReadonlyString());
086:
087: broker.beginTransaction();
088: oid = broker.serviceIdentity().buildIdentity(obj);
089: ret_obj = (TestObject) broker.getObjectByIdentity(oid);
090: broker.store(ret_obj);
091: broker.commitTransaction();
092:
093: assertNotNull(ret_obj);
094: assertNotNull(ret_obj.getName());
095: assertNull("Field should not be populated", ret_obj
096: .getReadonlyLong());
097: assertNull("Field should not be populated", ret_obj
098: .getReadonlyString());
099:
100: broker.beginTransaction();
101: oid = broker.serviceIdentity().buildIdentity(obj);
102: ret_obj = (TestObject) broker.getObjectByIdentity(oid);
103: // nevertheless we can remove the whole object
104: broker.delete(ret_obj);
105: broker.commitTransaction();
106:
107: oid = broker.serviceIdentity().buildIdentity(obj);
108: ret_obj = (TestObject) broker.getObjectByIdentity(oid);
109:
110: assertNull(ret_obj);
111: } finally {
112: if (fld_id != null)
113: fld_id.setAccess("readwrite");
114: if (fld_name != null)
115: fld_name.setAccess("readwrite");
116: }
117: }
118:
119: public static class TestObject {
120: private Long objId;
121: private String name;
122: private String readonlyString;
123: private Long readonlyLong;
124:
125: public TestObject() {
126: }
127:
128: public TestObject(Long objId, String name,
129: String readonlyString, Long readonlyLong) {
130: this .objId = objId;
131: this .name = name;
132: this .readonlyString = readonlyString;
133: this .readonlyLong = readonlyLong;
134: }
135:
136: public Long getObjId() {
137: return objId;
138: }
139:
140: public void setObjId(Long objId) {
141: this .objId = objId;
142: }
143:
144: public String getName() {
145: return name;
146: }
147:
148: public void setName(String name) {
149: this .name = name;
150: }
151:
152: public String getReadonlyString() {
153: return readonlyString;
154: }
155:
156: public void setReadonlyString(String readonlyString) {
157: this .readonlyString = readonlyString;
158: }
159:
160: public Long getReadonlyLong() {
161: return readonlyLong;
162: }
163:
164: public void setReadonlyLong(Long readonlyLong) {
165: this.readonlyLong = readonlyLong;
166: }
167: }
168: }
|