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 18 matching lines...) Expand all Loading... |
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 protobuf_unittest.UnittestProto.TestAllTypes; | 33 import protobuf_unittest.UnittestProto.TestAllTypes; |
34 import protobuf_unittest.UnittestProto.TestAllTypesOrBuilder; | 34 import protobuf_unittest.UnittestProto.TestAllTypesOrBuilder; |
35 | 35 |
36 import junit.framework.TestCase; | 36 import junit.framework.TestCase; |
37 | 37 |
38 /** | 38 /** |
39 * Tests for {@link SingleFieldBuilder}. This tests basic functionality. | 39 * Tests for {@link SingleFieldBuilderV3}. This tests basic functionality. |
40 * More extensive testing is provided via other tests that exercise the | 40 * More extensive testing is provided via other tests that exercise the |
41 * builder. | 41 * builder. |
42 * | 42 * |
43 * @author jonp@google.com (Jon Perlow) | 43 * @author jonp@google.com (Jon Perlow) |
44 */ | 44 */ |
45 public class SingleFieldBuilderTest extends TestCase { | 45 public class SingleFieldBuilderV3Test extends TestCase { |
46 | 46 |
47 public void testBasicUseAndInvalidations() { | 47 public void testBasicUseAndInvalidations() { |
48 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); | 48 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); |
49 SingleFieldBuilder<TestAllTypes, TestAllTypes.Builder, | 49 SingleFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, |
50 TestAllTypesOrBuilder> builder = | 50 TestAllTypesOrBuilder> builder = |
51 new SingleFieldBuilder<TestAllTypes, TestAllTypes.Builder, | 51 new SingleFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, |
52 TestAllTypesOrBuilder>( | 52 TestAllTypesOrBuilder>( |
53 TestAllTypes.getDefaultInstance(), | 53 TestAllTypes.getDefaultInstance(), |
54 mockParent, | 54 mockParent, |
55 false); | 55 false); |
56 assertSame(TestAllTypes.getDefaultInstance(), builder.getMessage()); | 56 assertSame(TestAllTypes.getDefaultInstance(), builder.getMessage()); |
57 assertEquals(TestAllTypes.getDefaultInstance(), | 57 assertEquals(TestAllTypes.getDefaultInstance(), |
58 builder.getBuilder().buildPartial()); | 58 builder.getBuilder().buildPartial()); |
59 assertEquals(0, mockParent.getInvalidationCount()); | 59 assertEquals(0, mockParent.getInvalidationCount()); |
60 | 60 |
61 builder.getBuilder().setOptionalInt32(10); | 61 builder.getBuilder().setOptionalInt32(10); |
62 assertEquals(0, mockParent.getInvalidationCount()); | 62 assertEquals(0, mockParent.getInvalidationCount()); |
63 TestAllTypes message = builder.build(); | 63 TestAllTypes message = builder.build(); |
64 assertEquals(10, message.getOptionalInt32()); | 64 assertEquals(10, message.getOptionalInt32()); |
65 | 65 |
66 // Test that we receive invalidations now that build has been called. | 66 // Test that we receive invalidations now that build has been called. |
67 assertEquals(0, mockParent.getInvalidationCount()); | 67 assertEquals(0, mockParent.getInvalidationCount()); |
68 builder.getBuilder().setOptionalInt32(20); | 68 builder.getBuilder().setOptionalInt32(20); |
69 assertEquals(1, mockParent.getInvalidationCount()); | 69 assertEquals(1, mockParent.getInvalidationCount()); |
70 | 70 |
71 // Test that we don't keep getting invalidations on every change | 71 // Test that we don't keep getting invalidations on every change |
72 builder.getBuilder().setOptionalInt32(30); | 72 builder.getBuilder().setOptionalInt32(30); |
73 assertEquals(1, mockParent.getInvalidationCount()); | 73 assertEquals(1, mockParent.getInvalidationCount()); |
74 | 74 |
75 } | 75 } |
76 | 76 |
77 public void testSetMessage() { | 77 public void testSetMessage() { |
78 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); | 78 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); |
79 SingleFieldBuilder<TestAllTypes, TestAllTypes.Builder, | 79 SingleFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, |
80 TestAllTypesOrBuilder> builder = | 80 TestAllTypesOrBuilder> builder = |
81 new SingleFieldBuilder<TestAllTypes, TestAllTypes.Builder, | 81 new SingleFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, |
82 TestAllTypesOrBuilder>( | 82 TestAllTypesOrBuilder>( |
83 TestAllTypes.getDefaultInstance(), | 83 TestAllTypes.getDefaultInstance(), |
84 mockParent, | 84 mockParent, |
85 false); | 85 false); |
86 builder.setMessage(TestAllTypes.newBuilder().setOptionalInt32(0).build()); | 86 builder.setMessage(TestAllTypes.newBuilder().setOptionalInt32(0).build()); |
87 assertEquals(0, builder.getMessage().getOptionalInt32()); | 87 assertEquals(0, builder.getMessage().getOptionalInt32()); |
88 | 88 |
89 // Update message using the builder | 89 // Update message using the builder |
90 builder.getBuilder().setOptionalInt32(1); | 90 builder.getBuilder().setOptionalInt32(1); |
91 assertEquals(0, mockParent.getInvalidationCount()); | 91 assertEquals(0, mockParent.getInvalidationCount()); |
92 assertEquals(1, builder.getBuilder().getOptionalInt32()); | 92 assertEquals(1, builder.getBuilder().getOptionalInt32()); |
93 assertEquals(1, builder.getMessage().getOptionalInt32()); | 93 assertEquals(1, builder.getMessage().getOptionalInt32()); |
94 builder.build(); | 94 builder.build(); |
95 builder.getBuilder().setOptionalInt32(2); | 95 builder.getBuilder().setOptionalInt32(2); |
96 assertEquals(2, builder.getBuilder().getOptionalInt32()); | 96 assertEquals(2, builder.getBuilder().getOptionalInt32()); |
97 assertEquals(2, builder.getMessage().getOptionalInt32()); | 97 assertEquals(2, builder.getMessage().getOptionalInt32()); |
98 | 98 |
99 // Make sure message stays cached | 99 // Make sure message stays cached |
100 assertSame(builder.getMessage(), builder.getMessage()); | 100 assertSame(builder.getMessage(), builder.getMessage()); |
101 } | 101 } |
102 | 102 |
103 public void testClear() { | 103 public void testClear() { |
104 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); | 104 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); |
105 SingleFieldBuilder<TestAllTypes, TestAllTypes.Builder, | 105 SingleFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, |
106 TestAllTypesOrBuilder> builder = | 106 TestAllTypesOrBuilder> builder = |
107 new SingleFieldBuilder<TestAllTypes, TestAllTypes.Builder, | 107 new SingleFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, |
108 TestAllTypesOrBuilder>( | 108 TestAllTypesOrBuilder>( |
109 TestAllTypes.getDefaultInstance(), | 109 TestAllTypes.getDefaultInstance(), |
110 mockParent, | 110 mockParent, |
111 false); | 111 false); |
112 builder.setMessage(TestAllTypes.newBuilder().setOptionalInt32(0).build()); | 112 builder.setMessage(TestAllTypes.newBuilder().setOptionalInt32(0).build()); |
113 assertNotSame(TestAllTypes.getDefaultInstance(), builder.getMessage()); | 113 assertNotSame(TestAllTypes.getDefaultInstance(), builder.getMessage()); |
114 builder.clear(); | 114 builder.clear(); |
115 assertSame(TestAllTypes.getDefaultInstance(), builder.getMessage()); | 115 assertSame(TestAllTypes.getDefaultInstance(), builder.getMessage()); |
116 | 116 |
117 builder.getBuilder().setOptionalInt32(1); | 117 builder.getBuilder().setOptionalInt32(1); |
118 assertNotSame(TestAllTypes.getDefaultInstance(), builder.getMessage()); | 118 assertNotSame(TestAllTypes.getDefaultInstance(), builder.getMessage()); |
119 builder.clear(); | 119 builder.clear(); |
120 assertSame(TestAllTypes.getDefaultInstance(), builder.getMessage()); | 120 assertSame(TestAllTypes.getDefaultInstance(), builder.getMessage()); |
121 } | 121 } |
122 | 122 |
123 public void testMerge() { | 123 public void testMerge() { |
124 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); | 124 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); |
125 SingleFieldBuilder<TestAllTypes, TestAllTypes.Builder, | 125 SingleFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, |
126 TestAllTypesOrBuilder> builder = | 126 TestAllTypesOrBuilder> builder = |
127 new SingleFieldBuilder<TestAllTypes, TestAllTypes.Builder, | 127 new SingleFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, |
128 TestAllTypesOrBuilder>( | 128 TestAllTypesOrBuilder>( |
129 TestAllTypes.getDefaultInstance(), | 129 TestAllTypes.getDefaultInstance(), |
130 mockParent, | 130 mockParent, |
131 false); | 131 false); |
132 | 132 |
133 // Merge into default field. | 133 // Merge into default field. |
134 builder.mergeFrom(TestAllTypes.getDefaultInstance()); | 134 builder.mergeFrom(TestAllTypes.getDefaultInstance()); |
135 assertSame(TestAllTypes.getDefaultInstance(), builder.getMessage()); | 135 assertSame(TestAllTypes.getDefaultInstance(), builder.getMessage()); |
136 | 136 |
137 // Merge into non-default field on existing builder. | 137 // Merge into non-default field on existing builder. |
138 builder.getBuilder().setOptionalInt32(2); | 138 builder.getBuilder().setOptionalInt32(2); |
139 builder.mergeFrom(TestAllTypes.newBuilder() | 139 builder.mergeFrom(TestAllTypes.newBuilder() |
140 .setOptionalDouble(4.0) | 140 .setOptionalDouble(4.0) |
141 .buildPartial()); | 141 .buildPartial()); |
142 assertEquals(2, builder.getMessage().getOptionalInt32()); | 142 assertEquals(2, builder.getMessage().getOptionalInt32()); |
143 assertEquals(4.0, builder.getMessage().getOptionalDouble()); | 143 assertEquals(4.0, builder.getMessage().getOptionalDouble()); |
144 | 144 |
145 // Merge into non-default field on existing message | 145 // Merge into non-default field on existing message |
146 builder.setMessage(TestAllTypes.newBuilder() | 146 builder.setMessage(TestAllTypes.newBuilder() |
147 .setOptionalInt32(10) | 147 .setOptionalInt32(10) |
148 .buildPartial()); | 148 .buildPartial()); |
149 builder.mergeFrom(TestAllTypes.newBuilder() | 149 builder.mergeFrom(TestAllTypes.newBuilder() |
150 .setOptionalDouble(5.0) | 150 .setOptionalDouble(5.0) |
151 .buildPartial()); | 151 .buildPartial()); |
152 assertEquals(10, builder.getMessage().getOptionalInt32()); | 152 assertEquals(10, builder.getMessage().getOptionalInt32()); |
153 assertEquals(5.0, builder.getMessage().getOptionalDouble()); | 153 assertEquals(5.0, builder.getMessage().getOptionalDouble()); |
154 } | 154 } |
155 } | 155 } |
OLD | NEW |