PacketTest.java revision 9330:8b1f1c2a400f
1/*
2 * Copyright (c) 2012, 2013, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24
25import java.io.IOException;
26import java.util.Map;
27import java.util.UUID;
28
29import org.testng.annotations.Test;
30import sun.management.jdp.JdpJmxPacket;
31
32import static org.testng.Assert.assertEquals;
33
34
35/**
36 * These tests are unit tests used for test development.
37 * These are not meant to be by automatically run by JTREG.
38 * They exists to support test development and should be run by the test developer.
39 * <p/>
40 * <p/>
41 * The JDP packet format:
42 * <p/>
43 * packet = header + payload
44 * <p/>
45 * header  = magic + version
46 * magic       = 4 bytes: 0xCOFFEE42
47 * version     = 2 bytes: 01 (As of 2013-05-01)
48 * <p/>
49 * payload = list key/value pairs
50 * keySize     = 2 bytes
51 * key         = (keySize) bytes
52 * valueSize   = 2 bytes
53 * value       = (valueSize) bytes
54 * <p/>
55 * <p/>
56 * Two entries are mandatory in the payload:
57 * UUID            (JdpJmxPacket.UUID_KEY)
58 * JMX service URL (JdpJmxPacket.JMX_SERVICE_URL_KEY)
59 * <p/>
60 * These two entries are optional:
61 * Main Class      (JdpJmxPacket.MAIN_CLASS_KEY)
62 * Instance name   (JdpJmxPacket.INSTANCE_NAME_KEY)
63 *
64 * @author Alex Schenkman
65 *         <p/>
66 *         Using TestNG framework.
67 */
68public class PacketTest {
69
70    final int MAGIC = 0xC0FFEE42;
71    final UUID id = UUID.randomUUID();
72    final String mainClass = "org.happy.Feet";
73    final String jmxServiceUrl = "fake://jmxUrl";
74    final String instanceName = "Joe";
75
76    private JdpJmxPacket createDefaultPacket() {
77        JdpJmxPacket packet = new JdpJmxPacket(id, jmxServiceUrl);
78        return packet;
79    }
80
81    private JdpJmxPacket createFullPacket() {
82        JdpJmxPacket packet = new JdpJmxPacket(id, jmxServiceUrl);
83        packet.setMainClass(mainClass);
84        packet.setInstanceName("Joe");
85        return packet;
86    }
87
88    @Test
89    public void testMagic() throws IOException {
90        byte[] rawData = createFullPacket().getPacketData();
91        int magic = JdpTestUtil.decode4ByteInt(rawData, 0);
92        assertEquals(MAGIC, magic, "MAGIC does not match!");
93    }
94
95    @Test
96    public void testVersion() throws IOException {
97        byte[] rawData = createFullPacket().getPacketData();
98        assertEquals(1, JdpTestUtil.decode2ByteInt(rawData, 4));
99    }
100
101    @Test
102    public void testAllEntries() throws IOException {
103        byte[] rawData = createFullPacket().getPacketData();
104        Map<String, String> payload = JdpTestUtil.readPayload(rawData);
105
106        assertEquals(4, payload.size());
107        assertEquals(mainClass, payload.get(JdpJmxPacket.MAIN_CLASS_KEY));
108        assertEquals(id.toString(), payload.get(JdpJmxPacket.UUID_KEY));
109        assertEquals(jmxServiceUrl, payload.get(JdpJmxPacket.JMX_SERVICE_URL_KEY));
110        assertEquals(instanceName, payload.get(JdpJmxPacket.INSTANCE_NAME_KEY));
111    }
112
113    public void testDefaultEntries() throws IOException {
114        byte[] rawData = createDefaultPacket().getPacketData();
115        Map<String, String> payload = JdpTestUtil.readPayload(rawData);
116
117        assertEquals(2, payload.size());
118        assertEquals(id.toString(), payload.get(JdpJmxPacket.UUID_KEY));
119        assertEquals(jmxServiceUrl, payload.get(JdpJmxPacket.JMX_SERVICE_URL_KEY));
120    }
121}
122