View Javadoc
1   /*
2    * Copyright (C) 2010-2014 Hamburg Sud and the contributors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.aludratest.service.flatfile;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.fail;
20  
21  import java.util.Locale;
22  
23  import org.aludratest.content.flat.FlatFileColumn;
24  import org.aludratest.content.flat.PrefixRowType;
25  import org.aludratest.content.flat.webdecs.RowParser;
26  import org.aludratest.exception.AutomationException;
27  import org.databene.commons.TimeUtil;
28  import org.junit.Test;
29  
30  /**
31   * Tests the {@link RowParser}.
32   * @author Volker Bergmann
33   */
34  @SuppressWarnings("javadoc")
35  public class RowParserTest {
36  
37      /** Tests the mapping of different classes with direct access 
38       *  to public attributes of the most important simple types. */
39      @Test
40      public void testAttributesAndBeanClasses() {
41          RowParser parser = new RowParser(Locale.US);
42          parser.addRowType(new PrefixRowType(FFFlatPerson.class, "P"));
43          parser.addRowType(new PrefixRowType(FFAddress.class, "A"));
44          assertEquals(new FFFlatPerson("Alice", 23, TimeUtil.date(1991, 0, 6), "Miez"), parser.parseRow("PAlice               02319910106Miez    "));
45          assertEquals(new FFFlatPerson("Bob", 34, TimeUtil.date(1980, 1, 3), "Hasso"), parser.parseRow("PBob                 03419800203Hasso   "));
46          assertEquals(new FFAddress("MAIN STREET 321", "NEW YORK", 123.45), parser.parseRow("AMAIN STREET 321     NEW YORK            0123.45"));
47      }
48  
49      /** Tests access to JavaBean properties with the format specification 
50       *  attached to the property's underlying attribute */
51      @Test
52      public void testProperties() {
53          RowParser parser = new RowParser(Locale.US);
54          parser.addRowType(new PrefixRowType(FFAddressBean.class, "A"));
55          assertEquals(new FFAddressBean("MAIN STREET 321", "NEW YORK", 123.45), parser.parseRow("AMAIN STREET 321     NEW YORK            0123.45"));
56          assertEquals(new FFAddressBean("2ND STREET 23", "NEW JERSEY", 0.5), parser.parseRow("A2ND STREET 23       NEW JERSEY          0000.50"));
57      }
58  
59      /** Tests a flat file attribute configured to be at column 0. */
60      @Test
61      public void testFirstRowAtIndex0() {
62          try {
63              RowParser parser = new RowParser(Locale.US);
64              parser.addRowType(new FixedRowType(StartIndex0.class));
65              parser.parseRow("");
66              fail("Expected " + AutomationException.class.getName());
67          } catch (AutomationException e) {
68              assertEquals("Flat file column indices must start at index 1, " + "but the first column 'org.aludratest.service." + "flatfile.RowParserTest$StartIndex0.s' starts at index 0",
69                      e.getMessage());
70          }
71      }
72  
73      /** FlatFileBean class with a startIndex of 0 */
74      public static class StartIndex0 {
75          @FlatFileColumn(startIndex = 0, format = "10")
76          public String s;
77      }
78  
79      /** Tests a flat file attribute configured to be at column 2. */
80      @Test
81      public void testFirstRowAtIndex2() {
82          try {
83              RowParser parser = new RowParser(Locale.US);
84              parser.addRowType(new FixedRowType(StartIndex2.class));
85              parser.parseRow("");
86              fail("Expected " + AutomationException.class.getName());
87          } catch (AutomationException e) {
88              assertEquals("Flat file column indices must start at index 1, " + "but the first column 'org.aludratest.service." + "flatfile.RowParserTest$StartIndex2.s' starts at index 2",
89                      e.getMessage());
90          }
91      }
92  
93      /** FlatFileBean class with a startIndex of 2. */
94      public static class StartIndex2 {
95          @FlatFileColumn(startIndex = 2, format = "10")
96          public String s;
97      }
98  
99      /** Tests a FlatFileBean with a gap between its attributes' startIndices. */
100     @Test
101     public void testIndexGap() {
102         try {
103             RowParser parser = new RowParser(Locale.US);
104             parser.addRowType(new FixedRowType(IndexGap.class));
105             parser.parseRow("");
106             fail("Expected " + AutomationException.class.getName());
107         } catch (AutomationException e) {
108             assertEquals("Flat file column 'org.aludratest." + "service.flatfile.RowParserTest$IndexGap.s2' " + "is expected at index 11, but was found at index 12", e.getMessage());
109         }
110     }
111 
112     /** FlatFileBean class with a gap between its attributes' startIndices. */
113     public static class IndexGap {
114         @FlatFileColumn(startIndex = 1, format = "10")
115         public String s1;
116         @FlatFileColumn(startIndex = 12, format = "10")
117         public String s2;
118     }
119 
120     /** Tests a FlatFileBean class with attributes that have overlapping column ranges. */
121     @Test
122     public void testIndexOverlap() {
123         try {
124             RowParser parser = new RowParser(Locale.US);
125             parser.addRowType(new FixedRowType(IndexOverlap.class));
126             parser.parseRow("");
127             fail("Expected " + AutomationException.class.getName());
128         } catch (AutomationException e) {
129             assertEquals("Flat file column 'org.aludratest.service.flatfile.RowParserTest$IndexOverlap.s2' is expected at index 11, " + "but was found at index 10", e.getMessage());
130         }
131     }
132 
133     /** FlatFileBean class with attributes that have overlapping column ranges. */
134     public static class IndexOverlap {
135         @FlatFileColumn(startIndex = 1, format = "10")
136         public String s1;
137         @FlatFileColumn(startIndex = 10, format = "10")
138         public String s2;
139     }
140 
141     /** Tests a FlatFileBean class with different attributes of the same startIndex */
142     @Test
143     public void testDuplicateIndex() {
144         try {
145             RowParser parser = new RowParser(Locale.US);
146             parser.addRowType(new FixedRowType(DuplicateIndex.class));
147             parser.parseRow("");
148             fail("Expected " + AutomationException.class.getName());
149         } catch (AutomationException e) {
150             assertEquals("Multiple column definitions at index 1: " + "'s1', and 's2'", e.getMessage());
151         }
152     }
153 
154     /** FlatFileBean class with different attributes of the same startIndex. */
155     public static class DuplicateIndex {
156         @FlatFileColumn(startIndex = 1, format = "10")
157         public String s1;
158         @FlatFileColumn(startIndex = 1, format = "8")
159         public String s2;
160     }
161 
162 }