wink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r935422 - in /incubator/wink/trunk/wink-common/src: main/java/org/apache/wink/common/internal/http/AcceptCharset.java test/java/org/apache/wink/common/internal/providers/header/AcceptCharsetHeaderDelegateTest.java
Date Sun, 18 Apr 2010 22:04:04 GMT
Author: bluk
Date: Sun Apr 18 22:04:04 2010
New Revision: 935422

URL: http://svn.apache.org/viewvc?rev=935422&view=rev
Log:
Fix an ordering problem in the AcceptCharset

ISO-8859-1 should be ordered first in various
cases for Accept-Charset header.  q-value = 1.0

This only occurs if no wildcard is in the
Accept-Charset.

See HTTP spec for complicated rules.

Added:
    incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/providers/header/AcceptCharsetHeaderDelegateTest.java
  (with props)
Modified:
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/http/AcceptCharset.java

Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/http/AcceptCharset.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/http/AcceptCharset.java?rev=935422&r1=935421&r2=935422&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/http/AcceptCharset.java
(original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/http/AcceptCharset.java
Sun Apr 18 22:04:04 2010
@@ -52,6 +52,43 @@ public class AcceptCharset {
         public boolean isWildcard() {
             return charset == null;
         }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (!(obj instanceof ValuedCharset)) {
+                return false;
+            }
+
+            ValuedCharset other = (ValuedCharset)obj;
+            if (other.qValue != this.qValue) {
+                return false;
+            }
+
+            if (charset == null) {
+                if (other.charset != null) {
+                    return false;
+                }
+            } else {
+                if (!charset.equals(other.charset)) {
+                    return false;
+                }
+            }
+
+            return true;
+        }
+
+        @Override
+        public int hashCode() {
+            int result = 17;
+            result = 31 * result + Double.valueOf(qValue).hashCode();
+            result = 31 * result + charset.hashCode();
+            return result;
+        }
+
+        @Override
+        public String toString() {
+            return charset + ";q=" + qValue;
+        }
     }
 
     private static final HeaderDelegate<AcceptCharset> delegate =
@@ -82,12 +119,12 @@ public class AcceptCharset {
         }
         if (!isISO8859Explicit && !anyAllowed) {
             ArrayList<String> acceptableCharsetsTemp = new ArrayList<String>(acceptableCharsets);
-            acceptableCharsetsTemp.add("ISO-8859-1"); //$NON-NLS-1$
+            acceptableCharsetsTemp.add(0, "ISO-8859-1"); //$NON-NLS-1$
             this.acceptable = Collections.unmodifiableList(acceptableCharsetsTemp);
 
             ArrayList<AcceptCharset.ValuedCharset> valuedCharsetsTemp =
                 new ArrayList<ValuedCharset>(valuedCharsets);
-            valuedCharsetsTemp.add(new AcceptCharset.ValuedCharset(1.0d, "ISO-8859-1"));
//$NON-NLS-1$
+            valuedCharsetsTemp.add(0, new AcceptCharset.ValuedCharset(1.0d, "ISO-8859-1"));
//$NON-NLS-1$
             this.valuedCharsets = Collections.unmodifiableList(valuedCharsetsTemp);
         } else {
             this.acceptable = Collections.unmodifiableList(acceptableCharsets);

Added: incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/providers/header/AcceptCharsetHeaderDelegateTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/providers/header/AcceptCharsetHeaderDelegateTest.java?rev=935422&view=auto
==============================================================================
--- incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/providers/header/AcceptCharsetHeaderDelegateTest.java
(added)
+++ incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/providers/header/AcceptCharsetHeaderDelegateTest.java
Sun Apr 18 22:04:04 2010
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ *  
+ */
+package org.apache.wink.common.internal.providers.header;
+
+import static org.junit.Assert.fail;
+import static org.junit.Assert.*;
+
+import java.util.Arrays;
+import java.util.Collections;
+
+import javax.ws.rs.ext.RuntimeDelegate;
+import javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate;
+
+import org.apache.wink.common.internal.http.AcceptCharset;
+import org.junit.Before;
+import org.junit.Test;
+
+public class AcceptCharsetHeaderDelegateTest {
+    private HeaderDelegate<AcceptCharset> acceptCharsetDelegate;
+
+    @Before
+    public void setup() {
+        RuntimeDelegate rd = RuntimeDelegate.getInstance();
+        acceptCharsetDelegate = rd.createHeaderDelegate(AcceptCharset.class);
+    }
+
+    @Test
+    public void testHeaderDelegateExists() {
+        if (acceptCharsetDelegate == null) {
+            fail("AcceptCharsetDelegate header delegate is not registered in RuntimeDelegateImpl");
+        }
+    }
+
+    @Test
+    public void testFromStringNull() {
+        AcceptCharset charset = acceptCharsetDelegate.fromString(null);
+        assertTrue(charset.isAnyCharsetAllowed());
+        assertEquals(Collections.EMPTY_LIST, charset.getAcceptableCharsets());
+        assertEquals(Collections.EMPTY_LIST, charset.getBannedCharsets());
+    }
+
+    @Test
+    public void testFromStringEmpty() {
+        AcceptCharset charset = acceptCharsetDelegate.fromString("");
+        assertFalse(charset.isAnyCharsetAllowed());
+        assertEquals(Collections.singletonList("ISO-8859-1"), charset.getAcceptableCharsets());
+        assertEquals(Collections.EMPTY_LIST, charset.getBannedCharsets());
+    }
+
+    @Test
+    public void testFromStringUTF8Only() {
+        AcceptCharset charset = acceptCharsetDelegate.fromString("utf-8");
+        assertFalse(charset.isAnyCharsetAllowed());
+        // see HTTP Accept-Char set
+        assertEquals(Arrays.asList(new String[] {"ISO-8859-1", "utf-8"}), charset
+            .getAcceptableCharsets());
+        assertEquals(Collections.EMPTY_LIST, charset.getBannedCharsets());
+    }
+
+    @Test
+    public void testFromStringUTF8AndWildcard() {
+        AcceptCharset charset = acceptCharsetDelegate.fromString("utf-8,*");
+        assertTrue(charset.isAnyCharsetAllowed());
+        assertEquals(Arrays.asList(new String[] {"utf-8"}), charset.getAcceptableCharsets());
+        // note that any charset allowed means ISO-8859-1 is part of wildcard,
+        // see HTTP spec
+        assertEquals(Collections.EMPTY_LIST, charset.getBannedCharsets());
+    }
+
+    @Test
+    public void testFromStringUTF8WithQuality() {
+        AcceptCharset charset = acceptCharsetDelegate.fromString("utf-8;q=0.8");
+        assertFalse(charset.isAnyCharsetAllowed());
+        assertEquals(Arrays.asList(new String[] {"ISO-8859-1", "utf-8"}), charset
+            .getAcceptableCharsets());
+        assertEquals(Arrays.asList(new AcceptCharset.ValuedCharset[] {
+            new AcceptCharset.ValuedCharset(1.0, "ISO-8859-1"),
+            new AcceptCharset.ValuedCharset(0.8, "utf-8")}), charset.getValuedCharsets());
+        assertEquals(Collections.EMPTY_LIST, charset.getBannedCharsets());
+    }
+
+    @Test
+    public void testFromStringUTF8WithQualityAndWildcardWithQuality() {
+        AcceptCharset charset = acceptCharsetDelegate.fromString("utf-8;q=0.8, *;q=0.4");
+        assertTrue(charset.isAnyCharsetAllowed());
+        assertEquals(Arrays.asList(new String[] {"utf-8"}), charset.getAcceptableCharsets());
+        assertEquals(Arrays.asList(new AcceptCharset.ValuedCharset[] {
+            new AcceptCharset.ValuedCharset(0.8, "utf-8"),
+            new AcceptCharset.ValuedCharset(0.4, null)}), charset.getValuedCharsets());
+        // note that any charset allowed means ISO-8859-1 is part of wildcard,
+        // see HTTP spec
+        assertEquals(Collections.EMPTY_LIST, charset.getBannedCharsets());
+    }
+
+}

Propchange: incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/providers/header/AcceptCharsetHeaderDelegateTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message