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.headers;
24
import java.net.InetAddress;
25
import java.util.Collection;
26
import java.util.HashMap;
27
import java.util.Iterator;
28
import java.util.List;
32
import javax.management.MBeanServer;
34
import junit.framework.TestCase;
36
import org.apache.log4j.ConsoleAppender;
37
import org.apache.log4j.Level;
38
import org.apache.log4j.Logger;
39
import org.apache.log4j.PatternLayout;
40
import org.jboss.remoting.Client;
41
import org.jboss.remoting.InvocationRequest;
42
import org.jboss.remoting.InvokerLocator;
43
import org.jboss.remoting.ServerInvocationHandler;
44
import org.jboss.remoting.ServerInvoker;
45
import org.jboss.remoting.callback.Callback;
46
import org.jboss.remoting.callback.HandleCallbackException;
47
import org.jboss.remoting.callback.InvokerCallbackHandler;
48
import org.jboss.remoting.transport.Connector;
49
import org.jboss.remoting.transport.PortUtil;
50
import org.jboss.remoting.transport.http.HTTPMetadataConstants;
53
* Unit test for JBREM-728.
55
* @author <a href="mailto:ron.sigal@jboss.com">Ron Sigal</a>
57
* Copyright (c) November 24, 2007
61
public class ResponseCodeTestCase extends TestCase
63
private static Logger log = Logger.getLogger(ResponseCodeTestCase.class);
65
private static boolean firstTime = true;
67
protected static String RESPONSE_KEY = "responseKey";
68
protected static String RESPONSE_VALUE = "responseValue";
69
protected static String RETURN_RESPONSE_HEADER = "returnResponseHeader";
71
protected String host;
73
protected String locatorURI;
74
protected InvokerLocator serverLocator;
75
protected Connector connector;
76
protected TestInvocationHandler invocationHandler;
79
public void setUp() throws Exception
84
Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
85
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
86
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
87
PatternLayout layout = new PatternLayout(pattern);
88
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
89
Logger.getRootLogger().addAppender(consoleAppender);
94
public void tearDown()
99
public void testResponseHeaders() throws Throwable
101
log.info("entering " + getName());
107
InvokerLocator clientLocator1 = new InvokerLocator(locatorURI);
108
HashMap clientConfig = new HashMap();
109
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
110
addExtraClientConfig(clientConfig);
111
Client client = new Client(clientLocator1, clientConfig);
113
log.info("client is connected");
116
HashMap metadata = new HashMap();
117
assertEquals(RETURN_RESPONSE_HEADER, client.invoke(RETURN_RESPONSE_HEADER, metadata));
118
log.info("connection is good");
119
log.info("metadata: " + metadata);
121
// Verify that all entries in the HTTPMetadataConstants.RESPONSE_HEADERS value
122
// also appear in metadata map.
123
Object o = metadata.remove(HTTPMetadataConstants.RESPONSE_HEADERS);
124
assertTrue(o instanceof Map);
125
Map responseHeaders = (Map) o;
126
Set responseHeadersEntries = responseHeaders.entrySet();
127
Set metadataEntries = metadata.entrySet();
129
Iterator it = responseHeadersEntries.iterator();
132
Map.Entry entry = (Map.Entry) it.next();
133
log.info("entry: " + entry);
134
if (entry.getKey() == null)
136
assertTrue("metadata does not contain entry " + entry, metadataEntries.contains(entry));
139
// Verify response map entry generated in invocation handler
140
// was added as a response header.
141
assertTrue(responseHeaders.keySet().contains(RESPONSE_KEY));
142
assertTrue(responseHeaders.get(RESPONSE_KEY) instanceof Collection);
143
List responses = (List) responseHeaders.get(RESPONSE_KEY);
144
assertTrue(responses.size() == 1);
145
assertEquals(RESPONSE_VALUE, responses.get(0));
149
log.info(getName() + " PASSES");
153
protected String getTransport()
159
protected void addExtraClientConfig(Map config) {}
160
protected void addExtraServerConfig(Map config) {}
163
protected void setupServer() throws Exception
165
host = InetAddress.getLocalHost().getHostAddress();
166
port = PortUtil.findFreePort(host);
167
locatorURI = getTransport() + "://" + host + ":" + port;
168
serverLocator = new InvokerLocator(locatorURI);
169
log.info("Starting remoting server with locator uri of: " + locatorURI);
170
HashMap config = new HashMap();
171
config.put(InvokerLocator.FORCE_REMOTE, "true");
172
addExtraServerConfig(config);
173
connector = new Connector(serverLocator, config);
175
invocationHandler = new TestInvocationHandler();
176
connector.addInvocationHandler("test", invocationHandler);
183
protected void shutdownServer() throws Exception
185
if (connector != null)
190
static class TestInvocationHandler implements ServerInvocationHandler
192
public void addListener(InvokerCallbackHandler callbackHandler) {}
193
public Object invoke(final InvocationRequest invocation) throws Throwable
195
if (RETURN_RESPONSE_HEADER.equals(invocation.getParameter()))
196
invocation.getReturnPayload().put(RESPONSE_KEY, RESPONSE_VALUE);
197
return invocation.getParameter();
199
public void removeListener(InvokerCallbackHandler callbackHandler) {}
200
public void setMBeanServer(MBeanServer server) {}
201
public void setInvoker(ServerInvoker invoker) {}
205
static class TestCallbackHandler implements InvokerCallbackHandler
207
public void handleCallback(Callback callback) throws HandleCallbackException
209
log.info("received callback");
b'\\ No newline at end of file'