BibTeXConverter { public Map<String, CSLItemData> toItemData(BibTeXDatabase db) { Map<String, CSLItemData> result = new LinkedHashMap<>(); for (Map.Entry<Key, BibTeXEntry> e : db.getEntries().entrySet()) { result.put(e.getKey().getValue(), toItemData(e.getValue())); } return result; } BibTeXConverter(); BibTeXDatabase loadDatabase(InputStream is); Map<String, CSLItemData> toItemData(BibTeXDatabase db); CSLItemData toItemData(BibTeXEntry e); CSLType toType(Key type);  }
@Test public void singleEntry() throws Exception { BibTeXDatabase db = loadUnixDatabase(); BibTeXEntry e = db.resolveEntry(new Key("Ritchie:1974:UTS")); BibTeXConverter conv = new BibTeXConverter(); CSLItemData cid = conv.toItemData(e); assertEquals("Ritchie:1974:UTS", cid.getId()); assertEquals(CSLType.ARTICLE_JOURNAL, cid.getType()); assertEquals(2, cid.getAuthor().length); assertEquals("Ritchie", cid.getAuthor()[0].getFamily()); assertEquals("Dennis W.", cid.getAuthor()[0].getGiven()); assertEquals("Thompson", cid.getAuthor()[1].getFamily()); assertEquals("Ken", cid.getAuthor()[1].getGiven()); assertEquals("Communications of the Association for Computing Machinery", cid.getCollectionTitle()); assertEquals("Communications of the Association for Computing Machinery", cid.getContainerTitle()); assertEquals("17", cid.getVolume()); assertEquals("7", cid.getIssue()); assertEquals("7", cid.getNumber()); assertEquals("11", cid.getNumberOfPages()); assertEquals("365-375", cid.getPage()); assertEquals("365", cid.getPageFirst()); assertEquals("The UNIX Time-Sharing System", cid.getTitle()); assertArrayEquals(new int[][] { new int[] { 1974, 7 } }, cid.getIssued().getDateParts()); }
@Test public void singleEntryWithDateRange() throws Exception { BibTeXDatabase db = loadUnixDatabase(); BibTeXEntry e = db.resolveEntry(new Key("Lycklama:1978:UTSb")); BibTeXConverter conv = new BibTeXConverter(); CSLItemData cid = conv.toItemData(e); assertEquals("Lycklama:1978:UTSb", cid.getId()); assertEquals(CSLType.ARTICLE_JOURNAL, cid.getType()); assertEquals(1, cid.getAuthor().length); assertEquals("Lycklama", cid.getAuthor()[0].getFamily()); assertEquals("H.", cid.getAuthor()[0].getGiven()); assertEquals("The Bell System Technical Journal", cid.getCollectionTitle()); assertEquals("The Bell System Technical Journal", cid.getContainerTitle()); assertEquals("57", cid.getVolume()); assertEquals("6", cid.getIssue()); assertEquals("6", cid.getNumber()); assertEquals("15", cid.getNumberOfPages()); assertEquals("2087-2101", cid.getPage()); assertEquals("2087", cid.getPageFirst()); assertEquals("UNIX Time-Sharing System: UNIX on a Microprocessor", cid.getTitle()); assertArrayEquals(new int[][] { new int[] { 1978, 7 }, new int[] { 1978, 8 } }, cid.getIssued().getDateParts()); }
@Test public void allEntries() throws Exception { BibTeXDatabase db = loadUnixDatabase(); BibTeXConverter conv = new BibTeXConverter(); Map<String, CSLItemData> cids = conv.toItemData(db); CSLItemData cid = cids.get("Ritchie:1974:UTS"); assertEquals("Ritchie:1974:UTS", cid.getId()); assertEquals(CSLType.ARTICLE_JOURNAL, cid.getType()); assertEquals(2, cid.getAuthor().length); assertEquals("Ritchie", cid.getAuthor()[0].getFamily()); assertEquals("Dennis W.", cid.getAuthor()[0].getGiven()); assertEquals("Thompson", cid.getAuthor()[1].getFamily()); assertEquals("Ken", cid.getAuthor()[1].getGiven()); assertEquals("Communications of the Association for Computing Machinery", cid.getCollectionTitle()); assertEquals("Communications of the Association for Computing Machinery", cid.getContainerTitle()); assertEquals("17", cid.getVolume()); assertEquals("7", cid.getIssue()); assertEquals("7", cid.getNumber()); assertEquals("11", cid.getNumberOfPages()); assertEquals("365-375", cid.getPage()); assertEquals("365", cid.getPageFirst()); assertEquals("The UNIX Time-Sharing System", cid.getTitle()); assertArrayEquals(new int[][] { new int[] { 1974, 7 } }, cid.getIssued().getDateParts()); }
@Test public void preserveOrder() throws Exception { BibTeXDatabase db = loadUnixDatabase(); BibTeXConverter conv = new BibTeXConverter(); Map<String, CSLItemData> cids = conv.toItemData(db); Iterator<Key> ik1 = db.getEntries().keySet().iterator(); Iterator<String> ik2 = cids.keySet().iterator(); while (ik1.hasNext() && ik2.hasNext()) { assertEquals(ik1.next().getValue(), ik2.next()); } assertFalse(ik1.hasNext()); assertFalse(ik2.hasNext()); }
@Test public void carriageReturnInTitle() { BibTeXEntry e = new BibTeXEntry(new Key("article"), new Key("a")); e.addField(new Key("title"), new StringValue( "syst\\`emes\r\ndiff\\'erentiels", StringValue.Style.QUOTED)); BibTeXConverter conv = new BibTeXConverter(); CSLItemData i = conv.toItemData(e); assertEquals("systèmes différentiels", i.getTitle()); }
@Test public void shouldReadTheLanguageFieldOfASingleEntry() throws IOException, ParseException { BibTeXDatabase db = loadUnixDatabase(); BibTeXEntry e = db.resolveEntry(new Key("Bach:1986:UTS")); BibTeXConverter conv = new BibTeXConverter(); CSLItemData cid = conv.toItemData(e); assertEquals("Bach:1986:UTS", cid.getId()); assertEquals("German", cid.getLanguage()); }
@Test public void shouldSetGenreFromTypeForTheses() throws IOException, ParseException { BibTeXDatabase db = loadUnixDatabase(); BibTeXEntry e = db.resolveEntry(new Key("Wang:2002:DIR")); BibTeXConverter conv = new BibTeXConverter(); CSLItemData cid = conv.toItemData(e); assertEquals("CSL 'type' not set", CSLType.THESIS, cid.getType()); assertEquals("CSL 'genre' not set", "Thesis (Ph.D.)", cid.getGenre()); }