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 
  56 /*
  57  * @test
  58  * @key jfr
  59  *
  60  * @library /test/lib
  61  * @modules java.xml
  62  *          jdk.jfr
  63  *
  64  * @run main/othervm jdk.jfr.event.metadata.TestDefaultConfigurations
  65  */
  66 public class TestDefaultConfigurations {
  67 
  68     private static final String LINE_SEPARATOR = System.getProperty("line.separator");
  69 
  70     public static void main(String[] args) throws Exception {
  71         List<String> errors = new ArrayList<>();
  72 
  73         errors.addAll(testConfiguration(Configuration.getConfiguration("default")));
  74         errors.addAll(testConfiguration(Configuration.getConfiguration("profile")));
  75 
  76         if (!errors.isEmpty()) {
  77             throwExceptionWithErrors(errors);
  78         }
  79     }
  80 
  81     private static List<String> testConfiguration(Configuration config) throws ParserConfigurationException, SAXException, IOException {
  82         List<String> errors = new ArrayList<>();
  83 
  84         Map<String, EventType> eventTypeLookup = new HashMap<>();
  85         for (EventType t : FlightRecorder.getFlightRecorder().getEventTypes()) {
  86             eventTypeLookup.put(t.getName(), t);
  87         }
  88         String content = config.getContents();
  89         Document doc = createDocument(content);
  90         Element configuration = doc.getDocumentElement();
  91         errors.addAll(checkConfiguration(configuration));
  92         for (Element event : getChildElements(configuration, "event")) {
  93             String name = event.getAttribute("name");
  94 
  95             EventType cd = eventTypeLookup.get(name);
  96             if (cd != null) {
  97                 errors.addAll(checkSettings(config, cd, event));
  98             } else {
  99                 errors.add("Preset '" + config.getName() + "' reference unknown event '" + name + "'");
 100             }
 101             eventTypeLookup.remove(name);
 102         }
 103         for (String name : eventTypeLookup.keySet()) {
 104             errors.add("Preset '" + config.getName() + "' doesn't configure event '" + name + "'");
 105         }
 106 
 107         return errors;
 108     }
 109 
 110     private static void throwExceptionWithErrors(List<String> errors) throws Exception {
 111         StringBuilder sb = new StringBuilder();
 112         for (String error : errors) {
 113             sb.append(error);
 114             sb.append(LINE_SEPARATOR);
 115         }
 116         throw new Exception(sb.toString());
 117     }
 118 
 119     private static List<String> checkConfiguration(Element configuration) {
 120         List<String> errors = new ArrayList<>();
 121         if (configuration.getAttribute("description").length() < 2) {
 122             errors.add("Configuration should have a valid description!");
 123         }
 124         if (configuration.getAttribute("label").length() < 2) {
 125             errors.add("Configuration should have a label!");
 126         }
 127         if (!configuration.getAttribute("provider").equals("Oracle")) {
 128             errors.add("Configuration should set provider to 'Oracle'!");
 129         }
 130         return errors;
 131     }
 132 
 133     private static List<String> checkSettings(Configuration config, EventType eventType, Element event) {
 134         List<String> errors = new ArrayList<>();
 135 
 136         Set<String> requiredSettings = createRequiredSettingNameSet(eventType);
 137         for (Element setting : getChildElements(event, "setting")) {
 138             String settingName = setting.getAttribute("name");
 139             if (requiredSettings.contains(settingName)) {
 140                 requiredSettings.remove(settingName);
 141             } else {
 142                 errors.add("Setting '" + settingName + "' for event '" + eventType.getName() + "' should not be part of confirguaration '" + config.getName()
 143                         + "' since it won't have an impact on the event.");
 144             }
 145         }
 146         for (String required : requiredSettings) {
 147             errors.add("Setting '" + required + "' in event '" + eventType.getName() + "' was not configured in the configuration '" + config.getName() + "'");
 148         }
 149 
 150         return errors;
 151     }
 152 
 153     private static Set<String> createRequiredSettingNameSet(EventType cd) {
 154         Set<String> requiredSettings = new HashSet<>();
 155         for (SettingDescriptor s : cd.getSettingDescriptors()) {
 156             requiredSettings.add(s.getName());
 157         }
 158         return requiredSettings;
 159     }
 160 
 161     private static Document createDocument(String content) throws ParserConfigurationException, SAXException, IOException {
 162         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
 163         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
 164         Document doc = dBuilder.parse(new InputSource(new StringReader(content)));
 165         doc.getDocumentElement().normalize();
 166         // Don't want to add these settings to the jfc-files we ship since they
 167         // are not useful to configure. They are however needed to make the test
 168         // pass.
 169         insertSetting(doc, EventNames.ActiveSetting, "stackTrace", "false");
 170         insertSetting(doc, EventNames.ActiveSetting, "threshold", "0 ns");
 171         insertSetting(doc, EventNames.ActiveRecording, "stackTrace", "false");
 172         insertSetting(doc, EventNames.ActiveRecording, "threshold", "0 ns");
 173         insertSetting(doc, EventNames.JavaExceptionThrow, "threshold", "0 ns");
 174         insertSetting(doc, EventNames.JavaErrorThrow, "threshold", "0 ns");
 175         return doc;
 176     }
 177 
 178     private static void insertSetting(Document doc, String eventName, String settingName, String settingValue) {
 179         for (Element event : getChildElements(doc.getDocumentElement(), "event")) {
 180             Attr attribute = event.getAttributeNode("name");
 181             if (attribute != null) {
 182                 if (eventName.equals(attribute.getValue())) {
 183                     Element setting = doc.createElement("setting");
 184                     setting.setAttribute("name", settingName);
 185                     setting.setTextContent(settingValue);
 186                     event.appendChild(setting);
 187                 }
 188             }
 189         }
 190     }
 191 
 192     private static Collection<Element> getChildElements(Element parent, String name) {
 193         NodeList elementsByTagName = parent.getElementsByTagName(name);
 194         List<Element> elements = new ArrayList<>();
 195         for (int i = 0; i < elementsByTagName.getLength(); i++) {
 196             Node node = elementsByTagName.item(i);
 197             if (node.getNodeType() == Node.ELEMENT_NODE) {
 198                 elements.add((Element) node);
 199             }
 200         }
 201         return elements;
 202     }
 203 }