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