KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > adl > conform > Test


1 /***
2  * Fractal ADL Parser
3  * Copyright (C) 2002-2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.adl.conform;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.io.Writer JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33
34 import org.objectweb.fractal.adl.Definition;
35 import org.objectweb.fractal.adl.Node;
36 import org.objectweb.fractal.adl.xml.XMLWriter;
37
38 import junit.framework.TestCase;
39
40 public class Test extends TestCase {
41
42   public Test (final String JavaDoc name) {
43     super(name);
44   }
45
46   public void print (final Node n) {
47     try {
48       Writer JavaDoc w = new PrintWriter JavaDoc(System.err, true);
49       new XMLWriter(w).write(n);
50       w.flush();
51     } catch (Exception JavaDoc e) {
52     }
53   }
54   
55   public void compare (final Definition d, final Definition e) {
56     compare((Node)d, (Node)e);
57   }
58   
59   public void compare (final Node n, final Node m) {
60     if (!n.astGetType().equals(m.astGetType())) {
61       fail();
62     }
63     
64     Map JavaDoc nAttrs = n.astGetAttributes();
65     Map JavaDoc mAttrs = m.astGetAttributes();
66     if (n.astGetType().equals("definition")) {
67       mAttrs.put("name", nAttrs.get("name"));
68     }
69     Iterator JavaDoc it = nAttrs.keySet().iterator();
70     while (it.hasNext()) {
71       Object JavaDoc key = it.next();
72       if (nAttrs.get(key) == null) {
73         it.remove();
74       }
75     }
76     it = mAttrs.keySet().iterator();
77     while (it.hasNext()) {
78       Object JavaDoc key = it.next();
79       if (mAttrs.get(key) == null) {
80         it.remove();
81       }
82     }
83     if (!nAttrs.equals(mAttrs)) {
84       fail();
85     }
86     
87     String JavaDoc[] nNames = n.astGetNodeTypes();
88     String JavaDoc[] mNames = m.astGetNodeTypes();
89     Set JavaDoc names = new HashSet JavaDoc();
90     names.addAll(Arrays.asList(nNames));
91     names.addAll(Arrays.asList(mNames));
92     Iterator JavaDoc i = names.iterator();
93     while (i.hasNext()) {
94       String JavaDoc name = (String JavaDoc)i.next();
95       Node[] nNodes = n.astGetNodes(name);
96       if (nNodes == null) {
97         nNodes = new Node[0];
98       }
99       Node[] mNodes = m.astGetNodes(name);
100       if (mNodes == null) {
101         mNodes = new Node[0];
102       }
103       if (nNodes.length == 0 && mNodes.length == 1 && mNodes[0] == null) {
104         continue;
105       }
106       if (mNodes.length == 0 && nNodes.length == 1 && nNodes[0] == null) {
107         continue;
108       }
109       if (nNodes.length != mNodes.length) {
110         fail();
111       }
112       for (int j = 0; j < nNodes.length; ++j) {
113         boolean ok = false;
114         for (int k = 0; k < mNodes.length; ++k) {
115           if (nNodes[j] == null) {
116             if (mNodes[k] == null) {
117               ok = true;
118               break;
119             }
120           } else if (mNodes[k] != null) {
121             try {
122               compare(nNodes[j], mNodes[k]);
123               ok = true;
124               break;
125             } catch (Throwable JavaDoc t) {
126             }
127           }
128         }
129         if (!ok) {
130           fail();
131         }
132       }
133       for (int k = 0; k < mNodes.length; ++k) {
134         boolean ok = false;
135         for (int j = 0; j < nNodes.length; ++j) {
136           if (mNodes[k] == null) {
137             if (nNodes[j] == null) {
138               ok = true;
139               break;
140             }
141           } else if (nNodes[j] != null) {
142             try {
143               compare(nNodes[j], mNodes[k]);
144               ok = true;
145               break;
146             } catch (Throwable JavaDoc t) {
147             }
148           }
149         }
150         if (!ok) {
151           fail();
152         }
153       }
154     }
155   }
156 }
157
Popular Tags