summaryrefslogtreecommitdiffstats
path: root/harmony-tests
diff options
context:
space:
mode:
authorNeil Fuller <nfuller@google.com>2014-04-04 14:57:11 +0100
committerNeil Fuller <nfuller@google.com>2014-04-07 10:11:01 +0100
commit63291dd98b3df687d87c7a95b8813e1cd75f3715 (patch)
treedb57ca2fc3839b7c87fda3bb018fd28576fe4cf5 /harmony-tests
parentdf945d726e02034dafdcf7469f71865b86dc3e9d (diff)
downloadlibcore-63291dd98b3df687d87c7a95b8813e1cd75f3715.zip
libcore-63291dd98b3df687d87c7a95b8813e1cd75f3715.tar.gz
libcore-63291dd98b3df687d87c7a95b8813e1cd75f3715.tar.bz2
Fix for ClassLoaderTest under CTS
The tests could not work given the arrangement of classloaders in Android apps. This also fixes a known failure case in ClassLoaderTest which was due to missing resources. Also updated ClassTest. Change-Id: I114aa9b1a177911aed0aef87819d93a260f0b8d1 Bug: 12491655
Diffstat (limited to 'harmony-tests')
-rw-r--r--harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/ClassLoaderTest.java155
-rw-r--r--harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/ClassTest.java46
2 files changed, 140 insertions, 61 deletions
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/ClassLoaderTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/ClassLoaderTest.java
index 1dba613..2caab39 100644
--- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/ClassLoaderTest.java
+++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/ClassLoaderTest.java
@@ -18,43 +18,24 @@
package org.apache.harmony.tests.java.lang;
import junit.framework.TestCase;
+
+import java.io.IOException;
import java.io.InputStream;
public class ClassLoaderTest extends TestCase {
- /**
- * java.lang.ClassLoader#getResource(java.lang.String)
- */
- public void test_getResourceLjava_lang_String() {
- // Test for method java.net.URL
- // java.lang.ClassLoader.getResource(java.lang.String)
- java.net.URL u = ClassLoader.getSystemClassLoader().getResource("hyts_Foo.c");
- assertNotNull("Unable to find resource", u);
- java.io.InputStream is = null;
- try {
- is = u.openStream();
- assertNotNull("Resource returned is invalid", is);
- is.close();
- } catch (java.io.IOException e) {
- fail("IOException getting stream for resource : " + e.getMessage());
- }
- }
- /**
- * java.lang.ClassLoader#getResourceAsStream(java.lang.String)
- */
- public void test_getResourceAsStreamLjava_lang_String() {
- // Test for method java.io.InputStream
- // java.lang.ClassLoader.getResourceAsStream(java.lang.String)
- // Need better test...
+ /** A resource known to be present in the boot classpath. */
+ private static final String BOOT_RESOURCE_NAME = "java/util/logging/logging.properties";
- java.io.InputStream is = null;
- assertNotNull("Failed to find resource: hyts_Foo.c", (is = ClassLoader
- .getSystemClassLoader().getResourceAsStream("hyts_Foo.c")));
- try {
- is.close();
- } catch (java.io.IOException e) {
- fail("Exception during getResourceAsStream: " + e.toString());
- }
+ /** A resource known to be present in the classpath associated with the test class. */
+ private static final String TEST_RESOURCE_NAME = ClassTest.RESOURCE_ABS_NAME;
+
+ private ClassLoader testClassLoader;
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ testClassLoader = getClass().getClassLoader();
}
/**
@@ -64,13 +45,18 @@ public class ClassLoaderTest extends TestCase {
// Test for method java.lang.ClassLoader
// java.lang.ClassLoader.getSystemClassLoader()
ClassLoader cl = ClassLoader.getSystemClassLoader();
- java.io.InputStream is = cl.getResourceAsStream("hyts_Foo.c");
- assertNotNull("Failed to find resource from system classpath", is);
- try {
- is.close();
- } catch (java.io.IOException e) {
- }
+ assertNotNull(cl);
+
+ // The SystemClassLoader's parent should be the Boot classloader, which is used to load
+ // the various libcore classes.
+ assertNotNull(cl.getParent());
+ Class<?> libcoreClass = Integer.class;
+ assertSame(cl.getParent(), libcoreClass.getClassLoader());
+ // It is difficult to test further because the CTS tests run as an instrumented TestCase.
+ // Android apps do not have a system classpath, and rely on an application classloader to
+ // load app classes and resources, not the System ClassLoader. The System ClassLoader is not
+ // usually the parent of the application class loader.
}
/**
@@ -79,17 +65,94 @@ public class ClassLoaderTest extends TestCase {
public void test_getSystemResourceLjava_lang_String() {
// Test for method java.net.URL
// java.lang.ClassLoader.getSystemResource(java.lang.String)
- // Need better test...
- assertNotNull("Failed to find resource: hyts_Foo.c", ClassLoader
- .getSystemResource("hyts_Foo.c"));
+
+ // It is difficult to test this because the CTS tests run as an instrumented TestCase.
+ // Android apps do not have a system classpath, and rely on an application classloader to
+ // load app classes and resources, not the System ClassLoader.
}
+ /**
+ * java.lang.ClassLoader#getResource(java.lang.String)
+ */
+ public void test_testClassLoader_getResourceLjava_lang_String() {
+ // Test for method java.net.URL
+ // java.lang.ClassLoader.getResource(java.lang.String)
+
+ // Test basic class loader behavior for the ClassLoader that was used to load the test
+ // class while being deliberately vague about which classloader it actually is.
+
+ ClassLoader parentClassLoader = testClassLoader.getParent();
+ assertNull(parentClassLoader.getResource(TEST_RESOURCE_NAME));
+ assertGetResourceIsValid(parentClassLoader, BOOT_RESOURCE_NAME);
+
+ assertGetResourceIsValid(testClassLoader, TEST_RESOURCE_NAME);
+ assertGetResourceIsValid(testClassLoader, BOOT_RESOURCE_NAME);
+ }
+
+ /**
+ * java.lang.ClassLoader#getResourceAsStream(java.lang.String)
+ */
+ public void test_testClassLoader_getResourceAsStreamLjava_lang_String() throws Exception {
+ // Test for method java.io.InputStream
+ // java.lang.ClassLoader.getResourceAsStream(java.lang.String)
+
+ // Test basic class loader behavior for the ClassLoader that was used to load the test
+ // class while being deliberately vague about which classloader it actually is.
+
+ ClassLoader parentClassLoader = testClassLoader.getParent();
+ assertGetResourceAsStreamNotNull(parentClassLoader, BOOT_RESOURCE_NAME);
+ assertNull(parentClassLoader.getResourceAsStream(TEST_RESOURCE_NAME));
+
+ assertGetResourceAsStreamNotNull(testClassLoader, BOOT_RESOURCE_NAME);
+ assertGetResourceAsStreamNotNull(testClassLoader, TEST_RESOURCE_NAME);
+ }
+
+ public void test_testClassLoader_loadClass() throws Exception {
+ // Test basic class loader behavior for the ClassLoader that was used to load the test
+ // class while being deliberately vague about which classloader it actually is.
+ String integerClassName = Integer.class.getName();
+ String testClassName = ClassLoaderTest.class.getName();
+
+ ClassLoader parentClassLoader = testClassLoader.getParent();
+ assertSame(Integer.class, parentClassLoader.loadClass(integerClassName));
+ try {
+ parentClassLoader.loadClass(testClassName);
+ fail();
+ } catch (ClassNotFoundException expected) {
+ }
+
+ assertSame(Integer.class, testClassLoader.loadClass(integerClassName));
+ assertSame(this.getClass(), testClassLoader.loadClass(testClassName));
+ }
//Regression Test for JIRA-2047
- public void test_getResourceAsStream_withSharpChar() throws Exception {
- InputStream in = this.getClass().getClassLoader().getResourceAsStream(
- ClassTest.FILENAME);
- assertNotNull(in);
- in.close();
+ public void test_testClassLoader_getResourceAsStream_withSharpChar() throws Exception {
+ assertGetResourceAsStreamNotNull(testClassLoader, ClassTest.SHARP_RESOURCE_ABS_NAME);
+ }
+
+ private static void assertGetResourceAsStreamNotNull(ClassLoader classLoader,
+ String resourceName) throws IOException {
+ InputStream is = null;
+ try {
+ is = classLoader.getResourceAsStream(resourceName);
+ assertNotNull(is);
+ } finally {
+ if (is != null) {
+ is.close();
+ }
+ }
+ }
+
+ private static void assertGetResourceIsValid(ClassLoader classLoader, String resourceName) {
+ java.net.URL u = classLoader.getResource(resourceName);
+ assertNotNull(u);
+ InputStream is = null;
+ try {
+ is = u.openStream();
+ assertNotNull(is);
+ is.close();
+ } catch (IOException e) {
+ fail("IOException getting stream for resource : " + e.getMessage());
+ }
}
} \ No newline at end of file
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/ClassTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/ClassTest.java
index 4d540c4..379dad2 100644
--- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/ClassTest.java
+++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/ClassTest.java
@@ -18,6 +18,7 @@
package org.apache.harmony.tests.java.lang;
import java.io.FileInputStream;
+import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.lang.reflect.Constructor;
@@ -37,17 +38,16 @@ import java.util.Vector;
public class ClassTest extends junit.framework.TestCase {
- public static final String FILENAME = ClassTest.class.getPackage().getName().replace('.', '/') + "/test#.properties";
+ // Relative resource paths.
+ private static final String SHARP_RESOURCE_RELATIVE_NAME = "test#.properties";
+ private static final String RESOURCE_RELATIVE_NAME = "test.properties";
- static class StaticMember$Class {
- class Member2$A {
- }
- }
-
- class Member$Class {
- class Member3$B {
- }
- }
+ // Absolute resource paths.
+ private static final String ABS_PATH =
+ ClassTest.class.getPackage().getName().replace('.', '/');
+ public static final String SHARP_RESOURCE_ABS_NAME =
+ ABS_PATH + "/" + SHARP_RESOURCE_RELATIVE_NAME;
+ public static final String RESOURCE_ABS_NAME = ABS_PATH + "/" + RESOURCE_RELATIVE_NAME;
public static class TestClass {
@SuppressWarnings("unused")
@@ -565,15 +565,31 @@ public class ClassTest extends junit.framework.TestCase {
// Regression Test for JIRA-2047
public void test_getResourceAsStream_withSharpChar() throws Exception {
- InputStream in = getClass().getResourceAsStream("/" + FILENAME);
+ // Class.getResourceAsStream() requires a leading "/" for absolute paths.
+ assertNull(getClass().getResourceAsStream(SHARP_RESOURCE_ABS_NAME));
+ assertResourceExists("/" + SHARP_RESOURCE_ABS_NAME);
+ assertResourceExists(SHARP_RESOURCE_RELATIVE_NAME);
+
+
+ InputStream in =
+ this.getClass().getClassLoader().getResourceAsStream(SHARP_RESOURCE_ABS_NAME);
assertNotNull(in);
in.close();
+ }
+
+ public void test_getResourceAsStream() throws Exception {
+ // Class.getResourceAsStream() requires a leading "/" for absolute paths.
+ assertNull(getClass().getResourceAsStream(RESOURCE_ABS_NAME));
+ assertResourceExists("/" + RESOURCE_ABS_NAME);
+ assertResourceExists(RESOURCE_RELATIVE_NAME);
- in = getClass().getResourceAsStream(FILENAME);
- assertNull(in);
+ InputStream in = this.getClass().getClassLoader().getResourceAsStream(RESOURCE_ABS_NAME);
+ assertNotNull(in);
+ in.close();
+ }
- in = this.getClass().getClassLoader().getResourceAsStream(
- FILENAME);
+ private void assertResourceExists(String resourceName) throws IOException {
+ InputStream in = getClass().getResourceAsStream(resourceName);
assertNotNull(in);
in.close();
}