summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--luni/src/test/etc/loading-test-jar/Test2.java45
-rw-r--r--luni/src/test/java/dalvik/system/DexClassLoaderTest.java59
-rw-r--r--luni/src/test/resources/dalvik/system/loading-test.dexbin2204 -> 3104 bytes
-rw-r--r--luni/src/test/resources/dalvik/system/loading-test.jarbin1764 -> 2240 bytes
4 files changed, 76 insertions, 28 deletions
diff --git a/luni/src/test/etc/loading-test-jar/Test2.java b/luni/src/test/etc/loading-test-jar/Test2.java
index 7d26918..8241001 100644
--- a/luni/src/test/etc/loading-test-jar/Test2.java
+++ b/luni/src/test/etc/loading-test-jar/Test2.java
@@ -16,6 +16,10 @@
package test;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
/**
* Class used as part of the class loading tests. This class uses other
* classes that should have come from the same jar/dex file. Each test
@@ -44,29 +48,70 @@ public class Test2 {
}
/**
+ * Stream reader, to avoid pulling in libcore as a dependency.
+ * This is a copy of the same-named method in {@code libcore.base.Streams}.
+ */
+ public static byte[] readFully(InputStream in) throws IOException {
+ byte[] buffer = new byte[1024];
+ ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+ while (true) {
+ int byteCount = in.read(buffer);
+ if (byteCount == -1) {
+ return bytes.toByteArray();
+ }
+ bytes.write(buffer, 0, byteCount);
+ }
+ }
+
+ /**
* Test that an instance of a sibling class can be constructed.
*/
public static void test_constructor() {
new Target();
}
+ /**
+ * Test calling a static method on a sibling class.
+ */
public static void test_callStaticMethod() {
assertSame("blort", Target.blort());
}
+ /**
+ * Test getting a static variable of a sibling class.
+ */
public static void test_getStaticVariable() {
Target.setStaticVariable(22);
assertSame(22, Target.staticVariable);
}
+ /**
+ * Test calling an instance method on a sibling class.
+ */
public static void test_callInstanceMethod() {
Target target = new Target();
assertSame("zorch", target.zorch());
}
+ /**
+ * Test getting an instance variable of a sibling class.
+ */
public static void test_getInstanceVariable() {
Target target = new Target();
target.setInstanceVariable(10098);
assertSame(10098, target.instanceVariable);
}
+
+ /**
+ * Test getting a resource which should be in the same jar
+ * file as this class.
+ */
+ public static void test_getResourceAsStream() throws IOException {
+ ClassLoader cl = Test2.class.getClassLoader();
+ InputStream in = cl.getResourceAsStream("test/Resource1.txt");
+ byte[] contents = readFully(in);
+ String s = new String(contents, "UTF-8");
+
+ assertSame("Muffins are tasty!\n", s.intern());
+ }
}
diff --git a/luni/src/test/java/dalvik/system/DexClassLoaderTest.java b/luni/src/test/java/dalvik/system/DexClassLoaderTest.java
index 6db1d80..e75a661 100644
--- a/luni/src/test/java/dalvik/system/DexClassLoaderTest.java
+++ b/luni/src/test/java/dalvik/system/DexClassLoaderTest.java
@@ -105,7 +105,7 @@ public class DexClassLoaderTest extends TestCase {
}
/**
- * Check that a class in the jar file may be used successfully. In this
+ * Check that a class in the jar/dex file may be used successfully. In this
* case, a trivial static method is called.
*/
public static void test_simpleUse(boolean useDex) throws Exception {
@@ -115,20 +115,6 @@ public class DexClassLoaderTest extends TestCase {
assertSame("blort", result);
}
- /**
- * Check that a resource in the jar file is retrievable and contains
- * the expected contents.
- */
- public static void test_getResourceAsStream(boolean useDex)
- throws Exception {
- DexClassLoader dcl = createInstance(useDex);
- InputStream in = dcl.getResourceAsStream("test/Resource1.txt");
- byte[] contents = Streams.readFully(in);
- String s = new String(contents, "UTF-8");
-
- assertEquals("Muffins are tasty!\n", s);
- }
-
/*
* All the following tests are just pass-throughs to test code
* that lives inside the loading-test dex/jar file.
@@ -159,9 +145,8 @@ public class DexClassLoaderTest extends TestCase {
}
/*
- * The rest of the file consists of the actual test methods, which
- * are all mostly just calls to the parametrically-defined tests
- * above.
+ * These methods are all essentially just calls to the
+ * parametrically-defined tests above.
*/
public void test_jar_init() throws Exception {
@@ -172,10 +157,6 @@ public class DexClassLoaderTest extends TestCase {
test_simpleUse(false);
}
- public void test_jar_getResourceAsStream() throws Exception {
- test_getResourceAsStream(false);
- }
-
public void test_jar_callStaticMethod() throws Exception {
test_callStaticMethod(false);
}
@@ -200,12 +181,6 @@ public class DexClassLoaderTest extends TestCase {
test_simpleUse(true);
}
- /*
- * Note: No getResourceAsStream() test, since the dex file doesn't
- * have any resources.
- */
- // public void test_dex_getResourceAsStream()
-
public void test_dex_callStaticMethod() throws Exception {
test_callStaticMethod(true);
}
@@ -221,4 +196,32 @@ public class DexClassLoaderTest extends TestCase {
public void test_dex_getInstanceVariable() throws Exception {
test_getInstanceVariable(true);
}
+
+ /*
+ * Tests specifically for resource-related functionality. Since
+ * raw dex files don't contain resources, these test only work
+ * with jar files.
+ */
+
+ /**
+ * Check that a resource in the jar file is retrievable and contains
+ * the expected contents.
+ */
+ public void test_directGetResourceAsStream() throws Exception {
+ DexClassLoader dcl = createInstance(false);
+ InputStream in = dcl.getResourceAsStream("test/Resource1.txt");
+ byte[] contents = Streams.readFully(in);
+ String s = new String(contents, "UTF-8");
+
+ assertEquals("Muffins are tasty!\n", s);
+ }
+
+ /**
+ * Check that a resource in the jar file can be retrieved from
+ * a class within that jar file.
+ */
+ public void test_getResourceAsStream() throws Exception {
+ createInstanceAndCallStaticMethod(
+ false, "test.Test2", "test_getResourceAsStream");
+ }
}
diff --git a/luni/src/test/resources/dalvik/system/loading-test.dex b/luni/src/test/resources/dalvik/system/loading-test.dex
index fee2995..abc1742 100644
--- a/luni/src/test/resources/dalvik/system/loading-test.dex
+++ b/luni/src/test/resources/dalvik/system/loading-test.dex
Binary files differ
diff --git a/luni/src/test/resources/dalvik/system/loading-test.jar b/luni/src/test/resources/dalvik/system/loading-test.jar
index f1b28fa..8bbb6e1 100644
--- a/luni/src/test/resources/dalvik/system/loading-test.jar
+++ b/luni/src/test/resources/dalvik/system/loading-test.jar
Binary files differ