KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > j2ee > lib > ContentComparator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * ContentComparator.java
22  *
23  * Created on December 14, 2005, 4:02 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.test.j2ee.lib;
30
31 import java.io.File JavaDoc;
32 import java.io.FileInputStream JavaDoc;
33 import java.io.FileNotFoundException JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.LinkedList JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.jar.Attributes JavaDoc;
39 import java.util.jar.Manifest JavaDoc;
40 import javax.xml.parsers.DocumentBuilder JavaDoc;
41 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
42 import javax.xml.parsers.ParserConfigurationException JavaDoc;
43 import org.w3c.dom.Document JavaDoc;
44 import org.w3c.dom.NamedNodeMap JavaDoc;
45 import org.w3c.dom.Node JavaDoc;
46 import org.w3c.dom.NodeList JavaDoc;
47 import org.xml.sax.SAXException JavaDoc;
48
49 /**
50  *
51  * @author jungi
52  */

53 public final class ContentComparator {
54     
55     /** Creates a new instance of ContentComparator */
56     private ContentComparator() {
57     }
58     
59     /**
60      *
61      * Compares the content of two xml files. Ignores whitespaces.
62      *
63      *@param f1 ususally goldenfile
64      *@param f2 other file which we want to compare against goldenfile (or any other file)
65      *@return true iff both files have the same content except of whitespaces
66      */

67     public static boolean equalsXML(File JavaDoc f1, File JavaDoc f2) {
68         try {
69             DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
70             DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
71             Document JavaDoc d1 = db.parse(f1);
72             Document JavaDoc d2 = db.parse(f2);
73             return compare(d1.getDocumentElement(), d2.getDocumentElement());
74         } catch (ParserConfigurationException JavaDoc e) {
75             System.err.println("Exception from test - comparing XML files");
76             e.printStackTrace(System.err);
77         } catch (SAXException JavaDoc e) {
78             System.err.println("Exception from test - comparing XML files");
79             e.printStackTrace(System.err);
80         } catch (IOException JavaDoc e) {
81             System.err.println("Exception from test - comparing XML files");
82             e.printStackTrace(System.err);
83         }
84         return false;
85     }
86     
87     /**
88      * Compares two manifest files. First check is for number of attributes,
89      * next one is comparing name-value pairs.
90      *
91      *@param mf1, mf2 manifests to compare
92      *@param ignoredEntries array of manifest entries to ignore
93      *@return true if files contains the same entries/values in manifest
94      */

95     public static boolean equalsManifest(File JavaDoc mf1, File JavaDoc mf2, String JavaDoc[] ignoredEntries) {
96         if (ignoredEntries == null) {
97             ignoredEntries = new String JavaDoc[] {};
98         }
99         try {
100             Manifest JavaDoc m1 = new Manifest JavaDoc(new FileInputStream JavaDoc(mf1));
101             Manifest JavaDoc m2 = new Manifest JavaDoc(new FileInputStream JavaDoc(mf2));
102             Attributes JavaDoc a1 = m1.getMainAttributes();
103             Attributes JavaDoc a2 = m2.getMainAttributes();
104             if (a1.size() != a2.size()) {
105                 return false;
106             }
107             for (Iterator JavaDoc i = a1.keySet().iterator(); i.hasNext();) {
108                 Attributes.Name JavaDoc a = (Attributes.Name JavaDoc) i.next();
109                 boolean b = true;
110                 for (int j = 0; j < ignoredEntries.length; j++) {
111                     if (a.toString().equals(ignoredEntries[j])) {
112                         a2.remove(a);
113                         b = false;
114                         break;
115                     }
116                 }
117                 if (b && (a1.get(a).equals(a2.get(a)))) {
118                     a2.remove(a);
119                 }
120             }
121             return a2.isEmpty();
122         } catch (FileNotFoundException JavaDoc fnfe) {
123             System.err.println("Exception from test - comparing manifests");
124             fnfe.printStackTrace(System.err);
125         } catch (IOException JavaDoc ioe) {
126             System.err.println("Exception from test - comparing manifests");
127             ioe.printStackTrace(System.err);
128         }
129         return false;
130     }
131
132     //gf
133
//bad
134
private static boolean compare(Node JavaDoc n1, Node JavaDoc n2) {
135         List JavaDoc l1 = new LinkedList JavaDoc();
136         List JavaDoc l2 = new LinkedList JavaDoc();
137         l1.add(n1);
138         l2.add(n2);
139         while (!l1.isEmpty() && !l2.isEmpty()) {
140             Node JavaDoc m1 = (Node JavaDoc) l1.remove(0);
141             Node JavaDoc m2 = (Node JavaDoc) l2.remove(0);
142             //check basic things - node name, value, attributes - iff they're OK, we can continue
143
if (sameNode(m1, m2)) {
144                 //now compare children
145
NodeList JavaDoc nl = m1.getChildNodes();
146                 for (int i = 0; i < nl.getLength(); i++) {
147                     Node JavaDoc e = nl.item(i);
148                     if (e.getNodeType() == Node.TEXT_NODE) {
149                         //ignore empty places
150
if (e.getNodeValue().trim().equals("")) {
151                             continue;
152                         }
153                     }
154                     l1.add(nl.item(i));
155                 }
156                 nl = m2.getChildNodes();
157                 for (int i = 0; i < nl.getLength(); i++) {
158                     Node JavaDoc e = nl.item(i);
159                     if (e.getNodeType() == Node.TEXT_NODE) {
160                         //ignore empty places
161
if (e.getNodeValue().trim().equals("")) {
162                             continue;
163                         }
164                     }
165                     l2.add(nl.item(i));
166                 }
167             } else {
168                 //nodes are not equals - print some info
169
System.err.println("================================================");
170                 System.err.println("m1: " + m1.getNodeName() + "; \'" + m1.getNodeValue() + "\'");
171                 System.err.println("m2: " + m2.getNodeName() + "; \'" + m2.getNodeValue() + "\'");
172                 System.err.println("================================================");
173                 return false;
174             }
175         }
176         return true;
177     }
178     
179     //attrs, name, value
180
private static boolean sameNode(Node JavaDoc n1, Node JavaDoc n2) {
181         //check node name
182
if (!n1.getNodeName().equals(n2.getNodeName())) {
183             System.err.println("================================================");
184             System.err.println("Expected node: " + n1.getNodeName() + ", got: " + n2.getNodeName());
185             System.err.println("================================================");
186             return false;
187         }
188         //check node value
189
if (!((n1.getNodeValue() != null)
190                 ? n1.getNodeValue().equals(n2.getNodeValue())
191                 : (n2.getNodeValue() == null))) {
192             System.err.println("================================================");
193             System.err.println("Expected node value: " + n1.getNodeValue() + ", got: " + n2.getNodeValue());
194             System.err.println("================================================");
195             return false;
196         }
197         //check node attributes
198
NamedNodeMap JavaDoc nnm1 = n1.getAttributes();
199         NamedNodeMap JavaDoc nnm2 = n2.getAttributes();
200         if ((nnm1 == null && nnm2 != null)
201                 || (nnm1 != null && nnm2 == null)) {
202             return false;
203         }
204         if (nnm1 == null && nnm2 == null) {
205             return true;
206         }
207         for (int i = 0; i < nnm1.getLength(); i++) {
208             Node JavaDoc x = nnm1.item(i);
209             Node JavaDoc y = nnm2.item(i);
210             if (!(x.getNodeName().equals(y.getNodeName())
211                     && x.getNodeValue().equals(y.getNodeValue()))) {
212                 //nodes are not equals - print some info
213
System.err.println("================================================");
214                 System.err.println("Expected attribute: " + x.getNodeName() + "=\'" + x.getNodeValue() + "\',"
215                         + " got: " + y.getNodeName() + "=\'" + y.getNodeValue() + "\'");
216                 System.err.println("================================================");
217                 return false;
218             }
219         }
220         return true;
221     }
222     
223 }
224
Popular Tags