OSDN Git Service

ART: PathClassLoader test
authorAndreas Gampe <agampe@google.com>
Mon, 21 Sep 2015 18:25:10 +0000 (11:25 -0700)
committerAndreas Gampe <agampe@google.com>
Tue, 22 Sep 2015 23:15:07 +0000 (16:15 -0700)
Add a test checking that subclassing PathClassLoader works as
expected.

Change-Id: Ic6bb733b748df5e29b21df021f302ed4237b0f99

test/142-classloader2/expected.txt [new file with mode: 0644]
test/142-classloader2/info.txt [new file with mode: 0644]
test/142-classloader2/smali/MyPathClassLoader.smali [new file with mode: 0644]
test/142-classloader2/src-ex/A.java [new file with mode: 0644]
test/142-classloader2/src/A.java [new file with mode: 0644]
test/142-classloader2/src/Main.java [new file with mode: 0644]

diff --git a/test/142-classloader2/expected.txt b/test/142-classloader2/expected.txt
new file mode 100644 (file)
index 0000000..86f5e22
--- /dev/null
@@ -0,0 +1 @@
+Everything OK.
diff --git a/test/142-classloader2/info.txt b/test/142-classloader2/info.txt
new file mode 100644 (file)
index 0000000..eb821a8
--- /dev/null
@@ -0,0 +1 @@
+Check sub-classing of PathClassLoader.
diff --git a/test/142-classloader2/smali/MyPathClassLoader.smali b/test/142-classloader2/smali/MyPathClassLoader.smali
new file mode 100644 (file)
index 0000000..553abd4
--- /dev/null
@@ -0,0 +1,13 @@
+# Simple subclass of PathClassLoader with methods overridden.
+# We need to use smali right now to subclass a libcore class, see b/24304298.
+
+.class public LMyPathClassLoader;
+
+.super Ldalvik/system/PathClassLoader;
+
+# Simple forwarding constructor.
+.method public constructor <init>(Ljava/lang/String;Ljava/lang/ClassLoader;)V
+    .registers 3
+    invoke-direct {p0, p1, p2}, Ldalvik/system/PathClassLoader;-><init>(Ljava/lang/String;Ljava/lang/ClassLoader;)V
+    return-void
+.end method
diff --git a/test/142-classloader2/src-ex/A.java b/test/142-classloader2/src-ex/A.java
new file mode 100644 (file)
index 0000000..d5fa1f9
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+/**
+ *  Identical class to the main src, except with a different value, so we can distinguish them.
+ */
+public class A {
+    public static String value = "Ex-A";
+}
diff --git a/test/142-classloader2/src/A.java b/test/142-classloader2/src/A.java
new file mode 100644 (file)
index 0000000..532df51
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+/**
+ *  Main class, with a simple value.
+ */
+public class A {
+    public static String value = "Src-A";
+}
diff --git a/test/142-classloader2/src/Main.java b/test/142-classloader2/src/Main.java
new file mode 100644 (file)
index 0000000..86c61eb
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+
+/**
+ * PathClassLoader test.
+ */
+public class Main {
+
+    private static ClassLoader createClassLoader(String dexPath, ClassLoader parent) {
+        try {
+            Class<?> myClassLoaderClass = Class.forName("MyPathClassLoader");
+            Constructor constructor = myClassLoaderClass.getConstructor(String.class,
+                                                                        ClassLoader.class);
+            return (ClassLoader)constructor.newInstance(dexPath, parent);
+        } catch (Exception e) {
+            // Ups, not available?!?!
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Main entry point.
+     */
+    public static void main(String[] args) throws Exception {
+        // Check the class-path for the second file. We'll use that one as the source of the
+        // new classloader.
+        String cp = System.getProperty("java.class.path");
+        if (cp.split(System.getProperty("path.separator")).length != 1) {
+            throw new IllegalStateException("Didn't find exactly one classpath element in " + cp);
+        }
+        if (!cp.endsWith("classloader2.jar")) {
+            throw new IllegalStateException("Don't understand classpath " + cp);
+        }
+        cp = cp.replace("classloader2.jar", "classloader2-ex.jar");
+
+        ClassLoader myClassLoader = createClassLoader(
+                cp, ClassLoader.getSystemClassLoader().getParent());
+
+        // Now load our test class.
+        Class<?> srcClass = A.class;
+        Class<?> exClass = myClassLoader.loadClass("A");
+
+        // First check: classes should be different.
+        if (srcClass == exClass) {
+            throw new IllegalStateException("Loaded class instances are the same");
+        }
+
+        // Secondary checks: get the static field values and make sure they aren't the same.
+        String srcValue = (String)srcClass.getDeclaredField("value").get(null);
+        if (!"Src-A".equals(srcValue)) {
+            throw new IllegalStateException("Expected Src-A, found " + srcValue);
+        }
+        String exValue = (String)exClass.getDeclaredField("value").get(null);
+        if (!"Ex-A".equals(exValue)) {
+            throw new IllegalStateException("Expected Ex-A, found " + exValue);
+        }
+
+        System.out.println("Everything OK.");
+    }
+}