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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/ipv6/ObjectNameWithIPv6AddressTestCase.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.ipv6;
 
23
 
 
24
import java.net.InetAddress;
 
25
import java.net.ServerSocket;
 
26
import java.security.AccessController;
 
27
import java.security.PrivilegedActionException;
 
28
import java.security.PrivilegedExceptionAction;
 
29
import java.util.HashMap;
 
30
import java.util.Iterator;
 
31
import java.util.Map;
 
32
import java.util.Set;
 
33
 
 
34
import javax.management.MBeanServer;
 
35
import javax.management.MBeanServerFactory;
 
36
import javax.management.ObjectInstance;
 
37
import javax.management.ObjectName;
 
38
 
 
39
import junit.framework.TestCase;
 
40
 
 
41
import org.apache.log4j.ConsoleAppender;
 
42
import org.apache.log4j.Level;
 
43
import org.apache.log4j.Logger;
 
44
import org.apache.log4j.PatternLayout;
 
45
import org.jboss.remoting.Client;
 
46
import org.jboss.remoting.InvocationRequest;
 
47
import org.jboss.remoting.InvokerLocator;
 
48
import org.jboss.remoting.ServerInvocationHandler;
 
49
import org.jboss.remoting.ServerInvoker;
 
50
import org.jboss.remoting.callback.Callback;
 
51
import org.jboss.remoting.callback.HandleCallbackException;
 
52
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
53
import org.jboss.remoting.network.NetworkRegistry;
 
54
import org.jboss.remoting.transport.Connector;
 
55
import org.jboss.remoting.transport.PortUtil;
 
56
 
 
57
 
 
58
public class ObjectNameWithIPv6AddressTestCase extends TestCase
 
59
{
 
60
   private static Logger log = Logger.getLogger(ObjectNameWithIPv6AddressTestCase.class);
 
61
   
 
62
   private static boolean firstTime = true;
 
63
   private static boolean ipv6IsAvailable = true;
 
64
   
 
65
   protected String host;
 
66
   protected int port;
 
67
   protected String locatorURI;
 
68
   protected InvokerLocator serverLocator;
 
69
   protected Connector connector;
 
70
   protected TestInvocationHandler invocationHandler;
 
71
 
 
72
   
 
73
   public void setUp() throws Exception
 
74
   {
 
75
      if (firstTime)
 
76
      {
 
77
         firstTime = false;
 
78
         Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
 
79
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
 
80
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
 
81
         PatternLayout layout = new PatternLayout(pattern);
 
82
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
 
83
         Logger.getRootLogger().addAppender(consoleAppender);
 
84
         log.info("java.version: " + System.getProperty("java.version"));
 
85
         
 
86
         try
 
87
         {
 
88
            InetAddress addr = InetAddress.getByName("[::1]");
 
89
            new ServerSocket(3333, 200, addr);
 
90
         }
 
91
         catch (Exception e)
 
92
         {
 
93
            if ("Protocol family unavailable".equalsIgnoreCase(e.getMessage()) ||
 
94
                "Protocol family not supported".equalsIgnoreCase(e.getMessage()))
 
95
            {
 
96
               ipv6IsAvailable = false;
 
97
               log.info("ipV6 is not available");
 
98
            }
 
99
         }
 
100
      }
 
101
   }
 
102
 
 
103
   
 
104
   public void tearDown()
 
105
   {
 
106
   }
 
107
   
 
108
   
 
109
   public void testIPv6Loopback() throws Throwable
 
110
   {
 
111
      log.info("entering " + getName());
 
112
      doTest("[::1]");
 
113
      log.info(getName() + " PASSES");
 
114
   }
 
115
   
 
116
   
 
117
   public void testIPv6Any() throws Throwable
 
118
   { 
 
119
      log.info("entering " + getName());
 
120
      doTest("[::]");
 
121
      log.info(getName() + " PASSES");
 
122
   }   
 
123
   
 
124
   
 
125
   public void testIPv4Mapped() throws Throwable
 
126
   {
 
127
      log.info("entering " + getName());
 
128
      doTest("[::ffff:127.0.0.1]");
 
129
      log.info(getName() + " PASSES");
 
130
   }
 
131
   
 
132
   
 
133
   protected void doTest(String host) throws Throwable
 
134
   {
 
135
      if (!ipv6IsAvailable) return;
 
136
      
 
137
      MBeanServer server = null;
 
138
      try
 
139
      {
 
140
         server = (MBeanServer) AccessController.doPrivileged( new PrivilegedExceptionAction()
 
141
         {
 
142
            public Object run() throws Exception
 
143
            {
 
144
               return MBeanServerFactory.createMBeanServer();
 
145
            }
 
146
         });
 
147
      }
 
148
      catch (PrivilegedActionException e)
 
149
      {
 
150
         throw (Exception) e.getCause();
 
151
      }
 
152
 
 
153
      Connector connector = new Connector();
 
154
      port = PortUtil.findFreePort(host);
 
155
      locatorURI = getTransport() + "://" + host + ":" + port; 
 
156
      serverLocator = new InvokerLocator(locatorURI);
 
157
      connector.setInvokerLocator(locatorURI);
 
158
      ObjectName obj = new ObjectName("jboss.remoting:type=Connector,transport=" + getTransport());
 
159
      server.registerMBean(connector, obj);
 
160
      connector.create();
 
161
      invocationHandler = new TestInvocationHandler();
 
162
      connector.addInvocationHandler("test", invocationHandler);
 
163
      connector.start();
 
164
      
 
165
      ServerInvoker serverInvoker = connector.getServerInvoker();
 
166
      ObjectName objectName = new ObjectName(serverInvoker.getMBeanObjectName());
 
167
      assertTrue(objectName.getCanonicalName().indexOf(host) >= 0);
 
168
      assertTrue(server.isRegistered(objectName));
 
169
      log.info(objectName + " is registered");
 
170
      
 
171
      // Create client.
 
172
      HashMap clientConfig = new HashMap();
 
173
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
174
      addExtraClientConfig(clientConfig);
 
175
      Client client = new Client(serverLocator, clientConfig);
 
176
      client.connect();
 
177
      log.info("client is connected");
 
178
      
 
179
      // Test connections.
 
180
      assertEquals("abc", client.invoke("abc"));
 
181
      log.info("connection is good");
 
182
      
 
183
      client.disconnect();
 
184
      shutdownServer();
 
185
   }
 
186
   
 
187
   
 
188
   protected String getTransport()
 
189
   {
 
190
      return "socket";
 
191
   }
 
192
   
 
193
   
 
194
   protected void addExtraClientConfig(Map config) {}
 
195
   protected void addExtraServerConfig(Map config) {}
 
196
   
 
197
 
 
198
   protected void setupServer() throws Exception
 
199
   {
 
200
      host = InetAddress.getLocalHost().getHostAddress();
 
201
      port = PortUtil.findFreePort(host);
 
202
      locatorURI = getTransport() + "://" + host + ":" + port; 
 
203
      serverLocator = new InvokerLocator(locatorURI);
 
204
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
205
      HashMap config = new HashMap();
 
206
      config.put(InvokerLocator.FORCE_REMOTE, "true");
 
207
      addExtraServerConfig(config);
 
208
      connector = new Connector(serverLocator, config);
 
209
      connector.create();
 
210
      invocationHandler = new TestInvocationHandler();
 
211
      connector.addInvocationHandler("test", invocationHandler);
 
212
      connector.start();
 
213
   }
 
214
   
 
215
   
 
216
   protected void shutdownServer() throws Exception
 
217
   {
 
218
      if (connector != null)
 
219
         connector.stop();
 
220
   }
 
221
   
 
222
   
 
223
   static class TestInvocationHandler implements ServerInvocationHandler
 
224
   {
 
225
      public void addListener(InvokerCallbackHandler callbackHandler) {}
 
226
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
227
      {
 
228
         return invocation.getParameter();
 
229
      }
 
230
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
231
      public void setMBeanServer(MBeanServer server) {}
 
232
      public void setInvoker(ServerInvoker invoker) {}
 
233
   }
 
234
   
 
235
   
 
236
   static class TestCallbackHandler implements InvokerCallbackHandler
 
237
   {
 
238
      public void handleCallback(Callback callback) throws HandleCallbackException
 
239
      {
 
240
         log.info("received callback");
 
241
      }  
 
242
   }
 
243
}
 
 
b'\\ No newline at end of file'