Search in sources :

Example 1 with MutableUserAgent

use of nl.basjes.parse.useragent.UserAgent.MutableUserAgent in project yauaa by nielsbasjes.

the class TestUseragent method comparingUserAgents.

@Test
void comparingUserAgents() {
    MutableUserAgent baseAgent = new MutableUserAgent("Something 2");
    MutableUserAgent mutableAgent0 = new MutableUserAgent("Something 2");
    MutableUserAgent mutableAgent1 = new MutableUserAgent("Something 1");
    MutableUserAgent mutableAgent2 = new MutableUserAgent("Something 2");
    MutableUserAgent mutableAgent3 = new MutableUserAgent("Something 2");
    MutableUserAgent mutableAgent4 = new MutableUserAgent("Something 2");
    MutableAgentField field0 = new MutableAgentField("Foo");
    field0.setValue("One", 1);
    MutableAgentField field1 = new MutableAgentField("Foo");
    field1.setValue("One", 1);
    // Same, different value
    MutableAgentField field2 = new MutableAgentField("Foo");
    field2.setValue("Two", 1);
    // Same, different confidence
    MutableAgentField field3 = new MutableAgentField("Foo");
    field3.setValue("One", 2);
    // Same, different default
    MutableAgentField field4 = new MutableAgentField(null);
    field4.setValue("One", 1);
    // We compare the base agent with 4 variations
    baseAgent.setImmediateForTesting("Field", field0);
    // Same
    mutableAgent0.setImmediateForTesting("Field", field1);
    // Different useragent
    mutableAgent1.setImmediateForTesting("Field", field1);
    // Different field value
    mutableAgent2.setImmediateForTesting("Field", field2);
    // Different field confidence
    mutableAgent3.setImmediateForTesting("Field", field3);
    // Different field default value
    mutableAgent4.setImmediateForTesting("Field", field4);
    // Check em
    assertEquals(baseAgent, baseAgent);
    assertEquals(baseAgent, mutableAgent0);
    assertEquals(mutableAgent0, baseAgent);
    assertEquals(baseAgent.hashCode(), mutableAgent0.hashCode());
    assertNotEquals(baseAgent, mutableAgent2);
    assertNotEquals(baseAgent, mutableAgent3);
    assertNotEquals(baseAgent, mutableAgent4);
    assertNotEquals("String", mutableAgent1);
    assertNotEquals(mutableAgent1, "String");
    UserAgent immutableAgent0 = new ImmutableUserAgent(mutableAgent0);
    UserAgent immutableAgent1 = new ImmutableUserAgent(mutableAgent1);
    UserAgent immutableAgent2 = new ImmutableUserAgent(mutableAgent2);
    UserAgent immutableAgent3 = new ImmutableUserAgent(mutableAgent3);
    UserAgent immutableAgent4 = new ImmutableUserAgent(mutableAgent4);
    assertEquals(mutableAgent0.hashCode(), immutableAgent0.hashCode());
    assertEquals(mutableAgent1.hashCode(), immutableAgent1.hashCode());
    assertEquals(mutableAgent2.hashCode(), immutableAgent2.hashCode());
    assertEquals(mutableAgent3.hashCode(), immutableAgent3.hashCode());
    assertEquals(mutableAgent4.hashCode(), immutableAgent4.hashCode());
    assertEquals(mutableAgent0, immutableAgent0);
    assertEquals(mutableAgent1, immutableAgent1);
    assertEquals(mutableAgent2, immutableAgent2);
    assertEquals(mutableAgent3, immutableAgent3);
    assertEquals(mutableAgent4, immutableAgent4);
    assertEquals(immutableAgent0, mutableAgent0);
    assertEquals(immutableAgent1, mutableAgent1);
    assertEquals(immutableAgent2, mutableAgent2);
    assertEquals(immutableAgent3, mutableAgent3);
    assertEquals(immutableAgent4, mutableAgent4);
    assertEquals(mutableAgent0.toString(), immutableAgent0.toString());
    assertEquals(mutableAgent1.toString(), immutableAgent1.toString());
    assertEquals(mutableAgent2.toString(), immutableAgent2.toString());
    assertEquals(mutableAgent3.toString(), immutableAgent3.toString());
    assertEquals(mutableAgent4.toString(), immutableAgent4.toString());
    assertEquals(mutableAgent0.toXML(), immutableAgent0.toXML());
    assertEquals(mutableAgent1.toXML(), immutableAgent1.toXML());
    assertEquals(mutableAgent2.toXML(), immutableAgent2.toXML());
    assertEquals(mutableAgent3.toXML(), immutableAgent3.toXML());
    assertEquals(mutableAgent4.toXML(), immutableAgent4.toXML());
    assertEquals(mutableAgent0.toYamlTestCase(), immutableAgent0.toYamlTestCase());
    assertEquals(mutableAgent1.toYamlTestCase(), immutableAgent1.toYamlTestCase());
    assertEquals(mutableAgent2.toYamlTestCase(), immutableAgent2.toYamlTestCase());
    assertEquals(mutableAgent3.toYamlTestCase(), immutableAgent3.toYamlTestCase());
    assertEquals(mutableAgent4.toYamlTestCase(), immutableAgent4.toYamlTestCase());
}
Also used : MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent) ImmutableUserAgent(nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent) MutableAgentField(nl.basjes.parse.useragent.AgentField.MutableAgentField) MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent) ImmutableUserAgent(nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent) Test(org.junit.jupiter.api.Test)

Example 2 with MutableUserAgent

use of nl.basjes.parse.useragent.UserAgent.MutableUserAgent in project yauaa by nielsbasjes.

the class TestUseragent method fullToString.

@Test
void fullToString() {
    MutableUserAgent userAgent1 = new MutableUserAgent("Some'Agent");
    ((MutableAgentField) userAgent1.get("Niels")).setValue("Basjes", 42);
    ((MutableAgentField) userAgent1.get("BackToDefault")).setValue("One", 42);
    assertEquals("One", userAgent1.getValue("BackToDefault"));
    assertEquals(42, userAgent1.getConfidence("BackToDefault"));
    ((MutableAgentField) userAgent1.get("BackToDefault")).setValue("<<<null>>>", 84);
    assertTrue(userAgent1.get("BackToDefault").isDefaultValue());
    assertEquals("Unknown", userAgent1.getValue("BackToDefault"));
    assertEquals(84, userAgent1.getConfidence("BackToDefault"));
    assertEquals("Basjes", userAgent1.getValue("Niels"));
    assertEquals(42, userAgent1.getConfidence("Niels"));
    UserAgent userAgent2 = new ImmutableUserAgent(userAgent1);
    assertEquals("Basjes", userAgent2.getValue("Niels"));
    assertEquals(42, userAgent2.getConfidence("Niels"));
    // Also values that are "Default" are copied.
    assertTrue(userAgent2.get("BackToDefault").isDefaultValue());
    assertEquals("Unknown", userAgent2.getValue("BackToDefault"));
    // You may have a default value ("<<<null>>>") with a confidence.
    assertEquals(84, userAgent2.getConfidence("BackToDefault"));
    assertEquals(// You get the fields in the order you ask them!
    "  - user_agent_string  : 'Some''Agent'\n" + "    Niels              : 'Basjes'\n" + "    DeviceClass        : 'Unknown'\n", userAgent1.toString("Niels", "DeviceClass"));
    assertEquals("  - user_agent_string                : 'Some''Agent'\n" + "    DeviceClass                      : 'Unknown'\n" + "    DeviceName                       : 'Unknown'\n" + "    DeviceBrand                      : 'Unknown'\n" + "    OperatingSystemClass             : 'Unknown'\n" + "    OperatingSystemName              : 'Unknown'\n" + "    OperatingSystemVersion           : '??'\n" + "    OperatingSystemVersionMajor      : '??'\n" + "    OperatingSystemNameVersion       : 'Unknown ??'\n" + "    OperatingSystemNameVersionMajor  : 'Unknown ??'\n" + "    LayoutEngineClass                : 'Unknown'\n" + "    LayoutEngineName                 : 'Unknown'\n" + "    LayoutEngineVersion              : '??'\n" + "    LayoutEngineVersionMajor         : '??'\n" + "    LayoutEngineNameVersion          : 'Unknown ??'\n" + "    LayoutEngineNameVersionMajor     : 'Unknown ??'\n" + "    AgentClass                       : 'Unknown'\n" + "    AgentName                        : 'Unknown'\n" + "    AgentVersion                     : '??'\n" + "    AgentVersionMajor                : '??'\n" + "    AgentNameVersion                 : 'Unknown ??'\n" + "    AgentNameVersionMajor            : 'Unknown ??'\n" + "    BackToDefault                    : 'Unknown'\n" + "    Niels                            : 'Basjes'\n", userAgent1.toString());
    assertEquals(userAgent1.getAvailableFieldNamesSorted(), userAgent2.getAvailableFieldNamesSorted());
    assertEquals(userAgent1.toString(), userAgent2.toString());
    assertEquals(userAgent1.toXML(), userAgent2.toXML());
    assertEquals(userAgent1.toYamlTestCase(), userAgent2.toYamlTestCase());
}
Also used : MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent) ImmutableUserAgent(nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent) MutableAgentField(nl.basjes.parse.useragent.AgentField.MutableAgentField) MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent) ImmutableUserAgent(nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent) Test(org.junit.jupiter.api.Test)

Example 3 with MutableUserAgent

use of nl.basjes.parse.useragent.UserAgent.MutableUserAgent in project yauaa by nielsbasjes.

the class TestClassifier method verifyEnum.

private void verifyEnum(String deviceClass) {
    MutableUserAgent userAgent = new MutableUserAgent();
    userAgent.set(DEVICE_CLASS, deviceClass, 1);
    assertEquals(deviceClass, getDeviceClass(userAgent).getValue());
}
Also used : MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent)

Example 4 with MutableUserAgent

use of nl.basjes.parse.useragent.UserAgent.MutableUserAgent in project yauaa by nielsbasjes.

the class TestDeveloperTools method validateStringOutputsAndMatches.

@Test
void validateStringOutputsAndMatches() {
    UserAgentAnalyzerTester uaa = UserAgentAnalyzerTester.newBuilder().withField("DeviceName").build();
    MutableUserAgent useragent = new MutableUserAgent("Mozilla/5.0 (Linux; Android 7.0; Nexus 6 Build/NBD90Z) " + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.124 Mobile Safari/537.36");
    ImmutableUserAgent parseResult = uaa.parse(useragent);
    assertTrue(parseResult.toString().contains("'Google Nexus 6'"));
    assertTrue(parseResult.toJson().contains("\"DeviceName\":\"Google Nexus 6\""));
    assertTrue(parseResult.toJson("DeviceName").contains("\"DeviceName\":\"Google Nexus 6\""));
    assertFalse(parseResult.toJson("DeviceClass").contains("\"DeviceName\":\"Google Nexus 6\""));
    assertTrue(parseResult.toXML().contains("<DeviceName>Google Nexus 6</DeviceName>"));
    assertTrue(parseResult.toXML("DeviceName").contains("<DeviceName>Google Nexus 6</DeviceName>"));
    assertFalse(parseResult.toXML("DeviceClass").contains("<DeviceName>Google Nexus 6</DeviceName>"));
    assertEquals("Google Nexus 6", parseResult.toMap().get("DeviceName"));
    assertEquals("Google Nexus 6", parseResult.toMap("DeviceName").get("DeviceName"));
    assertNull(parseResult.toMap("DeviceClass").get("DeviceName"));
    assertTrue(parseResult.toYamlTestCase(true).contains("'Google Nexus 6'"));
    boolean ok = false;
    for (Match match : uaa.getMatches()) {
        if ("agent.(1)product.(1)comments.(3)entry[3-3]".equals(match.getKey())) {
            assertEquals("Build", match.getValue());
            ok = true;
            break;
        }
    }
    assertTrue(ok, "Did not see the expected match.");
    ok = false;
    for (Match match : uaa.getUsedMatches(useragent)) {
        if ("agent.(1)product.(1)comments.(3)entry[3-3]".equals(match.getKey())) {
            assertEquals("Build", match.getValue());
            ok = true;
            break;
        }
    }
    assertTrue(ok, "Did not see the expected match.");
}
Also used : MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent) UserAgentAnalyzerTester(nl.basjes.parse.useragent.debug.UserAgentAnalyzerTester) ImmutableUserAgent(nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent) Match(nl.basjes.parse.useragent.analyze.MatchesList.Match) Test(org.junit.jupiter.api.Test)

Example 5 with MutableUserAgent

use of nl.basjes.parse.useragent.UserAgent.MutableUserAgent in project yauaa by nielsbasjes.

the class TestConcatenation method testFieldConcatenation.

@Test
void testFieldConcatenation() {
    FieldCalculator fc;
    MutableUserAgent userAgent = createUserAgent();
    fc = new ConcatNONDuplicatedCalculator("Combined1", "One", "Two");
    fc.calculate(userAgent);
    assertEquals("One Two", userAgent.getValue("Combined1"));
    fc = new ConcatNONDuplicatedCalculator("Combined2", "One", "One");
    fc.calculate(userAgent);
    assertEquals("One", userAgent.getValue("Combined2"));
    fc = new ConcatNONDuplicatedCalculator("Combined3", "MinusOne", "One");
    fc.calculate(userAgent);
    assertEquals("MinusOne One", userAgent.getValue("Combined3"));
    fc = new ConcatNONDuplicatedCalculator("Combined4", "One", "MinusOne");
    fc.calculate(userAgent);
    assertEquals("One MinusOne", userAgent.getValue("Combined4"));
}
Also used : MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent) FieldCalculator(nl.basjes.parse.useragent.calculate.FieldCalculator) ConcatNONDuplicatedCalculator(nl.basjes.parse.useragent.calculate.ConcatNONDuplicatedCalculator) Test(org.junit.jupiter.api.Test)

Aggregations

MutableUserAgent (nl.basjes.parse.useragent.UserAgent.MutableUserAgent)18 Test (org.junit.jupiter.api.Test)13 ConcatNONDuplicatedCalculator (nl.basjes.parse.useragent.calculate.ConcatNONDuplicatedCalculator)6 FieldCalculator (nl.basjes.parse.useragent.calculate.FieldCalculator)5 MutableAgentField (nl.basjes.parse.useragent.AgentField.MutableAgentField)4 ImmutableUserAgent (nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent)4 CalculateDeviceBrand (nl.basjes.parse.useragent.calculate.CalculateDeviceBrand)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 Match (nl.basjes.parse.useragent.analyze.MatchesList.Match)1 CalculateAgentName (nl.basjes.parse.useragent.calculate.CalculateAgentName)1 MajorVersionCalculator (nl.basjes.parse.useragent.calculate.MajorVersionCalculator)1 UserAgentAnalyzerTester (nl.basjes.parse.useragent.debug.UserAgentAnalyzerTester)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1