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.spi; 017 018 import org.easymock.EasyMock; 019 020 import org.ini4j.Config; 021 import org.ini4j.Ini4jCase; 022 023 import org.ini4j.test.Helper; 024 025 import static org.junit.Assert.assertEquals; 026 import static org.junit.Assert.assertNull; 027 028 import org.junit.Test; 029 030 import java.io.ByteArrayInputStream; 031 import java.io.InputStream; 032 033 public class IniSourceTest extends Ini4jCase 034 { 035 private static final String COMMENTS = ";#"; 036 private static final String NESTED_TXT = "nested.txt"; 037 private static final String NESTED = ":" + NESTED_TXT; 038 private static final String NESTED_PATH = "org/ini4j/spi/" + NESTED_TXT; 039 private static final String INCLUDE = ":include.txt"; 040 private static final String PART1 = ":part1.txt"; 041 private static final String PART2 = ":part2.txt"; 042 private static final String OUTER = ":outer"; 043 044 @Test public void testWithInclude() throws Exception 045 { 046 HandlerBase handler = EasyMock.createMock(HandlerBase.class); 047 048 handler.handleComment("-1" + OUTER); 049 handler.handleComment("-1" + NESTED); 050 handler.handleComment("-2" + NESTED); 051 handler.handleComment("-1" + INCLUDE); 052 handler.handleComment("-2" + INCLUDE); 053 handler.handleComment("-1" + PART1); 054 handler.handleComment("-2" + PART1); 055 handler.handleComment("-3" + INCLUDE); 056 handler.handleComment("-4" + INCLUDE); 057 handler.handleComment("-5" + INCLUDE); 058 handler.handleComment("-6" + INCLUDE); 059 handler.handleComment("-1" + PART2); 060 handler.handleComment("-2" + PART2); 061 handler.handleComment("-7" + INCLUDE); 062 handler.handleComment("-8" + INCLUDE); 063 handler.handleComment("-3" + NESTED); 064 handler.handleComment("-4" + NESTED); 065 handler.handleComment("-2" + OUTER); 066 EasyMock.replay(handler); 067 StringBuilder outer = new StringBuilder(); 068 069 outer.append(";-1" + OUTER + '\n'); 070 outer.append("1" + OUTER + '\n'); 071 outer.append('<'); 072 outer.append(Helper.getResourceURL(NESTED_PATH).toExternalForm()); 073 outer.append(">\n"); 074 outer.append("2" + OUTER + '\n'); 075 outer.append(";-2" + OUTER + '\n'); 076 InputStream in = new ByteArrayInputStream(outer.toString().getBytes()); 077 Config cfg = new Config(); 078 079 cfg.setInclude(true); 080 IniSource src = new IniSource(in, handler, COMMENTS, cfg); 081 082 assertEquals("1" + OUTER, src.readLine()); 083 assertEquals(2, src.getLineNumber()); 084 assertEquals("1" + NESTED, src.readLine()); 085 assertEquals(2, src.getLineNumber()); 086 assertEquals("1" + INCLUDE, src.readLine()); 087 assertEquals(2, src.getLineNumber()); 088 assertEquals("1" + PART1, src.readLine()); 089 assertEquals(2, src.getLineNumber()); 090 assertEquals("2" + PART1, src.readLine()); 091 assertEquals(4, src.getLineNumber()); 092 assertEquals("3" + PART1 + "\\\\", src.readLine()); 093 assertEquals(5, src.getLineNumber()); 094 assertEquals("4:\\\\part1.txt", src.readLine()); 095 assertEquals(7, src.getLineNumber()); 096 assertEquals("5" + PART1 + "\\\\\\\\", src.readLine()); 097 assertEquals(8, src.getLineNumber()); 098 assertEquals("6" + PART1 + ";", src.readLine()); 099 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 }