1 package org.apache.archiva.xml;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import java.io.BufferedReader;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.io.Reader;
27 import java.io.StringWriter;
30 * LatinEntityResolutionReaderTest
34 public class LatinEntityResolutionReaderTest
35 extends AbstractArchivaXmlTestCase
38 * A method to obtain the content of a reader as a String,
39 * while allowing for specifing the buffer size of the operation.
41 * This method is only really useful for testing a Reader implementation.
43 * @param input the reader to get the input from.
44 * @param bufsize the buffer size to use.
45 * @return the contents of the reader as a String.
46 * @throws IOException if there was an I/O error.
48 private String toStringFromReader( Reader input, int bufsize )
51 StringWriter output = new StringWriter();
53 final char[] buffer = new char[bufsize];
55 while ( -1 != ( n = input.read( buffer ) ) )
57 output.write( buffer, 0, n );
61 return output.toString();
65 * This reads a text file from the src/test/examples directory,
66 * normalizes the end of lines, and returns the contents as a big String.
68 * @param examplePath the name of the file in the src/test/examples directory.
69 * @return the contents of the provided file
70 * @throws IOException if there was an I/O error.
72 private String toStringFromExample( String examplePath )
75 File exampleFile = getExampleXml( examplePath );
76 FileReader fileReader = new FileReader( exampleFile );
77 BufferedReader lineReader = new BufferedReader( fileReader );
78 StringBuffer sb = new StringBuffer();
80 boolean hasContent = false;
82 String line = lineReader.readLine();
83 while ( line != null )
91 line = lineReader.readLine();
97 public void assertProperRead( String sourcePath, String expectedPath, int bufsize )
101 File inputFile = getExampleXml( sourcePath );
103 FileReader fileReader = new FileReader( inputFile );
104 LatinEntityResolutionReader testReader = new LatinEntityResolutionReader( fileReader );
106 String actualOutput = toStringFromReader( testReader, bufsize );
107 String expectedOutput = toStringFromExample( expectedPath );
109 assertEquals( expectedOutput, actualOutput );
111 catch ( IOException e )
113 fail( "IOException: " + e.getMessage() );
117 private void assertProperRead( StringBuffer expected, String sourcePath, int bufSize )
121 File inputFile = getExampleXml( sourcePath );
123 FileReader fileReader = new FileReader( inputFile );
124 LatinEntityResolutionReader testReader = new LatinEntityResolutionReader( fileReader );
126 String actualOutput = toStringFromReader( testReader, bufSize );
128 assertEquals( "Proper Read: ", expected.toString(), actualOutput );
130 catch ( IOException e )
132 fail( "IOException: " + e.getMessage() );
136 public void testReaderNormalBufsize()
139 StringBuffer expected = new StringBuffer();
141 expected.append( "<basic>\n" );
142 expected.append( " <names>\n" );
143 expected.append( " <name>" ).append( TRYGVIS ).append( "</name>\n" );
144 expected.append( " <name>" ).append( INFINITE_ARCHIVA ).append( "</name>\n" );
145 expected.append( " </names>\n" );
146 expected.append( "</basic>" );
148 assertProperRead( expected, "no-prolog-with-entities.xml", 4096 );
151 public void testReaderSmallBufsize()
154 StringBuffer expected = new StringBuffer();
156 expected.append( "<basic>\n" );
157 expected.append( " <names>\n" );
158 expected.append( " <name>" ).append( TRYGVIS ).append( "</name>\n" );
159 expected.append( " <name>" ).append( INFINITE_ARCHIVA ).append( "</name>\n" );
160 expected.append( " </names>\n" );
161 expected.append( "</basic>" );
163 assertProperRead( expected, "no-prolog-with-entities.xml", 1024 );
166 public void testReaderRediculouslyTinyBufsize()
169 StringBuffer expected = new StringBuffer();
171 expected.append( "<basic>\n" );
172 expected.append( " <names>\n" );
173 expected.append( " <name>" ).append( TRYGVIS ).append( "</name>\n" );
174 expected.append( " <name>" ).append( INFINITE_ARCHIVA ).append( "</name>\n" );
175 expected.append( " </names>\n" );
176 expected.append( "</basic>" );
178 assertProperRead( expected, "no-prolog-with-entities.xml", 32 );
181 public void testReaderHugeBufsize()
184 StringBuffer expected = new StringBuffer();
186 expected.append( "<basic>\n" );
187 expected.append( " <names>\n" );
188 expected.append( " <name>" ).append( TRYGVIS ).append( "</name>\n" );
189 expected.append( " <name>" ).append( INFINITE_ARCHIVA ).append( "</name>\n" );
190 expected.append( " </names>\n" );
191 expected.append( "</basic>" );
193 assertProperRead( expected, "no-prolog-with-entities.xml", 409600 );
196 public void testNoLatinEntitiesHugeLine()
198 assertProperRead( "commons-codec-1.2.pom", "commons-codec-1.2.pom", 4096 );