Permalink
Browse files

Reviewed integration testing

  • Loading branch information...
1 parent b7a7c72 commit 773c02b4cc547c644ef6daa7e267b6dd544ddfb8 @before committed May 12, 2012
View
129 ...uadetector-resources/src/test/java/net/sf/uadetector/OnlineUserAgentStringParserTest.java
@@ -1,129 +0,0 @@
-/*******************************************************************************
- * Copyright 2012 André Rouél
- *
- * Licensed 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 net.sf.uadetector;
-
-import java.util.Arrays;
-
-import net.sf.uadetector.internal.data.domain.Robot;
-import net.sf.uadetector.parser.OnlineUserAgentStringParserImpl;
-
-import org.junit.Assert;
-import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class OnlineUserAgentStringParserTest {
-
- private static final Logger LOG = LoggerFactory.getLogger(OnlineUserAgentStringParserTest.class);
-
- private static final OnlineUserAgentStringParserImpl parser = (OnlineUserAgentStringParserImpl) UADetectorServiceFactory
- .getOnlineUserAgentStringParser();
-
- @Test
- public void parse_CHROME13() throws Exception {
- final OperatingSystem os = new OperatingSystem("Mac OS X", "Mac OS X 10.6 Snow Leopard", "Apple Computer, Inc.",
- "http://www.apple.com/", "http://www.apple.com/macosx/", new VersionNumber(Arrays.asList("10", "6"), " Snow Leopard"));
- final UserAgent ua = parser
- .parse("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1");
- Assert.assertEquals("Chrome", ua.getFamily());
- Assert.assertEquals("Chrome", ua.getName());
- Assert.assertEquals(os, ua.getOperatingSystem());
- Assert.assertEquals("Google Inc.", ua.getProducer());
- Assert.assertEquals("http://www.google.com/", ua.getProducerUrl());
- Assert.assertEquals("Browser", ua.getTypeName());
- Assert.assertEquals("http://www.google.com/chrome", ua.getUrl());
- Assert.assertEquals("13.0.782.112", ua.getVersionNumber().toVersionString());
- }
-
- @Test
- public void parse_empty() throws Exception {
- final UserAgent.Builder builder = new UserAgent.Builder();
- final UserAgent expected = builder.build();
- final UserAgent unknownAgentString = parser.parse("");
- Assert.assertEquals(expected, unknownAgentString);
- }
-
- @Test
- public void parse_FIREFOX6() throws Exception {
- final OperatingSystem os = new OperatingSystem("Mac OS X", "Mac OS X 10.7 Lion", "Apple Computer, Inc.", "http://www.apple.com/",
- "http://www.apple.com/macosx/", new VersionNumber(Arrays.asList("10", "7"), " Lion"));
- final UserAgent ua = parser.parse("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:6.0) Gecko/20100101 Firefox/6.0");
- Assert.assertEquals("Firefox", ua.getFamily());
- Assert.assertEquals("Firefox", ua.getName());
- Assert.assertEquals(os, ua.getOperatingSystem());
- Assert.assertEquals("Mozilla Foundation", ua.getProducer());
- Assert.assertEquals("http://www.mozilla.org/", ua.getProducerUrl());
- Assert.assertEquals("Browser", ua.getTypeName());
- Assert.assertEquals("http://www.firefox.com/", ua.getUrl());
- Assert.assertEquals("6.0", ua.getVersionNumber().toVersionString());
- }
-
- @Test
- public void parse_GOOGLEBOT() throws Exception {
- final UserAgent ua = parser.parse("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
- Assert.assertEquals("Googlebot", ua.getFamily());
- Assert.assertEquals("Googlebot/2.1", ua.getName());
- Assert.assertEquals(OperatingSystem.EMPTY, ua.getOperatingSystem());
- Assert.assertEquals("Google Inc.", ua.getProducer());
- Assert.assertEquals("http://www.google.com/", ua.getProducerUrl());
- Assert.assertEquals(Robot.TYPENAME, ua.getTypeName());
- Assert.assertEquals("", ua.getUrl());
- Assert.assertEquals("2.1", ua.getVersionNumber().toVersionString());
- }
-
- @Test
- public void parse_SITESUCKER() throws Exception {
- final OperatingSystem os = new OperatingSystem("Mac OS", "Mac OS", "Apple Computer, Inc.", "http://www.apple.com/",
- "http://en.wikipedia.org/wiki/Mac_OS", VersionNumber.UNKNOWN);
- final UserAgent ua = parser.parse("SiteSucker/1.6.9");
- Assert.assertEquals("SiteSucker", ua.getFamily());
- Assert.assertEquals("SiteSucker", ua.getName());
- Assert.assertEquals(os, ua.getOperatingSystem());
- Assert.assertEquals("Rick Cranisky", ua.getProducer());
- Assert.assertEquals("", ua.getProducerUrl());
- Assert.assertEquals("Offline Browser", ua.getTypeName());
- Assert.assertEquals("http://www.sitesucker.us/", ua.getUrl());
- Assert.assertEquals("1.6.9", ua.getVersionNumber().toVersionString());
- }
-
- @Test
- public void parse_unknown() throws Exception {
- final UserAgent unknownAgentString = parser.parse("qwertzuiopasdfghjklyxcvbnm");
- Assert.assertEquals(UserAgent.EMPTY, unknownAgentString);
- Assert.assertEquals(OperatingSystem.EMPTY, unknownAgentString.getOperatingSystem());
- }
-
- @Test
- public void testUpdateMechanismWhileParsing() throws InterruptedException {
- final long firstLastUpdateCheck = parser.getLastUpdateCheck();
- LOG.debug("LastUpdateCheck at: " + firstLastUpdateCheck);
- final long originalInterval = parser.getUpdateInterval();
-
- // reduce the interval since testing
- LOG.debug("Reducing the update interval during the test.");
- parser.setUpdateInterval(10l);
- // we have to read to activate the update mechanism
- parser.parse("check for updates");
-
- Thread.sleep(100l);
- final long currentLastUpdateCheck = parser.getLastUpdateCheck();
- LOG.debug("LastUpdateCheck at: " + currentLastUpdateCheck);
- Assert.assertTrue(firstLastUpdateCheck < currentLastUpdateCheck);
-
- parser.setUpdateInterval(originalInterval);
- }
-
-}
View
21 modules/uadetector-resources/src/test/java/net/sf/uadetector/OperatingSystemExample.java
@@ -0,0 +1,21 @@
+package net.sf.uadetector;
+
+final class OperatingSystemExample {
+
+ private final String name;
+ private final String userAgentString;
+
+ public OperatingSystemExample(final String name, final String userAgentString) {
+ this.name = name;
+ this.userAgentString = userAgentString;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getUserAgentString() {
+ return userAgentString;
+ }
+
+}
View
71 ...s/uadetector-resources/src/test/java/net/sf/uadetector/OperatingSystemExamplesReader.java
@@ -0,0 +1,71 @@
+package net.sf.uadetector;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.List;
+
+import net.sf.uadetector.parser.OnlineUserAgentStringParserImpl;
+
+import org.apache.commons.csv.CSVParser;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class OperatingSystemExamplesReader {
+
+ /**
+ * Character set of the file
+ */
+ private static final String CHARSET = "UTF-8";
+
+ /**
+ * File to read
+ */
+ private static final String FILE = "examples/uasOS_example.csv";
+
+ /**
+ * Default log
+ */
+ private static final Logger LOG = LoggerFactory.getLogger(OnlineUserAgentStringParserImpl.class);
+
+ @Test
+ public static List<OperatingSystemExample> read() {
+ final InputStream stream = UserAgentStringParserIntegrationTest.class.getClassLoader().getResourceAsStream(FILE);
+
+ CSVParser csvParser = null;
+ try {
+ csvParser = new CSVParser(new InputStreamReader(stream, CHARSET));
+ } catch (final UnsupportedEncodingException e) {
+ LOG.warn(e.getLocalizedMessage(), e);
+ }
+
+ final List<OperatingSystemExample> examples = new ArrayList<OperatingSystemExample>(200);
+ if (csvParser != null) {
+ String[] line = null;
+ int i = 0;
+ do {
+
+ try {
+ line = csvParser.getLine();
+ } catch (final IOException e) {
+ line = null;
+ LOG.warn(e.getLocalizedMessage(), e);
+ }
+
+ if (line != null) {
+ i++;
+ if (line.length == 2) {
+ examples.add(new OperatingSystemExample(line[0], line[1]));
+ } else {
+ LOG.warn("Can not read operating system example " + i + ", there are too few fields: " + line.length);
+ }
+ }
+ } while (line != null);
+ }
+ return examples;
+ }
+
+}
View
27 modules/uadetector-resources/src/test/java/net/sf/uadetector/UserAgentExample.java
@@ -0,0 +1,27 @@
+package net.sf.uadetector;
+
+final class UserAgentExample {
+
+ private final String name;
+ private final String type;
+ private final String userAgentString;
+
+ public UserAgentExample(final String type, final String name, final String userAgentString) {
+ this.type = type;
+ this.name = name;
+ this.userAgentString = userAgentString;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public String getUserAgentString() {
+ return userAgentString;
+ }
+
+}
View
71 modules/uadetector-resources/src/test/java/net/sf/uadetector/UserAgentExamplesReader.java
@@ -0,0 +1,71 @@
+package net.sf.uadetector;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.List;
+
+import net.sf.uadetector.parser.OnlineUserAgentStringParserImpl;
+
+import org.apache.commons.csv.CSVParser;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class UserAgentExamplesReader {
+
+ /**
+ * Character set of the file
+ */
+ private static final String CHARSET = "UTF-8";
+
+ /**
+ * File to read
+ */
+ private static final String FILE = "examples/uas_example.csv";
+
+ /**
+ * Default log
+ */
+ private static final Logger LOG = LoggerFactory.getLogger(OnlineUserAgentStringParserImpl.class);
+
+ @Test
+ public static List<UserAgentExample> read() {
+ final InputStream stream = UserAgentStringParserIntegrationTest.class.getClassLoader().getResourceAsStream(FILE);
+
+ CSVParser csvParser = null;
+ try {
+ csvParser = new CSVParser(new InputStreamReader(stream, CHARSET));
+ } catch (final UnsupportedEncodingException e) {
+ LOG.warn(e.getLocalizedMessage(), e);
+ }
+
+ final List<UserAgentExample> examples = new ArrayList<UserAgentExample>(2000);
+ if (csvParser != null) {
+ String[] line = null;
+ int i = 0;
+ do {
+
+ try {
+ line = csvParser.getLine();
+ } catch (final IOException e) {
+ LOG.warn(e.getLocalizedMessage(), e);
+ line = null;
+ }
+
+ if (line != null) {
+ i++;
+ if (line.length == 3) {
+ examples.add(new UserAgentExample(line[0], line[1], line[2]));
+ } else {
+ LOG.warn("Can not read operating system example " + i + ", there are too few fields: " + line.length);
+ }
+ }
+ } while (line != null);
+ }
+ return examples;
+ }
+
+}
View
105 ...ector-resources/src/test/java/net/sf/uadetector/UserAgentStringParserIntegrationTest.java
@@ -15,73 +15,104 @@
******************************************************************************/
package net.sf.uadetector;
-import java.io.InputStream;
-import java.io.InputStreamReader;
+import java.util.Formatter;
+import java.util.List;
import net.sf.uadetector.internal.data.domain.Robot;
import net.sf.uadetector.parser.OnlineUserAgentStringParserImpl;
-import org.apache.commons.csv.CSVParser;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UserAgentStringParserIntegrationTest {
- private static final String CHARSET = "UTF-8";
+ /**
+ * Output buffer to store informations from the detection process of the examples.
+ */
+ public static class Output {
+
+ public static final String DEFAULT_FORMAT = "%-30.30s %-20.20s %s";
+
+ public static final char NEWLINE = '\n';
+
+ private final String format;
+
+ private final StringBuilder buffer = new StringBuilder();
+
+ public Output() {
+ this(DEFAULT_FORMAT);
+ }
+
+ public Output(final String format) {
+ this.format = format;
+ }
+
+ public void print(final String name, final VersionNumber version, final String userAgent) {
+ final Formatter formatter = new Formatter(buffer);
+ formatter.format(format, name, version.toVersionString(), userAgent);
+ buffer.append(NEWLINE);
+ }
+
+ @Override
+ public String toString() {
+ return buffer.toString();
+ }
+
+ }
/**
* Default log
*/
private static final Logger LOG = LoggerFactory.getLogger(OnlineUserAgentStringParserImpl.class);
+ private static final List<OperatingSystemExample> OS_EXAMPLES = OperatingSystemExamplesReader.read();
+
+ private static final List<UserAgentExample> UA_EXAMPLES = UserAgentExamplesReader.read();
+
private static final UserAgentStringParser parser = UADetectorServiceFactory.getUserAgentStringParser();
@Test
public void testOperatingSystemExamples() throws Exception {
- final InputStream stream = this.getClass().getClassLoader().getResourceAsStream("examples/uasOS_example.csv");
- final CSVParser csvParser = new CSVParser(new InputStreamReader(stream, CHARSET));
- String[] line = null;
+ final Output out = new Output();
int i = 0;
- do {
- line = csvParser.getLine();
- if (line != null) {
- i++;
- if (line.length == 2) {
- final UserAgent agent = parser.parse(line[1]);
- Assert.assertEquals(line[0], agent.getOperatingSystem().getName());
- } else {
- LOG.warn("Not enough fields: " + line.length);
- }
+ for (OperatingSystemExample example : OS_EXAMPLES) {
+ final UserAgent agent = parser.parse(example.getUserAgentString());
+
+ // comparing the name
+ Assert.assertEquals(example.getName(), agent.getOperatingSystem().getName());
+
+ // check for unknown family
+ if (OperatingSystemFamily.UNKNOWN == agent.getOperatingSystem().getFamily()) {
+ LOG.info("Unknown operating system family found. Please update the enum 'OperatingSystemFamily'.");
}
- } while (line != null);
+
+ // abort if unknown family
+ Assert.assertFalse(OperatingSystemFamily.UNKNOWN == agent.getOperatingSystem().getFamily());
+
+ // save read OS for printing out
+ out.print(agent.getOperatingSystem().getName(), agent.getOperatingSystem().getVersionNumber(), example.getUserAgentString());
+ i++;
+ }
+ LOG.info(Output.NEWLINE + out.toString());
LOG.info(i + " operating system examples validated");
}
@Test
public void testUserAgentExamples() throws Exception {
- final InputStream stream = this.getClass().getClassLoader().getResourceAsStream("examples/uas_example.csv");
- final CSVParser csvParser = new CSVParser(new InputStreamReader(stream, CHARSET));
- String[] line = null;
+ final Output out = new Output("%-40.40s %-30.30s %s");
int i = 0;
- do {
- line = csvParser.getLine();
- if (line != null) {
- i++;
- if (line.length == 3) {
- final UserAgent agent = parser.parse(line[2]);
- Assert.assertEquals(line[1], agent.getFamily());
- final String type = "robot".equals(line[0]) ? Robot.TYPENAME : line[0];
- if (Robot.TYPENAME.equals(type)) {
- System.out.println(agent.getName() + " v: " + agent.getVersionNumber().toVersionString());
- }
- Assert.assertEquals(type, agent.getType());
- } else {
- LOG.warn("Not enough fields: " + line.length);
- }
+ for (UserAgentExample example : UA_EXAMPLES) {
+ final UserAgent agent = parser.parse(example.getUserAgentString());
+ Assert.assertEquals(example.getName(), agent.getFamily());
+ final String type = "robot".equals(example.getType()) ? Robot.TYPENAME : example.getType();
+ if (Robot.TYPENAME.equals(type)) {
+ out.print(agent.getName(), agent.getVersionNumber(), example.getUserAgentString());
}
- } while (line != null);
+ Assert.assertEquals(type, agent.getTypeName());
+ }
+ LOG.info(Output.NEWLINE + out.toString());
LOG.info(i + " User-Agent examples validated");
}
View
103 modules/uadetector-resources/src/test/java/net/sf/uadetector/UserAgentStringParserTest.java
@@ -1,103 +0,0 @@
-/*******************************************************************************
- * Copyright 2012 André Rouél
- *
- * Licensed 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 net.sf.uadetector;
-
-import java.util.Arrays;
-
-import net.sf.uadetector.internal.data.domain.Robot;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-public class UserAgentStringParserTest {
-
- private static final UserAgentStringParser parser = UADetectorServiceFactory.getUserAgentStringParser();
-
- @Test
- public void parse_CHROME13() throws Exception {
- final OperatingSystem os = new OperatingSystem("Mac OS X", "Mac OS X 10.6 Snow Leopard", "Apple Computer, Inc.",
- "http://www.apple.com/", "http://www.apple.com/macosx/", new VersionNumber(Arrays.asList("10", "6"), " Snow Leopard"));
- final UserAgent ua = parser
- .parse("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1");
- Assert.assertEquals("Chrome", ua.getFamily());
- Assert.assertEquals("Chrome", ua.getName());
- Assert.assertEquals(os, ua.getOperatingSystem());
- Assert.assertEquals("Google Inc.", ua.getProducer());
- Assert.assertEquals("http://www.google.com/", ua.getProducerUrl());
- Assert.assertEquals("Browser", ua.getTypeName());
- Assert.assertEquals("http://www.google.com/chrome", ua.getUrl());
- Assert.assertEquals("13.0.782.112", ua.getVersionNumber().toVersionString());
- }
-
- @Test
- public void parse_empty() throws Exception {
- final UserAgent.Builder builder = new UserAgent.Builder();
- final UserAgent expected = builder.build();
- final UserAgent unknownAgentString = parser.parse("");
- Assert.assertEquals(expected, unknownAgentString);
- }
-
- @Test
- public void parse_FIREFOX6() throws Exception {
- final OperatingSystem os = new OperatingSystem("Mac OS X", "Mac OS X 10.7 Lion", "Apple Computer, Inc.", "http://www.apple.com/",
- "http://www.apple.com/macosx/", new VersionNumber(Arrays.asList("10", "7"), " Lion"));
- final UserAgent ua = parser.parse("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:6.0) Gecko/20100101 Firefox/6.0");
- Assert.assertEquals("Firefox", ua.getFamily());
- Assert.assertEquals("Firefox", ua.getName());
- Assert.assertEquals(os, ua.getOperatingSystem());
- Assert.assertEquals("Mozilla Foundation", ua.getProducer());
- Assert.assertEquals("http://www.mozilla.org/", ua.getProducerUrl());
- Assert.assertEquals("Browser", ua.getTypeName());
- Assert.assertEquals("http://www.firefox.com/", ua.getUrl());
- Assert.assertEquals("6.0", ua.getVersionNumber().toVersionString());
- }
-
- @Test
- public void parse_GOOGLEBOT() throws Exception {
- final UserAgent ua = parser.parse("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
- Assert.assertEquals("Googlebot", ua.getFamily());
- Assert.assertEquals("Googlebot/2.1", ua.getName());
- Assert.assertEquals(OperatingSystem.EMPTY, ua.getOperatingSystem());
- Assert.assertEquals("Google Inc.", ua.getProducer());
- Assert.assertEquals("http://www.google.com/", ua.getProducerUrl());
- Assert.assertEquals(Robot.TYPENAME, ua.getTypeName());
- Assert.assertEquals("", ua.getUrl());
- Assert.assertEquals("2.1", ua.getVersionNumber().toVersionString());
- }
-
- @Test
- public void parse_SITESUCKER() throws Exception {
- final OperatingSystem os = new OperatingSystem("Mac OS", "Mac OS", "Apple Computer, Inc.", "http://www.apple.com/",
- "http://en.wikipedia.org/wiki/Mac_OS", VersionNumber.UNKNOWN);
- final UserAgent ua = parser.parse("SiteSucker/1.6.9");
- Assert.assertEquals("SiteSucker", ua.getFamily());
- Assert.assertEquals("SiteSucker", ua.getName());
- Assert.assertEquals(os, ua.getOperatingSystem());
- Assert.assertEquals("Rick Cranisky", ua.getProducer());
- Assert.assertEquals("", ua.getProducerUrl());
- Assert.assertEquals("Offline Browser", ua.getTypeName());
- Assert.assertEquals("http://www.sitesucker.us/", ua.getUrl());
- Assert.assertEquals("1.6.9", ua.getVersionNumber().toVersionString());
- }
-
- @Test
- public void parse_unknown() throws Exception {
- final UserAgent unknownAgentString = parser.parse("qwertzuiopasdfghjklyxcvbnm");
- Assert.assertEquals(UserAgent.EMPTY, unknownAgentString);
- Assert.assertEquals(OperatingSystem.EMPTY, unknownAgentString.getOperatingSystem());
- }
-
-}

0 comments on commit 773c02b

Please sign in to comment.