1 4 package com.openedit.store; 5 6 import java.util.Iterator ; 7 import java.util.List ; 8 9 import junit.textui.TestRunner; 10 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 import org.apache.lucene.document.Document; 14 15 import com.openedit.WebPageRequest; 16 import com.openedit.modules.cart.CartModule; 17 import com.openedit.modules.search.LuceneHitTracker; 18 import com.openedit.modules.store.StoreSearchModule; 19 import com.openedit.store.edit.StoreEditor; 20 21 25 public class SearchTest extends StoreTestCase 26 { 27 private static final Log log = LogFactory.getLog(SearchTest.class); 28 29 protected StoreEditor fieldStoreEditor; 30 31 public StoreEditor getStoreEditor() throws Exception 32 { 33 if ( fieldStoreEditor == null) 34 { 35 fieldStoreEditor = new StoreEditor(); 36 fieldStoreEditor.setStore(getStore()); 37 fieldStoreEditor.setPageManager(getFixture().getPageManager()); 38 } 39 return fieldStoreEditor; 40 } 41 42 public SearchTest(String inName) 43 { 44 super( inName ); 45 } 46 public static void main(String [] args) throws Exception 47 { 48 TestRunner runner = new TestRunner(); 54 runner.run(SearchTest.class); 55 } 56 57 58 public void testMakeIndex() throws Exception 59 { 60 CartModule module = (CartModule)getFixture().getModuleManager().getModule("CartModule"); 62 Store store = getStore(); 63 Product three = store.getProduct("3"); 64 three.setAvailable(true); 65 store.saveProduct(three); 66 WebPageRequest context = getFixture().createPageRequest("/store/index.html"); 67 module.reIndexStore(context); 68 } 69 70 83 84 public void testSearch() throws Exception 85 { 86 StoreSearchModule module = (StoreSearchModule)getFixture().getModuleManager().getModule("StoreSearchModule"); 87 WebPageRequest context = getFixture().createPageRequest(); 88 context.setRequestParameter("query","catalogs:NOSUCHCATALOG"); 89 90 module.searchStore(context); 91 LuceneHitTracker hits = (LuceneHitTracker)context.getPageValue("hits"); 92 assertNotNull(hits); 93 assertFalse(hits.getAllHits().hasNext()); 95 } 96 97 public void testCatalogSearch() throws Exception 98 { 99 StoreEditor editor = getStoreEditor(); 100 Category catalog = editor.addNewCatalog( "SEARCHME","Ready for Searching"); 101 editor.saveCatalog( catalog ); 102 editor.setCurrentCatalog( catalog ); 103 Product product = null; 104 product = editor.getProduct("3"); 105 product.addCatalog(catalog); 106 product.setAvailable(true); 107 product.setDescription("Blank"); 108 editor.saveProduct(product); 109 product = editor.getProduct("1"); 110 product.addCatalog(catalog); 111 product.setAvailable(true); 112 product.setDescription("Blank"); 113 editor.saveProduct(product); 114 product = editor.getProduct("4"); 115 product.addCatalog(catalog); 116 product.setAvailable(true); 117 product.setDescription("Blank"); 118 editor.saveProduct(product); 119 WebPageRequest context = getFixture().createPageRequest("/store/categories/SEARCHME.html"); 121 getFixture().getModuleManager().executePathActions(context.getPage(),context); 122 LuceneHitTracker hits = (LuceneHitTracker)context.getPageValue("hits"); 123 assertNotNull(hits); 124 assertEquals(3,hits.getTotal()); 125 Iterator hitIterator = hits.getAllHits(); 126 Document doc = (Document) hitIterator.next(); 127 128 String id = doc.getField("id").stringValue(); 129 138 139 context = getFixture().createPageRequest("/store/categories/byprice/SEARCHME.html"); 140 141 getFixture().getModuleManager().executePathActions(context.getPage(),context); 142 hits = (LuceneHitTracker)context.getPageValue("hits"); 143 assertNotNull(hits); 144 assertEquals(3,hits.getTotal()); 145 hitIterator = hits.getAllHits(); 146 doc = (Document) hitIterator.next(); 147 id = doc.getField("id").stringValue(); 148 159 162 } 165 public void testWeirdIds() throws Exception 166 { 167 StoreEditor editor = getStoreEditor(); 168 Category catalog = editor.addNewCatalog( "SEA_R,Ch-ME.txt","Weird ID"); 169 editor.saveCatalog( catalog ); 170 editor.setCurrentCatalog( catalog ); 171 Product product = null; 172 product = editor.getProduct("4"); 173 product.addCatalog(catalog); 174 product.setAvailable(true); 175 getStore().getProductArchive().saveProductDescription(product,"Blank chris With chris_amy.jpg Category"); 176 editor.saveProduct(product); 177 178 186 LuceneHitTracker hits = editor.getStore().search("chris"); 187 assertEquals(1, hits.getTotal()); 188 189 192 } 193 private Product findProductById(Category inCatalog, String inId) throws Exception 194 { 195 List products = getStore().getProductsInCatalog( inCatalog ); 196 assertNotNull(products); 197 198 for (Iterator iter = products.iterator(); iter.hasNext();) 199 { 200 Product element = (Product) iter.next(); 201 if ( element.getId().equals(inId)) 202 { 203 return element; 204 } 205 } 206 return null; 207 } 208 209 public void testAdvancedSearch() throws Exception 210 { 211 StoreSearchModule module = (StoreSearchModule)getFixture().getModuleManager().getModule("StoreSearchModule"); 212 WebPageRequest context = getFixture().createPageRequest("/store/index.html"); 213 context.setRequestParameter("categoryid","testing"); 214 context.setRequestParameter("andwords","andthis"); 215 context.setRequestParameter("quotewords","this exactly"); 216 context.setRequestParameter("orwords","orthis1 orthis2"); 217 context.setRequestParameter("notwords","nothis"); 218 context.setRequestParameter("sortby","random"); 220 221 module.advancedSearch(context); 222 LuceneHitTracker hits = (LuceneHitTracker)context.getPageValue("hits"); 223 assertNotNull(hits); 224 String query = hits.getQuery(); 225 String q = " NOT nothis +category:testing +andthis +\"this exactly\" +(orthis1 OR orthis2)"; assertTrue(query.startsWith(q)); 229 } 230 231 public void testFieldSearch() throws Exception 232 { 233 StoreSearchModule module = (StoreSearchModule)getFixture().getModuleManager().getModule("StoreSearchModule"); 234 WebPageRequest context = getFixture().createPageRequest("/store/index.html"); 235 250 context.setRequestParameter("field",new String [] {"name","department","imagenumber", "dateshot"}); 251 context.setRequestParameter("fieldid",new String [] {"name","department","imagenumber", "dateshot"}); 252 context.setRequestParameter("operation",new String [] {"matches","matches","startswith", "after"}); 253 context.setRequestParameter("value",new String [] {"test","01","123", "1/10/2004"}); 254 module.fieldSearch(context); 255 LuceneHitTracker hits = (LuceneHitTracker)context.getPageValue("hits"); 256 assertNotNull(hits); 257 String query = hits.getQuery(); 258 String q = "name:test +department:01 +imagenumber:123* +dateshot:[20040110 TO null]"; 259 assertEquals(q, query); 261 } 262 263 264 } 265 | Popular Tags |