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.addon;
17  
18  import org.ini4j.Config;
19  import org.ini4j.Helper;
20  import org.ini4j.Ini;
21  import org.ini4j.IniHandler;
22  
23  import org.ini4j.spi.IniFormatter;
24  
25  import org.junit.AfterClass;
26  
27  import static org.junit.Assert.*;
28  
29  import org.junit.Before;
30  import org.junit.Test;
31  
32  import java.io.IOException;
33  import java.io.Reader;
34  import java.io.StringReader;
35  import java.io.StringWriter;
36  
37  @SuppressWarnings("deprecation")
38  public class FancyIniFormatterTest
39  {
40      private static final String NL = System.getProperty("line.separator");
41      private static final String STRICTOPERATOR = "[section]" + NL + "option=value" + NL + NL;
42      private static final String NORMALOPERATOR = "[section]" + NL + "option = value" + NL + NL;
43      private static final String WITHDUMMY = "[section]" + NL + "option=value" + NL + "dummy=" + NL + NL;
44      public static final String DUMMY = "dummy";
45      public static final String SECTION = "section";
46      private IniFormatter formatter;
47      private StringWriter output;
48  
49      @AfterClass public static void tearDownClass() throws Exception
50      {
51          Helper.resetConfig();
52      }
53  
54      @Before public void setUp() throws Exception
55      {
56          System.setProperty(IniFormatter.class.getName(), FancyIniFormatter.class.getName());
57          output = new StringWriter();
58          formatter = (FancyIniFormatter) IniFormatter.newInstance(output);
59      }
60  
61      @Test public void testDefaults() throws Exception
62      {
63          ((FancyIniFormatter) formatter).setConfig(new Config());
64          assertTrue(((FancyIniFormatter) formatter).isAllowStrictOperator());
65          assertTrue(((FancyIniFormatter) formatter).isAllowEmptyOption());
66      }
67  
68      @Test public void testEmptyOption() throws Exception
69      {
70          ((FancyIniFormatter) formatter).setAllowEmptyOption(true);
71          IniHelper ini = new IniHelper(new StringReader(STRICTOPERATOR));
72  
73          ini.get(SECTION).put(DUMMY, null);
74          ini.store(formatter);
75          assertEquals(WITHDUMMY, output.toString());
76      }
77  
78      @Test public void testNoEmptyOption() throws Exception
79      {
80          ((FancyIniFormatter) formatter).setAllowEmptyOption(false);
81          IniHelper ini = new IniHelper(new StringReader(STRICTOPERATOR));
82  
83          ini.get(SECTION).put(DUMMY, null);
84          ini.store(formatter);
85          System.err.println(output.toString());
86          assertEquals(STRICTOPERATOR, output.toString());
87      }
88  
89      @Test public void testNoStrictOperator() throws Exception
90      {
91          ((FancyIniFormatter) formatter).setAllowStrictOperator(false);
92          IniHelper ini = new IniHelper(new StringReader(NORMALOPERATOR));
93  
94          ini.store(formatter);
95          assertEquals(NORMALOPERATOR, output.toString());
96      }
97  
98      @Test public void testStrictOperator() throws Exception
99      {
100         ((FancyIniFormatter) formatter).setAllowStrictOperator(true);
101         IniHelper ini = new IniHelper(new StringReader(STRICTOPERATOR));
102 
103         ini.store(formatter);
104         assertEquals(STRICTOPERATOR, output.toString());
105     }
106 
107     protected static class IniHelper extends Ini
108     {
109         public IniHelper(Reader input) throws IOException
110         {
111             super(input);
112         }
113 
114         @Override protected void store(IniHandler formatter) throws IOException
115         {
116             super.store(formatter);
117         }
118     }
119 }