~ubuntu-branches/ubuntu/oneiric/libjboss-remoting-java/oneiric

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/transport/http/NullInputStreamTestCase.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
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.
6
 
*
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.
11
 
*
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.
16
 
*
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.
21
 
*/
22
 
package org.jboss.test.remoting.transport.http;
23
 
 
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;
33
 
import java.util.Map;
34
 
 
35
 
import javax.management.MBeanServer;
36
 
 
37
 
import junit.framework.TestCase;
38
 
 
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;
56
 
 
57
 
 
58
 
/**
59
 
 * Unit test for JBREM-1046.
60
 
 * 
61
 
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
62
 
 * @version $Revision: 1.1 $
63
 
 * <p>
64
 
 * Copyright Oct 27, 2008
65
 
 * </p>
66
 
 */
67
 
public class NullInputStreamTestCase extends TestCase
68
 
{
69
 
   private static Logger log = Logger.getLogger(NullInputStreamTestCase.class);
70
 
   
71
 
   private static boolean firstTime = true;
72
 
   
73
 
   protected String host;
74
 
   protected int port;
75
 
   protected String locatorURI;
76
 
   protected InvokerLocator serverLocator;
77
 
   protected Connector connector;
78
 
   protected Client client;
79
 
   protected TestInvocationHandler invocationHandler;
80
 
 
81
 
   
82
 
   public void setUp() throws Exception
83
 
   {
84
 
      if (firstTime)
85
 
      {
86
 
         firstTime = false;
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);  
93
 
      }
94
 
      
95
 
      TestUnMarshaller.clear();
96
 
   }
97
 
 
98
 
   
99
 
   public void tearDown()
100
 
   {
101
 
      if (client != null)
102
 
      {
103
 
         client.disconnect();
104
 
      }
105
 
      if (connector != null)
106
 
      {
107
 
         connector.destroy();
108
 
      }
109
 
   }
110
 
   
111
 
   
112
 
   /**
113
 
    * Tests default behavior with POST method.
114
 
    */
115
 
   public void testDefaultBehaviorPost() throws Throwable
116
 
   {
117
 
      log.info("entering " + getName());
118
 
      
119
 
      // Start server.
120
 
      setupServer();
121
 
      boolean ok = false; 
122
 
 
123
 
 
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");
129
 
      
130
 
      try
131
 
      {
132
 
         log.info("response: " + makeInvocation(config, metadata));
133
 
         fail("expected WebServerError");
134
 
      }
135
 
      catch (WebServerError e)
136
 
      {
137
 
         log.info("received expected WebServerError");
138
 
         ok = true;
139
 
      }
140
 
      
141
 
      assertTrue(ok);
142
 
      assertTrue(TestUnMarshaller.enteredRead);
143
 
      assertTrue(TestUnMarshaller.streamIsNull);
144
 
 
145
 
      log.info(getName() + " PASSES");
146
 
   }
147
 
   
148
 
   
149
 
   /**
150
 
    * Tests default behavior with HEAD method.
151
 
    */
152
 
   public void testDefaultBehaviorHead() throws Throwable
153
 
   {
154
 
      log.info("entering " + getName());
155
 
      
156
 
      // Start server.
157
 
      setupServer();
158
 
      boolean ok = false; 
159
 
 
160
 
 
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");
166
 
      
167
 
      try
168
 
      {
169
 
         log.info("response: " + makeInvocation(config, metadata));
170
 
         fail("expected WebServerError");
171
 
      }
172
 
      catch (WebServerError e)
173
 
      {
174
 
         log.info("received expected WebServerError");
175
 
         ok = true;
176
 
      }
177
 
      
178
 
      assertTrue(ok);
179
 
      assertTrue(TestUnMarshaller.enteredRead);
180
 
      assertTrue(TestUnMarshaller.streamIsNull);
181
 
 
182
 
      log.info(getName() + " PASSES");
183
 
   }
184
 
   
185
 
   
186
 
   /**
187
 
    * Tests behavior with unmarshallNullStream == true and with POST method.
188
 
    */
189
 
   public void testUnmarshalNullStreamTruePost() throws Throwable
190
 
   {
191
 
      log.info("entering " + getName());
192
 
      
193
 
      // Start server.
194
 
      setupServer();
195
 
      boolean ok = false; 
196
 
 
197
 
 
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");
204
 
      
205
 
      try
206
 
      {
207
 
         log.info("response: " + makeInvocation(config, metadata));
208
 
         fail("expected WebServerError");
209
 
      }
210
 
      catch (WebServerError e)
211
 
      {
212
 
         log.info("received expected WebServerError");
213
 
         ok = true;
214
 
      }
215
 
      
216
 
      assertTrue(ok);
217
 
      assertTrue(TestUnMarshaller.enteredRead);
218
 
      assertTrue(TestUnMarshaller.streamIsNull);
219
 
 
220
 
      log.info(getName() + " PASSES");
221
 
   }
222
 
   
223
 
   
224
 
   /**
225
 
    * Tests behavior with unmarshallNullStream == true and with HEAD method.
226
 
    */
227
 
   public void testUnmarshalNullStreamTrueHead() throws Throwable
228
 
   {
229
 
      log.info("entering " + getName());
230
 
      
231
 
      // Start server.
232
 
      setupServer();
233
 
      boolean ok = false; 
234
 
 
235
 
 
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");
242
 
      
243
 
      try
244
 
      {
245
 
         log.info("response: " + makeInvocation(config, metadata));
246
 
         fail("expected WebServerError");
247
 
      }
248
 
      catch (WebServerError e)
249
 
      {
250
 
         log.info("received expected WebServerError");
251
 
         ok = true;
252
 
      }
253
 
      
254
 
      assertTrue(ok);
255
 
      assertTrue(TestUnMarshaller.enteredRead);
256
 
      assertTrue(TestUnMarshaller.streamIsNull);
257
 
 
258
 
      log.info(getName() + " PASSES");
259
 
   }
260
 
   
261
 
   
262
 
   /**
263
 
    * Tests behavior with unmarshallNullStream == false and with POST method.
264
 
    */
265
 
   public void testUnmarshalNullStreamFalsePost() throws Throwable
266
 
   {
267
 
      log.info("entering " + getName());
268
 
      
269
 
      // Start server.
270
 
      setupServer();
271
 
      boolean ok = false; 
272
 
 
273
 
 
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");
280
 
      
281
 
      try
282
 
      {
283
 
         log.info("response: " + makeInvocation(config, metadata));
284
 
         fail("expected WebServerError");
285
 
      }
286
 
      catch (WebServerError e)
287
 
      {
288
 
         log.info("received expected WebServerError");
289
 
         ok = true;
290
 
      }
291
 
      
292
 
      assertTrue(ok);
293
 
      assertFalse(TestUnMarshaller.enteredRead);
294
 
 
295
 
      log.info(getName() + " PASSES");
296
 
   }
297
 
   
298
 
   
299
 
   
300
 
   /**
301
 
    * Tests behavior with unmarshallNullStream == false and with HEAD method.
302
 
    */
303
 
   public void testUnmarshalNullStreamFalseHead() throws Throwable
304
 
   {
305
 
      log.info("entering " + getName());
306
 
      
307
 
      // Start server.
308
 
      setupServer();
309
 
      boolean ok = false; 
310
 
 
311
 
 
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");
318
 
      
319
 
      try
320
 
      {
321
 
         log.info("response: " + makeInvocation(config, metadata));
322
 
         fail("expected WebServerError");
323
 
      }
324
 
      catch (WebServerError e)
325
 
      {
326
 
         log.info("received expected WebServerError");
327
 
         ok = true;
328
 
      }
329
 
      
330
 
      assertTrue(ok);
331
 
      assertFalse(TestUnMarshaller.enteredRead);
332
 
 
333
 
      log.info(getName() + " PASSES");
334
 
   }
335
 
   
336
 
   
337
 
   protected Object makeInvocation(HashMap config, HashMap metadata) throws Throwable
338
 
   {
339
 
      // Create client.
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);
345
 
      client.connect();
346
 
      log.info("client is connected");
347
 
      
348
 
      try
349
 
      {
350
 
         return client.invoke("abc", metadata);
351
 
      }
352
 
      catch (CannotConnectException e)
353
 
      {
354
 
         log.info("exception thrown during invocation: " + e.getMessage());
355
 
         return "CannotConnectException: ok";
356
 
      }
357
 
   }
358
 
   
359
 
 
360
 
   protected void setupServer() throws Exception
361
 
   {
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();
367
 
      
368
 
      new Thread()
369
 
      {
370
 
         public void run()
371
 
         {
372
 
            try
373
 
            {
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");
389
 
               
390
 
               ir.close();
391
 
               dos.close();
392
 
               s.close();
393
 
               ss.close();
394
 
            }
395
 
            catch (EOFException e1)
396
 
            {
397
 
               log.info("end of file");
398
 
            } 
399
 
            catch (Exception e2)
400
 
            {
401
 
               log.error("error", e2);
402
 
            }
403
 
         }
404
 
      }.start();
405
 
      log.info("started server");
406
 
   }
407
 
   
408
 
   
409
 
   protected void shutdownServer() throws Exception
410
 
   {
411
 
      if (connector != null)
412
 
         connector.stop();
413
 
   }
414
 
   
415
 
   
416
 
   static class TestInvocationHandler implements ServerInvocationHandler
417
 
   {
418
 
      public void addListener(InvokerCallbackHandler callbackHandler) {}
419
 
      public Object invoke(final InvocationRequest invocation) throws Throwable
420
 
      {
421
 
         return invocation.getParameter();
422
 
      }
423
 
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
424
 
      public void setMBeanServer(MBeanServer server) {}
425
 
      public void setInvoker(ServerInvoker invoker) {}
426
 
   }
427
 
   
428
 
   public static class TestUnMarshaller extends HTTPUnMarshaller
429
 
   {
430
 
      /** The serialVersionUID */
431
 
      private static final long serialVersionUID = 1L;
432
 
 
433
 
      public static boolean enteredRead;
434
 
      public static boolean streamIsNull;
435
 
      
436
 
      public Object read(InputStream inputStream, Map metadata, int version) throws IOException, ClassNotFoundException
437
 
      {
438
 
         enteredRead = true;
439
 
         streamIsNull = (inputStream == null);
440
 
         log.info("entered TestUnMarshaller.read()");
441
 
         if (inputStream != null)
442
 
         {
443
 
            return super.read(inputStream, metadata, version);
444
 
         }
445
 
         else
446
 
         {
447
 
            return null;
448
 
         }
449
 
      }
450
 
      
451
 
      public UnMarshaller cloneUnMarshaller() throws CloneNotSupportedException
452
 
      {
453
 
         HTTPUnMarshaller unmarshaller = new TestUnMarshaller();
454
 
         unmarshaller.setClassLoader(getClassLoader());
455
 
         return unmarshaller;
456
 
      }
457
 
      
458
 
      public static void clear()
459
 
      {
460
 
         enteredRead = false;
461
 
         streamIsNull = false;
462
 
      }
463
 
   }
464
 
}
 
 
b'\\ No newline at end of file'