1 8 9 package test.mx4j.tools.adaptor.http; 10 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 import java.net.HttpURLConnection ; 14 import java.net.URL ; 15 import java.net.URLConnection ; 16 import javax.management.Attribute ; 17 import javax.management.JMException ; 18 import javax.management.MBeanServer ; 19 import javax.management.MBeanServerFactory ; 20 import javax.management.ObjectName ; 21 22 import junit.framework.TestCase; 23 import mx4j.tools.adaptor.http.HttpCommandProcessorAdaptor; 24 import mx4j.tools.adaptor.http.HttpInputStream; 25 import org.w3c.dom.Document ; 26 27 32 public class HttpAdaptorTest extends TestCase 33 { 34 protected MBeanServer server; 35 protected ObjectName name; 36 protected static final int DEFAULT_PORT = 9090; 37 38 41 public HttpAdaptorTest() 42 { 43 super("HTTPAdapter Test"); 44 } 45 46 49 public HttpAdaptorTest(String name) 50 { 51 super(name); 52 } 53 54 public void setUp() 55 { 56 try 57 { 58 server = MBeanServerFactory.createMBeanServer("Http"); 59 name = new ObjectName ("Http:name=HttpAdaptor"); 60 server.createMBean("mx4j.tools.adaptor.http.HttpAdaptor", name, null); 61 server.setAttribute(name, new Attribute ("Port", new Integer (DEFAULT_PORT))); 63 server.invoke(name, "start", null, null); 64 } 65 catch (Exception e) 66 { 67 e.printStackTrace(); 68 } 69 } 70 71 public void tearDown() 72 { 73 waitToStop(); 74 try 75 { 76 server.unregisterMBean(name); 77 } 78 catch (Exception e) 79 { 80 } 82 } 83 84 public void testBasics() throws Exception 85 { 86 assertEquals(new Integer (DEFAULT_PORT), server.getAttribute(name, "Port")); 88 assertEquals("localhost", server.getAttribute(name, "Host")); 89 assertEquals("none", server.getAttribute(name, "AuthenticationMethod")); 90 91 waitToStop(); 92 server.setAttribute(name, new Attribute ("Port", new Integer (8000))); 94 assertEquals(new Integer (8000), server.getAttribute(name, "Port")); 95 server.setAttribute(name, new Attribute ("Host", "1.1.1.1")); 96 assertEquals("1.1.1.1", server.getAttribute(name, "Host")); 97 server.setAttribute(name, new Attribute ("AuthenticationMethod", "basic")); 98 assertEquals("basic", server.getAttribute(name, "AuthenticationMethod")); 99 boolean exception = false; 100 try 101 { 102 server.setAttribute(name, new Attribute ("AuthenticationMethod", "something")); 103 } 104 catch (Exception e) 105 { 106 exception = true; 107 } 108 assertTrue(exception); 109 exception = false; 110 try 111 { 112 server.invoke(name, "addAuthorization", 114 new Object []{null, null}, 115 new String []{"java.lang.String", "java.lang.String"}); 116 } 117 catch (Exception e) 118 { 119 exception = true; 120 } 121 assertTrue(exception); 122 server.invoke(name, "addAuthorization", 123 new Object []{"mx4j", "mx4j"}, 124 new String []{"java.lang.String", "java.lang.String"}); 125 waitToStop(); 126 exception = false; 128 try 129 { 130 server.setAttribute(name, new Attribute ("Port", new Integer (8000))); 131 } 132 catch (Exception e) 133 { 134 exception = true; 135 } 136 assertTrue(true); 137 exception = false; 138 try 139 { 140 server.setAttribute(name, new Attribute ("Host", "localhost")); 141 } 142 catch (Exception e) 143 { 144 exception = true; 145 } 146 assertTrue(true); 147 exception = false; 148 try 149 { 150 server.setAttribute(name, new Attribute ("AuthenticationMethod", "digest")); 151 } 152 catch (Exception e) 153 { 154 exception = true; 155 } 156 assertTrue(true); 157 } 158 159 public void testAuthentication() throws Exception 160 { 161 String host = "localhost"; 162 int port = DEFAULT_PORT; 163 URL url = new URL ("http://" + host + ":" + port + "/"); 164 URLConnection connection = url.openConnection(); 165 InputStream in = connection.getInputStream(); 166 in.close(); 167 waitToStop(); 168 server.setAttribute(name, new Attribute ("AuthenticationMethod", "basic")); 169 server.invoke(name, "addAuthorization", 170 new Object []{"mx4j", "mx4j"}, 171 new String []{"java.lang.String", "java.lang.String"}); 172 server.invoke(name, "start", null, null); 173 url = new URL ("http://" + host + ":" + port + "/"); 174 connection = url.openConnection(); 175 try 176 { 177 in = connection.getInputStream(); 178 } 179 catch (Exception e) 180 { 181 } 182 finally 183 { 184 in.close(); 185 } 186 assertEquals(((HttpURLConnection )connection).getResponseCode(), 401); 187 url = new URL ("http://" + host + ":" + port + "/"); 188 connection = url.openConnection(); 189 connection.setRequestProperty("Authorization", "basic bXg0ajpteDRq"); 190 in = connection.getInputStream(); 191 in.close(); 192 waitToStop(); 193 server.setAttribute(name, new Attribute ("AuthenticationMethod", "none")); 194 } 195 196 197 public void testAddCommandProcessor() throws Exception 198 { 199 String host = "localhost"; 200 int port = DEFAULT_PORT; 201 URLConnection connection = null; 202 URL url = new URL ("http://" + host + ":" + port + "/nonexistant"); 203 server.invoke(name, "addCommandProcessor", new Object []{"nonexistant", new DummyCommandProcessor()}, new String []{"java.lang.String", "mx4j.tools.adaptor.http.HttpCommandProcessor"}); 204 connection = url.openConnection(); 206 assertEquals(200, ((HttpURLConnection )connection).getResponseCode()); 207 server.invoke(name, "removeCommandProcessor", new Object []{"nonexistant"}, new String []{"java.lang.String"}); 208 connection = url.openConnection(); 210 assertEquals(404, ((HttpURLConnection )connection).getResponseCode()); 211 server.invoke(name, "addCommandProcessor", new Object []{"nonexistant", "test.mx4j.tools.adaptor.http.HttpAdaptorTest$DummyCommandProcessor"}, new String []{"java.lang.String", "java.lang.String"}); 212 connection = url.openConnection(); 214 assertEquals(200, ((HttpURLConnection )connection).getResponseCode()); 215 } 216 217 private void waitToStop() 218 { 219 try 220 { 221 while (((Boolean )server.getAttribute(name, "Active")).booleanValue()) 222 { 223 try 224 { 225 server.invoke(name, "stop", null, null); 226 synchronized (this) 227 { 228 wait(1000); 229 } 230 } 231 catch (Exception e) 232 { 233 continue; 234 } 235 } 236 } 237 catch (Exception e) 238 { 239 e.printStackTrace(); 240 } 241 } 242 243 public static class DummyCommandProcessor extends HttpCommandProcessorAdaptor 244 { 245 public Document executeRequest(HttpInputStream in) throws IOException , JMException 246 { 247 return builder.newDocument(); 248 } 249 } 250 251 } 252 | Popular Tags |