abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r1222382 - in /abdera/abdera2: common/src/main/java/org/apache/abdera2/common/http/Preference.java test/src/main/java/org/apache/abdera2/test/common/http/PreferenceTest.java
Date Thu, 22 Dec 2011 18:07:57 GMT
Author: jmsnell
Date: Thu Dec 22 18:07:57 2011
New Revision: 1222382

URL: http://svn.apache.org/viewvc?rev=1222382&view=rev
Log:
fixes and test cases

Modified:
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/http/Preference.java
    abdera/abdera2/test/src/main/java/org/apache/abdera2/test/common/http/PreferenceTest.java

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/http/Preference.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/http/Preference.java?rev=1222382&r1=1222381&r2=1222382&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/http/Preference.java (original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/http/Preference.java Thu
Dec 22 18:07:57 2011
@@ -282,22 +282,22 @@ public class Preference implements Seria
   
   public int getIntParam(String name) {
     String val = getParam(name);
-    return val != null ? Integer.parseInt(name) : -1;
+    return val != null ? Integer.parseInt(val) : -1;
   }
   
   public long getLongParam(String name) {
     String val = getParam(name);
-    return val != null ? Long.parseLong(name) : -1;
+    return val != null ? Long.parseLong(val) : -1;
   }
   
   public short getShortParam(String name) {
     String val = getParam(name);
-    return val != null ? Short.parseShort(name) : -1;
+    return val != null ? Short.parseShort(val) : -1;
   }
   
   public boolean getBooleanParam(String name) {
     String val = getParam(name);
-    return val != null ? Boolean.parseBoolean(name) : false;
+    return val != null ? Boolean.parseBoolean(val) : false;
   }
   
   public String getParam(String name) {
@@ -359,12 +359,11 @@ public class Preference implements Seria
         String pref = matcher.group(1);
         String params = matcher.group(2);
         String token = null, tokenval = null;
-        
         if (pref != null) {
           String[] ps = pref.split("\\s*\\*?=\\s*", 2);
           token = ps[0].trim();
           if (ps.length == 2)
-            tokenval = Codec.decode(CharUtils.unquote(CharUtils.unescape(ps[1])));
+            tokenval = Codec.decode(CharUtils.unescape(CharUtils.unquote(ps[1])));
         }
         
         Preference.Builder maker = 
@@ -375,7 +374,7 @@ public class Preference implements Seria
             String p = mparams.group(1);
             String[] ps = p.split("\\s*\\*?=\\s*", 2);
             if (ps.length == 2)
-              maker.param(ps[0], Codec.decode(CharUtils.unquote(CharUtils.unescape(ps[1]))));
+              maker.param(ps[0], Codec.decode(CharUtils.unescape(CharUtils.unquote(ps[1]))));
             else maker.param(ps[0]);
           }
         }

Modified: abdera/abdera2/test/src/main/java/org/apache/abdera2/test/common/http/PreferenceTest.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/test/src/main/java/org/apache/abdera2/test/common/http/PreferenceTest.java?rev=1222382&r1=1222381&r2=1222382&view=diff
==============================================================================
--- abdera/abdera2/test/src/main/java/org/apache/abdera2/test/common/http/PreferenceTest.java
(original)
+++ abdera/abdera2/test/src/main/java/org/apache/abdera2/test/common/http/PreferenceTest.java
Thu Dec 22 18:07:57 2011
@@ -4,6 +4,8 @@ import org.apache.abdera2.common.http.Pr
 import static org.apache.abdera2.common.http.Preference.*;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertThat;
+import static org.hamcrest.CoreMatchers.*;
 
 import org.junit.Test;
 
@@ -42,4 +44,100 @@ public class PreferenceTest {
     
   }
   
+  @Test
+  public void simplepref() {
+    Iterable<Preference> list = parse("lenient");
+    assertEquals(1,Iterables.size(list));
+    Preference pref = list.iterator().next();
+    assertEquals("lenient",pref.getToken());
+  }
+  
+  @Test
+  public void simpleprefval() {
+    Iterable<Preference> list = parse("wait=10");
+    assertEquals(1,Iterables.size(list));
+    Preference pref = list.iterator().next();
+    assertEquals("wait",pref.getToken());
+    assertEquals(10,pref.getIntValue());
+  }
+  
+  @Test
+  public void simpleprefvalq() {
+    Iterable<Preference> list = parse("foo=\"bar\"");
+    assertEquals(1,Iterables.size(list));
+    Preference pref = list.iterator().next();
+    assertEquals("foo",pref.getToken());
+    assertEquals("bar",pref.getValue());
+  }
+  
+  @Test
+  public void simpleprefparam() {
+    Iterable<Preference> list = parse("foo; bar");
+    assertEquals(1,Iterables.size(list));
+    Preference pref = list.iterator().next();
+    assertEquals("foo", pref.getToken());
+    assertTrue(pref.hasParam("bar"));
+    assertEquals("",pref.getParam("bar"));
+  }
+  
+  @Test
+  public void simpleprefparamval() {
+    Iterable<Preference> list = parse("foo; bar=1");
+    assertEquals(1,Iterables.size(list));
+    Preference pref = list.iterator().next();
+    assertEquals("foo", pref.getToken());
+    assertTrue(pref.hasParam("bar"));
+    assertEquals(1,pref.getIntParam("bar"));
+  }
+  
+  @Test
+  public void simpleprefparamvalq() {
+    Iterable<Preference> list = parse("foo; bar=\"testing \\\" testing \\\\\"");
+    assertEquals(1,Iterables.size(list));
+    Preference pref = list.iterator().next();
+    assertEquals("foo", pref.getToken());
+    assertTrue(pref.hasParam("bar"));
+    assertEquals("testing \" testing \\",pref.getParam("bar"));
+  }
+  
+  @Test
+  public void simpleprefvalparamval() {
+    Iterable<Preference> list = parse("foo=10; bar=\"testing \\\" testing \\\\\"");
+    assertEquals(1,Iterables.size(list));
+    Preference pref = list.iterator().next();
+    assertEquals("foo", pref.getToken());
+    assertEquals(10, pref.getIntValue());
+    assertTrue(pref.hasParam("bar"));
+    assertEquals("testing \" testing \\",pref.getParam("bar"));
+  }
+  
+  @SuppressWarnings("unchecked")
+  @Test
+  public void multipref() {
+    Iterable<Preference> list = parse("lenient, wait=10; bar=\"foo\"");
+    assertEquals(2, Iterables.size(list));
+    for (Preference pref : list) {
+      assertThat(pref.getToken(),anyOf(is("lenient"),is("wait")));
+      if ("wait".equals(pref.getToken())) {
+        assertEquals(10,pref.getIntValue());
+        assertTrue(pref.hasParam("bar"));
+        assertEquals("foo",pref.getParam("bar"));
+      }
+    }
+  }
+  
+  @SuppressWarnings("unchecked")
+  @Test
+  public void multiprefr() {
+    Iterable<Preference> list = parse("wait=10; bar=\"foo\", lenient");
+    assertEquals(2, Iterables.size(list));
+    for (Preference pref : list) {
+      assertThat(pref.getToken(),anyOf(is("lenient"),is("wait")));
+      if ("wait".equals(pref.getToken())) {
+        assertEquals(10,pref.getIntValue());
+        assertTrue(pref.hasParam("bar"));
+        assertEquals("foo",pref.getParam("bar"));
+      }
+    }
+  }
 }



Mime
View raw message