Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MedlineImporterTest #547

Merged
merged 2 commits into from
May 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/main/java/net/sf/jabref/logic/util/strings/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> getTestFiles() throws IOException {
List<String> files = new ArrayList<>();
try (DirectoryStream<Path> 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<BibEntry> 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());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind the formatting, please.


@Test
public void testGetCLIId() {
assertEquals("medline", medlineImporter.getCLIId());
}

@Test
public void testIsRecognizedFormatReject() throws IOException {
List<String> 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));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<String> fileNames() throws IOException {
List<String> files = new ArrayList<>();
try (DirectoryStream<Path> 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<BibEntry> 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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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}
}
Loading