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.
23
package org.jboss.test.remoting.callback.push;
25
import junit.framework.TestCase;
26
import org.jboss.remoting.Client;
27
import org.jboss.remoting.InvocationRequest;
28
import org.jboss.remoting.InvokerLocator;
29
import org.jboss.remoting.ServerInvocationHandler;
30
import org.jboss.remoting.ServerInvoker;
31
import org.jboss.remoting.callback.Callback;
32
import org.jboss.remoting.callback.HandleCallbackException;
33
import org.jboss.remoting.callback.InvokerCallbackHandler;
34
import org.jboss.remoting.transport.Connector;
36
import javax.management.MBeanServer;
37
import java.util.Random;
40
* Tests a push callback in the situation when the client, target server and callback server are in
41
* the same VM. No need for DistributedTestCase.
43
* @author <a href="mailto:ovidiu@jboss.org">Ovidiu Feodorov</a>
44
* @version <tt>$Revision: 1036 $</tt>
46
public class InVMPushCallbackTestCase extends TestCase
48
// Constants -----------------------------------------------------
50
// Static --------------------------------------------------------
52
// Attributes ----------------------------------------------------
54
protected InvokerLocator targetServerLocator;
55
protected InvokerLocator callbackServerLocator;
57
protected Connector targetServerConnector;
58
protected Connector callbackServerConnector;
60
protected ServerInvocationHandlerImpl targetServerInvocationHandler;
62
protected Client client;
63
protected InvokerCallbackHandlerImpl callbackHandler;
65
// Constructors --------------------------------------------------
67
public InVMPushCallbackTestCase(String name)
72
// TestCase override ---------------------------------------------
74
public void setUp() throws Exception
78
targetServerLocator = new InvokerLocator("socket://localhost:2323");
79
callbackServerLocator = new InvokerLocator("socket://localhost:3434");
81
targetServerConnector = new Connector();
82
targetServerConnector.setInvokerLocator(targetServerLocator.getLocatorURI());
83
targetServerConnector.start();
84
targetServerInvocationHandler = new ServerInvocationHandlerImpl();
85
targetServerConnector.addInvocationHandler("TARGET", targetServerInvocationHandler);
87
callbackServerConnector = new Connector();
88
callbackServerConnector.setInvokerLocator(callbackServerLocator.getLocatorURI());
89
callbackServerConnector.start();
90
callbackServerConnector.addInvocationHandler("IRRELEVANT", new ServerInvocationHandlerImpl());
92
client = new Client(targetServerLocator);
94
callbackHandler = new InvokerCallbackHandlerImpl();
97
client.addListener(callbackHandler, callbackServerLocator);
101
throw new Exception(t);
106
public void tearDown() throws Exception
108
callbackServerConnector.stop();
109
targetServerConnector.stop();
115
// Public --------------------------------------------------------
118
public void testPushCallback() throws Exception
120
// send callback, the callback handler must receive it
121
Long arg = new Long(new Random().nextLong());
122
targetServerInvocationHandler.sendCallback(arg);
124
assertEquals(arg, callbackHandler.getReceivedArgument());
127
// Package protected ---------------------------------------------
129
// Protected -----------------------------------------------------
131
// Private -------------------------------------------------------
133
// Inner classes -------------------------------------------------
135
private class ServerInvocationHandlerImpl implements ServerInvocationHandler
138
private MBeanServer server;
139
private ServerInvoker invoker;
140
private InvokerCallbackHandler theOnlyHandler;
142
// ServerInocationHandler implementation ---------------------
144
public void setMBeanServer(MBeanServer server)
146
this.server = server;
149
public void setInvoker(ServerInvoker invoker)
151
this.invoker = invoker;
154
public Object invoke(InvocationRequest invocation) throws Throwable
159
public void addListener(InvokerCallbackHandler callbackHandler)
161
theOnlyHandler = callbackHandler;
164
public void removeListener(InvokerCallbackHandler callbackHandler)
169
// Public ---------------------------------------------------
171
public void sendCallback(Object arg) throws Exception
173
Callback callback = new Callback(arg);
174
theOnlyHandler.handleCallback(callback);
179
private class InvokerCallbackHandlerImpl implements InvokerCallbackHandler
184
// InvokerCallbackHandler implementation ---------------------
186
public synchronized void handleCallback(Callback callback)
187
throws HandleCallbackException
189
receivedArg = callback.getParameter();
192
// Public ----------------------------------------------------
194
public synchronized Object getReceivedArgument()