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