1 19 20 package org.netbeans.modules.java.project; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.Reader ; 25 import java.io.StringReader ; 26 import java.io.StringWriter ; 27 import java.net.URL ; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 import java.util.Properties ; 31 import javax.swing.event.ChangeListener ; 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 50 public final class JavaAntLoggerTest extends NbTestCase { 51 52 public JavaAntLoggerTest(String name) { 53 super(name); 54 } 55 56 private File simpleAppDir; 57 private Properties props; 58 59 protected void setUp() throws Exception { 60 super.setUp(); 61 MockServices.setServices(IOP.class, IFL.class, SFBQ.class); 62 simpleAppDir = new File (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 junitJarS = System.getProperty("test.junit.jar"); 70 assertNotNull("defined test.junit.jar", junitJarS); 71 File junitJar = new File (junitJarS); 72 assertTrue("file " + junitJar + " exists", junitJar.isFile()); 73 props = new Properties (); 74 props.setProperty("libs.junit.classpath", junitJar.getAbsolutePath()); } 76 77 public void testHyperlinkRun() throws Exception { 78 FileObject buildXml = FileUtil.toFileObject(new File (simpleAppDir, "build.xml")); 79 assertNotNull("have build.xml as a FileObject", buildXml); 80 ActionUtils.runTarget(buildXml, new String [] {"clean", "run"}, props).result(); 81 assertTrue("got a hyperlink for Clazz.run NPE", hyperlinkedErr.contains("\tat simpleapp.Clazz.run(Clazz.java:4)")); 83 } 84 85 86 public void testHyperlinkTest() throws Exception { 87 FileObject buildXml = FileUtil.toFileObject(new File (simpleAppDir, "build.xml")); 88 assertNotNull("have build.xml as a FileObject", buildXml); 89 ActionUtils.runTarget(buildXml, new String [] {"clean", "test"}, props).result(); 90 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 buildClasses, buildTestClasses; 97 private FileObject src, testSrc; 98 99 public void setSimpleAppDir(File simpleAppDir) throws Exception { 100 buildClasses = slashify(new File (simpleAppDir, "build" + File.separatorChar + "classes").toURI().toURL()); 101 buildTestClasses = slashify(new File (simpleAppDir, "build" + File.separatorChar + "test" + File.separatorChar + "classes").toURI().toURL()); 102 src = FileUtil.toFileObject(new File (simpleAppDir, "src")); 103 testSrc = FileUtil.toFileObject(new File (simpleAppDir, "test")); 104 } 105 106 private static URL slashify(URL u) throws Exception { 107 String s = u.toExternalForm(); 108 if (s.endsWith("/")) { 109 return u; 110 } else { 111 return new URL (s + "/"); 112 } 113 } 114 115 public SourceForBinaryQuery.Result findSourceRoots(URL 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 l) {} 138 139 public void removeChangeListener(ChangeListener 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 name, boolean newIO) { 150 return this; 151 } 152 153 public OutputWriter getStdOut() { 154 throw new UnsupportedOperationException (); 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 getIn() { 166 return new StringReader (""); 167 } 168 169 @SuppressWarnings ("deprecation") 170 public Reader 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 <String > nonhyperlinkedOut = new ArrayList <String >(); 203 private static final List <String > nonhyperlinkedErr = new ArrayList <String >(); 204 private static final List <String > hyperlinkedOut = new ArrayList <String >(); 205 private static final List <String > hyperlinkedErr = new ArrayList <String >(); 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 ()); 213 this.err = err; 214 } 215 216 public void println(String s, OutputListener l) throws IOException { 217 message(s, l != null); 218 } 219 220 public void println(String x) { 221 message(x, false); 222 } 223 224 private void message(String msg, boolean hyperlinked) { 225 List <String > messages = hyperlinked ? 226 (err ? hyperlinkedErr : hyperlinkedOut) : 227 (err ? nonhyperlinkedErr : nonhyperlinkedOut); 228 messages.add(msg); 229 } 230 231 public void reset() throws IOException {} 232 233 } 234 235 236 public static final class IFL extends InstalledFileLocator { 237 public IFL() {} 238 public File locate(String relativePath, String codeNameBase, boolean localized) { 239 if (relativePath.equals("ant/nblib/bridge.jar")) { 240 String path = System.getProperty("test.bridge.jar"); 241 assertNotNull("must set test.bridge.jar", path); 242 return new File (path); 243 } else if (relativePath.equals("ant")) { 244 String path = System.getProperty("test.ant.home"); 245 assertNotNull("must set test.ant.home", path); 246 return new File (path); 247 } else if (relativePath.startsWith("ant/")) { 248 String path = System.getProperty("test.ant.home"); 249 assertNotNull("must set test.ant.home", path); 250 return new File (path, relativePath.substring(4).replace('/', File.separatorChar)); 251 } else { 252 return null; 253 } 254 } 255 } 256 257 } 258 | Popular Tags |