View Javadoc

1   /*
2    * Copyright 2005,2009 Ivan SZKIBA
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.ini4j;
17  
18  import org.ini4j.sample.Dwarf;
19  import org.ini4j.sample.Dwarfs;
20  
21  import org.ini4j.test.DwarfsData;
22  import org.ini4j.test.Helper;
23  import org.ini4j.test.TaleData;
24  
25  import static org.junit.Assert.assertArrayEquals;
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertNull;
28  import static org.junit.Assert.assertSame;
29  
30  import org.junit.Test;
31  
32  public class BasicProfileSectionTest extends Ini4jCase
33  {
34      @Test public void testAddChild() throws Exception
35      {
36          Profile prof = Helper.newTaleIni();
37          Profile.Section dwarfs = prof.get(TaleData.PROP_DWARFS);
38          Profile.Section doc = dwarfs.getChild(Dwarfs.PROP_DOC);
39          Profile.Section dopey2 = doc.addChild(Dwarfs.PROP_DOPEY);
40  
41          assertSame(doc, dopey2.getParent());
42          assertSame(dopey2, dwarfs.lookup(Dwarfs.PROP_DOC, Dwarfs.PROP_DOPEY));
43          assertSame(dopey2, dwarfs.lookup(Dwarfs.PROP_DOC + '/' + Dwarfs.PROP_DOPEY));
44          assertEquals(1, doc.childrenNames().length);
45          doc.removeChild(Dwarfs.PROP_DOPEY);
46          assertEquals(0, doc.childrenNames().length);
47          assertNull(dwarfs.lookup(Dwarfs.PROP_DOC, Dwarfs.PROP_DOPEY));
48          assertNull(dwarfs.lookup(Dwarfs.PROP_DOC + '/' + Dwarfs.PROP_DOPEY));
49      }
50  
51      @Test public void testGetChild() throws Exception
52      {
53          Profile prof = Helper.newTaleIni();
54          Profile.Section dwarfs = prof.get(TaleData.PROP_DWARFS);
55  
56          assertArrayEquals(DwarfsData.dwarfNames, dwarfs.childrenNames());
57          assertSame(prof.get(TaleData.bashful.name), dwarfs.getChild(Dwarfs.PROP_BASHFUL));
58          assertSame(prof.get(TaleData.doc.name), dwarfs.getChild(Dwarfs.PROP_DOC));
59          assertSame(prof.get(TaleData.dopey.name), dwarfs.getChild(Dwarfs.PROP_DOPEY));
60          assertSame(prof.get(TaleData.grumpy.name), dwarfs.getChild(Dwarfs.PROP_GRUMPY));
61          assertSame(prof.get(TaleData.happy.name), dwarfs.getChild(Dwarfs.PROP_HAPPY));
62          assertSame(prof.get(TaleData.sleepy.name), dwarfs.getChild(Dwarfs.PROP_SLEEPY));
63          assertSame(prof.get(TaleData.sneezy.name), dwarfs.getChild(Dwarfs.PROP_SNEEZY));
64      }
65  
66      @Test public void testGetParent() throws Exception
67      {
68          Profile prof = Helper.newTaleIni();
69          Profile.Section dwarfs = prof.get(TaleData.PROP_DWARFS);
70  
71          assertNull(dwarfs.getParent());
72          assertSame(dwarfs, prof.get(TaleData.bashful.name).getParent());
73          assertSame(dwarfs, prof.get(TaleData.doc.name).getParent());
74          assertSame(dwarfs, prof.get(TaleData.dopey.name).getParent());
75          assertSame(dwarfs, prof.get(TaleData.grumpy.name).getParent());
76          assertSame(dwarfs, prof.get(TaleData.happy.name).getParent());
77          assertSame(dwarfs, prof.get(TaleData.sleepy.name).getParent());
78          assertSame(dwarfs, prof.get(TaleData.sneezy.name).getParent());
79      }
80  
81      @Test public void testLoad() throws Exception
82      {
83          Profile prof = Helper.loadTaleIni();
84          Profile.Section dwarfs = prof.get(TaleData.PROP_DWARFS);
85  
86          Helper.assertEquals(DwarfsData.bashful, dwarfs.getChild(Dwarfs.PROP_BASHFUL).as(Dwarf.class));
87          Helper.assertEquals(DwarfsData.doc, dwarfs.getChild(Dwarfs.PROP_DOC).as(Dwarf.class));
88          Helper.assertEquals(DwarfsData.dopey, dwarfs.getChild(Dwarfs.PROP_DOPEY).as(Dwarf.class));
89          Helper.assertEquals(DwarfsData.grumpy, dwarfs.getChild(Dwarfs.PROP_GRUMPY).as(Dwarf.class));
90          Helper.assertEquals(DwarfsData.happy, dwarfs.getChild(Dwarfs.PROP_HAPPY).as(Dwarf.class));
91          Helper.assertEquals(DwarfsData.sleepy, dwarfs.getChild(Dwarfs.PROP_SLEEPY).as(Dwarf.class));
92          Helper.assertEquals(DwarfsData.sneezy, dwarfs.getChild(Dwarfs.PROP_SNEEZY).as(Dwarf.class));
93      }
94  }