1   ////////////////////////////////////////////////////////////////////////////////
2   //
3   //                  ObjectLab is sponsoring QALab
4   // 
5   // Based in London, we are world leaders in the design and development 
6   // of bespoke applications for the Securities Financing markets.
7   // 
8   // <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
9   //           ___  _     _           _   _          _
10  //          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
11  //         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
12  //         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
13  //          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
14  //                   |__/
15  //
16  //                   http://www.ObjectLab.co.uk
17  // ---------------------------------------------------------------------------
18  //
19  //QALab is released under the GNU General Public License.
20  //
21  //QALab: Collects QA Statistics from your build over time.
22  //2005+, ObjectLab Ltd
23  //
24  //This library is free software; you can redistribute it and/or
25  //modify it under the terms of the GNU General Public
26  //License as published by the Free Software Foundation; either
27  //version 2.1 of the License, or (at your option) any later version.
28  //
29  //This library is distributed in the hope that it will be useful,
30  //but WITHOUT ANY WARRANTY; without even the implied warranty of
31  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
32  //General Public License for more details.
33  //
34  //You should have received a copy of the GNU General Public
35  //License along with this library; if not, write to the Free Software
36  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
37  //
38  ////////////////////////////////////////////////////////////////////////////////
39  package net.objectlab.qalab.parser;
40  
41  import net.objectlab.qalab.AbstractTestCase;
42  import net.objectlab.qalab.util.QALabTags;
43  
44  import org.apache.xml.serialize.OutputFormat;
45  import org.apache.xml.serialize.XMLSerializer;
46  import org.w3c.dom.Document;
47  import org.w3c.dom.NamedNodeMap;
48  import org.w3c.dom.Node;
49  import org.w3c.dom.NodeList;
50  
51  import java.io.IOException;
52  import java.io.StringWriter;
53  
54  public abstract class BaseMerge extends AbstractTestCase {
55      /**
56       * Create the test case
57       *
58       * @param testName name of the test case
59       */
60      public BaseMerge(String testName) {
61          super(testName);
62      }
63  
64      protected void checkDTDPresent(Document document) {
65          try {
66              final String doc = streamToString(document);
67  
68              assertEquals("1 <!DOCTYPE qalab", 1,
69                  countInstances(doc, "<!DOCTYPE qalab "));
70              assertEquals("5 <!ELEMENT", 5, countInstances(doc, "<!ELEMENT "));
71              assertEquals("10 <!ATTLIST", 10, countInstances(doc, "<!ATTLIST "));
72          }
73          catch (IOException e) {
74              fail(e.toString());
75          }
76      }
77  
78      private int countInstances(String str, String token) {
79          int count = 0;
80          int index = -1;
81  
82          do {
83              index = str.indexOf(token, index + 1);
84              count++;
85          }
86          while (index > 0);
87  
88          return count - 1;
89      }
90  
91      protected void checkSummary(Document outputDocument,
92          int summaryEntriesCount, int errorCount, int fileCount) {
93          NodeList list = outputDocument.getElementsByTagName(QALabTags.SUMMARY_RESULT_TAG);
94  
95          assertEquals("number of summary results", summaryEntriesCount,
96              list.getLength());
97  
98          for (int i = 0; i < list.getLength(); i++) {
99              Node childNode = list.item(i);
100 
101             String value = getAttributeValue(childNode,
102                     QALabTags.STATVALUE_ATTRIBUTE);
103 
104             assertEquals("Number of errors", errorCount, Integer.parseInt(value));
105 
106             String files = getAttributeValue(childNode,
107                     QALabTags.FILECOUNT_ATTRIBUTE);
108 
109             assertEquals("Number of files", fileCount, Integer.parseInt(files));
110         }
111     }
112 
113     protected void checkSummary(Document outputDocument,
114         int summaryEntriesCount, int[] errorCount, int[] fileCount,
115         String[] types) {
116         NodeList list = outputDocument.getElementsByTagName(QALabTags.SUMMARY_RESULT_TAG);
117 
118         assertEquals("number of summary results", summaryEntriesCount,
119             list.getLength());
120 
121         for (int i = 0; i < list.getLength(); i++) {
122             Node childNode = list.item(i);
123 
124             String value = getAttributeValue(childNode,
125                     QALabTags.STATVALUE_ATTRIBUTE);
126 
127             assertEquals("Number of errors", errorCount[i],
128                 Integer.parseInt(value));
129 
130             String files = getAttributeValue(childNode,
131                     QALabTags.FILECOUNT_ATTRIBUTE);
132 
133             assertEquals("Number of files", fileCount[i],
134                 Integer.parseInt(files));
135 
136             String type = getAttributeValue(childNode, QALabTags.TYPE_ATTRIBUTE);
137 
138             assertEquals("Type of Stats", types[i], type);
139         }
140     }
141 
142     protected void checkFileCount(Document outputDocument, int fileCount,
143         int expectedTotalError) {
144         NodeList list = outputDocument.getElementsByTagName(QALabTags.FILE_TAG);
145 
146         assertEquals("number of files", fileCount, list.getLength());
147 
148         NodeList resultList = outputDocument.getElementsByTagName(QALabTags.RESULT_TAG);
149         int totalError = 0;
150 
151         for (int i = 0; i < resultList.getLength(); i++) {
152             Node childNode = resultList.item(i);
153 
154             String value = getAttributeValue(childNode,
155                     QALabTags.STATVALUE_ATTRIBUTE);
156 
157             totalError += Integer.parseInt(value);
158         }
159 
160         assertEquals("Expected number of errors", expectedTotalError, totalError);
161     }
162 
163     private String getAttributeValue(Node childNode, String attributeName) {
164         NamedNodeMap map = childNode.getAttributes();
165         Node node = map.getNamedItem(attributeName);
166 
167         return node.getNodeValue();
168     }
169 
170     /**
171      * This method takes a DOM Element and returns a String.
172      * @param elem the DOM Document to be streamed
173      * @return the String representation of the DOM element.
174      */
175     protected static String streamToString(final Document elem)
176         throws IOException {
177         final OutputFormat format = new OutputFormat(elem, "UTF-8", true); //Serialize DOM
178         final StringWriter stringOut = new StringWriter(); //Writer will be a String
179         final XMLSerializer serial = new XMLSerializer(stringOut, format);
180 
181         serial.asDOMSerializer(); // As a DOM Serializer
182         serial.serialize(elem);
183 
184         return stringOut.toString();
185     }
186 }
187 /*
188  *                   ObjectLab is sponsoring QALab
189  * 
190  * Based in London, we are world leaders in the design and development 
191  * of bespoke applications for the securities financing markets.
192  * 
193  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
194  *           ___  _     _           _   _          _
195  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
196  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
197  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
198  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
199  *                   |__/
200  *
201  *                     www.ObjectLab.co.uk
202  */