KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > mx4j > tools > adaptor > http > HttpAdaptorTest


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package test.mx4j.tools.adaptor.http;
10
11 import java.io.IOException JavaDoc;
12 import java.io.InputStream JavaDoc;
13 import java.net.HttpURLConnection JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.net.URLConnection JavaDoc;
16 import javax.management.Attribute JavaDoc;
17 import javax.management.JMException JavaDoc;
18 import javax.management.MBeanServer JavaDoc;
19 import javax.management.MBeanServerFactory JavaDoc;
20 import javax.management.ObjectName JavaDoc;
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 JavaDoc;
26
27 /**
28  * Class HttpAdaptorTest, tests the basics of the HttpAdaptor class
29  *
30  * @version $Revision: 1.3 $
31  */

32 public class HttpAdaptorTest extends TestCase
33 {
34    protected MBeanServer JavaDoc server;
35    protected ObjectName JavaDoc name;
36    protected static final int DEFAULT_PORT = 9090;
37
38    /**
39     * Constructor requested by the JUnit framework
40     */

41    public HttpAdaptorTest()
42    {
43       super("HTTPAdapter Test");
44    }
45
46    /**
47     * Constructor requested by the JUnit framework
48     */

49    public HttpAdaptorTest(String JavaDoc name)
50    {
51       super(name);
52    }
53
54    public void setUp()
55    {
56       try
57       {
58          server = MBeanServerFactory.createMBeanServer("Http");
59          name = new ObjectName JavaDoc("Http:name=HttpAdaptor");
60          server.createMBean("mx4j.tools.adaptor.http.HttpAdaptor", name, null);
61          // use another port. There is less proabablity of collision
62
server.setAttribute(name, new Attribute JavaDoc("Port", new Integer JavaDoc(DEFAULT_PORT)));
63          server.invoke(name, "start", null, null);
64       }
65       catch (Exception JavaDoc 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 JavaDoc e)
79       {
80          // ignore, it shouldn't happen
81
}
82    }
83
84    public void testBasics() throws Exception JavaDoc
85    {
86       // test default parameters
87
assertEquals(new Integer JavaDoc(DEFAULT_PORT), server.getAttribute(name, "Port"));
88       assertEquals("localhost", server.getAttribute(name, "Host"));
89       assertEquals("none", server.getAttribute(name, "AuthenticationMethod"));
90
91       waitToStop();
92       // test changing parameters
93
server.setAttribute(name, new Attribute JavaDoc("Port", new Integer JavaDoc(8000)));
94       assertEquals(new Integer JavaDoc(8000), server.getAttribute(name, "Port"));
95       server.setAttribute(name, new Attribute JavaDoc("Host", "1.1.1.1"));
96       assertEquals("1.1.1.1", server.getAttribute(name, "Host"));
97       server.setAttribute(name, new Attribute JavaDoc("AuthenticationMethod", "basic"));
98       assertEquals("basic", server.getAttribute(name, "AuthenticationMethod"));
99       boolean exception = false;
100       try
101       {
102          server.setAttribute(name, new Attribute JavaDoc("AuthenticationMethod", "something"));
103       }
104       catch (Exception JavaDoc e)
105       {
106          exception = true;
107       }
108       assertTrue(exception);
109       exception = false;
110       try
111       {
112          // test both null
113
server.invoke(name, "addAuthorization",
114                        new Object JavaDoc[]{null, null},
115                        new String JavaDoc[]{"java.lang.String", "java.lang.String"});
116       }
117       catch (Exception JavaDoc e)
118       {
119          exception = true;
120       }
121       assertTrue(exception);
122       server.invoke(name, "addAuthorization",
123                     new Object JavaDoc[]{"mx4j", "mx4j"},
124                     new String JavaDoc[]{"java.lang.String", "java.lang.String"});
125       waitToStop();
126       // test changing parameters when server running
127
exception = false;
128       try
129       {
130          server.setAttribute(name, new Attribute JavaDoc("Port", new Integer JavaDoc(8000)));
131       }
132       catch (Exception JavaDoc e)
133       {
134          exception = true;
135       }
136       assertTrue(true);
137       exception = false;
138       try
139       {
140          server.setAttribute(name, new Attribute JavaDoc("Host", "localhost"));
141       }
142       catch (Exception JavaDoc e)
143       {
144          exception = true;
145       }
146       assertTrue(true);
147       exception = false;
148       try
149       {
150          server.setAttribute(name, new Attribute JavaDoc("AuthenticationMethod", "digest"));
151       }
152       catch (Exception JavaDoc e)
153       {
154          exception = true;
155       }
156       assertTrue(true);
157    }
158
159    public void testAuthentication() throws Exception JavaDoc
160    {
161       String JavaDoc host = "localhost";
162       int port = DEFAULT_PORT;
163       URL JavaDoc url = new URL JavaDoc("http://" + host + ":" + port + "/");
164       URLConnection JavaDoc connection = url.openConnection();
165       InputStream JavaDoc in = connection.getInputStream();
166       in.close();
167       waitToStop();
168       server.setAttribute(name, new Attribute JavaDoc("AuthenticationMethod", "basic"));
169       server.invoke(name, "addAuthorization",
170                     new Object JavaDoc[]{"mx4j", "mx4j"},
171                     new String JavaDoc[]{"java.lang.String", "java.lang.String"});
172       server.invoke(name, "start", null, null);
173       url = new URL JavaDoc("http://" + host + ":" + port + "/");
174       connection = url.openConnection();
175       try
176       {
177          in = connection.getInputStream();
178       }
179       catch (Exception JavaDoc e)
180       {
181       }
182       finally
183       {
184          in.close();
185       }
186       assertEquals(((HttpURLConnection JavaDoc)connection).getResponseCode(), 401);
187       url = new URL JavaDoc("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 JavaDoc("AuthenticationMethod", "none"));
194    }
195
196
197    public void testAddCommandProcessor() throws Exception JavaDoc
198    {
199       String JavaDoc host = "localhost";
200       int port = DEFAULT_PORT;
201       URLConnection JavaDoc connection = null;
202       URL JavaDoc url = new URL JavaDoc("http://" + host + ":" + port + "/nonexistant");
203       server.invoke(name, "addCommandProcessor", new Object JavaDoc[]{"nonexistant", new DummyCommandProcessor()}, new String JavaDoc[]{"java.lang.String", "mx4j.tools.adaptor.http.HttpCommandProcessor"});
204       //connection.close();
205
connection = url.openConnection();
206       assertEquals(200, ((HttpURLConnection JavaDoc)connection).getResponseCode());
207       server.invoke(name, "removeCommandProcessor", new Object JavaDoc[]{"nonexistant"}, new String JavaDoc[]{"java.lang.String"});
208       //connection.close();
209
connection = url.openConnection();
210       assertEquals(404, ((HttpURLConnection JavaDoc)connection).getResponseCode());
211       server.invoke(name, "addCommandProcessor", new Object JavaDoc[]{"nonexistant", "test.mx4j.tools.adaptor.http.HttpAdaptorTest$DummyCommandProcessor"}, new String JavaDoc[]{"java.lang.String", "java.lang.String"});
212       //connection.close();
213
connection = url.openConnection();
214       assertEquals(200, ((HttpURLConnection JavaDoc)connection).getResponseCode());
215    }
216
217    private void waitToStop()
218    {
219       try
220       {
221          while (((Boolean JavaDoc)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 JavaDoc e)
232             {
233                continue;
234             }
235          }
236       }
237       catch (Exception JavaDoc e)
238       {
239          e.printStackTrace();
240       }
241    }
242
243    public static class DummyCommandProcessor extends HttpCommandProcessorAdaptor
244    {
245       public Document JavaDoc executeRequest(HttpInputStream in) throws IOException JavaDoc, JMException JavaDoc
246       {
247          return builder.newDocument();
248       }
249    }
250
251 }
252
Popular Tags