2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
22
package org.jboss.test.remoting.transport.http;
24
import java.io.DataOutputStream;
25
import java.io.EOFException;
26
import java.io.IOException;
27
import java.io.InputStream;
28
import java.io.InputStreamReader;
29
import java.net.InetAddress;
30
import java.net.ServerSocket;
31
import java.net.Socket;
32
import java.util.HashMap;
35
import javax.management.MBeanServer;
37
import junit.framework.TestCase;
39
import org.apache.log4j.ConsoleAppender;
40
import org.apache.log4j.Level;
41
import org.apache.log4j.Logger;
42
import org.apache.log4j.PatternLayout;
43
import org.jboss.logging.XLevel;
44
import org.jboss.remoting.CannotConnectException;
45
import org.jboss.remoting.Client;
46
import org.jboss.remoting.InvocationRequest;
47
import org.jboss.remoting.InvokerLocator;
48
import org.jboss.remoting.ServerInvocationHandler;
49
import org.jboss.remoting.ServerInvoker;
50
import org.jboss.remoting.callback.InvokerCallbackHandler;
51
import org.jboss.remoting.marshal.UnMarshaller;
52
import org.jboss.remoting.marshal.http.HTTPUnMarshaller;
53
import org.jboss.remoting.transport.Connector;
54
import org.jboss.remoting.transport.http.HTTPClientInvoker;
55
import org.jboss.remoting.transport.http.WebServerError;
59
* Unit test for JBREM-1046.
61
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
62
* @version $Revision: 1.1 $
64
* Copyright Oct 27, 2008
67
public class NullInputStreamTestCase extends TestCase
69
private static Logger log = Logger.getLogger(NullInputStreamTestCase.class);
71
private static boolean firstTime = true;
73
protected String host;
75
protected String locatorURI;
76
protected InvokerLocator serverLocator;
77
protected Connector connector;
78
protected Client client;
79
protected TestInvocationHandler invocationHandler;
82
public void setUp() throws Exception
87
Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
88
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
89
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
90
PatternLayout layout = new PatternLayout(pattern);
91
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
92
Logger.getRootLogger().addAppender(consoleAppender);
95
TestUnMarshaller.clear();
99
public void tearDown()
105
if (connector != null)
113
* Tests default behavior with POST method.
115
public void testDefaultBehaviorPost() throws Throwable
117
log.info("entering " + getName());
124
HashMap config = new HashMap();
125
config.put(InvokerLocator.FORCE_REMOTE, "true");
126
HashMap metadata = new HashMap();
127
metadata.put(Client.RAW, "true");
128
metadata.put("TYPE", "POST");
132
log.info("response: " + makeInvocation(config, metadata));
133
fail("expected WebServerError");
135
catch (WebServerError e)
137
log.info("received expected WebServerError");
142
assertTrue(TestUnMarshaller.enteredRead);
143
assertTrue(TestUnMarshaller.streamIsNull);
145
log.info(getName() + " PASSES");
150
* Tests default behavior with HEAD method.
152
public void testDefaultBehaviorHead() throws Throwable
154
log.info("entering " + getName());
161
HashMap config = new HashMap();
162
config.put(InvokerLocator.FORCE_REMOTE, "true");
163
HashMap metadata = new HashMap();
164
metadata.put(Client.RAW, "true");
165
metadata.put("TYPE", "HEAD");
169
log.info("response: " + makeInvocation(config, metadata));
170
fail("expected WebServerError");
172
catch (WebServerError e)
174
log.info("received expected WebServerError");
179
assertTrue(TestUnMarshaller.enteredRead);
180
assertTrue(TestUnMarshaller.streamIsNull);
182
log.info(getName() + " PASSES");
187
* Tests behavior with unmarshallNullStream == true and with POST method.
189
public void testUnmarshalNullStreamTruePost() throws Throwable
191
log.info("entering " + getName());
198
HashMap config = new HashMap();
199
config.put(InvokerLocator.FORCE_REMOTE, "true");
200
config.put(HTTPClientInvoker.UNMARSHAL_NULL_STREAM, "true");
201
HashMap metadata = new HashMap();
202
metadata.put(Client.RAW, "true");
203
metadata.put("TYPE", "POST");
207
log.info("response: " + makeInvocation(config, metadata));
208
fail("expected WebServerError");
210
catch (WebServerError e)
212
log.info("received expected WebServerError");
217
assertTrue(TestUnMarshaller.enteredRead);
218
assertTrue(TestUnMarshaller.streamIsNull);
220
log.info(getName() + " PASSES");
225
* Tests behavior with unmarshallNullStream == true and with HEAD method.
227
public void testUnmarshalNullStreamTrueHead() throws Throwable
229
log.info("entering " + getName());
236
HashMap config = new HashMap();
237
config.put(InvokerLocator.FORCE_REMOTE, "true");
238
config.put(HTTPClientInvoker.UNMARSHAL_NULL_STREAM, "true");
239
HashMap metadata = new HashMap();
240
metadata.put(Client.RAW, "true");
241
metadata.put("TYPE", "HEAD");
245
log.info("response: " + makeInvocation(config, metadata));
246
fail("expected WebServerError");
248
catch (WebServerError e)
250
log.info("received expected WebServerError");
255
assertTrue(TestUnMarshaller.enteredRead);
256
assertTrue(TestUnMarshaller.streamIsNull);
258
log.info(getName() + " PASSES");
263
* Tests behavior with unmarshallNullStream == false and with POST method.
265
public void testUnmarshalNullStreamFalsePost() throws Throwable
267
log.info("entering " + getName());
274
HashMap config = new HashMap();
275
config.put(InvokerLocator.FORCE_REMOTE, "true");
276
config.put(HTTPClientInvoker.UNMARSHAL_NULL_STREAM, "false");
277
HashMap metadata = new HashMap();
278
metadata.put(Client.RAW, "true");
279
metadata.put("TYPE", "POST");
283
log.info("response: " + makeInvocation(config, metadata));
284
fail("expected WebServerError");
286
catch (WebServerError e)
288
log.info("received expected WebServerError");
293
assertFalse(TestUnMarshaller.enteredRead);
295
log.info(getName() + " PASSES");
301
* Tests behavior with unmarshallNullStream == false and with HEAD method.
303
public void testUnmarshalNullStreamFalseHead() throws Throwable
305
log.info("entering " + getName());
312
HashMap config = new HashMap();
313
config.put(InvokerLocator.FORCE_REMOTE, "true");
314
config.put(HTTPClientInvoker.UNMARSHAL_NULL_STREAM, "false");
315
HashMap metadata = new HashMap();
316
metadata.put(Client.RAW, "true");
317
metadata.put("TYPE", "HEAD");
321
log.info("response: " + makeInvocation(config, metadata));
322
fail("expected WebServerError");
324
catch (WebServerError e)
326
log.info("received expected WebServerError");
331
assertFalse(TestUnMarshaller.enteredRead);
333
log.info(getName() + " PASSES");
337
protected Object makeInvocation(HashMap config, HashMap metadata) throws Throwable
340
locatorURI = "http://" + host + ":" + port;
341
locatorURI += "/?unmarshaller=" + TestUnMarshaller.class.getName();
342
log.info("connecting to " + locatorURI);
343
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
344
Client client = new Client(clientLocator, config);
346
log.info("client is connected");
350
return client.invoke("abc", metadata);
352
catch (CannotConnectException e)
354
log.info("exception thrown during invocation: " + e.getMessage());
355
return "CannotConnectException: ok";
360
protected void setupServer() throws Exception
362
log.info("setupServer()");
363
InetAddress localHost = InetAddress.getLocalHost();
364
final ServerSocket ss = new ServerSocket(0, 100, localHost);
365
host = localHost.getHostAddress();
366
port = ss.getLocalPort();
374
Socket s = ss.accept();
375
InputStreamReader ir = new InputStreamReader(s.getInputStream());
376
char[] cbuf = new char[1024];
377
int len = ir.read(cbuf);
378
log.info("Request:");
379
System.out.println();
380
System.out.println(String.copyValueOf(cbuf, 0, len));
381
System.out.println();
382
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
383
dos.writeBytes("HTTP/1.1 500 error" + "\r\n");
384
dos.writeBytes("Server: testServer");
385
dos.writeBytes("Content-Type: text/html" + "\r\n");
386
dos.writeBytes("Content-Length: 0\r\n");
387
dos.writeBytes("Connection: close\r\n");
388
dos.writeBytes("\r\n");
395
catch (EOFException e1)
397
log.info("end of file");
401
log.error("error", e2);
405
log.info("started server");
409
protected void shutdownServer() throws Exception
411
if (connector != null)
416
static class TestInvocationHandler implements ServerInvocationHandler
418
public void addListener(InvokerCallbackHandler callbackHandler) {}
419
public Object invoke(final InvocationRequest invocation) throws Throwable
421
return invocation.getParameter();
423
public void removeListener(InvokerCallbackHandler callbackHandler) {}
424
public void setMBeanServer(MBeanServer server) {}
425
public void setInvoker(ServerInvoker invoker) {}
428
public static class TestUnMarshaller extends HTTPUnMarshaller
430
/** The serialVersionUID */
431
private static final long serialVersionUID = 1L;
433
public static boolean enteredRead;
434
public static boolean streamIsNull;
436
public Object read(InputStream inputStream, Map metadata, int version) throws IOException, ClassNotFoundException
439
streamIsNull = (inputStream == null);
440
log.info("entered TestUnMarshaller.read()");
441
if (inputStream != null)
443
return super.read(inputStream, metadata, version);
451
public UnMarshaller cloneUnMarshaller() throws CloneNotSupportedException
453
HTTPUnMarshaller unmarshaller = new TestUnMarshaller();
454
unmarshaller.setClassLoader(getClassLoader());
458
public static void clear()
461
streamIsNull = false;
b'\\ No newline at end of file'