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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/util/PortUtilRangeOnServerWithServerConfigurationTestCase.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.util;
23
 
 
24
 
 
25
 
import java.net.InetAddress;
26
 
import java.net.UnknownHostException;
27
 
import java.util.HashMap;
28
 
import java.util.Map;
29
 
 
30
 
import javax.management.MBeanServer;
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.remoting.InvocationRequest;
39
 
import org.jboss.remoting.ServerConfiguration;
40
 
import org.jboss.remoting.ServerInvocationHandler;
41
 
import org.jboss.remoting.ServerInvoker;
42
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
43
 
import org.jboss.remoting.transport.Connector;
44
 
import org.jboss.remoting.transport.PortUtil;
45
 
 
46
 
/** 
47
 
 * Unit test for JBREM-1139.
48
 
 * 
49
 
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
50
 
 * @version $Revision: 2320 $
51
 
 * <p>
52
 
 * Copyright July 13, 2009.
53
 
 * </p>
54
 
 */
55
 
public class PortUtilRangeOnServerWithServerConfigurationTestCase extends TestCase
56
 
{
57
 
   private static Logger log = Logger.getLogger(PortUtilRangeOnServerWithServerConfigurationTestCase.class);
58
 
   private static boolean firstTime = true;
59
 
 
60
 
   
61
 
   public void setUp() throws Exception
62
 
   {
63
 
      if (firstTime)
64
 
      {
65
 
         firstTime = false;
66
 
         Logger.getLogger("org.jboss.remoting").setLevel(Level.DEBUG);
67
 
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
68
 
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
69
 
         PatternLayout layout = new PatternLayout(pattern);
70
 
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
71
 
         Logger.getRootLogger().addAppender(consoleAppender);
72
 
      }
73
 
   }
74
 
   
75
 
   
76
 
   public void testUpdateRangeOnServer() throws Exception
77
 
   {
78
 
      log.info("entering " + getName());
79
 
      
80
 
      // Test initial values.
81
 
      assertEquals(1024, PortUtil.getMinPort());
82
 
      assertEquals(65535, PortUtil.getMaxPort());
83
 
      
84
 
      // Set new values.
85
 
      Connector connector = new Connector();
86
 
      connector.setServerConfiguration(getServerConfiguration(2000, 60000));
87
 
      connector.start();
88
 
      assertEquals(2000, PortUtil.getMinPort());
89
 
      assertEquals(60000, PortUtil.getMaxPort());
90
 
      connector.stop();
91
 
      
92
 
      // Set more restrictive values.
93
 
      connector = new Connector();
94
 
      connector.setServerConfiguration(getServerConfiguration(3000, 50000));
95
 
      connector.start();
96
 
      assertEquals(3000, PortUtil.getMinPort());
97
 
      assertEquals(50000, PortUtil.getMaxPort());
98
 
      connector.stop();
99
 
      
100
 
      // Try to set less restrictive values - should have no effect.
101
 
      connector = new Connector();
102
 
      connector.setServerConfiguration(getServerConfiguration(2000, 60000));
103
 
      connector.start();
104
 
      assertEquals(3000, PortUtil.getMinPort());
105
 
      assertEquals(50000, PortUtil.getMaxPort());
106
 
      connector.stop();
107
 
            
108
 
      // Try to set invalid minPort - should have no effect.
109
 
      connector = new Connector();
110
 
      connector.setServerConfiguration(getServerConfiguration(60000, -1));
111
 
      try
112
 
      {
113
 
         log.info("=====================================");
114
 
         log.info("EXPECT ILLEGAL_STATE_EXCEPTION");
115
 
         connector.start();
116
 
      }
117
 
      catch (Exception e)
118
 
      {
119
 
         log.info(e.getMessage());
120
 
         if (e instanceof RuntimeException
121
 
               && e.getMessage().startsWith("Error setting up server invoker")
122
 
               && e.getCause() instanceof IllegalStateException
123
 
               && e.getCause().getMessage() != null
124
 
               && e.getCause().getMessage().startsWith("trying to set minPort to value greater than maxPort:"))
125
 
         {
126
 
            log.info("GOT EXPECTED ILLEGAL_STATE_EXCEPTION");
127
 
            log.info("=====================================");
128
 
         }
129
 
         else
130
 
         {
131
 
            fail("expected IllegalStateException: " + e.getMessage());
132
 
         }
133
 
      }
134
 
      assertEquals(3000, PortUtil.getMinPort());
135
 
      assertEquals(50000, PortUtil.getMaxPort());
136
 
      connector.stop();
137
 
    
138
 
      // Try to set invalid maxPort - should have no effect.
139
 
      connector = new Connector();
140
 
      connector.setServerConfiguration(getServerConfiguration(-1, 2000));
141
 
      try
142
 
      {
143
 
         log.info("=====================================");
144
 
         log.info("EXPECT ILLEGAL_STATE_EXCEPTION");
145
 
         connector.start();
146
 
      }
147
 
      catch (Exception e)
148
 
      {
149
 
         log.info(e.getMessage());
150
 
         if (e instanceof RuntimeException
151
 
               && e.getMessage().startsWith("Error setting up server invoker")
152
 
               && e.getCause() instanceof IllegalStateException
153
 
               && e.getCause().getMessage() != null
154
 
               && e.getCause().getMessage().startsWith("trying to set maxPort to value less than minPort:"))
155
 
         {
156
 
            log.info("GOT EXPECTED ILLEGAL_STATE_EXCEPTION");
157
 
            log.info("=====================================");
158
 
         }
159
 
         else
160
 
         {
161
 
            fail("expected IllegalStateException");
162
 
         }
163
 
      }
164
 
      assertEquals(3000, PortUtil.getMinPort());
165
 
      assertEquals(50000, PortUtil.getMaxPort());
166
 
      connector.stop();
167
 
      
168
 
      log.info(getName()+ " PASSES");
169
 
   }
170
 
   
171
 
   
172
 
//   Element getXML(int minPort, int maxPort) throws SAXException, IOException, ParserConfigurationException
173
 
//   {
174
 
//      String host = InetAddress.getLocalHost().getHostAddress();
175
 
//      StringBuffer buf = new StringBuffer();
176
 
//      buf.append("<?xml version=\"1.0\"?>\n");
177
 
//      buf.append("<config>\n");
178
 
//      buf.append("   <invoker transport=\"socket\">\n");
179
 
//      buf.append("      <attribute name=\"serverBindAddress\">" + host + "</attribute>\n");
180
 
//      if (minPort > -1)
181
 
//      {
182
 
//         buf.append("      <attribute name=\"" + PortUtil.MIN_PORT + "\">" + minPort + "</attribute>\n");
183
 
//      }
184
 
//      if (maxPort > -1)
185
 
//      {
186
 
//         buf.append("      <attribute name=\"" + PortUtil.MAX_PORT + "\">" + maxPort + "</attribute>\n");
187
 
//      }
188
 
//      buf.append("   </invoker>\n");
189
 
//      buf.append("   <handlers>\n");
190
 
//      buf.append("      <handler subsystem=\"test\">" + TestInvocationHandler.class.getName() + "</handler>\n");
191
 
//      buf.append("   </handlers>\n");
192
 
//      buf.append("</config>\n");
193
 
//      log.info("\n\n" + buf.toString());
194
 
//      ByteArrayInputStream bais = new ByteArrayInputStream(buf.toString().getBytes());
195
 
//      Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais);
196
 
//      return xml.getDocumentElement();
197
 
//   }
198
 
   
199
 
   
200
 
   ServerConfiguration getServerConfiguration(int minPort, int maxPort) throws UnknownHostException
201
 
   {
202
 
      ServerConfiguration serverConfiguration = new ServerConfiguration("socket");
203
 
      Map invokerLocatorParameters = new HashMap();
204
 
      invokerLocatorParameters.put("serverBindAddress", InetAddress.getLocalHost().getHostAddress());
205
 
      serverConfiguration.setInvokerLocatorParameters(invokerLocatorParameters);
206
 
      Map serverParameters = new HashMap();
207
 
      if (minPort > -1)
208
 
      {
209
 
         serverParameters.put(PortUtil.MIN_PORT, Integer.toString(minPort));
210
 
      }
211
 
      if (maxPort > -1)
212
 
      {
213
 
         serverParameters.put(PortUtil.MAX_PORT, Integer.toString(maxPort));   
214
 
      }
215
 
      serverConfiguration.setServerParameters(serverParameters);
216
 
      Map invocationHandlers = new HashMap();
217
 
      invocationHandlers.put("test", TestInvocationHandler.class.getName());
218
 
      serverConfiguration.setInvocationHandlers(invocationHandlers);
219
 
      return serverConfiguration;
220
 
   }
221
 
   
222
 
   public static class TestInvocationHandler implements ServerInvocationHandler
223
 
   {
224
 
      public void addListener(InvokerCallbackHandler callbackHandler) {}
225
 
      public Object invoke(final InvocationRequest invocation) throws Throwable
226
 
      {
227
 
         return invocation.getParameter();
228
 
      }
229
 
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
230
 
      public void setMBeanServer(MBeanServer server) {}
231
 
      public void setInvoker(ServerInvoker invoker) {}
232
 
   }
233
 
}
 
 
b'\\ No newline at end of file'