KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > hints > LazyHintComputationTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.hints;
20
21 import java.lang.ref.Reference JavaDoc;
22 import java.lang.ref.WeakReference JavaDoc;
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 /**
30  *
31  * @author Jan Lahoda
32  */

33 public class LazyHintComputationTest extends NbTestCase {
34     
35     /** Creates a new instance of LazyHintComputationTest */
36     public LazyHintComputationTest(String JavaDoc name) {
37         super(name);
38     }
39     
40     private FileObject data;
41     
42     @Override JavaDoc
43     public void setUp() throws Exception JavaDoc {
44         FileSystem fs = FileUtil.createMemoryFileSystem();
45         data = fs.getRoot().createData("test.java");
46     }
47     
48     public void testCancel() throws Exception JavaDoc {
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 JavaDoc() {
60             public void run() {
61                 firstCancelled[0] = true;
62             }
63         }));
64         
65         LazyHintComputationFactory.addToCompute(data, new CreatorBasedLazyFixListImpl(second, new Runnable JavaDoc() {
66             public void run() {
67                 if (doCancel[0]) {
68                     c.cancel();
69                     callback[0] = true;
70                 }
71             }
72         }, new Runnable JavaDoc() {
73             public void run() {
74                 secondCancelled[0] = true;
75             }
76         }));
77         
78         LazyHintComputationFactory.addToCompute(data, new CreatorBasedLazyFixListImpl(third, null, new Runnable JavaDoc() {
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 JavaDoc {
110         boolean[] computed = new boolean[1];
111         
112         CreatorBasedLazyFixListImpl l = new CreatorBasedLazyFixListImpl(data, computed, null, null);
113         
114         l.getFixes();
115         
116         Reference JavaDoc r = new WeakReference JavaDoc(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 JavaDoc callback;
127         private final Runnable JavaDoc cancelCallback;
128         
129         public CreatorBasedLazyFixListImpl(FileObject file, boolean[] marker, Runnable JavaDoc callback, Runnable JavaDoc 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 JavaDoc callback, Runnable JavaDoc cancelCallback) {
137             this(null, marker, callback, cancelCallback);
138         }
139         
140         @Override JavaDoc
141         public void compute(CompilationInfo info) {
142             marker[0] = true;
143             
144             if (callback != null)
145                 callback.run();
146         }
147         
148         @Override JavaDoc
149         public void cancel() {
150             if (cancelCallback != null) {
151                 cancelCallback.run();
152             }
153         }
154     }
155     
156 }
157
Popular Tags