Fixing --list with groups; tweaking progress.
authorjlahoda
Sun, 04 Sep 2016 16:45:52 +0200
changeset 10156b6e10c60a02
parent 1014 516c317376b6
child 1016 02ad9fe4588c
Fixing --list with groups; tweaking progress.
cmdline/tool/src/org/netbeans/modules/jackpot30/cmdline/Main.java
cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/MainTest.java
     1.1 --- a/cmdline/tool/src/org/netbeans/modules/jackpot30/cmdline/Main.java	Sun Oct 11 22:20:09 2015 +0200
     1.2 +++ b/cmdline/tool/src/org/netbeans/modules/jackpot30/cmdline/Main.java	Sun Sep 04 16:45:52 2016 +0200
     1.3 @@ -55,6 +55,7 @@
     1.4  import java.util.Arrays;
     1.5  import java.util.Collection;
     1.6  import java.util.HashMap;
     1.7 +import java.util.HashSet;
     1.8  import java.util.Iterator;
     1.9  import java.util.LinkedList;
    1.10  import java.util.List;
    1.11 @@ -117,6 +118,7 @@
    1.12  import org.openide.filesystems.FileUtil;
    1.13  import org.openide.util.Exceptions;
    1.14  import org.openide.util.Lookup;
    1.15 +import org.openide.util.Pair;
    1.16  import org.openide.util.RequestProcessor;
    1.17  import org.openide.util.lookup.Lookups;
    1.18  import org.openide.util.lookup.ProxyLookup;
    1.19 @@ -193,7 +195,17 @@
    1.20              }
    1.21          }
    1.22  
    1.23 -        final RootConfiguration globalRootConfiguration = new RootConfiguration(parsed, globalGroupOptions);
    1.24 +        final List<RootConfiguration> groups = new ArrayList<>();
    1.25 +
    1.26 +        groups.add(new RootConfiguration(parsed, globalGroupOptions));
    1.27 +
    1.28 +        for (String groupValue : parsed.valuesOf(group)) {
    1.29 +            OptionParser groupParser = new OptionParser();
    1.30 +            GroupOptions groupOptions = setupGroupParser(groupParser);
    1.31 +            OptionSet parsedGroup = groupParser.parse(splitGroupArg(groupValue));
    1.32 +
    1.33 +            groups.add(new RootConfiguration(parsedGroup, groupOptions));
    1.34 +        }
    1.35  
    1.36          if (parsed.has("show-gui")) {
    1.37              if (parsed.has(configFile)) {
    1.38 @@ -202,7 +214,8 @@
    1.39                      SwingUtilities.invokeAndWait(new Runnable() {
    1.40                          @Override public void run() {
    1.41                              try {
    1.42 -                                showGUICustomizer(settingsFile, globalRootConfiguration.binaryCP, globalRootConfiguration.sourceCP);
    1.43 +                                Pair<ClassPath, ClassPath> sourceAndBinaryCP = jointSourceAndBinaryCP(groups);
    1.44 +                                showGUICustomizer(settingsFile, sourceAndBinaryCP.second(), sourceAndBinaryCP.first());
    1.45                              } catch (IOException ex) {
    1.46                                  Exceptions.printStackTrace(ex);
    1.47                              } catch (BackingStoreException ex) {
    1.48 @@ -256,11 +269,19 @@
    1.49              RepositoryUpdater.getDefault().start(false);
    1.50  
    1.51              if (parsed.has("list")) {
    1.52 -                printHints(globalRootConfiguration.sourceCP, globalRootConfiguration.binaryCP);
    1.53 +                Pair<ClassPath, ClassPath> sourceAndBinaryCP = jointSourceAndBinaryCP(groups);
    1.54 +                printHints(sourceAndBinaryCP.first(),
    1.55 +                           sourceAndBinaryCP.second());
    1.56                  return 0;
    1.57              }
    1.58  
    1.59 -            int totalGroups = parsed.valuesOf(group).size() + (!globalRootConfiguration.rootFolders.isEmpty() ? 1 : 0);
    1.60 +            int totalGroups = 0;
    1.61 +
    1.62 +            for (RootConfiguration groupConfig : groups) {
    1.63 +                if (!groupConfig.rootFolders.isEmpty()) totalGroups++;
    1.64 +            }
    1.65 +
    1.66 +            System.err.println("totalGroups=" + totalGroups);
    1.67  
    1.68              ProgressHandleWrapper progress = parsed.has("progress") ? new ProgressHandleWrapper(new ConsoleProgressHandleAbstraction(), ProgressHandleWrapper.prepareParts(totalGroups)) : new ProgressHandleWrapper(1);
    1.69  
    1.70 @@ -303,13 +324,8 @@
    1.71              try (Writer outS = parsed.has(out) ? new BufferedWriter(new OutputStreamWriter(new FileOutputStream(parsed.valueOf(out)))) : null) {
    1.72                  GlobalConfiguration globalConfig = new GlobalConfiguration(hintSettingsPreferences, apply, runDeclarative, parsed.valueOf(hint), parsed.valueOf(hintFile), outS, parsed.has(OPTION_FAIL_ON_WARNINGS));
    1.73  
    1.74 -                result = result.join(handleGroup(parsed, globalGroupOptions, progress, globalConfig, parsed.valuesOf(config)));
    1.75 -
    1.76 -                for (String groupValue : parsed.valuesOf(group)) {
    1.77 -                    OptionParser groupParser = new OptionParser();
    1.78 -                    GroupOptions groupOptions = setupGroupParser(groupParser);
    1.79 -                    OptionSet parsedGroup = groupParser.parse(splitGroupArg(groupValue));
    1.80 -                    result = result.join(handleGroup(parsedGroup, groupOptions, progress, globalConfig, parsed.valuesOf(config)));
    1.81 +                for (RootConfiguration groupConfig : groups) {
    1.82 +                    result = result.join(handleGroup(groupConfig, progress, globalConfig, parsed.valuesOf(config)));
    1.83                  }
    1.84              }
    1.85  
    1.86 @@ -336,6 +352,17 @@
    1.87          }
    1.88      }
    1.89  
    1.90 +    private static Pair<ClassPath, ClassPath> jointSourceAndBinaryCP(List<RootConfiguration> groups) {
    1.91 +        Set<FileObject> sourceRoots = new HashSet<>();
    1.92 +        Set<FileObject> binaryRoots = new HashSet<>();
    1.93 +        for (RootConfiguration groupConfig : groups) {
    1.94 +            sourceRoots.addAll(Arrays.asList(groupConfig.sourceCP.getRoots()));
    1.95 +            binaryRoots.addAll(Arrays.asList(groupConfig.binaryCP.getRoots()));
    1.96 +        }
    1.97 +        return Pair.of(ClassPathSupport.createClassPath(sourceRoots.toArray(new FileObject[0])),
    1.98 +                       ClassPathSupport.createClassPath(binaryRoots.toArray(new FileObject[0])));
    1.99 +    }
   1.100 +
   1.101      private static GroupOptions setupGroupParser(OptionParser parser) {
   1.102          return new GroupOptions(parser.accepts("classpath", "classpath").withRequiredArg().withValuesSeparatedBy(File.pathSeparatorChar).ofType(File.class),
   1.103                                  parser.accepts("bootclasspath", "bootclasspath").withRequiredArg().withValuesSeparatedBy(File.pathSeparatorChar).ofType(File.class),
   1.104 @@ -368,16 +395,14 @@
   1.105          return result;
   1.106      }
   1.107  
   1.108 -    private static GroupResult handleGroup(OptionSet parsed, GroupOptions groupOptions, ProgressHandleWrapper w, GlobalConfiguration globalConfig, List<String> config) throws IOException {
   1.109 -        ProgressHandleWrapper progress = w.startNextPartWithEmbedding(1);
   1.110 +    private static GroupResult handleGroup(RootConfiguration rootConfiguration, ProgressHandleWrapper w, GlobalConfiguration globalConfig, List<String> config) throws IOException {
   1.111          Iterable<? extends HintDescription> hints;
   1.112  
   1.113 -        RootConfiguration rootConfiguration = new RootConfiguration(parsed, groupOptions);
   1.114 -
   1.115          if (rootConfiguration.rootFolders.isEmpty()) {
   1.116              return GroupResult.NOTHING_TO_DO;
   1.117          }
   1.118  
   1.119 +        ProgressHandleWrapper progress = w.startNextPartWithEmbedding(1);
   1.120          Preferences settings = globalConfig.configurationPreferences != null ? globalConfig.configurationPreferences : new MemoryPreferences();
   1.121          HintsSettings hintSettings = HintsSettings.createPreferencesBasedHintsSettings(settings, false, null);
   1.122  
   1.123 @@ -425,7 +450,7 @@
   1.124              }
   1.125          }
   1.126  
   1.127 -        String sourceLevel = parsed.valueOf(groupOptions.source);
   1.128 +        String sourceLevel = rootConfiguration.sourceLevel;
   1.129  
   1.130          if (!Pattern.compile(ACCEPTABLE_SOURCE_LEVEL_PATTERN).matcher(sourceLevel).matches()) {
   1.131              System.err.println("unrecognized source level specification: " + sourceLevel);
   1.132 @@ -806,6 +831,7 @@
   1.133          private final ClassPath compileCP;
   1.134          private final ClassPath sourceCP;
   1.135          private final ClassPath binaryCP;
   1.136 +        private final String    sourceLevel;
   1.137  
   1.138          public RootConfiguration(OptionSet parsed, GroupOptions groupOptions) throws IOException {
   1.139              this.rootFolders = new ArrayList<>();
   1.140 @@ -826,6 +852,7 @@
   1.141              this.compileCP = createClassPath(parsed.has(groupOptions.classpath) ? parsed.valuesOf(groupOptions.classpath) : null, ClassPath.EMPTY);
   1.142              this.sourceCP = createClassPath(parsed.has(groupOptions.sourcepath) ? parsed.valuesOf(groupOptions.sourcepath) : null, ClassPathSupport.createClassPath(roots.toArray(new FileObject[0])));
   1.143              this.binaryCP = ClassPathSupport.createProxyClassPath(bootCP, compileCP);
   1.144 +            this.sourceLevel = parsed.valueOf(groupOptions.source);
   1.145          }
   1.146  
   1.147      }
   1.148 @@ -914,7 +941,7 @@
   1.149  
   1.150      private static final class ConsoleProgressHandleAbstraction implements ProgressHandleAbstraction {
   1.151  
   1.152 -        private final int width = 80;
   1.153 +        private final int width = 80 - 2;
   1.154  
   1.155          private int total = -1;
   1.156          private int current = 0;
   1.157 @@ -940,27 +967,33 @@
   1.158          }
   1.159  
   1.160          @Override
   1.161 -        public void finish() {
   1.162 -            System.out.println();
   1.163 +        public synchronized void finish() {
   1.164 +            current = total;
   1.165 +            RequestProcessor.getDefault().post(new Runnable() {
   1.166 +                @Override
   1.167 +                public void run() {
   1.168 +                    doUpdate(false);
   1.169 +                    System.out.println();
   1.170 +                }
   1.171 +            });
   1.172          }
   1.173  
   1.174          private void update() {
   1.175              RequestProcessor.getDefault().post(new Runnable() {
   1.176 -
   1.177                  @Override
   1.178                  public void run() {
   1.179 -                    doUpdate();
   1.180 +                    doUpdate(true);
   1.181                  }
   1.182              });
   1.183          }
   1.184  
   1.185          private int currentShownDone = -1;
   1.186  
   1.187 -        private void doUpdate() {
   1.188 +        private void doUpdate(boolean moveCaret) {
   1.189              int done;
   1.190  
   1.191              synchronized(this) {
   1.192 -                done = (int) ((((double) width - 2) / total) * current);
   1.193 +                done = (int) ((((double) width) / total) * current);
   1.194  
   1.195                  if (done == currentShownDone) {
   1.196                      return;
   1.197 @@ -983,7 +1016,10 @@
   1.198                  pw.print(" ");
   1.199              }
   1.200  
   1.201 -            pw.print("]\r");
   1.202 +            pw.print("]");
   1.203 +
   1.204 +            if (moveCaret)
   1.205 +                pw.print("\r");
   1.206          }
   1.207  
   1.208      }
     2.1 --- a/cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/MainTest.java	Sun Oct 11 22:20:09 2015 +0200
     2.2 +++ b/cmdline/tool/test/unit/src/org/netbeans/modules/jackpot30/cmdline/MainTest.java	Sun Sep 04 16:45:52 2016 +0200
     2.3 @@ -409,14 +409,16 @@
     2.4              "    }\n" +
     2.5              "}\n";
     2.6  
     2.7 -        doRunCompiler(code,
     2.8 -                      "${workdir}/src/test/Test.java:4: warning: [Usage_of_size_equals_0] Usage of .size() == 0 can be replaced with .isEmpty()\n" +
     2.9 -                      "        boolean b1 = c.size() == 0;\n" +
    2.10 -                      "                     ^\n" +
    2.11 -                      "${workdir}/src/test/Test.java:5: warning: [Usage_of_size_equals_0] Usage of .size() == 0 can be replaced with .isEmpty()\n" +
    2.12 -                      "\tboolean b2 = c.size() == 0;\n" +
    2.13 -                      "\t             ^\n",
    2.14 -                      null,
    2.15 +        doRunCompiler(equivalentValidator(code),
    2.16 +                      equivalentValidator(
    2.17 +                          "${workdir}/src/test/Test.java:4: warning: [Usage_of_size_equals_0] Usage of .size() == 0 can be replaced with .isEmpty()\n" +
    2.18 +                          "        boolean b1 = c.size() == 0;\n" +
    2.19 +                          "                     ^\n" +
    2.20 +                          "${workdir}/src/test/Test.java:5: warning: [Usage_of_size_equals_0] Usage of .size() == 0 can be replaced with .isEmpty()\n" +
    2.21 +                          "\tboolean b2 = c.size() == 0;\n" +
    2.22 +                          "\t             ^\n"
    2.23 +                      ),
    2.24 +                      equivalentValidator(null),
    2.25                        1,
    2.26                        "src/test/Test.java",
    2.27                        code,
    2.28 @@ -464,6 +466,31 @@
    2.29                        "--classpath ${workdir}/cp2 ${workdir}/src2");
    2.30      }
    2.31  
    2.32 +    public void testGroupsList() throws Exception {
    2.33 +        doRunCompiler(null,
    2.34 +                      new Validator() {
    2.35 +                          @Override public void validate(String content) {
    2.36 +                              assertTrue("Missing expected content, actual content: " + content, content.contains("test\n"));
    2.37 +                          }
    2.38 +                      },
    2.39 +                      null,
    2.40 +                      "cp1/META-INF/upgrade/test.hint",
    2.41 +                      "$coll.size() == 0 :: $coll instanceof java.util.Collection;;",
    2.42 +                      "src1/test/Test.java",
    2.43 +                      "\n",
    2.44 +                      "cp2/META-INF/upgrade/test.hint",
    2.45 +                      "$coll.size() != 0 :: $coll instanceof java.util.Collection;;",
    2.46 +                      "src2/test/Test.java",
    2.47 +                      "\n",
    2.48 +                      null,
    2.49 +                      DONT_APPEND_PATH,
    2.50 +                      "--group",
    2.51 +                      "--classpath ${workdir}/cp1 ${workdir}/src1",
    2.52 +                      "--group",
    2.53 +                      "--classpath ${workdir}/cp2 ${workdir}/src2",
    2.54 +                      "--list");
    2.55 +    }
    2.56 +
    2.57      public void testGroupsParamEscape() throws Exception {
    2.58          assertEquals(Arrays.asList("a b", "a\\b"),
    2.59                       Arrays.asList(Main.splitGroupArg("a\\ b a\\\\b")));
    2.60 @@ -472,10 +499,14 @@
    2.61      private static final String DONT_APPEND_PATH = new String("DONT_APPEND_PATH");
    2.62  
    2.63      private void doRunCompiler(String golden, String stdOut, String stdErr, String... fileContentAndExtraOptions) throws Exception {
    2.64 -        doRunCompiler(golden, stdOut, stdErr, 0, fileContentAndExtraOptions);
    2.65 +        doRunCompiler(equivalentValidator(golden), equivalentValidator(stdOut), equivalentValidator(stdErr), fileContentAndExtraOptions);
    2.66      }
    2.67  
    2.68 -    private void doRunCompiler(String golden, String stdOut, String stdErr, int exitcode, String... fileContentAndExtraOptions) throws Exception {
    2.69 +    private void doRunCompiler(Validator fileContentValidator, Validator stdOutValidator, Validator stdErrValidator, String... fileContentAndExtraOptions) throws Exception {
    2.70 +        doRunCompiler(fileContentValidator, stdOutValidator, stdErrValidator, 0, fileContentAndExtraOptions);
    2.71 +    }
    2.72 +
    2.73 +    private void doRunCompiler(Validator fileContentValidator, Validator stdOutValidator, Validator stdErrValidator, int exitcode, String... fileContentAndExtraOptions) throws Exception {
    2.74          List<String> fileAndContent = new LinkedList<String>();
    2.75          List<String> extraOptions = new LinkedList<String>();
    2.76          List<String> fileContentAndExtraOptionsList = Arrays.asList(fileContentAndExtraOptions);
    2.77 @@ -524,16 +555,14 @@
    2.78  
    2.79          reallyRunCompiler(wd, exitcode, output, options.toArray(new String[0]));
    2.80  
    2.81 -        if (golden != null) {
    2.82 -            assertEquals(golden, TestUtilities.copyFileToString(source));
    2.83 +        if (fileContentValidator != null) {
    2.84 +            fileContentValidator.validate(TestUtilities.copyFileToString(source));
    2.85          }
    2.86 -
    2.87 -        if (stdOut != null) {
    2.88 -            assertEquals(stdOut, output[0].replaceAll(Pattern.quote(wd.getAbsolutePath()), Matcher.quoteReplacement("${workdir}")));
    2.89 +        if (stdOutValidator != null) {
    2.90 +            stdOutValidator.validate(output[0].replaceAll(Pattern.quote(wd.getAbsolutePath()), Matcher.quoteReplacement("${workdir}")));
    2.91          }
    2.92 -
    2.93 -        if (stdErr != null) {
    2.94 -            assertEquals(stdErr, output[1].replaceAll(Pattern.quote(wd.getAbsolutePath()), Matcher.quoteReplacement("${workdir}")));
    2.95 +        if (stdErrValidator != null) {
    2.96 +            stdErrValidator.validate(output[1].replaceAll(Pattern.quote(wd.getAbsolutePath()), Matcher.quoteReplacement("${workdir}")));
    2.97          }
    2.98      }
    2.99  
   2.100 @@ -619,4 +648,18 @@
   2.101          assertEquals(1, testResult.getFailureCount());
   2.102          assertTrue(testResult.getFailures().toString(), testResult.getFailures().get(0).getDescription().getMethodName().endsWith("/h.test/neg"));
   2.103      }
   2.104 +
   2.105 +    private static Validator equivalentValidator(final String expected) {
   2.106 +        if (expected == null) return null;
   2.107 +
   2.108 +        return new Validator() {
   2.109 +            @Override public void validate(String content) {
   2.110 +                assertEquals(expected, content);
   2.111 +            }
   2.112 +        };
   2.113 +    }
   2.114 +
   2.115 +    private static interface Validator {
   2.116 +        public void validate(String content);
   2.117 +    }
   2.118  }
   2.119 \ No newline at end of file