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-2007 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.io.IOException;
045: import java.util.Iterator;
046: import org.netbeans.junit.MockServices;
047: import org.netbeans.junit.NbTestCase;
048: import org.openide.filesystems.FileObject;
049: import org.openide.filesystems.FileSystem;
050: import org.openide.filesystems.FileUtil;
051: import org.openide.loaders.DataFolder;
052:
053: /**
054: *
055: * @author Marian Petras
056: */
057: public class CompoundSearchInfoTest extends NbTestCase {
058:
059: public CompoundSearchInfoTest(String testName) {
060: super (testName);
061: }
062:
063: public void setUp() {
064: MockServices.setServices(new Class[] { DummyDataLoader.class });
065: }
066:
067: public void testNullArgument() {
068: try {
069: new CompoundSearchInfo(null);
070: fail("constructor of CompoundSearchInfo should throw"
071: + " an IllegalArgumentException when null is passed");
072: } catch (IllegalArgumentException ex) {
073: //correct
074: } catch (Exception ex) {
075: fail("constructor of CompoundSearchInfo should throw"
076: + " an IllegalArgumentException when null is passed"
077: + " - different type of exception was thrown: "
078: + ex.getClass().getName());
079: }
080: }
081:
082: public void testEmptyList() {
083: SearchInfo searchInfo = new CompoundSearchInfo(
084: new SearchInfo[0]);
085: assertFalse(searchInfo.canSearch());
086: assertFalse(searchInfo.objectsToSearch().hasNext());
087: }
088:
089: public void testOneItemList() throws IOException {
090: FileSystem fs = FileUtil.createMemoryFileSystem();
091: FileObject fsRoot = fs.getRoot();
092:
093: FileObject dir = fsRoot.createFolder("dir");
094: dir.createData("a", DummyDataLoader.dummyExt);
095: dir.createData("b", DummyDataLoader.dummyExt);
096: dir.createData("c", DummyDataLoader.dummyExt);
097: DataFolder folder = DataFolder.findFolder(dir);
098:
099: SearchInfo refSearchInfo;
100: SearchInfo testSearchInfo;
101: boolean refCanSearch;
102: boolean testCanSearch;
103: Iterator refIt;
104: Iterator testIt;
105: int testIterationsCount;
106:
107: refSearchInfo = new SimpleSearchInfo(folder, false, null);
108: testSearchInfo = new CompoundSearchInfo(
109: new SearchInfo[] { refSearchInfo });
110: assertTrue(testSearchInfo.canSearch());
111:
112: refIt = refSearchInfo.objectsToSearch();
113: testIt = testSearchInfo.objectsToSearch();
114: for (testIterationsCount = 0;; testIterationsCount++) {
115: boolean refHasNext = refIt.hasNext();
116: boolean testHasNext = testIt.hasNext();
117: assertEquals(refHasNext, testHasNext);
118:
119: if (!refHasNext) {
120: break;
121: }
122:
123: Object refObj = refIt.next();
124: Object testObj = testIt.next();
125: assertSame(refObj, testObj);
126: }
127: assertEquals(3, testIterationsCount);
128:
129: refSearchInfo = new SimpleSearchInfo(folder, false, null) {
130: public boolean canSearch() {
131: return false;
132: }
133: };
134: testSearchInfo = new CompoundSearchInfo(
135: new SearchInfo[] { refSearchInfo });
136: refCanSearch = refSearchInfo.canSearch();
137: testCanSearch = testSearchInfo.canSearch();
138: assertEquals(refCanSearch, testCanSearch);
139: }
140:
141: public void testMultipleItemsList() throws IOException {
142: FileSystem fs = FileUtil.createMemoryFileSystem();
143: FileObject fsRoot = fs.getRoot();
144:
145: FileObject dir1 = fsRoot.createFolder("dir1");
146: dir1.createData("1a", DummyDataLoader.dummyExt);
147: dir1.createData("1b", DummyDataLoader.dummyExt);
148: dir1.createData("1c", DummyDataLoader.dummyExt);
149: DataFolder folder1 = DataFolder.findFolder(dir1);
150:
151: FileObject dir2 = fsRoot.createFolder("dir2");
152: dir2.createData("2a", DummyDataLoader.dummyExt);
153: dir2.createData("2b", DummyDataLoader.dummyExt);
154: DataFolder folder2 = DataFolder.findFolder(dir2);
155:
156: SearchInfo refSearchInfo1, refSearchInfo2;
157: SearchInfo testSearchInfo;
158: Iterator refIt;
159: Iterator testIt;
160: int testIterationsCount;
161:
162: refSearchInfo1 = new SimpleSearchInfo(folder1, false, null);
163: refSearchInfo2 = new SimpleSearchInfo(folder2, false, null);
164: testSearchInfo = new CompoundSearchInfo(new SearchInfo[] {
165: refSearchInfo1, refSearchInfo2 });
166: assertTrue(testSearchInfo.canSearch());
167:
168: testIterationsCount = 0;
169: testIt = testSearchInfo.objectsToSearch();
170: refIt = refSearchInfo1.objectsToSearch();
171: while (refIt.hasNext()) {
172: assertTrue(testIt.hasNext());
173: assertSame(refIt.next(), testIt.next());
174: testIterationsCount++;
175: }
176: assertTrue(testIt.hasNext());
177: refIt = refSearchInfo2.objectsToSearch();
178: while (refIt.hasNext()) {
179: assertTrue(testIt.hasNext());
180: assertSame(refIt.next(), testIt.next());
181: testIterationsCount++;
182: }
183: assertFalse(testIt.hasNext());
184: assertEquals(5, testIterationsCount);
185:
186: refSearchInfo1 = new SimpleSearchInfo(folder1, false, null);
187: refSearchInfo2 = new SimpleSearchInfo(folder2, false, null) {
188: public boolean canSearch() {
189: return false;
190: }
191: };
192: testSearchInfo = new CompoundSearchInfo(new SearchInfo[] {
193: refSearchInfo1, refSearchInfo2 });
194: assertTrue(testSearchInfo.canSearch());
195:
196: testIterationsCount = 0;
197: testIt = testSearchInfo.objectsToSearch();
198: refIt = refSearchInfo1.objectsToSearch();
199: while (refIt.hasNext()) {
200: assertTrue(testIt.hasNext());
201: assertSame(refIt.next(), testIt.next());
202: testIterationsCount++;
203: }
204: assertFalse(testIt.hasNext());
205: assertEquals(3, testIterationsCount);
206:
207: refSearchInfo1 = new SimpleSearchInfo(folder1, false, null) {
208: public boolean canSearch() {
209: return false;
210: }
211: };
212: refSearchInfo2 = new SimpleSearchInfo(folder2, false, null);
213: testSearchInfo = new CompoundSearchInfo(new SearchInfo[] {
214: refSearchInfo1, refSearchInfo2 });
215: assertTrue(testSearchInfo.canSearch());
216:
217: testIterationsCount = 0;
218: testIt = testSearchInfo.objectsToSearch();
219: refIt = refSearchInfo2.objectsToSearch();
220: while (refIt.hasNext()) {
221: assertTrue(testIt.hasNext());
222: assertSame(refIt.next(), testIt.next());
223: testIterationsCount++;
224: }
225: assertFalse(testIt.hasNext());
226: assertEquals(2, testIterationsCount);
227:
228: refSearchInfo1 = new SimpleSearchInfo(folder1, false, null) {
229: public boolean canSearch() {
230: return false;
231: }
232: };
233: refSearchInfo2 = new SimpleSearchInfo(folder2, false, null) {
234: public boolean canSearch() {
235: return false;
236: }
237: };
238: testSearchInfo = new CompoundSearchInfo(new SearchInfo[] {
239: refSearchInfo1, refSearchInfo2 });
240: assertFalse(testSearchInfo.canSearch());
241: }
242:
243: }
|