5652N/A/*
5652N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
5652N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5652N/A *
5652N/A * This code is free software; you can redistribute it and/or modify it
5652N/A * under the terms of the GNU General Public License version 2 only, as
5652N/A * published by the Free Software Foundation.
5652N/A *
5652N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5652N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5652N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5652N/A * version 2 for more details (a copy is included in the LICENSE file that
5652N/A * accompanied this code).
5652N/A *
5652N/A * You should have received a copy of the GNU General Public License version
5652N/A * 2 along with this work; if not, write to the Free Software Foundation,
5652N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5652N/A *
5652N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5652N/A * or visit www.oracle.com if you need additional information or have any
5652N/A * questions.
5652N/A */
5652N/A
5652N/Aimport java.io.IOException;
5652N/Aimport java.net.InetAddress;
5652N/Aimport java.util.UUID;
5652N/A
5652N/Aimport sun.management.jdp.JdpController;
5652N/Aimport sun.management.jdp.JdpPacket;
5652N/Aimport sun.management.jdp.JdpJmxPacket;
5652N/Aimport sun.management.jdp.JdpException;
5652N/A
5652N/Apublic class JdpUnitTest {
5652N/A
6135N/A
6135N/A static byte[] russian_name = {(byte)0xd0,(byte)0xbf,(byte)0xd1,(byte)0x80,(byte)0xd0,(byte)0xbe,(byte)0xd0,(byte)0xb2,
6135N/A (byte)0xd0,(byte)0xb5,(byte)0xd1,(byte)0x80,(byte)0xd0,(byte)0xba,(byte)0xd0,(byte)0xb0,
6135N/A (byte)0x20,(byte)0xd1,(byte)0x81,(byte)0xd0,(byte)0xb2,(byte)0xd1,(byte)0x8f,(byte)0xd0,
6135N/A (byte)0xb7,(byte)0xd0,(byte)0xb8,(byte)0x0a};
6135N/A
5652N/A /**
5652N/A * This test tests that complete packet is build correctly
5652N/A */
5652N/A public static void PacketBuilderTest()
5652N/A throws IOException, JdpException {
5652N/A
5652N/A /* Complete packet test */
5652N/A {
5652N/A JdpJmxPacket p1 = new JdpJmxPacket(UUID.randomUUID(), "fake://unit-test");
5652N/A p1.setMainClass("FakeUnitTest");
6135N/A p1.setInstanceName( new String(russian_name,"UTF-8"));
5652N/A byte[] b = p1.getPacketData();
5652N/A
5652N/A JdpJmxPacket p2 = new JdpJmxPacket(b);
5652N/A JdpDoSomething.printJdpPacket(p1);
5652N/A JdpDoSomething.compaireJdpPacketEx(p1, p2);
5652N/A }
5652N/A
5652N/A /*Missed field packet test*/
5652N/A {
5652N/A JdpJmxPacket p1 = new JdpJmxPacket(UUID.randomUUID(), "fake://unit-test");
5652N/A p1.setMainClass("FakeUnitTest");
5652N/A p1.setInstanceName(null);
5652N/A byte[] b = p1.getPacketData();
5652N/A
5652N/A JdpJmxPacket p2 = new JdpJmxPacket(b);
5652N/A JdpDoSomething.printJdpPacket(p1);
5652N/A JdpDoSomething.compaireJdpPacketEx(p1, p2);
5652N/A }
5652N/A
5652N/A System.out.println("OK: Test passed");
5652N/A
5652N/A }
5652N/A
5652N/A public static void startFakeDiscoveryService()
5652N/A throws IOException, JdpException {
5652N/A
5652N/A String discoveryPort = System.getProperty("com.sun.management.jdp.port");
5652N/A String discoveryAddress = System.getProperty("com.sun.management.jdp.address");
5652N/A InetAddress address = InetAddress.getByName(discoveryAddress);
5652N/A int port = Integer.parseInt(discoveryPort);
5652N/A JdpController.startDiscoveryService(address, port, "FakeDiscovery", "fake://unit-test");
5652N/A }
5652N/A
5652N/A public static void main(String[] args) {
5652N/A try {
5652N/A PacketBuilderTest();
5652N/A startFakeDiscoveryService();
5652N/A JdpDoSomething.doSomething();
5652N/A
5652N/A } catch (Throwable e) {
5652N/A e.printStackTrace();
5652N/A System.out.println("Test failed. unexpected error " + e);
5652N/A }
5652N/A }
5652N/A}