KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > test > PatternTestCase


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.test;
10
11 import junit.framework.*;
12
13 import net.sourceforge.chaperon.model.extended.*;
14
15 public class PatternTestCase extends TestCase
16 {
17   private SingleCharacter A = new SingleCharacter('A');
18   private SingleCharacter B = new SingleCharacter('B');
19   private SingleCharacter C = new SingleCharacter('C');
20   private SingleCharacter D = new SingleCharacter('D');
21   private Element e = new Element("element");
22
23   public PatternTestCase(String JavaDoc name)
24   {
25     super(name);
26   }
27
28   public void testPatternSet()
29   {
30     PatternSet actual = new PatternSet();
31     actual.addPattern(A);
32     actual.addPattern(B);
33
34     PatternSet expected = new PatternSet();
35     expected.addPattern(A);
36     expected.addPattern(B);
37
38     assertEquals("Test pattern set", expected, actual);
39
40     actual = new PatternSet();
41     actual.addPattern(A);
42
43     expected = new PatternSet();
44     expected.addPattern(A);
45     expected.addPattern(B);
46
47     assertTrue("Test pattern set", !expected.equals(actual));
48
49     actual = new PatternSet();
50     actual.addPattern(A);
51     actual.addPattern(B);
52
53     expected = new PatternSet();
54     expected.addPattern(A);
55
56     assertTrue("Test pattern set", !expected.equals(actual));
57
58     actual = new PatternSet();
59     actual.addPattern(A);
60     actual.addPattern(B);
61     actual.addPattern(A);
62
63     assertTrue("Test pattern set", actual.getPatternCount()==2);
64   }
65
66   public void testSequence()
67   {
68     Sequence sequence = new Sequence();
69     sequence.addPattern(A);
70     sequence.addPattern(B);
71     sequence.addPattern(C);
72     sequence.addPattern(D);
73
74     PatternSet expected = new PatternSet();
75     expected.addPattern(A);
76     assertEquals("Test first set", expected, sequence.getFirstSet());
77
78     expected = new PatternSet();
79     expected.addPattern(D);
80     assertEquals("Test last set", expected, sequence.getLastSet());
81
82     sequence.update();
83
84     assertTrue("Test successor", A.hasSuccessor(B));
85     assertTrue("Test successor", B.hasSuccessor(C));
86     assertTrue("Test successor", C.hasSuccessor(D));
87     assertTrue("Test ancestor", B.hasAncestor(A));
88     assertTrue("Test ancestor", C.hasAncestor(B));
89     assertTrue("Test ancestor", D.hasAncestor(C));
90   }
91
92   public void testChoice()
93   {
94     Sequence sequence1 = new Sequence();
95     sequence1.addPattern(A);
96     sequence1.addPattern(B);
97
98     Sequence sequence2 = new Sequence();
99     sequence2.addPattern(C);
100     sequence2.addPattern(D);
101
102     Choice choice = new Choice();
103     choice.addPattern(sequence1);
104     choice.addPattern(sequence2);
105
106     PatternSet expected = new PatternSet();
107     expected.addPattern(A);
108     expected.addPattern(C);
109     assertEquals("Test first set", expected, choice.getFirstSet());
110
111     expected = new PatternSet();
112     expected.addPattern(B);
113     expected.addPattern(D);
114     assertEquals("Test last set", expected, choice.getLastSet());
115
116     choice.update();
117
118     assertTrue("Test successor", A.hasSuccessor(B));
119     assertTrue("Test successor", C.hasSuccessor(D));
120     assertTrue("Test ancestor", B.hasAncestor(A));
121     assertTrue("Test ancestor", D.hasAncestor(C));
122   }
123
124   public void testZeroOrMore()
125   {
126     Sequence sequence = new Sequence();
127     sequence.addPattern(A);
128     sequence.addPattern(B);
129     sequence.addPattern(C);
130
131     ZeroOrMore zeroOrMore = new ZeroOrMore();
132     zeroOrMore.addPattern(sequence);
133
134     PatternSet expected = new PatternSet();
135     expected.addPattern(A);
136     assertEquals("Test first set", expected, zeroOrMore.getFirstSet());
137
138     expected = new PatternSet();
139     expected.addPattern(C);
140     assertEquals("Test last set", expected, zeroOrMore.getLastSet());
141
142     zeroOrMore.update();
143
144     assertTrue("Test successor", A.hasSuccessor(B));
145     assertTrue("Test successor", C.hasSuccessor(A));
146   }
147
148   public void testOptional()
149   {
150     Optional optional = new Optional();
151     optional.addPattern(B);
152
153     Sequence sequence = new Sequence();
154     sequence.addPattern(A);
155     sequence.addPattern(optional);
156     sequence.addPattern(C);
157
158     sequence.update();
159
160     assertTrue("Test successor", A.hasSuccessor(B));
161     assertTrue("Test successor", A.hasSuccessor(C));
162     assertTrue("Test successor", B.hasSuccessor(C));
163
164     optional = new Optional();
165     optional.addPattern(B);
166
167     sequence = new Sequence();
168     sequence.addPattern(optional);
169     sequence.addPattern(C);
170
171     PatternSet expected = new PatternSet();
172     expected.addPattern(B);
173     expected.addPattern(C);
174     assertEquals("Test first set", expected, sequence.getFirstSet());
175
176     optional = new Optional();
177     optional.addPattern(B);
178
179     sequence = new Sequence();
180     sequence.addPattern(A);
181     sequence.addPattern(optional);
182
183     expected = new PatternSet();
184     expected.addPattern(B);
185     expected.addPattern(A);
186     assertEquals("Test last set", expected, sequence.getLastSet());
187   }
188
189   public void testElement()
190   {
191     Choice choice = new Choice();
192     choice.addPattern(e);
193     choice.addPattern(A);
194
195     PatternSet expected = new PatternSet();
196     expected.addPattern(A);
197     expected.addPattern(e);
198     assertEquals("Test first set", expected, choice.getFirstSet());
199
200     expected = new PatternSet();
201     expected.addPattern(A);
202     expected.addPattern(e);
203     assertEquals("Test last set", expected, choice.getLastSet());
204   }
205
206   public void testFollowSet()
207   {
208     Choice choice = new Choice();
209     choice.addPattern(A);
210     choice.addPattern(B);
211
212     Sequence sequence = new Sequence();
213     sequence.addPattern(choice);
214     sequence.addPattern(C);
215
216     sequence.update();
217
218     assertTrue("Test ancestor", C.hasAncestor(A));
219     assertTrue("Test ancestor", C.hasAncestor(B));
220   }
221
222   public static Test suite()
223   {
224     return new TestSuite(PatternTestCase.class);
225   }
226 }
227
Popular Tags