@Test
  public void testPublishingAfterVersionDelete() {
    DocumentModel folder = session.createDocumentModel("/", "folder", "Folder");
    folder = session.createDocument(folder);
    DocumentModel doc = session.createDocumentModel("/", "file", "File");
    doc = session.createDocument(doc);
    checkVersions(doc);

    VersionModel lastVersion = session.getLastVersion(doc.getRef());
    assertNull(lastVersion);
    DocumentModel lastVersionDocument = session.getLastDocumentVersion(doc.getRef());
    assertNull(lastVersionDocument);

    // publish
    DocumentModel proxy = session.publishDocument(doc, folder);
    checkVersions(doc, "0.1");
    lastVersion = session.getLastVersion(doc.getRef());
    assertNotNull(lastVersion);
    assertEquals("0.1", lastVersion.getLabel());
    lastVersionDocument = session.getLastDocumentVersion(doc.getRef());
    assertNotNull(lastVersionDocument);
    assertEquals("file", lastVersionDocument.getName());

    // unpublish
    session.removeDocument(proxy.getRef());
    // delete the version
    List<VersionModel> versions = session.getVersionsForDocument(doc.getRef());
    assertEquals(1, versions.size());
    DocumentModel docVersion = session.getDocumentWithVersion(doc.getRef(), versions.get(0));
    session.removeDocument(docVersion.getRef());

    checkVersions(doc);
    lastVersion = session.getLastVersion(doc.getRef());
    assertNull(lastVersion);
    lastVersionDocument = session.getLastDocumentVersion(doc.getRef());
    assertNull(lastVersionDocument);

    // republish
    DocumentModel newProxy = session.publishDocument(doc, folder);
    checkVersions(doc, "0.2");
    lastVersion = session.getLastVersion(doc.getRef());
    assertNotNull(lastVersion);
    assertEquals("0.2", lastVersion.getLabel());
    lastVersionDocument = session.getLastDocumentVersion(doc.getRef());
    assertNotNull(lastVersionDocument);
    assertEquals("file", lastVersionDocument.getName());
  }
 private void checkVersions(DocumentModel doc, String... labels) {
   List<String> actual = new LinkedList<String>();
   for (DocumentModel ver : session.getVersions(doc.getRef())) {
     assertTrue(ver.isVersion());
     actual.add(ver.getVersionLabel());
   }
   // build a debug list of versions and creation times
   // in case of failure
   StringBuilder buf = new StringBuilder("version time: ");
   for (VersionModel vm : session.getVersionsForDocument(doc.getRef())) {
     buf.append(vm.getLabel());
     buf.append("=");
     buf.append(vm.getCreated().getTimeInMillis());
     buf.append(", ");
   }
   buf.setLength(buf.length() - 2);
   assertEquals(buf.toString(), Arrays.asList(labels), actual);
   List<DocumentRef> versionsRefs = session.getVersionsRefs(doc.getRef());
   assertEquals(labels.length, versionsRefs.size());
 }