cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/MainTest.java
author Jan Lahoda <jlahoda@netbeans.org>
Sun, 08 Jan 2017 20:11:44 +0100
changeset 1037 55c0650c5969
parent 1016 02ad9fe4588c
child 1041 b03a880d538e
permissions -rw-r--r--
Avoiding a binary class file in the repository
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2010-2011 Sun Microsystems, Inc. All rights reserved.
     5  *
     6  * The contents of this file are subject to the terms of either the GNU
     7  * General Public License Version 2 only ("GPL") or the Common
     8  * Development and Distribution License("CDDL") (collectively, the
     9  * "License"). You may not use this file except in compliance with the
    10  * License. You can obtain a copy of the License at
    11  * http://www.netbeans.org/cddl-gplv2.html
    12  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    13  * specific language governing permissions and limitations under the
    14  * License.  When distributing the software, include this License Header
    15  * Notice in each file and include the License file at
    16  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    17  * particular file as subject to the "Classpath" exception as provided
    18  * by Sun in the GPL Version 2 section of the License file that
    19  * accompanied this code. If applicable, add the following below the
    20  * License Header, with the fields enclosed by brackets [] replaced by
    21  * your own identifying information:
    22  * "Portions Copyrighted [year] [name of copyright owner]"
    23  *
    24  * If you wish your version of this file to be governed by only the CDDL
    25  * or only the GPL Version 2, indicate your decision by adding
    26  * "[Contributor] elects to include this software in this distribution
    27  * under the [CDDL or GPL Version 2] license." If you do not indicate a
    28  * single choice of license, a recipient has the option to distribute
    29  * your version of this file under either the CDDL, the GPL Version 2 or
    30  * to extend the choice of license to its licensees as provided above.
    31  * However, if you add GPL Version 2 code and therefore, elected the GPL
    32  * Version 2 license, then the option applies only if the new code is
    33  * made subject to such option by the copyright holder.
    34  *
    35  * Contributor(s):
    36  *
    37  * Portions Copyrighted 2010-2011 Sun Microsystems, Inc.
    38  */
    39 
    40 package org.netbeans.modules.jackpot30.cmdline;
    41 
    42 import java.io.ByteArrayOutputStream;
    43 import java.io.File;
    44 import java.io.FileOutputStream;
    45 import java.io.IOException;
    46 import java.io.InputStream;
    47 import java.io.PrintStream;
    48 import java.net.URI;
    49 import java.net.URISyntaxException;
    50 import java.net.URL;
    51 import java.net.URLClassLoader;
    52 import java.util.Arrays;
    53 import java.util.Collections;
    54 import java.util.LinkedList;
    55 import java.util.List;
    56 import java.util.regex.Matcher;
    57 import java.util.regex.Pattern;
    58 import javax.tools.SimpleJavaFileObject;
    59 import javax.tools.ToolProvider;
    60 import org.junit.runner.Result;
    61 import org.netbeans.api.java.source.TestUtilities;
    62 import org.netbeans.junit.NbTestCase;
    63 import org.openide.filesystems.FileUtil;
    64 
    65 /**XXX: should also test error conditions
    66  *
    67  * @author lahvac
    68  */
    69 public class MainTest extends NbTestCase {
    70 
    71     public MainTest(String name) {
    72         super(name);
    73     }
    74 
    75     public void testRunCompiler1() throws Exception {
    76         String golden =
    77             "package test;\n" +
    78             "public class Test {\n" +
    79             "    private void test(java.util.Collection c) {\n" +
    80             "        boolean b = c.isEmpty();\n" +
    81             "    }\n" +
    82             "}\n";
    83 
    84         doRunCompiler(golden,
    85                       null,
    86                       null,
    87                       "src/test/Test.java",
    88                       "package test;\n" +
    89                       "public class Test {\n" +
    90                       "    private void test(java.util.Collection c) {\n" +
    91                       "        boolean b = c.size() == 0;\n" +
    92                       "    }\n" +
    93                       "}\n",
    94                       null,
    95                       "--apply",
    96                       "--hint",
    97                       "Usage of .size() == 0");
    98     }
    99 
   100     public void testDoNotApply() throws Exception {
   101         String golden =
   102             "package test;\n" +
   103             "public class Test {\n" +
   104             "    private void test(java.util.Collection c) {\n" +
   105             "        boolean b1 = c.size() == 0;\n" +
   106             "\tboolean b2 = c.size() == 0;\n" +
   107             "    }\n" +
   108             "}\n";
   109 
   110         doRunCompiler(golden,
   111                       "${workdir}/src/test/Test.java:4: warning: [Usage_of_size_equals_0] Usage of .size() == 0 can be replaced with .isEmpty()\n" +
   112                       "        boolean b1 = c.size() == 0;\n" +
   113                       "                     ^\n" +
   114                       "${workdir}/src/test/Test.java:5: warning: [Usage_of_size_equals_0] Usage of .size() == 0 can be replaced with .isEmpty()\n" +
   115                       "\tboolean b2 = c.size() == 0;\n" +
   116                       "\t             ^\n",
   117                       null,
   118                       "src/test/Test.java",
   119                       "package test;\n" +
   120                       "public class Test {\n" +
   121                       "    private void test(java.util.Collection c) {\n" +
   122                       "        boolean b1 = c.size() == 0;\n" +
   123                       "\tboolean b2 = c.size() == 0;\n" +
   124                       "    }\n" +
   125                       "}\n",
   126                       null,
   127                       "--hint",
   128                       "Usage of .size() == 0",
   129                       "--no-apply");
   130     }
   131 
   132     public void testConfig() throws Exception {
   133         String golden =
   134             "package test;\n" +
   135             "public class Test {\n" +
   136             "    private int test(String str) {\n" +
   137             "        if (\"a\" == str) {\n" +
   138             "            return 1;\n" +
   139             "        } else if (\"b\" == str) {\n" +
   140             "            return 2;\n" +
   141             "        } else {\n" +
   142             "            return 3;\n" +
   143             "        }\n" +
   144             "    }\n" +
   145             "}\n";
   146 
   147         doRunCompiler(golden,
   148                       null,
   149                       null,
   150                       "src/test/Test.java",
   151                       "package test;\n" +
   152                       "public class Test {\n" +
   153                       "    private int test(String str) {\n" +
   154                       "        if (\"a\" == str) {\n" +
   155                       "            return 1;\n" +
   156                       "        } else if (\"b\" == str) {\n" +
   157                       "            return 2;\n" +
   158                       "        } else {\n" +
   159                       "            return 3;\n" +
   160                       "        }\n" +
   161                       "    }\n" +
   162                       "}\n",
   163                       null,
   164                       "--hint",
   165                       "Use switch over Strings where possible.",
   166                       "--config",
   167                       "also-equals=false");
   168     }
   169 
   170     public void testValidSourceLevel() throws Exception {
   171         String golden =
   172             "package test;\n" +
   173             "public class Test {\n" +
   174             "    private void test(java.util.Collection c) {\n" +
   175             "        boolean b = c.isEmpty();\n" +
   176             "    }\n" +
   177             "}\n";
   178 
   179         doRunCompiler(golden,
   180                       null,
   181                       null,
   182                       "src/test/Test.java",
   183                       "package test;\n" +
   184                       "public class Test {\n" +
   185                       "    private void test(java.util.Collection c) {\n" +
   186                       "        boolean b = c.size() == 0;\n" +
   187                       "    }\n" +
   188                       "}\n",
   189                       null,
   190                       "--apply",
   191                       "--hint",
   192                       "Usage of .size() == 0",
   193                       "--source",
   194                       "1.6");
   195     }
   196 
   197     public void testConfigurationFile() throws Exception {
   198         String golden =
   199             "package test;\n" +
   200             "public class Test {\n" +
   201             "    private void test(java.util.Collection c) {\n" +
   202             "        boolean b = c.isEmpty();\n" +
   203             "    }\n" +
   204             "}\n";
   205 
   206         doRunCompiler(golden,
   207                       null,
   208                       null,
   209                       "src/test/Test.java",
   210                       "package test;\n" +
   211                       "public class Test {\n" +
   212                       "    private void test(java.util.Collection c) {\n" +
   213                       "        boolean b = c.size() == 0;\n" +
   214                       "    }\n" +
   215                       "}\n",
   216                       "settings.xml",
   217                       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
   218                       "<hints apply=\"true\">\n" +
   219                       "    <settings>\n" +
   220                       "        <org.netbeans.modules.java.hints.perf.SizeEqualsZero check.not.equals=\"false\" enabled=\"true\" hintSeverity=\"VERIFIER\"/>\n" +
   221                       "    </settings>\n" +
   222                       "</hints>\n",
   223                       null,
   224                       "--config-file",
   225                       "${workdir}/settings.xml",
   226                       "--source",
   227                       "1.6");
   228     }
   229 
   230     public void testConfigurationFileCmdLineOverride() throws Exception {
   231         String golden =
   232             "package test;\n" +
   233             "public class Test {\n" +
   234             "    private void test(java.util.Collection c) {\n" +
   235             "        boolean b = c.size() == 0;\n" +
   236             "    }\n" +
   237             "}\n";
   238 
   239         doRunCompiler(golden,
   240                       "${workdir}/src/test/Test.java:4: warning: [Usage_of_size_equals_0] Usage of .size() == 0 can be replaced with .isEmpty()\n" +
   241                       "        boolean b = c.size() == 0;\n" +
   242                       "                    ^\n",
   243                       null,
   244                       "src/test/Test.java",
   245                       "package test;\n" +
   246                       "public class Test {\n" +
   247                       "    private void test(java.util.Collection c) {\n" +
   248                       "        boolean b = c.size() == 0;\n" +
   249                       "    }\n" +
   250                       "}\n",
   251                       "settings.xml",
   252                       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
   253                       "<hints apply=\"true\">\n" +
   254                       "    <settings>\n" +
   255                       "        <org.netbeans.modules.java.hints.perf.SizeEqualsZero check.not.equals=\"false\" enabled=\"true\" hintSeverity=\"VERIFIER\"/>\n" +
   256                       "    </settings>\n" +
   257                       "</hints>\n",
   258                       null,
   259                       "--config-file",
   260                       "${workdir}/settings.xml",
   261                       "--source",
   262                       "1.6",
   263                       "--no-apply");
   264     }
   265     
   266     public void testHintFile() throws Exception {
   267         String golden =
   268             "package test;\n" +
   269             "public class Test {\n" +
   270             "    private void test(java.util.Collection c) {\n" +
   271             "        boolean b = c.size() == 0;\n" +
   272             "    }\n" +
   273             "}\n";
   274 
   275         doRunCompiler(golden,
   276                       "",
   277                       null,
   278                       "src/test/Test.java",
   279                       "package test;\n" +
   280                       "public class Test {\n" +
   281                       "    private void test(java.util.Collection c) {\n" +
   282                       "        boolean b = c.isEmpty();\n" +
   283                       "    }\n" +
   284                       "}\n",
   285                       "test-rule.hint",
   286                       "$var.isEmpty() => $var.size() == 0;;",
   287                       null,
   288                       "--hint-file",
   289                       "${workdir}/test-rule.hint",
   290                       "--source",
   291                       "1.6",
   292                       "--apply");
   293     }
   294 
   295     public void testConfigurationFileDeclarative1() throws Exception {
   296         String golden =
   297             "package test;\n" +
   298             "public class Test {\n" +
   299             "    private void test(java.util.Collection c) {\n" +
   300             "        boolean b1 = c.isEmpty();\n" +
   301             "        boolean b2 = c.size() <= 0;\n" +
   302             "    }\n" +
   303             "}\n";
   304 
   305         doRunCompiler(golden,
   306                       null,
   307                       null,
   308                       "src/test/Test.java",
   309                       "package test;\n" +
   310                       "public class Test {\n" +
   311                       "    private void test(java.util.Collection c) {\n" +
   312                       "        boolean b1 = c.size() == 0;\n" +
   313                       "        boolean b2 = c.size() <= 0;\n" +
   314                       "    }\n" +
   315                       "}\n",
   316                       "META-INF/upgrade/test1.hint",
   317                       "$c.size() == 0 :: $c instanceof java.util.Collection => $c.isEmpty();;\n",
   318                       "META-INF/upgrade/test2.hint",
   319                       "$c.size() <= 0 :: $c instanceof java.util.Collection => $c.isEmpty();;\n",
   320                       "settings.xml",
   321                       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
   322                       "<hints apply=\"true\" runDeclarative=\"false\">\n" +
   323                       "    <settings>\n" +
   324                       "        <test1.hint enabled=\"true\"/>\n" +
   325                       "    </settings>\n" +
   326                       "</hints>\n",
   327                       null,
   328                       "--config-file",
   329                       "${workdir}/settings.xml",
   330                       "--source",
   331                       "1.6");
   332     }
   333 
   334     public void testConfigurationFileDeclarative2() throws Exception {
   335         String golden =
   336             "package test;\n" +
   337             "public class Test {\n" +
   338             "    private void test(java.util.Collection c) {\n" +
   339             "        boolean b1 = c.isEmpty();\n" +
   340             "        boolean b2 = c.isEmpty();\n" +
   341             "    }\n" +
   342             "}\n";
   343 
   344         doRunCompiler(golden,
   345                       null,
   346                       null,
   347                       "src/test/Test.java",
   348                       "package test;\n" +
   349                       "public class Test {\n" +
   350                       "    private void test(java.util.Collection c) {\n" +
   351                       "        boolean b1 = c.size() == 0;\n" +
   352                       "        boolean b2 = c.size() <= 0;\n" +
   353                       "    }\n" +
   354                       "}\n",
   355                       "META-INF/upgrade/test1.hint",
   356                       "$c.size() == 0 :: $c instanceof java.util.Collection => $c.isEmpty();;\n",
   357                       "META-INF/upgrade/test2.hint",
   358                       "$c.size() <= 0 :: $c instanceof java.util.Collection => $c.isEmpty();;\n",
   359                       "settings.xml",
   360                       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
   361                       "<hints apply=\"true\" runDeclarative=\"true\">\n" +
   362                       "    <settings>\n" +
   363                       "        <test1.hint enabled=\"true\"/>\n" +
   364                       "    </settings>\n" +
   365                       "</hints>\n",
   366                       null,
   367                       "--config-file",
   368                       "${workdir}/settings.xml",
   369                       "--source",
   370                       "1.6");
   371     }
   372 
   373     public void testSourcePath() throws Exception {
   374         String golden =
   375             "package test;\n" +
   376             "public class Test {\n" +
   377             "    private void test() {\n" +
   378             "        String s = test2.Test2.C;\n" +
   379             "    }\n" +
   380             "}\n";
   381 
   382         doRunCompiler(golden,
   383                       null,
   384                       null,
   385                       "src/test/Test.java",
   386                       "package test;\n" +
   387                       "public class Test {\n" +
   388                       "    private void test() {\n" +
   389                       "        String s = test2.Test2.C.intern();\n" +
   390                       "    }\n" +
   391                       "}\n",
   392                       "src/test2/Test2.java",
   393                       "package test2;\n" +
   394                       "public class Test2 {\n" +
   395                       "    public static final String C = \"a\";\n" +
   396                       "}\n",
   397                       null,
   398                       DONT_APPEND_PATH,
   399                       "--apply",
   400                       "--hint",
   401                       "String.intern() called on constant",
   402                       "--sourcepath",
   403                       "${workdir}/src",
   404                       "${workdir}/src/test");
   405     }
   406 
   407     public void testWarningsAreErrors() throws Exception {
   408         String code =
   409             "package test;\n" +
   410             "public class Test {\n" +
   411             "    private void test(java.util.Collection c) {\n" +
   412             "        boolean b1 = c.size() == 0;\n" +
   413             "\tboolean b2 = c.size() == 0;\n" +
   414             "    }\n" +
   415             "}\n";
   416 
   417         doRunCompiler(equivalentValidator(code),
   418                       equivalentValidator(
   419                           "${workdir}/src/test/Test.java:4: warning: [Usage_of_size_equals_0] Usage of .size() == 0 can be replaced with .isEmpty()\n" +
   420                           "        boolean b1 = c.size() == 0;\n" +
   421                           "                     ^\n" +
   422                           "${workdir}/src/test/Test.java:5: warning: [Usage_of_size_equals_0] Usage of .size() == 0 can be replaced with .isEmpty()\n" +
   423                           "\tboolean b2 = c.size() == 0;\n" +
   424                           "\t             ^\n"
   425                       ),
   426                       equivalentValidator(null),
   427                       1,
   428                       "src/test/Test.java",
   429                       code,
   430                       null,
   431                       "--hint",
   432                       "Usage of .size() == 0",
   433                       "--no-apply",
   434                       "--fail-on-warnings");
   435     }
   436 
   437     public void testGroups() throws Exception {
   438         doRunCompiler(null,
   439                       "${workdir}/src1/test/Test.java:4: warning: [test] test\n" +
   440                       "        boolean b1 = c.size() == 0;\n" +
   441                       "                     ^\n" +
   442                       "${workdir}/src2/test/Test.java:5: warning: [test] test\n" +
   443                       "        boolean b2 = c.size() != 0;\n" +
   444                       "                     ^\n",
   445                       null,
   446                       "cp1/META-INF/upgrade/test.hint",
   447                       "$coll.size() == 0 :: $coll instanceof java.util.Collection;;",
   448                       "src1/test/Test.java",
   449                       "package test;\n" +
   450                       "public class Test {\n" +
   451                       "    private void test(java.util.Collection c) {\n" +
   452                       "        boolean b1 = c.size() == 0;\n" +
   453                       "        boolean b2 = c.size() != 0;\n" +
   454                       "    }\n" +
   455                       "}\n",
   456                       "cp2/META-INF/upgrade/test.hint",
   457                       "$coll.size() != 0 :: $coll instanceof java.util.Collection;;",
   458                       "src2/test/Test.java",
   459                       "package test;\n" +
   460                       "public class Test {\n" +
   461                       "    private void test(java.util.Collection c) {\n" +
   462                       "        boolean b1 = c.size() == 0;\n" +
   463                       "        boolean b2 = c.size() != 0;\n" +
   464                       "    }\n" +
   465                       "}\n",
   466                       null,
   467                       DONT_APPEND_PATH,
   468                       "--group",
   469                       "--classpath ${workdir}/cp1 ${workdir}/src1",
   470                       "--group",
   471                       "--classpath ${workdir}/cp2 ${workdir}/src2");
   472     }
   473 
   474     public void testGroupsList() throws Exception {
   475         doRunCompiler(null,
   476                       new Validator() {
   477                           @Override public void validate(String content) {
   478                               assertTrue("Missing expected content, actual content: " + content, content.contains("test\n"));
   479                           }
   480                       },
   481                       null,
   482                       "cp1/META-INF/upgrade/test.hint",
   483                       "$coll.size() == 0 :: $coll instanceof java.util.Collection;;",
   484                       "src1/test/Test.java",
   485                       "\n",
   486                       "cp2/META-INF/upgrade/test.hint",
   487                       "$coll.size() != 0 :: $coll instanceof java.util.Collection;;",
   488                       "src2/test/Test.java",
   489                       "\n",
   490                       null,
   491                       DONT_APPEND_PATH,
   492                       "--group",
   493                       "--classpath ${workdir}/cp1 ${workdir}/src1",
   494                       "--group",
   495                       "--classpath ${workdir}/cp2 ${workdir}/src2",
   496                       "--list");
   497     }
   498 
   499     public void testNoHintsFoundWithGroups() throws Exception {
   500         doRunCompiler("package test;\n" +
   501                       "public class Test {\n" +
   502                       "    private void test(java.util.Collection c) {\n" +
   503                       "        boolean b1 = c.isEmpty();\n" +
   504                       "        boolean b2 = c.size() != 0;\n" +
   505                       "    }\n" +
   506                       "}\n",
   507                       "",
   508                       null,
   509                       "cp/META-INF/upgrade/test.hint",
   510                       "$coll.size() == 0 :: $coll instanceof java.util.Collection\n" +
   511                       "=>\n" +
   512                       "$coll.isEmpty()\n" +
   513                       ";;",
   514                       "src/test/Test.java",
   515                       "package test;\n" +
   516                       "public class Test {\n" +
   517                       "    private void test(java.util.Collection c) {\n" +
   518                       "        boolean b1 = c.size() == 0;\n" +
   519                       "        boolean b2 = c.size() != 0;\n" +
   520                       "    }\n" +
   521                       "}\n",
   522                       "src2/test/Test.java",
   523                       "package test;\n" +
   524                       "public class Test {\n" +
   525                       "    private void test(java.util.Collection c) {\n" +
   526                       "        boolean b1 = c.size() == 0;\n" +
   527                       "        boolean b2 = c.size() != 0;\n" +
   528                       "    }\n" +
   529                       "}\n",
   530                       null,
   531                       DONT_APPEND_PATH,
   532                       "--group",
   533                       "--classpath ${workdir}/cp ${workdir}/src",
   534                       "--group",
   535                       "${workdir}/src2",
   536                       "--apply");
   537     }
   538 
   539     public void testGroupsParamEscape() throws Exception {
   540         assertEquals(Arrays.asList("a b", "a\\b"),
   541                      Arrays.asList(Main.splitGroupArg("a\\ b a\\\\b")));
   542     }
   543 
   544     private static final String DONT_APPEND_PATH = new String("DONT_APPEND_PATH");
   545 
   546     private void doRunCompiler(String golden, String stdOut, String stdErr, String... fileContentAndExtraOptions) throws Exception {
   547         doRunCompiler(equivalentValidator(golden), equivalentValidator(stdOut), equivalentValidator(stdErr), fileContentAndExtraOptions);
   548     }
   549 
   550     private void doRunCompiler(Validator fileContentValidator, Validator stdOutValidator, Validator stdErrValidator, String... fileContentAndExtraOptions) throws Exception {
   551         doRunCompiler(fileContentValidator, stdOutValidator, stdErrValidator, 0, fileContentAndExtraOptions);
   552     }
   553 
   554     private void doRunCompiler(Validator fileContentValidator, Validator stdOutValidator, Validator stdErrValidator, int exitcode, String... fileContentAndExtraOptions) throws Exception {
   555         List<String> fileAndContent = new LinkedList<String>();
   556         List<String> extraOptions = new LinkedList<String>();
   557         List<String> fileContentAndExtraOptionsList = Arrays.asList(fileContentAndExtraOptions);
   558         int nullPos = fileContentAndExtraOptionsList.indexOf(null);
   559 
   560         if (nullPos == (-1)) {
   561             fileAndContent = fileContentAndExtraOptionsList;
   562             extraOptions = Collections.emptyList();
   563         } else {
   564             fileAndContent = fileContentAndExtraOptionsList.subList(0, nullPos);
   565             extraOptions = fileContentAndExtraOptionsList.subList(nullPos + 1, fileContentAndExtraOptionsList.size());
   566         }
   567 
   568         assertTrue(fileAndContent.size() % 2 == 0);
   569 
   570         clearWorkDir();
   571 
   572         for (int cntr = 0; cntr < fileAndContent.size(); cntr += 2) {
   573             File target = new File(getWorkDir(), fileAndContent.get(cntr));
   574 
   575             target.getParentFile().mkdirs();
   576             
   577             TestUtilities.copyStringToFile(target, fileAndContent.get(cntr + 1));
   578         }
   579 
   580         File wd = getWorkDir();
   581         File source = new File(wd, "src/test/Test.java");
   582 
   583         List<String> options = new LinkedList<String>();
   584         boolean appendPath = true;
   585 
   586         options.add("--cache");
   587         options.add("/tmp/cachex");
   588         for (String extraOption : extraOptions) {
   589             if (extraOption == DONT_APPEND_PATH) {
   590                 appendPath = false;
   591                 continue;
   592             }
   593             options.add(extraOption.replace("${workdir}", wd.getAbsolutePath()));
   594         }
   595 
   596         if (appendPath)
   597             options.add(wd.getAbsolutePath());
   598 
   599         String[] output = new String[2];
   600 
   601         reallyRunCompiler(wd, exitcode, output, options.toArray(new String[0]));
   602 
   603         if (fileContentValidator != null) {
   604             fileContentValidator.validate(TestUtilities.copyFileToString(source));
   605         }
   606         if (stdOutValidator != null) {
   607             stdOutValidator.validate(output[0].replaceAll(Pattern.quote(wd.getAbsolutePath()), Matcher.quoteReplacement("${workdir}")));
   608         }
   609         if (stdErrValidator != null) {
   610             stdErrValidator.validate(output[1].replaceAll(Pattern.quote(wd.getAbsolutePath()), Matcher.quoteReplacement("${workdir}")));
   611         }
   612     }
   613 
   614     protected void reallyRunCompiler(File workDir, int exitcode, String[] output, String... params) throws Exception {
   615         String oldUserDir = System.getProperty("user.dir");
   616 
   617         System.setProperty("user.dir", workDir.getAbsolutePath());
   618         
   619         PrintStream oldOut = System.out;
   620         ByteArrayOutputStream outData = new ByteArrayOutputStream();
   621         System.setOut(new PrintStream(outData, true, "UTF-8"));
   622 
   623         PrintStream oldErr = System.err;
   624         ByteArrayOutputStream errData = new ByteArrayOutputStream();
   625         System.setErr(new PrintStream(errData, true, "UTF-8"));
   626 
   627         try {
   628             assertEquals(exitcode, Main.compile(params));
   629         } finally {
   630             System.setProperty("user.dir", oldUserDir);
   631             System.out.close();
   632             output[0] = new String(outData.toByteArray(), "UTF-8");
   633             System.setOut(oldOut);
   634             System.err.close();
   635             output[1] = new String(errData.toByteArray(), "UTF-8");
   636             System.setErr(oldErr);
   637         }
   638     }
   639 
   640     //verify that the DeclarativeHintsTestBase works:
   641     private static final String CODE_RUN_DECLARATIVE =
   642             "package org.netbeans.modules.jackpot30.cmdline.testtool;\n" +
   643             "\n" +
   644             "import junit.framework.TestSuite;\n" +
   645             "import org.netbeans.modules.java.hints.declarative.test.api.DeclarativeHintsTestBase;\n" +
   646             "\n" +
   647             "public class DoRunTests extends DeclarativeHintsTestBase {\n" +
   648             "\n" +
   649             "    public static TestSuite suite() {\n" +
   650             "        return suite(DoRunTests.class);\n" +
   651             "    }\n" +
   652             "\n" +
   653             "}\n";
   654     public void testRunTest() throws Exception {
   655         clearWorkDir();
   656 
   657         File wd = getWorkDir();
   658         File classes = new File(wd, "classes");
   659 
   660         classes.mkdirs();
   661         TestUtilities.copyStringToFile(new File(classes, "h.hint"), "$1.equals(\"\") :: $1 instanceof java.lang.String => $1.isEmpty();;");
   662 
   663         String test = "%%TestCase pos\n" +
   664                       "package test;\n" +
   665                       "public class Test {{\n" +
   666                       " System.err.println(\"a\".equals(\"\"));\n" +
   667                       "}}\n" +
   668                       "%%=>\n" +
   669                       "package test;\n" +
   670                       "public class Test {{\n" +
   671                       " System.err.println(\"a\".isEmpty());\n" +
   672                       "}}\n" +
   673                       "%%TestCase neg\n" +
   674                       "package test;\n" +
   675                       "public class Test {{\n" +
   676                       " System.err.println(\"a\".equals(\"a\"));\n" +
   677                       "}}\n" +
   678                       "%%=>\n" +
   679                       "package test;\n" +
   680                       "public class Test {{\n" +
   681                       " System.err.println(\"a\".isEmpty());\n" +
   682                       "}}\n";
   683         TestUtilities.copyStringToFile(new File(classes, "h.test"), test);
   684 
   685         List<String> options = Arrays.asList("-d", classes.getAbsolutePath());
   686         List<SourceFO> files = Arrays.asList(new SourceFO("DoRunTests.java", CODE_RUN_DECLARATIVE));
   687 
   688         assertTrue(ToolProvider.getSystemJavaCompiler().getTask(null, null, null, options, null, files).call());
   689 
   690         runAndTest(classes);
   691     }
   692 
   693     private static final class SourceFO extends SimpleJavaFileObject {
   694 
   695         private final String code;
   696         private SourceFO(String name, String code) throws URISyntaxException {
   697             super(new URI("mem:///" + name), Kind.SOURCE);
   698             this.code = code;
   699         }
   700 
   701         @Override
   702         public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   703             return code;
   704         }
   705 
   706     }
   707 
   708     protected void runAndTest(File classes) throws Exception {
   709         ClassLoader cl = new URLClassLoader(new URL[] {classes.toURI().toURL()}, MainTest.class.getClassLoader());
   710         Class<?> doRunTests = Class.forName("org.netbeans.modules.jackpot30.cmdline.testtool.DoRunTests", true, cl);
   711         Result testResult = org.junit.runner.JUnitCore.runClasses(doRunTests);
   712 
   713         assertEquals(1, testResult.getFailureCount());
   714         assertTrue(testResult.getFailures().toString(), testResult.getFailures().get(0).getDescription().getMethodName().endsWith("/h.test/neg"));
   715     }
   716 
   717     private static Validator equivalentValidator(final String expected) {
   718         if (expected == null) return null;
   719 
   720         return new Validator() {
   721             @Override public void validate(String content) {
   722                 assertEquals(expected, content);
   723             }
   724         };
   725     }
   726 
   727     private static interface Validator {
   728         public void validate(String content);
   729     }
   730 }