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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/callback/push/MultipleCallbackServersTestCase.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.push;
 
24
 
 
25
import junit.framework.TestCase;
 
26
import org.jboss.logging.Logger;
 
27
import org.jboss.remoting.Client;
 
28
import org.jboss.remoting.InvocationRequest;
 
29
import org.jboss.remoting.InvokerLocator;
 
30
import org.jboss.remoting.ServerInvocationHandler;
 
31
import org.jboss.remoting.ServerInvoker;
 
32
import org.jboss.remoting.callback.Callback;
 
33
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
34
import org.jboss.remoting.transport.Connector;
 
35
 
 
36
import javax.management.MBeanServer;
 
37
import java.util.ArrayList;
 
38
import java.util.Collections;
 
39
import java.util.List;
 
40
 
 
41
/**
 
42
 * @author <a href="mailto:ovidiu@jboss.org">Ovidiu Feodorov</a>
 
43
 * @version <tt>$Revision: 1036 $</tt>
 
44
 *          <p/>
 
45
 *          $Id: MultipleCallbackServersTestCase.java 1036 2006-05-21 04:47:32Z telrod $
 
46
 */
 
47
public class MultipleCallbackServersTestCase extends TestCase
 
48
{
 
49
   // Constants -----------------------------------------------------
 
50
 
 
51
   private static final Logger log = Logger.getLogger(MultipleCallbackServersTestCase.class);
 
52
 
 
53
   // Static --------------------------------------------------------
 
54
 
 
55
   // Attributes ----------------------------------------------------
 
56
 
 
57
   // Constructors --------------------------------------------------
 
58
 
 
59
   public MultipleCallbackServersTestCase(String name)
 
60
   {
 
61
      super(name);
 
62
   }
 
63
 
 
64
   // Public --------------------------------------------------------
 
65
 
 
66
   public void setUp() throws Exception
 
67
   {
 
68
      super.setUp();
 
69
   }
 
70
 
 
71
   public void tearDown() throws Exception
 
72
   {
 
73
      super.tearDown();
 
74
   }
 
75
 
 
76
   public void testIfDisconnectClearsInvokerRegistry() throws Throwable
 
77
   {
 
78
 
 
79
      String serverlocatorURI = "socket://localhost:5555";
 
80
 
 
81
      Connector server = new Connector();
 
82
      ServerInvocationHandlerImpl serverHandler = new ServerInvocationHandlerImpl();
 
83
      server.setInvokerLocator(serverlocatorURI);
 
84
      server.start();
 
85
      server.addInvocationHandler("TEST", serverHandler);
 
86
 
 
87
      String callbackServer1URI = "socket://localhost:1111";
 
88
      Connector callbackServer1 = new Connector();
 
89
      callbackServer1.setInvokerLocator(callbackServer1URI);
 
90
      callbackServer1.start();
 
91
 
 
92
      String callbackServer2URI = "socket://localhost:2222";
 
93
      Connector callbackServer2 = new Connector();
 
94
      callbackServer2.setInvokerLocator(callbackServer2URI);
 
95
      callbackServer2.start();
 
96
 
 
97
      Client client = new Client(new InvokerLocator(serverlocatorURI), "TEST");
 
98
      client.connect();
 
99
 
 
100
      // add a listener that uses callbackServer1
 
101
      InvokerCallbackHandlerImpl listener1 = new InvokerCallbackHandlerImpl("ONE");
 
102
      client.addListener(listener1, new InvokerLocator(callbackServer1URI));
 
103
 
 
104
      assertEquals(1, serverHandler.size());
 
105
 
 
106
      // add a listener that uses callbackServer2
 
107
      InvokerCallbackHandlerImpl listener2 = new InvokerCallbackHandlerImpl("TWO");
 
108
      client.addListener(listener2, new InvokerLocator(callbackServer2URI));
 
109
 
 
110
      assertEquals(2, serverHandler.size());
 
111
 
 
112
      // remove them
 
113
 
 
114
      /**
 
115
       * Note, if uncomment the following, the test will fail.
 
116
       * This is because each Client has its own session id which
 
117
       * is used as part of the key for each callback listener regsitered
 
118
       * on the server.  Therefore a new Client, means the callback listener
 
119
       * key used for the remove will be different than the original one.
 
120
       */
 
121
      // client = new Client(new InvokerLocator(serverlocatorURI), "TEST");
 
122
 
 
123
      client.removeListener(listener1);
 
124
 
 
125
      assertEquals(1, serverHandler.size());
 
126
 
 
127
      try
 
128
      {
 
129
         client.removeListener(listener1);
 
130
         assertTrue("Expected to get exception for removing same listener twice.", false);
 
131
      }
 
132
      catch(Throwable thr)
 
133
      {
 
134
         assertTrue("Expected to get exception for removing same listener twice.", true);
 
135
      }
 
136
 
 
137
      client.removeListener(listener2);
 
138
 
 
139
      assertEquals(0, serverHandler.size());
 
140
 
 
141
      try
 
142
      {
 
143
         client.removeListener(listener2);
 
144
         assertTrue("Excpected to get exception from removing second listener twice.", false);
 
145
      }
 
146
      catch(Throwable thr)
 
147
      {
 
148
         assertTrue("Expected to get exception from removing second listener twice.", true);
 
149
      }
 
150
 
 
151
   }
 
152
 
 
153
   // Package protected ---------------------------------------------
 
154
 
 
155
   // Protected -----------------------------------------------------
 
156
 
 
157
   // Private -------------------------------------------------------
 
158
 
 
159
   // Inner classes -------------------------------------------------
 
160
 
 
161
   private class InvokerCallbackHandlerImpl implements InvokerCallbackHandler
 
162
   {
 
163
      private String name;
 
164
 
 
165
      public InvokerCallbackHandlerImpl(String name)
 
166
      {
 
167
         this.name = name;
 
168
      }
 
169
 
 
170
      public void handleCallback(Callback c)
 
171
      {
 
172
 
 
173
      }
 
174
 
 
175
      public String getName()
 
176
      {
 
177
         return name;
 
178
      }
 
179
   }
 
180
 
 
181
   private class ServerInvocationHandlerImpl implements ServerInvocationHandler
 
182
   {
 
183
      private List listeners = Collections.synchronizedList(new ArrayList());
 
184
 
 
185
      public void setMBeanServer(MBeanServer server)
 
186
      {
 
187
      }
 
188
 
 
189
      public void setInvoker(ServerInvoker invoker)
 
190
      {
 
191
      }
 
192
 
 
193
      public Object invoke(InvocationRequest invocation) throws Throwable
 
194
      {
 
195
         return null;
 
196
      }
 
197
 
 
198
 
 
199
      public void addListener(InvokerCallbackHandler callbackHandler)
 
200
      {
 
201
         listeners.add(callbackHandler);
 
202
      }
 
203
 
 
204
      public void removeListener(InvokerCallbackHandler callbackHandler)
 
205
      {
 
206
         log.info("Trying to remove listener: " + callbackHandler);
 
207
         boolean removed = listeners.remove(callbackHandler);
 
208
         if(removed)
 
209
         {
 
210
            log.info("Listener removed");
 
211
         }
 
212
         else
 
213
         {
 
214
            log.info("Listener NOT removed");
 
215
         }
 
216
 
 
217
      }
 
218
 
 
219
      public int size()
 
220
      {
 
221
         return listeners.size();
 
222
      }
 
223
   }
 
224
 
 
225
}