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.spi.WinEscapeToolTest;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  
24  import org.junit.Test;
25  
26  import java.io.File;
27  import java.io.FileInputStream;
28  import java.io.FileReader;
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.Reader;
32  
33  import java.net.URL;
34  
35  public class WiniTest extends Ini4jCase
36  {
37      @Test public void testConstructors() throws Exception
38      {
39          File f = File.createTempFile("wini", "test");
40  
41          f.deleteOnExit();
42          assertTrue(new WiniHelper(new FileInputStream(f)).isOK());
43          assertTrue(new WiniHelper(new FileReader(f)).isOK());
44          assertTrue(new WiniHelper(f).isOK());
45          assertTrue(new WiniHelper(f.toURI().toURL()).isOK());
46      }
47  
48      @Test public void testDefaults()
49      {
50          Wini wini = new Wini();
51          Config cfg = wini.getConfig();
52  
53          assertTrue(cfg.isGlobalSection());
54          assertTrue(cfg.isEmptyOption());
55          assertFalse(cfg.isMultiOption());
56          assertFalse(cfg.isEscape());
57      }
58  
59      @Test public void testEscape()
60      {
61          Wini instance = new Wini();
62  
63          assertEquals(WinEscapeToolTest.ESCAPE1, instance.escape(WinEscapeToolTest.VALUE1));
64          assertEquals(WinEscapeToolTest.ESCAPE2, instance.escape(WinEscapeToolTest.VALUE2));
65          assertEquals(WinEscapeToolTest.ESCAPE3, instance.escape(WinEscapeToolTest.VALUE3));
66          assertEquals(WinEscapeToolTest.ESCAPE4, instance.escape(WinEscapeToolTest.VALUE4));
67          assertEquals(WinEscapeToolTest.ESCAPE5, instance.escape(WinEscapeToolTest.VALUE5));
68      }
69  
70      @Test public void testUnescape() throws Exception
71      {
72          Wini instance = new Wini();
73  
74          assertEquals(WinEscapeToolTest.VALUE1, instance.unescape(WinEscapeToolTest.ESCAPE1));
75          assertEquals(WinEscapeToolTest.VALUE2, instance.unescape(WinEscapeToolTest.ESCAPE2));
76          assertEquals(WinEscapeToolTest.VALUE3, instance.unescape(WinEscapeToolTest.ESCAPE3));
77          assertEquals(WinEscapeToolTest.VALUE4, instance.unescape(WinEscapeToolTest.ESCAPE4));
78          assertEquals(WinEscapeToolTest.VALUE5, instance.unescape(WinEscapeToolTest.ESCAPE5));
79          assertEquals("=", instance.unescape("\\="));
80          assertEquals("xAx", instance.unescape("x\\o101x"));
81      }
82  
83      private static class WiniHelper extends Wini
84      {
85          private boolean _ok;
86  
87          public WiniHelper(Reader input) throws IOException, InvalidFileFormatException
88          {
89              super(input);
90          }
91  
92          public WiniHelper(InputStream input) throws IOException, InvalidFileFormatException
93          {
94              super(input);
95          }
96  
97          public WiniHelper(URL input) throws IOException, InvalidFileFormatException
98          {
99              super(input);
100         }
101 
102         public WiniHelper(File input) throws IOException, InvalidFileFormatException
103         {
104             super(input);
105         }
106 
107         public boolean isOK()
108         {
109             return _ok;
110         }
111 
112         @Override public void load(InputStream input) throws IOException, InvalidFileFormatException
113         {
114             _ok = true;
115         }
116 
117         @Override public void load(Reader input) throws IOException, InvalidFileFormatException
118         {
119             _ok = true;
120         }
121 
122         @Override public void load(File input) throws IOException, InvalidFileFormatException
123         {
124             _ok = true;
125         }
126 
127         @Override public void load(URL input) throws IOException, InvalidFileFormatException
128         {
129             _ok = true;
130         }
131     }
132 }