001: /* PaddingStringBufferTest
002: *
003: * $Id: PaddingStringBufferTest.java 4644 2006-09-20 22:40:21Z paul_jack $
004: *
005: * Created Tue Jan 20 14:17:59 PST 2004
006: *
007: * Copyright (C) 2004 Internet Archive.
008: *
009: * This file is part of the Heritrix web crawler (crawler.archive.org).
010: *
011: * Heritrix is free software; you can redistribute it and/or modify
012: * it under the terms of the GNU Lesser Public License as published by
013: * the Free Software Foundation; either version 2.1 of the License, or
014: * any later version.
015: *
016: * Heritrix is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: * GNU Lesser Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser Public License
022: * along with Heritrix; if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: */
025:
026: package org.archive.util;
027:
028: import junit.framework.Test;
029: import junit.framework.TestCase;
030: import junit.framework.TestSuite;
031:
032: /**
033: * JUnit test suite for PaddingStringBuffer
034: *
035: * @author <a href="mailto:me@jamesc.net">James Casey</a>
036: * @version $Id: PaddingStringBufferTest.java 4644 2006-09-20 22:40:21Z paul_jack $
037: */
038: public class PaddingStringBufferTest extends TestCase {
039: /**
040: * Create a new PaddingStringBufferTest object
041: *
042: * @param testName the name of the test
043: */
044: public PaddingStringBufferTest(final String testName) {
045: super (testName);
046: }
047:
048: /**
049: * run all the tests for PaddingStringBufferTest
050: *
051: * @param argv the command line arguments
052: */
053: public static void main(String argv[]) {
054: junit.textui.TestRunner.run(suite());
055: }
056:
057: /**
058: * return the suite of tests for PaddingStringBufferTest
059: *
060: * @return the suite of test
061: */
062: public static Test suite() {
063: return new TestSuite(PaddingStringBufferTest.class);
064: }
065:
066: public void setUp() {
067: buf = new PaddingStringBuffer();
068: }
069:
070: /** first check that padTo works ok, since all depends on it */
071: public void testPadTo() {
072: PaddingStringBuffer retBuf;
073: assertEquals("nothing in buffer", "", buf.toString());
074: retBuf = buf.padTo(5);
075: assertEquals("retBuf same as buf", retBuf, buf);
076: assertEquals("5 spaces", " ", buf.toString());
077:
078: // now do a smaller value - nothing should happen
079: buf.padTo(4);
080: assertEquals("5 spaces", " ", buf.toString());
081:
082: // now pad tro a greater length
083: buf.padTo(10);
084: assertEquals("10 spaces", " ", buf.toString());
085: }
086:
087: /** test that append(String) works correctly */
088: public void testAppendString() {
089: // a buf to hold the return buffer
090: PaddingStringBuffer retBuf;
091: assertEquals("nothing in buffer", "", buf.toString());
092: retBuf = buf.append("foo");
093: assertEquals("foo in buffer", "foo", buf.toString());
094: assertEquals("retBuf good", retBuf.toString(), buf.toString());
095: retBuf = buf.append("bar");
096: assertEquals("foobar in buffer", "foobar", buf.toString());
097: assertEquals("retBuf good", retBuf.toString(), buf.toString());
098: }
099:
100: /** check the reset method clears the buffer */
101: public void testReset() {
102: // append something into the buffer
103: 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
108: .toString());
109: }
110:
111: /** test the raAppend(String) works in the simple cases */
112: public void testRaAppend() {
113: // a buf to hold the return buffer
114: PaddingStringBuffer retBuf;
115: assertEquals("nothing in buffer", "", buf.toString());
116: retBuf = buf.raAppend(5, "foo");
117: assertEquals("foo in buffer", " foo", buf.toString());
118: assertEquals("retBuf good", retBuf.toString(), buf.toString());
119: retBuf = buf.raAppend(9, "bar");
120: assertEquals("foobar in buffer", " foo bar", buf.toString());
121: assertEquals("retBuf good", retBuf.toString(), buf.toString());
122:
123: // now check with out-of-range columns - should just append
124: buf = new PaddingStringBuffer();
125: buf.raAppend(-1, "foo");
126: assertEquals("no padding for -1", "foo", buf.toString());
127: buf = new PaddingStringBuffer();
128: buf.raAppend(0, "foo");
129: assertEquals("no padding for 0", "foo", buf.toString());
130:
131: }
132:
133: /** test the newline() */
134: public void testNewline() {
135: assertEquals("nothing should be in the buffer", "", buf
136: .toString());
137: buf.newline();
138: assertTrue("should contain newline", buf.toString().indexOf(
139: '\n') != -1);
140: assertEquals("line position should be 0", 0, buf.linePos);
141: }
142:
143: /** check what happens when we right append, but the string is longer
144: * than the space */
145: public void testRaAppendWithTooLongString() {
146: buf.raAppend(3, "foobar");
147: assertEquals(
148: "no padding when padding col less than string length",
149: "foobar", buf.toString());
150: buf.reset();
151: }
152:
153: /** check it all works with the length == the length of the string */
154: public void testRaAppendWithExactLengthString() {
155: buf.raAppend(6, "foobar");
156: buf.raAppend(12, "foobar");
157: assertEquals("no padding with exact length string",
158: "foobarfoobar", buf.toString());
159: }
160:
161: /** check that append(int) works */
162: public void testAppendInt() {
163: buf.append((int) 1);
164: assertEquals("buffer is '1'", "1", buf.toString());
165: buf.append((int) 234);
166: assertEquals("buffer is '1234'", "1234", buf.toString());
167: }
168:
169: /** check that raAppend(int) works */
170: public void testRaAppendInt() {
171: // right-append '1' to column 5
172: buf.raAppend(5, (int) 1);
173: assertEquals("buf is ' 1'", " 1", buf.toString());
174: // try appending a too-long int
175:
176: buf.raAppend(6, (int) 123);
177: assertEquals("'123' appended", " 1123", buf.toString());
178: }
179:
180: /** check that append(long) works */
181: public void testAppendLong() {
182: buf.append((long) 1);
183: assertEquals("buffer is '1'", "1", buf.toString());
184: buf.append((long) 234);
185: assertEquals("buffer is '1234'", "1234", buf.toString());
186: }
187:
188: /** check that raAppend(long) works */
189: public void testRaAppendLong() {
190: // right-append '1' to column 5
191: buf.raAppend(5, (long) 1);
192: assertEquals("buf is ' 1'", " 1", buf.toString());
193: // try appending a too-long int
194:
195: buf.raAppend(6, (long) 123);
196: assertEquals("'123' appended", " 1123", buf.toString());
197: }
198:
199: /** a temp buffer for testing with */
200: private PaddingStringBuffer buf;
201: }
|