1 19 20 package org.netbeans.modules.xml.dtd.grammar; 21 22 import java.util.ArrayList ; 23 import java.util.Enumeration ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Set ; 28 import java.util.StringTokenizer ; 29 import junit.framework.TestCase; 30 31 37 public class ContentModelTest extends TestCase { 38 39 public ContentModelTest(String testName) { 40 super(testName); 41 } 42 43 44 public void testParseContentModel() { 45 System.out.println("testParseContentModel"); 46 47 49 try { 50 ContentModel.parseContentModel("(simple)"); 51 ContentModel.parseContentModel("(se,qu,en,ce)"); 52 ContentModel.parseContentModel("(ch|oi|ce)"); 53 ContentModel.parseContentModel("(opt?,mand+,end)"); 54 ContentModel.parseContentModel("(#PCDATA|opt|mand+|end)"); 55 } catch (Exception ex) { 56 ex.printStackTrace(); 57 fail(ex.toString()); 58 } 59 } 60 61 62 public void testWhatCanFollow() { 63 System.out.println("testWhatCanFollow"); 64 65 Enumeration in, gold; 66 67 69 in = new InputEnumeration(""); 70 gold = new InputEnumeration("element"); 71 probe("(element)", in, gold); 72 73 in = new InputEnumeration("element"); 74 gold = new InputEnumeration("element"); 75 probe("(element)*", in, gold); 76 77 in = new InputEnumeration("element"); 78 gold = new InputEnumeration("element"); 79 probe("(element)+", in, gold); 80 81 in = new InputEnumeration("element"); 82 gold = new InputEnumeration(""); 83 probe("(element)?", in, gold); 84 85 in = new InputEnumeration("element"); 86 gold = new InputEnumeration(""); 87 probe("(element)", in, gold); 88 89 in = new InputEnumeration("invalid-element"); 90 gold = null; 91 probe("(element)", in, gold); 92 93 98 100 in = new InputEnumeration("se qu"); 101 gold = new InputEnumeration("en"); 102 probe("(se,qu,en,ce)", in, gold); 103 104 in = new InputEnumeration("se invalid-qu"); 105 gold = null; 106 probe("(se,qu,en,ce)", in, gold); 107 108 110 in = new InputEnumeration(""); 111 gold = new InputEnumeration("element element2"); 112 probe("(element|element2)", in, gold); 113 114 in = new InputEnumeration("invalid-element"); 115 gold = null; 116 probe("(element|element2)", in, gold); 117 118 in = new InputEnumeration(""); 119 gold = new InputEnumeration("element"); 120 probe("(element)?", in, gold); 121 122 in = new InputEnumeration("element"); 123 gold = new InputEnumeration(""); 124 probe("(element|element2)", in, gold); 125 126 in = new InputEnumeration("element2"); 127 gold = new InputEnumeration(""); 128 probe("(element|element2)", in, gold); 129 130 in = new InputEnumeration(""); 131 gold = new InputEnumeration("element"); 132 probe("(element|element)", in, gold); 133 134 in = new InputEnumeration(""); 135 gold = new InputEnumeration("element element2 element3 element4"); 136 probe("((element|element2)|(element3|element4))", in, gold); 137 138 in = new InputEnumeration("invalid-element"); 139 gold = null; 140 probe("((element|element2)|(element3|element4))", in, gold); 141 142 in = new InputEnumeration("element"); 143 gold = new InputEnumeration(""); 144 probe("((element|element2)|(element3|element4))", in, gold); 145 146 in = new InputEnumeration(""); 148 gold = new InputEnumeration("element element2 element3"); 149 probe("((element*|element2*),element3)", in, gold); 150 151 in = new InputEnumeration("a"); 153 gold = new InputEnumeration("a element element2"); 154 probe("(a*, (element*|element2*)", in, gold); 155 156 in = new InputEnumeration("element"); 158 gold = new InputEnumeration("element"); 159 probe("(element*|element2*)", in, gold); 160 161 162 164 in = new InputEnumeration("se qu"); 165 gold = new InputEnumeration("en ce"); 166 probe("(se*,qu?,en?,ce)", in, gold); 167 168 in = new InputEnumeration("se qu"); 169 gold = new InputEnumeration("en ce"); 170 probe("(se,qu,en?,ce)", in, gold); 171 172 in = new InputEnumeration("se"); 173 gold = new InputEnumeration("qu en ce"); 174 probe("(se?,(qu*|en*),ce?)", in, gold); 175 176 in = new InputEnumeration("se partial"); 178 gold = new InputEnumeration("partial en ce "); 179 probe("(se?,(partial*|y*),(en+|ce+))", in, gold); 180 181 in = new InputEnumeration("pointer"); 183 gold = new InputEnumeration("post"); 184 probe("(option?, pointer, post)", in, gold); 185 186 188 in = new InputEnumeration("conflict"); 189 gold = new InputEnumeration("qu ce se"); 190 probe("((conflict,qu) | (conflict,ce?,se))", in, gold); 191 192 in = new InputEnumeration(""); 193 gold = new InputEnumeration("a b"); 194 probe("(a*, b*)", in, gold); 195 196 in = new InputEnumeration("a"); 197 gold = new InputEnumeration("a b"); 198 probe("(a*, b*)", in, gold); 199 200 in = new InputEnumeration("a a a a a a"); 201 gold = new InputEnumeration("a b"); 202 probe("(a*, b*)", in, gold); 203 204 in = new InputEnumeration("a b"); 205 gold = new InputEnumeration("b"); 206 probe("(a*, b*)", in, gold); 207 208 in = new InputEnumeration("a b b b"); 209 gold = new InputEnumeration("b"); 210 probe("(a*, b*)", in, gold); 211 212 in = new InputEnumeration("book"); 213 gold = new InputEnumeration("book price"); 214 probe("(book+, price?)", in, gold); 215 216 } 217 218 221 private void probe(final String modelDesc, final Enumeration in, final Enumeration gold) { 222 System.out.println("Probing: " + modelDesc + " for: " + in); 223 224 ContentModel model = ContentModel.parseContentModel(modelDesc); 225 226 Enumeration out = model.whatCanFollow(in); 227 228 if (gold != null) { 229 assertNotNull("\tNon-null enumeration expected!", out); 230 ProbeEnum outp = new ProbeEnum(out); 231 ProbeEnum goldp = new ProbeEnum(gold); 232 assertEquals("Enums must be same!", goldp, outp); 233 } else { 234 assertNull("Null result expected.", out); 235 } 236 237 } 238 239 242 private static class InputEnumeration extends StringTokenizer { 243 244 private final String in; 245 246 InputEnumeration(String in) { 247 super(in); 248 this.in = in; 249 } 250 251 public String toString() { 252 return in; 253 } 254 } 255 256 259 private class ProbeEnum { 260 261 private List list = new ArrayList (9); 262 private Set set = new HashSet (9); 263 264 public ProbeEnum(Enumeration en) { 265 while (en.hasMoreElements()) { 266 Object next = en.nextElement(); 267 list.add(next); 268 set.add(next); 269 } 270 } 271 272 public boolean equals(Object obj) { 273 if (obj instanceof ProbeEnum) return equals((ProbeEnum) obj); 274 return super.equals(obj); 275 } 276 277 public boolean equals(ProbeEnum peer) { 278 return set.containsAll(peer.set) && peer.set.containsAll(set); 279 } 280 281 public String toString() { 282 StringBuffer buffer = new StringBuffer (); 283 for (Iterator it = list.iterator(); it.hasNext(); ) { 284 Object next = it.next(); 285 buffer.append(next.toString() + ","); 286 } 287 return buffer.toString(); 288 } 289 } 290 } 291 | Popular Tags |