1 /*
   2  * Copyright (c) 2012, 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 import java.io.IOException;
  25 import java.net.InetAddress;
  26 import java.util.UUID;
  27 
  28 import sun.management.jdp.JdpController;
  29 import sun.management.jdp.JdpPacket;
  30 import sun.management.jdp.JdpJmxPacket;
  31 import sun.management.jdp.JdpException;
  32 
  33 public class JdpUnitTest {
  34 
  35 
  36     static byte[] russian_name = {(byte)0xd0,(byte)0xbf,(byte)0xd1,(byte)0x80,(byte)0xd0,(byte)0xbe,(byte)0xd0,(byte)0xb2,
  37                                   (byte)0xd0,(byte)0xb5,(byte)0xd1,(byte)0x80,(byte)0xd0,(byte)0xba,(byte)0xd0,(byte)0xb0,
  38                                   (byte)0x20,(byte)0xd1,(byte)0x81,(byte)0xd0,(byte)0xb2,(byte)0xd1,(byte)0x8f,(byte)0xd0,
  39                                   (byte)0xb7,(byte)0xd0,(byte)0xb8,(byte)0x0a};
  40 
  41     /**
  42      * This test tests that complete packet is build correctly
  43      */
  44     public static void PacketBuilderTest()
  45         throws IOException, JdpException {
  46 
  47         /* Complete packet test */
  48         {
  49             JdpJmxPacket p1 = new JdpJmxPacket(UUID.randomUUID(), "fake://unit-test");
  50             p1.setMainClass("FakeUnitTest");
  51             p1.setInstanceName( new String(russian_name,"UTF-8"));
  52             byte[] b = p1.getPacketData();
  53 
  54             JdpJmxPacket p2 = new JdpJmxPacket(b);
  55             JdpDoSomething.printJdpPacket(p1);
  56             JdpDoSomething.compaireJdpPacketEx(p1, p2);
  57         }
  58 
  59         /*Missed field packet test*/
  60         {
  61             JdpJmxPacket p1 = new JdpJmxPacket(UUID.randomUUID(), "fake://unit-test");
  62             p1.setMainClass("FakeUnitTest");
  63             p1.setInstanceName(null);
  64             byte[] b = p1.getPacketData();
  65 
  66             JdpJmxPacket p2 = new JdpJmxPacket(b);
  67             JdpDoSomething.printJdpPacket(p1);
  68             JdpDoSomething.compaireJdpPacketEx(p1, p2);
  69         }
  70 
  71          System.out.println("OK: Test passed");
  72 
  73     }
  74 
  75     public static void startFakeDiscoveryService()
  76             throws IOException, JdpException {
  77 
  78         String discoveryPort = System.getProperty("com.sun.management.jdp.port");
  79         String discoveryAddress = System.getProperty("com.sun.management.jdp.address");
  80         InetAddress address = InetAddress.getByName(discoveryAddress);
  81         int port = Integer.parseInt(discoveryPort);
  82         JdpController.startDiscoveryService(address, port, "FakeDiscovery", "fake://unit-test");
  83     }
  84 
  85     public static void main(String[] args) {
  86         try {
  87             PacketBuilderTest();
  88             startFakeDiscoveryService();
  89             JdpDoSomething.doSomething();
  90 
  91         } catch (Throwable e) {
  92             e.printStackTrace();
  93             System.out.println("Test failed. unexpected error " + e);
  94         }
  95     }
  96 }