From b2a2b08d1306eb2558b7d8cfa327880c66c7d81c Mon Sep 17 00:00:00 2001
From: matthew_swift <matthew_swift@localhost>
Date: Tue, 17 Jul 2007 12:59:18 +0000
Subject: [PATCH] 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".

---
 opends/tests/unit-tests-testng/src/server/org/opends/server/admin/SizePropertyDefinitionTest.java     |   41 +++++++++----
 opends/src/server/org/opends/server/admin/SizePropertyDefinition.java                                 |    2 
 opends/tests/unit-tests-testng/src/server/org/opends/server/admin/DurationUnitTest.java               |   18 +++---
 opends/src/server/org/opends/server/admin/DurationPropertyDefinition.java                             |    1 
 opends/src/server/org/opends/server/admin/DurationUnit.java                                           |   56 ++++++++++--------
 opends/tests/unit-tests-testng/src/server/org/opends/server/admin/DurationPropertyDefinitionTest.java |   45 ++++++++++-----
 6 files changed, 100 insertions(+), 63 deletions(-)

diff --git a/opends/src/server/org/opends/server/admin/DurationPropertyDefinition.java b/opends/src/server/org/opends/server/admin/DurationPropertyDefinition.java
index eb269a5..1e0e749 100644
--- a/opends/src/server/org/opends/server/admin/DurationPropertyDefinition.java
+++ b/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();
   }
diff --git a/opends/src/server/org/opends/server/admin/DurationUnit.java b/opends/src/server/org/opends/server/admin/DurationUnit.java
index 134db63..ad6d9ef 100644
--- a/opends/src/server/org/opends/server/admin/DurationUnit.java
+++ b/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,19 +242,19 @@
    * 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
-   * toString(999)     // 999ms
-   * toString(1000)    // 1s
-   * toString(1500)    // 1s500ms
-   * toString(3650000) // 1h50s
-   * toString(3700000) // 1h1m40s
+   * toString(0)       // 0 ms
+   * toString(999)     // 999 ms
+   * toString(1000)    // 1 s
+   * toString(1500)    // 1 s 500 ms
+   * toString(3650000) // 1 h 50 s
+   * toString(3700000) // 1 h 1 m 40 s
    * </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.
@@ -267,19 +267,25 @@
     }
 
     if (duration == 0) {
-      return "0ms";
+      return "0 ms";
     }
 
     DurationUnit[] units = new DurationUnit[] { WEEKS, DAYS, HOURS, MINUTES,
         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) {
diff --git a/opends/src/server/org/opends/server/admin/SizePropertyDefinition.java b/opends/src/server/org/opends/server/admin/SizePropertyDefinition.java
index 55ddbda..28f060e 100644
--- a/opends/src/server/org/opends/server/admin/SizePropertyDefinition.java
+++ b/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();
   }
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/admin/DurationPropertyDefinitionTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/admin/DurationPropertyDefinitionTest.java
index 2d9d38d..c461c42 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/admin/DurationPropertyDefinitionTest.java
+++ b/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
@@ -97,10 +112,10 @@
     DurationPropertyDefinition spd = buildTestDefinition(builder);
     assertEquals(spd.getLowerLimit(), expectedValue);
   }
-  
+
   /**
    * Creates data for testing string-based limit values
-   * 
+   *
    * @return data
    */
   @DataProvider(name = "stringLimitData")
@@ -114,10 +129,10 @@
         { "m", "10s", 10000 }
     };
   }
-  
+
   /**
    * Tests setting/getting of lower limit as String.
-   * 
+   *
    * @param unit
    *          The unit.
    * @param value
@@ -281,16 +296,16 @@
   public Object[][] createEncodeValueData() {
     return new Object[][]{
             {-1L, "unlimited"},
-            {0L, "0s"},
-            {1L, "1s"},
-            {2L, "2s"},
-            {999L, "999s"},
-            {1000L, "1000s"},
-            {1001L, "1001s"},
-            {1023L, "1023s"},
-            {1024L, "1024s"},
-            {1025L, "1025s"},
-            {1000L * 1000L, "1000000s"},
+            {0L, "0 s"},
+            {1L, "1 s"},
+            {2L, "2 s"},
+            {999L, "999 s"},
+            {1000L, "1000 s"},
+            {1001L, "1001 s"},
+            {1023L, "1023 s"},
+            {1024L, "1024 s"},
+            {1025L, "1025 s"},
+            {1000L * 1000L, "1000000 s"},
     };
   }
 
@@ -409,7 +424,7 @@
             { "1h60m", 2L },
             { "1d10h", 34L },
             { "4d600m", 106L },
-            
+
             // conversion tests
             {"1 d", 24L},
             {"2 d", 48L},
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/admin/DurationUnitTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/admin/DurationUnitTest.java
index 4a2319d..2072c52 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/admin/DurationUnitTest.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/admin/DurationUnitTest.java
@@ -82,15 +82,15 @@
     @DataProvider(name = "testToString")
     public Object[][] createToStringData() {
       return new Object[][]{
-              { 0L, "0ms" },
-              { 1L, "1ms" },
-              { 999L, "999ms" },
-              { 1000L, "1s" },
-              { 1001L, "1s1ms" },
-              { 59999L, "59s999ms" },
-              { 60000L, "1m" },
-              { 3599999L, "59m59s999ms" },
-              { 3600000L, "1h" }
+              { 0L, "0 ms" },
+              { 1L, "1 ms" },
+              { 999L, "999 ms" },
+              { 1000L, "1 s" },
+              { 1001L, "1 s 1 ms" },
+              { 59999L, "59 s 999 ms" },
+              { 60000L, "1 m" },
+              { 3599999L, "59 m 59 s 999 ms" },
+              { 3600000L, "1 h" }
       };
     }
 
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/admin/SizePropertyDefinitionTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/admin/SizePropertyDefinitionTest.java
index 125bc28..fc3ae98 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/admin/SizePropertyDefinitionTest.java
+++ b/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
@@ -245,19 +260,19 @@
   public Object[][] createEncodeValueData() {
     return new Object[][]{
             {-1L, "unlimited"},
-            {0L, "0b"},
-            {1L, "1b"},
-            {2L, "2b"},
-            {999L, "999b"},
-            {1000L, "1kb"},
-            {1001L, "1001b"},
-            {1023L, "1023b"},
-            {1024L, "1kib"},
-            {1025L, "1025b"},
-            {1000L * 1000L, "1mb"},
-            {1000L * 1000L * 1000L, "1gb"},
-            {1024L * 1024L * 1024L, "1gib"},
-            {1000L * 1000L * 1000L * 1000L, "1tb"}
+            {0L, "0 b"},
+            {1L, "1 b"},
+            {2L, "2 b"},
+            {999L, "999 b"},
+            {1000L, "1 kb"},
+            {1001L, "1001 b"},
+            {1023L, "1023 b"},
+            {1024L, "1 kib"},
+            {1025L, "1025 b"},
+            {1000L * 1000L, "1 mb"},
+            {1000L * 1000L * 1000L, "1 gb"},
+            {1024L * 1024L * 1024L, "1 gib"},
+            {1000L * 1000L * 1000L * 1000L, "1 tb"}
 
     };
   }

--
Gitblit v1.10.0