View Javadoc
1   package org.apache.maven.doxia.module.xhtml;
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.FileFilter;
24  import java.util.Iterator;
25  import java.util.regex.Pattern;
26  
27  import org.apache.maven.doxia.parser.AbstractParserTest;
28  import org.apache.maven.doxia.parser.Parser;
29  import org.apache.maven.doxia.sink.impl.SinkEventElement;
30  import org.apache.maven.doxia.sink.impl.SinkEventTestingSink;
31  
32  /**
33   * @author <a href="mailto:lars@trieloff.net">Lars Trieloff</a>
34   */
35  public class XhtmlParserTest
36      extends AbstractParserTest
37  {
38      private XhtmlParser parser;
39  
40      /** {@inheritDoc} */
41      protected void setUp()
42          throws Exception
43      {
44          super.setUp();
45  
46          parser = (XhtmlParser) lookup( Parser.class, "xhtml" );
47  
48          // AbstractXmlParser.CachedFileEntityResolver downloads DTD/XSD files in ${java.io.tmpdir}
49          // Be sure to delete them
50          String tmpDir = System.getProperty( "java.io.tmpdir" );
51  
52          // Using FileFilter, because is it is much faster then FileUtils.listFiles 
53          File[] tmpFiles = new File( tmpDir ).listFiles( new FileFilter()
54          {
55              Pattern xsdPatterns = Pattern.compile( "(xhtml-lat1.ent|xhtml1-transitional.dtd|xhtml-special.ent|xhtml-symbol.ent)" );
56              
57              @Override
58              public boolean accept( File pathname )
59              {
60                  return xsdPatterns.matcher( pathname.getName() ).matches(); 
61              }
62          } );
63          
64          for ( File tmpFile : tmpFiles )
65          {
66              tmpFile.delete();
67          }
68  
69      }
70  
71      /** {@inheritDoc} */
72      protected Parser createParser()
73      {
74          return parser;
75      }
76  
77      /** {@inheritDoc} */
78      protected String outputExtension()
79      {
80          return "xhtml";
81      }
82  
83      /** @throws Exception  */
84      public void testDocumentBodyEventsList()
85          throws Exception
86      {
87          String text = "<html><body></body></html>";
88  
89          SinkEventTestingSink sink = new SinkEventTestingSink();
90  
91          ( (XhtmlParser) createParser() ).parse( text, sink );
92  
93          Iterator<SinkEventElement> it = sink.getEventList().iterator();
94  
95          assertEquals( "body", it.next().getName() );
96          assertEquals( "body_", it.next().getName() );
97          assertFalse( it.hasNext() );
98      }
99  
100     /** @throws Exception  */
101     public void testHeadEventsList()
102         throws Exception
103     {
104         String text = "<head><title>Title</title><meta name=\"author\" content=\"Author\" />"
105                 + "<meta name=\"date\" content=\"Date\" /><meta name=\"security\" content=\"low\"/></head>";
106 
107         SinkEventTestingSink sink = new SinkEventTestingSink();
108 
109         ( (XhtmlParser) createParser() ).parse( text, sink );
110 
111         Iterator<SinkEventElement> it = sink.getEventList().iterator();
112 
113         assertEquals( "head", it.next().getName() );
114         assertEquals( "title", it.next().getName() );
115         assertEquals( "text", it.next().getName() );
116         assertEquals( "title_", it.next().getName() );
117         assertEquals( "author", it.next().getName() );
118         assertEquals( "text", it.next().getName() );
119         assertEquals( "author_", it.next().getName() );
120         assertEquals( "date", it.next().getName() );
121         assertEquals( "text", it.next().getName() );
122         assertEquals( "date_", it.next().getName() );
123         assertEquals( "unknown", it.next().getName() );
124         assertEquals( "head_", it.next().getName() );
125         assertFalse( it.hasNext() );
126     }
127 
128     /** @throws Exception  */
129     public void testPreEventsList()
130         throws Exception
131     {
132         String text = "<pre></pre>";
133 
134         SinkEventTestingSink sink = new SinkEventTestingSink();
135 
136         ( (XhtmlParser) createParser() ).parse( text, sink );
137 
138         Iterator<SinkEventElement> it = sink.getEventList().iterator();
139 
140         assertEquals( "verbatim", it.next().getName() );
141         assertEquals( "verbatim_", it.next().getName() );
142         assertFalse( it.hasNext() );
143     }
144 
145     /**
146      * Test unknown tags.
147      *
148      * @throws java.lang.Exception if any.
149      */
150     public void testUnknown()
151         throws Exception
152     {
153         String text = "<applet><param name=\"name\" value=\"value\"/><unknown/></applet>";
154 
155         SinkEventTestingSink sink = new SinkEventTestingSink();
156 
157         ( (XhtmlParser) createParser() ).parse( text, sink );
158 
159         Iterator<SinkEventElement> it = sink.getEventList().iterator();
160         assertEquals( "unknown", it.next().getName() );
161         assertEquals( "unknown", it.next().getName() );
162         assertEquals( "unknown", it.next().getName() );
163         assertEquals( "unknown", it.next().getName() );
164         assertFalse( it.hasNext() );
165     }
166 
167     /** @throws Exception  */
168     public void testTocMacro()
169         throws Exception
170     {
171         String text = "<html><body>" +
172                 "<!-- MACRO{toc|fromDepth=1|toDepth=2} -->" +
173                 "<h1>heading 1</h1><h2>heading 2</h2><h3>heading 3</h3>" +
174                 "</body></html>";
175 
176         SinkEventTestingSink sink = new SinkEventTestingSink();
177 
178         ( (XhtmlParser) createParser() ).parse( text, sink );
179 
180         Iterator<SinkEventElement> it = sink.getEventList().iterator();
181 
182         assertEquals( "body", it.next().getName() );
183         assertEquals( "list", it.next().getName() );
184         assertEquals( "listItem", it.next().getName() );
185         assertEquals( "link", it.next().getName() );
186         assertEquals( "text", it.next().getName() );
187         assertEquals( "link_", it.next().getName() );
188         assertEquals( "list", it.next().getName() );
189         assertEquals( "listItem", it.next().getName() );
190         assertEquals( "link", it.next().getName() );
191         assertEquals( "text", it.next().getName() );
192         assertEquals( "link_", it.next().getName() );
193         assertEquals( "listItem_", it.next().getName() );
194         assertEquals( "list_", it.next().getName() );
195         assertEquals( "listItem_", it.next().getName() );
196         assertEquals( "list_", it.next().getName() );
197     }
198 }