Avoiding a binary class file in the repository
authorJan Lahoda <jlahoda@netbeans.org>
Sun, 08 Jan 2017 20:11:44 +0100
changeset 103755c0650c5969
parent 1036 3b6996b67a7c
child 1038 1e294e9e1ef4
Avoiding a binary class file in the repository
cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/DoRunTests.classx
cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/DoRunTests.javax
cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/MainTest.java
     1.1 Binary file cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/DoRunTests.classx has changed
     2.1 --- a/cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/DoRunTests.javax	Sun Jan 08 20:11:29 2017 +0100
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,16 +0,0 @@
     2.4 -package org.netbeans.modules.jackpot30.cmdline.testtool;
     2.5 -
     2.6 -import junit.framework.TestSuite;
     2.7 -import org.netbeans.modules.java.hints.declarative.test.api.DeclarativeHintsTestBase;
     2.8 -
     2.9 -/**
    2.10 - *
    2.11 - * @author lahvac
    2.12 - */
    2.13 -public class DoRunTests extends DeclarativeHintsTestBase {
    2.14 -
    2.15 -    public static TestSuite suite() {
    2.16 -        return suite(DoRunTests.class);
    2.17 -    }
    2.18 -
    2.19 -}
     3.1 --- a/cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/MainTest.java	Sun Jan 08 20:11:29 2017 +0100
     3.2 +++ b/cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/MainTest.java	Sun Jan 08 20:11:44 2017 +0100
     3.3 @@ -42,8 +42,11 @@
     3.4  import java.io.ByteArrayOutputStream;
     3.5  import java.io.File;
     3.6  import java.io.FileOutputStream;
     3.7 +import java.io.IOException;
     3.8  import java.io.InputStream;
     3.9  import java.io.PrintStream;
    3.10 +import java.net.URI;
    3.11 +import java.net.URISyntaxException;
    3.12  import java.net.URL;
    3.13  import java.net.URLClassLoader;
    3.14  import java.util.Arrays;
    3.15 @@ -52,6 +55,8 @@
    3.16  import java.util.List;
    3.17  import java.util.regex.Matcher;
    3.18  import java.util.regex.Pattern;
    3.19 +import javax.tools.SimpleJavaFileObject;
    3.20 +import javax.tools.ToolProvider;
    3.21  import org.junit.runner.Result;
    3.22  import org.netbeans.api.java.source.TestUtilities;
    3.23  import org.netbeans.junit.NbTestCase;
    3.24 @@ -633,6 +638,19 @@
    3.25      }
    3.26  
    3.27      //verify that the DeclarativeHintsTestBase works:
    3.28 +    private static final String CODE_RUN_DECLARATIVE =
    3.29 +            "package org.netbeans.modules.jackpot30.cmdline.testtool;\n" +
    3.30 +            "\n" +
    3.31 +            "import junit.framework.TestSuite;\n" +
    3.32 +            "import org.netbeans.modules.java.hints.declarative.test.api.DeclarativeHintsTestBase;\n" +
    3.33 +            "\n" +
    3.34 +            "public class DoRunTests extends DeclarativeHintsTestBase {\n" +
    3.35 +            "\n" +
    3.36 +            "    public static TestSuite suite() {\n" +
    3.37 +            "        return suite(DoRunTests.class);\n" +
    3.38 +            "    }\n" +
    3.39 +            "\n" +
    3.40 +            "}\n";
    3.41      public void testRunTest() throws Exception {
    3.42          clearWorkDir();
    3.43  
    3.44 @@ -664,22 +682,29 @@
    3.45                        "}}\n";
    3.46          TestUtilities.copyStringToFile(new File(classes, "h.test"), test);
    3.47  
    3.48 -        File runner = new File(classes, "org/netbeans/modules/jackpot30/cmdline/testtool/DoRunTests.class");
    3.49 +        List<String> options = Arrays.asList("-d", classes.getAbsolutePath());
    3.50 +        List<SourceFO> files = Arrays.asList(new SourceFO("DoRunTests.java", CODE_RUN_DECLARATIVE));
    3.51  
    3.52 -        assertTrue(runner.getParentFile().mkdirs());
    3.53 -
    3.54 -        FileOutputStream os = new FileOutputStream(runner);
    3.55 -        InputStream is = MainTest.class.getResourceAsStream("DoRunTests.classx");
    3.56 -
    3.57 -        assertNotNull(is);
    3.58 -
    3.59 -        FileUtil.copy(is, os);
    3.60 -        os.close ();
    3.61 -        is.close();
    3.62 +        assertTrue(ToolProvider.getSystemJavaCompiler().getTask(null, null, null, options, null, files).call());
    3.63  
    3.64          runAndTest(classes);
    3.65      }
    3.66  
    3.67 +    private static final class SourceFO extends SimpleJavaFileObject {
    3.68 +
    3.69 +        private final String code;
    3.70 +        private SourceFO(String name, String code) throws URISyntaxException {
    3.71 +            super(new URI("mem:///" + name), Kind.SOURCE);
    3.72 +            this.code = code;
    3.73 +        }
    3.74 +
    3.75 +        @Override
    3.76 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
    3.77 +            return code;
    3.78 +        }
    3.79 +
    3.80 +    }
    3.81 +
    3.82      protected void runAndTest(File classes) throws Exception {
    3.83          ClassLoader cl = new URLClassLoader(new URL[] {classes.toURI().toURL()}, MainTest.class.getClassLoader());
    3.84          Class<?> doRunTests = Class.forName("org.netbeans.modules.jackpot30.cmdline.testtool.DoRunTests", true, cl);