1 26 27 package net.sourceforge.groboutils.junit.v1; 28 29 import java.util.Properties ; 30 import java.util.Vector ; 31 import java.util.Enumeration ; 32 33 36 37 52 public class SysPropertiesUtil 53 { 54 private Properties originals; 55 private Properties newProps; 56 57 58 62 public SysPropertiesUtil() 63 { 64 this.originals = System.getProperties(); 65 restoreProps(); 66 } 67 68 69 74 public void setValue( String key, String newVal ) 75 { 76 if (key == null) 77 { 78 throw new IllegalArgumentException ( "No null keys." ); 79 } 80 synchronized( this ) 81 { 82 if (newVal == null) 83 { 84 this.newProps.remove( key ); 85 } 86 else 87 { 88 this.newProps.setProperty( key, newVal ); 89 } 90 setSystemProperties( this.newProps ); 91 } 92 } 93 94 95 99 public synchronized void reset() 100 { 101 setSystemProperties( this.originals ); 102 restoreProps(); 103 } 104 105 106 private void setSystemProperties( Properties props ) 107 { 108 System.setProperties( props ); 109 } 110 111 112 private void restoreProps() 113 { 114 this.newProps = (Properties )this.originals.clone(); 115 } 116 } 117 118 | Popular Tags |