Search in sources :

Example 1 with Demo

use of nl.example.Demo in project yauaa by nielsbasjes.

the class AbstractSerializationTest method doSerDeCycle.

private void doSerDeCycle(boolean runTestsBefore) throws IOException, ClassNotFoundException {
    LOG.info("==============================================================");
    LOG.info("Create");
    LOG.info("--------------------------------------------------------------");
    Demo demoBefore = new Demo();
    if (runTestsBefore) {
        LOG.info("--------------------------------------------------------------");
        testParser(demoBefore);
    }
    // Make sure it has been initialized
    demoBefore.parse(null);
    String before = demoBefore.toString();
    LOG.info("--------------------------------------------------------------");
    LOG.info("Serialize");
    long serializeStartNs = System.nanoTime();
    byte[] bytes = serialize(demoBefore);
    long serializeStopNs = System.nanoTime();
    LOG.info("Serialize took {} ns ({} ms)", serializeStopNs - serializeStartNs, (serializeStopNs - serializeStartNs) / 1_000_000);
    LOG.info("The UserAgentAnalyzer was serialized into {} bytes", bytes.length);
    LOG.info("--------------------------------------------------------------");
    LOG.info("Deserialize");
    long deserializeStartNs = System.nanoTime();
    Demo demoAfter = deserialize(bytes);
    long deserializeStopNs = System.nanoTime();
    LOG.info("Done");
    LOG.info("Deserialize took {} ns ({} ms)", deserializeStopNs - deserializeStartNs, (deserializeStopNs - deserializeStartNs) / 1_000_000);
    demoAfter.parse(null);
    String after = demoAfter.toString();
    // To avoid getting >60MiB of error log if it goes wrong.
    assertEquals(before.length(), after.length());
    assertEquals(before, after);
    LOG.info("==============================================================");
    LOG.info("Validating when getting all fields");
    LOG.info("--------------------------------------------------------------");
    testParser(demoAfter);
    LOG.info("==============================================================");
}
Also used : Demo(nl.example.Demo)

Example 2 with Demo

use of nl.example.Demo in project yauaa by nielsbasjes.

the class SerializeWithKryo method deserialize.

Demo deserialize(byte[] bytes) {
    Kryo kryo = (Kryo) createKryo();
    ByteBufferInput byteBufferInput = new ByteBufferInput(bytes);
    return (Demo) kryo.readClassAndObject(byteBufferInput);
}
Also used : Demo(nl.example.Demo) ByteBufferInput(com.esotericsoftware.kryo.io.ByteBufferInput) Kryo(com.esotericsoftware.kryo.Kryo)

Example 3 with Demo

use of nl.example.Demo in project yauaa by nielsbasjes.

the class SerializeWithJava method deserialize.

Demo deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    try (ObjectInput in = new ObjectInputStream(bis)) {
        Object o = in.readObject();
        assertTrue(o instanceof Demo);
        return (Demo) o;
    }
}
Also used : Demo(nl.example.Demo) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(java.io.ObjectInput) ObjectInputStream(java.io.ObjectInputStream)

Example 4 with Demo

use of nl.example.Demo in project yauaa by nielsbasjes.

the class TestDemo method testParser.

@Test
void testParser() {
    Demo demo = new Demo();
    String userAgent = "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";
    UserAgent result = demo.parse(userAgent);
    assertTrue(result.toXML().contains("<DeviceName>Google Nexus 6</DeviceName>"), "The parser must extract the correct DeviceName");
}
Also used : Demo(nl.example.Demo) UserAgent(nl.basjes.parse.useragent.UserAgent) Test(org.junit.jupiter.api.Test)

Aggregations

Demo (nl.example.Demo)4 Kryo (com.esotericsoftware.kryo.Kryo)1 ByteBufferInput (com.esotericsoftware.kryo.io.ByteBufferInput)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ObjectInput (java.io.ObjectInput)1 ObjectInputStream (java.io.ObjectInputStream)1 UserAgent (nl.basjes.parse.useragent.UserAgent)1 Test (org.junit.jupiter.api.Test)1