| 1 | package be.nikiroo.utils.test; |
| 2 | |
| 3 | import java.util.Date; |
| 4 | import java.util.HashMap; |
| 5 | import java.util.List; |
| 6 | import java.util.Map; |
| 7 | import java.util.Map.Entry; |
| 8 | |
| 9 | import be.nikiroo.utils.StringUtils; |
| 10 | import be.nikiroo.utils.StringUtils.Alignment; |
| 11 | |
| 12 | class StringUtilsTest extends TestLauncher { |
| 13 | public StringUtilsTest(String[] args) { |
| 14 | super("StringUtils test", args); |
| 15 | |
| 16 | addTest(new TestCase("Time serialisation") { |
| 17 | @Override |
| 18 | public void test() throws Exception { |
| 19 | for (long fullTime : new Long[] { 0l, 123456l, 123456000l, |
| 20 | new Date().getTime() }) { |
| 21 | // precise to the second, no more |
| 22 | long time = (fullTime / 1000) * 1000; |
| 23 | |
| 24 | String displayTime = StringUtils.fromTime(time); |
| 25 | assertNotNull("The stringified time for " + time |
| 26 | + " should not be null", displayTime); |
| 27 | assertEquals("The stringified time for " + time |
| 28 | + " should not be empty", false, displayTime.trim() |
| 29 | .isEmpty()); |
| 30 | |
| 31 | assertEquals("The time " + time |
| 32 | + " should be loop-convertable", time, |
| 33 | StringUtils.toTime(displayTime)); |
| 34 | |
| 35 | assertEquals("The time " + displayTime |
| 36 | + " should be loop-convertable", displayTime, |
| 37 | StringUtils.fromTime(StringUtils |
| 38 | .toTime(displayTime))); |
| 39 | } |
| 40 | } |
| 41 | }); |
| 42 | |
| 43 | addTest(new TestCase("MD5") { |
| 44 | @Override |
| 45 | public void test() throws Exception { |
| 46 | String mess = "The String we got is not what 'md5sum' said it should heve been"; |
| 47 | assertEquals(mess, "34ded48fcff4221d644be9a37e1cb1d9", |
| 48 | StringUtils.getMd5Hash("fanfan la tulipe")); |
| 49 | assertEquals(mess, "7691b0cb74ed0f94b4d8cd858abe1165", |
| 50 | StringUtils.getMd5Hash("je te do-o-o-o-o-o-nne")); |
| 51 | } |
| 52 | }); |
| 53 | |
| 54 | addTest(new TestCase("Padding") { |
| 55 | @Override |
| 56 | public void test() throws Exception { |
| 57 | for (String data : new String[] { "fanfan", "la tulipe", |
| 58 | "1234567890", "12345678901234567890", "1", "" }) { |
| 59 | String result = StringUtils.padString(data, -1); |
| 60 | assertEquals("A size of -1 is expected to produce a noop", |
| 61 | true, data.equals(result)); |
| 62 | for (int size : new Integer[] { 0, 1, 5, 10, 40 }) { |
| 63 | result = StringUtils.padString(data, size); |
| 64 | assertEquals( |
| 65 | "Padding a String at a certain size should give a String of the given size", |
| 66 | size, result.length()); |
| 67 | assertEquals( |
| 68 | "Padding a String should not change the content", |
| 69 | true, data.trim().startsWith(result.trim())); |
| 70 | |
| 71 | result = StringUtils.padString(data, size, false, null); |
| 72 | assertEquals( |
| 73 | "Padding a String without cutting should not shorten the String", |
| 74 | true, data.length() <= result.length()); |
| 75 | assertEquals( |
| 76 | "Padding a String without cutting should keep the whole content", |
| 77 | true, data.trim().equals(result.trim())); |
| 78 | |
| 79 | result = StringUtils.padString(data, size, false, |
| 80 | Alignment.RIGHT); |
| 81 | if (size > data.length()) { |
| 82 | assertEquals( |
| 83 | "Padding a String to the end should work as expected", |
| 84 | true, result.endsWith(data)); |
| 85 | } |
| 86 | |
| 87 | result = StringUtils.padString(data, size, false, |
| 88 | Alignment.JUSTIFY); |
| 89 | if (size > data.length()) { |
| 90 | String unspacedData = data.trim(); |
| 91 | String unspacedResult = result.trim(); |
| 92 | for (int i = 0; i < size; i++) { |
| 93 | unspacedData = unspacedData.replace(" ", " "); |
| 94 | unspacedResult = unspacedResult.replace(" ", |
| 95 | " "); |
| 96 | } |
| 97 | |
| 98 | assertEquals( |
| 99 | "Justified text trimmed with all spaces collapsed " |
| 100 | + "sould be identical to original text " |
| 101 | + "trimmed with all spaces collapsed", |
| 102 | unspacedData, unspacedResult); |
| 103 | } |
| 104 | |
| 105 | result = StringUtils.padString(data, size, false, |
| 106 | Alignment.CENTER); |
| 107 | if (size > data.length()) { |
| 108 | int before = 0; |
| 109 | for (int i = 0; i < result.length() |
| 110 | && result.charAt(i) == ' '; i++) { |
| 111 | before++; |
| 112 | } |
| 113 | |
| 114 | int after = 0; |
| 115 | for (int i = result.length() - 1; i >= 0 |
| 116 | && result.charAt(i) == ' '; i--) { |
| 117 | after++; |
| 118 | } |
| 119 | |
| 120 | if (result.trim().isEmpty()) { |
| 121 | after = before / 2; |
| 122 | if (before > (2 * after)) { |
| 123 | before = after + 1; |
| 124 | } else { |
| 125 | before = after; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | assertEquals( |
| 130 | "Padding a String on center should work as expected", |
| 131 | result.length(), before + data.length() |
| 132 | + after); |
| 133 | assertEquals( |
| 134 | "Padding a String on center should not uncenter the content", |
| 135 | true, Math.abs(before - after) <= 1); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | }); |
| 141 | |
| 142 | addTest(new TestCase("Justifying") { |
| 143 | @Override |
| 144 | public void test() throws Exception { |
| 145 | for (String data : new String[] { "test", |
| 146 | "let's test some words", "" }) { |
| 147 | int total = 0; |
| 148 | for (String word : data.split((" "))) { |
| 149 | total += word.replace("-", "").replace(" ", "") |
| 150 | .length(); |
| 151 | } |
| 152 | List<String> result = StringUtils.justifyText(data, 5, |
| 153 | StringUtils.Alignment.LEFT); |
| 154 | |
| 155 | System.out.println("["+data+"] -> ["); |
| 156 | |
| 157 | int totalResult = 0; |
| 158 | for (String resultLine : result) { |
| 159 | System.out.println(resultLine); |
| 160 | for (String word : resultLine.split((" "))) { |
| 161 | totalResult += word.replace("-", "") |
| 162 | .replace(" ", "").length(); |
| 163 | } |
| 164 | } |
| 165 | System.out.println("]"); |
| 166 | |
| 167 | assertEquals( |
| 168 | "The number of letters ('-' not included) should be identical before and after", |
| 169 | total, totalResult); |
| 170 | } |
| 171 | } |
| 172 | }); |
| 173 | |
| 174 | addTest(new TestCase("unhtml") { |
| 175 | @Override |
| 176 | public void test() throws Exception { |
| 177 | Map<String, String> data = new HashMap<String, String>(); |
| 178 | data.put("aa", "aa"); |
| 179 | data.put("test with spaces ", "test with spaces "); |
| 180 | data.put("<a href='truc://target/'>link</a>", "link"); |
| 181 | data.put("<html>Digimon</html>", "Digimon"); |
| 182 | data.put("", ""); |
| 183 | data.put(" ", " "); |
| 184 | |
| 185 | for (Entry<String, String> entry : data.entrySet()) { |
| 186 | String result = StringUtils.unhtml(entry.getKey()); |
| 187 | assertEquals("Result is not what we expected", |
| 188 | entry.getValue(), result); |
| 189 | } |
| 190 | } |
| 191 | }); |
| 192 | |
| 193 | addTest(new TestCase("zip64") { |
| 194 | @Override |
| 195 | public void test() throws Exception { |
| 196 | String orig = "test"; |
| 197 | String zipped = StringUtils.zip64(orig); |
| 198 | String unzipped = StringUtils.unzip64(zipped); |
| 199 | assertEquals(orig, unzipped); |
| 200 | } |
| 201 | }); |
| 202 | } |
| 203 | } |