Project

General

Profile

Revision 80466da5

ID80466da55174a2c3c29952f75f0645150e1fc2ad
Parent 8fe64a0b
Child 6d7ea74e

Added by Andreas Müller over 1 year ago

move some formatter tests to correct package

View differences:

cdmlib-model/src/test/java/eu/etaxonomy/cdm/format/CategoricalDataFormatterTest.java
1
/**
2
* Copyright (C) 2020 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.format;
10

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

  
16
import eu.etaxonomy.cdm.format.ICdmFormatter.FormatKey;
17
import eu.etaxonomy.cdm.format.description.CategoricalDataFormatter;
18
import eu.etaxonomy.cdm.model.common.Language;
19
import eu.etaxonomy.cdm.model.description.CategoricalData;
20
import eu.etaxonomy.cdm.model.description.Feature;
21
import eu.etaxonomy.cdm.model.description.State;
22
import eu.etaxonomy.cdm.model.description.StateData;
23
import eu.etaxonomy.cdm.model.term.DefaultTermInitializer;
24

  
25
/**
26
 * Test for {@link CategoricalDataFormatter}.
27
 *
28
 * @author a.mueller
29
 * @since 11.03.2020
30
 */
31
public class CategoricalDataFormatterTest {
32

  
33
    private State state1;
34
    private State state2;
35
    private State state3;
36
    private StateData stateData1;
37
    private StateData stateData2;
38
    private StateData stateData3;
39

  
40
    @Before
41
    public void setUp() throws Exception {
42
        state1 = State.NewInstance("state1 text", "state1", "st.1");
43
        state2 = State.NewInstance("state2 text", "state2", "st.2");
44
        state3 = State.NewInstance("state3 text", "state3", "st.3");
45
        stateData1 = StateData.NewInstance(state1);
46
        stateData2 = StateData.NewInstance(state2);
47
        stateData3 = StateData.NewInstance(state3);
48
    }
49

  
50
    @BeforeClass
51
    public static void setUpBeforeClass() throws Exception {
52
        if (Language.DEFAULT() == null){
53
            new DefaultTermInitializer().initialize();
54
        }
55
    }
56

  
57
    @Test
58
    public void testFormat() {
59
        CategoricalData catData = CategoricalData.NewInstance(Feature.HABITAT());
60
        catData.addStateData(stateData1);
61
        FormatKey[] formatKey = null;
62
        CategoricalDataFormatter formatter = new CategoricalDataFormatter(catData, formatKey);
63
        String text = formatter.format(catData, formatKey);
64
        Assert.assertEquals("state1", text);
65
        catData.addStateData(stateData2);
66
        text = formatter.format(catData, formatKey);
67
        Assert.assertEquals("state1, state2", text);
68
        catData.addStateData(stateData3);
69
        text = formatter.format(catData, formatKey);
70
        Assert.assertEquals("state1, state2, state3", text);
71
        //TODO test with modifiers and maybe with other basedata like timeperiod etc.
72
    }
73

  
74
}
cdmlib-model/src/test/java/eu/etaxonomy/cdm/format/QuantitativeDataFormatterTest.java
1
/**
2
* Copyright (C) 2020 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.format;
10

  
11
import java.math.BigDecimal;
12

  
13
import org.junit.Assert;
14
import org.junit.Before;
15
import org.junit.BeforeClass;
16
import org.junit.Test;
17

  
18
import eu.etaxonomy.cdm.format.ICdmFormatter.FormatKey;
19
import eu.etaxonomy.cdm.format.description.QuantitativeDataFormatter;
20
import eu.etaxonomy.cdm.model.common.Language;
21
import eu.etaxonomy.cdm.model.description.Feature;
22
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
23
import eu.etaxonomy.cdm.model.description.QuantitativeData;
24
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
25
import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
26
import eu.etaxonomy.cdm.model.term.DefaultTermInitializer;
27

  
28
/**
29
 * Test for {@link QuantitativeDataFormatter}.
30
 *
31
 * @author a.mueller
32
 * @since 12.03.2020
33
 */
34
public class QuantitativeDataFormatterTest {
35

  
36
    private StatisticalMeasurementValue min1;
37
    private StatisticalMeasurementValue max1;
38
    private StatisticalMeasurementValue n1;
39

  
40
    @Before
41
    public void setUp() throws Exception {
42
        min1 = StatisticalMeasurementValue.NewInstance(StatisticalMeasure.MIN(), new BigDecimal("0.1"));
43
        max1 = StatisticalMeasurementValue.NewInstance(StatisticalMeasure.MAX(), new BigDecimal("1.3"));
44
        n1 = StatisticalMeasurementValue.NewInstance(StatisticalMeasure.SAMPLE_SIZE(), new BigDecimal("2"));
45
    }
46

  
47
    @BeforeClass
48
    public static void setUpBeforeClass() throws Exception {
49
        if (Language.DEFAULT() == null){
50
            new DefaultTermInitializer().initialize();
51
        }
52
    }
53

  
54
    @Test
55
    public void testFormat() {
56
        QuantitativeData quantData = QuantitativeData.NewInstance(Feature.CHROMOSOME_NUMBER());
57
        FormatKey[] formatKey = null;
58

  
59
        quantData.addStatisticalValue(min1);
60
        quantData.addStatisticalValue(max1);
61

  
62
        QuantitativeDataFormatter formatter = new QuantitativeDataFormatter(quantData, formatKey);
63
        String text = formatter.format(quantData, formatKey);
64
        Assert.assertEquals("0.1-1.3", text);
65
        quantData.addStatisticalValue(n1);
66
        MeasurementUnit unit = MeasurementUnit.METER();
67
        quantData.setUnit(unit);
68

  
69
        text = formatter.format(quantData, formatKey);
70
        Assert.assertEquals("0.1-1.3 m [n=2]", text);
71
    }
72
}
cdmlib-model/src/test/java/eu/etaxonomy/cdm/format/TaxonRelationshipFormatterTest.java
1
/**
2
* Copyright (C) 2018 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.format;
10

  
11
import java.util.List;
12

  
13
import org.junit.Assert;
14
import org.junit.Before;
15
import org.junit.BeforeClass;
16
import org.junit.Test;
17

  
18
import eu.etaxonomy.cdm.format.taxon.TaxonRelationshipFormatter;
19
import eu.etaxonomy.cdm.model.agent.Person;
20
import eu.etaxonomy.cdm.model.agent.Team;
21
import eu.etaxonomy.cdm.model.common.Language;
22
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
23
import eu.etaxonomy.cdm.model.name.Rank;
24
import eu.etaxonomy.cdm.model.name.TaxonName;
25
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
26
import eu.etaxonomy.cdm.model.reference.Reference;
27
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
28
import eu.etaxonomy.cdm.model.taxon.Taxon;
29
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
30
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
31
import eu.etaxonomy.cdm.model.term.DefaultTermInitializer;
32
import eu.etaxonomy.cdm.strategy.cache.TaggedCacheHelper;
33
import eu.etaxonomy.cdm.strategy.cache.TaggedText;
34

  
35
/**
36
 * Test to test the {@link TaxonRelationshipFormatter}.
37
 *
38
 * @author a.mueller
39
 * @since 14.08.2018
40
 */
41
public class TaxonRelationshipFormatterTest {
42

  
43
    private static boolean WITHOUT_NAME = true;
44

  
45
    private TaxonRelationship taxonRel;
46
    private Reference relSec;
47

  
48
    private Taxon fromTaxon;
49
    private TaxonName fromName;
50
    private Reference fromSec;
51

  
52
    private Taxon toTaxon;
53
    private TaxonName toName;
54
    private Reference toSec;
55

  
56
    private TaxonRelationshipFormatter formatter;
57
    private boolean reverse;
58

  
59
    private Person toNameAuthor;
60
    private Person macFarlane;
61
    private Person cheek;
62
    private Person toSecAuthor;
63
    private List<Language> languages;
64

  
65
    @BeforeClass
66
    public static void setUpBeforeClass() throws Exception {
67
        if (Language.DEFAULT() == null){
68
            new DefaultTermInitializer().initialize();
69
        }
70
    }
71

  
72
    @Before
73
    public void setUp() throws Exception {
74
        fromName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
75
        fromName.setGenusOrUninomial("Abies");
76
        fromName.setSpecificEpithet("alba");
77

  
78
        toName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
79
        toName.setGenusOrUninomial("Pinus");
80
        toName.setSpecificEpithet("pinova");
81
        toNameAuthor = Person.NewInstance("Mill.", "Miller", "A.", "Andrew");
82
        toName.setCombinationAuthorship(toNameAuthor);
83

  
84
        fromSec = ReferenceFactory.newGeneric();
85
        fromSec.setTitle("From Sec");
86
        String initials = "J.M.";
87
        macFarlane = Person.NewInstance(null, "Macfarlane", initials, null);
88
        fromSec.setAuthorship(macFarlane);
89
        fromSec.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(1918));
90

  
91
        relSec = ReferenceFactory.newGeneric();
92
        relSec.setTitle("From rel reference");
93
        initials = null; //"M.R.";
94
        cheek = Person.NewInstance(null, "Cheek", initials, null);
95
        relSec.setAuthorship(cheek);
96
        relSec.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(1919));
97

  
98
        toSec = ReferenceFactory.newGeneric();
99
        toSec.setTitle("To Sec");
100
        toSecAuthor = Person.NewTitledInstance("ToSecAuthor");
101
        toSec.setAuthorship(toSecAuthor);
102
        toSec.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(1928));
103

  
104
        fromTaxon = Taxon.NewInstance(fromName, fromSec);
105
        toTaxon = Taxon.NewInstance(toName, toSec);
106

  
107
        TaxonRelationshipType type = TaxonRelationshipType.MISAPPLIED_NAME_FOR();
108
        taxonRel = fromTaxon.addTaxonRelation(toTaxon, type, relSec, "123");
109
        reverse = false;
110

  
111
        formatter = new TaxonRelationshipFormatter();
112

  
113
        languages = null;
114
    }
115

  
116
    /**
117
     * Test method for {@link eu.etaxonomy.cdm.format.taxon.TaxonRelationshipFormatter#getTaggedText(eu.etaxonomy.cdm.model.taxon.TaxonRelationship, boolean, java.util.List)}.
118
     */
119
    @Test
120
    public void testGetTaggedTextMisappliedName() {
121

  
122
        reverse = true;
123
        String inverseSymbol = TaxonRelationshipType.MISAPPLIED_NAME_FOR().getInverseSymbol();
124
        String symbol = TaxonRelationshipType.MISAPPLIED_NAME_FOR().getSymbol();
125

  
126
        List<TaggedText> tags = formatter.getTaggedText(taxonRel, reverse, languages);
127
        String str = TaggedCacheHelper.createString(tags);
128
        Assert.assertEquals(inverseSymbol + " \"Abies alba\" sensu Macfarlane 1918, err. sec. Cheek 1919: 123", str);
129

  
130
        //reverse
131
        tags = formatter.getTaggedText(taxonRel, !reverse, languages);
132
        str = TaggedCacheHelper.createString(tags);
133
        Assert.assertEquals(symbol + " Pinus pinova Mill. sec. ToSecAuthor 1928, rel. sec. Cheek 1919: 123", str);
134

  
135
        //auctores
136
        fromTaxon.setAppendedPhrase("auctores");
137
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
138
        str = TaggedCacheHelper.createString(tags);
139
        Assert.assertEquals(inverseSymbol + " \"Abies alba\" auctores sensu Macfarlane 1918, err. sec. Cheek 1919: 123", str);
140

  
141
        fromTaxon.setSec(null);
142
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
143
        str = TaggedCacheHelper.createString(tags);
144
        Assert.assertEquals(inverseSymbol + " \"Abies alba\" auctores, err. sec. Cheek 1919: 123", str);
145

  
146
        fromTaxon.setAppendedPhrase("");
147
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
148
        str = TaggedCacheHelper.createString(tags);
149
        Assert.assertEquals(inverseSymbol + " \"Abies alba\" auct., err. sec. Cheek 1919: 123", str);
150

  
151
        fromTaxon.setDoubtful(true);
152
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
153
        str = TaggedCacheHelper.createString(tags);
154
        System.out.println(str);
155
        Assert.assertEquals(inverseSymbol + " ?\u202F\"Abies alba\" auct., err. sec. Cheek 1919: 123", str);
156

  
157
    }
158

  
159
    @Test
160
    public void testGetTaggedTextConceptRelations() {
161

  
162
        reverse = false;
163

  
164
        TaxonRelationshipType relType = TaxonRelationshipType.INCLUDES();
165

  
166
        final String SYMBOL = relType.getSymbol();
167

  
168
        taxonRel.setType(relType);
169
        List<TaggedText> tags = formatter.getTaggedText(taxonRel, reverse, languages);
170
        String str = TaggedCacheHelper.createString(tags);
171
        Assert.assertEquals(SYMBOL + " Pinus pinova Mill. sec. ToSecAuthor 1928, rel. sec. Cheek 1919: 123", str);
172

  
173
        tags = formatter.getTaggedText(taxonRel, !reverse, languages);
174
        str = TaggedCacheHelper.createString(tags);
175
        Assert.assertEquals(relType.getInverseSymbol() + " Abies alba sec. Macfarlane 1918, rel. sec. Cheek 1919: 123", str);
176

  
177
        toTaxon.setAppendedPhrase("sensu stricto");
178
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
179
        str = TaggedCacheHelper.createString(tags);
180
        Assert.assertEquals(SYMBOL + " Pinus pinova Mill. sensu stricto sec. ToSecAuthor 1928, rel. sec. Cheek 1919: 123", str);
181

  
182

  
183
        toTaxon.setSec(null);
184
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
185
        str = TaggedCacheHelper.createString(tags);
186
        Assert.assertEquals(SYMBOL + " Pinus pinova Mill. sensu stricto, rel. sec. Cheek 1919: 123", str);
187

  
188
        toTaxon.setAppendedPhrase("");
189
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
190
        str = TaggedCacheHelper.createString(tags);
191
        Assert.assertEquals(SYMBOL + " Pinus pinova Mill. sec. ???, rel. sec. Cheek 1919: 123", str);
192

  
193
        taxonRel.setDoubtful(true);
194
        toTaxon.setAppendedPhrase("");
195
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
196
        str = TaggedCacheHelper.createString(tags);
197
        Assert.assertEquals("?" + SYMBOL + " Pinus pinova Mill. sec. ???, rel. sec. Cheek 1919: 123", str);
198

  
199
    }
200

  
201
    @Test
202
    public void testGetTaggedTextSynonymRelations() {
203

  
204
        reverse = false;
205

  
206
        TaxonRelationshipType type = TaxonRelationshipType.PRO_PARTE_SYNONYM_FOR();
207
        final String symbol = type.getSymbol();
208

  
209

  
210
        taxonRel.setType(type);
211
        List<TaggedText> tags = formatter.getTaggedText(taxonRel, reverse, languages);
212
        String str = TaggedCacheHelper.createString(tags);
213
        Assert.assertEquals(symbol + " Pinus pinova Mill. sec. ToSecAuthor 1928, syn. sec. Cheek 1919: 123", str);
214

  
215
        tags = formatter.getTaggedText(taxonRel, !reverse, languages);
216
        str = TaggedCacheHelper.createString(tags);
217
        Assert.assertEquals(type.getInverseSymbol() + " Abies alba sec. Macfarlane 1918, syn. sec. Cheek 1919: 123", str);
218

  
219
        toTaxon.setAppendedPhrase("sensu lato");
220
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
221
        str = TaggedCacheHelper.createString(tags);
222
        Assert.assertEquals(symbol + " Pinus pinova Mill. sensu lato sec. ToSecAuthor 1928, syn. sec. Cheek 1919: 123", str);
223

  
224

  
225
        toTaxon.setSec(null);
226
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
227
        str = TaggedCacheHelper.createString(tags);
228
        Assert.assertEquals(symbol + " Pinus pinova Mill. sensu lato, syn. sec. Cheek 1919: 123", str);
229

  
230
        toTaxon.setAppendedPhrase("");
231
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
232
        str = TaggedCacheHelper.createString(tags);
233
        Assert.assertEquals(symbol + " Pinus pinova Mill. sec. ???, syn. sec. Cheek 1919: 123", str);
234

  
235
    }
236

  
237
    @Test
238
    public void testGetFamilyNames() {
239

  
240
        //Test start condition with single person
241
        List<TaggedText> tags = formatter.getTaggedText(taxonRel, reverse, languages);
242
        String str = TaggedCacheHelper.createString(tags);
243
        Assert.assertFalse("Formatted text should not contain the team correctly formatted", str.contains("Macfarlane & Cheek"));
244

  
245
        //use team
246
        Team secRelTeam = Team.NewInstance();
247
        secRelTeam.addTeamMember(macFarlane);
248
        secRelTeam.addTeamMember(cheek);
249
        relSec.setAuthorship(secRelTeam);
250

  
251
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
252
        str = TaggedCacheHelper.createString(tags);
253
        System.out.println(str);
254
        Assert.assertTrue(str.contains("rel. sec. Macfarlane & Cheek 1919"));
255

  
256
        //add third member
257
        secRelTeam.addTeamMember(toSecAuthor);
258
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
259
        str = TaggedCacheHelper.createString(tags);
260
        System.out.println(str);
261
        Assert.assertTrue(str.contains("rel. sec. Macfarlane, Cheek & ToSecAuthor 1919"));
262

  
263
        //add et al.
264
        secRelTeam.setHasMoreMembers(true);
265
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
266
        str = TaggedCacheHelper.createString(tags);
267
        System.out.println(str);
268
        Assert.assertTrue(str.contains("rel. sec. Macfarlane, Cheek, ToSecAuthor & al. 1919"));
269

  
270
    }
271

  
272
    @Test
273
    public void testGetTaggedTextMisappliedNameWithoutName() {
274

  
275
        reverse = true;
276
        String inverseSymbol = TaxonRelationshipType.MISAPPLIED_NAME_FOR().getInverseSymbol();
277
        String symbol = TaxonRelationshipType.MISAPPLIED_NAME_FOR().getSymbol();
278

  
279
        List<TaggedText> tags = formatter.getTaggedText(taxonRel, reverse, languages, WITHOUT_NAME);
280
        String str = TaggedCacheHelper.createString(tags);
281
        Assert.assertEquals(inverseSymbol + " sensu Macfarlane 1918, err. sec. Cheek 1919: 123", str);
282

  
283
        //reverse
284
        tags = formatter.getTaggedText(taxonRel, !reverse, languages, WITHOUT_NAME);
285
        str = TaggedCacheHelper.createString(tags);
286
        Assert.assertEquals(symbol + " sec. ToSecAuthor 1928, rel. sec. Cheek 1919: 123", str);
287

  
288
        //auctores
289
        fromTaxon.setAppendedPhrase("auctores");
290
        tags = formatter.getTaggedText(taxonRel, reverse, languages, WITHOUT_NAME);
291
        str = TaggedCacheHelper.createString(tags);
292
        Assert.assertEquals(inverseSymbol + " auctores sensu Macfarlane 1918, err. sec. Cheek 1919: 123", str);
293

  
294
        fromTaxon.setSec(null);
295
        fromTaxon.setAppendedPhrase("");
296
        tags = formatter.getTaggedText(taxonRel, reverse, languages, WITHOUT_NAME);
297
        str = TaggedCacheHelper.createString(tags);
298
        Assert.assertEquals(inverseSymbol + " auct., err. sec. Cheek 1919: 123", str);
299

  
300
        fromTaxon.setDoubtful(true);
301
        tags = formatter.getTaggedText(taxonRel, reverse, languages, WITHOUT_NAME);
302
        str = TaggedCacheHelper.createString(tags);
303
        System.out.println(str);
304
        Assert.assertEquals(inverseSymbol + " ?\u202F auct., err. sec. Cheek 1919: 123", str);
305

  
306
    }
307

  
308
    @Test
309
    public void testGetTaggedTextConceptRelationsWithoutName() {
310

  
311
        reverse = false;
312

  
313
        TaxonRelationshipType relType = TaxonRelationshipType.INCLUDES();
314

  
315
        final String SYMBOL = relType.getSymbol();
316

  
317
        taxonRel.setType(relType);
318
        List<TaggedText> tags = formatter.getTaggedText(taxonRel, reverse, languages, WITHOUT_NAME);
319
        String str = TaggedCacheHelper.createString(tags);
320
        Assert.assertEquals(SYMBOL + " sec. ToSecAuthor 1928, rel. sec. Cheek 1919: 123", str);
321

  
322
        tags = formatter.getTaggedText(taxonRel, !reverse, languages, WITHOUT_NAME);
323
        str = TaggedCacheHelper.createString(tags);
324
        Assert.assertEquals(relType.getInverseSymbol() + " sec. Macfarlane 1918, rel. sec. Cheek 1919: 123", str);
325

  
326
        toTaxon.setAppendedPhrase("sensu stricto");
327
        tags = formatter.getTaggedText(taxonRel, reverse, languages, WITHOUT_NAME);
328
        str = TaggedCacheHelper.createString(tags);
329
        Assert.assertEquals(SYMBOL + " sensu stricto sec. ToSecAuthor 1928, rel. sec. Cheek 1919: 123", str);
330

  
331
        toTaxon.setSec(null);
332
        toTaxon.setAppendedPhrase("");
333
        tags = formatter.getTaggedText(taxonRel, reverse, languages, WITHOUT_NAME);
334
        str = TaggedCacheHelper.createString(tags);
335
        Assert.assertEquals(SYMBOL + " sec. ???, rel. sec. Cheek 1919: 123", str);
336

  
337
        taxonRel.setDoubtful(true);
338
        toTaxon.setAppendedPhrase("");
339
        tags = formatter.getTaggedText(taxonRel, reverse, languages, WITHOUT_NAME);
340
        str = TaggedCacheHelper.createString(tags);
341
        Assert.assertEquals("?" + SYMBOL + " sec. ???, rel. sec. Cheek 1919: 123", str);
342

  
343
    }
344

  
345
}
cdmlib-model/src/test/java/eu/etaxonomy/cdm/format/description/CategoricalDataFormatterTest.java
1
/**
2
* Copyright (C) 2020 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.format.description;
10

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

  
16
import eu.etaxonomy.cdm.format.ICdmFormatter;
17
import eu.etaxonomy.cdm.format.ICdmFormatter.FormatKey;
18
import eu.etaxonomy.cdm.format.description.CategoricalDataFormatter;
19
import eu.etaxonomy.cdm.model.common.Language;
20
import eu.etaxonomy.cdm.model.description.CategoricalData;
21
import eu.etaxonomy.cdm.model.description.Feature;
22
import eu.etaxonomy.cdm.model.description.State;
23
import eu.etaxonomy.cdm.model.description.StateData;
24
import eu.etaxonomy.cdm.model.term.DefaultTermInitializer;
25

  
26
/**
27
 * Test for {@link CategoricalDataFormatter}.
28
 *
29
 * @author a.mueller
30
 * @since 11.03.2020
31
 */
32
public class CategoricalDataFormatterTest {
33

  
34
    private State state1;
35
    private State state2;
36
    private State state3;
37
    private StateData stateData1;
38
    private StateData stateData2;
39
    private StateData stateData3;
40

  
41
    @Before
42
    public void setUp() throws Exception {
43
        state1 = State.NewInstance("state1 text", "state1", "st.1");
44
        state2 = State.NewInstance("state2 text", "state2", "st.2");
45
        state3 = State.NewInstance("state3 text", "state3", "st.3");
46
        stateData1 = StateData.NewInstance(state1);
47
        stateData2 = StateData.NewInstance(state2);
48
        stateData3 = StateData.NewInstance(state3);
49
    }
50

  
51
    @BeforeClass
52
    public static void setUpBeforeClass() throws Exception {
53
        if (Language.DEFAULT() == null){
54
            new DefaultTermInitializer().initialize();
55
        }
56
    }
57

  
58
    @Test
59
    public void testFormat() {
60
        CategoricalData catData = CategoricalData.NewInstance(Feature.HABITAT());
61
        catData.addStateData(stateData1);
62
        FormatKey[] formatKey = null;
63
        CategoricalDataFormatter formatter = new CategoricalDataFormatter(catData, formatKey);
64
        String text = formatter.format(catData, formatKey);
65
        Assert.assertEquals("state1", text);
66
        catData.addStateData(stateData2);
67
        text = formatter.format(catData, formatKey);
68
        Assert.assertEquals("state1, state2", text);
69
        catData.addStateData(stateData3);
70
        text = formatter.format(catData, formatKey);
71
        Assert.assertEquals("state1, state2, state3", text);
72
        //TODO test with modifiers and maybe with other basedata like timeperiod etc.
73
    }
74

  
75
}
cdmlib-model/src/test/java/eu/etaxonomy/cdm/format/description/QuantitativeDataFormatterTest.java
1
/**
2
* Copyright (C) 2020 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.format.description;
10

  
11
import java.math.BigDecimal;
12

  
13
import org.junit.Assert;
14
import org.junit.Before;
15
import org.junit.BeforeClass;
16
import org.junit.Test;
17

  
18
import eu.etaxonomy.cdm.format.ICdmFormatter;
19
import eu.etaxonomy.cdm.format.ICdmFormatter.FormatKey;
20
import eu.etaxonomy.cdm.format.description.QuantitativeDataFormatter;
21
import eu.etaxonomy.cdm.model.common.Language;
22
import eu.etaxonomy.cdm.model.description.Feature;
23
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
24
import eu.etaxonomy.cdm.model.description.QuantitativeData;
25
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
26
import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
27
import eu.etaxonomy.cdm.model.term.DefaultTermInitializer;
28

  
29
/**
30
 * Test for {@link QuantitativeDataFormatter}.
31
 *
32
 * @author a.mueller
33
 * @since 12.03.2020
34
 */
35
public class QuantitativeDataFormatterTest {
36

  
37
    private StatisticalMeasurementValue min1;
38
    private StatisticalMeasurementValue max1;
39
    private StatisticalMeasurementValue n1;
40

  
41
    @Before
42
    public void setUp() throws Exception {
43
        min1 = StatisticalMeasurementValue.NewInstance(StatisticalMeasure.MIN(), new BigDecimal("0.1"));
44
        max1 = StatisticalMeasurementValue.NewInstance(StatisticalMeasure.MAX(), new BigDecimal("1.3"));
45
        n1 = StatisticalMeasurementValue.NewInstance(StatisticalMeasure.SAMPLE_SIZE(), new BigDecimal("2"));
46
    }
47

  
48
    @BeforeClass
49
    public static void setUpBeforeClass() throws Exception {
50
        if (Language.DEFAULT() == null){
51
            new DefaultTermInitializer().initialize();
52
        }
53
    }
54

  
55
    @Test
56
    public void testFormat() {
57
        QuantitativeData quantData = QuantitativeData.NewInstance(Feature.CHROMOSOME_NUMBER());
58
        FormatKey[] formatKey = null;
59

  
60
        quantData.addStatisticalValue(min1);
61
        quantData.addStatisticalValue(max1);
62

  
63
        QuantitativeDataFormatter formatter = new QuantitativeDataFormatter(quantData, formatKey);
64
        String text = formatter.format(quantData, formatKey);
65
        Assert.assertEquals("0.1-1.3", text);
66
        quantData.addStatisticalValue(n1);
67
        MeasurementUnit unit = MeasurementUnit.METER();
68
        quantData.setUnit(unit);
69

  
70
        text = formatter.format(quantData, formatKey);
71
        Assert.assertEquals("0.1-1.3 m [n=2]", text);
72
    }
73
}
cdmlib-model/src/test/java/eu/etaxonomy/cdm/format/taxon/TaxonRelationshipFormatterTest.java
1
/**
2
* Copyright (C) 2018 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.format.taxon;
10

  
11
import java.util.List;
12

  
13
import org.junit.Assert;
14
import org.junit.Before;
15
import org.junit.BeforeClass;
16
import org.junit.Test;
17

  
18
import eu.etaxonomy.cdm.format.taxon.TaxonRelationshipFormatter;
19
import eu.etaxonomy.cdm.model.agent.Person;
20
import eu.etaxonomy.cdm.model.agent.Team;
21
import eu.etaxonomy.cdm.model.common.Language;
22
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
23
import eu.etaxonomy.cdm.model.name.Rank;
24
import eu.etaxonomy.cdm.model.name.TaxonName;
25
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
26
import eu.etaxonomy.cdm.model.reference.Reference;
27
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
28
import eu.etaxonomy.cdm.model.taxon.Taxon;
29
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
30
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
31
import eu.etaxonomy.cdm.model.term.DefaultTermInitializer;
32
import eu.etaxonomy.cdm.strategy.cache.TaggedCacheHelper;
33
import eu.etaxonomy.cdm.strategy.cache.TaggedText;
34

  
35
/**
36
 * Test to test the {@link TaxonRelationshipFormatter}.
37
 *
38
 * @author a.mueller
39
 * @since 14.08.2018
40
 */
41
public class TaxonRelationshipFormatterTest {
42

  
43
    private static boolean WITHOUT_NAME = true;
44

  
45
    private TaxonRelationship taxonRel;
46
    private Reference relSec;
47

  
48
    private Taxon fromTaxon;
49
    private TaxonName fromName;
50
    private Reference fromSec;
51

  
52
    private Taxon toTaxon;
53
    private TaxonName toName;
54
    private Reference toSec;
55

  
56
    private TaxonRelationshipFormatter formatter;
57
    private boolean reverse;
58

  
59
    private Person toNameAuthor;
60
    private Person macFarlane;
61
    private Person cheek;
62
    private Person toSecAuthor;
63
    private List<Language> languages;
64

  
65
    @BeforeClass
66
    public static void setUpBeforeClass() throws Exception {
67
        if (Language.DEFAULT() == null){
68
            new DefaultTermInitializer().initialize();
69
        }
70
    }
71

  
72
    @Before
73
    public void setUp() throws Exception {
74
        fromName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
75
        fromName.setGenusOrUninomial("Abies");
76
        fromName.setSpecificEpithet("alba");
77

  
78
        toName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
79
        toName.setGenusOrUninomial("Pinus");
80
        toName.setSpecificEpithet("pinova");
81
        toNameAuthor = Person.NewInstance("Mill.", "Miller", "A.", "Andrew");
82
        toName.setCombinationAuthorship(toNameAuthor);
83

  
84
        fromSec = ReferenceFactory.newGeneric();
85
        fromSec.setTitle("From Sec");
86
        String initials = "J.M.";
87
        macFarlane = Person.NewInstance(null, "Macfarlane", initials, null);
88
        fromSec.setAuthorship(macFarlane);
89
        fromSec.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(1918));
90

  
91
        relSec = ReferenceFactory.newGeneric();
92
        relSec.setTitle("From rel reference");
93
        initials = null; //"M.R.";
94
        cheek = Person.NewInstance(null, "Cheek", initials, null);
95
        relSec.setAuthorship(cheek);
96
        relSec.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(1919));
97

  
98
        toSec = ReferenceFactory.newGeneric();
99
        toSec.setTitle("To Sec");
100
        toSecAuthor = Person.NewTitledInstance("ToSecAuthor");
101
        toSec.setAuthorship(toSecAuthor);
102
        toSec.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(1928));
103

  
104
        fromTaxon = Taxon.NewInstance(fromName, fromSec);
105
        toTaxon = Taxon.NewInstance(toName, toSec);
106

  
107
        TaxonRelationshipType type = TaxonRelationshipType.MISAPPLIED_NAME_FOR();
108
        taxonRel = fromTaxon.addTaxonRelation(toTaxon, type, relSec, "123");
109
        reverse = false;
110

  
111
        formatter = new TaxonRelationshipFormatter();
112

  
113
        languages = null;
114
    }
115

  
116
    /**
117
     * Test method for {@link eu.etaxonomy.cdm.format.taxon.TaxonRelationshipFormatter#getTaggedText(eu.etaxonomy.cdm.model.taxon.TaxonRelationship, boolean, java.util.List)}.
118
     */
119
    @Test
120
    public void testGetTaggedTextMisappliedName() {
121

  
122
        reverse = true;
123
        String inverseSymbol = TaxonRelationshipType.MISAPPLIED_NAME_FOR().getInverseSymbol();
124
        String symbol = TaxonRelationshipType.MISAPPLIED_NAME_FOR().getSymbol();
125

  
126
        List<TaggedText> tags = formatter.getTaggedText(taxonRel, reverse, languages);
127
        String str = TaggedCacheHelper.createString(tags);
128
        Assert.assertEquals(inverseSymbol + " \"Abies alba\" sensu Macfarlane 1918, err. sec. Cheek 1919: 123", str);
129

  
130
        //reverse
131
        tags = formatter.getTaggedText(taxonRel, !reverse, languages);
132
        str = TaggedCacheHelper.createString(tags);
133
        Assert.assertEquals(symbol + " Pinus pinova Mill. sec. ToSecAuthor 1928, rel. sec. Cheek 1919: 123", str);
134

  
135
        //auctores
136
        fromTaxon.setAppendedPhrase("auctores");
137
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
138
        str = TaggedCacheHelper.createString(tags);
139
        Assert.assertEquals(inverseSymbol + " \"Abies alba\" auctores sensu Macfarlane 1918, err. sec. Cheek 1919: 123", str);
140

  
141
        fromTaxon.setSec(null);
142
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
143
        str = TaggedCacheHelper.createString(tags);
144
        Assert.assertEquals(inverseSymbol + " \"Abies alba\" auctores, err. sec. Cheek 1919: 123", str);
145

  
146
        fromTaxon.setAppendedPhrase("");
147
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
148
        str = TaggedCacheHelper.createString(tags);
149
        Assert.assertEquals(inverseSymbol + " \"Abies alba\" auct., err. sec. Cheek 1919: 123", str);
150

  
151
        fromTaxon.setDoubtful(true);
152
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
153
        str = TaggedCacheHelper.createString(tags);
154
        System.out.println(str);
155
        Assert.assertEquals(inverseSymbol + " ?\u202F\"Abies alba\" auct., err. sec. Cheek 1919: 123", str);
156

  
157
    }
158

  
159
    @Test
160
    public void testGetTaggedTextConceptRelations() {
161

  
162
        reverse = false;
163

  
164
        TaxonRelationshipType relType = TaxonRelationshipType.INCLUDES();
165

  
166
        final String SYMBOL = relType.getSymbol();
167

  
168
        taxonRel.setType(relType);
169
        List<TaggedText> tags = formatter.getTaggedText(taxonRel, reverse, languages);
170
        String str = TaggedCacheHelper.createString(tags);
171
        Assert.assertEquals(SYMBOL + " Pinus pinova Mill. sec. ToSecAuthor 1928, rel. sec. Cheek 1919: 123", str);
172

  
173
        tags = formatter.getTaggedText(taxonRel, !reverse, languages);
174
        str = TaggedCacheHelper.createString(tags);
175
        Assert.assertEquals(relType.getInverseSymbol() + " Abies alba sec. Macfarlane 1918, rel. sec. Cheek 1919: 123", str);
176

  
177
        toTaxon.setAppendedPhrase("sensu stricto");
178
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
179
        str = TaggedCacheHelper.createString(tags);
180
        Assert.assertEquals(SYMBOL + " Pinus pinova Mill. sensu stricto sec. ToSecAuthor 1928, rel. sec. Cheek 1919: 123", str);
181

  
182

  
183
        toTaxon.setSec(null);
184
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
185
        str = TaggedCacheHelper.createString(tags);
186
        Assert.assertEquals(SYMBOL + " Pinus pinova Mill. sensu stricto, rel. sec. Cheek 1919: 123", str);
187

  
188
        toTaxon.setAppendedPhrase("");
189
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
190
        str = TaggedCacheHelper.createString(tags);
191
        Assert.assertEquals(SYMBOL + " Pinus pinova Mill. sec. ???, rel. sec. Cheek 1919: 123", str);
192

  
193
        taxonRel.setDoubtful(true);
194
        toTaxon.setAppendedPhrase("");
195
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
196
        str = TaggedCacheHelper.createString(tags);
197
        Assert.assertEquals("?" + SYMBOL + " Pinus pinova Mill. sec. ???, rel. sec. Cheek 1919: 123", str);
198

  
199
    }
200

  
201
    @Test
202
    public void testGetTaggedTextSynonymRelations() {
203

  
204
        reverse = false;
205

  
206
        TaxonRelationshipType type = TaxonRelationshipType.PRO_PARTE_SYNONYM_FOR();
207
        final String symbol = type.getSymbol();
208

  
209

  
210
        taxonRel.setType(type);
211
        List<TaggedText> tags = formatter.getTaggedText(taxonRel, reverse, languages);
212
        String str = TaggedCacheHelper.createString(tags);
213
        Assert.assertEquals(symbol + " Pinus pinova Mill. sec. ToSecAuthor 1928, syn. sec. Cheek 1919: 123", str);
214

  
215
        tags = formatter.getTaggedText(taxonRel, !reverse, languages);
216
        str = TaggedCacheHelper.createString(tags);
217
        Assert.assertEquals(type.getInverseSymbol() + " Abies alba sec. Macfarlane 1918, syn. sec. Cheek 1919: 123", str);
218

  
219
        toTaxon.setAppendedPhrase("sensu lato");
220
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
221
        str = TaggedCacheHelper.createString(tags);
222
        Assert.assertEquals(symbol + " Pinus pinova Mill. sensu lato sec. ToSecAuthor 1928, syn. sec. Cheek 1919: 123", str);
223

  
224

  
225
        toTaxon.setSec(null);
226
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
227
        str = TaggedCacheHelper.createString(tags);
228
        Assert.assertEquals(symbol + " Pinus pinova Mill. sensu lato, syn. sec. Cheek 1919: 123", str);
229

  
230
        toTaxon.setAppendedPhrase("");
231
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
232
        str = TaggedCacheHelper.createString(tags);
233
        Assert.assertEquals(symbol + " Pinus pinova Mill. sec. ???, syn. sec. Cheek 1919: 123", str);
234

  
235
    }
236

  
237
    @Test
238
    public void testGetFamilyNames() {
239

  
240
        //Test start condition with single person
241
        List<TaggedText> tags = formatter.getTaggedText(taxonRel, reverse, languages);
242
        String str = TaggedCacheHelper.createString(tags);
243
        Assert.assertFalse("Formatted text should not contain the team correctly formatted", str.contains("Macfarlane & Cheek"));
244

  
245
        //use team
246
        Team secRelTeam = Team.NewInstance();
247
        secRelTeam.addTeamMember(macFarlane);
248
        secRelTeam.addTeamMember(cheek);
249
        relSec.setAuthorship(secRelTeam);
250

  
251
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
252
        str = TaggedCacheHelper.createString(tags);
253
        System.out.println(str);
254
        Assert.assertTrue(str.contains("rel. sec. Macfarlane & Cheek 1919"));
255

  
256
        //add third member
257
        secRelTeam.addTeamMember(toSecAuthor);
258
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
259
        str = TaggedCacheHelper.createString(tags);
260
        System.out.println(str);
261
        Assert.assertTrue(str.contains("rel. sec. Macfarlane, Cheek & ToSecAuthor 1919"));
262

  
263
        //add et al.
264
        secRelTeam.setHasMoreMembers(true);
265
        tags = formatter.getTaggedText(taxonRel, reverse, languages);
266
        str = TaggedCacheHelper.createString(tags);
267
        System.out.println(str);
268
        Assert.assertTrue(str.contains("rel. sec. Macfarlane, Cheek, ToSecAuthor & al. 1919"));
269

  
270
    }
271

  
272
    @Test
273
    public void testGetTaggedTextMisappliedNameWithoutName() {
274

  
275
        reverse = true;
276
        String inverseSymbol = TaxonRelationshipType.MISAPPLIED_NAME_FOR().getInverseSymbol();
277
        String symbol = TaxonRelationshipType.MISAPPLIED_NAME_FOR().getSymbol();
278

  
279
        List<TaggedText> tags = formatter.getTaggedText(taxonRel, reverse, languages, WITHOUT_NAME);
280
        String str = TaggedCacheHelper.createString(tags);
281
        Assert.assertEquals(inverseSymbol + " sensu Macfarlane 1918, err. sec. Cheek 1919: 123", str);
282

  
283
        //reverse
284
        tags = formatter.getTaggedText(taxonRel, !reverse, languages, WITHOUT_NAME);
285
        str = TaggedCacheHelper.createString(tags);
286
        Assert.assertEquals(symbol + " sec. ToSecAuthor 1928, rel. sec. Cheek 1919: 123", str);
287

  
288
        //auctores
289
        fromTaxon.setAppendedPhrase("auctores");
290
        tags = formatter.getTaggedText(taxonRel, reverse, languages, WITHOUT_NAME);
291
        str = TaggedCacheHelper.createString(tags);
292
        Assert.assertEquals(inverseSymbol + " auctores sensu Macfarlane 1918, err. sec. Cheek 1919: 123", str);
293

  
294
        fromTaxon.setSec(null);
295
        fromTaxon.setAppendedPhrase("");
296
        tags = formatter.getTaggedText(taxonRel, reverse, languages, WITHOUT_NAME);
297
        str = TaggedCacheHelper.createString(tags);
298
        Assert.assertEquals(inverseSymbol + " auct., err. sec. Cheek 1919: 123", str);
299

  
300
        fromTaxon.setDoubtful(true);
301
        tags = formatter.getTaggedText(taxonRel, reverse, languages, WITHOUT_NAME);
302
        str = TaggedCacheHelper.createString(tags);
303
        System.out.println(str);
304
        Assert.assertEquals(inverseSymbol + " ?\u202F auct., err. sec. Cheek 1919: 123", str);
305

  
306
    }
307

  
308
    @Test
309
    public void testGetTaggedTextConceptRelationsWithoutName() {
310

  
311
        reverse = false;
312

  
313
        TaxonRelationshipType relType = TaxonRelationshipType.INCLUDES();
314

  
315
        final String SYMBOL = relType.getSymbol();
316

  
317
        taxonRel.setType(relType);
318
        List<TaggedText> tags = formatter.getTaggedText(taxonRel, reverse, languages, WITHOUT_NAME);
319
        String str = TaggedCacheHelper.createString(tags);
320
        Assert.assertEquals(SYMBOL + " sec. ToSecAuthor 1928, rel. sec. Cheek 1919: 123", str);
321

  
322
        tags = formatter.getTaggedText(taxonRel, !reverse, languages, WITHOUT_NAME);
323
        str = TaggedCacheHelper.createString(tags);
324
        Assert.assertEquals(relType.getInverseSymbol() + " sec. Macfarlane 1918, rel. sec. Cheek 1919: 123", str);
325

  
326
        toTaxon.setAppendedPhrase("sensu stricto");
327
        tags = formatter.getTaggedText(taxonRel, reverse, languages, WITHOUT_NAME);
328
        str = TaggedCacheHelper.createString(tags);
329
        Assert.assertEquals(SYMBOL + " sensu stricto sec. ToSecAuthor 1928, rel. sec. Cheek 1919: 123", str);
330

  
331
        toTaxon.setSec(null);
332
        toTaxon.setAppendedPhrase("");
333
        tags = formatter.getTaggedText(taxonRel, reverse, languages, WITHOUT_NAME);
334
        str = TaggedCacheHelper.createString(tags);
335
        Assert.assertEquals(SYMBOL + " sec. ???, rel. sec. Cheek 1919: 123", str);
336

  
337
        taxonRel.setDoubtful(true);
338
        toTaxon.setAppendedPhrase("");
339
        tags = formatter.getTaggedText(taxonRel, reverse, languages, WITHOUT_NAME);
340
        str = TaggedCacheHelper.createString(tags);
341
        Assert.assertEquals("?" + SYMBOL + " sec. ???, rel. sec. Cheek 1919: 123", str);
342

  
343
    }
344

  
345
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)