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.*;
19  
20  import java.io.File;
21  import java.util.Calendar;
22  
23  import org.aludratest.content.flat.PrefixRowType;
24  import org.aludratest.content.flat.data.FlatFileBeanData;
25  import org.aludratest.content.flat.data.WrappedRowData;
26  import org.aludratest.service.AbstractAludraServiceTest;
27  import org.aludratest.service.flatfile.FlatFileReader;
28  import org.aludratest.service.flatfile.FlatFileService;
29  import org.aludratest.service.flatfile.FlatFileWriter;
30  import org.databene.commons.IOUtil;
31  import org.databene.commons.ReaderLineIterator;
32  import org.databene.commons.TimeUtil;
33  import org.junit.Test;
34  
35  /**
36   * Tests the {@link FlatFileService}.
37   * @author Volker Bergmann
38   */
39  @SuppressWarnings("javadoc")
40  public class FlatFileServiceIntegrationTest extends AbstractAludraServiceTest {
41  
42      /** Tests the BeanFlatFileWriter with FlatFileBeans. */
43      @Test
44      public void testAnnotationBeanWriter() throws Exception {
45          // set up the FlatFileService
46          FlatFileService service = getLoggingService(FlatFileService.class, "annotest");
47  
48          // GIVEN an ArrayFlatFileWriter
49          String fileName = "target" + File.separator + getClass().getSimpleName() + ".anno.fcw";
50          MyFlatFileWriter writer = new MyFlatFileWriter(fileName, service, true);
51  
52          // WHEN persisting 2 FFPerson and 1 FFAddress beans
53          writer.writeRow(new FFFlatPerson("Alice", 23, TimeUtil.date(1991, Calendar.JANUARY, 6), "Miez"));
54          writer.writeRow(new FFFlatPerson("Bob", 34, TimeUtil.date(1980, Calendar.FEBRUARY, 3), "Hasso"));
55          writer.writeRow(new FFAddress("MAIN STREET 321", "NEW YORK", 123.45));
56          writer.close();
57  
58          // THEN the data shall be formatted properly according to the individual formats.
59          ReaderLineIterator iterator = new ReaderLineIterator(IOUtil.getReaderForURI(fileName));
60          assertTrue(iterator.hasNext());
61          assertEquals("PAlice               02319910106Miez    ", iterator.next());
62          assertTrue(iterator.hasNext());
63          assertEquals("PBob                 03419800203Hasso   ", iterator.next());
64          assertTrue(iterator.hasNext());
65          assertEquals("AMAIN STREET 321     NEW YORK            0123.45", iterator.next());
66          assertFalse(iterator.hasNext());
67  
68          // tear down the FlatFileService
69          IOUtil.close(service);
70          assertNotFailed();
71      }
72  
73      /** Tests the BeanFlatFileReader with FlatFileBeans */
74      @Test
75      public void testAnnotationBeanReader() throws Exception {
76          // set up the FlatFileService
77          FlatFileService service = getLoggingService(FlatFileService.class, "annotest");
78  
79          String filePath = getClass().getPackage().getName().replace('.', File.separatorChar) + File.separator + "reader_test.flat";
80          FlatFileReader<FlatFileBeanData> reader = new FlatFileReader<FlatFileBeanData>(filePath, service) {
81          };
82          reader.addRowType(new PrefixRowType(FFFlatPerson.class, "P"));
83          reader.addRowType(new PrefixRowType(FFAddress.class, "A"));
84          assertNotFailed();
85  
86          WrappedRowData row = new WrappedRowData();
87          reader.readRow(row);
88          assertEquals(new FFFlatPerson("Alice", 23, TimeUtil.date(1991, 0, 6), "Miez"), row.getValue());
89          reader.readRow(row);
90          assertEquals(new FFFlatPerson("Bob", 34, TimeUtil.date(1980, 1, 3), "Hasso"), row.getValue());
91          reader.readRow(row);
92          assertEquals(new FFAddress("MAIN STREET 321", "NEW YORK", 123.45), row.getValue());
93          reader.readRow(row);
94          assertNull(row.getValue());
95          reader.close();
96  
97          // tear down the FlatFileService
98          IOUtil.close(service);
99          assertNotFailed();
100     }
101     
102     public static final class MyFlatFileWriter extends FlatFileWriter<FlatFileBeanData, MyFlatFileWriter> {
103         public MyFlatFileWriter(String filePath, FlatFileService service, boolean overwrite) {
104             super(filePath, service, overwrite);
105         }
106     }
107     
108 }