Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: third_party/protobuf/java/core/src/test/java/com/google/protobuf/LiteTest.java

Issue 2590803003: Revert "third_party/protobuf: Update to HEAD (83d681ee2c)" (Closed)
Patch Set: Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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;
38 import com.google.protobuf.UnittestLite.ForeignEnumLite; 39 import com.google.protobuf.UnittestLite.ForeignEnumLite;
39 import com.google.protobuf.UnittestLite.ForeignMessageLite; 40 import com.google.protobuf.UnittestLite.ForeignMessageLite;
40 import com.google.protobuf.UnittestLite.TestAllExtensionsLite; 41 import com.google.protobuf.UnittestLite.TestAllExtensionsLite;
41 import com.google.protobuf.UnittestLite.TestAllTypesLite; 42 import com.google.protobuf.UnittestLite.TestAllTypesLite;
42 import com.google.protobuf.UnittestLite.TestAllTypesLite.NestedEnum; 43 import com.google.protobuf.UnittestLite.TestAllTypesLite.NestedEnum;
43 import com.google.protobuf.UnittestLite.TestAllTypesLite.NestedMessage; 44 import com.google.protobuf.UnittestLite.TestAllTypesLite.NestedMessage;
44 import com.google.protobuf.UnittestLite.TestAllTypesLite.OneofFieldCase; 45 import com.google.protobuf.UnittestLite.TestAllTypesLite.OneofFieldCase;
45 import com.google.protobuf.UnittestLite.TestAllTypesLite.OptionalGroup; 46 import com.google.protobuf.UnittestLite.TestAllTypesLite.OptionalGroup;
46 import com.google.protobuf.UnittestLite.TestAllTypesLite.RepeatedGroup; 47 import com.google.protobuf.UnittestLite.TestAllTypesLite.RepeatedGroup;
47 import com.google.protobuf.UnittestLite.TestAllTypesLiteOrBuilder; 48 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
55 import junit.framework.TestCase; 56 import junit.framework.TestCase;
56 57
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
57 /** 64 /**
58 * Test lite runtime. 65 * Test lite runtime.
59 * 66 *
60 * @author kenton@google.com Kenton Varda 67 * @author kenton@google.com Kenton Varda
61 */ 68 */
62 public class LiteTest extends TestCase { 69 public class LiteTest extends TestCase {
63 @Override 70 @Override
64 public void setUp() throws Exception { 71 public void setUp() throws Exception {
65 // Test that nested extensions are initialized correctly even if the outer 72 // Test that nested extensions are initialized correctly even if the outer
66 // class has not been accessed directly. This was once a bug with lite 73 // 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
125 assertEquals(1, message2.getExtension( 132 assertEquals(1, message2.getExtension(
126 UnittestLite.repeatedStringExtensionLite).size()); 133 UnittestLite.repeatedStringExtensionLite).size());
127 assertEquals("hello", message2.getExtension( 134 assertEquals("hello", message2.getExtension(
128 UnittestLite.repeatedStringExtensionLite, 0)); 135 UnittestLite.repeatedStringExtensionLite, 0));
129 assertEquals(TestAllTypesLite.NestedEnum.BAZ, message2.getExtension( 136 assertEquals(TestAllTypesLite.NestedEnum.BAZ, message2.getExtension(
130 UnittestLite.optionalNestedEnumExtensionLite)); 137 UnittestLite.optionalNestedEnumExtensionLite));
131 assertEquals(7, message2.getExtension( 138 assertEquals(7, message2.getExtension(
132 UnittestLite.optionalNestedMessageExtensionLite).getBb()); 139 UnittestLite.optionalNestedMessageExtensionLite).getBb());
133 } 140 }
134 141
142
135 public void testClone() { 143 public void testClone() {
136 TestAllTypesLite.Builder expected = TestAllTypesLite.newBuilder() 144 TestAllTypesLite.Builder expected = TestAllTypesLite.newBuilder()
137 .setOptionalInt32(123); 145 .setOptionalInt32(123);
138 assertEquals( 146 assertEquals(
139 expected.getOptionalInt32(), expected.clone().getOptionalInt32()); 147 expected.getOptionalInt32(), expected.clone().getOptionalInt32());
140 148
141 TestAllExtensionsLite.Builder expected2 = TestAllExtensionsLite.newBuilder() 149 TestAllExtensionsLite.Builder expected2 = TestAllExtensionsLite.newBuilder()
142 .setExtension(UnittestLite.optionalInt32ExtensionLite, 123); 150 .setExtension(UnittestLite.optionalInt32ExtensionLite, 123);
143 assertEquals( 151 assertEquals(
144 expected2.getExtension(UnittestLite.optionalInt32ExtensionLite), 152 expected2.getExtension(UnittestLite.optionalInt32ExtensionLite),
(...skipping 1273 matching lines...) Expand 10 before | Expand all | Expand 10 after
1418 UnittestLite.optionalInt32ExtensionLite)); 1426 UnittestLite.optionalInt32ExtensionLite));
1419 assertEquals( 1427 assertEquals(
1420 11, (int) extendableMessage.getExtension( 1428 11, (int) extendableMessage.getExtension(
1421 UnittestLite.optionalFixed32ExtensionLite)); 1429 UnittestLite.optionalFixed32ExtensionLite));
1422 } 1430 }
1423 1431
1424 public void testToStringDefaultInstance() throws Exception { 1432 public void testToStringDefaultInstance() throws Exception {
1425 assertToStringEquals("", TestAllTypesLite.getDefaultInstance()); 1433 assertToStringEquals("", TestAllTypesLite.getDefaultInstance());
1426 } 1434 }
1427 1435
1428 public void testToStringScalarFieldsSuffixedWithList() throws Exception {
1429 assertToStringEquals("deceptively_named_list: 7", TestAllTypesLite.newBuilde r()
1430 .setDeceptivelyNamedList(7)
1431 .build());
1432 }
1433
1434 public void testToStringPrimitives() throws Exception { 1436 public void testToStringPrimitives() throws Exception {
1435 TestAllTypesLite proto = TestAllTypesLite.newBuilder() 1437 TestAllTypesLite proto = TestAllTypesLite.newBuilder()
1436 .setOptionalInt32(1) 1438 .setOptionalInt32(1)
1437 .setOptionalInt64(9223372036854775807L) 1439 .setOptionalInt64(9223372036854775807L)
1438 .build(); 1440 .build();
1439 assertToStringEquals("optional_int32: 1\noptional_int64: 9223372036854775807 ", proto); 1441 assertToStringEquals("optional_int32: 1\noptional_int64: 9223372036854775807 ", proto);
1440 1442
1441 proto = TestAllTypesLite.newBuilder() 1443 proto = TestAllTypesLite.newBuilder()
1442 .setOptionalBool(true) 1444 .setOptionalBool(true)
1443 .setOptionalNestedEnum(TestAllTypesLite.NestedEnum.BAZ) 1445 .setOptionalNestedEnum(TestAllTypesLite.NestedEnum.BAZ)
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
1621 } 1623 }
1622 1624
1623 public void testMergeFromStream_invalidBytes() throws Exception { 1625 public void testMergeFromStream_invalidBytes() throws Exception {
1624 TestAllTypesLite.Builder builder = TestAllTypesLite.newBuilder() 1626 TestAllTypesLite.Builder builder = TestAllTypesLite.newBuilder()
1625 .setDefaultBool(true); 1627 .setDefaultBool(true);
1626 try { 1628 try {
1627 builder.mergeFrom(CodedInputStream.newInstance("Invalid bytes".getBytes(In ternal.UTF_8))); 1629 builder.mergeFrom(CodedInputStream.newInstance("Invalid bytes".getBytes(In ternal.UTF_8)));
1628 fail(); 1630 fail();
1629 } catch (InvalidProtocolBufferException expected) {} 1631 } catch (InvalidProtocolBufferException expected) {}
1630 } 1632 }
1631 1633
1632 public void testMergeFrom_sanity() throws Exception { 1634 public void testMergeFrom_sanity() throws Exception {
1633 TestAllTypesLite one = TestUtilLite.getAllLiteSetBuilder().build(); 1635 TestAllTypesLite one = TestUtilLite.getAllLiteSetBuilder().build();
1634 byte[] bytes = one.toByteArray(); 1636 byte[] bytes = one.toByteArray();
1635 TestAllTypesLite two = TestAllTypesLite.parseFrom(bytes); 1637 TestAllTypesLite two = TestAllTypesLite.parseFrom(bytes);
1636 1638
1637 one = one.toBuilder().mergeFrom(one).build(); 1639 one = one.toBuilder().mergeFrom(one).build();
1638 two = two.toBuilder().mergeFrom(bytes).build(); 1640 two = two.toBuilder().mergeFrom(bytes).build();
1639 assertEquals(one, two); 1641 assertEquals(one, two);
1640 assertEquals(two, one); 1642 assertEquals(two, one);
1641 assertEquals(one.hashCode(), two.hashCode()); 1643 assertEquals(one.hashCode(), two.hashCode());
1642 } 1644 }
1643 1645
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
1656 public void testEquals_notEqual() throws Exception { 1646 public void testEquals_notEqual() throws Exception {
1657 TestAllTypesLite one = TestUtilLite.getAllLiteSetBuilder().build(); 1647 TestAllTypesLite one = TestUtilLite.getAllLiteSetBuilder().build();
1658 byte[] bytes = one.toByteArray(); 1648 byte[] bytes = one.toByteArray();
1659 TestAllTypesLite two = one.toBuilder().mergeFrom(one).mergeFrom(bytes).build (); 1649 TestAllTypesLite two = one.toBuilder().mergeFrom(one).mergeFrom(bytes).build ();
1660 1650
1661 assertFalse(one.equals(two)); 1651 assertFalse(one.equals(two));
1662 assertFalse(two.equals(one)); 1652 assertFalse(two.equals(one));
1663 1653
1664 assertFalse(one.equals(TestAllTypesLite.getDefaultInstance())); 1654 assertFalse(one.equals(TestAllTypesLite.getDefaultInstance()));
1665 assertFalse(TestAllTypesLite.getDefaultInstance().equals(one)); 1655 assertFalse(TestAllTypesLite.getDefaultInstance().equals(one));
(...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after
2205 .setExtension(Bar.fooExt, Bar.newBuilder() 2195 .setExtension(Bar.fooExt, Bar.newBuilder()
2206 .setName("name") 2196 .setName("name")
2207 .build()) 2197 .build())
2208 .build(); 2198 .build();
2209 2199
2210 Foo fooWithValueAndUnknownFields = Foo.parseFrom(fooWithValueAndExtension.to ByteArray()); 2200 Foo fooWithValueAndUnknownFields = Foo.parseFrom(fooWithValueAndExtension.to ByteArray());
2211 2201
2212 assertEqualsAndHashCodeAreFalse(fooWithOnlyValue, fooWithValueAndUnknownFiel ds); 2202 assertEqualsAndHashCodeAreFalse(fooWithOnlyValue, fooWithValueAndUnknownFiel ds);
2213 assertEqualsAndHashCodeAreFalse(fooWithValueAndExtension, fooWithValueAndUnk nownFields); 2203 assertEqualsAndHashCodeAreFalse(fooWithValueAndExtension, fooWithValueAndUnk nownFields);
2214 } 2204 }
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 }
2230 2205
2231 // Test to ensure we avoid a class cast exception with oneofs. 2206 // Test to ensure we avoid a class cast exception with oneofs.
2232 public void testEquals_oneOfMessages() { 2207 public void testEquals_oneOfMessages() {
2233 TestAllTypesLite mine = TestAllTypesLite.newBuilder() 2208 TestAllTypesLite mine = TestAllTypesLite.newBuilder()
2234 .setOneofString("Hello") 2209 .setOneofString("Hello")
2235 .build(); 2210 .build();
2236 2211
2237 TestAllTypesLite other = TestAllTypesLite.newBuilder() 2212 TestAllTypesLite other = TestAllTypesLite.newBuilder()
2238 .setOneofNestedMessage(NestedMessage.getDefaultInstance()) 2213 .setOneofNestedMessage(NestedMessage.getDefaultInstance())
2239 .build(); 2214 .build();
2240 2215
2241 assertFalse(mine.equals(other)); 2216 assertFalse(mine.equals(other));
2242 assertFalse(other.equals(mine)); 2217 assertFalse(other.equals(mine));
2243 } 2218 }
2244 2219
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
2264 private void assertEqualsAndHashCodeAreFalse(Object o1, Object o2) { 2220 private void assertEqualsAndHashCodeAreFalse(Object o1, Object o2) {
2265 assertFalse(o1.equals(o2)); 2221 assertFalse(o1.equals(o2));
2266 assertFalse(o1.hashCode() == o2.hashCode()); 2222 assertFalse(o1.hashCode() == o2.hashCode());
2267 } 2223 }
2268 2224
2269 public void testRecursiveHashcode() { 2225 public void testRecursiveHashcode() {
2270 // This tests that we don't infinite loop. 2226 // This tests that we don't infinite loop.
2271 TestRecursiveOneof.getDefaultInstance().hashCode(); 2227 TestRecursiveOneof.getDefaultInstance().hashCode();
2272 } 2228 }
2273 } 2229 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698