|
| 1 | +package com.baeldung.kryo; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.io.FileInputStream; |
| 6 | +import java.io.FileNotFoundException; |
| 7 | +import java.io.FileOutputStream; |
| 8 | +import java.util.Date; |
| 9 | +import java.util.logging.Level; |
| 10 | +import java.util.logging.Logger; |
| 11 | + |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +import com.esotericsoftware.kryo.Kryo; |
| 16 | +import com.esotericsoftware.kryo.io.Input; |
| 17 | +import com.esotericsoftware.kryo.io.Output; |
| 18 | +import com.esotericsoftware.kryo.serializers.JavaSerializer; |
| 19 | + |
| 20 | +public class KryoUnitTest { |
| 21 | + |
| 22 | + private Kryo kryo; |
| 23 | + private Output output; |
| 24 | + private Input input; |
| 25 | + |
| 26 | + @Before |
| 27 | + public void init() { |
| 28 | + kryo = new Kryo(); |
| 29 | + try { |
| 30 | + output = new Output(new FileOutputStream("file.dat")); |
| 31 | + input = new Input(new FileInputStream("file.dat")); |
| 32 | + } catch (FileNotFoundException ex) { |
| 33 | + Logger.getLogger(KryoUnitTest.class.getName()) |
| 34 | + .log(Level.SEVERE, null, ex); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void givenObject_whenSerializing_thenReadCorrectly() { |
| 40 | + Object someObject = "Some string"; |
| 41 | + |
| 42 | + kryo.writeClassAndObject(output, someObject); |
| 43 | + output.close(); |
| 44 | + |
| 45 | + Object theObject = kryo.readClassAndObject(input); |
| 46 | + input.close(); |
| 47 | + |
| 48 | + assertEquals(theObject, "Some string"); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void givenObjects_whenSerializing_thenReadCorrectly() { |
| 53 | + String someString = "Multiple Objects"; |
| 54 | + Date someDate = new Date(915170400000L); |
| 55 | + |
| 56 | + kryo.writeObject(output, someString); |
| 57 | + kryo.writeObject(output, someDate); |
| 58 | + output.close(); |
| 59 | + |
| 60 | + String readString = kryo.readObject(input, String.class); |
| 61 | + Date readDate = kryo.readObject(input, Date.class); |
| 62 | + input.close(); |
| 63 | + |
| 64 | + assertEquals(readString, "Multiple Objects"); |
| 65 | + assertEquals(readDate.getTime(), 915170400000L); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void givenPerson_whenSerializing_thenReadCorrectly() { |
| 70 | + Person person = new Person(); |
| 71 | + |
| 72 | + kryo.writeObject(output, person); |
| 73 | + output.close(); |
| 74 | + |
| 75 | + Person readPerson = kryo.readObject(input, Person.class); |
| 76 | + input.close(); |
| 77 | + |
| 78 | + assertEquals(readPerson.getName(), "John Doe"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void givenPerson_whenUsingCustomSerializer_thenReadCorrectly() { |
| 83 | + Person person = new Person(); |
| 84 | + person.setAge(0); |
| 85 | + kryo.register(Person.class, new PersonSerializer()); |
| 86 | + |
| 87 | + kryo.writeObject(output, person); |
| 88 | + output.close(); |
| 89 | + |
| 90 | + Person readPerson = kryo.readObject(input, Person.class); |
| 91 | + input.close(); |
| 92 | + |
| 93 | + assertEquals(readPerson.getName(), "John Doe"); |
| 94 | + assertEquals(readPerson.getAge(), 18); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void givenPerson_whenCustomSerialization_thenReadCorrectly() { |
| 99 | + Person person = new Person(); |
| 100 | + |
| 101 | + kryo.writeObject(output, person); |
| 102 | + output.close(); |
| 103 | + |
| 104 | + Person readPerson = kryo.readObject(input, Person.class); |
| 105 | + input.close(); |
| 106 | + |
| 107 | + assertEquals(readPerson.getName(), "John Doe"); |
| 108 | + assertEquals(readPerson.getAge(), 18); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void givenJavaSerializable_whenSerializing_thenReadCorrectly() { |
| 113 | + ComplexClass complexClass = new ComplexClass(); |
| 114 | + kryo.register(ComplexClass.class, new JavaSerializer()); |
| 115 | + |
| 116 | + kryo.writeObject(output, complexClass); |
| 117 | + output.close(); |
| 118 | + |
| 119 | + ComplexClass readComplexObject = kryo.readObject(input, ComplexClass.class); |
| 120 | + input.close(); |
| 121 | + |
| 122 | + assertEquals(readComplexObject.getName(), "Bael"); |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments