    1   /* Copyright 2004 Acegi Technology Pty Limited
    2    *
    3    * Licensed under the Apache License, Version 2.0 (the "License");
    4    * you may not use this file except in compliance with the License.
    5    * You may obtain a copy of the License at
    6    *
    7    *     http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15
    16  package org.acegisecurity.util;
    17
    18  import junit.framework.TestCase;
    19
    20  import java.util.HashMap;
    21  import java.util.Map;
    22
    23
    24  /***
    25   * Tests {@link PortMapperImpl}.
    26   *
    27   * @author Ben Alex
    28   * @version $Id: PortMapperImplTests.java,v 1.2 2005/11/17 00:56:08 benalex Exp $
    29   */
    30  public class PortMapperImplTests extends TestCase {
    31      //~ Constructors ===========================================================
    32
    33      public PortMapperImplTests() {
    34          super();
    35      }
    36
    37      public PortMapperImplTests(String arg0) {
    38          super(arg0);
    39      }
    40
    41      //~ Methods ================================================================
    42
    43      public final void setUp() throws Exception {
    44          super.setUp();
    45      }
    46
    47      public static void main(String[] args) {
    48          junit.textui.TestRunner.run(PortMapperImplTests.class);
    49      }
    50
    51      public void testDefaultMappingsAreKnown() throws Exception {
    52          PortMapperImpl portMapper = new PortMapperImpl();
    53          assertEquals(new Integer(80),
    54              portMapper.lookupHttpPort(new Integer(443)));
    55          assertEquals(new Integer(8080),
    56              portMapper.lookupHttpPort(new Integer(8443)));
    57          assertEquals(new Integer(443),
    58              portMapper.lookupHttpsPort(new Integer(80)));
    59          assertEquals(new Integer(8443),
    60              portMapper.lookupHttpsPort(new Integer(8080)));
    61      }
    62
    63      public void testDetectsEmptyMap() throws Exception {
    64          PortMapperImpl portMapper = new PortMapperImpl();
    65
    66          try {
    67              portMapper.setPortMappings(new HashMap());
    68              fail("Should have thrown IllegalArgumentException");
    69          } catch (IllegalArgumentException expected) {
    70              assertTrue(true);
    71          }
    72      }
    73
    74      public void testDetectsNullMap() throws Exception {
    75          PortMapperImpl portMapper = new PortMapperImpl();
    76
    77          try {
    78              portMapper.setPortMappings(null);
    79              fail("Should have thrown IllegalArgumentException");
    80          } catch (IllegalArgumentException expected) {
    81              assertTrue(true);
    82          }
    83      }
    84
    85      public void testGetTranslatedPortMappings() {
    86          PortMapperImpl portMapper = new PortMapperImpl();
    87          assertEquals(2, portMapper.getTranslatedPortMappings().size());
    88      }
    89
    90      public void testRejectsOutOfRangeMappings() {
    91          PortMapperImpl portMapper = new PortMapperImpl();
    92          Map map = new HashMap();
    93          map.put("79", "80559");
    94
    95          try {
    96              portMapper.setPortMappings(map);
    97              fail("Should have thrown IllegalArgumentException");
    98          } catch (IllegalArgumentException expected) {
    99              assertTrue(true);
    100         }
    101     }
    102
    103     public void testReturnsNullIfHttpPortCannotBeFound() {
    104         PortMapperImpl portMapper = new PortMapperImpl();
    105         assertTrue(portMapper.lookupHttpPort(new Integer("34343")) == null);
    106     }
    107
    108     public void testSupportsCustomMappings() {
    109         PortMapperImpl portMapper = new PortMapperImpl();
    110         Map map = new HashMap();
    111         map.put("79", "442");
    112
    113         portMapper.setPortMappings(map);
    114
    115         assertEquals(new Integer(79),
    116             portMapper.lookupHttpPort(new Integer(442)));
    117         assertEquals(new Integer(442),
    118             portMapper.lookupHttpsPort(new Integer(79)));
    119     }
    120 }