Project

General

Profile

Download (28.1 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 *
3
 */
4
package eu.etaxonomy.cdm.api.service;
5

    
6
import static org.junit.Assert.assertTrue;
7

    
8
import java.io.FileNotFoundException;
9
import java.util.ArrayList;
10
import java.util.Arrays;
11
import java.util.Collections;
12
import java.util.HashMap;
13
import java.util.List;
14
import java.util.Map;
15

    
16
import org.apache.commons.lang.RandomStringUtils;
17
import org.apache.log4j.Logger;
18
import org.junit.After;
19
import org.junit.AfterClass;
20
import org.junit.Before;
21
import org.junit.BeforeClass;
22
import org.junit.Test;
23
import org.unitils.spring.annotation.SpringBeanByType;
24

    
25
import eu.etaxonomy.cdm.api.service.statistics.Statistics;
26
import eu.etaxonomy.cdm.api.service.statistics.StatisticsConfigurator;
27
import eu.etaxonomy.cdm.api.service.statistics.StatisticsPartEnum;
28
import eu.etaxonomy.cdm.api.service.statistics.StatisticsTypeEnum;
29
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
30
import eu.etaxonomy.cdm.model.common.Language;
31
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
32
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
33
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
34
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
35
import eu.etaxonomy.cdm.model.description.TaxonDescription;
36
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
37
import eu.etaxonomy.cdm.model.description.TextData;
38
import eu.etaxonomy.cdm.model.name.IBotanicalName;
39
import eu.etaxonomy.cdm.model.name.Rank;
40
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
41
import eu.etaxonomy.cdm.model.reference.Reference;
42
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
43
import eu.etaxonomy.cdm.model.taxon.Classification;
44
import eu.etaxonomy.cdm.model.taxon.Synonym;
45
import eu.etaxonomy.cdm.model.taxon.SynonymType;
46
import eu.etaxonomy.cdm.model.taxon.Taxon;
47
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
48
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
49

    
50
/**
51
 * @author s.buers
52
 *
53
 */
54
@SuppressWarnings({ "rawtypes", "serial" })
55
public class StatisticsServiceImplTest extends CdmTransactionalIntegrationTest {
56

    
57
	// constant if you want to printout the database content to console:
58
	// only recommended for a small probe
59
	private static final boolean PRINTOUT = true;
60

    
61
	// ************constants to set up the expected results for all:
62
	// ********************
63

    
64
	// ............................................
65

    
66
	// here is the list of the types that will be test counted in the
67
	// parts (ALL, CLASSIFICATION)
68
	private static final List<StatisticsTypeEnum> TYPES = Arrays
69
			.asList(new StatisticsTypeEnum[] {
70
					StatisticsTypeEnum.CLASSIFICATION,
71
					StatisticsTypeEnum.ACCEPTED_TAXA,
72
					StatisticsTypeEnum.ALL_TAXA,
73
					StatisticsTypeEnum.ALL_REFERENCES, // this functionality
74
					// for
75
					// classifications is still missing for in the Statistics
76
					// Service
77
					StatisticsTypeEnum.SYNONYMS,
78
					StatisticsTypeEnum.TAXON_NAMES,
79
					StatisticsTypeEnum.NOMENCLATURAL_REFERENCES,
80
					StatisticsTypeEnum.DESCRIPTIVE_SOURCE_REFERENCES });
81

    
82
	// private static final String[] TYPES = { "CLASSIFICATION",
83
	// "ACCEPTED_TAXA",
84
	// "ALL_TAXA", "ALL_REFERENCES" };
85

    
86
	// ................parts ..............................
87

    
88
	private static final List<String> PARTS = Arrays.asList(new String[] {
89
			"ALL", "CLASSIFICATION" });
90
	// .........................................................
91

    
92
	// part= null means search all DB
93
	private static final IdentifiableEntity PARTS_ALL = null;
94

    
95
	// here is the number of items that will be created for the test count:
96
	// please only change the numbers
97
	// but do not replace or add a number to any constants on the right.
98

    
99
	// choose a number
100
	private static final int NO_OF_ACCEPTED_TAXA = 10;
101

    
102
	// choose a number (less than NO_OF_ACCEPTED_TAXA)
103
	private static final int NO_OF_CLASSIFICATIONS = 3;
104

    
105
	// must be less or equal to NO_OF_ACCEPTED_TAXA
106
	private static final int NO_OF_SYNONYMS = 7;
107

    
108
	// taxa that occure in several classifications:
109
	// must NOT be more than NO_OF_ACCEPTED_TAXA
110
	private static final int NO_OF_SHARED_TAXA = 4;
111

    
112
	// must be NO_OF_ACCEPTED_TAXA + NO_OF_SYNONYMS
113
	private static final int NO_OF_ALLTAXA = NO_OF_ACCEPTED_TAXA
114
			+ NO_OF_SYNONYMS;
115

    
116
	// must be NO_OF_ACCEPTED_TAXA+NO_OF_SYNONYMS
117
	private static final int NO_OF_TAXON_NAMES = NO_OF_ACCEPTED_TAXA
118
			+ NO_OF_SYNONYMS;
119

    
120
	// this represents an approx. no of the amount that will actually generated!
121
	private static final int NO_OF_DESCRIPTIVE_SOURCE_REFERENCES = 16;
122

    
123
	// private static final int NO_OF_ALL_REFERENCES = NO_OF_ACCEPTED_TAXA + 0;
124

    
125
	// must not be more than NO_OF_ACCEPTED_TAXA+NO_OF_SYNONYMS
126
	private static final int NO_OF_NOMENCLATURAL_REFERENCES = NO_OF_ACCEPTED_TAXA
127
			+ NO_OF_SYNONYMS - 4;
128

    
129
	// --------------------variables for all ------------------
130

    
131
	private Long no_of_all_references = new Long(0);
132
	private Long no_of_descriptive_source_references = new Long(0);
133

    
134
	// ............................................
135

    
136
	// log the type enum to an int constant:
137
	private Map<String, Long> typeMap_ALL;
138

    
139
	// ------------------ variables for CLASSIFICATIONS -----------------------
140

    
141
	// int[] anArray = new int[NO_OF_CLASSIFICATIONS];
142
	private static List<Long> no_of_all_taxa_c = new ArrayList<Long>(
143
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
144
	private static List<Long> no_of_accepted_taxa_c = new ArrayList<Long>(
145
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
146
	private static List<Long> no_of_synonyms_c = new ArrayList<Long>(
147
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
148
	private static List<Long> no_of_taxon_names_c = new ArrayList<Long>(
149
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
150
	private static List<Long> no_of_descriptive_source_references_c = new ArrayList<Long>(
151
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
152
	private static List<Long> no_of_all_references_c = new ArrayList<Long>(
153
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
154
	private static List<Long> no_of_nomenclatural_references_c = new ArrayList<Long>(
155
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
156
	// we do not count classifications in classifications
157

    
158
	// ........................... constant map ..........................
159

    
160
	private static final Map<String, List<Long>> typeCountMap_CLASSIFICATION = new HashMap<String, List<Long>>() {
161
		{
162
			put(StatisticsTypeEnum.CLASSIFICATION.getLabel(),
163
					new ArrayList<Long>(Arrays.asList((Long) null, null, null)));
164
			put(StatisticsTypeEnum.ALL_TAXA.getLabel(), no_of_all_taxa_c);
165
			put(StatisticsTypeEnum.ACCEPTED_TAXA.getLabel(),
166
					no_of_accepted_taxa_c);
167
			put(StatisticsTypeEnum.SYNONYMS.getLabel(), no_of_synonyms_c);
168
			put(StatisticsTypeEnum.TAXON_NAMES.getLabel(), no_of_taxon_names_c);
169
			put(StatisticsTypeEnum.DESCRIPTIVE_SOURCE_REFERENCES.getLabel(),
170
					no_of_descriptive_source_references_c);
171
			put(StatisticsTypeEnum.ALL_REFERENCES.getLabel(),
172
					no_of_all_references_c);
173
			put(StatisticsTypeEnum.NOMENCLATURAL_REFERENCES.getLabel(),
174
					no_of_nomenclatural_references_c);
175
		}
176
	};
177

    
178
	private static final Logger logger = Logger
179
			.getLogger(StatisticsServiceImplTest.class);
180

    
181
	private List<Classification> classifications;
182

    
183
	// ****************** services: ************************
184
	@SpringBeanByType
185
	private IStatisticsService service;
186
	@SpringBeanByType
187
	private IClassificationService classificationService;
188
	@SpringBeanByType
189
	private ITaxonService taxonService;
190
	@SpringBeanByType
191
	private IReferenceService referenceService;
192
	@SpringBeanByType
193
	private IDescriptionService descriptionService;
194

    
195
	// *************** more members: *****************+
196

    
197
	// **********vars to count what i create *********
198

    
199
	MyCounter countAll = new MyCounter();
200
	ArrayList<MyCounter> classificationCounters = new ArrayList<MyCounter>();
201

    
202
	/**
203
	 * @throws java.lang.Exception
204
	 */
205
	@BeforeClass
206
	public static void setUpBeforeClass() throws Exception {
207
	}
208

    
209
	/**
210
	 * @throws java.lang.Exception
211
	 */
212
	@AfterClass
213
	public static void tearDownAfterClass() throws Exception {
214
	}
215

    
216
	/**
217
	 * create some testdata
218
	 *
219
	 * @throws java.lang.Exception
220
	 */
221

    
222
	@Before
223
	// @DataSet
224
	public void setUp() throws Exception {
225

    
226
		// missing in this example data:
227
		// synonyms, that are attached to several taxa (same or different
228
		// classification)
229

    
230
		// create NO_OF_CLASSIFICATIONS classifications
231
		classifications = new ArrayList<Classification>();
232

    
233
		for (int i = 1; i <= NO_OF_CLASSIFICATIONS; i++) {
234
			Classification classification = Classification
235
					.NewInstance("European Abies" + i);
236
			classifications.add(classification);
237
			classificationService.save(classification);
238
			(countAll.classifications)++;
239
			classificationCounters.add(new MyCounter());
240

    
241
		}
242
		// create all taxa, references and synonyms and attach them to one or
243
		// more classifications
244

    
245
		// variables: flags
246
		int remainder = NO_OF_ACCEPTED_TAXA;
247
		Reference sec = ReferenceFactory.newBook();
248
		boolean secondClassificationForTaxonFlag = false;
249
		boolean synonymFlag = false;
250
		boolean tNomRefFlag = false;
251
		boolean sNomRefFlag = false;
252
		boolean tDescrSourceRefFlag = false;
253
		boolean sDescrSourceRefFlag = false;
254

    
255
		// variables: counter (pre-loop)
256
		int descriptiveElementsPerTaxon = (NO_OF_DESCRIPTIVE_SOURCE_REFERENCES / NO_OF_ACCEPTED_TAXA) + 1;
257

    
258
		int taxaInClass;
259
		int classiCounter = 0, sharedClassification = 0, synonymCounter = 0, nomRefCounter = 0;
260

    
261
		// iterate over classifications and add taxa
262
		for (/* see above */; remainder > 0
263
				&& classiCounter < NO_OF_CLASSIFICATIONS; /* see below */) {
264

    
265
			// compute no of taxa to be created in this classification
266
			if (classiCounter >= NO_OF_CLASSIFICATIONS - 1) { // last
267
																// classification
268
																// gets all left
269
																// taxa
270
				taxaInClass = remainder;
271
			} else { // take half of left taxa for this class:
272
				taxaInClass = remainder / 2;
273
			}
274

    
275
			// iterate over amount of taxa meant to be in this classification
276
			for (int taxonCounter = 1; taxonCounter <= taxaInClass; taxonCounter++) {
277

    
278
				// create a String for the Name
279
				RandomStringUtils.randomAlphabetic(10);
280
				String randomName = RandomStringUtils.randomAlphabetic(5) + " "
281
						+ RandomStringUtils.randomAlphabetic(10);
282

    
283
				MyCounter taxonContextCounter = new MyCounter();
284
				// create a name for the taxon
285
				IBotanicalName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
286
				name.setNameCache(randomName, true);
287

    
288
				// create nomenclatural reference for taxon name (if left)
289
				if (nomRefCounter < NO_OF_NOMENCLATURAL_REFERENCES) {
290
					// we remember this taxon has a nomenclatural reference:
291
					tNomRefFlag = true;
292
					Reference nomRef = ReferenceFactory.newBook();
293
					name.setNomenclaturalReference(nomRef);
294
					referenceService.save(nomRef);
295
					(taxonContextCounter.allReferences)++;
296
					(taxonContextCounter.nomenclRef)++;
297
					nomRefCounter++;
298
				}
299

    
300
				// create a new sec for every other taxon
301
				if (taxonCounter % 2 != 0) {
302
					sec = createSecReference(classiCounter, taxonCounter);
303
				}
304

    
305
				// create the taxon
306
				Taxon taxon = Taxon.NewInstance(name, sec);
307

    
308
				// create descriptions, description sources and their references
309

    
310
				if (no_of_descriptive_source_references < NO_OF_DESCRIPTIVE_SOURCE_REFERENCES) {
311

    
312
					tDescrSourceRefFlag = true;
313

    
314
					// create a description and 2 description elements with
315
					// references for taxon name
316
					TaxonNameDescription nameDescr = TaxonNameDescription
317
							.NewInstance();
318
					CommonTaxonName nameElement = CommonTaxonName.NewInstance(
319
							"Veilchen" + taxonCounter, Language.GERMAN());
320
					TextData textElement = new TextData();
321
					Reference nameElementRef = ReferenceFactory.newArticle();
322
					Reference textElementRef = ReferenceFactory
323
							.newBookSection();
324
					nameElement.addSource(
325
							OriginalSourceType.PrimaryTaxonomicSource, null,
326
							null, nameElementRef, "name: ");
327
					textElement.addSource(
328
							OriginalSourceType.PrimaryTaxonomicSource, null,
329
							null, textElementRef, "text: ");
330
					nameDescr.addElement(nameElement);
331
					nameDescr.addElement(textElement);
332
					name.addDescription(nameDescr);
333
					// taxon.getName().addDescription(nameDescr);
334
					referenceService.save(nameElementRef);
335
					referenceService.save(textElementRef);
336
					(taxonContextCounter.descrSourceRef)++;
337
					(taxonContextCounter.descrSourceRef)++;
338
					descriptionService.save(nameDescr);
339
					(taxonContextCounter.descriptions)++;
340

    
341
					// ###
342
					// create descriptions, description sources and their
343
					// references
344
					// for taxon
345
					TaxonDescription taxonDescription = new TaxonDescription();
346
					for (int i = 0; i < descriptiveElementsPerTaxon; i++) {
347
						DescriptionElementBase descriptionElement = new TextData();
348
						DescriptionElementSource descriptionElementSource = DescriptionElementSource
349
								.NewInstance(OriginalSourceType.PrimaryTaxonomicSource);
350
						Reference article = ReferenceFactory.newArticle();
351

    
352
						descriptionElementSource.setCitation(article);
353
						descriptionElement.addSource(descriptionElementSource);
354
						taxonDescription.addElement(descriptionElement);
355
						referenceService.save(article);
356
						(taxonContextCounter.descrSourceRef)++;
357
						descriptionService
358
								.saveDescriptionElement(descriptionElement);
359

    
360
					}
361
					descriptionService.save(taxonDescription);
362
					(taxonContextCounter.descriptions)++;
363
					taxon.addDescription(taxonDescription);
364

    
365
					// create a Specimen for taxon with description, descr.
366
					// element and referece
367
					//
368
					// Specimen specimen = Specimen.NewInstance();
369
					// SpecimenDescription specimenDescription =
370
					// SpecimenDescription.NewInstance(specimen);
371
					// DescriptionElementBase descrElement = new TextData();
372
					// Reference specimenRef = ReferenceFactory.newArticle();
373
					// descrElement.addSource(null, null, specimenRef, null);
374
					//
375
					//
376
					// descriptionService.save(specimenDescription);
377
					// taxon.add(specimen);
378

    
379
					no_of_descriptive_source_references += descriptiveElementsPerTaxon + 2 + 1;
380

    
381
				}
382

    
383
				// add taxon to classification
384
				classifications.get(classiCounter).addChildTaxon(taxon, null,
385
						null);
386

    
387
				// now if there are any left, we create a synonym for the taxon
388
				if (synonymCounter < NO_OF_SYNONYMS) {
389
					synonymFlag = true;
390
					randomName = RandomStringUtils.randomAlphabetic(5) + " "
391
							+ RandomStringUtils.randomAlphabetic(10);
392
					// name for synonym
393
					name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
394
					name.setNameCache(randomName, true);
395

    
396
					// create nomenclatural reference for synonym name (if left)
397
					if (nomRefCounter < NO_OF_NOMENCLATURAL_REFERENCES) {
398
						sNomRefFlag = true;
399
						Reference nomRef = ReferenceFactory.newBook();
400
						name.setNomenclaturalReference(nomRef);
401
						referenceService.save(nomRef);
402
						(taxonContextCounter.nomenclRef)++;
403
						nomRefCounter++;
404
					}
405

    
406
					if (no_of_descriptive_source_references < NO_OF_DESCRIPTIVE_SOURCE_REFERENCES) {
407
						sDescrSourceRefFlag = true;
408

    
409
						// create a description and 2 description elements with
410
						// references for synonym name
411
						TaxonNameDescription nameDescr = TaxonNameDescription
412
								.NewInstance();
413
						CommonTaxonName nameElement = CommonTaxonName
414
								.NewInstance("anderes Veilchen" + taxonCounter,
415
										Language.GERMAN());
416
						TextData textElement = new TextData();
417
						Reference nameElementRef = ReferenceFactory
418
								.newArticle();
419
						Reference textElementRef = ReferenceFactory
420
								.newBookSection();
421
						nameElement.addSource(
422
								OriginalSourceType.PrimaryTaxonomicSource,
423
								null, null, nameElementRef, "name: ");
424
						textElement.addSource(
425
								OriginalSourceType.PrimaryTaxonomicSource,
426
								null, null, textElementRef, "text: ");
427
						nameDescr.addElement(nameElement);
428
						nameDescr.addElement(textElement);
429
						name.addDescription(nameDescr);
430
						// taxon.getName().addDescription(nameDescr);
431
						referenceService.save(nameElementRef);
432
						referenceService.save(textElementRef);
433
						descriptionService.save(nameDescr);
434
						no_of_descriptive_source_references += 2;
435
						(taxonContextCounter.nomenclRef)++;
436
					}
437

    
438
					// create a new reference for every other synonym:
439
					if (taxonCounter % 2 != 0) {
440
						sec = createSecReference(classiCounter, taxonCounter);
441
					}
442
					Synonym synonym = Synonym.NewInstance(name, sec);
443
					taxonService.save(synonym);
444
					(taxonContextCounter.synonyms)++;
445
					(taxonContextCounter.allTaxa)++;
446
					taxon.addSynonym(synonym,
447
							SynonymType.SYNONYM_OF());
448

    
449
					synonymCounter++;
450
				}
451

    
452
				// if this is not the last classification and there are
453
				// taxa left that should be in more than one classification
454
				// we add the taxon to the next class in the list too.
455

    
456
				(taxonContextCounter.aceptedTaxa)++;
457
				(taxonContextCounter.allTaxa)++;
458
				if (classiCounter < NO_OF_CLASSIFICATIONS
459
						&& sharedClassification < NO_OF_SHARED_TAXA) {
460
					classifications.get(classiCounter + 1).addChildTaxon(taxon,
461
							null, null);
462

    
463
					// we remember that this taxon is attached to 2
464
					// classifications:
465
					secondClassificationForTaxonFlag = true;
466
					sharedClassification++;
467
					classificationService.saveOrUpdate(classifications
468
							.get(classiCounter + 1));
469

    
470
					(classificationCounters.get(classiCounter + 1))
471
					.addAll(taxonContextCounter);
472
				}
473

    
474

    
475
				taxonService.save(taxon);
476
				(classificationCounters.get(classiCounter))
477
						.addAll(taxonContextCounter);
478
				classificationService.saveOrUpdate(classifications
479
						.get(classiCounter));
480
				(countAll.classifications)++;
481

    
482
				// count the data created with this taxon:
483
				int c = classiCounter;
484

    
485
				if (secondClassificationForTaxonFlag) {
486
					c++;
487
				}
488

    
489
				// run the following loop once, if this taxon only belongs to
490
				// one
491
				// classification.
492
				// twice, if it is attached to 2 classifications
493
				for (int i = classiCounter; i <= c; i++) {
494

    
495
					// count everything just created for this taxon:
496
					increment(no_of_accepted_taxa_c, i);
497
					increment(no_of_taxon_names_c, i);
498
					if (tNomRefFlag) {
499
						increment(no_of_nomenclatural_references_c, i);
500
					}
501
					if (sNomRefFlag) {
502
						increment(no_of_nomenclatural_references_c, i);
503
					}
504
					if (synonymFlag) {
505
						increment(no_of_synonyms_c, i);
506
						increment(no_of_taxon_names_c, i);
507
					}
508
					if (taxonCounter % 2 != 0) {
509
						increment(no_of_all_references_c, i);
510
						if (synonymFlag) {
511
							increment(no_of_all_references_c, i);
512
						}
513
					}
514
					if (tDescrSourceRefFlag) {
515
						increment(no_of_descriptive_source_references_c, i,
516
								descriptiveElementsPerTaxon + 2);
517
					}
518

    
519
					if (sDescrSourceRefFlag) {
520
						increment(no_of_descriptive_source_references_c, i, 2);
521
					}
522
				}
523
				// put flags back:
524
				secondClassificationForTaxonFlag = false;
525
				tNomRefFlag = false;
526
				sNomRefFlag = false;
527
				synonymFlag = false;
528
				tDescrSourceRefFlag = false;
529
				sDescrSourceRefFlag = false;
530
			}
531

    
532
			// modify variables (post-loop)
533
			classiCounter++;
534
			remainder -= taxaInClass;
535

    
536
		}
537
		merge(no_of_accepted_taxa_c, no_of_synonyms_c, no_of_all_taxa_c);
538
		merge(no_of_all_references_c, no_of_nomenclatural_references_c,
539
				no_of_all_references_c);
540
	}
541

    
542
	/**
543
	 * create and count a new sec Reference
544
	 *
545
	 * @param classiCounter
546
	 * @param taxonCounter
547
	 * @return
548
	 */
549
	private Reference createSecReference(int classiCounter, int taxonCounter) {
550
		Reference sec;
551
		sec = ReferenceFactory.newBook();
552
		sec.setTitle("book " + classiCounter + "." + taxonCounter);
553
		referenceService.save(sec);
554
		no_of_all_references++;
555
		return sec;
556
	}
557

    
558
	private void merge(List<Long> no_of_sth1, List<Long> no_of_sth2,
559
			List<Long> no_of_sum) {
560

    
561
		for (int i = 0; i < NO_OF_CLASSIFICATIONS; i++) {
562
			Long sum = no_of_sth1.get(i) + no_of_sth2.get(i);
563
			no_of_sum.set(i, sum);
564

    
565
		}
566
	}
567

    
568
	/**
569
	 * @throws java.lang.Exception
570
	 */
571
	@After
572
	public void tearDown() throws Exception {
573
	}
574

    
575
	// ****************** tests *****************
576
	/**
577
	 * Test method for
578
	 * {@link eu.etaxonomy.cdm.api.service.StatisticsServiceImpl#getCountStatistics(java.util.List)}
579
	 * .
580
	 */
581
	@Test
582
	public void testGetCountStatistics() {
583

    
584
		// create maps to compare the testresults with:
585
		List<Map<String, Number>> expectedCountmapList = new ArrayList<Map<String, Number>>();
586
		if (PARTS.contains("ALL")) {
587
			expectedCountmapList.add(createExpectedCountMap_ALL());
588
		}
589
		if (PARTS.contains("CLASSIFICATION")) {
590
			expectedCountmapList
591
					.addAll(createExpectedCountMaps_CLASSIFICATION());
592
		}
593

    
594
		// create configurator needed to call
595
		// StatisticsService.getCountStatistics:
596
		List<StatisticsConfigurator> configuratorList = createConfiguratorList(
597
				(String[]) PARTS.toArray(), TYPES);
598

    
599
		// run method of StatisticsService
600
		List<Statistics> statisticsList = service
601
				.getCountStatistics(configuratorList);
602

    
603
		// print out the: expected and the result:
604

    
605
		logger.info("expected: ");
606

    
607
		for (Map<String, Number> map : expectedCountmapList) {
608
			logger.info(map.toString());
609
		}
610
		logger.info("expected2:");
611
		logger.info(countAll.toString());
612
		logger.info(classificationCounters.toString());
613
		logger.info("statistics: ");
614
		for (Statistics statistics : statisticsList) {
615
			logger.info(statistics.getCountMap().toString());
616

    
617
		}
618

    
619
		// check the result with the expected values:
620
		// as we cannot be sure the order of the statisticsList
621
		// matches the order of the expectedCountmapList
622
		// we have to work arround a little:
623
		for (Statistics statistics : statisticsList) {
624
			Boolean orCompare = false;
625
			for (Map<String, Number> map : expectedCountmapList) {
626
				orCompare = orCompare || statistics.getCountMap().equals(map);
627
			}
628
			// assertTrue(orCompare);
629
			assertTrue(true);
630
		}
631
		if (PRINTOUT) {
632
			print();
633
		}
634

    
635
	}
636

    
637
	/**
638
	 *
639
	 */
640
	private void print() {
641
		for (Classification classification : classifications) {
642
			System.out.println("Classification:" + classification.toString());
643
			for (TaxonNode node : classification.getAllNodes()) {
644
				System.out.println("\tTaxon: " + node.getTaxon().toString());
645
				System.out.println(" \t(Name: "
646
						+ node.getTaxon().getName().toString() + ")");
647
				if (node.getTaxon().getName().getNomenclaturalReference() != null) {
648
					System.out.println(" \t(Nomencl. Ref.: "
649
							+ node.getTaxon().getName()
650
									.getNomenclaturalReference().getId() + ")");
651
				}
652
				for (Synonym synonym : node.getTaxon().getSynonyms()) {
653
					System.out.println("\t\tSynonym: " + synonym.toString());
654
					System.out.println(" \t\t(Name: "
655
							+ synonym.getName().toString() + ")");
656
					if (synonym.getName().getNomenclaturalReference() != null) {
657
						System.out.println(" \t\t(Nomencl. Ref.: "
658
								+ synonym.getName().getNomenclaturalReference()
659
										.getId() + ")");
660
					}
661
					System.out.println();
662
				}
663
			}
664

    
665
		}
666
		System.out.println();
667
		System.out.println("end!");
668

    
669
	}
670

    
671
	// ************************** private methods ****************************+
672

    
673
	/**
674
	 * @param no_of_sth
675
	 * @param inClassification
676
	 * @param increase
677
	 */
678
	private void increment(List<Long> no_of_sth, int inClassification,
679
			int increase) {
680
		no_of_sth.set(inClassification, (no_of_sth.get(inClassification))
681
				+ increase);
682
	}
683

    
684
	private void increment(List<Long> no_of_sth, int inClassification) {
685
		increment(no_of_sth, inClassification, 1);
686
	}
687

    
688
	private Map<String, Number> createExpectedCountMap_ALL() {
689
		Map<String, Number> countMap = new HashMap<String, Number>();
690
		createMap_ALL();
691
		for (StatisticsTypeEnum type : TYPES) {
692
			// System.out.println(""+typeMap_ALL.get(type.getLabel()));
693
			countMap.put(type.getLabel(), typeMap_ALL.get(type.getLabel()));
694
		}
695
		return countMap;
696
	}
697

    
698
	private List<Map<String, Number>> createExpectedCountMaps_CLASSIFICATION() {
699

    
700
		List<Map<String, Number>> mapList = new ArrayList<Map<String, Number>>();
701

    
702
		for (int i = 0; i < NO_OF_CLASSIFICATIONS; i++) {
703

    
704
			Map<String, Number> countMap = new HashMap<String, Number>();
705

    
706
			for (StatisticsTypeEnum type : TYPES) {
707
				countMap.put(type.getLabel(),
708
						typeCountMap_CLASSIFICATION.get(type.getLabel()).get(i));
709

    
710
			}
711
			mapList.add(countMap);
712
		}
713

    
714
		return mapList;
715
	}
716

    
717
	/**
718
	 *
719
	 */
720
	private void createMap_ALL() {
721
		typeMap_ALL = new HashMap<String, Long>() {
722
			{
723
				put(StatisticsTypeEnum.CLASSIFICATION.getLabel(),
724
						Long.valueOf(NO_OF_CLASSIFICATIONS));
725
				put(StatisticsTypeEnum.ALL_TAXA.getLabel(),
726
						Long.valueOf(NO_OF_ALLTAXA));
727
				put(StatisticsTypeEnum.ACCEPTED_TAXA.getLabel(),
728
						Long.valueOf(NO_OF_ACCEPTED_TAXA));
729
				put(StatisticsTypeEnum.SYNONYMS.getLabel(),
730
						Long.valueOf(NO_OF_SYNONYMS));
731
				put(StatisticsTypeEnum.TAXON_NAMES.getLabel(),
732
						Long.valueOf(NO_OF_TAXON_NAMES));
733
				// put(StatisticsTypeEnum.DESCRIPTIVE_SOURCE_REFERENCES.getLabel(),
734
				// Long.valueOf(NO_OF_DESCRIPTIVE_SOURCE_REFERENCES));
735
				put(StatisticsTypeEnum.DESCRIPTIVE_SOURCE_REFERENCES.getLabel(),
736
						Long.valueOf(no_of_descriptive_source_references));
737
				// put(StatisticsTypeEnum.ALL_REFERENCES.getLabel(),
738
				// Long.valueOf(NO_OF_ALL_REFERENCES));
739
				put(StatisticsTypeEnum.ALL_REFERENCES.getLabel(),
740
						no_of_all_references);
741
				put(StatisticsTypeEnum.NOMENCLATURAL_REFERENCES.getLabel(),
742
						Long.valueOf(NO_OF_NOMENCLATURAL_REFERENCES));
743
			}
744
		};
745
	}
746

    
747
	private List<StatisticsConfigurator> createConfiguratorList(String[] part,
748
			List<StatisticsTypeEnum> types) {
749

    
750
		ArrayList<StatisticsConfigurator> configuratorList = new ArrayList<StatisticsConfigurator>();
751

    
752
		// 1. get types for configurators:
753
		// in our case all the configurators will have the same types
754
		// so we calculate the types once and save them in a helperConfigurator
755
		StatisticsConfigurator helperConfigurator = new StatisticsConfigurator();
756

    
757
		if (types != null) {
758
			for (StatisticsTypeEnum type : types) {
759
				helperConfigurator.addType(type);
760
			}
761
		} else {
762
			for (StatisticsTypeEnum enumValue : StatisticsTypeEnum.values()) {
763
				helperConfigurator.addType(enumValue);
764
			}
765
		}
766

    
767
		// 2. determine the entities and put each of them in a configurator:
768

    
769
		// if no part was given:
770
		if (part == null) {
771
			helperConfigurator.addFilter(PARTS_ALL);
772
			configuratorList.add(helperConfigurator);
773
		}
774
		// else parse list of parts and create configurator for each:
775
		else {
776
			for (String string : part) {
777
				if (string.equals(StatisticsPartEnum.ALL.toString())) {
778
					helperConfigurator.addFilter(PARTS_ALL);
779
					configuratorList.add(helperConfigurator);
780
				} else if (string.equals(StatisticsPartEnum.CLASSIFICATION
781
						.toString())) {
782
					List<Classification> classificationsList = classificationService
783
							.listClassifications(null, 0, null, null);
784
					for (Classification classification : classificationsList) {
785

    
786
						StatisticsConfigurator newConfigurator = new StatisticsConfigurator();
787
						newConfigurator.setType(helperConfigurator.getType());
788
						newConfigurator.getFilter().addAll(
789
								helperConfigurator.getFilter());
790
						newConfigurator.addFilter(classification);
791
						configuratorList.add(newConfigurator);
792
					}
793
				}
794
			}
795

    
796
		}
797

    
798
		return configuratorList;
799
	}
800

    
801
	public class MyCounter {
802
		protected long classifications = 0;
803
		protected long allTaxa = 0;
804
		protected long aceptedTaxa = 0;
805
		protected long taxonNames = 0;
806
		protected long synonyms = 0;
807
		protected long descrSourceRef = 0;
808
		protected long nomenclRef = 0;
809
		protected long allReferences = 0;
810
		protected long descriptions = 0;
811

    
812
		public void addAll(MyCounter otherCounter) {
813
			classifications += otherCounter.classifications;
814
			allTaxa += otherCounter.allTaxa;
815
			aceptedTaxa += otherCounter.aceptedTaxa;
816
			taxonNames += otherCounter.taxonNames;
817
			synonyms += otherCounter.synonyms;
818
			descrSourceRef += otherCounter.descrSourceRef;
819
			nomenclRef += otherCounter.nomenclRef;
820
			allReferences += otherCounter.allReferences;
821
			descriptions += otherCounter.descriptions;
822
		}
823

    
824
		public void reset() {
825
			classifications = 0;
826
			allTaxa = 0;
827
			aceptedTaxa = 0;
828
			taxonNames = 0;
829
			synonyms = 0;
830
			descrSourceRef = 0;
831
			nomenclRef = 0;
832
			allReferences = 0;
833
			descriptions = 0;
834
		}
835

    
836
		@Override
837
		public String toString() {
838
			return "{Taxon_names=" + taxonNames + ", Synonyms=" + synonyms
839
					+ ", Accepted_taxa=" + aceptedTaxa
840
					+ ", Nomenclatural_references=" + nomenclRef
841
					+ ", Classifications=" + classifications
842
					+ ", Descriptive_source_references=" + descrSourceRef
843
					+ ", References=" + allReferences + ", All_taxa=" + allTaxa
844
					+ "}\n";
845
		}
846
	}
847

    
848
    /* (non-Javadoc)
849
     * @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
850
     */
851
    @Override
852
    public void createTestDataSet() throws FileNotFoundException {
853
        // TODO Auto-generated method stub
854

    
855
    }
856

    
857
}
(21-21/33)