2

我正在使用proto3Android 应用程序,但我遇到了对象相等问题,这使得测试非常困难,尤其是对于验证方法

这是代表问题的单元测试:

@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

}

很明显,该方法equals无法正常工作,检查生成的代码(附在底部)没有相等,生成哈希码。我可以assertThat通过 using.toString方法解决这个问题,但我找不到任何其他验证方式,例如。verify(anyMock).anyMethod(playerCards)

如果检查时不是非常小心,恐怕这也可能会影响我的运行时间。

  • 有没有办法生成equals, hashcode?
  • 如果没有,我至少可以扩展、覆盖verify以便toString在检查原始生成的对象时使用吗?

代码片段:

我的原型文件是:

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;
}

我通过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);
  }
}
4

1 回答 1

1

证明缺少文件。

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

将此添加到 gradle 生成选项后equalshashcode生成的方法!

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
                }
            }
        }
    }
}
于 2017-01-06T23:38:03.257 回答