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