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 6921644
27 * @summary Tests references to cached integer
28 * @author Sergey Malenkov
29 */
30
31import java.beans.ConstructorProperties;
32import java.util.ArrayList;
33import java.util.List;
34
35public final class Test6921644 extends AbstractTest {
36    public static void main(String[] args) {
37        new Test6921644().test(true);
38    }
39
40    protected Object getObject() {
41        Owner<Author> o = new Owner<Author>(100); // it works if ID >= 128
42
43        Category c = new Category(o);
44
45        Document d1 = new Document(o);
46        Document d2 = new Document(o);
47        Document d3 = new Document(o);
48
49        Author a1 = new Author(o);
50        Author a2 = new Author(o);
51        Author a3 = new Author(o);
52
53        o.getList().add(a1);
54        o.getList().add(a2);
55        o.getList().add(a3);
56
57        a3.setRef(o.getId());
58
59        d2.setCategory(c);
60        d3.setCategory(c);
61
62        a1.addDocument(d1);
63        a1.addDocument(d2);
64        a3.addDocument(d3);
65
66        c.addDocument(d2);
67        c.addDocument(d3);
68
69        return o;
70    }
71
72    public static class Owner<T> {
73        private int id;
74        private List<T> list = new ArrayList<T>();
75
76        @ConstructorProperties("id")
77        public Owner(int id) {
78            this.id = id;
79        }
80
81        public int getId() {
82            return this.id;
83        }
84
85        public List<T> getList() {
86            return this.list;
87        }
88
89        public void setList(List<T> list) {
90            this.list = list;
91        }
92    }
93
94    public static class Author {
95        private int id;
96        private int ref;
97        private Owner owner;
98        private List<Document> list = new ArrayList<Document>();
99
100        @ConstructorProperties("owner")
101        public Author(Owner<Author> owner) {
102            this.owner = owner;
103            this.id = owner.getId();
104        }
105
106        public Owner getOwner() {
107            return this.owner;
108        }
109
110        public Integer getId() {
111            return this.id;
112        }
113
114        public void setId(Integer id) {
115            this.id = id;
116        }
117
118        public Integer getRef() {
119            return this.ref;
120        }
121
122        public void setRef(Integer ref) {
123            this.ref = ref;
124        }
125
126        public List<Document> getList() {
127            return this.list;
128        }
129
130        public void setList(List<Document> list) {
131            this.list = list;
132        }
133
134        public void addDocument(Document document) {
135            this.list.add(document);
136            document.setAuthor(this);
137        }
138    }
139
140    public static class Category {
141        private Owner owner;
142        private List<Document> list = new ArrayList<Document>();
143
144        @ConstructorProperties("owner")
145        public Category(Owner owner) {
146            this.owner = owner;
147        }
148
149        public Owner getOwner() {
150            return this.owner;
151        }
152
153        public List<Document> getList() {
154            return this.list;
155        }
156
157        public void setList(List<Document> list) {
158            this.list = list;
159        }
160
161        public void addDocument(Document document) {
162            this.list.add(document);
163            document.setCategory(this);
164        }
165    }
166
167    public static class Document {
168        private Owner owner;
169        private Author author;
170        private Category category;
171
172        @ConstructorProperties("owner")
173        public Document(Owner owner) {
174            this.owner = owner;
175        }
176
177        public Owner getOwner() {
178            return this.owner;
179        }
180
181        public Author getAuthor() {
182            return this.author;
183        }
184
185        public void setAuthor(Author author) {
186            this.author = author;
187        }
188
189        public Category getCategory() {
190            return this.category;
191        }
192
193        public void setCategory(Category category) {
194            this.category = category;
195        }
196    }
197}
198