KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > freeform > TestQuery


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.java.freeform;
21
22 import java.io.File JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.List JavaDoc;
28 import org.netbeans.modules.ant.freeform.spi.support.Util;
29 import org.netbeans.spi.java.queries.MultipleRootsUnitTestForSourceQueryImplementation;
30 import org.netbeans.spi.project.AuxiliaryConfiguration;
31 import org.netbeans.spi.project.support.ant.AntProjectHelper;
32 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileStateInvalidException;
35 import org.openide.filesystems.FileUtil;
36 import org.w3c.dom.Element JavaDoc;
37
38 /**
39  * Reports location of unit tests.
40  * Rather than associating each test root to each source root, the project may
41  * have any number of source and test roots, and each source root is associated
42  * with all test roots, and each test root is associated with all source roots.
43  * This is not as precise as it could be but in practice it is unlikely to matter.
44  * Also all package roots within one compilation unit are treated interchangeably.
45  * @see "#47835"
46  * @author Jesse Glick
47  */

48 final class TestQuery implements MultipleRootsUnitTestForSourceQueryImplementation {
49     
50     private final AntProjectHelper helper;
51     private final PropertyEvaluator eval;
52     private final AuxiliaryConfiguration aux;
53     
54     public TestQuery(AntProjectHelper helper, PropertyEvaluator eval, AuxiliaryConfiguration aux) {
55         this.helper = helper;
56         this.eval = eval;
57         this.aux = aux;
58     }
59
60     public URL JavaDoc[] findUnitTests(FileObject source) {
61         URL JavaDoc[][] data = findSourcesAndTests();
62         URL JavaDoc sourceURL;
63         try {
64             sourceURL = source.getURL();
65         } catch (FileStateInvalidException e) {
66             return null;
67         }
68         if (Arrays.asList(data[0]).contains(sourceURL)) {
69             return data[1];
70         } else {
71             return null;
72         }
73     }
74
75     public URL JavaDoc[] findSources(FileObject unitTest) {
76         URL JavaDoc[][] data = findSourcesAndTests();
77         URL JavaDoc testURL;
78         try {
79             testURL = unitTest.getURL();
80         } catch (FileStateInvalidException e) {
81             return null;
82         }
83         if (Arrays.asList(data[1]).contains(testURL)) {
84             return data[0];
85         } else {
86             return null;
87         }
88     }
89     
90     /**
91      * Look for all source roots and test source roots in the project.
92      * @return two-element array: first source roots, then test source roots
93      */

94     private URL JavaDoc[][] findSourcesAndTests() {
95         List JavaDoc<URL JavaDoc> sources = new ArrayList JavaDoc<URL JavaDoc>();
96         List JavaDoc<URL JavaDoc> tests = new ArrayList JavaDoc<URL JavaDoc>();
97         Element JavaDoc data = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true);
98         if (data != null) {
99             for (Element JavaDoc cu : Util.findSubElements(data)) {
100                 assert cu.getLocalName().equals("compilation-unit") : cu;
101                 boolean isTests = Util.findElement(cu, "unit-tests", JavaProjectNature.NS_JAVA_2) != null; // NOI18N
102
for (Element JavaDoc pr : Util.findSubElements(cu)) {
103                     if (pr.getLocalName().equals("package-root")) { // NOI18N
104
String JavaDoc rawtext = Util.findText(pr);
105                         assert rawtext != null;
106                         String JavaDoc evaltext = eval.evaluate(rawtext);
107                         if (evaltext != null) {
108                             (isTests ? tests : sources).add(evalTextToURL(evaltext));
109                         }
110                     }
111                 }
112             }
113         }
114         return new URL JavaDoc[][] {
115             sources.toArray(new URL JavaDoc[sources.size()]),
116             tests.toArray(new URL JavaDoc[tests.size()]),
117         };
118     }
119
120     private URL JavaDoc evalTextToURL(String JavaDoc evaltext) {
121         File JavaDoc location = helper.resolveFile(evaltext);
122         URL JavaDoc u;
123         try {
124             u = location.toURI().toURL();
125         } catch (MalformedURLException JavaDoc e) {
126             throw new AssertionError JavaDoc(e);
127         }
128         if (FileUtil.isArchiveFile(u)) {
129             return FileUtil.getArchiveRoot(u);
130         } else {
131             String JavaDoc us = u.toExternalForm();
132             if (us.endsWith("/")) {
133                 return u;
134             } else {
135                 try {
136                     return new URL JavaDoc(us + '/');
137                 } catch (MalformedURLException JavaDoc e) {
138                     throw new AssertionError JavaDoc(e);
139                 }
140             }
141         }
142     }
143     
144 }
145
Popular Tags