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 java.io.File;
26  import java.nio.file.Files;
27  import java.util.HashSet;
28  import java.util.List;
29  import java.util.Properties;
30  import java.util.Set;
31  import java.util.regex.Matcher;
32  import java.util.regex.Pattern;
33  
34  import org.codehaus.modello.AbstractModelloGeneratorTest;
35  import org.codehaus.modello.core.ModelloCore;
36  import org.codehaus.modello.model.Model;
37  import org.codehaus.modello.model.ModelClass;
38  import org.codehaus.modello.model.ModelField;
39  import org.codehaus.modello.model.Version;
40  import org.codehaus.modello.plugins.xml.metadata.XmlFieldMetadata;
41  import org.codehaus.modello.verifier.VerifierException;
42  import org.codehaus.plexus.util.FileUtils;
43  import org.junit.Assert;
44  import org.xmlunit.builder.DiffBuilder;
45  import org.xmlunit.builder.Input;
46  import org.xmlunit.diff.Diff;
47  
48  /**
49   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
50   */
51  public class XdocGeneratorTest extends AbstractModelloGeneratorTest {
52  
53      public XdocGeneratorTest() {
54          super("xdoc");
55      }
56  
57      protected File getOutputDirectory() {
58          return getTestFile("target/generated-site/xdoc");
59      }
60  
61      public void testXdocGenerator() throws Exception {
62          checkMavenXdocGenerator();
63          checkFeaturesXdocGenerator();
64          checkSettingsXdocGenerator();
65      }
66  
67      public void testHtmlToXml() throws Exception {
68          ModelloCore modello = (ModelloCore) lookup(ModelloCore.ROLE);
69  
70          Model model = modello.loadModel(getXmlResourceReader("/html4.mdo"));
71  
72          Properties parameters = getModelloParameters("1.0.0");
73  
74          modello.generate(model, "xdoc", parameters);
75  
76          Diff diff = DiffBuilder.compare(
77                          Input.fromStream(XdocGeneratorTest.class.getResourceAsStream("/html4.expected.xml")))
78                  .withTest(Input.fromFile(new File(getOutputDirectory(), "html4.xml")))
79                  .build();
80  
81          assertFalse(
82                  diff.toString() + "\nGenerated output:\n"
83                          + new String(Files.readAllBytes(new File(getOutputDirectory(), "html4.xml").toPath())),
84                  diff.hasDifferences());
85      }
86  
87      private void checkMavenXdocGenerator() throws Exception {
88          ModelloCore modello = (ModelloCore) lookup(ModelloCore.ROLE);
89  
90          Model model = modello.loadModel(getXmlResourceReader("/maven.mdo"));
91  
92          List<ModelClass> classesList = model.getClasses(new Version("4.0.0"));
93  
94          assertEquals(26, classesList.size());
95  
96          ModelClass clazz = (ModelClass) classesList.get(0);
97  
98          assertEquals("Model", clazz.getName());
99  
100         ModelField extend = clazz.getField("extend", new Version("4.0.0"));
101 
102         assertTrue(extend.hasMetadata(XmlFieldMetadata.ID));
103 
104         XmlFieldMetadata xml = (XmlFieldMetadata) extend.getMetadata(XmlFieldMetadata.ID);
105 
106         assertNotNull(xml);
107 
108         assertTrue(xml.isAttribute());
109 
110         assertEquals("extender", xml.getTagName());
111 
112         ModelField build = clazz.getField("build", new Version("4.0.0"));
113 
114         assertTrue(build.hasMetadata(XmlFieldMetadata.ID));
115 
116         xml = (XmlFieldMetadata) build.getMetadata(XmlFieldMetadata.ID);
117 
118         assertNotNull(xml);
119 
120         assertEquals("builder", xml.getTagName());
121 
122         Properties parameters = getModelloParameters("4.0.0");
123 
124         modello.generate(model, "xdoc", parameters);
125 
126         // addDependency( "modello", "modello-core", "1.0-SNAPSHOT" );
127 
128         // verify( "org.codehaus.modello.generator.xml.cdoc.XdocVerifier", "xdoc" );
129         checkInternalLinks("maven.xml");
130     }
131 
132     public void checkFeaturesXdocGenerator() throws Exception {
133         ModelloCore modello = (ModelloCore) lookup(ModelloCore.ROLE);
134 
135         Model model = modello.loadModel(getXmlResourceReader("/features.mdo"));
136 
137         Properties parameters = getModelloParameters("1.5.0");
138 
139         modello.generate(model, "xdoc", parameters);
140 
141         checkInternalLinks("features.xml");
142 
143         String content = FileUtils.fileRead(new File(getOutputDirectory(), "features.xml"), "UTF-8");
144 
145         assertTrue("Transient fields were erroneously documented", !content.contains("transientString"));
146     }
147 
148     public void checkSettingsXdocGenerator() throws Exception {
149         ModelloCore modello = (ModelloCore) lookup(ModelloCore.ROLE);
150 
151         Model model = modello.loadModel(getXmlResourceReader("/settings.mdo"));
152 
153         Properties parameters = getModelloParameters("1.5.0");
154 
155         modello.generate(model, "xdoc", parameters);
156 
157         checkInternalLinks("settings.xml");
158 
159         String content = FileUtils.fileRead(new File(getOutputDirectory(), "settings.xml"), "UTF-8");
160 
161         assertTrue("Properties field was erroneously documented", !content.contains("&lt;properties/&gt;"));
162     }
163 
164     /**
165      * Checks internal links in the xdoc content: for every 'a href="#xxx"' link, a 'a name="xxx"' must exist (or there
166      * is a problem in the generated content).
167      *
168      * @param xdoc
169      * @throws Exception
170      */
171     private void checkInternalLinks(String filename) throws Exception {
172         String content = FileUtils.fileRead(new File(getOutputDirectory(), filename), "UTF-8");
173 
174         Set<String> hrefs = new HashSet<String>();
175         Pattern p = Pattern.compile("<a href=\"#(class_[^\"]+)\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
176         Matcher m = p.matcher(content);
177         while (m.find()) {
178             hrefs.add(m.group(1));
179         }
180         Assert.assertTrue("should find some '<a href=' links", hrefs.size() > 0);
181 
182         Set<String> names = new HashSet<String>();
183         p = Pattern.compile("<a name=\"(class_[^\"]+)\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
184         m = p.matcher(content);
185         while (m.find()) {
186             names.add(m.group(1));
187         }
188         Assert.assertTrue("should find some '<a name=' anchor definitions", names.size() > 0);
189 
190         hrefs.removeAll(names);
191         if (hrefs.size() > 0) {
192             throw new VerifierException("some internal hrefs in " + filename + " are not defined: " + hrefs);
193         }
194     }
195 }