Search in sources :

Example 1 with MutableAgentField

use of nl.basjes.parse.useragent.AgentField.MutableAgentField in project yauaa by nielsbasjes.

the class TestUseragent method comparingUserAgentFields.

@Test
void comparingUserAgentFields() {
    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);
    // This is mainly used when rendering in a debugger.
    assertEquals("{ value:'One', confidence:'1', default:'Foo', isDefault:false }", field0.toString());
    assertEquals("{ value:'One', confidence:'1', default:'Foo', isDefault:false }", field1.toString());
    assertEquals("{ value:'Two', confidence:'1', default:'Foo', isDefault:false }", field2.toString());
    assertEquals("{ value:'One', confidence:'2', default:'Foo', isDefault:false }", field3.toString());
    assertEquals("{ value:'One', confidence:'1', default:null, isDefault:false }", field4.toString());
    assertEquals(field1, field1);
    assertEquals(field1.hashCode(), field1.hashCode());
    assertEquals(field0, field1);
    assertEquals(field1, field0);
    assertNotEquals("String", field1);
    assertNotEquals(field1, "String");
    assertNotEquals(field1.hashCode(), field2.hashCode());
    assertNotEquals(field1.hashCode(), field3.hashCode());
    assertNotEquals(field1.hashCode(), field4.hashCode());
    assertNotEquals(field1, field2);
    assertNotEquals(field1, field3);
    assertNotEquals(field1, field4);
}
Also used : MutableAgentField(nl.basjes.parse.useragent.AgentField.MutableAgentField) Test(org.junit.jupiter.api.Test)

Example 2 with MutableAgentField

use of nl.basjes.parse.useragent.AgentField.MutableAgentField 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 3 with MutableAgentField

use of nl.basjes.parse.useragent.AgentField.MutableAgentField 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 4 with MutableAgentField

use of nl.basjes.parse.useragent.AgentField.MutableAgentField in project yauaa by nielsbasjes.

the class TestUseragent method testUseragentValuesDebug.

private void testUseragentValuesDebug(boolean debug) {
    String name = "Attribute";
    String uaString = "Foo Bar";
    MutableUserAgent agent = new MutableUserAgent(uaString);
    agent.setDebug(debug);
    // Setting unknown new attributes
    assertNotNull(agent.get("UnknownOne"));
    assertEquals("Unknown", agent.getValue("UnknownOne"));
    assertEquals(-1, agent.getConfidence("UnknownOne").longValue());
    agent.set("UnknownOne", "One", 111);
    check(agent, "UnknownOne", "One", 111);
    ((MutableAgentField) agent.get("UnknownOne")).reset();
    check(agent, "UnknownOne", "Unknown", -1);
    // Setting unknown new attributes FORCED
    assertNotNull(agent.get("UnknownTwo"));
    assertEquals("Unknown", agent.getValue("UnknownTwo"));
    assertEquals(-1, agent.getConfidence("UnknownTwo").longValue());
    agent.setForced("UnknownTwo", "Two", 222);
    check(agent, "UnknownTwo", "Two", 222);
    ((MutableAgentField) agent.get("UnknownTwo")).reset();
    check(agent, "UnknownTwo", "Unknown", -1);
    // Setting known attributes
    check(agent, "AgentClass", "Unknown", -1);
    agent.set("AgentClass", "One", 111);
    check(agent, "AgentClass", "One", 111);
    ((MutableAgentField) agent.get("AgentClass")).reset();
    check(agent, "AgentClass", "Unknown", -1);
    // Setting known attributes FORCED
    check(agent, "AgentVersion", "??", -1);
    agent.setForced("AgentVersion", "Two", 222);
    check(agent, "AgentVersion", "Two", 222);
    ((MutableAgentField) agent.get("AgentVersion")).reset();
    check(agent, "AgentVersion", "??", -1);
    agent.set(name, "One", 111);
    check(agent, name, "One", 111);
    // Should be ignored
    agent.set(name, "Two", 22);
    check(agent, name, "One", 111);
    // Should be used
    agent.set(name, "Three", 333);
    check(agent, name, "Three", 333);
    // Should be used
    agent.setForced(name, "Four", 4);
    check(agent, name, "Four", 4);
    // Should be ignored
    agent.set(name, "<<<null>>>", 2);
    check(agent, name, "Four", 4);
    // Should be used
    agent.set(name, "<<<null>>>", 5);
    check(agent, name, "Unknown", 5);
    // Should be IGNORED
    agent.set(name, "Four", 4);
    check(agent, name, "Unknown", 5);
    // Set a 'normal' value again.
    // Should be used
    agent.set(name, "Three", 333);
    check(agent, name, "Three", 333);
    MutableAgentField field = (MutableAgentField) agent.get(name);
    // Should be used
    field.setValueForced("Five", 5);
    check(agent, name, "Five", 5);
    // Should be used
    field.setValueForced("<<<null>>>", 4);
    check(agent, name, "Unknown", 4);
}
Also used : MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent) MutableAgentField(nl.basjes.parse.useragent.AgentField.MutableAgentField)

Example 5 with MutableAgentField

use of nl.basjes.parse.useragent.AgentField.MutableAgentField in project yauaa by nielsbasjes.

the class TestUseragent method limitedToString.

@Test
void limitedToString() {
    List<String> wanted = Arrays.asList("Unsure", "DeviceClass", "AgentVersion");
    // When only asking for a limited set of fields then the internal datastructures are
    // initialized with only the known attributes for which we have 'non standard' default values.
    MutableUserAgent userAgent = new MutableUserAgent("Some Agent", wanted);
    assertEquals("  - user_agent_string  : 'Some Agent'\n" + "    DeviceClass        : 'Unknown'\n" + "    AgentVersion       : '??'\n" + "    Unsure             : 'Unknown'\n", userAgent.toString());
    // We set the wanted field
    // --> isDefault !
    ((MutableAgentField) (userAgent.get("Unsure"))).setValueForced("--> Unsure", -1);
    userAgent.set("DeviceClass", "--> DeviceClass", 1);
    userAgent.set("AgentVersion", "--> AgentVersion", 2);
    // We also set an unwanted field
    userAgent.set("SomethingElse", "--> SomethingElse", 3);
    // We should be able to retrieve all of them
    assertEquals("--> Unsure", userAgent.getValue("Unsure"));
    assertEquals("--> DeviceClass", userAgent.getValue("DeviceClass"));
    assertEquals("--> AgentVersion", userAgent.getValue("AgentVersion"));
    assertEquals("--> SomethingElse", userAgent.getValue("SomethingElse"));
    // Default value?
    assertTrue(userAgent.get("Unsure").isDefaultValue());
    assertFalse(userAgent.get("DeviceClass").isDefaultValue());
    assertFalse(userAgent.get("AgentVersion").isDefaultValue());
    assertFalse(userAgent.get("SomethingElse").isDefaultValue());
    // The output map if requested should only contain the wanted fields.
    assertEquals("  - user_agent_string  : 'Some Agent'\n" + "    DeviceClass        : '--> DeviceClass'\n" + "    AgentVersion       : '--> AgentVersion'\n" + "    Unsure             : '--> Unsure'\n", userAgent.toString());
    userAgent.destroy();
}
Also used : MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent) MutableAgentField(nl.basjes.parse.useragent.AgentField.MutableAgentField) Test(org.junit.jupiter.api.Test)

Aggregations

MutableAgentField (nl.basjes.parse.useragent.AgentField.MutableAgentField)6 Test (org.junit.jupiter.api.Test)5 MutableUserAgent (nl.basjes.parse.useragent.UserAgent.MutableUserAgent)4 ImmutableUserAgent (nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent)2