Path: blob/master/test/jdk/sun/management/jdp/PacketTest.java
41152 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324import java.io.IOException;25import java.util.Map;26import java.util.UUID;2728import org.testng.annotations.Test;29import sun.management.jdp.JdpJmxPacket;3031import static org.testng.Assert.assertEquals;323334/**35* These tests are unit tests used for test development.36* These are not meant to be by automatically run by JTREG.37* They exists to support test development and should be run by the test developer.38* <p/>39* <p/>40* The JDP packet format:41* <p/>42* packet = header + payload43* <p/>44* header = magic + version45* magic = 4 bytes: 0xCOFFEE4246* version = 2 bytes: 01 (As of 2013-05-01)47* <p/>48* payload = list key/value pairs49* keySize = 2 bytes50* key = (keySize) bytes51* valueSize = 2 bytes52* value = (valueSize) bytes53* <p/>54* <p/>55* Two entries are mandatory in the payload:56* UUID (JdpJmxPacket.UUID_KEY)57* JMX service URL (JdpJmxPacket.JMX_SERVICE_URL_KEY)58* <p/>59* These two entries are optional:60* Main Class (JdpJmxPacket.MAIN_CLASS_KEY)61* Instance name (JdpJmxPacket.INSTANCE_NAME_KEY)62*63* @author Alex Schenkman64* <p/>65* Using TestNG framework.66*/67public class PacketTest {6869final int MAGIC = 0xC0FFEE42;70final UUID id = UUID.randomUUID();71final String mainClass = "org.happy.Feet";72final String jmxServiceUrl = "fake://jmxUrl";73final String instanceName = "Joe";7475private JdpJmxPacket createDefaultPacket() {76JdpJmxPacket packet = new JdpJmxPacket(id, jmxServiceUrl);77return packet;78}7980private JdpJmxPacket createFullPacket() {81JdpJmxPacket packet = new JdpJmxPacket(id, jmxServiceUrl);82packet.setMainClass(mainClass);83packet.setInstanceName("Joe");84return packet;85}8687@Test88public void testMagic() throws IOException {89byte[] rawData = createFullPacket().getPacketData();90int magic = JdpTestUtil.decode4ByteInt(rawData, 0);91assertEquals(MAGIC, magic, "MAGIC does not match!");92}9394@Test95public void testVersion() throws IOException {96byte[] rawData = createFullPacket().getPacketData();97assertEquals(1, JdpTestUtil.decode2ByteInt(rawData, 4));98}99100@Test101public void testAllEntries() throws IOException {102byte[] rawData = createFullPacket().getPacketData();103Map<String, String> payload = JdpTestUtil.readPayload(rawData);104105assertEquals(4, payload.size());106assertEquals(mainClass, payload.get(JdpJmxPacket.MAIN_CLASS_KEY));107assertEquals(id.toString(), payload.get(JdpJmxPacket.UUID_KEY));108assertEquals(jmxServiceUrl, payload.get(JdpJmxPacket.JMX_SERVICE_URL_KEY));109assertEquals(instanceName, payload.get(JdpJmxPacket.INSTANCE_NAME_KEY));110}111112public void testDefaultEntries() throws IOException {113byte[] rawData = createDefaultPacket().getPacketData();114Map<String, String> payload = JdpTestUtil.readPayload(rawData);115116assertEquals(2, payload.size());117assertEquals(id.toString(), payload.get(JdpJmxPacket.UUID_KEY));118assertEquals(jmxServiceUrl, payload.get(JdpJmxPacket.JMX_SERVICE_URL_KEY));119}120}121122123