1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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 }