001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.test;
010:
011: import junit.framework.*;
012:
013: import net.sourceforge.chaperon.model.extended.*;
014:
015: public class PatternTestCase extends TestCase {
016: private SingleCharacter A = new SingleCharacter('A');
017: private SingleCharacter B = new SingleCharacter('B');
018: private SingleCharacter C = new SingleCharacter('C');
019: private SingleCharacter D = new SingleCharacter('D');
020: private Element e = new Element("element");
021:
022: public PatternTestCase(String name) {
023: super (name);
024: }
025:
026: public void testPatternSet() {
027: PatternSet actual = new PatternSet();
028: actual.addPattern(A);
029: actual.addPattern(B);
030:
031: PatternSet expected = new PatternSet();
032: expected.addPattern(A);
033: expected.addPattern(B);
034:
035: assertEquals("Test pattern set", expected, actual);
036:
037: actual = new PatternSet();
038: actual.addPattern(A);
039:
040: expected = new PatternSet();
041: expected.addPattern(A);
042: expected.addPattern(B);
043:
044: assertTrue("Test pattern set", !expected.equals(actual));
045:
046: actual = new PatternSet();
047: actual.addPattern(A);
048: actual.addPattern(B);
049:
050: expected = new PatternSet();
051: expected.addPattern(A);
052:
053: assertTrue("Test pattern set", !expected.equals(actual));
054:
055: actual = new PatternSet();
056: actual.addPattern(A);
057: actual.addPattern(B);
058: actual.addPattern(A);
059:
060: assertTrue("Test pattern set", actual.getPatternCount() == 2);
061: }
062:
063: public void testSequence() {
064: Sequence sequence = new Sequence();
065: sequence.addPattern(A);
066: sequence.addPattern(B);
067: sequence.addPattern(C);
068: sequence.addPattern(D);
069:
070: PatternSet expected = new PatternSet();
071: expected.addPattern(A);
072: assertEquals("Test first set", expected, sequence.getFirstSet());
073:
074: expected = new PatternSet();
075: expected.addPattern(D);
076: assertEquals("Test last set", expected, sequence.getLastSet());
077:
078: sequence.update();
079:
080: assertTrue("Test successor", A.hasSuccessor(B));
081: assertTrue("Test successor", B.hasSuccessor(C));
082: assertTrue("Test successor", C.hasSuccessor(D));
083: assertTrue("Test ancestor", B.hasAncestor(A));
084: assertTrue("Test ancestor", C.hasAncestor(B));
085: assertTrue("Test ancestor", D.hasAncestor(C));
086: }
087:
088: public void testChoice() {
089: Sequence sequence1 = new Sequence();
090: sequence1.addPattern(A);
091: sequence1.addPattern(B);
092:
093: Sequence sequence2 = new Sequence();
094: sequence2.addPattern(C);
095: sequence2.addPattern(D);
096:
097: Choice choice = new Choice();
098: choice.addPattern(sequence1);
099: choice.addPattern(sequence2);
100:
101: PatternSet expected = new PatternSet();
102: expected.addPattern(A);
103: expected.addPattern(C);
104: assertEquals("Test first set", expected, choice.getFirstSet());
105:
106: expected = new PatternSet();
107: expected.addPattern(B);
108: expected.addPattern(D);
109: assertEquals("Test last set", expected, choice.getLastSet());
110:
111: choice.update();
112:
113: assertTrue("Test successor", A.hasSuccessor(B));
114: assertTrue("Test successor", C.hasSuccessor(D));
115: assertTrue("Test ancestor", B.hasAncestor(A));
116: assertTrue("Test ancestor", D.hasAncestor(C));
117: }
118:
119: public void testZeroOrMore() {
120: Sequence sequence = new Sequence();
121: sequence.addPattern(A);
122: sequence.addPattern(B);
123: sequence.addPattern(C);
124:
125: ZeroOrMore zeroOrMore = new ZeroOrMore();
126: zeroOrMore.addPattern(sequence);
127:
128: PatternSet expected = new PatternSet();
129: expected.addPattern(A);
130: assertEquals("Test first set", expected, zeroOrMore
131: .getFirstSet());
132:
133: expected = new PatternSet();
134: expected.addPattern(C);
135: assertEquals("Test last set", expected, zeroOrMore.getLastSet());
136:
137: zeroOrMore.update();
138:
139: assertTrue("Test successor", A.hasSuccessor(B));
140: assertTrue("Test successor", C.hasSuccessor(A));
141: }
142:
143: public void testOptional() {
144: Optional optional = new Optional();
145: optional.addPattern(B);
146:
147: Sequence sequence = new Sequence();
148: sequence.addPattern(A);
149: sequence.addPattern(optional);
150: sequence.addPattern(C);
151:
152: sequence.update();
153:
154: assertTrue("Test successor", A.hasSuccessor(B));
155: assertTrue("Test successor", A.hasSuccessor(C));
156: assertTrue("Test successor", B.hasSuccessor(C));
157:
158: optional = new Optional();
159: optional.addPattern(B);
160:
161: sequence = new Sequence();
162: sequence.addPattern(optional);
163: sequence.addPattern(C);
164:
165: PatternSet expected = new PatternSet();
166: expected.addPattern(B);
167: expected.addPattern(C);
168: assertEquals("Test first set", expected, sequence.getFirstSet());
169:
170: optional = new Optional();
171: optional.addPattern(B);
172:
173: sequence = new Sequence();
174: sequence.addPattern(A);
175: sequence.addPattern(optional);
176:
177: expected = new PatternSet();
178: expected.addPattern(B);
179: expected.addPattern(A);
180: assertEquals("Test last set", expected, sequence.getLastSet());
181: }
182:
183: public void testElement() {
184: Choice choice = new Choice();
185: choice.addPattern(e);
186: choice.addPattern(A);
187:
188: PatternSet expected = new PatternSet();
189: expected.addPattern(A);
190: expected.addPattern(e);
191: assertEquals("Test first set", expected, choice.getFirstSet());
192:
193: expected = new PatternSet();
194: expected.addPattern(A);
195: expected.addPattern(e);
196: assertEquals("Test last set", expected, choice.getLastSet());
197: }
198:
199: public void testFollowSet() {
200: Choice choice = new Choice();
201: choice.addPattern(A);
202: choice.addPattern(B);
203:
204: Sequence sequence = new Sequence();
205: sequence.addPattern(choice);
206: sequence.addPattern(C);
207:
208: sequence.update();
209:
210: assertTrue("Test ancestor", C.hasAncestor(A));
211: assertTrue("Test ancestor", C.hasAncestor(B));
212: }
213:
214: public static Test suite() {
215: return new TestSuite(PatternTestCase.class);
216: }
217: }
|