1   /*
2    * Copyright 2004 the original author or authors.
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 com.ancientprogramming.fixedformat4j.format.impl;
17  
18  import com.ancientprogramming.fixedformat4j.exception.FixedFormatException;
19  import com.ancientprogramming.fixedformat4j.format.FixedFormatManager;
20  import com.ancientprogramming.fixedformat4j.format.ParseException;
21  import junit.framework.Assert;
22  import junit.framework.TestCase;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  import java.math.BigDecimal;
27  import java.util.Calendar;
28  
29  /**
30   * @author Jacob von Eyben - http://www.ancientprogramming.com
31   * @since 1.0.0
32   */
33  public class TestFixedFormatManagerImpl extends TestCase {
34  
35    private static final Log LOG = LogFactory.getLog(TestFixedFormatManagerImpl.class);
36  
37    private static String STR = "some text ";
38  
39    public static final String MY_RECORD_DATA = "some text 0012320080514CT001100000010350000002056-0012 01200000002056";
40    public static final String MULTIBLE_RECORD_DATA = "some      2008101320081013                       0100";
41    public static final String MULTIBLE_RECORD_DATA_X_PADDED = "some      2008101320081013xxxxxxxxxxxxxxxxxxxxxxx0100";
42  
43    FixedFormatManager manager = null;
44  
45    @Override
46    protected void setUp() throws Exception {
47      super.setUp();
48      manager = new FixedFormatManagerImpl();
49    }
50  
51    public void testLoadRecord() {
52      MyRecord loadedRecord = manager.load(MyRecord.class, MY_RECORD_DATA);
53      Assert.assertNotNull(loadedRecord);
54      Assert.assertEquals(STR, loadedRecord.getStringData());
55      Assert.assertTrue(loadedRecord.isBooleanData());
56    }
57  
58    public void testLoadMultibleFieldsRecord() {
59      //when reading data having multible field annotations the first field will decide what data to return
60      Calendar someDay = Calendar.getInstance();
61      someDay.set(2008, 9, 13, 0, 0, 0);
62      someDay.set(Calendar.MILLISECOND, 0);
63      MultibleFieldsRecord loadedRecord = manager.load(MultibleFieldsRecord.class, MULTIBLE_RECORD_DATA);
64      Assert.assertNotNull(loadedRecord);
65      Assert.assertEquals("some      ", loadedRecord.getStringData());
66      Assert.assertEquals(someDay.getTime(), loadedRecord.getDateData());
67    }
68  
69    public void testExportRecordObject() {
70      Calendar someDay = Calendar.getInstance();
71      someDay.set(2008, 4, 14, 0, 0, 0);
72      someDay.set(Calendar.MILLISECOND, 0);
73  
74      MyRecord myRecord = new MyRecord();
75      myRecord.setBooleanData(true);
76      myRecord.setCharData('C');
77      myRecord.setDateData(someDay.getTime());
78      myRecord.setDoubleData(10.35);
79      myRecord.setFloatData(20.56F);
80      myRecord.setLongData(11L);
81      myRecord.setIntegerData(123);
82      myRecord.setStringData("some text ");
83      myRecord.setBigDecimalData(new BigDecimal(-12.012));
84      myRecord.setSimpleFloatData(20.56F);
85      Assert.assertEquals("wrong record exported", MY_RECORD_DATA, manager.export(myRecord));
86    }
87  
88    public void testExportMultibleFieldRecordObject() {
89      Calendar someDay = Calendar.getInstance();
90      someDay.set(2008, 9, 13, 0, 0, 0);
91      someDay.set(Calendar.MILLISECOND, 0);
92  
93      MultibleFieldsRecord multibleFieldsRecord = new MultibleFieldsRecord();
94      multibleFieldsRecord.setDateData(someDay.getTime());
95      multibleFieldsRecord.setStringData("some      ");
96      multibleFieldsRecord.setIntegerdata(100);
97      manager.export(multibleFieldsRecord);
98      Assert.assertEquals("wrong record exported", MULTIBLE_RECORD_DATA, manager.export(multibleFieldsRecord));
99    }
100 
101   public void testExportIntoExistingString() {
102     Calendar someDay = Calendar.getInstance();
103     someDay.set(2008, 9, 13, 0, 0, 0);
104     someDay.set(Calendar.MILLISECOND, 0);
105 
106     MultibleFieldsRecord multibleFieldsRecord = new MultibleFieldsRecord();
107     multibleFieldsRecord.setDateData(someDay.getTime());
108     multibleFieldsRecord.setStringData("some      ");
109     multibleFieldsRecord.setIntegerdata(100);
110     String exportedString = manager.export("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", multibleFieldsRecord);
111     Assert.assertEquals("wrong record exported", MULTIBLE_RECORD_DATA_X_PADDED, exportedString);
112   }
113 
114   public void testLoadNonRecordAnnotatedClass() {
115     try {
116       manager.load(String.class, "some");
117     } catch (FixedFormatException e) {
118       //expected
119     }
120   }
121 
122   public void testExportAnnotatedNestedClass() {
123     MyRecord.MyStaticNestedClass myStaticNestedClass = new MyRecord.MyStaticNestedClass();
124     myStaticNestedClass.setStringData("xyz");
125     String exportedString = manager.export(myStaticNestedClass);
126     Assert.assertEquals("xyz       ", exportedString);
127 
128     NoDefaultConstructorClass.MyStaticNestedClass myStaticNestedClass2 = new NoDefaultConstructorClass.MyStaticNestedClass();
129     myStaticNestedClass2.setStringData("xyz");
130     String exportedString2 = manager.export(myStaticNestedClass2);
131     Assert.assertEquals("xyz       ", exportedString2);
132   }
133 
134   public void testExportAnnotatedInnerClass() {
135     MyRecord myRecord = new MyRecord();
136     MyRecord.MyInnerClass myInnerClass = myRecord.new MyInnerClass();
137     myInnerClass.setStringData("xyz");
138     String exportedString = manager.export(myInnerClass);
139     Assert.assertEquals("xyz       ", exportedString);
140  
141     NoDefaultConstructorClass noDefaultConstructorClass = new NoDefaultConstructorClass("foobar");
142     NoDefaultConstructorClass.MyInnerClass myInnerClass2 = noDefaultConstructorClass.new MyInnerClass();
143     myInnerClass2.setStringData("xyz");
144     exportedString = manager.export(myInnerClass2);
145     Assert.assertEquals("xyz       ", exportedString);
146   }
147 
148   public void testImportAnnotatedNestedClass() {
149     MyRecord.MyStaticNestedClass staticNested = manager.load(MyRecord.MyStaticNestedClass.class, "xyz       ");
150     Assert.assertEquals("xyz", staticNested.getStringData());
151 
152     NoDefaultConstructorClass.MyStaticNestedClass staticNested2 = manager.load(NoDefaultConstructorClass.MyStaticNestedClass.class, "xyz       ");
153     Assert.assertEquals("xyz", staticNested2.getStringData());
154   }
155 
156   public void testImportAnnotatedInnerClass() {
157     MyRecord.MyInnerClass inner = manager.load(MyRecord.MyInnerClass.class, "xyz       ");
158     Assert.assertEquals("xyz", inner.getStringData());
159 
160 
161     try {
162       manager.load(NoDefaultConstructorClass.MyInnerClass.class, "xyz       ");
163       fail(String.format("expected an %s exception to be thrown", FixedFormatException.class.getName()));
164     } catch (FixedFormatException e) {
165       //expected this
166     }
167   }
168 
169   public void testParseFail() {
170     try {
171       manager.load(MyRecord.class, "foobarfoobarfoobarfoobar");
172       fail("expected parse exception");
173     } catch (ParseException e) {
174       if (LOG.isDebugEnabled()) {
175         LOG.debug("bu", e);
176       }
177       //expected
178     }
179   }
180 }