View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.flatfile;
18  
19  import static org.junit.Assert.assertArrayEquals;
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assert.fail;
23  
24  import java.io.ByteArrayInputStream;
25  import java.io.IOException;
26  import java.util.Arrays;
27  
28  import org.apache.commons.flatfile.FieldSupport.Overflow;
29  import org.apache.commons.flatfile.FieldSupport.Underflow;
30  import org.junit.Test;
31  
32  /**
33   * Test dynamic fields.
34   */
35  public class DynamicFieldTest extends EntityParserTestBase {
36      private static final int BUFFER_SIZE = 20;
37  
38      private static final byte[] SMALL_BYTES = new byte[0];
39      private static final byte[] LARGE_BYTES = new byte[1024];
40  
41      static {
42          Arrays.fill(LARGE_BYTES, (byte) 'a');
43      }
44  
45      @Test
46      public void test1() throws Exception {
47          DynamicField df = get("df1");
48          assertEquals(0, df.length());
49          assertBounds(df, 0, 1);
50          assertLoad(df);
51      }
52  
53      @Test
54      public void test2() throws Exception {
55          DynamicField df = get("df2");
56          assertEquals(0, df.length());
57          assertBounds(df, DynamicField.Bounds.DEFAULT.getMinimum(), 1);
58          assertLoad(df);
59      }
60  
61      @Test
62      public void test3() throws Exception {
63          DynamicField df = get("df3");
64          assertEquals(0, df.length());
65          assertBounds(df, 0, DynamicField.Bounds.DEFAULT.getMaximum());
66          assertLoad(df);
67      }
68  
69      @Test
70      public void test4() throws Exception {
71          DynamicField df = get("df4");
72          assertEquals(0, df.length());
73          assertBounds(df, DynamicField.Bounds.DEFAULT);
74          assertLoad(df);
75      }
76  
77      @Test
78      public void test5() throws Exception {
79          DynamicField df = get("df5");
80          assertEquals(3, df.length());
81          byte[] foo = "foo".getBytes();
82          assertArrayEquals(foo, df.getValue());
83          assertBounds(df, DynamicField.Bounds.DEFAULT);
84          assertLoad(df);
85      }
86  
87      @Test
88      public void test6() throws Exception {
89          DynamicField df = get("df6");
90          assertEquals(0, df.length());
91          assertBounds(df, 0, 0);
92          assertLoad(df);
93      }
94  
95      @Test
96      public void test7() throws Exception {
97          DynamicField df = get("df7");
98          assertEquals(0, df.length());
99          assertBounds(df, DynamicField.Bounds.DEFAULT.getMinimum(), 0);
100         assertLoad(df);
101     }
102 
103     @Test
104     public void test8() throws Exception {
105         DynamicField df = get("df8");
106         assertBounds(df, 1, 1);
107         assertArrayEquals(new byte[] { ' ' }, df.getValue());
108         assertLoad(df);
109     }
110 
111     @Test
112     public void test9() throws Exception {
113         DynamicField df = get("df9");
114         assertBounds(df, 1, 3);
115         assertArrayEquals(" ".getBytes(), df.getValue());
116         assertLoad(df);
117     }
118 
119     @Test
120     public void testUnboundedArray() throws Exception {
121         EntityArray a = (EntityArray) entityFactory.getEntity("unboundedArray");
122         assertEquals(0, a.length());
123         a.getChild(1).setValue("foo".getBytes());
124         assertArrayEquals("foo".getBytes(), a.getValue());
125     }
126 
127     @Test
128     public void testUnboundedDelimitedArray() throws Exception {
129         EntityArray a = (EntityArray) entityFactory
130                 .getEntity("unboundedDelimitedArray");
131         assertArrayEquals("---".getBytes(), a.getValue());
132         a.getChild(1).setValue("foo".getBytes());
133         assertArrayEquals("-foo--".getBytes(), a.getValue());
134     }
135 
136     @Test
137     public void testBoundedArray() throws Exception {
138         EntityArray a = (EntityArray) entityFactory.getEntity("boundedArray");
139         assertArrayEquals("   ".getBytes(), a.getValue());
140         a.getChild(1).setValue("foo".getBytes());
141         assertArrayEquals(" foo ".getBytes(), a.getValue());
142     }
143 
144     @Test
145     public void testBoundedDelimitedArray() throws Exception {
146         EntityArray a = (EntityArray) entityFactory
147                 .getEntity("boundedDelimitedArray");
148         assertArrayEquals(" \n \n \n".getBytes(), a.getValue());
149         a.getChild(1).setValue("foo".getBytes());
150         assertArrayEquals(" \nfoo\n \n".getBytes(), a.getValue());
151 
152     }
153 
154     @Test
155     public void testNestedUncertainty() throws Exception {
156         EntityArray a = (EntityArray) entityFactory
157                 .getEntity("nestedUncertainty");
158         assertTrue(a.isSizable());
159         a.setSize(10);
160         assertArrayEquals(SMALL_BYTES, a.getValue());
161         StringBuffer buf = new StringBuffer();
162         for (int i = 0; i < 10; i++) {
163             byte[] b = new byte[i + 1];
164             Arrays.fill(b, (byte) ((int) 'a' + i));
165             buf.append(new String(b));
166             a.getChild(i).setValue(b);
167         }
168         assertArrayEquals(buf.toString().getBytes(), a.getValue());
169     }
170 
171     private void assertBounds(DynamicField df, int min, int max) {
172         assertBounds(df, new DynamicField.Bounds(min, max));
173     }
174 
175     private void assertBounds(DynamicField df, DynamicField.Bounds bounds) {
176         assertEquals(bounds, df.getBounds());
177         testValue(df, SMALL_BYTES);
178         testValue(df, LARGE_BYTES);
179     }
180 
181     private void assertLoad(DynamicField df) throws IOException {
182         df.readFrom(new ByteArrayInputStream(LARGE_BYTES));
183         int max = df.getBounds().getMaximum();
184         int expectedLength = Math.min(max, LARGE_BYTES.length);
185         assertEquals(expectedLength, df.length());
186         byte[] expec;
187         if (LARGE_BYTES.length < max) {
188             expec = LARGE_BYTES;
189         } else {
190             expec = new byte[max];
191             System.arraycopy(LARGE_BYTES, 0, expec, 0, expec.length);
192         }
193         assertArrayEquals(expec, df.getValue());
194     }
195 
196     private void testValue(DynamicField df, byte[] value) {
197         int len = value.length;
198         int min = df.getBounds().getMinimum();
199         int max = df.getBounds().getMaximum();
200         if (df.getUnderflow() == Underflow.ERROR && len < min
201                 || df.getOverflow() == Overflow.ERROR && len > max) {
202             byte[] old = df.getValue();
203             try {
204                 df.setValue(value);
205                 fail(new String(value));
206             } catch (IllegalArgumentException e) {
207                 assertArrayEquals(old, df.getValue());
208             }
209         } else {
210             df.setValue(value);
211             assertTrue(len < min || len > max
212                     || Arrays.equals(value, df.getValue()));
213         }
214     }
215 
216     private DynamicField get(String name) {
217         return (DynamicField) entityFactory.getEntity(name);
218     }
219 
220     protected String getSource() {
221         return "dynamicField.test";
222     }
223 
224 }