cmdline/tool/src/org/netbeans/modules/jackpot30/cmdline/Main.java
changeset 1041 b03a880d538e
parent 1016 02ad9fe4588c
     1.1 --- a/cmdline/tool/src/org/netbeans/modules/jackpot30/cmdline/Main.java	Sun Sep 04 16:57:57 2016 +0200
     1.2 +++ b/cmdline/tool/src/org/netbeans/modules/jackpot30/cmdline/Main.java	Sun Feb 12 10:11:52 2017 +0100
     1.3 @@ -54,6 +54,7 @@
     1.4  import java.util.ArrayList;
     1.5  import java.util.Arrays;
     1.6  import java.util.Collection;
     1.7 +import java.util.Collections;
     1.8  import java.util.HashMap;
     1.9  import java.util.HashSet;
    1.10  import java.util.Iterator;
    1.11 @@ -87,6 +88,10 @@
    1.12  import org.netbeans.core.startup.MainLookup;
    1.13  import org.netbeans.modules.jackpot30.cmdline.lib.Utils;
    1.14  import org.netbeans.modules.jackpot30.ui.settings.XMLHintPreferences;
    1.15 +import org.netbeans.modules.java.hints.declarative.DeclarativeHintRegistry;
    1.16 +import org.netbeans.modules.java.hints.declarative.test.TestParser;
    1.17 +import org.netbeans.modules.java.hints.declarative.test.TestParser.TestCase;
    1.18 +import org.netbeans.modules.java.hints.declarative.test.TestPerformer;
    1.19  import org.netbeans.modules.java.hints.jackpot.spi.PatternConvertor;
    1.20  import org.netbeans.modules.java.hints.providers.spi.HintDescription;
    1.21  import org.netbeans.modules.java.hints.providers.spi.HintMetadata;
    1.22 @@ -108,6 +113,8 @@
    1.23  import org.netbeans.modules.parsing.impl.indexing.CacheFolder;
    1.24  import org.netbeans.modules.parsing.impl.indexing.RepositoryUpdater;
    1.25  import org.netbeans.spi.editor.hints.ErrorDescription;
    1.26 +import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
    1.27 +import org.netbeans.spi.editor.hints.Fix;
    1.28  import org.netbeans.spi.editor.hints.Severity;
    1.29  import org.netbeans.spi.java.classpath.ClassPathProvider;
    1.30  import org.netbeans.spi.java.classpath.support.ClassPathSupport;
    1.31 @@ -133,6 +140,7 @@
    1.32      private static final String OPTION_APPLY = "apply";
    1.33      private static final String OPTION_NO_APPLY = "no-apply";
    1.34      private static final String OPTION_FAIL_ON_WARNINGS = "fail-on-warnings";
    1.35 +    private static final String RUN_TESTS = "run-tests";
    1.36      private static final String SOURCE_LEVEL_DEFAULT = "1.7";
    1.37      private static final String ACCEPTABLE_SOURCE_LEVEL_PATTERN = "(1\\.)?[2-9][0-9]*";
    1.38      
    1.39 @@ -162,6 +170,7 @@
    1.40          parser.accepts(OPTION_APPLY, "apply changes");
    1.41          parser.accepts("show-gui", "show configuration dialog");
    1.42          parser.accepts(OPTION_FAIL_ON_WARNINGS, "fail when warnings are detected");
    1.43 +        parser.accepts(RUN_TESTS, "run tests for declarative rules that were used");
    1.44  
    1.45          OptionSet parsed;
    1.46  
    1.47 @@ -286,6 +295,7 @@
    1.48              Preferences hintSettingsPreferences;
    1.49              boolean apply;
    1.50              boolean runDeclarative;
    1.51 +            boolean runDeclarativeTests;
    1.52  
    1.53              if (parsed.has(configFile)) {
    1.54                  Preferences settingsFromConfigFile;
    1.55 @@ -293,6 +303,7 @@
    1.56                  hintSettingsPreferences = settingsFromConfigFile.node("settings");
    1.57                  apply = settingsFromConfigFile.getBoolean("apply", false);
    1.58                  runDeclarative = settingsFromConfigFile.getBoolean("runDeclarative", true);
    1.59 +                runDeclarativeTests = settingsFromConfigFile.getBoolean("runDeclarativeTests", false);
    1.60                  if (parsed.has(hint)) {
    1.61                      System.err.println("cannot specify --hint and --config-file together");
    1.62                      return 1;
    1.63 @@ -304,6 +315,7 @@
    1.64                  hintSettingsPreferences = null;
    1.65                  apply = false;
    1.66                  runDeclarative = true;
    1.67 +                runDeclarativeTests = parsed.has(RUN_TESTS);
    1.68              }
    1.69  
    1.70              if (parsed.has(config) && !parsed.has(hint)) {
    1.71 @@ -320,7 +332,7 @@
    1.72              GroupResult result = GroupResult.NOTHING_TO_DO;
    1.73  
    1.74              try (Writer outS = parsed.has(out) ? new BufferedWriter(new OutputStreamWriter(new FileOutputStream(parsed.valueOf(out)))) : null) {
    1.75 -                GlobalConfiguration globalConfig = new GlobalConfiguration(hintSettingsPreferences, apply, runDeclarative, parsed.valueOf(hint), parsed.valueOf(hintFile), outS, parsed.has(OPTION_FAIL_ON_WARNINGS));
    1.76 +                GlobalConfiguration globalConfig = new GlobalConfiguration(hintSettingsPreferences, apply, runDeclarative, runDeclarativeTests, parsed.valueOf(hint), parsed.valueOf(hintFile), outS, parsed.has(OPTION_FAIL_ON_WARNINGS));
    1.77  
    1.78                  for (RootConfiguration groupConfig : groups) {
    1.79                      result = result.join(handleGroup(groupConfig, progress, globalConfig, parsed.valuesOf(config)));
    1.80 @@ -405,6 +417,8 @@
    1.81              return GroupResult.NOTHING_TO_DO;
    1.82          }
    1.83  
    1.84 +        WarningsAndErrors wae = new WarningsAndErrors();
    1.85 +
    1.86          ProgressHandleWrapper progress = w.startNextPartWithEmbedding(1);
    1.87          Preferences settings = globalConfig.configurationPreferences != null ? globalConfig.configurationPreferences : new MemoryPreferences();
    1.88          HintsSettings hintSettings = HintsSettings.createPreferencesBasedHintsSettings(settings, false, null);
    1.89 @@ -420,6 +434,46 @@
    1.90              }
    1.91          } else {
    1.92              hints = readHints(rootConfiguration.sourceCP, rootConfiguration.binaryCP, hintSettings, settings, globalConfig.runDeclarative);
    1.93 +            if (globalConfig.runDeclarativeTests) {
    1.94 +                Set<String> enabledHints = new HashSet<>();
    1.95 +                for (HintDescription desc : hints) {
    1.96 +                    enabledHints.add(desc.getMetadata().id);
    1.97 +                }
    1.98 +                ClassPath combined = ClassPathSupport.createProxyClassPath(rootConfiguration.sourceCP, rootConfiguration.binaryCP);
    1.99 +                Map<FileObject, FileObject> testFiles = new HashMap<>();
   1.100 +                for (FileObject upgrade : combined.findAllResources("META-INF/upgrade")) {
   1.101 +                    for (FileObject c : upgrade.getChildren()) {
   1.102 +                        if (c.getExt().equals("test")) {
   1.103 +                            FileObject hintFile = FileUtil.findBrother(c, "hint");
   1.104 +
   1.105 +                            for (HintMetadata hm : DeclarativeHintRegistry.parseHintFile(hintFile).keySet()) {
   1.106 +                                if (enabledHints.contains(hm.id)) {
   1.107 +                                    testFiles.put(c, hintFile);
   1.108 +                                    break;
   1.109 +                                }
   1.110 +                            }
   1.111 +                        }
   1.112 +                    }
   1.113 +                }
   1.114 +                for (Entry<FileObject, FileObject> e : testFiles.entrySet()) {
   1.115 +                    TestCase[] testCases = TestParser.parse(e.getKey().asText()); //XXX: encoding
   1.116 +                    try {
   1.117 +                        Map<TestCase, Collection<String>> testResult = TestPerformer.performTest(e.getValue(), e.getKey(), testCases, new AtomicBoolean());
   1.118 +                        for (TestCase tc : testCases) {
   1.119 +                            List<String> expected = Arrays.asList(tc.getResults());
   1.120 +                            List<String> actual = new ArrayList<>(testResult.get(tc));
   1.121 +                            if (!expected.equals(actual)) {
   1.122 +                                int pos = tc.getTestCaseStart();
   1.123 +                                String id = "test-failure";
   1.124 +                                ErrorDescription ed = ErrorDescriptionFactory.createErrorDescription(id, Severity.ERROR, "Actual results did not match the expected test results. Actual results: " + expected, null, ErrorDescriptionFactory.lazyListForFixes(Collections.<Fix>emptyList()), e.getKey(), pos, pos);
   1.125 +                                print(ed, wae, Collections.singletonMap(id, id));
   1.126 +                            }
   1.127 +                        }
   1.128 +                    } catch (Exception ex) {
   1.129 +                        ex.printStackTrace();
   1.130 +                    }
   1.131 +                }
   1.132 +            }
   1.133          }
   1.134  
   1.135          if (config != null && !config.isEmpty()) {
   1.136 @@ -478,10 +532,8 @@
   1.137              if (globalConfig.apply) {
   1.138                  apply(hints, rootConfiguration.rootFolders.toArray(new Folder[0]), progress, hintSettings, globalConfig.out);
   1.139  
   1.140 -                return GroupResult.SUCCESS;
   1.141 +                return GroupResult.SUCCESS; //TODO: WarningsAndErrors?
   1.142              } else {
   1.143 -                WarningsAndErrors wae = new WarningsAndErrors();
   1.144 -
   1.145                  findOccurrences(hints, rootConfiguration.rootFolders.toArray(new Folder[0]), progress, hintSettings, wae);
   1.146  
   1.147                  if (wae.errors != 0 || (wae.warnings != 0 && globalConfig.failOnWarnings)) {
   1.148 @@ -870,15 +922,17 @@
   1.149          private final Preferences configurationPreferences;
   1.150          private final boolean apply;
   1.151          private final boolean runDeclarative;
   1.152 +        private final boolean runDeclarativeTests;
   1.153          private final String hint;
   1.154          private final File hintFile;
   1.155          private final Writer out;
   1.156          private final boolean failOnWarnings;
   1.157  
   1.158 -        public GlobalConfiguration(Preferences configurationPreferences, boolean apply, boolean runDeclarative, String hint, File hintFile, Writer out, boolean failOnWarnings) {
   1.159 +        public GlobalConfiguration(Preferences configurationPreferences, boolean apply, boolean runDeclarative, boolean runDeclarativeTests, String hint, File hintFile, Writer out, boolean failOnWarnings) {
   1.160              this.configurationPreferences = configurationPreferences;
   1.161              this.apply = apply;
   1.162              this.runDeclarative = runDeclarative;
   1.163 +            this.runDeclarativeTests = runDeclarativeTests;
   1.164              this.hint = hint;
   1.165              this.hintFile = hintFile;
   1.166              this.out = out;