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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/template/TestCaseTemplate.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 2009, 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.template;
 
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.Callback;
 
42
import org.jboss.remoting.callback.HandleCallbackException;
 
43
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
44
import org.jboss.remoting.transport.Connector;
 
45
import org.jboss.remoting.transport.PortUtil;
 
46
 
 
47
 
 
48
public class TestCaseTemplate extends TestCase
 
49
{
 
50
   private static Logger log = Logger.getLogger(TestCaseTemplate.class);
 
51
   
 
52
   private static boolean firstTime = true;
 
53
   
 
54
   protected String host;
 
55
   protected int port;
 
56
   protected String locatorURI;
 
57
   protected InvokerLocator serverLocator;
 
58
   protected Connector connector;
 
59
   protected TestInvocationHandler invocationHandler;
 
60
 
 
61
   
 
62
   public void setUp() throws Exception
 
63
   {
 
64
      if (firstTime)
 
65
      {
 
66
         firstTime = false;
 
67
         Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
 
68
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
 
69
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
 
70
         PatternLayout layout = new PatternLayout(pattern);
 
71
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
 
72
         Logger.getRootLogger().addAppender(consoleAppender);  
 
73
      }
 
74
   }
 
75
 
 
76
   
 
77
   public void tearDown()
 
78
   {
 
79
   }
 
80
   
 
81
   
 
82
   public void testMethod() throws Throwable
 
83
   {
 
84
      log.info("entering " + getName());
 
85
      
 
86
      // Start server.
 
87
      setupServer();
 
88
      
 
89
      // Create client.
 
90
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
 
91
      HashMap clientConfig = new HashMap();
 
92
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
93
      addExtraClientConfig(clientConfig);
 
94
      Client client = new Client(clientLocator, clientConfig);
 
95
      client.connect();
 
96
      log.info("client is connected");
 
97
      
 
98
      // Test connection.
 
99
      assertEquals("abc", client.invoke("abc"));
 
100
      log.info("connection is good");
 
101
      
 
102
      client.disconnect();
 
103
      shutdownServer();
 
104
      log.info(getName() + " PASSES");
 
105
   }
 
106
   
 
107
   
 
108
   protected String getTransport()
 
109
   {
 
110
      return "socket";
 
111
   }
 
112
   
 
113
   
 
114
   protected void addExtraClientConfig(Map config) {}
 
115
   protected void addExtraServerConfig(Map config) {}
 
116
   
 
117
 
 
118
   protected void setupServer() throws Exception
 
119
   {
 
120
      host = InetAddress.getLocalHost().getHostAddress();
 
121
      port = PortUtil.findFreePort(host);
 
122
      locatorURI = getTransport() + "://" + host + ":" + port;
 
123
      String metadata = System.getProperty("remoting.metadata");
 
124
      if (metadata != null)
 
125
      {
 
126
         locatorURI += "/?" + metadata;
 
127
      }
 
128
      serverLocator = new InvokerLocator(locatorURI);
 
129
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
130
      HashMap config = new HashMap();
 
131
      config.put(InvokerLocator.FORCE_REMOTE, "true");
 
132
      addExtraServerConfig(config);
 
133
      connector = new Connector(serverLocator, config);
 
134
      connector.create();
 
135
      invocationHandler = new TestInvocationHandler();
 
136
      connector.addInvocationHandler("test", invocationHandler);
 
137
      connector.start();
 
138
   }
 
139
   
 
140
   
 
141
   protected void shutdownServer() throws Exception
 
142
   {
 
143
      if (connector != null)
 
144
         connector.stop();
 
145
   }
 
146
   
 
147
   
 
148
   static class TestInvocationHandler implements ServerInvocationHandler
 
149
   {
 
150
      public void addListener(InvokerCallbackHandler callbackHandler) {}
 
151
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
152
      {
 
153
         return invocation.getParameter();
 
154
      }
 
155
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
156
      public void setMBeanServer(MBeanServer server) {}
 
157
      public void setInvoker(ServerInvoker invoker) {}
 
158
   }
 
159
   
 
160
   
 
161
   static class TestCallbackHandler implements InvokerCallbackHandler
 
162
   {
 
163
      public void handleCallback(Callback callback) throws HandleCallbackException
 
164
      {
 
165
         log.info("received callback");
 
166
      }  
 
167
   }
 
168
}
 
 
b'\\ No newline at end of file'