1 20 package org.apache.derbyTesting.junit; 21 22 import java.security.PrivilegedActionException ; 23 import java.util.Enumeration ; 24 import java.util.Properties ; 25 26 27 import junit.extensions.TestSetup; 28 import junit.framework.Test; 29 30 35 public class SystemPropertyTestSetup extends TestSetup { 36 37 private Properties newValues; 38 private Properties oldValues; 39 40 47 public SystemPropertyTestSetup(Test test, 48 Properties newValues) 49 { 50 super(test); 51 this.newValues = newValues; 52 this.oldValues = new Properties (); 53 } 54 55 59 protected void setUp() 60 throws java.lang.Exception 61 { 62 setProperties(newValues); 63 } 64 65 68 protected void tearDown() 69 throws java.lang.Exception 70 { 71 for (Enumeration e = newValues.propertyNames(); e.hasMoreElements();) 74 { 75 String key = (String ) e.nextElement(); 76 if (oldValues.getProperty(key) == null) 77 BaseTestCase.removeSystemProperty(key); 78 } 79 setProperties(oldValues); 81 newValues = null; 82 oldValues = null; 83 } 84 85 private void setProperties(Properties values) 86 throws PrivilegedActionException 87 { 88 for (Enumeration e = values.propertyNames(); e.hasMoreElements();) 89 { 90 String key = (String ) e.nextElement(); 91 String value = values.getProperty(key); 92 String old = BaseTestCase.getSystemProperty(key); 93 94 boolean change; 95 if (old != null) 96 { 97 change = !old.equals(value); 99 100 if (change && (values != oldValues)) 103 oldValues.setProperty(key, old); 104 } 105 else { 106 change = true; 108 } 109 110 if (change) { 111 BaseTestCase.setSystemProperty(key, value); 112 } 113 } 114 } 115 } 116 | Popular Tags |