KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > store > SearchTest


1 /*
2  * Created on May 2, 2004
3  */

4 package com.openedit.store;
5
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
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 /**
22  * @author cburkey
23  *
24  */

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 JavaDoc
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 JavaDoc inName)
43      {
44         super( inName );
45      }
46         public static void main(String JavaDoc[] args) throws Exception JavaDoc
47         {
48 // runner = new TestRunner();
49
//TestRunner.run(ProductEditTest.class);
50
//SearchTest test = new SearchTest("SearchTest");
51
//test.setUp();
52
//test.testMakeIndex();
53
TestRunner runner = new TestRunner();
54             runner.run(SearchTest.class);
55         }
56
57      
58      public void testMakeIndex() throws Exception JavaDoc
59      {
60         //store.reindexall
61
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      /* TODO: Write a test for store.getProperties("sometype")
71      public void testFieldSearchLoad() throws Exception
72      {
73             //load the store search module
74         
75             StoreSearchModule module = (StoreSearchModule)getFixture().getModuleManager().getModule("StoreSearchModule");
76             WebPageRequest context = getFixture().createPageRequest("/store/index.html");
77             module.loadSearchFields(context);
78             List fields = (List) context.getPageValue("searchFieldList");
79             assertNotNull(fields);
80         
81      }
82      */

83      
84      public void testSearch() throws Exception JavaDoc
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         //This product should not be available
94
assertFalse(hits.getAllHits().hasNext());
95      }
96  
97     public void testCatalogSearch() throws Exception JavaDoc
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         //editor.getStore().reindexAll();
120
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 JavaDoc hitIterator = hits.getAllHits();
126         Document doc = (Document) hitIterator.next();
127         
128         String JavaDoc id = doc.getField("id").stringValue();
129         /*
130         assertEquals( "1",id);
131         doc = (Document) hitIterator.next();
132         id = doc.getField("id").stringValue();
133         assertEquals( "3",id);
134         doc = (Document) hitIterator.next();
135         id = doc.getField("id").stringValue();
136         assertEquals( "4",id);
137 */

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 // log.info(id);
149
// assertEquals( "1",id);
150
// doc = (Document) hitIterator.next();
151
// id = doc.getField("id").stringValue();
152
// log.info(id);
153
// assertEquals( "4",id);
154
// doc = (Document) hitIterator.next();
155
// id = doc.getField("id").stringValue();
156
// log.info(id);
157
// assertEquals( "3",id);
158

159         //Document hit = hits.getHits().doc(0);
160
//Document hit2 = hits.getHits().doc(1);
161

162         //assertEquals("PAAAIAOKJOLAOMAH",hit.get("id"));
163
//assertEquals("PAAAIAMBOGHOOMAH",hit2.get("id"));
164
}
165     public void testWeirdIds() throws Exception JavaDoc
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         /*
179         Category fake = new Category("sea","Fake");
180         Product found = findProductById(fake,"4");
181         assertNull(found);
182
183         found = findProductById(catalog,"4");
184         assertNotNull(found);
185         */

186         LuceneHitTracker hits = editor.getStore().search("chris");
187         assertEquals(1, hits.getTotal());
188         
189         //hits = editor.getStore().search("really");
190
//assertEquals(1, hits.length());
191

192     }
193     private Product findProductById(Category inCatalog, String JavaDoc inId) throws Exception JavaDoc
194     {
195         List JavaDoc products = getStore().getProductsInCatalog( inCatalog );
196         assertNotNull(products);
197
198         for (Iterator JavaDoc 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 JavaDoc
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("daterange","12");
219
context.setRequestParameter("sortby","random");
220     
221         module.advancedSearch(context);
222         LuceneHitTracker hits = (LuceneHitTracker)context.getPageValue("hits");
223         assertNotNull(hits);
224         String JavaDoc query = hits.getQuery();
225         //String q = "NOT nothis AND category:testing AND andthis AND \"this exactly\" AND (orthis1 OR orthis2) AND date:";//[07/19/2005 TO 07/19/2006] ";
226
String JavaDoc q = " NOT nothis +category:testing +andthis +\"this exactly\" +(orthis1 OR orthis2)";//NOT nothis +category:testing +andthis +\"this exactly\" +(orthis1 OR orthis2) +date:[";
227
//assertFalse(hits.getAllHits().hasNext());
228
assertTrue(query.startsWith(q));
229      }
230  
231      public void testFieldSearch() throws Exception JavaDoc
232      {
233         StoreSearchModule module = (StoreSearchModule)getFixture().getModuleManager().getModule("StoreSearchModule");
234         WebPageRequest context = getFixture().createPageRequest("/store/index.html");
235     /*
236         <input name="field" value="Product Name" type="hidden">
237 <input name="op" value="contains" type="hidden">
238 <input name="value" value="" size="15" type="text"><br>
239     <input value="go" name="go" type="submit"><br>
240     <input name="sorton" value="" type="hidden">
241     <input name="ascending" value="1" type="hidden">
242 <br>
243 <input name="field" value="Department" type="hidden">
244 Department<br>
245 matches<br>
246 <input name="op" value="matches" type="hidden">
247 <select name="value">
248 <option></option>
249 */

250         context.setRequestParameter("field",new String JavaDoc[] {"name","department","imagenumber", "dateshot"});
251         context.setRequestParameter("fieldid",new String JavaDoc[] {"name","department","imagenumber", "dateshot"});
252         context.setRequestParameter("operation",new String JavaDoc[] {"matches","matches","startswith", "after"});
253         context.setRequestParameter("value",new String JavaDoc[] {"test","01","123", "1/10/2004"});
254         module.fieldSearch(context);
255         LuceneHitTracker hits = (LuceneHitTracker)context.getPageValue("hits");
256         assertNotNull(hits);
257         String JavaDoc query = hits.getQuery();
258         String JavaDoc q = "name:test +department:01 +imagenumber:123* +dateshot:[20040110 TO null]";
259         //assertFalse(hits.getAllHits().hasNext());
260
assertEquals(q, query);
261      }
262  
263      
264 }
265
Popular Tags