View Javadoc

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