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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/transport/web/WebInvokerTestClient.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

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
 
 
23
 
package org.jboss.test.remoting.transport.web;
24
 
 
25
 
import java.io.OutputStream;
26
 
import java.net.HttpURLConnection;
27
 
import java.net.URL;
28
 
import java.util.HashMap;
29
 
import java.util.Map;
30
 
import java.util.Properties;
31
 
 
32
 
import javax.net.ssl.HttpsURLConnection;
33
 
 
34
 
import junit.framework.TestCase;
35
 
 
36
 
import org.apache.log4j.Logger;
37
 
import org.jboss.remoting.Client;
38
 
import org.jboss.remoting.InvokerLocator;
39
 
import org.jboss.remoting.transport.http.HTTPMetadataConstants;
40
 
import org.jboss.remoting.transport.http.WebServerError;
41
 
import org.jboss.remoting.transport.web.WebUtil;
42
 
 
43
 
/**
44
 
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
45
 
 */
46
 
public abstract class WebInvokerTestClient extends TestCase
47
 
{
48
 
   protected static Logger log = Logger.getLogger(WebInvokerTestClient.class);
49
 
   
50
 
   public abstract String getLocatorURI();
51
 
 
52
 
   public void testPostInvocation() throws Exception
53
 
   {
54
 
      testPostInvocationSub(true);
55
 
      testPostInvocationSub(false);
56
 
      checkContentType();
57
 
   }
58
 
   
59
 
   /**
60
 
    * If raw == true, set content-type appropriately.
61
 
    * If raw == false, all content will be binary and well be treated as binary.
62
 
    */
63
 
   public void testPostInvocationSub(boolean raw) throws Exception
64
 
   {
65
 
      Client remotingClient = null;
66
 
 
67
 
      try
68
 
      {
69
 
         InvokerLocator locator = new InvokerLocator(getLocatorURI());
70
 
         log.debug("Calling remoting server with locator uri of: " + getLocatorURI());
71
 
 
72
 
         remotingClient = new Client(locator);
73
 
         remotingClient.connect();
74
 
 
75
 
         Map metadata = new HashMap();
76
 
         
77
 
         if (raw)
78
 
         {
79
 
 
80
 
            // The following use of two versions of Client.RAW is to account for the
81
 
            // fact that the value of Client.RAW changed from Remoting 1.4.x to
82
 
            // Remoting 2.0.0.  This test is used as part of the version compatibility
83
 
            // test suite.  Yuck.
84
 
            metadata.put("rawPayload", Boolean.TRUE);
85
 
            metadata.put("RAW_PAYLOAD", Boolean.TRUE);
86
 
         }
87
 
         
88
 
         metadata.put("TYPE", "POST");
89
 
 
90
 
         Properties headerProps = new Properties();
91
 
         
92
 
         if (raw)
93
 
         {
94
 
            headerProps.put(HTTPMetadataConstants.REMOTING_CONTENT_TYPE, HTTPMetadataConstants.REMOTING_CONTENT_TYPE_STRING);
95
 
         }
96
 
         else
97
 
         {
98
 
            headerProps.put("Content-Type", WebUtil.BINARY);
99
 
            headerProps.put(HTTPMetadataConstants.REMOTING_CONTENT_TYPE, HTTPMetadataConstants.REMOTING_CONTENT_TYPE_NON_STRING);
100
 
         }
101
 
         
102
 
         addHeaders(headerProps);
103
 
         metadata.put("HEADER", headerProps);
104
 
         
105
 
         
106
 
         Properties headerProps1 = new Properties(headerProps);
107
 
         HashMap onewayMetadata1 = new HashMap(metadata);
108
 
         onewayMetadata1.put("HEADER", headerProps1);
109
 
         remotingClient.invokeOneway("Do something", onewayMetadata1, true);
110
 
 
111
 
         Properties headerProps2 = new Properties(headerProps);
112
 
         HashMap onewayMetadata2 = new HashMap(metadata);
113
 
         onewayMetadata2.put("HEADER", headerProps2);
114
 
         remotingClient.invokeOneway("Do something", onewayMetadata2, false);
115
 
         Thread.sleep(100);
116
 
 
117
 
         // test with null return expected
118
 
         Object response = null;
119
 
         response = remotingClient.invoke(WebInvocationHandler.NULL_RETURN_PARAM, metadata);
120
 
         log.debug("First response should be null and was: " + response);
121
 
         assertNull(response);
122
 
 
123
 
         response = remotingClient.invoke("Do something", metadata);
124
 
         log.debug("Second response should be " + WebInvocationHandler.HTML_PAGE_RESPONSE + " and was: " + response);
125
 
         assertEquals(WebInvocationHandler.HTML_PAGE_RESPONSE, response);
126
 
 
127
 
         if (raw)
128
 
         {
129
 
            headerProps.put("Content-Type", WebUtil.BINARY);
130
 
         }
131
 
         response = remotingClient.invoke(new ComplexObject(2, "foo", true), metadata);
132
 
         log.debug("Third response should be " + WebInvocationHandler.OBJECT_RESPONSE_VALUE + " and was: " + response);
133
 
         assertEquals(WebInvocationHandler.OBJECT_RESPONSE_VALUE, response);
134
 
 
135
 
         response = remotingClient.invoke(new ComplexObject(2, "foo", true, 3000), metadata);
136
 
         log.debug("Fourth response should be " + WebInvocationHandler.LARGE_OBJECT_RESPONSE_VALUE + " and was: " + response);
137
 
         assertEquals(WebInvocationHandler.LARGE_OBJECT_RESPONSE_VALUE, response);
138
 
 
139
 
         if (raw)
140
 
         {
141
 
            headerProps.put("Content-Type", "application/soap+xml");
142
 
         }
143
 
         response = remotingClient.invoke(WebInvocationHandler.STRING_RETURN_PARAM, metadata);
144
 
         log.debug("Fifth response should be " + WebInvocationHandler.RESPONSE_VALUE + " and was: " + response);
145
 
         assertEquals(WebInvocationHandler.RESPONSE_VALUE, response);
146
 
 
147
 
         checkUserAgent(remotingClient, metadata);
148
 
         
149
 
         makeExceptionInvocation(remotingClient, metadata);
150
 
         
151
 
      }
152
 
      catch (Throwable throwable)
153
 
      {
154
 
         throw new Exception(throwable);
155
 
      }
156
 
      finally
157
 
      {
158
 
         if (remotingClient != null)
159
 
         {
160
 
            remotingClient.disconnect();
161
 
         }
162
 
      }
163
 
   }
164
 
 
165
 
   protected void checkUserAgent(Client remotingClient, Map metadata)
166
 
         throws Throwable
167
 
   {
168
 
      Object response;
169
 
      String remotingUserAgentValue = "JBossRemoting - ";
170
 
      response = remotingClient.invoke(WebInvocationHandler.USER_AGENT_PARAM, metadata);
171
 
      log.debug("Sixth response start with " + remotingUserAgentValue + " and was: " + response);
172
 
      boolean correctUserAgent = ((String) response).startsWith(remotingUserAgentValue);
173
 
      assertTrue("User-Agent should be begin with " + remotingUserAgentValue + " but was " + response, correctUserAgent);
174
 
   }
175
 
 
176
 
   protected void makeExceptionInvocation(Client remotingClient, Map metadata)
177
 
         throws Throwable
178
 
   {
179
 
 
180
 
      Object response = null;
181
 
 
182
 
      try
183
 
      {
184
 
         log.debug("making exception invocation");
185
 
         response = remotingClient.invoke(WebInvocationHandler.THROW_EXCEPTION_PARAM, metadata);
186
 
         assertTrue("Should have thrown WebServerError and not made it to here.", false);
187
 
      }
188
 
      catch (Exception error)
189
 
      {
190
 
         log.debug("exception: " + error + " " + error.getMessage());
191
 
         // having to check class name instead of just catching type WebServerError so
192
 
         // can use for backwards compatibility tests since WebServerError is new since 2.0.0.CR1.
193
 
 
194
 
         if (getLocatorURI().indexOf("dont-return-exception=true") >= 0)
195
 
         {
196
 
            assertTrue("Did not get WebServerError", error instanceof WebServerError);
197
 
            assertNotNull(error.getMessage());
198
 
            assertTrue(error.getMessage().indexOf("Error occurred processing invocation request. ") >= 0);
199
 
         }
200
 
         else if (Boolean.TRUE.equals(metadata.get("rawPayload")) ||
201
 
                  Boolean.TRUE.equals(metadata.get("RAW_PAYLOAD")))
202
 
         {
203
 
            assertTrue("Did not get WebServerError", error instanceof WebServerError);
204
 
            assertNotNull(error.getMessage());
205
 
            log.debug("message: " + error.getMessage());
206
 
            log.debug("message type: " + error.getMessage().getClass());
207
 
            assertTrue(error.getMessage().startsWith("Error received when calling on web server."));
208
 
         }
209
 
         else
210
 
         {
211
 
            assertTrue("Did not get WebTestException", error instanceof WebTestException);
212
 
         }
213
 
      }
214
 
 
215
 
      metadata.put(HTTPMetadataConstants.NO_THROW_ON_ERROR, "true");
216
 
      response = remotingClient.invoke(WebInvocationHandler.THROW_EXCEPTION_PARAM, metadata);
217
 
      if (response instanceof Exception)
218
 
      {
219
 
         log.debug("Return from invocation is of type Exception as expected.");
220
 
         assertTrue("Received exception return as expected.", true);
221
 
      }
222
 
      else
223
 
      {
224
 
         log.info("Did not get Exception type returned as expected.");
225
 
         assertTrue("Should have received Exception as return.", false);
226
 
      }
227
 
      metadata.remove(HTTPMetadataConstants.NO_THROW_ON_ERROR);
228
 
   }
229
 
   
230
 
   protected void checkContentType() throws Exception
231
 
   {
232
 
      log.debug("check_content_type: " + System.getProperty("check_content_type"));
233
 
      String s = System.getProperty("check_content_type", "true");
234
 
      boolean doCheck = Boolean.valueOf(s).booleanValue();
235
 
      if (!doCheck)
236
 
      {
237
 
         log.debug("skipping content type check");
238
 
         return;
239
 
      }
240
 
      
241
 
      String urlString = getLocatorURI();
242
 
      int pos = urlString.indexOf("servlet");
243
 
      if (pos == 0)
244
 
      {
245
 
         urlString = "http" + urlString.substring("servlet".length());
246
 
      }
247
 
      pos = urlString.indexOf("sslservlet");
248
 
      if (pos == 0)
249
 
      {
250
 
         urlString = "https" + urlString.substring("sslservlet".length());
251
 
      }
252
 
      
253
 
      URL url = new URL(urlString);
254
 
      OutputStream os = null;
255
 
      String contentType = null;
256
 
      
257
 
      if (urlString.startsWith("https"))
258
 
      {
259
 
         HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
260
 
         conn.setDoOutput(true);
261
 
         conn.setDoInput(true);
262
 
         conn.addRequestProperty(HTTPMetadataConstants.REMOTING_CONTENT_TYPE, HTTPMetadataConstants.REMOTING_CONTENT_TYPE_STRING);
263
 
         os = conn.getOutputStream();
264
 
         byte[] requestBytes = WebInvocationHandler.SET_CONTENT_TYPE.getBytes();
265
 
         os.write(requestBytes);
266
 
         contentType = conn.getContentType();
267
 
      }
268
 
      else
269
 
      {
270
 
         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
271
 
         conn.setDoOutput(true);
272
 
         conn.setDoInput(true);
273
 
         conn.addRequestProperty(HTTPMetadataConstants.REMOTING_CONTENT_TYPE, HTTPMetadataConstants.REMOTING_CONTENT_TYPE_STRING);
274
 
         os = conn.getOutputStream();
275
 
         byte[] requestBytes = WebInvocationHandler.SET_CONTENT_TYPE.getBytes();
276
 
         os.write(requestBytes);
277
 
         contentType = conn.getContentType();
278
 
      }
279
 
      
280
 
      // Verify that content-type is the value set in the ServerInvocationHandler.
281
 
      log.debug("content-type: " + contentType);
282
 
      assertEquals(WebInvocationHandler.CONTENT_TYPE, contentType);
283
 
   }
284
 
 
285
 
   public void testGetInvocation() throws Exception
286
 
   {
287
 
      testGetInvocationSub(true);
288
 
      testGetInvocationSub(false);
289
 
   }
290
 
   
291
 
   protected void testGetInvocationSub(boolean raw) throws Exception
292
 
   {
293
 
      Client remotingClient = null;
294
 
 
295
 
      try
296
 
      {
297
 
         InvokerLocator locator = new InvokerLocator(getLocatorURI());
298
 
         log.debug("Calling remoting server with locator uri of: " + getLocatorURI());
299
 
 
300
 
         remotingClient = new Client(locator);
301
 
         remotingClient.connect();
302
 
 
303
 
         Map metadata = new HashMap();
304
 
         if (raw)
305
 
         {
306
 
            metadata.put(Client.RAW, Boolean.TRUE);
307
 
         }
308
 
         metadata.put("TYPE", "GET");
309
 
 
310
 
         Object response = null;
311
 
 
312
 
         // test with null return expected
313
 
         response = remotingClient.invoke((Object) null, metadata);
314
 
         log.debug("Response should be " + WebInvocationHandler.HTML_PAGE_RESPONSE + " and was: " + response);
315
 
         assertEquals(WebInvocationHandler.HTML_PAGE_RESPONSE, response);
316
 
 
317
 
         response = remotingClient.invoke((Object) null, metadata);
318
 
         log.debug("Response should be " + WebInvocationHandler.HTML_PAGE_RESPONSE + " and was: " + response);
319
 
         assertEquals(WebInvocationHandler.HTML_PAGE_RESPONSE, response);
320
 
      }
321
 
      catch (Throwable throwable)
322
 
      {
323
 
         throw new Exception(throwable);
324
 
      }
325
 
      finally
326
 
      {
327
 
         if (remotingClient != null)
328
 
         {
329
 
            remotingClient.disconnect();
330
 
         }
331
 
      }
332
 
   }
333
 
 
334
 
   protected void addHeaders(Properties headerProps)
335
 
   {
336
 
      //NO OP - for overriding by sub-classes.
337
 
   }
338
 
}
 
 
b'\\ No newline at end of file'