KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > dtd > grammar > ContentModelTest


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 package org.netbeans.modules.xml.dtd.grammar;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29 import junit.framework.TestCase;
30
31 /**
32  * Tests DTD code completion for exact content models.
33  * It contains tescases covering all known bugs.
34  *
35  * @author Petr Kuzel
36  */

37 public class ContentModelTest extends TestCase {
38
39     public ContentModelTest(String JavaDoc testName) {
40         super(testName);
41     }
42
43     /** Test of parseContentModel method, of class org.netbeans.modules.xml.text.completion.dtd.ContentModel. */
44     public void testParseContentModel() {
45         System.out.println("testParseContentModel");
46
47         // test for exceptions only
48

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 JavaDoc ex) {
56             ex.printStackTrace();
57             fail(ex.toString());
58         }
59     }
60     
61     /** Test of whatCanFollow method, of class org.netbeans.modules.xml.text.completion.dtd.ContentModel. */
62     public void testWhatCanFollow() {
63         System.out.println("testWhatCanFollow");
64
65         Enumeration JavaDoc in, gold;
66
67         // test Element and multiplicity group models ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68

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         //!!! offers element (not so bad)
94
// in = new InputEnumeration("invalid-element");
95
// gold = null;
96
// probe("(element*)", in, gold);
97

98         // test sequence ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99

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         // test choice ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
109

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         // group of optional CM is also optional
147
in = new InputEnumeration("");
148         gold = new InputEnumeration("element element2 element3");
149         probe("((element*|element2*),element3)", in, gold);
150
151         // #47738 test case
152
in = new InputEnumeration("a");
153         gold = new InputEnumeration("a element element2");
154         probe("(a*, (element*|element2*)", in, gold);
155
156         // uniq subpath of choice implies that other options are invalid
157
in = new InputEnumeration("element");
158         gold = new InputEnumeration("element");
159         probe("(element*|element2*)", in, gold);
160         
161         
162         // test options in sequence ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
163

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         // one sequence element is partialy feeded
177
in = new InputEnumeration("se partial");
178         gold = new InputEnumeration("partial en ce ");
179         probe("(se?,(partial*|y*),(en+|ce+))", in, gold);
180         
181         // missing optional, mandatory, choice
182
in = new InputEnumeration("pointer");
183         gold = new InputEnumeration("post");
184         probe("(option?, pointer, post)", in, gold);
185         
186         // test a choice of conflicting sequences
187

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     /**
219      * Perform whatCanFollow() and compare it to expected result.
220      */

221     private void probe(final String JavaDoc modelDesc, final Enumeration JavaDoc in, final Enumeration JavaDoc gold) {
222         System.out.println("Probing: " + modelDesc + " for: " + in);
223                 
224         ContentModel model = ContentModel.parseContentModel(modelDesc);
225         
226         Enumeration JavaDoc 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     /**
240      * Subclass StringTokenizer for better toString() reports.
241      */

242     private static class InputEnumeration extends StringTokenizer JavaDoc {
243         
244         private final String JavaDoc in;
245         
246         InputEnumeration(String JavaDoc in) {
247             super(in);
248             this.in = in;
249         }
250         
251         public String JavaDoc toString() {
252             return in;
253         }
254     }
255     
256     /**
257      * Two enumerations are same if contains the sama value <b>set</b>.
258      */

259     private class ProbeEnum {
260         
261         private List JavaDoc list = new ArrayList JavaDoc(9);
262         private Set JavaDoc set = new HashSet JavaDoc(9);
263         
264         public ProbeEnum(Enumeration JavaDoc en) {
265             while (en.hasMoreElements()) {
266                 Object JavaDoc next = en.nextElement();
267                 list.add(next);
268                 set.add(next);
269             }
270         }
271         
272         public boolean equals(Object JavaDoc 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 JavaDoc toString() {
282             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
283             for (Iterator JavaDoc it = list.iterator(); it.hasNext(); ) {
284                 Object JavaDoc next = it.next();
285                 buffer.append(next.toString() + ",");
286             }
287             return buffer.toString();
288         }
289     }
290 }
291
Popular Tags