1 19 20 package org.netbeans.modules.xml.xam.ui.search; 21 22 import junit.framework.TestCase; 23 24 29 public class WildcardStringMatcherTest extends TestCase { 30 31 public WildcardStringMatcherTest(String testName) { 32 super(testName); 33 } 34 35 38 public void testContainsWildcards() { 39 assertTrue(WildcardStringMatcher.containsWildcards("a*")); 40 assertTrue(WildcardStringMatcher.containsWildcards("a?")); 41 assertTrue(WildcardStringMatcher.containsWildcards("a*b")); 42 assertTrue(WildcardStringMatcher.containsWildcards("a?b")); 43 assertTrue(WildcardStringMatcher.containsWildcards("*b")); 44 assertTrue(WildcardStringMatcher.containsWildcards("?b")); 45 assertTrue(WildcardStringMatcher.containsWildcards("*")); 46 assertTrue(WildcardStringMatcher.containsWildcards("?")); 47 assertFalse(WildcardStringMatcher.containsWildcards("abc")); 48 } 49 50 53 public void testMatch() { 54 TestData[] data; 55 data = new TestData[] { 56 new TestData("foo", true), 57 new TestData("ffoo", false), 58 new TestData("fofoo", false), 59 new TestData("foobar", true), 60 new TestData("fofoobar", false), 61 new TestData("barfoo", false), 62 new TestData("barfoofo", false), 63 new TestData("barfoobaz", false), 64 new TestData("foofoofoo", true), 65 new TestData("fofofoo", false), 66 new TestData("", false), 67 }; 68 performTest(data, "foo*"); 69 70 data = new TestData[] { 71 new TestData("foo", true), 72 new TestData("foobar", false), 73 new TestData("barfoo", true), 74 new TestData("barfoobaz", false), 75 new TestData("foofoofoo", true), 76 new TestData("fofofoo", true), 77 new TestData("", false), 78 }; 79 performTest(data, "*foo"); 80 81 data = new TestData[] { 82 new TestData("foo", true), 83 new TestData("foobar", false), 84 new TestData("barfoo", true), 85 new TestData("barfoobaz", false), 86 new TestData("foofoofoo", true), 87 new TestData("fofofoo", true), 88 new TestData("", false), 89 }; 90 performTest(data, "**foo"); 91 92 data = new TestData[] { 93 new TestData("foo", false), 94 new TestData("foobar", false), 95 new TestData("barfoo", true), 96 new TestData("barfoobaz", true), 97 new TestData("foofoofoo", false), 98 new TestData("", false), 99 }; 100 performTest(data, "bar*"); 101 102 data = new TestData[] { 103 new TestData("foo", false), 104 new TestData("foobar", false), 105 new TestData("barfoo", true), 106 new TestData("barfoobaz", true), 107 new TestData("foofoofoo", false), 108 new TestData("", false), 109 }; 110 performTest(data, "bar**"); 111 112 data = new TestData[] { 113 new TestData("bar", false), 114 new TestData("bar1", true), 115 new TestData("barZ", true), 116 new TestData("1bar", false), 117 new TestData("foofoofoo", false), 118 new TestData("", false), 119 }; 120 performTest(data, "bar?"); 121 122 data = new TestData[] { 123 new TestData("foo", true), 124 new TestData("bar", true), 125 new TestData("baz", true), 126 new TestData("", true), 127 }; 128 performTest(data, "*"); 129 130 data = new TestData[] { 131 new TestData("foobarbaz", true), 132 new TestData("foobar", true), 133 new TestData("foo", true), 134 new TestData("fo", true), 135 new TestData("f", true), 136 new TestData("", true), 137 }; 138 performTest(data, "***"); 139 140 data = new TestData[] { 141 new TestData("foobarbaz", true), 142 new TestData("foo", true), 143 new TestData("ba", true), 144 new TestData("b", false), 145 new TestData("", false), 146 }; 147 performTest(data, "*?*?*"); 148 149 data = new TestData[] { 150 new TestData("f", true), 151 new TestData("b", true), 152 new TestData("foo", false), 153 new TestData("", false), 154 }; 155 performTest(data, "?"); 156 157 data = new TestData[] { 158 new TestData("a", true), 159 new TestData("1a", true), 160 new TestData("aaa", true), 161 new TestData("cba", true), 162 new TestData("abc", false), 163 new TestData("c", false), 164 new TestData("", false), 165 }; 166 performTest(data, "*a"); 167 168 data = new TestData[] { 169 new TestData("foo", true), 170 new TestData("bar", true), 171 new TestData("foobar", false), 172 new TestData("", false), 173 }; 174 performTest(data, "???"); 175 176 data = new TestData[] { 177 new TestData("fooabcbar1baz", true), 178 new TestData("foobarbarbar!baz", true), 179 new TestData("foobarXbaz", true), 180 new TestData("foobarbaz", false), 181 new TestData("bar", false), 182 new TestData("baz", false), 183 new TestData("", false), 184 }; 185 performTest(data, "foo*bar?baz"); 186 187 data = new TestData[] { 188 new TestData("fooabc123baz", true), 189 new TestData("foobarbarbar!baz", true), 190 new TestData("foobarXbaz", true), 191 new TestData("foobarbaz", true), 192 new TestData("foobaz", false), 193 new TestData("foobar", false), 194 new TestData("foo123bar", false), 195 new TestData("", false), 196 }; 197 performTest(data, "foo*???baz"); 198 199 data = new TestData[] { 200 new TestData("hereheroherr", true), 201 new TestData("herheroher", true), 202 new TestData("abcherodef", true), 203 new TestData("hero", true), 204 new TestData("heroherohero", true), 205 new TestData("herehero", true), 206 new TestData("heroherr", true), 207 new TestData("he1ro", false), 208 new TestData("", false), 209 }; 210 performTest(data, "*hero*"); 211 } 212 213 219 private void performTest(TestData[] data, String query) { 220 for (TestData datum : data) { 221 String text = datum.getText(); 222 boolean matches = WildcardStringMatcher.match(text, query); 223 if (matches != datum.match) { 224 if (matches) { 225 fail("Mistakenly found " + query + " in \"" + text + '"'); 226 } else { 227 fail("Did not find " + query + " in \"" + text + '"'); 228 } 229 } 230 } 231 } 232 233 236 private static class TestData { 237 private String text; 238 private boolean match; 239 240 public TestData(String text, boolean match) { 241 this.text = text; 242 this.match = match; 243 } 244 245 public String getText() { 246 return text; 247 } 248 249 public boolean shouldMatch() { 250 return match; 251 } 252 } 253 } 254 | Popular Tags |