cleanup
[cdmlib.git] / cdmlib-persistence / src / test / java / eu / etaxonomy / cdm / persistence / dao / hibernate / statistics / StatisticsDaoHibernateImplTest.java
index 880dd49e7ca379cf995fdd94f40ead9721c13fb2..ff36298f24f99866ccf451a34add276d36a2c6de 100644 (file)
@@ -1,7 +1,16 @@
+/**
+* Copyright (C) 2009 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
 package eu.etaxonomy.cdm.persistence.dao.hibernate.statistics;
 
 import static org.junit.Assert.assertTrue;
 
+import java.io.FileNotFoundException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -20,20 +29,21 @@ import org.junit.Test;
 import org.unitils.spring.annotation.SpringBeanByType;
 
 import eu.etaxonomy.cdm.model.common.Language;
-import eu.etaxonomy.cdm.model.common.OriginalSourceType;
 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
 import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
 import eu.etaxonomy.cdm.model.description.TaxonDescription;
 import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
 import eu.etaxonomy.cdm.model.description.TextData;
-import eu.etaxonomy.cdm.model.name.BotanicalName;
+import eu.etaxonomy.cdm.model.name.IBotanicalName;
 import eu.etaxonomy.cdm.model.name.Rank;
+import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
+import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
 import eu.etaxonomy.cdm.model.reference.Reference;
 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
 import eu.etaxonomy.cdm.model.taxon.Classification;
 import eu.etaxonomy.cdm.model.taxon.Synonym;
-import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
+import eu.etaxonomy.cdm.model.taxon.SynonymType;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
 import eu.etaxonomy.cdm.model.view.context.AuditEventContextHolder;
@@ -46,21 +56,19 @@ import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
 import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonNodeDao;
 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
 
-public class StatisticsDaoHibernateImplTest extends
-               CdmTransactionalIntegrationTest {
+public class StatisticsDaoHibernateImplTest
+        extends CdmTransactionalIntegrationTest {
 
-       private static final boolean PRINTOUT = true;
+    private static final Logger logger = Logger.getLogger(StatisticsDaoHibernateImplTest.class);
 
        @SpringBeanByType
        private IStatisticsDao statisticsDao;
 
-       private UUID nodeUuid;
+       @SuppressWarnings("unused")
+    private UUID nodeUuid;
 
        private List<Classification> classifications;
 
-       private static final Logger logger = Logger
-                       .getLogger(StatisticsDaoHibernateImplTest.class);
-
        // choose a number
        private static final int NO_OF_ACCEPTED_TAXA = 10;
 
@@ -75,12 +83,12 @@ public class StatisticsDaoHibernateImplTest extends
        private static final int NO_OF_SHARED_TAXA = 4;
 
        // must be NO_OF_ACCEPTED_TAXA + NO_OF_SYNONYMS
-       private static final int NO_OF_ALLTAXA = NO_OF_ACCEPTED_TAXA
-                       + NO_OF_SYNONYMS;
+       private static final int NO_OF_ALLTAXA =
+               NO_OF_ACCEPTED_TAXA + NO_OF_SYNONYMS;
 
        // must be NO_OF_ACCEPTED_TAXA+NO_OF_SYNONYMS
-       private static final int NO_OF_TAXON_NAMES = NO_OF_ACCEPTED_TAXA
-                       + NO_OF_SYNONYMS;
+       private static final int NO_OF_TAXON_NAMES =
+               NO_OF_ACCEPTED_TAXA + NO_OF_SYNONYMS;
 
        // this represents an approx. no of the amount that will actually generated!
        private static final int NO_OF_DESCRIPTIVE_SOURCE_REFERENCES = 16;
@@ -88,8 +96,8 @@ public class StatisticsDaoHibernateImplTest extends
        // private static final int NO_OF_ALL_REFERENCES = NO_OF_ACCEPTED_TAXA + 0;
 
        // must not be more than NO_OF_ACCEPTED_TAXA+NO_OF_SYNONYMS
-       private static final int NO_OF_NOMECLATURAL_REFERENCES = NO_OF_ACCEPTED_TAXA
-                       + NO_OF_SYNONYMS - 4;
+       private static final int NO_OF_NOMENCLATURAL_REFERENCES =
+               NO_OF_ACCEPTED_TAXA + NO_OF_SYNONYMS - 4;
 
        // --------------------variables for all ------------------
 
@@ -98,25 +106,22 @@ public class StatisticsDaoHibernateImplTest extends
 
        // ............................................
 
-       // log the type enum to an int constant:
-       private Map<String, Long> typeMap_ALL;
-
        // ------------------ variables for CLASSIFICATIONS -----------------------
 
        // int[] anArray = new int[NO_OF_CLASSIFICATIONS];
        private static List<Long> no_of_all_taxa_c = new ArrayList<Long>(
                        Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
-       private static List<Long> no_of_accepted_taxa_c = new ArrayList<Long>(
+       private static List<Long> no_of_accepted_taxa_c = new ArrayList<>(
                        Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
-       private static List<Long> no_of_synonyms_c = new ArrayList<Long>(
+       private static List<Long> no_of_synonyms_c = new ArrayList<>(
                        Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
-       private static List<Long> no_of_taxon_names_c = new ArrayList<Long>(
+       private static List<Long> no_of_taxon_names_c = new ArrayList<>(
                        Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
-       private static List<Long> no_of_descriptive_source_references_c = new ArrayList<Long>(
+       private static List<Long> no_of_descriptive_source_references_c = new ArrayList<>(
                        Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
-       private static List<Long> no_of_all_references_c = new ArrayList<Long>(
+       private static List<Long> no_of_all_references_c = new ArrayList<>(
                        Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
-       private static List<Long> no_of_nomenclatural_references_c = new ArrayList<Long>(
+       private static List<Long> no_of_nomenclatural_references_c = new ArrayList<>(
                        Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
        // we do not count classifications in classifications
 
@@ -133,11 +138,12 @@ public class StatisticsDaoHibernateImplTest extends
                        put("DESCRIPTIVE_SOURCE_REFERENCES",
                                        no_of_descriptive_source_references_c);
                        put("ALL_REFERENCES", no_of_all_references_c);
-                       put("NOMECLATURAL_REFERENCES", no_of_nomenclatural_references_c);
+                       put("NOMENCLATURAL_REFERENCES", no_of_nomenclatural_references_c);
                }
        };
 
        // ****************** services: ************************
+
        @SpringBeanByType
        private IStatisticsDao service;
        @SpringBeanByType
@@ -152,8 +158,7 @@ public class StatisticsDaoHibernateImplTest extends
        private IDescriptionElementDao descriptionElementDao;
        @SpringBeanByType
        private ITaxonNodeDao taxonNodeDao;
-       
-       private UUID rootUuid;
+
 
        @Before
        public void setUp() {
@@ -161,7 +166,6 @@ public class StatisticsDaoHibernateImplTest extends
                nodeUuid = UUID.fromString("0b5846e5-b8d2-4ca9-ac51-099286ea4adc");
 
                AuditEventContextHolder.clearContext();
-
        }
 
        @After
@@ -177,12 +181,13 @@ public class StatisticsDaoHibernateImplTest extends
                for (Classification classification : classifications) {
                        TaxonNode root;
                        root= createTaxTree(classification);
+
                        result=statisticsDao.getAllChildNodeIds(root.getUuid());
-                       System.out.println("classification "+ classification.getName()+": ");
-                       System.out.println("result: "+result.toString());
-                       System.out.println("");
+                   logger.debug("classification "+ classification.getName()+": ");
+                   logger.debug("result: "+result.toString());
+
                }
-               if (PRINTOUT) {
+               if (logger.isTraceEnabled()) {
                        print();
                }
                // result=statisticsDao.getAllTaxonIds(nodeUuid);
@@ -196,8 +201,7 @@ public class StatisticsDaoHibernateImplTest extends
                classifications = new ArrayList<Classification>();
 
                for (int i = 1; i <= NO_OF_CLASSIFICATIONS; i++) {
-                       Classification classification = Classification
-                                       .NewInstance("European Abies" + i);
+                       Classification classification = Classification.NewInstance("European Abies" + i);
                        classifications.add(classification);
                        classificationDao.save(classification);
                }
@@ -225,10 +229,8 @@ public class StatisticsDaoHibernateImplTest extends
                                && classiCounter < NO_OF_CLASSIFICATIONS; /* see below */) {
 
                        // compute no of taxa to be created in this classification
-                       if (classiCounter >= NO_OF_CLASSIFICATIONS - 1) { // last
-                                                                                                                               // classification
-                                                                                                                               // gets all left
-                                                                                                                               // taxa
+                       if (classiCounter >= NO_OF_CLASSIFICATIONS - 1) { // last classification
+                                                                                                                               // gets all left taxa
                                taxaInClass = remainder;
                        } else { // take half of left taxa for this class:
                                taxaInClass = remainder / 2;
@@ -243,11 +245,11 @@ public class StatisticsDaoHibernateImplTest extends
                                                + RandomStringUtils.randomAlphabetic(10);
 
                                // create a name for the taxon
-                               BotanicalName name = BotanicalName.NewInstance(Rank.SPECIES());
+                               IBotanicalName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
                                name.setNameCache(randomName, true);
 
                                // create nomenclatural reference for taxon name (if left)
-                               if (nomRefCounter < NO_OF_NOMECLATURAL_REFERENCES) {
+                               if (nomRefCounter < NO_OF_NOMENCLATURAL_REFERENCES) {
                                        // we remember this taxon has a nomenclatural reference:
                                        tNomRefFlag = true;
                                        Reference nomRef = ReferenceFactory.newBook();
@@ -336,11 +338,11 @@ public class StatisticsDaoHibernateImplTest extends
                                        randomName = RandomStringUtils.randomAlphabetic(5) + " "
                                                        + RandomStringUtils.randomAlphabetic(10);
                                        // name for synonym
-                                       name = BotanicalName.NewInstance(Rank.SPECIES());
+                                       name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
                                        name.setNameCache(randomName, true);
 
                                        // create nomenclatural reference for synonym name (if left)
-                                       if (nomRefCounter < NO_OF_NOMECLATURAL_REFERENCES) {
+                                       if (nomRefCounter < NO_OF_NOMENCLATURAL_REFERENCES) {
                                                sNomRefFlag = true;
                                                Reference nomRef = ReferenceFactory.newBook();
                                                name.setNomenclaturalReference(nomRef);
@@ -379,7 +381,7 @@ public class StatisticsDaoHibernateImplTest extends
                                        Synonym synonym = Synonym.NewInstance(name, sec);
                                        taxonDao.save(synonym);
                                        taxon.addSynonym(synonym,
-                                                       SynonymRelationshipType.SYNONYM_OF());
+                                                       SynonymType.SYNONYM_OF());
 
                                        synonymCounter++;
                                }
@@ -461,17 +463,10 @@ public class StatisticsDaoHibernateImplTest extends
                merge(no_of_accepted_taxa_c, no_of_synonyms_c, no_of_all_taxa_c);
                merge(no_of_all_references_c, no_of_nomenclatural_references_c,
                                no_of_all_references_c);
-
-               // TODO Auto-generated method stub
-
        }
 
        /**
         * create and count a new sec Reference
-        * 
-        * @param classiCounter
-        * @param taxonCounter
-        * @return
         */
        private Reference createSecReference(int classiCounter, int taxonCounter) {
                Reference sec;
@@ -503,37 +498,34 @@ public class StatisticsDaoHibernateImplTest extends
                for (int i = 0; i < NO_OF_CLASSIFICATIONS; i++) {
                        Long sum = no_of_sth1.get(i) + no_of_sth2.get(i);
                        no_of_sum.set(i, sum);
-
                }
        }
 
        private TaxonNode createTaxTree(Classification classification) {
                Random rand = new Random();
-               
+
                        Set<TaxonNode> nodes = classification.getAllNodes();
-                       ArrayList<TaxonNode> children = new ArrayList<TaxonNode>();
+                       ArrayList<TaxonNode> children = new ArrayList<>();
                        TaxonNode parent = nodes.iterator().next();
-                       
+
                        TaxonNode root = parent;
                        nodes.remove(parent);
                        while (!nodes.isEmpty()) {
                                int n = rand.nextInt(2) + 1;
                                for (int i = 1; i <= n && !(nodes.isEmpty()); i++) {
                                        TaxonNode nextNode = nodes.iterator().next();
-                                       parent.getChildNodes().add(nextNode);
+                                       nextNode = parent.addChildNode(nextNode, null, null);
                                        children.add(nextNode);
                                        nodes.remove(nextNode);
                                }
-                               taxonNodeDao.save(parent);
+
                                parent = children.get(0);
                                children.remove(0);
                        }
+
                return root;
        }
 
-       /**
-        * 
-        */
        private void print() {
                for (Classification classification : classifications) {
                        System.out.println("Classification:" + classification.toString());
@@ -568,6 +560,8 @@ public class StatisticsDaoHibernateImplTest extends
                }
                System.out.println();
                System.out.println("end!");
-
        }
+
+    @Override
+    public void createTestDataSet() throws FileNotFoundException {}
 }