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; 34 35 import static org.junit.Assert.assertTrue; 36 37 import org.junit.Test; 38 import org.openjdk.jmc.common.io.IOToolkit; 39 import org.openjdk.jmc.rjmx.ConnectionDescriptorBuilder; 40 import org.openjdk.jmc.rjmx.ConnectionException; 41 import org.openjdk.jmc.rjmx.IConnectionDescriptor; 42 import org.openjdk.jmc.rjmx.IConnectionHandle; 43 import org.openjdk.jmc.rjmx.IServerDescriptor; 44 import org.openjdk.jmc.rjmx.IServerHandle; 45 import org.openjdk.jmc.rjmx.RJMXPlugin; 46 import org.openjdk.jmc.rjmx.servermodel.IServer; 47 import org.openjdk.jmc.rjmx.servermodel.IServerModel; 48 import org.openjdk.jmc.rjmx.subscription.IMBeanHelperService; 49 import org.openjdk.jmc.rjmx.subscription.IMRISubscription; 50 import org.openjdk.jmc.rjmx.subscription.IMRIValueListener; 51 import org.openjdk.jmc.rjmx.subscription.ISubscriptionService; 52 import org.openjdk.jmc.rjmx.subscription.MRI; 53 import org.openjdk.jmc.rjmx.subscription.MRI.Type; 54 import org.openjdk.jmc.rjmx.subscription.MRIValueEvent; 55 import org.openjdk.jmc.rjmx.subscription.PolicyFactory; 56 import org.openjdk.jmc.ui.common.util.Environment; 57 58 /** 59 * This test suite is supposed to test the example code that we ship with the documentation for the 60 * org.openjdk.jmc.rjmx bundle. That is, for each code example included in 61 * org.openjdk.jmc.rjmx/src/org/openjdk/jmc/rjmx/package.html, there should be a test method in here 62 * with a verbatim copy of that code. 63 * <p> 64 * Included in the RJMXTestSuite. 65 */ 66 // NOTE: If you change the verbatim test YOU MUST update the corresponding package.html document. 67 public class PackageExampleTest { 68 private volatile boolean gotEvent; 69 70 /** 71 * Tests that the package documentation example actually makes sense and compiles as expected. 72 * 73 * @throws ConnectionException 74 */ 75 @Test 76 public void testPackageExampleVerbatim() throws Exception { 77 IConnectionDescriptor descriptor = new ConnectionDescriptorBuilder().hostName("localhost").port(0).build(); 78 IServerHandle serverHandle = IServerHandle.create(descriptor); 79 IConnectionHandle handle = serverHandle.connect("Usage description"); 80 try { 81 ISubscriptionService service = handle.getServiceOrThrow(ISubscriptionService.class); 82 MRI attribute = new MRI(Type.ATTRIBUTE, "java.lang:type=Threading", "ThreadCount"); 83 service.addMRIValueListener(attribute, new IMRIValueListener() { 84 @Override 85 public void valueChanged(MRIValueEvent event) { 86 System.out.println(event.getValue()); 87 } 88 }); 89 IMRISubscription subscription = service.getMRISubscription(attribute); 90 subscription.setUpdatePolicy(PolicyFactory.createSimpleUpdatePolicy(1500)); 91 } finally { 92 // Always close IConnectionHandle when done 93 IOToolkit.closeSilently(handle); 94 } 95 96 } 97 98 @Test 99 public void testPackageExampleFunctionality() throws Exception { 100 ConnectionDescriptorBuilder builder = new ConnectionDescriptorBuilder(); 101 IConnectionDescriptor descriptor = builder.hostName("localhost").port(0).build(); 102 IConnectionHandle handle = IServerHandle.create(descriptor).connect("Usage description"); 103 try { 104 ISubscriptionService service = handle.getServiceOrThrow(ISubscriptionService.class); 105 gotEvent = false; 106 MRI attribute = new MRI(Type.ATTRIBUTE, "java.lang:type=Threading", "ThreadCount"); 107 service.addMRIValueListener(attribute, new IMRIValueListener() { 108 @Override 109 public void valueChanged(MRIValueEvent event) { 110 synchronized (PackageExampleTest.this) { 111 gotEvent = true; 112 PackageExampleTest.this.notifyAll(); 113 } 114 } 115 }); 116 IMRISubscription subscription = service.getMRISubscription(attribute); 117 subscription.setUpdatePolicy(PolicyFactory.createSimpleUpdatePolicy(1500)); 118 synchronized (PackageExampleTest.this) { 119 this.wait(4000); 120 } 121 } finally { 122 IOToolkit.closeSilently(handle); 123 } 124 assertTrue("Never got any event!", gotEvent); 125 } 126 127 @Test 128 public void testUseServerModel() throws Exception { 129 IServerModel model = RJMXPlugin.getDefault().getService(IServerModel.class); 130 for (IServer server : model.elements()) { 131 IServerDescriptor descriptor = server.getServerHandle().getServerDescriptor(); 132 if (descriptor.getJvmInfo() != null 133 && Integer.valueOf(Environment.getThisPID()).equals(descriptor.getJvmInfo().getPid())) { 134 IConnectionHandle handle = server.getServerHandle().connect("Usage description"); 135 try { 136 handle.getServiceOrThrow(IMBeanHelperService.class).getMBeanNames().size(); 137 return; 138 } finally { 139 IOToolkit.closeSilently(handle); 140 } 141 } 142 } 143 } 144 }