@Override
  public void publish(ScannerReportWriter writer) {
    ScannerReport.Component.Builder projectBuilder = prepareProjectBuilder();

    ScannerReport.Component.Builder fileBuilder = ScannerReport.Component.newBuilder();
    for (DefaultInputFile file : inputComponentStore.allFilesToPublish()) {
      projectBuilder.addChildRef(file.scannerId());

      fileBuilder.clear();

      // non-null fields
      fileBuilder.setRef(file.scannerId());
      fileBuilder.setType(ComponentType.FILE);

      fileBuilder.setIsTest(file.type() == InputFile.Type.TEST);
      fileBuilder.setLines(file.lines());
      fileBuilder.setStatus(convert(file.status()));
      fileBuilder.setMarkedAsUnchanged(file.isMarkedAsUnchanged());

      String oldRelativePath = file.oldRelativePath();
      if (oldRelativePath != null) {
        fileBuilder.setOldRelativeFilePath(oldRelativePath);
      }

      String lang = getLanguageKey(file);
      if (lang != null) {
        fileBuilder.setLanguage(lang);
      }
      fileBuilder.setProjectRelativePath(file.getProjectRelativePath());
      writer.writeComponent(fileBuilder.build());
    }

    writer.writeComponent(projectBuilder.build());
  }
@Test
  public void publish_project_with_links() throws Exception {
    ProjectInfo projectInfo = mock(ProjectInfo.class);
    when(projectInfo.getAnalysisDate()).thenReturn(DateUtils.parseDate("2012-12-12"));

    ProjectDefinition rootDef = ProjectDefinition.create()
      .setKey("foo")
      .setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "1.0")
      .setName("Root project")
      .setProperty(CoreProperties.LINKS_HOME_PAGE, "http://home")
      .setProperty(CoreProperties.LINKS_CI, "http://ci")
      .setDescription("Root description")
      .setBaseDir(temp.newFolder())
      .setWorkDir(temp.newFolder());
    DefaultInputProject project = new DefaultInputProject(rootDef, 1);

    InputComponentStore store = new InputComponentStore(branchConfiguration, sonarRuntime);
    ComponentsPublisher publisher = new ComponentsPublisher(project, store);
    publisher.publish(writer);

    ScannerReportReader reader = new ScannerReportReader(fileStructure);
    Component rootProtobuf = reader.readComponent(1);
    assertThat(rootProtobuf.getLinkCount()).isEqualTo(2);
    assertThat(rootProtobuf.getLink(0).getType()).isEqualTo(ComponentLinkType.HOME);
    assertThat(rootProtobuf.getLink(0).getHref()).isEqualTo("http://home");
    assertThat(rootProtobuf.getLink(1).getType()).isEqualTo(ComponentLinkType.CI);
    assertThat(rootProtobuf.getLink(1).getHref()).isEqualTo("http://ci");
  }