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.io.ByteArrayInputStream;
26
import java.io.IOException;
27
import java.net.InetAddress;
29
import javax.management.MBeanServer;
30
import javax.xml.parsers.DocumentBuilderFactory;
31
import javax.xml.parsers.ParserConfigurationException;
33
import junit.framework.TestCase;
35
import org.apache.log4j.Logger;
36
import org.jboss.remoting.InvocationRequest;
37
import org.jboss.remoting.ServerInvocationHandler;
38
import org.jboss.remoting.ServerInvoker;
39
import org.jboss.remoting.callback.InvokerCallbackHandler;
40
import org.jboss.remoting.transport.Connector;
41
import org.jboss.remoting.transport.PortUtil;
42
import org.w3c.dom.Document;
43
import org.w3c.dom.Element;
44
import org.xml.sax.SAXException;
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 PortUtilRangeOnServerWithXMLTestCase extends TestCase
57
private static Logger log = Logger.getLogger(PortUtilRangeOnServerWithXMLTestCase.class);
61
public void testUpdateRangeOnServer() throws Exception
63
log.info("entering " + getName());
65
// Test initial values.
66
assertEquals(1024, PortUtil.getMinPort());
67
assertEquals(65535, PortUtil.getMaxPort());
70
Connector connector = new Connector();
71
connector.setConfiguration(getXML(2000, 60000));
73
assertEquals(2000, PortUtil.getMinPort());
74
assertEquals(60000, PortUtil.getMaxPort());
77
// Set more restrictive values.
78
connector = new Connector();
79
connector.setConfiguration(getXML(3000, 50000));
81
assertEquals(3000, PortUtil.getMinPort());
82
assertEquals(50000, PortUtil.getMaxPort());
85
// Try to set less restrictive values - should have no effect.
86
connector = new Connector();
87
connector.setConfiguration(getXML(2000, 60000));
89
assertEquals(3000, PortUtil.getMinPort());
90
assertEquals(50000, PortUtil.getMaxPort());
93
// Try to set invalid minPort - should have no effect.
94
connector = new Connector();
95
connector.setConfiguration(getXML(60000, -1));
98
log.info("=====================================");
99
log.info("EXPECT ILLEGAL_STATE_EXCEPTION");
104
log.info(e.getMessage());
105
if (e instanceof IllegalStateException
106
&& e.getMessage() != null
107
&& e.getMessage().startsWith("Error configuring invoker for connector."))
109
log.info("GOT EXPECTED ILLEGAL_STATE_EXCEPTION");
110
log.info("=====================================");
114
fail("expected IllegalStateException");
117
assertEquals(3000, PortUtil.getMinPort());
118
assertEquals(50000, PortUtil.getMaxPort());
121
// Try to set invalid maxPort - should have no effect.
122
connector = new Connector();
123
connector.setConfiguration(getXML(-1, 2000));
126
log.info("=====================================");
127
log.info("EXPECT ILLEGAL_STATE_EXCEPTION");
132
log.info(e.getMessage());
133
if (e instanceof IllegalStateException
134
&& e.getMessage() != null
135
&& e.getMessage().startsWith("Error configuring invoker for connector."))
137
log.info("GOT EXPECTED ILLEGAL_STATE_EXCEPTION");
138
log.info("=====================================");
142
fail("expected IllegalStateException");
145
assertEquals(3000, PortUtil.getMinPort());
146
assertEquals(50000, PortUtil.getMaxPort());
149
log.info(getName()+ " PASSES");
153
Element getXML(int minPort, int maxPort) throws SAXException, IOException, ParserConfigurationException
155
String host = InetAddress.getLocalHost().getHostAddress();
156
StringBuffer buf = new StringBuffer();
157
buf.append("<?xml version=\"1.0\"?>\n");
158
buf.append("<config>\n");
159
buf.append(" <invoker transport=\"socket\">\n");
160
buf.append(" <attribute name=\"serverBindAddress\">" + host + "</attribute>\n");
163
buf.append(" <attribute name=\"" + PortUtil.MIN_PORT + "\">" + minPort + "</attribute>\n");
167
buf.append(" <attribute name=\"" + PortUtil.MAX_PORT + "\">" + maxPort + "</attribute>\n");
169
buf.append(" </invoker>\n");
170
buf.append(" <handlers>\n");
171
buf.append(" <handler subsystem=\"test\">" + TestInvocationHandler.class.getName() + "</handler>\n");
172
buf.append(" </handlers>\n");
173
buf.append("</config>\n");
174
log.info("\n\n" + buf.toString());
175
ByteArrayInputStream bais = new ByteArrayInputStream(buf.toString().getBytes());
176
Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais);
177
return xml.getDocumentElement();
181
public static class TestInvocationHandler implements ServerInvocationHandler
183
public void addListener(InvokerCallbackHandler callbackHandler) {}
184
public Object invoke(final InvocationRequest invocation) throws Throwable
186
return invocation.getParameter();
188
public void removeListener(InvokerCallbackHandler callbackHandler) {}
189
public void setMBeanServer(MBeanServer server) {}
190
public void setInvoker(ServerInvoker invoker) {}
b'\\ No newline at end of file'