~verterok/ubuntu/lucid/protobuf/2.4.0a-backport

« back to all changes in this revision

Viewing changes to java/src/test/java/com/google/protobuf/TestUtil.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2011-05-31 14:41:47 UTC
  • mfrom: (2.2.8 sid)
  • Revision ID: james.westby@ubuntu.com-20110531144147-s41g5fozgvyo462l
Tags: 2.4.0a-2ubuntu1
* Merge with Debian; remaining changes:
  - Fix linking with -lpthread.

Show diffs side-by-side

added added

removed removed

Lines of Context:
214
214
import static com.google.protobuf.UnittestLite.packedEnumExtensionLite;
215
215
 
216
216
import protobuf_unittest.UnittestProto.TestAllExtensions;
 
217
import protobuf_unittest.UnittestProto.TestAllExtensionsOrBuilder;
217
218
import protobuf_unittest.UnittestProto.TestAllTypes;
 
219
import protobuf_unittest.UnittestProto.TestAllTypesOrBuilder;
218
220
import protobuf_unittest.UnittestProto.TestPackedExtensions;
219
221
import protobuf_unittest.UnittestProto.TestPackedTypes;
220
222
import protobuf_unittest.UnittestProto.TestUnpackedTypes;
225
227
 
226
228
import com.google.protobuf.UnittestLite.TestAllTypesLite;
227
229
import com.google.protobuf.UnittestLite.TestAllExtensionsLite;
 
230
import com.google.protobuf.UnittestLite.TestAllExtensionsLiteOrBuilder;
228
231
import com.google.protobuf.UnittestLite.TestPackedExtensionsLite;
229
232
import com.google.protobuf.UnittestLite.ForeignMessageLite;
230
233
import com.google.protobuf.UnittestLite.ForeignEnumLite;
244
247
 * set all fields of a message, serialize it, parse it, and check that all
245
248
 * fields are set.
246
249
 *
 
250
 * <p>This code is not to be used outside of {@code com.google.protobuf} and
 
251
 * subpackages.
 
252
 *
247
253
 * @author kenton@google.com Kenton Varda
248
254
 */
249
 
class TestUtil {
 
255
public final class TestUtil {
250
256
  private TestUtil() {}
251
257
 
252
258
  /** Helper to convert a String to ByteString. */
485
491
   * Assert (using {@code junit.framework.Assert}} that all fields of
486
492
   * {@code message} are set to the values assigned by {@code setAllFields}.
487
493
   */
488
 
  public static void assertAllFieldsSet(TestAllTypes message) {
 
494
  public static void assertAllFieldsSet(TestAllTypesOrBuilder message) {
489
495
    Assert.assertTrue(message.hasOptionalInt32   ());
490
496
    Assert.assertTrue(message.hasOptionalInt64   ());
491
497
    Assert.assertTrue(message.hasOptionalUint32  ());
682
688
  }
683
689
 
684
690
  // -------------------------------------------------------------------
685
 
 
686
691
  /**
687
692
   * Assert (using {@code junit.framework.Assert}} that all fields of
688
693
   * {@code message} are cleared, and that getting the fields returns their
689
694
   * default values.
690
695
   */
691
 
  public static void assertClear(TestAllTypes message) {
 
696
  public static void assertClear(TestAllTypesOrBuilder message) {
692
697
    // hasBlah() should initially be false for all optional fields.
693
698
    Assert.assertFalse(message.hasOptionalInt32   ());
694
699
    Assert.assertFalse(message.hasOptionalInt64   ());
838
843
   * {@code message} are set to the values assigned by {@code setAllFields}
839
844
   * followed by {@code modifyRepeatedFields}.
840
845
   */
841
 
  public static void assertRepeatedFieldsModified(TestAllTypes message) {
 
846
  public static void assertRepeatedFieldsModified(
 
847
      TestAllTypesOrBuilder message) {
842
848
    // ModifyRepeatedFields only sets the second repeated element of each
843
849
    // field.  In addition to verifying this, we also verify that the first
844
850
    // element and size were *not* modified.
1352
1358
   * Assert (using {@code junit.framework.Assert}} that all extensions of
1353
1359
   * {@code message} are set to the values assigned by {@code setAllExtensions}.
1354
1360
   */
1355
 
  public static void assertAllExtensionsSet(TestAllExtensions message) {
 
1361
  public static void assertAllExtensionsSet(
 
1362
      TestAllExtensionsOrBuilder message) {
1356
1363
    Assert.assertTrue(message.hasExtension(optionalInt32Extension   ));
1357
1364
    Assert.assertTrue(message.hasExtension(optionalInt64Extension   ));
1358
1365
    Assert.assertTrue(message.hasExtension(optionalUint32Extension  ));
1567
1574
   * {@code message} are cleared, and that getting the extensions returns their
1568
1575
   * default values.
1569
1576
   */
1570
 
  public static void assertExtensionsClear(TestAllExtensions message) {
 
1577
  public static void assertExtensionsClear(TestAllExtensionsOrBuilder message) {
1571
1578
    // hasBlah() should initially be false for all optional fields.
1572
1579
    Assert.assertFalse(message.hasExtension(optionalInt32Extension   ));
1573
1580
    Assert.assertFalse(message.hasExtension(optionalInt64Extension   ));
1752
1759
   * followed by {@code modifyRepeatedExtensions}.
1753
1760
   */
1754
1761
  public static void assertRepeatedExtensionsModified(
1755
 
      TestAllExtensions message) {
 
1762
      TestAllExtensionsOrBuilder message) {
1756
1763
    // ModifyRepeatedFields only sets the second repeated element of each
1757
1764
    // field.  In addition to verifying this, we also verify that the first
1758
1765
    // element and size were *not* modified.
2106
2113
   * Assert (using {@code junit.framework.Assert}} that all extensions of
2107
2114
   * {@code message} are set to the values assigned by {@code setAllExtensions}.
2108
2115
   */
2109
 
  public static void assertAllExtensionsSet(TestAllExtensionsLite message) {
 
2116
  public static void assertAllExtensionsSet(
 
2117
      TestAllExtensionsLiteOrBuilder message) {
2110
2118
    Assert.assertTrue(message.hasExtension(optionalInt32ExtensionLite   ));
2111
2119
    Assert.assertTrue(message.hasExtension(optionalInt64ExtensionLite   ));
2112
2120
    Assert.assertTrue(message.hasExtension(optionalUint32ExtensionLite  ));
2321
2329
   * {@code message} are cleared, and that getting the extensions returns their
2322
2330
   * default values.
2323
2331
   */
2324
 
  public static void assertExtensionsClear(TestAllExtensionsLite message) {
 
2332
  public static void assertExtensionsClear(
 
2333
      TestAllExtensionsLiteOrBuilder message) {
2325
2334
    // hasBlah() should initially be false for all optional fields.
2326
2335
    Assert.assertFalse(message.hasExtension(optionalInt32ExtensionLite   ));
2327
2336
    Assert.assertFalse(message.hasExtension(optionalInt64ExtensionLite   ));
2478
2487
   * followed by {@code modifyRepeatedExtensions}.
2479
2488
   */
2480
2489
  public static void assertRepeatedExtensionsModified(
2481
 
      TestAllExtensionsLite message) {
 
2490
      TestAllExtensionsLiteOrBuilder message) {
2482
2491
    // ModifyRepeatedFields only sets the second repeated element of each
2483
2492
    // field.  In addition to verifying this, we also verify that the first
2484
2493
    // element and size were *not* modified.
3015
3024
     * {@code message} are set to the values assigned by {@code setAllFields},
3016
3025
     * using the {@link Message} reflection interface.
3017
3026
     */
3018
 
    public void assertAllFieldsSetViaReflection(Message message) {
 
3027
    public void assertAllFieldsSetViaReflection(MessageOrBuilder message) {
3019
3028
      Assert.assertTrue(message.hasField(f("optional_int32"   )));
3020
3029
      Assert.assertTrue(message.hasField(f("optional_int64"   )));
3021
3030
      Assert.assertTrue(message.hasField(f("optional_uint32"  )));
3248
3257
     * {@code message} are cleared, and that getting the fields returns their
3249
3258
     * default values, using the {@link Message} reflection interface.
3250
3259
     */
3251
 
    public void assertClearViaReflection(Message message) {
 
3260
    public void assertClearViaReflection(MessageOrBuilder message) {
3252
3261
      // has_blah() should initially be false for all optional fields.
3253
3262
      Assert.assertFalse(message.hasField(f("optional_int32"   )));
3254
3263
      Assert.assertFalse(message.hasField(f("optional_int64"   )));
3405
3414
      Assert.assertEquals("123", message.getField(f("default_cord")));
3406
3415
    }
3407
3416
 
 
3417
 
3408
3418
    // ---------------------------------------------------------------
3409
3419
 
3410
 
    public void assertRepeatedFieldsModifiedViaReflection(Message message) {
 
3420
    public void assertRepeatedFieldsModifiedViaReflection(
 
3421
        MessageOrBuilder message) {
3411
3422
      // ModifyRepeatedFields only sets the second repeated element of each
3412
3423
      // field.  In addition to verifying this, we also verify that the first
3413
3424
      // element and size were *not* modified.
3543
3554
      message.addRepeatedField(f("packed_enum" ),  foreignBaz);
3544
3555
    }
3545
3556
 
3546
 
    public void assertPackedFieldsSetViaReflection(Message message) {
 
3557
    public void assertPackedFieldsSetViaReflection(MessageOrBuilder message) {
3547
3558
      Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_int32"   )));
3548
3559
      Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_int64"   )));
3549
3560
      Assert.assertEquals(2, message.getRepeatedFieldCount(f("packed_uint32"  )));
3699
3710
 
3700
3711
  /**
3701
3712
   * @param filePath The path relative to
3702
 
   * {@link com.google.testing.util.TestUtil#getDefaultSrcDir}.
 
3713
   * {@link #getTestDataDir}.
3703
3714
   */
3704
3715
  public static String readTextFromFile(String filePath) {
3705
3716
    return readBytesFromFile(filePath).toStringUtf8();
3728
3739
  }
3729
3740
 
3730
3741
  /**
3731
 
   * @param filePath The path relative to
3732
 
   * {@link com.google.testing.util.TestUtil#getDefaultSrcDir}.
 
3742
   * @param filename The path relative to
 
3743
   * {@link #getTestDataDir}.
3733
3744
   */
3734
3745
  public static ByteString readBytesFromFile(String filename) {
3735
3746
    File fullPath = new File(getTestDataDir(), filename);
3749
3760
  /**
3750
3761
   * Get the bytes of the "golden message".  This is a serialized TestAllTypes
3751
3762
   * with all fields set as they would be by
3752
 
   * {@link setAllFields(TestAllTypes.Builder)}, but it is loaded from a file
 
3763
   * {@link #setAllFields(TestAllTypes.Builder)}, but it is loaded from a file
3753
3764
   * on disk rather than generated dynamically.  The file is actually generated
3754
3765
   * by C++ code, so testing against it verifies compatibility with C++.
3755
3766
   */
3764
3775
  /**
3765
3776
   * Get the bytes of the "golden packed fields message".  This is a serialized
3766
3777
   * TestPackedTypes with all fields set as they would be by
3767
 
   * {@link setPackedFields(TestPackedTypes.Builder)}, but it is loaded from a
 
3778
   * {@link #setPackedFields(TestPackedTypes.Builder)}, but it is loaded from a
3768
3779
   * file on disk rather than generated dynamically.  The file is actually
3769
3780
   * generated by C++ code, so testing against it verifies compatibility with
3770
3781
   * C++.
3777
3788
    return goldenPackedFieldsMessage;
3778
3789
  }
3779
3790
  private static ByteString goldenPackedFieldsMessage = null;
 
3791
 
 
3792
  /**
 
3793
   * Mock implementation of {@link GeneratedMessage.BuilderParent} for testing.
 
3794
   *
 
3795
   * @author jonp@google.com (Jon Perlow)
 
3796
   */
 
3797
  public static class MockBuilderParent
 
3798
      implements GeneratedMessage.BuilderParent {
 
3799
 
 
3800
    private int invalidations;
 
3801
 
 
3802
    @Override
 
3803
    public void markDirty() {
 
3804
      invalidations++;
 
3805
    }
 
3806
 
 
3807
    public int getInvalidationCount() {
 
3808
      return invalidations;
 
3809
    }
 
3810
  }
3780
3811
}