Skip to content
Browse files

add test for serialization/deserialization of protobuf collection

  • Loading branch information...
1 parent de54862 commit 66ef6af61b2f080211b993d26dada8f468d83fdf Soo Kim committed Apr 4, 2012
Showing with 27 additions and 0 deletions.
  1. +27 −0 src/test/java/org/sooo/ProtocolBufferTest.java
View
27 src/test/java/org/sooo/ProtocolBufferTest.java
@@ -1,13 +1,20 @@
package org.sooo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
+import java.util.List;
import org.junit.Test;
import org.sooo.LoginLogProtos.LoginLog;
+import org.sooo.LoginLogProtos.LoginLogs;
+import com.google.common.collect.Lists;
import com.google.gson.Gson;
+import com.google.protobuf.InvalidProtocolBufferException;
public class ProtocolBufferTest {
@@ -68,4 +75,24 @@ public void compareByteLength() throws Exception {
bos.close();
return bos.toByteArray();
}
+
+ @Test
+ public void serializeAndDeserializeLoginLogs()
+ throws InvalidProtocolBufferException {
+ LoginLogs loginLogs = createLoginLogs(3);
+ String expected = loginLogs.toString();
+
+ byte[] serialized = loginLogs.toByteArray();
+
+ LoginLogs deserialized = LoginLogs.parseFrom(serialized);
+ assertThat(deserialized.toString(), is(expected));
+ }
+
+ private LoginLogs createLoginLogs(int size) {
+ List<LoginLog> list = Lists.newArrayList();
+ for (int i = 0; i < size; i++)
+ list.add(LoginLog.newBuilder().setTimestamp(timestamp + i)
+ .setId(id + i).setUrl(url + i).build());
+ return LoginLogs.newBuilder().addAllLoginLogs(list).build();
+ }
}

0 comments on commit 66ef6af

Please sign in to comment.
Something went wrong with that request. Please try again.