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