1 19 20 package org.netbeans.modules.turbo; 21 22 import junit.framework.TestCase; 23 import org.openide.util.Lookup; 24 import org.openide.util.lookup.InstanceContent; 25 import org.openide.util.lookup.AbstractLookup; 26 import org.openide.filesystems.FileObject; 27 import org.openide.filesystems.LocalFileSystem; 28 import org.openide.filesystems.FileSystem; 29 30 import java.io.File ; 31 32 37 public class TurboTest extends TestCase { 38 39 private static InstanceContent content = new InstanceContent(); 40 41 private FileSystem fs; 42 43 private static TestEnvironment env; 44 45 private int tearDownCounter = countTestCases(); 46 47 static { 49 env = new TestEnvironment(); 50 Turbo.initEnvironment(env); 51 } 52 53 protected void setUp() throws Exception { 55 56 58 LocalFileSystem fs = new LocalFileSystem(); 59 File tmp = new File (System.getProperty("java.io.tmpdir") + File.separator + "turbo-test"); 60 tmp.mkdir(); 61 tmp.deleteOnExit(); 62 File theFile = new File (tmp, "theFile"); 63 theFile.createNewFile(); 64 theFile.deleteOnExit(); 65 fs.setRootDirectory(tmp); 66 67 System.setProperty("netbeans.experimental.vcsTurboStatistics", "performance"); 68 this.fs = fs; 69 } 70 71 protected void tearDown() throws Exception { 72 if (tearDownCounter-- == 1) { 73 try { 74 Turbo.getDefault().finalize(); 75 } catch (Throwable t) { 76 t.printStackTrace(); 77 } 78 } 79 } 80 81 82 87 public void testHitAfterSet() throws Exception { 88 Turbo faq = Turbo.getDefault(); 89 90 FileObject fo = fs.getRoot().getFileObject("theFile"); 91 AttributeListener l = new AttributeListener(); 92 faq.addTurboListener(l); 93 94 faq.writeEntry(fo, "test", "testValue"); 95 assertTrue("Missing Turbo event", l.hit()); 96 97 faq.readEntry(fo, "test"); 99 100 l.reset(); 102 faq.writeEntry(fo, "test", "testValue"); 103 assertFalse("Missing Turbo event", l.hit()); 104 } 105 106 109 public void testInvalidation() throws Exception { 110 Turbo faq = Turbo.getDefault(); 111 112 FileObject fo = fs.getRoot().getFileObject("theFile"); 113 114 faq.writeEntry(fo, "invalidate", "testValue"); 115 faq.writeEntry(fo, "invalidate", null); 116 assertFalse(faq.isPrepared(fo, "invalidate")); 117 118 faq.readEntry(fo, "invalidate"); 120 assertTrue(faq.isPrepared(fo, "invalidate")); 121 } 122 123 private static class TestEnvironment extends Turbo.Environment { 124 125 private Lookup l; 126 127 public TestEnvironment() { 128 content.add(new AssertingTurboProvider()); 129 l = new AbstractLookup(content); 130 } 131 132 public Lookup getLookup() { 133 return l; 134 } 135 } 136 137 private static class AssertingTurboProvider implements TurboProvider { 138 139 public boolean recognizesAttribute(String name) { 140 return "test".equals(name); 141 } 142 143 public boolean recognizesEntity(Object key) { 144 return true; 145 } 146 147 public Object readEntry(Object key, String name, MemoryCache memoryCache) { 148 fail(); 149 return null; 150 } 151 152 public boolean writeEntry(Object key, String name, Object value) { 153 return true; 154 } 155 } 156 157 private class AttributeListener implements TurboListener { 158 159 private volatile boolean hit = false; 160 161 public void reset() { 162 hit = false; 163 } 164 165 public boolean hit() { 166 return hit; 167 } 168 169 public synchronized void entryChanged(Object key, String name, Object value) { 170 hit = true; 171 notifyAll(); 172 } 173 174 public synchronized void waitForHit(int timeout) { 175 long start = System.currentTimeMillis(); 176 while (hit == false) { 177 try { 178 wait(timeout); 179 if (System.currentTimeMillis() - start > timeout) return; 180 } catch (InterruptedException e) { 181 return; 182 } 183 } 184 } 185 } 186 187 188 191 public void testSlowProvider() throws Exception { 192 Turbo faq = Turbo.getDefault(); 193 194 FileObject fo = fs.getRoot().getFileObject("theFile"); 195 196 SlowProvider sp = new SlowProvider(); 197 content.add(sp); 198 try { 199 faq.prepareEntry(fo, "slow"); 201 203 AttributeListener expecting = new AttributeListener(); 204 faq.addTurboListener(expecting); 205 faq.writeEntry(fo, "slow", "result"); faq.removeTurboListener(expecting); 207 assertTrue(expecting.hit()); 208 209 211 AttributeListener besilent = new AttributeListener(); 212 faq.addTurboListener(besilent); 213 sp.notifyReady(); besilent.waitForHit(100); 215 faq.removeTurboListener(besilent); 216 217 } finally { 220 content.remove(sp); 221 } 222 } 223 224 private class SlowProvider implements TurboProvider { 225 226 volatile boolean ready; 227 228 public boolean recognizesAttribute(String name) { 229 return "slow".equals(name); 230 } 231 232 public boolean recognizesEntity(Object key) { 233 return true; 234 } 235 236 public synchronized Object readEntry(Object key, String name, MemoryCache memoryCache) { 237 while (ready == false) { 238 try { 239 wait(); 240 } catch (InterruptedException e) { 241 break; 242 } 243 } 244 return "result"; 245 } 246 247 public boolean writeEntry(Object key, String name, Object value) { 248 return true; 250 } 251 252 public synchronized void notifyReady() { 253 ready = true; 254 notifyAll(); 255 } 256 } 257 258 259 263 public void testPrepare() throws Exception { 264 Turbo faq = Turbo.getDefault(); 265 FileObject fo = fs.getRoot().getFileObject("theFile"); 266 PrepareProvider pp = new PrepareProvider(); 267 content.add(pp); 268 try { 269 270 AttributeListener l = new AttributeListener(); 271 faq.addTurboListener(l); 272 faq.prepareEntry(fo, "prepare"); 273 l.waitForHit(500); 274 faq.removeTurboListener(l); 275 276 assertTrue(l.hit); 277 278 280 l.reset(); 281 faq.addTurboListener(l); 282 faq.prepareEntry(fo, "prepare2"); 283 l.waitForHit(500); 284 faq.removeTurboListener(l); 285 286 assertTrue(l.hit); 287 } finally { 288 content.remove(pp); 289 } 290 } 291 292 private class PrepareProvider implements TurboProvider { 293 294 public boolean recognizesAttribute(String name) { 295 return "prepare".equals(name) || "prepare2".equals(name); 296 } 297 298 public boolean recognizesEntity(Object key) { 299 return true; 300 } 301 302 public Object readEntry(Object key, String name, MemoryCache memoryCache) { 303 return "done"; 304 } 305 306 public boolean writeEntry(Object key, String name, Object value) { 307 return true; 308 } 309 } 310 311 315 public void testWrongProvider() { 316 WrongProvider provider = new WrongProvider(); 317 318 Lookup.Template t = new Lookup.Template(TurboProvider.class); 319 Lookup.Result r = env.getLookup().lookup(t); 320 content.add(provider); 321 assert r.allInstances().size() == 2 : "r=" + r.allInstances(); 322 323 Turbo faq = Turbo.getDefault(); 324 325 FileObject fo = fs.getRoot().getFileObject("theFile"); 326 327 try { 328 assertNull(faq.readEntry(fo, "wrong")); 329 } catch (RuntimeException ex) { 330 fail("WrongProvider crashed framework"); 331 } 332 333 assertTrue("Provider registration was missed", provider.queried); 334 335 content.remove(provider); 336 } 337 338 private class WrongProvider implements TurboProvider { 339 340 volatile boolean queried; 341 342 public boolean recognizesAttribute(String name) { 343 queried = true; 344 return "wrong".equals(name); 345 } 347 348 public boolean recognizesEntity(Object key) { 349 return true; 350 } 351 352 public Object readEntry(Object key, String name, MemoryCache memoryCache) { 353 throw new RuntimeException (); 354 } 355 356 public boolean writeEntry(Object key, String name, Object value) { 357 throw new RuntimeException (); 358 } 359 } 360 361 362 } 363 | Popular Tags |