KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > project > JavaAntLoggerTest


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.project;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.Reader JavaDoc;
25 import java.io.StringReader JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Properties JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
32 import org.apache.tools.ant.module.api.support.ActionUtils;
33 import org.netbeans.api.java.queries.SourceForBinaryQuery;
34 import org.netbeans.junit.MockServices;
35 import org.netbeans.junit.NbTestCase;
36 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileUtil;
39 import org.openide.modules.InstalledFileLocator;
40 import org.openide.util.Lookup;
41 import org.openide.windows.IOProvider;
42 import org.openide.windows.InputOutput;
43 import org.openide.windows.OutputListener;
44 import org.openide.windows.OutputWriter;
45
46 /**
47  * Test hyperlinking functionality of {@link JavaAntLogger}.
48  * @author Jesse Glick
49  */

50 public final class JavaAntLoggerTest extends NbTestCase {
51     
52     public JavaAntLoggerTest(String JavaDoc name) {
53         super(name);
54     }
55     
56     private File JavaDoc simpleAppDir;
57     private Properties JavaDoc props;
58     
59     protected void setUp() throws Exception JavaDoc {
60         super.setUp();
61         MockServices.setServices(IOP.class, IFL.class, SFBQ.class);
62         simpleAppDir = new File JavaDoc(getDataDir(), "simple-app");
63         assertTrue("have dir " + simpleAppDir, simpleAppDir.isDirectory());
64         Lookup.getDefault().lookup(SFBQ.class).setSimpleAppDir(simpleAppDir);
65         nonhyperlinkedOut.clear();
66         nonhyperlinkedErr.clear();
67         hyperlinkedOut.clear();
68         hyperlinkedErr.clear();
69         String JavaDoc junitJarS = System.getProperty("test.junit.jar");
70         assertNotNull("defined test.junit.jar", junitJarS);
71         File JavaDoc junitJar = new File JavaDoc(junitJarS);
72         assertTrue("file " + junitJar + " exists", junitJar.isFile());
73         props = new Properties JavaDoc();
74         props.setProperty("libs.junit.classpath", junitJar.getAbsolutePath()); // #50261
75
}
76     
77     public void testHyperlinkRun() throws Exception JavaDoc {
78         FileObject buildXml = FileUtil.toFileObject(new File JavaDoc(simpleAppDir, "build.xml"));
79         assertNotNull("have build.xml as a FileObject", buildXml);
80         ActionUtils.runTarget(buildXml, new String JavaDoc[] {"clean", "run"}, props).result();
81         //System.out.println("nonhyperlinkedOut=" + nonhyperlinkedOut + " nonhyperlinkedErr=" + nonhyperlinkedErr + " hyperlinkedOut=" + hyperlinkedOut + " hyperlinkedErr=" + hyperlinkedErr);
82
assertTrue("got a hyperlink for Clazz.run NPE", hyperlinkedErr.contains("\tat simpleapp.Clazz.run(Clazz.java:4)"));
83     }
84     
85     /** See #44328. */
86     public void testHyperlinkTest() throws Exception JavaDoc {
87         FileObject buildXml = FileUtil.toFileObject(new File JavaDoc(simpleAppDir, "build.xml"));
88         assertNotNull("have build.xml as a FileObject", buildXml);
89         ActionUtils.runTarget(buildXml, new String JavaDoc[] {"clean", "test"}, props).result();
90         //System.out.println("nonhyperlinkedOut=" + nonhyperlinkedOut + " nonhyperlinkedErr=" + nonhyperlinkedErr + " hyperlinkedOut=" + hyperlinkedOut + " hyperlinkedErr=" + hyperlinkedErr);
91
assertTrue("got a hyperlink for Clazz.run NPE in " + hyperlinkedErr, hyperlinkedErr.contains("\tat simpleapp.Clazz.run(Clazz.java:4)"));
92     }
93     
94     public static final class SFBQ implements SourceForBinaryQueryImplementation {
95         
96         private URL JavaDoc buildClasses, buildTestClasses;
97         private FileObject src, testSrc;
98         
99         public void setSimpleAppDir(File JavaDoc simpleAppDir) throws Exception JavaDoc {
100             buildClasses = slashify(new File JavaDoc(simpleAppDir, "build" + File.separatorChar + "classes").toURI().toURL());
101             buildTestClasses = slashify(new File JavaDoc(simpleAppDir, "build" + File.separatorChar + "test" + File.separatorChar + "classes").toURI().toURL());
102             src = FileUtil.toFileObject(new File JavaDoc(simpleAppDir, "src"));
103             testSrc = FileUtil.toFileObject(new File JavaDoc(simpleAppDir, "test"));
104         }
105         
106         private static URL JavaDoc slashify(URL JavaDoc u) throws Exception JavaDoc {
107             String JavaDoc s = u.toExternalForm();
108             if (s.endsWith("/")) {
109                 return u;
110             } else {
111                 return new URL JavaDoc(s + "/");
112             }
113         }
114         
115         public SourceForBinaryQuery.Result findSourceRoots(URL JavaDoc binaryRoot) {
116             if (binaryRoot.equals(buildClasses)) {
117                 return new FixedResult(src);
118             } else if (binaryRoot.equals(buildTestClasses)) {
119                 return new FixedResult(testSrc);
120             } else {
121                 return null;
122             }
123         }
124         
125         private static final class FixedResult implements SourceForBinaryQuery.Result {
126             
127             private final FileObject dir;
128             
129             public FixedResult(FileObject dir) {
130                 this.dir = dir;
131             }
132
133             public FileObject[] getRoots() {
134                 return new FileObject[] {dir};
135             }
136
137             public void addChangeListener(ChangeListener JavaDoc l) {}
138
139             public void removeChangeListener(ChangeListener JavaDoc l) {}
140             
141         }
142         
143     }
144     
145     public static final class IOP extends IOProvider implements InputOutput {
146         
147         public IOP() {}
148
149         public InputOutput getIO(String JavaDoc name, boolean newIO) {
150             return this;
151         }
152
153         public OutputWriter getStdOut() {
154             throw new UnsupportedOperationException JavaDoc();
155         }
156
157         public OutputWriter getOut() {
158             return new OW(false);
159         }
160
161         public OutputWriter getErr() {
162             return new OW(true);
163         }
164
165         public Reader JavaDoc getIn() {
166             return new StringReader JavaDoc("");
167         }
168
169         @SuppressWarnings JavaDoc("deprecation")
170         public Reader JavaDoc flushReader() {
171             return getIn();
172         }
173
174         public void closeInputOutput() {}
175
176         public boolean isClosed() {
177             return false;
178         }
179
180         public boolean isErrSeparated() {
181             return false;
182         }
183
184         public boolean isFocusTaken() {
185             return false;
186         }
187
188         public void select() {}
189
190         public void setErrSeparated(boolean value) {}
191
192         public void setErrVisible(boolean value) {}
193
194         public void setFocusTaken(boolean value) {}
195
196         public void setInputVisible(boolean value) {}
197
198         public void setOutputVisible(boolean value) {}
199         
200     }
201     
202     private static final List JavaDoc<String JavaDoc> nonhyperlinkedOut = new ArrayList JavaDoc<String JavaDoc>();
203     private static final List JavaDoc<String JavaDoc> nonhyperlinkedErr = new ArrayList JavaDoc<String JavaDoc>();
204     private static final List JavaDoc<String JavaDoc> hyperlinkedOut = new ArrayList JavaDoc<String JavaDoc>();
205     private static final List JavaDoc<String JavaDoc> hyperlinkedErr = new ArrayList JavaDoc<String JavaDoc>();
206     
207     private static final class OW extends OutputWriter {
208         
209         private final boolean err;
210         
211         public OW(boolean err) {
212             super(new StringWriter JavaDoc());
213             this.err = err;
214         }
215
216         public void println(String JavaDoc s, OutputListener l) throws IOException JavaDoc {
217             message(s, l != null);
218         }
219
220         public void println(String JavaDoc x) {
221             message(x, false);
222         }
223         
224         private void message(String JavaDoc msg, boolean hyperlinked) {
225             List JavaDoc<String JavaDoc> messages = hyperlinked ?
226                 (err ? hyperlinkedErr : hyperlinkedOut) :
227                 (err ? nonhyperlinkedErr : nonhyperlinkedOut);
228             messages.add(msg);
229         }
230         
231         public void reset() throws IOException JavaDoc {}
232
233     }
234
235     /** Copied from AntLoggerTest. */
236     public static final class IFL extends InstalledFileLocator {
237         public IFL() {}
238         public File JavaDoc locate(String JavaDoc relativePath, String JavaDoc codeNameBase, boolean localized) {
239             if (relativePath.equals("ant/nblib/bridge.jar")) {
240                 String JavaDoc path = System.getProperty("test.bridge.jar");
241                 assertNotNull("must set test.bridge.jar", path);
242                 return new File JavaDoc(path);
243             } else if (relativePath.equals("ant")) {
244                 String JavaDoc path = System.getProperty("test.ant.home");
245                 assertNotNull("must set test.ant.home", path);
246                 return new File JavaDoc(path);
247             } else if (relativePath.startsWith("ant/")) {
248                 String JavaDoc path = System.getProperty("test.ant.home");
249                 assertNotNull("must set test.ant.home", path);
250                 return new File JavaDoc(path, relativePath.substring(4).replace('/', File.separatorChar));
251             } else {
252                 return null;
253             }
254         }
255     }
256
257 }
258
Popular Tags