@Override
  public void write(ProjectDump.Metadata metadata) {
    checkNotPublished();
    if (metadataWritten.get()) {
      throw new IllegalStateException("Metadata has already been written");
    }
    File file = new File(rootDir, METADATA.filename());
    try (FileOutputStream output = FILES2.openOutputStream(file, false)) {
      PROTOBUF2.writeTo(metadata, output);
      metadataWritten.set(true);
    } catch (IOException e) {
      throw new IllegalStateException("Can not write to file " + file, e);
    }
  }
@Test
  public void writeMetadata_fails_if_called_twice() {
    underTest.write(newMetadata());

    assertThatThrownBy(() -> underTest.write(newMetadata()))
      .isInstanceOf(IllegalStateException.class)
      .hasMessage("Metadata has already been written");
  }