|
@@ -18,20 +18,21 @@
|
|
package org.apache.hadoop.jmx;
|
|
package org.apache.hadoop.jmx;
|
|
|
|
|
|
|
|
|
|
-import java.net.URL;
|
|
|
|
-import java.util.regex.Matcher;
|
|
|
|
-import java.util.regex.Pattern;
|
|
|
|
-
|
|
|
|
-import org.apache.commons.logging.Log;
|
|
|
|
-import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.hadoop.http.HttpServer2;
|
|
import org.apache.hadoop.http.HttpServer2;
|
|
import org.apache.hadoop.http.HttpServerFunctionalTest;
|
|
import org.apache.hadoop.http.HttpServerFunctionalTest;
|
|
import org.junit.AfterClass;
|
|
import org.junit.AfterClass;
|
|
import org.junit.BeforeClass;
|
|
import org.junit.BeforeClass;
|
|
import org.junit.Test;
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
+import java.net.HttpURLConnection;
|
|
|
|
+import java.net.URL;
|
|
|
|
+import java.util.regex.Matcher;
|
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
+
|
|
|
|
+import static org.apache.hadoop.jmx.JMXJsonServlet.ACCESS_CONTROL_ALLOW_METHODS;
|
|
|
|
+import static org.apache.hadoop.jmx.JMXJsonServlet.ACCESS_CONTROL_ALLOW_ORIGIN;
|
|
|
|
+
|
|
public class TestJMXJsonServlet extends HttpServerFunctionalTest {
|
|
public class TestJMXJsonServlet extends HttpServerFunctionalTest {
|
|
- private static final Log LOG = LogFactory.getLog(TestJMXJsonServlet.class);
|
|
|
|
private static HttpServer2 server;
|
|
private static HttpServer2 server;
|
|
private static URL baseUrl;
|
|
private static URL baseUrl;
|
|
|
|
|
|
@@ -53,54 +54,31 @@ public class TestJMXJsonServlet extends HttpServerFunctionalTest {
|
|
|
|
|
|
@Test public void testQuery() throws Exception {
|
|
@Test public void testQuery() throws Exception {
|
|
String result = readOutput(new URL(baseUrl, "/jmx?qry=java.lang:type=Runtime"));
|
|
String result = readOutput(new URL(baseUrl, "/jmx?qry=java.lang:type=Runtime"));
|
|
- LOG.info("/jmx?qry=java.lang:type=Runtime RESULT: "+result);
|
|
|
|
assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Runtime\"", result);
|
|
assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Runtime\"", result);
|
|
assertReFind("\"modelerType\"", result);
|
|
assertReFind("\"modelerType\"", result);
|
|
|
|
|
|
result = readOutput(new URL(baseUrl, "/jmx?qry=java.lang:type=Memory"));
|
|
result = readOutput(new URL(baseUrl, "/jmx?qry=java.lang:type=Memory"));
|
|
- LOG.info("/jmx?qry=java.lang:type=Memory RESULT: "+result);
|
|
|
|
assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
|
|
assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
|
|
assertReFind("\"modelerType\"", result);
|
|
assertReFind("\"modelerType\"", result);
|
|
|
|
|
|
result = readOutput(new URL(baseUrl, "/jmx"));
|
|
result = readOutput(new URL(baseUrl, "/jmx"));
|
|
- LOG.info("/jmx RESULT: "+result);
|
|
|
|
assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
|
|
assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
|
|
|
|
|
|
// test to get an attribute of a mbean
|
|
// test to get an attribute of a mbean
|
|
result = readOutput(new URL(baseUrl,
|
|
result = readOutput(new URL(baseUrl,
|
|
"/jmx?get=java.lang:type=Memory::HeapMemoryUsage"));
|
|
"/jmx?get=java.lang:type=Memory::HeapMemoryUsage"));
|
|
- LOG.info("/jmx RESULT: "+result);
|
|
|
|
assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
|
|
assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
|
|
assertReFind("\"committed\"\\s*:", result);
|
|
assertReFind("\"committed\"\\s*:", result);
|
|
|
|
|
|
// negative test to get an attribute of a mbean
|
|
// negative test to get an attribute of a mbean
|
|
result = readOutput(new URL(baseUrl,
|
|
result = readOutput(new URL(baseUrl,
|
|
"/jmx?get=java.lang:type=Memory::"));
|
|
"/jmx?get=java.lang:type=Memory::"));
|
|
- LOG.info("/jmx RESULT: "+result);
|
|
|
|
- assertReFind("\"ERROR\"", result);
|
|
|
|
-
|
|
|
|
- // test to get JSONP result
|
|
|
|
- result = readOutput(new URL(baseUrl, "/jmx?qry=java.lang:type=Memory&callback=mycallback1"));
|
|
|
|
- LOG.info("/jmx?qry=java.lang:type=Memory&callback=mycallback RESULT: "+result);
|
|
|
|
- assertReFind("^mycallback1\\(\\{", result);
|
|
|
|
- assertReFind("\\}\\);$", result);
|
|
|
|
-
|
|
|
|
- // negative test to get an attribute of a mbean as JSONP
|
|
|
|
- result = readOutput(new URL(baseUrl,
|
|
|
|
- "/jmx?get=java.lang:type=Memory::&callback=mycallback2"));
|
|
|
|
- LOG.info("/jmx RESULT: "+result);
|
|
|
|
- assertReFind("^mycallback2\\(\\{", result);
|
|
|
|
assertReFind("\"ERROR\"", result);
|
|
assertReFind("\"ERROR\"", result);
|
|
- assertReFind("\\}\\);$", result);
|
|
|
|
-
|
|
|
|
- // test to get an attribute of a mbean as JSONP
|
|
|
|
- result = readOutput(new URL(baseUrl,
|
|
|
|
- "/jmx?get=java.lang:type=Memory::HeapMemoryUsage&callback=mycallback3"));
|
|
|
|
- LOG.info("/jmx RESULT: "+result);
|
|
|
|
- assertReFind("^mycallback3\\(\\{", result);
|
|
|
|
- assertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
|
|
|
|
- assertReFind("\"committed\"\\s*:", result);
|
|
|
|
- assertReFind("\\}\\);$", result);
|
|
|
|
|
|
|
|
|
|
+ // test to CORS headers
|
|
|
|
+ HttpURLConnection conn = (HttpURLConnection)
|
|
|
|
+ new URL(baseUrl, "/jmx?qry=java.lang:type=Memory").openConnection();
|
|
|
|
+ assertEquals("GET", conn.getHeaderField(ACCESS_CONTROL_ALLOW_METHODS));
|
|
|
|
+ assertNotNull(conn.getHeaderField(ACCESS_CONTROL_ALLOW_ORIGIN));
|
|
}
|
|
}
|
|
}
|
|
}
|