1 package org.mmbase.storage.search.implementation; 2 3 import junit.framework.*; 4 import java.util.*; 5 import org.mmbase.module.core.*; 6 import org.mmbase.module.corebuilders.*; 7 import org.mmbase.storage.search.*; 8 import org.mmbase.storage.search.StringSearchConstraint; 9 10 16 public class BasicStringSearchConstraintTest extends TestCase { 17 18 private final static String BUILDER_NAME = "images"; 19 private final static String FIELD_NAME1 = "title"; 20 private final static String FIELD_NAME2 = "number"; 21 private final static String SEARCHTERMS1 = "some search terms"; 22 23 24 private BasicStringSearchConstraint instance = null; 25 26 27 private MMBase mmbase = null; 28 29 30 private BasicStepField field1 = null; 31 32 33 private StepField field2 = null; 34 35 public BasicStringSearchConstraintTest(java.lang.String testName) { 36 super(testName); 37 } 38 39 public static void main(java.lang.String [] args) { 40 junit.textui.TestRunner.run(suite()); 41 } 42 43 46 public void setUp() throws Exception { 47 MMBaseContext.init(); 48 mmbase = MMBase.getMMBase(); 49 MMObjectBuilder builder = mmbase.getBuilder(BUILDER_NAME); 50 Step step = new BasicStep(builder); 51 52 FieldDefs fieldDefs = builder.getField(FIELD_NAME1); 54 field1 = new BasicStepField(step, fieldDefs); 55 56 fieldDefs = builder.getField(FIELD_NAME2); 58 field2 = new BasicStepField(step, fieldDefs); 59 60 instance = new BasicStringSearchConstraint(field1, 61 StringSearchConstraint.SEARCH_TYPE_PHRASE_ORIENTED, 62 StringSearchConstraint.MATCH_TYPE_LITERAL, SEARCHTERMS1); 63 } 64 65 68 public void tearDown() throws Exception {} 69 70 71 public void testConstructor1() { 72 List searchTerms 73 = Arrays.asList(new String [] {"some", "search", "terms"}); 74 try { 76 new BasicStringSearchConstraint(field2, 77 StringSearchConstraint.SEARCH_TYPE_PHRASE_ORIENTED, 78 StringSearchConstraint.MATCH_TYPE_LITERAL, searchTerms); 79 fail("Applied to integer field, should throw IllegalArgumentException."); 80 } catch(IllegalArgumentException e) {} 81 82 try { 84 new BasicStringSearchConstraint(field1, 85 StringSearchConstraint.SEARCH_TYPE_PHRASE_ORIENTED, 86 StringSearchConstraint.MATCH_TYPE_LITERAL, new ArrayList()); 87 fail("Empty searchterm string, should throw IllegalArgumentException."); 88 } catch(IllegalArgumentException e) {} 89 } 90 91 92 public void testConstructor2() { 93 try { 95 new BasicStringSearchConstraint(field2, 96 StringSearchConstraint.SEARCH_TYPE_PHRASE_ORIENTED, 97 StringSearchConstraint.MATCH_TYPE_LITERAL, SEARCHTERMS1); 98 fail("Applied to integer field, should throw IllegalArgumentException."); 99 } catch(IllegalArgumentException e) {} 100 101 try { 103 new BasicStringSearchConstraint(field1, 104 StringSearchConstraint.SEARCH_TYPE_PHRASE_ORIENTED, 105 StringSearchConstraint.MATCH_TYPE_LITERAL, " "); 106 fail("Empty searchterm string, should throw IllegalArgumentException."); 107 } catch(IllegalArgumentException e) {} 108 } 109 110 111 public void testGetBasicSupportLevel() { 112 assertTrue( 114 instance.getBasicSupportLevel() == SearchQueryHandler.SUPPORT_NONE); 115 } 116 117 118 public void testSetMatchType() { 119 try { 120 instance.setMatchType(0); 122 fail("Invalid value, should throw IllegalArgumentException."); 123 } catch (IllegalArgumentException e) {} 124 125 try { 126 instance.setMatchType(4); 128 fail("Invalid value, should throw IllegalArgumentException."); 129 } catch (IllegalArgumentException e) {} 130 131 instance.setMatchType(StringSearchConstraint.MATCH_TYPE_LITERAL); 132 assertTrue( 133 instance.getMatchType() == StringSearchConstraint.MATCH_TYPE_LITERAL); 134 instance.setMatchType(StringSearchConstraint.MATCH_TYPE_FUZZY); 135 assertTrue( 136 instance.getMatchType() == StringSearchConstraint.MATCH_TYPE_FUZZY); 137 BasicStringSearchConstraint result 138 = instance.setMatchType(StringSearchConstraint.MATCH_TYPE_SYNONYM); 139 assertTrue( 140 instance.getMatchType() == StringSearchConstraint.MATCH_TYPE_SYNONYM); 141 assertTrue(result == instance); 142 } 143 144 145 public void testSetSearchType() { 146 try { 147 instance.setSearchType(0); 149 fail("Invalid value, should throw IllegalArgumentException."); 150 } catch (IllegalArgumentException e) {} 151 152 try { 153 instance.setSearchType(4); 155 fail("Invalid value, should throw IllegalArgumentException."); 156 } catch (IllegalArgumentException e) {} 157 158 instance.setSearchType(StringSearchConstraint.SEARCH_TYPE_WORD_ORIENTED); 159 assertTrue(instance.getSearchType() 160 == StringSearchConstraint.SEARCH_TYPE_WORD_ORIENTED); 161 instance.setSearchType(StringSearchConstraint.SEARCH_TYPE_PHRASE_ORIENTED); 162 assertTrue(instance.getSearchType() 163 == StringSearchConstraint.SEARCH_TYPE_PHRASE_ORIENTED); 164 BasicStringSearchConstraint result 165 = instance.setSearchType(StringSearchConstraint.SEARCH_TYPE_PROXIMITY_ORIENTED); 166 assertTrue(instance.getSearchType() 167 == StringSearchConstraint.SEARCH_TYPE_PROXIMITY_ORIENTED); 168 assertTrue(result == instance); 169 } 170 171 public void testSetParameter() { 172 try { 173 instance.setParameter("ongeldige parameter", new Integer (0)); 175 fail("Invalid parameter name, should throw IllegalArgumentException."); 176 } catch (IllegalArgumentException e) {} 177 178 try { 179 instance.setParameter( 181 StringSearchConstraint.PARAM_FUZZINESS, new Integer (0)); 182 fail("Invalid parameter type, should throw IllegalArgumentException."); 183 } catch (IllegalArgumentException e) {} 184 185 try { 186 instance.setParameter( 188 StringSearchConstraint.PARAM_PROXIMITY_LIMIT, new Float (1.0)); 189 fail("Invalid parameter type, should throw IllegalArgumentException."); 190 } catch (IllegalArgumentException e) {} 191 192 try { 193 instance.setParameter( 195 StringSearchConstraint.PARAM_FUZZINESS, new Float (-1.0)); 196 fail("Invalid parameter value, should throw IllegalArgumentException."); 197 } catch (IllegalArgumentException e) {} 198 199 try { 200 instance.setParameter( 202 StringSearchConstraint.PARAM_FUZZINESS, new Float (1.1)); 203 fail("Invalid parameter value, should throw IllegalArgumentException."); 204 } catch (IllegalArgumentException e) {} 205 206 try { 207 instance.setParameter( 209 StringSearchConstraint.PARAM_PROXIMITY_LIMIT, new Integer (0)); 210 fail("Invalid parameter value, should throw IllegalArgumentException."); 211 } catch (IllegalArgumentException e) {} 212 213 Float fuzziness = new Float (0.5); 214 instance.setMatchType(StringSearchConstraint.MATCH_TYPE_LITERAL); 215 216 try { 218 instance.setParameter(StringSearchConstraint.PARAM_FUZZINESS, fuzziness); 220 fail("Invalid matchtype, should throw IllegalArgumentException."); 221 } catch (IllegalArgumentException e) {} 222 assertTrue(instance.getParameters().get(StringSearchConstraint.PARAM_FUZZINESS) 223 == null); 224 225 instance.setMatchType(StringSearchConstraint.MATCH_TYPE_FUZZY); 227 instance.setParameter(StringSearchConstraint.PARAM_FUZZINESS, fuzziness); 228 assertTrue(instance.getParameters().get(StringSearchConstraint.PARAM_FUZZINESS). 229 equals(fuzziness)); 230 231 instance.setMatchType(StringSearchConstraint.MATCH_TYPE_LITERAL); 233 assertTrue(instance.getParameters().get(StringSearchConstraint.PARAM_FUZZINESS) 234 == null); 235 236 Integer proximityLimit = new Integer (2); 237 instance.setSearchType(StringSearchConstraint.SEARCH_TYPE_WORD_ORIENTED); 238 239 try { 241 instance.setParameter( 243 StringSearchConstraint.PARAM_PROXIMITY_LIMIT, proximityLimit); 244 fail("Invalid matchtype, should throw IllegalArgumentException."); 245 } catch (IllegalArgumentException e) {} 246 assertTrue(instance.getParameters().get(StringSearchConstraint.PARAM_PROXIMITY_LIMIT) 247 == null); 248 249 instance.setSearchType(StringSearchConstraint.SEARCH_TYPE_PROXIMITY_ORIENTED); 250 BasicStringSearchConstraint result 251 = instance.setParameter( 252 StringSearchConstraint.PARAM_PROXIMITY_LIMIT, proximityLimit); 253 assertTrue(instance.getParameters() 254 .get(StringSearchConstraint.PARAM_PROXIMITY_LIMIT) 255 .equals(proximityLimit)); 256 assertTrue(result == instance); 257 258 instance.setSearchType(StringSearchConstraint.SEARCH_TYPE_WORD_ORIENTED); 260 assertTrue(instance.getParameters() 261 .get(StringSearchConstraint.PARAM_PROXIMITY_LIMIT) 262 == null); 263 } 264 265 266 public void testGetMatchType() { 267 testSetMatchType(); 269 } 270 271 272 public void testGetSearchType() { 273 testSetSearchType(); 275 } 276 277 278 public void testAddSearchTerm() { 279 String newTerm = "skeukowkk"; 280 int nrTerms = instance.getSearchTerms().size(); 281 BasicStringSearchConstraint result = instance.addSearchTerm(newTerm); 282 List searchTerms = instance.getSearchTerms(); 283 assertTrue(searchTerms.size() == (nrTerms + 1)); 284 assertTrue(searchTerms.get(nrTerms).equals(newTerm)); 285 assertTrue(result == instance); 286 287 try { 288 instance.addSearchTerm(" "); 290 fail("White space searchterm, should throw IllegalArgumentException."); 291 } catch (IllegalArgumentException e) {} 292 try { 293 instance.addSearchTerm(""); 295 fail("White space searchterm, should throw IllegalArgumentException."); 296 } catch (IllegalArgumentException e) {} 297 } 298 299 300 public void testSetSearchTerms() { 301 List searchTerms = new ArrayList(); 302 searchTerms.add("kjeid"); 303 searchTerms.add("uerui"); 304 searchTerms.add("zcvvc"); 305 BasicStringSearchConstraint result 306 = instance.setSearchTerms(searchTerms); 307 assertTrue(instance.getSearchTerms().equals(searchTerms)); 308 assertTrue(result == instance); 309 310 try { 311 instance.setSearchTerms(new ArrayList()); 313 fail("Empty list of searchterms, should throw IllegalArgumentException."); 314 } catch (IllegalArgumentException e) {} 315 316 searchTerms.add(new Integer (0)); 317 try { 318 instance.setSearchTerms(new ArrayList()); 320 fail("Non-string searchterms, should throw IllegalArgumentException."); 321 } catch (IllegalArgumentException e) {} 322 } 323 324 325 public void testSetSearchTerms2() { 326 String searchTerm1 = "qwei"; 327 String searchTerm2 = "2838"; 328 String searchTerm3 = "i3wn"; 329 BasicStringSearchConstraint result 330 = instance.setSearchTerms("\n\t\r " + searchTerm1 331 + "\r" + searchTerm2 + " " + searchTerm3 + " \n "); 332 assertTrue(instance.getSearchTerms().size() == 3); 333 assertTrue(instance.getSearchTerms().get(0).equals(searchTerm1)); 334 assertTrue(instance.getSearchTerms().get(1).equals(searchTerm2)); 335 assertTrue(instance.getSearchTerms().get(2).equals(searchTerm3)); 336 assertTrue(result == instance); 337 338 try { 339 instance.setSearchTerms("\n\r\t "); 341 fail("Empty list of searchterms, should throw IllegalArgumentException."); 342 } catch (IllegalArgumentException e) {} 343 344 } 345 346 347 public void testGetSearchTerms() { 348 testSetSearchTerms(); 350 testSetSearchTerms2(); 351 } 352 353 354 public void testGetParameters() { 355 testSetParameter(); 357 358 Map map = instance.getParameters(); 359 try { 360 map.put("kdj", "iiup"); 362 fail("Trying to modify map, should throw UnsupportedOperationException."); 363 } catch (UnsupportedOperationException e) {} 364 } 365 366 367 public void testEquals() { 368 } 370 371 372 public void testHashCode() { 373 } 375 public static Test suite() { 376 TestSuite suite = new TestSuite(BasicStringSearchConstraintTest.class); 377 378 return suite; 379 } 380 381 } 382 | Popular Tags |