1 19 20 package org.openidex.search; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.PrintStream ; 25 import java.util.ArrayList ; 26 import java.util.Collections ; 27 import java.util.Iterator ; 28 import java.util.List ; 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 42 public final class SearchIteratorTest extends NbTestCase { 43 44 45 private FileObject dataDir; 46 47 FileObject projectRoot; 48 49 51 public SearchIteratorTest(String name) { 52 super(name); 53 } 54 55 57 public static void main(String args[]) { 58 TestRunner.run(new NbTestSuite(SearchIteratorTest.class)); 59 } 60 61 63 protected void setUp() throws Exception { 64 dataDir = FileUtil.toFileObject(getDataDir()); 65 assert dataDir != null; 66 67 projectRoot = dataDir.getFileObject("projects/Project1"); assert projectRoot != null; 69 70 FileObject testDir; 71 72 testDir = projectRoot; 73 ensureTildeCopyExists(testDir, "build", "xml"); 75 testDir = projectRoot.getFileObject("src/foo/bar/baz"); ensureTildeCopyExists(testDir, "SampleClass", "java"); } 78 79 81 private void ensureTildeCopyExists(FileObject folder, 82 String name, 83 String ext) throws IOException { 84 String 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 96 public void testPlainSearchInfo() throws Exception { 97 generateSearchableFileNames(projectRoot, 98 true, false, false, getRef()); 102 compareReferenceFiles(); 103 } 104 105 107 public void testVisibilitySearchInfo() throws Exception { 108 generateSearchableFileNames(projectRoot, 109 true, true, false, getRef()); 113 compareReferenceFiles(); 114 } 115 116 118 public void testSharabilitySearchInfo() throws Exception { 119 generateSearchableFileNames(projectRoot, 120 true, false, true, getRef()); 124 compareReferenceFiles(); 125 } 126 127 129 public void testVisibSharSearchInfo() throws Exception { 130 generateSearchableFileNames(projectRoot, 131 true, true, true, getRef()); 135 compareReferenceFiles(); 136 } 137 138 public void testNonRecursiveSearchInfo() throws Exception { 139 generateSearchableFileNames(projectRoot, 140 false, false, 142 false, 143 getRef()); 144 compareReferenceFiles(); 145 } 146 147 149 private void generateSearchableFileNames( 150 FileObject folder, 151 boolean recursive, 152 boolean checkVisibility, 153 boolean checkSharability, 154 PrintStream 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 foundFilesPaths = new ArrayList (16); 188 for (Iterator i = searchInfo.objectsToSearch(); i.hasNext(); ) { 189 FileObject primaryFile = ((DataObject) i.next()).getPrimaryFile(); 190 String relativePath = FileUtil.getRelativePath(projectRoot, 191 primaryFile); 192 foundFilesPaths.add(relativePath); 193 } 194 195 Collections.sort(foundFilesPaths); 196 197 for (Iterator i = foundFilesPaths.iterator(); i.hasNext(); ) { 198 refPrintStream.println((String ) i.next()); 199 } 200 } 201 202 } 203 | Popular Tags |