UdgerParserTest.java
1package org.udger.parser;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.fail;
5
6import java.io.IOException;
7import java.net.URL;
8import java.net.UnknownHostException;
9import java.sql.SQLException;
10import java.util.concurrent.ConcurrentLinkedQueue;
11import java.util.concurrent.CyclicBarrier;
12
13import org.junit.After;
14import org.junit.Before;
15import org.junit.Test;
16
17public class UdgerParserTest {
18
19 private UdgerParser parser;
20 private UdgerParser inMemoryParser;
21 private UdgerParser.ParserDbData parserDbData;
22
23 @Before
24 public void initialize() throws SQLException {
25 URL resource = this.getClass().getClassLoader().getResource("udgerdb_test_v3.dat");
26 parserDbData = new UdgerParser.ParserDbData(resource.getFile());
27 parser = new UdgerParser(parserDbData);
28 inMemoryParser = new UdgerParser(parserDbData, true, 0); // no cache
29 }
30
31 @After
32 public void close() throws IOException {
33 parser.close();
34 }
35
36 @Test
37 public void testUaString1() throws SQLException {
38 String uaQuery = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0";
39 UdgerUaResult qr = parser.parseUa(uaQuery);
40 assertEquals(qr.getUa(), "Firefox 40.0");
41 assertEquals(qr.getOs(), "Windows 10");
42 assertEquals(qr.getUaFamily(), "Firefox");
43 }
44
45 @Test
46 public void testIp() throws SQLException, UnknownHostException {
47 String ipQuery = "108.61.199.93";
48 UdgerIpResult qr = parser.parseIp(ipQuery);
49 assertEquals(qr.getIpClassificationCode(), "crawler");
50 }
51
52 @Test
53 public void testUaStringInMemoryParser() throws SQLException {
54 String uaQuery = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0";
55 UdgerUaResult qr = inMemoryParser.parseUa(uaQuery);
56 assertEquals(qr.getUa(), "Firefox 40.0");
57 assertEquals(qr.getOs(), "Windows 10");
58 assertEquals(qr.getUaFamily(), "Firefox");
59 }
60
61 @Test
62 public void testIpInMemoryParser() throws SQLException, UnknownHostException {
63 String ipQuery = "108.61.199.93";
64 UdgerIpResult qr = inMemoryParser.parseIp(ipQuery);
65 assertEquals(qr.getIpClassificationCode(), "crawler");
66 }
67
68 @Test
69 public void testParserDbDataThreadSafety() throws Throwable {
70 final int numThreads = 500;
71 final String uaQuery = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0";
72
73 final CyclicBarrier gate = new CyclicBarrier(numThreads);
74 final ConcurrentLinkedQueue<Throwable> failures = new ConcurrentLinkedQueue<>();
75
76 Thread[] threads = new Thread[numThreads];
77 for (int i = 0; i < numThreads; i++) {
78 threads[i] = new Thread(new Runnable() {
79 @Override
80 public void run() {
81 UdgerParser threadParser = new UdgerParser(parserDbData);
82 try {
83 gate.await();
84 for (int j = 0; j < 100; j++) {
85 UdgerUaResult qr = threadParser.parseUa(uaQuery);
86 assertEquals(qr.getUa(), "Firefox 40.0");
87 assertEquals(qr.getOs(), "Windows 10");
88 assertEquals(qr.getUaFamily(), "Firefox");
89 }
90 } catch (Throwable t) {
91 failures.add(t);
92 }
93 }
94 });
95 threads[i].start();
96 }
97
98 for (int i = 0; i < numThreads; i++) {
99 threads[i].join();
100 }
101
102 if (!failures.isEmpty()) {
103 for (Throwable throwable : failures) {
104 throwable.printStackTrace();
105 }
106
107 fail("Parsing threads failed, see printed exceptions");
108 }
109 }
110}
UdgerIpResult parseIp(String ipString)
UdgerUaResult parseUa(String uaString)