1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * 
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * The contents of this file are subject to the terms of either the Universal Permissive License
   7  * v 1.0 as shown at http://oss.oracle.com/licenses/upl
   8  *
   9  * or the following license:
  10  *
  11  * Redistribution and use in source and binary forms, with or without modification, are permitted
  12  * provided that the following conditions are met:
  13  * 
  14  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
  15  * and the following disclaimer.
  16  * 
  17  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  18  * conditions and the following disclaimer in the documentation and/or other materials provided with
  19  * the distribution.
  20  * 
  21  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
  22  * endorse or promote products derived from this software without specific prior written permission.
  23  * 
  24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  26  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  31  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32  */
  33 package org.openjdk.jmc.rjmx.test.subscription;
  34 
  35 import static org.junit.Assert.assertEquals;
  36 import static org.junit.Assert.assertNotNull;
  37 import static org.junit.Assert.assertTrue;
  38 
  39 import java.lang.management.MemoryUsage;
  40 import java.util.List;
  41 import java.util.Map;
  42 
  43 import org.junit.Assume;
  44 import org.junit.Test;
  45 
  46 import org.openjdk.jmc.rjmx.IConnectionHandle;
  47 import org.openjdk.jmc.rjmx.IServerHandle;
  48 import org.openjdk.jmc.rjmx.subscription.IMRIMetadata;
  49 import org.openjdk.jmc.rjmx.subscription.IMRIMetadataProvider;
  50 import org.openjdk.jmc.rjmx.subscription.IMRIMetadataService;
  51 import org.openjdk.jmc.rjmx.subscription.IMRIService;
  52 import org.openjdk.jmc.rjmx.subscription.MRI;
  53 import org.openjdk.jmc.rjmx.subscription.MRIMetadataToolkit;
  54 import org.openjdk.jmc.rjmx.subscription.MRI.Type;
  55 import org.openjdk.jmc.rjmx.test.LocalRJMXTestToolkit;
  56 import org.openjdk.jmc.rjmx.test.RjmxTestCase;
  57 
  58 /**
  59  * Tests the basic functionality of the {@link IMRIMetadataService}.
  60  */
  61 public class MRIMetadataServiceTest extends RjmxTestCase {
  62         private static final String MOOH = "Mooh!"; //$NON-NLS-1$
  63         private static final String MY_COW = "MyCow"; //$NON-NLS-1$
  64 
  65         @Test
  66         public void testGetAttributeInfo() throws Exception {
  67                 @SuppressWarnings("nls")
  68                 IConnectionHandle handle = IServerHandle.create(LocalRJMXTestToolkit.createDefaultDescriptor()).connect("Test");
  69                 IMRIMetadataService service = LocalRJMXTestToolkit.getInfoService(handle);
  70                 IMRIMetadata info = service.getMetadata(new MRI(Type.ATTRIBUTE, "java.lang:type=OperatingSystem", //$NON-NLS-1$
  71                                 "SystemCpuLoad")); //$NON-NLS-1$
  72                 assertNotNull(info);
  73                 assertNotNull(info.getMRI());
  74                 assertNotNull(info.getValueType());
  75                 assertNotNull(info.getDescription());
  76                 handle.close();
  77         }
  78 
  79         @Test
  80         public void testGetMetadata() throws Exception {
  81                 IConnectionHandle handle = IServerHandle.create(LocalRJMXTestToolkit.createDefaultDescriptor()).connect("Test"); //$NON-NLS-1$
  82                 IMRIMetadataService service = LocalRJMXTestToolkit.getInfoService(handle);
  83                 IMRIMetadata info = service.getMetadata(new MRI(Type.ATTRIBUTE, "java.lang:type=OperatingSystem", //$NON-NLS-1$
  84                                 "SystemCpuLoad")); //$NON-NLS-1$
  85                 assertNotNull(info);
  86 
  87                 String description = info.getDescription();
  88                 assertNotNull(description);
  89                 assertTrue(description.length() > 12);
  90                 handle.close();
  91         }
  92 
  93         @Test
  94         public void testGetExtendedProperties() throws Exception {
  95                 IConnectionHandle handle = IServerHandle.create(LocalRJMXTestToolkit.createDefaultDescriptor()).connect("Test"); //$NON-NLS-1$
  96                 IMRIMetadataService service = LocalRJMXTestToolkit.getInfoService(handle);
  97                 IMRIMetadata info = service
  98                                 .getMetadata(new MRI(Type.ATTRIBUTE, "java.lang:type=Memory", "HeapMemoryUsage/used")); //$NON-NLS-1$ //$NON-NLS-2$
  99                 assertNotNull(info);
 100                 assertTrue("Should be numerical", MRIMetadataToolkit.isNumerical(info)); //$NON-NLS-1$
 101                 assertNotNull(info.getMetadata("color")); //$NON-NLS-1$
 102         }
 103 
 104         @Test
 105         public void testGetNumericalMetadata() throws Exception {
 106                 IConnectionHandle handle = IServerHandle.create(LocalRJMXTestToolkit.createDefaultDescriptor()).connect("Test"); //$NON-NLS-1$
 107                 IMRIMetadataService service = LocalRJMXTestToolkit.getInfoService(handle);
 108                 evaluateNumericalMetadata(service, new MRI(Type.ATTRIBUTE, "java.lang:type=Memory", "HeapMemoryUsage"), false); //$NON-NLS-1$ //$NON-NLS-2$
 109                 evaluateNumericalMetadata(service,
 110                                 new MRI(Type.ATTRIBUTE, "java.lang:type=Memory", "HeapMemoryUsage/committed"), true); //$NON-NLS-1$ //$NON-NLS-2$
 111                 handle.close();
 112         }
 113 
 114         public void evaluateNumericalMetadata(IMRIMetadataService service, MRI mri, boolean isNumerical) {
 115                 IMRIMetadata info = service.getMetadata(mri);
 116                 assertNotNull(info);
 117                 assertTrue(isNumerical == MRIMetadataToolkit.isNumerical(info));
 118         }
 119 
 120         @Test
 121         public void testAttributeTypes() throws Exception {
 122                 IConnectionHandle handle = IServerHandle.create(LocalRJMXTestToolkit.createDefaultDescriptor()).connect("Test"); //$NON-NLS-1$
 123                 IMRIMetadataService service = LocalRJMXTestToolkit.getInfoService(handle);
 124                 evaluateAttributeType(service, new MRI(Type.ATTRIBUTE, "java.lang:type=Memory", "HeapMemoryUsage"), //$NON-NLS-1$ //$NON-NLS-2$
 125                                 MemoryUsage.class);
 126                 evaluateAttributeType(service, new MRI(Type.ATTRIBUTE, "java.lang:type=Memory", "HeapMemoryUsage/committed"), //$NON-NLS-1$ //$NON-NLS-2$
 127                                 Long.TYPE);
 128                 evaluateAttributeType(service, new MRI(Type.ATTRIBUTE, "JMImplementation:type=MBeanServerDelegate", //$NON-NLS-1$
 129                                 "ImplementationVendor"), String.class); //$NON-NLS-1$
 130                 evaluateAttributeType(service, new MRI(Type.ATTRIBUTE, "java.lang:type=Runtime", "InputArguments"), //$NON-NLS-1$ //$NON-NLS-2$
 131                                 List.class);
 132                 evaluateAttributeType(service, new MRI(Type.ATTRIBUTE, "java.lang:type=Runtime", "SystemProperties"), //$NON-NLS-1$ //$NON-NLS-2$
 133                                 Map.class);
 134                 handle.close();
 135         }
 136 
 137         private void evaluateAttributeType(IMRIMetadataService service, MRI mri, Class<?> clazz) {
 138                 IMRIMetadata info = service.getMetadata(mri);
 139                 assertNotNull(info);
 140                 String typeName = stripGenericType(info.getValueType());
 141                 if (clazz.isPrimitive()) {
 142                         assertTrue("Not assignable!", clazz.getName().equals(typeName)); //$NON-NLS-1$
 143                 } else {
 144                         try {
 145                                 assertTrue("Not assignable!", clazz.isAssignableFrom(Class.forName(typeName))); //$NON-NLS-1$
 146                         } catch (ClassNotFoundException e) {
 147                                 assertTrue("Could not instantiate metadata type " + typeName, false); //$NON-NLS-1$
 148                         }
 149                 }
 150         }
 151 
 152         private String stripGenericType(String className) {
 153                 int start = className.indexOf('<');
 154                 if (start >= 0) {
 155                         assertTrue(className.charAt(className.length() - 1) == '>');
 156                         return className.substring(0, start);
 157                 }
 158                 return className;
 159         }
 160 
 161         @Test
 162         public void testSetMetadata() throws Exception {
 163                 IConnectionHandle handle = IServerHandle.create(LocalRJMXTestToolkit.createDefaultDescriptor()).connect("Test"); //$NON-NLS-1$
 164                 IMRIMetadataService service = LocalRJMXTestToolkit.getInfoService(handle);
 165                 IMRIMetadata info = service.getMetadata(new MRI(Type.ATTRIBUTE, "java.lang:type=OperatingSystem", //$NON-NLS-1$
 166                                 "SystemCpuLoad")); //$NON-NLS-1$
 167                 assertNotNull(info);
 168                 service.setMetadata(info.getMRI(), "testgegga", "Oh, testgegga!"); //$NON-NLS-1$ //$NON-NLS-2$
 169                 String testGegga = (String) service.getMetadata(info.getMRI(), "testgegga"); //$NON-NLS-1$
 170                 assertEquals("Oh, testgegga!", testGegga); //$NON-NLS-1$
 171                 handle.close();
 172         }
 173 
 174         @Test
 175         public void testSetMetadataInDifferentConnections() throws Exception {
 176                 Assume.assumeTrue("Will not pass until BUG XYZ is fixed", false); //$NON-NLS-1$
 177 
 178                 IConnectionHandle handle1 = IServerHandle.create(LocalRJMXTestToolkit.createDefaultDescriptor())
 179                                 .connect("Test"); //$NON-NLS-1$
 180                 IConnectionHandle handle2 = IServerHandle.create(LocalRJMXTestToolkit.createAlternativeDescriptor())
 181                                 .connect("Test"); //$NON-NLS-1$
 182                 IMRIMetadataService service1 = LocalRJMXTestToolkit.getInfoService(handle1);
 183                 IMRIMetadataService service2 = LocalRJMXTestToolkit.getInfoService(handle2);
 184                 MRI mri = new MRI(Type.ATTRIBUTE, "java.lang:type=OperatingSystem", "SystemCpuLoad"); //$NON-NLS-1$ //$NON-NLS-2$
 185                 service1.setMetadata(mri, MY_COW, MOOH);
 186                 assertNotNull(service1.getMetadata(mri, MY_COW));
 187                 assertEquals(MOOH, service1.getMetadata(mri, MY_COW));
 188                 assertNull(service2.getMetadata(mri).getMetadata(MY_COW));
 189                 handle1.close();
 190                 handle2.close();
 191         }
 192 
 193         @Test
 194         public void testOverrideDefultMetadata() throws Exception {
 195                 IConnectionHandle handle = IServerHandle.create(LocalRJMXTestToolkit.createDefaultDescriptor()).connect("Test"); //$NON-NLS-1$
 196                 IMRIMetadataService service = LocalRJMXTestToolkit.getInfoService(handle);
 197                 MRI mri = new MRI(Type.ATTRIBUTE, "java.lang:type=Memory", //$NON-NLS-1$
 198                                 "HeapMemoryUsage/committed"); //$NON-NLS-1$
 199                 IMRIMetadata info = service.getMetadata(mri);
 200                 String description = info.getDescription();
 201                 String newDescription = "[ja]" + description; //$NON-NLS-1$
 202                 service.setMetadata(mri, IMRIMetadataProvider.KEY_DESCRIPTION, newDescription);
 203                 assertEquals("Description not updated", newDescription, info.getDescription()); //$NON-NLS-1$
 204                 handle.close();
 205         }
 206 
 207         @Test
 208         public void testCompositeIsChild() throws Exception {
 209                 IConnectionHandle handle = IServerHandle.create(LocalRJMXTestToolkit.createDefaultDescriptor()).connect("Test"); //$NON-NLS-1$
 210                 IMRIMetadataService service = LocalRJMXTestToolkit.getInfoService(handle);
 211                 IMRIMetadata info = service.getMetadata(new MRI(Type.ATTRIBUTE, "java.lang:type=Memory", "HeapMemoryUsage")); //$NON-NLS-1$ //$NON-NLS-2$
 212                 assertTrue("MRI is not composite!", MRIMetadataToolkit.isComposite(info)); //$NON-NLS-1$
 213                 int childCount = 0;
 214                 for (MRI mri : handle.getServiceOrThrow(IMRIService.class).getMRIs()) {
 215                         if (info.getMRI().isChild(mri)) {
 216                                 childCount++;
 217                         }
 218                 }
 219                 assertEquals("There is not four composite children!", 4, childCount); //$NON-NLS-1$
 220                 handle.close();
 221         }
 222 }