Ejemplo n.º 1
0
  private void assertFileContentsEqual(Path expectedFile, Path observedFile) throws IOException {
    String cleanPathToObservedFile =
        MoreStrings.withoutSuffix(
            templatePath.relativize(expectedFile).toString(), EXPECTED_SUFFIX);

    String expectedFileContent = new String(Files.readAllBytes(expectedFile), UTF_8);
    String observedFileContent = new String(Files.readAllBytes(observedFile), UTF_8);
    // It is possible, on Windows, to have Git keep "\n"-style newlines, or convert them to
    // "\r\n"-style newlines.  Support both ways by normalizing to "\n"-style newlines.
    // See https://help.github.com/articles/dealing-with-line-endings/ for more information.
    expectedFileContent = expectedFileContent.replace("\r\n", "\n");
    observedFileContent = observedFileContent.replace("\r\n", "\n");
    assertEquals(
        String.format(
            "In %s, expected content of %s to match that of %s.",
            cleanPathToObservedFile, expectedFileContent, observedFileContent),
        expectedFileContent,
        observedFileContent);
  }
Ejemplo n.º 2
0
  public void assertFilesEqual(Path expected, Path actual) throws IOException {
    if (!expected.isAbsolute()) {
      expected = templatePath.resolve(expected);
    }
    if (!actual.isAbsolute()) {
      actual = destPath.resolve(actual);
    }
    if (!Files.isRegularFile(actual)) {
      fail("Expected file " + actual + " could not be found.");
    }

    String extension = MorePaths.getFileExtension(actual);
    String cleanPathToObservedFile =
        MoreStrings.withoutSuffix(templatePath.relativize(expected).toString(), EXPECTED_SUFFIX);

    switch (extension) {
        // For Apple .plist and .stringsdict files, we define equivalence if:
        // 1. The two files are the same type (XML or binary)
        // 2. If binary: unserialized objects are deeply-equivalent.
        //    Otherwise, fall back to exact string match.
      case "plist":
      case "stringsdict":
        NSObject expectedObject;
        try {
          expectedObject = BinaryPropertyListParser.parse(expected.toFile());
        } catch (Exception e) {
          // Not binary format.
          expectedObject = null;
        }

        NSObject observedObject;
        try {
          observedObject = BinaryPropertyListParser.parse(actual.toFile());
        } catch (Exception e) {
          // Not binary format.
          observedObject = null;
        }

        assertTrue(
            String.format(
                "In %s, expected plist to be of %s type.",
                cleanPathToObservedFile, (expectedObject != null) ? "binary" : "XML"),
            (expectedObject != null) == (observedObject != null));

        if (expectedObject != null) {
          // These keys depend on the locally installed version of Xcode, so ignore them
          // in comparisons.
          String[] ignoredKeys = {
            "DTSDKName",
            "DTPlatformName",
            "DTPlatformVersion",
            "MinimumOSVersion",
            "DTSDKBuild",
            "DTPlatformBuild",
            "DTXcode",
            "DTXcodeBuild"
          };
          if (observedObject instanceof NSDictionary && expectedObject instanceof NSDictionary) {
            for (String key : ignoredKeys) {
              ((NSDictionary) observedObject).remove(key);
              ((NSDictionary) expectedObject).remove(key);
            }
          }

          assertEquals(
              String.format(
                  "In %s, expected binary plist contents to match.", cleanPathToObservedFile),
              expectedObject,
              observedObject);
          break;
        } else {
          assertFileContentsEqual(expected, actual);
        }
        break;

      default:
        assertFileContentsEqual(expected, actual);
    }
  }
Ejemplo n.º 3
0
public enum SizeUnit {
  BYTES(0, "B"),
  KILOBYTES(1, "KB"),
  MEGABYTES(2, "MB"),
  GIGABYTES(3, "GB"),
  TERABYTES(4, "TB");

  private final int ordinal;
  private final String abbreviation;

  private SizeUnit(int ordinal, String abbreviation) {
    this.ordinal = ordinal;
    this.abbreviation = abbreviation;
  }

  public int getOrdinal() {
    return ordinal;
  }

  public String getAbbreviation() {
    return abbreviation;
  }

  private static long multiplyByByteOrderOfMagnitude(double size, int magnitude) {
    if (magnitude == 0) {
      return (long) size;
    } else if (magnitude > 0) {
      return BigDecimal.valueOf(size).multiply(BigDecimal.valueOf(1024).pow(magnitude)).longValue();
    } else {
      return BigDecimal.valueOf(size)
          .divide(BigDecimal.valueOf(1024).pow(-1 * magnitude))
          .longValue();
    }
  }

  private static final ImmutableMap<String, SizeUnit> SHORT_TO_CODE =
      ImmutableMap.<String, SizeUnit>builder()
          .put("b", BYTES)
          .put("kb", KILOBYTES)
          .put("kilobytes", KILOBYTES)
          .put("mb", MEGABYTES)
          .put("megabytes", MEGABYTES)
          .put("gb", GIGABYTES)
          .put("gigabytes", GIGABYTES)
          .put("tb", TERABYTES)
          .put("terabytes", TERABYTES)
          .build();

  private static final Pattern SIZE_PATTERN =
      Pattern.compile(
          "([\\d]+(?:\\.[\\d]+)?)\\s*" + MoreStrings.regexPatternForAny(SHORT_TO_CODE.keySet()),
          Pattern.CASE_INSENSITIVE);

  /** Parses a string that represents a size into the number of bytes represented by that string. */
  public static long parseBytes(String input) throws NumberFormatException {
    Matcher matcher = SIZE_PATTERN.matcher(input);
    if (matcher.find()) {
      String number = matcher.group(1);
      SizeUnit sizeUnit = SHORT_TO_CODE.get(matcher.group(2).toLowerCase());
      if (sizeUnit != null) {
        try {
          double value = Double.parseDouble(number);
          return sizeUnit.toBytes(value);
        } catch (NumberFormatException e) {
          // If the number was so large as to overflow Long.MAX_VALUE, return LONG.MAX_VALUE.
          return Long.MAX_VALUE;
        }
      }
    }
    throw new NumberFormatException(String.format("%s is not a valid file size", input));
  }

  public long toBytes(double size) {
    return multiplyByByteOrderOfMagnitude(size, getOrdinal() - BYTES.getOrdinal());
  }

  public long toKilobytes(double size) {
    return multiplyByByteOrderOfMagnitude(size, getOrdinal() - KILOBYTES.getOrdinal());
  }

  public long toMegabytes(double size) {
    return multiplyByByteOrderOfMagnitude(size, getOrdinal() - MEGABYTES.getOrdinal());
  }

  public long toGigabytes(double size) {
    return multiplyByByteOrderOfMagnitude(size, getOrdinal() - GIGABYTES.getOrdinal());
  }

  public long toTerabytes(double size) {
    return multiplyByByteOrderOfMagnitude(size, getOrdinal() - TERABYTES.getOrdinal());
  }

  public static Pair<Double, SizeUnit> getHumanReadableSize(double size, SizeUnit unit) {
    if (size == 0) {
      return new Pair<>(size, unit);
    }
    int ordinal = unit.getOrdinal();
    double resultSize = size;
    if (size > 1) {
      while (size > 1 && ordinal < 4) {
        size = size / 1024;
        if (size > 1) {
          ordinal++;
          resultSize = size;
        }
      }
    } else {
      while (size < 1024 && ordinal > 0) {
        size = size * 1024;
        if (size < 1024) {
          ordinal--;
          resultSize = size;
        }
      }
    }
    return new Pair<>(resultSize, SizeUnit.values()[ordinal]);
  }

  public static String toHumanReadableString(Pair<Double, SizeUnit> size, Locale locale) {
    return String.format(locale, "%.2f %s", size.getFirst(), size.getSecond().getAbbreviation());
  }
}