1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 package com.google.protobuf;
33 import com.google.protobuf.UnittestLite;
34 import com.google.protobuf.UnittestLite.TestAllTypesLite;
35 import com.google.protobuf.UnittestLite.TestAllExtensionsLite;
36 import com.google.protobuf.UnittestLite.TestNestedExtensionLite;
38 import junit.framework.TestCase;
40 import java.io.ByteArrayInputStream;
41 import java.io.ByteArrayOutputStream;
42 import java.io.ObjectInputStream;
43 import java.io.ObjectOutputStream;
48 * @author kenton@google.com Kenton Varda
50 public class LiteTest extends TestCase {
51 public void setUp() throws Exception {
52 // Test that nested extensions are initialized correctly even if the outer
53 // class has not been accessed directly. This was once a bug with lite
56 // We put this in setUp() rather than in its own test method because we
57 // need to make sure it runs before any actual tests.
58 assertTrue(TestNestedExtensionLite.nestedExtension != null);
61 public void testLite() throws Exception {
62 // Since lite messages are a subset of regular messages, we can mostly
63 // assume that the functionality of lite messages is already thoroughly
64 // tested by the regular tests. All this test really verifies is that
65 // a proto with optimize_for = LITE_RUNTIME compiles correctly when
66 // linked only against the lite library. That is all tested at compile
67 // time, leaving not much to do in this method. Let's just do some random
68 // stuff to make sure the lite message is actually here and usable.
70 TestAllTypesLite message =
71 TestAllTypesLite.newBuilder()
72 .setOptionalInt32(123)
73 .addRepeatedString("hello")
74 .setOptionalNestedMessage(
75 TestAllTypesLite.NestedMessage.newBuilder().setBb(7))
78 ByteString data = message.toByteString();
80 TestAllTypesLite message2 = TestAllTypesLite.parseFrom(data);
82 assertEquals(123, message2.getOptionalInt32());
83 assertEquals(1, message2.getRepeatedStringCount());
84 assertEquals("hello", message2.getRepeatedString(0));
85 assertEquals(7, message2.getOptionalNestedMessage().getBb());
88 public void testLiteExtensions() throws Exception {
89 // TODO(kenton): Unlike other features of the lite library, extensions are
90 // implemented completely differently from the regular library. We
91 // should probably test them more thoroughly.
93 TestAllExtensionsLite message =
94 TestAllExtensionsLite.newBuilder()
95 .setExtension(UnittestLite.optionalInt32ExtensionLite, 123)
96 .addExtension(UnittestLite.repeatedStringExtensionLite, "hello")
97 .setExtension(UnittestLite.optionalNestedEnumExtensionLite,
98 TestAllTypesLite.NestedEnum.BAZ)
99 .setExtension(UnittestLite.optionalNestedMessageExtensionLite,
100 TestAllTypesLite.NestedMessage.newBuilder().setBb(7).build())
103 // Test copying a message, since coping extensions actually does use a
104 // different code path between lite and regular libraries, and as of this
105 // writing, parsing hasn't been implemented yet.
106 TestAllExtensionsLite message2 = message.toBuilder().build();
108 assertEquals(123, (int) message2.getExtension(
109 UnittestLite.optionalInt32ExtensionLite));
110 assertEquals(1, message2.getExtensionCount(
111 UnittestLite.repeatedStringExtensionLite));
112 assertEquals(1, message2.getExtension(
113 UnittestLite.repeatedStringExtensionLite).size());
114 assertEquals("hello", message2.getExtension(
115 UnittestLite.repeatedStringExtensionLite, 0));
116 assertEquals(TestAllTypesLite.NestedEnum.BAZ, message2.getExtension(
117 UnittestLite.optionalNestedEnumExtensionLite));
118 assertEquals(7, message2.getExtension(
119 UnittestLite.optionalNestedMessageExtensionLite).getBb());
122 public void testSerialize() throws Exception {
123 ByteArrayOutputStream baos = new ByteArrayOutputStream();
124 TestAllTypesLite expected =
125 TestAllTypesLite.newBuilder()
126 .setOptionalInt32(123)
127 .addRepeatedString("hello")
128 .setOptionalNestedMessage(
129 TestAllTypesLite.NestedMessage.newBuilder().setBb(7))
131 ObjectOutputStream out = new ObjectOutputStream(baos);
133 out.writeObject(expected);
137 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
138 ObjectInputStream in = new ObjectInputStream(bais);
139 TestAllTypesLite actual = (TestAllTypesLite) in.readObject();
140 assertEquals(expected.getOptionalInt32(), actual.getOptionalInt32());
141 assertEquals(expected.getRepeatedStringCount(),
142 actual.getRepeatedStringCount());
143 assertEquals(expected.getRepeatedString(0),
144 actual.getRepeatedString(0));
145 assertEquals(expected.getOptionalNestedMessage().getBb(),
146 actual.getOptionalNestedMessage().getBb());