3
3
import static org .junit .Assert .*;
4
4
5
5
import java .io .IOException ;
6
+ import java .io .InputStream ;
6
7
7
8
import org .junit .Test ;
8
9
9
10
/**
10
- * No real JUnit tests
11
+ * Test the various parser implementations
11
12
*
12
- * @author oli
13
+ * @author chetan
13
14
*
14
15
*/
15
16
public class TestParsers {
16
17
18
+ protected boolean testDeviceInfo = true ;
19
+
17
20
@ Test
18
21
public void runUAParser () throws IOException {
19
- UASparser p = new UASparser (OnlineUpdater . getVendoredInputStream ());
22
+ UASparser p = new UASparser (getDataInputStream ());
20
23
testUserAgents (p );
21
24
}
22
25
@@ -34,16 +37,24 @@ public void runCachedOnlineUAParser() throws IOException {
34
37
35
38
@ Test
36
39
public void testSingleThreadedParser () throws IOException {
37
- UASparser p = new SingleThreadedUASparser (OnlineUpdater . getVendoredInputStream ());
40
+ UASparser p = new SingleThreadedUASparser (getDataInputStream ());
38
41
testUserAgents (p );
39
42
}
40
43
41
44
@ Test
42
45
public void testMultithreadedParser () throws IOException {
43
- UASparser p = new MultithreadedUASparser (OnlineUpdater . getVendoredInputStream ());
46
+ UASparser p = new MultithreadedUASparser (getDataInputStream ());
44
47
testUserAgents (p );
45
48
}
46
49
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
+
47
58
private void testUserAgents (UASparser parser ) throws IOException {
48
59
testRobotAgents (parser );
49
60
testBrowserAgent (parser );
@@ -63,7 +74,7 @@ private void testRobotAgents(UASparser parser) throws IOException {
63
74
uai = parser .parse ("Pingdom.com_bot_version_1.4_(http://www.pingdom.com/)" );
64
75
assertTrue (uai .isRobot ());
65
76
}
66
-
77
+
67
78
private void testBrowserAgent (UASparser parser ) throws IOException {
68
79
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" );
69
80
assertNotNull (uai );
@@ -75,7 +86,7 @@ private void testBrowserAgent(UASparser parser) throws IOException {
75
86
assertEquals ("Windows" , uai .getOsFamily ());
76
87
assertEquals ("Microsoft Corporation." , uai .getOsCompany ());
77
88
}
78
-
89
+
79
90
private void testEmailAgent (UASparser parser ) throws IOException {
80
91
UserAgentInfo uai = parser .parse ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_5_8) AppleWebKit/534.50.2 (KHTML, like Gecko)" );
81
92
assertNotNull (uai );
@@ -87,35 +98,41 @@ private void testEmailAgent(UASparser parser) throws IOException {
87
98
assertEquals ("OS X" , uai .getOsFamily ());
88
99
assertEquals ("Apple Computer, Inc." , uai .getOsCompany ());
89
100
}
90
-
101
+
91
102
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" );
93
104
assertNotNull (uai );
94
105
assertEquals ("Mobile Browser" , uai .getType ());
95
- assertEquals ("Mobile Safari 7 .0" , uai .getUaName ());
106
+ assertEquals ("Mobile Safari 6 .0" , uai .getUaName ());
96
107
assertEquals ("Mobile Safari" , uai .getUaFamily ());
97
108
assertEquals ("Apple Inc." , uai .getUaCompany ());
98
- assertEquals ("iOS 7 " , uai .getOsName ());
109
+ assertEquals ("iOS 6 " , uai .getOsName ());
99
110
assertEquals ("iOS" , uai .getOsFamily ());
100
111
assertEquals ("Apple Inc." , uai .getOsCompany ());
101
-
112
+
113
+ if (!testDeviceInfo ) {
114
+ return ;
115
+ }
102
116
assertTrue (uai .hasDeviceInfo ());
103
117
assertEquals ("Tablet" , uai .getDeviceType ());
104
118
assertEquals ("tablet.png" , uai .getDeviceIcon ());
105
119
assertEquals ("http://user-agent-string.info/list-of-ua/device-detail?device=Tablet" , uai .getDeviceInfoUrl ());
106
120
}
107
-
121
+
108
122
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" );
110
124
assertNotNull (uai );
111
125
assertEquals ("Mobile Browser" , uai .getType ());
112
126
assertEquals ("Mobile Safari 6.0" , uai .getUaName ());
113
127
assertEquals ("Mobile Safari" , uai .getUaFamily ());
114
128
assertEquals ("Apple Inc." , uai .getUaCompany ());
115
- assertEquals ("iOS 7 " , uai .getOsName ());
129
+ assertEquals ("iOS 6 " , uai .getOsName ());
116
130
assertEquals ("iOS" , uai .getOsFamily ());
117
131
assertEquals ("Apple Inc." , uai .getOsCompany ());
118
-
132
+
133
+ // if (!testDeviceInfo) {
134
+ // return;
135
+ // }
119
136
// assertTrue(uai.hasDeviceInfo());
120
137
// assertEquals("Smartphone", uai.getDeviceType());
121
138
// assertEquals("phone.png", uai.getDeviceIcon());
@@ -125,8 +142,7 @@ private void testSmartphoneAgent(UASparser parser) throws IOException {
125
142
@ Test
126
143
public void testArrayIndexBug () throws IOException {
127
144
// should not throw exception
128
- UASparser p = new UASparser (OnlineUpdater . getVendoredInputStream ());
145
+ UASparser p = new UASparser (getDataInputStream ());
129
146
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)" );
130
147
}
131
-
132
148
}
0 commit comments