mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

matthew_swift
17.59.2007 b2a2b08d1306eb2558b7d8cfa327880c66c7d81c
Fix issue 1958. Make sure duration and size values are always separated from their unit by a single space. For example, the value 5 seconds should be displayed as "5 s" and not "5s".
6 files modified
75 ■■■■ changed files
opends/src/server/org/opends/server/admin/DurationPropertyDefinition.java 1 ●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/admin/DurationUnit.java 42 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/admin/SizePropertyDefinition.java 2 ●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/admin/DurationPropertyDefinitionTest.java 15 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/admin/DurationUnitTest.java patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/admin/SizePropertyDefinitionTest.java 15 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/admin/DurationPropertyDefinition.java
@@ -499,6 +499,7 @@
    // Encode the size value using the base unit.
    StringBuilder builder = new StringBuilder();
    builder.append(value);
    builder.append(' ');
    builder.append(baseUnit.toString());
    return builder.toString();
  }
opends/src/server/org/opends/server/admin/DurationUnit.java
@@ -107,7 +107,7 @@
  /**
   * Parse the provided duration string and return its equivalent
   * duration in milli-seconds. The duration string must specify the
   * duration in milliseconds. The duration string must specify the
   * unit e.g. "10s". This method will parse duration string
   * representations produced from the {@link #toString(long)} method.
   * Therefore, a duration can comprise of multiple duration
@@ -115,7 +115,7 @@
   *
   * @param s
   *          The duration string to be parsed.
   * @return Returns the parsed duration in milli-seconds.
   * @return Returns the parsed duration in milliseconds.
   * @throws NumberFormatException
   *           If the provided duration string could not be parsed.
   * @see #toString(long)
@@ -128,7 +128,7 @@
  /**
   * Parse the provided duration string and return its equivalent
   * duration in milli-seconds. This method will parse duration string
   * duration in milliseconds. This method will parse duration string
   * representations produced from the {@link #toString(long)} method.
   * Therefore, a duration can comprise of multiple duration
   * specifiers, for example <code>1d15m25s</code>.
@@ -139,7 +139,7 @@
   *          The default unit to use if there is no unit specified in
   *          the duration string, or <code>null</code> if the
   *          string must always contain a unit.
   * @return Returns the parsed duration in milli-seconds.
   * @return Returns the parsed duration in milliseconds.
   * @throws NumberFormatException
   *           If the provided duration string could not be parsed.
   * @see #toString(long)
@@ -151,9 +151,9 @@
      throw new NumberFormatException("Empty duration value \"" + s + "\"");
    }
    Pattern p1 = Pattern.compile("^((\\d+)w)?" + "((\\d+)d)?"
        + "((\\d+)h)?" + "((\\d+)m)?" + "((\\d+)s)?" + "((\\d+)ms)?$",
        Pattern.CASE_INSENSITIVE);
    Pattern p1 = Pattern.compile("^\\s*((\\d+)\\s*w)?" + "\\s*((\\d+)\\s*d)?"
        + "\\s*((\\d+)\\s*h)?" + "\\s*((\\d+)\\s*m)?" + "\\s*((\\d+)\\s*s)?"
        + "\\s*((\\d+)\\s*ms)?\\s*$", Pattern.CASE_INSENSITIVE);
    Matcher m1 = p1.matcher(ns);
    if (m1.matches()) {
      // Value must be of the form produced by toString(long).
@@ -197,7 +197,7 @@
      return duration;
    } else {
      // Value must be a floating point number followed by a unit.
      Pattern p2 = Pattern.compile("^(\\d+(\\.\\d+)?)\\s*(\\w+)?$");
      Pattern p2 = Pattern.compile("^\\s*(\\d+(\\.\\d+)?)\\s*(\\w+)?\\s*$");
      Matcher m2 = p2.matcher(ns);
      if (!m2.matches()) {
@@ -242,7 +242,7 @@
   * string representation can be parsed using the
   * {@link #parseValue(String)} method. The string representation is
   * comprised of one or more of the number of weeks, days, hours,
   * minutes, seconds, and milli-seconds. Here are some examples:
   * minutes, seconds, and milliseconds. Here are some examples:
   *
   * <pre>
   * toString(0)       // 0ms
@@ -254,7 +254,7 @@
   * </pre>
   *
   * @param duration
   *          The duration in milli-seconds.
   *          The duration in milliseconds.
   * @return Returns a string representation of the provided duration.
   * @throws IllegalArgumentException
   *           If the provided duration is negative.
@@ -274,12 +274,18 @@
        SECONDS, MILLI_SECONDS };
    long remainder = duration;
    StringBuilder builder = new StringBuilder();
    boolean isFirst = true;
    for (DurationUnit unit : units) {
      long count = remainder / unit.getDuration();
      if (count > 0) {
        if (!isFirst) {
          builder.append(' ');
        }
        builder.append(count);
        builder.append(' ');
        builder.append(unit.getShortName());
        remainder = remainder - (count * unit.getDuration());
        isFirst = false;
      }
    }
    return builder.toString();
@@ -291,7 +297,7 @@
  // The abbreviation of the unit.
  private final String shortName;
  // The size of the unit in milli-seconds.
  // The size of the unit in milliseconds.
  private final long sz;
@@ -306,11 +312,11 @@
  /**
   * Converts the specified duration in milli-seconds to this unit.
   * Converts the specified duration in milliseconds to this unit.
   *
   * @param duration
   *          The duration in milli-seconds.
   * @return Returns milli-seconds in this unit.
   *          The duration in milliseconds.
   * @return Returns milliseconds in this unit.
   */
  public double fromMilliSeconds(long duration) {
    return ((double) duration / sz);
@@ -319,9 +325,9 @@
  /**
   * Get the number of milli-seconds that this unit represents.
   * Get the number of milliseconds that this unit represents.
   *
   * @return Returns the number of milli-seconds that this unit
   * @return Returns the number of milliseconds that this unit
   *         represents.
   */
  public long getDuration() {
@@ -353,11 +359,11 @@
  /**
   * Converts the specified duration in this unit to milli-seconds.
   * Converts the specified duration in this unit to milliseconds.
   *
   * @param duration
   *          The duration as a quantity of this unit.
   * @return Returns the number of milli-seconds that the duration
   * @return Returns the number of milliseconds that the duration
   *         represents.
   */
  public long toMilliSeconds(double duration) {
opends/src/server/org/opends/server/admin/SizePropertyDefinition.java
@@ -317,7 +317,7 @@
    // Cast to a long to remove fractional part (which should not be there
    // anyway as the best-fit unit should result in an exact conversion).
    builder.append((long) unit.fromBytes(value));
    builder.append(' ');
    builder.append(unit.toString());
    return builder.toString();
  }
opends/tests/unit-tests-testng/src/server/org/opends/server/admin/DurationPropertyDefinitionTest.java
@@ -29,7 +29,9 @@
import static org.testng.Assert.*;
import org.opends.server.TestCaseUtils;
import org.opends.server.admin.std.meta.RootCfgDefn;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
@@ -39,6 +41,19 @@
public class DurationPropertyDefinitionTest {
  /**
   * Sets up tests
   *
   * @throws Exception
   *           If the server could not be initialized.
   */
  @BeforeClass
  public void setUp() throws Exception {
    // This test suite depends on having the schema available, so
    // we'll start the server.
    TestCaseUtils.startServer();
  }
  /**
   * Tests creation of builder succeeds
   */
  @Test
opends/tests/unit-tests-testng/src/server/org/opends/server/admin/DurationUnitTest.java
opends/tests/unit-tests-testng/src/server/org/opends/server/admin/SizePropertyDefinitionTest.java
@@ -29,7 +29,9 @@
import static org.testng.Assert.*;
import org.opends.server.TestCaseUtils;
import org.opends.server.admin.std.meta.RootCfgDefn;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
@@ -39,6 +41,19 @@
public class SizePropertyDefinitionTest {
  /**
   * Sets up tests
   *
   * @throws Exception
   *           If the server could not be initialized.
   */
  @BeforeClass
  public void setUp() throws Exception {
    // This test suite depends on having the schema available, so
    // we'll start the server.
    TestCaseUtils.startServer();
  }
  /**
   * Tests creation of builder succeeds
   */
  @Test