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 junit.framework.Assert;
23 import org.dom4j.DocumentException;
24 import org.dom4j.io.SAXReader;
26 import java.io.BufferedReader;
28 import java.io.FileReader;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.io.Reader;
33 import java.io.StringWriter;
37 * LatinEntityResolutionReaderTest
41 public class LatinEntityResolutionReaderTest
42 extends AbstractArchivaXmlTestCase
45 * A method to obtain the content of a reader as a String,
46 * while allowing for specifing the buffer size of the operation.
48 * This method is only really useful for testing a Reader implementation.
50 * @param input the reader to get the input from.
51 * @param bufsize the buffer size to use.
52 * @return the contents of the reader as a String.
53 * @throws IOException if there was an I/O error.
55 private String toStringFromReader( Reader input, int bufsize )
58 StringWriter output = new StringWriter();
60 final char[] buffer = new char[bufsize];
62 while ( -1 != ( n = input.read( buffer ) ) )
64 output.write( buffer, 0, n );
68 return output.toString();
72 * This reads a text file from the src/test/examples directory,
73 * normalizes the end of lines, and returns the contents as a big String.
75 * @param examplePath the name of the file in the src/test/examples directory.
76 * @return the contents of the provided file
77 * @throws IOException if there was an I/O error.
79 private String toStringFromExample( String examplePath )
82 File exampleFile = getExampleXml( examplePath );
83 FileReader fileReader = new FileReader( exampleFile );
84 BufferedReader lineReader = new BufferedReader( fileReader );
85 StringBuffer sb = new StringBuffer();
87 boolean hasContent = false;
89 String line = lineReader.readLine();
90 while ( line != null )
98 line = lineReader.readLine();
101 return sb.toString();
104 public void assertProperRead( String sourcePath, String expectedPath, int bufsize )
108 File inputFile = getExampleXml( sourcePath );
110 FileReader fileReader = new FileReader( inputFile );
111 LatinEntityResolutionReader testReader = new LatinEntityResolutionReader( fileReader );
113 String actualOutput = toStringFromReader( testReader, bufsize );
114 String expectedOutput = toStringFromExample( expectedPath );
116 assertEquals( expectedOutput, actualOutput );
118 catch ( IOException e )
120 fail( "IOException: " + e.getMessage() );
124 private void assertProperRead( StringBuffer expected, String sourcePath, int bufSize )
128 File inputFile = getExampleXml( sourcePath );
130 FileReader fileReader = new FileReader( inputFile );
131 LatinEntityResolutionReader testReader = new LatinEntityResolutionReader( fileReader );
133 String actualOutput = toStringFromReader( testReader, bufSize );
135 assertEquals( "Proper Read: ", expected.toString(), actualOutput );
137 catch ( IOException e )
139 fail( "IOException: " + e.getMessage() );
143 public void testReaderNormalBufsize()
146 StringBuffer expected = new StringBuffer();
148 expected.append( "<basic>\n" );
149 expected.append( " <names>\n" );
150 expected.append( " <name>" ).append( TRYGVIS ).append( "</name>\n" );
151 expected.append( " <name>" ).append( INFINITE_ARCHIVA ).append( "</name>\n" );
152 expected.append( " </names>\n" );
153 expected.append( "</basic>" );
155 assertProperRead( expected, "no-prolog-with-entities.xml", 4096 );
158 public void testReaderSmallBufsize()
161 StringBuffer expected = new StringBuffer();
163 expected.append( "<basic>\n" );
164 expected.append( " <names>\n" );
165 expected.append( " <name>" ).append( TRYGVIS ).append( "</name>\n" );
166 expected.append( " <name>" ).append( INFINITE_ARCHIVA ).append( "</name>\n" );
167 expected.append( " </names>\n" );
168 expected.append( "</basic>" );
170 assertProperRead( expected, "no-prolog-with-entities.xml", 1024 );
173 public void testReaderRediculouslyTinyBufsize()
176 StringBuffer expected = new StringBuffer();
178 expected.append( "<basic>\n" );
179 expected.append( " <names>\n" );
180 expected.append( " <name>" ).append( TRYGVIS ).append( "</name>\n" );
181 expected.append( " <name>" ).append( INFINITE_ARCHIVA ).append( "</name>\n" );
182 expected.append( " </names>\n" );
183 expected.append( "</basic>" );
185 assertProperRead( expected, "no-prolog-with-entities.xml", 32 );
188 public void testReaderHugeBufsize()
191 StringBuffer expected = new StringBuffer();
193 expected.append( "<basic>\n" );
194 expected.append( " <names>\n" );
195 expected.append( " <name>" ).append( TRYGVIS ).append( "</name>\n" );
196 expected.append( " <name>" ).append( INFINITE_ARCHIVA ).append( "</name>\n" );
197 expected.append( " </names>\n" );
198 expected.append( "</basic>" );
200 assertProperRead( expected, "no-prolog-with-entities.xml", 409600 );
204 public void testReaderLeftOver()
207 File inputFile = getExampleXml( "maven-metadata-leftover.xml" );
208 //Bits from RepositoryMetadataReader.read
209 InputStream in = null;
210 SAXReader reader = new SAXReader();
211 URL url = inputFile.toURL();
212 in = url.openStream();
213 InputStreamReader inReader = new InputStreamReader( in, "UTF-8" );
214 LatinEntityResolutionReader latinReader = new LatinEntityResolutionReader( inReader );
217 reader.read( latinReader );
219 catch ( DocumentException e )
221 Assert.fail( "Should not have failed here." + e );
222 IOException ioe = new IOException();
229 public void testNoLatinEntitiesHugeLine()
231 assertProperRead( "commons-codec-1.2.pom", "commons-codec-1.2.pom", 4096 );