1 /*
2  * Copyright (c) 2013, 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.runtime;
27 
28 import java.util.HashMap;
29 import java.util.Map;
30 
31 import jdk.jfr.Recording;
32 import jdk.jfr.consumer.RecordedEvent;
33 import jdk.test.lib.Asserts;
34 import jdk.test.lib.jfr.EventNames;
35 import jdk.test.lib.jfr.Events;
36 
37 /**
38  * @test
39  * @key jfr
40  * @requires vm.hasJFR
41  * @library /test/lib
42  * @run main/othervm jdk.jfr.event.runtime.TestSystemPropertyEvent
43  */
44 public class TestSystemPropertyEvent {
45 
46     private final static String EVENT_NAME = EventNames.InitialSystemProperty;
47 
main(String[] args)48     public static void main(String[] args) throws Throwable {
49         Map<String, String> systemProps = createInitialSystemProperties();
50         // Recording should only contain properties defined at JVM start.
51         // This property should not be included.
52         System.setProperty("party.pooper", "buh!");
53 
54         Recording recording = new Recording();
55         recording.enable(EVENT_NAME);
56         recording.start();
57         recording.stop();
58 
59         Map<String, String> eventProps = new HashMap<>();
60         for (RecordedEvent event : Events.fromRecording(recording)) {
61             String key = Events.assertField(event, "key").notEmpty().getValue();
62             String value = Events.assertField(event, "value").notNull().getValue();
63             if (!eventProps.containsKey(key)) {
64                 // Event is received at both start and end of chunk. Only log first.
65                 System.out.println("Event:" + event);
66             }
67             eventProps.put(key, value);
68         }
69         Asserts.assertGreaterThan(eventProps.size(), 4, "Should have at least 5 events");
70 
71         // Value of System.properties may change. We can not expect all values in
72         // events and System.getProperties() to be equal.
73         // To do some verification of property values we require that at least one
74         // property with non-empty value is equal.
75         int countEqualAndNonEmpty = 0;
76 
77         String missingKeys = "";
78         for (String key : eventProps.keySet()) {
79             if (!systemProps.containsKey(key)) {
80                 missingKeys += key + " ";
81                 continue;
82             }
83             if (isEqualAndNonEmpty(key, eventProps.get(key), systemProps.get(key))) {
84                 countEqualAndNonEmpty++;
85             }
86         }
87 
88         if (!missingKeys.isEmpty()) {
89             Asserts.fail("Event properties not found in System.properties(): " + missingKeys);
90         }
91         Asserts.assertTrue(countEqualAndNonEmpty > 0, "No property had expected value");
92     }
93 
isEqualAndNonEmpty(String key, String eventValue, String systemValue)94     private static boolean isEqualAndNonEmpty(String key, String eventValue, String systemValue) {
95         if (eventValue == null || systemValue == null || eventValue.isEmpty() || systemValue.isEmpty()) {
96             return false;
97         }
98         boolean isEquals = eventValue.equals(systemValue);
99         System.out.printf("eq=%b, key='%s', event='%s', system='%s'%n", isEquals, key, eventValue, systemValue);
100         return isEquals;
101     }
102 
createInitialSystemProperties()103     private static Map<String, String> createInitialSystemProperties() {
104         Map<String, String> result = new HashMap<>();
105         for (Object keyObject : System.getProperties().keySet()) {
106             String key = (String) keyObject;
107             result.put(key, System.getProperty(key));
108             System.out.println("initialProp: " + key);
109         }
110         return result;
111     }
112 }
113