1 package org.apache.ojb.junit; 2 3 import java.util.HashMap ; 4 import java.util.Iterator ; 5 import java.util.Map ; 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 21 public class OJBTestCase extends TestCase 22 { 23 private static final String SKIP_STR = "OJB.skip.issues"; 24 private static final String SKIP_DEFAULT_VALUE = "false"; 25 private MetadataHelper referenceHelper; 26 27 public OJBTestCase() 28 { 29 } 30 31 public OJBTestCase(String name) 32 { 33 super(name); 34 } 35 36 protected void setUp() throws Exception 37 { 38 super.setUp(); 39 ojbSleep(); 42 } 43 44 protected void tearDown() throws Exception 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 67 public void ojbSleep() 68 { 69 try 70 { 71 Thread.sleep(11); 74 } 75 catch (InterruptedException ignore) 76 { 77 } 78 } 79 80 83 public void ojbSkipTestMessage(String message) 84 { 85 if(message == null) 86 { 87 message = "No description, please see test case"; 88 } 89 String className = this.getClass().getName(); 90 System.out.println("# [Skip test in " + className + "] " + message + " #"); 91 } 92 93 98 public boolean ojbSkipKnownIssueProblem() 99 { 100 return ojbSkipKnownIssueProblem(null); 101 } 102 103 108 public boolean ojbSkipKnownIssueProblem(String message) 109 { 110 String result = SKIP_DEFAULT_VALUE; 111 boolean skip = false; 112 try 113 { 114 result = System.getProperty(SKIP_STR, result); 115 skip = new Boolean (result).booleanValue(); 116 } 117 catch(Exception 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 className = this.getClass().getName(); 128 System.out.println("# [Skip known issue in " + className + "] " + message + " #"); 129 } 130 return skip; 131 } 132 133 145 public void ojbChangeReferenceSetting(Class clazz, String 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 177 public void ojbChangeReferenceSetting(Class clazz, String 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 209 public class MetadataHelper 210 { 211 private Map oldSettings; 212 213 public MetadataHelper() 214 { 215 oldSettings = new HashMap (); 216 } 217 218 protected void restoreMetadataSettings(PersistenceBroker broker) 219 { 220 if(oldSettings.size() == 0) return; 221 Iterator it = oldSettings.entrySet().iterator(); 222 Map.Entry entry; 223 while(it.hasNext()) 224 { 225 entry = (Map.Entry ) it.next(); 226 String clazz = (String ) entry.getKey(); 227 Map fieldMap = (Map ) entry.getValue(); 228 Iterator iter = fieldMap.entrySet().iterator(); 229 Map.Entry entry2; 230 ClassDescriptor cld = broker.getDescriptorRepository().getDescriptorFor(clazz); 231 while(iter.hasNext()) 232 { 233 entry2 = (Map.Entry ) iter.next(); 234 String oldRefName = (String ) entry2.getKey(); 235 ObjectReferenceDescriptor oldRef = (ObjectReferenceDescriptor) entry2.getValue(); 236 ObjectReferenceDescriptor ref = cld.getCollectionDescriptorByName(oldRefName); 238 if(ref == null) ref = cld.getObjectReferenceDescriptorByName(oldRefName); 239 240 ref.setCascadeRetrieve(oldRef.getCascadeRetrieve()); 244 ref.setCascadingStore(oldRef.getCascadingStore()); 245 ref.setCascadingDelete(oldRef.getCascadingDelete()); 246 ref.setLazy(oldRef.isLazy()); 247 } 250 } 251 oldSettings.clear(); 252 } 253 254 public void changeReferenceSetting(PersistenceBroker broker, Class clazz, 255 String 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 } 277 278 public void changeReferenceSetting(PersistenceBroker broker, Class clazz, 279 String 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 } 301 302 void prepareSetting(ObjectReferenceDescriptor ref, ClassDescriptor cld, Class clazz, String referenceField) 303 { 304 HashMap fieldMap = (HashMap ) oldSettings.get(cld.getClassNameOfObject()); 305 if(fieldMap == null) 306 { 307 fieldMap = new HashMap (); 308 oldSettings.put(cld.getClassNameOfObject(), fieldMap); 309 } 310 311 ObjectReferenceDescriptor oldRef = (ObjectReferenceDescriptor) fieldMap.get(ref.getPersistentField().getName()); 312 if(oldRef == null) 314 { 315 oldRef = (ObjectReferenceDescriptor) SerializationUtils.clone(ref); 317 fieldMap.put(ref.getPersistentField().getName(), oldRef); 318 } 319 } 320 } 321 } 322 | Popular Tags |