KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openidex > search > SearchIteratorTest


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.openidex.search;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.PrintStream JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import junit.textui.TestRunner;
30 import org.netbeans.junit.NbTestCase;
31 import org.netbeans.junit.NbTestSuite;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.loaders.DataObject;
35 import org.openidex.search.SearchInfo;
36 import org.openidex.search.SearchInfoFactory;
37
38 /**
39  *
40  * @author Marian Petras
41  */

42 public final class SearchIteratorTest extends NbTestCase {
43
44     /** */
45     private FileObject dataDir;
46     /** */
47     FileObject projectRoot;
48
49     /**
50      */

51     public SearchIteratorTest(String JavaDoc name) {
52         super(name);
53     }
54
55     /**
56      */

57     public static void main(String JavaDoc args[]) {
58         TestRunner.run(new NbTestSuite(SearchIteratorTest.class));
59     }
60
61     /**
62      */

63     protected void setUp() throws Exception JavaDoc {
64         dataDir = FileUtil.toFileObject(getDataDir());
65         assert dataDir != null;
66         
67         projectRoot = dataDir.getFileObject("projects/Project1"); //NOI18N
68
assert projectRoot != null;
69         
70         FileObject testDir;
71
72         testDir = projectRoot;
73         ensureTildeCopyExists(testDir, "build", "xml"); //NOI18N
74

75         testDir = projectRoot.getFileObject("src/foo/bar/baz"); //NOI18N
76
ensureTildeCopyExists(testDir, "SampleClass", "java"); //NOI18N
77
}
78     
79     /**
80      */

81     private void ensureTildeCopyExists(FileObject folder,
82                                        String JavaDoc name,
83                                        String JavaDoc ext) throws IOException JavaDoc {
84         String JavaDoc tildeExt = ext + '~';
85         
86         FileObject orig = folder.getFileObject(name, ext);
87         assert orig != null;
88         FileObject copy = folder.getFileObject(name, tildeExt);
89         if (copy == null) {
90             orig.copy(folder, name, tildeExt);
91         }
92     }
93     
94     /**
95      */

96     public void testPlainSearchInfo() throws Exception JavaDoc {
97         generateSearchableFileNames(projectRoot,
98                                     true, //recursive
99
false, //check visibility?
100
false, //check sharability?
101
getRef());
102         compareReferenceFiles();
103     }
104     
105     /**
106      */

107     public void testVisibilitySearchInfo() throws Exception JavaDoc {
108         generateSearchableFileNames(projectRoot,
109                                     true, //recursive
110
true, //check visibility?
111
false, //check sharability?
112
getRef());
113         compareReferenceFiles();
114     }
115     
116     /**
117      */

118     public void testSharabilitySearchInfo() throws Exception JavaDoc {
119         generateSearchableFileNames(projectRoot,
120                                     true, //recursive
121
false, //check visibility?
122
true, //check sharability?
123
getRef());
124         compareReferenceFiles();
125     }
126     
127     /**
128      */

129     public void testVisibSharSearchInfo() throws Exception JavaDoc {
130         generateSearchableFileNames(projectRoot,
131                                     true, //recursive
132
true, //check visibility?
133
true, //check sharability?
134
getRef());
135         compareReferenceFiles();
136     }
137     
138     public void testNonRecursiveSearchInfo() throws Exception JavaDoc {
139         generateSearchableFileNames(projectRoot,
140                                     false, //not recursive
141
false,
142                                     false,
143                                     getRef());
144         compareReferenceFiles();
145     }
146     
147     /**
148      */

149     private void generateSearchableFileNames(
150             FileObject folder,
151             boolean recursive,
152             boolean checkVisibility,
153             boolean checkSharability,
154             PrintStream JavaDoc refPrintStream) {
155                 
156         FileObjectFilter[] filters;
157
158         int filtersCount = 0;
159         if (checkVisibility) {
160             filtersCount++;
161         }
162         if (checkSharability) {
163             filtersCount++;
164         }
165
166         if (filtersCount == 0) {
167             filters = null;
168         } else {
169             filters = new FileObjectFilter[filtersCount];
170
171             int i = 0;
172             if (checkVisibility) {
173                 filters[i++] = SearchInfoFactory.VISIBILITY_FILTER;
174             }
175             if (checkSharability) {
176                 filters[i++] = SearchInfoFactory.SHARABILITY_FILTER;
177             }
178         }
179
180         SearchInfo searchInfo = SearchInfoFactory.createSearchInfo(
181                 folder,
182                 recursive,
183                 filters);
184         
185         assertTrue("project root not searchable", searchInfo.canSearch());
186         
187         List JavaDoc foundFilesPaths = new ArrayList JavaDoc(16);
188         for (Iterator JavaDoc i = searchInfo.objectsToSearch(); i.hasNext(); ) {
189             FileObject primaryFile = ((DataObject) i.next()).getPrimaryFile();
190             String JavaDoc relativePath = FileUtil.getRelativePath(projectRoot,
191                                                            primaryFile);
192             foundFilesPaths.add(relativePath);
193         }
194         
195         Collections.sort(foundFilesPaths);
196         
197         for (Iterator JavaDoc i = foundFilesPaths.iterator(); i.hasNext(); ) {
198             refPrintStream.println((String JavaDoc) i.next());
199         }
200     }
201
202 }
203
Popular Tags