1   package com.generationjava.io;
2   
3   import junit.framework.Test;
4   import junit.framework.TestCase;
5   import junit.framework.TestSuite;
6   import junit.textui.TestRunner;
7   
8   import java.io.IOException;
9   import java.io.StringReader;
10  
11  public class FixedWidthReaderTest extends TestCase {
12  
13      public FixedWidthReaderTest(String name) {
14          super(name);
15      }
16  
17      //-----------------------------------------------------------------------
18      // To test: 
19      //   SimpleAlias(Object) -> getAlias()
20  
21      public void testRead() {
22          try {
23              String test = "hello*one*ant*8*beer***amonggrass**";
24              StringReader rdr = new StringReader(test);
25              FixedWidthReader fwr = new FixedWidthReader(rdr);
26      //        fwr.setWidths( new int[] {5,5,4,2,0,5,7} );
27              String widths = "5,5,4,2,*,5,7";
28              fwr.setWidths( widths );
29              fwr.setTrim( "*" );
30              Object[] objs = null;
31              while( (objs = fwr.readLine()) != null ) {
32                  assertEquals("hello", objs[0]);
33                  assertEquals("one", objs[1]);
34                  assertEquals("ant", objs[2]);
35                  assertEquals("8", objs[3]);
36                  assertEquals("beer", objs[4]);
37                  assertEquals("among", objs[5]);
38                  assertEquals("grass", objs[6]);
39              }
40          } catch(IOException ioe) {
41              ioe.printStackTrace();
42          }
43      }
44  
45  }