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;
020
021import org.junit.Test;
022
023/**
024 *
025 */
026public class ValuesTest extends EntityParserTestBase {
027
028    @Test
029    public void testValues() throws Exception {
030        EntityMap values = (EntityMap) entityFactory.getEntity("values");
031        assertValue("A", values.getChild("A"));
032        assertValue("BB", values.getChild("BB"));
033        assertValue("CCC", values.getChild("CCC"));
034        assertValue("DDDD", values.getChild("DDDD"));
035        assertValue("AAAA", values.getChild("A4"));
036        assertValue("BBBB", values.getChild("B4"));
037        assertValue("CCCC", values.getChild("C4"));
038        assertValue("DDDD", values.getChild("D4"));
039        testArray((EntityArray) ((EntityMap) values.getChild("initarray"))
040                .getChild("array"));
041        testArray((EntityArray) values.getChild("array"));
042        assertValue("twv", values.getChild("x"));
043        assertValue("vwt", values.getChild("y"));
044        assertValue("123123", values.getChild("z"));
045        assertValue("twvtwv", values.getChild("typeWithValue"));
046    }
047
048    private void testArray(EntityArray array) throws Exception {
049        assertValue("123456", array);
050        assertValue("12", array.getChild(0));
051        assertValue("34", array.getChild(1));
052        assertValue("56", array.getChild(2));
053    }
054
055    @Test
056    public void testImplicitLength() throws Exception {
057        Entity e = entityFactory.getEntity("implicitLength");
058        assertEquals(12, e.length());
059        assertValue("foo-bar-baz!", e);
060    }
061
062    @Test
063    public void testOverrideChildren() throws Exception {
064        Entity e = entityFactory.getEntity("overrideChildren");
065        assertEquals(45, e.length());
066        assertEquals("", new String(e.getValue()).trim());
067    }
068
069    protected String getSource() {
070        return "values.test";
071    }
072}