Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/reflect/ReflectionFactory/ReflectionFactoryTest.java
38838 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.ByteArrayInputStream;24import java.io.ByteArrayOutputStream;25import java.io.Externalizable;26import java.io.IOException;27import java.io.ObjectInput;28import java.io.ObjectInputStream;29import java.io.ObjectOutput;30import java.io.ObjectOutputStream;31import java.io.OptionalDataException;32import java.io.Serializable;33import java.lang.invoke.MethodHandle;34import java.lang.reflect.Constructor;35import java.lang.reflect.InvocationTargetException;3637import sun.reflect.ReflectionFactory;3839import org.testng.Assert;40import org.testng.annotations.BeforeClass;41import org.testng.annotations.Test;42import org.testng.annotations.DataProvider;43import org.testng.TestNG;4445/*46* @test47* @bug 8137058 8164908 816898048* @run testng ReflectionFactoryTest49* @run testng/othervm/policy=security.policy ReflectionFactoryTest50* @summary Basic test for the unsupported ReflectionFactory51*/5253public class ReflectionFactoryTest {5455// Initialized by init()56static ReflectionFactory factory;5758@DataProvider(name = "ClassConstructors")59static Object[][] classConstructors() {60return new Object[][] {61{Object.class},62{Foo.class},63{Bar.class},64};65}6667@BeforeClass68static void init() {69factory = ReflectionFactory.getReflectionFactory();70}7172/**73* Test that the correct Constructor is selected and run.74* @param type type of object to create75* @throws NoSuchMethodException - error76* @throws InstantiationException - error77* @throws IllegalAccessException - error78* @throws InvocationTargetException - error79*/80@Test(dataProvider="ClassConstructors")81static void testConstructor(Class<?> type)82throws NoSuchMethodException, InstantiationException,83IllegalAccessException, InvocationTargetException84{85@SuppressWarnings("unchecked")86Constructor<?> c = factory.newConstructorForSerialization(type);8788Object o = c.newInstance();89Assert.assertEquals(o.getClass(), type, "Instance is wrong type");90if (o instanceof Foo) {91Foo foo = (Foo)o;92foo.check();93}94}9596@DataProvider(name = "NonSerialConstructors")97static Object[][] constructors() throws NoSuchMethodException {98return new Object[][] {99{Foo.class, Object.class.getDeclaredConstructor()},100{Foo.class, Foo.class.getDeclaredConstructor()},101{Baz.class, Object.class.getDeclaredConstructor()},102{Baz.class, Foo.class.getDeclaredConstructor()},103{Baz.class, Baz.class.getDeclaredConstructor()}104};105}106107/**108* Tests that the given Constructor, in the hierarchy, is run.109*/110@Test(dataProvider="NonSerialConstructors")111static void testNonSerializableConstructor(Class<?> cl,112Constructor<?> constructorToCall)113throws ReflectiveOperationException114{115@SuppressWarnings("unchecked")116Constructor<?> c = factory.newConstructorForSerialization(cl,117constructorToCall);118119Object o = c.newInstance();120Assert.assertEquals(o.getClass(), cl, "Instance is wrong type");121122int expectedFoo = 0;123int expectedBaz = 0;124if (constructorToCall.getName().equals("ReflectionFactoryTest$Foo")) {125expectedFoo = 1;126} else if (constructorToCall.getName().equals("ReflectionFactoryTest$Baz")) {127expectedFoo = 1;128expectedBaz = 4;129}130131Assert.assertEquals(((Foo)o).foo(), expectedFoo);132if (o instanceof Baz) {133Assert.assertEquals(((Baz)o).baz(), expectedBaz);134}135}136137static class Foo {138private int foo;139public Foo() {140this.foo = 1;141}142143public String toString() {144return "foo: " + foo;145}146147public void check() {148int expectedFoo = 1;149Assert.assertEquals(foo, expectedFoo, "foo() constructor not run");150}151152public int foo() { return foo; }153}154155static class Bar extends Foo implements Serializable {156private static final long serialVersionUID = 3L;157158private int bar;159public Bar() {160this.bar = 1;161}162163public String toString() {164return super.toString() + ", bar: " + bar;165}166167public void check() {168super.check();169int expectedBar = 0;170Assert.assertEquals(bar, expectedBar, "bar() constructor not run");171}172}173174static class Baz extends Foo {175private static final long serialVersionUID = 4L;176177private final int baz;178public Baz() { this.baz = 4; }179public int baz() { return baz; }180}181182/**183* Test newConstructorForExternalization returns the constructor and it can be called.184* @throws NoSuchMethodException - error185* @throws InstantiationException - error186* @throws IllegalAccessException - error187* @throws InvocationTargetException - error188*/189@Test190static void newConstructorForExternalization()191throws NoSuchMethodException, InstantiationException,192IllegalAccessException, InvocationTargetException {193Constructor<?> cons = factory.newConstructorForExternalization(Ext.class);194Ext ext = (Ext)cons.newInstance();195Assert.assertEquals(ext.ext, 1, "Constructor not run");196}197198static class Ext implements Externalizable {199private static final long serialVersionUID = 1L;200201int ext;202203public Ext() {204ext = 1;205}206207@Override208public void writeExternal(ObjectOutput out) throws IOException {}209210@Override211public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {}212}213214@Test215static void testReadWriteObjectForSerialization() throws Throwable {216MethodHandle readObjectMethod = factory.readObjectForSerialization(Ser.class);217Assert.assertNotNull(readObjectMethod, "readObjectMethod not found");218219MethodHandle readObjectNoDataMethod = factory.readObjectNoDataForSerialization(Ser.class);220Assert.assertNotNull(readObjectNoDataMethod, "readObjectNoDataMethod not found");221222MethodHandle writeObjectMethod = factory.writeObjectForSerialization(Ser.class);223Assert.assertNotNull(writeObjectMethod, "writeObjectMethod not found");224225MethodHandle readResolveMethod = factory.readResolveForSerialization(Ser.class);226Assert.assertNotNull(readResolveMethod, "readResolveMethod not found");227228MethodHandle writeReplaceMethod = factory.writeReplaceForSerialization(Ser.class);229Assert.assertNotNull(writeReplaceMethod, "writeReplaceMethod not found");230231byte[] data = null;232try (ByteArrayOutputStream baos = new ByteArrayOutputStream();233ObjectOutputStream oos = new ObjectOutputStream(baos)) {234Ser ser = new Ser();235236writeReplaceMethod.invoke(ser);237Assert.assertTrue(ser.writeReplaceCalled, "writeReplace not called");238Assert.assertFalse(ser.writeObjectCalled, "writeObject should not have been called");239240writeObjectMethod.invoke(ser, oos);241Assert.assertTrue(ser.writeReplaceCalled, "writeReplace should have been called");242Assert.assertTrue(ser.writeObjectCalled, "writeObject not called");243oos.flush();244data = baos.toByteArray();245}246247try (ByteArrayInputStream bais = new ByteArrayInputStream(data);248ObjectInputStream ois = new ObjectInputStream(bais)) {249Ser ser2 = new Ser();250251readObjectMethod.invoke(ser2, ois);252Assert.assertTrue(ser2.readObjectCalled, "readObject not called");253Assert.assertFalse(ser2.readObjectNoDataCalled, "readObjectNoData should not be called");254Assert.assertFalse(ser2.readResolveCalled, "readResolve should not be called");255256readObjectNoDataMethod.invoke(ser2, ois);257Assert.assertTrue(ser2.readObjectCalled, "readObject should have been called");258Assert.assertTrue(ser2.readObjectNoDataCalled, "readObjectNoData not called");259Assert.assertFalse(ser2.readResolveCalled, "readResolve should not be called");260261readResolveMethod.invoke(ser2);262Assert.assertTrue(ser2.readObjectCalled, "readObject should have been called");263Assert.assertTrue(ser2.readObjectNoDataCalled, "readObjectNoData not called");264Assert.assertTrue(ser2.readResolveCalled, "readResolve not called");265}266}267268@Test269static void hasStaticInitializer() {270boolean actual = factory.hasStaticInitializerForSerialization(Ser.class);271Assert.assertTrue(actual, "hasStaticInitializerForSerialization is wrong");272}273274static class Ser implements Serializable {275private static final long serialVersionUID = 2L;276static {277// Define a static class initialization method278}279280boolean readObjectCalled = false;281boolean readObjectNoDataCalled = false;282boolean writeObjectCalled = false;283boolean readResolveCalled = false;284boolean writeReplaceCalled = false;285286public Ser() {}287288private void readObject(ObjectInputStream ois) throws IOException {289Assert.assertFalse(writeObjectCalled, "readObject called too many times");290readObjectCalled = ois.readBoolean();291}292293private void readObjectNoData(ObjectInputStream ois) throws IOException {294Assert.assertFalse(readObjectNoDataCalled, "readObjectNoData called too many times");295readObjectNoDataCalled = true;296}297298private void writeObject(ObjectOutputStream oos) throws IOException {299Assert.assertFalse(writeObjectCalled, "writeObject called too many times");300writeObjectCalled = true;301oos.writeBoolean(writeObjectCalled);302}303304private Object writeReplace() {305Assert.assertFalse(writeReplaceCalled, "writeReplace called too many times");306writeReplaceCalled = true;307return this;308}309310private Object readResolve() {311Assert.assertFalse(readResolveCalled, "readResolve called too many times");312readResolveCalled = true;313return this;314}315}316317/**318* Test the constructor of OptionalDataExceptions.319*/320@Test321static void newOptionalDataException() {322OptionalDataException ode = factory.newOptionalDataExceptionForSerialization(true);323Assert.assertTrue(ode.eof, "eof wrong");324ode = factory.newOptionalDataExceptionForSerialization(false);325Assert.assertFalse(ode.eof, "eof wrong");326327}328329330331// Main can be used to run the tests from the command line with only testng.jar.332@SuppressWarnings("raw_types")333@Test(enabled = false)334public static void main(String[] args) {335Class<?>[] testclass = {ReflectionFactoryTest.class};336TestNG testng = new TestNG();337testng.setTestClasses(testclass);338testng.run();339}340}341342343