001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.openidex.search;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import junit.textui.TestRunner;
047: import org.netbeans.junit.NbTestCase;
048: import org.netbeans.junit.NbTestSuite;
049:
050: /**
051: *
052: * @author Martin Roskanin
053: */
054: public final class SearchHistoryTest extends NbTestCase {
055:
056: /**
057: */
058: public SearchHistoryTest(String name) {
059: super (name);
060: }
061:
062: /**
063: */
064: public static void main(String args[]) {
065: TestRunner.run(new NbTestSuite(SearchHistoryTest.class));
066: }
067:
068: /**
069: */
070: public void testSearchPatternSize() throws Exception {
071: assertSize("SearchPattern size", 80, SearchPattern.create(
072: "searchText", true, true, false));
073: }
074:
075: public void testSearchHistoryListSize() throws Exception {
076: for (int i = 0; i < 60; i++) {
077: SearchHistory.getDefault().add(
078: SearchPattern.create(String.valueOf(i), true, true,
079: false));
080: }
081: assertTrue(SearchHistory.getDefault().getSearchPatterns()
082: .size() == 50);
083: }
084:
085: public void testLastSelectedListener() throws Exception {
086: final boolean fired[] = new boolean[1];
087: fired[0] = false;
088: PropertyChangeListener pcl = new PropertyChangeListener() {
089: public void propertyChange(PropertyChangeEvent evt) {
090: if (evt != null
091: && SearchHistory.LAST_SELECTED.equals(evt
092: .getPropertyName())) {
093: fired[0] = true;
094: }
095: }
096: };
097: SearchHistory.getDefault().addPropertyChangeListener(pcl);
098: SearchHistory.getDefault().setLastSelected(
099: SearchPattern.create("searchtext", true, true, false));
100: SearchHistory.getDefault().removePropertyChangeListener(pcl);
101: assertTrue(fired[0]);
102: }
103:
104: public void testAddToSearchHistoryListener() throws Exception {
105: final boolean fired[] = new boolean[2];
106: PropertyChangeListener pcl = new PropertyChangeListener() {
107: public void propertyChange(PropertyChangeEvent evt) {
108: if (evt != null
109: && SearchHistory.ADD_TO_HISTORY.equals(evt
110: .getPropertyName())) {
111: fired[0] = true;
112: } else {
113: fired[1] = true;
114: }
115: }
116: };
117: SearchHistory.getDefault().addPropertyChangeListener(pcl);
118: SearchHistory.getDefault().add(
119: SearchPattern.create("searchtext", true, true, false));
120: SearchHistory.getDefault().removePropertyChangeListener(pcl);
121: assertTrue(fired[0]);
122: assertFalse("Only the expected change is fired", fired[1]);
123: }
124:
125: public void testAddIncorrectItemToSearchHistoryListener()
126: throws Exception {
127: final boolean fired[] = new boolean[2];
128:
129: PropertyChangeListener pcl = new PropertyChangeListener() {
130: public void propertyChange(PropertyChangeEvent evt) {
131: if (evt != null
132: && SearchHistory.ADD_TO_HISTORY.equals(evt
133: .getPropertyName())) {
134: fired[0] = true;
135: } else {
136: fired[1] = true;
137: }
138: }
139: };
140: SearchHistory.getDefault().addPropertyChangeListener(pcl);
141:
142: // add valid pattern
143: fired[0] = false;
144: fired[1] = false;
145: SearchHistory.getDefault().add(
146: SearchPattern.create("searchtext2", true, true, false));
147: assertTrue(fired[0]);
148: assertFalse("Only the expected change is fired", fired[1]);
149:
150: // add the same pattern, it shouldn't be added
151: fired[0] = false;
152: fired[1] = false;
153: SearchHistory.getDefault().add(
154: SearchPattern.create("searchtext2", true, true, false));
155: assertTrue(!fired[0]);
156: assertFalse("Only the expected change is fired", fired[1]);
157:
158: // add null pattern
159: fired[0] = false;
160: fired[1] = false;
161: SearchHistory.getDefault().add(null);
162: assertTrue(!fired[0]);
163: assertFalse("Only the expected change is fired", fired[1]);
164:
165: // add pattern with null searchExpression
166: fired[0] = false;
167: fired[1] = false;
168: SearchHistory.getDefault().add(
169: SearchPattern.create(null, true, true, false));
170: assertTrue(!fired[0]);
171: assertFalse("Only the expected change is fired", fired[1]);
172:
173: // add pattern with empty searchExpression
174: fired[0] = false;
175: fired[1] = false;
176: SearchHistory.getDefault().add(
177: SearchPattern.create("", true, true, false));
178: assertTrue(!fired[0]);
179: assertFalse("Only the expected change is fired", fired[1]);
180:
181: SearchHistory.getDefault().removePropertyChangeListener(pcl);
182: }
183:
184: public void testEquals() throws Exception {
185: SearchPattern pattern_one = SearchPattern.create("one", false,
186: false, false);
187: SearchPattern pattern_one_a = SearchPattern.create("one", true,
188: false, false);
189: SearchPattern pattern_one_b = SearchPattern.create("one",
190: false, true, false);
191: SearchPattern pattern_one_c = SearchPattern.create("one",
192: false, false, true);
193: SearchPattern pattern_one_d = SearchPattern.create("one", true,
194: true, false);
195: SearchPattern pattern_one_e = SearchPattern.create("one", true,
196: false, true);
197: SearchPattern pattern_one_f = SearchPattern.create("one", true,
198: true, true);
199:
200: SearchPattern pattern_two = SearchPattern.create("two", false,
201: false, false);
202: SearchPattern pattern_TwO = SearchPattern.create("TwO", false,
203: false, false);
204: SearchPattern pattern_TwO_a = SearchPattern.create("TwO",
205: false, true, false);
206:
207: SearchPattern pattern_one_test = SearchPattern.create("one",
208: false, false, false);
209: SearchPattern pattern_one_a_test = SearchPattern.create("one",
210: true, false, false);
211: SearchPattern pattern_one_b_test = SearchPattern.create("one",
212: false, true, false);
213: SearchPattern pattern_one_c_test = SearchPattern.create("one",
214: false, false, true);
215: SearchPattern pattern_one_d_test = SearchPattern.create("one",
216: true, true, false);
217: SearchPattern pattern_one_e_test = SearchPattern.create("one",
218: true, false, true);
219: SearchPattern pattern_one_f_test = SearchPattern.create("one",
220: true, true, true);
221:
222: SearchPattern pattern_two_test = SearchPattern.create("two",
223: false, false, false);
224: SearchPattern pattern_TwO_test = SearchPattern.create("TwO",
225: false, false, false);
226: SearchPattern pattern_TwO_a_test = SearchPattern.create("TwO",
227: false, true, false);
228:
229: assertTrue(pattern_one.equals(pattern_one_test));
230: assertTrue(pattern_one_test.equals(pattern_one));
231: assertTrue(pattern_one_a_test.equals(pattern_one_a));
232: assertTrue(pattern_one_b_test.equals(pattern_one_b));
233: assertTrue(pattern_one_c_test.equals(pattern_one_c));
234: assertTrue(pattern_one_d_test.equals(pattern_one_d));
235: assertTrue(pattern_one_e_test.equals(pattern_one_e));
236: assertTrue(pattern_one_f_test.equals(pattern_one_f));
237: //--------------------------------------------------
238: assertTrue(!pattern_one_a_test.equals(pattern_one_b));
239: assertTrue(!pattern_one_a_test.equals(pattern_one_c));
240: assertTrue(!pattern_one_b_test.equals(pattern_one_c));
241: assertTrue(!pattern_one_d_test.equals(pattern_one_e));
242:
243: assertTrue(pattern_two.equals(pattern_two_test));
244: assertTrue(pattern_TwO.equals(pattern_TwO_test));
245: assertTrue(pattern_TwO_a.equals(pattern_TwO_a_test));
246: assertTrue(!pattern_two.equals(pattern_TwO_test));
247: assertTrue(!pattern_TwO_a.equals(pattern_TwO_test));
248: }
249:
250: public void testHashCode() throws Exception {
251: int pattern_one = SearchPattern.create("one", false, false,
252: false).hashCode();
253: int pattern_one_a = SearchPattern.create("one", true, false,
254: false).hashCode();
255: int pattern_one_b = SearchPattern.create("one", false, true,
256: false).hashCode();
257: int pattern_one_c = SearchPattern.create("one", false, false,
258: true).hashCode();
259: int pattern_one_d = SearchPattern.create("one", true, true,
260: false).hashCode();
261: int pattern_one_e = SearchPattern.create("one", true, false,
262: true).hashCode();
263: int pattern_one_f = SearchPattern.create("one", true, true,
264: true).hashCode();
265:
266: int pattern_two = SearchPattern.create("two", false, false,
267: false).hashCode();
268: int pattern_TwO = SearchPattern.create("TwO", false, false,
269: false).hashCode();
270: int pattern_TwO_a = SearchPattern.create("TwO", false, true,
271: false).hashCode();
272:
273: int pattern_one_test = SearchPattern.create("one", false,
274: false, false).hashCode();
275: int pattern_one_a_test = SearchPattern.create("one", true,
276: false, false).hashCode();
277: int pattern_one_b_test = SearchPattern.create("one", false,
278: true, false).hashCode();
279: int pattern_one_c_test = SearchPattern.create("one", false,
280: false, true).hashCode();
281: int pattern_one_d_test = SearchPattern.create("one", true,
282: true, false).hashCode();
283: int pattern_one_e_test = SearchPattern.create("one", true,
284: false, true).hashCode();
285: int pattern_one_f_test = SearchPattern.create("one", true,
286: true, true).hashCode();
287:
288: int pattern_two_test = SearchPattern.create("two", false,
289: false, false).hashCode();
290: int pattern_TwO_test = SearchPattern.create("TwO", false,
291: false, false).hashCode();
292: int pattern_TwO_a_test = SearchPattern.create("TwO", false,
293: true, false).hashCode();
294:
295: assertTrue(pattern_one == pattern_one_test);
296: assertTrue(pattern_one_test == pattern_one);
297: assertTrue(pattern_one_a_test == pattern_one_a);
298: assertTrue(pattern_one_b_test == pattern_one_b);
299: assertTrue(pattern_one_c_test == pattern_one_c);
300: assertTrue(pattern_one_d_test == pattern_one_d);
301: assertTrue(pattern_one_e_test == pattern_one_e);
302: assertTrue(pattern_one_f_test == pattern_one_f);
303: //--------------------------------------------------
304: assertTrue(pattern_one_a_test != pattern_one_b);
305: assertTrue(pattern_one_a_test != pattern_one_c);
306: assertTrue(pattern_one_b_test != pattern_one_c);
307: assertTrue(pattern_one_d_test != pattern_one_e);
308:
309: assertTrue(pattern_two == pattern_two_test);
310: assertTrue(pattern_TwO == pattern_TwO_test);
311: assertTrue(pattern_TwO_a == pattern_TwO_a_test);
312: assertTrue(pattern_two != pattern_TwO_test);
313: assertTrue(pattern_TwO_a != pattern_TwO_test);
314: }
315:
316: }
|