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.net.UnknownHostException;
27
import java.util.HashMap;
30
import javax.management.MBeanServer;
32
import junit.framework.TestCase;
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;
47
* Unit test for JBREM-1139.
49
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
50
* @version $Revision: 2320 $
52
* Copyright July 13, 2009.
55
public class PortUtilRangeOnServerWithServerConfigurationTestCase extends TestCase
57
private static Logger log = Logger.getLogger(PortUtilRangeOnServerWithServerConfigurationTestCase.class);
58
private static boolean firstTime = true;
61
public void setUp() throws Exception
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);
76
public void testUpdateRangeOnServer() throws Exception
78
log.info("entering " + getName());
80
// Test initial values.
81
assertEquals(1024, PortUtil.getMinPort());
82
assertEquals(65535, PortUtil.getMaxPort());
85
Connector connector = new Connector();
86
connector.setServerConfiguration(getServerConfiguration(2000, 60000));
88
assertEquals(2000, PortUtil.getMinPort());
89
assertEquals(60000, PortUtil.getMaxPort());
92
// Set more restrictive values.
93
connector = new Connector();
94
connector.setServerConfiguration(getServerConfiguration(3000, 50000));
96
assertEquals(3000, PortUtil.getMinPort());
97
assertEquals(50000, PortUtil.getMaxPort());
100
// Try to set less restrictive values - should have no effect.
101
connector = new Connector();
102
connector.setServerConfiguration(getServerConfiguration(2000, 60000));
104
assertEquals(3000, PortUtil.getMinPort());
105
assertEquals(50000, PortUtil.getMaxPort());
108
// Try to set invalid minPort - should have no effect.
109
connector = new Connector();
110
connector.setServerConfiguration(getServerConfiguration(60000, -1));
113
log.info("=====================================");
114
log.info("EXPECT ILLEGAL_STATE_EXCEPTION");
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:"))
126
log.info("GOT EXPECTED ILLEGAL_STATE_EXCEPTION");
127
log.info("=====================================");
131
fail("expected IllegalStateException: " + e.getMessage());
134
assertEquals(3000, PortUtil.getMinPort());
135
assertEquals(50000, PortUtil.getMaxPort());
138
// Try to set invalid maxPort - should have no effect.
139
connector = new Connector();
140
connector.setServerConfiguration(getServerConfiguration(-1, 2000));
143
log.info("=====================================");
144
log.info("EXPECT ILLEGAL_STATE_EXCEPTION");
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:"))
156
log.info("GOT EXPECTED ILLEGAL_STATE_EXCEPTION");
157
log.info("=====================================");
161
fail("expected IllegalStateException");
164
assertEquals(3000, PortUtil.getMinPort());
165
assertEquals(50000, PortUtil.getMaxPort());
168
log.info(getName()+ " PASSES");
172
// Element getXML(int minPort, int maxPort) throws SAXException, IOException, ParserConfigurationException
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");
182
// buf.append(" <attribute name=\"" + PortUtil.MIN_PORT + "\">" + minPort + "</attribute>\n");
186
// buf.append(" <attribute name=\"" + PortUtil.MAX_PORT + "\">" + maxPort + "</attribute>\n");
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();
200
ServerConfiguration getServerConfiguration(int minPort, int maxPort) throws UnknownHostException
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();
209
serverParameters.put(PortUtil.MIN_PORT, Integer.toString(minPort));
213
serverParameters.put(PortUtil.MAX_PORT, Integer.toString(maxPort));
215
serverConfiguration.setServerParameters(serverParameters);
216
Map invocationHandlers = new HashMap();
217
invocationHandlers.put("test", TestInvocationHandler.class.getName());
218
serverConfiguration.setInvocationHandlers(invocationHandlers);
219
return serverConfiguration;
222
public static class TestInvocationHandler implements ServerInvocationHandler
224
public void addListener(InvokerCallbackHandler callbackHandler) {}
225
public Object invoke(final InvocationRequest invocation) throws Throwable
227
return invocation.getParameter();
229
public void removeListener(InvokerCallbackHandler callbackHandler) {}
230
public void setMBeanServer(MBeanServer server) {}
231
public void setInvoker(ServerInvoker invoker) {}
b'\\ No newline at end of file'