StringUtils: NPE fix if bad arg for padding, tests
authorNiki Roo <niki@nikiroo.be>
Fri, 1 Dec 2017 13:44:32 +0000 (14:44 +0100)
committerNiki Roo <niki@nikiroo.be>
Fri, 1 Dec 2017 13:44:32 +0000 (14:44 +0100)
src/be/nikiroo/utils/StringUtils.java
src/be/nikiroo/utils/test/StringUtilsTest.java

index 0ceb673104902170a6860294a4aa37b19547a610..acd632aa1968c50fa631131b3f9b216da6e2f001 100644 (file)
@@ -51,7 +51,7 @@ public class StringUtils {
         * @return the resulting {@link String} of size <i>size</i>
         */
        static public String padString(String text, int width) {
-               return padString(text, width, true, Alignment.Beginning);
+               return padString(text, width, true, null);
        }
 
        /**
@@ -67,13 +67,17 @@ public class StringUtils {
         *            cut the {@link String} shorter if needed
         * @param align
         *            align the {@link String} in this position if we have enough
-        *            space
+        *            space (default is Alignment.Beginning)
         * 
         * @return the resulting {@link String} of size <i>size</i> minimum
         */
        static public String padString(String text, int width, boolean cut,
                        Alignment align) {
 
+               if (align == null) {
+                       align = Alignment.Beginning;
+               }
+
                if (width >= 0) {
                        if (text == null)
                                text = "";
@@ -164,11 +168,15 @@ public class StringUtils {
        }
 
        /**
-        * Convert between time in milliseconds to {@link String} in a "static" way
-        * (to exchange data over the wire, for instance).
+        * Convert between the time in milliseconds to a {@link String} in a "fixed"
+        * way (to exchange data over the wire, for instance).
+        * <p>
+        * Precise to the second.
         * 
         * @param time
-        *            the time in milliseconds
+        *            the specified number of milliseconds since the standard base
+        *            time known as "the epoch", namely January 1, 1970, 00:00:00
+        *            GMT
         * 
         * @return the time as a {@link String}
         */
@@ -178,13 +186,16 @@ public class StringUtils {
        }
 
        /**
-        * Convert between time as a {@link String} to milliseconds in a "static"
+        * Convert between the time as a {@link String} to milliseconds in a "fixed"
         * way (to exchange data over the wire, for instance).
+        * <p>
+        * Precise to the second.
         * 
         * @param displayTime
         *            the time as a {@link String}
         * 
-        * @return the time in milliseconds
+        * @return the number of milliseconds since the standard base time known as
+        *         "the epoch", namely January 1, 1970, 00:00:00 GMT
         */
        static public long toTime(String displayTime) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
index 2b220b7f37be3e78f18f0dce9eb9edfbc7d70f4b..5c939575059b4e2a87805f718467bc8eee56d396 100644 (file)
 package be.nikiroo.utils.test;
 
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
 import be.nikiroo.utils.StringUtils;
+import be.nikiroo.utils.StringUtils.Alignment;
 
 class StringUtilsTest extends TestLauncher {
        public StringUtilsTest(String[] args) {
                super("StringUtils test", args);
 
+               addTest(new TestCase("Time serialisation") {
+                       @Override
+                       public void test() throws Exception {
+                               for (long fullTime : new Long[] { 0l, 123456l, 123456000l,
+                                               new Date().getTime() }) {
+                                       // precise to the second, no more
+                                       long time = (fullTime / 1000) * 1000;
+
+                                       String displayTime = StringUtils.fromTime(time);
+                                       assertNotNull("The stringified time for " + time
+                                                       + " should not be null", displayTime);
+                                       assertEquals("The stringified time for " + time
+                                                       + " should not be empty", false, displayTime.trim()
+                                                       .isEmpty());
+
+                                       assertEquals("The time " + time
+                                                       + " should be loop-convertable", time,
+                                                       StringUtils.toTime(displayTime));
+
+                                       assertEquals("The time " + displayTime
+                                                       + " should be loop-convertable", displayTime,
+                                                       StringUtils.fromTime(StringUtils
+                                                                       .toTime(displayTime)));
+                               }
+                       }
+               });
+
+               addTest(new TestCase("MD5") {
+                       @Override
+                       public void test() throws Exception {
+                               String mess = "The String we got is not what 'md5sum' said it should heve been";
+                               assertEquals(mess, "34ded48fcff4221d644be9a37e1cb1d9",
+                                               StringUtils.getMd5Hash("fanfan la tulipe"));
+                               assertEquals(mess, "7691b0cb74ed0f94b4d8cd858abe1165",
+                                               StringUtils.getMd5Hash("je te do-o-o-o-o-o-nne"));
+                       }
+               });
+
+               addTest(new TestCase("Padding") {
+                       @Override
+                       public void test() throws Exception {
+                               for (String data : new String[] { "fanfan", "la tulipe",
+                                               "1234567890", "12345678901234567890", "1", "" }) {
+                                       String result = StringUtils.padString(data, -1);
+                                       assertEquals("A size of -1 is expected to produce a noop",
+                                                       true, data.equals(result));
+                                       for (int size : new Integer[] { 0, 1, 5, 10, 40 }) {
+                                               result = StringUtils.padString(data, size);
+                                               assertEquals(
+                                                               "Padding a String at a certain size should give a String of the given size",
+                                                               size, result.length());
+                                               assertEquals(
+                                                               "Padding a String should not change the content",
+                                                               true, data.trim().startsWith(result.trim()));
+
+                                               result = StringUtils.padString(data, size, false, null);
+                                               assertEquals(
+                                                               "Padding a String without cutting should not shorten the String",
+                                                               true, data.length() <= result.length());
+                                               assertEquals(
+                                                               "Padding a String without cutting should keep the whole content",
+                                                               true, data.trim().equals(result.trim()));
+
+                                               result = StringUtils.padString(data, size, false,
+                                                               Alignment.End);
+                                               if (size > data.length()) {
+                                                       assertEquals(
+                                                                       "Padding a String to the end should work as expected",
+                                                                       true, result.endsWith(data));
+                                               }
+
+                                               result = StringUtils.padString(data, size, false,
+                                                               Alignment.Center);
+                                               if (size > data.length()) {
+                                                       int before = 0;
+                                                       for (int i = 0; i < result.length()
+                                                                       && result.charAt(i) == ' '; i++) {
+                                                               before++;
+                                                       }
+
+                                                       int after = 0;
+                                                       for (int i = result.length() - 1; i >= 0
+                                                                       && result.charAt(i) == ' '; i--) {
+                                                               after++;
+                                                       }
+
+                                                       if (result.trim().isEmpty()) {
+                                                               after = before / 2;
+                                                               if (before > (2 * after)) {
+                                                                       before = after + 1;
+                                                               } else {
+                                                                       before = after;
+                                                               }
+                                                       }
+
+                                                       assertEquals(
+                                                                       "Padding a String on center should work as expected",
+                                                                       result.length(), before + data.length()
+                                                                                       + after);
+                                                       assertEquals(
+                                                                       "Padding a String on center should not uncenter the content",
+                                                                       true, Math.abs(before - after) <= 1);
+                                               }
+                                       }
+                               }
+                       }
+               });
+
+               addTest(new TestCase("unhtml") {
+                       @Override
+                       public void test() throws Exception {
+                               Map<String, String> data = new HashMap<String, String>();
+                               data.put("aa", "aa");
+                               data.put("test with spaces ", "test with spaces ");
+                               data.put("<a href='truc://target/'>link</a>", "link");
+                               data.put("<html>Digimon</html>", "Digimon");
+                               data.put("", "");
+                               data.put(" ", " ");
+
+                               for (Entry<String, String> entry : data.entrySet()) {
+                                       String result = StringUtils.unhtml(entry.getKey());
+                                       assertEquals("Result is not what we expected",
+                                                       entry.getValue(), result);
+                               }
+                       }
+               });
+
                addTest(new TestCase("zip64") {
                        @Override
                        public void test() throws Exception {