1 /*
2  * Copyright (c) 2015, 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 import java.util.HashMap;
25 import java.util.Map;
26 import javax.management.openmbean.ArrayType;
27 import javax.management.openmbean.CompositeData;
28 import javax.management.openmbean.CompositeDataSupport;
29 import javax.management.openmbean.CompositeType;
30 import javax.management.openmbean.OpenDataException;
31 import javax.management.openmbean.OpenType;
32 import javax.management.openmbean.SimpleType;
33 import sun.management.LazyCompositeData;
34 
35 /**
36  * @test
37  * @bug 8139870
38  * @summary sun.management.LazyCompositeData.isTypeMatched() fails for composite types with items of ArrayType
39  * @author Jaroslav Bachorik
40  */
41 
42 public class LazyCompositeDataTest {
43     private final static CompositeData dataV1, dataV2;
44 
45     static {
46         try {
47             // ***
48             // prepare the composite types
49 
50             // composite type stored in an array; V1
51             CompositeType subtypeV1 = new CompositeType(
52                     "Subtype1",
53                     "Version 1",
54                     new String[]{"item1"},
55                     new String[]{"Item 1"},
56                     new OpenType<?>[]{
57                         SimpleType.STRING
58                     }
59             );
60 
61             // composite type stored in an array; V2
62             CompositeType subtypeV2 = new CompositeType(
63                     "Subtype2",
64                     "Version 2",
65                     new String[]{"item1", "item2"},
66                     new String[]{"Item 1", "Item 2"},
67                     new OpenType<?>[]{
68                         SimpleType.STRING,
69                         SimpleType.INTEGER
70                     }
71             );
72 
73 
74             // main composite type; V1
75             // one of the items is array of 'subtypeV1' instances
76             CompositeType typeV1 = new CompositeType(
77                     "MyDataV1",
78                     "Version 1",
79                     new String[]{"item1", "item2"},
80                     new String[]{"Item 1", "Item 2"},
81                     new OpenType<?>[]{
82                         SimpleType.STRING,
83                         ArrayType.getArrayType(subtypeV1)
84                     }
85             );
86 
87             // main composite type; V2
88             // one of the items is array of 'subtypeV2' instances
89             CompositeType typeV2 = new CompositeType(
90                     "MyDataV2",
91                     "Version 2",
92                     new String[]{"item1", "item2"},
93                     new String[]{"Item 1", "Item 2"},
94                     new OpenType<?>[]{
95                         SimpleType.STRING,
96                         ArrayType.getArrayType(subtypeV2)
97                     }
98             );
99             // ***
100 
101             // ***
102             // construct the data
103             Map<String, Object> subitemsV1 = new HashMap<>();
104             Map<String, Object> subitemsV2 = new HashMap<>();
105 
106             Map<String, Object> itemsV1 = new HashMap<>();
107             Map<String, Object> itemsV2 = new HashMap<>();
108 
109             subitemsV1.put("item1", "item1");
110             subitemsV2.put("item1", "item1");
111             subitemsV2.put("item2", 42);
112 
113             itemsV1.put("item1", "item1");
114             itemsV1.put("item2", new CompositeData[]{new CompositeDataSupport(subtypeV1, subitemsV1)});
115 
116             itemsV2.put("item1", "item1");
117             itemsV2.put("item2", new CompositeData[]{new CompositeDataSupport(subtypeV2, subitemsV2)});
118 
119             dataV1 = new CompositeDataSupport(typeV1, itemsV1);
120             dataV2 = new CompositeDataSupport(typeV2, itemsV2);
121             // ***
122         } catch (OpenDataException e) {
123             throw new Error(e);
124         }
125     }
126 
127     private static class MyDataV1 extends LazyCompositeData {
128         @Override
getCompositeData()129         protected CompositeData getCompositeData() {
130             return dataV1;
131         }
132 
isTypeMached(CompositeType type)133         public boolean isTypeMached(CompositeType type) {
134             return isTypeMatched(this.getCompositeType(), type);
135         }
136     }
137 
138     private static class MyDataV2 extends LazyCompositeData {
139         @Override
getCompositeData()140         protected CompositeData getCompositeData() {
141             return dataV2;
142         }
143 
144     }
145 
main(String[] args)146     public static void main(String[] args) throws Exception {
147         System.out.println("Checking LazyCompositeData.isTypeMatched()");
148         MyDataV1 v1 = new MyDataV1();
149         MyDataV2 v2 = new MyDataV2();
150 
151         if (!v1.isTypeMached(v2.getCompositeType())) {
152             System.err.println("=== FAILED");
153             System.err.println("V1 should be matched by V2");
154             System.err.println("\n=== V1");
155             System.err.println(v1.getCompositeType());
156             System.err.println("\n=== V2");
157             System.err.println(v2.getCompositeType());
158             throw new Error();
159         }
160         System.out.println("=== PASSED");
161     }
162 }
163 
164