TestHttpVersion xref

    1   /*
    2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestHttpVersion.java,v 1.3 2004/05/09 12:16:12 olegk Exp $
    3    * $Revision: 480424 $
    4    * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
    5    * ====================================================================
    6    *
    7    *  Licensed to the Apache Software Foundation (ASF) under one or more
    8    *  contributor license agreements.  See the NOTICE file distributed with
    9    *  this work for additional information regarding copyright ownership.
    10   *  The ASF licenses this file to You under the Apache License, Version 2.0
    11   *  (the "License"); you may not use this file except in compliance with
    12   *  the License.  You may obtain a copy of the License at
    13   *
    14   *      http://www.apache.org/licenses/LICENSE-2.0
    15   *
    16   *  Unless required by applicable law or agreed to in writing, software
    17   *  distributed under the License is distributed on an "AS IS" BASIS,
    18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    19   *  See the License for the specific language governing permissions and
    20   *  limitations under the License.
    21   * ====================================================================
    22   *
    23   * This software consists of voluntary contributions made by many
    24   * individuals on behalf of the Apache Software Foundation.  For more
    25   * information on the Apache Software Foundation, please see
    26   * <http://www.apache.org/>.
    27   *
    28   * [Additional notices, if required by prior licensing conditions]
    29   *
    30   */
    31
    32  package org.apache.commons.httpclient;
    33
    34  import junit.framework.Test;
    35  import junit.framework.TestCase;
    36  import junit.framework.TestSuite;
    37
    38  /**
    39   * Test cases for HTTP version class
    40   *
    41   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
    42   *
    43   * @version $Revision: 480424 $
    44   */
    45  public class TestHttpVersion extends TestCase {
    46
    47      // ------------------------------------------------------------ Constructor
    48
    49      public TestHttpVersion(String name) {
    50          super(name);
    51      }
    52
    53      // ------------------------------------------------------- TestCase Methods
    54
    55      public static Test suite() {
    56          return new TestSuite(TestHttpVersion.class);
    57      }
    58
    59      // ------------------------------------------------------------------ Tests
    60
    61      public void testHttpVersionInvalidConstructorInput() throws Exception {
    62          try {
    63              HttpVersion ver = new HttpVersion(-1, -1);
    64              fail("IllegalArgumentException should have been thrown");
    65          } catch (IllegalArgumentException e) {
    66              // expected
    67          }
    68          try {
    69              HttpVersion ver = new HttpVersion(0, -1);
    70              fail("IllegalArgumentException should have been thrown");
    71          } catch (IllegalArgumentException e) {
    72              // expected
    73          }
    74      }
    75
    76      public void testHttpVersionParsing() throws Exception {
    77          String s = "HTTP/1.1";
    78          HttpVersion version = HttpVersion.parse(s);
    79          assertEquals("HTTP major version number", 1, version.getMajor());
    80          assertEquals("HTTP minor version number", 1, version.getMinor());
    81          assertEquals("HTTP version number", s, version.toString());
    82
    83          s = "HTTP/123.4567";
    84          version = HttpVersion.parse(s);
    85          assertEquals("HTTP major version number", 123, version.getMajor());
    86          assertEquals("HTTP minor version number", 4567, version.getMinor());
    87          assertEquals("HTTP version number", s, version.toString());
    88      }
    89
    90      public void testInvalidHttpVersionParsing() throws Exception {
    91          try {
    92              HttpVersion.parse(null);
    93              fail("IllegalArgumentException should have been thrown");
    94          } catch (IllegalArgumentException e) {
    95              //expected
    96          }
    97          try {
    98              HttpVersion.parse("crap");
    99              fail("ProtocolException should have been thrown");
    100         } catch (ProtocolException e) {
    101             //expected
    102         }
    103         try {
    104             HttpVersion.parse("HTTP/crap");
    105             fail("ProtocolException should have been thrown");
    106         } catch (ProtocolException e) {
    107             //expected
    108         }
    109         try {
    110             HttpVersion.parse("HTTP/1");
    111             fail("ProtocolException should have been thrown");
    112         } catch (ProtocolException e) {
    113             //expected
    114         }
    115         try {
    116             HttpVersion.parse("HTTP/1234   ");
    117             fail("ProtocolException should have been thrown");
    118         } catch (ProtocolException e) {
    119             //expected
    120         }
    121         try {
    122             HttpVersion.parse("HTTP/1.");
    123             fail("ProtocolException should have been thrown");
    124         } catch (ProtocolException e) {
    125             //expected
    126         }
    127         try {
    128             HttpVersion.parse("HTTP/1.1 crap");
    129             fail("ProtocolException should have been thrown");
    130         } catch (ProtocolException e) {
    131             //expected
    132         }
    133         try {
    134             HttpVersion.parse("HTTP/whatever.whatever whatever");
    135             fail("ProtocolException should have been thrown");
    136         } catch (ProtocolException e) {
    137             //expected
    138         }
    139         try {
    140             HttpVersion.parse("HTTP/1.whatever whatever");
    141             fail("ProtocolException should have been thrown");
    142         } catch (ProtocolException e) {
    143             //expected
    144         }
    145     }
    146
    147     public void testHttpVersionEquality() throws Exception {
    148         HttpVersion ver1 = new HttpVersion(1, 1);
    149         HttpVersion ver2 = new HttpVersion(1, 1);
    150
    151         assertEquals(ver1.hashCode(), ver2.hashCode());
    152         assertTrue(ver1.equals(ver1));
    153         assertTrue(ver1.equals(ver2));
    154         assertTrue(ver1.equals((Object)ver1));
    155         assertTrue(ver1.equals((Object)ver2));
    156
    157         assertFalse(ver1.equals(new Float(1.1)));
    158
    159         try {
    160             ver1.equals(null);
    161             fail("IllegalArgumentException should have been thrown");
    162         } catch (IllegalArgumentException e) {
    163         }
    164
    165         assertTrue((new HttpVersion(0, 9)).equals(HttpVersion.HTTP_0_9));
    166         assertTrue((new HttpVersion(1, 0)).equals(HttpVersion.HTTP_1_0));
    167         assertTrue((new HttpVersion(1, 1)).equals(HttpVersion.HTTP_1_1));
    168         assertFalse((new HttpVersion(1, 1)).equals(HttpVersion.HTTP_1_0));
    169     }
    170
    171     public void testHttpVersionComparison() {
    172         assertTrue(HttpVersion.HTTP_0_9.lessEquals(HttpVersion.HTTP_1_1));
    173         assertTrue(HttpVersion.HTTP_0_9.greaterEquals(HttpVersion.HTTP_0_9));
    174         assertFalse(HttpVersion.HTTP_0_9.greaterEquals(HttpVersion.HTTP_1_0));
    175
    176         assertTrue(HttpVersion.HTTP_1_0.compareTo((Object)HttpVersion.HTTP_1_1) < 0);
    177         assertTrue(HttpVersion.HTTP_1_0.compareTo((Object)HttpVersion.HTTP_0_9) > 0);
    178         assertTrue(HttpVersion.HTTP_1_0.compareTo((Object)HttpVersion.HTTP_1_0) == 0);
    179    }
    180 }
    181