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.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assert.fail;
23  
24  import org.junit.Test;
25  
26  public class EntityArrayLifecycleTest extends EntityParserTestBase {
27  
28      @Test
29      public void testMe() throws Exception {
30          EntityArray e = (EntityArray) entityFactory.getEntity("hypo");
31          try {
32              e.getValue();
33              fail("should fail");
34          } catch (IllegalStateException ise) {
35              // okay
36          }
37          assertTrue(e.isSizable());
38          e.setSize(5);
39          e.fill((byte) 'a');
40          assertValue("aaaaaaaaaa", e);
41          e = (EntityArray) entityFactory.getEntity("hypo");
42          assertTrue(e.isSizable());
43          e.setSize(2);
44          e.fill((byte) 'b');
45          assertValue("bbbb", e);
46          e = (EntityArray) entityFactory.getEntity("complete");
47          assertFalse(e.isSizable());
48          expectInvalidSize(e, e.size() + 1,
49                  "should fail to set size of completed entity",
50                  IllegalStateException.class);
51          e.setSize(e.size());
52          e = (EntityArray) entityFactory.getEntity("rangeMin1");
53          assertTrue(e.isSizable());
54          expectInvalidSize(e, 0, "should fail to set size < 1",
55                  IllegalArgumentException.class);
56          e.setSize(665);
57          assertEquals(665, e.size());
58          e = (EntityArray) entityFactory.getEntity("explicitRange");
59          expectInvalidSize(e, 0, "should fail to set size < 1",
60                  IllegalArgumentException.class);
61          expectInvalidSize(e, 4, "should fail to set size > 4",
62                  IllegalArgumentException.class);
63          e.setSize(3);
64          assertEquals(3, e.size());
65          e = (EntityArray) entityFactory.getEntity("optional");
66          assertEquals(0, e.getMinimumSize());
67          assertEquals(1, e.getMaximumSize());
68          e.setSize(0);
69          assertEquals(0, e.size());
70          assertEquals(0, e.getValue().length);
71          e = (EntityArray) entityFactory.getEntity("optional2");
72          assertEquals(0, e.getMinimumSize());
73          assertEquals(1, e.getMaximumSize());
74          e.setSize(0);
75          assertEquals(0, e.size());
76          assertEquals(0, e.getValue().length);
77      }
78  
79      private void expectInvalidSize(EntityArray e, int size,
80              String failureMessage, Class<? extends Exception> exceptionType) {
81          try {
82              e.setSize(size);
83              fail(failureMessage);
84          } catch (Exception exc) {
85              assertTrue("Unexpected exception " + exc, exceptionType == null
86                      || exceptionType.isInstance(exc));
87          }
88      }
89  
90      protected String getSource() {
91          return "entityarraylifecycle.test";
92      }
93  }