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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/util/PortUtilRangeOnServerWithXMLTestCase.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.io.ByteArrayInputStream;
26
 
import java.io.IOException;
27
 
import java.net.InetAddress;
28
 
 
29
 
import javax.management.MBeanServer;
30
 
import javax.xml.parsers.DocumentBuilderFactory;
31
 
import javax.xml.parsers.ParserConfigurationException;
32
 
 
33
 
import junit.framework.TestCase;
34
 
 
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;
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 PortUtilRangeOnServerWithXMLTestCase extends TestCase
56
 
{
57
 
   private static Logger log = Logger.getLogger(PortUtilRangeOnServerWithXMLTestCase.class);
58
 
 
59
 
   
60
 
   
61
 
   public void testUpdateRangeOnServer() throws Exception
62
 
   {
63
 
      log.info("entering " + getName());
64
 
      
65
 
      // Test initial values.
66
 
      assertEquals(1024, PortUtil.getMinPort());
67
 
      assertEquals(65535, PortUtil.getMaxPort());
68
 
      
69
 
      // Set new values.
70
 
      Connector connector = new Connector();
71
 
      connector.setConfiguration(getXML(2000, 60000));
72
 
      connector.start();
73
 
      assertEquals(2000, PortUtil.getMinPort());
74
 
      assertEquals(60000, PortUtil.getMaxPort());
75
 
      connector.stop();
76
 
      
77
 
      // Set more restrictive values.
78
 
      connector = new Connector();
79
 
      connector.setConfiguration(getXML(3000, 50000));
80
 
      connector.start();
81
 
      assertEquals(3000, PortUtil.getMinPort());
82
 
      assertEquals(50000, PortUtil.getMaxPort());
83
 
      connector.stop();
84
 
      
85
 
      // Try to set less restrictive values - should have no effect.
86
 
      connector = new Connector();
87
 
      connector.setConfiguration(getXML(2000, 60000));
88
 
      connector.start();
89
 
      assertEquals(3000, PortUtil.getMinPort());
90
 
      assertEquals(50000, PortUtil.getMaxPort());
91
 
      connector.stop();
92
 
            
93
 
      // Try to set invalid minPort - should have no effect.
94
 
      connector = new Connector();
95
 
      connector.setConfiguration(getXML(60000, -1));
96
 
      try
97
 
      {
98
 
         log.info("=====================================");
99
 
         log.info("EXPECT ILLEGAL_STATE_EXCEPTION");
100
 
         connector.start();
101
 
      }
102
 
      catch (Exception e)
103
 
      {
104
 
         log.info(e.getMessage());
105
 
         if (e instanceof IllegalStateException
106
 
               && e.getMessage() != null
107
 
               && e.getMessage().startsWith("Error configuring invoker for connector."))
108
 
         {
109
 
            log.info("GOT EXPECTED ILLEGAL_STATE_EXCEPTION");
110
 
            log.info("=====================================");
111
 
         }
112
 
         else
113
 
         {
114
 
            fail("expected IllegalStateException");
115
 
         }
116
 
      }
117
 
      assertEquals(3000, PortUtil.getMinPort());
118
 
      assertEquals(50000, PortUtil.getMaxPort());
119
 
      connector.stop();
120
 
    
121
 
      // Try to set invalid maxPort - should have no effect.
122
 
      connector = new Connector();
123
 
      connector.setConfiguration(getXML(-1, 2000));
124
 
      try
125
 
      {
126
 
         log.info("=====================================");
127
 
         log.info("EXPECT ILLEGAL_STATE_EXCEPTION");
128
 
         connector.start();
129
 
      }
130
 
      catch (Exception e)
131
 
      {
132
 
         log.info(e.getMessage());
133
 
         if (e instanceof IllegalStateException
134
 
               && e.getMessage() != null
135
 
               && e.getMessage().startsWith("Error configuring invoker for connector."))
136
 
         {
137
 
            log.info("GOT EXPECTED ILLEGAL_STATE_EXCEPTION");
138
 
            log.info("=====================================");
139
 
         }
140
 
         else
141
 
         {
142
 
            fail("expected IllegalStateException");
143
 
         }
144
 
      }
145
 
      assertEquals(3000, PortUtil.getMinPort());
146
 
      assertEquals(50000, PortUtil.getMaxPort());
147
 
      connector.stop();
148
 
      
149
 
      log.info(getName()+ " PASSES");
150
 
   }
151
 
   
152
 
   
153
 
   Element getXML(int minPort, int maxPort) throws SAXException, IOException, ParserConfigurationException
154
 
   {
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");
161
 
      if (minPort > -1)
162
 
      {
163
 
         buf.append("      <attribute name=\"" + PortUtil.MIN_PORT + "\">" + minPort + "</attribute>\n");
164
 
      }
165
 
      if (maxPort > -1)
166
 
      {
167
 
         buf.append("      <attribute name=\"" + PortUtil.MAX_PORT + "\">" + maxPort + "</attribute>\n");
168
 
      }
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();
178
 
   }
179
 
   
180
 
   
181
 
   public static class TestInvocationHandler implements ServerInvocationHandler
182
 
   {
183
 
      public void addListener(InvokerCallbackHandler callbackHandler) {}
184
 
      public Object invoke(final InvocationRequest invocation) throws Throwable
185
 
      {
186
 
         return invocation.getParameter();
187
 
      }
188
 
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
189
 
      public void setMBeanServer(MBeanServer server) {}
190
 
      public void setInvoker(ServerInvoker invoker) {}
191
 
   }
192
 
}
 
 
b'\\ No newline at end of file'