50113
|
1 |
/*
|
|
2 |
* Copyright (c) 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. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package jdk.jfr.event.metadata;
|
|
27 |
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.io.StringReader;
|
|
30 |
import java.util.ArrayList;
|
|
31 |
import java.util.Collection;
|
|
32 |
import java.util.HashMap;
|
|
33 |
import java.util.HashSet;
|
|
34 |
import java.util.List;
|
|
35 |
import java.util.Map;
|
|
36 |
import java.util.Set;
|
|
37 |
|
|
38 |
import javax.xml.parsers.DocumentBuilder;
|
|
39 |
import javax.xml.parsers.DocumentBuilderFactory;
|
|
40 |
import javax.xml.parsers.ParserConfigurationException;
|
|
41 |
|
|
42 |
import jdk.jfr.Configuration;
|
|
43 |
import jdk.jfr.EventType;
|
|
44 |
import jdk.jfr.FlightRecorder;
|
|
45 |
import jdk.jfr.SettingDescriptor;
|
|
46 |
import jdk.test.lib.jfr.EventNames;
|
|
47 |
|
|
48 |
import org.w3c.dom.Attr;
|
|
49 |
import org.w3c.dom.Document;
|
|
50 |
import org.w3c.dom.Element;
|
|
51 |
import org.w3c.dom.Node;
|
|
52 |
import org.w3c.dom.NodeList;
|
|
53 |
import org.xml.sax.InputSource;
|
|
54 |
import org.xml.sax.SAXException;
|
|
55 |
|
51214
|
56 |
/**
|
50113
|
57 |
* @test
|
|
58 |
* @key jfr
|
51214
|
59 |
* @requires vm.hasJFR
|
50113
|
60 |
*
|
|
61 |
* @library /test/lib
|
|
62 |
* @modules java.xml
|
|
63 |
* jdk.jfr
|
|
64 |
*
|
|
65 |
* @run main/othervm jdk.jfr.event.metadata.TestDefaultConfigurations
|
|
66 |
*/
|
|
67 |
public class TestDefaultConfigurations {
|
|
68 |
|
|
69 |
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
|
|
70 |
|
|
71 |
public static void main(String[] args) throws Exception {
|
|
72 |
List<String> errors = new ArrayList<>();
|
|
73 |
|
|
74 |
errors.addAll(testConfiguration(Configuration.getConfiguration("default")));
|
|
75 |
errors.addAll(testConfiguration(Configuration.getConfiguration("profile")));
|
|
76 |
|
|
77 |
if (!errors.isEmpty()) {
|
|
78 |
throwExceptionWithErrors(errors);
|
|
79 |
}
|
|
80 |
}
|
|
81 |
|
|
82 |
private static List<String> testConfiguration(Configuration config) throws ParserConfigurationException, SAXException, IOException {
|
|
83 |
List<String> errors = new ArrayList<>();
|
|
84 |
|
|
85 |
Map<String, EventType> eventTypeLookup = new HashMap<>();
|
|
86 |
for (EventType t : FlightRecorder.getFlightRecorder().getEventTypes()) {
|
|
87 |
eventTypeLookup.put(t.getName(), t);
|
|
88 |
}
|
|
89 |
String content = config.getContents();
|
|
90 |
Document doc = createDocument(content);
|
|
91 |
Element configuration = doc.getDocumentElement();
|
|
92 |
errors.addAll(checkConfiguration(configuration));
|
|
93 |
for (Element event : getChildElements(configuration, "event")) {
|
|
94 |
String name = event.getAttribute("name");
|
|
95 |
|
|
96 |
EventType cd = eventTypeLookup.get(name);
|
|
97 |
if (cd != null) {
|
|
98 |
errors.addAll(checkSettings(config, cd, event));
|
|
99 |
} else {
|
|
100 |
errors.add("Preset '" + config.getName() + "' reference unknown event '" + name + "'");
|
|
101 |
}
|
|
102 |
eventTypeLookup.remove(name);
|
|
103 |
}
|
|
104 |
for (String name : eventTypeLookup.keySet()) {
|
|
105 |
errors.add("Preset '" + config.getName() + "' doesn't configure event '" + name + "'");
|
|
106 |
}
|
|
107 |
|
|
108 |
return errors;
|
|
109 |
}
|
|
110 |
|
|
111 |
private static void throwExceptionWithErrors(List<String> errors) throws Exception {
|
|
112 |
StringBuilder sb = new StringBuilder();
|
|
113 |
for (String error : errors) {
|
|
114 |
sb.append(error);
|
|
115 |
sb.append(LINE_SEPARATOR);
|
|
116 |
}
|
|
117 |
throw new Exception(sb.toString());
|
|
118 |
}
|
|
119 |
|
|
120 |
private static List<String> checkConfiguration(Element configuration) {
|
|
121 |
List<String> errors = new ArrayList<>();
|
|
122 |
if (configuration.getAttribute("description").length() < 2) {
|
|
123 |
errors.add("Configuration should have a valid description!");
|
|
124 |
}
|
|
125 |
if (configuration.getAttribute("label").length() < 2) {
|
|
126 |
errors.add("Configuration should have a label!");
|
|
127 |
}
|
|
128 |
if (!configuration.getAttribute("provider").equals("Oracle")) {
|
|
129 |
errors.add("Configuration should set provider to 'Oracle'!");
|
|
130 |
}
|
|
131 |
return errors;
|
|
132 |
}
|
|
133 |
|
|
134 |
private static List<String> checkSettings(Configuration config, EventType eventType, Element event) {
|
|
135 |
List<String> errors = new ArrayList<>();
|
|
136 |
|
|
137 |
Set<String> requiredSettings = createRequiredSettingNameSet(eventType);
|
|
138 |
for (Element setting : getChildElements(event, "setting")) {
|
|
139 |
String settingName = setting.getAttribute("name");
|
|
140 |
if (requiredSettings.contains(settingName)) {
|
|
141 |
requiredSettings.remove(settingName);
|
|
142 |
} else {
|
|
143 |
errors.add("Setting '" + settingName + "' for event '" + eventType.getName() + "' should not be part of confirguaration '" + config.getName()
|
|
144 |
+ "' since it won't have an impact on the event.");
|
|
145 |
}
|
|
146 |
}
|
|
147 |
for (String required : requiredSettings) {
|
|
148 |
errors.add("Setting '" + required + "' in event '" + eventType.getName() + "' was not configured in the configuration '" + config.getName() + "'");
|
|
149 |
}
|
|
150 |
|
|
151 |
return errors;
|
|
152 |
}
|
|
153 |
|
|
154 |
private static Set<String> createRequiredSettingNameSet(EventType cd) {
|
|
155 |
Set<String> requiredSettings = new HashSet<>();
|
|
156 |
for (SettingDescriptor s : cd.getSettingDescriptors()) {
|
|
157 |
requiredSettings.add(s.getName());
|
|
158 |
}
|
|
159 |
return requiredSettings;
|
|
160 |
}
|
|
161 |
|
|
162 |
private static Document createDocument(String content) throws ParserConfigurationException, SAXException, IOException {
|
|
163 |
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
|
164 |
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
|
165 |
Document doc = dBuilder.parse(new InputSource(new StringReader(content)));
|
|
166 |
doc.getDocumentElement().normalize();
|
|
167 |
// Don't want to add these settings to the jfc-files we ship since they
|
|
168 |
// are not useful to configure. They are however needed to make the test
|
|
169 |
// pass.
|
|
170 |
insertSetting(doc, EventNames.ActiveSetting, "stackTrace", "false");
|
|
171 |
insertSetting(doc, EventNames.ActiveSetting, "threshold", "0 ns");
|
|
172 |
insertSetting(doc, EventNames.ActiveRecording, "stackTrace", "false");
|
|
173 |
insertSetting(doc, EventNames.ActiveRecording, "threshold", "0 ns");
|
|
174 |
insertSetting(doc, EventNames.JavaExceptionThrow, "threshold", "0 ns");
|
|
175 |
insertSetting(doc, EventNames.JavaErrorThrow, "threshold", "0 ns");
|
|
176 |
return doc;
|
|
177 |
}
|
|
178 |
|
|
179 |
private static void insertSetting(Document doc, String eventName, String settingName, String settingValue) {
|
|
180 |
for (Element event : getChildElements(doc.getDocumentElement(), "event")) {
|
|
181 |
Attr attribute = event.getAttributeNode("name");
|
|
182 |
if (attribute != null) {
|
|
183 |
if (eventName.equals(attribute.getValue())) {
|
|
184 |
Element setting = doc.createElement("setting");
|
|
185 |
setting.setAttribute("name", settingName);
|
|
186 |
setting.setTextContent(settingValue);
|
|
187 |
event.appendChild(setting);
|
|
188 |
}
|
|
189 |
}
|
|
190 |
}
|
|
191 |
}
|
|
192 |
|
|
193 |
private static Collection<Element> getChildElements(Element parent, String name) {
|
|
194 |
NodeList elementsByTagName = parent.getElementsByTagName(name);
|
|
195 |
List<Element> elements = new ArrayList<>();
|
|
196 |
for (int i = 0; i < elementsByTagName.getLength(); i++) {
|
|
197 |
Node node = elementsByTagName.item(i);
|
|
198 |
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
|
199 |
elements.add((Element) node);
|
|
200 |
}
|
|
201 |
}
|
|
202 |
return elements;
|
|
203 |
}
|
|
204 |
}
|