Author: bdelacretaz
Date: Tue Jan  7 09:21:32 2014
New Revision: 1556154

URL: http://svn.apache.org/r1556154
Log:
TestEntryTest added

Added:
    
incubator/devicemap/trunk/devicemap/java/src/test/java/org/apache/devicemap/client/TestEntryTest.java
Modified:
    
incubator/devicemap/trunk/devicemap/java/src/test/java/org/apache/devicemap/client/TestEntry.java

Modified: 
incubator/devicemap/trunk/devicemap/java/src/test/java/org/apache/devicemap/client/TestEntry.java
URL: 
http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/src/test/java/org/apache/devicemap/client/TestEntry.java?rev=1556154&r1=1556153&r2=1556154&view=diff
==============================================================================
--- 
incubator/devicemap/trunk/devicemap/java/src/test/java/org/apache/devicemap/client/TestEntry.java
 (original)
+++ 
incubator/devicemap/trunk/devicemap/java/src/test/java/org/apache/devicemap/client/TestEntry.java
 Tue Jan  7 09:21:32 2014
@@ -28,36 +28,56 @@ class TestEntry {
 
     public static final String DMAP_EXPECT = "DMAP_EXPECT";
     public static final String DMAP_DIFFERS = "DMAP_JAVA_DIFFERS";
+    public static final String ID = "id";
 
     /** Build test entry from a line like 
      *      some user agent DMAP_EXPECT id:foo something DMAP_JAVA_DIFFERS 
id:bar otherwise
      * */
     TestEntry(int index, String line) {
         this.index = index;
-        this.differs = false;
-        final String[] mainParts = line.split(DMAP_EXPECT);
-        if (mainParts.length < 2) {
-            throw new IllegalStateException("Invalid input line: " + line);
-        }
-        userAgent = mainParts[0].trim();
-        String tmpId = null;
-        final String[] kvParts = mainParts[1].split(" ");
-        for (String part : kvParts) {
-            part = part.trim();
-            if (part.length() == 0) {
-                continue;
-            }
-            final int colonPos = part.indexOf(":");
-            if (colonPos > 0) {
-                final String key = part.substring(0, colonPos).trim();
-                if ("id".equals(key)) {
-                    tmpId = part.substring(colonPos + 1).trim();
-                    break;
-                }
+        
+        final String expectPart = substringAfter(line, DMAP_EXPECT);
+        final String differsPart = substringAfter(expectPart, DMAP_DIFFERS);
+        
+        if(expectPart == null) {
+            userAgent = id = null;
+            differs = false;
+        } else {
+            userAgent = substringBefore(line, DMAP_EXPECT);
+            if(differsPart == null) {
+                id = getField(expectPart, ID);
+                differs = false;
+            } else {
+                id = getField(differsPart, ID);
+                differs = true;
             }
         }
-
-        id = tmpId;
+    }
+    
+    private String getField(String input, String id) {
+        return substringAfter(input, id + ":");
+    }
+    
+    private String substringAfter(String input, String delimiter) {
+        if(input == null) {
+            return null;
+        }
+        final int pos = input.indexOf(delimiter);
+        if(pos < 0) {
+            return null;
+        }
+        return input.substring(pos + delimiter.length()).trim();
+    }
+    
+    private String substringBefore(String input, String delimiter) {
+        if(input == null) {
+            return null;
+        }
+        final int pos = input.indexOf(delimiter);
+        if(pos < 0) {
+            return null;
+        }
+        return input.substring(0, pos).trim();
     }
     
     String getId() {

Added: 
incubator/devicemap/trunk/devicemap/java/src/test/java/org/apache/devicemap/client/TestEntryTest.java
URL: 
http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/src/test/java/org/apache/devicemap/client/TestEntryTest.java?rev=1556154&view=auto
==============================================================================
--- 
incubator/devicemap/trunk/devicemap/java/src/test/java/org/apache/devicemap/client/TestEntryTest.java
 (added)
+++ 
incubator/devicemap/trunk/devicemap/java/src/test/java/org/apache/devicemap/client/TestEntryTest.java
 Tue Jan  7 09:21:32 2014
@@ -0,0 +1,69 @@
+/*
+   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.devicemap.client;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class TestEntryTest {
+    private final String id;
+    private final String ua;
+    private final boolean differs;
+    private final TestEntry entry;
+    
+    @Parameters(name="{1}")
+    public static Collection<Object[]> data() {
+            List<Object[]> params = new ArrayList<Object[]>();
+            params.add(new Object[] { "UA 1 DMAP_EXPECT id:first ID", "UA 1", 
"first ID", false });
+            params.add(new Object[] { "UA 2 DMAP_EXPECT id:first ID 
DMAP_JAVA_DIFFERS id:second ID", "UA 2", "second ID", true });
+            params.add(new Object[] { "UA 3 DMAP_NOTHING", null, null, false 
});
+            return params;
+    }
+    
+    public TestEntryTest(String input, String ua, String id, boolean differs) {
+        this.id = id;
+        this.ua = ua;
+        this.differs = differs;
+        entry = new TestEntry(0,  input);
+    }
+    
+    @Test
+    public void testId() {
+        assertEquals(id, entry.getId());
+    }
+    
+    @Test
+    public void testDiffers() {
+        assertEquals(differs, entry.differs());
+    }
+    
+    @Test
+    public void testUA() {
+        assertEquals(ua, entry.getUserAgent());
+    }
+}
\ No newline at end of file


Reply via email to