~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/ListenerRemovedTestCase.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: 566 $</tt>
 
55
 */
 
56
public class ListenerRemovedTestCase extends TestCase
 
57
{
 
58
   private static List callbacks = new ArrayList();
 
59
 
 
60
   public void testCallbacks() throws Throwable
 
61
   {
 
62
 
 
63
      // Start the callback server
 
64
      InvokerLocator callbackServerLocator = new InvokerLocator("socket://localhost:2222");
 
65
      Connector callbackConnector = new Connector();
 
66
      callbackConnector.setInvokerLocator(callbackServerLocator.getLocatorURI());
 
67
      callbackConnector.start();
 
68
 
 
69
      // Start the target server
 
70
      InvokerLocator targetServerLocator = new InvokerLocator("socket://localhost:3456");
 
71
      Connector connector = new Connector();
 
72
      connector.setInvokerLocator(targetServerLocator.getLocatorURI());
 
73
      connector.create();
 
74
      connector.addInvocationHandler("MySubsystem", new ServerInvocationHandlerImpl());
 
75
      connector.start();
 
76
 
 
77
 
 
78
      Client client = new Client(targetServerLocator);
 
79
      client.connect();
 
80
 
 
81
      InvokerCallbackHandler callbackHandler1 = new InvokerCallbackHandlerImpl("ONE");
 
82
 
 
83
      client.addListener(callbackHandler1, callbackServerLocator);
 
84
 
 
85
      client.removeListener(callbackHandler1);
 
86
 
 
87
      try
 
88
      {
 
89
         client.removeListener(callbackHandler1);
 
90
         assertTrue("Should throw an exception if remove a callback handler that has already been removed.", false);
 
91
      }
 
92
      catch(Throwable t)
 
93
      {
 
94
         assertTrue("Got exception from server as expected.", true);
 
95
      }
 
96
      finally
 
97
      {
 
98
         connector.stop();
 
99
         callbackConnector.stop();
 
100
 
 
101
         connector.destroy();
 
102
         callbackConnector.destroy();
 
103
      }
 
104
   }
 
105
 
 
106
 
 
107
   private static class InvokerCallbackHandlerImpl implements InvokerCallbackHandler
 
108
   {
 
109
      private String id;
 
110
 
 
111
      public InvokerCallbackHandlerImpl(String id)
 
112
      {
 
113
         this.id = id;
 
114
      }
 
115
 
 
116
      public void handleCallback(Callback callback) throws HandleCallbackException
 
117
      {
 
118
         System.out.println("Handler " + id + " got callback: " + callback.getParameter());
 
119
         callbacks.add(callback.getParameter());
 
120
         //throw new CustomListenerException("This is the custom listener exception that I want to know about.");
 
121
      }
 
122
   }
 
123
 
 
124
   private static class ServerInvocationHandlerImpl implements ServerInvocationHandler
 
125
   {
 
126
 
 
127
      protected ServerInvoker serverInvoker;
 
128
      protected ArrayList callbackHandlers = new ArrayList();
 
129
 
 
130
      public void setMBeanServer(MBeanServer server)
 
131
      {
 
132
      }
 
133
 
 
134
      public void setInvoker(ServerInvoker invoker)
 
135
      {
 
136
         serverInvoker = invoker;
 
137
      }
 
138
 
 
139
      public Object invoke(InvocationRequest invocation) throws Throwable
 
140
      {
 
141
         String param = (String) invocation.getParameter();
 
142
         System.out.println(param);
 
143
 
 
144
         if(param.startsWith("call back"))
 
145
         {
 
146
            String arg = param.substring("call back".length()).trim();
 
147
            for(Iterator i = callbackHandlers.iterator(); i.hasNext();)
 
148
            {
 
149
               InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) i.next();
 
150
               callbackHandler.handleCallback(new Callback(arg));
 
151
            }
 
152
         }
 
153
 
 
154
         return null;
 
155
      }
 
156
 
 
157
      public void addListener(InvokerCallbackHandler callbackHandler)
 
158
      {
 
159
         System.out.println("adding listener " + callbackHandler);
 
160
         callbackHandlers.add(callbackHandler);
 
161
      }
 
162
 
 
163
      public void removeListener(InvokerCallbackHandler callbackHandler)
 
164
      {
 
165
         callbackHandlers.remove(callbackHandler);
 
166
      }
 
167
   }
 
168
 
 
169
   private static class CustomListenerException extends RuntimeException
 
170
   {
 
171
      public CustomListenerException(String e)
 
172
      {
 
173
         super(e);
 
174
      }
 
175
   }
 
176
 
 
177
}