|
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 * This test tests that complete packet is build correctly |
|
37 */ |
|
38 public static void PacketBuilderTest() |
|
39 throws IOException, JdpException { |
|
40 |
|
41 /* Complete packet test */ |
|
42 { |
|
43 JdpJmxPacket p1 = new JdpJmxPacket(UUID.randomUUID(), "fake://unit-test"); |
|
44 p1.setMainClass("FakeUnitTest"); |
|
45 p1.setInstanceName("Fake"); |
|
46 byte[] b = p1.getPacketData(); |
|
47 |
|
48 JdpJmxPacket p2 = new JdpJmxPacket(b); |
|
49 JdpDoSomething.printJdpPacket(p1); |
|
50 JdpDoSomething.compaireJdpPacketEx(p1, p2); |
|
51 } |
|
52 |
|
53 /*Missed field packet test*/ |
|
54 { |
|
55 JdpJmxPacket p1 = new JdpJmxPacket(UUID.randomUUID(), "fake://unit-test"); |
|
56 p1.setMainClass("FakeUnitTest"); |
|
57 p1.setInstanceName(null); |
|
58 byte[] b = p1.getPacketData(); |
|
59 |
|
60 JdpJmxPacket p2 = new JdpJmxPacket(b); |
|
61 JdpDoSomething.printJdpPacket(p1); |
|
62 JdpDoSomething.compaireJdpPacketEx(p1, p2); |
|
63 } |
|
64 |
|
65 System.out.println("OK: Test passed"); |
|
66 |
|
67 } |
|
68 |
|
69 public static void startFakeDiscoveryService() |
|
70 throws IOException, JdpException { |
|
71 |
|
72 String discoveryPort = System.getProperty("com.sun.management.jdp.port"); |
|
73 String discoveryAddress = System.getProperty("com.sun.management.jdp.address"); |
|
74 InetAddress address = InetAddress.getByName(discoveryAddress); |
|
75 int port = Integer.parseInt(discoveryPort); |
|
76 JdpController.startDiscoveryService(address, port, "FakeDiscovery", "fake://unit-test"); |
|
77 } |
|
78 |
|
79 public static void main(String[] args) { |
|
80 try { |
|
81 PacketBuilderTest(); |
|
82 startFakeDiscoveryService(); |
|
83 JdpDoSomething.doSomething(); |
|
84 |
|
85 } catch (Throwable e) { |
|
86 e.printStackTrace(); |
|
87 System.out.println("Test failed. unexpected error " + e); |
|
88 } |
|
89 } |
|
90 } |