| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.logging; |
| 34 | | | |
| 35 | | | |
| 36 | | | import java.nio.CharBuffer; |
| 37 | | | import java.text.Format; |
| 38 | | | import java.text.ParseException; |
| 39 | | | import org.jcoderz.commons.TestCase; |
| 40 | | | |
| 41 | | | |
| 42 | | | |
| 43 | | | |
| 44 | | | |
| 45 | 100 | (1) | public class WhitespaceFormatTest |
| 46 | | | extends TestCase |
| 47 | | | { |
| 48 | | | private static final char PRESERVED_CHAR = '\u0020'; |
| 49 | | | |
| 50 | | | |
| 51 | | | |
| 52 | | | |
| 53 | | | public void testSimpleWhiteSpace () |
| 54 | | | { |
| 55 | 100 | (2) | final String t1 = "This is a string."; |
| 56 | 100 | | final String t2 = " This is a string with long whitespace."; |
| 57 | 100 | | final String r1 = WhitespaceFormat.format(t1); |
| 58 | 100 | | final String r2 = WhitespaceFormat.format(t2); |
| 59 | 100 | | final CharBuffer tb1 = CharBuffer.wrap(t1); |
| 60 | 100 | | final CharBuffer tb2 = CharBuffer.wrap(t2); |
| 61 | 100 | | final CharBuffer rb1 = WhitespaceFormat.format(tb1); |
| 62 | 100 | | final CharBuffer rb2 = WhitespaceFormat.format(tb2); |
| 63 | 100 | | compare(t1, r1); |
| 64 | 100 | | compare(t2, r2); |
| 65 | 100 | | compare(tb1, rb1); |
| 66 | 100 | | compare(tb2, rb2); |
| 67 | 100 | | } |
| 68 | | | |
| 69 | | | |
| 70 | | | |
| 71 | | | |
| 72 | | | public void testTabWhiteSpace () |
| 73 | | | { |
| 74 | 100 | | final String t1 = "This is a string."; |
| 75 | 100 | | final String t2 = "\tThis is\t\ta\tstring \twith\t\t tabs."; |
| 76 | 100 | | reformatAndCheck(t1, t2); |
| 77 | 100 | | parseAndCompare(String.valueOf(PRESERVED_CHAR), PRESERVED_CHAR + t2); |
| 78 | 100 | | } |
| 79 | | | |
| 80 | | | |
| 81 | | | |
| 82 | | | |
| 83 | | | |
| 84 | | | public void testLfWhiteSpace () |
| 85 | | | { |
| 86 | 100 | | final String t1 = "This is a string."; |
| 87 | 100 | | final String t2 = "\n\nThis is\na string with\n line feeds."; |
| 88 | 100 | | reformatAndCheck(t1, t2); |
| 89 | 100 | | parseAndCompare( |
| 90 | | | String.valueOf(PRESERVED_CHAR) + String.valueOf(PRESERVED_CHAR), |
| 91 | | | String.valueOf(PRESERVED_CHAR) + String.valueOf(PRESERVED_CHAR) + t2 |
| 92 | | | ); |
| 93 | 100 | | } |
| 94 | | | |
| 95 | | | |
| 96 | | | |
| 97 | | | |
| 98 | | | public void testMixedWhiteSpace () |
| 99 | | | { |
| 100 | 100 | | final String t1 = "This is a string."; |
| 101 | 100 | | final String t2 |
| 102 | | | = "This is\na\tstring\t \twith\n mixed\n\n \twhite\nspace."; |
| 103 | | | |
| 104 | 100 | | reformatAndCheck(t1, t2); |
| 105 | 100 | | } |
| 106 | | | |
| 107 | | | |
| 108 | | | private void reformatAndCheck (final String t1, final String t2) |
| 109 | | | { |
| 110 | 100 | | final String r1 = WhitespaceFormat.format(t1); |
| 111 | 100 | | final String r2 = WhitespaceFormat.format(t2); |
| 112 | 100 | | final CharBuffer tb1 = CharBuffer.wrap(t1); |
| 113 | 100 | | final CharBuffer tb2 = CharBuffer.wrap(t2); |
| 114 | 100 | | final CharBuffer rb1 = WhitespaceFormat.format(tb1); |
| 115 | 100 | | final CharBuffer rb2 = WhitespaceFormat.format(tb2); |
| 116 | 100 | | compare(t1, r1); |
| 117 | 100 | | parseAndCompare(t1, r1); |
| 118 | 100 | | compare(t2, r2); |
| 119 | 100 | | parseAndCompare(t2, r2); |
| 120 | 100 | | compare(tb1, rb1); |
| 121 | 100 | | compare(tb2, rb2); |
| 122 | 100 | | } |
| 123 | | | |
| 124 | | | private void compare (final String source, final String formatted) |
| 125 | | | { |
| 126 | 75 | | assertTrue("Formatted string must not be longer than source string.", |
| 127 | | | source.length() >= formatted.length()); |
| 128 | | | |
| 129 | 100 | | assertNotNull("The formatted string must not be null", formatted); |
| 130 | | | |
| 131 | 100 | | int fIdx = 0; |
| 132 | 100 | | boolean firstWs = true; |
| 133 | | | |
| 134 | 100 | | for (int sIdx = 0; sIdx < source.length(); ++sIdx) |
| 135 | | | { |
| 136 | 100 | | final char sc = source.charAt(sIdx); |
| 137 | 100 | | final char fc = formatted.charAt(fIdx); |
| 138 | | | |
| 139 | 100 | | if (Character.isWhitespace(sc) && (sc != PRESERVED_CHAR)) |
| 140 | | | { |
| 141 | 100 | | if (firstWs) |
| 142 | | | { |
| 143 | 100 | | assertEquals("Formatted char must be whitespace", |
| 144 | | | String.valueOf(fc), String.valueOf(PRESERVED_CHAR)); |
| 145 | 100 | | fIdx++; |
| 146 | 100 | | firstWs = false; |
| 147 | | | } |
| 148 | | | } |
| 149 | | | else |
| 150 | | | { |
| 151 | 100 | | assertEquals("Formatted char must match source char", |
| 152 | | | String.valueOf(fc), String.valueOf(sc)); |
| 153 | 100 | | fIdx++; |
| 154 | 100 | | firstWs = true; |
| 155 | | | } |
| 156 | | | } |
| 157 | 100 | | } |
| 158 | | | |
| 159 | | | private void compare (final CharBuffer source, final CharBuffer formatted) |
| 160 | | | { |
| 161 | 100 | (3)(4) | compare(source.toString(), formatted.toString()); |
| 162 | 100 | | } |
| 163 | | | |
| 164 | | | private void parseAndCompare (final String source, final String formatted) |
| 165 | | | { |
| 166 | 100 | | final Format format = new WhitespaceFormat(); |
| 167 | 100 | | String parsed = null; |
| 168 | | | try |
| 169 | | | { |
| 170 | 100 | | parsed = (String) format.parseObject(formatted); |
| 171 | | | } |
| 172 | 0 | | catch (ParseException ex) |
| 173 | | | { |
| 174 | 0 | | fail("Got a parse exception: " + ex); |
| 175 | 100 | | } |
| 176 | 100 | | if (parsed != null) |
| 177 | | | { |
| 178 | 100 | | compare(source, parsed); |
| 179 | | | } |
| 180 | 100 | | } |
| 181 | | | } |