Unit Test proto3 generated objects with verify

2.6k views Asked by At

I am using proto3 for an Android app and I am having a problem with object equality, which makes it really hard on testing, especially for verify methods

Here is a unit-test representing the problem:

@Test
public void test_equal () {
    PlayerCards playerCards1 = new PlayerCards();
    playerCards1.playerId = 1;
    playerCards1.cards = new int[]{2};

    PlayerCards playerCards2 = new PlayerCards();
    playerCards2.playerId = 1;
    playerCards2.cards = new int[]{2};


    assertThat(playerCards1.toString(), is(playerCards2.toString())); // pass
    assertThat(PlayerCards.toByteArray(playerCards1), 
                          is(PlayerCards.toByteArray(playerCards2))); // pass
    assertThat(playerCards1, is(playerCards2)); // <----- fail

}

It is quite clear that the method equals is not working properly, checking the produced code (attached at the bottom) no equals, hashcode are generated. I can workaround the assertThat by using .toString method but I cannot find any other way for verifications, eg. verify(anyMock).anyMethod(playerCards)

I am afraid that this may also affect my runtime if am not extremely careful on checks.

  • Is there any way to generate equals, hashcode?
  • If not, can I at least extend, override verify so as to use toString when checking against proto-generated objects?

code-snippets:

My proto file is:

syntax = "proto3";

option java_multiple_files = true;
option optimize_for = LITE_RUNTIME; // existing or not has no effect.
option java_package = "com.package.my";
option java_outer_classname = "Proto";
option objc_class_prefix = "ABC";

package com.package.protos;

message PlayerCards {
    int64 playerId = 1;
    repeated int32 cards = 2;
}

I generate the files through gradle build and use the following properties

buildscript {
// ...
    dependencies {
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
    }
}

apply plugin: 'com.google.protobuf'
protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.1.0'
    }

    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
                javanano {
                    // Options added to --javanano_out
                    option 'ignore_services=true'
                    option 'enum_style=java'
                    option 'generate_intdefs=true'
                }
            }
        }
    }
}


dependencies {
// ...
    compile 'io.grpc:grpc-protobuf-nano:1.0.2'
}

The generated output:

// Generated by the protocol buffer compiler.  DO NOT EDIT!

package com.package.my.nano;

@SuppressWarnings("hiding")
public final class PlayerCards extends
    com.google.protobuf.nano.MessageNano {

  private static volatile PlayerCards[] _emptyArray;
  public static PlayerCards[] emptyArray() {
    // Lazily initializes the empty array
    if (_emptyArray == null) {
      synchronized (
          com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) {
        if (_emptyArray == null) {
          _emptyArray = new PlayerCards[0];
        }
      }
    }
    return _emptyArray;
  }

  // optional int64 playerId = 1;
  public long playerId;

  // repeated int32 cards = 2;
  public int[] cards;

  public PlayerCards() {
    clear();
  }

  public PlayerCards clear() {
    playerId = 0L;
    cards = com.google.protobuf.nano.WireFormatNano.EMPTY_INT_ARRAY;
    cachedSize = -1;
    return this;
  }

  @Override
  public void writeTo(com.google.protobuf.nano.CodedOutputByteBufferNano output)
      throws java.io.IOException {
    if (this.playerId != 0L) {
      output.writeInt64(1, this.playerId);
    }
    if (this.cards != null && this.cards.length > 0) {
      for (int i = 0; i < this.cards.length; i++) {
        output.writeInt32(2, this.cards[i]);
      }
    }
    super.writeTo(output);
  }

  @Override
  protected int computeSerializedSize() {
    int size = super.computeSerializedSize();
    if (this.playerId != 0L) {
      size += com.google.protobuf.nano.CodedOutputByteBufferNano
          .computeInt64Size(1, this.playerId);
    }
    if (this.cards != null && this.cards.length > 0) {
      int dataSize = 0;
      for (int i = 0; i < this.cards.length; i++) {
        int element = this.cards[i];
        dataSize += com.google.protobuf.nano.CodedOutputByteBufferNano
            .computeInt32SizeNoTag(element);
      }
      size += dataSize;
      size += 1 * this.cards.length;
    }
    return size;
  }

  @Override
  public PlayerCards mergeFrom(
          com.google.protobuf.nano.CodedInputByteBufferNano input)
      throws java.io.IOException {
    while (true) {
      int tag = input.readTag();
      switch (tag) {
        case 0:
          return this;
        default: {
          if (!com.google.protobuf.nano.WireFormatNano.parseUnknownField(input, tag)) {
            return this;
          }
          break;
        }
        case 8: {
          this.playerId = input.readInt64();
          break;
        }
        case 16: {
          int arrayLength = com.google.protobuf.nano.WireFormatNano
              .getRepeatedFieldArrayLength(input, 16);
          int i = this.cards == null ? 0 : this.cards.length;
          int[] newArray = new int[i + arrayLength];
          if (i != 0) {
            java.lang.System.arraycopy(this.cards, 0, newArray, 0, i);
          }
          for (; i < newArray.length - 1; i++) {
            newArray[i] = input.readInt32();
            input.readTag();
          }
          // Last one without readTag.
          newArray[i] = input.readInt32();
          this.cards = newArray;
          break;
        }
        case 18: {
          int length = input.readRawVarint32();
          int limit = input.pushLimit(length);
          // First pass to compute array length.
          int arrayLength = 0;
          int startPos = input.getPosition();
          while (input.getBytesUntilLimit() > 0) {
            input.readInt32();
            arrayLength++;
          }
          input.rewindToPosition(startPos);
          int i = this.cards == null ? 0 : this.cards.length;
          int[] newArray = new int[i + arrayLength];
          if (i != 0) {
            java.lang.System.arraycopy(this.cards, 0, newArray, 0, i);
          }
          for (; i < newArray.length; i++) {
            newArray[i] = input.readInt32();
          }
          this.cards = newArray;
          input.popLimit(limit);
          break;
        }
      }
    }
  }

  public static PlayerCards parseFrom(byte[] data)
      throws com.google.protobuf.nano.InvalidProtocolBufferNanoException {
    return com.google.protobuf.nano.MessageNano.mergeFrom(new PlayerCards(), data);
  }

  public static PlayerCards parseFrom(
          com.google.protobuf.nano.CodedInputByteBufferNano input)
      throws java.io.IOException {
    return new PlayerCards().mergeFrom(input);
  }
}
1

There are 1 answers

0
madlymad On BEST ANSWER

Proved a missing documentation.

Googling around I noticed the following property in a pom (!?) file generate_equals=true.

After adding this to gradle generation options the methods equals, hashcode generated!

ie.

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.1.0'
    }

    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
                javanano {
                    // Options added to --javanano_out
                    option 'ignore_services=true'
                    option 'enum_style=java'
                    option 'generate_intdefs=true'
                    option 'generate_equals=true' // <--- this one
                }
            }
        }
    }
}