1 19 20 package org.openide.filesystems; 21 22 23 import org.netbeans.junit.*; 24 import org.openide.util.Lookup; 25 26 import java.net.URL ; 27 import java.io.File ; 28 29 34 public class URLMapper50852Test extends NbTestCase { 35 private static URL testURL = null; 36 private static final MyThread resultsComputingThread = new MyThread(); 37 private static final MyThread secondThread = new MyThread(); 38 static MyURLMapper MAPPER_INSTANCE = null; 39 40 41 public URLMapper50852Test(String name) { 42 super(name); 43 } 44 45 public static void main(String [] args) throws Exception { 46 junit.textui.TestRunner.run(new NbTestSuite(URLMapper50852Test.class)); 47 } 48 49 52 protected void setUp() throws Exception { 53 File workdir = getWorkDir(); 54 testURL = workdir.toURI().toURL(); 55 System.setProperty("org.openide.util.Lookup", "org.openide.filesystems.URLMapper50852Test$Lkp"); 56 MAPPER_INSTANCE = new MyURLMapper (); 57 Lkp lkp = (Lkp)org.openide.util.Lookup.getDefault(); 58 lkp.getInstanceContent().add(MAPPER_INSTANCE); 59 } 60 61 public void testURLMapper50852 () throws Exception { 62 resultsComputingThread.start(); 63 Thread.sleep(1000); 64 secondThread.start(); 65 Thread.sleep(1000); 66 67 for (int i = 0; i < 5; i++) { 68 if (!resultsComputingThread.isFinished() && secondThread.isFinished() ) { 69 break; 70 } 71 Thread.sleep(1000); 72 } 73 assertFalse (resultsComputingThread.isFinished()); 74 assertTrue ("Even if a thread is blocked in the computation, another one can proceed", secondThread.isFinished()); 75 assertTrue ("and successfully call into the mapper", MAPPER_INSTANCE.called); 76 synchronized (testURL) { 77 testURL.notifyAll(); 78 } 79 80 } 81 82 83 public static final class Lkp extends org.openide.util.lookup.AbstractLookup { 84 private org.openide.util.lookup.InstanceContent ic; 85 public Lkp() { 86 this(new org.openide.util.lookup.InstanceContent()); 87 } 88 89 private Lkp(org.openide.util.lookup.InstanceContent ic) { 90 super(ic); 91 this.ic = ic; 92 } 93 94 org.openide.util.lookup.InstanceContent getInstanceContent () { 95 return ic; 96 } 97 98 protected void beforeLookup(Template template) { 99 super.beforeLookup(template); 100 101 synchronized (testURL) { 102 if (Thread.currentThread() == resultsComputingThread) { 103 try { 104 testURL.wait(); 105 } catch (InterruptedException e) { 106 fail (); 107 } 108 } 109 } 110 } 111 112 } 114 public static final class MyURLMapper extends URLMapper { 115 private boolean called = false; 116 117 118 public URL getURL(FileObject fo, int type) { 119 called = true; 120 return null; 121 } 122 123 public FileObject[] getFileObjects(URL url) { 124 called = true; 125 return new FileObject[0]; 126 } 127 128 public String findMIMEType(FileObject fo) { 129 called = true; 130 return null; 131 } 132 133 boolean isCalled() { 134 return called; 135 } 136 } 137 138 private static class MyThread extends Thread { 139 private boolean finished = false; 140 141 public void run() { 142 super.run(); 143 URLMapper.findFileObject(testURL); 144 finished = true; 145 } 146 147 boolean isFinished() { 148 return finished; 149 } 150 } 151 } 152 153 154 | Popular Tags |