View Javadoc

1   /*
2    * Copyright 2010-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.testdoc.core.export;
17  
18  import java.util.List;
19  
20  import org.w3c.dom.DOMException;
21  import org.w3c.dom.Document;
22  import org.w3c.dom.Element;
23  import org.w3c.dom.Text;
24  
25  import de.smartics.testdoc.core.doc.ScenarioTestDoc;
26  import de.smartics.testdoc.core.doc.TestMethodDoc;
27  import de.smartics.testdoc.core.doc.Type;
28  import de.smartics.testdoc.core.doc.UnitTestDoc;
29  
30  /**
31   * Creates an XML document instance.
32   *
33   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
34   * @version $Revision:591 $
35   */
36  class XmlDocumentBuilder
37  {
38    // ********************************* Fields *********************************
39  
40    // --- constants ------------------------------------------------------------
41  
42    /**
43     * The URI of the XML schema instance.
44     * <p>
45     * The value of this constant is {@value}.
46     * </p>
47     */
48    private static final String XML_SCHEMA_INSTANCE =
49        "http://www.w3.org/2001/XMLSchema-instance";
50  
51    /**
52     * The URI of the test documentation doctype.
53     * <p>
54     * The value of this constant is {@value}.
55     * </p>
56     */
57    private static final String CODE_URI =
58        "http://www.smartics.de/project/process/implementation/testdoc";
59  
60    // --- members --------------------------------------------------------------
61  
62    /**
63     * The empty document to write to.
64     */
65    private final Document document;
66  
67    // ****************************** Initializer *******************************
68  
69    // ****************************** Constructors ******************************
70  
71    /**
72     * Default constructor.
73     */
74    XmlDocumentBuilder(final Document document)
75    {
76      this.document = document;
77    }
78  
79    // ****************************** Inner Classes *****************************
80  
81    // ********************************* Methods ********************************
82  
83    // --- init -----------------------------------------------------------------
84  
85    // --- get&set --------------------------------------------------------------
86  
87    // --- business -------------------------------------------------------------
88  
89    Document build(final UnitTestDoc testDoc) throws DOMException
90    {
91      final Element root = createDocRoot();
92  
93      addUutElement(testDoc, root);
94      addScenarioElements(testDoc, root);
95  
96      return document;
97    }
98  
99    private Element createDocRoot() throws DOMException
100   {
101     final Element docRoot = document.createElement("testdoc");
102     docRoot.setAttribute("xmlns:xsi", XML_SCHEMA_INSTANCE);
103     docRoot.setAttribute("xmlns", CODE_URI);
104     docRoot.setAttribute("xsi:schemaLocation", CODE_URI + ' ' + CODE_URI);
105     document.appendChild(docRoot);
106     return docRoot;
107   }
108 
109   private void addUutElement(final UnitTestDoc testDoc, final Element root)
110   {
111     final Type uutType = testDoc.getUutType();
112     final Element uut = createTextElement("uut", uutType.toString());
113     root.appendChild(uut);
114   }
115 
116   private void addScenarioElements(final UnitTestDoc testDoc, final Element root)
117   {
118     final List<ScenarioTestDoc> scenarios = testDoc.getScenarios();
119     if (!scenarios.isEmpty())
120     {
121       final Element scenariosElement = document.createElement("scenarios");
122 
123       for (final ScenarioTestDoc scenario : scenarios)
124       {
125         final Element scenarioElement = createScenarioElement(scenario);
126         scenariosElement.appendChild(scenarioElement);
127       }
128       root.appendChild(scenariosElement);
129     }
130   }
131 
132   private Element createScenarioElement(final ScenarioTestDoc scenario)
133   {
134     final Element scenarioElement = document.createElement("scenario");
135 
136     final Element testCase =
137         createTextElement("testCase", scenario.getTestCaseType().toString());
138     scenarioElement.appendChild(testCase);
139     final TestMethodDoc testMethod = scenario.getTestMethod();
140     final Element testName =
141         createTextElement("testName", testMethod.getTestName());
142     scenarioElement.appendChild(testName);
143     final Element sentence =
144         createTextElement("sentence", testMethod.getTestSentence());
145     scenarioElement.appendChild(sentence);
146 
147     appendCategories(scenarioElement, scenario);
148 
149     return scenarioElement;
150   }
151 
152   private void appendCategories(final Element scenarioElement,
153       final ScenarioTestDoc scenario)
154   {
155     final List<String> categories = scenario.getCategories();
156     if (!categories.isEmpty())
157     {
158       final Element categoriesElement = document.createElement("categories");
159 
160       for (final String category : categories)
161       {
162         final Element categoryElement = createTextElement("category", category);
163         categoriesElement.appendChild(categoryElement);
164       }
165 
166       scenarioElement.appendChild(categoriesElement);
167     }
168   }
169 
170   private Element createTextElement(final String elementName,
171       final String content)
172   {
173     final Element element = document.createElement(elementName);
174     final Text text = document.createTextNode(content);
175     element.appendChild(text);
176     return element;
177   }
178 
179   // --- object basics --------------------------------------------------------
180 
181 }