1 package org.apache.archiva.xml;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.dom4j.DocumentException;
23 import org.dom4j.io.SAXReader;
24 import org.junit.Assert;
25 import org.junit.Test;
26
27 import java.io.BufferedReader;
28 import java.io.File;
29 import java.io.FileReader;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.io.Reader;
34 import java.io.StringWriter;
35 import java.net.URL;
36 import java.nio.charset.Charset;
37
38
39
40
41 public class LatinEntityResolutionReaderTest
42 extends AbstractArchivaXmlTestCase
43 {
44
45
46
47
48
49
50
51
52
53
54
55 private String toStringFromReader( Reader input, int bufsize )
56 throws IOException
57 {
58 StringWriter output = new StringWriter();
59
60 final char[] buffer = new char[bufsize];
61 int n = 0;
62 while ( -1 != ( n = input.read( buffer ) ) )
63 {
64 output.write( buffer, 0, n );
65 }
66 output.flush();
67
68 return output.toString();
69 }
70
71
72
73
74
75
76
77
78
79 private String toStringFromExample( String examplePath )
80 throws IOException
81 {
82 File exampleFile = getExampleXml( examplePath );
83 FileReader fileReader = new FileReader( exampleFile );
84 BufferedReader lineReader = new BufferedReader( fileReader );
85 StringBuilder sb = new StringBuilder();
86
87 boolean hasContent = false;
88
89 String line = lineReader.readLine();
90 while ( line != null )
91 {
92 if ( hasContent )
93 {
94 sb.append( "\n" );
95 }
96 sb.append( line );
97 hasContent = true;
98 line = lineReader.readLine();
99 }
100
101 return sb.toString();
102 }
103
104 public void assertProperRead( String sourcePath, String expectedPath, int bufsize )
105 {
106 try
107 {
108 File inputFile = getExampleXml( sourcePath );
109
110 FileReader fileReader = new FileReader( inputFile );
111 LatinEntityResolutionReader testReader = new LatinEntityResolutionReader( fileReader );
112
113 String actualOutput = toStringFromReader( testReader, bufsize );
114 String expectedOutput = toStringFromExample( expectedPath );
115
116 assertEquals( expectedOutput, actualOutput );
117 }
118 catch ( IOException e )
119 {
120 fail( "IOException: " + e.getMessage() );
121 }
122 }
123
124 private void assertProperRead( StringBuilder expected, String sourcePath, int bufSize )
125 {
126 try
127 {
128 File inputFile = getExampleXml( sourcePath );
129
130 FileReader fileReader = new FileReader( inputFile );
131 LatinEntityResolutionReader testReader = new LatinEntityResolutionReader( fileReader );
132
133 String actualOutput = toStringFromReader( testReader, bufSize );
134
135 assertEquals( "Proper Read: ", expected.toString(), actualOutput );
136 }
137 catch ( IOException e )
138 {
139 fail( "IOException: " + e.getMessage() );
140 }
141 }
142
143 @Test
144 public void testReaderNormalBufsize()
145 throws IOException
146 {
147 StringBuilder expected = new StringBuilder();
148
149 expected.append( "<basic>\n" );
150 expected.append( " <names>\n" );
151 expected.append( " <name>" ).append( TRYGVIS ).append( "</name>\n" );
152 expected.append( " <name>" ).append( INFINITE_ARCHIVA ).append( "</name>\n" );
153 expected.append( " </names>\n" );
154 expected.append( "</basic>" );
155
156 assertProperRead( expected, "no-prolog-with-entities.xml", 4096 );
157 }
158
159 @Test
160 public void testReaderSmallBufsize()
161 throws IOException
162 {
163 StringBuilder expected = new StringBuilder();
164
165 expected.append( "<basic>\n" );
166 expected.append( " <names>\n" );
167 expected.append( " <name>" ).append( TRYGVIS ).append( "</name>\n" );
168 expected.append( " <name>" ).append( INFINITE_ARCHIVA ).append( "</name>\n" );
169 expected.append( " </names>\n" );
170 expected.append( "</basic>" );
171
172 assertProperRead( expected, "no-prolog-with-entities.xml", 1024 );
173 }
174
175 @Test
176 public void testReaderRediculouslyTinyBufsize()
177 throws IOException
178 {
179 StringBuilder expected = new StringBuilder();
180
181 expected.append( "<basic>\n" );
182 expected.append( " <names>\n" );
183 expected.append( " <name>" ).append( TRYGVIS ).append( "</name>\n" );
184 expected.append( " <name>" ).append( INFINITE_ARCHIVA ).append( "</name>\n" );
185 expected.append( " </names>\n" );
186 expected.append( "</basic>" );
187
188 assertProperRead( expected, "no-prolog-with-entities.xml", 32 );
189 }
190
191 @Test
192 public void testReaderHugeBufsize()
193 throws IOException
194 {
195 StringBuilder expected = new StringBuilder();
196
197 expected.append( "<basic>\n" );
198 expected.append( " <names>\n" );
199 expected.append( " <name>" ).append( TRYGVIS ).append( "</name>\n" );
200 expected.append( " <name>" ).append( INFINITE_ARCHIVA ).append( "</name>\n" );
201 expected.append( " </names>\n" );
202 expected.append( "</basic>" );
203
204 assertProperRead( expected, "no-prolog-with-entities.xml", 409600 );
205 }
206
207 @Test
208 public void testReaderLeftOver()
209 throws IOException
210 {
211 File inputFile = getExampleXml( "maven-metadata-leftover.xml" );
212
213 InputStream in = null;
214 SAXReader reader = new SAXReader();
215 URL url = inputFile.toURL();
216 in = url.openStream();
217 InputStreamReader inReader = new InputStreamReader( in, Charset.forName( "UTF-8" ) );
218 LatinEntityResolutionReader latinReader = new LatinEntityResolutionReader( inReader );
219 try
220 {
221 reader.read( latinReader );
222 }
223 catch ( DocumentException e )
224 {
225 Assert.fail( "Should not have failed here." + e );
226 IOException ioe = new IOException();
227 ioe.initCause( e );
228 throw ioe;
229 }
230 }
231
232 @Test
233 public void testNoLatinEntitiesHugeLine()
234 {
235 assertProperRead( "commons-codec-1.2.pom", "commons-codec-1.2.pom", 4096 );
236 }
237 }