diff options
38 files changed, 657 insertions, 671 deletions
diff --git a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java index 146c679..fb326a6 100644 --- a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java +++ b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java @@ -23,6 +23,7 @@ import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestTargetNew; import tests.support.Support_PlatformFile; import tests.support.resource.Support_Resources; +import tests.util.TestEnvironment; import java.io.ByteArrayOutputStream; import java.io.File; @@ -142,7 +143,6 @@ public class ZipFileTest extends junit.framework.TestCase { args = {java.lang.String.class} ) public void test_ConstructorLjava_lang_String() throws IOException { - String oldUserDir = System.getProperty("user.dir"); System.setProperty("user.dir", System.getProperty("java.io.tmpdir")); zfile.close(); // about to reopen the same temp file @@ -167,7 +167,6 @@ public class ZipFileTest extends junit.framework.TestCase { // expected } finally { System.setSecurityManager(oldSm); - System.setProperty("user.dir", oldUserDir); } } @@ -583,6 +582,7 @@ public class ZipFileTest extends junit.framework.TestCase { */ @Override protected void tearDown() { + TestEnvironment.reset(); try { if (zfile != null) { // Note zfile is a user-defined zip file used by other tests and diff --git a/crypto/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/CipherTest.java b/crypto/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/CipherTest.java index 1245eb3..c7c1a5d 100644 --- a/crypto/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/CipherTest.java +++ b/crypto/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/CipherTest.java @@ -25,6 +25,7 @@ import dalvik.annotation.TestTargets; import org.apache.harmony.crypto.tests.support.MyCipher; import tests.support.resource.Support_Resources; +import tests.util.TestEnvironment; import java.io.ByteArrayOutputStream; import java.io.File; @@ -86,7 +87,12 @@ public class CipherTest extends junit.framework.TestCase { fail("No key " + e); } } - + + @Override protected void setUp() throws Exception { + super.setUp(); + TestEnvironment.reset(); + } + /** * @tests javax.crypto.Cipher#getInstance(java.lang.String) */ diff --git a/crypto/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/EncryptedPrivateKeyInfoTest.java b/crypto/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/EncryptedPrivateKeyInfoTest.java index d3d0857..63ed789 100644 --- a/crypto/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/EncryptedPrivateKeyInfoTest.java +++ b/crypto/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/EncryptedPrivateKeyInfoTest.java @@ -57,6 +57,7 @@ import javax.crypto.spec.PBEParameterSpec; import org.apache.harmony.crypto.tests.support.EncryptedPrivateKeyInfoData; import junit.framework.TestCase; +import tests.util.TestEnvironment; @TestTargetClass(EncryptedPrivateKeyInfo.class) /** @@ -195,7 +196,12 @@ public class EncryptedPrivateKeyInfoTest extends TestCase { // {"RSA",null}, // 1.2.840.113549.1.1.1 // {"1.2.840.113549.1.1.1", null}, }; - + + @Override protected void setUp() throws Exception { + super.setUp(); + TestEnvironment.reset(); + } + @TestTargetNew( level = TestLevel.COMPLETE, notes = "", diff --git a/crypto/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/func/CipherAesWrapTest.java b/crypto/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/func/CipherAesWrapTest.java index d196edc..a5d8d90 100644 --- a/crypto/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/func/CipherAesWrapTest.java +++ b/crypto/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/func/CipherAesWrapTest.java @@ -22,10 +22,17 @@ import dalvik.annotation.TestTargetNew; import junit.framework.TestCase; import targets.Cipher; +import tests.util.TestEnvironment; @TestTargetClass(Cipher.AESWrap.class) public class CipherAesWrapTest extends TestCase { -// 3 cases checked + + @Override protected void setUp() throws Exception { + super.setUp(); + TestEnvironment.reset(); + } + + // 3 cases checked @TestTargetNew( level = TestLevel.COMPLETE, notes = "", diff --git a/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/FileHandlerTest.java b/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/FileHandlerTest.java index 3ff1fc9..e4ef413 100644 --- a/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/FileHandlerTest.java +++ b/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/FileHandlerTest.java @@ -50,6 +50,7 @@ import junit.framework.TestCase; import org.apache.harmony.logging.tests.java.util.logging.HandlerTest.NullOutputStream; import org.apache.harmony.logging.tests.java.util.logging.util.EnvironmentHelper; +import tests.util.TestEnvironment; /** */ @@ -62,18 +63,11 @@ public class FileHandlerTest extends TestCase { final static String className = FileHandlerTest.class.getName(); - final static StringWriter writer = new StringWriter(); - - final static SecurityManager securityManager = new MockLogSecurityManager(); - final static String SEP = File.separator; - private String oldHomePath = System.getProperty("user.home"); - - // The HOMEPATH can't be used in android. - final static String HOMEPATH = System.getProperty("java.io.tmpdir") + SEP + "home"; + String HOMEPATH; - final static String TEMPPATH = System.getProperty("java.io.tmpdir"); + String TEMPPATH; private final PrintStream err = System.err; @@ -88,6 +82,7 @@ public class FileHandlerTest extends TestCase { */ protected void setUp() throws Exception { super.setUp(); + TestEnvironment.reset(); manager.reset(); //initProp @@ -106,19 +101,10 @@ public class FileHandlerTest extends TestCase { props.put("java.util.logging.FileHandler.append", "true"); props.put("java.util.logging.FileHandler.pattern", "%t/log/java%u.test"); - - File home = new File(HOMEPATH); - if (!home.exists()) { - home.mkdirs(); - } else if (!home.isDirectory()) { - home.delete(); - home.mkdirs(); - } - if(!home.isDirectory()) { - fail("unable to create temp path"); - } - System.setProperty("user.home", HOMEPATH); - + + HOMEPATH = System.getProperty("user.home"); + TEMPPATH = System.getProperty("java.io.tmpdir"); + File file = new File(TEMPPATH + SEP + "log"); file.mkdir(); manager.readConfiguration(EnvironmentHelper @@ -140,8 +126,7 @@ public class FileHandlerTest extends TestCase { } reset(TEMPPATH + SEP + "log", ""); System.setErr(err); - System.setProperty("user.home", oldHomePath); - new File(HOMEPATH).delete(); + TestEnvironment.reset(); super.tearDown(); } @@ -634,8 +619,6 @@ public class FileHandlerTest extends TestCase { assertFileContent(TEMPPATH, "testLimitCount0.0", new LogRecord[] { rs[9] }, handler.getFormatter()); - String oldUserDir = System.getProperty("user.dir"); - System.setProperty("user.dir", System.getProperty("java.io.tmpdir")); FileHandler h1 = null; FileHandler h2 = null; try { @@ -656,7 +639,6 @@ public class FileHandlerTest extends TestCase { } catch (Exception e) { } reset("log", ""); - System.setProperty("user.dir", oldUserDir); } } @TestTargets({ @@ -1000,14 +982,6 @@ public class FileHandlerTest extends TestCase { fail("should throw IllegalArgumentException"); } catch (IllegalArgumentException e) { } - - // always parse special pattern - System.setProperty("user.home", "home"); - try { - h1 = new FileHandler("%t/%h.txt"); - } catch (Exception e) { - fail("Unexpected exception " + e.toString()); - } } /* diff --git a/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/LogManagerTest.java b/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/LogManagerTest.java index cf608bc..44389be 100644 --- a/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/LogManagerTest.java +++ b/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/LogManagerTest.java @@ -45,6 +45,7 @@ import dalvik.annotation.TestLevel; import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestTargetNew; import dalvik.annotation.TestTargets; +import tests.util.TestEnvironment; /** * @@ -107,6 +108,7 @@ public class LogManagerTest extends TestCase { */ @Override protected void tearDown() throws Exception { + TestEnvironment.reset(); super.tearDown(); handler = null; } @@ -1194,24 +1196,17 @@ public class LogManagerTest extends TestCase { args = {} ) public void testValidConfigClass() throws Exception { - String oldProperty = System.getProperty("java.util.logging.config.class"); - try { - // System.setProperty("java.util.logging.config.class", "org.apache.harmony.logging.tests.java.util.logging.LogManagerTest$ConfigClass"); - System.setProperty("java.util.logging.config.class", this.getClass().getName() - + "$ConfigClass"); - assertNull(manager.getLogger("testConfigClass.foo")); - - manager.readConfiguration(); - assertNull(manager.getLogger("testConfigClass.foo")); - Logger l = Logger.getLogger("testConfigClass.foo.child"); - assertSame(Level.FINEST, manager.getLogger("").getLevel()); - assertEquals(0, manager.getLogger("").getHandlers().length); - assertEquals("testConfigClass.foo", l.getParent().getName()); - } finally { - if (oldProperty != null) { - System.setProperty("java.util.logging.config.class", oldProperty); - } - } + // System.setProperty("java.util.logging.config.class", "org.apache.harmony.logging.tests.java.util.logging.LogManagerTest$ConfigClass"); + System.setProperty("java.util.logging.config.class", this.getClass().getName() + + "$ConfigClass"); + assertNull(manager.getLogger("testConfigClass.foo")); + + manager.readConfiguration(); + assertNull(manager.getLogger("testConfigClass.foo")); + Logger l = Logger.getLogger("testConfigClass.foo.child"); + assertSame(Level.FINEST, manager.getLogger("").getLevel()); + assertEquals(0, manager.getLogger("").getHandlers().length); + assertEquals("testConfigClass.foo", l.getParent().getName()); } /* diff --git a/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/LogRecordTest.java b/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/LogRecordTest.java index 5b21099..6d4f784 100644 --- a/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/LogRecordTest.java +++ b/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/LogRecordTest.java @@ -295,7 +295,7 @@ public class LogRecordTest extends TestCase { assertNull(lr.getSourceClassName()); // find class and method who called logger - Logger logger = Logger.global; + Logger logger = Logger.getLogger("testGetSourceDefaultValue"); MockHandler handler = new MockHandler(); logger.addHandler(handler); logger.log(Level.SEVERE, MSG); diff --git a/luni/src/test/java/org/apache/harmony/luni/tests/internal/net/www/protocol/https/HttpsURLConnectionTest.java b/luni/src/test/java/org/apache/harmony/luni/tests/internal/net/www/protocol/https/HttpsURLConnectionTest.java index 275ea9d..e26cf74 100644 --- a/luni/src/test/java/org/apache/harmony/luni/tests/internal/net/www/protocol/https/HttpsURLConnectionTest.java +++ b/luni/src/test/java/org/apache/harmony/luni/tests/internal/net/www/protocol/https/HttpsURLConnectionTest.java @@ -25,6 +25,7 @@ import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestTargetNew; import dalvik.annotation.TestTargets; import junit.framework.TestCase; +import tests.util.TestEnvironment; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; @@ -99,19 +100,6 @@ public class HttpsURLConnectionTest extends TestCase { // Proxy authentication required response code private static final int AUTHENTICATION_REQUIRED_CODE = 407; - // fields keeping the system values of corresponding properties - private static String systemKeyStoreType; - - private static String systemKeyStore; - - private static String systemKeyStorePassword; - - private static String systemTrustStoreType; - - private static String systemTrustStore; - - private static String systemTrustStorePassword; - private static File store; static { @@ -136,19 +124,14 @@ public class HttpsURLConnectionTest extends TestCase { // set up the properties defining the default values needed by SSL stuff setUpStoreProperties(); - try { - SSLSocketFactory defaultSSLSF = HttpsURLConnection - .getDefaultSSLSocketFactory(); - ServerSocket ss = new ServerSocket(0); - Socket s = defaultSSLSF - .createSocket("localhost", ss.getLocalPort()); - ss.accept(); - s.close(); - ss.close(); - } finally { - // roll the properties back to system values - tearDownStoreProperties(); - } + SSLSocketFactory defaultSSLSF = HttpsURLConnection + .getDefaultSSLSocketFactory(); + ServerSocket ss = new ServerSocket(0); + Socket s = defaultSSLSF + .createSocket("localhost", ss.getLocalPort()); + ss.accept(); + s.close(); + ss.close(); } /** @@ -171,33 +154,28 @@ public class HttpsURLConnectionTest extends TestCase { // set up the properties defining the default values needed by SSL stuff setUpStoreProperties(); - try { - // create the SSL server socket acting as a server - SSLContext ctx = getContext(); - ServerSocket ss = ctx.getServerSocketFactory() - .createServerSocket(0); + // create the SSL server socket acting as a server + SSLContext ctx = getContext(); + ServerSocket ss = ctx.getServerSocketFactory() + .createServerSocket(0); - // create the HostnameVerifier to check hostname verification - TestHostnameVerifier hnv = new TestHostnameVerifier(); - HttpsURLConnection.setDefaultHostnameVerifier(hnv); + // create the HostnameVerifier to check hostname verification + TestHostnameVerifier hnv = new TestHostnameVerifier(); + HttpsURLConnection.setDefaultHostnameVerifier(hnv); - // create url connection to be tested - URL url = new URL("https://localhost:" + ss.getLocalPort()); - HttpsURLConnection connection = (HttpsURLConnection) url - .openConnection(); + // create url connection to be tested + URL url = new URL("https://localhost:" + ss.getLocalPort()); + HttpsURLConnection connection = (HttpsURLConnection) url + .openConnection(); - // perform the interaction between the peers - SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss); + // perform the interaction between the peers + SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss); - // check the connection state - checkConnectionStateParameters(connection, peerSocket); + // check the connection state + checkConnectionStateParameters(connection, peerSocket); - // should silently exit - connection.connect(); - } finally { - // roll the properties back to system values - tearDownStoreProperties(); - } + // should silently exit + connection.connect(); } /** @@ -231,37 +209,32 @@ public class HttpsURLConnectionTest extends TestCase { // set up the properties defining the default values needed by SSL stuff setUpStoreProperties(); - try { - // create the SSL server socket acting as a server - SSLContext ctx = getContext(); - ServerSocket ss = ctx.getServerSocketFactory() - .createServerSocket(0); + // create the SSL server socket acting as a server + SSLContext ctx = getContext(); + ServerSocket ss = ctx.getServerSocketFactory() + .createServerSocket(0); - // create the HostnameVerifier to check hostname verification - TestHostnameVerifier hnv = new TestHostnameVerifier(); - HttpsURLConnection.setDefaultHostnameVerifier(hnv); + // create the HostnameVerifier to check hostname verification + TestHostnameVerifier hnv = new TestHostnameVerifier(); + HttpsURLConnection.setDefaultHostnameVerifier(hnv); - // create url connection to be tested - URL url = new URL("https://localhost:" + ss.getLocalPort()); - HttpsURLConnection connection = (HttpsURLConnection) url - .openConnection(); + // create url connection to be tested + URL url = new URL("https://localhost:" + ss.getLocalPort()); + HttpsURLConnection connection = (HttpsURLConnection) url + .openConnection(); - try { - doInteraction(connection, ss, NOT_FOUND_CODE); - fail("Expected exception was not thrown."); - } catch (FileNotFoundException e) { - if (DO_LOG) { - System.out.println("Expected exception was thrown: " - + e.getMessage()); - } + try { + doInteraction(connection, ss, NOT_FOUND_CODE); + fail("Expected exception was not thrown."); + } catch (FileNotFoundException e) { + if (DO_LOG) { + System.out.println("Expected exception was thrown: " + + e.getMessage()); } - - // should silently exit - connection.connect(); - } finally { - // roll the properties back to system values - tearDownStoreProperties(); } + + // should silently exit + connection.connect(); } /** @@ -458,39 +431,34 @@ public class HttpsURLConnectionTest extends TestCase { // setting up the properties pointing to the key/trust stores setUpStoreProperties(); - try { - // create the SSLServerSocket which will be used by server side - SSLServerSocket ss = (SSLServerSocket) getContext() - .getServerSocketFactory().createServerSocket(0); - - // create the HostnameVerifier to check that Hostname verification - // is done - TestHostnameVerifier hnv = new TestHostnameVerifier(); - HttpsURLConnection.setDefaultHostnameVerifier(hnv); - - // create HttpsURLConnection to be tested - URL url = new URL("https://localhost:" + ss.getLocalPort()); - HttpsURLConnection connection = (HttpsURLConnection) url - .openConnection(); - - TestHostnameVerifier hnv_late = new TestHostnameVerifier(); - // replace default verifier - connection.setHostnameVerifier(hnv_late); - - // perform the interaction between the peers and check the results - SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss); - assertTrue("Hostname verification was not done", hnv_late.verified); - assertFalse( - "Hostname verification should not be done by this verifier", - hnv.verified); - checkConnectionStateParameters(connection, peerSocket); - - // should silently exit - connection.connect(); - } finally { - // roll the properties back to system values - tearDownStoreProperties(); - } + // create the SSLServerSocket which will be used by server side + SSLServerSocket ss = (SSLServerSocket) getContext() + .getServerSocketFactory().createServerSocket(0); + + // create the HostnameVerifier to check that Hostname verification + // is done + TestHostnameVerifier hnv = new TestHostnameVerifier(); + HttpsURLConnection.setDefaultHostnameVerifier(hnv); + + // create HttpsURLConnection to be tested + URL url = new URL("https://localhost:" + ss.getLocalPort()); + HttpsURLConnection connection = (HttpsURLConnection) url + .openConnection(); + + TestHostnameVerifier hnv_late = new TestHostnameVerifier(); + // replace default verifier + connection.setHostnameVerifier(hnv_late); + + // perform the interaction between the peers and check the results + SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss); + assertTrue("Hostname verification was not done", hnv_late.verified); + assertFalse( + "Hostname verification should not be done by this verifier", + hnv.verified); + checkConnectionStateParameters(connection, peerSocket); + + // should silently exit + connection.connect(); } /** @@ -509,32 +477,27 @@ public class HttpsURLConnectionTest extends TestCase { // setting up the properties pointing to the key/trust stores setUpStoreProperties(); - try { - // create the SSLServerSocket which will be used by server side - SSLServerSocket ss = (SSLServerSocket) getContext() - .getServerSocketFactory().createServerSocket(0); - - // create the HostnameVerifier to check that Hostname verification - // is done - TestHostnameVerifier hnv = new TestHostnameVerifier(); - HttpsURLConnection.setDefaultHostnameVerifier(hnv); - - // create HttpsURLConnection to be tested - URL url = new URL("https://localhost:" + ss.getLocalPort()); - HttpsURLConnection connection = (HttpsURLConnection) url - .openConnection(); - connection.setDoOutput(true); - - // perform the interaction between the peers and check the results - SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss); - checkConnectionStateParameters(connection, peerSocket); - - // should silently exit - connection.connect(); - } finally { - // roll the properties back to system values - tearDownStoreProperties(); - } + // create the SSLServerSocket which will be used by server side + SSLServerSocket ss = (SSLServerSocket) getContext() + .getServerSocketFactory().createServerSocket(0); + + // create the HostnameVerifier to check that Hostname verification + // is done + TestHostnameVerifier hnv = new TestHostnameVerifier(); + HttpsURLConnection.setDefaultHostnameVerifier(hnv); + + // create HttpsURLConnection to be tested + URL url = new URL("https://localhost:" + ss.getLocalPort()); + HttpsURLConnection connection = (HttpsURLConnection) url + .openConnection(); + connection.setDoOutput(true); + + // perform the interaction between the peers and check the results + SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss); + checkConnectionStateParameters(connection, peerSocket); + + // should silently exit + connection.connect(); } /** @@ -566,32 +529,27 @@ public class HttpsURLConnectionTest extends TestCase { // setting up the properties pointing to the key/trust stores setUpStoreProperties(); - try { - // create the SSLServerSocket which will be used by server side - ServerSocket ss = new ServerSocket(0); - - // create the HostnameVerifier to check that Hostname verification - // is done - TestHostnameVerifier hnv = new TestHostnameVerifier(); - HttpsURLConnection.setDefaultHostnameVerifier(hnv); - - // create HttpsURLConnection to be tested - URL url = new URL("https://requested.host:55556/requested.data"); - HttpsURLConnection connection = (HttpsURLConnection) url - .openConnection(new Proxy(Proxy.Type.HTTP, - new InetSocketAddress("localhost", ss - .getLocalPort()))); - - // perform the interaction between the peers and check the results - SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss); - checkConnectionStateParameters(connection, peerSocket); - - // should silently exit - connection.connect(); - } finally { - // roll the properties back to system values - tearDownStoreProperties(); - } + // create the SSLServerSocket which will be used by server side + ServerSocket ss = new ServerSocket(0); + + // create the HostnameVerifier to check that Hostname verification + // is done + TestHostnameVerifier hnv = new TestHostnameVerifier(); + HttpsURLConnection.setDefaultHostnameVerifier(hnv); + + // create HttpsURLConnection to be tested + URL url = new URL("https://requested.host:55556/requested.data"); + HttpsURLConnection connection = (HttpsURLConnection) url + .openConnection(new Proxy(Proxy.Type.HTTP, + new InetSocketAddress("localhost", ss + .getLocalPort()))); + + // perform the interaction between the peers and check the results + SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss); + checkConnectionStateParameters(connection, peerSocket); + + // should silently exit + connection.connect(); } /** @@ -624,40 +582,35 @@ public class HttpsURLConnectionTest extends TestCase { // setting up the properties pointing to the key/trust stores setUpStoreProperties(); - try { - // create the SSLServerSocket which will be used by server side - ServerSocket ss = new ServerSocket(0); + // create the SSLServerSocket which will be used by server side + ServerSocket ss = new ServerSocket(0); - // create the HostnameVerifier to check that Hostname verification - // is done - TestHostnameVerifier hnv = new TestHostnameVerifier(); - HttpsURLConnection.setDefaultHostnameVerifier(hnv); + // create the HostnameVerifier to check that Hostname verification + // is done + TestHostnameVerifier hnv = new TestHostnameVerifier(); + HttpsURLConnection.setDefaultHostnameVerifier(hnv); - Authenticator.setDefault(new Authenticator() { + Authenticator.setDefault(new Authenticator() { - protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication("user", "password" - .toCharArray()); - } - }); + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication("user", "password" + .toCharArray()); + } + }); - // create HttpsURLConnection to be tested - URL url = new URL("https://requested.host:55555/requested.data"); - HttpsURLConnection connection = (HttpsURLConnection) url - .openConnection(new Proxy(Proxy.Type.HTTP, - new InetSocketAddress("localhost", ss - .getLocalPort()))); + // create HttpsURLConnection to be tested + URL url = new URL("https://requested.host:55555/requested.data"); + HttpsURLConnection connection = (HttpsURLConnection) url + .openConnection(new Proxy(Proxy.Type.HTTP, + new InetSocketAddress("localhost", ss + .getLocalPort()))); - // perform the interaction between the peers and check the results - SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss); - checkConnectionStateParameters(connection, peerSocket); + // perform the interaction between the peers and check the results + SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss); + checkConnectionStateParameters(connection, peerSocket); - // should silently exit - connection.connect(); - } finally { - // roll the properties back to system values - tearDownStoreProperties(); - } + // should silently exit + connection.connect(); } /** @@ -692,40 +645,35 @@ public class HttpsURLConnectionTest extends TestCase { // setting up the properties pointing to the key/trust stores setUpStoreProperties(); - try { - // create the SSLServerSocket which will be used by server side - ServerSocket ss = new ServerSocket(0); - - // create the HostnameVerifier to check that Hostname verification - // is done - TestHostnameVerifier hnv = new TestHostnameVerifier(); - HttpsURLConnection.setDefaultHostnameVerifier(hnv); - - // create HttpsURLConnection to be tested - URL url = new URL("https://requested.host:55555/requested.data"); - HttpsURLConnection connection = (HttpsURLConnection) url - .openConnection(new Proxy(Proxy.Type.HTTP, - new InetSocketAddress("localhost", ss - .getLocalPort()))); - - // perform the interaction between the peers and check the results - SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss); - checkConnectionStateParameters(connection, peerSocket); - - // create another SSLServerSocket which will be used by server side - ss = new ServerSocket(0); - - connection = (HttpsURLConnection) url.openConnection(new Proxy( - Proxy.Type.HTTP, new InetSocketAddress("localhost", ss - .getLocalPort()))); - - // perform the interaction between the peers and check the results - peerSocket = (SSLSocket) doInteraction(connection, ss); - checkConnectionStateParameters(connection, peerSocket); - } finally { - // roll the properties back to system values - tearDownStoreProperties(); - } + // create the SSLServerSocket which will be used by server side + ServerSocket ss = new ServerSocket(0); + + // create the HostnameVerifier to check that Hostname verification + // is done + TestHostnameVerifier hnv = new TestHostnameVerifier(); + HttpsURLConnection.setDefaultHostnameVerifier(hnv); + + // create HttpsURLConnection to be tested + URL url = new URL("https://requested.host:55555/requested.data"); + HttpsURLConnection connection = (HttpsURLConnection) url + .openConnection(new Proxy(Proxy.Type.HTTP, + new InetSocketAddress("localhost", ss + .getLocalPort()))); + + // perform the interaction between the peers and check the results + SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss); + checkConnectionStateParameters(connection, peerSocket); + + // create another SSLServerSocket which will be used by server side + ss = new ServerSocket(0); + + connection = (HttpsURLConnection) url.openConnection(new Proxy( + Proxy.Type.HTTP, new InetSocketAddress("localhost", ss + .getLocalPort()))); + + // perform the interaction between the peers and check the results + peerSocket = (SSLSocket) doInteraction(connection, ss); + checkConnectionStateParameters(connection, peerSocket); } /** @@ -765,39 +713,34 @@ public class HttpsURLConnectionTest extends TestCase { // setting up the properties pointing to the key/trust stores setUpStoreProperties(); - try { - // create the SSLServerSocket which will be used by server side - ServerSocket ss = new ServerSocket(0); + // create the SSLServerSocket which will be used by server side + ServerSocket ss = new ServerSocket(0); - // create the HostnameVerifier to check that Hostname verification - // is done - TestHostnameVerifier hnv = new TestHostnameVerifier(); - HttpsURLConnection.setDefaultHostnameVerifier(hnv); + // create the HostnameVerifier to check that Hostname verification + // is done + TestHostnameVerifier hnv = new TestHostnameVerifier(); + HttpsURLConnection.setDefaultHostnameVerifier(hnv); - Authenticator.setDefault(new Authenticator() { + Authenticator.setDefault(new Authenticator() { - protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication("user", "password" - .toCharArray()); - } - }); - - // create HttpsURLConnection to be tested - URL url = new URL("https://requested.host:55554/requested.data"); - HttpsURLConnection connection = (HttpsURLConnection) url - .openConnection(new Proxy(Proxy.Type.HTTP, - new InetSocketAddress("localhost", ss - .getLocalPort()))); - connection.setDoOutput(true); - - // perform the interaction between the peers and check the results - SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss, - OK_CODE, true); - checkConnectionStateParameters(connection, peerSocket); - } finally { - // roll the properties back to system values - tearDownStoreProperties(); - } + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication("user", "password" + .toCharArray()); + } + }); + + // create HttpsURLConnection to be tested + URL url = new URL("https://requested.host:55554/requested.data"); + HttpsURLConnection connection = (HttpsURLConnection) url + .openConnection(new Proxy(Proxy.Type.HTTP, + new InetSocketAddress("localhost", ss + .getLocalPort()))); + connection.setDoOutput(true); + + // perform the interaction between the peers and check the results + SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss, + OK_CODE, true); + checkConnectionStateParameters(connection, peerSocket); } /** @@ -830,36 +773,31 @@ public class HttpsURLConnectionTest extends TestCase { // setting up the properties pointing to the key/trust stores setUpStoreProperties(); + // create the SSLServerSocket which will be used by server side + ServerSocket ss = new ServerSocket(0); + + // create the HostnameVerifier to check that Hostname verification + // is done + TestHostnameVerifier hnv = new TestHostnameVerifier(); + HttpsURLConnection.setDefaultHostnameVerifier(hnv); + + // create HttpsURLConnection to be tested + URL url = new URL("https://requested.host:55555/requested.data"); + HttpURLConnection connection = (HttpURLConnection) url + .openConnection(new Proxy(Proxy.Type.HTTP, + new InetSocketAddress("localhost", ss + .getLocalPort()))); + + // perform the interaction between the peers and check the results try { - // create the SSLServerSocket which will be used by server side - ServerSocket ss = new ServerSocket(0); - - // create the HostnameVerifier to check that Hostname verification - // is done - TestHostnameVerifier hnv = new TestHostnameVerifier(); - HttpsURLConnection.setDefaultHostnameVerifier(hnv); - - // create HttpsURLConnection to be tested - URL url = new URL("https://requested.host:55555/requested.data"); - HttpURLConnection connection = (HttpURLConnection) url - .openConnection(new Proxy(Proxy.Type.HTTP, - new InetSocketAddress("localhost", ss - .getLocalPort()))); - - // perform the interaction between the peers and check the results - try { - doInteraction(connection, ss, AUTHENTICATION_REQUIRED_CODE, - true); - } catch (IOException e) { - // SSL Tunnelling failed - if (DO_LOG) { - System.out.println("Got expected IOException: " - + e.getMessage()); - } + doInteraction(connection, ss, AUTHENTICATION_REQUIRED_CODE, + true); + } catch (IOException e) { + // SSL Tunnelling failed + if (DO_LOG) { + System.out.println("Got expected IOException: " + + e.getMessage()); } - } finally { - // roll the properties back to system values - tearDownStoreProperties(); } } @@ -893,34 +831,29 @@ public class HttpsURLConnectionTest extends TestCase { // setting up the properties pointing to the key/trust stores setUpStoreProperties(); - try { - // create the SSLServerSocket which will be used by server side - ServerSocket ss = new ServerSocket(0); + // create the SSLServerSocket which will be used by server side + ServerSocket ss = new ServerSocket(0); - // create the HostnameVerifier to check that Hostname verification - // is done - TestHostnameVerifier hnv = new TestHostnameVerifier(); - HttpsURLConnection.setDefaultHostnameVerifier(hnv); + // create the HostnameVerifier to check that Hostname verification + // is done + TestHostnameVerifier hnv = new TestHostnameVerifier(); + HttpsURLConnection.setDefaultHostnameVerifier(hnv); - // create HttpsURLConnection to be tested - URL url = new URL("https://localhost:" + ss.getLocalPort()); - HttpURLConnection connection = (HttpURLConnection) url - .openConnection(new Proxy(Proxy.Type.HTTP, - new InetSocketAddress("localhost", ss - .getLocalPort()))); + // create HttpsURLConnection to be tested + URL url = new URL("https://localhost:" + ss.getLocalPort()); + HttpURLConnection connection = (HttpURLConnection) url + .openConnection(new Proxy(Proxy.Type.HTTP, + new InetSocketAddress("localhost", ss + .getLocalPort()))); - try { - doInteraction(connection, ss, NOT_FOUND_CODE); // NOT FOUND - fail("Expected exception was not thrown."); - } catch (FileNotFoundException e) { - if (DO_LOG) { - System.out.println("Expected exception was thrown: " - + e.getMessage()); - } + try { + doInteraction(connection, ss, NOT_FOUND_CODE); // NOT FOUND + fail("Expected exception was not thrown."); + } catch (FileNotFoundException e) { + if (DO_LOG) { + System.out.println("Expected exception was thrown: " + + e.getMessage()); } - } finally { - // roll the properties back to system values - tearDownStoreProperties(); } } @@ -932,6 +865,9 @@ public class HttpsURLConnectionTest extends TestCase { * Log the name of the test case to be executed. */ public void setUp() throws Exception { + super.setUp(); + TestEnvironment.reset(); + if (DO_LOG) { System.out.println(); System.out.println("------------------------"); @@ -959,6 +895,7 @@ public class HttpsURLConnectionTest extends TestCase { } public void tearDown() { + TestEnvironment.reset(); if (store != null) { store.delete(); } @@ -1042,17 +979,6 @@ public class HttpsURLConnectionTest extends TestCase { private static void setUpStoreProperties() throws Exception { String type = KeyStore.getDefaultType(); - systemKeyStoreType = System.getProperty("javax.net.ssl.keyStoreType"); - systemKeyStore = System.getProperty("javax.net.ssl.keyStore"); - systemKeyStorePassword = System - .getProperty("javax.net.ssl.keyStorePassword"); - - systemTrustStoreType = System - .getProperty("javax.net.ssl.trustStoreType"); - systemTrustStore = System.getProperty("javax.net.ssl.trustStore"); - systemTrustStorePassword = System - .getProperty("javax.net.ssl.trustStorePassword"); - System.setProperty("javax.net.ssl.keyStoreType", type); System.setProperty("javax.net.ssl.keyStore", getKeyStoreFileName()); System.setProperty("javax.net.ssl.keyStorePassword", KS_PASSWORD); @@ -1063,48 +989,6 @@ public class HttpsURLConnectionTest extends TestCase { } /** - * Rolls back the values of system properties. - */ - private static void tearDownStoreProperties() { - if (systemKeyStoreType == null) { - System.clearProperty("javax.net.ssl.keyStoreType"); - } else { - System - .setProperty("javax.net.ssl.keyStoreType", - systemKeyStoreType); - } - if (systemKeyStore == null) { - System.clearProperty("javax.net.ssl.keyStore"); - } else { - System.setProperty("javax.net.ssl.keyStore", systemKeyStore); - } - if (systemKeyStorePassword == null) { - System.clearProperty("javax.net.ssl.keyStorePassword"); - } else { - System.setProperty("javax.net.ssl.keyStorePassword", - systemKeyStorePassword); - } - - if (systemTrustStoreType == null) { - System.clearProperty("javax.net.ssl.trustStoreType"); - } else { - System.setProperty("javax.net.ssl.trustStoreType", - systemTrustStoreType); - } - if (systemTrustStore == null) { - System.clearProperty("javax.net.ssl.trustStore"); - } else { - System.setProperty("javax.net.ssl.trustStore", systemTrustStore); - } - if (systemTrustStorePassword == null) { - System.clearProperty("javax.net.ssl.trustStorePassword"); - } else { - System.setProperty("javax.net.ssl.trustStorePassword", - systemTrustStorePassword); - } - } - - /** * Performs interaction between client's HttpURLConnection and * servers side (ServerSocket). */ diff --git a/luni/src/test/java/org/apache/harmony/luni/tests/java/io/FileTest.java b/luni/src/test/java/org/apache/harmony/luni/tests/java/io/FileTest.java index 84cddf2..53ce506 100644 --- a/luni/src/test/java/org/apache/harmony/luni/tests/java/io/FileTest.java +++ b/luni/src/test/java/org/apache/harmony/luni/tests/java/io/FileTest.java @@ -25,10 +25,21 @@ import org.apache.harmony.testframework.serialization.SerializationTest; import dalvik.annotation.TestLevel; import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestTargetNew; +import tests.util.TestEnvironment; @TestTargetClass(File.class) public class FileTest extends TestCase { + @Override protected void setUp() throws Exception { + super.setUp(); + TestEnvironment.reset(); + } + + @Override protected void tearDown() throws Exception { + TestEnvironment.reset(); + super.tearDown(); + } + /** * @tests java.io.File#File(java.io.File, java.lang.String) */ diff --git a/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/RuntimeTest.java b/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/RuntimeTest.java index b1de669..1b61d6f 100644 --- a/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/RuntimeTest.java +++ b/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/RuntimeTest.java @@ -33,6 +33,7 @@ import java.util.Arrays; import java.util.Vector; import tests.support.resource.Support_Resources; +import tests.util.TestEnvironment; @TestTargetClass(Runtime.class) public class RuntimeTest extends junit.framework.TestCase { @@ -72,6 +73,11 @@ public class RuntimeTest extends junit.framework.TestCase { return new RuntimeTest("FT"); } + @Override protected void tearDown() throws Exception { + TestEnvironment.reset(); + super.tearDown(); + } + /** * @tests java.lang.Runtime#exec(java.lang.String) */ @@ -864,7 +870,6 @@ public class RuntimeTest extends junit.framework.TestCase { byte[] expected = {72, 0, 101, 0, 97, 0, 114, 0, 116, 0, 32, 0, 60, 47}; byte[] returned = new byte[expected.length]; - String oldEncoding = System.getProperty("file.encoding"); System.setProperty("file.encoding", "UTF-16LE"); try { @@ -883,8 +888,6 @@ public class RuntimeTest extends junit.framework.TestCase { Arrays.equals(expected, returned)); } catch (UnsupportedEncodingException e) { fail("UnsupportedEncodingException was thrown."); - } finally { - System.setProperty("file.encoding", oldEncoding); } } diff --git a/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/SystemTest.java b/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/SystemTest.java index f17c3d8..08412d2 100644 --- a/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/SystemTest.java +++ b/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/SystemTest.java @@ -22,6 +22,7 @@ import dalvik.annotation.TestTargets; import dalvik.annotation.TestLevel; import dalvik.annotation.TestTargetNew; import dalvik.annotation.TestTargetClass; +import tests.util.TestEnvironment; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -1207,10 +1208,16 @@ public class SystemTest extends junit.framework.TestCase { @Override protected void setUp() { + TestEnvironment.reset(); flag = false; ranFinalize = false; } + @Override protected void tearDown() throws Exception { + TestEnvironment.reset(); + super.tearDown(); + } + protected SystemTest createInstance() { return new SystemTest("FT"); } diff --git a/luni/src/test/java/org/apache/harmony/luni/tests/java/net/InetAddressTest.java b/luni/src/test/java/org/apache/harmony/luni/tests/java/net/InetAddressTest.java index 5635ecb..4053271 100644 --- a/luni/src/test/java/org/apache/harmony/luni/tests/java/net/InetAddressTest.java +++ b/luni/src/test/java/org/apache/harmony/luni/tests/java/net/InetAddressTest.java @@ -39,6 +39,7 @@ import org.apache.harmony.testframework.serialization.SerializationTest; import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert; import tests.support.Support_Configuration; +import tests.util.TestEnvironment; @TestTargetClass(InetAddress.class) public class InetAddressTest extends junit.framework.TestCase { @@ -49,6 +50,16 @@ public class InetAddressTest extends junit.framework.TestCase { protected static String threadedTestErrorString; + @Override protected void setUp() throws Exception { + super.setUp(); + TestEnvironment.reset(); + } + + @Override protected void tearDown() throws Exception { + TestEnvironment.reset(); + super.tearDown(); + } + /** * This class is used to test inet_ntoa, gethostbyaddr and gethostbyname * functions in the VM to make sure they're threadsafe. getByName will cause @@ -391,60 +402,47 @@ public class InetAddressTest extends junit.framework.TestCase { } // Make sure there is no caching - String originalPropertyValue = System - .getProperty("networkaddress.cache.ttl"); System.setProperty("networkaddress.cache.ttl", "0"); // Test for threadsafety - try { - InetAddress lookup1 = InetAddress - .getByName(Support_Configuration.InetTestAddress); - assertTrue(lookup1 + " expected " - + Support_Configuration.InetTestIP, - Support_Configuration.InetTestIP.equals(lookup1 - .getHostAddress())); - InetAddress lookup2 = InetAddress - .getByName(Support_Configuration.InetTestAddress2); - assertTrue(lookup2 + " expected " - + Support_Configuration.InetTestIP2, - Support_Configuration.InetTestIP2.equals(lookup2 - .getHostAddress())); - threadsafeTestThread thread1 = new threadsafeTestThread("1", - lookup1.getHostName(), lookup1, 0); - threadsafeTestThread thread2 = new threadsafeTestThread("2", - lookup2.getHostName(), lookup2, 0); - threadsafeTestThread thread3 = new threadsafeTestThread("3", - lookup1.getHostAddress(), lookup1, 1); - threadsafeTestThread thread4 = new threadsafeTestThread("4", - lookup2.getHostAddress(), lookup2, 1); - - // initialize the flags - threadedTestSucceeded = true; - synchronized (someoneDone) { - thread1.start(); - thread2.start(); - thread3.start(); - thread4.start(); - } - thread1.join(); - thread2.join(); - thread3.join(); - thread4.join(); - /* FIXME: comment the assertion below because it is platform/configuration dependent - * Please refer to HARMONY-1664 (https://issues.apache.org/jira/browse/HARMONY-1664) - * for details - */ -// assertTrue(threadedTestErrorString, threadedTestSucceeded); - } finally { - // restore the old value of the property - if (originalPropertyValue == null) - // setting the property to -1 has the same effect as having the - // property be null - System.setProperty("networkaddress.cache.ttl", "-1"); - else - System.setProperty("networkaddress.cache.ttl", - originalPropertyValue); + InetAddress lookup1 = InetAddress + .getByName(Support_Configuration.InetTestAddress); + assertTrue(lookup1 + " expected " + + Support_Configuration.InetTestIP, + Support_Configuration.InetTestIP.equals(lookup1 + .getHostAddress())); + InetAddress lookup2 = InetAddress + .getByName(Support_Configuration.InetTestAddress2); + assertTrue(lookup2 + " expected " + + Support_Configuration.InetTestIP2, + Support_Configuration.InetTestIP2.equals(lookup2 + .getHostAddress())); + threadsafeTestThread thread1 = new threadsafeTestThread("1", + lookup1.getHostName(), lookup1, 0); + threadsafeTestThread thread2 = new threadsafeTestThread("2", + lookup2.getHostName(), lookup2, 0); + threadsafeTestThread thread3 = new threadsafeTestThread("3", + lookup1.getHostAddress(), lookup1, 1); + threadsafeTestThread thread4 = new threadsafeTestThread("4", + lookup2.getHostAddress(), lookup2, 1); + + // initialize the flags + threadedTestSucceeded = true; + synchronized (someoneDone) { + thread1.start(); + thread2.start(); + thread3.start(); + thread4.start(); } + thread1.join(); + thread2.join(); + thread3.join(); + thread4.join(); + /* FIXME: comment the assertion below because it is platform/configuration dependent + * Please refer to HARMONY-1664 (https://issues.apache.org/jira/browse/HARMONY-1664) + * for details + */ +// assertTrue(threadedTestErrorString, threadedTestSucceeded); } /** diff --git a/luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLTest.java b/luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLTest.java index b4eb0ad..188d88d 100644 --- a/luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLTest.java +++ b/luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLTest.java @@ -27,6 +27,7 @@ import junit.framework.TestCase; import tests.support.Support_Configuration; import tests.support.Support_PortManager; import tests.support.resource.Support_Resources; +import tests.util.TestEnvironment; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -60,6 +61,17 @@ import java.util.List; public class URLTest extends TestCase { private static final String helloWorldString = "Hello World"; + + @Override protected void setUp() throws Exception { + super.setUp(); + TestEnvironment.reset(); + } + + @Override protected void tearDown() throws Exception { + TestEnvironment.reset(); + super.tearDown(); + } + /** * @tests java.net.URL#getHost() */ @@ -137,19 +149,12 @@ public class URLTest extends TestCase { public void test_java_protocol_handler_pkgs_prop() throws MalformedURLException { // Regression test for Harmony-3094 final String HANDLER_PKGS = "java.protocol.handler.pkgs"; - String pkgs = System.getProperty(HANDLER_PKGS); System.setProperty(HANDLER_PKGS, "fake|org.apache.harmony.luni.tests.java.net"); try { new URL("test_protocol", "", "fake.jar"); } catch (MalformedURLException e) { // expected - } finally { - if (pkgs == null) { - System.clearProperty(HANDLER_PKGS); - } else { - System.setProperty(HANDLER_PKGS, pkgs); - } } } diff --git a/luni/src/test/java/tests/api/java/io/FilePermissionTest.java b/luni/src/test/java/tests/api/java/io/FilePermissionTest.java index 27f416d..2fa0828 100644 --- a/luni/src/test/java/tests/api/java/io/FilePermissionTest.java +++ b/luni/src/test/java/tests/api/java/io/FilePermissionTest.java @@ -17,28 +17,34 @@ package tests.api.java.io; -import java.io.File; -import java.io.FilePermission; -import java.security.PermissionCollection; - import dalvik.annotation.TestLevel; import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestTargetNew; +import tests.util.TestEnvironment; + +import java.io.File; +import java.io.FilePermission; +import java.security.PermissionCollection; @TestTargetClass(FilePermission.class) public class FilePermissionTest extends junit.framework.TestCase { - FilePermission readAllFiles = new FilePermission("<<ALL FILES>>", "read"); - - FilePermission alsoReadAllFiles = new FilePermission("<<ALL FILES>>", - "read"); - - FilePermission allInCurrent = new FilePermission("*", - "read, write, execute,delete"); - - FilePermission readInCurrent = new FilePermission("*", "read"); - - FilePermission readInFile = new FilePermission("aFile.file", "read"); + FilePermission readAllFiles; + FilePermission alsoReadAllFiles; + FilePermission allInCurrent; + FilePermission readInCurrent; + FilePermission readInFile; + + @Override protected void setUp() throws Exception { + super.setUp(); + TestEnvironment.reset(); + + readAllFiles = new FilePermission("<<ALL FILES>>", "read"); + alsoReadAllFiles = new FilePermission("<<ALL FILES>>", "read"); + allInCurrent = new FilePermission("*", "read, write, execute,delete"); + readInCurrent = new FilePermission("*", "read"); + readInFile = new FilePermission("aFile.file", "read"); + } /** * @tests java.io.FilePermission#FilePermission(java.lang.String, @@ -58,9 +64,9 @@ public class FilePermissionTest extends junit.framework.TestCase { "write"); assertEquals("action given to the constructor did not correspond - constructor failed", "write", constructFile.getActions()); - assertTrue( - "name given to the construcotr did not correspond - construcotr failed", - constructFile.getName() == "test constructor"); + assertEquals( + "name given to the constructor did not correspond - constructor failed", + "test constructor", constructFile.getName()); // Regression test for HARMONY-1050 try { @@ -244,18 +250,4 @@ public class FilePermissionTest extends junit.framework.TestCase { readInCurrent.hashCode() != allInCurrent.hashCode()); } - - /** - * Sets up the fixture, for example, open a network connection. This method - * is called before a test is executed. - */ - protected void setUp() { - } - - /** - * Tears down the fixture, for example, close a network connection. This - * method is called after a test is executed. - */ - protected void tearDown() { - } } diff --git a/luni/src/test/java/tests/api/java/io/FileTest.java b/luni/src/test/java/tests/api/java/io/FileTest.java index 3063b89..adcaccd 100644 --- a/luni/src/test/java/tests/api/java/io/FileTest.java +++ b/luni/src/test/java/tests/api/java/io/FileTest.java @@ -39,6 +39,7 @@ import dalvik.annotation.TestTargetNew; import dalvik.annotation.TestTargets; import static tests.support.Support_Exec.javaProcessBuilder; import static tests.support.Support_Exec.execAndGetOutput; +import tests.util.TestEnvironment; @TestTargetClass(File.class) public class FileTest extends junit.framework.TestCase { @@ -97,7 +98,6 @@ public class FileTest extends junit.framework.TestCase { public void test_ConstructorLjava_io_FileLjava_lang_String() throws Exception { String error; String dirName = System.getProperty("java.io.tmpdir"); - String oldUserDir = System.getProperty("user.dir"); System.setProperty("user.dir", dirName); File d = new File(dirName); @@ -127,8 +127,6 @@ public class FileTest extends junit.framework.TestCase { d = new File(s, "/abc"); assertEquals("Test 4: Incorrect file created;", f.getAbsolutePath(), d.getAbsolutePath()); - - System.setProperty("user.dir", oldUserDir); } /** @@ -173,7 +171,6 @@ public class FileTest extends junit.framework.TestCase { String fileName = "input.tst"; String userDir = System.getProperty("java.io.tmpdir"); - String oldUserDir = System.getProperty("user.dir"); System.setProperty("user.dir", userDir); File f = new File(dirName, fileName); @@ -206,8 +203,6 @@ public class FileTest extends junit.framework.TestCase { .getAbsolutePath()); assertEquals("Test3: Created Incorrect File", "/abc", f .getAbsolutePath()); - - System.setProperty("user.dir", oldUserDir); } /** @@ -931,7 +926,6 @@ public class FileTest extends junit.framework.TestCase { String expected; String error; String tmpDir = System.getProperty("java.io.tmpdir"); - String oldUserDir = System.getProperty("user.dir"); System.setProperty("user.dir", tmpDir); try { String base = new File(tmpDir).getCanonicalPath(); @@ -978,8 +972,6 @@ public class FileTest extends junit.framework.TestCase { } catch (IOException e) { fail("Unexpected IOException During Test : " + e.getMessage()); - } finally { - System.setProperty("user.dir", oldUserDir); } } @@ -1094,7 +1086,6 @@ public class FileTest extends junit.framework.TestCase { args = {} ) public void test_getPath() { - String oldUserDir = System.getProperty("java.io.tmpdir"); System.setProperty("user.dir", System.getProperty("java.io.tmpdir")); String base = System.getProperty("user.dir"); String fname; @@ -1120,7 +1111,6 @@ public class FileTest extends junit.framework.TestCase { f2.delete(); f3.delete(); f4.delete(); - System.setProperty("user.dir", oldUserDir); } /** @@ -2503,6 +2493,9 @@ public class FileTest extends junit.framework.TestCase { * is called before a test is executed. */ protected void setUp() throws Exception { + super.setUp(); + TestEnvironment.reset(); + // Make sure that system properties are set correctly String userDir = System.getProperty("java.io.tmpdir"); if (userDir == null) @@ -2534,6 +2527,8 @@ public class FileTest extends junit.framework.TestCase { * method is called after a test is executed. */ protected void tearDown() { + TestEnvironment.reset(); + if (tempFile.exists() && !tempFile.delete()) System.out .println("FileTest.tearDown() failed, could not delete file!"); diff --git a/luni/src/test/java/tests/api/java/net/ExcludedProxyTest.java b/luni/src/test/java/tests/api/java/net/ExcludedProxyTest.java index ae76723..eeb46db 100644 --- a/luni/src/test/java/tests/api/java/net/ExcludedProxyTest.java +++ b/luni/src/test/java/tests/api/java/net/ExcludedProxyTest.java @@ -36,6 +36,7 @@ import java.net.URLConnection; import tests.support.Support_Configuration; import tests.support.resource.Support_Resources; import junit.framework.TestCase; +import tests.util.TestEnvironment; /* * This test is designed for collecting all the testcases which needs a proxy @@ -46,6 +47,12 @@ import junit.framework.TestCase; @TestTargetClass(Proxy.class) public class ExcludedProxyTest extends TestCase { + + @Override protected void setUp() throws Exception { + super.setUp(); + TestEnvironment.reset(); + } + /** * @tests java.net.HttpURLConnection#usingProxy() */ diff --git a/luni/src/test/java/tests/api/java/net/ProxySelectorTest.java b/luni/src/test/java/tests/api/java/net/ProxySelectorTest.java index f208954..ee8195b 100644 --- a/luni/src/test/java/tests/api/java/net/ProxySelectorTest.java +++ b/luni/src/test/java/tests/api/java/net/ProxySelectorTest.java @@ -34,6 +34,7 @@ import java.util.List; import java.util.Properties; import junit.framework.TestCase; +import tests.util.TestEnvironment; @TestTargetClass(ProxySelector.class) public class ProxySelectorTest extends TestCase { @@ -77,11 +78,6 @@ public class ProxySelectorTest extends TestCase { } } - /* - * Original system properties must be restored after running each test case. - */ - private Properties orignalSystemProperties; - /** * @tests java.net.ProxySelector#getDefault() */ @@ -722,16 +718,14 @@ public class ProxySelectorTest extends TestCase { */ protected void setUp() throws Exception { super.setUp(); - // save original system properties - orignalSystemProperties = (Properties) System.getProperties().clone(); + TestEnvironment.reset(); } /* * @see junit.framework.TestCase#tearDown() */ protected void tearDown() throws Exception { - // restore orignal system properties - System.setProperties(orignalSystemProperties); + TestEnvironment.reset(); super.tearDown(); } } diff --git a/luni/src/test/java/tests/api/java/net/ServerSocketTest.java b/luni/src/test/java/tests/api/java/net/ServerSocketTest.java index ecdfd01..a750536 100644 --- a/luni/src/test/java/tests/api/java/net/ServerSocketTest.java +++ b/luni/src/test/java/tests/api/java/net/ServerSocketTest.java @@ -45,6 +45,7 @@ import java.util.Properties; import tests.support.Support_Configuration; import tests.support.Support_PortManager; +import tests.util.TestEnvironment; @TestTargetClass(value = ServerSocket.class) public class ServerSocketTest extends SocketTestCase { @@ -1289,7 +1290,7 @@ public class ServerSocketTest extends SocketTestCase { * method is called after a test is executed. */ protected void tearDown() { - + TestEnvironment.reset(); try { if (s != null) s.close(); diff --git a/luni/src/test/java/tests/api/java/util/FormatterTest.java b/luni/src/test/java/tests/api/java/util/FormatterTest.java index 6f86818..5a29fdc 100644 --- a/luni/src/test/java/tests/api/java/util/FormatterTest.java +++ b/luni/src/test/java/tests/api/java/util/FormatterTest.java @@ -59,6 +59,7 @@ import dalvik.annotation.KnownFailure; import dalvik.annotation.TestLevel; import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestTargetNew; +import tests.util.TestEnvironment; @TestTargetClass(Formatter.class) public class FormatterTest extends TestCase { @@ -1268,8 +1269,6 @@ public class FormatterTest extends TestCase { } catch (IllegalFormatPrecisionException e) { // expected } - - System.setProperty("line.separator", oldSeparator); } /** @@ -4725,6 +4724,7 @@ public class FormatterTest extends TestCase { * Setup resource files for testing */ protected void setUp() throws IOException { + TestEnvironment.reset(); notExist = File.createTempFile("notexist", null); notExist.delete(); @@ -4748,6 +4748,7 @@ public class FormatterTest extends TestCase { * Delete the resource files if they exist */ protected void tearDown() { + TestEnvironment.reset(); if (notExist.exists()) { notExist.delete(); } diff --git a/nio_char/src/test/java/org/apache/harmony/nio_char/tests/java/nio/charset/CharsetTest.java b/nio_char/src/test/java/org/apache/harmony/nio_char/tests/java/nio/charset/CharsetTest.java index 6c36ec2..e7c5f5c 100644 --- a/nio_char/src/test/java/org/apache/harmony/nio_char/tests/java/nio/charset/CharsetTest.java +++ b/nio_char/src/test/java/org/apache/harmony/nio_char/tests/java/nio/charset/CharsetTest.java @@ -20,6 +20,7 @@ import dalvik.annotation.TestLevel; import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestTargetNew; import junit.framework.TestCase; +import tests.util.TestEnvironment; import java.nio.charset.Charset; import java.nio.charset.IllegalCharsetNameException; @@ -40,6 +41,8 @@ public class CharsetTest extends TestCase { * JUnit set-up method */ public void setUp() { + TestEnvironment.reset(); + // Populate the known charset vars Set names = Charset.availableCharsets().keySet(); for (Iterator nameItr = names.iterator(); nameItr.hasNext();) { diff --git a/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/AbstractPreferencesTest.java b/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/AbstractPreferencesTest.java index 76ef4e7..b45a75a 100644 --- a/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/AbstractPreferencesTest.java +++ b/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/AbstractPreferencesTest.java @@ -16,12 +16,12 @@ package org.apache.harmony.prefs.tests.java.util.prefs; -import dalvik.annotation.TestTargets; import dalvik.annotation.TestLevel; -import dalvik.annotation.TestTargetNew; import dalvik.annotation.TestTargetClass; - +import dalvik.annotation.TestTargetNew; +import dalvik.annotation.TestTargets; import junit.framework.TestCase; +import tests.util.TestEnvironment; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -35,13 +35,9 @@ import java.util.prefs.PreferenceChangeEvent; import java.util.prefs.PreferenceChangeListener; import java.util.prefs.Preferences; -import tests.util.PrefsTester; - @TestTargetClass(AbstractPreferences.class) public class AbstractPreferencesTest extends TestCase { - private final PrefsTester prefsTester = new PrefsTester(); - AbstractPreferences pref; static AbstractPreferences root; @@ -55,7 +51,7 @@ public class AbstractPreferencesTest extends TestCase { protected void setUp() throws Exception { super.setUp(); - prefsTester.setUp(); + TestEnvironment.reset(); root = (AbstractPreferences) Preferences.userRoot(); parent = (AbstractPreferences) Preferences.userNodeForPackage(this.getClass()); @@ -64,7 +60,6 @@ public class AbstractPreferencesTest extends TestCase { } protected void tearDown() throws Exception { - prefsTester.tearDown(); super.tearDown(); } diff --git a/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/FilePreferencesImplTest.java b/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/FilePreferencesImplTest.java index 0cb1975..4535a59 100644 --- a/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/FilePreferencesImplTest.java +++ b/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/FilePreferencesImplTest.java @@ -17,33 +17,29 @@ package org.apache.harmony.prefs.tests.java.util.prefs; import dalvik.annotation.AndroidOnly; -import dalvik.annotation.TestTargetClass; -import dalvik.annotation.TestTargets; import dalvik.annotation.TestLevel; +import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestTargetNew; +import dalvik.annotation.TestTargets; +import junit.framework.TestCase; +import tests.util.TestEnvironment; import java.io.FilePermission; import java.security.Permission; import java.util.prefs.BackingStoreException; import java.util.prefs.Preferences; -import junit.framework.TestCase; -import tests.util.PrefsTester; - @TestTargetClass(java.util.prefs.Preferences.class) public class FilePreferencesImplTest extends TestCase { - private final PrefsTester prefsTester = new PrefsTester(); - @Override protected void setUp() throws Exception { super.setUp(); - prefsTester.setUp(); + TestEnvironment.reset(); } @Override protected void tearDown() throws Exception { - prefsTester.tearDown(); super.tearDown(); } diff --git a/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/NodeChangeEventTest.java b/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/NodeChangeEventTest.java index 1afd755..e24026c 100644 --- a/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/NodeChangeEventTest.java +++ b/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/NodeChangeEventTest.java @@ -16,10 +16,12 @@ package org.apache.harmony.prefs.tests.java.util.prefs; -import dalvik.annotation.TestTargetClass; -import dalvik.annotation.TestTargets; import dalvik.annotation.TestLevel; +import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestTargetNew; +import junit.framework.TestCase; +import org.apache.harmony.testframework.serialization.SerializationTest; +import tests.util.TestEnvironment; import java.io.NotSerializableException; import java.util.prefs.AbstractPreferences; @@ -27,28 +29,20 @@ import java.util.prefs.BackingStoreException; import java.util.prefs.NodeChangeEvent; import java.util.prefs.Preferences; -import junit.framework.TestCase; - -import org.apache.harmony.testframework.serialization.SerializationTest; -import tests.util.PrefsTester; - /** * */ @TestTargetClass(NodeChangeEvent.class) public class NodeChangeEventTest extends TestCase { - private final PrefsTester prefsTester = new PrefsTester(); - NodeChangeEvent event; protected void setUp() throws Exception { super.setUp(); - prefsTester.setUp(); + TestEnvironment.reset(); } protected void tearDown() throws Exception { - prefsTester.tearDown(); super.tearDown(); } diff --git a/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/NodeChangeListenerTest.java b/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/NodeChangeListenerTest.java index 3cdb4d9..8e01589 100644 --- a/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/NodeChangeListenerTest.java +++ b/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/NodeChangeListenerTest.java @@ -16,26 +16,22 @@ package org.apache.harmony.prefs.tests.java.util.prefs; -import dalvik.annotation.TestTargetClass; -import dalvik.annotation.TestTargets; import dalvik.annotation.TestLevel; +import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestTargetNew; +import junit.framework.TestCase; +import tests.util.TestEnvironment; import java.util.prefs.NodeChangeEvent; import java.util.prefs.NodeChangeListener; import java.util.prefs.Preferences; -import junit.framework.TestCase; -import tests.util.PrefsTester; - /** * */ @TestTargetClass(NodeChangeListener.class) public class NodeChangeListenerTest extends TestCase { - private final PrefsTester prefsTester = new PrefsTester(); - NodeChangeListener l; /* @@ -44,7 +40,7 @@ public class NodeChangeListenerTest extends TestCase { @Override protected void setUp() throws Exception { super.setUp(); - prefsTester.setUp(); + TestEnvironment.reset(); l = new NodeChangeListenerImpl(); } @@ -53,7 +49,6 @@ public class NodeChangeListenerTest extends TestCase { */ @Override protected void tearDown() throws Exception { - prefsTester.tearDown(); super.tearDown(); } diff --git a/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferenceChangeEventTest.java b/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferenceChangeEventTest.java index aa55457..47e621e 100644 --- a/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferenceChangeEventTest.java +++ b/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferenceChangeEventTest.java @@ -16,37 +16,32 @@ package org.apache.harmony.prefs.tests.java.util.prefs; -import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestLevel; +import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestTargetNew; +import junit.framework.TestCase; +import org.apache.harmony.testframework.serialization.SerializationTest; +import tests.util.TestEnvironment; import java.io.NotSerializableException; import java.util.prefs.AbstractPreferences; import java.util.prefs.PreferenceChangeEvent; import java.util.prefs.Preferences; -import junit.framework.TestCase; - -import org.apache.harmony.testframework.serialization.SerializationTest; -import tests.util.PrefsTester; - /** * */ @TestTargetClass(PreferenceChangeEvent.class) public class PreferenceChangeEventTest extends TestCase { - private final PrefsTester prefsTester = new PrefsTester(); - PreferenceChangeEvent event; @Override protected void setUp() throws Exception { super.setUp(); - prefsTester.setUp(); + TestEnvironment.reset(); } @Override protected void tearDown() throws Exception { - prefsTester.tearDown(); super.tearDown(); } diff --git a/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferencesTest.java b/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferencesTest.java index c9c74fd..122dacd 100644 --- a/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferencesTest.java +++ b/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferencesTest.java @@ -21,11 +21,10 @@ import dalvik.annotation.TestLevel; import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestTargetNew; import dalvik.annotation.TestTargets; - import junit.framework.TestCase; +import tests.util.TestEnvironment; import java.io.ByteArrayInputStream; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -39,16 +38,12 @@ import java.util.prefs.NodeChangeListener; import java.util.prefs.PreferenceChangeListener; import java.util.prefs.Preferences; -import tests.util.PrefsTester; - /** * */ @TestTargetClass(Preferences.class) public class PreferencesTest extends TestCase { - private final PrefsTester prefsTester = new PrefsTester(); - MockSecurityManager manager = new MockSecurityManager(); MockInputStream stream = null; @@ -82,7 +77,7 @@ public class PreferencesTest extends TestCase { "<!DOCTYPE preferences SYSTEM \"http://java.sun.com/dtd/preferences.dtd\"><preferences><root type=\"user\"><map></map></root></preferences>" .getBytes("UTF-8")); stream = new MockInputStream(in); - prefsTester.setUp(); + TestEnvironment.reset(); } /* @@ -91,7 +86,6 @@ public class PreferencesTest extends TestCase { @Override protected void tearDown() throws Exception { stream.close(); - prefsTester.tearDown(); super.tearDown(); } diff --git a/security/src/test/java/org/apache/harmony/security/tests/java/security/PolicyTest.java b/security/src/test/java/org/apache/harmony/security/tests/java/security/PolicyTest.java index cfa2af1..ce57328 100644 --- a/security/src/test/java/org/apache/harmony/security/tests/java/security/PolicyTest.java +++ b/security/src/test/java/org/apache/harmony/security/tests/java/security/PolicyTest.java @@ -31,6 +31,7 @@ import junit.framework.TestCase; import org.apache.harmony.security.tests.support.SecurityChecker; import org.apache.harmony.security.tests.support.TestUtils; +import tests.util.TestEnvironment; import java.io.File; import java.io.FilePermission; @@ -60,6 +61,11 @@ public class PolicyTest extends TestCase { junit.textui.TestRunner.run(PolicyTest.class); } + @Override protected void tearDown() throws Exception { + TestEnvironment.reset(); + super.tearDown(); + } + /** * @tests constructor Policy() */ diff --git a/security/src/test/java/tests/security/permissions/JavaLangSystemTest.java b/security/src/test/java/tests/security/permissions/JavaLangSystemTest.java index d1bf7c2..b9217a1 100644 --- a/security/src/test/java/tests/security/permissions/JavaLangSystemTest.java +++ b/security/src/test/java/tests/security/permissions/JavaLangSystemTest.java @@ -22,6 +22,7 @@ import dalvik.annotation.TestTargetNew; import dalvik.annotation.TestTargetClass; import junit.framework.TestCase; +import tests.util.TestEnvironment; import java.io.InputStream; import java.io.PrintStream; @@ -46,6 +47,7 @@ public class JavaLangSystemTest extends TestCase { @Override protected void tearDown() throws Exception { + TestEnvironment.reset(); System.setSecurityManager(old); super.tearDown(); } diff --git a/sql/src/test/java/org/apache/harmony/sql/tests/java/sql/DriverManagerTest.java b/sql/src/test/java/org/apache/harmony/sql/tests/java/sql/DriverManagerTest.java index ebb055c..d53f078 100644 --- a/sql/src/test/java/org/apache/harmony/sql/tests/java/sql/DriverManagerTest.java +++ b/sql/src/test/java/org/apache/harmony/sql/tests/java/sql/DriverManagerTest.java @@ -39,6 +39,7 @@ import java.util.Properties; import junit.framework.TestCase; import static tests.support.Support_Exec.javaProcessBuilder; import static tests.support.Support_Exec.execAndGetOutput; +import tests.util.TestEnvironment; @TestTargetClass(DriverManager.class) /** @@ -77,6 +78,7 @@ public class DriverManagerTest extends TestCase { // test methods as needed. @Override public void setUp() { + TestEnvironment.reset(); numberLoaded = loadDrivers(); } // end setUp() diff --git a/support/src/test/java/tests/util/PrefsTester.java b/support/src/test/java/tests/util/PrefsTester.java deleted file mode 100644 index 047b357..0000000 --- a/support/src/test/java/tests/util/PrefsTester.java +++ /dev/null @@ -1,73 +0,0 @@ -/* Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package tests.util; - -import java.util.prefs.BackingStoreException; -import java.util.prefs.Preferences; -import java.util.Arrays; - -/** - * Prepares the shared preferences store for a test by wiping preference data - * before and after the test. Sample usage: - * <pre> - * public void MyPreferencesTest extends TestCase { - * private final PrefsTester prefsTester = new PrefsTester(); - * - * public void setUp() throws BackingStoreException { - * super.setUp(); - * prefsTester.setUp(); - * } - * - * public void tearDown() throws BackingStoreException { - * prefsTester.tearDown(); - * super.tearDown(); - * } - * - * ... - * }</pre> - * - * <p>Once the preferences classes have been initialized, the path where their - * data is stored is fixed. For that reason, every test that reads or writes - * preferences must first prepare preferences for testing by using this class. - */ -public final class PrefsTester { - - static { - String tmp = System.getProperty("java.io.tmpdir"); - System.setProperty("user.home", tmp); - System.setProperty("java.home", tmp); - } - - public void setUp() throws BackingStoreException { - clear(); - } - - public void tearDown() throws BackingStoreException { - clear(); - } - - private void clear() throws BackingStoreException { - for (Preferences root : Arrays .asList( - Preferences.systemRoot(), Preferences.userRoot())) { - for (String child : root.childrenNames()) { - root.node(child).removeNode(); - } - root.clear(); - root.flush(); - } - } -} diff --git a/support/src/test/java/tests/util/TestEnvironment.java b/support/src/test/java/tests/util/TestEnvironment.java new file mode 100644 index 0000000..92facda --- /dev/null +++ b/support/src/test/java/tests/util/TestEnvironment.java @@ -0,0 +1,159 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tests.util; + +import java.io.File; +import java.util.Arrays; +import java.util.Properties; +import java.util.prefs.BackingStoreException; +import java.util.prefs.Preferences; + +/** + * Prepares system properties and preferences to hygienic values for testing: + * <ul> + * <li>System properties are set to reasonable defaults. Referenced + * directories such as the user and Java home directories will be + * writable. + * <li>System and user preferences are cleared. Once the preferences classes + * have been initialized, the path where their data is stored is fixed. + * For this reason, every test that reads or writes preferences should + * first reset the system configuration with this API. + * </ul> + * + * <p>Use this class to clean up before and/or after your test. Sample usage: + * <pre> + * public void MyTest extends TestCase { + * + * protected void setUp() throws Exception { + * super.setUp(); + * TestEnvironment().reset(); + * } + * + * protected void tearDown() throws Exception { + * TestEnvironment().reset(); + * super.tearDown(); + * } + * + * ... + * }</pre> + */ +public final class TestEnvironment { + private TestEnvironment() {} + + public static synchronized void reset() { + resetSystemProperties(); + resetPreferences(); + + // TODO: TimeZone?, SecurityManager? + } + + private static void resetSystemProperties() { + String tmpDir = System.getProperty("java.io.tmpdir"); + if (tmpDir == null) { + throw new IllegalStateException("Test execution requires the" + + " system property java.io.tmpdir to be set."); + } + + Properties p = new Properties(); + + // runtime properties that we never want to clobber + copyProperty(p, "android.vm.dexfile"); + copyProperty(p, "java.boot.class.path"); + copyProperty(p, "java.class.path"); + copyProperty(p, "java.io.tmpdir"); + copyProperty(p, "java.library.path"); + copyProperty(p, "os.arch"); + copyProperty(p, "os.name"); + copyProperty(p, "os.version"); + + // paths with writable values for testing + String userHome = tmpDir + "/user.home"; + String javaHome = tmpDir + "/java.home"; + String userDir = tmpDir + "/user.dir"; + makeDirectory(new File(userHome)); + makeDirectory(new File(javaHome)); + makeDirectory(new File(userDir)); + p.put("java.home", javaHome); + p.put("user.dir", userDir); + p.put("user.home", userHome); + + // hardcoded properties + p.put("file.encoding", "UTF-8"); + p.put("file.separator", "/"); + p.put("java.class.version", "46.0"); + p.put("java.compiler", ""); + p.put("java.ext.dirs", ""); + p.put("java.net.preferIPv6Addresses", "true"); + p.put("java.runtime.name", "Android Runtime"); + p.put("java.runtime.version", "0.9"); + p.put("java.specification.name", "Dalvik Core Library"); + p.put("java.specification.vendor", "The Android Project"); + p.put("java.specification.version", "0.9"); + p.put("java.vendor", "The Android Project"); + p.put("java.vendor.url", "http://www.android.com/"); + p.put("java.version", "0"); + p.put("java.vm.name", "Dalvik"); + p.put("java.vm.specification.name", "Dalvik Virtual Machine Specification"); + p.put("java.vm.specification.vendor", "The Android Project"); + p.put("java.vm.specification.version", "0.9"); + p.put("java.vm.vendor", "The Android Project"); + p.put("java.vm.vendor.url", "http://www.android.com/"); + p.put("java.vm.version", "1.2.0"); + p.put("javax.net.ssl.trustStore", "/etc/security/cacerts.bks"); + p.put("line.separator", "\n"); + p.put("path.separator", ":"); + p.put("user.language", "en"); + p.put("user.name", ""); + p.put("user.region", "US"); + + System.setProperties(p); + } + + private static void copyProperty(Properties p, String key) { + p.put(key, System.getProperty(key)); + } + + private static void makeDirectory(File path) { + boolean success; + if (!path.exists()) { + success = path.mkdirs(); + } else if (!path.isDirectory()) { + success = path.delete() && path.mkdirs(); + } else { + success = true; + } + + if (!success) { + throw new RuntimeException("Failed to make directory " + path); + } + } + + private static void resetPreferences() { + try { + for (Preferences root : Arrays.asList( + Preferences.systemRoot(), Preferences.userRoot())) { + for (String child : root.childrenNames()) { + root.node(child).removeNode(); + } + root.clear(); + root.flush(); + } + } catch (BackingStoreException e) { + throw new RuntimeException(e); + } + } +} diff --git a/x-net/src/test/java/tests/api/javax/net/ssl/SSLEngineTest.java b/x-net/src/test/java/tests/api/javax/net/ssl/SSLEngineTest.java index 0e3fee3..f659919 100644 --- a/x-net/src/test/java/tests/api/javax/net/ssl/SSLEngineTest.java +++ b/x-net/src/test/java/tests/api/javax/net/ssl/SSLEngineTest.java @@ -49,6 +49,7 @@ import dalvik.annotation.TestLevel; import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestTargetNew; import dalvik.annotation.TestTargets; +import tests.util.TestEnvironment; /** @@ -65,6 +66,11 @@ public class SSLEngineTest extends TestCase { junit.textui.TestRunner.run(SSLEngineTest.class); } + @Override protected void setUp() throws Exception { + super.setUp(); + TestEnvironment.reset(); + } + /** * Test for <code>SSLEngine()</code> constructor Assertion: creates * SSLEngine object with null host and -1 port diff --git a/xml/src/test/java/tests/api/javax/xml/parsers/DocumentBuilderFactoryTest.java b/xml/src/test/java/tests/api/javax/xml/parsers/DocumentBuilderFactoryTest.java index 1e1ffdd..b6c400f 100644 --- a/xml/src/test/java/tests/api/javax/xml/parsers/DocumentBuilderFactoryTest.java +++ b/xml/src/test/java/tests/api/javax/xml/parsers/DocumentBuilderFactoryTest.java @@ -31,6 +31,7 @@ import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import tests.api.javax.xml.parsers.SAXParserFactoryTest.MyHandler; +import tests.util.TestEnvironment; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -57,6 +58,7 @@ public class DocumentBuilderFactoryTest extends TestCase { protected void setUp() throws Exception { super.setUp(); + TestEnvironment.reset(); dbf = DocumentBuilderFactory.newInstance(); cdataElements = new ArrayList<String>(); diff --git a/xml/src/test/java/tests/api/javax/xml/parsers/SAXParserFactoryTest.java b/xml/src/test/java/tests/api/javax/xml/parsers/SAXParserFactoryTest.java index 5d41356..a918ac2 100644 --- a/xml/src/test/java/tests/api/javax/xml/parsers/SAXParserFactoryTest.java +++ b/xml/src/test/java/tests/api/javax/xml/parsers/SAXParserFactoryTest.java @@ -41,6 +41,7 @@ import dalvik.annotation.TestLevel; import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestTargetNew; import dalvik.annotation.TestTargets; +import tests.util.TestEnvironment; @TestTargetClass(SAXParserFactory.class) public class SAXParserFactoryTest extends TestCase { @@ -66,7 +67,9 @@ public class SAXParserFactoryTest extends TestCase { } public void tearDown() throws Exception { + TestEnvironment.reset(); is1.close(); + super.tearDown(); } @TestTargetNew( @@ -175,13 +178,10 @@ public class SAXParserFactoryTest extends TestCase { args = {} ) public void test_newInstance() { - String className = null; try { SAXParserFactory dtf = SAXParserFactory.newInstance(); assertNotNull("New Instance of DatatypeFactory is null", dtf); - className = System.getProperty("javax.xml.parsers.SAXParserFactory"); - System.setProperty("javax.xml.parsers.SAXParserFactory", "org.apache.harmony.xml.parsers.SAXParserFactoryImpl"); @@ -205,13 +205,6 @@ public class SAXParserFactoryTest extends TestCase { } } catch (IOException ioe) { fail("Unexpected exception " + ioe.toString()); - } finally { - if (className == null) { - System.clearProperty("javax.xml.parsers.SAXParserFactory"); - } else { - System.setProperty("javax.xml.parsers.SAXParserFactory", - className); - } } } diff --git a/xml/src/test/java/tests/api/org/xml/sax/helpers/ParserAdapterTest.java b/xml/src/test/java/tests/api/org/xml/sax/helpers/ParserAdapterTest.java index 27a7e78..608d52c 100644 --- a/xml/src/test/java/tests/api/org/xml/sax/helpers/ParserAdapterTest.java +++ b/xml/src/test/java/tests/api/org/xml/sax/helpers/ParserAdapterTest.java @@ -44,6 +44,7 @@ import dalvik.annotation.TestLevel; import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestTargetNew; import dalvik.annotation.TestTargets; +import tests.util.TestEnvironment; @SuppressWarnings("deprecation") @TestTargetClass(ParserAdapter.class) @@ -79,11 +80,17 @@ public class ParserAdapterTest extends TestCase { @Override public void setUp() { + TestEnvironment.reset(); adapter.setContentHandler(handler); adapter.setDTDHandler(handler); adapter.setErrorHandler(handler); } - + + @Override protected void tearDown() throws Exception { + TestEnvironment.reset(); + super.tearDown(); + } + @TestTargetNew( level = TestLevel.COMPLETE, method = "ParserAdapter", diff --git a/xml/src/test/java/tests/api/org/xml/sax/helpers/ParserFactoryTest.java b/xml/src/test/java/tests/api/org/xml/sax/helpers/ParserFactoryTest.java index dd4b1c3..113a569 100644 --- a/xml/src/test/java/tests/api/org/xml/sax/helpers/ParserFactoryTest.java +++ b/xml/src/test/java/tests/api/org/xml/sax/helpers/ParserFactoryTest.java @@ -23,6 +23,7 @@ import dalvik.annotation.TestTargetNew; import junit.framework.TestCase; import org.xml.sax.helpers.ParserFactory; +import tests.util.TestEnvironment; import java.util.Iterator; import java.util.Properties; @@ -32,6 +33,11 @@ import java.util.Map.Entry; @TestTargetClass(ParserFactory.class) public class ParserFactoryTest extends TestCase { + @Override protected void tearDown() throws Exception { + TestEnvironment.reset(); + super.tearDown(); + } + @TestTargetNew( level = TestLevel.COMPLETE, method = "makeParser", diff --git a/xml/src/test/java/tests/api/org/xml/sax/helpers/XMLReaderAdapterTest.java b/xml/src/test/java/tests/api/org/xml/sax/helpers/XMLReaderAdapterTest.java index 20488c5..0fe6f52 100644 --- a/xml/src/test/java/tests/api/org/xml/sax/helpers/XMLReaderAdapterTest.java +++ b/xml/src/test/java/tests/api/org/xml/sax/helpers/XMLReaderAdapterTest.java @@ -42,6 +42,7 @@ import tests.api.org.xml.sax.support.MockResolver; import dalvik.annotation.TestLevel; import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestTargetNew; +import tests.util.TestEnvironment; @SuppressWarnings("deprecation") @TestTargetClass(XMLReaderAdapter.class) @@ -68,11 +69,17 @@ public class XMLReaderAdapterTest extends TestCase { @Override public void setUp() { + TestEnvironment.reset(); adapter.setDocumentHandler(handler); adapter.setDTDHandler(handler); adapter.setErrorHandler(handler); } - + + @Override protected void tearDown() throws Exception { + TestEnvironment.reset(); + super.tearDown(); + } + @TestTargetNew( level = TestLevel.COMPLETE, method = "XMLReaderAdapter", diff --git a/xml/src/test/java/tests/api/org/xml/sax/helpers/XMLReaderFactoryTest.java b/xml/src/test/java/tests/api/org/xml/sax/helpers/XMLReaderFactoryTest.java index bfb1bd1..2edb918 100644 --- a/xml/src/test/java/tests/api/org/xml/sax/helpers/XMLReaderFactoryTest.java +++ b/xml/src/test/java/tests/api/org/xml/sax/helpers/XMLReaderFactoryTest.java @@ -24,10 +24,21 @@ import junit.framework.TestCase; import org.xml.sax.SAXException; import org.xml.sax.helpers.XMLReaderFactory; +import tests.util.TestEnvironment; @TestTargetClass(XMLReaderFactory.class) public class XMLReaderFactoryTest extends TestCase { + @Override protected void setUp() throws Exception { + TestEnvironment.reset(); + super.setUp(); + } + + @Override protected void tearDown() throws Exception { + TestEnvironment.reset(); + super.tearDown(); + } + @TestTargetNew( level = TestLevel.SUFFICIENT, method = "createXMLReader", |