summaryrefslogtreecommitdiffstats
path: root/test/042-new-instance
diff options
context:
space:
mode:
authorjeffhao <jeffhao@google.com>2011-09-29 17:41:15 -0700
committerjeffhao <jeffhao@google.com>2011-09-29 17:41:15 -0700
commit5d1ac920fdaef5d4ec8f66bb734488cd9660b024 (patch)
treedd372f306ab70f4c86759869b1f74eca62ff6f2b /test/042-new-instance
parentc31664f3d82e6cd68275a529a8a73f067a52e8be (diff)
downloadart-5d1ac920fdaef5d4ec8f66bb734488cd9660b024.tar.gz
art-5d1ac920fdaef5d4ec8f66bb734488cd9660b024.tar.bz2
art-5d1ac920fdaef5d4ec8f66bb734488cd9660b024.zip
Adding old unit tests to test suite.
These tests are copied straight over. They'll still run, but they're using the old system. Change-Id: If494519e52ddf858a9febfc55bdae830468cb3c8
Diffstat (limited to 'test/042-new-instance')
-rw-r--r--test/042-new-instance/expected.txt8
-rw-r--r--test/042-new-instance/info.txt2
-rw-r--r--test/042-new-instance/src/Main.java156
-rw-r--r--test/042-new-instance/src/MaybeAbstract.java20
-rw-r--r--test/042-new-instance/src/otherpackage/PackageAccess.java6
-rw-r--r--test/042-new-instance/src2/MaybeAbstract.java20
6 files changed, 212 insertions, 0 deletions
diff --git a/test/042-new-instance/expected.txt b/test/042-new-instance/expected.txt
new file mode 100644
index 0000000000..53447db2b6
--- /dev/null
+++ b/test/042-new-instance/expected.txt
@@ -0,0 +1,8 @@
+LocalClass succeeded
+Got expected PackageAccess complaint
+LocalClass3 succeeded
+Got expected InstantationError
+Cons LocalClass failed as expected
+Cons LocalClass2 succeeded
+Cons got expected PackageAccess complaint
+Cons got expected InstantationException
diff --git a/test/042-new-instance/info.txt b/test/042-new-instance/info.txt
new file mode 100644
index 0000000000..49c9e0285a
--- /dev/null
+++ b/test/042-new-instance/info.txt
@@ -0,0 +1,2 @@
+Test various permutations of Class.newInstance and Constructor.newInstance,
+looking for correct handling of access rights and abstract classes.
diff --git a/test/042-new-instance/src/Main.java b/test/042-new-instance/src/Main.java
new file mode 100644
index 0000000000..8faef1349f
--- /dev/null
+++ b/test/042-new-instance/src/Main.java
@@ -0,0 +1,156 @@
+/*
+ * Copyright (C) 2007 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.Constructor;
+
+/**
+ * Test instance creation.
+ */
+public class Main {
+ public static void main(String[] args) {
+ testClassNewInstance();
+ testConstructorNewInstance();
+ }
+
+ /**
+ * Tests Class.newInstance().
+ */
+ static void testClassNewInstance() {
+ // should succeed
+ try {
+ Class c = Class.forName("LocalClass");
+ Object obj = c.newInstance();
+ System.out.println("LocalClass succeeded");
+ } catch (Exception ex) {
+ System.err.println("LocalClass failed");
+ ex.printStackTrace();
+ }
+
+ // should fail
+ try {
+ Class c = Class.forName("otherpackage.PackageAccess");
+ Object obj = c.newInstance();
+ System.err.println("ERROR: PackageAccess succeeded unexpectedly");
+ } catch (IllegalAccessException iae) {
+ System.out.println("Got expected PackageAccess complaint");
+ } catch (Exception ex) {
+ System.err.println("Got unexpected PackageAccess failure");
+ ex.printStackTrace();
+ }
+
+ LocalClass3.main();
+
+ try {
+ MaybeAbstract ma = new MaybeAbstract();
+ System.err.println("ERROR: MaybeAbstract succeeded unexpectedly");
+ } catch (InstantiationError ie) {
+ System.out.println("Got expected InstantationError");
+ } catch (Exception ex) {
+ System.err.println("Got unexpected MaybeAbstract failure");
+ }
+ }
+
+ /**
+ * Tests Constructor.newInstance().
+ */
+ static void testConstructorNewInstance() {
+ // should fail -- getConstructor only returns public constructors
+ try {
+ Class c = Class.forName("LocalClass");
+ Constructor cons = c.getConstructor(new Class[0] /*(Class[])null*/);
+ System.err.println("Cons LocalClass succeeded unexpectedly");
+ } catch (NoSuchMethodException nsme) {
+ System.out.println("Cons LocalClass failed as expected");
+ } catch (Exception ex) {
+ System.err.println("Cons LocalClass failed strangely");
+ ex.printStackTrace();
+ }
+
+ // should succeed
+ try {
+ Class c = Class.forName("LocalClass2");
+ Constructor cons = c.getConstructor((Class[]) null);
+ Object obj = cons.newInstance();
+ System.out.println("Cons LocalClass2 succeeded");
+ } catch (Exception ex) {
+ System.err.println("Cons LocalClass2 failed");
+ ex.printStackTrace();
+ }
+
+ // should fail
+ try {
+ Class c = Class.forName("otherpackage.PackageAccess");
+ Constructor cons = c.getConstructor(new Class[0] /*(Class[])null*/);
+ System.err.println("ERROR: Cons PackageAccess succeeded unexpectedly");
+ } catch (NoSuchMethodException nsme) {
+ System.out.println("Cons got expected PackageAccess complaint");
+ } catch (Exception ex) {
+ System.err.println("Cons got unexpected PackageAccess failure");
+ ex.printStackTrace();
+ }
+
+ // should fail
+ try {
+ Class c = Class.forName("MaybeAbstract");
+ Constructor cons = c.getConstructor(new Class[0] /*(Class[])null*/);
+ Object obj = cons.newInstance();
+ System.err.println("ERROR: Cons MaybeAbstract succeeded unexpectedly");
+ } catch (InstantiationException ie) {
+ // note InstantiationException vs. InstantiationError
+ System.out.println("Cons got expected InstantationException");
+ } catch (Exception ex) {
+ System.err.println("Cons got unexpected MaybeAbstract failure");
+ ex.printStackTrace();
+ }
+ }
+}
+
+class LocalClass {
+ // this class has a default constructor with package visibility
+}
+
+class LocalClass2 {
+ public LocalClass2() {}
+}
+
+
+class LocalClass3 {
+ public static void main() {
+ try {
+ CC.newInstance();
+ System.out.println("LocalClass3 succeeded");
+ } catch (Exception ex) {
+ System.err.println("Got unexpected LocalClass3 failure");
+ ex.printStackTrace();
+ }
+ }
+
+ static class CC {
+ private CC() {}
+
+ static Object newInstance() {
+ try {
+ Class c = CC.class;
+ return c.newInstance();
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ return null;
+ }
+ }
+ }
+}
diff --git a/test/042-new-instance/src/MaybeAbstract.java b/test/042-new-instance/src/MaybeAbstract.java
new file mode 100644
index 0000000000..6d3b05bb3d
--- /dev/null
+++ b/test/042-new-instance/src/MaybeAbstract.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+public /*abstract*/ class MaybeAbstract {
+ public MaybeAbstract() {}
+ int foo() { return 0; }
+}
diff --git a/test/042-new-instance/src/otherpackage/PackageAccess.java b/test/042-new-instance/src/otherpackage/PackageAccess.java
new file mode 100644
index 0000000000..0749d67922
--- /dev/null
+++ b/test/042-new-instance/src/otherpackage/PackageAccess.java
@@ -0,0 +1,6 @@
+package otherpackage;
+
+class PackageAccess {
+ /*package*/ PackageAccess() {
+ }
+}
diff --git a/test/042-new-instance/src2/MaybeAbstract.java b/test/042-new-instance/src2/MaybeAbstract.java
new file mode 100644
index 0000000000..8b70a0712d
--- /dev/null
+++ b/test/042-new-instance/src2/MaybeAbstract.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+public abstract class MaybeAbstract {
+ public MaybeAbstract() {}
+ int foo() { return 0; }
+}