Project

General

Profile

Download (12.7 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.strategy.cache.taxon;
10

    
11
import static org.junit.Assert.assertEquals;
12

    
13
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
14
import org.joda.time.DateTime;
15
import org.junit.Assert;
16
import org.junit.Before;
17
import org.junit.Test;
18

    
19
import eu.etaxonomy.cdm.common.URI;
20
import eu.etaxonomy.cdm.model.agent.Person;
21
import eu.etaxonomy.cdm.model.agent.Team;
22
import eu.etaxonomy.cdm.model.name.IBotanicalName;
23
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
24
import eu.etaxonomy.cdm.model.name.Rank;
25
import eu.etaxonomy.cdm.model.name.TaxonName;
26
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
27
//import eu.etaxonomy.cdm.model.reference.Book;
28
import eu.etaxonomy.cdm.model.reference.Reference;
29
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
30
import eu.etaxonomy.cdm.model.reference.ReferenceType;
31
import eu.etaxonomy.cdm.model.taxon.Classification;
32
import eu.etaxonomy.cdm.model.taxon.Synonym;
33
import eu.etaxonomy.cdm.model.taxon.Taxon;
34
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
35
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
36
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
37
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
38
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
39
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
40
import eu.etaxonomy.cdm.test.TermTestBase;
41

    
42
/**
43
 * @author a.mueller
44
 * @since 21.09.2009
45
 */
46
public class TaxonBaseDefaultCacheStrategyTest extends TermTestBase {
47

    
48
	@SuppressWarnings("unused")
49
	private static final Logger logger = LogManager.getLogger(TaxonBaseDefaultCacheStrategyTest.class);
50

    
51
	private final String expectedNameTitleCache = "Abies alba (L.) Mill.";
52
	private final String expectedNameCache = "Abies alba";
53
	private IBotanicalName name;
54
	private Reference sec;
55

    
56
	@Before
57
	public void setUp() throws Exception {
58
		name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
59
		name.setGenusOrUninomial("Abies");
60
		name.setSpecificEpithet("alba");
61
		Person combinationAuthor = Person.NewInstance();
62
		combinationAuthor.setNomenclaturalTitle("Mill.");
63
		Person basionymAuthor = Person.NewInstance();
64
		basionymAuthor.setNomenclaturalTitle("L.");
65

    
66
		name.setCombinationAuthorship(combinationAuthor);
67
		name.setBasionymAuthorship(basionymAuthor);
68
		assertEquals("Namecache should be Abies alba", expectedNameCache, name.getNameCache());
69
		assertEquals("Titlecache should be Abies alba (Mill.) L.", expectedNameTitleCache, name.getTitleCache());
70
		sec = ReferenceFactory.newBook();
71
		sec.setTitle("Sp.Pl.");
72
	}
73

    
74
//******************************* TESTS ********************************************************
75

    
76
	@Test
77
	public void testGetTitleCache() {
78
		Taxon taxon = Taxon.NewInstance(name, sec);
79
		assertEquals("Taxon titlecache is wrong", expectedNameTitleCache + " sec. Sp.Pl.", taxon.getTitleCache());
80
		//without sec.
81
		taxon.setSec(null);
82
		assertEquals("Taxon titlecache is wrong", expectedNameTitleCache + " sec. ???", taxon.getTitleCache());
83
		//appended phrase without sec.
84
		String appendedPhrase = "aff. 'schippii'";
85
		taxon.setAppendedPhrase(appendedPhrase);
86
		assertEquals("Taxon titlecache is wrong", expectedNameTitleCache + " aff. 'schippii'", taxon.getTitleCache());
87
		//appended phrase with sec.
88
		taxon.setSec(sec);
89
		assertEquals("Taxon titlecache is wrong", expectedNameTitleCache + " aff. 'schippii' sec. Sp.Pl.", taxon.getTitleCache());
90
		//use name cache
91
		taxon.setUseNameCache(true);
92
		assertEquals("Taxon titlecache is wrong", expectedNameCache + " aff. 'schippii' sec. Sp.Pl.", taxon.getTitleCache());
93
		taxon.setDoubtful(true);
94
        assertEquals("Taxon titlecache is wrong", "?" + expectedNameCache + " aff. 'schippii' sec. Sp.Pl.", taxon.getTitleCache());
95
        //with nom status
96
        taxon.setAppendedPhrase(null);
97
        taxon.setUseNameCache(false);
98
        taxon.setDoubtful(false);
99
        name.addStatus(NomenclaturalStatusType.ILLEGITIMATE(), null, null);
100
        assertEquals("Taxon titlecache is wrong", expectedNameTitleCache + ", nom. illeg., sec. Sp.Pl.", taxon.getTitleCache());
101
	}
102

    
103
    @Test
104
    public void testGetTitleCacheWithNameUsedInSource() {
105
        Taxon taxon = Taxon.NewInstance(name, sec);
106
        TaxonName nameUsedInSource = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
107
        nameUsedInSource.setGenusOrUninomial("Pinus");
108
        nameUsedInSource.setSpecificEpithet("blanca");
109
        taxon.getSecSource().setNameUsedInSource(nameUsedInSource);
110
        assertEquals("Taxon titlecache is wrong", expectedNameTitleCache + " sec. Sp.Pl. (sub Pinus blanca)", taxon.getTitleCache());
111
    }
112

    
113
	//same as for accepted taxa but with syn. sec. instead of sec.
114
    @Test
115
    public void testSynSec() {
116
        Synonym syn = Synonym.NewInstance(name, sec);
117
        assertEquals("Taxon titlecache is wrong", expectedNameTitleCache + " syn. sec. Sp.Pl.", syn.getTitleCache());
118
        //without sec.
119
        syn.setSec(null);
120
        assertEquals("Taxon titlecache is wrong", expectedNameTitleCache + " syn. sec. ???", syn.getTitleCache());
121
        //appended phrase without sec.
122
        String appendedPhrase = "aff. 'schippii'";
123
        syn.setAppendedPhrase(appendedPhrase);
124
        assertEquals("Taxon titlecache is wrong", expectedNameTitleCache + " aff. 'schippii'", syn.getTitleCache());
125
        //appended phrase with sec.
126
        syn.setSec(sec);
127
        assertEquals("Taxon titlecache is wrong", expectedNameTitleCache + " aff. 'schippii' syn. sec. Sp.Pl.", syn.getTitleCache());
128
        //use name cache
129
        syn.setUseNameCache(true);
130
        assertEquals("Taxon titlecache is wrong", expectedNameCache + " aff. 'schippii' syn. sec. Sp.Pl.", syn.getTitleCache());
131
        syn.setDoubtful(true);
132
        assertEquals("Taxon titlecache is wrong", "?" + expectedNameCache + " aff. 'schippii' syn. sec. Sp.Pl.", syn.getTitleCache());
133
        //with nom status
134
        syn.setAppendedPhrase(null);
135
        syn.setUseNameCache(false);
136
        syn.setDoubtful(false);
137
        name.addStatus(NomenclaturalStatusType.ILLEGITIMATE(), null, null);
138
        assertEquals("Taxon titlecache is wrong", expectedNameTitleCache + ", nom. illeg., syn. sec. Sp.Pl.", syn.getTitleCache());
139
    }
140

    
141
   @Test
142
    public void testGetTitleCacheWithoutName() {
143
        Taxon taxon = Taxon.NewInstance(null, sec);
144
        assertEquals("Taxon titlecache is wrong", "??? sec. Sp.Pl.", taxon.getTitleCache());
145
    }
146

    
147
	//test missing "&" in title cache  #3822
148
	@Test
149
	public void testAndInTitleCache() {
150
		Taxon taxon = Taxon.NewInstance(name, sec);
151
		Team team = Team.NewInstance();
152
		team.addTeamMember((Person)name.getCombinationAuthorship());
153
		team.addTeamMember((Person)name.getBasionymAuthorship());
154
		name.setCombinationAuthorship(team);
155
//		System.out.println(taxonBase.generateTitle());
156
		assertEquals("Abies alba (L.) Mill. \u0026 L. sec. Sp.Pl.", taxon.generateTitle());
157

    
158
		name = TaxonNameFactory.NewBotanicalInstance(null);
159
		NonViralNameParserImpl.NewInstance().parseFullName(name, "Cichorium glandulosum Boiss. \u0026 A. Huet", null, true);
160
		Taxon taxon2 = Taxon.NewInstance(name, sec);
161
		assertEquals("Cichorium glandulosum Boiss. \u0026 A. Huet sec. Sp.Pl.", taxon2.getTitleCache());
162
	}
163

    
164
    @Test
165
	public void testProtectedTitleCache(){
166
	    Taxon taxon = Taxon.NewInstance(name, sec);
167
        taxon.setTitleCache("abc", true);
168
        taxon.setDoubtful(true);
169
        Assert.assertEquals("abc", taxon.getTitleCache());
170
	}
171

    
172
    @Test
173
    public void testProtectedSecTitleCache(){
174
        Taxon taxon = Taxon.NewInstance(name, sec);
175
        sec.setTitleCache("My protected sec ref", true);
176
        taxon.setDoubtful(true);
177
        taxon.setSecMicroReference("123");
178
        Assert.assertEquals("?Abies alba (L.) Mill. sec. My protected sec ref: 123", taxon.getTitleCache());
179
    }
180

    
181
    @Test
182
    public void testMicroReference(){
183
        Taxon taxon = Taxon.NewInstance(name, sec);
184
        String secMicroRef = "p. 553";
185

    
186
        //not atomized
187
        taxon.setSecMicroReference(secMicroRef);
188
        assertEquals("Taxon titlecache is wrong", expectedNameTitleCache + " sec. Sp.Pl.: p. 553",
189
                taxon.getTitleCache());
190

    
191
        //atomized
192
        sec.setAuthorship(Team.NewTitledInstance("Team", "T."));
193
        sec.setDatePublished(TimePeriodParser.parseStringVerbatim("1798"));
194
        taxon.setTitleCache(null, false);
195
        assertEquals("Taxon titlecache is wrong", expectedNameTitleCache + " sec. Team 1798: p. 553",
196
                taxon.getTitleCache());
197
    }
198

    
199
    @Test
200
    public void testWebPageSec(){
201
        Reference sec = ReferenceFactory.newWebPage();
202
        sec.setTitle("My long webpage");
203
        sec.setAbbrevTitle("MLW");
204
        sec.setUri(URI.create("https://abc.de"));
205
        sec.setDatePublished(TimePeriodParser.parseStringVerbatim("2 Jan 1982"));
206
        TaxonBase<?> taxonBase = Taxon.NewInstance(name, sec);
207
        Assert.assertEquals("Abies alba (L.) Mill. sec. MLW 1982", taxonBase.getTitleCache());
208

    
209
        sec.setDatePublished(null);
210
        taxonBase.setTitleCache(null, false);
211
        Assert.assertEquals("Abies alba (L.) Mill. sec. MLW", taxonBase.getTitleCache());
212

    
213
        sec.setAccessed(DateTime.parse("1983-06-30"));
214
        taxonBase.setTitleCache(null, false);
215
        Assert.assertEquals("Abies alba (L.) Mill. sec. MLW 1983", taxonBase.getTitleCache());
216

    
217
        sec.setAbbrevTitle(null);
218
        taxonBase.setTitleCache(null, false);
219
        Assert.assertEquals("Abies alba (L.) Mill. sec. My long webpage 1983", taxonBase.getTitleCache());
220

    
221
        sec.setAbbrevTitle("MLW");
222
        taxonBase.setSecMicroReference("table 1");
223
        Assert.assertEquals("Abies alba (L.) Mill. sec. MLW 1983: table 1", taxonBase.getTitleCache());
224

    
225
        sec.setType(ReferenceType.Database);
226
        taxonBase.setSecMicroReference(null);
227
        Assert.assertEquals("Abies alba (L.) Mill. sec. MLW 1983", taxonBase.getTitleCache());
228

    
229
        sec.setType(ReferenceType.Map);
230
        taxonBase.setTitleCache(null, false);
231
        Assert.assertEquals("Abies alba (L.) Mill. sec. MLW 1983", taxonBase.getTitleCache());
232

    
233
    }
234

    
235
    @Test
236
    public void testMisapplication(){
237
        //assert default (taxon without relation)
238
        Taxon man = Taxon.NewInstance(name, sec);
239
        ITaxonCacheStrategy<Taxon> cacheStrategy = man.cacheStrategy();
240
        assertEquals("Taxon titlecache must use sec", expectedNameTitleCache + " sec. Sp.Pl.", cacheStrategy.getTitleCache(man));
241

    
242
        //make it a MAN only
243
        Taxon mainTaxon = Taxon.NewInstance(TaxonNameFactory.NewBacterialInstance(Rank.SPECIES()), ReferenceFactory.newBook() );
244
        mainTaxon.addMisappliedName(man, null, null);
245
        assertEquals("Taxon titlecache must use sensu", expectedNameCache + " sensu Sp.Pl., non (L.) Mill.", cacheStrategy.getTitleCache(man));
246
        man.setSec(null);
247
        assertEquals("Taxon titlecache must use sensu", expectedNameCache + " auct., non (L.) Mill.", cacheStrategy.getTitleCache(man));
248
        man.setAppendedPhrase("aucts.");
249
        assertEquals("Taxon titlecache must use sensu", expectedNameCache + " aucts., non (L.) Mill.", cacheStrategy.getTitleCache(man));
250
        //reset sec + appendedPhrase
251
        man.setSec(sec);
252
        man.setAppendedPhrase(null);
253

    
254
        //add another from relation
255
        Taxon relatedTaxon = Taxon.NewInstance(TaxonNameFactory.NewBacterialInstance(Rank.SPECIES()), ReferenceFactory.newBook() );
256
        TaxonRelationship rel = man.addTaxonRelation(relatedTaxon, TaxonRelationshipType.CONGRUENT_TO(), null, null);
257
        assertEquals("Taxon titlecache must use sec", expectedNameTitleCache + " sec. Sp.Pl.", cacheStrategy.getTitleCache(man));
258
        man.removeTaxonRelation(rel);
259
        assertEquals("Taxon titlecache must use sensu", expectedNameCache + " sensu Sp.Pl., non (L.) Mill.", cacheStrategy.getTitleCache(man));
260

    
261
        //add another to relation
262
        rel = relatedTaxon.addTaxonRelation(man, TaxonRelationshipType.CONGRUENT_TO(), null, null);
263
        assertEquals("Taxon titlecache must use sec", expectedNameTitleCache + " sec. Sp.Pl.", cacheStrategy.getTitleCache(man));
264
        man.removeTaxonRelation(rel);
265
        assertEquals("Taxon titlecache must use sensu", expectedNameCache + " sensu Sp.Pl., non (L.) Mill.", cacheStrategy.getTitleCache(man));
266

    
267
        //add taxon node
268
        Classification c = Classification.NewInstance("Test");
269
        TaxonNode tn = c.addChildTaxon(man, null);
270
        assertEquals("Taxon titlecache must use sec", expectedNameTitleCache + " sec. Sp.Pl.", cacheStrategy.getTitleCache(man));
271
        man.removeTaxonNode(tn);
272
        assertEquals("Taxon titlecache must use sensu", expectedNameCache + " sensu Sp.Pl., non (L.) Mill.", cacheStrategy.getTitleCache(man));
273
    }
274
}
    (1-1/1)