KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > queries > UnitTestForSourceQueryImpl


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
20 package org.netbeans.modules.apisupport.project.queries;
21
22 import java.io.File JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import org.netbeans.api.project.FileOwnerQuery;
26 import org.netbeans.api.project.Project;
27 import org.netbeans.modules.apisupport.project.NbModuleProject;
28 import org.netbeans.spi.java.queries.MultipleRootsUnitTestForSourceQueryImplementation;
29 import org.netbeans.spi.project.support.ant.AntProjectHelper;
30 import org.openide.filesystems.FileObject;
31
32 public class UnitTestForSourceQueryImpl implements MultipleRootsUnitTestForSourceQueryImplementation {
33
34     private NbModuleProject project;
35
36     public UnitTestForSourceQueryImpl(NbModuleProject project) {
37         this.project = project;
38     }
39
40     public URL JavaDoc[] findUnitTests(FileObject source) {
41         return find(source, "src.dir", "test.unit.src.dir"); // NOI18N
42
}
43     
44     public URL JavaDoc[] findSources(FileObject unitTest) {
45         return find(unitTest, "test.unit.src.dir", "src.dir"); // NOI18N
46
}
47     
48     private URL JavaDoc[] find(FileObject file, String JavaDoc from, String JavaDoc to) {
49         Project p = FileOwnerQuery.getOwner(file);
50         if (p == null) {
51             return null;
52         }
53         AntProjectHelper helper = project.getHelper();
54         String JavaDoc val = project.evaluator().getProperty(from);
55         assert val != null : "No value for " + from + " in " + project;
56         FileObject fromRoot = helper.resolveFileObject(val);
57         if (!file.equals(fromRoot)) {
58             return null;
59         }
60         val = project.evaluator().getProperty(to);
61         assert val != null : "No value for " + to + " in " + project;
62         String JavaDoc path = helper.resolvePath(val);
63         try {
64             File JavaDoc f = helper.resolveFile(path);
65             if (!f.exists()) {
66                 return null;
67             }
68             else {
69                 return new URL JavaDoc[] {f.toURI().normalize().toURL()};
70             }
71         } catch (MalformedURLException JavaDoc e) {
72             throw new AssertionError JavaDoc(e);
73         }
74     }
75     
76 }
77
Popular Tags