commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient TestEffectiveHttpVersion.java TestHttpVersion.java TestNoHost.java
Date Sun, 09 May 2004 12:16:12 GMT
olegk       2004/05/09 05:16:12

  Modified:    httpclient/src/test/org/apache/commons/httpclient
                        TestHttpVersion.java TestNoHost.java
  Added:       httpclient/src/test/org/apache/commons/httpclient
                        TestEffectiveHttpVersion.java
  Log:
  Additional test coverage for the HttpVersion class and effective protocol version logic
  
  Contributed by Oleg Kalnichevski
  
  Revision  Changes    Path
  1.3       +63 -7     jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpVersion.java
  
  Index: TestHttpVersion.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpVersion.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestHttpVersion.java	22 Feb 2004 18:08:49 -0000	1.2
  +++ TestHttpVersion.java	9 May 2004 12:16:12 -0000	1.3
  @@ -57,6 +57,21 @@
   
       // ------------------------------------------------------------------ Tests
       
  +    public void testHttpVersionInvalidConstructorInput() throws Exception {
  +        try {
  +            HttpVersion ver = new HttpVersion(-1, -1); 
  +            fail("IllegalArgumentException should have been thrown");
  +        } catch (IllegalArgumentException e) {
  +            // expected
  +        }
  +        try {
  +            HttpVersion ver = new HttpVersion(0, -1); 
  +            fail("IllegalArgumentException should have been thrown");
  +        } catch (IllegalArgumentException e) {
  +            // expected
  +        }
  +    }
  +
       public void testHttpVersionParsing() throws Exception {
           String s = "HTTP/1.1";
           HttpVersion version = HttpVersion.parse(s);
  @@ -73,6 +88,12 @@
   
       public void testInvalidHttpVersionParsing() throws Exception {
           try {
  +            HttpVersion.parse(null);
  +            fail("IllegalArgumentException should have been thrown");
  +        } catch (IllegalArgumentException e) {
  +            //expected
  +        }
  +        try {
               HttpVersion.parse("crap");
               fail("ProtocolException should have been thrown");
           } catch (ProtocolException e) {
  @@ -108,17 +129,52 @@
           } catch (ProtocolException e) {
               //expected
           }
  +        try {
  +            HttpVersion.parse("HTTP/whatever.whatever whatever");
  +            fail("ProtocolException should have been thrown");
  +        } catch (ProtocolException e) {
  +            //expected
  +        }
  +        try {
  +            HttpVersion.parse("HTTP/1.whatever whatever");
  +            fail("ProtocolException should have been thrown");
  +        } catch (ProtocolException e) {
  +            //expected
  +        }
       }
   
  -    public void testHttpVersionComparison() {
  +    public void testHttpVersionEquality() throws Exception {
  +        HttpVersion ver1 = new HttpVersion(1, 1); 
  +        HttpVersion ver2 = new HttpVersion(1, 1); 
  +        
  +        assertEquals(ver1.hashCode(), ver2.hashCode());
  +        assertTrue(ver1.equals(ver1));
  +        assertTrue(ver1.equals(ver2));
  +        assertTrue(ver1.equals((Object)ver1));
  +        assertTrue(ver1.equals((Object)ver2));
  +
  +        assertFalse(ver1.equals(new Float(1.1)));
  +        
  +        try {
  +            ver1.equals(null);
  +            fail("IllegalArgumentException should have been thrown");
  +        } catch (IllegalArgumentException e) {
  +        }
  +
           assertTrue((new HttpVersion(0, 9)).equals(HttpVersion.HTTP_0_9));
           assertTrue((new HttpVersion(1, 0)).equals(HttpVersion.HTTP_1_0));
           assertTrue((new HttpVersion(1, 1)).equals(HttpVersion.HTTP_1_1));
           assertFalse((new HttpVersion(1, 1)).equals(HttpVersion.HTTP_1_0));
  +    }
  +
  +    public void testHttpVersionComparison() {
           assertTrue(HttpVersion.HTTP_0_9.lessEquals(HttpVersion.HTTP_1_1));
           assertTrue(HttpVersion.HTTP_0_9.greaterEquals(HttpVersion.HTTP_0_9));
           assertFalse(HttpVersion.HTTP_0_9.greaterEquals(HttpVersion.HTTP_1_0));
  -    }
  -
  +        
  +        assertTrue(HttpVersion.HTTP_1_0.compareTo((Object)HttpVersion.HTTP_1_1) < 0);
  +        assertTrue(HttpVersion.HTTP_1_0.compareTo((Object)HttpVersion.HTTP_0_9) > 0);
  +        assertTrue(HttpVersion.HTTP_1_0.compareTo((Object)HttpVersion.HTTP_1_0) == 0);
  +   }
   }
   
  
  
  
  1.37      +5 -4      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java
  
  Index: TestNoHost.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java,v
  retrieving revision 1.36
  retrieving revision 1.37
  diff -u -r1.36 -r1.37
  --- TestNoHost.java	25 Apr 2004 21:49:35 -0000	1.36
  +++ TestNoHost.java	9 May 2004 12:16:12 -0000	1.37
  @@ -84,6 +84,7 @@
           suite.addTest(TestMethodCharEncoding.suite());
           suite.addTest(TestExceptions.suite());
           suite.addTest(TestHttpVersion.suite());
  +        suite.addTest(TestEffectiveHttpVersion.suite());
           suite.addTest(TestHttpParser.suite());
           suite.addTest(TestBadContentLength.suite());
           suite.addTest(TestEquals.suite());
  
  
  
  1.1                  jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestEffectiveHttpVersion.java
  
  Index: TestEffectiveHttpVersion.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestEffectiveHttpVersion.java,v
1.1 2004/05/09 12:16:12 olegk Exp $
   * $Revision: 1.1 $
   * $Date: 2004/05/09 12:16:12 $
   * ====================================================================
   *
   *  Copyright 1999-2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  package org.apache.commons.httpclient;
  import java.io.IOException;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.apache.commons.httpclient.methods.GetMethod;
  import org.apache.commons.httpclient.params.HttpMethodParams;
  import org.apache.commons.httpclient.server.HttpService;
  import org.apache.commons.httpclient.server.SimpleRequest;
  import org.apache.commons.httpclient.server.SimpleResponse;
  
  /**
   * HTTP protocol versioning tests.
   *
   * @author Oleg Kalnichevski
   * 
   * @version $Revision: 1.1 $
   */
  public class TestEffectiveHttpVersion extends HttpClientTestBase {
  
      // ------------------------------------------------------------ Constructor
      public TestEffectiveHttpVersion(String testName) {
          super(testName);
      }
  
      // ------------------------------------------------------------------- Main
      public static void main(String args[]) {
          String[] testCaseName = { TestEffectiveHttpVersion.class.getName() };
          junit.textui.TestRunner.main(testCaseName);
      }
  
      // ------------------------------------------------------- TestCase Methods
  
      public static Test suite() {
          return new TestSuite(TestEffectiveHttpVersion.class);
      }
  
      private class EchoService implements HttpService {
  
          public EchoService() {
              super();
          }
  
          public boolean process(final SimpleRequest request, final SimpleResponse response)
              throws IOException
          {
              String protocol = request.getRequestLine().getProtocol();
              response.setStatusLine(protocol + " 200 OK");
              response.setBodyString(request.getBodyString());
              return true;
          }
      }
  
      public void testClientLevelHttpVersion() throws IOException {
          this.server.setHttpService(new EchoService());
  
          HttpVersion testver = new HttpVersion(1, 10);
  
          this.client.getParams().setVersion(testver);
          GetMethod httpget = new GetMethod("/test/");
          try {
              this.client.executeMethod(httpget);
          } finally {
              httpget.releaseConnection();
          }
          assertEquals(testver, httpget.getEffectiveVersion());
      }
  
      public void testMethodLevelHttpVersion() throws IOException {
          this.server.setHttpService(new EchoService());
  
          HttpVersion globalver = new HttpVersion(1, 10);
          HttpVersion testver1 = new HttpVersion(1, 11);
          HttpVersion testver2 = new HttpVersion(1, 12);
  
          this.client.getParams().setVersion(globalver);
          
          GetMethod httpget1 = new GetMethod("/test/");
          httpget1.getParams().setVersion(testver1);
          try {
              this.client.executeMethod(httpget1);
          } finally {
              httpget1.releaseConnection();
          }
          assertEquals(testver1, httpget1.getEffectiveVersion());
  
          GetMethod httpget2 = new GetMethod("/test/");
          httpget2.getParams().setVersion(testver2);
          try {
              this.client.executeMethod(httpget2);
          } finally {
              httpget2.releaseConnection();
          }
          assertEquals(testver2, httpget2.getEffectiveVersion());
  
          GetMethod httpget3 = new GetMethod("/test/");
          try {
              this.client.executeMethod(httpget3);
          } finally {
              httpget3.releaseConnection();
          }
          assertEquals(globalver, httpget3.getEffectiveVersion());
      }
  
      public void testHostLevelHttpVersion() throws IOException {
          this.server.setHttpService(new EchoService());
  
          HttpVersion testver = new HttpVersion(1, 11);
          HttpVersion hostver = new HttpVersion(1, 12);
  
          this.client.getParams().setVersion(testver);
          
          GetMethod httpget1 = new GetMethod("/test/");
          httpget1.getParams().setVersion(testver);
          
          HostConfiguration hostconf = new HostConfiguration();
          hostconf.setHost(this.server.getLocalAddress(), this.server.getLocalPort(), "http");

          try {
              this.client.executeMethod(hostconf, httpget1);
          } finally {
              httpget1.releaseConnection();
          }
          assertEquals(testver, httpget1.getEffectiveVersion());
  
          GetMethod httpget2 = new GetMethod("/test/");
          hostconf.setHost(this.server.getLocalAddress(), this.server.getLocalPort(), "http");
          hostconf.getParams().setParameter(HttpMethodParams.PROTOCOL_VERSION, hostver); 
          try {
              this.client.executeMethod(hostconf, httpget2);
          } finally {
              httpget2.releaseConnection();
          }
          assertEquals(hostver, httpget2.getEffectiveVersion());
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message