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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 import protobuf_unittest.UnittestProto.ForeignMessageOrBuilder; | 58 import protobuf_unittest.UnittestProto.ForeignMessageOrBuilder; |
59 import protobuf_unittest.UnittestProto.NestedTestAllTypes; | 59 import protobuf_unittest.UnittestProto.NestedTestAllTypes; |
60 import protobuf_unittest.UnittestProto.TestAllExtensions; | 60 import protobuf_unittest.UnittestProto.TestAllExtensions; |
61 import protobuf_unittest.UnittestProto.TestAllTypes; | 61 import protobuf_unittest.UnittestProto.TestAllTypes; |
62 import protobuf_unittest.UnittestProto.TestAllTypes.NestedMessage; | 62 import protobuf_unittest.UnittestProto.TestAllTypes.NestedMessage; |
63 import protobuf_unittest.UnittestProto.TestAllTypesOrBuilder; | 63 import protobuf_unittest.UnittestProto.TestAllTypesOrBuilder; |
64 import protobuf_unittest.UnittestProto.TestExtremeDefaultValues; | 64 import protobuf_unittest.UnittestProto.TestExtremeDefaultValues; |
65 import protobuf_unittest.UnittestProto.TestOneof2; | 65 import protobuf_unittest.UnittestProto.TestOneof2; |
66 import protobuf_unittest.UnittestProto.TestPackedTypes; | 66 import protobuf_unittest.UnittestProto.TestPackedTypes; |
67 import protobuf_unittest.UnittestProto.TestUnpackedTypes; | 67 import protobuf_unittest.UnittestProto.TestUnpackedTypes; |
| 68 |
| 69 import junit.framework.TestCase; |
| 70 |
68 import java.io.ByteArrayInputStream; | 71 import java.io.ByteArrayInputStream; |
69 import java.io.ByteArrayOutputStream; | 72 import java.io.ByteArrayOutputStream; |
70 import java.io.ObjectInputStream; | 73 import java.io.ObjectInputStream; |
71 import java.io.ObjectOutputStream; | 74 import java.io.ObjectOutputStream; |
72 import java.util.Arrays; | 75 import java.util.Arrays; |
73 import java.util.Collections; | 76 import java.util.Collections; |
74 import java.util.Iterator; | 77 import java.util.Iterator; |
75 import java.util.List; | 78 import java.util.List; |
76 import junit.framework.TestCase; | |
77 | 79 |
78 /** | 80 /** |
79 * Unit test for generated messages and generated code. See also | 81 * Unit test for generated messages and generated code. See also |
80 * {@link MessageTest}, which tests some generated message functionality. | 82 * {@link MessageTest}, which tests some generated message functionality. |
81 * | 83 * |
82 * @author kenton@google.com Kenton Varda | 84 * @author kenton@google.com Kenton Varda |
83 */ | 85 */ |
84 public class GeneratedMessageTest extends TestCase { | 86 public class GeneratedMessageTest extends TestCase { |
85 TestUtil.ReflectionTester reflectionTester = | 87 TestUtil.ReflectionTester reflectionTester = |
86 new TestUtil.ReflectionTester(TestAllTypes.getDescriptor(), null); | 88 new TestUtil.ReflectionTester(TestAllTypes.getDescriptor(), null); |
(...skipping 876 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
963 TestAllTypes.NestedMessage nestedMessage1 = | 965 TestAllTypes.NestedMessage nestedMessage1 = |
964 TestAllTypes.NestedMessage.newBuilder().build(); | 966 TestAllTypes.NestedMessage.newBuilder().build(); |
965 TestAllTypes.NestedMessage nestedMessage2 = | 967 TestAllTypes.NestedMessage nestedMessage2 = |
966 TestAllTypes.NestedMessage.newBuilder().build(); | 968 TestAllTypes.NestedMessage.newBuilder().build(); |
967 | 969 |
968 // Set all three flavors (enum, primitive, message and singular/repeated) | 970 // Set all three flavors (enum, primitive, message and singular/repeated) |
969 // and verify no invalidations fired | 971 // and verify no invalidations fired |
970 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); | 972 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); |
971 | 973 |
972 TestAllTypes.Builder builder = (TestAllTypes.Builder) | 974 TestAllTypes.Builder builder = (TestAllTypes.Builder) |
973 ((AbstractMessage) TestAllTypes.getDefaultInstance()). | 975 ((GeneratedMessage) TestAllTypes.getDefaultInstance()). |
974 newBuilderForType(mockParent); | 976 newBuilderForType(mockParent); |
975 builder.setOptionalInt32(1); | 977 builder.setOptionalInt32(1); |
976 builder.setOptionalNestedEnum(TestAllTypes.NestedEnum.BAR); | 978 builder.setOptionalNestedEnum(TestAllTypes.NestedEnum.BAR); |
977 builder.setOptionalNestedMessage(nestedMessage1); | 979 builder.setOptionalNestedMessage(nestedMessage1); |
978 builder.addRepeatedInt32(1); | 980 builder.addRepeatedInt32(1); |
979 builder.addRepeatedNestedEnum(TestAllTypes.NestedEnum.BAR); | 981 builder.addRepeatedNestedEnum(TestAllTypes.NestedEnum.BAR); |
980 builder.addRepeatedNestedMessage(nestedMessage1); | 982 builder.addRepeatedNestedMessage(nestedMessage1); |
981 assertEquals(0, mockParent.getInvalidationCount()); | 983 assertEquals(0, mockParent.getInvalidationCount()); |
982 | 984 |
983 // Now tell it we want changes and make sure it's only fired once | 985 // Now tell it we want changes and make sure it's only fired once |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1018 builder.addRepeatedNestedMessage(nestedMessage2); | 1020 builder.addRepeatedNestedMessage(nestedMessage2); |
1019 builder.addRepeatedNestedMessage(nestedMessage1); | 1021 builder.addRepeatedNestedMessage(nestedMessage1); |
1020 assertEquals(6, mockParent.getInvalidationCount()); | 1022 assertEquals(6, mockParent.getInvalidationCount()); |
1021 | 1023 |
1022 } | 1024 } |
1023 | 1025 |
1024 public void testInvalidations_Extensions() throws Exception { | 1026 public void testInvalidations_Extensions() throws Exception { |
1025 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); | 1027 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); |
1026 | 1028 |
1027 TestAllExtensions.Builder builder = (TestAllExtensions.Builder) | 1029 TestAllExtensions.Builder builder = (TestAllExtensions.Builder) |
1028 ((AbstractMessage) TestAllExtensions.getDefaultInstance()). | 1030 ((GeneratedMessage) TestAllExtensions.getDefaultInstance()). |
1029 newBuilderForType(mockParent); | 1031 newBuilderForType(mockParent); |
1030 | 1032 |
1031 builder.addExtension(UnittestProto.repeatedInt32Extension, 1); | 1033 builder.addExtension(UnittestProto.repeatedInt32Extension, 1); |
1032 builder.setExtension(UnittestProto.repeatedInt32Extension, 0, 2); | 1034 builder.setExtension(UnittestProto.repeatedInt32Extension, 0, 2); |
1033 builder.clearExtension(UnittestProto.repeatedInt32Extension); | 1035 builder.clearExtension(UnittestProto.repeatedInt32Extension); |
1034 assertEquals(0, mockParent.getInvalidationCount()); | 1036 assertEquals(0, mockParent.getInvalidationCount()); |
1035 | 1037 |
1036 // Now tell it we want changes and make sure it's only fired once | 1038 // Now tell it we want changes and make sure it's only fired once |
1037 builder.buildPartial(); | 1039 builder.buildPartial(); |
1038 builder.addExtension(UnittestProto.repeatedInt32Extension, 2); | 1040 builder.addExtension(UnittestProto.repeatedInt32Extension, 2); |
(...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1665 } | 1667 } |
1666 try { | 1668 try { |
1667 builder.getRepeatedFieldBuilder( | 1669 builder.getRepeatedFieldBuilder( |
1668 descriptor.findFieldByName("optional_nested_message"), 0); | 1670 descriptor.findFieldByName("optional_nested_message"), 0); |
1669 fail("Exception was not thrown"); | 1671 fail("Exception was not thrown"); |
1670 } catch (UnsupportedOperationException e) { | 1672 } catch (UnsupportedOperationException e) { |
1671 // We expect this exception. | 1673 // We expect this exception. |
1672 } | 1674 } |
1673 } | 1675 } |
1674 } | 1676 } |
OLD | NEW |