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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/security/CallbackErrorHandlerProxyTestCase.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
package org.jboss.test.remoting.security;
 
23
 
 
24
import java.net.InetAddress;
 
25
import java.util.HashMap;
 
26
import java.util.Map;
 
27
 
 
28
import javax.management.MBeanServer;
 
29
import javax.management.MBeanServerFactory;
 
30
import javax.management.ObjectName;
 
31
 
 
32
import junit.framework.TestCase;
 
33
 
 
34
import org.apache.log4j.ConsoleAppender;
 
35
import org.apache.log4j.Level;
 
36
import org.apache.log4j.Logger;
 
37
import org.apache.log4j.PatternLayout;
 
38
import org.jboss.logging.XLevel;
 
39
import org.jboss.remoting.Client;
 
40
import org.jboss.remoting.InvocationRequest;
 
41
import org.jboss.remoting.InvokerLocator;
 
42
import org.jboss.remoting.ServerInvocationHandler;
 
43
import org.jboss.remoting.ServerInvoker;
 
44
import org.jboss.remoting.callback.Callback;
 
45
import org.jboss.remoting.callback.HandleCallbackException;
 
46
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
47
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
 
48
import org.jboss.remoting.transport.Connector;
 
49
import org.jboss.remoting.transport.PortUtil;
 
50
 
 
51
 
 
52
/**
 
53
 * Unit test for JBREM-977.
 
54
 * 
 
55
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
56
 * @version $Revision: 1.1 $
 
57
 * <p>
 
58
 * Copyright May 4, 2008
 
59
 * </p>
 
60
 */
 
61
public class CallbackErrorHandlerProxyTestCase extends TestCase
 
62
{
 
63
   private static Logger log = Logger.getLogger(CallbackErrorHandlerProxyTestCase.class);
 
64
   
 
65
   private static boolean firstTime = true;
 
66
   protected String host;
 
67
   protected int port;
 
68
   protected String locatorURI;
 
69
   protected InvokerLocator serverLocator;
 
70
   protected Connector connector;
 
71
   protected TestCallbackErrorHandler errorHandler;
 
72
   protected MBeanServer server;
 
73
   protected ObjectName errorHandlerObjectName;
 
74
 
 
75
   
 
76
   public void setUp() throws Exception
 
77
   {
 
78
      if (firstTime)
 
79
      {
 
80
         firstTime = false;
 
81
         Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
 
82
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
 
83
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
 
84
         PatternLayout layout = new PatternLayout(pattern);
 
85
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
 
86
         Logger.getRootLogger().addAppender(consoleAppender);  
 
87
      }
 
88
   }
 
89
 
 
90
   
 
91
   public void tearDown()
 
92
   {
 
93
   }
 
94
   
 
95
   
 
96
   public void testCallbackErrorHandlerProxy() throws Throwable
 
97
   {
 
98
      log.info("entering " + getName());
 
99
      
 
100
      // Start server.
 
101
      setupServer();
 
102
      log.info("ServerInvocationHandler: " + errorHandler);
 
103
      
 
104
      // Create client.
 
105
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
 
106
      HashMap clientConfig = new HashMap();
 
107
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
108
      addExtraClientConfig(clientConfig);
 
109
      Client client = new Client(clientLocator, clientConfig);
 
110
      client.connect();
 
111
      log.info("client is connected");
 
112
      
 
113
      // Check connection.
 
114
      assertEquals("abc", client.invoke("abc"));
 
115
      log.info("connection is good");
 
116
      
 
117
      // Verify callbacks work.
 
118
      TestCallbackHandler callbackHandler = new TestCallbackHandler();
 
119
      client.addListener(callbackHandler, null, null, true);
 
120
      assertEquals(1, callbackHandler.counter);
 
121
      
 
122
      // Verify CallbackErrorHandler proxy gets used.
 
123
      client.addListener(callbackHandler, null, null, true);
 
124
      assertEquals(1, callbackHandler.counter);
 
125
      assertEquals(1, errorHandler.counter);
 
126
      int counter = ((Integer) server.getAttribute(errorHandlerObjectName, "Counter")).intValue();
 
127
      assertEquals(errorHandler.counter, counter);
 
128
 
 
129
      client.disconnect();
 
130
      shutdownServer();
 
131
      log.info(getName() + " PASSES");
 
132
   }
 
133
   
 
134
   
 
135
   protected String getTransport()
 
136
   {
 
137
      return "socket";
 
138
   }
 
139
   
 
140
   
 
141
   protected void addExtraClientConfig(Map config) {}
 
142
   protected void addExtraServerConfig(Map config) {}
 
143
   
 
144
 
 
145
   protected void setupServer() throws Exception
 
146
   {
 
147
      host = InetAddress.getLocalHost().getHostAddress();
 
148
      port = PortUtil.findFreePort(host);
 
149
      locatorURI = getTransport() + "://" + host + ":" + port; 
 
150
      serverLocator = new InvokerLocator(locatorURI);
 
151
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
152
      HashMap config = new HashMap();
 
153
      config.put(InvokerLocator.FORCE_REMOTE, "true");
 
154
      addExtraServerConfig(config);
 
155
      
 
156
      // Create CallbackErrorHandler.
 
157
      server = MBeanServerFactory.createMBeanServer();
 
158
      errorHandler = new TestCallbackErrorHandler();
 
159
      String objectNameString = "test:type=TestCallbackErrorHandler";
 
160
      errorHandlerObjectName = new ObjectName(objectNameString);
 
161
      server.registerMBean(errorHandler, errorHandlerObjectName);
 
162
      config.put(ServerInvokerCallbackHandler.CALLBACK_ERROR_HANDLER_KEY, objectNameString);
 
163
      
 
164
      // Create Connector..
 
165
      connector = new Connector(serverLocator, config);
 
166
      server.registerMBean(connector, new ObjectName("test:type=Connector"));
 
167
      connector.create();
 
168
      connector.addInvocationHandler("test", new TestServerInvocationHandler());
 
169
      connector.start();
 
170
   }
 
171
   
 
172
   
 
173
   protected void shutdownServer() throws Exception
 
174
   {
 
175
      if (connector != null)
 
176
         connector.stop();
 
177
   }
 
178
   
 
179
   
 
180
   public static class TestServerInvocationHandler implements ServerInvocationHandler
 
181
   {
 
182
      static Logger log = Logger.getLogger(TestServerInvocationHandler.class);
 
183
      private int counter;
 
184
      
 
185
      public void addListener(InvokerCallbackHandler callbackHandler)
 
186
      {
 
187
         try
 
188
         {
 
189
            if (counter++ == 0)
 
190
            {
 
191
               // First time, send callback.
 
192
               callbackHandler.handleCallback(new Callback("callback"));
 
193
            }
 
194
            else
 
195
            {
 
196
               // Next, generate callback exception.
 
197
               callbackHandler.handleCallback(new Callback(new NotSerializable()));
 
198
            }
 
199
         }
 
200
         catch (HandleCallbackException e)
 
201
         {
 
202
            if (counter == 0)
 
203
               log.error("Unexpected exception", e);
 
204
            else
 
205
               log.info("Expected exception: " + e.getMessage());
 
206
         }
 
207
      }
 
208
 
 
209
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
210
      {
 
211
         return invocation.getParameter();
 
212
      }
 
213
      
 
214
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
215
      public void setMBeanServer(MBeanServer server) {}
 
216
      public void setInvoker(ServerInvoker invoker) {}
 
217
      
 
218
      public int getCounter()
 
219
      {
 
220
         return counter;
 
221
      }
 
222
   }
 
223
   
 
224
   
 
225
   static class TestCallbackHandler implements InvokerCallbackHandler
 
226
   {
 
227
      int counter;
 
228
      
 
229
      public void handleCallback(Callback callback) throws HandleCallbackException
 
230
      {
 
231
         counter++;
 
232
         log.info("received callback");
 
233
      }  
 
234
   }
 
235
   
 
236
   static class NotSerializable {}
 
237
}
 
 
b'\\ No newline at end of file'