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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/callback/listeners/MultipleListenersTestCase.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
 
 
23
package org.jboss.test.remoting.callback.listeners;
 
24
 
 
25
import java.util.ArrayList;
 
26
import java.util.Iterator;
 
27
import java.util.List;
 
28
import javax.management.MBeanServer;
 
29
import org.jboss.remoting.Client;
 
30
import org.jboss.remoting.InvocationRequest;
 
31
import org.jboss.remoting.InvokerLocator;
 
32
import org.jboss.remoting.ServerInvocationHandler;
 
33
import org.jboss.remoting.ServerInvoker;
 
34
import org.jboss.remoting.callback.Callback;
 
35
import org.jboss.remoting.callback.HandleCallbackException;
 
36
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
37
import org.jboss.remoting.transport.Connector;
 
38
 
 
39
import junit.framework.TestCase;
 
40
 
 
41
/**
 
42
 * This is a test to make sure multiple callback listeners can be added
 
43
 * from the same client and the each gets their callbacks, as well
 
44
 * as making sure they are removed correctly.
 
45
 * <p/>
 
46
 * This test really only works the way it is coded because know that
 
47
 * everything will be run locally, meaning that I can check the
 
48
 * callback values after making in invoke call because the same
 
49
 * thread will make the callbacks before the thread returns from
 
50
 * invoke method.
 
51
 *
 
52
 * @author <a href="mailto:ovidiu@jboss.org">Ovidiu Feodorov</a>
 
53
 * @author <a href="mailto:tom@jboss.org">Ton Elrod</a>
 
54
 * @version <tt>$Revision: 1610 $</tt>
 
55
 */
 
56
public class MultipleListenersTestCase extends TestCase
 
57
{
 
58
   private static List callbacks = new ArrayList();
 
59
 
 
60
   private static String value1 = "FOO";
 
61
   private static String value2 = "BAR";
 
62
   private static String value3 = "FOOBAR";
 
63
 
 
64
   public void testCallbacks() throws Throwable
 
65
   {
 
66
 
 
67
      // Start the callback server
 
68
      InvokerLocator callbackServerLocator = new InvokerLocator("socket://localhost:2222");
 
69
      Connector callbackConnector = new Connector();
 
70
      callbackConnector.setInvokerLocator(callbackServerLocator.getLocatorURI());
 
71
      callbackConnector.start();
 
72
 
 
73
      // Start the target server
 
74
      InvokerLocator targetServerLocator = new InvokerLocator("socket://localhost:3456");
 
75
      Connector connector = new Connector();
 
76
      connector.setInvokerLocator(targetServerLocator.getLocatorURI());
 
77
      connector.create();
 
78
      connector.addInvocationHandler("MySubsystem", new ServerInvocationHandlerImpl());
 
79
      connector.start();
 
80
 
 
81
 
 
82
      Client client = new Client(targetServerLocator);
 
83
      client.connect();
 
84
 
 
85
      InvokerCallbackHandler callbackHandler1 = new InvokerCallbackHandlerImpl("ONE");
 
86
      InvokerCallbackHandler callbackHandler2 = new InvokerCallbackHandlerImpl("TWO");
 
87
 
 
88
      client.addListener(callbackHandler1, callbackServerLocator);
 
89
 
 
90
      client.invoke("call back " + value1);
 
91
 
 
92
      assertEquals(1, callbacks.size());
 
93
      callbacks.clear();
 
94
 
 
95
      client.addListener(callbackHandler2, callbackServerLocator);
 
96
 
 
97
      client.invoke("call back " + value2);
 
98
 
 
99
      assertEquals(2, callbacks.size());
 
100
      callbacks.clear();
 
101
 
 
102
      client.removeListener(callbackHandler1);
 
103
 
 
104
      client.invoke("call back " + value3);
 
105
 
 
106
      assertEquals(1, callbacks.size());
 
107
      callbacks.clear();
 
108
 
 
109
      client.removeListener(callbackHandler2);
 
110
 
 
111
      connector.stop();
 
112
      callbackConnector.stop();
 
113
 
 
114
      connector.destroy();
 
115
      callbackConnector.destroy();
 
116
   }
 
117
 
 
118
 
 
119
   private static class InvokerCallbackHandlerImpl implements InvokerCallbackHandler
 
120
   {
 
121
      private String id;
 
122
 
 
123
      public InvokerCallbackHandlerImpl(String id)
 
124
      {
 
125
         this.id = id;
 
126
      }
 
127
 
 
128
      public void handleCallback(Callback callback) throws HandleCallbackException
 
129
      {
 
130
         System.out.println("Handler " + id + " got callback: " + callback.getParameter());
 
131
         callbacks.add(callback.getParameter());
 
132
         //throw new CustomListenerException("This is the custom listener exception that I want to know about.");
 
133
      }
 
134
   }
 
135
 
 
136
   private static class ServerInvocationHandlerImpl implements ServerInvocationHandler
 
137
   {
 
138
 
 
139
      protected ServerInvoker serverInvoker;
 
140
      protected ArrayList callbackHandlers = new ArrayList();
 
141
 
 
142
      public void setMBeanServer(MBeanServer server)
 
143
      {
 
144
      }
 
145
 
 
146
      public void setInvoker(ServerInvoker invoker)
 
147
      {
 
148
         serverInvoker = invoker;
 
149
      }
 
150
 
 
151
      public Object invoke(InvocationRequest invocation) throws Throwable
 
152
      {
 
153
         String param = (String) invocation.getParameter();
 
154
         System.out.println(param);
 
155
 
 
156
         if(param.startsWith("call back"))
 
157
         {
 
158
            String arg = param.substring("call back".length()).trim();
 
159
            for(Iterator i = callbackHandlers.iterator(); i.hasNext();)
 
160
            {
 
161
               InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) i.next();
 
162
               System.out.println("sending callback to " + callbackHandler);
 
163
               callbackHandler.handleCallback(new Callback(arg));
 
164
            }
 
165
         }
 
166
 
 
167
         return null;
 
168
      }
 
169
 
 
170
      public void addListener(InvokerCallbackHandler callbackHandler)
 
171
      {
 
172
         System.out.println("adding listener " + callbackHandler);
 
173
         callbackHandlers.add(callbackHandler);
 
174
      }
 
175
 
 
176
      public void removeListener(InvokerCallbackHandler callbackHandler)
 
177
      {
 
178
         System.out.println("removing listener " + callbackHandler);
 
179
         callbackHandlers.remove(callbackHandler);
 
180
      }
 
181
   }
 
182
 
 
183
   private static class CustomListenerException extends RuntimeException
 
184
   {
 
185
      public CustomListenerException(String e)
 
186
      {
 
187
         super(e);
 
188
      }
 
189
   }
 
190
 
 
191
}