001 /*
002 * Copyright 2005,2009 Ivan SZKIBA
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.ini4j;
017
018 import org.ini4j.sample.Dwarf;
019 import org.ini4j.sample.Dwarfs;
020
021 import org.ini4j.test.DwarfsData;
022 import org.ini4j.test.Helper;
023 import org.ini4j.test.TaleData;
024
025 import static org.junit.Assert.assertArrayEquals;
026 import static org.junit.Assert.assertEquals;
027 import static org.junit.Assert.assertNull;
028 import static org.junit.Assert.assertSame;
029
030 import org.junit.Test;
031
032 public class BasicProfileSectionTest extends Ini4jCase
033 {
034 @Test public void testAddChild() throws Exception
035 {
036 Profile prof = Helper.newTaleIni();
037 Profile.Section dwarfs = prof.get(TaleData.PROP_DWARFS);
038 Profile.Section doc = dwarfs.getChild(Dwarfs.PROP_DOC);
039 Profile.Section dopey2 = doc.addChild(Dwarfs.PROP_DOPEY);
040
041 assertSame(doc, dopey2.getParent());
042 assertSame(dopey2, dwarfs.lookup(Dwarfs.PROP_DOC, Dwarfs.PROP_DOPEY));
043 assertSame(dopey2, dwarfs.lookup(Dwarfs.PROP_DOC + '/' + Dwarfs.PROP_DOPEY));
044 assertEquals(1, doc.childrenNames().length);
045 doc.removeChild(Dwarfs.PROP_DOPEY);
046 assertEquals(0, doc.childrenNames().length);
047 assertNull(dwarfs.lookup(Dwarfs.PROP_DOC, Dwarfs.PROP_DOPEY));
048 assertNull(dwarfs.lookup(Dwarfs.PROP_DOC + '/' + Dwarfs.PROP_DOPEY));
049 }
050
051 @Test public void testGetChild() throws Exception
052 {
053 Profile prof = Helper.newTaleIni();
054 Profile.Section dwarfs = prof.get(TaleData.PROP_DWARFS);
055
056 assertArrayEquals(DwarfsData.dwarfNames, dwarfs.childrenNames());
057 assertSame(prof.get(TaleData.bashful.name), dwarfs.getChild(Dwarfs.PROP_BASHFUL));
058 assertSame(prof.get(TaleData.doc.name), dwarfs.getChild(Dwarfs.PROP_DOC));
059 assertSame(prof.get(TaleData.dopey.name), dwarfs.getChild(Dwarfs.PROP_DOPEY));
060 assertSame(prof.get(TaleData.grumpy.name), dwarfs.getChild(Dwarfs.PROP_GRUMPY));
061 assertSame(prof.get(TaleData.happy.name), dwarfs.getChild(Dwarfs.PROP_HAPPY));
062 assertSame(prof.get(TaleData.sleepy.name), dwarfs.getChild(Dwarfs.PROP_SLEEPY));
063 assertSame(prof.get(TaleData.sneezy.name), dwarfs.getChild(Dwarfs.PROP_SNEEZY));
064 }
065
066 @Test public void testGetParent() throws Exception
067 {
068 Profile prof = Helper.newTaleIni();
069 Profile.Section dwarfs = prof.get(TaleData.PROP_DWARFS);
070
071 assertNull(dwarfs.getParent());
072 assertSame(dwarfs, prof.get(TaleData.bashful.name).getParent());
073 assertSame(dwarfs, prof.get(TaleData.doc.name).getParent());
074 assertSame(dwarfs, prof.get(TaleData.dopey.name).getParent());
075 assertSame(dwarfs, prof.get(TaleData.grumpy.name).getParent());
076 assertSame(dwarfs, prof.get(TaleData.happy.name).getParent());
077 assertSame(dwarfs, prof.get(TaleData.sleepy.name).getParent());
078 assertSame(dwarfs, prof.get(TaleData.sneezy.name).getParent());
079 }
080
081 @Test public void testLoad() throws Exception
082 {
083 Profile prof = Helper.loadTaleIni();
084 Profile.Section dwarfs = prof.get(TaleData.PROP_DWARFS);
085
086 Helper.assertEquals(DwarfsData.bashful, dwarfs.getChild(Dwarfs.PROP_BASHFUL).as(Dwarf.class));
087 Helper.assertEquals(DwarfsData.doc, dwarfs.getChild(Dwarfs.PROP_DOC).as(Dwarf.class));
088 Helper.assertEquals(DwarfsData.dopey, dwarfs.getChild(Dwarfs.PROP_DOPEY).as(Dwarf.class));
089 Helper.assertEquals(DwarfsData.grumpy, dwarfs.getChild(Dwarfs.PROP_GRUMPY).as(Dwarf.class));
090 Helper.assertEquals(DwarfsData.happy, dwarfs.getChild(Dwarfs.PROP_HAPPY).as(Dwarf.class));
091 Helper.assertEquals(DwarfsData.sleepy, dwarfs.getChild(Dwarfs.PROP_SLEEPY).as(Dwarf.class));
092 Helper.assertEquals(DwarfsData.sneezy, dwarfs.getChild(Dwarfs.PROP_SNEEZY).as(Dwarf.class));
093 }
094 }