1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
34 @SuppressWarnings("javadoc")
35 public class RowParserTest {
36
37
38
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
50
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
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
74 public static class StartIndex0 {
75 @FlatFileColumn(startIndex = 0, format = "10")
76 public String s;
77 }
78
79
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
94 public static class StartIndex2 {
95 @FlatFileColumn(startIndex = 2, format = "10")
96 public String s;
97 }
98
99
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
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
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
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
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
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 }