Project

General

Profile

Download (5.05 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.common;
10

    
11
import org.junit.Assert;
12
import org.junit.Before;
13
import org.junit.Test;
14

    
15
/**
16
 * 
17
 * Test class for testing the {@link DOI} class.
18
 * 
19
 * For doi syntax see also http://www.doi.org/doi_handbook/2_Numbering.html
20
 * or
21
 * http://stackoverflow.com/questions/27910/finding-a-doi-in-a-document-or-page  
22
 * 
23
 * @author a.mueller
24
 *
25
 */
26
public class DoiTest {
27

    
28
	/**
29
	 * @throws java.lang.Exception
30
	 */
31
	@Before
32
	public void setUp() throws Exception {
33
	}
34

    
35
	@Test
36
	public void testValidParser() {
37
		String validDoi = "10.1002/1234";
38
		DOI doi = DOI.fromString(validDoi);
39
		Assert.assertEquals("10.1002", doi.getPrefix());
40
		Assert.assertEquals("1234", doi.getSuffix());
41
		
42
		validDoi = "10.1002/(SICI)1522-2594(199911)42:5<952::AID-MRM16>3.0.CO;2-S";
43
		doi = DOI.fromString(validDoi);
44
		Assert.assertEquals("10.1002", doi.getPrefix());
45
		Assert.assertEquals("(SICI)1522-2594(199911)42:5<952::AID-MRM16>3.0.CO;2-S", doi.getSuffix());
46
		
47
		validDoi = "10.1007.10/978-3-642-28108-2_19";
48
		doi = DOI.fromString(validDoi);
49
		Assert.assertEquals("10.1007.10", doi.getPrefix());
50
		Assert.assertEquals("978-3-642-28108-2_19", doi.getSuffix());
51
		
52
		validDoi="10.1579/0044-7447(2006)35\\[89:RDUICP\\]2.0.CO;2";
53
		doi = DOI.fromString(validDoi);
54
		Assert.assertEquals("10.1579", doi.getPrefix());
55
		Assert.assertEquals("0044-7447(2006)35\\[89:RDUICP\\]2.0.CO;2", doi.getSuffix());
56

    
57
	}
58
	
59
	@Test
60
	public void testFromRegistrantCodeAndSuffix() {
61
		DOI doi = DOI.fromRegistrantCodeAndSuffix("1579", "978-3-642-28108-2_19");
62
		Assert.assertEquals("10.1579", doi.getPrefix());
63
		Assert.assertEquals("978-3-642-28108-2_19", doi.getSuffix());
64
		Assert.assertNotEquals("1234", doi.getSuffix());
65
	}
66
	
67
	@Test
68
	public void testParserFail() {
69
		String invalidDoi = "10.4515260,51.1656910";  //must never match to avoid matches with geo coordinates
70
		testInvalid(invalidDoi);
71
		invalidDoi = "4210.1000/123456";  //directoryIndicator must always be 10
72
		testInvalid(invalidDoi);
73
		invalidDoi = "10.1002/12\u0004345";  //control characters (here U+0004) must fail
74
		testInvalid(invalidDoi);
75
		invalidDoi = "10.1a02/12345";  //registrant code must include only number and dots (to separate sub codes)
76
		testInvalid(invalidDoi);
77
		invalidDoi = "10.1002:12345";  //column separator is only allowed (+required) in URNs
78
		testInvalid(invalidDoi);
79
		invalidDoi = "10.1002/";  //doi must always have a suffix length > 0 (if this should changed in future, please do adapt equals and hashCode)
80
		testInvalid(invalidDoi);
81
		invalidDoi = "10./1234";  //doi must always have a registrant prefix length > 0 (if this should changed in future, please do adapt equals and hashCode)
82
		testInvalid(invalidDoi);
83
	}
84

    
85
	@Test
86
	public void testParserWithPrefixes() {
87
		String validDoi = "DOI: 10.1002/1234";
88
		DOI doi = DOI.fromString(validDoi);
89
		Assert.assertEquals("10.1002", doi.getPrefix());
90
		Assert.assertEquals("1234", doi.getSuffix());
91
		
92
		validDoi = "http://doi.org/10.1002/1234";
93
		doi = DOI.fromString(validDoi);
94
		Assert.assertEquals("10.1002", doi.getPrefix());
95
		Assert.assertEquals("1234", doi.getSuffix());
96
	
97
		
98
		validDoi = "http://doi.org/urn:doi:10.123:456ABC%2Fzyz";
99
		doi = DOI.fromString(validDoi);
100
		Assert.assertEquals("10.123", doi.getPrefix());
101
		Assert.assertEquals("456ABC/zyz", doi.getSuffix());  //urn must be percentage encoded ( / -> %2F)
102
		
103
	}
104
	
105
	@Test
106
	public void testEquals() {
107
		String validDoi = "10.1002/12a4";
108
		DOI doi1 = DOI.fromString(validDoi);
109
		validDoi = "10.1002/12A4";
110
		DOI doi2 = DOI.fromString(validDoi);
111
		Assert.assertEquals("DOIs must be equal case insensitive", doi1, doi2);
112
		validDoi = "10.1002/12b4";
113
		DOI doi3 = DOI.fromString(validDoi);
114
		Assert.assertNotEquals("Different DOIs must not be equal", doi1, doi3);
115
	}
116
	
117
	@Test
118
	public void testAsURI() {
119
		//mandatory encoding according to http://www.doi.org/doi_handbook/2_Numbering.html#2.5.2.4
120
		String validDoi = "10.1002/1234%56\"78#90 12?34";
121
		DOI doi1 = DOI.fromString(validDoi);
122
		String uri = doi1.asURI();
123
		Assert.assertEquals(DOI.HTTP_DOI_ORG + "10.1002/1234%2556%2278%2390%2012%3f34", uri);
124
		
125
		//recommendedEncoding
126
		validDoi = "10.1002/1234<56>78{90}12^34";
127
		doi1 = DOI.fromString(validDoi);
128
		uri = doi1.asURI();
129
		Assert.assertEquals(DOI.HTTP_DOI_ORG + "10.1002/1234%3c56%3e78%7b90%7d12%5e34", uri);
130
		
131
		//recommendedEncoding (cont.)
132
		validDoi = "10.1002/1234[56]78`90|12\\34+56";
133
		doi1 = DOI.fromString(validDoi);
134
		uri = doi1.asURI();
135
		Assert.assertEquals(DOI.HTTP_DOI_ORG + "10.1002/1234%5b56%5d78%6090%7c12%5c34%2b56", uri);
136
		
137
	}
138

    
139
	
140

    
141
	
142
	private void testInvalid(String invalidDoi) {
143
		try {
144
			DOI.fromString(invalidDoi);
145
			Assert.fail("DOI should not be parsable: " + invalidDoi);
146
		} catch (IllegalArgumentException e) {
147
			//OK
148
		}
149
	}		
150

    
151
}
(2-2/7)