hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r321318 - in /jakarta/httpclient/trunk/http-common/src: java/org/apache/http/util/HeadersParser.java java/org/apache/http/util/ParameterFormatter.java test/org/apache/http/util/TestParameterFormatter.java
Date Sat, 15 Oct 2005 09:50:44 GMT
Author: olegk
Date: Sat Oct 15 02:50:35 2005
New Revision: 321318

URL: http://svn.apache.org/viewcvs?rev=321318&view=rev
Log:
* ParameterFormatter made static method only
* More test cases

Modified:
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/HeadersParser.java
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/ParameterFormatter.java
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestParameterFormatter.java

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/HeadersParser.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/HeadersParser.java?rev=321318&r1=321317&r2=321318&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/HeadersParser.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/HeadersParser.java
Sat Oct 15 02:50:35 2005
@@ -50,7 +50,6 @@
 public class HeadersParser  {
 
     private HeadersParser() {
-        super();
     }
     
     public static Header[] processHeaders(final HttpDataReceiver datareceiver) 

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/ParameterFormatter.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/ParameterFormatter.java?rev=321318&r1=321317&r2=321318&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/ParameterFormatter.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/ParameterFormatter.java
Sat Oct 15 02:50:35 2005
@@ -103,15 +103,7 @@
             '"', '\\'
             };
     
-    /**
-     * This flag determines whether all parameter values must be enclosed in 
-     * quotation marks, even if they do not contain any special characters
-     */
-    private boolean alwaysUseQuotes = true;
-    
-    /** Default ParameterFormatter constructor */
-    public ParameterFormatter() {
-        super();
+    private ParameterFormatter() {
     }
     
     private static boolean isOneOf(char[] chars, char ch) {
@@ -132,27 +124,6 @@
     }
 
     /**
-     * Determines whether all parameter values must be enclosed in quotation 
-     * marks, even if they do not contain any special characters
-     * 
-     * @return <tt>true</tt> if all parameter values must be enclosed in 
-     * quotation marks, <tt>false</tt> otherwise
-     */
-    public boolean isAlwaysUseQuotes() {
-        return alwaysUseQuotes;
-    }
-    
-    /**
-     * Defines whether all parameter values must be enclosed in quotation 
-     * marks, even if they do not contain any special characters
-     * 
-     * @param alwaysUseQuotes
-     */
-    public void setAlwaysUseQuotes(boolean alwaysUseQuotes) {
-        this.alwaysUseQuotes = alwaysUseQuotes;
-    }
-    
-    /**
      * Formats the given parameter value using formatting rules defined
      * in RFC 2616 
      * 
@@ -163,8 +134,10 @@
      * characters<tt>, false</tt> only if the parameter value contains 
      * potentially unsafe special characters
      */
-    public static void formatValue(
-            final StringBuffer buffer, final String value, boolean alwaysUseQuotes) {
+    public static void format(
+            final StringBuffer buffer, 
+            final String value, 
+            boolean alwaysUseQuotes) {
         if (buffer == null) {
             throw new IllegalArgumentException("String buffer may not be null");
         }
@@ -207,8 +180,10 @@
      *  
      * @param buffer output buffer 
      * @param param the parameter to be formatted
+     * @param alwaysUseQuotes <tt>true</tt> if the parameter values must 
+     * always be enclosed in quotation marks, <tt>false</tt> otherwise
      */
-    public void format(final StringBuffer buffer, final NameValuePair param) {
+    public static void format(final StringBuffer buffer, final NameValuePair param, boolean
alwaysUseQuotes) {
         if (buffer == null) {
             throw new IllegalArgumentException("String buffer may not be null");
         }
@@ -219,7 +194,7 @@
         String value = param.getValue();
         if (value != null) {
             buffer.append("=");
-            formatValue(buffer, value, this.alwaysUseQuotes);
+            format(buffer, value, alwaysUseQuotes);
         }
     }
     
@@ -228,13 +203,15 @@
      * formatting rules defined in RFC 2616
      *  
      * @param param the parameter to be formatted
+     * @param alwaysUseQuotes <tt>true</tt> if the parameter values must 
+     * always be enclosed in quotation marks, <tt>false</tt> otherwise
      * 
      * @return RFC 2616 conformant textual representaion of the 
      * attribute/value pair
      */
-    public String format(final NameValuePair param) {
+    public static String format(final NameValuePair param, boolean alwaysUseQuotes) {
         StringBuffer buffer = new StringBuffer();
-        format(buffer, param);
+        format(buffer, param, alwaysUseQuotes);
         return buffer.toString();
     }
 

Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestParameterFormatter.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestParameterFormatter.java?rev=321318&r1=321317&r2=321318&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestParameterFormatter.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestParameterFormatter.java
Sat Oct 15 02:50:35 2005
@@ -60,30 +60,56 @@
     }
 
     public void testBasicValueFormatting() throws Exception {
-        ParameterFormatter formatter = new ParameterFormatter();
-        
         NameValuePair param1 = new NameValuePair("param", "regular_stuff"); 
         NameValuePair param2 = new NameValuePair("param", "this\\that"); 
         NameValuePair param3 = new NameValuePair("param", "this,that"); 
         NameValuePair param4 = new NameValuePair("param", "quote marks (\") must be escaped");

         NameValuePair param5 = new NameValuePair("param", "back slash (\\) must be escaped
too"); 
         NameValuePair param6 = new NameValuePair("param", "values with\tblanks must always
be quoted"); 
+        NameValuePair param7 = new NameValuePair("param", null); 
         
-        formatter.setAlwaysUseQuotes(false);
-        assertEquals("param=regular_stuff", formatter.format(param1));
-        assertEquals("param=\"this\\\\that\"", formatter.format(param2));
-        assertEquals("param=\"this,that\"", formatter.format(param3));
-        assertEquals("param=\"quote marks (\\\") must be escaped\"", formatter.format(param4));
-        assertEquals("param=\"back slash (\\\\) must be escaped too\"", formatter.format(param5));
-        assertEquals("param=\"values with\tblanks must always be quoted\"", formatter.format(param6));
+        assertEquals("param=regular_stuff", ParameterFormatter.format(param1, false));
+        assertEquals("param=\"this\\\\that\"", ParameterFormatter.format(param2, false));
+        assertEquals("param=\"this,that\"", ParameterFormatter.format(param3, false));
+        assertEquals("param=\"quote marks (\\\") must be escaped\"", ParameterFormatter.format(param4,
false));
+        assertEquals("param=\"back slash (\\\\) must be escaped too\"", ParameterFormatter.format(param5,
false));
+        assertEquals("param=\"values with\tblanks must always be quoted\"", ParameterFormatter.format(param6,
false));
+        assertEquals("param", ParameterFormatter.format(param7, false));
+
+        assertEquals("param=\"regular_stuff\"", ParameterFormatter.format(param1, true));
+        assertEquals("param=\"this\\\\that\"", ParameterFormatter.format(param2, true));
+        assertEquals("param=\"this,that\"", ParameterFormatter.format(param3, true));
+        assertEquals("param=\"quote marks (\\\") must be escaped\"", ParameterFormatter.format(param4,
true));
+        assertEquals("param=\"back slash (\\\\) must be escaped too\"", ParameterFormatter.format(param5,
true));
+        assertEquals("param=\"values with\tblanks must always be quoted\"", ParameterFormatter.format(param6,
true));
+        assertEquals("param", ParameterFormatter.format(param7, false));
+    }
 
-        formatter.setAlwaysUseQuotes(true);
-        assertEquals("param=\"regular_stuff\"", formatter.format(param1));
-        assertEquals("param=\"this\\\\that\"", formatter.format(param2));
-        assertEquals("param=\"this,that\"", formatter.format(param3));
-        assertEquals("param=\"quote marks (\\\") must be escaped\"", formatter.format(param4));
-        assertEquals("param=\"back slash (\\\\) must be escaped too\"", formatter.format(param5));
-        assertEquals("param=\"values with\tblanks must always be quoted\"", formatter.format(param6));
+    public void testInvalidInput() throws Exception {
+        try {
+        	ParameterFormatter.format(null, new NameValuePair("param", "value"), true);
+            fail("IllegalArgumentException should habe been thrown");
+        } catch (IllegalArgumentException ex) {
+        	// expected
+        }
+        try {
+        	ParameterFormatter.format(null, "ssss", true);
+            fail("IllegalArgumentException should habe been thrown");
+        } catch (IllegalArgumentException ex) {
+        	// expected
+        }
+        try {
+        	ParameterFormatter.format(new StringBuffer(), (NameValuePair) null, true);
+            fail("IllegalArgumentException should habe been thrown");
+        } catch (IllegalArgumentException ex) {
+        	// expected
+        }
+        try {
+        	ParameterFormatter.format(new StringBuffer(), (String) null, true);
+            fail("IllegalArgumentException should habe been thrown");
+        } catch (IllegalArgumentException ex) {
+        	// expected
+        }
     }
     
 }



Mime
View raw message