1/*
2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 5023550
27 * @summary Tests complex references to owner
28 * @author Sergey Malenkov
29 */
30
31import java.beans.DefaultPersistenceDelegate;
32import java.beans.Encoder;
33import java.beans.Expression;
34import java.beans.XMLDecoder;
35import java.beans.XMLEncoder;
36
37public class Test5023550 extends AbstractTest {
38    public static void main(String[] args) {
39        new Test5023550().test(true);
40    }
41
42    private final Owner owner = new Owner();
43
44    @Override
45    protected void initialize(XMLEncoder encoder) {
46        encoder.setOwner(this.owner);
47        encoder.setPersistenceDelegate(A.class, new ADelegate());
48        encoder.setPersistenceDelegate(B.class, new BDelegate());
49        encoder.setPersistenceDelegate(C.class, new CDelegate());
50    }
51
52    @Override
53    protected void initialize(XMLDecoder decoder) {
54        decoder.setOwner(this.owner);
55    }
56
57    protected Object getObject() {
58        return this.owner.newA(this.owner.newB().newC());
59    }
60
61    public static class Owner {
62        public A newA(C c) {
63            return new A(c);
64        }
65
66        public B newB() {
67            return new B();
68        }
69    }
70
71    public static class A {
72        private final C c;
73
74        private A(C c) {
75            this.c = c;
76        }
77
78        public C getC() {
79            return this.c;
80        }
81    }
82
83    public static class B {
84        public C newC() {
85            return new C(this);
86        }
87    }
88
89    public static class C {
90        private final B b;
91
92        private C(B b) {
93            this.b = b;
94        }
95
96        public B getB() {
97            return this.b;
98        }
99    }
100
101    public static class ADelegate extends DefaultPersistenceDelegate {
102        protected Expression instantiate(Object old, Encoder out) {
103            XMLEncoder encoder = (XMLEncoder) out;
104            A a = (A) old;
105            return new Expression(old, encoder.getOwner(), "newA", new Object[] { a.getC() });
106        }
107    }
108
109    public static class BDelegate extends DefaultPersistenceDelegate {
110        protected Expression instantiate(Object old, Encoder out) {
111            XMLEncoder encoder = (XMLEncoder) out;
112            return new Expression(old, encoder.getOwner(), "newB", new Object[0]);
113        }
114    }
115
116    public static class CDelegate extends DefaultPersistenceDelegate {
117        protected Expression instantiate(Object old, Encoder out) {
118            C c = (C) old;
119            return new Expression(c, c.getB(), "newC", new Object[0]);
120        }
121    }
122}
123