Project

General

Profile

Download (34.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.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(3, 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
     //   assertEquals(3, pager.getRecords().size());
177

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

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

    
186
        // =========================================================
187

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

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

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

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

    
209
        // ---------------------------------------------------------
210

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

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

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

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

    
234
        // =========================================================
235

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

    
241
        setTaxaAndNamesModes(conf, false, true, true, true, false);
242
        pager = taxonService.findTaxaAndNames(conf);
243
        logSearchResults(pager, Level.DEBUG);
244
        assertEquals(1, pager.getRecords().size());
245

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

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

    
256
        // ---------------------------------------------------------
257

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

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

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

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

    
281
        // =========================================================
282

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

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

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

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

    
304
        // ---------------------------------------------------------
305

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

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

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

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

    
327

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

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

    
351
        // ---------------------------------------------------------
352

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

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

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

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

    
373
        // ---------------------------------------------------------
374

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

    
379

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

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

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

    
393
        // =========================================================
394

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

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

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

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

    
416
        // ---------------------------------------------------------
417

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

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

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

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

    
441
        // =========================================================
442

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

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

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

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

    
464
        // ---------------------------------------------------------
465

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

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

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

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

    
489
        // =========================================================
490

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

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

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

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

    
512
        // ---------------------------------------------------------
513

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

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

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

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

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

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

    
562
    }
563

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

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

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

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

    
603
        List<UuidAndTitleCache<IdentifiableEntity>> list = taxonService.findTaxaAndNamesForEditor(configurator);
604

    
605
         Assert.assertEquals("Expecting one entity", 1, list.size());
606

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

    
614
         list = taxonService.findTaxaAndNamesForEditor(configurator);
615

    
616
         Assert.assertEquals("Expecting one entity", 1, list.size());
617

    
618
    }
619

    
620

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

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

    
645
        return conf;
646
    }
647

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
771
        d_abies_alba.setUuid(UUID.fromString(D_ABIES_ALBA_UUID));
772
        d_abies_balsamea.setUuid(UUID.fromString(D_ABIES_BALSAMEA_UUID));
773

    
774

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

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

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

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

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

    
823
        setComplete();
824
        endTransaction();
825

    
826

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

    
837
    }
838

    
839
}
(26-26/33)