public void testRunReport() throws Exception {
    MockMartusApp app = MockMartusApp.create();
    app.getLocalization().setCurrentLanguageCode(MiniLocalization.ENGLISH);
    app.loadSampleData();
    BulletinStore store = app.getStore();
    ReportFormat rf = new ReportFormat();
    rf.setDetailSection("$i. $bulletin.localId\n");
    ReportOutput result = new ReportOutput();
    Set leafUids = store.getAllBulletinLeafUids();
    SortableBulletinList list =
        new SortableBulletinList(app.getLocalization(), new MiniFieldSpec[0]);
    Iterator it = leafUids.iterator();
    while (it.hasNext()) {
      DatabaseKey key = DatabaseKey.createLegacyKey((UniversalId) it.next());
      list.add(BulletinLoader.loadFromDatabase(store.getDatabase(), key, app.getSecurity()));
    }

    RunReportOptions options = new RunReportOptions();
    rr.runReport(
        rf, store.getDatabase(), list, result, options, PoolOfReusableChoicesLists.EMPTY_POOL);
    result.close();
    StringBuffer expected = new StringBuffer();
    UniversalId[] uids = list.getSortedUniversalIds();
    for (int i = 0; i < uids.length; ++i) {
      expected.append(Integer.toString(i + 1));
      expected.append(". ");
      expected.append(uids[i].getLocalId());
      expected.append("\n");
    }
    assertEquals(new String(expected), result.getPageText(0));
  }
  public void testCustomField() throws Exception {
    FieldSpec[] specs =
        new FieldSpec[] {
          FieldSpec.createStandardField("date", new FieldTypeDate()),
          FieldSpec.createStandardField("text", new FieldTypeNormal()),
          FieldSpec.createStandardField("multi", new FieldTypeMultiline()),
          FieldSpec.createStandardField("range", new FieldTypeDateRange()),
          FieldSpec.createStandardField("bool", new FieldTypeBoolean()),
          FieldSpec.createStandardField("language", new FieldTypeLanguage()),
          LegacyCustomFields.createFromLegacy("custom,Custom <label>"),
        };

    MockMartusApp app = MockMartusApp.create();
    app.getLocalization().setCurrentLanguageCode(MiniLocalization.ENGLISH);
    Bulletin b =
        new Bulletin(app.getSecurity(), new FieldSpecCollection(specs), new FieldSpecCollection());
    String sampleCustomData = "Robert Plant";
    b.set("custom", sampleCustomData);
    b.setAllPrivate(false);
    app.saveBulletin(b, app.getFolderDraftOutbox());

    SortableBulletinList list =
        new SortableBulletinList(app.getLocalization(), new MiniFieldSpec[0]);
    list.add(b);
    ReportFormat rf = new ReportFormat();
    rf.setDetailSection("$bulletin.field('custom')");
    ReportOutput result = new ReportOutput();
    RunReportOptions options = new RunReportOptions();
    rr.runReport(
        rf,
        app.getStore().getDatabase(),
        list,
        result,
        options,
        PoolOfReusableChoicesLists.EMPTY_POOL);
    result.close();

    assertEquals(sampleCustomData, result.getPageText(0));
  }
  public void testOmitDetail() throws Exception {
    String sampleDate = "2004-06-19";
    MockMartusApp app = createAppWithBulletinsForBreaks(sampleDate);
    RunReportOptions options = new RunReportOptions();
    options.includePrivate = true;
    options.printBreaks = true;

    ReportFormat rf = new ReportFormat();
    rf.setDocumentStartSection("Start ");
    rf.setDetailSection("Detail ");
    rf.setBreakSection("Break ");
    rf.setHeaderSection("Header ");
    rf.setFooterSection("Footer ");
    rf.setTotalBreakSection("TotalBreak ");
    rf.setTotalSection("Total ");
    rf.setDocumentEndSection("End ");
    rf.setFakePageBreakSection(". ");

    ReportOutput sortByAuthorSummaryWithDetail = runReportOnAppData(rf, app, options);
    assertEquals(
        "Start Header Detail Detail Break Detail Break Break Detail Break Break TotalBreak Footer . End ",
        sortByAuthorSummaryWithDetail.getPrintableDocument());

    options.hideDetail = true;
    ReportOutput sortByAuthorSummaryWithoutDetail = runReportOnAppData(rf, app, options);
    assertEquals("Start Total . End ", sortByAuthorSummaryWithoutDetail.getPrintableDocument());

    rf.setBulletinPerPage(true);
    options.hideDetail = false;
    ReportOutput pageWithDetail = runReportOnAppData(rf, app, options);
    assertEquals(
        "Start Header Detail Footer . Header Detail Footer . Header Detail Footer . Header Detail Footer . Total . End ",
        pageWithDetail.getPrintableDocument());

    options.hideDetail = true;
    ReportOutput pageWithoutDetail = runReportOnAppData(rf, app, options);
    assertEquals("Start Total . End ", pageWithoutDetail.getPrintableDocument());
  }
  public void testPageReport() throws Exception {
    MockMartusApp app = MockMartusApp.create();
    FieldSpec[] topFields = {
      FieldSpec.createStandardField(Bulletin.TAGAUTHOR, new FieldTypeNormal()),
      FieldSpec.createCustomField("tag2", "Label 2", new FieldTypeDate()),
    };
    Bulletin b =
        new Bulletin(
            app.getSecurity(),
            new FieldSpecCollection(topFields),
            StandardFieldSpecs.getDefaultBottomSectionFieldSpecs());
    b.set(topFields[0].getTag(), "First");
    b.set(topFields[1].getTag(), "2005-04-07");
    b.set(Bulletin.TAGPRIVATEINFO, "Secret");
    app.saveBulletin(b, app.getFolderDraftOutbox());

    Bulletin b2 =
        new Bulletin(
            app.getSecurity(),
            new FieldSpecCollection(topFields),
            StandardFieldSpecs.getDefaultBottomSectionFieldSpecs());
    b2.set(topFields[0].getTag(), "Second");
    b2.set(topFields[1].getTag(), "2003-03-29");
    b2.set(Bulletin.TAGPRIVATEINFO, "Another secret");
    app.saveBulletin(b2, app.getFolderDraftOutbox());

    ReportFormat rf = new ReportFormat();
    rf.setBulletinPerPage(true);
    rf.setHeaderSection("Header\n");
    rf.setFooterSection("Footer\n");
    rf.setFakePageBreakSection("----\n");
    rf.setDetailSection(
        "TOP:\n"
            + "#foreach($field in $bulletin.getTopFields())\n"
            + "$field.getLocalizedLabel($localization) $field.html($localization)\n"
            + "#end\n"
            + "BOTTOM:\n"
            + "#foreach($field in $bulletin.getBottomFields())\n"
            + "$field.getLocalizedLabel($localization) $field.html($localization)\n"
            + "#end\n"
            + "");
    String expected0 =
        "Header\n"
            + "TOP:\n"
            + "<field:author> First\n"
            + "Label 2 04/07/2005\n"
            + "BOTTOM:\n"
            + "<field:privateinfo> Secret\n"
            + "Footer\n";
    String expected1 =
        "Header\n"
            + "TOP:\n"
            + "<field:author> Second\n"
            + "Label 2 03/29/2003\n"
            + "BOTTOM:\n"
            + "<field:privateinfo> Another secret\n"
            + "Footer\n";

    RunReportOptions options = new RunReportOptions();
    options.includePrivate = true;
    ReportOutput result = runReportOnAppData(rf, app, options);
    assertEquals("Wrong page report output?", expected0, result.getPageText(0));
    assertEquals("Wrong page report output?", expected1, result.getPageText(1));
    assertEquals("Didn't set fake page break?", "----\n", result.getFakePageBreak());
  }