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.issues;
17  
18  import com.ancientprogramming.fixedformat4j.annotation.Align;
19  import com.ancientprogramming.fixedformat4j.annotation.Field;
20  import com.ancientprogramming.fixedformat4j.annotation.Record;
21  import com.ancientprogramming.fixedformat4j.format.FixedFormatManager;
22  import com.ancientprogramming.fixedformat4j.format.impl.FixedFormatManagerImpl;
23  import junit.framework.TestCase;
24  import org.junit.Test;
25  
26  /**
27   * Verifies Issue 9
28   * 
29   * @author Jacob von Eyben - http://www.ancientprogramming.com
30   * @since 1.2.1
31   */
32  public class TestIssue9 extends TestCase {
33  
34    FixedFormatManager fixedFormatManager = new FixedFormatManagerImpl();
35  
36    @Test
37    public void testLastColumnIsIgnored() {
38      String text21 = "1234567890some       ";
39      String text20 = "1234567890some      ";
40      String text19 = "1234567890some     ";
41      String text11 = "1234567890x";
42      String text10 = "1234567890";
43      String text9 = "123456789";
44  
45      assertText(1234567890, "some", fixedFormatManager.load(Issue9.class, text21));
46      assertText(1234567890, "some", fixedFormatManager.load(Issue9.class, text20));  
47      assertText(1234567890, "some", fixedFormatManager.load(Issue9.class, text19));
48      assertText(1234567890, "x", fixedFormatManager.load(Issue9.class, text11));
49      assertText(1234567890, null, fixedFormatManager.load(Issue9.class, text10));
50      assertText(123456789, null, fixedFormatManager.load(Issue9.class, text9));
51  
52    }
53  
54    private void assertText(int expectedNumber, String expectedString, Issue9 actual) {
55      assertEquals(expectedNumber, actual.getNumber());
56      assertEquals(expectedString, actual.getString());
57    }
58  
59  
60    @Record
61    public static class Issue9 {
62  
63      private int number;
64      private String string;
65  
66  
67  
68      @Field(offset = 1, length = 10, align = Align.RIGHT, paddingChar = '0')
69      public int getNumber() {
70        return number;
71      }
72  
73      public void setNumber(int number) {
74        this.number = number;
75      }
76  
77  
78      @Field(offset = 11, length = 10)
79      public String getString() {
80        return string;
81      }
82  
83      public void setString(String string) {
84        this.string = string;
85      }
86    }
87  }