1 19 20 package org.netbeans.api.project; 21 22 import java.beans.PropertyVetoException ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.net.URL ; 28 import java.util.Map ; 29 import java.util.StringTokenizer ; 30 import java.util.WeakHashMap ; 31 import junit.framework.Assert; 32 import org.netbeans.junit.NbTestCase; 33 import org.netbeans.spi.project.ProjectFactory; 34 import org.netbeans.spi.project.ProjectState; 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.FileUtil; 37 import org.openide.filesystems.LocalFileSystem; 38 import org.openide.filesystems.Repository; 39 import org.openide.filesystems.URLMapper; 40 import org.openide.util.Lookup; 41 import org.openide.util.lookup.Lookups; 42 import org.openide.util.lookup.ProxyLookup; 43 44 48 public final class TestUtil extends ProxyLookup { 49 50 static { 51 TestUtil.class.getClassLoader().setDefaultAssertionStatus(true); 52 System.setProperty("org.openide.util.Lookup", TestUtil.class.getName()); 53 Assert.assertEquals(TestUtil.class, Lookup.getDefault().getClass()); 54 } 55 56 private static TestUtil DEFAULT; 57 58 public TestUtil() { 59 Assert.assertNull(DEFAULT); 60 DEFAULT = this; 61 setLookup(new Object [0]); 62 } 63 64 69 public static void setLookup(Lookup l) { 70 DEFAULT.setLookups(new Lookup[] {l}); 71 } 72 73 76 public static void setLookup(Object [] instances) { 77 ClassLoader l = TestUtil.class.getClassLoader(); 78 DEFAULT.setLookups(new Lookup[] { 79 Lookups.fixed(instances), 80 Lookups.metaInfServices(l), 81 Lookups.singleton(l), 82 }); 83 } 84 85 private static boolean warned = false; 86 91 public static FileObject makeScratchDir(NbTestCase test) throws IOException { 92 test.clearWorkDir(); 93 File root = test.getWorkDir(); 94 assert root.isDirectory() && root.list().length == 0; 95 FileObject fo = FileUtil.toFileObject(root); 96 if (fo != null) { 97 return fo; 98 } else { 99 if (!warned) { 100 warned = true; 101 System.err.println("No FileObject for " + root + " found.\n" + 102 "Maybe you need ${openide/masterfs.dir}/modules/org-netbeans-modules-masterfs.jar\n" + 103 "in test.unit.run.cp.extra, or make sure Lookups.metaInfServices is included in Lookup.default, so that\n" + 104 "Lookup.default<URLMapper>=" + Lookup.getDefault().lookupAll(URLMapper.class) + " includes MasterURLMapper\n" + 105 "e.g. by using TestUtil.setLookup(Object[]) rather than TestUtil.setLookup(Lookup)."); 106 } 107 LocalFileSystem lfs = new LocalFileSystem(); 109 try { 110 lfs.setRootDirectory(root); 111 } catch (PropertyVetoException e) { 112 assert false : e; 113 } 114 Repository.getDefault().addFileSystem(lfs); 115 return lfs.getRoot(); 116 } 117 } 118 119 122 public static void deleteRec(File f) throws IOException { 123 if (f.isDirectory()) { 124 File [] kids = f.listFiles(); 125 if (kids == null) { 126 throw new IOException ("List " + f); 127 } 128 for (File kid : kids) { 129 deleteRec(kid); 130 } 131 } 132 if (!f.delete()) { 133 throw new IOException ("Delete " + f); 134 } 135 } 136 137 143 public static ProjectFactory testProjectFactory() { 144 return new TestProjectFactory(); 145 } 146 147 150 public static void gc() { 151 System.gc(); 152 System.runFinalization(); 153 System.gc(); 154 } 155 156 private static final Map <FileObject,Integer > loadCount = new WeakHashMap <FileObject,Integer >(); 157 158 162 public static int projectLoadCount(FileObject dir) { 163 Integer i = loadCount.get(dir); 164 if (i != null) { 165 return i; 166 } else { 167 return 0; 168 } 169 } 170 171 178 public static void setProjectSaveWillFail(Project p, Throwable error) { 179 ((TestProject)p).error = error; 180 } 181 182 187 public static int projectSaveCount(Project p) { 188 return ((TestProject)p).saveCount; 189 } 190 191 195 public static void modify(Project p) { 196 ((TestProject)p).state.markModified(); 197 } 198 199 203 public static void notifyDeleted(Project p) { 204 ((TestProject)p).state.notifyDeleted(); 205 } 206 207 212 public static Object BROKEN_PROJECT_LOAD_LOCK = null; 213 214 private static final class TestProjectFactory implements ProjectFactory { 215 216 TestProjectFactory() {} 217 218 public Project loadProject(FileObject projectDirectory, ProjectState state) throws IOException { 219 Integer i = loadCount.get(projectDirectory); 220 if (i == null) { 221 i = 1; 222 } else { 223 i = i + 1; 224 } 225 loadCount.put(projectDirectory, i); 226 FileObject testproject = projectDirectory.getFileObject("testproject"); 227 if (testproject != null && testproject.isFolder()) { 228 if (testproject.getFileObject("broken") != null) { 229 if (BROKEN_PROJECT_LOAD_LOCK != null) { 230 synchronized (BROKEN_PROJECT_LOAD_LOCK) { 231 try { 232 BROKEN_PROJECT_LOAD_LOCK.wait(); 233 } catch (InterruptedException e) { 234 assert false : e; 235 } 236 } 237 } 238 throw new IOException ("Load failed of " + projectDirectory); 239 } else { 240 return new TestProject(projectDirectory, state); 241 } 242 } else { 243 return null; 244 } 245 } 246 247 public void saveProject(Project project) throws IOException , ClassCastException { 248 TestProject p = (TestProject)project; 249 Throwable t = p.error; 250 if (t != null) { 251 p.error = null; 252 if (t instanceof IOException ) { 253 throw (IOException )t; 254 } else if (t instanceof Error ) { 255 throw (Error )t; 256 } else { 257 throw (RuntimeException )t; 258 } 259 } 260 p.saveCount++; 261 } 262 263 public boolean isProject(FileObject dir) { 264 FileObject testproject = dir.getFileObject("testproject"); 265 return testproject != null && testproject.isFolder(); 266 } 267 268 } 269 270 private static final class TestProject implements Project { 271 272 private final FileObject dir; 273 final ProjectState state; 274 Throwable error; 275 int saveCount = 0; 276 277 public TestProject(FileObject dir, ProjectState state) { 278 this.dir = dir; 279 this.state = state; 280 } 281 282 public Lookup getLookup() { 283 return Lookup.EMPTY; 284 } 285 286 public FileObject getProjectDirectory() { 287 return dir; 288 } 289 290 public String toString() { 291 return "testproject:" + getProjectDirectory().getNameExt(); 292 } 293 294 310 311 } 312 313 321 public static FileObject createFileFromContent(URL content, FileObject parent, String path) throws IOException { 322 if (parent == null) { 323 throw new IllegalArgumentException ("null parent"); 324 } 325 Assert.assertTrue("folder", parent.isFolder()); 326 FileObject fo = parent; 327 StringTokenizer tok = new StringTokenizer (path, "/"); 328 boolean touch = false; 329 while (tok.hasMoreTokens()) { 330 Assert.assertNotNull("fo is null (parent=" + parent + " path=" + path + ")", fo); 331 String name = tok.nextToken(); 332 if (tok.hasMoreTokens()) { 333 FileObject sub = fo.getFileObject(name); 334 if (sub == null) { 335 FileObject fo2 = fo.createFolder(name); 336 Assert.assertNotNull("createFolder(" + fo + ", " + name + ") -> null", fo2); 337 fo = fo2; 338 } else { 339 Assert.assertTrue("folder", sub.isFolder()); 340 fo = sub; 341 } 342 } else { 343 FileObject sub = fo.getFileObject(name); 344 if (sub == null) { 345 FileObject fo2 = fo.createData(name); 346 Assert.assertNotNull("createData(" + fo + ", " + name + ") -> null", fo2); 347 fo = fo2; 348 } else { 349 fo = sub; 350 touch = true; 351 } 352 } 353 } 354 assert fo.isData(); 355 if (content != null || touch) { 356 OutputStream os = fo.getOutputStream(); 357 try { 358 if (content != null) { 359 InputStream is = content.openStream(); 360 try { 361 FileUtil.copy(is, os); 362 } finally { 363 is.close(); 364 } 365 } 366 } finally { 367 os.close(); 368 } 369 } 370 return fo; 371 } 372 373 } 374 | Popular Tags |