Search in sources :

Example 16 with User

use of org.infinispan.protostream.domain.User in project protostream by infinispan.

the class MarshallingTest method doMarshallUserTest.

private void doMarshallUserTest(EncoderMethod<User> method) throws Exception {
    ImmutableSerializationContext ctx = createContext();
    User user = new User();
    user.setId(1);
    user.setName("John");
    user.setSurname("Batman");
    user.setGender(User.Gender.MALE);
    user.setAccountIds(new HashSet<>(Arrays.asList(1, 3)));
    user.setAddresses(Collections.singletonList(new Address("Old Street", "XYZ42", -12)));
    User decoded = method.encodeAndDecode(user, ctx);
    assertEquals(1, decoded.getId());
    assertEquals("John", decoded.getName());
    assertEquals("Batman", decoded.getSurname());
    assertEquals(User.Gender.MALE, decoded.getGender());
    assertNotNull(decoded.getAddresses());
    assertEquals(1, decoded.getAddresses().size());
    assertEquals("Old Street", decoded.getAddresses().get(0).getStreet());
    assertEquals("XYZ42", decoded.getAddresses().get(0).getPostCode());
    assertNotNull(decoded.getAccountIds());
    assertEquals(2, decoded.getAccountIds().size());
    assertTrue(decoded.getAccountIds().contains(1));
    assertTrue(decoded.getAccountIds().contains(3));
}
Also used : User(org.infinispan.protostream.domain.User) Address(org.infinispan.protostream.domain.Address)

Example 17 with User

use of org.infinispan.protostream.domain.User in project protostream by infinispan.

the class ProtobufUtilTest method testFromByteArrayWithExtraPadding.

@Test(expected = MalformedProtobufException.class)
public void testFromByteArrayWithExtraPadding() throws Exception {
    ImmutableSerializationContext ctx = createContext();
    User user = new User();
    user.setId(1);
    user.setName("John");
    user.setSurname("Batman");
    user.setGender(User.Gender.MALE);
    user.setAccountIds(new HashSet<>(Arrays.asList(1, 3)));
    user.setAddresses(Arrays.asList(new Address("Old Street", "XYZ42", -12), new Address("Bond Street", "W23", 2)));
    byte[] userBytes = ProtobufUtil.toByteArray(ctx, user);
    byte[] userBytesWithPadding = new byte[userBytes.length + 20];
    System.arraycopy(userBytes, 0, userBytesWithPadding, 0, userBytes.length);
    Arrays.fill(userBytesWithPadding, userBytes.length, userBytes.length + 20, (byte) 42);
    // this must fail
    ProtobufUtil.fromByteArray(ctx, userBytesWithPadding, User.class);
}
Also used : User(org.infinispan.protostream.domain.User) Address(org.infinispan.protostream.domain.Address) Test(org.junit.Test) AbstractProtoStreamTest(org.infinispan.protostream.test.AbstractProtoStreamTest)

Example 18 with User

use of org.infinispan.protostream.domain.User in project protostream by infinispan.

the class ProtobufUtilTest method testCanonicalJSON.

@Test
public void testCanonicalJSON() throws Exception {
    ImmutableSerializationContext ctx = createContext();
    User user = new User();
    user.setId(1);
    user.setName("John");
    user.setSurname("Batman");
    user.setGender(User.Gender.MALE);
    user.setAccountIds(new HashSet<>(Arrays.asList(1, 3)));
    user.setAddresses(Arrays.asList(new Address("Old Street", "XYZ42", -12, false), new Address("Bond Street", "W23", 2, true)));
    Address address = new Address("Abbey Rd", "NW89AY", 3);
    Account account = createAccount();
    testJsonConversion(ctx, address);
    testJsonConversion(ctx, 3.14);
    testJsonConversion(ctx, 777L);
    testJsonConversion(ctx, 3.14f);
    testJsonConversion(ctx, 1);
    testJsonConversion(ctx, null);
    testJsonConversion(ctx, true);
    testJsonConversion(ctx, "Merry Christmas, you filthy animal. And a Happy New Year!");
    testJsonConversion(ctx, User.Gender.FEMALE);
    testJsonConversion(ctx, account);
    testJsonConversion(ctx, user);
}
Also used : Account(org.infinispan.protostream.domain.Account) User(org.infinispan.protostream.domain.User) Address(org.infinispan.protostream.domain.Address) Test(org.junit.Test) AbstractProtoStreamTest(org.infinispan.protostream.test.AbstractProtoStreamTest)

Example 19 with User

use of org.infinispan.protostream.domain.User in project protostream by infinispan.

the class PerformanceTest method testComparativeReadWrite.

/**
 * We run a total of 100 million writes and reads and expect better performance form ProtoStream than the other
 * marshallers, allowing 5 cases out of 1000 to have bad performance due to unfavorable thread scheduling, time
 * measurement inaccuracies, garbage collection pauses or whatever bad karma.
 */
@Test
public void testComparativeReadWrite() throws Exception {
    User user = createTestObject();
    int jinx = 0;
    byte[][] bytes = new byte[3][];
    long[][] results = new long[3][1];
    for (int i = 0; i < NUM_OUTER_LOOPS; i++) {
        log.infof("----------------------- # %d ------------------------", i);
        bytes[0] = writeWithProtoStream(user, results[0]);
        log.infof("ProtoStream payload length          = %d bytes", bytes[0].length);
        bytes[1] = writeWithJavaSerialization(user, results[1]);
        log.infof("Java serialization length           = %d bytes", bytes[1].length);
        bytes[2] = writeWithJBossMarshalling(user, results[2]);
        log.infof("JBoss Marshalling payload length    = %d bytes", bytes[2].length);
        // assert smaller size for Protobuf encoding
        assertTrue(bytes[0].length < bytes[1].length);
        assertTrue(bytes[0].length < bytes[2].length);
        // check better time
        if (/*results[0][0] > results[1][0] || */
        results[0][0] > results[2][0]) {
            jinx++;
        }
    }
    // allow 5 cases where performance is worse than others
    assertTrue(jinx <= 5);
    jinx = 0;
    for (int i = 0; i < NUM_OUTER_LOOPS; i++) {
        log.infof("---------------------- # %d -------------------------", i);
        readWithProtoStream(bytes[0], results[0]);
        readWithJavaSerialization(bytes[1], results[1]);
        readWithJBossMarshalling(bytes[2], results[2]);
        // check smaller time
        if (results[0][0] > results[1][0] || results[0][0] > results[2][0]) {
            jinx++;
        }
    }
    // allow 5 cases where performance is worse than others
    assertTrue(jinx <= 5);
}
Also used : User(org.infinispan.protostream.domain.User) Test(org.junit.Test)

Example 20 with User

use of org.infinispan.protostream.domain.User in project protostream by infinispan.

the class NoteMarshaller method readFrom.

@Override
public Note readFrom(ProtoStreamReader reader) throws IOException {
    String text = reader.readString("text");
    User author = reader.readObject("author", User.class);
    Note note2 = reader.readObject("note", Note.class);
    List<Note> notes = reader.readCollection("notes", new ArrayList<>(), Note.class);
    Date creationDate = reader.readDate("creationDate");
    byte[] digest = reader.readBytes("digest");
    byte[] blurb = reader.readBytes("blurb");
    Note note = new Note();
    note.setText(text);
    note.setAuthor(author);
    note.note = note2;
    note.notes = notes;
    note.setCreationDate(creationDate);
    note.setDigest(digest);
    note.setBlurb(blurb);
    return note;
}
Also used : User(org.infinispan.protostream.domain.User) Note(org.infinispan.protostream.domain.Note) Date(java.util.Date)

Aggregations

User (org.infinispan.protostream.domain.User)21 Test (org.junit.Test)15 Address (org.infinispan.protostream.domain.Address)12 AbstractProtoStreamTest (org.infinispan.protostream.test.AbstractProtoStreamTest)11 ArrayList (java.util.ArrayList)3 Date (java.util.Date)2 SerializationContext (org.infinispan.protostream.SerializationContext)2 Descriptor (org.infinispan.protostream.descriptors.Descriptor)2 FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)2 Note (org.infinispan.protostream.domain.Note)2 IOException (java.io.IOException)1 Instant (java.time.Instant)1 List (java.util.List)1 ImmutableSerializationContext (org.infinispan.protostream.ImmutableSerializationContext)1 MessageMarshaller (org.infinispan.protostream.MessageMarshaller)1 Configuration (org.infinispan.protostream.config.Configuration)1 EnumDescriptor (org.infinispan.protostream.descriptors.EnumDescriptor)1 GenericDescriptor (org.infinispan.protostream.descriptors.GenericDescriptor)1 Account (org.infinispan.protostream.domain.Account)1