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