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;
14
import org.apache.logging.log4j.Logger;
15
import org.joda.time.DateTime;
16
import org.junit.Assert;
17
import org.junit.Before;
18
import org.junit.Test;
19

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

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

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

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

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

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

    
75
//******************************* TESTS ********************************************************
76

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
234
    }
235

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

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

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

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

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