1 19 package org.netbeans.modules.java.hints; 20 21 import java.lang.ref.Reference ; 22 import java.lang.ref.WeakReference ; 23 import org.netbeans.api.java.source.CompilationInfo; 24 import org.netbeans.junit.NbTestCase; 25 import org.openide.filesystems.FileObject; 26 import org.openide.filesystems.FileSystem; 27 import org.openide.filesystems.FileUtil; 28 29 33 public class LazyHintComputationTest extends NbTestCase { 34 35 36 public LazyHintComputationTest(String name) { 37 super(name); 38 } 39 40 private FileObject data; 41 42 @Override 43 public void setUp() throws Exception { 44 FileSystem fs = FileUtil.createMemoryFileSystem(); 45 data = fs.getRoot().createData("test.java"); 46 } 47 48 public void testCancel() throws Exception { 49 final LazyHintComputation c = new LazyHintComputation(data); 50 boolean[] first = new boolean[1]; 51 boolean[] second = new boolean[1]; 52 boolean[] third = new boolean[1]; 53 final boolean[] callback = new boolean[1]; 54 final boolean[] doCancel = new boolean[] {true}; 55 final boolean[] firstCancelled = new boolean[1]; 56 final boolean[] secondCancelled = new boolean[1]; 57 final boolean[] thirdCancelled = new boolean[1]; 58 59 LazyHintComputationFactory.addToCompute(data, new CreatorBasedLazyFixListImpl(first, null, new Runnable () { 60 public void run() { 61 firstCancelled[0] = true; 62 } 63 })); 64 65 LazyHintComputationFactory.addToCompute(data, new CreatorBasedLazyFixListImpl(second, new Runnable () { 66 public void run() { 67 if (doCancel[0]) { 68 c.cancel(); 69 callback[0] = true; 70 } 71 } 72 }, new Runnable () { 73 public void run() { 74 secondCancelled[0] = true; 75 } 76 })); 77 78 LazyHintComputationFactory.addToCompute(data, new CreatorBasedLazyFixListImpl(third, null, new Runnable () { 79 public void run() { 80 thirdCancelled[0] = true; 81 } 82 })); 83 84 c.run(null); 85 86 assertTrue(first[0]); 87 assertTrue(second[0]); 88 assertFalse(third[0]); 89 assertTrue(callback[0]); 90 assertFalse(firstCancelled[0]); 91 assertTrue(secondCancelled[0]); 92 assertFalse(thirdCancelled[0]); 93 94 first[0] = second[0] = callback[0] = secondCancelled[0] = false; 95 96 doCancel[0] = false; 97 98 c.run(null); 99 100 assertFalse(first[0]); 101 assertTrue(second[0]); 102 assertTrue(third[0]); 103 assertFalse(callback[0]); 104 assertFalse(firstCancelled[0]); 105 assertFalse(secondCancelled[0]); 106 assertFalse(thirdCancelled[0]); 107 } 108 109 public void test88996() throws Exception { 110 boolean[] computed = new boolean[1]; 111 112 CreatorBasedLazyFixListImpl l = new CreatorBasedLazyFixListImpl(data, computed, null, null); 113 114 l.getFixes(); 115 116 Reference r = new WeakReference (l); 117 118 l = null; 119 120 assertGC("Not holding the CreatorBasedLazyFixList hard", r); 121 } 122 123 private static final class CreatorBasedLazyFixListImpl extends CreatorBasedLazyFixList { 124 125 private final boolean[] marker; 126 private final Runnable callback; 127 private final Runnable cancelCallback; 128 129 public CreatorBasedLazyFixListImpl(FileObject file, boolean[] marker, Runnable callback, Runnable cancelCallback) { 130 super(file, null, -1, null, null); 131 this.marker = marker; 132 this.callback = callback; 133 this.cancelCallback = cancelCallback; 134 } 135 136 public CreatorBasedLazyFixListImpl(boolean[] marker, Runnable callback, Runnable cancelCallback) { 137 this(null, marker, callback, cancelCallback); 138 } 139 140 @Override 141 public void compute(CompilationInfo info) { 142 marker[0] = true; 143 144 if (callback != null) 145 callback.run(); 146 } 147 148 @Override 149 public void cancel() { 150 if (cancelCallback != null) { 151 cancelCallback.run(); 152 } 153 } 154 } 155 156 } 157 | Popular Tags |