|
19 | 19 |
|
20 | 20 | import com.google.common.collect.ImmutableList;
|
21 | 21 | import com.google.protobuf.ByteString;
|
| 22 | +import com.google.protobuf.LazyStringArrayList; |
| 23 | +import com.google.protobuf.UnmodifiableLazyStringList; |
| 24 | +import java.io.ByteArrayInputStream; |
| 25 | +import java.io.ByteArrayOutputStream; |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.ObjectInputStream; |
| 28 | +import java.io.ObjectOutputStream; |
| 29 | +import java.util.Arrays; |
22 | 30 | import java.util.Comparator;
|
23 | 31 | import java.util.List;
|
24 | 32 | import org.junit.Test;
|
@@ -98,4 +106,46 @@ public void compareTest() {
|
98 | 106 | RowCell.create("family1", col1, timestamp2, labels1, value1)))
|
99 | 107 | .isEqualTo(1);
|
100 | 108 | }
|
| 109 | + |
| 110 | + @Test |
| 111 | + public void testSerialization() throws IOException, ClassNotFoundException { |
| 112 | + LazyStringArrayList lazyList = new LazyStringArrayList(); |
| 113 | + lazyList.add("lazy"); |
| 114 | + lazyList.add("very lazy"); |
| 115 | + List[] labelLists = { |
| 116 | + Arrays.asList("str1", "str2", "str3"), |
| 117 | + ImmutableList.of("string1", "string2"), |
| 118 | + new UnmodifiableLazyStringList(lazyList), |
| 119 | + new UnmodifiableLazyStringList(LazyStringArrayList.EMPTY) |
| 120 | + }; |
| 121 | + |
| 122 | + for (int i = 0; i < labelLists.length; i++) { |
| 123 | + String family = "family_" + i; |
| 124 | + ByteString col = ByteString.copyFromUtf8("col_" + i); |
| 125 | + long timestamp = 1000L * (i + 1); |
| 126 | + List<String> labels = labelLists[i]; |
| 127 | + ByteString value = ByteString.copyFromUtf8("value_" + i); |
| 128 | + RowCell cell = RowCell.create(family, col, timestamp, labels, value); |
| 129 | + RowCell deserialized = (RowCell) serializeDeserialize(cell); |
| 130 | + |
| 131 | + assertThat(cell.getFamily()).isEqualTo(deserialized.getFamily()); |
| 132 | + assertThat(cell.getQualifier()).isEqualTo(deserialized.getQualifier()); |
| 133 | + assertThat(cell.getTimestamp()).isEqualTo(deserialized.getTimestamp()); |
| 134 | + assertThat(cell.getLabels()).isEqualTo(deserialized.getLabels()); |
| 135 | + assertThat(cell.getValue()).isEqualTo(deserialized.getValue()); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private static Object serializeDeserialize(Object obj) |
| 140 | + throws IOException, ClassNotFoundException { |
| 141 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 142 | + try (ObjectOutputStream outStream = new ObjectOutputStream(bos)) { |
| 143 | + outStream.writeObject(obj); |
| 144 | + } |
| 145 | + |
| 146 | + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); |
| 147 | + try (ObjectInputStream inStream = new ObjectInputStream(bis)) { |
| 148 | + return inStream.readObject(); |
| 149 | + } |
| 150 | + } |
101 | 151 | }
|
0 commit comments