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.junit.Assert;
19  import org.junit.Test;
20  
21  import java.nio.charset.Charset;
22  
23  public class ConfigTest extends Ini4jCase
24  {
25      @Test public void testDefaults()
26      {
27          Config def = newDefaultConfig();
28  
29          assertEquals(def, new Config());
30          assertEquals(def, Config.getGlobal());
31          assertEquals(def, Config.getGlobal().clone());
32      }
33  
34      @Test public void testSystemProperties()
35      {
36          Config exp = newInverseConfig();
37  
38          setBoolean(Config.PROP_EMPTY_OPTION, exp.isEmptyOption());
39          setBoolean(Config.PROP_EMPTY_SECTION, exp.isEmptySection());
40          setBoolean(Config.PROP_GLOBAL_SECTION, exp.isGlobalSection());
41          setString(Config.PROP_GLOBAL_SECTION_NAME, exp.getGlobalSectionName());
42          setBoolean(Config.PROP_INCLUDE, exp.isInclude());
43          setBoolean(Config.PROP_LOWER_CASE_OPTION, exp.isLowerCaseOption());
44          setBoolean(Config.PROP_LOWER_CASE_SECTION, exp.isLowerCaseSection());
45          setBoolean(Config.PROP_MULTI_OPTION, exp.isMultiOption());
46          setBoolean(Config.PROP_MULTI_SECTION, exp.isMultiSection());
47          setBoolean(Config.PROP_STRICT_OPERATOR, exp.isStrictOperator());
48          setBoolean(Config.PROP_UNNAMED_SECTION, exp.isUnnamedSection());
49          setBoolean(Config.PROP_ESCAPE, exp.isEscape());
50          setBoolean(Config.PROP_ESCAPE_NEWLINE, exp.isEscapeNewline());
51          setChar(Config.PROP_PATH_SEPARATOR, exp.getPathSeparator());
52          setBoolean(Config.PROP_TREE, exp.isTree());
53          setBoolean(Config.PROP_PROPERTY_FIRST_UPPER, exp.isPropertyFirstUpper());
54          setString(Config.PROP_LINE_SEPARATOR, exp.getLineSeparator());
55          setCharset(Config.PROP_FILE_ENCODING, exp.getFileEncoding());
56          setBoolean(Config.PROP_COMMENT, exp.isComment());
57          setBoolean(Config.PROP_HEADER_COMMENT, exp.isHeaderComment());
58          Config cfg = new Config();
59  
60          assertEquals(exp, cfg);
61      }
62  
63      private void setBoolean(String prop, boolean value)
64      {
65          System.setProperty(Config.KEY_PREFIX + prop, String.valueOf(value));
66      }
67  
68      private void setChar(String prop, char value)
69      {
70          System.setProperty(Config.KEY_PREFIX + prop, String.valueOf(value));
71      }
72  
73      private void setCharset(String prop, Charset value)
74      {
75          System.setProperty(Config.KEY_PREFIX + prop, String.valueOf(value));
76      }
77  
78      private void setString(String prop, String value)
79      {
80          System.setProperty(Config.KEY_PREFIX + prop, value);
81      }
82  
83      private void assertEquals(Config exp, Config act)
84      {
85          Assert.assertEquals(exp.isEmptyOption(), act.isEmptyOption());
86          Assert.assertEquals(exp.isEmptySection(), act.isEmptySection());
87          Assert.assertEquals(exp.isEscape(), act.isEscape());
88          Assert.assertEquals(exp.isEscapeNewline(), act.isEscapeNewline());
89          Assert.assertEquals(exp.isGlobalSection(), act.isGlobalSection());
90          Assert.assertEquals(exp.isInclude(), act.isInclude());
91          Assert.assertEquals(exp.isLowerCaseOption(), act.isLowerCaseOption());
92          Assert.assertEquals(exp.isLowerCaseSection(), act.isLowerCaseSection());
93          Assert.assertEquals(exp.isMultiOption(), act.isMultiOption());
94          Assert.assertEquals(exp.isMultiSection(), act.isMultiSection());
95          Assert.assertEquals(exp.isStrictOperator(), act.isStrictOperator());
96          Assert.assertEquals(exp.isUnnamedSection(), act.isUnnamedSection());
97          Assert.assertEquals(exp.getGlobalSectionName(), act.getGlobalSectionName());
98          Assert.assertEquals(exp.getPathSeparator(), act.getPathSeparator());
99          Assert.assertEquals(exp.isTree(), act.isTree());
100         Assert.assertEquals(exp.isPropertyFirstUpper(), act.isPropertyFirstUpper());
101         Assert.assertEquals(exp.getLineSeparator(), act.getLineSeparator());
102         Assert.assertEquals(exp.getFileEncoding(), act.getFileEncoding());
103         Assert.assertEquals(exp.isComment(), act.isComment());
104         Assert.assertEquals(exp.isHeaderComment(), act.isHeaderComment());
105     }
106 
107     private Config newDefaultConfig()
108     {
109         Config cfg = new Config();
110 
111         cfg.setEmptyOption(false);
112         cfg.setEmptySection(false);
113         cfg.setEscape(true);
114         cfg.setEscapeNewline(true);
115         cfg.setGlobalSection(false);
116         cfg.setGlobalSectionName("?");
117         cfg.setInclude(false);
118         cfg.setLowerCaseOption(false);
119         cfg.setLowerCaseSection(false);
120         cfg.setMultiSection(false);
121         cfg.setMultiOption(true);
122         cfg.setStrictOperator(false);
123         cfg.setUnnamedSection(false);
124         cfg.setPathSeparator('/');
125         cfg.setTree(true);
126         cfg.setPropertyFirstUpper(false);
127         cfg.setLineSeparator(System.getProperty("line.separator"));
128         cfg.setFileEncoding(Charset.forName("UTF-8"));
129         cfg.setComment(true);
130         cfg.setHeaderComment(true);
131 
132         return cfg;
133     }
134 
135     private Config newInverseConfig()
136     {
137         Config cfg = newDefaultConfig();
138 
139         cfg.setEmptyOption(!cfg.isEmptyOption());
140         cfg.setEmptySection(!cfg.isEmptySection());
141         cfg.setEscape(!cfg.isEscape());
142         cfg.setEscapeNewline(!cfg.isEscapeNewline());
143         cfg.setGlobalSection(!cfg.isGlobalSection());
144         cfg.setGlobalSectionName("+");
145         cfg.setInclude(!cfg.isInclude());
146         cfg.setLowerCaseOption(!cfg.isLowerCaseOption());
147         cfg.setLowerCaseSection(!cfg.isLowerCaseSection());
148         cfg.setMultiSection(!cfg.isMultiSection());
149         cfg.setMultiOption(!cfg.isMultiOption());
150         cfg.setStrictOperator(!cfg.isStrictOperator());
151         cfg.setUnnamedSection(!cfg.isUnnamedSection());
152         cfg.setPathSeparator('?');
153         cfg.setTree(!cfg.isTree());
154         cfg.setPropertyFirstUpper(!cfg.isPropertyFirstUpper());
155         cfg.setComment(!cfg.isComment());
156         cfg.setHeaderComment(!cfg.isHeaderComment());
157 
158         //cfg.setLineSeparator("\t");
159         //cfg.setFileEncoding(Charset.forName("ASCII"));
160         return cfg;
161     }
162 }