Merge branch 'develop' into ticket-5066
[cdmlib.git] / cdmlib-persistence / src / test / java / eu / etaxonomy / cdm / persistence / dao / hibernate / FreeTextSearchIntegration.java
index 0d66ec8fb665458b2512c77a7b7915c072a854b6..24ff27fb82d9b74d5882bc55beb4dad7339ddf7c 100644 (file)
-/**\r
-* Copyright (C) 2009 EDIT\r
-* European Distributed Institute of Taxonomy\r
-* http://www.e-taxonomy.eu\r
-*\r
-* The contents of this file are subject to the Mozilla Public License Version 1.1\r
-* See LICENSE.TXT at the top of this package for the full license terms.\r
-*/\r
-\r
-package eu.etaxonomy.cdm.persistence.dao.hibernate;\r
-import static org.junit.Assert.assertEquals;\r
-import static org.junit.Assert.assertNotNull;\r
-import static org.junit.Assert.assertTrue;\r
-\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-\r
-import org.apache.commons.logging.Log;\r
-import org.apache.commons.logging.LogFactory;\r
-import org.hibernate.Hibernate;\r
-import org.junit.Ignore;\r
-import org.junit.Test;\r
-import org.unitils.dbunit.annotation.DataSet;\r
-import org.unitils.spring.annotation.SpringBeanByType;\r
-\r
-import eu.etaxonomy.cdm.model.description.DescriptionElementBase;\r
-import eu.etaxonomy.cdm.model.description.TaxonDescription;\r
-import eu.etaxonomy.cdm.model.description.TextData;\r
-import eu.etaxonomy.cdm.model.taxon.TaxonBase;\r
-import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionElementDao;\r
-import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;\r
-import eu.etaxonomy.cdm.persistence.query.OrderHint;\r
-import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;\r
-import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;\r
-\r
-@DataSet\r
-@Ignore //TODO indexing does not work at all, even before the unitils upgrade\r
-public class FreeTextSearchIntegration extends CdmTransactionalIntegrationTest {\r
-\r
-       private static Log log = LogFactory.getLog(FreeTextSearchIntegration.class);\r
-\r
-       @SpringBeanByType\r
-       ITaxonDao taxonDao;\r
-\r
-       @SpringBeanByType\r
-       IDescriptionElementDao descriptionElementDao;\r
-\r
-//     @SpringBeanByType\r
-//     TaxonAlternativeSpellingSuggestionParser alternativeSpellingSuggestionParser;\r
-//\r
-//     @Test\r
-//     public void test() {\r
-//             taxonDao.rebuildIndex();\r
-//             taxonDao.optimizeIndex();\r
-//\r
-//             descriptionElementDao.rebuildIndex();\r
-//             descriptionElementDao.optimizeIndex();\r
-//             setComplete();\r
-//             endTransaction();\r
-//             taxonDao.countTaxa("Arum",null); // For some reason this flushes the indexes and allows the next method to create the spellings index\r
-//     }\r
-//\r
-//     @Test\r
-//     public void test1() {\r
-//             alternativeSpellingSuggestionParser.refresh();\r
-//     }\r
-\r
-       @Test\r
-       public void testSearchTextData() {\r
-               List<OrderHint> orderHints = new ArrayList<OrderHint>();\r
-               orderHints.add(new OrderHint("inDescription.titleCache",SortOrder.ASCENDING));\r
-\r
-               List<String> propertyPaths = new ArrayList<String>();\r
-               propertyPaths.add("inDescription");\r
-               propertyPaths.add("inDescription.taxon");\r
-               List<DescriptionElementBase> results = descriptionElementDao.search(TextData.class,"Lorem",null,null,orderHints,propertyPaths);\r
-\r
-               assertNotNull("searchTextData should return a List",results);\r
-               assertEquals("there should be 4 TextData entities in the list",4,results.size());\r
-\r
-               assertTrue("DescriptionElementBase.feature should be initialized",Hibernate.isInitialized(results.get(0).getFeature()));\r
-               assertTrue("DescriptionElementBase.inDescription should be initialized",Hibernate.isInitialized(results.get(0).getInDescription()));\r
-               assertTrue("inDescription should be an instance of TaxonDescription",results.get(0).getInDescription() instanceof TaxonDescription);\r
-               TaxonDescription taxonDescription = (TaxonDescription)results.get(0).getInDescription();\r
-               assertTrue("TaxonDescription.taxon should be initialized",Hibernate.isInitialized(taxonDescription.getTaxon()));\r
-               assertEquals("The results should be sorted alphabetically","Aglaodorum Schott sec. cate-araceae.org",taxonDescription.getTaxon().getTitleCache());\r
-       }\r
-\r
-    @Test\r
-    public void testCountTextData() {\r
-       int matches = descriptionElementDao.count(TextData.class,"Lorem");\r
-       assertEquals("countTextData should return 4",4,matches);\r
-    }\r
-\r
-    @Test\r
-    public void testSearchWord() {\r
-       List<OrderHint> orderHints = new ArrayList<OrderHint>();\r
-       orderHints.add(new OrderHint("name.titleCache",SortOrder.ASCENDING));\r
-       List<String> propertyPaths = new ArrayList<String>();\r
-       propertyPaths.add("name");\r
-\r
-       List<TaxonBase> results = taxonDao.search(null,"Arum", null, null, orderHints, propertyPaths);\r
-               assertEquals("searchTaxa should return 463 results",46,results.size());\r
-               assertTrue("TaxonBase.name should be initialized",Hibernate.isInitialized(results.get(0).getName()));\r
-    }\r
-\r
-    @Test\r
-    public void testSearchCount() {\r
-               int numberOfResults = taxonDao.count(null,"Arum");\r
-               assertEquals("countTaxa should return 46",46,numberOfResults);\r
-\r
-    }\r
-\r
-    @Test\r
-    public void testSearchPaged() {\r
-       List<OrderHint> orderHints = new ArrayList<OrderHint>();\r
-       orderHints.add(new OrderHint("name.titleCache",SortOrder.ASCENDING));\r
-       List<String> propertyPaths = new ArrayList<String>();\r
-       propertyPaths.add("name");\r
-               List<TaxonBase> page1 = taxonDao.search(null,"Arum", 30, 0,orderHints,propertyPaths);\r
-               List<TaxonBase> page2 = taxonDao.search(null,"Arum", 30, 1,orderHints,propertyPaths);\r
-\r
-               assertEquals("page 1 should contain 30 taxa",30,page1.size());\r
-               assertEquals("page 1 should be sorted alphabetically","Arum L.",page1.get(0).getName().getTitleCache());\r
-               assertEquals("page 1 should be sorted alphabetically","Arum lucanum Cavara & Grande",page1.get(29).getName().getTitleCache());\r
-               assertEquals("page 2 should contain 16 taxa",16,page2.size());\r
-               assertEquals("page 2 should be sorted alphabetically","Arum maculatum L.",page2.get(0).getName().getTitleCache());\r
-               assertEquals("page 2 should be sorted alphabetically","Arum x sooi TerpĆ³",page2.get(15).getName().getTitleCache());\r
-    }\r
-\r
-    @Test\r
-    public void testSearchPhrase() {\r
-       List<OrderHint> orderHints = new ArrayList<OrderHint>();\r
-       orderHints.add(new OrderHint("name.titleCache",SortOrder.ASCENDING));\r
-       List<String> propertyPaths = new ArrayList<String>();\r
-       propertyPaths.add("name");\r
-\r
-               List<TaxonBase> results = taxonDao.search(null,"\"Arum italicum\"", null, null,orderHints,propertyPaths);\r
-               assertEquals("searchTaxa should return 5 taxa",5,results.size());\r
-    }\r
-\r
-    @Test\r
-    public void testSearchWildcard()  {\r
-       List<OrderHint> orderHints = new ArrayList<OrderHint>();\r
-       orderHints.add(new OrderHint("name.titleCache",SortOrder.ASCENDING));\r
-       List<String> propertyPaths = new ArrayList<String>();\r
-       propertyPaths.add("name");\r
-\r
-               List<TaxonBase> results = taxonDao.search(null,"Aroph*",  null, null,orderHints,propertyPaths);\r
-               assertEquals("searchTaxa should return 6 taxa",7,results.size());\r
-    }\r
-\r
-    @Test\r
-    public void testSuggestSingleTerm() {\r
-       String suggestion = taxonDao.suggestQuery("Aram");\r
-       assertNotNull("suggestQuery should return a String",suggestion);\r
-       assertEquals("The spelling suggestion for \"Aram\" should be \"arum\"","arum",suggestion);\r
-    }\r
-\r
-    @Test\r
-    public void testSuggestSingleTermInCompositeQuery() {\r
-       String suggestion = taxonDao.suggestQuery("Aram italicum");\r
-       assertNotNull("suggestQuery should return a String",suggestion);\r
-       assertEquals("The spelling suggestion for \"Aram italicum\" should be \"arum italicum\"","arum italicum",suggestion);\r
-    }\r
-\r
-    @Test\r
-    public void testSuggestMultipleTermsInCompositeQuery() {\r
-       String suggestion = taxonDao.suggestQuery("Aram italocum");\r
-       assertNotNull("suggestQuery should return a String",suggestion);\r
-       assertEquals("The spelling suggestion for \"Aram italocum\" should be \"arum italicum\"","arum italicum",suggestion);\r
-    }\r
-\r
-    @Test\r
-    public void testSuggestMultipleTermsInCompositeQueryWithAnd() {\r
-       String suggestion = taxonDao.suggestQuery("Aram AND italocum");\r
-       assertNotNull("suggestQuery should return a String",suggestion);\r
-       assertEquals("The spelling suggestion for \"Aram AND italocum\" should be \"+arum +italicum\"","+arum +italicum",suggestion);\r
-    }\r
-}\r
+/**
+* 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;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.FileNotFoundException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.hibernate.Hibernate;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.unitils.dbunit.annotation.DataSet;
+import org.unitils.spring.annotation.SpringBeanByType;
+
+import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
+import eu.etaxonomy.cdm.model.description.TaxonDescription;
+import eu.etaxonomy.cdm.model.description.TextData;
+import eu.etaxonomy.cdm.model.taxon.TaxonBase;
+import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionElementDao;
+import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
+import eu.etaxonomy.cdm.persistence.query.OrderHint;
+import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
+import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
+
+@DataSet
+@Ignore //TODO indexing does not work at all, even before the unitils upgrade
+public class FreeTextSearchIntegration extends CdmTransactionalIntegrationTest {
+
+       @SuppressWarnings("unused")
+       private static Log log = LogFactory.getLog(FreeTextSearchIntegration.class);
+
+       @SpringBeanByType
+       ITaxonDao taxonDao;
+
+       @SpringBeanByType
+       IDescriptionElementDao descriptionElementDao;
+
+//     @SpringBeanByType
+//     TaxonAlternativeSpellingSuggestionParser alternativeSpellingSuggestionParser;
+//
+//     @Test
+//     public void test() {
+//             taxonDao.rebuildIndex();
+//             taxonDao.optimizeIndex();
+//
+//             descriptionElementDao.rebuildIndex();
+//             descriptionElementDao.optimizeIndex();
+//             setComplete();
+//             endTransaction();
+//             taxonDao.countTaxa("Arum",null); // For some reason this flushes the indexes and allows the next method to create the spellings index
+//     }
+//
+//     @Test
+//     public void test1() {
+//             alternativeSpellingSuggestionParser.refresh();
+//     }
+
+       @Test
+       public void testSearchTextData() {
+               List<OrderHint> orderHints = new ArrayList<OrderHint>();
+               orderHints.add(new OrderHint("inDescription.titleCache",SortOrder.ASCENDING));
+
+               List<String> propertyPaths = new ArrayList<String>();
+               propertyPaths.add("inDescription");
+               propertyPaths.add("inDescription.taxon");
+               List<DescriptionElementBase> results = descriptionElementDao.search(TextData.class,"Lorem",null,null,orderHints,propertyPaths);
+
+               assertNotNull("searchTextData should return a List",results);
+               assertEquals("there should be 4 TextData entities in the list",4,results.size());
+
+               assertTrue("DescriptionElementBase.feature should be initialized",Hibernate.isInitialized(results.get(0).getFeature()));
+               assertTrue("DescriptionElementBase.inDescription should be initialized",Hibernate.isInitialized(results.get(0).getInDescription()));
+               assertTrue("inDescription should be an instance of TaxonDescription",results.get(0).getInDescription() instanceof TaxonDescription);
+               TaxonDescription taxonDescription = (TaxonDescription)results.get(0).getInDescription();
+               assertTrue("TaxonDescription.taxon should be initialized",Hibernate.isInitialized(taxonDescription.getTaxon()));
+               assertEquals("The results should be sorted alphabetically","Aglaodorum Schott sec. cate-araceae.org",taxonDescription.getTaxon().getTitleCache());
+       }
+
+    @Test
+    public void testCountTextData() {
+       int matches = descriptionElementDao.count(TextData.class,"Lorem");
+       assertEquals("countTextData should return 4",4,matches);
+    }
+
+    @Test
+    public void testSearchWord() {
+       List<OrderHint> orderHints = new ArrayList<OrderHint>();
+       orderHints.add(new OrderHint("name.titleCache",SortOrder.ASCENDING));
+       List<String> propertyPaths = new ArrayList<String>();
+       propertyPaths.add("name");
+
+       List<TaxonBase> results = taxonDao.search(null,"Arum", null, null, orderHints, propertyPaths);
+               assertEquals("searchTaxa should return 463 results",46,results.size());
+               assertTrue("TaxonBase.name should be initialized",Hibernate.isInitialized(results.get(0).getName()));
+    }
+
+    @Test
+    public void testSearchCount() {
+               int numberOfResults = taxonDao.count(null,"Arum");
+               assertEquals("countTaxa should return 46",46,numberOfResults);
+
+    }
+
+    @Test
+    public void testSearchPaged() {
+       List<OrderHint> orderHints = new ArrayList<OrderHint>();
+       orderHints.add(new OrderHint("name.titleCache",SortOrder.ASCENDING));
+       List<String> propertyPaths = new ArrayList<String>();
+       propertyPaths.add("name");
+               List<TaxonBase> page1 = taxonDao.search(null,"Arum", 30, 0,orderHints,propertyPaths);
+               List<TaxonBase> page2 = taxonDao.search(null,"Arum", 30, 1,orderHints,propertyPaths);
+
+               assertEquals("page 1 should contain 30 taxa",30,page1.size());
+               assertEquals("page 1 should be sorted alphabetically","Arum L.",page1.get(0).getName().getTitleCache());
+               assertEquals("page 1 should be sorted alphabetically","Arum lucanum Cavara & Grande",page1.get(29).getName().getTitleCache());
+               assertEquals("page 2 should contain 16 taxa",16,page2.size());
+               assertEquals("page 2 should be sorted alphabetically","Arum maculatum L.",page2.get(0).getName().getTitleCache());
+               assertEquals("page 2 should be sorted alphabetically","Arum x sooi TerpĆ³",page2.get(15).getName().getTitleCache());
+    }
+
+    @Test
+    public void testSearchPhrase() {
+       List<OrderHint> orderHints = new ArrayList<OrderHint>();
+       orderHints.add(new OrderHint("name.titleCache",SortOrder.ASCENDING));
+       List<String> propertyPaths = new ArrayList<String>();
+       propertyPaths.add("name");
+
+               List<TaxonBase> results = taxonDao.search(null,"\"Arum italicum\"", null, null,orderHints,propertyPaths);
+               assertEquals("searchTaxa should return 5 taxa",5,results.size());
+    }
+
+    @Test
+    public void testSearchWildcard()  {
+       List<OrderHint> orderHints = new ArrayList<OrderHint>();
+       orderHints.add(new OrderHint("name.titleCache",SortOrder.ASCENDING));
+       List<String> propertyPaths = new ArrayList<String>();
+       propertyPaths.add("name");
+
+               List<TaxonBase> results = taxonDao.search(null,"Aroph*",  null, null,orderHints,propertyPaths);
+               assertEquals("searchTaxa should return 6 taxa",7,results.size());
+    }
+
+    @Test
+    @Ignore //we currently don't use suggest anymore
+    public void testSuggestSingleTerm() {
+       String suggestion = taxonDao.suggestQuery("Aram");
+       assertNotNull("suggestQuery should return a String",suggestion);
+       assertEquals("The spelling suggestion for \"Aram\" should be \"arum\"","arum",suggestion);
+    }
+
+    @Test
+    @Ignore //we currently don't use suggest anymore
+    public void testSuggestSingleTermInCompositeQuery() {
+       String suggestion = taxonDao.suggestQuery("Aram italicum");
+       assertNotNull("suggestQuery should return a String",suggestion);
+       assertEquals("The spelling suggestion for \"Aram italicum\" should be \"arum italicum\"","arum italicum",suggestion);
+    }
+
+    @Test
+    @Ignore //we currently don't use suggest anymore
+    public void testSuggestMultipleTermsInCompositeQuery() {
+       String suggestion = taxonDao.suggestQuery("Aram italocum");
+       assertNotNull("suggestQuery should return a String",suggestion);
+       assertEquals("The spelling suggestion for \"Aram italocum\" should be \"arum italicum\"","arum italicum",suggestion);
+    }
+
+    @Test
+    @Ignore //we currently don't use suggest anymore
+    public void testSuggestMultipleTermsInCompositeQueryWithAnd() {
+       String suggestion = taxonDao.suggestQuery("Aram AND italocum");
+       assertNotNull("suggestQuery should return a String",suggestion);
+       assertEquals("The spelling suggestion for \"Aram AND italocum\" should be \"+arum +italicum\"","+arum +italicum",suggestion);
+    }
+
+    /* (non-Javadoc)
+     * @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
+     */
+    @Override
+    public void createTestDataSet() throws FileNotFoundException {
+        // TODO Auto-generated method stub
+
+    }
+}