Author: bdelacretaz
Date: Tue Jan  7 07:36:45 2014
New Revision: 1556128

URL: http://svn.apache.org/r1556128
Log:
Factor out TestEntry, prepare for DMAP_JAVA_DIFFERS test support

Added:
    
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/RefDataTest.java

Modified: 
incubator/devicemap/trunk/devicemap/java/src/test/java/org/apache/devicemap/client/RefDataTest.java
URL: 
http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/src/test/java/org/apache/devicemap/client/RefDataTest.java?rev=1556128&r1=1556127&r2=1556128&view=diff
==============================================================================
--- 
incubator/devicemap/trunk/devicemap/java/src/test/java/org/apache/devicemap/client/RefDataTest.java
 (original)
+++ 
incubator/devicemap/trunk/devicemap/java/src/test/java/org/apache/devicemap/client/RefDataTest.java
 Tue Jan  7 07:36:45 2014
@@ -75,54 +75,6 @@ public class RefDataTest {
     public static final String MAX_TESTS_PROPERTY = "devicemap.max.tests";
     public static final int MAX_TESTS = 
Integer.valueOf(System.getProperty(MAX_TESTS_PROPERTY, "0"));
     
-    public static final String DMAP_EXPECT = "DMAP_EXPECT";
-    
-    static private class TestEntry {
-        final String userAgent;
-        final String id;
-        final int index;
-        
-        TestEntry(int index, String line) {
-            this.index = index;
-            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;
-                    }
-                }
-            }
-            
-            id = tmpId;
-        }
-        
-        boolean isValid() {
-            return userAgent != null && id != null;
-        }
-        
-        static boolean ignoreLine(String line) {
-            line = line.trim();
-            return line.length() == 0 || line.startsWith("#");
-        }
-        
-        public String toString() {
-            return getClass().getSimpleName() + " " + index + " " + userAgent;
-        }
-    };
- 
     @Parameters(name="{0}")
     public static List<Object[]> data() throws IOException {
         final List<Object[]> result = new ArrayList<Object[]>();
@@ -158,8 +110,8 @@ public class RefDataTest {
     
     public RefDataTest(TestEntry entry) {
         this.entry = entry;
-        if(entry.index % 1000 == 0) {
-            log.info("Starting test {}", entry.index);
+        if(entry.getIndex() % 1000 == 0) {
+            log.info("Starting test {}", entry.getIndex());
         }
     }
     
@@ -179,34 +131,37 @@ public class RefDataTest {
         }
     }
     
-    /** If the corresponding option is set, output to stderr the results that
-     *  we get, in the same format as our test file, so that we can see 
-     *  the diffs with it. 
+    /** If the corresponding option is set, output to stderr the test file
+     *  entry that would cause the tests to pass.
      * @param actualId if null, the value from our TestEntry is used.
      */
     private void outputActualData(String actualId) {
         if(outputActualDataExcept != null) {
-            String newId = entry.id;
-            if(actualId != null && !outputActualDataExcept.equals(actualId)) {
-                newId = actualId;
+            if(actualId == null) {
+                // We found an id that matches, DMAP_DIFFERS not needed 
+                System.err.println(String.format("%s %s id:%s", 
+                        entry.getUserAgent(), TestEntry.DMAP_EXPECT, 
entry.getId()));
+            } else {
+                // Would need a DMAP_DIFFERS entry in test file
+                System.err.println(String.format("%s %s id:%s %s id:%s", 
+                        entry.getUserAgent(), TestEntry.DMAP_EXPECT, 
entry.getId(), TestEntry.DMAP_DIFFERS, actualId));
             }
-            System.err.println(String.format("%s DMAP_EXPECT id:%s", 
entry.userAgent, newId));
         }
     }
     
     @Test
     public void testId() {
-        final Map<String, String> props = 
deviceMapClient.classify(entry.userAgent);
+        final Map<String, String> props = 
deviceMapClient.classify(entry.getUserAgent());
         if(props == null) {
             // TODO this should be a test failure
             ignoredNoEntry.add(entry.toString());
-            outputActualData(null);
+            outputActualData("DMAP_NOT_FOUND");
             return;
         }
         
         final String actual = props.get("id");
-        assertNotNull("Expecting valid entry", entry.id);
-        if(!entry.id.equals(actual)) {
+        assertNotNull("Expecting valid entry", entry.getId());
+        if(!entry.getId().equals(actual)) {
             // TODO this should be a test failure
             ignoredNoMatch.add(entry.toString());
             outputActualData(actual);

Added: 
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=1556128&view=auto
==============================================================================
--- 
incubator/devicemap/trunk/devicemap/java/src/test/java/org/apache/devicemap/client/TestEntry.java
 (added)
+++ 
incubator/devicemap/trunk/devicemap/java/src/test/java/org/apache/devicemap/client/TestEntry.java
 Tue Jan  7 07:36:45 2014
@@ -0,0 +1,91 @@
+/*
+   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;
+
+
+/** A Test entry, built from a line in our reference data file */
+class TestEntry {
+    private final String userAgent;
+    private final String id;
+    private final int index;
+    private final boolean differs;
+
+    public static final String DMAP_EXPECT = "DMAP_EXPECT";
+    public static final String DMAP_DIFFERS = "DMAP_JAVA_DIFFERS";
+
+    /** 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;
+                }
+            }
+        }
+
+        id = tmpId;
+    }
+    
+    String getId() {
+        return id;
+    }
+    
+    String getUserAgent() {
+        return userAgent;
+    }
+    
+    int getIndex() {
+        return index;
+    }
+    
+    boolean differs() {
+        return differs;
+    }
+    
+    boolean isValid() {
+        return userAgent != null && id != null;
+    }
+
+    static boolean ignoreLine(String line) {
+        line = line.trim();
+        return line.length() == 0 || line.startsWith("#");
+    }
+
+    public String toString() {
+        return getClass().getSimpleName() + " " + index + " " + userAgent;
+    }
+}
\ No newline at end of file


Reply via email to