View Javadoc

1   package org.apache.maven.doxia.siterenderer;
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.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.io.StringWriter;
30  import java.util.Collections;
31  import java.util.HashMap;
32  import java.util.Iterator;
33  import java.util.List;
34  import java.util.Locale;
35  import java.util.Map;
36  import java.util.jar.JarOutputStream;
37  import java.util.zip.ZipEntry;
38  
39  import org.apache.commons.io.IOUtils;
40  import org.apache.maven.doxia.sink.render.RenderingContext;
41  import org.apache.maven.doxia.site.decoration.DecorationModel;
42  import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Reader;
43  import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
44  import org.apache.maven.doxia.xsd.AbstractXmlValidator;
45  import org.codehaus.plexus.PlexusTestCase;
46  import org.codehaus.plexus.util.FileUtils;
47  import org.codehaus.plexus.util.IOUtil;
48  import org.codehaus.plexus.util.ReaderFactory;
49  import org.codehaus.plexus.util.StringUtils;
50  import org.xml.sax.EntityResolver;
51  
52  /**
53   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
54   * @author <a href="mailto:evenisse@codehaus.org>Emmanuel Venisse</a>
55   * @version $Id: DefaultSiteRendererTest.java 1451951 2013-03-02 22:21:20Z olamy $
56   */
57  public class DefaultSiteRendererTest
58      extends PlexusTestCase
59  {
60      /**
61       * All output produced by this test will go here.
62       */
63      private static final String OUTPUT = "target/output";
64  
65      /**
66       * The renderer used to produce output.
67       */
68      private Renderer renderer;
69  
70      /**
71       * The locale before executing tests.
72       */
73      private Locale oldLocale;
74  
75      private File skinJar = new File( getBasedir(), "target/test-classes/skin.jar" );
76  
77      /**
78       * @throws java.lang.Exception if something goes wrong.
79       * @see org.codehaus.plexus.PlexusTestCase#setUp()
80       */
81      @Override
82      protected void setUp()
83          throws Exception
84      {
85          super.setUp();
86  
87          renderer = (Renderer) lookup( Renderer.ROLE );
88  
89          // copy the default-site.vm
90          InputStream is =
91              this.getResourceAsStream( "/org/apache/maven/doxia/siterenderer/resources/default-site.vm" );
92          assertNotNull( is );
93          OutputStream os = new FileOutputStream( new File( getBasedir(), "target/test-classes/default-site.vm" ) );
94          try
95          {
96              IOUtil.copy( is, os );
97          }
98          finally
99          {
100             IOUtil.close( is );
101             IOUtil.close( os );
102         }
103 
104         InputStream skinIS = this.getResourceAsStream( "velocity-toolmanager.vm" );
105         JarOutputStream jarOS = new JarOutputStream( new FileOutputStream( skinJar ) );
106         try
107         {
108             jarOS.putNextEntry( new ZipEntry( "META-INF/maven/site.vm" ) );
109             IOUtil.copy( skinIS, jarOS );
110             jarOS.closeEntry();
111         }
112         finally
113         {
114             IOUtil.close( skinIS );
115             IOUtil.close( jarOS );
116         }
117 
118         oldLocale = Locale.getDefault();
119         Locale.setDefault( Locale.ENGLISH );
120     }
121 
122     /**
123      * @throws java.lang.Exception if something goes wrong.
124      * @see org.codehaus.plexus.PlexusTestCase#tearDown()
125      */
126     @Override
127     protected void tearDown()
128         throws Exception
129     {
130         release( renderer );
131         super.tearDown();
132 
133         Locale.setDefault( oldLocale );
134     }
135 
136     /**
137      * @throws Exception if something goes wrong.
138      */
139     public void testRender()
140         throws Exception
141     {
142         // Safety
143         FileUtils.deleteDirectory( getTestFile( OUTPUT ) );
144 
145         // ----------------------------------------------------------------------
146         // Render the site from src/test/resources/site to OUTPUT
147         // ----------------------------------------------------------------------
148         DecorationModel decoration = new DecorationXpp3Reader()
149             .read( new FileReader( getTestFile( "src/test/resources/site/site.xml" ) ) );
150 
151         SiteRenderingContext ctxt = getSiteRenderingContext( decoration, "src/test/resources/site", false );
152         renderer.render( renderer.locateDocumentFiles( ctxt ).values(), ctxt, getTestFile( OUTPUT ) );
153 
154         ctxt = getSiteRenderingContext( decoration, "src/test/resources/site-validate", true );
155         renderer.render( renderer.locateDocumentFiles( ctxt ).values(), ctxt, getTestFile( OUTPUT ) );
156 
157         // ----------------------------------------------------------------------
158         // Verify specific pages
159         // ----------------------------------------------------------------------
160         verifyHeadPage();
161         verifyCdcPage();
162         verifyNestedItemsPage();
163         verifyMultipleBlock();
164         verifyMacro();
165         verifyEntitiesPage();
166         verifyJavascriptPage();
167         verifyFaqPage();
168         verifyAttributes();
169         verifyMisc();
170         verifyDocbookPageExists();
171         verifyApt();
172 
173         // ----------------------------------------------------------------------
174         // Validate the rendering pages
175         // ----------------------------------------------------------------------
176         validatePages();
177     }
178 
179     public void testVelocityToolManager()
180         throws Exception
181     {
182         StringWriter writer = new StringWriter();
183 
184         SiteRenderingContext siteRenderingContext = new SiteRenderingContext();
185         siteRenderingContext.setDecoration(new DecorationModel());
186         siteRenderingContext.setTemplateName( "org/apache/maven/doxia/siterenderer/velocity-toolmanager.vm" );
187         RenderingContext context = new RenderingContext( new File( "" ), "document.html" );
188         SiteRendererSink sink = new SiteRendererSink( context );
189         renderer.generateDocument( writer, sink, siteRenderingContext );
190 
191         String renderResult = writer.toString();
192         String expectedResult = IOUtils.toString( getClass().getResourceAsStream( "velocity-toolmanager.expected.txt" ) );
193         assertEquals( expectedResult, renderResult );
194     }
195 
196     public void testVelocityToolManagerForTemplate() throws Exception
197     {
198         StringWriter writer = new StringWriter();
199 
200         File templateFile = new File( getBasedir(), "target/test-classes/org/apache/maven/doxia/siterenderer/velocity-toolmanager.vm" );
201         Map<String, ?> attributes = Collections.emptyMap();
202 
203         SiteRenderingContext siteRenderingContext = renderer.createContextForTemplate( templateFile, null, attributes, new DecorationModel(), "defaultWindowTitle", Locale.ENGLISH );
204         RenderingContext context = new RenderingContext( new File( "" ), "document.html" );
205         SiteRendererSink sink = new SiteRendererSink( context );
206         renderer.generateDocument( writer, sink, siteRenderingContext );
207 
208         String renderResult = writer.toString();
209         String expectedResult = IOUtils.toString( getClass().getResourceAsStream( "velocity-toolmanager.expected.txt" ) );
210         assertEquals( expectedResult, renderResult );
211     }
212 
213     public void testVelocityToolManagerForSkin() throws Exception
214     {
215         StringWriter writer = new StringWriter();
216 
217         File skinFile = skinJar;
218         Map<String, ?> attributes = Collections.emptyMap();
219         SiteRenderingContext siteRenderingContext = renderer.createContextForSkin( skinFile, attributes, new DecorationModel(), "defaultWindowTitle", Locale.ENGLISH );
220         RenderingContext context = new RenderingContext( new File( "" ), "document.html" );
221         SiteRendererSink sink = new SiteRendererSink( context );
222         renderer.generateDocument( writer, sink, siteRenderingContext );
223 
224         String renderResult = writer.toString();
225         String expectedResult = IOUtils.toString( getClass().getResourceAsStream( "velocity-toolmanager.expected.txt" ) );
226         assertEquals( expectedResult, renderResult );
227     }
228 
229     private SiteRenderingContext getSiteRenderingContext( DecorationModel decoration, String siteDir, boolean validate )
230     {
231         SiteRenderingContext ctxt = new SiteRenderingContext();
232         ctxt.setTemplateName( "default-site.vm" );
233         ctxt.setTemplateClassLoader( getClassLoader() );
234         ctxt.setUsingDefaultTemplate( true );
235         final Map<String, String> templateProp = new HashMap<String, String>();
236         templateProp.put( "outputEncoding", "UTF-8" );
237         ctxt.setTemplateProperties( templateProp );
238         ctxt.setDecoration( decoration );
239         ctxt.addSiteDirectory( getTestFile( siteDir ) );
240         ctxt.setValidate( validate );
241 
242         return ctxt;
243     }
244 
245     /**
246      * @throws Exception if something goes wrong.
247      */
248     public void verifyHeadPage()
249         throws Exception
250     {
251         new HeadVerifier().verify( "target/output/head.html" );
252     }
253 
254     /**
255      * @throws Exception if something goes wrong.
256      */
257     public void verifyCdcPage()
258         throws Exception
259     {
260         File nestedItems = getTestFile( "target/output/cdc.html" );
261         assertNotNull( nestedItems );
262         assertTrue( nestedItems.exists() );
263     }
264 
265     /**
266      * @throws Exception if something goes wrong.
267      */
268     public void verifyNestedItemsPage()
269         throws Exception
270     {
271         NestedItemsVerifier verifier = new NestedItemsVerifier();
272         verifier.verify( "target/output/nestedItems.html" );
273     }
274 
275     /**
276      * @throws Exception if something goes wrong.
277      */
278     public void verifyMultipleBlock()
279         throws Exception
280     {
281         MultipleBlockVerifier verifier = new MultipleBlockVerifier();
282         verifier.verify( "target/output/multipleblock.html" );
283     }
284 
285     /**
286      * @throws Exception if something goes wrong.
287      */
288     public void verifyMacro()
289         throws Exception
290     {
291         File macro = getTestFile( "target/output/macro.html" );
292         assertNotNull( macro );
293         assertTrue( macro.exists() );
294 
295         Reader reader = null;
296         try
297         {
298             reader = ReaderFactory.newXmlReader( macro );
299             String content = IOUtil.toString( reader );
300             assertEquals( content.indexOf( "</macro>" ), -1 );
301         }
302         finally
303         {
304             IOUtil.close( reader );
305         }
306     }
307 
308     /**
309      * @throws Exception if something goes wrong.
310      */
311     public void verifyEntitiesPage()
312         throws Exception
313     {
314         EntitiesVerifier verifier = new EntitiesVerifier();
315         verifier.verify( "target/output/entityTest.html" );
316     }
317 
318     /**
319      * @throws Exception if something goes wrong.
320      */
321     public void verifyJavascriptPage()
322         throws Exception
323     {
324         JavascriptVerifier verifier = new JavascriptVerifier();
325         verifier.verify( "target/output/javascript.html" );
326     }
327 
328     /**
329      * @throws Exception if something goes wrong.
330      */
331     public void verifyFaqPage()
332         throws Exception
333     {
334         FaqVerifier verifier = new FaqVerifier();
335         verifier.verify( "target/output/faq.html" );
336     }
337 
338     /**
339      * @throws Exception if something goes wrong.
340      */
341     public void verifyAttributes()
342         throws Exception
343     {
344         AttributesVerifier verifier = new AttributesVerifier();
345         verifier.verify( "target/output/attributes.html" );
346     }
347 
348     /**
349      * @throws Exception if something goes wrong.
350      */
351     public void verifyMisc()
352         throws Exception
353     {
354         MiscVerifier verifier = new MiscVerifier();
355         verifier.verify( "target/output/misc.html" );
356     }
357 
358     /**
359      * @throws Exception if something goes wrong.
360      */
361     public void verifyDocbookPageExists()
362         throws Exception
363     {
364         File nestedItems = getTestFile( "target/output/docbook.html" );
365         assertNotNull( nestedItems );
366         assertTrue( nestedItems.exists() );
367     }
368 
369     /**
370      * @throws Exception if something goes wrong.
371      */
372     public void verifyApt()
373         throws Exception
374     {
375         AptVerifier verifier = new AptVerifier();
376         verifier.verify( "target/output/apt.html" );
377     }
378 
379     /**
380      * Validate the generated pages.
381      *
382      * @throws Exception if something goes wrong.
383      * @since 1.1.1
384      */
385     public void validatePages() throws Exception
386     {
387         new XhtmlValidatorTest().validateGeneratedPages();
388     }
389 
390     protected static class XhtmlValidatorTest
391         extends AbstractXmlValidator
392     {
393         /**
394          * Validate the generated documents.
395          *
396          * @throws Exception
397          */
398         public void validateGeneratedPages()
399             throws Exception
400         {
401             setUp();
402             testValidateFiles();
403             tearDown();
404         }
405 
406         private static String[] getIncludes()
407         {
408             return new String[] { "**/*.html" };
409         }
410 
411         /** {@inheritDoc} */
412         protected String addNamespaces( String content )
413         {
414             return content;
415         }
416 
417         /** {@inheritDoc} */
418         protected EntityResolver getEntityResolver()
419         {
420             return new XhtmlEntityResolver();
421         }
422 
423         /** {@inheritDoc} */
424         protected Map<String,String> getTestDocuments()
425             throws IOException
426         {
427             Map<String,String> testDocs = new HashMap<String,String>();
428 
429             File dir = new File( getBasedir(), "target/output" );
430 
431             @SuppressWarnings ( "unchecked" )
432             List<String> l = FileUtils.getFileNames( dir, getIncludes()[0], FileUtils.getDefaultExcludesAsString(), true );
433             for ( Iterator<String> it = l.iterator(); it.hasNext(); )
434             {
435                 String file = it.next();
436                 file = StringUtils.replace( file, "\\", "/" );
437 
438                 Reader reader = ReaderFactory.newXmlReader( new File( file ) );
439                 try
440                 {
441                     testDocs.put( file, IOUtil.toString( reader ) );
442                 }
443                 finally
444                 {
445                     IOUtil.close( reader );
446                 }
447             }
448 
449             return testDocs;
450         }
451 
452         /** {@inheritDoc} */
453         @Override
454         protected boolean isFailErrorMessage( String message )
455         {
456             return true;
457         }
458     }
459 }