Android 单元测试proto3生成的带有验证的对象

Android 单元测试proto3生成的带有验证的对象,android,unit-testing,grpc-java,proto3,Android,Unit Testing,Grpc Java,Proto3,我正在为Android应用程序使用proto3,我在对象平等性方面遇到了问题,这使得测试非常困难,尤其是验证方法 下面是一个表示问题的单元测试: @Test public void test_equal () { PlayerCards playerCards1 = new PlayerCards(); playerCards1.playerId = 1; playerCards1.cards = new int[]{2}; PlayerCards player

我正在为Android应用程序使用
proto3
,我在对象平等性方面遇到了问题,这使得测试非常困难,尤其是验证方法

下面是一个表示问题的单元测试:

@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

}
我通过gradle build生成文件,并使用以下属性

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'
}
生成的输出

// 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);
  }
}
//由协议缓冲区编译器生成。不要编辑!
package com.package.my.nano;
@抑制警告(“隐藏”)
公开期末班游戏卡扩展
com.google.protobuf.nano.MessageNano{
私人静态易失性播放卡[]_emptyArray;
公共静态播放卡[]空播放(){
//延迟初始化空数组
如果(_emptyArray==null){
同步的(
com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK){
如果(_emptyArray==null){
_emptyArray=新的播放卡[0];
}
}
}
返回空数组;
}
//可选的int64播放器ID=1;
公共长播放ID;
//重复int32卡=2;
公共int[]卡;
公共游戏卡(){
清除();
}
公共播放卡清除(){
playerId=0升;
cards=com.google.protobuf.nano.WireFormatNano.EMPTY\u INT\u数组;
cachedSize=-1;
归还这个;
}
@凌驾
public void writeTo(com.google.protobuf.nano.CodedOutputByteBufferNano输出)
抛出java.io.IOException{
如果(this.playerId!=0L){
output.writeInt64(1,this.playerId);
}
如果(this.cards!=null&&this.cards.length>0){
for(int i=0;i0){
int-dataSize=0;
for(int i=0;i0){
input.readInt32();
arrayLength++;
}
输入。倒带位置(startPos);
int i=this.cards==null?0:this.cards.length;
int[]newArray=newint[i+arraylelength];
如果(i!=0){
java.lang.System.arraycopy(this.cards,0,newArray,0,i);
}
对于(;i
证明缺少文档

在谷歌上搜索时,我注意到pom(!?)文件中的以下属性
generate_equals=true

将此添加到gradle生成选项后,方法
等于生成的
hashcode

protobuf{
原始人{
artifact='com.google.protobuf:protoc:3.1.0'
}
生成原型任务{
all()。每个{task->
task.builtins{
删除java
爪哇纳{
//添加到--javanano的选项
选项“忽略服务=真”
选项“enum_style=java”
选项“generate_intdefs=true”
选项“generate_equals=true”//
// 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);
  }
}
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
                }
            }
        }
    }
}