001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.flatfile;
018
019import static org.junit.Assert.assertEquals;
020import static org.junit.Assert.assertFalse;
021import static org.junit.Assert.assertTrue;
022import static org.junit.Assert.fail;
023
024import org.junit.Test;
025
026public class EntityArrayLifecycleTest extends EntityParserTestBase {
027
028    @Test
029    public void testMe() throws Exception {
030        EntityArray e = (EntityArray) entityFactory.getEntity("hypo");
031        try {
032            e.getValue();
033            fail("should fail");
034        } catch (IllegalStateException ise) {
035            // okay
036        }
037        assertTrue(e.isSizable());
038        e.setSize(5);
039        e.fill((byte) 'a');
040        assertValue("aaaaaaaaaa", e);
041        e = (EntityArray) entityFactory.getEntity("hypo");
042        assertTrue(e.isSizable());
043        e.setSize(2);
044        e.fill((byte) 'b');
045        assertValue("bbbb", e);
046        e = (EntityArray) entityFactory.getEntity("complete");
047        assertFalse(e.isSizable());
048        expectInvalidSize(e, e.size() + 1,
049                "should fail to set size of completed entity",
050                IllegalStateException.class);
051        e.setSize(e.size());
052        e = (EntityArray) entityFactory.getEntity("rangeMin1");
053        assertTrue(e.isSizable());
054        expectInvalidSize(e, 0, "should fail to set size < 1",
055                IllegalArgumentException.class);
056        e.setSize(665);
057        assertEquals(665, e.size());
058        e = (EntityArray) entityFactory.getEntity("explicitRange");
059        expectInvalidSize(e, 0, "should fail to set size < 1",
060                IllegalArgumentException.class);
061        expectInvalidSize(e, 4, "should fail to set size > 4",
062                IllegalArgumentException.class);
063        e.setSize(3);
064        assertEquals(3, e.size());
065        e = (EntityArray) entityFactory.getEntity("optional");
066        assertEquals(0, e.getMinimumSize());
067        assertEquals(1, e.getMaximumSize());
068        e.setSize(0);
069        assertEquals(0, e.size());
070        assertEquals(0, e.getValue().length);
071        e = (EntityArray) entityFactory.getEntity("optional2");
072        assertEquals(0, e.getMinimumSize());
073        assertEquals(1, e.getMaximumSize());
074        e.setSize(0);
075        assertEquals(0, e.size());
076        assertEquals(0, e.getValue().length);
077    }
078
079    private void expectInvalidSize(EntityArray e, int size,
080            String failureMessage, Class<? extends Exception> exceptionType) {
081        try {
082            e.setSize(size);
083            fail(failureMessage);
084        } catch (Exception exc) {
085            assertTrue("Unexpected exception " + exc, exceptionType == null
086                    || exceptionType.isInstance(exc));
087        }
088    }
089
090    protected String getSource() {
091        return "entityarraylifecycle.test";
092    }
093}