KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > metadata > ReadonlyTest


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 /**
12  *
13  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
14  * @version $Id: ReadonlyTest.java,v 1.1.4.1 2005/12/05 15:06:32 arminw Exp $
15  */

16 public class ReadonlyTest extends PBTestCase
17 {
18     public static void main(String JavaDoc[] args)
19     {
20         String JavaDoc[] arr = {ReadonlyTest.class.getName()};
21         junit.textui.TestRunner.main(arr);
22     }
23
24     public void setUp() throws Exception JavaDoc
25     {
26         super.setUp();
27     }
28
29     public void tearDown() throws Exception JavaDoc
30     {
31         super.tearDown();
32     }
33
34     public void testReadonly() throws Exception JavaDoc
35     {
36         long timestamp = System.currentTimeMillis();
37         TestObject obj = new TestObject(null, "testReadonly_"+timestamp, "should not persisted", new Long JavaDoc(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 JavaDoc
56     {
57         long timestamp = System.currentTimeMillis();
58         TestObject obj = new TestObject(null, "testReadonlyAll_"+timestamp, "should not persisted", new Long JavaDoc(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             // nevertheless we can remove the whole object
98
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 JavaDoc objId;
116         private String JavaDoc name;
117         private String JavaDoc readonlyString;
118         private Long JavaDoc readonlyLong;
119
120         public TestObject()
121         {
122         }
123
124         public TestObject(Long JavaDoc objId, String JavaDoc name, String JavaDoc readonlyString, Long JavaDoc readonlyLong)
125         {
126             this.objId = objId;
127             this.name = name;
128             this.readonlyString = readonlyString;
129             this.readonlyLong = readonlyLong;
130         }
131
132         public Long JavaDoc getObjId()
133         {
134             return objId;
135         }
136
137         public void setObjId(Long JavaDoc objId)
138         {
139             this.objId = objId;
140         }
141
142         public String JavaDoc getName()
143         {
144             return name;
145         }
146
147         public void setName(String JavaDoc name)
148         {
149             this.name = name;
150         }
151
152         public String JavaDoc getReadonlyString()
153         {
154             return readonlyString;
155         }
156
157         public void setReadonlyString(String JavaDoc readonlyString)
158         {
159             this.readonlyString = readonlyString;
160         }
161
162         public Long JavaDoc getReadonlyLong()
163         {
164             return readonlyLong;
165         }
166
167         public void setReadonlyLong(Long JavaDoc readonlyLong)
168         {
169             this.readonlyLong = readonlyLong;
170         }
171     }
172 }
173
Popular Tags