PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/protostuff-me/src/test/java/com/dyuproject/protostuff/me/DelimiterTest.java

http://protostuff.googlecode.com/
Java | 263 lines | 162 code | 56 blank | 45 comment | 0 complexity | ad08a00d758ee80446663dd36d4606da MD5 | raw file
Possible License(s): Apache-2.0
  1. //========================================================================
  2. //Copyright 2012 David Yu
  3. //------------------------------------------------------------------------
  4. //Licensed under the Apache License, Version 2.0 (the "License");
  5. //you may not use this file except in compliance with the License.
  6. //You may obtain a copy of the License at
  7. //http://www.apache.org/licenses/LICENSE-2.0
  8. //Unless required by applicable law or agreed to in writing, software
  9. //distributed under the License is distributed on an "AS IS" BASIS,
  10. //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. //See the License for the specific language governing permissions and
  12. //limitations under the License.
  13. //========================================================================
  14. package com.dyuproject.protostuff.me;
  15. import java.io.ByteArrayInputStream;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import java.util.Arrays;
  21. import com.dyuproject.protostuff.me.Foo.EnumSample;
  22. /**
  23. * Test writing/reading to/from streams using writeDelimitedTo, mergeDelimitedFrom,
  24. * optWriteDelimitedTo and optMergeDelimitedFrom.
  25. *
  26. * @author David Yu
  27. * @created Aug 29, 2012
  28. */
  29. public abstract class DelimiterTest extends AbstractTest
  30. {
  31. /**
  32. * Serializes the {@code message} (delimited) into
  33. * an {@link OutputStream} via {@link DeferredOutput} using the given schema.
  34. */
  35. protected abstract int writeDelimitedTo(OutputStream out, Object message,
  36. Schema schema, LinkedBuffer buffer) throws IOException;
  37. /**
  38. * Deserializes from the byte array and data is merged/saved to the message.
  39. */
  40. protected abstract void mergeDelimitedFrom(InputStream in, Object message,
  41. Schema schema, LinkedBuffer buffer) throws IOException;
  42. /**
  43. * Serializes the {@code message} (delimited) into
  44. * an {@link OutputStream} via {@link DeferredOutput} using the given schema.
  45. */
  46. protected abstract int optWriteDelimitedTo(OutputStream out, Object message,
  47. Schema schema, LinkedBuffer buffer) throws IOException;
  48. /**
  49. * Deserializes from the byte array and data is merged/saved to the message.
  50. */
  51. protected abstract boolean optMergeDelimitedFrom(InputStream in, Object message,
  52. Schema schema, LinkedBuffer buffer) throws IOException;
  53. void verifyOptData(byte[] optData, Object message, Schema schema,
  54. LinkedBuffer buffer) throws IOException
  55. {
  56. ByteArrayOutputStream out = new ByteArrayOutputStream();
  57. int size = writeDelimitedTo(out, message, schema, buf());
  58. int delimSize = ProtobufOutput.computeRawVarint32Size(size);
  59. byte[] data = out.toByteArray();
  60. int expectedSize = size + delimSize;
  61. assertEquals(expectedSize, data.length);
  62. // compare both outputs
  63. assertEquals(optData.length, data.length);
  64. /*StringBuilder s1 = new StringBuilder();
  65. for(int b : optData)
  66. s1.append(b).append(' ');
  67. StringBuilder s2 = new StringBuilder();
  68. for(int b : data)
  69. s2.append(b).append(' ');
  70. assertEquals(s1.toString(), s2.toString());*/
  71. assertTrue(Arrays.equals(optData, data));
  72. }
  73. public void testFoo() throws Exception
  74. {
  75. Schema schema = Foo.getSchema();
  76. Foo message = SerializableObjects.foo;
  77. ByteArrayOutputStream out = new ByteArrayOutputStream();
  78. int size = optWriteDelimitedTo(out, message, schema, buf());
  79. int delimSize = ProtobufOutput.computeRawVarint32Size(size);
  80. byte[] data = out.toByteArray();
  81. int expectedSize = size + delimSize;
  82. assertEquals(expectedSize, data.length);
  83. verifyOptData(data, message, schema, buf());
  84. ByteArrayInputStream in = new ByteArrayInputStream(data);
  85. Foo parsedMessage = new Foo();
  86. boolean merged = optMergeDelimitedFrom(in, parsedMessage, schema, buf(512));
  87. assertTrue(merged);
  88. assertEquals(message, parsedMessage);
  89. }
  90. public void testFooEmpty() throws Exception
  91. {
  92. Schema schema = Foo.getSchema();
  93. Foo message = new Foo();
  94. ByteArrayOutputStream out = new ByteArrayOutputStream();
  95. int size = optWriteDelimitedTo(out, message, schema, buf());
  96. int delimSize = ProtobufOutput.computeRawVarint32Size(size);
  97. byte[] data = out.toByteArray();
  98. int expectedSize = size + delimSize;
  99. assertEquals(expectedSize, data.length);
  100. verifyOptData(data, message, schema, buf());
  101. // empty
  102. assertEquals(size, 0);
  103. assertEquals(delimSize, 1);
  104. ByteArrayInputStream in = new ByteArrayInputStream(data);
  105. Foo parsedMessage = new Foo();
  106. boolean merged = optMergeDelimitedFrom(in, parsedMessage, schema, buf());
  107. assertTrue(merged);
  108. assertEquals(message, parsedMessage);
  109. }
  110. public void testFooTooLarge() throws Exception
  111. {
  112. Schema schema = Foo.getSchema();
  113. Foo message = SerializableObjects.newFoo(
  114. new Integer[]{90210,-90210, 0, 128},
  115. new String[]{"ab", "cd"},
  116. new Bar[]{SerializableObjects.bar, SerializableObjects.negativeBar},
  117. new int[]{EnumSample.TYPE0, EnumSample.TYPE2},
  118. new ByteString[]{ByteString.copyFromUtf8("ef"), ByteString.copyFromUtf8("gh")},
  119. new Boolean[]{true, false},
  120. new Float[]{1234.4321f, -1234.4321f, 0f},
  121. new Double[]{12345678.87654321d, -12345678.87654321d, 0d},
  122. new Long[]{7060504030201l, -7060504030201l, 0l});
  123. ByteArrayOutputStream out = new ByteArrayOutputStream();
  124. int size = optWriteDelimitedTo(out, message, schema, buf());
  125. int delimSize = ProtobufOutput.computeRawVarint32Size(size);
  126. byte[] data = out.toByteArray();
  127. int expectedSize = size + delimSize; // 258
  128. assertEquals(expectedSize, data.length);
  129. verifyOptData(data, message, schema, buf());
  130. ByteArrayInputStream in = new ByteArrayInputStream(data);
  131. Foo parsedMessage = new Foo();
  132. boolean merged = optMergeDelimitedFrom(in, parsedMessage, schema, buf(256));
  133. assertFalse(merged);
  134. }
  135. public void testBar() throws Exception
  136. {
  137. Schema schema = Bar.getSchema();
  138. Bar message = SerializableObjects.bar;
  139. ByteArrayOutputStream out = new ByteArrayOutputStream();
  140. int size = optWriteDelimitedTo(out, message, schema, buf());
  141. int delimSize = ProtobufOutput.computeRawVarint32Size(size);
  142. byte[] data = out.toByteArray();
  143. int expectedSize = size + delimSize;
  144. assertEquals(expectedSize, data.length);
  145. verifyOptData(data, message, schema, buf());
  146. ByteArrayInputStream in = new ByteArrayInputStream(data);
  147. Bar parsedMessage = new Bar();
  148. boolean merged = optMergeDelimitedFrom(in, parsedMessage, schema, buf());
  149. assertTrue(merged);
  150. assertEquals(message, parsedMessage);
  151. }
  152. public void testBaz() throws Exception
  153. {
  154. Schema schema = Baz.getSchema();
  155. Baz message = SerializableObjects.baz;
  156. ByteArrayOutputStream out = new ByteArrayOutputStream();
  157. int size = optWriteDelimitedTo(out, message, schema, buf());
  158. int delimSize = ProtobufOutput.computeRawVarint32Size(size);
  159. byte[] data = out.toByteArray();
  160. int expectedSize = size + delimSize;
  161. assertEquals(expectedSize, data.length);
  162. verifyOptData(data, message, schema, buf());
  163. ByteArrayInputStream in = new ByteArrayInputStream(data);
  164. Baz parsedMessage = new Baz();
  165. boolean merged = optMergeDelimitedFrom(in, parsedMessage, schema, buf());
  166. assertTrue(merged);
  167. assertEquals(message, parsedMessage);
  168. }
  169. public void testBarTooLarge2() throws Exception
  170. {
  171. Schema schema = Bar.getSchema();
  172. Bar message = new Bar();
  173. message.setSomeBytes(ByteString.wrap(
  174. new byte[StringSerializer.THREE_BYTE_LOWER_LIMIT-1]));
  175. ByteArrayOutputStream out = new ByteArrayOutputStream();
  176. int size = optWriteDelimitedTo(out, message, schema, buf());
  177. int delimSize = ProtobufOutput.computeRawVarint32Size(size);
  178. byte[] data = out.toByteArray();
  179. int expectedSize = size + delimSize;
  180. assertEquals(expectedSize, data.length);
  181. verifyOptData(data, message, schema, buf());
  182. ByteArrayInputStream in = new ByteArrayInputStream(data);
  183. Bar parsedMessage = new Bar();
  184. boolean merged = optMergeDelimitedFrom(in, parsedMessage, schema, buf(256));
  185. assertFalse(merged);
  186. }
  187. public void testBarTooLarge3() throws Exception
  188. {
  189. Schema schema = Bar.getSchema();
  190. Bar message = new Bar();
  191. message.setSomeBytes(ByteString.wrap(
  192. new byte[StringSerializer.FOUR_BYTE_LOWER_LIMIT-1]));
  193. ByteArrayOutputStream out = new ByteArrayOutputStream();
  194. int size = optWriteDelimitedTo(out, message, schema, buf());
  195. int delimSize = ProtobufOutput.computeRawVarint32Size(size);
  196. byte[] data = out.toByteArray();
  197. int expectedSize = size + delimSize;
  198. assertEquals(expectedSize, data.length);
  199. verifyOptData(data, message, schema, buf());
  200. ByteArrayInputStream in = new ByteArrayInputStream(data);
  201. Bar parsedMessage = new Bar();
  202. boolean merged = optMergeDelimitedFrom(in, parsedMessage, schema, buf(256));
  203. assertFalse(merged);
  204. }
  205. }