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;
35 import org.junit.Test;
38 * LatinEntityResolutionReaderTest
42 public class LatinEntityResolutionReaderTest
43 extends AbstractArchivaXmlTestCase
46 * A method to obtain the content of a reader as a String,
47 * while allowing for specifing the buffer size of the operation.
49 * This method is only really useful for testing a Reader implementation.
51 * @param input the reader to get the input from.
52 * @param bufsize the buffer size to use.
53 * @return the contents of the reader as a String.
54 * @throws IOException if there was an I/O error.
56 private String toStringFromReader( Reader input, int bufsize )
59 StringWriter output = new StringWriter();
61 final char[] buffer = new char[bufsize];
63 while ( -1 != ( n = input.read( buffer ) ) )
65 output.write( buffer, 0, n );
69 return output.toString();
73 * This reads a text file from the src/test/examples directory,
74 * normalizes the end of lines, and returns the contents as a big String.
76 * @param examplePath the name of the file in the src/test/examples directory.
77 * @return the contents of the provided file
78 * @throws IOException if there was an I/O error.
80 private String toStringFromExample( String examplePath )
83 File exampleFile = getExampleXml( examplePath );
84 FileReader fileReader = new FileReader( exampleFile );
85 BufferedReader lineReader = new BufferedReader( fileReader );
86 StringBuffer sb = new StringBuffer();
88 boolean hasContent = false;
90 String line = lineReader.readLine();
91 while ( line != null )
99 line = lineReader.readLine();
102 return sb.toString();
105 public void assertProperRead( String sourcePath, String expectedPath, int bufsize )
109 File inputFile = getExampleXml( sourcePath );
111 FileReader fileReader = new FileReader( inputFile );
112 LatinEntityResolutionReader testReader = new LatinEntityResolutionReader( fileReader );
114 String actualOutput = toStringFromReader( testReader, bufsize );
115 String expectedOutput = toStringFromExample( expectedPath );
117 assertEquals( expectedOutput, actualOutput );
119 catch ( IOException e )
121 fail( "IOException: " + e.getMessage() );
125 private void assertProperRead( StringBuffer expected, String sourcePath, int bufSize )
129 File inputFile = getExampleXml( sourcePath );
131 FileReader fileReader = new FileReader( inputFile );
132 LatinEntityResolutionReader testReader = new LatinEntityResolutionReader( fileReader );
134 String actualOutput = toStringFromReader( testReader, bufSize );
136 assertEquals( "Proper Read: ", expected.toString(), actualOutput );
138 catch ( IOException e )
140 fail( "IOException: " + e.getMessage() );
145 public void testReaderNormalBufsize()
148 StringBuffer expected = new StringBuffer();
150 expected.append( "<basic>\n" );
151 expected.append( " <names>\n" );
152 expected.append( " <name>" ).append( TRYGVIS ).append( "</name>\n" );
153 expected.append( " <name>" ).append( INFINITE_ARCHIVA ).append( "</name>\n" );
154 expected.append( " </names>\n" );
155 expected.append( "</basic>" );
157 assertProperRead( expected, "no-prolog-with-entities.xml", 4096 );
161 public void testReaderSmallBufsize()
164 StringBuffer expected = new StringBuffer();
166 expected.append( "<basic>\n" );
167 expected.append( " <names>\n" );
168 expected.append( " <name>" ).append( TRYGVIS ).append( "</name>\n" );
169 expected.append( " <name>" ).append( INFINITE_ARCHIVA ).append( "</name>\n" );
170 expected.append( " </names>\n" );
171 expected.append( "</basic>" );
173 assertProperRead( expected, "no-prolog-with-entities.xml", 1024 );
177 public void testReaderRediculouslyTinyBufsize()
180 StringBuffer expected = new StringBuffer();
182 expected.append( "<basic>\n" );
183 expected.append( " <names>\n" );
184 expected.append( " <name>" ).append( TRYGVIS ).append( "</name>\n" );
185 expected.append( " <name>" ).append( INFINITE_ARCHIVA ).append( "</name>\n" );
186 expected.append( " </names>\n" );
187 expected.append( "</basic>" );
189 assertProperRead( expected, "no-prolog-with-entities.xml", 32 );
193 public void testReaderHugeBufsize()
196 StringBuffer expected = new StringBuffer();
198 expected.append( "<basic>\n" );
199 expected.append( " <names>\n" );
200 expected.append( " <name>" ).append( TRYGVIS ).append( "</name>\n" );
201 expected.append( " <name>" ).append( INFINITE_ARCHIVA ).append( "</name>\n" );
202 expected.append( " </names>\n" );
203 expected.append( "</basic>" );
205 assertProperRead( expected, "no-prolog-with-entities.xml", 409600 );
209 public void testReaderLeftOver()
212 File inputFile = getExampleXml( "maven-metadata-leftover.xml" );
213 //Bits from RepositoryMetadataReader.read
214 InputStream in = null;
215 SAXReader reader = new SAXReader();
216 URL url = inputFile.toURL();
217 in = url.openStream();
218 InputStreamReader inReader = new InputStreamReader( in, "UTF-8" );
219 LatinEntityResolutionReader latinReader = new LatinEntityResolutionReader( inReader );
222 reader.read( latinReader );
224 catch ( DocumentException e )
226 Assert.fail( "Should not have failed here." + e );
227 IOException ioe = new IOException();
234 public void testNoLatinEntitiesHugeLine()
236 assertProperRead( "commons-codec-1.2.pom", "commons-codec-1.2.pom", 4096 );