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.spi;
17  
18  import org.easymock.EasyMock;
19  
20  import org.ini4j.Config;
21  import org.ini4j.Ini4jCase;
22  
23  import org.ini4j.test.Helper;
24  
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertNull;
27  
28  import org.junit.Test;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.InputStream;
32  
33  public class IniSourceTest extends Ini4jCase
34  {
35      private static final String COMMENTS = ";#";
36      private static final String NESTED_TXT = "nested.txt";
37      private static final String NESTED = ":" + NESTED_TXT;
38      private static final String NESTED_PATH = "org/ini4j/spi/" + NESTED_TXT;
39      private static final String INCLUDE = ":include.txt";
40      private static final String PART1 = ":part1.txt";
41      private static final String PART2 = ":part2.txt";
42      private static final String OUTER = ":outer";
43  
44      @Test public void testWithInclude() throws Exception
45      {
46          HandlerBase handler = EasyMock.createMock(HandlerBase.class);
47  
48          handler.handleComment("-1" + OUTER);
49          handler.handleComment("-1" + NESTED);
50          handler.handleComment("-2" + NESTED);
51          handler.handleComment("-1" + INCLUDE);
52          handler.handleComment("-2" + INCLUDE);
53          handler.handleComment("-1" + PART1);
54          handler.handleComment("-2" + PART1);
55          handler.handleComment("-3" + INCLUDE);
56          handler.handleComment("-4" + INCLUDE);
57          handler.handleComment("-5" + INCLUDE);
58          handler.handleComment("-6" + INCLUDE);
59          handler.handleComment("-1" + PART2);
60          handler.handleComment("-2" + PART2);
61          handler.handleComment("-7" + INCLUDE);
62          handler.handleComment("-8" + INCLUDE);
63          handler.handleComment("-3" + NESTED);
64          handler.handleComment("-4" + NESTED);
65          handler.handleComment("-2" + OUTER);
66          EasyMock.replay(handler);
67          StringBuilder outer = new StringBuilder();
68  
69          outer.append(";-1" + OUTER + '\n');
70          outer.append("1" + OUTER + '\n');
71          outer.append('<');
72          outer.append(Helper.getResourceURL(NESTED_PATH).toExternalForm());
73          outer.append(">\n");
74          outer.append("2" + OUTER + '\n');
75          outer.append(";-2" + OUTER + '\n');
76          InputStream in = new ByteArrayInputStream(outer.toString().getBytes());
77          Config cfg = new Config();
78  
79          cfg.setInclude(true);
80          IniSource src = new IniSource(in, handler, COMMENTS, cfg);
81  
82          assertEquals("1" + OUTER, src.readLine());
83          assertEquals(2, src.getLineNumber());
84          assertEquals("1" + NESTED, src.readLine());
85          assertEquals(2, src.getLineNumber());
86          assertEquals("1" + INCLUDE, src.readLine());
87          assertEquals(2, src.getLineNumber());
88          assertEquals("1" + PART1, src.readLine());
89          assertEquals(2, src.getLineNumber());
90          assertEquals("2" + PART1, src.readLine());
91          assertEquals(4, src.getLineNumber());
92          assertEquals("3" + PART1 + "\\\\", src.readLine());
93          assertEquals(5, src.getLineNumber());
94          assertEquals("4:\\\\part1.txt", src.readLine());
95          assertEquals(7, src.getLineNumber());
96          assertEquals("5" + PART1 + "\\\\\\\\", src.readLine());
97          assertEquals(8, src.getLineNumber());
98          assertEquals("6" + PART1 + ";", src.readLine());
99          assertEquals(10, src.getLineNumber());
100         assertEquals("2" + INCLUDE, src.readLine());
101         assertEquals(6, src.getLineNumber());
102         assertEquals("3" + INCLUDE, src.readLine());
103         assertEquals(10, src.getLineNumber());
104         assertEquals("1" + PART2, src.readLine());
105         assertEquals(3, src.getLineNumber());
106         assertEquals("4" + INCLUDE, src.readLine());
107         assertEquals(14, src.getLineNumber());
108         assertEquals("2" + NESTED, src.readLine());
109         assertEquals(6, src.getLineNumber());
110         assertEquals("2" + OUTER, src.readLine());
111         assertEquals(4, src.getLineNumber());
112         assertNull(src.readLine());
113         EasyMock.verify(handler);
114     }
115 
116     @Test public void testWithoutInclude() throws Exception
117     {
118         HandlerBase handler = EasyMock.createMock(HandlerBase.class);
119 
120         handler.handleComment("-1" + NESTED);
121         handler.handleComment("-2" + NESTED);
122         handler.handleComment("-3" + NESTED);
123         handler.handleComment("-4" + NESTED);
124         EasyMock.replay(handler);
125         Config cfg = new Config();
126 
127         cfg.setInclude(false);
128         IniSource src = new IniSource(Helper.getResourceURL(NESTED_PATH), handler, COMMENTS, cfg);
129 
130         assertEquals("1" + NESTED, src.readLine());
131         assertEquals("<include.txt>", src.readLine());
132         assertEquals("2" + NESTED, src.readLine());
133         assertNull(src.readLine());
134         EasyMock.verify(handler);
135     }
136 }