コード例 #1
0
  @Test
  public void testSourceFormatter() throws Exception {
    SourceFormatterArgs sourceFormatterArgs = new SourceFormatterArgs();

    sourceFormatterArgs.setAutoFix(false);
    sourceFormatterArgs.setBaseDirName("../../../");
    sourceFormatterArgs.setPrintErrors(false);
    sourceFormatterArgs.setThrowException(true);
    sourceFormatterArgs.setUseProperties(false);

    SourceFormatter sourceFormatter = new SourceFormatter(sourceFormatterArgs);

    try {
      sourceFormatter.format();
    } catch (SourceMismatchException sme) {
      try {
        Assert.assertEquals(sme.getFileName(), sme.getFormattedSource(), sme.getOriginalSource());
      } catch (AssertionError ae) {
        String message = ae.getMessage();

        if (message.length() >= _MAX_MESSAGE_SIZE) {
          message = "Truncated message :\n" + message.substring(0, _MAX_MESSAGE_SIZE);

          throw new AssertionError(message, ae.getCause());
        }

        throw ae;
      }
    }
  }
コード例 #2
0
  @Test
  public void testFileNameWithIncorrectExtension() throws Exception {
    SourceFormatterArgs sourceFormatterArgs = new SourceFormatterArgs();

    sourceFormatterArgs.setAutoFix(false);
    sourceFormatterArgs.setPrintErrors(false);
    sourceFormatterArgs.setThrowException(false);
    sourceFormatterArgs.setUseProperties(false);

    String fileName = "src/test/resources/com/liferay/source/formatter/dependencies" + "/wrong.foo";

    sourceFormatterArgs.setFileNames(Collections.singletonList(fileName));

    SourceFormatter sourceFormatter = new SourceFormatter(sourceFormatterArgs);

    sourceFormatter.format();

    List<String> modifiedFileNames = sourceFormatter.getModifiedFileNames();

    Assert.assertTrue(modifiedFileNames.isEmpty());
  }
コード例 #3
0
  public static void main(String[] args) throws Exception {
    Map<String, String> arguments = ArgumentsUtil.parseArguments(args);

    try {
      SourceFormatterArgs sourceFormatterArgs = new SourceFormatterArgs();

      boolean autoFix =
          GetterUtil.getBoolean(arguments.get("source.auto.fix"), SourceFormatterArgs.AUTO_FIX);

      sourceFormatterArgs.setAutoFix(autoFix);

      String baseDirName =
          GetterUtil.getString(arguments.get("source.base.dir"), SourceFormatterArgs.BASE_DIR_NAME);

      sourceFormatterArgs.setBaseDirName(baseDirName);

      boolean formatCurrentBranch =
          GetterUtil.getBoolean(
              arguments.get("format.current.branch"), SourceFormatterArgs.FORMAT_CURRENT_BRANCH);

      sourceFormatterArgs.setFormatCurrentBranch(formatCurrentBranch);

      boolean formatLatestAuthor =
          GetterUtil.getBoolean(
              arguments.get("format.latest.author"), SourceFormatterArgs.FORMAT_LATEST_AUTHOR);

      sourceFormatterArgs.setFormatLatestAuthor(formatLatestAuthor);

      boolean formatLocalChanges =
          GetterUtil.getBoolean(
              arguments.get("format.local.changes"), SourceFormatterArgs.FORMAT_LOCAL_CHANGES);

      sourceFormatterArgs.setFormatLocalChanges(formatLocalChanges);

      if (formatCurrentBranch) {
        sourceFormatterArgs.setRecentChangesFileNames(
            GitUtil.getCurrentBranchFileNames(baseDirName));
      } else if (formatLatestAuthor) {
        sourceFormatterArgs.setRecentChangesFileNames(
            GitUtil.getLatestAuthorFileNames(baseDirName));
      } else if (formatLocalChanges) {
        sourceFormatterArgs.setRecentChangesFileNames(
            GitUtil.getLocalChangesFileNames(baseDirName));
      }

      String copyrightFileName =
          GetterUtil.getString(
              arguments.get("source.copyright.file"), SourceFormatterArgs.COPYRIGHT_FILE_NAME);

      sourceFormatterArgs.setCopyrightFileName(copyrightFileName);

      String[] fileNames = StringUtil.split(arguments.get("source.files"), StringPool.COMMA);

      if (ArrayUtil.isNotEmpty(fileNames)) {
        sourceFormatterArgs.setFileNames(Arrays.asList(fileNames));
      }

      boolean printErrors =
          GetterUtil.getBoolean(
              arguments.get("source.print.errors"), SourceFormatterArgs.PRINT_ERRORS);

      sourceFormatterArgs.setPrintErrors(printErrors);

      boolean throwException =
          GetterUtil.getBoolean(
              arguments.get("source.throw.exception"), SourceFormatterArgs.THROW_EXCEPTION);

      sourceFormatterArgs.setThrowException(throwException);

      boolean useProperties =
          GetterUtil.getBoolean(
              arguments.get("source.use.properties"), SourceFormatterArgs.USE_PROPERTIES);

      sourceFormatterArgs.setUseProperties(useProperties);

      SourceFormatter sourceFormatter = new SourceFormatter(sourceFormatterArgs);

      sourceFormatter.format();
    } catch (GitException ge) {
      System.out.println(ge.getMessage());

      System.exit(0);
    } catch (Exception e) {
      ArgumentsUtil.processMainException(arguments, e);
    }
  }