KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > junit > OJBTestCase


1 package org.apache.ojb.junit;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import junit.framework.TestCase;
8 import org.apache.commons.lang.SerializationUtils;
9 import org.apache.ojb.broker.OJBRuntimeException;
10 import org.apache.ojb.broker.PersistenceBroker;
11 import org.apache.ojb.broker.PersistenceBrokerFactory;
12 import org.apache.ojb.broker.metadata.ClassDescriptor;
13 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
14
15 /**
16  * Extension of the JUnit test class.
17  *
18  * @author <a HREF="mailto:arminw@apache.org">Armin Waibel</a>
19  * @version $Id: OJBTestCase.java,v 1.5.2.6 2005/03/18 19:22:30 arminw Exp $
20  */

21 public class OJBTestCase extends TestCase
22 {
23     private static final String JavaDoc SKIP_STR = "OJB.skip.issues";
24     private static final String JavaDoc SKIP_DEFAULT_VALUE = "false";
25     private MetadataHelper referenceHelper;
26
27     public OJBTestCase()
28     {
29     }
30
31     public OJBTestCase(String JavaDoc name)
32     {
33         super(name);
34     }
35
36     protected void setUp() throws Exception JavaDoc
37     {
38         super.setUp();
39         // sleep thread to guarantee different timestamp values for
40
// each test
41
ojbSleep();
42     }
43
44     protected void tearDown() throws Exception JavaDoc
45     {
46         super.tearDown();
47         if(referenceHelper != null)
48         {
49             PersistenceBroker temp = PersistenceBrokerFactory.defaultPersistenceBroker();
50             try
51             {
52                 referenceHelper.restoreMetadataSettings(temp);
53             }
54             finally
55             {
56                 if(temp != null)
57                 {
58                     temp.close();
59                 }
60             }
61         }
62     }
63
64     /**
65      * Sleep current thread for a minimal period.
66      */

67     public void ojbSleep()
68     {
69         try
70         {
71             // most systems has system time precision of 10 msec
72
// so wait to guarantee new system time value for each test.
73
Thread.sleep(11);
74         }
75         catch (InterruptedException JavaDoc ignore)
76         {
77         }
78     }
79
80     /**
81      * This method could be used to print a message before skip 'problematic' test cases.
82      */

83     public void ojbSkipTestMessage(String JavaDoc message)
84     {
85         if(message == null)
86         {
87            message = "No description, please see test case";
88         }
89         String JavaDoc className = this.getClass().getName();
90         System.out.println("# [Skip test in " + className + "] " + message + " #");
91     }
92
93     /**
94      * This method could be used to skip 'problematic' test cases or known issues before
95      * a release was made. To enable the skipped tests set a system property 'skip.issues'
96      * to <tt>false</tt>.
97      */

98     public boolean ojbSkipKnownIssueProblem()
99     {
100         return ojbSkipKnownIssueProblem(null);
101     }
102
103     /**
104      * This method could be used to skip 'problematic' test cases or known issues before
105      * a release was made. To enable the skipped tests set a system property 'skip.issues'
106      * to <tt>false</tt>.
107      */

108     public boolean ojbSkipKnownIssueProblem(String JavaDoc message)
109     {
110         String JavaDoc result = SKIP_DEFAULT_VALUE;
111         boolean skip = false;
112         try
113         {
114             result = System.getProperty(SKIP_STR, result);
115             skip = new Boolean JavaDoc(result).booleanValue();
116         }
117         catch(Exception JavaDoc e)
118         {
119             System.err.println("Seems that system property '" + SKIP_STR + "=" + result + "' is not a valid boolean value");
120         }
121         if(skip)
122         {
123             if(message == null)
124             {
125                message = "No description, please see test case";
126             }
127             String JavaDoc className = this.getClass().getName();
128             System.out.println("# [Skip known issue in " + className + "] " + message + " #");
129         }
130         return skip;
131     }
132
133     /**
134      * Allows to do a global change of object/collection reference settings. When the test
135      * is tear down the old settings will be restored. Be careful when override setUp/tearDown method, don't
136      * forget the "super call", else this method couldn't work properly.
137      *
138      * @param clazz
139      * @param referenceField
140      * @param autoRetrieve
141      * @param autoUpdate
142      * @param autoDelete
143      * @param useProxy
144      */

145     public void ojbChangeReferenceSetting(Class JavaDoc clazz, String JavaDoc referenceField, boolean autoRetrieve, int autoUpdate, int autoDelete, boolean useProxy)
146     {
147         if(referenceHelper == null)
148         {
149             referenceHelper = new MetadataHelper();
150         }
151         PersistenceBroker temp = PersistenceBrokerFactory.defaultPersistenceBroker();
152         try
153         {
154             referenceHelper.changeReferenceSetting(temp, clazz, referenceField, autoRetrieve, autoUpdate, autoDelete, useProxy);
155         }
156         finally
157         {
158             if(temp != null)
159             {
160                 temp.close();
161             }
162         }
163     }
164
165     /**
166      * Allows to do a global change of object/collection reference settings. When the test
167      * is tear down the old settings will be restored. Be careful when override setUp/tearDown method, don't
168      * forget the "super call", else this method couldn't work properly.
169      *
170      * @param clazz
171      * @param referenceField
172      * @param autoRetrieve
173      * @param autoUpdate
174      * @param autoDelete
175      * @param useProxy
176      */

177     public void ojbChangeReferenceSetting(Class JavaDoc clazz, String JavaDoc referenceField, boolean autoRetrieve, boolean autoUpdate, boolean autoDelete, boolean useProxy)
178     {
179         if(referenceHelper == null)
180         {
181             referenceHelper = new MetadataHelper();
182         }
183         PersistenceBroker temp = PersistenceBrokerFactory.defaultPersistenceBroker();
184         try
185         {
186             referenceHelper.changeReferenceSetting(temp, clazz, referenceField, autoRetrieve, autoUpdate, autoDelete, useProxy);
187         }
188         finally
189         {
190             if(temp != null)
191             {
192                 temp.close();
193             }
194         }
195     }
196
197
198
199     //================================================================
200
// inner class
201
//================================================================
202
/**
203      * Class that help us to do changes on metadata and restore old state on
204      * tear down of the test.
205      * NOTE: This strategy is not recommended in production application because
206      * the made changes will be global and all threads will recognize them immediately.
207      *
208      */

209     public class MetadataHelper
210     {
211         private Map JavaDoc oldSettings;
212
213         public MetadataHelper()
214         {
215             oldSettings = new HashMap JavaDoc();
216         }
217
218         protected void restoreMetadataSettings(PersistenceBroker broker)
219         {
220             if(oldSettings.size() == 0) return;
221             Iterator JavaDoc it = oldSettings.entrySet().iterator();
222             Map.Entry JavaDoc entry;
223             while(it.hasNext())
224             {
225                 entry = (Map.Entry JavaDoc) it.next();
226                 String JavaDoc clazz = (String JavaDoc) entry.getKey();
227                 Map JavaDoc fieldMap = (Map JavaDoc) entry.getValue();
228                 Iterator JavaDoc iter = fieldMap.entrySet().iterator();
229                 Map.Entry JavaDoc entry2;
230                 ClassDescriptor cld = broker.getDescriptorRepository().getDescriptorFor(clazz);
231                 while(iter.hasNext())
232                 {
233                     entry2 = (Map.Entry JavaDoc) iter.next();
234                     String JavaDoc oldRefName = (String JavaDoc) entry2.getKey();
235                     ObjectReferenceDescriptor oldRef = (ObjectReferenceDescriptor) entry2.getValue();
236                     // lookup single object or collection descriptor
237
ObjectReferenceDescriptor ref = cld.getCollectionDescriptorByName(oldRefName);
238                     if(ref == null) ref = cld.getObjectReferenceDescriptorByName(oldRefName);
239
240 // System.out.println("Restoring metadata for " + clazz
241
// + " from " + ref.toXML()
242
// + " === to ===> " + oldRef.toXML());
243
ref.setCascadeRetrieve(oldRef.getCascadeRetrieve());
244                     ref.setCascadingStore(oldRef.getCascadingStore());
245                     ref.setCascadingDelete(oldRef.getCascadingDelete());
246                     ref.setLazy(oldRef.isLazy());
247 // System.out.println("Restore metadata for " + clazz
248
// + " to " + ref.toXML());
249
}
250             }
251             oldSettings.clear();
252         }
253
254         public void changeReferenceSetting(PersistenceBroker broker, Class JavaDoc clazz,
255                                            String JavaDoc referenceField, boolean autoRetrieve,
256                                            int autoUpdate, int autoDelete, boolean useProxy)
257         {
258             ClassDescriptor cld = broker.getClassDescriptor(clazz);
259             ObjectReferenceDescriptor ref = cld.getCollectionDescriptorByName(referenceField);
260             ref = cld.getCollectionDescriptorByName(referenceField);
261             if(ref == null) ref = cld.getObjectReferenceDescriptorByName(referenceField);
262             if(ref == null)
263             {
264                 throw new OJBRuntimeException("Given field " + referenceField + " does not match a reference in " + clazz);
265             }
266
267             prepareSetting(ref, cld, clazz, referenceField);
268
269             ref.setLazy(useProxy);
270             ref.setCascadeRetrieve(autoRetrieve);
271             ref.setCascadingStore(autoUpdate);
272             ref.setCascadingDelete(autoDelete);
273
274 // System.out.println("old settings: " + oldRef.toXML());
275
// System.out.println("new settings: " + ref.toXML());
276
}
277
278         public void changeReferenceSetting(PersistenceBroker broker, Class JavaDoc clazz,
279                                            String JavaDoc referenceField, boolean autoRetrieve,
280                                            boolean autoUpdate, boolean autoDelete, boolean useProxy)
281         {
282             ClassDescriptor cld = broker.getClassDescriptor(clazz);
283             ObjectReferenceDescriptor ref = cld.getCollectionDescriptorByName(referenceField);
284             ref = cld.getCollectionDescriptorByName(referenceField);
285             if(ref == null) ref = cld.getObjectReferenceDescriptorByName(referenceField);
286             if(ref == null)
287             {
288                 throw new OJBRuntimeException("Given field " + referenceField + " does not match a reference in " + clazz);
289             }
290
291             prepareSetting(ref, cld, clazz, referenceField);
292
293             ref.setLazy(useProxy);
294             ref.setCascadeRetrieve(autoRetrieve);
295             ref.setCascadeStore(autoUpdate);
296             ref.setCascadeDelete(autoDelete);
297
298 // System.out.println("old settings: " + oldRef.toXML());
299
// System.out.println("new settings: " + ref.toXML());
300
}
301
302         void prepareSetting(ObjectReferenceDescriptor ref, ClassDescriptor cld, Class JavaDoc clazz, String JavaDoc referenceField)
303         {
304             HashMap JavaDoc fieldMap = (HashMap JavaDoc) oldSettings.get(cld.getClassNameOfObject());
305             if(fieldMap == null)
306             {
307                 fieldMap = new HashMap JavaDoc();
308                 oldSettings.put(cld.getClassNameOfObject(), fieldMap);
309             }
310
311             ObjectReferenceDescriptor oldRef = (ObjectReferenceDescriptor) fieldMap.get(ref.getPersistentField().getName());
312             // if we don't find old settings buffer it
313
if(oldRef == null)
314             {
315                 // buffer deep copy of old settings
316
oldRef = (ObjectReferenceDescriptor) SerializationUtils.clone(ref);
317                 fieldMap.put(ref.getPersistentField().getName(), oldRef);
318             }
319         }
320     }
321 }
322
Popular Tags