|
| 1 | + |
| 2 | +package org.scijava.util; |
| 3 | + |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertFalse; |
| 6 | +import static org.junit.Assert.assertNotNull; |
| 7 | +import static org.junit.Assert.assertNull; |
| 8 | +import static org.junit.Assert.assertTrue; |
| 9 | + |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +/** |
| 13 | + * Tests for the utility methods in {@link StringUtils} |
| 14 | + * |
| 15 | + * @author Richard Domander (Royal Veterinary College, London) |
| 16 | + */ |
| 17 | +public class StringUtilsTest { |
| 18 | + |
| 19 | + @Test |
| 20 | + public void isNullOrEmptyFalseIfString() throws Exception { |
| 21 | + assertFalse(StringUtils.isNullOrEmpty("Fresh out of Red Leicester")); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void isNullOrEmpty() throws Exception { |
| 26 | + assertTrue(StringUtils.isNullOrEmpty(null)); |
| 27 | + assertTrue(StringUtils.isNullOrEmpty("")); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void padEndNullStringReturnsNull() throws Exception { |
| 32 | + assertNull(StringUtils.padEnd(null, 5, '*')); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void padEndEmptyString() throws Exception { |
| 37 | + final int length = 5; |
| 38 | + final char padChar = '*'; |
| 39 | + |
| 40 | + final String padded = StringUtils.padEnd("", length, padChar); |
| 41 | + assertNotNull(padded); |
| 42 | + assertFalse(padded.isEmpty()); |
| 43 | + assertEquals(length, padded.length()); |
| 44 | + assertTrue(padded.chars().allMatch(c -> c == padChar)); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void padEndLengthSmaller() throws Exception { |
| 49 | + final String s = "Eric the fruit bat"; |
| 50 | + |
| 51 | + final String padded = StringUtils.padEnd(s, 3, '*'); |
| 52 | + |
| 53 | + assertEquals(s, padded); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void padEndLengthEqual() throws Exception { |
| 58 | + final String s = "Eric the cat"; |
| 59 | + |
| 60 | + final String padded = StringUtils.padEnd(s, s.length(), '*'); |
| 61 | + |
| 62 | + assertEquals(s, padded); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void padEndLengthNegative() throws Exception { |
| 67 | + final String s = "Eric the dog"; |
| 68 | + |
| 69 | + final String padded = StringUtils.padEnd(s, -1, '~'); |
| 70 | + |
| 71 | + assertEquals(s, padded); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void padEnd() throws Exception { |
| 76 | + final String s = "Eric the halibut"; |
| 77 | + final int newLength = s.length() + 5; |
| 78 | + |
| 79 | + final String padded = StringUtils.padEnd(s, newLength); |
| 80 | + |
| 81 | + assertEquals(newLength, padded.length()); |
| 82 | + final String padding = padded.substring(padded.length() - 5); |
| 83 | + assertTrue(padding.chars().allMatch(c -> c == StringUtils.DEFAULT_PAD_CHAR)); |
| 84 | + assertEquals(s, padded.substring(0, padded.length() - 5)); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void padStartNullStringReturnsNull() throws Exception { |
| 89 | + assertNull(StringUtils.padStart(null, 5, '*')); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void padStartEmptyString() throws Exception { |
| 94 | + final int length = 5; |
| 95 | + final char padChar = '*'; |
| 96 | + |
| 97 | + final String padded = StringUtils.padStart("", length, padChar); |
| 98 | + assertNotNull(padded); |
| 99 | + assertFalse(padded.isEmpty()); |
| 100 | + assertEquals(length, padded.length()); |
| 101 | + assertTrue(padded.chars().allMatch(c -> c == padChar)); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void padStartLengthSmaller() throws Exception { |
| 106 | + final String s = "Eric the dog"; |
| 107 | + |
| 108 | + final String padded = StringUtils.padStart(s, 3, '*'); |
| 109 | + |
| 110 | + assertEquals(s, padded); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void padStartLengthEqual() throws Exception { |
| 115 | + final String s = "Simon the prawn"; |
| 116 | + |
| 117 | + final String padded = StringUtils.padStart(s, s.length(), '*'); |
| 118 | + |
| 119 | + assertEquals(s, padded); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void padStartLengthNegative() throws Exception { |
| 124 | + final String s = "Norman the pike"; |
| 125 | + |
| 126 | + final String padded = StringUtils.padStart(s, -1, '~'); |
| 127 | + |
| 128 | + assertEquals(s, padded); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void padStart() throws Exception { |
| 133 | + final String s = "Norman the pike"; |
| 134 | + final int newLength = s.length() + 5; |
| 135 | + |
| 136 | + final String padded = StringUtils.padStart(s, newLength); |
| 137 | + |
| 138 | + assertEquals(newLength, padded.length()); |
| 139 | + final String padding = padded.substring(0, 5); |
| 140 | + assertTrue(padding.chars().allMatch(c -> c == StringUtils.DEFAULT_PAD_CHAR)); |
| 141 | + assertEquals(s, padded.substring(5, padded.length())); |
| 142 | + } |
| 143 | +} |
0 commit comments