harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r882634 - /harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/SecureClassLoader2Test.java
Date Fri, 20 Nov 2009 16:28:08 GMT
Author: tellison
Date: Fri Nov 20 16:28:07 2009
New Revision: 882634

URL: http://svn.apache.org/viewvc?rev=882634&view=rev
Log:
Make test more robust by ensuring all bytes are read from input stream, not just those that
are immediately available.
Let any IOException percolate up to test framework for handling.

Modified:
    harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/SecureClassLoader2Test.java

Modified: harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/SecureClassLoader2Test.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/SecureClassLoader2Test.java?rev=882634&r1=882633&r2=882634&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/SecureClassLoader2Test.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/SecureClassLoader2Test.java
Fri Nov 20 16:28:07 2009
@@ -17,6 +17,7 @@
 
 package org.apache.harmony.security.tests.java.security;
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -35,7 +36,7 @@
 	/**
 	 * @tests java.security.SecureClassLoader#getPermissions(java.security.CodeSource)
 	 */
-	public void test_getPermissionsLjava_security_CodeSource() {
+	public void test_getPermissionsLjava_security_CodeSource() throws IOException {
 		class MyClassLoader extends SecureClassLoader {
 			public PermissionCollection getPerms() {
 				return super.getPermissions(new CodeSource(null,
@@ -58,21 +59,49 @@
 		}
 		assertEquals("expected no permissions", 0, count);
 
-		byte[] bytes = null;
-		try {
-			File file = Support_GetLocal.getLocalFile("hyts_security.jar");
-			JarFile jar = new JarFile(file);
-			InputStream in = jar.getInputStream(jar
-					.getEntry("packA/SecurityTest.class"));
-			bytes = new byte[in.available()];
-			in.read(bytes);
-			in.close();
-		} catch (IOException e) {
-			fail("unexpected IOException : " + e);
-		}
-		Class c = myloader.define("packA.SecurityTest", bytes);
+        File file = Support_GetLocal.getLocalFile("hyts_security.jar");
+        JarFile jar = new JarFile(file);
+        InputStream in = jar.getInputStream(jar
+                .getEntry("packA/SecurityTest.class"));
+        byte[] bytes = drain(in);
+        Class c = myloader.define("packA.SecurityTest", bytes);
 		ProtectionDomain pd = c.getProtectionDomain();
 		assertNotNull("Expected dynamic policy", pd.getClassLoader());
 		assertNull("Expected null permissions", pd.getPermissions());
 	}
+
+    /*
+     * Drains the entire content from the given input stream and returns it as a
+     * byte[]. The stream is closed after being drained, or if an IOException
+     * occurs.
+     */
+    private byte[] drain(InputStream is) throws IOException {
+        try {
+            // Initial read
+            byte[] buffer = new byte[1024];
+            int count = is.read(buffer);
+            int nextByte = is.read();
+
+            // Did we get it all in one read?
+            if (nextByte == -1) {
+                byte[] dest = new byte[count];
+                System.arraycopy(buffer, 0, dest, 0, count);
+                return dest;
+            }
+
+            // Requires additional reads
+            ByteArrayOutputStream baos = new ByteArrayOutputStream(count * 2);
+            baos.write(buffer, 0, count);
+            baos.write(nextByte);
+            while (true) {
+                count = is.read(buffer);
+                if (count == -1) {
+                    return baos.toByteArray();
+                }
+                baos.write(buffer, 0, count);
+            }
+        } finally {
+            is.close();
+        }
+    }
 }
\ No newline at end of file



Mime
View raw message