1 23 24 package org.objectweb.fractal.adl.conform; 25 26 import java.io.PrintWriter ; 27 import java.io.Writer ; 28 import java.util.Arrays ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 import java.util.Set ; 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 name) { 43 super(name); 44 } 45 46 public void print (final Node n) { 47 try { 48 Writer w = new PrintWriter (System.err, true); 49 new XMLWriter(w).write(n); 50 w.flush(); 51 } catch (Exception 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 nAttrs = n.astGetAttributes(); 65 Map mAttrs = m.astGetAttributes(); 66 if (n.astGetType().equals("definition")) { 67 mAttrs.put("name", nAttrs.get("name")); 68 } 69 Iterator it = nAttrs.keySet().iterator(); 70 while (it.hasNext()) { 71 Object 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 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 [] nNames = n.astGetNodeTypes(); 88 String [] mNames = m.astGetNodeTypes(); 89 Set names = new HashSet (); 90 names.addAll(Arrays.asList(nNames)); 91 names.addAll(Arrays.asList(mNames)); 92 Iterator i = names.iterator(); 93 while (i.hasNext()) { 94 String name = (String )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 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 t) { 147 } 148 } 149 } 150 if (!ok) { 151 fail(); 152 } 153 } 154 } 155 } 156 } 157 | Popular Tags |