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.junit.Assert;
019 import org.junit.Test;
020
021 import java.nio.charset.Charset;
022
023 public class ConfigTest extends Ini4jCase
024 {
025 @Test public void testDefaults()
026 {
027 Config def = newDefaultConfig();
028
029 assertEquals(def, new Config());
030 assertEquals(def, Config.getGlobal());
031 assertEquals(def, Config.getGlobal().clone());
032 }
033
034 @Test public void testSystemProperties()
035 {
036 Config exp = newInverseConfig();
037
038 setBoolean(Config.PROP_EMPTY_OPTION, exp.isEmptyOption());
039 setBoolean(Config.PROP_EMPTY_SECTION, exp.isEmptySection());
040 setBoolean(Config.PROP_GLOBAL_SECTION, exp.isGlobalSection());
041 setString(Config.PROP_GLOBAL_SECTION_NAME, exp.getGlobalSectionName());
042 setBoolean(Config.PROP_INCLUDE, exp.isInclude());
043 setBoolean(Config.PROP_LOWER_CASE_OPTION, exp.isLowerCaseOption());
044 setBoolean(Config.PROP_LOWER_CASE_SECTION, exp.isLowerCaseSection());
045 setBoolean(Config.PROP_MULTI_OPTION, exp.isMultiOption());
046 setBoolean(Config.PROP_MULTI_SECTION, exp.isMultiSection());
047 setBoolean(Config.PROP_STRICT_OPERATOR, exp.isStrictOperator());
048 setBoolean(Config.PROP_UNNAMED_SECTION, exp.isUnnamedSection());
049 setBoolean(Config.PROP_ESCAPE, exp.isEscape());
050 setChar(Config.PROP_PATH_SEPARATOR, exp.getPathSeparator());
051 setBoolean(Config.PROP_TREE, exp.isTree());
052 setBoolean(Config.PROP_PROPERTY_FIRST_UPPER, exp.isPropertyFirstUpper());
053 setString(Config.PROP_LINE_SEPARATOR, exp.getLineSeparator());
054 setCharset(Config.PROP_FILE_ENCODING, exp.getFileEncoding());
055 setBoolean(Config.PROP_COMMENT, exp.isComment());
056 setBoolean(Config.PROP_HEADER_COMMENT, exp.isHeaderComment());
057 Config cfg = new Config();
058
059 assertEquals(exp, cfg);
060 }
061
062 private void setBoolean(String prop, boolean value)
063 {
064 System.setProperty(Config.KEY_PREFIX + prop, String.valueOf(value));
065 }
066
067 private void setChar(String prop, char value)
068 {
069 System.setProperty(Config.KEY_PREFIX + prop, String.valueOf(value));
070 }
071
072 private void setCharset(String prop, Charset value)
073 {
074 System.setProperty(Config.KEY_PREFIX + prop, String.valueOf(value));
075 }
076
077 private void setString(String prop, String value)
078 {
079 System.setProperty(Config.KEY_PREFIX + prop, value);
080 }
081
082 private void assertEquals(Config exp, Config act)
083 {
084 Assert.assertEquals(exp.isEmptyOption(), act.isEmptyOption());
085 Assert.assertEquals(exp.isEmptySection(), act.isEmptySection());
086 Assert.assertEquals(exp.isEscape(), act.isEscape());
087 Assert.assertEquals(exp.isGlobalSection(), act.isGlobalSection());
088 Assert.assertEquals(exp.isInclude(), act.isInclude());
089 Assert.assertEquals(exp.isLowerCaseOption(), act.isLowerCaseOption());
090 Assert.assertEquals(exp.isLowerCaseSection(), act.isLowerCaseSection());
091 Assert.assertEquals(exp.isMultiOption(), act.isMultiOption());
092 Assert.assertEquals(exp.isMultiSection(), act.isMultiSection());
093 Assert.assertEquals(exp.isStrictOperator(), act.isStrictOperator());
094 Assert.assertEquals(exp.isUnnamedSection(), act.isUnnamedSection());
095 Assert.assertEquals(exp.getGlobalSectionName(), act.getGlobalSectionName());
096 Assert.assertEquals(exp.getPathSeparator(), act.getPathSeparator());
097 Assert.assertEquals(exp.isTree(), act.isTree());
098 Assert.assertEquals(exp.isPropertyFirstUpper(), act.isPropertyFirstUpper());
099 Assert.assertEquals(exp.getLineSeparator(), act.getLineSeparator());
100 Assert.assertEquals(exp.getFileEncoding(), act.getFileEncoding());
101 Assert.assertEquals(exp.isComment(), act.isComment());
102 Assert.assertEquals(exp.isHeaderComment(), act.isHeaderComment());
103 }
104
105 private Config newDefaultConfig()
106 {
107 Config cfg = new Config();
108
109 cfg.setEmptyOption(false);
110 cfg.setEmptySection(false);
111 cfg.setEscape(true);
112 cfg.setGlobalSection(false);
113 cfg.setGlobalSectionName("?");
114 cfg.setInclude(false);
115 cfg.setLowerCaseOption(false);
116 cfg.setLowerCaseSection(false);
117 cfg.setMultiSection(false);
118 cfg.setMultiOption(true);
119 cfg.setStrictOperator(false);
120 cfg.setUnnamedSection(false);
121 cfg.setPathSeparator('/');
122 cfg.setTree(true);
123 cfg.setPropertyFirstUpper(false);
124 cfg.setLineSeparator(System.getProperty("line.separator"));
125 cfg.setFileEncoding(Charset.forName("UTF-8"));
126 cfg.setComment(true);
127 cfg.setHeaderComment(true);
128
129 return cfg;
130 }
131
132 private Config newInverseConfig()
133 {
134 Config cfg = newDefaultConfig();
135
136 cfg.setEmptyOption(!cfg.isEmptyOption());
137 cfg.setEmptySection(!cfg.isEmptySection());
138 cfg.setEscape(!cfg.isEscape());
139 cfg.setGlobalSection(!cfg.isGlobalSection());
140 cfg.setGlobalSectionName("+");
141 cfg.setInclude(!cfg.isInclude());
142 cfg.setLowerCaseOption(!cfg.isLowerCaseOption());
143 cfg.setLowerCaseSection(!cfg.isLowerCaseSection());
144 cfg.setMultiSection(!cfg.isMultiSection());
145 cfg.setMultiOption(!cfg.isMultiOption());
146 cfg.setStrictOperator(!cfg.isStrictOperator());
147 cfg.setUnnamedSection(!cfg.isUnnamedSection());
148 cfg.setPathSeparator('?');
149 cfg.setTree(!cfg.isTree());
150 cfg.setPropertyFirstUpper(!cfg.isPropertyFirstUpper());
151 cfg.setComment(!cfg.isComment());
152 cfg.setHeaderComment(!cfg.isHeaderComment());
153
154 //cfg.setLineSeparator("\t");
155 //cfg.setFileEncoding(Charset.forName("ASCII"));
156 return cfg;
157 }
158 }