Index: modules/security/test/common/unit/org/apache/harmony/security/test/TestUtils.java =================================================================== --- modules/security/test/common/unit/org/apache/harmony/security/test/TestUtils.java (revision 385732) +++ modules/security/test/common/unit/org/apache/harmony/security/test/TestUtils.java (working copy) @@ -22,6 +22,7 @@ package org.apache.harmony.security.test; import java.io.File; +import java.util.Properties; /** * Test utility class @@ -65,4 +66,20 @@ } System.out.println(""); } + + /** + * Sets system property + * + * @param key - the name of the system property. + * @param value - the value to be set + */ + public static void setSystemProperty(String key, String value) { + Properties properties = System.getProperties(); + if (value == null) { + properties.remove(key); + } else { + properties.setProperty(key, value); + } + System.setProperties(properties); + } } Index: modules/security/test/common/unit/org/apache/harmony/security/x/security/auth/login/DefaultConfigurationTest.java =================================================================== --- modules/security/test/common/unit/org/apache/harmony/security/x/security/auth/login/DefaultConfigurationTest.java (revision 385732) +++ modules/security/test/common/unit/org/apache/harmony/security/x/security/auth/login/DefaultConfigurationTest.java (working copy) @@ -34,6 +34,7 @@ import javax.security.auth.login.AppConfigurationEntry; import junit.framework.TestCase; +import org.apache.harmony.security.test.TestUtils; import org.apache.harmony.security.x.security.auth.login.DefaultConfiguration; @@ -43,6 +44,8 @@ public class DefaultConfigurationTest extends TestCase { + private static final String LOGIN_CONFIG = "java.security.auth.login.config"; + static String outputPath = System.getProperty("TEST_SRC_DIR", "test/common/unit"); static String otherConfFile = outputPath + File.separator + "org" + @@ -62,21 +65,17 @@ public void setUp() throws Exception { createConfFile(); - try { - oldp1 = Security.getProperty("login.config.url.1"); - oldp2 = Security.getProperty("login.config.url.2"); - } catch (NullPointerException e) { - } - + + oldp1 = Security.getProperty("login.config.url.1"); + oldp2 = Security.getProperty("login.config.url.2"); } public void tearDown() throws Exception { System.setSecurityManager(old); - try { - Security.setProperty("login.config.url.1", oldp1); - Security.setProperty("login.config.url.2", oldp2); - } catch (NullPointerException e) { - } + + TestUtils.setSystemProperty("login.config.url.1", oldp1); + TestUtils.setSystemProperty("login.config.url.2", oldp2); + defaultConfFile.delete(); } @@ -133,9 +132,10 @@ * using -Djava.security.auth.login.config */ public void testLoadConfigFile_1() throws IOException { + + String oldp = System.getProperty(LOGIN_CONFIG); try { - String oldp = System.getProperty("java.security.auth.login.config"); - System.setProperty("java.security.auth.login.config", + System.setProperty(LOGIN_CONFIG, new File(otherConfFile).getCanonicalPath()); DefaultConfiguration dc = new DefaultConfiguration(); assertNotNull(dc); @@ -188,16 +188,17 @@ assertEquals(m, ents[3].getOptions()); } finally { - System.setProperty("java.security.auth.login.config",""); + TestUtils.setSystemProperty(LOGIN_CONFIG, oldp); } } /** * test of the refresh method */ public void testRefresh() throws IOException { + + String oldp = System.getProperty(LOGIN_CONFIG); try { - String oldp = System.getProperty("java.security.auth.login.config"); - System.setProperty("java.security.auth.login.config", + System.setProperty(LOGIN_CONFIG, new File(otherConfFile).getCanonicalPath()); DefaultConfiguration dc = new DefaultConfiguration(); @@ -214,7 +215,7 @@ } catch (SecurityException ex) { } } finally { - System.setProperty("java.security.auth.login.config",""); + TestUtils.setSystemProperty(LOGIN_CONFIG, oldp); } } Index: modules/security/test/common/unit/javax/security/auth/PolicyTest.java =================================================================== --- modules/security/test/common/unit/javax/security/auth/PolicyTest.java (revision 385732) +++ modules/security/test/common/unit/javax/security/auth/PolicyTest.java (working copy) @@ -35,6 +35,7 @@ import junit.framework.TestCase; import org.apache.harmony.security.test.SecurityChecker; +import org.apache.harmony.security.test.TestUtils; /** @@ -156,8 +157,7 @@ } catch (SecurityException ok) { } } finally { - Security.setProperty(POLICY_PROVIDER, (oldProvider == null) ? "" - : oldProvider); + TestUtils.setSystemProperty(POLICY_PROVIDER, oldProvider); Policy.setPolicy(oldPolicy); } } @@ -179,6 +179,8 @@ + File.separator + "security" + File.separator + "auth" + File.separator + "jaas_policy2.txt"; + private static final String POLICY_PROP = "java.security.auth.policy"; + public void test_GetPermissions() throws Exception { PermissionCollection c; @@ -187,8 +189,6 @@ Enumeration e; CodeSource source; - String POLICY_PROP = "java.security.auth.policy"; - String oldProp = System.getProperty(POLICY_PROP); try { System.setProperty(POLICY_PROP, inputFile1); @@ -323,7 +323,7 @@ } } finally { - System.setProperty(POLICY_PROP, (oldProp == null) ? "" : oldProp); + TestUtils.setSystemProperty(POLICY_PROP, oldProp); } } @@ -334,8 +334,6 @@ Subject subject; Enumeration e; - String POLICY_PROP = "java.security.auth.policy"; - String oldProp = System.getProperty(POLICY_PROP); try { // @@ -370,7 +368,7 @@ assertEquals("Permission: ", per, new RuntimePermission( "createClassLoader")); } finally { - System.setProperty(POLICY_PROP, (oldProp == null) ? "" : oldProp); + TestUtils.setSystemProperty(POLICY_PROP, oldProp); } } }