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.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.maven.doxia.parser.AbstractParserTest;
27  import org.apache.maven.doxia.parser.Parser;
28  import org.apache.maven.doxia.sink.SinkEventElement;
29  import org.apache.maven.doxia.sink.SinkEventTestingSink;
30  import org.codehaus.plexus.util.FileUtils;
31  
32  /**
33   * @author <a href="mailto:lars@trieloff.net">Lars Trieloff</a>
34   * @version $Id: XhtmlParserTest.java 1196253 2011-11-01 20:13:24Z hboutemy $
35   */
36  public class XhtmlParserTest
37      extends AbstractParserTest
38  {
39      private XhtmlParser parser;
40  
41      /** {@inheritDoc} */
42      protected void setUp()
43          throws Exception
44      {
45          super.setUp();
46  
47          parser = (XhtmlParser) lookup( Parser.ROLE, "xhtml" );
48  
49          // AbstractXmlParser.CachedFileEntityResolver downloads DTD/XSD files in ${java.io.tmpdir}
50          // Be sure to delete them
51          String tmpDir = System.getProperty( "java.io.tmpdir" );
52          String excludes = "xhtml-lat1.ent, xhtml1-transitional.dtd, xhtml-special.ent, xhtml-symbol.ent";
53          @SuppressWarnings( "unchecked" )
54          List<String> tmpFiles = FileUtils.getFileNames( new File( tmpDir ), excludes, null, true );
55          for ( String filename : tmpFiles )
56          {
57              File tmpFile = new File( filename );
58              tmpFile.delete();
59          }
60      }
61  
62      /** {@inheritDoc} */
63      protected Parser createParser()
64      {
65          return parser;
66      }
67  
68      /** {@inheritDoc} */
69      protected String outputExtension()
70      {
71          return "xhtml";
72      }
73  
74      /** @throws Exception  */
75      public void testDocumentBodyEventsList()
76          throws Exception
77      {
78          String text = "<html><body></body></html>";
79  
80          SinkEventTestingSink sink = new SinkEventTestingSink();
81  
82          ( (XhtmlParser) createParser() ).parse( text, sink );
83  
84          Iterator<SinkEventElement> it = sink.getEventList().iterator();
85  
86          assertEquals( "body", it.next().getName() );
87          assertEquals( "body_", it.next().getName() );
88          assertFalse( it.hasNext() );
89      }
90  
91      /** @throws Exception  */
92      public void testHeadEventsList()
93          throws Exception
94      {
95          String text = "<head><title>Title</title><meta name=\"author\" content=\"Author\" />"
96                  + "<meta name=\"date\" content=\"Date\" /><meta name=\"security\" content=\"low\"/></head>";
97  
98          SinkEventTestingSink sink = new SinkEventTestingSink();
99  
100         ( (XhtmlParser) createParser() ).parse( text, sink );
101 
102         Iterator<SinkEventElement> it = sink.getEventList().iterator();
103 
104         assertEquals( "head", it.next().getName() );
105         assertEquals( "title", it.next().getName() );
106         assertEquals( "text", it.next().getName() );
107         assertEquals( "title_", it.next().getName() );
108         assertEquals( "author", it.next().getName() );
109         assertEquals( "text", it.next().getName() );
110         assertEquals( "author_", it.next().getName() );
111         assertEquals( "date", it.next().getName() );
112         assertEquals( "text", it.next().getName() );
113         assertEquals( "date_", it.next().getName() );
114         assertEquals( "unknown", it.next().getName() );
115         assertEquals( "head_", it.next().getName() );
116         assertFalse( it.hasNext() );
117     }
118 
119     /** @throws Exception  */
120     public void testPreEventsList()
121         throws Exception
122     {
123         String text = "<pre></pre>";
124 
125         SinkEventTestingSink sink = new SinkEventTestingSink();
126 
127         ( (XhtmlParser) createParser() ).parse( text, sink );
128 
129         Iterator<SinkEventElement> it = sink.getEventList().iterator();
130 
131         assertEquals( "verbatim", it.next().getName() );
132         assertEquals( "verbatim_", it.next().getName() );
133         assertFalse( it.hasNext() );
134     }
135 
136     /**
137      * Test unknown tags.
138      *
139      * @throws java.lang.Exception if any.
140      */
141     public void testUnknown()
142         throws Exception
143     {
144         String text = "<applet><param name=\"name\" value=\"value\"/><unknown/></applet>";
145 
146         SinkEventTestingSink sink = new SinkEventTestingSink();
147 
148         ( (XhtmlParser) createParser() ).parse( text, sink );
149 
150         Iterator<SinkEventElement> it = sink.getEventList().iterator();
151         assertEquals( "unknown", it.next().getName() );
152         assertEquals( "unknown", it.next().getName() );
153         assertEquals( "unknown", it.next().getName() );
154         assertEquals( "unknown", it.next().getName() );
155         assertFalse( it.hasNext() );
156     }
157 }