001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.java.source.tasklist;
043:
044: import java.io.File;
045: import java.util.Arrays;
046: import java.util.Collection;
047: import java.util.HashMap;
048: import java.util.Iterator;
049: import java.util.List;
050: import java.util.Locale;
051: import java.util.Map;
052: import javax.tools.Diagnostic;
053: import javax.tools.Diagnostic.Kind;
054: import org.netbeans.api.java.source.SourceUtilsTestUtil;
055: import org.netbeans.junit.NbTestCase;
056: import org.netbeans.modules.tasklist.trampoline.Accessor;
057: import org.netbeans.modules.tasklist.trampoline.TaskManager;
058: import org.netbeans.spi.tasklist.FileTaskScanner;
059: import org.netbeans.spi.tasklist.PushTaskScanner;
060: import org.netbeans.spi.tasklist.Task;
061: import org.netbeans.spi.tasklist.TaskScanningScope;
062: import org.openide.filesystems.FileObject;
063: import org.openide.filesystems.FileUtil;
064: import org.openide.util.Lookup;
065: import org.openide.util.lookup.InstanceContent;
066: import org.openide.util.lookup.Lookups;
067: import org.openide.util.lookup.ProxyLookup;
068:
069: /**
070: *
071: * @author Jan Lahoda
072: */
073: public class JavaTaskProviderTest extends NbTestCase {
074:
075: public JavaTaskProviderTest(String testName) {
076: super (testName);
077: }
078:
079: private FileObject src;
080: private FileObject file1;
081: private FileObject file2;
082: private FileObject file3;
083:
084: protected void setUp() throws Exception {
085: SourceUtilsTestUtil.prepareTest(new String[0], new Object[0]);
086:
087: super .setUp();
088:
089: File f = getWorkDir();
090:
091: clearWorkDir();
092:
093: FileObject workDir = FileUtil.toFileObject(f);
094:
095: assertNotNull(f);
096:
097: src = workDir.createFolder("src");
098:
099: FileObject cache = workDir.createFolder("cache");
100: FileObject build = workDir.createFolder("build");
101: FileObject pack = src.createFolder("pack");
102:
103: file1 = pack.createData("file1.java");
104: file2 = pack.createData("file2.java");
105: file3 = pack.createData("file3.java");
106:
107: SourceUtilsTestUtil.prepareTest(src, build, cache);
108: }
109:
110: public void testUpdates() throws Exception {
111: JavaTaskProvider jtp = new JavaTaskProvider();
112: TaskManagerImpl tm = new TaskManagerImpl();
113: TaskScanningScopeImpl scope1 = new TaskScanningScopeImpl(file1,
114: file2);
115:
116: TaskCache.getDefault().dumpErrors(src.getURL(), file1.getURL(),
117: FileUtil.toFile(file1),
118: Arrays.asList(new DiagnosticImpl(Kind.ERROR, "x", 3)));
119:
120: jtp.setScope(scope1, Accessor.DEFAULT.createCallback(tm, jtp));
121: jtp.waitWorkFinished();
122:
123: assertTasks(file1, Task.create(file1, "nb-tasklist-error", "x",
124: 3));
125: assertTasks(file2);
126:
127: TaskCache.getDefault().dumpErrors(src.getURL(), file2.getURL(),
128: FileUtil.toFile(file2),
129: Arrays.asList(new DiagnosticImpl(Kind.ERROR, "y", 4)));
130: JavaTaskProvider.refresh(file2);
131: jtp.waitWorkFinished();
132:
133: assertTasks(file1, Task.create(file1, "nb-tasklist-error", "x",
134: 3));
135: assertTasks(file2, Task.create(file2, "nb-tasklist-error", "y",
136: 4));
137: }
138:
139: private void assertTasks(FileObject file, Task... tasks) {
140: Collection<? extends Task> inTasklist = tasklist.get(file);
141:
142: if (inTasklist == null || inTasklist.isEmpty()) {
143: assertTrue(Arrays.asList(tasks).toString(),
144: tasks.length == 0);
145: }
146:
147: assertEquals(tasks.length, inTasklist.size());
148:
149: int index = 0;
150:
151: for (Task t : inTasklist) {
152: assertEquals(tasks[index++], t);
153: }
154: }
155:
156: private Map<FileObject, Collection<? extends Task>> tasklist = new HashMap<FileObject, Collection<? extends Task>>();
157:
158: private class TaskManagerImpl extends TaskManager {
159:
160: public void refresh(FileTaskScanner scanner,
161: FileObject... files) {
162: throw new UnsupportedOperationException(
163: "Not supported yet.");
164: }
165:
166: public void refresh(FileTaskScanner scanner) {
167: throw new UnsupportedOperationException(
168: "Not supported yet.");
169: }
170:
171: public void refresh(TaskScanningScope scope) {
172: throw new UnsupportedOperationException(
173: "Not supported yet.");
174: }
175:
176: public void started(PushTaskScanner scanner) {
177: }
178:
179: public void finished(PushTaskScanner scanner) {
180: }
181:
182: public void setTasks(PushTaskScanner scanner,
183: FileObject resource, List<? extends Task> tasks) {
184: tasklist.put(resource, tasks);
185: }
186:
187: public void clearAllTasks(PushTaskScanner scanner) {
188: tasklist.clear();
189: }
190:
191: }
192:
193: private static class TaskScanningScopeImpl extends
194: TaskScanningScope {
195:
196: private List<FileObject> files;
197: private ChangeableLookup l;
198:
199: public TaskScanningScopeImpl(FileObject... files) {
200: super ("", "", null, false);
201: this .files = Arrays.asList(files);
202: this .l = new ChangeableLookup();
203: setFiles(files);
204: }
205:
206: public boolean isInScope(FileObject resource) {
207: return files.contains(resource);
208: }
209:
210: public void attach(Callback callback) {
211: }
212:
213: public Lookup getLookup() {
214: return l;
215: }
216:
217: public Iterator<FileObject> iterator() {
218: return files.iterator();
219: }
220:
221: public void setFiles(FileObject... files) {
222: l.setLookupsImpl(Lookups.fixed((Object[]) files));
223: }
224:
225: }
226:
227: private static class DiagnosticImpl implements Diagnostic {
228:
229: private Kind k;
230: private String message;
231: private int line;
232:
233: public DiagnosticImpl(Kind k, String message, int line) {
234: this .k = k;
235: this .message = message;
236: this .line = line;
237: }
238:
239: public Kind getKind() {
240: return k;
241: }
242:
243: public Object getSource() {
244: throw new UnsupportedOperationException(
245: "Not supported yet.");
246: }
247:
248: public long getPosition() {
249: throw new UnsupportedOperationException(
250: "Not supported yet.");
251: }
252:
253: public long getStartPosition() {
254: throw new UnsupportedOperationException(
255: "Not supported yet.");
256: }
257:
258: public long getEndPosition() {
259: throw new UnsupportedOperationException(
260: "Not supported yet.");
261: }
262:
263: public long getLineNumber() {
264: return line;
265: }
266:
267: public long getColumnNumber() {
268: throw new UnsupportedOperationException(
269: "Not supported yet.");
270: }
271:
272: public String getCode() {
273: throw new UnsupportedOperationException(
274: "Not supported yet.");
275: }
276:
277: public String getMessage(Locale locale) {
278: return message;
279: }
280:
281: }
282:
283: private static class ChangeableLookup extends ProxyLookup {
284:
285: public void setLookupsImpl(Lookup... lookups) {
286: super.setLookups(lookups);
287: }
288:
289: }
290:
291: }
|