KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > queries > BinaryForSourceQueryTest


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.java.queries;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27 import javax.swing.event.ChangeListener JavaDoc;
28 import org.netbeans.api.java.classpath.ClassPath;
29 import org.netbeans.api.java.queries.SourceForBinaryQuery.Result;
30 import org.netbeans.junit.MockServices;
31 import org.netbeans.junit.NbTestCase;
32 import org.netbeans.spi.java.classpath.ClassPathProvider;
33 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
34 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.FileUtil;
37 import org.openide.filesystems.URLMapper;
38
39 /**
40  * @author Tomas Zezula
41  */

42 public class BinaryForSourceQueryTest extends NbTestCase {
43     
44     private FileObject srcRoot1;
45     private FileObject srcRoot2;
46     private FileObject binaryRoot2;
47     
48
49     public BinaryForSourceQueryTest (String JavaDoc n) {
50         super(n);
51     }
52     
53     @Override JavaDoc
54     protected void setUp () throws IOException JavaDoc {
55         MockServices.setServices(new Class JavaDoc[] {CPProvider.class, SFBQImpl.class});
56         this.clearWorkDir();
57         File JavaDoc wd = this.getWorkDir();
58         FileObject root = FileUtil.toFileObject(wd);
59         assertNotNull(root);
60         srcRoot1 = root.createFolder("src1");
61         assertNotNull(srcRoot1);
62         srcRoot2 = root.createFolder("src2");
63         assertNotNull(srcRoot2);
64         binaryRoot2 = root.createFolder("binary2");
65         assertNotNull(binaryRoot2);
66         SFBQImpl.clear();
67         CPProvider.clear();
68         SFBQImpl.register(srcRoot2.getURL(), binaryRoot2.getURL());
69         CPProvider.register(srcRoot2, ClassPath.SOURCE, ClassPathSupport.createClassPath(new FileObject[] {srcRoot2}));
70         CPProvider.register(srcRoot2, ClassPath.EXECUTE, ClassPathSupport.createClassPath(new FileObject[] {binaryRoot2}));
71     }
72     
73     public void testQuery() throws Exception JavaDoc {
74         BinaryForSourceQuery.Result result = BinaryForSourceQuery.findBinaryRoots(srcRoot1.getURL());
75         assertEquals(0,result.getRoots().length);
76         result = BinaryForSourceQuery.findBinaryRoots(srcRoot2.getURL());
77         assertEquals(1,result.getRoots().length);
78         assertEquals(binaryRoot2.getURL(), result.getRoots()[0]);
79     }
80     
81     public static class SFBQImpl implements SourceForBinaryQueryImplementation {
82                 
83         private static final Map JavaDoc<URL JavaDoc,URL JavaDoc> data = new HashMap JavaDoc<URL JavaDoc,URL JavaDoc>();
84         
85         static void clear () {
86             data.clear();
87         }
88         
89         static void register (URL JavaDoc source, URL JavaDoc binary) {
90             data.put (binary,source);
91         }
92             
93         public Result findSourceRoots(URL JavaDoc binaryRoot) {
94             URL JavaDoc src = data.get (binaryRoot);
95             if (src == null) {
96                 return null;
97             }
98             final FileObject fo = URLMapper.findFileObject(src);
99             if (fo == null) {
100                 return null;
101             }
102             return new SourceForBinaryQuery.Result () {
103                 public FileObject[] getRoots() {
104                     return new FileObject[] {fo};
105                 }
106                 public void addChangeListener (ChangeListener JavaDoc l) {}
107                 
108                 public void removeChangeListener (ChangeListener JavaDoc l) {}
109             };
110         }
111     }
112     
113     public static class CPProvider implements ClassPathProvider {
114         
115         
116         private static final Map JavaDoc<FileObject,Map JavaDoc<String JavaDoc,ClassPath>> data = new HashMap JavaDoc<FileObject,Map JavaDoc<String JavaDoc,ClassPath>>();
117         
118         static void clear () {
119             data.clear();
120         }
121         
122         static void register (FileObject fo, String JavaDoc type, ClassPath cp) {
123             Map JavaDoc<String JavaDoc,ClassPath> m = data.get (fo);
124             if (m == null) {
125                 m = new HashMap JavaDoc<String JavaDoc,ClassPath>();
126                 data.put (fo,m);
127             }
128             m.put (type,cp);
129         }
130             
131         public ClassPath findClassPath(FileObject file, String JavaDoc type) {
132             Map JavaDoc<String JavaDoc,ClassPath> m = data.get (file);
133             if (m == null) {
134                 return null;
135             }
136             return m.get (type);
137         }
138     }
139     
140     
141 }
142
Popular Tags