@Test
  public void writeGroupsAndEncoding() throws Exception {
    SavePreferences preferences = new SavePreferences().withEncoding(Charsets.US_ASCII);

    GroupTreeNode groupRoot = GroupTreeNode.fromGroup(new AllEntriesGroup(""));
    groupRoot.addChild(
        GroupTreeNode.fromGroup(new ExplicitGroup("test", GroupHierarchyType.INCLUDING, ',')));
    metaData.setGroups(groupRoot);

    StringSaveSession session =
        databaseWriter.savePartOfDatabase(bibtexContext, Collections.emptyList(), preferences);

    // @formatter:off
    assertEquals(
        "% Encoding: US-ASCII"
            + OS.NEWLINE
            + OS.NEWLINE
            + "@Comment{jabref-meta: groupstree:"
            + OS.NEWLINE
            + "0 AllEntriesGroup:;"
            + OS.NEWLINE
            + "1 ExplicitGroup:test\\;2\\;;"
            + OS.NEWLINE
            + "}"
            + OS.NEWLINE,
        session.getStringValue());
    // @formatter:on
  }
  @Test
  public void writeFileDirectories() throws Exception {
    metaData.setDefaultFileDirectory("\\Literature\\");
    metaData.setUserFileDirectory("defaultOwner-user", "D:\\Documents");

    StringSaveSession session =
        databaseWriter.savePartOfDatabase(
            bibtexContext, Collections.emptyList(), new SavePreferences());

    assertEquals(
        OS.NEWLINE
            + "@Comment{jabref-meta: fileDirectory:\\\\Literature\\\\;}"
            + OS.NEWLINE
            + OS.NEWLINE
            + "@Comment{jabref-meta: fileDirectory-defaultOwner-user:D:\\\\Documents;}"
            + OS.NEWLINE,
        session.getStringValue());
  }
  @Test
  public void writeProtectedFlag() throws Exception {
    metaData.markAsProtected();

    StringSaveSession session =
        databaseWriter.savePartOfDatabase(
            bibtexContext, Collections.emptyList(), new SavePreferences());

    assertEquals(
        OS.NEWLINE + "@Comment{jabref-meta: protectedFlag:true;}" + OS.NEWLINE,
        session.getStringValue());
  }
  @Test
  public void writeBiblatexMode() throws Exception {
    metaData.setMode(BibDatabaseMode.BIBLATEX);

    StringSaveSession session =
        databaseWriter.savePartOfDatabase(
            bibtexContext, Collections.emptyList(), new SavePreferences());

    assertEquals(
        OS.NEWLINE + "@Comment{jabref-meta: databaseType:biblatex;}" + OS.NEWLINE,
        session.getStringValue());
  }
  @Test
  public void writeMetadata() throws Exception {
    DatabaseBibtexKeyPattern bibtexKeyPattern = new DatabaseBibtexKeyPattern(prefs.getKeyPattern());
    bibtexKeyPattern.setDefaultValue("test");
    metaData.setCiteKeyPattern(bibtexKeyPattern);

    StringSaveSession session =
        databaseWriter.savePartOfDatabase(
            bibtexContext, Collections.emptyList(), new SavePreferences());

    assertEquals(
        OS.NEWLINE + "@Comment{jabref-meta: keypatterndefault:test;}" + OS.NEWLINE,
        session.getStringValue());
  }
  @Test
  public void writeMetadataAndEncoding() throws Exception {
    SavePreferences preferences = new SavePreferences().withEncoding(Charsets.US_ASCII);
    DatabaseBibtexKeyPattern bibtexKeyPattern = new DatabaseBibtexKeyPattern(prefs.getKeyPattern());
    bibtexKeyPattern.setDefaultValue("test");
    metaData.setCiteKeyPattern(bibtexKeyPattern);

    StringSaveSession session =
        databaseWriter.savePartOfDatabase(bibtexContext, Collections.emptyList(), preferences);

    assertEquals(
        "% Encoding: US-ASCII"
            + OS.NEWLINE
            + OS.NEWLINE
            + "@Comment{jabref-meta: keypatterndefault:test;}"
            + OS.NEWLINE,
        session.getStringValue());
  }
  @Test
  public void writeSaveOrderConfig() throws Exception {
    SaveOrderConfig saveOrderConfig =
        new SaveOrderConfig(
            false,
            new SaveOrderConfig.SortCriterion("author", false),
            new SaveOrderConfig.SortCriterion("year", true),
            new SaveOrderConfig.SortCriterion("abstract", false));
    metaData.setSaveOrderConfig(saveOrderConfig);

    StringSaveSession session =
        databaseWriter.savePartOfDatabase(
            bibtexContext, Collections.emptyList(), new SavePreferences());

    assertEquals(
        OS.NEWLINE
            + "@Comment{jabref-meta: saveOrderConfig:specified;author;false;year;true;abstract;false;}"
            + OS.NEWLINE,
        session.getStringValue());
  }
  @Test
  public void writeSaveActions() throws Exception {
    FieldFormatterCleanups saveActions =
        new FieldFormatterCleanups(
            true,
            Collections.singletonList(
                new FieldFormatterCleanup("title", new LowerCaseFormatter())));
    metaData.setSaveActions(saveActions);

    StringSaveSession session =
        databaseWriter.savePartOfDatabase(
            bibtexContext, Collections.emptyList(), new SavePreferences());

    assertEquals(
        OS.NEWLINE
            + "@Comment{jabref-meta: saveActions:enabled;"
            + OS.NEWLINE
            + "title[lower_case]"
            + OS.NEWLINE
            + ";}"
            + OS.NEWLINE,
        session.getStringValue());
  }
  @Test
  public void writeEntriesSorted() throws Exception {
    SaveOrderConfig saveOrderConfig =
        new SaveOrderConfig(
            false,
            new SaveOrderConfig.SortCriterion("author", false),
            new SaveOrderConfig.SortCriterion("year", true),
            new SaveOrderConfig.SortCriterion("abstract", false));
    metaData.setSaveOrderConfig(saveOrderConfig);

    BibEntry firstEntry = new BibEntry();
    firstEntry.setType(BibtexEntryTypes.ARTICLE);
    firstEntry.setField("author", "A");
    firstEntry.setField("year", "2000");

    BibEntry secondEntry = new BibEntry();
    secondEntry.setType(BibtexEntryTypes.ARTICLE);
    secondEntry.setField("author", "A");
    secondEntry.setField("year", "2010");

    BibEntry thirdEntry = new BibEntry();
    thirdEntry.setType(BibtexEntryTypes.ARTICLE);
    thirdEntry.setField("author", "B");
    thirdEntry.setField("year", "2000");

    database.insertEntry(secondEntry);
    database.insertEntry(thirdEntry);
    database.insertEntry(firstEntry);

    StringSaveSession session =
        databaseWriter.savePartOfDatabase(
            bibtexContext, database.getEntries(), new SavePreferences());

    assertEquals(
        OS.NEWLINE
            + "@Article{,"
            + OS.NEWLINE
            + "  author = {A},"
            + OS.NEWLINE
            + "  year   = {2000},"
            + OS.NEWLINE
            + "}"
            + OS.NEWLINE
            + OS.NEWLINE
            + "@Article{,"
            + OS.NEWLINE
            + "  author = {A},"
            + OS.NEWLINE
            + "  year   = {2010},"
            + OS.NEWLINE
            + "}"
            + OS.NEWLINE
            + OS.NEWLINE
            + "@Article{,"
            + OS.NEWLINE
            + "  author = {B},"
            + OS.NEWLINE
            + "  year   = {2000},"
            + OS.NEWLINE
            + "}"
            + OS.NEWLINE
            + OS.NEWLINE
            + "@Comment{jabref-meta: databaseType:bibtex;}"
            + OS.NEWLINE
            + OS.NEWLINE
            + "@Comment{jabref-meta: saveOrderConfig:specified;author;false;year;true;abstract;false;}"
            + OS.NEWLINE,
        session.getStringValue());
  }