Skip to content

Commit

Permalink
Merge pull request #5268 from pkiraly/5265-extend-dataCitation-tests
Browse files Browse the repository at this point in the history
issue #5265: adding tests to every public methods.
  • Loading branch information
kcondon authored Nov 2, 2018
2 parents 023a9a2 + cd7b177 commit c2dec15
Showing 1 changed file with 218 additions and 41 deletions.
259 changes: 218 additions & 41 deletions src/test/java/edu/harvard/iq/dataverse/DataCitationTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.harvard.iq.dataverse;

import org.junit.jupiter.api.Test;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -9,6 +10,7 @@
import java.text.SimpleDateFormat;
import java.util.*;

import static junit.framework.TestCase.assertNull;
import static org.junit.Assert.assertEquals;

/**
Expand All @@ -17,13 +19,49 @@
*/
public class DataCitationTest {

/**
* Test the public properties of DataCitation class via their getters
* @throws ParseException
*/
@Test
public void testProperties() throws ParseException {
DataCitation dataCitation = new DataCitation(createATestDatasetVersion(true, true));
assertEquals("First Last", dataCitation.getAuthorsString());
assertNull(dataCitation.getFileTitle());
assertEquals("doi:10.5072/FK2/LK0D1H", dataCitation.getPersistentId().asString());
assertEquals("LibraScholar", dataCitation.getPublisher());
assertEquals("Dataset Title", dataCitation.getTitle());
assertNull(dataCitation.getUNF());
assertEquals("V1", dataCitation.getVersion());
assertEquals("1955", dataCitation.getYear());
}

/**
* Test DataCite metadata
* @throws ParseException
*/
@Test
public void testGetDataCiteMetadata() throws ParseException {
DataCitation dataCitation = new DataCitation(createATestDatasetVersion(true, true));
Map<String, String> properties = dataCitation.getDataCiteMetadata();
assertEquals(4, properties.size());
assertEquals(
"datacite.creator, datacite.publisher, datacite.title, datacite.publicationyear",
StringUtils.join(properties.keySet(), ", ")
);
assertEquals("First Last", properties.get("datacite.creator"));
assertEquals("LibraScholar", properties.get("datacite.publisher"));
assertEquals("Dataset Title", properties.get("datacite.title"));
assertEquals("1955", properties.get("datacite.publicationyear"));
}

/**
* Test that bibtex data export contains a closing bracket
* @throws ParseException
* @throws IOException
*/
@Test
void testWriteAsBibtexCitation() throws ParseException, IOException {
public void testWriteAsBibtexCitation() throws ParseException, IOException {
DatasetVersion datasetVersion = createATestDatasetVersion(true, true);

DataCitation dataCitation = new DataCitation(datasetVersion);
Expand All @@ -32,13 +70,13 @@ void testWriteAsBibtexCitation() throws ParseException, IOException {
String out = new String(os.toByteArray(), "UTF-8");
assertEquals(
"@data{LK0D1H_1955,\r\n" +
"author = {First Last},\r\n" +
"publisher = {LibraScholar},\r\n" +
"title = {Dataset Title},\r\n" +
"year = {1955},\r\n" +
"doi = {10.5072/FK2/LK0D1H},\r\n" +
"url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n" +
"}\r\n",
"author = {First Last},\r\n" +
"publisher = {LibraScholar},\r\n" +
"title = {Dataset Title},\r\n" +
"year = {1955},\r\n" +
"doi = {10.5072/FK2/LK0D1H},\r\n" +
"url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n" +
"}\r\n",
out
);
}
Expand All @@ -48,18 +86,18 @@ void testWriteAsBibtexCitation() throws ParseException, IOException {
* @throws ParseException
*/
@Test
void testToBibtexString() throws ParseException {
public void testToBibtexString() throws ParseException {
DatasetVersion datasetVersion = createATestDatasetVersion(true, true);
DataCitation dataCitation = new DataCitation(datasetVersion);
assertEquals(
"@data{LK0D1H_1955,\r\n" +
"author = {First Last},\r\n" +
"publisher = {LibraScholar},\r\n" +
"title = {Dataset Title},\r\n" +
"year = {1955},\r\n" +
"doi = {10.5072/FK2/LK0D1H},\r\n" +
"url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n" +
"}\r\n",
"author = {First Last},\r\n" +
"publisher = {LibraScholar},\r\n" +
"title = {Dataset Title},\r\n" +
"year = {1955},\r\n" +
"doi = {10.5072/FK2/LK0D1H},\r\n" +
"url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n" +
"}\r\n",
dataCitation.toBibtexString()
);
}
Expand All @@ -69,18 +107,18 @@ void testToBibtexString() throws ParseException {
* @throws ParseException
*/
@Test
void testToBibtexString_withoutAuthor() throws ParseException {
public void testToBibtexString_withoutAuthor() throws ParseException {
DatasetVersion datasetVersion = createATestDatasetVersion(true, false);
DataCitation dataCitation = new DataCitation(datasetVersion);
assertEquals(
"@data{LK0D1H_1955,\r\n" +
"author = {},\r\n" +
"publisher = {LibraScholar},\r\n" +
"title = {Dataset Title},\r\n" +
"year = {1955},\r\n" +
"doi = {10.5072/FK2/LK0D1H},\r\n" +
"url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n" +
"}\r\n",
"author = {},\r\n" +
"publisher = {LibraScholar},\r\n" +
"title = {Dataset Title},\r\n" +
"year = {1955},\r\n" +
"doi = {10.5072/FK2/LK0D1H},\r\n" +
"url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n" +
"}\r\n",
dataCitation.toBibtexString()
);
}
Expand All @@ -90,18 +128,18 @@ void testToBibtexString_withoutAuthor() throws ParseException {
* @throws ParseException
*/
@Test
void testToBibtexString_withoutTitle() throws ParseException {
public void testToBibtexString_withoutTitle() throws ParseException {
DatasetVersion datasetVersion = createATestDatasetVersion(false, true);
DataCitation dataCitation = new DataCitation(datasetVersion);
assertEquals(
"@data{LK0D1H_1955,\r\n" +
"author = {First Last},\r\n" +
"publisher = {LibraScholar},\r\n" +
"title = {},\r\n" +
"year = {1955},\r\n" +
"doi = {10.5072/FK2/LK0D1H},\r\n" +
"url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n" +
"}\r\n",
"author = {First Last},\r\n" +
"publisher = {LibraScholar},\r\n" +
"title = {},\r\n" +
"year = {1955},\r\n" +
"doi = {10.5072/FK2/LK0D1H},\r\n" +
"url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n" +
"}\r\n",
dataCitation.toBibtexString()
);
}
Expand All @@ -111,22 +149,161 @@ void testToBibtexString_withoutTitle() throws ParseException {
* @throws ParseException
*/
@Test
void testToBibtexString_withoutTitleAndAuthor() throws ParseException, IOException {
public void testToBibtexString_withoutTitleAndAuthor() throws ParseException {
DatasetVersion datasetVersion = createATestDatasetVersion(false, false);
DataCitation dataCitation = new DataCitation(datasetVersion);
assertEquals(
"@data{LK0D1H_1955,\r\n" +
"author = {},\r\n" +
"publisher = {LibraScholar},\r\n" +
"title = {},\r\n" +
"year = {1955},\r\n" +
"doi = {10.5072/FK2/LK0D1H},\r\n" +
"url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n" +
"}\r\n",
"author = {},\r\n" +
"publisher = {LibraScholar},\r\n" +
"title = {},\r\n" +
"year = {1955},\r\n" +
"doi = {10.5072/FK2/LK0D1H},\r\n" +
"url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n" +
"}\r\n",
dataCitation.toBibtexString()
);
}

@Test
public void testToRISString_withTitleAndAuthor() throws ParseException {
DatasetVersion datasetVersion = createATestDatasetVersion(true, true);
DataCitation dataCitation = new DataCitation(datasetVersion);
assertEquals(
"Provider: LibraScholar\r\n" +
"Content: text/plain; charset=\"utf-8\"\r\n" +
"TY - DATA\r\n" +
"T1 - Dataset Title\r\n" +
"AU - First Last\r\n" +
"DO - doi:10.5072/FK2/LK0D1H\r\n" +
"ET - V1\r\n" +
"PY - 1955\r\n" +
"SE - 1955-11-05 00:00:00.0\r\n" +
"UR - https://doi.org/10.5072/FK2/LK0D1H\r\n" +
"PB - LibraScholar\r\n" +
"ER - \r\n",
dataCitation.toRISString()
);
}

@Test
public void testToRISString_withoutTitleAndAuthor() throws ParseException {
DatasetVersion datasetVersion = createATestDatasetVersion(false, false);
DataCitation dataCitation = new DataCitation(datasetVersion);
assertEquals(
"Provider: LibraScholar\r\n" +
"Content: text/plain; charset=\"utf-8\"\r\n" +
"TY - DATA\r\n" +
"T1 - \r\n" +
"DO - doi:10.5072/FK2/LK0D1H\r\n" +
"ET - V1\r\n" +
"PY - 1955\r\n" +
"SE - 1955-11-05 00:00:00.0\r\n" +
"UR - https://doi.org/10.5072/FK2/LK0D1H\r\n" +
"PB - LibraScholar\r\n" +
"ER - \r\n",
dataCitation.toRISString()
);
}

@Test
public void testToEndNoteString_withTitleAndAuthor() throws ParseException {
DatasetVersion datasetVersion = createATestDatasetVersion(true, true);
DataCitation dataCitation = new DataCitation(datasetVersion);
assertEquals(
"<?xml version='1.0' encoding='UTF-8'?>" +
"<xml>" +
"<records>" +
"<record>" +
"<ref-type name=\"Dataset\">59</ref-type>" +
"<contributors>" +
"<authors><author>First Last</author></authors>" +
"</contributors>" +
"<titles><title>Dataset Title</title></titles>" +
"<section>1955-11-05</section>" +
"<abstract />" +
"<dates><year>1955</year></dates>" +
"<edition>V1</edition>" +
"<publisher>LibraScholar</publisher>" +
"<urls><related-urls><url>https://doi.org/10.5072/FK2/LK0D1H</url></related-urls></urls>" +
"<electronic-resource-num>doi/10.5072/FK2/LK0D1H</electronic-resource-num>" +
"</record>" +
"</records>" +
"</xml>",
dataCitation.toEndNoteString()
);
}

@Test
public void testToEndNoteString_withoutTitleAndAuthor() throws ParseException {
DatasetVersion datasetVersion = createATestDatasetVersion(false, false);
DataCitation dataCitation = new DataCitation(datasetVersion);
assertEquals(
"<?xml version='1.0' encoding='UTF-8'?>" +
"<xml>" +
"<records>" +
"<record>" +
"<ref-type name=\"Dataset\">59</ref-type>" +
"<contributors />" +
"<titles><title></title></titles>" +
"<section>1955-11-05</section>" +
"<abstract />" +
"<dates><year>1955</year></dates>" +
"<edition>V1</edition>" +
"<publisher>LibraScholar</publisher>" +
"<urls><related-urls><url>https://doi.org/10.5072/FK2/LK0D1H</url></related-urls></urls>" +
"<electronic-resource-num>doi/10.5072/FK2/LK0D1H</electronic-resource-num>" +
"</record>" +
"</records>" +
"</xml>",
dataCitation.toEndNoteString()
);
}

@Test
public void testToString_withTitleAndAuthor() throws ParseException {
DatasetVersion datasetVersion = createATestDatasetVersion(true, true);
DataCitation dataCitation = new DataCitation(datasetVersion);
assertEquals(
"First Last, 1955, \"Dataset Title\", https://doi.org/10.5072/FK2/LK0D1H, LibraScholar, V1",
dataCitation.toString()
);
}

@Test
public void testToString_withoutTitleAndAuthor() throws ParseException {
DatasetVersion datasetVersion = createATestDatasetVersion(false, false);
DataCitation dataCitation = new DataCitation(datasetVersion);
assertEquals(
"1955, https://doi.org/10.5072/FK2/LK0D1H, LibraScholar, V1",
dataCitation.toString()
);
}

@Test
public void testToHtmlString_withTitleAndAuthor() throws ParseException {
DatasetVersion datasetVersion = createATestDatasetVersion(true, true);
DataCitation dataCitation = new DataCitation(datasetVersion);
assertEquals(
"First Last, 1955, \"Dataset Title\"," +
" <a href=\"https://doi.org/10.5072/FK2/LK0D1H\" target=\"_blank\">https://doi.org/10.5072/FK2/LK0D1H</a>," +
" LibraScholar, V1",
dataCitation.toString(true)
);
}

@Test
public void testToHtmlString_withoutTitleAndAuthor() throws ParseException {
DatasetVersion datasetVersion = createATestDatasetVersion(false, false);
DataCitation dataCitation = new DataCitation(datasetVersion);
assertEquals(
"1955," +
" <a href=\"https://doi.org/10.5072/FK2/LK0D1H\" target=\"_blank\">https://doi.org/10.5072/FK2/LK0D1H</a>," +
" LibraScholar, V1",
dataCitation.toString(true)
);
}

private DatasetVersion createATestDatasetVersion(boolean withTitle, boolean withAuthor) throws ParseException {
Dataverse dataverse = new Dataverse();
dataverse.setName("LibraScholar");
Expand Down

0 comments on commit c2dec15

Please sign in to comment.