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.
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.
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.
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.
22
package org.jboss.test.remoting.util;
25
import java.net.InetAddress;
26
import java.util.HashMap;
29
import junit.framework.TestCase;
31
import org.apache.log4j.Logger;
32
import org.jboss.remoting.InvokerLocator;
33
import org.jboss.remoting.transport.Connector;
34
import org.jboss.remoting.transport.PortUtil;
37
* Unit test for JBREM-1139.
39
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
40
* @version $Revision: 2320 $
42
* Copyright July 13, 2009.
45
public class PortUtilRangeOnServerTestCase extends TestCase
47
private static Logger log = Logger.getLogger(PortUtilRangeOnServerTestCase.class);
51
public void testUpdateRangeOnServer() throws Exception
53
log.info("entering " + getName());
55
// Test initial values.
56
assertEquals(1024, PortUtil.getMinPort());
57
assertEquals(65535, PortUtil.getMaxPort());
59
// Set new values using configuration mapr.
60
Map serverConfig = new HashMap();
61
serverConfig.put(PortUtil.MIN_PORT, "2000");
62
serverConfig.put(PortUtil.MAX_PORT, "60000");
63
String host = InetAddress.getLocalHost().getHostAddress();
64
InvokerLocator locator = new InvokerLocator("socket://" + host);
65
Connector connector = new Connector(locator, serverConfig);
67
log.info("InvokerLocator: " + connector.getInvokerLocator());
68
assertEquals(2000, PortUtil.getMinPort());
69
assertEquals(60000, PortUtil.getMaxPort());
72
// Set more restrictive values using configuration map.
73
serverConfig.put(PortUtil.MIN_PORT, "3000");
74
serverConfig.put(PortUtil.MAX_PORT, "50000");
75
connector = new Connector(locator, serverConfig);
77
log.info("InvokerLocator: " + connector.getInvokerLocator());
78
assertEquals(3000, PortUtil.getMinPort());
79
assertEquals(50000, PortUtil.getMaxPort());
82
// Set more restrictive values with InvokerLocator overriding configuration map.
83
serverConfig.put(PortUtil.MIN_PORT, "3500");
84
serverConfig.put(PortUtil.MAX_PORT, "45000");
85
locator = new InvokerLocator("socket://" + host + "/?" + PortUtil.MIN_PORT + "=4000&" + PortUtil.MAX_PORT + "=40000");
86
connector = new Connector(locator, serverConfig);
88
log.info("InvokerLocator: " + connector.getInvokerLocator());
89
assertEquals(4000, PortUtil.getMinPort());
90
assertEquals(40000, PortUtil.getMaxPort());
93
// Try to set less restrictive values - should have no effect.
94
serverConfig.put(PortUtil.MIN_PORT, "2000");
95
serverConfig.put(PortUtil.MAX_PORT, "60000");
96
locator = new InvokerLocator("socket://" + host);
97
connector = new Connector(locator, serverConfig);
99
log.info("InvokerLocator: " + connector.getInvokerLocator());
100
assertEquals(4000, PortUtil.getMinPort());
101
assertEquals(40000, PortUtil.getMaxPort());
104
// Try to set invalid minPort - should have no effect.
105
serverConfig.put(PortUtil.MIN_PORT, "60000");
106
serverConfig.remove(PortUtil.MAX_PORT);
107
connector = new Connector(locator, serverConfig);
110
log.info("=====================================");
111
log.info("EXPECT ILLEGAL_STATE_EXCEPTION");
116
log.info(e.getCause());
117
if (e.getCause() instanceof IllegalStateException
118
&& e.getCause().getMessage() != null
119
&& e.getCause().getMessage().startsWith("trying to set minPort"))
121
log.info("GOT EXPECTED ILLEGAL_STATE_EXCEPTION");
122
log.info("=====================================");
126
fail("expected IllegalStateException");
129
log.info("InvokerLocator: " + connector.getInvokerLocator());
130
assertEquals(4000, PortUtil.getMinPort());
131
assertEquals(40000, PortUtil.getMaxPort());
134
// Try to set invalid maxPort - should have no effect.
135
serverConfig.remove(PortUtil.MIN_PORT);
136
serverConfig.put(PortUtil.MAX_PORT, "2000");
137
connector = new Connector(locator, serverConfig);
140
log.info("=====================================");
141
log.info("EXPECT ILLEGAL_STATE_EXCEPTION");
146
log.info(e.getCause());
147
if (e.getCause() instanceof IllegalStateException
148
&& e.getCause().getMessage() != null
149
&& e.getCause().getMessage().startsWith("trying to set maxPort"))
151
log.info("GOT EXPECTED ILLEGAL_STATE_EXCEPTION");
152
log.info("=====================================");
156
fail("expected IllegalStateException");
159
log.info("InvokerLocator: " + connector.getInvokerLocator());
160
assertEquals(4000, PortUtil.getMinPort());
161
assertEquals(40000, PortUtil.getMaxPort());
164
log.info(getName()+ " PASSES");
b'\\ No newline at end of file'