1 package org.apache.maven.archiva.xml;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
31
32
33
34 public class LatinEntityResolutionReaderTest
35 extends AbstractArchivaXmlTestCase
36 {
37
38
39
40
41
42
43
44
45
46
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
66
67
68
69
70
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 }