View Javadoc

1   package org.codehaus.modello.plugin.xdoc;
2   
3   import java.io.File;
4   import java.util.HashSet;
5   import java.util.List;
6   import java.util.Properties;
7   import java.util.Set;
8   import java.util.regex.Matcher;
9   import java.util.regex.Pattern;
10  
11  /*
12   * Copyright (c) 2004, Codehaus.org
13   *
14   * Permission is hereby granted, free of charge, to any person obtaining a copy of
15   * this software and associated documentation files (the "Software"), to deal in
16   * the Software without restriction, including without limitation the rights to
17   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
18   * of the Software, and to permit persons to whom the Software is furnished to do
19   * so, subject to the following conditions:
20   *
21   * The above copyright notice and this permission notice shall be included in all
22   * copies or substantial portions of the Software.
23   *
24   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30   * SOFTWARE.
31   */
32  
33  import org.codehaus.modello.AbstractModelloGeneratorTest;
34  import org.codehaus.modello.core.ModelloCore;
35  import org.codehaus.modello.model.Model;
36  import org.codehaus.modello.model.ModelClass;
37  import org.codehaus.modello.model.ModelField;
38  import org.codehaus.modello.model.Version;
39  import org.codehaus.modello.plugins.xml.metadata.XmlFieldMetadata;
40  import org.codehaus.modello.verifier.VerifierException;
41  import org.codehaus.plexus.util.FileUtils;
42  import org.xmlunit.builder.DiffBuilder;
43  import org.xmlunit.builder.Input;
44  import org.xmlunit.diff.Diff;
45  
46  import junit.framework.Assert;
47  
48  /**
49   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
50   */
51  public class XdocGeneratorTest
52      extends AbstractModelloGeneratorTest
53  
54  {
55      public XdocGeneratorTest()
56      {
57          super( "xdoc" );
58      }
59  
60      protected File getOutputDirectory()
61      {
62          return getTestFile( "target/generated-site/xdoc" );
63      }
64  
65      public void testXdocGenerator()
66          throws Exception
67      {
68          checkMavenXdocGenerator();
69          checkFeaturesXdocGenerator();
70          checkSettingsXdocGenerator();
71      }
72      
73      public void testHtmlToXml() throws Exception
74      {
75          ModelloCore modello = (ModelloCore) lookup( ModelloCore.ROLE );
76  
77          Model model = modello.loadModel( getXmlResourceReader( "/html4.mdo" ) );
78  
79          Properties parameters = getModelloParameters( "1.0.0" );
80  
81          modello.generate( model, "xdoc", parameters );
82          
83          Diff diff = DiffBuilder.compare( Input.fromStream( XdocGeneratorTest.class.getResourceAsStream( "/html4.expected.xml" ) ) )
84                     .withTest( Input.fromFile( new File( getOutputDirectory(), "html4.xml" ) ) ).build();
85          
86          assertFalse(diff.toString(), diff.hasDifferences());
87      }
88  
89      private void checkMavenXdocGenerator()
90          throws Exception
91      {
92          ModelloCore modello = (ModelloCore) lookup( ModelloCore.ROLE );
93  
94          Model model = modello.loadModel( getXmlResourceReader( "/maven.mdo" ) );
95  
96          List<ModelClass> classesList = model.getClasses( new Version( "4.0.0" ) );
97  
98          assertEquals( 26, classesList.size() );
99  
100         ModelClass clazz = (ModelClass) classesList.get( 0 );
101 
102         assertEquals( "Model", clazz.getName() );
103 
104         ModelField extend = clazz.getField( "extend", new Version( "4.0.0" ) );
105 
106         assertTrue( extend.hasMetadata( XmlFieldMetadata.ID ) );
107 
108         XmlFieldMetadata xml = (XmlFieldMetadata) extend.getMetadata( XmlFieldMetadata.ID );
109 
110         assertNotNull( xml );
111 
112         assertTrue( xml.isAttribute() );
113 
114         assertEquals( "extender", xml.getTagName() );
115 
116         ModelField build = clazz.getField( "build", new Version( "4.0.0" ) );
117 
118         assertTrue( build.hasMetadata( XmlFieldMetadata.ID ) );
119 
120         xml = (XmlFieldMetadata) build.getMetadata( XmlFieldMetadata.ID );
121 
122         assertNotNull( xml );
123 
124         assertEquals( "builder", xml.getTagName() );
125 
126         Properties parameters = getModelloParameters( "4.0.0" );
127 
128         modello.generate( model, "xdoc", parameters );
129 
130         //addDependency( "modello", "modello-core", "1.0-SNAPSHOT" );
131 
132         //verify( "org.codehaus.modello.generator.xml.cdoc.XdocVerifier", "xdoc" );
133         checkInternalLinks( "maven.xml" );
134     }
135 
136     public void checkFeaturesXdocGenerator()
137         throws Exception
138     {
139         ModelloCore modello = (ModelloCore) lookup( ModelloCore.ROLE );
140 
141         Model model = modello.loadModel( getXmlResourceReader( "/features.mdo" ) );
142 
143         Properties parameters = getModelloParameters( "1.5.0" );
144 
145         modello.generate( model, "xdoc", parameters );
146 
147         checkInternalLinks( "features.xml" );
148 
149         String content = FileUtils.fileRead( new File( getOutputDirectory(), "features.xml" ), "UTF-8" );
150 
151         assertTrue( "Transient fields were erroneously documented", !content.contains( "transientString" ) );
152     }
153 
154     public void checkSettingsXdocGenerator()
155         throws Exception
156     {
157         ModelloCore modello = (ModelloCore) lookup( ModelloCore.ROLE );
158 
159         Model model = modello.loadModel( getXmlResourceReader( "/settings.mdo" ) );
160 
161         Properties parameters = getModelloParameters( "1.5.0" );
162 
163         modello.generate( model, "xdoc", parameters );
164 
165         checkInternalLinks( "settings.xml" );
166 
167         String content = FileUtils.fileRead( new File( getOutputDirectory(), "settings.xml" ), "UTF-8" );
168 
169         assertTrue( "Properties field was erroneously documented", !content.contains("&lt;properties/&gt;"));
170     }
171 
172     /**
173      * Checks internal links in the xdoc content: for every 'a href="#xxx"' link, a 'a name="xxx"' must exist (or there
174      * is a problem in the generated content).
175      *
176      * @param xdoc
177      * @throws Exception
178      */
179     private void checkInternalLinks( String filename )
180         throws Exception
181     {
182         String content = FileUtils.fileRead( new File( getOutputDirectory(), filename ), "UTF-8" );
183 
184         Set<String> hrefs = new HashSet<String>();
185         Pattern p = Pattern.compile( "<a href=\"#(class_[^\"]+)\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE );
186         Matcher m = p.matcher( content );
187         while ( m.find() )
188         {
189             hrefs.add( m.group( 1 ) );
190         }
191         Assert.assertTrue( "should find some '<a href=' links", hrefs.size() > 0 );
192 
193         Set<String> names = new HashSet<String>();
194         p = Pattern.compile( "<a name=\"(class_[^\"]+)\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE );
195         m = p.matcher( content );
196         while ( m.find() )
197         {
198             names.add( m.group( 1 ) );
199         }
200         Assert.assertTrue( "should find some '<a name=' anchor definitions", names.size() > 0 );
201 
202         hrefs.removeAll( names );
203         if ( hrefs.size() > 0 )
204         {
205             throw new VerifierException( "some internal hrefs in " + filename + " are not defined: " + hrefs );
206         }
207     }
208 }