1 25 26 package org.archive.util; 27 28 import junit.framework.Test; 29 import junit.framework.TestCase; 30 import junit.framework.TestSuite; 31 32 38 public class PaddingStringBufferTest extends TestCase { 39 44 public PaddingStringBufferTest(final String testName) { 45 super(testName); 46 } 47 48 53 public static void main(String argv[]) { 54 junit.textui.TestRunner.run(suite()); 55 } 56 57 62 public static Test suite() { 63 return new TestSuite(PaddingStringBufferTest.class); 64 } 65 66 public void setUp() { 67 buf = new PaddingStringBuffer(); 68 } 69 70 71 public void testPadTo() { 72 PaddingStringBuffer retBuf; 73 assertEquals("nothing in buffer", "", buf.toString()); 74 retBuf = buf.padTo(5); 75 assertEquals("retBuf same as buf", retBuf, buf); 76 assertEquals("5 spaces", " ", buf.toString()); 77 78 buf.padTo(4); 80 assertEquals("5 spaces", " ", buf.toString()); 81 82 buf.padTo(10); 84 assertEquals("10 spaces", " ", buf.toString()); 85 } 86 87 88 public void testAppendString() { 89 PaddingStringBuffer retBuf; 91 assertEquals("nothing in buffer", "", buf.toString()); 92 retBuf = buf.append("foo"); 93 assertEquals("foo in buffer", "foo", buf.toString()); 94 assertEquals("retBuf good", retBuf.toString(), buf.toString()); 95 retBuf = buf.append("bar"); 96 assertEquals("foobar in buffer", "foobar", buf.toString()); 97 assertEquals("retBuf good", retBuf.toString(), buf.toString()); 98 } 99 100 101 public void testReset() { 102 assertEquals("nothing in buffer", "", buf.toString()); 104 buf.append("foo"); 105 assertEquals("buffer is 'foo'", "foo", buf.toString()); 106 buf.reset(); 107 assertEquals("nothing in buffer after reset", "", buf.toString()); 108 } 109 110 111 public void testRaAppend() { 112 PaddingStringBuffer retBuf; 114 assertEquals("nothing in buffer", "", buf.toString()); 115 retBuf = buf.raAppend(5, "foo"); 116 assertEquals("foo in buffer", " foo", buf.toString()); 117 assertEquals("retBuf good", retBuf.toString(), buf.toString()); 118 retBuf = buf.raAppend(9, "bar"); 119 assertEquals("foobar in buffer", " foo bar", buf.toString()); 120 assertEquals("retBuf good", retBuf.toString(), buf.toString()); 121 122 buf = new PaddingStringBuffer(); 124 buf.raAppend(-1, "foo"); 125 assertEquals("no padding for -1", "foo", buf.toString()); 126 buf = new PaddingStringBuffer(); 127 buf.raAppend(0, "foo"); 128 assertEquals("no padding for 0", "foo", buf.toString()); 129 130 } 131 132 133 public void testNewline(){ 134 assertEquals("nothing should be in the buffer", "", buf.toString()); 135 buf.newline(); 136 assertTrue("should contain newline", buf.toString().indexOf('\n')!=-1); 137 assertEquals("line position should be 0",0,buf.linePos); 138 } 139 140 142 public void testRaAppendWithTooLongString() { 143 buf.raAppend(3,"foobar"); 144 assertEquals("no padding when padding col less than string length", 145 "foobar", buf.toString()); 146 buf.reset(); 147 } 148 149 150 public void testRaAppendWithExactLengthString() { 151 buf.raAppend(6, "foobar"); 152 buf.raAppend(12, "foobar"); 153 assertEquals("no padding with exact length string", 154 "foobarfoobar", buf.toString()); 155 } 156 157 158 public void testAppendInt() { 159 buf.append((int)1); 160 assertEquals("buffer is '1'", "1", buf.toString()); 161 buf.append((int)234); 162 assertEquals("buffer is '1234'", "1234", buf.toString()); 163 } 164 165 166 public void testRaAppendInt() { 167 buf.raAppend(5, (int)1); 169 assertEquals("buf is ' 1'", " 1", buf.toString()); 170 172 buf.raAppend(6,(int)123); 173 assertEquals("'123' appended", " 1123", buf.toString()); 174 } 175 176 177 public void testAppendLong() { 178 buf.append((long)1); 179 assertEquals("buffer is '1'", "1", buf.toString()); 180 buf.append((long)234); 181 assertEquals("buffer is '1234'", "1234", buf.toString()); 182 } 183 184 185 public void testRaAppendLong() { 186 buf.raAppend(5, (long) 1); 188 assertEquals("buf is ' 1'", " 1", buf.toString()); 189 191 buf.raAppend(6, (long) 123); 192 assertEquals("'123' appended", " 1123", buf.toString()); 193 } 194 195 196 private PaddingStringBuffer buf; 197 } 198 199 | Popular Tags |