Include ability to test declarative hints using the command line tool.
1.1 --- a/cmdline/lib/src/org/netbeans/modules/jackpot30/cmdline/lib/StandaloneTools.java Fri Dec 16 17:22:16 2011 +0100
1.2 +++ b/cmdline/lib/src/org/netbeans/modules/jackpot30/cmdline/lib/StandaloneTools.java Sat Dec 17 12:14:20 2011 +0100
1.3 @@ -60,13 +60,16 @@
1.4 import org.netbeans.api.java.source.ClasspathInfo;
1.5 import org.netbeans.modules.java.hints.jackpot.impl.Utilities.SPI;
1.6 import org.netbeans.modules.java.source.indexing.JavaCustomIndexer;
1.7 +import org.netbeans.modules.java.source.parsing.JavacParser;
1.8 import org.netbeans.modules.java.source.parsing.JavacParserFactory;
1.9 import org.netbeans.spi.editor.mimelookup.MimeDataProvider;
1.10 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
1.11 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
1.12 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation2;
1.13 +import org.openide.filesystems.FileObject;
1.14 import org.openide.filesystems.FileSystem;
1.15 import org.openide.filesystems.FileUtil;
1.16 +import org.openide.filesystems.MIMEResolver;
1.17 import org.openide.filesystems.MultiFileSystem;
1.18 import org.openide.filesystems.Repository;
1.19 import org.openide.filesystems.XMLFileSystem;
1.20 @@ -317,4 +320,21 @@
1.21 return ClassPathSupport.createClassPath(roots.toArray(new URL[roots.size()]));
1.22 }
1.23
1.24 + @ServiceProvider(service=MIMEResolver.class)
1.25 + public static final class JavaMimeResolver extends MIMEResolver {
1.26 +
1.27 + public JavaMimeResolver() {
1.28 + super(JavacParser.MIME_TYPE);
1.29 + }
1.30 +
1.31 + @Override
1.32 + public String findMIMEType(FileObject fo) {
1.33 + if ("java".equals(fo.getExt())) {
1.34 + return JavacParser.MIME_TYPE;
1.35 + }
1.36 +
1.37 + return null;
1.38 + }
1.39 +
1.40 + }
1.41 }
2.1 --- a/cmdline/lib/test/unit/src/org/netbeans/modules/jackpot30/cmdline/lib/CreateStandaloneJar.java Fri Dec 16 17:22:16 2011 +0100
2.2 +++ b/cmdline/lib/test/unit/src/org/netbeans/modules/jackpot30/cmdline/lib/CreateStandaloneJar.java Sat Dec 17 12:14:20 2011 +0100
2.3 @@ -64,6 +64,7 @@
2.4 import java.util.Map.Entry;
2.5 import java.util.Set;
2.6 import java.util.jar.JarOutputStream;
2.7 +import java.util.regex.Pattern;
2.8 import java.util.zip.ZipEntry;
2.9 import org.apache.lucene.analysis.tokenattributes.OffsetAttributeImpl;
2.10 import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttributeImpl;
2.11 @@ -72,6 +73,7 @@
2.12 import org.netbeans.junit.NbTestCase;
2.13 import org.netbeans.modules.classfile.ClassFile;
2.14 import org.netbeans.modules.classfile.ClassName;
2.15 +import org.netbeans.modules.jackpot30.cmdline.lib.StandaloneTools.JavaMimeResolver;
2.16 import org.netbeans.modules.jackpot30.cmdline.lib.StandaloneTools.RepositoryImpl;
2.17 import org.netbeans.modules.jackpot30.spi.Hacks.HintPreferencesProvider;
2.18 import org.netbeans.modules.java.hints.declarative.DeclarativeHintRegistry;
2.19 @@ -125,13 +127,17 @@
2.20 Set<String> done = new HashSet<String>();
2.21 Set<String> bundlesToCopy = new HashSet<String>();
2.22
2.23 - while (!toProcess.isEmpty()) {
2.24 + OUTER: while (!toProcess.isEmpty()) {
2.25 String fqn = toProcess.remove(0);
2.26
2.27 if (!done.add(fqn)) {
2.28 continue;
2.29 }
2.30
2.31 + for (Pattern p : info.exclude) {
2.32 + if (p.matcher(fqn).matches()) continue OUTER;
2.33 + }
2.34 +
2.35 // System.err.println("processing: " + fqn);
2.36
2.37 String fileName = fqn.replace('.', '/') + ".class";
2.38 @@ -256,6 +262,7 @@
2.39 private final List<MetaInfRegistration> metaInf = new LinkedList<MetaInfRegistration>();
2.40 private final Set<String> copyMetaInfRegistration = new HashSet<String>();
2.41 private boolean escapeJavaxLang;
2.42 + private final List<Pattern> exclude = new LinkedList<Pattern>();
2.43 public Info() {}
2.44 public Info addAdditionalRoots(String... fqns) {
2.45 additionalRoots.addAll(Arrays.asList(fqns));
2.46 @@ -269,6 +276,10 @@
2.47 copyMetaInfRegistration.addAll(Arrays.asList(registrationsToCopy));
2.48 return this;
2.49 }
2.50 + public Info addExcludePattern(Pattern... pattern) {
2.51 + exclude.addAll(Arrays.asList(pattern));
2.52 + return this;
2.53 + }
2.54 public Info setEscapeJavaxLang() {
2.55 this.escapeJavaxLang = true;
2.56 return this;
2.57 @@ -414,6 +425,7 @@
2.58 , "org.netbeans.modules.java.hints.infrastructure.RulesManager$HintProviderImpl"
2.59 , Tree.class.getName()
2.60 ,JavacTool.class.getName()
2.61 + ,JavaMimeResolver.class.getName()
2.62 ));
2.63
2.64 private static final Set<String> COPY_REGISTRATION = new HashSet<String>(Arrays.<String>asList(
2.65 @@ -422,7 +434,8 @@
2.66 "org.openide.util.Lookup",
2.67 "org.netbeans.modules.openide.util.PreferencesProvider",
2.68 HintPreferencesProvider.class.getName(),
2.69 - ClassPathBasedHintProvider.class.getName()
2.70 + ClassPathBasedHintProvider.class.getName(),
2.71 + "org.openide.filesystems.MIMEResolver"
2.72 ));
2.73
2.74 private static final Set<String> RESOURCES = new HashSet<String>(Arrays.asList(
3.1 --- a/cmdline/tool/nbproject/genfiles.properties Fri Dec 16 17:22:16 2011 +0100
3.2 +++ b/cmdline/tool/nbproject/genfiles.properties Sat Dec 17 12:14:20 2011 +0100
3.3 @@ -3,6 +3,6 @@
3.4 build.xml.stylesheet.CRC32=a56c6a5b@1.44
3.5 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
3.6 # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
3.7 -nbproject/build-impl.xml.data.CRC32=050807bd
3.8 +nbproject/build-impl.xml.data.CRC32=af581875
3.9 nbproject/build-impl.xml.script.CRC32=08f1fb11
3.10 -nbproject/build-impl.xml.stylesheet.CRC32=238281d1@2.47
3.11 +nbproject/build-impl.xml.stylesheet.CRC32=238281d1@2.49
4.1 --- a/cmdline/tool/nbproject/project.xml Fri Dec 16 17:22:16 2011 +0100
4.2 +++ b/cmdline/tool/nbproject/project.xml Sat Dec 17 12:14:20 2011 +0100
4.3 @@ -268,6 +268,10 @@
4.4 <compile-dependency/>
4.5 </test-dependency>
4.6 <test-dependency>
4.7 + <code-name-base>org.netbeans.modules.java.hints.declarative.test</code-name-base>
4.8 + <compile-dependency/>
4.9 + </test-dependency>
4.10 + <test-dependency>
4.11 <code-name-base>org.netbeans.modules.java.j2seplatform</code-name-base>
4.12 <recursive/>
4.13 <compile-dependency/>
5.1 --- a/cmdline/tool/src/org/netbeans/modules/jackpot30/cmdline/Main.java Fri Dec 16 17:22:16 2011 +0100
5.2 +++ b/cmdline/tool/src/org/netbeans/modules/jackpot30/cmdline/Main.java Sat Dec 17 12:14:20 2011 +0100
5.3 @@ -1,7 +1,7 @@
5.4 /*
5.5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5.6 *
5.7 - * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
5.8 + * Copyright 2010-2011 Sun Microsystems, Inc. All rights reserved.
5.9 *
5.10 * The contents of this file are subject to the terms of either the GNU
5.11 * General Public License Version 2 only ("GPL") or the Common
5.12 @@ -34,7 +34,7 @@
5.13 *
5.14 * Contributor(s):
5.15 *
5.16 - * Portions Copyrighted 2010 Sun Microsystems, Inc.
5.17 + * Portions Copyrighted 2010-2011 Sun Microsystems, Inc.
5.18 */
5.19
5.20 package org.netbeans.modules.jackpot30.cmdline;
5.21 @@ -273,8 +273,6 @@
5.22 MainLookup.register(new JavaPathRecognizer());
5.23 MainLookup.register(new SourceLevelQueryImpl(sourceCP, "1.7"));
5.24
5.25 - FileUtil.setMIMEType("java", "text/x-java");
5.26 -
5.27 ProgressHandleWrapper progress = parsed.has("progress") ? new ProgressHandleWrapper(new ConsoleProgressHandleAbstraction(), 1) : new ProgressHandleWrapper(1);
5.28
5.29 if (parsed.has(OPTION_NO_APPLY)) {
6.1 --- a/cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/CreateTool.java Fri Dec 16 17:22:16 2011 +0100
6.2 +++ b/cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/CreateTool.java Sat Dec 17 12:14:20 2011 +0100
6.3 @@ -1,7 +1,7 @@
6.4 /*
6.5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
6.6 *
6.7 - * Copyright 2009-2010 Sun Microsystems, Inc. All rights reserved.
6.8 + * Copyright 2009-2011 Sun Microsystems, Inc. All rights reserved.
6.9 *
6.10 * The contents of this file are subject to the terms of either the GNU
6.11 * General Public License Version 2 only ("GPL") or the Common
6.12 @@ -34,12 +34,14 @@
6.13 *
6.14 * Contributor(s):
6.15 *
6.16 - * Portions Copyrighted 2009-2010 Sun Microsystems, Inc.
6.17 + * Portions Copyrighted 2009-2011 Sun Microsystems, Inc.
6.18 */
6.19 package org.netbeans.modules.jackpot30.cmdline;
6.20
6.21 +import java.util.regex.Pattern;
6.22 import org.netbeans.modules.jackpot30.cmdline.lib.CreateStandaloneJar;
6.23 import org.netbeans.modules.jackpot30.cmdline.lib.CreateStandaloneJar.Info;
6.24 +import org.netbeans.modules.java.hints.declarative.test.api.DeclarativeHintsTestBase;
6.25 import org.netbeans.modules.java.j2seproject.J2SEProject;
6.26 import org.netbeans.modules.java.platform.DefaultJavaPlatformProvider;
6.27 import org.netbeans.modules.project.ui.OpenProjectsTrampolineImpl;
6.28 @@ -56,8 +58,9 @@
6.29
6.30 @Override
6.31 protected Info computeInfo() {
6.32 - return new Info().addAdditionalRoots(Main.class.getName(), OpenProjectsTrampolineImpl.class.getName(), J2SEProject.class.getName(), DefaultJavaPlatformProvider.class.getName())
6.33 + return new Info().addAdditionalRoots(Main.class.getName(), DeclarativeHintsTestBase.class.getName(), OpenProjectsTrampolineImpl.class.getName(), J2SEProject.class.getName(), DefaultJavaPlatformProvider.class.getName())
6.34 .addMetaInfRegistrations(new MetaInfRegistration(org.netbeans.modules.project.uiapi.OpenProjectsTrampoline.class, OpenProjectsTrampolineImpl.class))
6.35 + .addExcludePattern(Pattern.compile("junit\\.framework\\..*"))
6.36 .setEscapeJavaxLang();
6.37 }
6.38
7.1 --- a/cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/CreateToolTest.java Fri Dec 16 17:22:16 2011 +0100
7.2 +++ b/cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/CreateToolTest.java Sat Dec 17 12:14:20 2011 +0100
7.3 @@ -1,7 +1,7 @@
7.4 /*
7.5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
7.6 *
7.7 - * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
7.8 + * Copyright 2010-2011 Sun Microsystems, Inc. All rights reserved.
7.9 *
7.10 * The contents of this file are subject to the terms of either the GNU
7.11 * General Public License Version 2 only ("GPL") or the Common
7.12 @@ -34,7 +34,7 @@
7.13 *
7.14 * Contributor(s):
7.15 *
7.16 - * Portions Copyrighted 2010 Sun Microsystems, Inc.
7.17 + * Portions Copyrighted 2010-2011 Sun Microsystems, Inc.
7.18 */
7.19
7.20 package org.netbeans.modules.jackpot30.cmdline;
7.21 @@ -45,10 +45,14 @@
7.22 import java.io.InputStream;
7.23 import java.io.OutputStream;
7.24 import java.util.Arrays;
7.25 +import java.util.HashSet;
7.26 +import java.util.Iterator;
7.27 import java.util.LinkedList;
7.28 import java.util.List;
7.29 +import java.util.Set;
7.30 import junit.extensions.TestSetup;
7.31 import junit.framework.Test;
7.32 +import org.junit.runner.JUnitCore;
7.33 import org.netbeans.junit.NbTestSuite;
7.34 import org.openide.filesystems.FileUtil;
7.35 import org.openide.util.Exceptions;
7.36 @@ -93,6 +97,55 @@
7.37 throw new IOException(t);
7.38 }
7.39 }
7.40 +
7.41 + private static final Set<String> MUST_CONTAIN = new HashSet<String>(Arrays.asList(" 1 failure", "/h.test/neg", "Tests run: 2, Failures: 1"));
7.42 + private static final Set<String> MUST_NOT_CONTAIN = new HashSet<String>(Arrays.asList("/h.test/pos"));
7.43 +
7.44 + @Override
7.45 + protected void runAndTest(File classes) throws Exception {
7.46 + List<String> ll = new LinkedList<String>();
7.47 + ll.add("java");
7.48 + ll.add("-classpath");
7.49 + ll.add(compiler.getAbsolutePath() + File.pathSeparator + classes.getAbsolutePath() + File.pathSeparator + new File(JUnitCore.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getAbsolutePath());
7.50 +
7.51 +// ll.add("-Xdebug");
7.52 +// ll.add("-Xrunjdwp:transport=dt_socket,suspend=y,server=y,address=8889");
7.53 +
7.54 + ll.add("org.junit.runner.JUnitCore");
7.55 + ll.add("org.netbeans.modules.jackpot30.cmdline.testtool.DoRunTests");
7.56 +
7.57 + System.err.println("ll=" + ll);
7.58 + Process p = Runtime.getRuntime().exec(ll.toArray(new String[0]), null, getWorkDir());
7.59 + String[] output = new String[2];
7.60 + CopyStream outCopy = new CopyStream(p.getInputStream(), output, 0);
7.61 + CopyStream errCopy = new CopyStream(p.getErrorStream(), output, 1);
7.62 +
7.63 + outCopy.start();
7.64 + errCopy.start();
7.65 +
7.66 + p.waitFor();
7.67 +
7.68 + outCopy.doJoin();
7.69 + errCopy.doJoin();
7.70 +
7.71 + Set<String> mustContainCopy = new HashSet<String>(MUST_CONTAIN);
7.72 +
7.73 + System.err.println(output[0]);
7.74 + System.err.println(output[1]);
7.75 +
7.76 + verify(output[0], mustContainCopy);
7.77 + verify(output[1], mustContainCopy);
7.78 + }
7.79 +
7.80 + private void verify(String output, Set<String> mustContainCopy) {
7.81 + for (Iterator<String> it = mustContainCopy.iterator(); it.hasNext();) {
7.82 + assertTrue(output.contains(it.next()));
7.83 + it.remove();
7.84 + }
7.85 + for (String nc : MUST_NOT_CONTAIN) {
7.86 + assertFalse(output.contains(nc));
7.87 + }
7.88 + }
7.89
7.90 private static final class CopyStream extends Thread {
7.91 private final InputStream ins;
8.1 Binary file cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/DoRunTests.classx has changed
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/DoRunTests.javax Sat Dec 17 12:14:20 2011 +0100
9.3 @@ -0,0 +1,16 @@
9.4 +package org.netbeans.modules.jackpot30.cmdline.testtool;
9.5 +
9.6 +import junit.framework.TestSuite;
9.7 +import org.netbeans.modules.java.hints.declarative.test.api.DeclarativeHintsTestBase;
9.8 +
9.9 +/**
9.10 + *
9.11 + * @author lahvac
9.12 + */
9.13 +public class DoRunTests extends DeclarativeHintsTestBase {
9.14 +
9.15 + public static TestSuite suite() {
9.16 + return suite(DoRunTests.class);
9.17 + }
9.18 +
9.19 +}
10.1 --- a/cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/MainTest.java Fri Dec 16 17:22:16 2011 +0100
10.2 +++ b/cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/MainTest.java Sat Dec 17 12:14:20 2011 +0100
10.3 @@ -1,7 +1,7 @@
10.4 /*
10.5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
10.6 *
10.7 - * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
10.8 + * Copyright 2010-2011 Sun Microsystems, Inc. All rights reserved.
10.9 *
10.10 * The contents of this file are subject to the terms of either the GNU
10.11 * General Public License Version 2 only ("GPL") or the Common
10.12 @@ -34,22 +34,28 @@
10.13 *
10.14 * Contributor(s):
10.15 *
10.16 - * Portions Copyrighted 2010 Sun Microsystems, Inc.
10.17 + * Portions Copyrighted 2010-2011 Sun Microsystems, Inc.
10.18 */
10.19
10.20 package org.netbeans.modules.jackpot30.cmdline;
10.21
10.22 import java.io.ByteArrayOutputStream;
10.23 import java.io.File;
10.24 +import java.io.FileOutputStream;
10.25 +import java.io.InputStream;
10.26 import java.io.PrintStream;
10.27 +import java.net.URL;
10.28 +import java.net.URLClassLoader;
10.29 import java.util.Arrays;
10.30 import java.util.Collections;
10.31 import java.util.LinkedList;
10.32 import java.util.List;
10.33 import java.util.regex.Matcher;
10.34 import java.util.regex.Pattern;
10.35 +import org.junit.runner.Result;
10.36 import org.netbeans.api.java.source.TestUtilities;
10.37 import org.netbeans.junit.NbTestCase;
10.38 +import org.openide.filesystems.FileUtil;
10.39
10.40 /**
10.41 *
10.42 @@ -232,4 +238,60 @@
10.43 }
10.44 }
10.45
10.46 + //verify that the DeclarativeHintsTestBase works:
10.47 + public void testRunTest() throws Exception {
10.48 + clearWorkDir();
10.49 +
10.50 + File wd = getWorkDir();
10.51 + File classes = new File(wd, "classes");
10.52 +
10.53 + classes.mkdirs();
10.54 + TestUtilities.copyStringToFile(new File(classes, "h.hint"), "$1.equals(\"\") :: $1 instanceof java.lang.String => $1.isEmpty();;");
10.55 +
10.56 + String test = "%%TestCase pos\n" +
10.57 + "package test;\n" +
10.58 + "public class Test {{\n" +
10.59 + " System.err.println(\"a\".equals(\"\"));\n" +
10.60 + "}}\n" +
10.61 + "%%=>\n" +
10.62 + "package test;\n" +
10.63 + "public class Test {{\n" +
10.64 + " System.err.println(\"a\".isEmpty());\n" +
10.65 + "}}\n" +
10.66 + "%%TestCase neg\n" +
10.67 + "package test;\n" +
10.68 + "public class Test {{\n" +
10.69 + " System.err.println(\"a\".equals(\"a\"));\n" +
10.70 + "}}\n" +
10.71 + "%%=>\n" +
10.72 + "package test;\n" +
10.73 + "public class Test {{\n" +
10.74 + " System.err.println(\"a\".isEmpty());\n" +
10.75 + "}}\n";
10.76 + TestUtilities.copyStringToFile(new File(classes, "h.test"), test);
10.77 +
10.78 + File runner = new File(classes, "org/netbeans/modules/jackpot30/cmdline/testtool/DoRunTests.class");
10.79 +
10.80 + assertTrue(runner.getParentFile().mkdirs());
10.81 +
10.82 + FileOutputStream os = new FileOutputStream(runner);
10.83 + InputStream is = MainTest.class.getResourceAsStream("DoRunTests.classx");
10.84 +
10.85 + assertNotNull(is);
10.86 +
10.87 + FileUtil.copy(is, os);
10.88 + os.close ();
10.89 + is.close();
10.90 +
10.91 + runAndTest(classes);
10.92 + }
10.93 +
10.94 + protected void runAndTest(File classes) throws Exception {
10.95 + ClassLoader cl = new URLClassLoader(new URL[] {classes.toURI().toURL()}, MainTest.class.getClassLoader());
10.96 + Class<?> doRunTests = Class.forName("org.netbeans.modules.jackpot30.cmdline.testtool.DoRunTests", true, cl);
10.97 + Result testResult = org.junit.runner.JUnitCore.runClasses(doRunTests);
10.98 +
10.99 + assertEquals(1, testResult.getFailureCount());
10.100 + assertTrue(testResult.getFailures().toString(), testResult.getFailures().get(0).getDescription().getMethodName().endsWith("/h.test/neg"));
10.101 + }
10.102 }
10.103 \ No newline at end of file