Project

General

Profile

Download (34.5 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.hibernate.HibernateProxyHelper;
33
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
34
import eu.etaxonomy.cdm.model.common.Language;
35
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
36
import eu.etaxonomy.cdm.model.description.Distribution;
37
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
38
import eu.etaxonomy.cdm.model.description.TaxonDescription;
39
import eu.etaxonomy.cdm.model.description.TextData;
40
import eu.etaxonomy.cdm.model.location.Country;
41
import eu.etaxonomy.cdm.model.location.NamedArea;
42
import eu.etaxonomy.cdm.model.name.BotanicalName;
43
import eu.etaxonomy.cdm.model.name.NonViralName;
44
import eu.etaxonomy.cdm.model.name.Rank;
45
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
46
import eu.etaxonomy.cdm.model.reference.Reference;
47
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
48
import eu.etaxonomy.cdm.model.taxon.Classification;
49
import eu.etaxonomy.cdm.model.taxon.Synonym;
50
import eu.etaxonomy.cdm.model.taxon.SynonymType;
51
import eu.etaxonomy.cdm.model.taxon.Taxon;
52
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
53
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
54
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
55
import eu.etaxonomy.cdm.persistence.query.MatchMode;
56
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
57
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
58

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

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

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

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

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

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

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

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

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

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

    
96
    @SpringBeanByType
97
    private ITaxonNodeService nodeService;
98

    
99

    
100
    private NamedArea germany;
101
    private NamedArea france ;
102
    private NamedArea russia ;
103
    private NamedArea canada ;
104

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

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

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

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

    
124
    /**
125
     * Test method for
126
     * {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#findTaxaAndNames(eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator)}
127
     * .
128
     */
129
    /**
130
     * This test permutes through all search mode combinations.
131
     *
132
     * 7 Accepted Taxa
133
     * 1 Synonym
134
     * 1 misapplied which is also in the set of accepted
135
     * 2 Names without taxa
136
     *
137
     */
138
    @Test
139
    @DataSet
140
    public final void testFindTaxaAndNames() {
141

    
142
        IFindTaxaAndNamesConfigurator<?> conf = new FindTaxaAndNamesConfiguratorImpl();
143
        conf.setTitleSearchString("Abies*");
144
        conf.setMatchMode(MatchMode.BEGINNING);
145

    
146
        // ---------------------------------------------------------
147

    
148
        setTaxaAndNamesModes(conf, true, true, true, true, true);
149
        Pager<IdentifiableEntity> pager = taxonService.findTaxaAndNames(conf);
150
        logSearchResults(pager, Level.DEBUG);
151
        assertEquals(10, pager.getRecords().size());
152

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

    
157
        setTaxaAndNamesModes(conf, true, false, true, true, true);
158
        pager = taxonService.findTaxaAndNames(conf);
159
        assertEquals(9, pager.getRecords().size());
160

    
161
        setTaxaAndNamesModes(conf, false, false, true, true, true);
162
        pager = taxonService.findTaxaAndNames(conf);
163
        //logSearchResults(pager, Level.DEBUG);
164
        assertEquals(3, pager.getRecords().size());
165

    
166
        // ---------------------------------------------------------
167

    
168
        setTaxaAndNamesModes(conf, true, true, false, true, true);
169
        pager = taxonService.findTaxaAndNames(conf);
170
        assertEquals(10, pager.getRecords().size());
171

    
172

    
173
     // FIXME org.hibernate.QueryParameterException: could not locate named parameter [taxa]
174
        setTaxaAndNamesModes(conf, false, true, false, true, true);
175
        pager = taxonService.findTaxaAndNames(conf);
176
        // there are one synonym, two names without taxa and a misapplied name
177
        assertEquals(4, pager.getRecords().size());
178

    
179
        setTaxaAndNamesModes(conf, true, false, false, true, true);
180
        pager = taxonService.findTaxaAndNames(conf);
181
        assertEquals(9, pager.getRecords().size());
182

    
183
        setTaxaAndNamesModes(conf, false, false, false, true, true);
184
        pager = taxonService.findTaxaAndNames(conf);
185
        assertEquals(3, pager.getRecords().size());
186

    
187
        // =========================================================
188

    
189
        setTaxaAndNamesModes(conf, true, true, true, false, true);
190
        pager = taxonService.findTaxaAndNames(conf);
191
        // logSearchResults(pager, Level.DEBUG);
192
        assertEquals(10, pager.getRecords().size());
193

    
194
        // FIXME the Synonym Abies subalpina missing in result set
195
        setTaxaAndNamesModes(conf, false, true, true, false, true);
196
        pager = taxonService.findTaxaAndNames(conf);
197
        logSearchResults(pager, Level.DEBUG);
198
        assertEquals(3, pager.getRecords().size());
199

    
200
        setTaxaAndNamesModes(conf, true, false, true, false, true);
201
        pager = taxonService.findTaxaAndNames(conf);
202
        logSearchResults(pager, Level.DEBUG);
203
        assertEquals(9, pager.getRecords().size());
204

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

    
210
        // ---------------------------------------------------------
211

    
212
        setTaxaAndNamesModes(conf, true, true, false, false, true);
213
        pager = taxonService.findTaxaAndNames(conf);
214
        logSearchResults(pager, Level.DEBUG);
215
        assertEquals(10, pager.getRecords().size());
216

    
217
        setTaxaAndNamesModes(conf, false, true, false, false, true);
218
        pager = taxonService.findTaxaAndNames(conf);
219
        logSearchResults(pager, Level.DEBUG);
220
        assertEquals(3, pager.getRecords().size());
221

    
222
        setTaxaAndNamesModes(conf, true, false, false, false, true);
223
        pager = taxonService.findTaxaAndNames(conf);
224
        logSearchResults(pager, Level.DEBUG);
225
        assertEquals(9, pager.getRecords().size());
226

    
227
        // only names without taxa
228
        // - Abies borisii-regis
229
        // - Abies lasio
230
        setTaxaAndNamesModes(conf, false, false, false, false, true);
231
        pager = taxonService.findTaxaAndNames(conf);
232
        logSearchResults(pager, Level.DEBUG);
233
        assertEquals(2, pager.getRecords().size());
234

    
235
        // =========================================================
236

    
237
        setTaxaAndNamesModes(conf, true, true, true, true, false);
238
        pager = taxonService.findTaxaAndNames(conf);
239
        logSearchResults(pager, Level.DEBUG);
240
        assertEquals(8, pager.getRecords().size());
241

    
242
        setTaxaAndNamesModes(conf, false, true, true, true, false);
243
        pager = taxonService.findTaxaAndNames(conf);
244
        logSearchResults(pager, Level.DEBUG);
245
        //There is one synonym and a misapplied name
246
        assertEquals(2, pager.getRecords().size());
247

    
248
        setTaxaAndNamesModes(conf, true, false, true, true, false);
249
        pager = taxonService.findTaxaAndNames(conf);
250
        logSearchResults(pager, Level.DEBUG);
251
        assertEquals(7, pager.getRecords().size());
252

    
253
        setTaxaAndNamesModes(conf, false, false, true, true, false);
254
        pager = taxonService.findTaxaAndNames(conf);
255
        logSearchResults(pager, Level.DEBUG);
256
        assertEquals(1, pager.getRecords().size());
257

    
258
        // ---------------------------------------------------------
259

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

    
265
        // FIXME org.hibernate.QueryParameterException: could not locate named parameter [taxa]
266
        setTaxaAndNamesModes(conf, false, true, false, true, false);
267
        pager = taxonService.findTaxaAndNames(conf);
268
        logSearchResults(pager, Level.DEBUG);
269
        assertEquals(2, pager.getRecords().size());
270

    
271
        setTaxaAndNamesModes(conf, true, false, false, true, false);
272
        pager = taxonService.findTaxaAndNames(conf);
273
        logSearchResults(pager, Level.DEBUG);
274
        assertEquals(7, pager.getRecords().size());
275

    
276
        // only misapplied names
277
        // - Abies kawakamii sec. Komarov, V. L., Flora SSSR 29
278
        setTaxaAndNamesModes(conf, false, false, false, true, false);
279
        pager = taxonService.findTaxaAndNames(conf);
280
        logSearchResults(pager, Level.DEBUG);
281
        assertEquals(1, pager.getRecords().size());
282

    
283
        // =========================================================
284

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

    
290
        // FIXME the Synonym Abies subalpina missing in result set
291
//        setTaxaAndNamesModes(conf, false, true, true, false, false);
292
//        pager = taxonService.findTaxaAndNames(conf);
293
//        logSearchResults(pager, Level.DEBUG);
294
//        assertEquals(1, pager.getRecords().size());
295

    
296
        setTaxaAndNamesModes(conf, true, false, true, false, false);
297
        pager = taxonService.findTaxaAndNames(conf);
298
        logSearchResults(pager, Level.DEBUG);
299
        assertEquals(7, pager.getRecords().size());
300

    
301
        setTaxaAndNamesModes(conf, false, false, true, false, false);
302
        pager = taxonService.findTaxaAndNames(conf);
303
        logSearchResults(pager, Level.DEBUG);
304
        assertEquals(0, pager.getRecords().size());
305

    
306
        // ---------------------------------------------------------
307

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

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

    
318
        setTaxaAndNamesModes(conf, true, false, false, false, false);
319
        pager = taxonService.findTaxaAndNames(conf);
320
        logSearchResults(pager, Level.DEBUG);
321
        assertEquals(7, pager.getRecords().size());
322

    
323
        setTaxaAndNamesModes(conf, false, false, false, false, false);
324
        pager = taxonService.findTaxaAndNames(conf);
325
        logSearchResults(pager, Level.DEBUG);
326
        assertEquals(0, pager.getRecords().size());
327
    }
328

    
329

    
330
    /**
331
     * Test method for
332
     * {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#findTaxaAndNames(eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator)}
333
     * .
334
     */
335
    /**
336
     * This test permutes through all search mode combinations with classification filter
337
     *
338
     * 1 accepted taxon
339
     * 1 Synonym
340
     * 1 misapplied
341
     * 2 names without taxa
342
     *
343
     */
344
    @Test
345
    @DataSet
346
    public final void testFindTaxaAndNames_with_classification() {
347

    
348
        IFindTaxaAndNamesConfigurator<?> conf = new FindTaxaAndNamesConfiguratorImpl();
349
        conf.setTitleSearchString("Abies*");
350
        conf.setClassification(classificationService.load(UUID.fromString(CLASSIFICATION_UUID)));
351
        conf.setMatchMode(MatchMode.BEGINNING);
352

    
353
        // ---------------------------------------------------------
354

    
355
        setTaxaAndNamesModes(conf, true, true, true, true, true);
356
        Pager<IdentifiableEntity> pager = taxonService.findTaxaAndNames(conf);
357
        logSearchResults(pager, Level.DEBUG);
358
        assertEquals(5, pager.getRecords().size());
359

    
360
        // FIXME the Synonym Abies subalpina missing in result set
361
//        setTaxaAndNamesModes(conf, false, true, true, true, true);
362
//        pager = taxonService.findTaxaAndNames(conf);
363
//        logSearchResults(pager, Level.DEBUG);
364
//        assertEquals(4, pager.getRecords().size());
365

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

    
370
        setTaxaAndNamesModes(conf, false, false, true, true, true);
371
        pager = taxonService.findTaxaAndNames(conf);
372
        //logSearchResults(pager, Level.DEBUG);
373
        assertEquals(3, pager.getRecords().size());
374

    
375
        // ---------------------------------------------------------
376

    
377
        setTaxaAndNamesModes(conf, true, true, false, true, true);
378
        pager = taxonService.findTaxaAndNames(conf);
379
        assertEquals(5, pager.getRecords().size());
380

    
381

    
382
     // FIXME org.hibernate.QueryParameterException: could not locate named parameter [taxa]
383
     //   setTaxaAndNamesModes(conf, false, true, false, true, true);
384
     //   pager = taxonService.findTaxaAndNames(conf);
385
     //   assertEquals(4, pager.getRecords().size());
386

    
387
        setTaxaAndNamesModes(conf, true, false, false, true, true);
388
        pager = taxonService.findTaxaAndNames(conf);
389
        assertEquals(4, pager.getRecords().size());
390

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

    
395
        // =========================================================
396

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

    
402
        // FIXME the Synonym Abies subalpina missing in result set
403
//        setTaxaAndNamesModes(conf, false, true, true, false, true);
404
//        pager = taxonService.findTaxaAndNames(conf);
405
//        logSearchResults(pager, Level.DEBUG);
406
//        assertEquals(3, pager.getRecords().size());
407

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

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

    
418
        // ---------------------------------------------------------
419

    
420
        setTaxaAndNamesModes(conf, true, true, false, false, true);
421
        pager = taxonService.findTaxaAndNames(conf);
422
        logSearchResults(pager, Level.DEBUG);
423
        assertEquals(4, pager.getRecords().size());
424

    
425
        setTaxaAndNamesModes(conf, false, true, false, false, true);
426
        pager = taxonService.findTaxaAndNames(conf);
427
        logSearchResults(pager, Level.DEBUG);
428
        assertEquals(3, pager.getRecords().size());
429

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

    
435
        // only names without taxa
436
        // - Abies borisii-regis
437
        // - Abies lasio
438
        setTaxaAndNamesModes(conf, false, false, false, false, true);
439
        pager = taxonService.findTaxaAndNames(conf);
440
        logSearchResults(pager, Level.DEBUG);
441
        assertEquals(2, pager.getRecords().size());
442

    
443
        // =========================================================
444

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

    
450
        // FIXME the Synonym Abies subalpina missing in result set
451
//        setTaxaAndNamesModes(conf, false, true, true, true, false);
452
//        pager = taxonService.findTaxaAndNames(conf);
453
//        logSearchResults(pager, Level.DEBUG);
454
//        assertEquals(2, pager.getRecords().size());
455

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

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

    
466
        // ---------------------------------------------------------
467

    
468
        setTaxaAndNamesModes(conf, true, true, false, true, false);
469
        pager = taxonService.findTaxaAndNames(conf);
470
        logSearchResults(pager, Level.DEBUG);
471
        assertEquals(3, pager.getRecords().size());
472

    
473
        // FIXME org.hibernate.QueryParameterException: could not locate named parameter [taxa]
474
//        setTaxaAndNamesModes(conf, false, true, false, true, false);
475
//        pager = taxonService.findTaxaAndNames(conf);
476
//        logSearchResults(pager, Level.DEBUG);
477
//        assertEquals(2, pager.getRecords().size());
478

    
479
        setTaxaAndNamesModes(conf, true, false, false, true, false);
480
        pager = taxonService.findTaxaAndNames(conf);
481
        logSearchResults(pager, Level.DEBUG);
482
        assertEquals(2, pager.getRecords().size());
483

    
484
        // only misapplied names
485
        // - Abies kawakamii sec. Komarov, V. L., Flora SSSR 29
486
        setTaxaAndNamesModes(conf, false, false, false, true, false);
487
        pager = taxonService.findTaxaAndNames(conf);
488
        logSearchResults(pager, Level.DEBUG);
489
        assertEquals(1, pager.getRecords().size());
490

    
491
        // =========================================================
492

    
493
        setTaxaAndNamesModes(conf, true, true, true, false, false);
494
        pager = taxonService.findTaxaAndNames(conf);
495
        logSearchResults(pager, Level.DEBUG);
496
        assertEquals(2, pager.getRecords().size());
497

    
498
        // FIXME the Synonym Abies subalpina missing in result set
499
//        setTaxaAndNamesModes(conf, false, true, true, false, false);
500
//        pager = taxonService.findTaxaAndNames(conf);
501
//        logSearchResults(pager, Level.DEBUG);
502
//        assertEquals(1, pager.getRecords().size());
503

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

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

    
514
        // ---------------------------------------------------------
515

    
516
        setTaxaAndNamesModes(conf, true, true, false, false, false);
517
        pager = taxonService.findTaxaAndNames(conf);
518
        logSearchResults(pager, Level.DEBUG);
519
        assertEquals(2, pager.getRecords().size());
520

    
521
        setTaxaAndNamesModes(conf, false, true, false, false, false);
522
        pager = taxonService.findTaxaAndNames(conf);
523
        logSearchResults(pager, Level.DEBUG);
524
        assertEquals(1, pager.getRecords().size());
525

    
526
        setTaxaAndNamesModes(conf, true, false, false, false, false);
527
        pager = taxonService.findTaxaAndNames(conf);
528
        logSearchResults(pager, Level.DEBUG);
529
        assertEquals(1, pager.getRecords().size());
530

    
531
        setTaxaAndNamesModes(conf, false, false, false, false, false);
532
        pager = taxonService.findTaxaAndNames(conf);
533
        logSearchResults(pager, Level.DEBUG);
534
        assertEquals(0, pager.getRecords().size());
535
    }
536

    
537
    /**
538
     * Test method for
539
     * {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#findTaxaAndNames(eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator)}
540
     * .
541
     */
542
    @Test
543
    @DataSet
544
    public final void testFindTaxaAndNames_CommonName() {
545
     // pass 1
546
        IFindTaxaAndNamesConfigurator<?> configurator = new FindTaxaAndNamesConfiguratorImpl();
547
        configurator.setMatchMode(MatchMode.BEGINNING);
548
        configurator.setDoTaxa(true);
549
        configurator.setDoSynonyms(true);
550
        configurator.setDoNamesWithoutTaxa(true);
551
        configurator.setDoTaxaByCommonNames(true);
552
        configurator.setTitleSearchString("Balsam-Tanne");
553

    
554
        Pager<IdentifiableEntity> pager = taxonService.findTaxaAndNames(configurator);
555
        List<IdentifiableEntity> list = pager.getRecords();
556
        assertEquals(1, list.size());
557
        configurator.setDoTaxaByCommonNames(false);
558
        configurator.setDoMisappliedNames(true);
559
        configurator.setClassification(classificationService.load(UUID.fromString(CLASSIFICATION_UUID)));
560
        pager = taxonService.findTaxaAndNames(configurator);
561
        list = pager.getRecords();
562
        assertEquals(0, list.size());
563

    
564
    }
565

    
566
    /**
567
     * Test method for
568
     * {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#findTaxaAndNames(eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator)}
569
     * .
570
     */
571
    @Test
572
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
573
    public final void testFindTaxaAndNamesWithHybridFormula() {
574

    
575
        // pass 1
576
        IFindTaxaAndNamesConfigurator<?> configurator = new FindTaxaAndNamesConfiguratorImpl();
577
        configurator.setTitleSearchString("Achillea*");
578
        configurator.setMatchMode(MatchMode.BEGINNING);
579
        configurator.setDoTaxa(true);
580
        configurator.setDoSynonyms(true);
581
        configurator.setDoNamesWithoutTaxa(true);
582
        configurator.setDoTaxaByCommonNames(true);
583

    
584
        Pager<IdentifiableEntity> pager = taxonService.findTaxaAndNames(configurator);
585
    //    Assert.assertEquals("Expecting one taxon",1,pager.getRecords().size());
586
        List<IdentifiableEntity> list = pager.getRecords();
587
    }
588

    
589
    /**
590
     * Test method for
591
     * {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#searchTaxaByName(java.lang.String, eu.etaxonomy.cdm.model.reference.Reference)}
592
     * .
593
     */
594
    @Test
595
    @DataSet
596
    public final void testfindTaxaAndNamesForEditor() {
597
         IFindTaxaAndNamesConfigurator<?> configurator = new FindTaxaAndNamesConfiguratorImpl();
598
         configurator.setTitleSearchString("Abies bor*");
599
         configurator.setMatchMode(MatchMode.BEGINNING);
600
         configurator.setDoTaxa(true);
601
         configurator.setDoSynonyms(false);
602
         configurator.setDoNamesWithoutTaxa(true);
603
         configurator.setDoTaxaByCommonNames(false);
604

    
605
        List<UuidAndTitleCache<IdentifiableEntity>> list = taxonService.findTaxaAndNamesForEditor(configurator);
606

    
607
         Assert.assertEquals("Expecting one entity", 1, list.size());
608

    
609
         configurator.setTitleSearchString("silver fir");
610
         configurator.setMatchMode(MatchMode.BEGINNING);
611
         configurator.setDoTaxa(false);
612
         configurator.setDoSynonyms(false);
613
         configurator.setDoNamesWithoutTaxa(true);
614
         configurator.setDoTaxaByCommonNames(true);
615

    
616
         list = taxonService.findTaxaAndNamesForEditor(configurator);
617

    
618
         Assert.assertEquals("Expecting one entity", 1, list.size());
619

    
620
    }
621

    
622

    
623
    /**
624
     *
625
     * @param conf
626
     * @param doTaxa
627
     * @param doSynonyms
628
     * @param doCommonNames
629
     * @param doMisappliedNames
630
     * @param doNamesWithoutTaxa
631
     * @return
632
     */
633
    protected IFindTaxaAndNamesConfigurator<?> setTaxaAndNamesModes(
634
            IFindTaxaAndNamesConfigurator<?> conf,
635
            boolean doTaxa,
636
            boolean doSynonyms,
637
            boolean doCommonNames,
638
            boolean doMisappliedNames,
639
            boolean doNamesWithoutTaxa){
640

    
641
        conf.setDoTaxa(doTaxa);
642
        conf.setDoSynonyms(doSynonyms);
643
        conf.setDoTaxaByCommonNames(doCommonNames);
644
        conf.setDoMisappliedNames(doMisappliedNames);
645
        conf.setDoNamesWithoutTaxa(doNamesWithoutTaxa);
646

    
647
        return conf;
648
    }
649

    
650
    /**
651
     * @param pager
652
     */
653
    protected void logSearchResults(Pager<IdentifiableEntity> pager, Level level) {
654
        List<IdentifiableEntity> list = pager.getRecords();
655
        logger.debug("number of taxa: " + list.size());
656

    
657
        if (logger.isDebugEnabled()) {
658
            for (int i = 0; i < list.size(); i++) {
659
                String nameCache = "";
660
                if (list.get(i) instanceof NonViralName) {
661
                    nameCache = ((NonViralName<?>) list.get(i)).getNameCache();
662
                } else if (list.get(i) instanceof TaxonBase) {
663
                    TaxonNameBase<?,?> taxonNameBase = ((TaxonBase) list.get(i)).getName();
664
                    nameCache = HibernateProxyHelper.deproxy(taxonNameBase, NonViralName.class).getNameCache();
665
                } else {
666
                }
667
                logger.log(level, list.get(i).getClass() + "(" + i + ")" + ": Name Cache = " + nameCache + ", Title Cache = "
668
                        + list.get(i).getTitleCache());
669
            }
670
        }
671
    }
672

    
673
    /**
674
     * uncomment @Test annotation to create the dataset for this test
675
     */
676
    @Override
677
    // @Test
678
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="BlankDataSet.xml")
679
    public final void createTestDataSet() throws FileNotFoundException {
680

    
681
        Classification europeanAbiesClassification = Classification.NewInstance("European Abies");
682
        europeanAbiesClassification.setUuid(UUID.fromString(CLASSIFICATION_UUID));
683
        classificationService.save(europeanAbiesClassification);
684

    
685
        Classification alternativeClassification = Classification.NewInstance("Abies alternative");
686
        alternativeClassification.setUuid(UUID.fromString(CLASSIFICATION_ALT_UUID));
687
        classificationService.save(alternativeClassification);
688

    
689
        Reference sec = ReferenceFactory.newBook();
690
        sec.setTitleCache("Kohlbecker, A., Testcase standart views, 2013", true);
691
        Reference sec_sensu = ReferenceFactory.newBook();
692
        sec_sensu.setTitleCache("Komarov, V. L., Flora SSSR 29", true);
693
        referenceService.save(sec);
694
        referenceService.save(sec_sensu);
695

    
696
        BotanicalName n_abies = BotanicalName.NewInstance(Rank.GENUS());
697
        n_abies.setNameCache("Abies", true);
698
        Taxon t_abies = Taxon.NewInstance(n_abies, sec);
699
        taxonService.save(t_abies);
700

    
701
        BotanicalName n_abies_alba = BotanicalName.NewInstance(Rank.SPECIES());
702
        n_abies_alba.setNameCache("Abies alba", true);
703
        Taxon t_abies_alba = Taxon.NewInstance(n_abies_alba, sec);
704
        t_abies_alba.setUuid(UUID.fromString(ABIES_ALBA_UUID));
705
        taxonService.save(t_abies_alba);
706

    
707
        BotanicalName n_abies_subalpina = BotanicalName.NewInstance(Rank.SPECIES());
708
        n_abies_subalpina.setNameCache("Abies subalpina", true);
709
        Synonym s_abies_subalpina = Synonym.NewInstance(n_abies_subalpina, sec);
710
        taxonService.save(s_abies_subalpina);
711

    
712
        BotanicalName n_abies_balsamea = BotanicalName.NewInstance(Rank.SPECIES());
713
        n_abies_balsamea.setNameCache("Abies balsamea", true);
714
        Taxon t_abies_balsamea = Taxon.NewInstance(n_abies_balsamea, sec);
715
        t_abies_balsamea.setUuid(UUID.fromString(ABIES_BALSAMEA_UUID));
716
        t_abies_balsamea.addSynonym(s_abies_subalpina, SynonymType.SYNONYM_OF());
717
        taxonService.save(t_abies_balsamea);
718

    
719
        BotanicalName n_abies_grandis = BotanicalName.NewInstance(Rank.SPECIES());
720
        n_abies_grandis.setNameCache("Abies grandis", true);
721
        Taxon t_abies_grandis = Taxon.NewInstance(n_abies_grandis, sec);
722
        taxonService.save(t_abies_grandis);
723

    
724
        BotanicalName n_abies_kawakamii = BotanicalName.NewInstance(Rank.SPECIES());
725
        n_abies_kawakamii.setNameCache("Abies kawakamii", true);
726
        Taxon t_abies_kawakamii = Taxon.NewInstance(n_abies_kawakamii, sec);
727
        t_abies_kawakamii.getTitleCache();
728
        taxonService.save(t_abies_kawakamii);
729

    
730
        //
731
        // 1 Misapplied Name
732
        //
733
        // abies_kawakamii_sensu_komarov as missapplied name for t_abies_balsamea
734
        Taxon t_abies_kawakamii_sensu_komarov = Taxon.NewInstance(n_abies_kawakamii, sec_sensu);
735
        taxonService.save(t_abies_kawakamii_sensu_komarov);
736
        t_abies_kawakamii_sensu_komarov.addTaxonRelation(t_abies_balsamea, TaxonRelationshipType.MISAPPLIED_NAME_FOR(), null, null);
737
        taxonService.saveOrUpdate(t_abies_kawakamii_sensu_komarov);
738

    
739
        BotanicalName n_abies_lasiocarpa = BotanicalName.NewInstance(Rank.SPECIES());
740
        n_abies_lasiocarpa.setNameCache("Abies lasiocarpa", true);
741
        Taxon t_abies_lasiocarpa = Taxon.NewInstance(n_abies_lasiocarpa, sec);
742
        taxonService.save(t_abies_lasiocarpa);
743

    
744
        // add 3 taxa to classifications
745
        europeanAbiesClassification.addChildTaxon(t_abies_balsamea, null, null);
746
        alternativeClassification.addChildTaxon(t_abies_lasiocarpa, null, null);
747
        classificationService.saveOrUpdate(europeanAbiesClassification);
748
        classificationService.saveOrUpdate(alternativeClassification);
749

    
750
        //
751
        // 2 Names without taxa
752
        //
753
        BotanicalName n_abies_borisiiregis = BotanicalName.NewInstance(Rank.SPECIES());
754
        n_abies_borisiiregis.setNameCache("Abies borisii-regis", true);
755
        nameService.save(n_abies_borisiiregis);
756

    
757
        BotanicalName n_abies_lasio = BotanicalName.NewInstance(Rank.SPECIES());
758
        n_abies_lasio.setNameCache("Abies lasio", true);
759
        nameService.save(n_abies_lasio);
760

    
761
        // A hybrid name not from Abies
762
        BotanicalName n_abies_millefolium_x_Achillea_nobilis = BotanicalName.NewInstance(Rank.SPECIES());
763
        n_abies_millefolium_x_Achillea_nobilis.setNameCache("Achillea millefolium × Achillea nobilis", true);
764
        Taxon t_abies_millefolium_x_Achillea_nobilis = Taxon.NewInstance(n_abies_millefolium_x_Achillea_nobilis, sec);
765
        taxonService.save(t_abies_millefolium_x_Achillea_nobilis);
766

    
767
        //
768
        // Description
769
        //
770
        TaxonDescription d_abies_alba = TaxonDescription.NewInstance(t_abies_alba);
771
        TaxonDescription d_abies_balsamea = TaxonDescription.NewInstance(t_abies_balsamea);
772

    
773
        d_abies_alba.setUuid(UUID.fromString(D_ABIES_ALBA_UUID));
774
        d_abies_balsamea.setUuid(UUID.fromString(D_ABIES_BALSAMEA_UUID));
775

    
776

    
777
        // CommonTaxonName
778
        d_abies_alba.addElement(CommonTaxonName.NewInstance("Wei"+UTF8.SHARP_S+"tanne", Language.GERMAN()));
779
        d_abies_alba.addElement(CommonTaxonName.NewInstance("silver fir", Language.ENGLISH()));
780
        d_abies_alba.addElement(Distribution
781
                .NewInstance(
782
                        germany,
783
                        PresenceAbsenceTerm.NATIVE()));
784
        d_abies_alba.addElement(Distribution
785
                .NewInstance(
786
                        russia,
787
                        PresenceAbsenceTerm.ABSENT()));
788

    
789
        // TextData
790
        d_abies_balsamea
791
            .addElement(TextData
792
                    .NewInstance(
793
                            "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.",
794
                            Language.GERMAN(), null));
795
        d_abies_balsamea
796
        .addElement(CommonTaxonName
797
                .NewInstance(
798
                        "Balsam-Tanne",
799
                        Language.GERMAN(), null));
800

    
801
        d_abies_balsamea
802
        .addElement(Distribution
803
                .NewInstance(
804
                        canada,
805
                        PresenceAbsenceTerm.PRESENT()));
806

    
807
        d_abies_balsamea
808
        .addElement(Distribution
809
                .NewInstance(
810
                        germany,
811
                        PresenceAbsenceTerm.NATIVE()));
812

    
813
        d_abies_balsamea
814
                .addElement(TextData
815
                        .NewInstance(
816
                                TaxonServiceSearchTestUtf8Constants.RUSSIAN_ABIES_ALBA_LONG,
817
                                Language.RUSSIAN(), null));
818
        d_abies_balsamea
819
        .addElement(CommonTaxonName
820
                .NewInstance(
821
                        TaxonServiceSearchTestUtf8Constants.RUSSIAN_ABIES_ALBA_SHORT,
822
                        Language.RUSSIAN(), null));
823
        descriptionService.saveOrUpdate(d_abies_balsamea);
824

    
825
        setComplete();
826
        endTransaction();
827

    
828

    
829
        writeDbUnitDataSetFile(new String[] {
830
            "TAXONBASE", "TAXONNAMEBASE",
831
            "TAXONRELATIONSHIP",
832
            "REFERENCE", "DESCRIPTIONELEMENTBASE", "DESCRIPTIONBASE",
833
            "AGENTBASE", "HOMOTYPICALGROUP",
834
            "CLASSIFICATION", "TAXONNODE",
835
            "LANGUAGESTRING", "DESCRIPTIONELEMENTBASE_LANGUAGESTRING",
836
            "HIBERNATE_SEQUENCES" // IMPORTANT!!!
837
            });
838

    
839
    }
840

    
841
}
(26-26/33)