You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devicemap.apache.org by bd...@apache.org on 2013/09/30 13:57:16 UTC

svn commit: r1527520 - in /incubator/devicemap/trunk/devicemapjava: ./ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/devicemap/ src/test/java/org/apache/devicemap/client/

Author: bdelacretaz
Date: Mon Sep 30 11:57:16 2013
New Revision: 1527520

URL: http://svn.apache.org/r1527520
Log:
DMAP-29 - automated tests for the devicemapjava module

Added:
    incubator/devicemap/trunk/devicemapjava/src/test/
    incubator/devicemap/trunk/devicemapjava/src/test/java/
    incubator/devicemap/trunk/devicemapjava/src/test/java/org/
    incubator/devicemap/trunk/devicemapjava/src/test/java/org/apache/
    incubator/devicemap/trunk/devicemapjava/src/test/java/org/apache/devicemap/
    incubator/devicemap/trunk/devicemapjava/src/test/java/org/apache/devicemap/client/
    incubator/devicemap/trunk/devicemapjava/src/test/java/org/apache/devicemap/client/DeviceMapTestClient.java
    incubator/devicemap/trunk/devicemapjava/src/test/java/org/apache/devicemap/client/RefDataTest.java
Modified:
    incubator/devicemap/trunk/devicemapjava/   (props changed)
    incubator/devicemap/trunk/devicemapjava/pom.xml

Propchange: incubator/devicemap/trunk/devicemapjava/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Mon Sep 30 11:57:16 2013
@@ -1 +1,13 @@
 target
+bin
+*.iml
+*.ipr
+*.iws
+.settings
+.project
+.classpath
+.externalToolBuilders
+maven-eclipse.xml
+
+
+

Modified: incubator/devicemap/trunk/devicemapjava/pom.xml
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapjava/pom.xml?rev=1527520&r1=1527519&r2=1527520&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemapjava/pom.xml (original)
+++ incubator/devicemap/trunk/devicemapjava/pom.xml Mon Sep 30 11:57:16 2013
@@ -29,10 +29,22 @@
     <groupId>org.apache.devicemap</groupId>
     <artifactId>devicemap-java</artifactId>
     <packaging>jar</packaging>
-    <version>0.1</version>
+    <version>0.1.0</version>
     <name>Apache DeviceMap Java Client</name>
     <description>DeviceMap Java Client</description>
     <url>http://incubator.apache.org/devicemap</url>
+    
+    <!-- Test settings that can be overridden from the Maven command line -->
+    <properties>
+        <!-- Set this to 0 to execute all tests -->
+        <devicemap.max.tests>5042</devicemap.max.tests>
+        
+        <!-- The test data file maps User-Agent values to expected device properties -->
+        <devicemap.test.data.file>${project.build.directory}/devicemap/test-data/dmap_20130522.txt</devicemap.test.data.file>
+        
+        <!-- Where to find the device data -->
+        <devicemap.device.data.folder>${project.build.directory}/devicemap/devicedata</devicemap.device.data.folder>
+    </properties>
 
     <scm>
         <connection>scm:svn:http://svn.apache.org/repos/asf/incubator/devicemap/trunk/devicemapjava</connection>
@@ -41,6 +53,36 @@
     </scm>
   
     <dependencies>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.11</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+            <version>1.7.5</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-simple</artifactId>
+            <version>1.7.5</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.devicemap</groupId>
+            <artifactId>devicemap-device-data</artifactId>
+            <version>0.9.9-SNAPSHOT</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.devicemap</groupId>
+            <artifactId>devicemap-test-data</artifactId>
+            <version>0.9.9-SNAPSHOT</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>
@@ -56,10 +98,41 @@
                     <debug>true</debug>
                 </configuration>
             </plugin>
-        </plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-surefire-plugin</artifactId>
+                <version>2.16</version>
+                <configuration>
+                    <argLine>-Xmx512M -XX:MaxPermSize=256m</argLine>
+                    <systemPropertyVariables>
+                        <devicemap.test.data.file>${devicemap.test.data.file}</devicemap.test.data.file>
+                        <devicemap.max.tests>${devicemap.max.tests}</devicemap.max.tests>
+                        <devicemap.test.data.file>${devicemap.test.data.file}</devicemap.test.data.file>
+                        <devicemap.device.data.folder>${devicemap.device.data.folder}</devicemap.device.data.folder>
+                    </systemPropertyVariables>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-dependency-plugin</artifactId>
+                <version>2.6</version>
+                <executions>
+                    <execution>
+                        <id>unpack-dependencies</id>
+                        <phase>process-resources</phase>
+                        <goals>
+                            <goal>unpack-dependencies</goal>
+                        </goals>
+                        <configuration>
+                            <includes>**/*.xml,**/*.txt</includes>
+                            <outputDirectory>${project.build.directory}/devicemap</outputDirectory>
+                            <overWriteReleases>false</overWriteReleases>
+                            <overWriteSnapshots>true</overWriteSnapshots>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+         </plugins>
     </build>
-  
-    <profiles>
-    </profiles>
-</project>
+  </project>
 

Added: incubator/devicemap/trunk/devicemapjava/src/test/java/org/apache/devicemap/client/DeviceMapTestClient.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapjava/src/test/java/org/apache/devicemap/client/DeviceMapTestClient.java?rev=1527520&view=auto
==============================================================================
--- incubator/devicemap/trunk/devicemapjava/src/test/java/org/apache/devicemap/client/DeviceMapTestClient.java (added)
+++ incubator/devicemap/trunk/devicemapjava/src/test/java/org/apache/devicemap/client/DeviceMapTestClient.java Mon Sep 30 11:57:16 2013
@@ -0,0 +1,34 @@
+/*
+   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 java.io.File;
+import java.io.IOException;
+
+/** DeviceMapClient used for tests, loads its data
+ *  from a folder defined by system properties.
+ */
+public class DeviceMapTestClient extends DeviceMapClient {
+    public static final String DATA_FOLDER_PROPERTY = "devicemap.device.data.folder";
+    public static final String DATA_FOLDER = System.getProperty(DATA_FOLDER_PROPERTY, "DATA_FOLDER_NOT_SET??");
+    
+    public DeviceMapTestClient() throws IOException {
+        loadFromFolder(new File(DATA_FOLDER).getAbsolutePath());
+    }
+}

Added: incubator/devicemap/trunk/devicemapjava/src/test/java/org/apache/devicemap/client/RefDataTest.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapjava/src/test/java/org/apache/devicemap/client/RefDataTest.java?rev=1527520&view=auto
==============================================================================
--- incubator/devicemap/trunk/devicemapjava/src/test/java/org/apache/devicemap/client/RefDataTest.java (added)
+++ incubator/devicemap/trunk/devicemapjava/src/test/java/org/apache/devicemap/client/RefDataTest.java Mon Sep 30 11:57:16 2013
@@ -0,0 +1,199 @@
+/*
+   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.assertNotNull;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.junit.AfterClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Test DeviceMapClient against a reference data file
+ *  that contains User-Agents and the corresponding
+ *  expected output.
+ *  
+ *  A system property can limit the number of tests that
+ *  we run, to allow for switching between quick smoke
+ *  tests and a full tests.
+ */
+@RunWith(Parameterized.class)
+public class RefDataTest {
+    private final TestEntry entry;
+    private static final Logger log = LoggerFactory.getLogger(RefDataTest.class);
+    private static final DeviceMapClient deviceMapClient;
+    
+    private static Set<String> ignoredNoEntry = new HashSet<String>();
+    private static Set<String> ignoredNoMatch = new HashSet<String>();
+    private static Set<String> successful = new HashSet<String>();
+    private static int totalTests;
+    
+    static {
+        DeviceMapClient c = null;
+        try {
+            c = new DeviceMapTestClient();
+        } catch(IOException ioe) {
+            log.error("Exception while creating DeviceMapClient", ioe);
+        }
+        deviceMapClient = c;
+    }
+    
+    public static final String TEST_FILENAME_PROPERTY = "devicemap.test.data.file";
+    public static final String TEST_DATA_FILENAME = System.getProperty(TEST_FILENAME_PROPERTY, "TEST_DATA_FILENAME_NOT_SET??");
+    
+    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[]>();
+        final File f = new File(TEST_DATA_FILENAME);
+        log.info("Using {} data file as defined by {} system property", f.getAbsolutePath(), TEST_FILENAME_PROPERTY);
+        final BufferedReader r = new BufferedReader(new FileReader(f));
+        String line = null;
+        try {
+            while((line = r.readLine()) != null) {
+                if(MAX_TESTS > 0 && result.size() >= MAX_TESTS) {
+                    log.info(
+                            "Reached {} tests as defined by {} system property, ignoring the remaining ones",
+                            result.size(), MAX_TESTS_PROPERTY);
+                    break;
+                }
+                if(TestEntry.ignoreLine(line)) {
+                    continue;
+                }
+                final TestEntry t = new TestEntry(result.size(), line);
+                if(!t.isValid()) {
+                    log.warn("Invalid input line, ignored: {}", line);
+                } else {
+                    result.add(new Object [] { t });
+                }
+            }
+            log.info("Collected {} tests from file {}", result.size(), f.getName());
+        } finally {
+            r.close();
+        }
+        totalTests = result.size();
+        return result;
+    }
+    
+    public RefDataTest(TestEntry entry) {
+        this.entry = entry;
+        if(entry.index % 1000 == 0) {
+            log.info("Starting test {}", entry.index);
+        }
+    }
+    
+    @AfterClass
+    public static void report() {
+        if(!ignoredNoEntry.isEmpty()) {
+            log.warn("TODO: all tests passed but {} User-Agents were not classified", ignoredNoEntry.size());
+        }
+        if(!ignoredNoMatch.isEmpty()) {
+            log.warn("TODO: all tests passed but {} User-Agents did not match the expected IDs", ignoredNoMatch.size());
+        }
+        
+        if(successful.size() == totalTests) {
+            log.info("All {} tests successful", totalTests);
+        } else {
+            log.warn("Out of {} collected tests, only {} were successful", totalTests, successful.size());
+        }
+    }
+    
+    @Test
+    public void testId() {
+        final Map<String, String> props = deviceMapClient.classify(entry.userAgent);
+        if(props == null) {
+            // TODO this should be a test failure
+            ignoredNoEntry.add(entry.toString());
+            return;
+        }
+        
+        final String actual = props.get("id");
+        assertNotNull("Expecting valid entry", entry.id);
+        if(!entry.id.equals(actual)) {
+            // TODO this should be a test failure
+            ignoredNoMatch.add(entry.toString());
+            return;
+        }
+        
+        successful.add(entry.toString());
+    }
+}
\ No newline at end of file