Path: blob/master/test/jdk/java/beans/XMLEncoder/Test5023559.java
41152 views
/*1* Copyright (c) 2007, 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*/2223/*24* @test25* @bug 502355926* @summary Tests encoding of the object with nested target27* @run main/othervm -Djava.security.manager=allow Test502355928* @author Sergey Malenkov29*/3031import java.beans.Encoder;32import java.beans.Expression;33import java.beans.PersistenceDelegate;34import java.beans.XMLEncoder;3536public final class Test5023559 extends AbstractTest {37public static void main(String[] args) {38new Test5023559().test(true);39}4041protected Object getObject() {42return new GrandParent().create().create();43}4445protected void initialize(XMLEncoder encoder) {46encoder.setPersistenceDelegate(Parent.class, new PersistenceDelegate() {47protected Expression instantiate(Object old, Encoder out) {48Parent parent = (Parent) old;49return new Expression(old, parent.getParent(), "create", new Object[] {});50}51});52encoder.setPersistenceDelegate(Child.class, new PersistenceDelegate() {53protected Expression instantiate(Object old, Encoder out) {54Child child = (Child) old;55return new Expression(old, child.getParent(), "create", new Object[] {});56}57});58}5960public static final class GrandParent {61public Parent create() {62return new Parent(this);63}64}6566public static final class Parent {67private final GrandParent parent;6869private Parent(GrandParent parent) {70this.parent = parent;71}7273public GrandParent getParent() {74return this.parent;75}7677public Child create() {78return new Child(this);79}80}8182public static final class Child {83private final Parent parent;8485private Child(Parent parent) {86this.parent = parent;87}8889public Parent getParent() {90return this.parent;91}92}93}949596