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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/callback/push/InVMPushCallbackTestCase.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.callback.push;
 
24
 
 
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;
 
35
 
 
36
import javax.management.MBeanServer;
 
37
import java.util.Random;
 
38
 
 
39
/**
 
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.
 
42
 *
 
43
 * @author <a href="mailto:ovidiu@jboss.org">Ovidiu Feodorov</a>
 
44
 * @version <tt>$Revision: 1036 $</tt>
 
45
 */
 
46
public class InVMPushCallbackTestCase extends TestCase
 
47
{
 
48
   // Constants -----------------------------------------------------
 
49
 
 
50
   // Static --------------------------------------------------------
 
51
 
 
52
   // Attributes ----------------------------------------------------
 
53
 
 
54
   protected InvokerLocator targetServerLocator;
 
55
   protected InvokerLocator callbackServerLocator;
 
56
 
 
57
   protected Connector targetServerConnector;
 
58
   protected Connector callbackServerConnector;
 
59
 
 
60
   protected ServerInvocationHandlerImpl targetServerInvocationHandler;
 
61
 
 
62
   protected Client client;
 
63
   protected InvokerCallbackHandlerImpl callbackHandler;
 
64
 
 
65
   // Constructors --------------------------------------------------
 
66
 
 
67
   public InVMPushCallbackTestCase(String name)
 
68
   {
 
69
      super(name);
 
70
   }
 
71
 
 
72
   // TestCase override ---------------------------------------------
 
73
 
 
74
   public void setUp() throws Exception
 
75
   {
 
76
      super.setUp();
 
77
 
 
78
      targetServerLocator = new InvokerLocator("socket://localhost:2323");
 
79
      callbackServerLocator = new InvokerLocator("socket://localhost:3434");
 
80
 
 
81
      targetServerConnector = new Connector();
 
82
      targetServerConnector.setInvokerLocator(targetServerLocator.getLocatorURI());
 
83
      targetServerConnector.start();
 
84
      targetServerInvocationHandler = new ServerInvocationHandlerImpl();
 
85
      targetServerConnector.addInvocationHandler("TARGET", targetServerInvocationHandler);
 
86
 
 
87
      callbackServerConnector = new Connector();
 
88
      callbackServerConnector.setInvokerLocator(callbackServerLocator.getLocatorURI());
 
89
      callbackServerConnector.start();
 
90
      callbackServerConnector.addInvocationHandler("IRRELEVANT", new ServerInvocationHandlerImpl());
 
91
 
 
92
      client = new Client(targetServerLocator);
 
93
      client.connect();
 
94
      callbackHandler = new InvokerCallbackHandlerImpl();
 
95
      try
 
96
      {
 
97
         client.addListener(callbackHandler, callbackServerLocator);
 
98
      }
 
99
      catch(Throwable t)
 
100
      {
 
101
         throw new Exception(t);
 
102
      }
 
103
      client.connect();
 
104
   }
 
105
 
 
106
   public void tearDown() throws Exception
 
107
   {
 
108
      callbackServerConnector.stop();
 
109
      targetServerConnector.stop();
 
110
      client.disconnect();
 
111
 
 
112
      super.tearDown();
 
113
   }
 
114
 
 
115
   // Public --------------------------------------------------------
 
116
 
 
117
 
 
118
   public void testPushCallback() throws Exception
 
119
   {
 
120
      // send callback, the callback handler must receive it
 
121
      Long arg = new Long(new Random().nextLong());
 
122
      targetServerInvocationHandler.sendCallback(arg);
 
123
 
 
124
      assertEquals(arg, callbackHandler.getReceivedArgument());
 
125
   }
 
126
 
 
127
   // Package protected ---------------------------------------------
 
128
 
 
129
   // Protected -----------------------------------------------------
 
130
 
 
131
   // Private -------------------------------------------------------
 
132
 
 
133
   // Inner classes -------------------------------------------------
 
134
 
 
135
   private class ServerInvocationHandlerImpl implements ServerInvocationHandler
 
136
   {
 
137
 
 
138
      private MBeanServer server;
 
139
      private ServerInvoker invoker;
 
140
      private InvokerCallbackHandler theOnlyHandler;
 
141
 
 
142
      // ServerInocationHandler implementation ---------------------
 
143
 
 
144
      public void setMBeanServer(MBeanServer server)
 
145
      {
 
146
         this.server = server;
 
147
      }
 
148
 
 
149
      public void setInvoker(ServerInvoker invoker)
 
150
      {
 
151
         this.invoker = invoker;
 
152
      }
 
153
 
 
154
      public Object invoke(InvocationRequest invocation) throws Throwable
 
155
      {
 
156
         return null;
 
157
      }
 
158
 
 
159
      public void addListener(InvokerCallbackHandler callbackHandler)
 
160
      {
 
161
         theOnlyHandler = callbackHandler;
 
162
      }
 
163
 
 
164
      public void removeListener(InvokerCallbackHandler callbackHandler)
 
165
      {
 
166
         // noop
 
167
      }
 
168
 
 
169
      // Public ---------------------------------------------------
 
170
 
 
171
      public void sendCallback(Object arg) throws Exception
 
172
      {
 
173
         Callback callback = new Callback(arg);
 
174
         theOnlyHandler.handleCallback(callback);
 
175
      }
 
176
 
 
177
   }
 
178
 
 
179
   private class InvokerCallbackHandlerImpl implements InvokerCallbackHandler
 
180
   {
 
181
 
 
182
      Object receivedArg;
 
183
 
 
184
      // InvokerCallbackHandler implementation ---------------------
 
185
 
 
186
      public synchronized void handleCallback(Callback callback)
 
187
            throws HandleCallbackException
 
188
      {
 
189
         receivedArg = callback.getParameter();
 
190
      }
 
191
 
 
192
      // Public ----------------------------------------------------
 
193
 
 
194
      public synchronized Object getReceivedArgument()
 
195
      {
 
196
         return receivedArg;
 
197
      }
 
198
   }
 
199
 
 
200
}