Project

General

Profile

Download (33.4 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2009 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

    
10
package eu.etaxonomy.cdm.api.service;
11

    
12
import static org.junit.Assert.assertEquals;
13
import static org.junit.Assert.assertNotNull;
14

    
15
import java.io.FileNotFoundException;
16
import java.util.List;
17
import java.util.UUID;
18

    
19
import org.apache.log4j.Level;
20
import org.apache.log4j.Logger;
21
import org.junit.Assert;
22
import org.junit.Before;
23
import org.junit.Test;
24
import org.unitils.dbunit.annotation.DataSet;
25
import org.unitils.spring.annotation.SpringBeanByType;
26

    
27
import eu.etaxonomy.cdm.api.service.config.FindTaxaAndNamesConfiguratorImpl;
28
import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
29
import eu.etaxonomy.cdm.api.service.pager.Pager;
30
import eu.etaxonomy.cdm.api.service.search.ICdmMassIndexer;
31
import eu.etaxonomy.cdm.common.UTF8;
32
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
33
import eu.etaxonomy.cdm.model.common.Language;
34
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
35
import eu.etaxonomy.cdm.model.description.Distribution;
36
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
37
import eu.etaxonomy.cdm.model.description.TaxonDescription;
38
import eu.etaxonomy.cdm.model.description.TextData;
39
import eu.etaxonomy.cdm.model.location.Country;
40
import eu.etaxonomy.cdm.model.location.NamedArea;
41
import eu.etaxonomy.cdm.model.name.IBotanicalName;
42
import eu.etaxonomy.cdm.model.name.Rank;
43
import eu.etaxonomy.cdm.model.name.TaxonName;
44
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
45
import eu.etaxonomy.cdm.model.reference.Reference;
46
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
47
import eu.etaxonomy.cdm.model.taxon.Classification;
48
import eu.etaxonomy.cdm.model.taxon.Synonym;
49
import eu.etaxonomy.cdm.model.taxon.SynonymType;
50
import eu.etaxonomy.cdm.model.taxon.Taxon;
51
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
52
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
53
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
54
import eu.etaxonomy.cdm.persistence.query.MatchMode;
55
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
56
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
57

    
58
/**
59
 * @author a.kohlbecker
60
 * @since 04.02.2009
61
 */
62
public class TaxonServiceSearchTaxaAndNamesTest extends CdmTransactionalIntegrationTest {
63

    
64
    private static Logger logger = Logger.getLogger(TaxonServiceSearchTaxaAndNamesTest.class);
65

    
66
    private static final String ABIES_BALSAMEA_UUID = "f65d47bd-4f49-4ab1-bc4a-bc4551eaa1a8";
67

    
68
    private static final String ABIES_ALBA_UUID = "7dbd5810-a3e5-44b6-b563-25152b8867f4";
69

    
70
    private static final String CLASSIFICATION_UUID = "2a5ceebb-4830-4524-b330-78461bf8cb6b";
71

    
72
    private static final String CLASSIFICATION_ALT_UUID = "d7c741e3-ae9e-4a7d-a566-9e3a7a0b51ce";
73

    
74
    private static final String D_ABIES_BALSAMEA_UUID = "900108d8-e6ce-495e-b32e-7aad3099135e";
75

    
76
    private static final String D_ABIES_ALBA_UUID = "ec8bba03-d993-4c85-8472-18b14942464b";
77

    
78
//    private static final String D_ABIES_KAWAKAMII_SEC_KOMAROV_UUID = "e9d8c2fd-6409-46d5-9c2e-14a2bbb1b2b1";
79

    
80
    @SpringBeanByType
81
    private ITaxonService taxonService;
82
    @SpringBeanByType
83
    private ITermService termService;
84
    @SpringBeanByType
85
    private IClassificationService classificationService;
86
    @SpringBeanByType
87
    private IReferenceService referenceService;
88
    @SpringBeanByType
89
    private IDescriptionService descriptionService;
90
    @SpringBeanByType
91
    private INameService nameService;
92
    @SpringBeanByType
93
    private ICdmMassIndexer indexer;
94

    
95
    @SpringBeanByType
96
    private ITaxonNodeService nodeService;
97

    
98

    
99
    private NamedArea germany;
100
//    private NamedArea france ;
101
    private NamedArea russia ;
102
    private NamedArea canada ;
103

    
104
    /**
105
     * @throws java.lang.Exception
106
     */
107
    @Before
108
    public void setUp() throws Exception {
109

    
110
        germany =  Country.GERMANY();
111
//        france = Country.FRANCEFRENCHREPUBLIC();
112
        russia = Country.RUSSIANFEDERATION();
113
        canada = Country.CANADA();
114
    }
115

    
116
    @Test
117
    public void testDbUnitUsageTest() throws Exception {
118

    
119
        assertNotNull("taxonService should exist", taxonService);
120
        assertNotNull("nameService should exist", nameService);
121
    }
122

    
123
    /**
124
     * This test permutes through all search mode combinations.
125
     *
126
     * 7 Accepted Taxa
127
     * 1 Synonym
128
     * 1 misapplied which is also in the set of accepted
129
     * 2 Names without taxa
130
     *
131
     */
132
    @Test
133
    @DataSet
134
    public final void testFindTaxaAndNames() {
135

    
136
        IFindTaxaAndNamesConfigurator conf = FindTaxaAndNamesConfiguratorImpl.NewInstance();
137
        conf.setTitleSearchString("Abies*");
138
        conf.setMatchMode(MatchMode.BEGINNING);
139

    
140
        // ---------------------------------------------------------
141

    
142
        setTaxaAndNamesModes(conf, true, true, true, true, true);
143
        @SuppressWarnings("rawtypes")
144
        Pager<IdentifiableEntity> pager = taxonService.findTaxaAndNames(conf);
145
        logSearchResults(pager, Level.DEBUG);
146
        assertEquals(10, pager.getRecords().size());
147

    
148
        setTaxaAndNamesModes(conf, false, true, true, true, true);
149
        pager = taxonService.findTaxaAndNames(conf);
150
        assertEquals(4, pager.getRecords().size());
151

    
152
        setTaxaAndNamesModes(conf, true, false, true, true, true);
153
        pager = taxonService.findTaxaAndNames(conf);
154
        assertEquals(9, pager.getRecords().size());
155

    
156
        setTaxaAndNamesModes(conf, false, false, true, true, true);
157
        pager = taxonService.findTaxaAndNames(conf);
158
        //logSearchResults(pager, Level.DEBUG);
159
        assertEquals(3, pager.getRecords().size());
160

    
161
        // ---------------------------------------------------------
162

    
163
        setTaxaAndNamesModes(conf, true, true, false, true, true);
164
        pager = taxonService.findTaxaAndNames(conf);
165
        logSearchResults(pager, Level.DEBUG);
166
        assertEquals(10, pager.getRecords().size());
167

    
168
        setTaxaAndNamesModes(conf, false, true, false, true, true);
169
        pager = taxonService.findTaxaAndNames(conf);
170
        assertEquals("one synonym, two names without taxa and a misapplied name", 4, pager.getRecords().size());
171

    
172
        setTaxaAndNamesModes(conf, true, false, false, true, true);
173
        pager = taxonService.findTaxaAndNames(conf);
174
        assertEquals(9, pager.getRecords().size());
175

    
176
        setTaxaAndNamesModes(conf, false, false, false, true, true);
177
        pager = taxonService.findTaxaAndNames(conf);
178
        assertEquals(3, pager.getRecords().size());
179

    
180
        // =========================================================
181

    
182
        setTaxaAndNamesModes(conf, true, true, true, false, true);
183
        pager = taxonService.findTaxaAndNames(conf);
184
        // logSearchResults(pager, Level.DEBUG);
185
        assertEquals(10, pager.getRecords().size());
186

    
187
        setTaxaAndNamesModes(conf, false, true, true, false, true);
188
        pager = taxonService.findTaxaAndNames(conf);
189
        logSearchResults(pager, Level.DEBUG);
190
        assertEquals(3, pager.getRecords().size());
191

    
192
        setTaxaAndNamesModes(conf, true, false, true, false, true);
193
        pager = taxonService.findTaxaAndNames(conf);
194
        logSearchResults(pager, Level.DEBUG);
195
        assertEquals(9, pager.getRecords().size());
196

    
197
        setTaxaAndNamesModes(conf, false, false, true, false, true);
198
        pager = taxonService.findTaxaAndNames(conf);
199
        logSearchResults(pager, Level.DEBUG);
200
        assertEquals(2, pager.getRecords().size());
201

    
202
        // ---------------------------------------------------------
203

    
204
        setTaxaAndNamesModes(conf, true, true, false, false, true);
205
        pager = taxonService.findTaxaAndNames(conf);
206
        logSearchResults(pager, Level.DEBUG);
207
        assertEquals(10, pager.getRecords().size());
208

    
209
        setTaxaAndNamesModes(conf, false, true, false, false, true);
210
        pager = taxonService.findTaxaAndNames(conf);
211
        logSearchResults(pager, Level.DEBUG);
212
        assertEquals(3, pager.getRecords().size());
213

    
214
        setTaxaAndNamesModes(conf, true, false, false, false, true);
215
        pager = taxonService.findTaxaAndNames(conf);
216
        logSearchResults(pager, Level.DEBUG);
217
        assertEquals(9, pager.getRecords().size());
218

    
219
        // only names without taxa
220
        // - Abies borisii-regis
221
        // - Abies lasio
222
        setTaxaAndNamesModes(conf, false, false, false, false, true);
223
        pager = taxonService.findTaxaAndNames(conf);
224
        logSearchResults(pager, Level.DEBUG);
225
        assertEquals(2, pager.getRecords().size());
226

    
227
        // =========================================================
228

    
229
        setTaxaAndNamesModes(conf, true, true, true, true, false);
230
        pager = taxonService.findTaxaAndNames(conf);
231
        logSearchResults(pager, Level.DEBUG);
232
        assertEquals(8, pager.getRecords().size());
233

    
234
        setTaxaAndNamesModes(conf, false, true, true, true, false);
235
        pager = taxonService.findTaxaAndNames(conf);
236
        logSearchResults(pager, Level.DEBUG);
237
        assertEquals("one synonym and a misapplied name", 2, pager.getRecords().size());
238

    
239
        setTaxaAndNamesModes(conf, true, false, true, true, false);
240
        pager = taxonService.findTaxaAndNames(conf);
241
        logSearchResults(pager, Level.DEBUG);
242
        assertEquals(7, pager.getRecords().size());
243

    
244
        setTaxaAndNamesModes(conf, false, false, true, true, false);
245
        pager = taxonService.findTaxaAndNames(conf);
246
        logSearchResults(pager, Level.DEBUG);
247
        assertEquals(1, pager.getRecords().size());
248

    
249
        // ---------------------------------------------------------
250

    
251
        setTaxaAndNamesModes(conf, true, true, false, true, false);
252
        pager = taxonService.findTaxaAndNames(conf);
253
        logSearchResults(pager, Level.DEBUG);
254
        assertEquals(8, pager.getRecords().size());
255

    
256
        setTaxaAndNamesModes(conf, false, true, false, true, false);
257
        pager = taxonService.findTaxaAndNames(conf);
258
        logSearchResults(pager, Level.DEBUG);
259
        assertEquals(2, pager.getRecords().size());
260

    
261
        setTaxaAndNamesModes(conf, true, false, false, true, false);
262
        pager = taxonService.findTaxaAndNames(conf);
263
        logSearchResults(pager, Level.DEBUG);
264
        assertEquals(7, pager.getRecords().size());
265

    
266
        // only misapplied names
267
        // - Abies kawakamii sec. Komarov, V. L., Flora SSSR 29
268
        setTaxaAndNamesModes(conf, false, false, false, true, false);
269
        pager = taxonService.findTaxaAndNames(conf);
270
        logSearchResults(pager, Level.DEBUG);
271
        assertEquals(1, pager.getRecords().size());
272

    
273
        // =========================================================
274

    
275
        setTaxaAndNamesModes(conf, true, true, true, false, false);
276
        pager = taxonService.findTaxaAndNames(conf);
277
        logSearchResults(pager, Level.DEBUG);
278
        assertEquals(8, pager.getRecords().size());
279

    
280

    
281
        setTaxaAndNamesModes(conf, false, true, true, false, false);
282
        pager = taxonService.findTaxaAndNames(conf);
283
        logSearchResults(pager, Level.DEBUG);
284
        assertEquals(1, pager.getRecords().size());
285

    
286
        setTaxaAndNamesModes(conf, true, false, true, false, false);
287
        pager = taxonService.findTaxaAndNames(conf);
288
        logSearchResults(pager, Level.DEBUG);
289
        assertEquals(7, pager.getRecords().size());
290

    
291
        setTaxaAndNamesModes(conf, false, false, true, false, false);
292
        pager = taxonService.findTaxaAndNames(conf);
293
        logSearchResults(pager, Level.DEBUG);
294
        assertEquals(0, pager.getRecords().size());
295

    
296
        // ---------------------------------------------------------
297

    
298
        setTaxaAndNamesModes(conf, true, true, false, false, false);
299
        pager = taxonService.findTaxaAndNames(conf);
300
        logSearchResults(pager, Level.DEBUG);
301
        assertEquals(8, pager.getRecords().size());
302

    
303
        setTaxaAndNamesModes(conf, false, true, false, false, false);
304
        pager = taxonService.findTaxaAndNames(conf);
305
        logSearchResults(pager, Level.DEBUG);
306
        assertEquals(1, pager.getRecords().size());
307

    
308
        setTaxaAndNamesModes(conf, true, false, false, false, false);
309
        pager = taxonService.findTaxaAndNames(conf);
310
        logSearchResults(pager, Level.DEBUG);
311
        assertEquals(7, pager.getRecords().size());
312

    
313
        setTaxaAndNamesModes(conf, false, false, false, false, false);
314
        pager = taxonService.findTaxaAndNames(conf);
315
        logSearchResults(pager, Level.DEBUG);
316
        assertEquals(0, pager.getRecords().size());
317
    }
318

    
319
    /**
320
     * This test permutes through all search mode combinations with classification filter
321
     *
322
     * 1 accepted taxon
323
     * 1 Synonym
324
     * 1 misapplied
325
     * 2 names without taxa
326
     *
327
     */
328
    @Test
329
    @DataSet
330
    public final void testFindTaxaAndNames_with_classification() {
331

    
332
        IFindTaxaAndNamesConfigurator conf = FindTaxaAndNamesConfiguratorImpl.NewInstance();
333
        conf.setTitleSearchString("Abies*");
334
        conf.setClassification(classificationService.load(UUID.fromString(CLASSIFICATION_UUID)));
335
        conf.setMatchMode(MatchMode.BEGINNING);
336

    
337
        // ---------------------------------------------------------
338

    
339
        setTaxaAndNamesModes(conf, true, true, true, true, true);
340
        @SuppressWarnings("rawtypes")
341
        Pager<IdentifiableEntity> pager = taxonService.findTaxaAndNames(conf);
342
        logSearchResults(pager, Level.DEBUG);
343
        assertEquals(5, pager.getRecords().size());
344

    
345

    
346
        setTaxaAndNamesModes(conf, false, true, true, true, true);
347
        pager = taxonService.findTaxaAndNames(conf);
348
        logSearchResults(pager, Level.DEBUG);
349
        assertEquals(4, pager.getRecords().size());
350

    
351
        setTaxaAndNamesModes(conf, true, false, true, true, true);
352
        pager = taxonService.findTaxaAndNames(conf);
353
        assertEquals(4, pager.getRecords().size());
354

    
355
        setTaxaAndNamesModes(conf, false, false, true, true, true);
356
        pager = taxonService.findTaxaAndNames(conf);
357
        //logSearchResults(pager, Level.DEBUG);
358
        assertEquals(3, pager.getRecords().size());
359

    
360
        // ---------------------------------------------------------
361

    
362
        setTaxaAndNamesModes(conf, true, true, false, true, true);
363
        pager = taxonService.findTaxaAndNames(conf);
364
        assertEquals(5, pager.getRecords().size());
365

    
366
        setTaxaAndNamesModes(conf, false, true, false, true, true);
367
        pager = taxonService.findTaxaAndNames(conf);
368
        assertEquals(4, pager.getRecords().size());
369

    
370
        setTaxaAndNamesModes(conf, true, false, false, true, true);
371
        pager = taxonService.findTaxaAndNames(conf);
372
        assertEquals(4, pager.getRecords().size());
373

    
374
        setTaxaAndNamesModes(conf, false, false, false, true, true);
375
        pager = taxonService.findTaxaAndNames(conf);
376
        assertEquals(3, pager.getRecords().size());
377

    
378
        // =========================================================
379

    
380
        setTaxaAndNamesModes(conf, true, true, true, false, true);
381
        pager = taxonService.findTaxaAndNames(conf);
382
        // logSearchResults(pager, Level.DEBUG);
383
        assertEquals(4, pager.getRecords().size());
384

    
385

    
386
        setTaxaAndNamesModes(conf, false, true, true, false, true);
387
        pager = taxonService.findTaxaAndNames(conf);
388
        logSearchResults(pager, Level.DEBUG);
389
        assertEquals(3, pager.getRecords().size());
390

    
391
        setTaxaAndNamesModes(conf, true, false, true, false, true);
392
        pager = taxonService.findTaxaAndNames(conf);
393
        logSearchResults(pager, Level.DEBUG);
394
        assertEquals(3, pager.getRecords().size());
395

    
396
        setTaxaAndNamesModes(conf, false, false, true, false, true);
397
        pager = taxonService.findTaxaAndNames(conf);
398
        logSearchResults(pager, Level.DEBUG);
399
        assertEquals(2, pager.getRecords().size());
400

    
401
        // ---------------------------------------------------------
402

    
403
        setTaxaAndNamesModes(conf, true, true, false, false, true);
404
        pager = taxonService.findTaxaAndNames(conf);
405
        logSearchResults(pager, Level.DEBUG);
406
        assertEquals(4, pager.getRecords().size());
407

    
408
        setTaxaAndNamesModes(conf, false, true, false, false, true);
409
        pager = taxonService.findTaxaAndNames(conf);
410
        logSearchResults(pager, Level.DEBUG);
411
        assertEquals(3, pager.getRecords().size());
412

    
413
        setTaxaAndNamesModes(conf, true, false, false, false, true);
414
        pager = taxonService.findTaxaAndNames(conf);
415
        logSearchResults(pager, Level.DEBUG);
416
        assertEquals(3, pager.getRecords().size());
417

    
418
        // only names without taxa
419
        // - Abies borisii-regis
420
        // - Abies lasio
421
        setTaxaAndNamesModes(conf, false, false, false, false, true);
422
        pager = taxonService.findTaxaAndNames(conf);
423
        logSearchResults(pager, Level.DEBUG);
424
        assertEquals(2, pager.getRecords().size());
425

    
426
        // =========================================================
427

    
428
        setTaxaAndNamesModes(conf, true, true, true, true, false);
429
        pager = taxonService.findTaxaAndNames(conf);
430
        logSearchResults(pager, Level.DEBUG);
431
        assertEquals(3, pager.getRecords().size());
432

    
433

    
434
        setTaxaAndNamesModes(conf, false, true, true, true, false);
435
        pager = taxonService.findTaxaAndNames(conf);
436
        logSearchResults(pager, Level.DEBUG);
437
        assertEquals(2, pager.getRecords().size());
438

    
439
        setTaxaAndNamesModes(conf, true, false, true, true, false);
440
        pager = taxonService.findTaxaAndNames(conf);
441
        logSearchResults(pager, Level.DEBUG);
442
        assertEquals(2, pager.getRecords().size());
443

    
444
        setTaxaAndNamesModes(conf, false, false, true, true, false);
445
        pager = taxonService.findTaxaAndNames(conf);
446
        logSearchResults(pager, Level.DEBUG);
447
        assertEquals(1, pager.getRecords().size());
448

    
449
        // ---------------------------------------------------------
450

    
451
        setTaxaAndNamesModes(conf, true, true, false, true, false);
452
        pager = taxonService.findTaxaAndNames(conf);
453
        logSearchResults(pager, Level.DEBUG);
454
        assertEquals(3, pager.getRecords().size());
455

    
456
        setTaxaAndNamesModes(conf, false, true, false, true, false);
457
        pager = taxonService.findTaxaAndNames(conf);
458
        logSearchResults(pager, Level.DEBUG);
459
        assertEquals(2, pager.getRecords().size());
460

    
461
        setTaxaAndNamesModes(conf, true, false, false, true, false);
462
        pager = taxonService.findTaxaAndNames(conf);
463
        logSearchResults(pager, Level.DEBUG);
464
        assertEquals(2, pager.getRecords().size());
465

    
466
        // only misapplied names
467
        // - Abies kawakamii sec. Komarov, V. L., Flora SSSR 29
468
        setTaxaAndNamesModes(conf, false, false, false, true, false);
469
        pager = taxonService.findTaxaAndNames(conf);
470
        logSearchResults(pager, Level.DEBUG);
471
        assertEquals(1, pager.getRecords().size());
472

    
473
        // =========================================================
474

    
475
        setTaxaAndNamesModes(conf, true, true, true, false, false);
476
        pager = taxonService.findTaxaAndNames(conf);
477
        logSearchResults(pager, Level.DEBUG);
478
        assertEquals(2, pager.getRecords().size());
479

    
480

    
481
        setTaxaAndNamesModes(conf, false, true, true, false, false);
482
        pager = taxonService.findTaxaAndNames(conf);
483
        logSearchResults(pager, Level.DEBUG);
484
        assertEquals(1, pager.getRecords().size());
485

    
486
        setTaxaAndNamesModes(conf, true, false, true, false, false);
487
        pager = taxonService.findTaxaAndNames(conf);
488
        logSearchResults(pager, Level.DEBUG);
489
        assertEquals(1, pager.getRecords().size());
490

    
491
        setTaxaAndNamesModes(conf, false, false, true, false, false);
492
        pager = taxonService.findTaxaAndNames(conf);
493
        logSearchResults(pager, Level.DEBUG);
494
        assertEquals(0, pager.getRecords().size());
495

    
496
        // ---------------------------------------------------------
497

    
498
        setTaxaAndNamesModes(conf, true, true, false, false, false);
499
        pager = taxonService.findTaxaAndNames(conf);
500
        logSearchResults(pager, Level.DEBUG);
501
        assertEquals(2, pager.getRecords().size());
502

    
503
        setTaxaAndNamesModes(conf, false, true, false, false, false);
504
        pager = taxonService.findTaxaAndNames(conf);
505
        logSearchResults(pager, Level.DEBUG);
506
        assertEquals(1, pager.getRecords().size());
507

    
508
        setTaxaAndNamesModes(conf, true, false, false, false, false);
509
        pager = taxonService.findTaxaAndNames(conf);
510
        logSearchResults(pager, Level.DEBUG);
511
        assertEquals(1, pager.getRecords().size());
512

    
513
        setTaxaAndNamesModes(conf, false, false, false, false, false);
514
        pager = taxonService.findTaxaAndNames(conf);
515
        logSearchResults(pager, Level.DEBUG);
516
        assertEquals(0, pager.getRecords().size());
517
    }
518

    
519
    @Test
520
    @DataSet
521
    public final void testFindTaxaAndNames_CommonName() {
522
     // pass 1
523
        IFindTaxaAndNamesConfigurator configurator = FindTaxaAndNamesConfiguratorImpl.NewInstance();
524
        configurator.setMatchMode(MatchMode.BEGINNING);
525
        configurator.setDoTaxa(true);
526
        configurator.setDoSynonyms(true);
527
        configurator.setDoNamesWithoutTaxa(true);
528
        configurator.setDoTaxaByCommonNames(true);
529
        configurator.setTitleSearchString("Balsam-Tanne");
530

    
531
        @SuppressWarnings("rawtypes")
532
        Pager<IdentifiableEntity> pager = taxonService.findTaxaAndNames(configurator);
533

    
534
        @SuppressWarnings("rawtypes")
535
        List<IdentifiableEntity> list = pager.getRecords();
536
        assertEquals(1, list.size());
537
        configurator.setTitleSearchString("Abies*");
538
        configurator.setDoTaxa(false);
539
        configurator.setDoSynonyms(false);
540
        configurator.setDoNamesWithoutTaxa(false);
541
        configurator.setDoTaxaByCommonNames(true);
542
        configurator.setDoMisappliedNames(true);
543
        configurator.setClassification(classificationService.load(UUID.fromString(CLASSIFICATION_UUID)));
544
        pager = taxonService.findTaxaAndNames(configurator);
545
        list = pager.getRecords();
546
        assertEquals(1, list.size());
547

    
548
    }
549

    
550
    @Test
551
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
552
    public final void testFindTaxaAndNamesWithHybridFormula() {
553

    
554
        // pass 1
555
        IFindTaxaAndNamesConfigurator configurator = FindTaxaAndNamesConfiguratorImpl.NewInstance();
556
        configurator.setTitleSearchString("Achillea*");
557
        configurator.setMatchMode(MatchMode.BEGINNING);
558
        configurator.setDoTaxa(true);
559
        configurator.setDoSynonyms(true);
560
        configurator.setDoNamesWithoutTaxa(true);
561
        configurator.setDoTaxaByCommonNames(true);
562

    
563
        @SuppressWarnings("rawtypes")
564
        Pager<IdentifiableEntity> pager = taxonService.findTaxaAndNames(configurator);
565
    //    Assert.assertEquals("Expecting one taxon",1,pager.getRecords().size());
566

    
567
    }
568

    
569
    /**
570
     * Test method for
571
     * {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#searchByName(java.lang.String, eu.etaxonomy.cdm.model.reference.Reference)}
572
     * .
573
     */
574
    @Test
575
    @DataSet
576
    public final void testfindTaxaAndNamesForEditor() {
577
         IFindTaxaAndNamesConfigurator configurator = FindTaxaAndNamesConfiguratorImpl.NewInstance();
578
         configurator.setTitleSearchString("Abies bor*");
579
         configurator.setMatchMode(MatchMode.BEGINNING);
580
         configurator.setDoTaxa(true);
581
         configurator.setDoSynonyms(false);
582
         configurator.setDoNamesWithoutTaxa(true);
583
         configurator.setDoTaxaByCommonNames(false);
584

    
585
        @SuppressWarnings("rawtypes")
586
        List<UuidAndTitleCache<? extends IdentifiableEntity>> list = taxonService.findTaxaAndNamesForEditor(configurator);
587

    
588
         Assert.assertEquals("Expecting one entity", 1, list.size());
589

    
590
         configurator.setTitleSearchString("silver fir");
591
         configurator.setMatchMode(MatchMode.BEGINNING);
592
         configurator.setDoTaxa(false);
593
         configurator.setDoSynonyms(false);
594
         configurator.setDoNamesWithoutTaxa(true);
595
         configurator.setDoTaxaByCommonNames(true);
596

    
597
         list = taxonService.findTaxaAndNamesForEditor(configurator);
598

    
599
         Assert.assertEquals("Expecting one entity", 1, list.size());
600

    
601
    }
602

    
603

    
604
    /**
605
     *
606
     * @param conf
607
     * @param doTaxa
608
     * @param doSynonyms
609
     * @param doCommonNames
610
     * @param doMisappliedNames
611
     * @param doNamesWithoutTaxa
612
     * @return
613
     */
614
    protected IFindTaxaAndNamesConfigurator setTaxaAndNamesModes(
615
            IFindTaxaAndNamesConfigurator conf,
616
            boolean doTaxa,
617
            boolean doSynonyms,
618
            boolean doCommonNames,
619
            boolean doMisappliedNames,
620
            boolean doNamesWithoutTaxa){
621

    
622
        conf.setDoTaxa(doTaxa);
623
        conf.setDoSynonyms(doSynonyms);
624
        conf.setDoTaxaByCommonNames(doCommonNames);
625
        conf.setDoMisappliedNames(doMisappliedNames);
626
        conf.setDoNamesWithoutTaxa(doNamesWithoutTaxa);
627

    
628
        return conf;
629
    }
630

    
631
    /**
632
     * @param pager
633
     */
634
    protected void logSearchResults(@SuppressWarnings("rawtypes") Pager<IdentifiableEntity> pager, Level level) {
635
        @SuppressWarnings("rawtypes")
636
        List<IdentifiableEntity> list = pager.getRecords();
637
        logger.debug("number of taxa: " + list.size());
638

    
639
        if (logger.isDebugEnabled()) {
640
            for (int i = 0; i < list.size(); i++) {
641
                String nameCache = "";
642
                if (list.get(i) instanceof TaxonName) {
643
                    nameCache = ((TaxonName)list.get(i)).getNameCache();
644
                } else if (list.get(i) instanceof TaxonBase) {
645
                    TaxonName taxonName = ((TaxonBase<?>) list.get(i)).getName();
646
                    nameCache = taxonName.getNameCache();
647
                } else {
648
                }
649
                logger.log(level, list.get(i).getClass() + "(" + i + ")" + ": Name Cache = " + nameCache + ", Title Cache = "
650
                        + list.get(i).getTitleCache());
651
            }
652
        }
653
    }
654

    
655
    /**
656
     * uncomment @Test annotation to create the dataset for this test
657
     */
658
    @Override
659
    // @Test
660
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="ClearDBDataSet.xml")
661
    public final void createTestDataSet() throws FileNotFoundException {
662

    
663
        Classification europeanAbiesClassification = Classification.NewInstance("European Abies");
664
        europeanAbiesClassification.setUuid(UUID.fromString(CLASSIFICATION_UUID));
665
        classificationService.save(europeanAbiesClassification);
666

    
667
        Classification alternativeClassification = Classification.NewInstance("Abies alternative");
668
        alternativeClassification.setUuid(UUID.fromString(CLASSIFICATION_ALT_UUID));
669
        classificationService.save(alternativeClassification);
670

    
671
        Reference sec = ReferenceFactory.newBook();
672
        sec.setTitleCache("Kohlbecker, A., Testcase standart views, 2013", true);
673
        Reference sec_sensu = ReferenceFactory.newBook();
674
        sec_sensu.setTitleCache("Komarov, V. L., Flora SSSR 29", true);
675
        referenceService.save(sec);
676
        referenceService.save(sec_sensu);
677

    
678
        IBotanicalName n_abies = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
679
        n_abies.setNameCache("Abies", true);
680
        Taxon t_abies = Taxon.NewInstance(n_abies, sec);
681
        taxonService.save(t_abies);
682

    
683
        IBotanicalName n_abies_alba = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
684
        n_abies_alba.setNameCache("Abies alba", true);
685
        Taxon t_abies_alba = Taxon.NewInstance(n_abies_alba, sec);
686
        t_abies_alba.setUuid(UUID.fromString(ABIES_ALBA_UUID));
687
        taxonService.save(t_abies_alba);
688

    
689
        IBotanicalName n_abies_subalpina = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
690
        n_abies_subalpina.setNameCache("Abies subalpina", true);
691
        Synonym s_abies_subalpina = Synonym.NewInstance(n_abies_subalpina, sec);
692
        taxonService.save(s_abies_subalpina);
693

    
694
        IBotanicalName n_abies_balsamea = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
695
        n_abies_balsamea.setNameCache("Abies balsamea", true);
696
        Taxon t_abies_balsamea = Taxon.NewInstance(n_abies_balsamea, sec);
697
        t_abies_balsamea.setUuid(UUID.fromString(ABIES_BALSAMEA_UUID));
698
        t_abies_balsamea.addSynonym(s_abies_subalpina, SynonymType.SYNONYM_OF());
699
        taxonService.save(t_abies_balsamea);
700

    
701
        IBotanicalName n_abies_grandis = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
702
        n_abies_grandis.setNameCache("Abies grandis", true);
703
        Taxon t_abies_grandis = Taxon.NewInstance(n_abies_grandis, sec);
704
        taxonService.save(t_abies_grandis);
705

    
706
        IBotanicalName n_abies_kawakamii = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
707
        n_abies_kawakamii.setNameCache("Abies kawakamii", true);
708
        Taxon t_abies_kawakamii = Taxon.NewInstance(n_abies_kawakamii, sec);
709
        t_abies_kawakamii.getTitleCache();
710
        taxonService.save(t_abies_kawakamii);
711

    
712
        //
713
        // 1 Misapplied Name
714
        //
715
        // abies_kawakamii_sensu_komarov as missapplied name for t_abies_balsamea
716
        Taxon t_abies_kawakamii_sensu_komarov = Taxon.NewInstance(n_abies_kawakamii, sec_sensu);
717
        taxonService.save(t_abies_kawakamii_sensu_komarov);
718
        t_abies_kawakamii_sensu_komarov.addTaxonRelation(t_abies_balsamea, TaxonRelationshipType.MISAPPLIED_NAME_FOR(), null, null);
719
        taxonService.saveOrUpdate(t_abies_kawakamii_sensu_komarov);
720

    
721
        IBotanicalName n_abies_lasiocarpa = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
722
        n_abies_lasiocarpa.setNameCache("Abies lasiocarpa", true);
723
        Taxon t_abies_lasiocarpa = Taxon.NewInstance(n_abies_lasiocarpa, sec);
724
        taxonService.save(t_abies_lasiocarpa);
725

    
726
        // add 3 taxa to classifications
727
        europeanAbiesClassification.addChildTaxon(t_abies_balsamea, null, null);
728
        alternativeClassification.addChildTaxon(t_abies_lasiocarpa, null, null);
729
        classificationService.saveOrUpdate(europeanAbiesClassification);
730
        classificationService.saveOrUpdate(alternativeClassification);
731

    
732
        //
733
        // 2 Names without taxa
734
        //
735
        TaxonName n_abies_borisiiregis = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
736
        n_abies_borisiiregis.setNameCache("Abies borisii-regis", true);
737
        nameService.save(n_abies_borisiiregis);
738

    
739
        TaxonName n_abies_lasio = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
740
        n_abies_lasio.setNameCache("Abies lasio", true);
741
        nameService.save(n_abies_lasio);
742

    
743
        // A hybrid name not from Abies
744
        IBotanicalName n_abies_millefolium_x_Achillea_nobilis = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
745
        n_abies_millefolium_x_Achillea_nobilis.setNameCache("Achillea millefolium × Achillea nobilis", true);
746
        Taxon t_abies_millefolium_x_Achillea_nobilis = Taxon.NewInstance(n_abies_millefolium_x_Achillea_nobilis, sec);
747
        taxonService.save(t_abies_millefolium_x_Achillea_nobilis);
748

    
749
        //
750
        // Description
751
        //
752
        TaxonDescription d_abies_alba = TaxonDescription.NewInstance(t_abies_alba);
753
        TaxonDescription d_abies_balsamea = TaxonDescription.NewInstance(t_abies_balsamea);
754

    
755
        d_abies_alba.setUuid(UUID.fromString(D_ABIES_ALBA_UUID));
756
        d_abies_balsamea.setUuid(UUID.fromString(D_ABIES_BALSAMEA_UUID));
757

    
758

    
759
        // CommonTaxonName
760
        d_abies_alba.addElement(CommonTaxonName.NewInstance("Wei"+UTF8.SHARP_S+"tanne", Language.GERMAN()));
761
        d_abies_alba.addElement(CommonTaxonName.NewInstance("silver fir", Language.ENGLISH()));
762
        d_abies_alba.addElement(Distribution
763
                .NewInstance(
764
                        germany,
765
                        PresenceAbsenceTerm.NATIVE()));
766
        d_abies_alba.addElement(Distribution
767
                .NewInstance(
768
                        russia,
769
                        PresenceAbsenceTerm.ABSENT()));
770

    
771
        // TextData
772
        d_abies_balsamea
773
            .addElement(TextData
774
                    .NewInstance(
775
                            "Die Balsam-Tanne (Abies balsamea) ist eine Pflanzenart aus der Gattung der Tannen (Abies). Sie wächst im nordöstlichen Nordamerika, wo sie sowohl Tief- als auch Bergland besiedelt. Sie gilt als relativ anspruchslos gegenüber dem Standort und ist frosthart. In vielen Teilen des natürlichen Verbreitungsgebietes stellt sie die Klimaxbaumart dar.",
776
                            Language.GERMAN(), null));
777
        d_abies_balsamea
778
        .addElement(CommonTaxonName
779
                .NewInstance(
780
                        "Balsam-Tanne",
781
                        Language.GERMAN(), null));
782

    
783
        d_abies_balsamea
784
        .addElement(Distribution
785
                .NewInstance(
786
                        canada,
787
                        PresenceAbsenceTerm.PRESENT()));
788

    
789
        d_abies_balsamea
790
        .addElement(Distribution
791
                .NewInstance(
792
                        germany,
793
                        PresenceAbsenceTerm.NATIVE()));
794

    
795
        d_abies_balsamea
796
                .addElement(TextData
797
                        .NewInstance(
798
                                TaxonServiceSearchTestUtf8Constants.RUSSIAN_ABIES_ALBA_LONG,
799
                                Language.RUSSIAN(), null));
800
        d_abies_balsamea
801
        .addElement(CommonTaxonName
802
                .NewInstance(
803
                        TaxonServiceSearchTestUtf8Constants.RUSSIAN_ABIES_ALBA_SHORT,
804
                        Language.RUSSIAN(), null));
805
        descriptionService.saveOrUpdate(d_abies_balsamea);
806

    
807
        setComplete();
808
        endTransaction();
809

    
810

    
811
        writeDbUnitDataSetFile(new String[] {
812
            "TAXONBASE", "TAXONNAME",
813
            "TAXONRELATIONSHIP",
814
            "REFERENCE", "DESCRIPTIONELEMENTBASE", "DESCRIPTIONBASE",
815
            "AGENTBASE", "HOMOTYPICALGROUP",
816
            "CLASSIFICATION", "TAXONNODE",
817
            "LANGUAGESTRING", "DESCRIPTIONELEMENTBASE_LANGUAGESTRING",
818
            "HIBERNATE_SEQUENCES" // IMPORTANT!!!
819
            });
820

    
821
    }
822

    
823
}
(32-32/40)