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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/ipv6/IPv6TestCase.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.io.InputStream;
25
 
import java.io.OutputStream;
26
 
import java.net.InetAddress;
27
 
import java.net.ServerSocket;
28
 
import java.net.Socket;
29
 
import java.util.HashMap;
30
 
import java.util.Map;
31
 
 
32
 
import javax.management.MBeanServer;
33
 
 
34
 
import junit.framework.TestCase;
35
 
 
36
 
import org.apache.log4j.ConsoleAppender;
37
 
import org.apache.log4j.Level;
38
 
import org.apache.log4j.Logger;
39
 
import org.apache.log4j.PatternLayout;
40
 
import org.jboss.remoting.Client;
41
 
import org.jboss.remoting.InvocationRequest;
42
 
import org.jboss.remoting.InvokerLocator;
43
 
import org.jboss.remoting.ServerInvocationHandler;
44
 
import org.jboss.remoting.ServerInvoker;
45
 
import org.jboss.remoting.callback.Callback;
46
 
import org.jboss.remoting.callback.HandleCallbackException;
47
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
48
 
import org.jboss.remoting.transport.Connector;
49
 
import org.jboss.remoting.transport.PortUtil;
50
 
 
51
 
 
52
 
/**
53
 
 * 
54
 
 * Unit test for JBREM-852.
55
 
 * 
56
 
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
57
 
 * @version $Revision: 1.1 $
58
 
 * <p>
59
 
 * Copyright Dec 4, 2007
60
 
 * </p>
61
 
 */
62
 
public class IPv6TestCase extends TestCase
63
 
{
64
 
   private static Logger log = Logger.getLogger(IPv6TestCase.class);
65
 
   
66
 
   private static boolean firstTime = true;
67
 
   private static boolean ipv6IsAvailable = true;
68
 
   
69
 
   // remoting server connector
70
 
   private Connector connector;
71
 
   private InvokerLocator serverLocator;
72
 
   private TestInvocationHandler invocationHandler;
73
 
 
74
 
   
75
 
   public void setUp() throws Exception
76
 
   {
77
 
      if (firstTime)
78
 
      {
79
 
         firstTime = false;
80
 
         Logger.getLogger("org.jboss.remoting").setLevel(Level.DEBUG);
81
 
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
82
 
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
83
 
         PatternLayout layout = new PatternLayout(pattern);
84
 
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
85
 
         Logger.getRootLogger().addAppender(consoleAppender);
86
 
         log.info("java.version: " + System.getProperty("java.version"));
87
 
         
88
 
         try
89
 
         {
90
 
            InetAddress addr = InetAddress.getByName("[::1]");
91
 
            new ServerSocket(3333, 200, addr);
92
 
         }
93
 
         catch (Exception e)
94
 
         {
95
 
            if ("Protocol family unavailable".equalsIgnoreCase(e.getMessage()) ||
96
 
                "Protocol family not supported".equalsIgnoreCase(e.getMessage()))
97
 
            {
98
 
               ipv6IsAvailable = false;
99
 
               log.info("ipV6 is not available");
100
 
            }
101
 
         }
102
 
      }
103
 
   }
104
 
 
105
 
   
106
 
   public void tearDown()
107
 
   {
108
 
   }
109
 
 
110
 
 
111
 
   public void testRawIPv6() throws Throwable
112
 
   {
113
 
      log.info("entering " + getName());
114
 
      if (!ipv6IsAvailable) return;
115
 
      
116
 
      final InetAddress addr = InetAddress.getByName("[::1]");
117
 
 
118
 
      new Thread()
119
 
      {
120
 
         public void run()
121
 
         {
122
 
            try
123
 
            {
124
 
               ServerSocket ss = new ServerSocket(4446, 200, addr);
125
 
               log.info("ss address: " + ss.getInetAddress());
126
 
               Socket s = ss.accept();
127
 
               InputStream is = s.getInputStream();
128
 
               log.info("read: " + is.read());
129
 
               s.close();
130
 
               ss.close();
131
 
            }
132
 
            catch (Exception e)
133
 
            {
134
 
               log.error("error", e);
135
 
            }
136
 
         }
137
 
      }.start();
138
 
 
139
 
      
140
 
      Thread.sleep(2000);
141
 
      
142
 
      Socket s = new Socket(addr, 4446);
143
 
      log.info("s address: " + s.getInetAddress());
144
 
      log.info("s local address: " + s.getLocalAddress());
145
 
      OutputStream os = s.getOutputStream();
146
 
      os.write(17);
147
 
      log.info("wrote 17");
148
 
      s.close();
149
 
      log.info(getName() + " PASSES");
150
 
      
151
 
   }
152
 
   
153
 
   
154
 
   public void testRemotingIPv6Loopback() throws Throwable
155
 
   {
156
 
      log.info("entering " + getName());
157
 
      if (!ipv6IsAvailable) return;
158
 
      
159
 
      doRemotingTest("[::1]");
160
 
      log.info(getName() + " PASSES");
161
 
   }
162
 
   
163
 
   
164
 
   public void testRemotingIPv6Any() throws Throwable
165
 
   { 
166
 
      log.info("entering " + getName());
167
 
      if (!ipv6IsAvailable) return;
168
 
      doRemotingTest("[::]");
169
 
      log.info(getName() + " PASSES");
170
 
   }   
171
 
   
172
 
   
173
 
   public void testRemotingIPv4Mapped() throws Throwable
174
 
   {
175
 
      if (!ipv6IsAvailable) return;
176
 
      doRemotingTest("[::ffff:127.0.0.1]");
177
 
   }
178
 
   
179
 
   
180
 
   protected void doRemotingTest(String host) throws Throwable
181
 
   {
182
 
      // Start server.
183
 
      InetAddress[] addresses = InetAddress.getAllByName("localhost");
184
 
      for (int i = 0; i < addresses.length; i++)
185
 
      {
186
 
         log.info("addresses[" + i + "]: " + addresses[i]);
187
 
      }
188
 
 
189
 
      int port = PortUtil.findFreePort(host); 
190
 
      String locatorURI = "socket://" + host + ":" + port + "/?timeout=10000";
191
 
      serverLocator = new InvokerLocator(locatorURI);
192
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
193
 
      HashMap config = new HashMap();
194
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
195
 
      addExtraServerConfig(config);
196
 
      connector = new Connector(serverLocator, config);
197
 
      connector.create();
198
 
      invocationHandler = new TestInvocationHandler();
199
 
      connector.addInvocationHandler("sample", invocationHandler);
200
 
      connector.start();
201
 
      
202
 
      // Create client.
203
 
      InvokerLocator clientLocator1 = new InvokerLocator(locatorURI);
204
 
      HashMap clientConfig = new HashMap();
205
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
206
 
      addExtraClientConfig(clientConfig);
207
 
      Client client = new Client(clientLocator1, clientConfig);
208
 
      client.connect();
209
 
      log.info("client is connected");
210
 
      
211
 
      // Test connections.
212
 
      assertEquals("abc", client.invoke("abc"));
213
 
      log.info("connection is good");
214
 
      
215
 
      client.disconnect();
216
 
      connector.stop();
217
 
   }
218
 
   
219
 
   
220
 
   protected String getTransport()
221
 
   {
222
 
      return "socket";
223
 
   }
224
 
   
225
 
   
226
 
   protected void addExtraClientConfig(Map config) {}
227
 
   protected void addExtraServerConfig(Map config) {}
228
 
   
229
 
 
230
 
   static class TestInvocationHandler implements ServerInvocationHandler
231
 
   {
232
 
      public void addListener(InvokerCallbackHandler callbackHandler) {}
233
 
      public Object invoke(final InvocationRequest invocation) throws Throwable
234
 
      {
235
 
         return invocation.getParameter();
236
 
      }
237
 
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
238
 
      public void setMBeanServer(MBeanServer server) {}
239
 
      public void setInvoker(ServerInvoker invoker) {}
240
 
   }
241
 
   
242
 
   
243
 
   static class TestCallbackHandler implements InvokerCallbackHandler
244
 
   {
245
 
      public void handleCallback(Callback callback) throws HandleCallbackException
246
 
      {
247
 
         log.info("received callback");
248
 
      }  
249
 
   }
250
 
}
 
 
b'\\ No newline at end of file'