Home | History | Annotate | Download | only in protobuf
      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.TestAllExtensions;
     34 import protobuf_unittest.UnittestProto.TestAllTypes;
     35 
     36 import junit.framework.TestCase;
     37 
     38 import java.io.IOException;
     39 
     40 /**
     41  * Unit test for {@link LazyField}.
     42  *
     43  * @author xiangl (at) google.com (Xiang Li)
     44  */
     45 public class LazyFieldTest extends TestCase {
     46   public void testHashCode() {
     47     MessageLite message = TestUtil.getAllSet();
     48     LazyField lazyField =
     49         createLazyFieldFromMessage(message);
     50     assertEquals(message.hashCode(), lazyField.hashCode());
     51     lazyField.getValue();
     52     assertEquals(message.hashCode(), lazyField.hashCode());
     53     changeValue(lazyField);
     54     // make sure two messages have different hash code
     55     assertNotEqual(message.hashCode(), lazyField.hashCode());
     56   }
     57 
     58   public void testHashCodeEx() throws Exception {
     59     TestAllExtensions message = TestUtil.getAllExtensionsSet();
     60     LazyField lazyField = createLazyFieldFromMessage(message);
     61     assertEquals(message.hashCode(), lazyField.hashCode());
     62     lazyField.getValue();
     63     assertEquals(message.hashCode(), lazyField.hashCode());
     64     changeValue(lazyField);
     65     // make sure two messages have different hash code
     66     assertNotEqual(message.hashCode(), lazyField.hashCode());
     67   }
     68 
     69   public void testGetValue() {
     70     MessageLite message = TestUtil.getAllSet();
     71     LazyField lazyField = createLazyFieldFromMessage(message);
     72     assertEquals(message, lazyField.getValue());
     73     changeValue(lazyField);
     74     assertNotEqual(message, lazyField.getValue());
     75   }
     76 
     77   public void testGetValueEx() throws Exception {
     78     TestAllExtensions message = TestUtil.getAllExtensionsSet();
     79     LazyField lazyField = createLazyFieldFromMessage(message);
     80     assertEquals(message, lazyField.getValue());
     81     changeValue(lazyField);
     82     assertNotEqual(message, lazyField.getValue());
     83   }
     84 
     85   public void testEqualsObject() {
     86     MessageLite message = TestUtil.getAllSet();
     87     LazyField lazyField = createLazyFieldFromMessage(message);
     88     assertTrue(lazyField.equals(message));
     89     changeValue(lazyField);
     90     assertFalse(lazyField.equals(message));
     91     assertFalse(message.equals(lazyField.getValue()));
     92   }
     93 
     94   public void testEqualsObjectEx() throws Exception {
     95     TestAllExtensions message = TestUtil.getAllExtensionsSet();
     96     LazyField lazyField = createLazyFieldFromMessage(message);
     97     assertTrue(lazyField.equals(message));
     98     changeValue(lazyField);
     99     assertFalse(lazyField.equals(message));
    100     assertFalse(message.equals(lazyField.getValue()));
    101   }
    102 
    103   // Help methods.
    104 
    105   private LazyField createLazyFieldFromMessage(MessageLite message) {
    106     ByteString bytes = message.toByteString();
    107     return new LazyField(message.getDefaultInstanceForType(),
    108         TestUtil.getExtensionRegistry(), bytes);
    109   }
    110 
    111   private void changeValue(LazyField lazyField) {
    112     TestAllTypes.Builder builder = TestUtil.getAllSet().toBuilder();
    113     builder.addRepeatedBool(true);
    114     MessageLite newMessage = builder.build();
    115     lazyField.setValue(newMessage);
    116   }
    117 
    118   private void assertNotEqual(Object unexpected, Object actual) {
    119     assertFalse(unexpected == actual
    120         || (unexpected != null && unexpected.equals(actual)));
    121   }
    122 }
    123