View Javadoc

1   package org.apache.maven.archiva.xml;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import java.io.BufferedReader;
23  import java.io.File;
24  import java.io.FileReader;
25  import java.io.IOException;
26  import java.io.Reader;
27  import java.io.StringWriter;
28  
29  /**
30   * LatinEntityResolutionReaderTest 
31   *
32   * @version $Id: LatinEntityResolutionReaderTest.java 718864 2008-11-19 06:33:35Z brett $
33   */
34  public class LatinEntityResolutionReaderTest
35      extends AbstractArchivaXmlTestCase
36  {
37      /**
38       * A method to obtain the content of a reader as a String,
39       * while allowing for specifing the buffer size of the operation.
40       * 
41       * This method is only really useful for testing a Reader implementation.
42       * 
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.
47       */
48      private String toStringFromReader( Reader input, int bufsize )
49          throws IOException
50      {
51          StringWriter output = new StringWriter();
52  
53          final char[] buffer = new char[bufsize];
54          int n = 0;
55          while ( -1 != ( n = input.read( buffer ) ) )
56          {
57              output.write( buffer, 0, n );
58          }
59          output.flush();
60  
61          return output.toString();
62      }
63  
64      /**
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.
67       * 
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.
71       */
72      private String toStringFromExample( String examplePath )
73          throws IOException
74      {
75          File exampleFile = getExampleXml( examplePath );
76          FileReader fileReader = new FileReader( exampleFile );
77          BufferedReader lineReader = new BufferedReader( fileReader );
78          StringBuffer sb = new StringBuffer();
79  
80          boolean hasContent = false;
81  
82          String line = lineReader.readLine();
83          while ( line != null )
84          {
85              if ( hasContent )
86              {
87                  sb.append( "\n" );
88              }
89              sb.append( line );
90              hasContent = true;
91              line = lineReader.readLine();
92          }
93  
94          return sb.toString();
95      }
96  
97      public void assertProperRead( String sourcePath, String expectedPath, int bufsize )
98      {
99          try
100         {
101             File inputFile = getExampleXml( sourcePath );
102 
103             FileReader fileReader = new FileReader( inputFile );
104             LatinEntityResolutionReader testReader = new LatinEntityResolutionReader( fileReader );
105 
106             String actualOutput = toStringFromReader( testReader, bufsize );
107             String expectedOutput = toStringFromExample( expectedPath );
108 
109             assertEquals( expectedOutput, actualOutput );
110         }
111         catch ( IOException e )
112         {
113             fail( "IOException: " + e.getMessage() );
114         }
115     }
116 
117     private void assertProperRead( StringBuffer expected, String sourcePath, int bufSize )
118     {
119         try
120         {
121             File inputFile = getExampleXml( sourcePath );
122 
123             FileReader fileReader = new FileReader( inputFile );
124             LatinEntityResolutionReader testReader = new LatinEntityResolutionReader( fileReader );
125 
126             String actualOutput = toStringFromReader( testReader, bufSize );
127 
128             assertEquals( "Proper Read: ", expected.toString(), actualOutput );
129         }
130         catch ( IOException e )
131         {
132             fail( "IOException: " + e.getMessage() );
133         }
134     }
135 
136     public void testReaderNormalBufsize()
137         throws IOException
138     {
139         StringBuffer expected = new StringBuffer();
140 
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>" );
147 
148         assertProperRead( expected, "no-prolog-with-entities.xml", 4096 );
149     }
150 
151     public void testReaderSmallBufsize()
152         throws IOException
153     {
154         StringBuffer expected = new StringBuffer();
155 
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>" );
162 
163         assertProperRead( expected, "no-prolog-with-entities.xml", 1024 );
164     }
165 
166     public void testReaderRediculouslyTinyBufsize()
167         throws IOException
168     {
169         StringBuffer expected = new StringBuffer();
170 
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>" );
177 
178         assertProperRead( expected, "no-prolog-with-entities.xml", 32 );
179     }
180 
181     public void testReaderHugeBufsize()
182         throws IOException
183     {
184         StringBuffer expected = new StringBuffer();
185 
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>" );
192 
193         assertProperRead( expected, "no-prolog-with-entities.xml", 409600 );
194     }
195 
196     public void testNoLatinEntitiesHugeLine()
197     {
198         assertProperRead( "commons-codec-1.2.pom", "commons-codec-1.2.pom", 4096 );
199     }
200 }