package org.udger.parser;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.net.URL;
import java.net.UnknownHostException;
import java.sql.SQLException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class UdgerParserTest {

    private UdgerParser parser;
    private UdgerParser inMemoryParser;

    @Before
    public void initialize() throws SQLException {
        URL resource = this.getClass().getClassLoader().getResource("udgerdb_test_v3.dat");
        UdgerParser.ParserDbData parserDbData = new UdgerParser.ParserDbData(resource.getFile());
        parser = new UdgerParser(parserDbData);
        inMemoryParser = new UdgerParser(parserDbData, true, 0); // no cache
    }

    @After
    public void close() throws IOException {
        parser.close();
    }

    @Test
    public void testUaString1() throws SQLException {
        String uaQuery = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0";
        UdgerUaResult qr = parser.parseUa(uaQuery);
        assertEquals(qr.getUa(), "Firefox 40.0");
        assertEquals(qr.getOs(), "Windows 10");
        assertEquals(qr.getUaFamily(), "Firefox");
    }

    @Test
    public void testIp() throws SQLException, UnknownHostException {
        String ipQuery = "108.61.199.93";
        UdgerIpResult qr = parser.parseIp(ipQuery);
        assertEquals(qr.getIpClassificationCode(), "crawler");
    }

    @Test
    public void testUaStringInMemoryParser() throws SQLException {
        String uaQuery = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0";
        UdgerUaResult qr = inMemoryParser.parseUa(uaQuery);
        assertEquals(qr.getUa(), "Firefox 40.0");
        assertEquals(qr.getOs(), "Windows 10");
        assertEquals(qr.getUaFamily(), "Firefox");
    }

    @Test
    public void testIpInMemoryParser() throws SQLException, UnknownHostException {
        String ipQuery = "108.61.199.93";
        UdgerIpResult qr = inMemoryParser.parseIp(ipQuery);
        assertEquals(qr.getIpClassificationCode(), "crawler");
    }
}

@@ -64,16 +64,53 @@
 tEquals;
+%0Aimport static org.junit.Assert.fail;
 %0A%0Aimport
@@ -219,16 +219,110 @@
 ception;
+%0Aimport java.util.concurrent.ConcurrentLinkedQueue;%0Aimport java.util.concurrent.CyclicBarrier;
 %0A%0Aimport
@@ -491,16 +491,67 @@
 yParser;
+%0A    private UdgerParser.ParserDbData parserDbData;
 %0A%0A    @B
@@ -711,33 +711,8 @@
     
-UdgerParser.ParserDbData 
 pars
@@ -2190,32 +2190,32 @@
 rseIp(ipQuery);%0A
-
         assertEq
@@ -2266,11 +2266,1616 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testParserDbDataThreadSafety() throws Throwable %7B%0A        final int numThreads = 500;%0A        final String uaQuery = %22Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0%22;%0A%0A        final CyclicBarrier gate = new CyclicBarrier(numThreads);%0A        final ConcurrentLinkedQueue%3CThrowable%3E failures = new ConcurrentLinkedQueue%3C%3E();%0A%0A        Thread%5B%5D threads = new Thread%5BnumThreads%5D;%0A        for (int i = 0; i %3C numThreads; i++) %7B%0A            threads%5Bi%5D = new Thread(new Runnable() %7B%0A                @Override%0A                public void run() %7B%0A                    UdgerParser threadParser = new UdgerParser(parserDbData);%0A                    try %7B%0A                        gate.await();%0A                        for (int j = 0; j %3C 100; j++) %7B%0A                            UdgerUaResult qr = threadParser.parseUa(uaQuery);%0A                            assertEquals(qr.getUa(), %22Firefox 40.0%22);%0A                            assertEquals(qr.getOs(), %22Windows 10%22);%0A                            assertEquals(qr.getUaFamily(), %22Firefox%22);%0A                        %7D%0A                    %7D catch (Throwable t) %7B%0A                        failures.add(t);%0A                    %7D%0A                %7D%0A            %7D);%0A            threads%5Bi%5D.start();%0A        %7D%0A%0A        for (int i = 0; i %3C numThreads; i++) %7B%0A            threads%5Bi%5D.join();%0A        %7D%0A%0A        if (!failures.isEmpty()) %7B%0A            for (Throwable throwable : failures) %7B%0A                throwable.printStackTrace();%0A            %7D%0A%0A            fail(%22Parsing threads failed, see printed exceptions%22);%0A        %7D%0A    %7D
 %0A%7D%0A

Add test exercising ParserDbData thread safety