hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r157293 - in jakarta/httpclient/trunk/http-common/src: java/org/apache/http/HeaderElement.java test/org/apache/http/TestHeader.java test/org/apache/http/TestHeaderElement.java test/org/apache/http/TestNameValuePair.java
Date Sat, 12 Mar 2005 22:53:58 GMT
Author: olegk
Date: Sat Mar 12 14:53:57 2005
New Revision: 157293

URL: http://svn.apache.org/viewcvs?view=rev&rev=157293
Log:
More test coverage

Modified:
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HeaderElement.java
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestHeader.java
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestHeaderElement.java
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestNameValuePair.java

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HeaderElement.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HeaderElement.java?view=diff&r1=157292&r2=157293
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HeaderElement.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HeaderElement.java Sat Mar
12 14:53:57 2005
@@ -91,9 +91,9 @@
     /**
      * Constructor with name, value and parameters.
      *
-     * @param name my name
-     * @param value my (possibly <tt>null</tt>) value
-     * @param parameters my (possibly <tt>null</tt>) parameters
+     * @param name header element name
+     * @param value header element value. May be <tt>null</tt>
+     * @param parameters header element parameters. May be <tt>null</tt>
      */
     public HeaderElement(
             final String name, 
@@ -105,16 +105,20 @@
         }
         this.name = name;
         this.value = value;
-        this.parameters = parameters;
+        if (parameters != null) {
+            this.parameters = parameters;
+        } else {
+            this.parameters = new NameValuePair[] {};
+        }
     }
 
     /**
      * Constructor with name and value.
      * 
-     * @param name my name
-     * @param value my (possibly <tt>null</tt>) value
+     * @param name header element name
+     * @param value header element value. May be <tt>null</tt>
      */
-    public HeaderElement(String name, String value) {
+    public HeaderElement(final String name, final String value) {
        this(name, value, null);
     }
 
@@ -144,7 +148,7 @@
         } else {
             this.name = "";
             this.value = null;
-            this.parameters = null;
+            this.parameters = new NameValuePair[] {};
         }
     }
 
@@ -258,14 +262,11 @@
             throw new IllegalArgumentException("Name may not be null");
         } 
         NameValuePair found = null;
-        NameValuePair parameters[] = getParameters();
-        if (parameters != null) {
-            for (int i = 0; i < parameters.length; i++) {
-                NameValuePair current = parameters[ i ];
-                if (current.getName().equalsIgnoreCase(name)) {
-                    found = current;
-                    break;
-                }
+        for (int i = 0; i < this.parameters.length; i++) {
+            NameValuePair current = this.parameters[ i ];
+            if (current.getName().equalsIgnoreCase(name)) {
+                found = current;
+                break;
             }
         }
         return found;
@@ -288,10 +289,8 @@
         int hash = LangUtils.HASH_SEED;
         hash = LangUtils.hashCode(hash, this.name);
         hash = LangUtils.hashCode(hash, this.value);
-        if (this.parameters != null) {
-            for (int i = 0; i < this.parameters.length; i++) {
-                hash = LangUtils.hashCode(hash, this.parameters[i]);
-            }
+        for (int i = 0; i < this.parameters.length; i++) {
+            hash = LangUtils.hashCode(hash, this.parameters[i]);
         }
         return hash;
     }

Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestHeader.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestHeader.java?view=diff&r1=157292&r2=157293
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestHeader.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestHeader.java Sat Mar
12 14:53:57 2005
@@ -53,7 +53,7 @@
         return new TestSuite(TestHeader.class);
     }
 
-    public void testBasicGet() {
+    public void testConstructor() {
         Header header = new Header("name", "value", true);
         assertEquals("name", header.getName()); 
         assertEquals("value", header.getValue()); 
@@ -93,4 +93,20 @@
         Header header1 = new Header("name1", "value1");
         assertEquals("name1: value1", header1.toString());
     }
+    
+    public void testHeaderElements() {
+        Header header = new Header("name", "element1 = value1, element2; param1 = value1,
element3");
+        HeaderElement[] elements = header.getElements(); 
+        assertNotNull(elements); 
+        assertEquals(3, elements.length); 
+        assertEquals("element1", elements[0].getName()); 
+        assertEquals("value1", elements[0].getValue()); 
+        assertEquals("element2", elements[1].getName()); 
+        assertEquals(null, elements[1].getValue()); 
+        assertEquals("element3", elements[2].getName()); 
+        assertEquals(null, elements[2].getValue()); 
+        assertEquals(1, elements[1].getParameters().length); 
+
+    }    
+    
 }

Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestHeaderElement.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestHeaderElement.java?view=diff&r1=157292&r2=157293
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestHeaderElement.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestHeaderElement.java Sat
Mar 12 14:53:57 2005
@@ -60,6 +60,44 @@
         return new TestSuite(TestHeaderElement.class);
     }
 
+    public void testConstructor() throws Exception {
+        HeaderElement element = new HeaderElement("name", "value", 
+                new NameValuePair[] {
+                    new NameValuePair("param1", "value1"),
+                    new NameValuePair("param2", "value2")
+                } );
+        assertEquals("name", element.getName());
+        assertEquals("value", element.getValue());
+        assertEquals(2, element.getParameters().length);
+        assertEquals("value1", element.getParameterByName("param1").getValue());
+        assertEquals("value2", element.getParameterByName("param2").getValue());
+    }
+
+    public void testConstructor2() throws Exception {
+        HeaderElement element = new HeaderElement("name", "value");
+        assertEquals("name", element.getName());
+        assertEquals("value", element.getValue());
+        assertEquals(0, element.getParameters().length);
+    }
+
+    public void testCharArrayConstructor() throws Exception {
+        String s = "name = value; param1 = value1";
+        HeaderElement element = new HeaderElement(s.toCharArray()); 
+        assertEquals("name", element.getName());
+        assertEquals("value", element.getValue());
+        assertEquals(1, element.getParameters().length);
+        assertEquals("value1", element.getParameterByName("param1").getValue());
+    }
+    
+    public void testInvalidName() {
+        try {
+            HeaderElement element = new HeaderElement(null, null, null); 
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            //expected
+        }
+    }
+    
     public void testParseHeaderElements() throws Exception {
         // this is derived from the old main method in HeaderElement
         String headerValue = "name1 = value1; name2; name3=\"value3\" , name4=value4; " +
@@ -100,17 +138,92 @@
         assertEquals("Number of elements", 1, elements.length);
     }
 
-
     public void testFringeCase2() throws Exception {
         String headerValue = "name1 = value1, ";
         HeaderElement[] elements = HeaderElement.parseElements(headerValue);
         assertEquals("Number of elements", 1, elements.length);
     }
 
-
     public void testFringeCase3() throws Exception {
         String headerValue = ",, ,, ,";
         HeaderElement[] elements = HeaderElement.parseElements(headerValue);
         assertEquals("Number of elements", 0, elements.length);
     }
+    
+    public void testNullInput() throws Exception {
+        HeaderElement[] elements = HeaderElement.parseElements((char [])null);
+        assertNotNull(elements);
+        assertEquals("Number of elements", 0, elements.length);
+        elements = HeaderElement.parseElements((String)null);
+        assertNotNull(elements);
+        assertEquals("Number of elements", 0, elements.length);
+    }
+
+    public void testParamByName() throws Exception {
+        String s = "name = value; param1 = value1; param2 = value2";
+        HeaderElement element = new HeaderElement(s.toCharArray()); 
+        assertEquals("value1", element.getParameterByName("param1").getValue());
+        assertEquals("value2", element.getParameterByName("param2").getValue());
+        assertNull(element.getParameterByName("param3"));
+        try {
+            element.getParameterByName(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            //expected
+        }
+    }
+
+    public void testHashCode() {
+        HeaderElement element1 = new HeaderElement("name", "value", 
+                new NameValuePair[] {
+                    new NameValuePair("param1", "value1"),
+                    new NameValuePair("param2", "value2")
+                } );
+        HeaderElement element2 = new HeaderElement("name", "value", 
+                new NameValuePair[] {
+                    new NameValuePair("param2", "value2"),
+                    new NameValuePair("param1", "value1")
+                } );
+        HeaderElement element3 = new HeaderElement("name", "value"); 
+        HeaderElement element4 = new HeaderElement("name", "value"); 
+        HeaderElement element5 = new HeaderElement("name", "value", 
+                new NameValuePair[] {
+                    new NameValuePair("param1", "value1"),
+                    new NameValuePair("param2", "value2")
+                } );
+        assertTrue(element1.hashCode() != element2.hashCode());
+        assertTrue(element1.hashCode() != element3.hashCode());
+        assertTrue(element2.hashCode() != element3.hashCode());
+        assertTrue(element3.hashCode() == element4.hashCode());
+        assertTrue(element1.hashCode() == element5.hashCode());
+    }
+    
+    public void testEquals() {
+        HeaderElement element1 = new HeaderElement("name", "value", 
+                new NameValuePair[] {
+                    new NameValuePair("param1", "value1"),
+                    new NameValuePair("param2", "value2")
+                } );
+        HeaderElement element2 = new HeaderElement("name", "value", 
+                new NameValuePair[] {
+                    new NameValuePair("param2", "value2"),
+                    new NameValuePair("param1", "value1")
+                } );
+        HeaderElement element3 = new HeaderElement("name", "value"); 
+        HeaderElement element4 = new HeaderElement("name", "value"); 
+        HeaderElement element5 = new HeaderElement("name", "value", 
+                new NameValuePair[] {
+                    new NameValuePair("param1", "value1"),
+                    new NameValuePair("param2", "value2")
+                } );
+        assertTrue(element1.equals(element1));
+        assertTrue(!element1.equals(element2));
+        assertTrue(!element1.equals(element3));
+        assertTrue(!element2.equals(element3));
+        assertTrue(element3.equals(element4));
+        assertTrue(element1.equals(element5));
+        assertFalse(element1.equals(null));
+        assertFalse(element1.equals("name = value; param1 = value1; param2 = value2"));
+    }
+    
 }

Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestNameValuePair.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestNameValuePair.java?view=diff&r1=157292&r2=157293
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestNameValuePair.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/TestNameValuePair.java Sat
Mar 12 14:53:57 2005
@@ -53,7 +53,7 @@
         return new TestSuite(TestNameValuePair.class);
     }
 
-    public void testBasicGet() {
+    public void testConstructor() {
         NameValuePair param = new NameValuePair("name", "value");
         assertEquals("name", param.getName()); 
         assertEquals("value", param.getValue()); 



Mime
View raw message