-
Notifications
You must be signed in to change notification settings - Fork 479
/
DataCitationTest.java
414 lines (380 loc) · 16 KB
/
DataCitationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package edu.harvard.iq.dataverse;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import static junit.framework.TestCase.assertNull;
import static org.junit.Assert.assertEquals;
/**
* Testing DataCitation class
* @author pkiraly@gwdg.de
*/
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("Dataset Title", 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("Dataset Title", 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
public void testWriteAsBibtexCitation() throws ParseException, IOException {
DatasetVersion datasetVersion = createATestDatasetVersion("Dataset Title", true);
DataCitation dataCitation = new DataCitation(datasetVersion);
ByteArrayOutputStream os = new ByteArrayOutputStream();
dataCitation.writeAsBibtexCitation(os);
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"
+ "version = {V1},\r\n"
+ "doi = {10.5072/FK2/LK0D1H},\r\n"
+ "url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n"
+ "}\r\n",
out
);
}
/**
* Test that bibtex data export contains a closing bracket
*
* @throws ParseException
*/
@Test
public void testToBibtexString() throws ParseException {
DatasetVersion datasetVersion = createATestDatasetVersion("Dataset Title", 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"
+ "version = {V1},\r\n"
+ "doi = {10.5072/FK2/LK0D1H},\r\n"
+ "url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n"
+ "}\r\n",
dataCitation.toBibtexString()
);
}
/**
* Test that bibtex data export contains an empty author if no author is
* specified
*
* @throws ParseException
*/
@Test
public void testToBibtexString_withoutAuthor() throws ParseException {
DatasetVersion datasetVersion = createATestDatasetVersion("Dataset Title", 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"
+ "version = {V1},\r\n"
+ "doi = {10.5072/FK2/LK0D1H},\r\n"
+ "url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n"
+ "}\r\n",
dataCitation.toBibtexString()
);
}
/**
* Test that bibtex data export contains an empty title if no title is
* specified
*
* @throws ParseException
*/
@Test
public void testToBibtexString_withoutTitle() throws ParseException {
String nullDatasetTitle = null;
DatasetVersion datasetVersion = createATestDatasetVersion(nullDatasetTitle, 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"
+ "version = {V1},\r\n"
+ "doi = {10.5072/FK2/LK0D1H},\r\n"
+ "url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n"
+ "}\r\n",
dataCitation.toBibtexString()
);
}
/**
* Test that bibtex data export contains an empty author and title if no
* author, nor title is specified
*
* @throws ParseException
*/
@Test
public void testToBibtexString_withoutTitleAndAuthor() throws ParseException {
String nullDatasetTitle = null;
DatasetVersion datasetVersion = createATestDatasetVersion(nullDatasetTitle, 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"
+ "version = {V1},\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("Dataset Title", 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 {
String nullDatasetTitle = null;
DatasetVersion datasetVersion = createATestDatasetVersion(nullDatasetTitle, 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("Dataset Title", 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>" +
"<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 {
String nullDatasetTitle = null;
DatasetVersion datasetVersion = createATestDatasetVersion(nullDatasetTitle, 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>" +
"<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("Dataset Title", 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 {
String nullDatasetTitle = null;
DatasetVersion datasetVersion = createATestDatasetVersion(nullDatasetTitle, 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("Dataset Title", 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 {
String nullDatasetTitle = null;
DatasetVersion datasetVersion = createATestDatasetVersion(nullDatasetTitle, 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)
);
}
@Test
public void testTitleWithQuotes() throws ParseException {
DataCitation dataCitation = new DataCitation(createATestDatasetVersion("This Title \"Has Quotes\" In It", true));
assertEquals("First Last", dataCitation.getAuthorsString());
assertNull(dataCitation.getFileTitle());
assertEquals("doi:10.5072/FK2/LK0D1H", dataCitation.getPersistentId().asString());
assertEquals("LibraScholar", dataCitation.getPublisher());
assertEquals("This Title \"Has Quotes\" In It", dataCitation.getTitle());
assertNull(dataCitation.getUNF());
assertEquals("V1", dataCitation.getVersion());
assertEquals("1955", dataCitation.getYear());
assertEquals(
"@data{LK0D1H_1955,\r\n"
+ "author = {First Last},\r\n"
+ "publisher = {LibraScholar},\r\n"
+ "title = {{This Title ``Has Quotes'' In It}},\r\n"
+ "year = {1955},\r\n"
+ "version = {V1},\r\n"
+ "doi = {10.5072/FK2/LK0D1H},\r\n"
+ "url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n"
+ "}\r\n",
dataCitation.toBibtexString()
);
}
private DatasetVersion createATestDatasetVersion(String withTitle, boolean withAuthor) throws ParseException {
Dataverse dataverse = new Dataverse();
dataverse.setName("LibraScholar");
Dataset dataset = new Dataset();
dataset.setProtocol("doi");
dataset.setAuthority("10.5072/FK2");
dataset.setIdentifier("LK0D1H");
dataset.setOwner(dataverse);
DatasetVersion datasetVersion = new DatasetVersion();
datasetVersion.setDataset(dataset);
datasetVersion.setVersionState(DatasetVersion.VersionState.DRAFT);
datasetVersion.setVersionState(DatasetVersion.VersionState.RELEASED);
datasetVersion.setVersionNumber(1L);
List<DatasetField> fields = new ArrayList<>();
if (withTitle != null) {
fields.add(createTitleField(withTitle));
}
if (withAuthor) {
fields.add(createAuthorField("First Last"));
}
if (!fields.isEmpty()) {
datasetVersion.setDatasetFields(fields);
}
SimpleDateFormat dateFmt = new SimpleDateFormat("yyyyMMdd");
Date publicationDate = dateFmt.parse("19551105");
datasetVersion.setReleaseTime(publicationDate);
dataset.setPublicationDate(new Timestamp(publicationDate.getTime()));
return datasetVersion;
}
private DatasetField createAuthorField(String value) {
DatasetField author = new DatasetField();
author.setDatasetFieldType(new DatasetFieldType(DatasetFieldConstant.author, DatasetFieldType.FieldType.TEXT, false));
List<DatasetFieldCompoundValue> compoundValues = new LinkedList<>();
DatasetFieldCompoundValue compoundValue = new DatasetFieldCompoundValue();
compoundValue.setParentDatasetField(author);
compoundValue.setChildDatasetFields(Arrays.asList(
constructPrimitive(DatasetFieldConstant.authorName, value)
));
compoundValues.add(compoundValue);
author.setDatasetFieldCompoundValues(compoundValues);
return author;
}
private DatasetField createTitleField(String value) {
return constructPrimitive(DatasetFieldConstant.title, value);
}
DatasetField constructPrimitive(String fieldName, String value) {
DatasetField field = new DatasetField();
field.setDatasetFieldType(
new DatasetFieldType(fieldName, DatasetFieldType.FieldType.TEXT, false));
field.setDatasetFieldValues(
Collections.singletonList(
new DatasetFieldValue(field, value)));
return field;
}
}