cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/MainTest.java
author jlahoda
Sun, 12 Feb 2017 10:11:52 +0100
changeset 1041 b03a880d538e
parent 1037 55c0650c5969
child 1042 06b3af1708ef
permissions -rw-r--r--
Adding ability to run (custom) tests.
     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 testAutomaticTestRun() throws Exception {
   540         class Config {
   541             private final String commandLineOption;
   542             private final int result;
   543             private final Validator validator;
   544 
   545             public Config(String commandLineOption, int result, Validator validator) {
   546                 this.commandLineOption = commandLineOption;
   547                 this.result = result;
   548                 this.validator = validator;
   549             }
   550 
   551         }
   552         Validator testValidator =
   553                 equivalentValidator("${workdir}/src/META-INF/upgrade/test.test:15: error: [test_failure] Actual results did not match the expected test results. Actual results: []\n" +
   554                                     "%%TestCase pos-neg\n" +
   555                                     "^\n" +
   556                                     "${workdir}/src/META-INF/upgrade/test.test:29: error: [test_failure] Actual results did not match the expected test results. Actual results: [package test;\n" +
   557                                     "public class Test {{\n" +
   558                                     "    private void test(java.util.Collection c) {\n" +
   559                                     "        boolean b1 = c.isEmpty();\n" +
   560                                     "    }\n" +
   561                                     "}}\n" +
   562                                     "]\n" +
   563                                     "%%TestCase neg-neg\n" +
   564                                     "^\n" +
   565                                     "${workdir}/src/test/Test.java:4: warning: [test] test\n" +
   566                                     "        boolean b1 = c.size() == 0;\n" +
   567                                     "                     ^\n");
   568         List<? extends Config> options =
   569                 Arrays.asList(new Config("--run-tests", 1, testValidator),
   570                               new Config(IGNORE, 0, equivalentValidator("${workdir}/src/test/Test.java:4: warning: [test] test\n" +
   571                                                                         "        boolean b1 = c.size() == 0;\n" +
   572                                                                         "                     ^\n")));
   573         for (Config testConfig : options) {
   574             doRunCompiler(equivalentValidator("package test;\n" +
   575                                               "public class Test {\n" +
   576                                               "    private void test(java.util.Collection c) {\n" +
   577                                               "        boolean b1 = c.size() == 0;\n" +
   578                                               "        boolean b2 = c.size() != 0;\n" +
   579                                               "    }\n" +
   580                                               "}\n"),
   581                           testConfig.validator,
   582                           equivalentValidator(""),
   583                           testConfig.result,
   584                           "src/META-INF/upgrade/test.hint",
   585                           "$coll.size() == 0 :: $coll instanceof java.util.Collection\n" +
   586                           "=>\n" +
   587                           "$coll.isEmpty()\n" +
   588                           ";;",
   589                           "src/META-INF/upgrade/test.test",
   590                           "%%TestCase pos-pos\n" +
   591                           "package test;\n" +
   592                           "public class Test {{\n" +
   593                           "    private void test(java.util.Collection c) {\n" +
   594                           "        boolean b1 = c.size() == 0;\n" +
   595                           "    }\n" +
   596                           "}}\n" +
   597                           "%%=>\n" +
   598                           "package test;\n" +
   599                           "public class Test {{\n" +
   600                           "    private void test(java.util.Collection c) {\n" +
   601                           "        boolean b1 = c.isEmpty();\n" +
   602                           "    }\n" +
   603                           "}}\n" +
   604                           "%%TestCase pos-neg\n" +
   605                           "package test;\n" +
   606                           "public class Test {{\n" +
   607                           "    private void test(java.util.Collection c) {\n" +
   608                           "        boolean b1 = c.size() == 0;\n" +
   609                           "    }\n" +
   610                           "}}\n" +
   611                           "%%TestCase neg-pos\n" +
   612                           "package test;\n" +
   613                           "public class Test {{\n" +
   614                           "    private void test(java.util.Collection c) {\n" +
   615                           "        boolean b1 = c.size() != 0;\n" +
   616                           "    }\n" +
   617                           "}}\n" +
   618                           "%%TestCase neg-neg\n" +
   619                           "package test;\n" +
   620                           "public class Test {{\n" +
   621                           "    private void test(java.util.Collection c) {\n" +
   622                           "        boolean b1 = c.size() != 0;\n" +
   623                           "    }\n" +
   624                           "}}\n" +
   625                           "%%=>\n" +
   626                           "package test;\n" +
   627                           "public class Test {{\n" +
   628                           "    private void test(java.util.Collection c) {\n" +
   629                           "        boolean b1 = c.isEmpty();\n" +
   630                           "    }\n" +
   631                           "}}\n",
   632                           "src/test/Test.java",
   633                           "package test;\n" +
   634                           "public class Test {\n" +
   635                           "    private void test(java.util.Collection c) {\n" +
   636                           "        boolean b1 = c.size() == 0;\n" +
   637                           "        boolean b2 = c.size() != 0;\n" +
   638                           "    }\n" +
   639                           "}\n",
   640                           null,
   641                           testConfig.commandLineOption,
   642                           "--sourcepath", "${workdir}/src");
   643         }
   644     }
   645 
   646     public void testGroupsParamEscape() throws Exception {
   647         assertEquals(Arrays.asList("a b", "a\\b"),
   648                      Arrays.asList(Main.splitGroupArg("a\\ b a\\\\b")));
   649     }
   650 
   651     private static final String DONT_APPEND_PATH = new String("DONT_APPEND_PATH");
   652     private static final String IGNORE = new String("IGNORE");
   653 
   654     private void doRunCompiler(String golden, String stdOut, String stdErr, String... fileContentAndExtraOptions) throws Exception {
   655         doRunCompiler(equivalentValidator(golden), equivalentValidator(stdOut), equivalentValidator(stdErr), fileContentAndExtraOptions);
   656     }
   657 
   658     private void doRunCompiler(Validator fileContentValidator, Validator stdOutValidator, Validator stdErrValidator, String... fileContentAndExtraOptions) throws Exception {
   659         doRunCompiler(fileContentValidator, stdOutValidator, stdErrValidator, 0, fileContentAndExtraOptions);
   660     }
   661 
   662     private void doRunCompiler(Validator fileContentValidator, Validator stdOutValidator, Validator stdErrValidator, int exitcode, String... fileContentAndExtraOptions) throws Exception {
   663         List<String> fileAndContent = new LinkedList<String>();
   664         List<String> extraOptions = new LinkedList<String>();
   665         List<String> fileContentAndExtraOptionsList = Arrays.asList(fileContentAndExtraOptions);
   666         int nullPos = fileContentAndExtraOptionsList.indexOf(null);
   667 
   668         if (nullPos == (-1)) {
   669             fileAndContent = fileContentAndExtraOptionsList;
   670             extraOptions = Collections.emptyList();
   671         } else {
   672             fileAndContent = fileContentAndExtraOptionsList.subList(0, nullPos);
   673             extraOptions = fileContentAndExtraOptionsList.subList(nullPos + 1, fileContentAndExtraOptionsList.size());
   674         }
   675 
   676         assertTrue(fileAndContent.size() % 2 == 0);
   677 
   678         clearWorkDir();
   679 
   680         for (int cntr = 0; cntr < fileAndContent.size(); cntr += 2) {
   681             File target = new File(getWorkDir(), fileAndContent.get(cntr));
   682 
   683             target.getParentFile().mkdirs();
   684             
   685             TestUtilities.copyStringToFile(target, fileAndContent.get(cntr + 1));
   686         }
   687 
   688         File wd = getWorkDir();
   689         File source = new File(wd, "src/test/Test.java");
   690 
   691         List<String> options = new LinkedList<String>();
   692         boolean appendPath = true;
   693 
   694         options.add("--cache");
   695         options.add("/tmp/cachex");
   696         for (String extraOption : extraOptions) {
   697             if (extraOption == DONT_APPEND_PATH) {
   698                 appendPath = false;
   699                 continue;
   700             }
   701             if (extraOption == IGNORE) {
   702                 continue;
   703             }
   704             options.add(extraOption.replace("${workdir}", wd.getAbsolutePath()));
   705         }
   706 
   707         if (appendPath)
   708             options.add(wd.getAbsolutePath());
   709 
   710         String[] output = new String[2];
   711 
   712         reallyRunCompiler(wd, exitcode, output, options.toArray(new String[0]));
   713 
   714         if (fileContentValidator != null) {
   715             fileContentValidator.validate(TestUtilities.copyFileToString(source));
   716         }
   717         if (stdOutValidator != null) {
   718             stdOutValidator.validate(output[0].replaceAll(Pattern.quote(wd.getAbsolutePath()), Matcher.quoteReplacement("${workdir}")));
   719         }
   720         if (stdErrValidator != null) {
   721             stdErrValidator.validate(output[1].replaceAll(Pattern.quote(wd.getAbsolutePath()), Matcher.quoteReplacement("${workdir}")));
   722         }
   723     }
   724 
   725     protected void reallyRunCompiler(File workDir, int exitcode, String[] output, String... params) throws Exception {
   726         String oldUserDir = System.getProperty("user.dir");
   727 
   728         System.setProperty("user.dir", workDir.getAbsolutePath());
   729         
   730         PrintStream oldOut = System.out;
   731         ByteArrayOutputStream outData = new ByteArrayOutputStream();
   732         System.setOut(new PrintStream(outData, true, "UTF-8"));
   733 
   734         PrintStream oldErr = System.err;
   735         ByteArrayOutputStream errData = new ByteArrayOutputStream();
   736         System.setErr(new PrintStream(errData, true, "UTF-8"));
   737 
   738         try {
   739             assertEquals(exitcode, Main.compile(params));
   740         } finally {
   741             System.setProperty("user.dir", oldUserDir);
   742             System.out.close();
   743             output[0] = new String(outData.toByteArray(), "UTF-8");
   744             System.setOut(oldOut);
   745             System.err.close();
   746             output[1] = new String(errData.toByteArray(), "UTF-8");
   747             System.setErr(oldErr);
   748         }
   749     }
   750 
   751     //verify that the DeclarativeHintsTestBase works:
   752     private static final String CODE_RUN_DECLARATIVE =
   753             "package org.netbeans.modules.jackpot30.cmdline.testtool;\n" +
   754             "\n" +
   755             "import junit.framework.TestSuite;\n" +
   756             "import org.netbeans.modules.java.hints.declarative.test.api.DeclarativeHintsTestBase;\n" +
   757             "\n" +
   758             "public class DoRunTests extends DeclarativeHintsTestBase {\n" +
   759             "\n" +
   760             "    public static TestSuite suite() {\n" +
   761             "        return suite(DoRunTests.class);\n" +
   762             "    }\n" +
   763             "\n" +
   764             "}\n";
   765     public void testRunTest() throws Exception {
   766         clearWorkDir();
   767 
   768         File wd = getWorkDir();
   769         File classes = new File(wd, "classes");
   770 
   771         classes.mkdirs();
   772         TestUtilities.copyStringToFile(new File(classes, "h.hint"), "$1.equals(\"\") :: $1 instanceof java.lang.String => $1.isEmpty();;");
   773 
   774         String test = "%%TestCase pos\n" +
   775                       "package test;\n" +
   776                       "public class Test {{\n" +
   777                       " System.err.println(\"a\".equals(\"\"));\n" +
   778                       "}}\n" +
   779                       "%%=>\n" +
   780                       "package test;\n" +
   781                       "public class Test {{\n" +
   782                       " System.err.println(\"a\".isEmpty());\n" +
   783                       "}}\n" +
   784                       "%%TestCase neg\n" +
   785                       "package test;\n" +
   786                       "public class Test {{\n" +
   787                       " System.err.println(\"a\".equals(\"a\"));\n" +
   788                       "}}\n" +
   789                       "%%=>\n" +
   790                       "package test;\n" +
   791                       "public class Test {{\n" +
   792                       " System.err.println(\"a\".isEmpty());\n" +
   793                       "}}\n";
   794         TestUtilities.copyStringToFile(new File(classes, "h.test"), test);
   795 
   796         List<String> options = Arrays.asList("-d", classes.getAbsolutePath());
   797         List<SourceFO> files = Arrays.asList(new SourceFO("DoRunTests.java", CODE_RUN_DECLARATIVE));
   798 
   799         assertTrue(ToolProvider.getSystemJavaCompiler().getTask(null, null, null, options, null, files).call());
   800 
   801         runAndTest(classes);
   802     }
   803 
   804     private static final class SourceFO extends SimpleJavaFileObject {
   805 
   806         private final String code;
   807         private SourceFO(String name, String code) throws URISyntaxException {
   808             super(new URI("mem:///" + name), Kind.SOURCE);
   809             this.code = code;
   810         }
   811 
   812         @Override
   813         public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   814             return code;
   815         }
   816 
   817     }
   818 
   819     protected void runAndTest(File classes) throws Exception {
   820         ClassLoader cl = new URLClassLoader(new URL[] {classes.toURI().toURL()}, MainTest.class.getClassLoader());
   821         Class<?> doRunTests = Class.forName("org.netbeans.modules.jackpot30.cmdline.testtool.DoRunTests", true, cl);
   822         Result testResult = org.junit.runner.JUnitCore.runClasses(doRunTests);
   823 
   824         assertEquals(1, testResult.getFailureCount());
   825         assertTrue(testResult.getFailures().toString(), testResult.getFailures().get(0).getDescription().getMethodName().endsWith("/h.test/neg"));
   826     }
   827 
   828     private static Validator equivalentValidator(final String expected) {
   829         if (expected == null) return null;
   830 
   831         return new Validator() {
   832             @Override public void validate(String content) {
   833                 assertEquals(expected, content);
   834             }
   835         };
   836     }
   837 
   838     private static interface Validator {
   839         public void validate(String content);
   840     }
   841 }