1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.http.jmx;
19
20 import java.net.URL;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.hbase.testclassification.SmallTests;
27 import org.apache.hadoop.hbase.http.HttpServer;
28 import org.apache.hadoop.hbase.http.HttpServerFunctionalTest;
29 import org.junit.AfterClass;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.junit.experimental.categories.Category;
33
34 @Category(SmallTests.class)
35 public class TestJMXJsonServlet extends HttpServerFunctionalTest {
36 private static final Log LOG = LogFactory.getLog(TestJMXJsonServlet.class);
37 private static HttpServer server;
38 private static URL baseUrl;
39
40 @BeforeClass public static void setup() throws Exception {
41 server = createTestServer();
42 server.start();
43 baseUrl = getServerURL(server);
44 }
45
46 @AfterClass public static void cleanup() throws Exception {
47 server.stop();
48 }
49
50 public static void assertReFind(String re, String value) {
51 Pattern p = Pattern.compile(re);
52 Matcher m = p.matcher(value);
53 assertTrue("'"+p+"' does not match "+value, m.find());
54 }
55
56 @Test public void testQuery() throws Exception {
57 String result = readOutput(new URL(baseUrl, "/jmx?qry=java.lang:type=Runtime"));
58 LOG.info("/jmx?qry=java.lang:type=Runtime RESULT: "+result);
59 assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Runtime\"", result);
60 assertReFind("\"modelerType\"", result);
61
62 result = readOutput(new URL(baseUrl, "/jmx?qry=java.lang:type=Memory"));
63 LOG.info("/jmx?qry=java.lang:type=Memory RESULT: "+result);
64 assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
65 assertReFind("\"modelerType\"", result);
66
67 result = readOutput(new URL(baseUrl, "/jmx"));
68 LOG.info("/jmx RESULT: "+result);
69 assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
70
71
72 result = readOutput(new URL(baseUrl,
73 "/jmx?get=java.lang:type=Memory::HeapMemoryUsage"));
74 LOG.info("/jmx RESULT: "+result);
75 assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
76 assertReFind("\"committed\"\\s*:", result);
77
78
79 result = readOutput(new URL(baseUrl,
80 "/jmx?get=java.lang:type=Memory::"));
81 LOG.info("/jmx RESULT: "+result);
82 assertReFind("\"ERROR\"", result);
83
84
85 result = readOutput(new URL(baseUrl, "/jmx?qry=java.lang:type=Memory&callback=mycallback1"));
86 LOG.info("/jmx?qry=java.lang:type=Memory&callback=mycallback RESULT: "+result);
87 assertReFind("^mycallback1\\(\\{", result);
88 assertReFind("\\}\\);$", result);
89
90
91 result = readOutput(new URL(baseUrl,
92 "/jmx?get=java.lang:type=Memory::&callback=mycallback2"));
93 LOG.info("/jmx RESULT: "+result);
94 assertReFind("^mycallback2\\(\\{", result);
95 assertReFind("\"ERROR\"", result);
96 assertReFind("\\}\\);$", result);
97
98
99 result = readOutput(new URL(baseUrl,
100 "/jmx?get=java.lang:type=Memory::HeapMemoryUsage&callback=mycallback3"));
101 LOG.info("/jmx RESULT: "+result);
102 assertReFind("^mycallback3\\(\\{", result);
103 assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
104 assertReFind("\"committed\"\\s*:", result);
105 assertReFind("\\}\\);$", result);
106
107
108 result = readOutput(new URL(baseUrl, "/jmx?qry=java.lang:type=Memory&callback=<script>alert('hello')</script>"));
109 LOG.info("/jmx?qry=java.lang:type=Memory&callback=<script>alert('hello')</script> RESULT: "+result);
110 assertTrue(!result.contains("<script>"));
111
112
113 }
114 }