| 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 // http://code.google.com/p/protobuf/ | 
| 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. | 
| 11 //     * Redistributions in binary form must reproduce the above | 11 //     * Redistributions in binary form must reproduce the above | 
| 12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer | 
| 13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the | 
| 14 // distribution. | 14 // distribution. | 
| 15 //     * Neither the name of Google Inc. nor the names of its | 15 //     * Neither the name of Google Inc. nor the names of its | 
| 16 // contributors may be used to endorse or promote products derived from | 16 // contributors may be used to endorse or promote products derived from | 
| 17 // this software without specific prior written permission. | 17 // this software without specific prior written permission. | 
| 18 // | 18 // | 
| 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
| 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
| 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
| 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
| 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
| 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
| 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
| 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
| 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.test; | 
|  | 32 import com.google.protobuf.*; | 
| 32 | 33 | 
| 33 import com.google.protobuf.Descriptors.FieldDescriptor; | 34 import com.google.protobuf.Descriptors.FieldDescriptor; | 
| 34 import protobuf_unittest.UnittestOptimizeFor.TestOptimizedForSize; | 35 import protobuf_unittest.UnittestOptimizeFor.TestOptimizedForSize; | 
| 35 import protobuf_unittest.UnittestProto; | 36 import protobuf_unittest.UnittestProto; | 
| 36 import protobuf_unittest.UnittestProto.ForeignMessage; | 37 import protobuf_unittest.UnittestProto.ForeignMessage; | 
| 37 import protobuf_unittest.UnittestProto.TestAllExtensions; | 38 import protobuf_unittest.UnittestProto.TestAllExtensions; | 
| 38 import protobuf_unittest.UnittestProto.TestAllTypes; | 39 import protobuf_unittest.UnittestProto.TestAllTypes; | 
| 39 import protobuf_unittest.UnittestProto.TestPackedTypes; | 40 import protobuf_unittest.UnittestProto.TestPackedTypes; | 
| 40 import protobuf_unittest.UnittestProto.TestRequired; | 41 import protobuf_unittest.UnittestProto.TestRequired; | 
| 41 import protobuf_unittest.UnittestProto.TestRequiredForeign; | 42 import protobuf_unittest.UnittestProto.TestRequiredForeign; | 
| 42 import protobuf_unittest.UnittestProto.TestUnpackedTypes; | 43 import protobuf_unittest.UnittestProto.TestUnpackedTypes; | 
|  | 44 | 
| 43 import junit.framework.TestCase; | 45 import junit.framework.TestCase; | 
| 44 | 46 | 
| 45 import java.util.Map; | 47 import java.util.Map; | 
| 46 | 48 | 
| 47 /** | 49 /** | 
| 48  * Unit test for {@link AbstractMessage}. | 50  * Unit test for {@link AbstractMessage}. | 
| 49  * | 51  * | 
| 50  * @author kenton@google.com Kenton Varda | 52  * @author kenton@google.com Kenton Varda | 
| 51  */ | 53  */ | 
| 52 public class AbstractMessageTest extends TestCase { | 54 public class AbstractMessageTest extends TestCase { | 
| 53   /** | 55   /** | 
| 54    * Extends AbstractMessage and wraps some other message object.  The methods | 56    * Extends AbstractMessage and wraps some other message object.  The methods | 
| 55    * of the Message interface which aren't explicitly implemented by | 57    * of the Message interface which aren't explicitly implemented by | 
| 56    * AbstractMessage are forwarded to the wrapped object.  This allows us to | 58    * AbstractMessage are forwarded to the wrapped object.  This allows us to | 
| 57    * test that AbstractMessage's implementations work even if the wrapped | 59    * test that AbstractMessage's implementations work even if the wrapped | 
| 58    * object does not use them. | 60    * object does not use them. | 
| 59    */ | 61    */ | 
| 60   private static class AbstractMessageWrapper extends AbstractMessage { | 62   private static class AbstractMessageWrapper extends AbstractMessage { | 
| 61     private final Message wrappedMessage; | 63     private final Message wrappedMessage; | 
| 62 | 64 | 
| 63     public AbstractMessageWrapper(Message wrappedMessage) { | 65     public AbstractMessageWrapper(Message wrappedMessage) { | 
| 64       this.wrappedMessage = wrappedMessage; | 66       this.wrappedMessage = wrappedMessage; | 
| 65     } | 67     } | 
| 66 | 68 | 
| 67     @Override |  | 
| 68     public Descriptors.Descriptor getDescriptorForType() { | 69     public Descriptors.Descriptor getDescriptorForType() { | 
| 69       return wrappedMessage.getDescriptorForType(); | 70       return wrappedMessage.getDescriptorForType(); | 
| 70     } | 71     } | 
| 71     @Override |  | 
| 72     public AbstractMessageWrapper getDefaultInstanceForType() { | 72     public AbstractMessageWrapper getDefaultInstanceForType() { | 
| 73       return new AbstractMessageWrapper( | 73       return new AbstractMessageWrapper( | 
| 74         wrappedMessage.getDefaultInstanceForType()); | 74         wrappedMessage.getDefaultInstanceForType()); | 
| 75     } | 75     } | 
| 76     @Override |  | 
| 77     public Map<Descriptors.FieldDescriptor, Object> getAllFields() { | 76     public Map<Descriptors.FieldDescriptor, Object> getAllFields() { | 
| 78       return wrappedMessage.getAllFields(); | 77       return wrappedMessage.getAllFields(); | 
| 79     } | 78     } | 
| 80     @Override |  | 
| 81     public boolean hasField(Descriptors.FieldDescriptor field) { | 79     public boolean hasField(Descriptors.FieldDescriptor field) { | 
| 82       return wrappedMessage.hasField(field); | 80       return wrappedMessage.hasField(field); | 
| 83     } | 81     } | 
| 84     @Override |  | 
| 85     public Object getField(Descriptors.FieldDescriptor field) { | 82     public Object getField(Descriptors.FieldDescriptor field) { | 
| 86       return wrappedMessage.getField(field); | 83       return wrappedMessage.getField(field); | 
| 87     } | 84     } | 
| 88     @Override |  | 
| 89     public int getRepeatedFieldCount(Descriptors.FieldDescriptor field) { | 85     public int getRepeatedFieldCount(Descriptors.FieldDescriptor field) { | 
| 90       return wrappedMessage.getRepeatedFieldCount(field); | 86       return wrappedMessage.getRepeatedFieldCount(field); | 
| 91     } | 87     } | 
| 92     @Override | 88     public Object getRepeatedField( | 
| 93     public Object getRepeatedField(Descriptors.FieldDescriptor field, int index)
      { | 89         Descriptors.FieldDescriptor field, int index) { | 
| 94       return wrappedMessage.getRepeatedField(field, index); | 90       return wrappedMessage.getRepeatedField(field, index); | 
| 95     } | 91     } | 
| 96     @Override |  | 
| 97     public UnknownFieldSet getUnknownFields() { | 92     public UnknownFieldSet getUnknownFields() { | 
| 98       return wrappedMessage.getUnknownFields(); | 93       return wrappedMessage.getUnknownFields(); | 
| 99     } | 94     } | 
| 100     @Override |  | 
| 101     public Builder newBuilderForType() { | 95     public Builder newBuilderForType() { | 
| 102       return new Builder(wrappedMessage.newBuilderForType()); | 96       return new Builder(wrappedMessage.newBuilderForType()); | 
| 103     } | 97     } | 
| 104     @Override |  | 
| 105     public Builder toBuilder() { | 98     public Builder toBuilder() { | 
| 106       return new Builder(wrappedMessage.toBuilder()); | 99       return new Builder(wrappedMessage.toBuilder()); | 
| 107     } | 100     } | 
| 108 | 101 | 
| 109     static class Builder extends AbstractMessage.Builder<Builder> { | 102     static class Builder extends AbstractMessage.Builder<Builder> { | 
| 110       private final Message.Builder wrappedBuilder; | 103       private final Message.Builder wrappedBuilder; | 
| 111 | 104 | 
| 112       public Builder(Message.Builder wrappedBuilder) { | 105       public Builder(Message.Builder wrappedBuilder) { | 
| 113         this.wrappedBuilder = wrappedBuilder; | 106         this.wrappedBuilder = wrappedBuilder; | 
| 114       } | 107       } | 
| 115 | 108 | 
| 116       @Override |  | 
| 117       public AbstractMessageWrapper build() { | 109       public AbstractMessageWrapper build() { | 
| 118         return new AbstractMessageWrapper(wrappedBuilder.build()); | 110         return new AbstractMessageWrapper(wrappedBuilder.build()); | 
| 119       } | 111       } | 
| 120       @Override |  | 
| 121       public AbstractMessageWrapper buildPartial() { | 112       public AbstractMessageWrapper buildPartial() { | 
| 122         return new AbstractMessageWrapper(wrappedBuilder.buildPartial()); | 113         return new AbstractMessageWrapper(wrappedBuilder.buildPartial()); | 
| 123       } | 114       } | 
| 124       @Override |  | 
| 125       public Builder clone() { | 115       public Builder clone() { | 
| 126         return new Builder(wrappedBuilder.clone()); | 116         return new Builder(wrappedBuilder.clone()); | 
| 127       } | 117       } | 
| 128       @Override |  | 
| 129       public boolean isInitialized() { | 118       public boolean isInitialized() { | 
| 130         return clone().buildPartial().isInitialized(); | 119         return clone().buildPartial().isInitialized(); | 
| 131       } | 120       } | 
| 132       @Override |  | 
| 133       public Descriptors.Descriptor getDescriptorForType() { | 121       public Descriptors.Descriptor getDescriptorForType() { | 
| 134         return wrappedBuilder.getDescriptorForType(); | 122         return wrappedBuilder.getDescriptorForType(); | 
| 135       } | 123       } | 
| 136       @Override |  | 
| 137       public AbstractMessageWrapper getDefaultInstanceForType() { | 124       public AbstractMessageWrapper getDefaultInstanceForType() { | 
| 138         return new AbstractMessageWrapper( | 125         return new AbstractMessageWrapper( | 
| 139           wrappedBuilder.getDefaultInstanceForType()); | 126           wrappedBuilder.getDefaultInstanceForType()); | 
| 140       } | 127       } | 
| 141       @Override |  | 
| 142       public Map<Descriptors.FieldDescriptor, Object> getAllFields() { | 128       public Map<Descriptors.FieldDescriptor, Object> getAllFields() { | 
| 143         return wrappedBuilder.getAllFields(); | 129         return wrappedBuilder.getAllFields(); | 
| 144       } | 130       } | 
| 145       @Override |  | 
| 146       public Builder newBuilderForField(Descriptors.FieldDescriptor field) { | 131       public Builder newBuilderForField(Descriptors.FieldDescriptor field) { | 
| 147         return new Builder(wrappedBuilder.newBuilderForField(field)); | 132         return new Builder(wrappedBuilder.newBuilderForField(field)); | 
| 148       } | 133       } | 
| 149       @Override |  | 
| 150       public boolean hasField(Descriptors.FieldDescriptor field) { | 134       public boolean hasField(Descriptors.FieldDescriptor field) { | 
| 151         return wrappedBuilder.hasField(field); | 135         return wrappedBuilder.hasField(field); | 
| 152       } | 136       } | 
| 153       @Override |  | 
| 154       public Object getField(Descriptors.FieldDescriptor field) { | 137       public Object getField(Descriptors.FieldDescriptor field) { | 
| 155         return wrappedBuilder.getField(field); | 138         return wrappedBuilder.getField(field); | 
| 156       } | 139       } | 
| 157       @Override |  | 
| 158       public Builder setField(Descriptors.FieldDescriptor field, Object value) { | 140       public Builder setField(Descriptors.FieldDescriptor field, Object value) { | 
| 159         wrappedBuilder.setField(field, value); | 141         wrappedBuilder.setField(field, value); | 
| 160         return this; | 142         return this; | 
| 161       } | 143       } | 
| 162       @Override |  | 
| 163       public Builder clearField(Descriptors.FieldDescriptor field) { | 144       public Builder clearField(Descriptors.FieldDescriptor field) { | 
| 164         wrappedBuilder.clearField(field); | 145         wrappedBuilder.clearField(field); | 
| 165         return this; | 146         return this; | 
| 166       } | 147       } | 
| 167       @Override |  | 
| 168       public int getRepeatedFieldCount(Descriptors.FieldDescriptor field) { | 148       public int getRepeatedFieldCount(Descriptors.FieldDescriptor field) { | 
| 169         return wrappedBuilder.getRepeatedFieldCount(field); | 149         return wrappedBuilder.getRepeatedFieldCount(field); | 
| 170       } | 150       } | 
| 171       @Override | 151       public Object getRepeatedField( | 
| 172       public Object getRepeatedField(Descriptors.FieldDescriptor field, int inde
     x) { | 152           Descriptors.FieldDescriptor field, int index) { | 
| 173         return wrappedBuilder.getRepeatedField(field, index); | 153         return wrappedBuilder.getRepeatedField(field, index); | 
| 174       } | 154       } | 
| 175       @Override | 155       public Builder setRepeatedField(Descriptors.FieldDescriptor field, | 
| 176       public Builder setRepeatedField(Descriptors.FieldDescriptor field, int ind
     ex, Object value) { | 156                                       int index, Object value) { | 
| 177         wrappedBuilder.setRepeatedField(field, index, value); | 157         wrappedBuilder.setRepeatedField(field, index, value); | 
| 178         return this; | 158         return this; | 
| 179       } | 159       } | 
| 180       @Override | 160       public Builder addRepeatedField( | 
| 181       public Builder addRepeatedField(Descriptors.FieldDescriptor field, Object 
     value) { | 161           Descriptors.FieldDescriptor field, Object value) { | 
| 182         wrappedBuilder.addRepeatedField(field, value); | 162         wrappedBuilder.addRepeatedField(field, value); | 
| 183         return this; | 163         return this; | 
| 184       } | 164       } | 
| 185       @Override |  | 
| 186       public UnknownFieldSet getUnknownFields() { | 165       public UnknownFieldSet getUnknownFields() { | 
| 187         return wrappedBuilder.getUnknownFields(); | 166         return wrappedBuilder.getUnknownFields(); | 
| 188       } | 167       } | 
| 189       @Override |  | 
| 190       public Builder setUnknownFields(UnknownFieldSet unknownFields) { | 168       public Builder setUnknownFields(UnknownFieldSet unknownFields) { | 
| 191         wrappedBuilder.setUnknownFields(unknownFields); | 169         wrappedBuilder.setUnknownFields(unknownFields); | 
| 192         return this; | 170         return this; | 
| 193       } | 171       } | 
| 194       @Override | 172       @Override | 
| 195       public Message.Builder getFieldBuilder(FieldDescriptor field) { | 173       public Message.Builder getFieldBuilder(FieldDescriptor field) { | 
| 196         return wrappedBuilder.getFieldBuilder(field); | 174         return wrappedBuilder.getFieldBuilder(field); | 
| 197       } | 175       } | 
| 198     } | 176     } | 
| 199     @Override |  | 
| 200     public Parser<? extends Message> getParserForType() { | 177     public Parser<? extends Message> getParserForType() { | 
| 201       return wrappedMessage.getParserForType(); | 178       return wrappedMessage.getParserForType(); | 
| 202     } | 179     } | 
| 203   } | 180   } | 
| 204 | 181 | 
| 205   // ================================================================= | 182   // ================================================================= | 
| 206 | 183 | 
| 207   TestUtil.ReflectionTester reflectionTester = | 184   TestUtil.ReflectionTester reflectionTester = | 
| 208     new TestUtil.ReflectionTester(TestAllTypes.getDescriptor(), null); | 185     new TestUtil.ReflectionTester(TestAllTypes.getDescriptor(), null); | 
| 209 | 186 | 
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 523    */ | 500    */ | 
| 524   private void checkNotEqual(Message m1, Message m2) { | 501   private void checkNotEqual(Message m1, Message m2) { | 
| 525     String equalsError = String.format("%s should not be equal to %s", m1, m2); | 502     String equalsError = String.format("%s should not be equal to %s", m1, m2); | 
| 526     assertFalse(equalsError, m1.equals(m2)); | 503     assertFalse(equalsError, m1.equals(m2)); | 
| 527     assertFalse(equalsError, m2.equals(m1)); | 504     assertFalse(equalsError, m2.equals(m1)); | 
| 528 | 505 | 
| 529     assertFalse( | 506     assertFalse( | 
| 530         String.format("%s should have a different hash code from %s", m1, m2), | 507         String.format("%s should have a different hash code from %s", m1, m2), | 
| 531         m1.hashCode() == m2.hashCode()); | 508         m1.hashCode() == m2.hashCode()); | 
| 532   } | 509   } | 
| 533 |  | 
| 534   public void testCheckByteStringIsUtf8OnUtf8() { |  | 
| 535     ByteString byteString = ByteString.copyFromUtf8("some text"); |  | 
| 536     AbstractMessageLite.checkByteStringIsUtf8(byteString); |  | 
| 537     // No exception thrown. |  | 
| 538   } |  | 
| 539 |  | 
| 540   public void testCheckByteStringIsUtf8OnNonUtf8() { |  | 
| 541     ByteString byteString = |  | 
| 542         ByteString.copyFrom(new byte[]{(byte) 0x80}); // A lone continuation byt
     e. |  | 
| 543     try { |  | 
| 544       AbstractMessageLite.checkByteStringIsUtf8(byteString); |  | 
| 545       fail("Expected AbstractMessageLite.checkByteStringIsUtf8 to throw IllegalA
     rgumentException"); |  | 
| 546     } catch (IllegalArgumentException exception) { |  | 
| 547       assertEquals("Byte string is not UTF-8.", exception.getMessage()); |  | 
| 548     } |  | 
| 549   } |  | 
| 550 |  | 
| 551 } | 510 } | 
| OLD | NEW | 
|---|