LiteTest.java revision fbaaef999ba563838ebd00874ed8a1c01fbf286d
1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc.  All rights reserved.
3// http://code.google.com/p/protobuf/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
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
14// distribution.
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.
18//
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.
30
31package com.google.protobuf;
32
33import com.google.protobuf.UnittestLite;
34import com.google.protobuf.UnittestLite.TestAllTypesLite;
35import com.google.protobuf.UnittestLite.TestAllExtensionsLite;
36import com.google.protobuf.UnittestLite.TestNestedExtensionLite;
37
38import junit.framework.TestCase;
39
40/**
41 * Test lite runtime.
42 *
43 * @author kenton@google.com Kenton Varda
44 */
45public class LiteTest extends TestCase {
46  public void setUp() throws Exception {
47    // Test that nested extensions are initialized correctly even if the outer
48    // class has not been accessed directly.  This was once a bug with lite
49    // messages.
50    //
51    // We put this in setUp() rather than in its own test method because we
52    // need to make sure it runs before any actual tests.
53    assertTrue(TestNestedExtensionLite.nestedExtension != null);
54  }
55
56  public void testLite() throws Exception {
57    // Since lite messages are a subset of regular messages, we can mostly
58    // assume that the functionality of lite messages is already thoroughly
59    // tested by the regular tests.  All this test really verifies is that
60    // a proto with optimize_for = LITE_RUNTIME compiles correctly when
61    // linked only against the lite library.  That is all tested at compile
62    // time, leaving not much to do in this method.  Let's just do some random
63    // stuff to make sure the lite message is actually here and usable.
64
65    TestAllTypesLite message =
66      TestAllTypesLite.newBuilder()
67                      .setOptionalInt32(123)
68                      .addRepeatedString("hello")
69                      .setOptionalNestedMessage(
70                          TestAllTypesLite.NestedMessage.newBuilder().setBb(7))
71                      .build();
72
73    ByteString data = message.toByteString();
74
75    TestAllTypesLite message2 = TestAllTypesLite.parseFrom(data);
76
77    assertEquals(123, message2.getOptionalInt32());
78    assertEquals(1, message2.getRepeatedStringCount());
79    assertEquals("hello", message2.getRepeatedString(0));
80    assertEquals(7, message2.getOptionalNestedMessage().getBb());
81  }
82
83  public void testLiteExtensions() throws Exception {
84    // TODO(kenton):  Unlike other features of the lite library, extensions are
85    //   implemented completely differently from the regular library.  We
86    //   need to test them more thoroughly, once they are fully-implemented.
87
88    TestAllExtensionsLite message =
89      TestAllExtensionsLite.newBuilder()
90        .setExtension(UnittestLite.optionalInt32ExtensionLite, 123)
91        .addExtension(UnittestLite.repeatedStringExtensionLite, "hello")
92        .setExtension(UnittestLite.optionalNestedEnumExtensionLite,
93            TestAllTypesLite.NestedEnum.BAZ)
94        .setExtension(UnittestLite.optionalNestedMessageExtensionLite,
95            TestAllTypesLite.NestedMessage.newBuilder().setBb(7).build())
96        .build();
97
98    // Test copying a message, since coping extensions actually does use a
99    // different code path between lite and regular libraries, and as of this
100    // writing, parsing hasn't been implemented yet.
101    TestAllExtensionsLite message2 = message.toBuilder().build();
102
103    assertEquals(123, (int) message2.getExtension(
104        UnittestLite.optionalInt32ExtensionLite));
105    assertEquals(1, message2.getExtensionCount(
106        UnittestLite.repeatedStringExtensionLite));
107    assertEquals("hello", message2.getExtension(
108        UnittestLite.repeatedStringExtensionLite, 0));
109    assertEquals(TestAllTypesLite.NestedEnum.BAZ, message2.getExtension(
110        UnittestLite.optionalNestedEnumExtensionLite));
111    assertEquals(7, message2.getExtension(
112        UnittestLite.optionalNestedMessageExtensionLite).getBb());
113  }
114}
115