KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > support > LookupBasedJavaSourceTaskFactoryTest


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.api.java.source.support;
20
21 import javax.swing.event.ChangeEvent JavaDoc;
22 import javax.swing.event.ChangeListener JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import org.netbeans.api.java.source.CancellableTask;
26 import org.netbeans.api.java.source.CompilationInfo;
27 import org.netbeans.api.java.source.JavaSource;
28 import org.netbeans.api.java.source.JavaSource.Phase;
29 import org.netbeans.api.java.source.SourceUtilsTestUtil;
30 import org.netbeans.junit.NbTestCase;
31 import org.openide.filesystems.FileObject;
32 import org.openide.loaders.DataObject;
33 import org.openide.nodes.Node;
34 import org.openide.util.Lookup;
35 import org.openide.util.lookup.Lookups;
36 import org.openide.util.lookup.ProxyLookup;
37
38 /**
39  *
40  * @author Jan Lahoda
41  */

42 public class LookupBasedJavaSourceTaskFactoryTest extends NbTestCase {
43     
44     public LookupBasedJavaSourceTaskFactoryTest(String JavaDoc testName) {
45         super(testName);
46     }
47
48     private FileObject testDir;
49     private FileObject testFile1;
50     private FileObject testFile2;
51     private DataObject testFile1DO;
52     private DataObject testFile2DO;
53     private Node testFile1Node;
54     private Node testFile2Node;
55     
56     protected void setUp() throws Exception JavaDoc {
57         testDir = SourceUtilsTestUtil.makeScratchDir(this);
58         testFile1 = testDir.createData("test1.txt");
59         testFile2 = testDir.createData("test2.txt");
60         testFile1DO = DataObject.find(testFile1);
61         testFile2DO = DataObject.find(testFile2);
62         testFile1Node = testFile1DO.getNodeDelegate();
63         testFile2Node = testFile2DO.getNodeDelegate();
64     }
65     
66     public void testFactoryListensOnLookupChanges() throws Exception JavaDoc {
67         int[] changeCount = new int[1];
68         LookupBasedJavaSourceTaskFactory factory = new LookupBasedJavaSourceTaskFactoryImpl(changeCount);
69         ChangeableLookup lookup = new ChangeableLookup();
70         
71         factory.setLookup(lookup);
72         
73         assertEquals(1, changeCount[0]);
74         assertEquals(0, factory.getFileObjects().size());
75         
76         lookup.setLookupsImpl(new Lookup[] {Lookups.singleton(testFile1)});
77         
78         assertEquals(2, changeCount[0]);
79         assertEquals(1, factory.getFileObjects().size());
80         assertEquals(testFile1, factory.getFileObjects().get(0));
81         
82         lookup.setLookupsImpl(new Lookup[] {Lookups.singleton(testFile2)});
83         
84         assertEquals(3, changeCount[0]);
85         assertEquals(1, factory.getFileObjects().size());
86         assertEquals(testFile2, factory.getFileObjects().get(0));
87         
88         lookup.setLookupsImpl(new Lookup[] {});
89         
90         assertEquals(4, changeCount[0]);
91         assertEquals(0, factory.getFileObjects().size());
92         
93         lookup.setLookupsImpl(new Lookup[] {Lookups.fixed(new Object JavaDoc[] {testFile1, testFile2})});
94         
95         assertEquals(5, changeCount[0]);
96         assertEquals(2, factory.getFileObjects().size());
97         assertEquals(new HashSet JavaDoc(Arrays.asList(testFile1, testFile2)), new HashSet JavaDoc(factory.getFileObjects()));
98         
99         lookup.setLookupsImpl(new Lookup[] {});
100         
101         assertEquals(6, changeCount[0]);
102         assertEquals(0, factory.getFileObjects().size());
103         
104         lookup.setLookupsImpl(new Lookup[] {Lookups.singleton(testFile1DO)});
105         
106         assertEquals(7, changeCount[0]);
107         assertEquals(1, factory.getFileObjects().size());
108         assertEquals(testFile1, factory.getFileObjects().get(0));
109         
110         lookup.setLookupsImpl(new Lookup[] {Lookups.fixed(new Object JavaDoc[] {testFile1DO, testFile1Node})});
111         
112         assertEquals(8, changeCount[0]);
113         assertEquals(1, factory.getFileObjects().size());
114         assertEquals(testFile1, factory.getFileObjects().get(0));
115         
116         lookup.setLookupsImpl(new Lookup[] {Lookups.singleton(testFile2Node)});
117         
118         assertEquals(10, changeCount[0]);
119         assertEquals(1, factory.getFileObjects().size());
120         assertEquals(testFile2, factory.getFileObjects().get(0));
121     }
122
123     private static class LookupBasedJavaSourceTaskFactoryImpl extends LookupBasedJavaSourceTaskFactory {
124         
125         private int[] changeCount;
126         
127         public LookupBasedJavaSourceTaskFactoryImpl(int[] changeCount) {
128             super(null, null);
129             this.changeCount = changeCount;
130         }
131         
132         public CancellableTask<CompilationInfo> createTask(FileObject file) {
133             throw new IllegalStateException JavaDoc();
134         }
135         
136         @Override JavaDoc
137         protected void lookupContentChanged() {
138             changeCount[0]++;
139         }
140         
141     }
142     
143     private static class ChangeableLookup extends ProxyLookup {
144         
145         public void setLookupsImpl(Lookup[] lookups) {
146             setLookups(lookups);
147         }
148     }
149 }
150
Popular Tags