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 static org.junit.Assert.*;
22  
23  import org.junit.Test;
24  
25  import java.io.File;
26  
27  import java.util.prefs.BackingStoreException;
28  
29  /**
30   * JUnit test of IniFile class.
31   */
32  public class IniFileTest
33  {
34  
35      /**
36       * Test of various error conditions.
37       *
38       * @throws Exception on error
39       */
40      @SuppressWarnings("empty-statement")
41      @Test public void testErrors() throws Exception
42      {
43          File tmp = File.createTempFile("ini4j", ".ini");
44  
45          // write only can't sync
46          IniFile f = new IniFile(tmp, IniFile.Mode.WO);
47  
48          try
49          {
50              f.sync();
51              fail();
52          }
53          catch (BackingStoreException x)
54          {
55              ;
56          }
57  
58          // invalid file for write
59          f = new IniFile(new File("/non existent path/to file"), IniFile.Mode.WO);
60          try
61          {
62              f.flush();
63              fail();
64          }
65          catch (BackingStoreException x)
66          {
67              ;
68          }
69  
70          // invalid file for read
71          try
72          {
73              f = new IniFile(new File("/non existent path/to file"), IniFile.Mode.RO);
74              fail();
75          }
76          catch (BackingStoreException x)
77          {
78              ;
79          }
80  
81          // read only can't flush
82          f = new IniFile(tmp, IniFile.Mode.RO);
83          try
84          {
85              f.sync();
86              f.flush();
87          }
88          catch (BackingStoreException x)
89          {
90              ;
91          }
92      }
93  
94      /**
95       * Test of flush method.
96       *
97       * @throws Exception on error
98       */
99      @Test public void testFlush() throws Exception
100     {
101         File tmp = File.createTempFile("ini4j", ".ini");
102         IniFile f = new IniFile(tmp, IniFile.Mode.RW);
103 
104         assertEquals(IniFile.Mode.RW, f.getMode());
105         assertEquals(tmp, f.getFile());
106         f.node(Dwarfs.PROP_DOC).put(Dwarf.PROP_WEIGHT, "65");
107         f.flush();
108         f = new IniFile(tmp);
109         assertEquals(f.node(Dwarfs.PROP_DOC).get(Dwarf.PROP_WEIGHT, null), "65");
110         tmp.delete();
111     }
112 }