KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > ui > search > WildcardStringMatcherTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xam.ui.search;
21
22 import junit.framework.TestCase;
23
24 /**
25  * Unit test for WildcardStringMatcher class.
26  *
27  * @author Nathan Fiedler
28  */

29 public class WildcardStringMatcherTest extends TestCase {
30     
31     public WildcardStringMatcherTest(String JavaDoc testName) {
32         super(testName);
33     }
34
35     /**
36      * Test of containsWildcards method, of class match.WildcardStringMatcher.
37      */

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     /**
51      * Test of match method, of class match.WildcardStringMatcher.
52      */

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     /**
214      * Perform tests on the given data.
215      *
216      * @param data set of test data (text and expected result).
217      * @param query the wildcard query string.
218      */

219     private void performTest(TestData[] data, String JavaDoc query) {
220         for (TestData datum : data) {
221             String JavaDoc 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     /**
234      * Holds the test data for testing the string matcher.
235      */

236     private static class TestData {
237         private String JavaDoc text;
238         private boolean match;
239
240         public TestData(String JavaDoc text, boolean match) {
241             this.text = text;
242             this.match = match;
243         }
244
245         public String JavaDoc getText() {
246             return text;
247         }
248
249         public boolean shouldMatch() {
250             return match;
251         }
252     }
253 }
254
Popular Tags