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.api.java.queries;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.net.URL;
047: import java.util.HashMap;
048: import java.util.Map;
049: import javax.swing.event.ChangeListener;
050: import org.netbeans.api.java.classpath.ClassPath;
051: import org.netbeans.api.java.queries.SourceForBinaryQuery.Result;
052: import org.netbeans.junit.MockServices;
053: import org.netbeans.junit.NbTestCase;
054: import org.netbeans.spi.java.classpath.ClassPathProvider;
055: import org.netbeans.spi.java.classpath.support.ClassPathSupport;
056: import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
057: import org.openide.filesystems.FileObject;
058: import org.openide.filesystems.FileUtil;
059: import org.openide.filesystems.URLMapper;
060:
061: /**
062: * @author Tomas Zezula
063: */
064: public class BinaryForSourceQueryTest extends NbTestCase {
065:
066: private FileObject srcRoot1;
067: private FileObject srcRoot2;
068: private FileObject binaryRoot2;
069:
070: public BinaryForSourceQueryTest(String n) {
071: super (n);
072: }
073:
074: @Override
075: protected void setUp() throws IOException {
076: MockServices.setServices(new Class[] { CPProvider.class,
077: SFBQImpl.class });
078: this .clearWorkDir();
079: File wd = this .getWorkDir();
080: FileObject root = FileUtil.toFileObject(wd);
081: assertNotNull(root);
082: srcRoot1 = root.createFolder("src1");
083: assertNotNull(srcRoot1);
084: srcRoot2 = root.createFolder("src2");
085: assertNotNull(srcRoot2);
086: binaryRoot2 = root.createFolder("binary2");
087: assertNotNull(binaryRoot2);
088: SFBQImpl.clear();
089: CPProvider.clear();
090: SFBQImpl.register(srcRoot2.getURL(), binaryRoot2.getURL());
091: CPProvider
092: .register(srcRoot2, ClassPath.SOURCE, ClassPathSupport
093: .createClassPath(new FileObject[] { srcRoot2 }));
094: CPProvider
095: .register(
096: srcRoot2,
097: ClassPath.EXECUTE,
098: ClassPathSupport
099: .createClassPath(new FileObject[] { binaryRoot2 }));
100: }
101:
102: public void testQuery() throws Exception {
103: BinaryForSourceQuery.Result result = BinaryForSourceQuery
104: .findBinaryRoots(srcRoot1.getURL());
105: assertEquals(0, result.getRoots().length);
106: result = BinaryForSourceQuery
107: .findBinaryRoots(srcRoot2.getURL());
108: assertEquals(1, result.getRoots().length);
109: assertEquals(binaryRoot2.getURL(), result.getRoots()[0]);
110: }
111:
112: public static class SFBQImpl implements
113: SourceForBinaryQueryImplementation {
114:
115: private static final Map<URL, URL> data = new HashMap<URL, URL>();
116:
117: static void clear() {
118: data.clear();
119: }
120:
121: static void register(URL source, URL binary) {
122: data.put(binary, source);
123: }
124:
125: public Result findSourceRoots(URL binaryRoot) {
126: URL src = data.get(binaryRoot);
127: if (src == null) {
128: return null;
129: }
130: final FileObject fo = URLMapper.findFileObject(src);
131: if (fo == null) {
132: return null;
133: }
134: return new SourceForBinaryQuery.Result() {
135: public FileObject[] getRoots() {
136: return new FileObject[] { fo };
137: }
138:
139: public void addChangeListener(ChangeListener l) {
140: }
141:
142: public void removeChangeListener(ChangeListener l) {
143: }
144: };
145: }
146: }
147:
148: public static class CPProvider implements ClassPathProvider {
149:
150: private static final Map<FileObject, Map<String, ClassPath>> data = new HashMap<FileObject, Map<String, ClassPath>>();
151:
152: static void clear() {
153: data.clear();
154: }
155:
156: static void register(FileObject fo, String type, ClassPath cp) {
157: Map<String, ClassPath> m = data.get(fo);
158: if (m == null) {
159: m = new HashMap<String, ClassPath>();
160: data.put(fo, m);
161: }
162: m.put(type, cp);
163: }
164:
165: public ClassPath findClassPath(FileObject file, String type) {
166: Map<String, ClassPath> m = data.get(file);
167: if (m == null) {
168: return null;
169: }
170: return m.get(type);
171: }
172: }
173:
174: }
|