View Javadoc

1   /* PaddingStringBufferTest
2    *
3    * $Id: PaddingStringBufferTest.java 4644 2006-09-20 22:40:21Z paul_jack $
4    *
5    * Created Tue Jan 20 14:17:59 PST 2004
6    *
7    * Copyright (C) 2004 Internet Archive.
8    *
9    * This file is part of the Heritrix web crawler (crawler.archive.org).
10   *
11   * Heritrix is free software; you can redistribute it and/or modify
12   * it under the terms of the GNU Lesser Public License as published by
13   * the Free Software Foundation; either version 2.1 of the License, or
14   * any later version.
15   *
16   * Heritrix is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU Lesser Public License for more details.
20   *
21   * You should have received a copy of the GNU Lesser Public License
22   * along with Heritrix; if not, write to the Free Software
23   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24   */
25  
26  package org.archive.util;
27  
28  import junit.framework.Test;
29  import junit.framework.TestCase;
30  import junit.framework.TestSuite;
31  
32  /***
33   * JUnit test suite for PaddingStringBuffer
34   *
35   * @author <a href="mailto:me@jamesc.net">James Casey</a>
36   * @version $Id: PaddingStringBufferTest.java 4644 2006-09-20 22:40:21Z paul_jack $
37   */
38  public class PaddingStringBufferTest extends TestCase {
39      /***
40       * Create a new PaddingStringBufferTest object
41       *
42       * @param testName the name of the test
43       */
44      public PaddingStringBufferTest(final String testName) {
45          super(testName);
46      }
47  
48      /***
49       * run all the tests for PaddingStringBufferTest
50       *
51       * @param argv the command line arguments
52       */
53      public static void main(String argv[]) {
54          junit.textui.TestRunner.run(suite());
55      }
56  
57      /***
58       * return the suite of tests for PaddingStringBufferTest
59       *
60       * @return the suite of test
61       */
62      public static Test suite() {
63          return new TestSuite(PaddingStringBufferTest.class);
64      }
65  
66      public void setUp() {
67          buf = new PaddingStringBuffer();
68      }
69  
70      /*** first check that padTo works ok, since all depends on it */
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          // now do a smaller value - nothing should happen
79          buf.padTo(4);
80          assertEquals("5 spaces", "     ", buf.toString());
81  
82          // now pad tro a greater length
83          buf.padTo(10);
84          assertEquals("10 spaces", "          ", buf.toString());
85      }
86  
87      /*** test that append(String) works correctly */
88      public void testAppendString() {
89          // a buf to hold the return buffer
90          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     /*** 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.toString());
108     }
109 
110     /*** test the raAppend(String) works in the simple cases */
111     public void testRaAppend() {
112         // a buf to hold the return buffer
113         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         // now check with out-of-range columns - should just append
123         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     /*** test the newline() */
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     /*** check what happens when we right append, but the string is longer
141      * than the space */
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     /*** check it all works with the length == the length of the string */
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     /*** check that append(int) works */
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     /*** check that raAppend(int) works */
166     public void testRaAppendInt() {
167         // right-append '1' to column 5
168         buf.raAppend(5, (int)1);
169         assertEquals("buf is '    1'", "    1", buf.toString());
170         // try appending a too-long int
171 
172         buf.raAppend(6,(int)123);
173         assertEquals("'123' appended", "    1123", buf.toString());
174     }
175 
176     /*** check that  append(long) works */
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     /*** check that raAppend(long) works */
185     public void testRaAppendLong() {
186         // right-append '1' to column 5
187         buf.raAppend(5, (long) 1);
188         assertEquals("buf is '    1'", "    1", buf.toString());
189         // try appending a too-long int
190 
191         buf.raAppend(6, (long) 123);
192         assertEquals("'123' appended", "    1123", buf.toString());
193     }
194 
195     /*** a temp buffer for testing with */
196     private PaddingStringBuffer buf;
197 }
198