diff --git a/src/main/java/net/sf/jabref/logic/util/strings/StringUtil.java b/src/main/java/net/sf/jabref/logic/util/strings/StringUtil.java index 86a6e3bdf6c..12ff8c21e8a 100644 --- a/src/main/java/net/sf/jabref/logic/util/strings/StringUtil.java +++ b/src/main/java/net/sf/jabref/logic/util/strings/StringUtil.java @@ -23,6 +23,7 @@ import java.util.regex.Pattern; import net.sf.jabref.Globals; +import net.sf.jabref.model.entry.Author; import com.google.common.base.CharMatcher; @@ -625,6 +626,51 @@ public static String replaceSpecialCharacters(String s) { return result; } + /** + * Expand initials, e.g. EH Wissler -> E. H. Wissler or Wissler, EH -> Wissler, E. H. + * + * @param name + * @return The name after expanding initials. + */ + public static String expandAuthorInitials(String name) { + String[] authors = name.split(" and "); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < authors.length; i++) { + if (authors[i].contains(", ")) { + String[] names = authors[i].split(", "); + if (names.length > 0) { + sb.append(names[0]); + if (names.length > 1) { + sb.append(", "); + } + } + for (int j = 1; j < names.length; j++) { + if (j == 1) { + sb.append(Author.addDotIfAbbreviation(names[j])); + } else { + sb.append(names[j]); + } + if (j < (names.length - 1)) { + sb.append(", "); + } + } + } else { + String[] names = authors[i].split(" "); + if (names.length > 0) { + sb.append(Author.addDotIfAbbreviation(names[0])); + } + for (int j = 1; j < names.length; j++) { + sb.append(' '); + sb.append(names[j]); + } + } + if (i < (authors.length - 1)) { + sb.append(" and "); + } + } + return sb.toString().trim(); + } + /** * Return a String with n spaces * diff --git a/src/test/java/net/sf/jabref/importer/fileformat/MedlineImporterTest.java b/src/test/java/net/sf/jabref/importer/fileformat/MedlineImporterTest.java new file mode 100644 index 00000000000..5a16d5c1f1b --- /dev/null +++ b/src/test/java/net/sf/jabref/importer/fileformat/MedlineImporterTest.java @@ -0,0 +1,100 @@ +package net.sf.jabref.importer.fileformat; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +import net.sf.jabref.Globals; +import net.sf.jabref.JabRefPreferences; +import net.sf.jabref.importer.OutputPrinterToNull; +import net.sf.jabref.model.entry.BibEntry; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; + +import static org.junit.Assert.assertEquals; + +/** + * Articles in the medline format can be downloaded from http://www.ncbi.nlm.nih.gov/pubmed/. + * 1. Search for a term and make sure you have selected the PubMed database + * 2. Select the results you want to export by checking their checkboxes + * 3. Press on the 'Send to' drop down menu on top of the search results + * 4. Select 'File' as Destination and 'XML' as Format + * 5. Press 'Create File' to download your search results in a medline xml file + * + * @author Daniel Mair/Bruehl + * + */ +@RunWith(MockitoJUnitRunner.class) +public class MedlineImporterTest { + + private MedlineImporter medlineImporter; + private static final String FILEFORMAT_PATH = "src/test/resources/net/sf/jabref/importer/fileformat"; + + /** + * Generates a List of all files in the package "/src/test/resources/net/sf/jabref/importer/fileformat" + * @return A list of Names + * @throws IOException + */ + public List getTestFiles() throws IOException { + List files = new ArrayList<>(); + try (DirectoryStream stream = Files.newDirectoryStream(Paths.get(FILEFORMAT_PATH))) { + stream.forEach(n -> files.add(n.getFileName().toString())); + } + return files; + } + + @Before + public void setUp() throws Exception { + Globals.prefs = JabRefPreferences.getInstance(); + this.medlineImporter = new MedlineImporter(); + } + + @Test + public void testExceptionOnInputStream() throws IOException { + try (InputStream is = Mockito.mock(InputStream.class)) { + Mockito.doThrow(new IOException()).when(is).read(); + List entry = medlineImporter.importEntries(is, new OutputPrinterToNull()); + Assert.assertTrue(entry.isEmpty()); + } + } + + @Test + public void testGetItemsEmpty() { + MedlineHandler handler = new MedlineHandler(); + assertEquals(Collections.emptyList(), handler.getItems()); + } + + @Test + public void testGetFormatName() { + assertEquals("Medline", medlineImporter.getFormatName()); + } + + @Test + public void testGetCLIId() { + assertEquals("medline", medlineImporter.getCLIId()); + } + + @Test + public void testIsRecognizedFormatReject() throws IOException { + List list = getTestFiles().stream().filter(n -> !n.startsWith("MedlineImporter")) + .collect(Collectors.toList()); + + for (String str : list) { + try (InputStream is = MedlineImporter.class.getResourceAsStream(str)) { + Assert.assertFalse(medlineImporter.isRecognizedFormat(is)); + } + } + } +} diff --git a/src/test/java/net/sf/jabref/importer/fileformat/MedlineImporterTestFiles.java b/src/test/java/net/sf/jabref/importer/fileformat/MedlineImporterTestFiles.java new file mode 100644 index 00000000000..333bbb6330b --- /dev/null +++ b/src/test/java/net/sf/jabref/importer/fileformat/MedlineImporterTestFiles.java @@ -0,0 +1,79 @@ +package net.sf.jabref.importer.fileformat; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +import net.sf.jabref.Globals; +import net.sf.jabref.JabRefPreferences; +import net.sf.jabref.importer.OutputPrinterToNull; +import net.sf.jabref.logic.bibtex.BibEntryAssert; +import net.sf.jabref.model.entry.BibEntry; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +import static org.junit.Assert.assertEquals; + +@RunWith(Parameterized.class) +public class MedlineImporterTestFiles { + + private final static String FILEFORMAT_PATH = "src/test/resources/net/sf/jabref/importer/fileformat"; + + private MedlineImporter medlineImporter; + + @Parameter + public String fileName; + + + @Before + public void setUp() { + Globals.prefs = JabRefPreferences.getInstance(); + medlineImporter = new MedlineImporter(); + } + + @Parameters(name = "{0}") + public static Collection fileNames() throws IOException { + List files = new ArrayList<>(); + try (DirectoryStream stream = Files.newDirectoryStream(Paths.get(FILEFORMAT_PATH))) { + stream.forEach(n -> files.add(n.getFileName().toString())); + } + return files.stream().filter(n -> n.startsWith("MedlineImporterTest")).filter(n -> n.endsWith(".xml")) + .collect(Collectors.toList()); + } + + @Test + public void testIsRecognizedFormat() throws IOException { + try (InputStream stream = MedlineImporterTest.class.getResourceAsStream(fileName)) { + Assert.assertTrue(medlineImporter.isRecognizedFormat(stream)); + } + } + + @Test + @Ignore + public void testImportEntries() throws IOException { + try (InputStream inputStream = MedlineImporterTest.class.getResourceAsStream(fileName)) { + List medlineEntries = medlineImporter.importEntries(inputStream, new OutputPrinterToNull()); + String bibFileName = fileName.replace(".xml", ".bib"); + if (medlineEntries.isEmpty()) { + assertEquals(Collections.emptyList(), medlineEntries); + } else { + BibEntryAssert.assertEquals(MedlineImporterTest.class, bibFileName, medlineEntries); + } + } + } +} \ No newline at end of file diff --git a/src/test/resources/net/sf/jabref/importer/fileformat/MedlineImporterTestArticleID.bib b/src/test/resources/net/sf/jabref/importer/fileformat/MedlineImporterTestArticleID.bib new file mode 100644 index 00000000000..d0414750291 --- /dev/null +++ b/src/test/resources/net/sf/jabref/importer/fileformat/MedlineImporterTestArticleID.bib @@ -0,0 +1,14 @@ +% Encoding: UTF-8 + +@article{, + abstract = {This abstract is a dummy.}, + author = {Rhein, Joshua and Bahr, Nathan C. and Hemmert, Andrew C. and Cloud, Joann L. and Bellamkonda, Satya and Oswald, Cody and Lo, Eric and Nabeta, Henry and Kiggundu, Reuben and Akampurira, Andrew and Musubire, Abdu and Williams, Darlisha A. and Meya, David B. and Boulware, David R. and , A. S. T. R. O.-C. M. Team}, + institution = {Division of Infectious Disease and International Health, Department of Medicine, University of Minnesota, Minneapolis, MN, USA.}, + journal = {Diagn Microbiol Infect Dis}, + language = {eng}, + medline-pst = {aheadofprint}, + month = {Dec}, + pmid = {26711635}, + title = {Diagnostic performance of a multiplex PCR assay for meningitis in an HIV-infected population in Uganda.}, + year = {2015} +} \ No newline at end of file diff --git a/src/test/resources/net/sf/jabref/importer/fileformat/MedlineImporterTestArticleID.xml b/src/test/resources/net/sf/jabref/importer/fileformat/MedlineImporterTestArticleID.xml new file mode 100644 index 00000000000..3b64f43676f --- /dev/null +++ b/src/test/resources/net/sf/jabref/importer/fileformat/MedlineImporterTestArticleID.xml @@ -0,0 +1,230 @@ + + + + + + + 26711635 + + 2015 + 12 + 29 + + + 2015 + 12 + 30 + +
+ + 1879-0070 + + + 2015 + Dec + 1 + + + Diagnostic microbiology and infectious disease + Diagn. Microbiol. Infect. Dis. + + Diagnostic performance of a multiplex PCR assay for meningitis in an HIV-infected population in Uganda. + + + + S0732-8893(15)00432-0 + 10.1016/j.diagmicrobio.2015.11.017 + + This abstract is a dummy. + + + + Rhein + Joshua + J + + Division of Infectious Disease and International Health, Department of Medicine, University of Minnesota, Minneapolis, MN, USA; Infectious Disease Institute, Makerere University, Kampala, Uganda. Electronic address: joshua.rhein@gmail.com. + + + + Bahr + Nathan C + NC + + Division of Infectious Disease and International Health, Department of Medicine, University of Minnesota, Minneapolis, MN, USA. + + + + Hemmert + Andrew C + AC + + BioFire Diagnostics, LLC, Salt Lake City, UT, USA. + + + + Cloud + Joann L + JL + + BioFire Diagnostics, LLC, Salt Lake City, UT, USA. + + + + Bellamkonda + Satya + S + + BioFire Diagnostics, LLC, Salt Lake City, UT, USA. + + + + Oswald + Cody + C + + BioFire Diagnostics, LLC, Salt Lake City, UT, USA. + + + + Lo + Eric + E + + BioFire Diagnostics, LLC, Salt Lake City, UT, USA. + + + + Nabeta + Henry + H + + Infectious Disease Institute, Makerere University, Kampala, Uganda. + + + + Kiggundu + Reuben + R + + Infectious Disease Institute, Makerere University, Kampala, Uganda. + + + + Akampurira + Andrew + A + + Infectious Disease Institute, Makerere University, Kampala, Uganda. + + + + Musubire + Abdu + A + + Division of Infectious Disease and International Health, Department of Medicine, University of Minnesota, Minneapolis, MN, USA; Infectious Disease Institute, Makerere University, Kampala, Uganda. + + + + Williams + Darlisha A + DA + + Division of Infectious Disease and International Health, Department of Medicine, University of Minnesota, Minneapolis, MN, USA; Infectious Disease Institute, Makerere University, Kampala, Uganda. + + + + Meya + David B + DB + + Division of Infectious Disease and International Health, Department of Medicine, University of Minnesota, Minneapolis, MN, USA; Infectious Disease Institute, Makerere University, Kampala, Uganda. + + + + Boulware + David R + DR + + Division of Infectious Disease and International Health, Department of Medicine, University of Minnesota, Minneapolis, MN, USA. + + + + ASTRO-CM Team + + + ENG + + JOURNAL ARTICLE + + + 2015 + 12 + 1 + +
+ + Diagn Microbiol Infect Dis + 8305899 + 0732-8893 + + + Cryptococcal meningitis + Diagnostics + HIV + Immunocompromised + Meningitis + PCR + +
+ + + + 2015 + 7 + 21 + + + 2015 + 11 + 17 + + + 2015 + 11 + 18 + + + 2015 + 12 + 30 + 6 + 0 + + + 2015 + 12 + 30 + 6 + 0 + + + 2015 + 12 + 30 + 6 + 0 + + + aheadofprint + + S0732-8893(15)00432-0 + 10.1016/j.diagmicrobio.2015.11.017 + 26711635 + + +
+ + +
\ No newline at end of file diff --git a/src/test/resources/net/sf/jabref/importer/fileformat/MedlineImporterTestMalformedEntry.xml b/src/test/resources/net/sf/jabref/importer/fileformat/MedlineImporterTestMalformedEntry.xml new file mode 100644 index 00000000000..098eac24b63 --- /dev/null +++ b/src/test/resources/net/sf/jabref/importer/fileformat/MedlineImporterTestMalformedEntry.xml @@ -0,0 +1,4 @@ + + + wrong format + \ No newline at end of file diff --git a/src/test/resources/net/sf/jabref/importer/fileformat/MedlineImporterTestMinimalEntry.bib b/src/test/resources/net/sf/jabref/importer/fileformat/MedlineImporterTestMinimalEntry.bib new file mode 100644 index 00000000000..d1f3b6ffbc8 --- /dev/null +++ b/src/test/resources/net/sf/jabref/importer/fileformat/MedlineImporterTestMinimalEntry.bib @@ -0,0 +1,4 @@ +% Encoding: UTF-8 + +@Article{, +} \ No newline at end of file diff --git a/src/test/resources/net/sf/jabref/importer/fileformat/MedlineImporterTestMinimalEntry.xml b/src/test/resources/net/sf/jabref/importer/fileformat/MedlineImporterTestMinimalEntry.xml new file mode 100644 index 00000000000..65fca5c28a4 --- /dev/null +++ b/src/test/resources/net/sf/jabref/importer/fileformat/MedlineImporterTestMinimalEntry.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/test/resources/net/sf/jabref/importer/fileformat/MedlineImporterTestSingleEntry.bib b/src/test/resources/net/sf/jabref/importer/fileformat/MedlineImporterTestSingleEntry.bib new file mode 100644 index 00000000000..d27cd647e68 --- /dev/null +++ b/src/test/resources/net/sf/jabref/importer/fileformat/MedlineImporterTestSingleEntry.bib @@ -0,0 +1,15 @@ +% Encoding: UTF-8 + +@Article{, + Title = {Formate assay in body fluids: application in methanol poisoning.}, + Author = {Makar, A. B. and McMartin, K. E. and Palese, M. and Tephly, T. R.}, + Journal = {Biochem Med}, + Year = {1999}, + Number = {2}, + Pages = {117--126}, + Volume = {13}, + Keywords = {Aldehyde Oxidoreductases, metabolism; Animals; Body Fluids, analysis; Carbon Dioxide, blood; Formates, blood/poisoning; Haplorhini; Humans; Hydrogen-Ion Concentration; Kinetics; Methanol, blood; Methods; Pseudomonas, enzymology}, + Language = {eng}, + Medline-pst = {ppublish}, + Pmid = {1} +} \ No newline at end of file diff --git a/src/test/resources/net/sf/jabref/importer/fileformat/MedlineImporterTestSingleEntry.xml b/src/test/resources/net/sf/jabref/importer/fileformat/MedlineImporterTestSingleEntry.xml new file mode 100644 index 00000000000..2a3e6ba030e --- /dev/null +++ b/src/test/resources/net/sf/jabref/importer/fileformat/MedlineImporterTestSingleEntry.xml @@ -0,0 +1,143 @@ + + + +1 + +1976 +01 +16 + + +1976 +01 +16 + + +2013 +11 +21 + +
+ +0006-2944 + +13 +2 + +1999 Spring + + +Biochemical medicine +Biochem Med + +Formate assay in body fluids: application in methanol poisoning. + +117-26 + + + +Makar +A B +AB + + +McMartin +K E +KE + + +Palese +M +M + + +Tephly +T R +TR + + +eng + +Journal Article +Research Support, U.S. Gov't, P.H.S. + +
+ +UNITED STATES +Biochem Med +0151424 +0006-2944 + + + +0 +Formates + + +142M471B3J +Carbon Dioxide + + +EC 1.2.- +Aldehyde Oxidoreductases + + +Y4S76JWI15 +Methanol + + +IM + + +Aldehyde Oxidoreductases +metabolism + + +Animals + + +Body Fluids +analysis + + +Carbon Dioxide +blood + + +Formates +blood +poisoning + + +Haplorhini + + +Humans + + +Hydrogen-Ion Concentration + + +Kinetics + + +Methanol +blood + + +Methods + + +Pseudomonas +enzymology + + +
+ +ppublish + +1 + + +
+
\ No newline at end of file