OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 17 matching lines...) Expand all Loading... |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
31 package com.google.protobuf; | 31 package com.google.protobuf; |
32 | 32 |
33 import static java.util.Collections.emptyList; | 33 import static java.util.Collections.emptyList; |
34 import static java.util.Collections.singletonList; | 34 import static java.util.Collections.singletonList; |
35 | 35 |
36 import com.google.protobuf.UnittestImportLite.ImportEnumLite; | 36 import com.google.protobuf.UnittestImportLite.ImportEnumLite; |
37 import com.google.protobuf.UnittestImportPublicLite.PublicImportMessageLite; | 37 import com.google.protobuf.UnittestImportPublicLite.PublicImportMessageLite; |
38 import com.google.protobuf.UnittestLite; | |
39 import com.google.protobuf.UnittestLite.ForeignEnumLite; | 38 import com.google.protobuf.UnittestLite.ForeignEnumLite; |
40 import com.google.protobuf.UnittestLite.ForeignMessageLite; | 39 import com.google.protobuf.UnittestLite.ForeignMessageLite; |
41 import com.google.protobuf.UnittestLite.TestAllExtensionsLite; | 40 import com.google.protobuf.UnittestLite.TestAllExtensionsLite; |
42 import com.google.protobuf.UnittestLite.TestAllTypesLite; | 41 import com.google.protobuf.UnittestLite.TestAllTypesLite; |
43 import com.google.protobuf.UnittestLite.TestAllTypesLite.NestedEnum; | 42 import com.google.protobuf.UnittestLite.TestAllTypesLite.NestedEnum; |
44 import com.google.protobuf.UnittestLite.TestAllTypesLite.NestedMessage; | 43 import com.google.protobuf.UnittestLite.TestAllTypesLite.NestedMessage; |
45 import com.google.protobuf.UnittestLite.TestAllTypesLite.OneofFieldCase; | 44 import com.google.protobuf.UnittestLite.TestAllTypesLite.OneofFieldCase; |
46 import com.google.protobuf.UnittestLite.TestAllTypesLite.OptionalGroup; | 45 import com.google.protobuf.UnittestLite.TestAllTypesLite.OptionalGroup; |
47 import com.google.protobuf.UnittestLite.TestAllTypesLite.RepeatedGroup; | 46 import com.google.protobuf.UnittestLite.TestAllTypesLite.RepeatedGroup; |
48 import com.google.protobuf.UnittestLite.TestAllTypesLiteOrBuilder; | 47 import com.google.protobuf.UnittestLite.TestAllTypesLiteOrBuilder; |
| 48 import com.google.protobuf.UnittestLite.TestHugeFieldNumbersLite; |
49 import com.google.protobuf.UnittestLite.TestNestedExtensionLite; | 49 import com.google.protobuf.UnittestLite.TestNestedExtensionLite; |
50 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Bar; | 50 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Bar; |
51 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.BarPrime; | 51 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.BarPrime; |
52 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Foo; | 52 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Foo; |
53 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.TestOneofEquals; | 53 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.TestOneofEquals; |
54 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.TestRecursiveOne
of; | 54 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.TestRecursiveOne
of; |
55 | |
56 import junit.framework.TestCase; | 55 import junit.framework.TestCase; |
57 | 56 |
58 import java.io.ByteArrayInputStream; | |
59 import java.io.ByteArrayOutputStream; | |
60 import java.io.NotSerializableException; | |
61 import java.io.ObjectInputStream; | |
62 import java.io.ObjectOutputStream; | |
63 | |
64 /** | 57 /** |
65 * Test lite runtime. | 58 * Test lite runtime. |
66 * | 59 * |
67 * @author kenton@google.com Kenton Varda | 60 * @author kenton@google.com Kenton Varda |
68 */ | 61 */ |
69 public class LiteTest extends TestCase { | 62 public class LiteTest extends TestCase { |
70 @Override | 63 @Override |
71 public void setUp() throws Exception { | 64 public void setUp() throws Exception { |
72 // Test that nested extensions are initialized correctly even if the outer | 65 // Test that nested extensions are initialized correctly even if the outer |
73 // class has not been accessed directly. This was once a bug with lite | 66 // class has not been accessed directly. This was once a bug with lite |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 assertEquals(1, message2.getExtension( | 125 assertEquals(1, message2.getExtension( |
133 UnittestLite.repeatedStringExtensionLite).size()); | 126 UnittestLite.repeatedStringExtensionLite).size()); |
134 assertEquals("hello", message2.getExtension( | 127 assertEquals("hello", message2.getExtension( |
135 UnittestLite.repeatedStringExtensionLite, 0)); | 128 UnittestLite.repeatedStringExtensionLite, 0)); |
136 assertEquals(TestAllTypesLite.NestedEnum.BAZ, message2.getExtension( | 129 assertEquals(TestAllTypesLite.NestedEnum.BAZ, message2.getExtension( |
137 UnittestLite.optionalNestedEnumExtensionLite)); | 130 UnittestLite.optionalNestedEnumExtensionLite)); |
138 assertEquals(7, message2.getExtension( | 131 assertEquals(7, message2.getExtension( |
139 UnittestLite.optionalNestedMessageExtensionLite).getBb()); | 132 UnittestLite.optionalNestedMessageExtensionLite).getBb()); |
140 } | 133 } |
141 | 134 |
142 | |
143 public void testClone() { | 135 public void testClone() { |
144 TestAllTypesLite.Builder expected = TestAllTypesLite.newBuilder() | 136 TestAllTypesLite.Builder expected = TestAllTypesLite.newBuilder() |
145 .setOptionalInt32(123); | 137 .setOptionalInt32(123); |
146 assertEquals( | 138 assertEquals( |
147 expected.getOptionalInt32(), expected.clone().getOptionalInt32()); | 139 expected.getOptionalInt32(), expected.clone().getOptionalInt32()); |
148 | 140 |
149 TestAllExtensionsLite.Builder expected2 = TestAllExtensionsLite.newBuilder() | 141 TestAllExtensionsLite.Builder expected2 = TestAllExtensionsLite.newBuilder() |
150 .setExtension(UnittestLite.optionalInt32ExtensionLite, 123); | 142 .setExtension(UnittestLite.optionalInt32ExtensionLite, 123); |
151 assertEquals( | 143 assertEquals( |
152 expected2.getExtension(UnittestLite.optionalInt32ExtensionLite), | 144 expected2.getExtension(UnittestLite.optionalInt32ExtensionLite), |
(...skipping 1273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1426 UnittestLite.optionalInt32ExtensionLite)); | 1418 UnittestLite.optionalInt32ExtensionLite)); |
1427 assertEquals( | 1419 assertEquals( |
1428 11, (int) extendableMessage.getExtension( | 1420 11, (int) extendableMessage.getExtension( |
1429 UnittestLite.optionalFixed32ExtensionLite)); | 1421 UnittestLite.optionalFixed32ExtensionLite)); |
1430 } | 1422 } |
1431 | 1423 |
1432 public void testToStringDefaultInstance() throws Exception { | 1424 public void testToStringDefaultInstance() throws Exception { |
1433 assertToStringEquals("", TestAllTypesLite.getDefaultInstance()); | 1425 assertToStringEquals("", TestAllTypesLite.getDefaultInstance()); |
1434 } | 1426 } |
1435 | 1427 |
| 1428 public void testToStringScalarFieldsSuffixedWithList() throws Exception { |
| 1429 assertToStringEquals("deceptively_named_list: 7", TestAllTypesLite.newBuilde
r() |
| 1430 .setDeceptivelyNamedList(7) |
| 1431 .build()); |
| 1432 } |
| 1433 |
1436 public void testToStringPrimitives() throws Exception { | 1434 public void testToStringPrimitives() throws Exception { |
1437 TestAllTypesLite proto = TestAllTypesLite.newBuilder() | 1435 TestAllTypesLite proto = TestAllTypesLite.newBuilder() |
1438 .setOptionalInt32(1) | 1436 .setOptionalInt32(1) |
1439 .setOptionalInt64(9223372036854775807L) | 1437 .setOptionalInt64(9223372036854775807L) |
1440 .build(); | 1438 .build(); |
1441 assertToStringEquals("optional_int32: 1\noptional_int64: 9223372036854775807
", proto); | 1439 assertToStringEquals("optional_int32: 1\noptional_int64: 9223372036854775807
", proto); |
1442 | 1440 |
1443 proto = TestAllTypesLite.newBuilder() | 1441 proto = TestAllTypesLite.newBuilder() |
1444 .setOptionalBool(true) | 1442 .setOptionalBool(true) |
1445 .setOptionalNestedEnum(TestAllTypesLite.NestedEnum.BAZ) | 1443 .setOptionalNestedEnum(TestAllTypesLite.NestedEnum.BAZ) |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1623 } | 1621 } |
1624 | 1622 |
1625 public void testMergeFromStream_invalidBytes() throws Exception { | 1623 public void testMergeFromStream_invalidBytes() throws Exception { |
1626 TestAllTypesLite.Builder builder = TestAllTypesLite.newBuilder() | 1624 TestAllTypesLite.Builder builder = TestAllTypesLite.newBuilder() |
1627 .setDefaultBool(true); | 1625 .setDefaultBool(true); |
1628 try { | 1626 try { |
1629 builder.mergeFrom(CodedInputStream.newInstance("Invalid bytes".getBytes(In
ternal.UTF_8))); | 1627 builder.mergeFrom(CodedInputStream.newInstance("Invalid bytes".getBytes(In
ternal.UTF_8))); |
1630 fail(); | 1628 fail(); |
1631 } catch (InvalidProtocolBufferException expected) {} | 1629 } catch (InvalidProtocolBufferException expected) {} |
1632 } | 1630 } |
1633 | 1631 |
1634 public void testMergeFrom_sanity() throws Exception { | 1632 public void testMergeFrom_sanity() throws Exception { |
1635 TestAllTypesLite one = TestUtilLite.getAllLiteSetBuilder().build(); | 1633 TestAllTypesLite one = TestUtilLite.getAllLiteSetBuilder().build(); |
1636 byte[] bytes = one.toByteArray(); | 1634 byte[] bytes = one.toByteArray(); |
1637 TestAllTypesLite two = TestAllTypesLite.parseFrom(bytes); | 1635 TestAllTypesLite two = TestAllTypesLite.parseFrom(bytes); |
1638 | 1636 |
1639 one = one.toBuilder().mergeFrom(one).build(); | 1637 one = one.toBuilder().mergeFrom(one).build(); |
1640 two = two.toBuilder().mergeFrom(bytes).build(); | 1638 two = two.toBuilder().mergeFrom(bytes).build(); |
1641 assertEquals(one, two); | 1639 assertEquals(one, two); |
1642 assertEquals(two, one); | 1640 assertEquals(two, one); |
1643 assertEquals(one.hashCode(), two.hashCode()); | 1641 assertEquals(one.hashCode(), two.hashCode()); |
1644 } | 1642 } |
1645 | 1643 |
| 1644 public void testMergeFromNoLazyFieldSharing() throws Exception { |
| 1645 TestAllTypesLite.Builder sourceBuilder = TestAllTypesLite.newBuilder().setOp
tionalLazyMessage( |
| 1646 TestAllTypesLite.NestedMessage.newBuilder().setBb(1)); |
| 1647 TestAllTypesLite.Builder targetBuilder = |
| 1648 TestAllTypesLite.newBuilder().mergeFrom(sourceBuilder.build()); |
| 1649 assertEquals(1, sourceBuilder.getOptionalLazyMessage().getBb()); |
| 1650 // now change the sourceBuilder, and target value shouldn't be affected. |
| 1651 sourceBuilder.setOptionalLazyMessage( |
| 1652 TestAllTypesLite.NestedMessage.newBuilder().setBb(2)); |
| 1653 assertEquals(1, targetBuilder.getOptionalLazyMessage().getBb()); |
| 1654 } |
| 1655 |
1646 public void testEquals_notEqual() throws Exception { | 1656 public void testEquals_notEqual() throws Exception { |
1647 TestAllTypesLite one = TestUtilLite.getAllLiteSetBuilder().build(); | 1657 TestAllTypesLite one = TestUtilLite.getAllLiteSetBuilder().build(); |
1648 byte[] bytes = one.toByteArray(); | 1658 byte[] bytes = one.toByteArray(); |
1649 TestAllTypesLite two = one.toBuilder().mergeFrom(one).mergeFrom(bytes).build
(); | 1659 TestAllTypesLite two = one.toBuilder().mergeFrom(one).mergeFrom(bytes).build
(); |
1650 | 1660 |
1651 assertFalse(one.equals(two)); | 1661 assertFalse(one.equals(two)); |
1652 assertFalse(two.equals(one)); | 1662 assertFalse(two.equals(one)); |
1653 | 1663 |
1654 assertFalse(one.equals(TestAllTypesLite.getDefaultInstance())); | 1664 assertFalse(one.equals(TestAllTypesLite.getDefaultInstance())); |
1655 assertFalse(TestAllTypesLite.getDefaultInstance().equals(one)); | 1665 assertFalse(TestAllTypesLite.getDefaultInstance().equals(one)); |
(...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2195 .setExtension(Bar.fooExt, Bar.newBuilder() | 2205 .setExtension(Bar.fooExt, Bar.newBuilder() |
2196 .setName("name") | 2206 .setName("name") |
2197 .build()) | 2207 .build()) |
2198 .build(); | 2208 .build(); |
2199 | 2209 |
2200 Foo fooWithValueAndUnknownFields = Foo.parseFrom(fooWithValueAndExtension.to
ByteArray()); | 2210 Foo fooWithValueAndUnknownFields = Foo.parseFrom(fooWithValueAndExtension.to
ByteArray()); |
2201 | 2211 |
2202 assertEqualsAndHashCodeAreFalse(fooWithOnlyValue, fooWithValueAndUnknownFiel
ds); | 2212 assertEqualsAndHashCodeAreFalse(fooWithOnlyValue, fooWithValueAndUnknownFiel
ds); |
2203 assertEqualsAndHashCodeAreFalse(fooWithValueAndExtension, fooWithValueAndUnk
nownFields); | 2213 assertEqualsAndHashCodeAreFalse(fooWithValueAndExtension, fooWithValueAndUnk
nownFields); |
2204 } | 2214 } |
| 2215 |
| 2216 public void testEqualsAndHashCodeWithExtensions() throws InvalidProtocolBuffer
Exception { |
| 2217 Foo fooWithOnlyValue = Foo.newBuilder() |
| 2218 .setValue(1) |
| 2219 .build(); |
| 2220 |
| 2221 Foo fooWithValueAndExtension = fooWithOnlyValue.toBuilder() |
| 2222 .setValue(1) |
| 2223 .setExtension(Bar.fooExt, Bar.newBuilder() |
| 2224 .setName("name") |
| 2225 .build()) |
| 2226 .build(); |
| 2227 |
| 2228 assertEqualsAndHashCodeAreFalse(fooWithOnlyValue, fooWithValueAndExtension); |
| 2229 } |
2205 | 2230 |
2206 // Test to ensure we avoid a class cast exception with oneofs. | 2231 // Test to ensure we avoid a class cast exception with oneofs. |
2207 public void testEquals_oneOfMessages() { | 2232 public void testEquals_oneOfMessages() { |
2208 TestAllTypesLite mine = TestAllTypesLite.newBuilder() | 2233 TestAllTypesLite mine = TestAllTypesLite.newBuilder() |
2209 .setOneofString("Hello") | 2234 .setOneofString("Hello") |
2210 .build(); | 2235 .build(); |
2211 | 2236 |
2212 TestAllTypesLite other = TestAllTypesLite.newBuilder() | 2237 TestAllTypesLite other = TestAllTypesLite.newBuilder() |
2213 .setOneofNestedMessage(NestedMessage.getDefaultInstance()) | 2238 .setOneofNestedMessage(NestedMessage.getDefaultInstance()) |
2214 .build(); | 2239 .build(); |
2215 | 2240 |
2216 assertFalse(mine.equals(other)); | 2241 assertFalse(mine.equals(other)); |
2217 assertFalse(other.equals(mine)); | 2242 assertFalse(other.equals(mine)); |
2218 } | 2243 } |
2219 | 2244 |
| 2245 public void testHugeFieldNumbers() throws InvalidProtocolBufferException { |
| 2246 TestHugeFieldNumbersLite message = |
| 2247 TestHugeFieldNumbersLite.newBuilder() |
| 2248 .setOptionalInt32(1) |
| 2249 .addRepeatedInt32(2) |
| 2250 .setOptionalEnum(ForeignEnumLite.FOREIGN_LITE_FOO) |
| 2251 .setOptionalString("xyz") |
| 2252 .setOptionalMessage(ForeignMessageLite.newBuilder().setC(3).build()) |
| 2253 .build(); |
| 2254 |
| 2255 TestHugeFieldNumbersLite parsedMessage = |
| 2256 TestHugeFieldNumbersLite.parseFrom(message.toByteArray()); |
| 2257 assertEquals(1, parsedMessage.getOptionalInt32()); |
| 2258 assertEquals(2, parsedMessage.getRepeatedInt32(0)); |
| 2259 assertEquals(ForeignEnumLite.FOREIGN_LITE_FOO, parsedMessage.getOptionalEnum
()); |
| 2260 assertEquals("xyz", parsedMessage.getOptionalString()); |
| 2261 assertEquals(3, parsedMessage.getOptionalMessage().getC()); |
| 2262 } |
| 2263 |
2220 private void assertEqualsAndHashCodeAreFalse(Object o1, Object o2) { | 2264 private void assertEqualsAndHashCodeAreFalse(Object o1, Object o2) { |
2221 assertFalse(o1.equals(o2)); | 2265 assertFalse(o1.equals(o2)); |
2222 assertFalse(o1.hashCode() == o2.hashCode()); | 2266 assertFalse(o1.hashCode() == o2.hashCode()); |
2223 } | 2267 } |
2224 | 2268 |
2225 public void testRecursiveHashcode() { | 2269 public void testRecursiveHashcode() { |
2226 // This tests that we don't infinite loop. | 2270 // This tests that we don't infinite loop. |
2227 TestRecursiveOneof.getDefaultInstance().hashCode(); | 2271 TestRecursiveOneof.getDefaultInstance().hashCode(); |
2228 } | 2272 } |
2229 } | 2273 } |
OLD | NEW |