2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / tests / xmldocdiff.cs
1 using System;
2 using System.Collections;
3 using System.Xml;
4
5 public class Test
6 {
7         public class ComparisonException : Exception
8         {
9                 public ComparisonException (string message)
10                         : base (message)
11                 {
12                 }
13         }
14
15         static bool debug = false;
16         static bool error = false;
17
18         public static void Main (string [] args)
19         {
20                 if (args.Length < 2) {
21                         Console.Error.WriteLine ("Usage: xmldocdiff [reference_output.xml] [actual_output.xml]");
22                         return;
23                 }
24                 if (args.Length > 2 && args [2].EndsWith ("-debug"))
25                         debug = true;
26
27                 try {
28                         Run (args);
29                 } catch (Exception ex) {
30                         Console.WriteLine ("FAIL: " + args [1]);
31                         throw ex;
32                 }
33                 Console.WriteLine ("PASS: " + args [1]);
34         }
35
36         private static void Run (string [] args)
37         {
38                 XmlDocument doc1 = new XmlDocument ();
39                 doc1.Load (args [0]);
40                 XmlDocument doc2 = new XmlDocument ();
41                 doc2.Load (args [1]);
42
43                 XmlNodeList memberList1 = doc1.SelectNodes ("/doc/members/member");
44                 XmlNodeList memberList2 = doc2.SelectNodes ("/doc/members/member");
45
46                 Hashtable namedItems = new Hashtable ();
47
48                 foreach (XmlElement el in memberList1)
49                         namedItems.Add (el.GetAttribute ("name"), el);
50                 foreach (XmlElement el2 in memberList2) {
51                         string name = el2.GetAttribute ("name");
52                         XmlElement el1 = namedItems [name] as XmlElement;
53                         if (el1 == null) {
54                                 Report ("Extraneous element found. Name is '{0}'", name);
55                                 continue;
56                         }
57                         namedItems.Remove (name);
58
59                         CompareNodes (el1, el2);
60
61                 }
62                 foreach (string name in namedItems.Keys)
63                         Report ("Expected comment was not found. Name is {0}, XML is {1}", name, ((XmlElement) namedItems [name]).OuterXml);
64
65                 // finally, check other nodes than members
66                 doc1.SelectSingleNode ("/doc/members").RemoveAll ();
67                 doc2.SelectSingleNode ("/doc/members").RemoveAll ();
68                 string xml1 = doc1.OuterXml.Replace ("\r", "").Trim ();
69                 string xml2 = doc2.OuterXml.Replace ("\r", "").Trim ();
70                 if (xml1 != xml2)
71                         Report (@"Either of doc, assembly, name, members elements  are different.
72 doc1: {0}
73 doc2: {1}", xml1, xml2);
74         }
75
76         private static void CompareNodes (XmlNode n1, XmlNode n2)
77         {
78                 if (n2 == null) {
79                         Report (@"Nodes does not exist:
80 Node1: {0}", n1.OuterXml);
81                         return;
82                 }
83                 if (n1.NodeType != n2.NodeType) {
84                         Report (@"Nodes differ:
85 Node1: {0}
86 Node2: {1}", n1.OuterXml, n2.OuterXml);
87                         return;
88                 }
89                 if (n1.Name != n2.Name) {
90                         Report (@"Node names differ:
91 Node1: {0}
92 Node2: {1}", n1.OuterXml, n2.OuterXml);
93                         return;
94                 }
95                 if (n1 is XmlElement) {
96                         for (int i = 0; i < n1.Attributes.Count; i++)
97                                 CompareNodes (n1.Attributes [i],
98                                         n2.Attributes [i]);
99                         for (int i = 0; i < n1.ChildNodes.Count; i++)
100                                 CompareNodes (n1.ChildNodes [i],
101                                         n2.ChildNodes [i]);
102                 }
103                 if (n1.NodeType != XmlNodeType.Comment && n1.Value != null) {
104                         string v1 = n1.Value.Trim ().Replace ("\r", "");
105                         string v2 = n2.Value.Trim ().Replace ("\r", "");
106                         if (v1 != v2)
107                                 Report (@"Node values differ:
108 Node1: {0}
109 Node2: {1}", v1, v2);
110                 }
111         }
112
113         static void Report (string format, params object [] args)
114         {
115                 error = true;
116                 if (debug)
117                         Console.WriteLine (format, args);
118                 else
119                         throw new ComparisonException (String.Format (format, args));
120         }
121 }
122