1 /*
2  * Copyright (c) 2015, 2018, 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.CompositeData;
27 import javax.management.openmbean.CompositeDataSupport;
28 import javax.management.openmbean.CompositeType;
29 import javax.management.openmbean.OpenType;
30 import javax.management.openmbean.SimpleType;
31 
32 import sun.management.StackTraceElementCompositeData;
33 
34 import org.testng.annotations.*;
35 import static org.testng.Assert.*;
36 
37 /*
38  * @test
39  * @bug     8139587 8212197
40  * @modules java.management/sun.management
41  * @summary Check backward compatibility of StackTraceElementCompositeData
42  * @author  Jaroslav Bachorik
43  *
44  * @run testng CompatibilityTest
45  */
46 
47 public class CompatibilityTest {
48     private static CompositeType compositeTypeV6;
49     private static CompositeType compositeType;
50 
51     // Attribute names
52     private static final String CLASS_LOADER_NAME = "classLoaderName";
53     private static final String MODULE_NAME       = "moduleName";
54     private static final String MODULE_VERSION    = "moduleVersion";
55     private static final String CLASS_NAME        = "className";
56     private static final String METHOD_NAME       = "methodName";
57     private static final String FILE_NAME         = "fileName";
58     private static final String LINE_NUMBER       = "lineNumber";
59     private static final String NATIVE_METHOD     = "nativeMethod";
60 
61     @BeforeClass
setup()62     public static void setup() throws Exception {
63         String[] v6Names = {
64             CLASS_NAME, METHOD_NAME, FILE_NAME, NATIVE_METHOD, LINE_NUMBER
65         };
66         String[] names = {
67             CLASS_LOADER_NAME, MODULE_NAME, MODULE_VERSION,
68             CLASS_NAME, METHOD_NAME, FILE_NAME, NATIVE_METHOD, LINE_NUMBER
69         };
70         compositeTypeV6 = new CompositeType(
71             StackTraceElement.class.getName(),
72             "StackTraceElement",
73             v6Names,
74             v6Names,
75             new OpenType[] {
76                 SimpleType.STRING,
77                 SimpleType.STRING,
78                 SimpleType.STRING,
79                 SimpleType.BOOLEAN,
80                 SimpleType.INTEGER
81             }
82         );
83         compositeType = new CompositeType(
84             StackTraceElement.class.getName(),
85             "StackTraceElement",
86             names,
87             names,
88             new OpenType[] {
89                 SimpleType.STRING,
90                 SimpleType.STRING,
91                 SimpleType.STRING,
92                 SimpleType.STRING,
93                 SimpleType.STRING,
94                 SimpleType.STRING,
95                 SimpleType.BOOLEAN,
96                 SimpleType.INTEGER
97             }
98         );
99     }
100 
makeCompositeDataV6()101     private static CompositeData makeCompositeDataV6() throws Exception {
102         Map<String, Object> itemsV6 = new HashMap<>();
103         itemsV6.put(CLASS_NAME, "MyClass");
104         itemsV6.put(METHOD_NAME, "myMethod");
105         itemsV6.put(FILE_NAME, "MyClass.java");
106         itemsV6.put(NATIVE_METHOD, false);
107         itemsV6.put(LINE_NUMBER, 123);
108 
109         return new CompositeDataSupport(compositeTypeV6, itemsV6);
110     }
111 
makeCompositeData()112     private static CompositeData makeCompositeData() throws Exception {
113         Map<String, Object> items = new HashMap<>();
114         items.put(CLASS_LOADER_NAME, "app");
115         items.put(MODULE_NAME, "m");
116         items.put(MODULE_VERSION, "1.0");
117         items.put(CLASS_NAME, "MyClass");
118         items.put(METHOD_NAME, "myMethod");
119         items.put(FILE_NAME, "MyClass.java");
120         items.put(NATIVE_METHOD, false);
121         items.put(LINE_NUMBER, 123);
122 
123         return new CompositeDataSupport(compositeType, items);
124     }
125 
126     @Test
testV6Compatibility()127     public void testV6Compatibility() throws Exception {
128         StackTraceElement ste = StackTraceElementCompositeData.from(makeCompositeDataV6());
129 
130         assertNotNull(ste);
131         assertEquals(ste.getClassName(), "MyClass");
132         assertEquals(ste.getMethodName(), "myMethod");
133         assertEquals(ste.getFileName(), "MyClass.java");
134         assertEquals(ste.isNativeMethod(), false);
135         assertEquals(ste.getLineNumber(), 123);
136 
137         assertNull(ste.getModuleName());
138         assertNull(ste.getModuleVersion());
139     }
140 
141     @Test
test()142     public void test() throws Exception {
143         StackTraceElement ste = StackTraceElementCompositeData.from(makeCompositeData());
144 
145         assertNotNull(ste);
146 
147         assertEquals(ste.getModuleName(), "m");
148         assertEquals(ste.getModuleVersion(), "1.0");
149         assertEquals(ste.getClassLoaderName(), "app");
150 
151         assertEquals(ste.getClassName(), "MyClass");
152         assertEquals(ste.getMethodName(), "myMethod");
153         assertEquals(ste.getFileName(), "MyClass.java");
154         assertEquals(ste.isNativeMethod(), false);
155         assertEquals(ste.getLineNumber(), 123);
156     }
157 
158     @Test
testCompositeData()159     public void testCompositeData() throws Exception {
160         StackTraceElement ste = new StackTraceElement("app",
161                                                       "m", "1.0",
162                                                       "p.MyClass", "myMethod",
163                                                       "MyClass.java", 123);
164         CompositeData cd = StackTraceElementCompositeData.toCompositeData(ste);
165         StackTraceElement ste1 = StackTraceElementCompositeData.from(cd);
166         assertEquals(ste, ste1);
167     }
168 }
169 
170