1 package org.apache.maven.doxia.siterenderer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.io.Reader;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Map;
33
34 import org.apache.maven.doxia.site.decoration.DecorationModel;
35 import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Reader;
36 import org.apache.maven.doxia.xsd.AbstractXmlValidatorTest;
37
38 import org.codehaus.plexus.PlexusTestCase;
39 import org.codehaus.plexus.util.FileUtils;
40 import org.codehaus.plexus.util.IOUtil;
41 import org.codehaus.plexus.util.ReaderFactory;
42 import org.codehaus.plexus.util.StringUtils;
43
44
45
46
47
48
49
50 public class DefaultSiteRendererTest
51 extends PlexusTestCase
52 {
53
54
55
56 private static final String OUTPUT = "target/output";
57
58
59
60
61 private Renderer renderer;
62
63
64
65
66
67 protected void setUp()
68 throws Exception
69 {
70 super.setUp();
71
72 renderer = (Renderer) lookup( Renderer.ROLE );
73
74
75 InputStream is =
76 this.getResourceAsStream( "/org/apache/maven/doxia/siterenderer/resources/default-site.vm" );
77 assertNotNull( is );
78 OutputStream os = new FileOutputStream( new File( getBasedir(), "target/test-classes/default-site.vm" ) );
79 try
80 {
81 IOUtil.copy( is, os );
82 }
83 finally
84 {
85 IOUtil.close( is );
86 IOUtil.close( os );
87 }
88
89
90 FileUtils.deleteDirectory( getTestFile( OUTPUT ) );
91 }
92
93
94
95
96
97 protected void tearDown()
98 throws Exception
99 {
100 release( renderer );
101 super.tearDown();
102 }
103
104
105
106
107 public void testRender()
108 throws Exception
109 {
110
111
112
113 DecorationModel decoration = new DecorationXpp3Reader()
114 .read( new FileReader( getTestFile( "src/test/resources/site/site.xml" ) ) );
115
116 SiteRenderingContext ctxt = new SiteRenderingContext();
117 ctxt.setTemplateName( "default-site.vm" );
118 ctxt.setTemplateClassLoader( getClassLoader() );
119 ctxt.setUsingDefaultTemplate( true );
120 Map templateProp = new HashMap();
121 templateProp.put( "outputEncoding", "UTF-8" );
122 ctxt.setTemplateProperties( templateProp );
123 ctxt.setDecoration( decoration );
124 ctxt.addSiteDirectory( getTestFile( "src/test/resources/site" ) );
125
126 renderer.render( renderer.locateDocumentFiles( ctxt ).values(), ctxt, getTestFile( OUTPUT ) );
127
128
129
130
131 verifyHeadPage();
132 verifyCdcPage();
133 verifyNestedItemsPage();
134 verifyMultipleBlock();
135 verifyMacro();
136 verifyEntitiesPage();
137 verifyJavascriptPage();
138 verifyFaqPage();
139 verifyAttributes();
140 verifyMisc();
141 verifyDocbookPageExists();
142 verifyApt();
143
144
145
146
147 validatePages();
148 }
149
150
151
152
153 public void verifyHeadPage()
154 throws Exception
155 {
156 new HeadVerifier().verify( "target/output/head.html" );
157 }
158
159
160
161
162 public void verifyCdcPage()
163 throws Exception
164 {
165 File nestedItems = getTestFile( "target/output/cdc.html" );
166 assertNotNull( nestedItems );
167 assertTrue( nestedItems.exists() );
168 }
169
170
171
172
173 public void verifyNestedItemsPage()
174 throws Exception
175 {
176 NestedItemsVerifier verifier = new NestedItemsVerifier();
177 verifier.verify( "target/output/nestedItems.html" );
178 }
179
180
181
182
183 public void verifyMultipleBlock()
184 throws Exception
185 {
186 MultipleBlockVerifier verifier = new MultipleBlockVerifier();
187 verifier.verify( "target/output/multipleblock.html" );
188 }
189
190
191
192
193 public void verifyMacro()
194 throws Exception
195 {
196 File macro = getTestFile( "target/output/macro.html" );
197 assertNotNull( macro );
198 assertTrue( macro.exists() );
199
200 Reader reader = null;
201 try
202 {
203 reader = ReaderFactory.newXmlReader( macro );
204 String content = IOUtil.toString( reader );
205 assertEquals( content.indexOf( "</macro>" ), -1 );
206 }
207 finally
208 {
209 IOUtil.close( reader );
210 }
211 }
212
213
214
215
216 public void verifyEntitiesPage()
217 throws Exception
218 {
219 EntitiesVerifier verifier = new EntitiesVerifier();
220 verifier.verify( "target/output/entityTest.html" );
221 }
222
223
224
225
226 public void verifyJavascriptPage()
227 throws Exception
228 {
229 JavascriptVerifier verifier = new JavascriptVerifier();
230 verifier.verify( "target/output/javascript.html" );
231 }
232
233
234
235
236 public void verifyFaqPage()
237 throws Exception
238 {
239 FaqVerifier verifier = new FaqVerifier();
240 verifier.verify( "target/output/faq.html" );
241 }
242
243
244
245
246 public void verifyAttributes()
247 throws Exception
248 {
249 AttributesVerifier verifier = new AttributesVerifier();
250 verifier.verify( "target/output/attributes.html" );
251 }
252
253
254
255
256 public void verifyMisc()
257 throws Exception
258 {
259 MiscVerifier verifier = new MiscVerifier();
260 verifier.verify( "target/output/misc.html" );
261 }
262
263
264
265
266 public void verifyDocbookPageExists()
267 throws Exception
268 {
269 File nestedItems = getTestFile( "target/output/docbook.html" );
270 assertNotNull( nestedItems );
271 assertTrue( nestedItems.exists() );
272 }
273
274
275
276
277 public void verifyApt()
278 throws Exception
279 {
280 AptVerifier verifier = new AptVerifier();
281 verifier.verify( "target/output/apt.html" );
282 }
283
284
285
286
287
288
289
290 public void validatePages() throws Exception
291 {
292
293 XhtmlValidatorTest validator = new XhtmlValidatorTest();
294 validator.setUp();
295 validator.testValidateFiles();
296 }
297
298 protected static class XhtmlValidatorTest
299 extends AbstractXmlValidatorTest
300 {
301
302 protected void setUp()
303 throws Exception
304 {
305 super.setUp();
306 }
307
308
309 protected void tearDown()
310 throws Exception
311 {
312 super.tearDown();
313 }
314
315
316 protected String[] getIncludes()
317 {
318 return new String[] { "**/*.html" };
319 }
320
321
322 protected String addNamespaces( String content )
323 {
324 return content;
325 }
326
327
328 protected Map getTestDocuments()
329 throws IOException
330 {
331 Map testDocs = new HashMap();
332
333 File dir = new File( getBasedir(), "target/output" );
334
335 List l = FileUtils.getFileNames( dir, getIncludes()[0], FileUtils.getDefaultExcludesAsString(), true );
336 for ( Iterator it = l.iterator(); it.hasNext(); )
337 {
338 String file = it.next().toString();
339 file = StringUtils.replace( file, "\\", "/" );
340
341 Reader reader = ReaderFactory.newXmlReader( new File( file ) );
342 try
343 {
344 testDocs.put( file, IOUtil.toString( reader ) );
345 }
346 finally
347 {
348 IOUtil.close( reader );
349 }
350 }
351
352 return testDocs;
353 }
354
355
356 protected boolean isFailErrorMessage( String message )
357 {
358 return true;
359 }
360 }
361 }