Skip to content

Commit 4b5770c

Browse files
committed
added tests for old UA database without devices
1 parent 019f3bf commit 4b5770c

File tree

4 files changed

+73
-19
lines changed

4 files changed

+73
-19
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cz.mallat.uasparser;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.util.zip.GZIPInputStream;
6+
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
/**
11+
* Test against a copy of the database without device info
12+
*
13+
* @author chetan
14+
*
15+
*/
16+
public class TestOldDatabase extends TestParsers {
17+
18+
@Before
19+
public void disableDeviceTests() {
20+
this.testDeviceInfo = false;
21+
}
22+
23+
@Override
24+
protected InputStream getDataInputStream() {
25+
try {
26+
return new GZIPInputStream(this.getClass().getClassLoader().getResourceAsStream("uas-nodevice.txt.gz"));
27+
} catch (IOException e) {
28+
}
29+
return null;
30+
}
31+
32+
@Override
33+
@Test
34+
public void runOnlineUAParser() throws IOException {
35+
// disable
36+
}
37+
38+
}

src/test/java/cz/mallat/uasparser/TestParsers.java

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@
33
import static org.junit.Assert.*;
44

55
import java.io.IOException;
6+
import java.io.InputStream;
67

78
import org.junit.Test;
89

910
/**
10-
* No real JUnit tests
11+
* Test the various parser implementations
1112
*
12-
* @author oli
13+
* @author chetan
1314
*
1415
*/
1516
public class TestParsers {
1617

18+
protected boolean testDeviceInfo = true;
19+
1720
@Test
1821
public void runUAParser() throws IOException {
19-
UASparser p = new UASparser(OnlineUpdater.getVendoredInputStream());
22+
UASparser p = new UASparser(getDataInputStream());
2023
testUserAgents(p);
2124
}
2225

@@ -34,16 +37,24 @@ public void runCachedOnlineUAParser() throws IOException {
3437

3538
@Test
3639
public void testSingleThreadedParser() throws IOException {
37-
UASparser p = new SingleThreadedUASparser(OnlineUpdater.getVendoredInputStream());
40+
UASparser p = new SingleThreadedUASparser(getDataInputStream());
3841
testUserAgents(p);
3942
}
4043

4144
@Test
4245
public void testMultithreadedParser() throws IOException {
43-
UASparser p = new MultithreadedUASparser(OnlineUpdater.getVendoredInputStream());
46+
UASparser p = new MultithreadedUASparser(getDataInputStream());
4447
testUserAgents(p);
4548
}
4649

50+
/**
51+
* Get database file to test against as an {@link InputStream}
52+
* @return
53+
*/
54+
protected InputStream getDataInputStream() {
55+
return OnlineUpdater.getVendoredInputStream();
56+
}
57+
4758
private void testUserAgents(UASparser parser) throws IOException {
4859
testRobotAgents(parser);
4960
testBrowserAgent(parser);
@@ -63,7 +74,7 @@ private void testRobotAgents(UASparser parser) throws IOException {
6374
uai = parser.parse("Pingdom.com_bot_version_1.4_(http://www.pingdom.com/)");
6475
assertTrue(uai.isRobot());
6576
}
66-
77+
6778
private void testBrowserAgent(UASparser parser) throws IOException {
6879
UserAgentInfo uai = parser.parse("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12");
6980
assertNotNull(uai);
@@ -75,7 +86,7 @@ private void testBrowserAgent(UASparser parser) throws IOException {
7586
assertEquals("Windows", uai.getOsFamily());
7687
assertEquals("Microsoft Corporation.", uai.getOsCompany());
7788
}
78-
89+
7990
private void testEmailAgent(UASparser parser) throws IOException {
8091
UserAgentInfo uai = parser.parse("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_5_8) AppleWebKit/534.50.2 (KHTML, like Gecko)");
8192
assertNotNull(uai);
@@ -87,35 +98,41 @@ private void testEmailAgent(UASparser parser) throws IOException {
8798
assertEquals("OS X", uai.getOsFamily());
8899
assertEquals("Apple Computer, Inc.", uai.getOsCompany());
89100
}
90-
101+
91102
private void testTabletAgent(UASparser parser) throws IOException {
92-
UserAgentInfo uai = parser.parse("Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53");
103+
UserAgentInfo uai = parser.parse("Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/6.0 Mobile/11A465 Safari/9537.53");
93104
assertNotNull(uai);
94105
assertEquals("Mobile Browser", uai.getType());
95-
assertEquals("Mobile Safari 7.0", uai.getUaName());
106+
assertEquals("Mobile Safari 6.0", uai.getUaName());
96107
assertEquals("Mobile Safari", uai.getUaFamily());
97108
assertEquals("Apple Inc.", uai.getUaCompany());
98-
assertEquals("iOS 7", uai.getOsName());
109+
assertEquals("iOS 6", uai.getOsName());
99110
assertEquals("iOS", uai.getOsFamily());
100111
assertEquals("Apple Inc.", uai.getOsCompany());
101-
112+
113+
if (!testDeviceInfo) {
114+
return;
115+
}
102116
assertTrue(uai.hasDeviceInfo());
103117
assertEquals("Tablet", uai.getDeviceType());
104118
assertEquals("tablet.png", uai.getDeviceIcon());
105119
assertEquals("http://user-agent-string.info/list-of-ua/device-detail?device=Tablet", uai.getDeviceInfoUrl());
106120
}
107-
121+
108122
private void testSmartphoneAgent(UASparser parser) throws IOException {
109-
UserAgentInfo uai = parser.parse("Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X) AppleWebKit/546.10 (KHTML, like Gecko) Version/6.0 Mobile/7E18WD Safari/8536.25");
123+
UserAgentInfo uai = parser.parse("Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/546.10 (KHTML, like Gecko) Version/6.0 Mobile/7E18WD Safari/8536.25");
110124
assertNotNull(uai);
111125
assertEquals("Mobile Browser", uai.getType());
112126
assertEquals("Mobile Safari 6.0", uai.getUaName());
113127
assertEquals("Mobile Safari", uai.getUaFamily());
114128
assertEquals("Apple Inc.", uai.getUaCompany());
115-
assertEquals("iOS 7", uai.getOsName());
129+
assertEquals("iOS 6", uai.getOsName());
116130
assertEquals("iOS", uai.getOsFamily());
117131
assertEquals("Apple Inc.", uai.getOsCompany());
118-
132+
133+
// if (!testDeviceInfo) {
134+
// return;
135+
// }
119136
// assertTrue(uai.hasDeviceInfo());
120137
// assertEquals("Smartphone", uai.getDeviceType());
121138
// assertEquals("phone.png", uai.getDeviceIcon());
@@ -125,8 +142,7 @@ private void testSmartphoneAgent(UASparser parser) throws IOException {
125142
@Test
126143
public void testArrayIndexBug() throws IOException {
127144
// should not throw exception
128-
UASparser p = new UASparser(OnlineUpdater.getVendoredInputStream());
145+
UASparser p = new UASparser(getDataInputStream());
129146
UserAgentInfo uai = p.parse("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; MyIE2; MRA 4.7 (build 01670); .NET CLR 1.1.4322)");
130147
}
131-
132148
}

src/test/java/cz/mallat/uasparser/TestSuite.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.junit.runners.Suite.SuiteClasses;
66

77
@RunWith(Suite.class)
8-
@SuiteClasses({ TestOnlineUpdater.class, TestParsers.class })
8+
@SuiteClasses({ TestOnlineUpdater.class, TestParsers.class, TestOldDatabase.class })
99
public class TestSuite {
1010

1111
}
91 KB
Binary file not shown.

0 commit comments

Comments
 (0)