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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/client/ClientSerializationTestCase.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
 
package org.jboss.test.remoting.client;
23
 
 
24
 
import java.net.InetAddress;
25
 
import java.util.HashMap;
26
 
import java.util.Map;
27
 
 
28
 
import javax.management.MBeanServer;
29
 
 
30
 
import junit.framework.TestCase;
31
 
 
32
 
import org.apache.log4j.ConsoleAppender;
33
 
import org.apache.log4j.Level;
34
 
import org.apache.log4j.Logger;
35
 
import org.apache.log4j.PatternLayout;
36
 
import org.jboss.remoting.Client;
37
 
import org.jboss.remoting.InvocationRequest;
38
 
import org.jboss.remoting.InvokerLocator;
39
 
import org.jboss.remoting.ServerInvocationHandler;
40
 
import org.jboss.remoting.ServerInvoker;
41
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
42
 
import org.jboss.remoting.transport.Connector;
43
 
import org.jboss.remoting.transport.PortUtil;
44
 
 
45
 
/**
46
 
 * ClientSerializationTestCase verifies that org.jboss.remoting.Client can be
47
 
 * serialized.
48
 
 * 
49
 
 * See JBREM-708: http://jira.jboss.org/jira/browse/JBREM-708
50
 
 * 
51
 
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
52
 
 * @version $Revision: 2225 $
53
 
 * <p>
54
 
 * Copyright Feb 23, 2007
55
 
 * </p>
56
 
 */
57
 
public class ClientSerializationTestCase extends TestCase
58
 
{
59
 
   protected static Logger log = Logger.getLogger(ClientSerializationTestCase.class);
60
 
   protected static boolean firstTime = true;
61
 
   
62
 
   protected static final String TEST = "test";
63
 
   protected static final String RETURN_CLIENT = "returnClient";
64
 
   
65
 
   protected InvokerLocator locator;
66
 
   protected Connector connector;
67
 
   protected Client client;
68
 
   
69
 
   
70
 
   public void setUp() throws Exception
71
 
   {
72
 
      if (firstTime)
73
 
      {
74
 
         firstTime = false;
75
 
         Logger.getLogger("org.jboss.remoting").setLevel(Level.DEBUG);
76
 
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.DEBUG);
77
 
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
78
 
         PatternLayout layout = new PatternLayout(pattern);
79
 
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
80
 
         Logger.getRootLogger().addAppender(consoleAppender);  
81
 
      }
82
 
   }
83
 
 
84
 
   
85
 
   public void tearDown() throws Exception
86
 
   {
87
 
      client.disconnect();
88
 
      connector.stop();
89
 
   }
90
 
   
91
 
   
92
 
   /**
93
 
    * Calling client.invoke(RETURN_CLIENT) will cause the invoker handler to return
94
 
    * a Client.  This method uses java serialization.
95
 
    */
96
 
   public void testJavaSerialization() throws Throwable
97
 
   {
98
 
      log.info("entering " + getName());
99
 
      init("java");
100
 
      assertEquals(TEST, client.invoke(TEST));
101
 
      Object response = client.invoke(RETURN_CLIENT);
102
 
      assertTrue(response instanceof Client);
103
 
      log.info("received Client");
104
 
      Client returnedClient = (Client) response;
105
 
      assertEquals(TEST, returnedClient.invoke(TEST));
106
 
      log.info(getName() + " PASSES");
107
 
   }
108
 
   
109
 
   
110
 
   /**
111
 
    * Calling client.invoke(RETURN_CLIENT) will cause the invoker handler to return
112
 
    * a Client.  This method uses jboss serialization.
113
 
    */
114
 
   public void testJBossSerialization() throws Throwable
115
 
   {
116
 
      log.info("entering " + getName());
117
 
      init("jboss");
118
 
      assertEquals(TEST, client.invoke(TEST));
119
 
      Object response = client.invoke(RETURN_CLIENT);
120
 
      assertTrue(response instanceof Client);
121
 
      log.info("received Client");
122
 
      Client returnedClient = (Client) response;
123
 
      assertEquals(TEST, returnedClient.invoke(TEST));
124
 
      log.info(getName() + " PASSES");
125
 
   }
126
 
   
127
 
   
128
 
   /**
129
 
    * Create a Connector and a Client.
130
 
    */
131
 
   protected void init(String serializationType) throws Exception
132
 
   {
133
 
      String host = InetAddress.getLocalHost().getHostAddress();
134
 
      int port = PortUtil.findFreePort(host);
135
 
      String locatorURI = getTransport() + "://" + host + ":" + port;
136
 
      locatorURI += "/?serializationtype=" + serializationType;
137
 
      log.info("server locator: " + locatorURI);
138
 
      locator = new InvokerLocator(locatorURI);
139
 
      HashMap serverConfig = new HashMap();
140
 
      addServerConfig(serverConfig);
141
 
      connector = new Connector(locator, serverConfig);
142
 
      connector.create();
143
 
      connector.addInvocationHandler("test", new TestHandler());
144
 
      connector.start();
145
 
      
146
 
      HashMap clientConfig = new HashMap();
147
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
148
 
      addClientConfig(clientConfig);
149
 
      client = new Client(locator, clientConfig);
150
 
      client.connect();
151
 
   }
152
 
   
153
 
   
154
 
   protected String getTransport()
155
 
   {
156
 
      return "socket";
157
 
   }
158
 
   
159
 
   
160
 
   protected void addServerConfig(Map config)
161
 
   {  
162
 
   }
163
 
   
164
 
   
165
 
   protected void addClientConfig(Map config)
166
 
   {  
167
 
   }
168
 
   
169
 
   
170
 
   public class TestHandler implements ServerInvocationHandler
171
 
   {
172
 
 
173
 
      public void setMBeanServer(MBeanServer server) {}
174
 
      public void setInvoker(ServerInvoker invoker) {}
175
 
 
176
 
      public Object invoke(InvocationRequest invocation) throws Throwable
177
 
      {
178
 
         String command = (String) invocation.getParameter();
179
 
         if (TEST.equals(command))
180
 
         {
181
 
            return command;
182
 
         }
183
 
         else if (RETURN_CLIENT.equals(command))
184
 
         {
185
 
            HashMap config = new HashMap();
186
 
            config.put(InvokerLocator.FORCE_REMOTE, "true");
187
 
            Client client = new Client(locator, config);
188
 
            client.connect();
189
 
            
190
 
            if (TEST.equals(client.invoke(TEST)))
191
 
               return client;
192
 
            else
193
 
               throw new Exception("unable to create working client");
194
 
         }
195
 
         else
196
 
         {
197
 
            log.error("unrecognized command: " + command);
198
 
            throw new Exception("unrecognized command: " + command);
199
 
         }
200
 
      }
201
 
 
202
 
      public void addListener(InvokerCallbackHandler callbackHandler) {}
203
 
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
204
 
   }
205
 
}