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.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   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
47   * @author <a href="mailto:evenisse@codehaus.org>Emmanuel Venisse</a>
48   * @version $Id: DefaultSiteRendererTest.java 779968 2009-05-29 13:40:51Z vsiveton $
49   */
50  public class DefaultSiteRendererTest
51      extends PlexusTestCase
52  {
53      /**
54       * All output produced by this test will go here.
55       */
56      private static final String OUTPUT = "target/output";
57  
58      /**
59       * The renderer used to produce output.
60       */
61      private Renderer renderer;
62  
63      /**
64       * @throws java.lang.Exception if something goes wrong.
65       * @see org.codehaus.plexus.PlexusTestCase#setUp()
66       */
67      protected void setUp()
68          throws Exception
69      {
70          super.setUp();
71  
72          renderer = (Renderer) lookup( Renderer.ROLE );
73  
74          // copy the default-site.vm
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          // Safety
90          FileUtils.deleteDirectory( getTestFile( OUTPUT ) );
91      }
92  
93      /**
94       * @throws java.lang.Exception if something goes wrong.
95       * @see org.codehaus.plexus.PlexusTestCase#tearDown()
96       */
97      protected void tearDown()
98          throws Exception
99      {
100         release( renderer );
101         super.tearDown();
102     }
103 
104     /**
105      * @throws Exception if something goes wrong.
106      */
107     public void testRender()
108         throws Exception
109     {
110         // ----------------------------------------------------------------------
111         // Render the site
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         // Verify specific pages
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         // Validate the rendering pages
146         // ----------------------------------------------------------------------
147         validatePages();
148     }
149 
150     /**
151      * @throws Exception if something goes wrong.
152      */
153     public void verifyHeadPage()
154         throws Exception
155     {
156         new HeadVerifier().verify( "target/output/head.html" );
157     }
158 
159     /**
160      * @throws Exception if something goes wrong.
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      * @throws Exception if something goes wrong.
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      * @throws Exception if something goes wrong.
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      * @throws Exception if something goes wrong.
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      * @throws Exception if something goes wrong.
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      * @throws Exception if something goes wrong.
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      * @throws Exception if something goes wrong.
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      * @throws Exception if something goes wrong.
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      * @throws Exception if something goes wrong.
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      * @throws Exception if something goes wrong.
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      * @throws Exception if something goes wrong.
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      * Validate the generated pages.
286      *
287      * @throws Exception if something goes wrong.
288      * @since 1.1.1
289      */
290     public void validatePages() throws Exception
291     {
292         // Need to refactor...
293         XhtmlValidatorTest validator = new XhtmlValidatorTest();
294         validator.setUp();
295         validator.testValidateFiles();
296     }
297 
298     protected static class XhtmlValidatorTest
299         extends AbstractXmlValidatorTest
300     {
301         /** {@inheritDoc} */
302         protected void setUp()
303             throws Exception
304         {
305             super.setUp();
306         }
307 
308         /** {@inheritDoc} */
309         protected void tearDown()
310             throws Exception
311         {
312             super.tearDown();
313         }
314 
315         /** {@inheritDoc} */
316         protected String[] getIncludes()
317         {
318             return new String[] { "**/*.html" };
319         }
320 
321         /** {@inheritDoc} */
322         protected String addNamespaces( String content )
323         {
324             return content;
325         }
326 
327         /** {@inheritDoc} */
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         /** {@inheritDoc} */
356         protected boolean isFailErrorMessage( String message )
357         {
358             return true;
359         }
360     }
361 }