abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r1221883 - in /abdera/abdera2: common/src/main/java/org/apache/abdera2/common/http/ test/src/main/java/org/apache/abdera2/test/activities/ test/src/main/java/org/apache/abdera2/test/client/ test/src/main/java/org/apache/abdera2/test/common/...
Date Wed, 21 Dec 2011 21:04:22 GMT
Author: jmsnell
Date: Wed Dec 21 21:04:22 2011
New Revision: 1221883

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

Added:
    abdera/abdera2/test/src/main/java/org/apache/abdera2/test/common/http/PreferenceTest.java
  (with props)
Modified:
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/http/Preference.java
    abdera/abdera2/test/src/main/java/org/apache/abdera2/test/activities/ActivitiesTest.java
    abdera/abdera2/test/src/main/java/org/apache/abdera2/test/client/AppTest.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=1221883&r1=1221882&r2=1221883&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 Wed
Dec 21 21:04:22 2011
@@ -17,9 +17,7 @@
  */
 package org.apache.abdera2.common.http;
 
-
 import java.io.Serializable;
-import java.util.Collection;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
@@ -96,6 +94,30 @@ public class Preference implements Seria
     return new Builder();
   }
   
+  public static Builder make(String token) {
+    return make().token(token.toLowerCase(Locale.US));
+  }
+  
+  public static Builder make(String token, String value) {
+    return make(token).value(value);
+  }
+
+  public static Builder make(String token, int value) {
+    return make(token).value(value);
+  }
+  
+  public static Builder make(String token, long value) {
+    return make(token).value(value);
+  }
+  
+  public static Builder make(String token, short value) {
+    return make(token).value(value);
+  }
+  
+  public static Builder make(String token, boolean value) {
+    return make(token).value(value);
+  }
+  
   public static class Builder implements Supplier<Preference> {
 
     String token;
@@ -188,19 +210,19 @@ public class Preference implements Seria
   }
   
   public long getLongValue() {
-    return Long.parseLong(value);
+    return value != null ? Long.parseLong(value) : -1;
   }
   
   public int getIntValue() {
-    return Integer.parseInt(value);
+    return value != null ? Integer.parseInt(value) : -1;
   }
   
   public short getShortValue() {
-    return Short.parseShort(value);
+    return value != null ? Short.parseShort(value) : -1;
   }
   
   public boolean getBooleanValue() {
-    return Boolean.parseBoolean(value);
+    return value != null ? Boolean.parseBoolean(value) : false;
   }
   
   static final Set<String> reserved = 
@@ -248,10 +270,35 @@ public class Preference implements Seria
     return true;
   }
 
+  public boolean hasParam(String name) {
+    checkNotNull(name);
+    name = name.toLowerCase(Locale.US);
+    return params.containsKey(name);
+  }
+  
+  public int getIntParam(String name) {
+    String val = getParam(name);
+    return val != null ? Integer.parseInt(name) : -1;
+  }
+  
+  public long getLongParam(String name) {
+    String val = getParam(name);
+    return val != null ? Long.parseLong(name) : -1;
+  }
+  
+  public short getShortParam(String name) {
+    String val = getParam(name);
+    return val != null ? Short.parseShort(name) : -1;
+  }
+  
+  public boolean getBooleanParam(String name) {
+    String val = getParam(name);
+    return val != null ? Boolean.parseBoolean(name) : false;
+  }
+  
   public String getParam(String name) {
     checkNotNull(name);
     name = name.toLowerCase(Locale.US);
-    checkArgument(!reserved(name));
     return params.get(name);
   }
   
@@ -290,15 +337,14 @@ public class Preference implements Seria
   }
   
   private final static String TOKEN = "[\\!\\#\\$\\%\\&\\'\\*\\+\\-\\.\\^\\_\\`\\|\\~a-zA-Z0-9]+";
-  private final static String PARAM = TOKEN+"\\s*={1}\\s*(?:(?:\"[^\"]+\")|(?:"+TOKEN+"))";
-  private final static String PREF = TOKEN+"(?:\\s*={1}\\s*(?:(?:\"[^\"]+\")|(?:"+TOKEN+"))){0,1}";
-  private final static String PARAMS = "(?:\\s*;\\s*(" + PARAM + "(?:\\s*;\\s*"+PARAM+")))*";
-  private final static String PATTERN = "("+PREF+")" + PARAMS;
+  private final static String PREF = TOKEN+"(?:\\s*=\\s*(?:(?:\"[^\"]+\")|(?:"+TOKEN+")))?";
+  private final static String PARAMS = "(?:\\s*;\\s*" + PREF + ")*";
+  private final static String PATTERN = "("+PREF+")(" + PARAMS + ")";
 
   private final static Pattern pattern = 
     Pattern.compile(PATTERN);
   private final static Pattern param = 
-    Pattern.compile("("+PARAM+")");
+    Pattern.compile("("+PREF+")");
   
   public static Iterable<Preference> parse(String text) {
     ImmutableList.Builder<Preference> prefs = ImmutableList.builder();
@@ -376,14 +422,13 @@ public class Preference implements Seria
   
   /**
    * Utility method that checks to see if the given token is included
-   * in the collection of preference
+   * in the collection of preference... this ignores the parameters
+   * and looks only at the preference token
    */
   public static boolean contains(
     Iterable<Preference> preferences, 
     Preference preference) {
-      return preferences instanceof Collection ?
-        ((Collection<Preference>)preferences).contains(preference) :
-        contains(preferences,preference.getToken());
+      return get(preferences,preference.getToken()) != null;
   }
   
   public static Preference get(Iterable<Preference> preferences, String token) {

Modified: abdera/abdera2/test/src/main/java/org/apache/abdera2/test/activities/ActivitiesTest.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/test/src/main/java/org/apache/abdera2/test/activities/ActivitiesTest.java?rev=1221883&r1=1221882&r2=1221883&view=diff
==============================================================================
--- abdera/abdera2/test/src/main/java/org/apache/abdera2/test/activities/ActivitiesTest.java
(original)
+++ abdera/abdera2/test/src/main/java/org/apache/abdera2/test/activities/ActivitiesTest.java
Wed Dec 21 21:04:22 2011
@@ -249,7 +249,7 @@ public class ActivitiesTest {
       .to(Objects.ME)
       .cc(PersonObject.makePerson("Jane"))
       .bcc(Objects.ME)
-      .bto(Objects.NETWORK)      
+      .bto(Objects.NETWORK)   
       .get();
     assertNotNull(activity.getActor());
     assertNotNull(activity.getVerb());

Modified: abdera/abdera2/test/src/main/java/org/apache/abdera2/test/client/AppTest.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/test/src/main/java/org/apache/abdera2/test/client/AppTest.java?rev=1221883&r1=1221882&r2=1221883&view=diff
==============================================================================
--- abdera/abdera2/test/src/main/java/org/apache/abdera2/test/client/AppTest.java (original)
+++ abdera/abdera2/test/src/main/java/org/apache/abdera2/test/client/AppTest.java Wed Dec
21 21:04:22 2011
@@ -52,11 +52,7 @@ import org.apache.abdera2.parser.ParserO
 import org.apache.abdera2.protocol.client.AbderaClient;
 import org.apache.abdera2.protocol.client.AbderaClientResponse;
 import org.apache.abdera2.protocol.client.AbderaSession;
-import org.apache.abdera2.protocol.client.BasicClient;
-import org.apache.abdera2.protocol.client.Client;
 import org.apache.abdera2.protocol.client.RequestOptions;
-import org.apache.abdera2.protocol.client.Session;
-import org.apache.abdera2.common.date.DateTimes;
 import org.apache.abdera2.common.http.CacheControl;
 import org.apache.abdera2.common.http.EntityTag;
 import org.apache.abdera2.common.mediatype.MimeTypeHelper;

Added: 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=1221883&view=auto
==============================================================================
--- abdera/abdera2/test/src/main/java/org/apache/abdera2/test/common/http/PreferenceTest.java
(added)
+++ abdera/abdera2/test/src/main/java/org/apache/abdera2/test/common/http/PreferenceTest.java
Wed Dec 21 21:04:22 2011
@@ -0,0 +1,43 @@
+package org.apache.abdera2.test.common.http;
+
+import org.apache.abdera2.common.http.Preference;
+import static org.apache.abdera2.common.http.Preference.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+public class PreferenceTest {
+
+  @Test
+  public void testPreference() {
+    
+    String prefs = 
+      Preference.toString(
+        PREF_LENIENT, 
+        WAIT(10), 
+        PREF_RETURN_ASYNCH,
+        make("A","B").param("B", "c").param("C","foo bar baz").get());
+
+    assertEquals("lenient,wait=10,return-asynch,a=B;b=c;c=\"foo bar baz\"",prefs);
+    
+    Iterable<Preference> list = parse(prefs);
+    assertTrue(contains(list, WAIT(10)));
+    assertTrue(contains(list, PREF_LENIENT));
+    assertTrue(contains(list, PREF_RETURN_ASYNCH));
+    assertTrue(contains(list, make("A").get()));
+    
+    Preference wait = get(list,"wait");
+    assertEquals(10,wait.getIntValue());
+    
+    Preference a = get(list,"a");
+    assertEquals("B",a.getValue());
+    assertTrue(a.hasParam("b"));
+    assertEquals("c",a.getParam("B"));
+    
+    System.out.println(a.getParam("b"));
+    System.out.println(a.getParam("c"));
+    
+  }
+  
+}

Propchange: abdera/abdera2/test/src/main/java/org/apache/abdera2/test/common/http/PreferenceTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message