1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // https://developers.google.com/protocol-buffers/ 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 31 package com.google.protobuf; 32 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; 37 38 import junit.framework.TestCase; 39 40 import java.io.ByteArrayInputStream; 41 import java.io.ByteArrayOutputStream; 42 import java.io.ObjectInputStream; 43 import java.io.ObjectOutputStream; 44 45 /** 46 * Test lite runtime. 47 * 48 * @author kenton (at) google.com Kenton Varda 49 */ 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 54 // messages. 55 // 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); 59 } 60 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. 69 70 TestAllTypesLite message = 71 TestAllTypesLite.newBuilder() 72 .setOptionalInt32(123) 73 .addRepeatedString("hello") 74 .setOptionalNestedMessage( 75 TestAllTypesLite.NestedMessage.newBuilder().setBb(7)) 76 .build(); 77 78 ByteString data = message.toByteString(); 79 80 TestAllTypesLite message2 = TestAllTypesLite.parseFrom(data); 81 82 assertEquals(123, message2.getOptionalInt32()); 83 assertEquals(1, message2.getRepeatedStringCount()); 84 assertEquals("hello", message2.getRepeatedString(0)); 85 assertEquals(7, message2.getOptionalNestedMessage().getBb()); 86 } 87 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. 92 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()) 101 .build(); 102 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(); 107 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()); 120 } 121 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)) 130 .build(); 131 ObjectOutputStream out = new ObjectOutputStream(baos); 132 try { 133 out.writeObject(expected); 134 } finally { 135 out.close(); 136 } 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()); 147 } 148 } 149