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.spi.WinEscapeToolTest;
019
020 import static org.junit.Assert.assertEquals;
021 import static org.junit.Assert.assertFalse;
022 import static org.junit.Assert.assertTrue;
023
024 import org.junit.Test;
025
026 import java.io.File;
027 import java.io.FileInputStream;
028 import java.io.FileReader;
029 import java.io.IOException;
030 import java.io.InputStream;
031 import java.io.Reader;
032
033 import java.net.URL;
034
035 public class WiniTest extends Ini4jCase
036 {
037 @Test public void testConstructors() throws Exception
038 {
039 File f = File.createTempFile("wini", "test");
040
041 f.deleteOnExit();
042 assertTrue(new WiniHelper(new FileInputStream(f)).isOK());
043 assertTrue(new WiniHelper(new FileReader(f)).isOK());
044 assertTrue(new WiniHelper(f).isOK());
045 assertTrue(new WiniHelper(f.toURI().toURL()).isOK());
046 }
047
048 @Test public void testDefaults()
049 {
050 Wini wini = new Wini();
051 Config cfg = wini.getConfig();
052
053 assertTrue(cfg.isGlobalSection());
054 assertTrue(cfg.isEmptyOption());
055 assertFalse(cfg.isMultiOption());
056 assertFalse(cfg.isEscape());
057 }
058
059 @Test public void testEscape()
060 {
061 Wini instance = new Wini();
062
063 assertEquals(WinEscapeToolTest.ESCAPE1, instance.escape(WinEscapeToolTest.VALUE1));
064 assertEquals(WinEscapeToolTest.ESCAPE2, instance.escape(WinEscapeToolTest.VALUE2));
065 assertEquals(WinEscapeToolTest.ESCAPE3, instance.escape(WinEscapeToolTest.VALUE3));
066 assertEquals(WinEscapeToolTest.ESCAPE4, instance.escape(WinEscapeToolTest.VALUE4));
067 assertEquals(WinEscapeToolTest.ESCAPE5, instance.escape(WinEscapeToolTest.VALUE5));
068 }
069
070 @Test public void testUnescape() throws Exception
071 {
072 Wini instance = new Wini();
073
074 assertEquals(WinEscapeToolTest.VALUE1, instance.unescape(WinEscapeToolTest.ESCAPE1));
075 assertEquals(WinEscapeToolTest.VALUE2, instance.unescape(WinEscapeToolTest.ESCAPE2));
076 assertEquals(WinEscapeToolTest.VALUE3, instance.unescape(WinEscapeToolTest.ESCAPE3));
077 assertEquals(WinEscapeToolTest.VALUE4, instance.unescape(WinEscapeToolTest.ESCAPE4));
078 assertEquals(WinEscapeToolTest.VALUE5, instance.unescape(WinEscapeToolTest.ESCAPE5));
079 assertEquals("=", instance.unescape("\\="));
080 assertEquals("xAx", instance.unescape("x\\o101x"));
081 }
082
083 private static class WiniHelper extends Wini
084 {
085 private boolean _ok;
086
087 public WiniHelper(Reader input) throws IOException, InvalidFileFormatException
088 {
089 super(input);
090 }
091
092 public WiniHelper(InputStream input) throws IOException, InvalidFileFormatException
093 {
094 super(input);
095 }
096
097 public WiniHelper(URL input) throws IOException, InvalidFileFormatException
098 {
099 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 }