1 package org.apache.maven.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
32 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
35 public class LatinEntityResolutionReaderTest
36 extends AbstractArchivaXmlTestCase
39 * A method to obtain the content of a reader as a String,
40 * while allowing for specifing the buffer size of the operation.
42 * This method is only really useful for testing a Reader implementation.
44 * @param input the reader to get the input from.
45 * @param bufsize the buffer size to use.
46 * @return the contents of the reader as a String.
47 * @throws IOException if there was an I/O error.
49 private String toStringFromReader( Reader input, int bufsize )
52 StringWriter output = new StringWriter();
54 final char[] buffer = new char[bufsize];
56 while ( -1 != ( n = input.read( buffer ) ) )
58 output.write( buffer, 0, n );
62 return output.toString();
66 * This reads a text file from the src/test/examples directory,
67 * normalizes the end of lines, and returns the contents as a big String.
69 * @param examplePath the name of the file in the src/test/examples directory.
70 * @return the contents of the provided file
71 * @throws IOException if there was an I/O error.
73 private String toStringFromExample( String examplePath )
76 File exampleFile = getExampleXml( examplePath );
77 FileReader fileReader = new FileReader( exampleFile );
78 BufferedReader lineReader = new BufferedReader( fileReader );
79 StringBuffer sb = new StringBuffer();
81 boolean hasContent = false;
83 String line = lineReader.readLine();
84 while ( line != null )
92 line = lineReader.readLine();
98 public void assertProperRead( String sourcePath, String expectedPath, int bufsize )
102 File inputFile = getExampleXml( sourcePath );
104 FileReader fileReader = new FileReader( inputFile );
105 LatinEntityResolutionReader testReader = new LatinEntityResolutionReader( fileReader );
107 String actualOutput = toStringFromReader( testReader, bufsize );
108 String expectedOutput = toStringFromExample( expectedPath );
110 assertEquals( expectedOutput, actualOutput );
112 catch ( IOException e )
114 fail( "IOException: " + e.getMessage() );
118 public void testReaderNormalBufsize()
121 assertProperRead( "no-prolog-with-entities.xml", "no-prolog-with-entities.xml-resolved", 4096 );
124 public void testReaderSmallBufsize()
127 assertProperRead( "no-prolog-with-entities.xml", "no-prolog-with-entities.xml-resolved", 1024 );
130 public void testReaderRediculouslyTinyBufsize()
133 assertProperRead( "no-prolog-with-entities.xml", "no-prolog-with-entities.xml-resolved", 32 );
136 public void testReaderHugeBufsize()
139 assertProperRead( "no-prolog-with-entities.xml", "no-prolog-with-entities.xml-resolved", 409600 );
142 public void testNoLatinEntitiesHugeLine()
144 assertProperRead( "commons-codec-1.2.pom", "commons-codec-1.2.pom", 4096 );