Example usage for org.springframework.core.serializer Serializer Serializer

List of usage examples for org.springframework.core.serializer Serializer Serializer

Introduction

In this page you can find the example usage for org.springframework.core.serializer Serializer Serializer.

Prototype

Serializer

Source Link

Usage

From source file:org.musa.tcpserver.MainTest.java

@Test
public void testSetup1() {
    System.out.println("Integration test2");

    //GenericXmlApplicationContext context = Main.setupTestContext();

    client.setSerializer(new Serializer<SpaceMarine>() {

        public void serialize(SpaceMarine spaceMarine, OutputStream out) throws IOException {

            /*//from  w  w w.jav a2  s. c  o  m
            disassembling the space marine...
            */

            byte[] nameParticles = (spaceMarine.getName() + ';').getBytes();
            out.write(nameParticles);

            byte[] chapterParticles = (spaceMarine.getChapter() + ';').getBytes();
            out.write(chapterParticles);

            byte[] killsParticles = (Integer.toString(spaceMarine.getKills()) + ';').getBytes();
            out.write(killsParticles);

            byte[] rankParticles = (spaceMarine.getRank().name() + ';').getBytes();
            out.write(rankParticles);

            byte[] loyaltyParticles = (spaceMarine.getLoyalty().name() + ';').getBytes();
            out.write(loyaltyParticles);

            byte[] statusParticles = (spaceMarine.getStatus().name() + ';').getBytes();
            out.write(statusParticles);

            byte[] damageParticles = (Integer.toString(spaceMarine.getDamage()) + ';').getBytes();
            out.write(damageParticles);

            out.flush();

        }

    });

    SpaceMarine horus = new SpaceMarine("Horus", "Sons of Horus", 999999, SMRank.Primarch, SMLoyalty.Traitor,
            999);
    Message<SpaceMarine> m = MessageBuilder.withPayload(horus).build();

    String res = gw.send(m);

    assertEquals("noone hurt", res);

    //
    //assertNotNull(context);
}

From source file:org.springframework.integration.jdbc.JdbcMessageStoreTests.java

@Test
@Transactional//  w  w  w .  j  a  va2  s  .c om
public void testSerializer() throws Exception {
    // N.B. these serializers are not realistic (just for test purposes)
    messageStore.setSerializer(new Serializer<Message<?>>() {
        @Override
        public void serialize(Message<?> object, OutputStream outputStream) throws IOException {
            outputStream.write(((Message<?>) object).getPayload().toString().getBytes());
            outputStream.flush();
        }
    });
    messageStore.setDeserializer(new Deserializer<GenericMessage<String>>() {
        @Override
        public GenericMessage<String> deserialize(InputStream inputStream) throws IOException {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            return new GenericMessage<String>(reader.readLine());
        }
    });
    Message<String> message = MessageBuilder.withPayload("foo").build();
    Message<String> saved = messageStore.addMessage(message);
    assertNotNull(messageStore.getMessage(message.getHeaders().getId()));
    Message<?> result = messageStore.getMessage(saved.getHeaders().getId());
    assertNotNull(result);
    assertEquals("foo", result.getPayload());
}

From source file:org.springframework.integration.jdbc.mysql.MySqlJdbcMessageStoreTests.java

@Test
@Transactional/*from w w w  .  ja v a  2s.  c o  m*/
public void testSerializer() throws Exception {
    // N.B. these serializers are not realistic (just for test purposes)
    messageStore.setSerializer(new Serializer<Message<?>>() {
        public void serialize(Message<?> object, OutputStream outputStream) throws IOException {
            outputStream.write(((Message<?>) object).getPayload().toString().getBytes());
            outputStream.flush();
        }
    });
    messageStore.setDeserializer(new Deserializer<GenericMessage<String>>() {
        public GenericMessage<String> deserialize(InputStream inputStream) throws IOException {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            return new GenericMessage<String>(reader.readLine());
        }
    });
    Message<String> message = MessageBuilder.withPayload("foo").build();
    Message<String> saved = messageStore.addMessage(message);
    assertNotNull(messageStore.getMessage(message.getHeaders().getId()));
    Message<?> result = messageStore.getMessage(saved.getHeaders().getId());
    assertNotNull(result);
    assertEquals("foo", result.getPayload());
}