1 19 package org.netbeans.api.java.source.support; 20 21 import javax.swing.event.ChangeEvent ; 22 import javax.swing.event.ChangeListener ; 23 import java.util.Arrays ; 24 import java.util.HashSet ; 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 42 public class LookupBasedJavaSourceTaskFactoryTest extends NbTestCase { 43 44 public LookupBasedJavaSourceTaskFactoryTest(String 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 { 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 { 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 [] {testFile1, testFile2})}); 94 95 assertEquals(5, changeCount[0]); 96 assertEquals(2, factory.getFileObjects().size()); 97 assertEquals(new HashSet (Arrays.asList(testFile1, testFile2)), new HashSet (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 [] {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 (); 134 } 135 136 @Override 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 |