001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.scxml;
018:
019: import java.util.HashSet;
020: import java.util.Set;
021:
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025:
026: import org.apache.commons.scxml.env.MockErrorReporter;
027: import org.apache.commons.scxml.env.SimpleErrorReporter;
028: import org.apache.commons.scxml.model.Parallel;
029: import org.apache.commons.scxml.model.State;
030: import org.apache.commons.scxml.model.TransitionTarget;
031: import org.apache.commons.scxml.semantics.ErrorConstants;
032:
033: public class SCXMLHelperTest extends TestCase {
034:
035: public SCXMLHelperTest(String testName) {
036: super (testName);
037: }
038:
039: public static Test suite() {
040: return new TestSuite(SCXMLHelperTest.class);
041: }
042:
043: public static void main(String args[]) {
044: String[] testCaseName = { SCXMLHelperTest.class.getName() };
045: junit.textui.TestRunner.main(testCaseName);
046: }
047:
048: public void testIsStringEmptyNull() {
049: assertTrue(SCXMLHelper.isStringEmpty(null));
050: }
051:
052: public void testIsStringEmptyZeroLength() {
053: assertTrue(SCXMLHelper.isStringEmpty(""));
054: }
055:
056: public void testIsStringEmpty() {
057: assertFalse(SCXMLHelper.isStringEmpty("value"));
058: }
059:
060: public void testIsDescendantNullParent() {
061: TransitionTarget state = new State();
062: TransitionTarget context = new State();
063:
064: assertFalse(SCXMLHelper.isDescendant(state, context));
065: }
066:
067: public void testIsDescendantNotEqual() {
068: TransitionTarget state = new State();
069: state.setParent(new State());
070: TransitionTarget context = new State();
071:
072: assertFalse(SCXMLHelper.isDescendant(state, context));
073: }
074:
075: public void testIsDescendantEqual() {
076: TransitionTarget state = new State();
077: TransitionTarget context = new State();
078: state.setParent(context);
079:
080: assertTrue(SCXMLHelper.isDescendant(state, context));
081: }
082:
083: public void testIsDescendantParentEqual() {
084: TransitionTarget state = new State();
085: TransitionTarget context = new State();
086: TransitionTarget parent = new State();
087:
088: parent.setParent(context);
089: state.setParent(parent);
090:
091: assertTrue(SCXMLHelper.isDescendant(state, context));
092: }
093:
094: public void testGetAncestorClosureEmptySet() {
095: Set states = new HashSet();
096:
097: Set returnValue = SCXMLHelper.getAncestorClosure(states,
098: new HashSet());
099:
100: assertEquals(0, returnValue.size());
101: }
102:
103: public void testGetAncestorClosureUpperBoundNotNullAndContains() {
104: Set states = new HashSet();
105: TransitionTarget state = new State();
106: state.setId("1");
107: states.add(state);
108:
109: Set upperBounds = new HashSet();
110: upperBounds.add(state);
111:
112: Set returnValue = SCXMLHelper.getAncestorClosure(states,
113: upperBounds);
114:
115: assertEquals(1, returnValue.size());
116: assertEquals("1", ((TransitionTarget) returnValue.toArray()[0])
117: .getId());
118: }
119:
120: public void testGetAncestorClosureContainsParent() {
121: Set states = new HashSet();
122: TransitionTarget state = new State();
123: state.setId("1");
124: state.setParent(state);
125: states.add(state);
126:
127: Set upperBounds = new HashSet();
128:
129: Set returnValue = SCXMLHelper.getAncestorClosure(states,
130: upperBounds);
131:
132: assertEquals(1, returnValue.size());
133: assertEquals("1", ((TransitionTarget) returnValue.toArray()[0])
134: .getId());
135: }
136:
137: public void testIsLegalConfigNoStates() {
138: Set states = new HashSet();
139:
140: assertTrue(SCXMLHelper.isLegalConfig(states,
141: new SimpleErrorReporter()));
142: }
143:
144: public void testIsLegalConfigInvalidParallel() {
145: Set states = new HashSet();
146: Parallel parallel = new Parallel();
147:
148: Parallel parent = new Parallel();
149: parent.setId("4");
150:
151: State state1 = new State();
152: state1.setId("1");
153: State state2 = new State();
154: state2.setId("2");
155:
156: parent.addState(state1);
157: parent.addState(state2);
158:
159: parallel.setParent(parent);
160:
161: states.add(parallel);
162:
163: MockErrorReporter errorReporter = new MockErrorReporter();
164:
165: assertFalse(SCXMLHelper.isLegalConfig(states, errorReporter));
166: assertEquals(ErrorConstants.ILLEGAL_CONFIG, errorReporter
167: .getErrCode());
168: assertEquals("Not all AND states active for parallel 4",
169: errorReporter.getErrDetail());
170: }
171:
172: public void testIsLegalConfigMultipleTopLevel() {
173: Set states = new HashSet();
174:
175: State state1 = new State();
176: state1.setId("1");
177: State state2 = new State();
178: state2.setId("2");
179:
180: states.add(state1);
181: states.add(state2);
182:
183: MockErrorReporter errorReporter = new MockErrorReporter();
184:
185: assertTrue(SCXMLHelper.isLegalConfig(states, errorReporter));
186: assertEquals(ErrorConstants.ILLEGAL_CONFIG, errorReporter
187: .getErrCode());
188: assertEquals("Multiple top-level OR states active!",
189: errorReporter.getErrDetail());
190: }
191:
192: public void testIsLegalConfigMultipleStatesActive() {
193: Set states = new HashSet();
194:
195: State state1 = new State();
196: state1.setId("1");
197:
198: State state2 = new State();
199: state2.setId("2");
200:
201: State parent = new State();
202: parent.setId("parentid");
203:
204: state2.setParent(parent);
205: state1.setParent(parent);
206:
207: states.add(state1);
208: states.add(state2);
209:
210: MockErrorReporter errorReporter = new MockErrorReporter();
211:
212: assertFalse(SCXMLHelper.isLegalConfig(states, errorReporter));
213: assertEquals(ErrorConstants.ILLEGAL_CONFIG, errorReporter
214: .getErrCode());
215: assertEquals("Multiple OR states active for state parentid",
216: errorReporter.getErrDetail());
217: }
218:
219: public void testGetLCASameTarget() {
220: TransitionTarget target = new State();
221: target.setId("1");
222:
223: TransitionTarget returnValue = SCXMLHelper.getLCA(target,
224: target);
225:
226: assertEquals("1", returnValue.getId());
227: }
228:
229: public void testGetLCAIsDescendant() {
230: TransitionTarget target = new State();
231: target.setId("1");
232:
233: TransitionTarget parent = new State();
234: parent.setId("2");
235:
236: target.setParent(parent);
237:
238: TransitionTarget returnValue = SCXMLHelper.getLCA(target,
239: parent);
240:
241: assertEquals("2", returnValue.getId());
242: }
243:
244: public void testGetLCAIsDescendantReverse() {
245: TransitionTarget target = new State();
246: target.setId("1");
247:
248: TransitionTarget parent = new State();
249: parent.setId("2");
250:
251: parent.setParent(target); // reversed
252:
253: TransitionTarget returnValue = SCXMLHelper.getLCA(target,
254: parent);
255:
256: assertEquals("1", returnValue.getId());
257: }
258:
259: public void testGetLCANull() {
260: TransitionTarget target = new State();
261: target.setId("1");
262:
263: TransitionTarget notParent = new State();
264: notParent.setId("2");
265:
266: TransitionTarget returnValue = SCXMLHelper.getLCA(target,
267: notParent);
268:
269: assertNull(returnValue);
270: }
271:
272: public void testGetLCADistantAncestor() {
273: TransitionTarget target1 = new State();
274: target1.setId("1");
275:
276: TransitionTarget target2 = new State();
277: target2.setId("2");
278:
279: TransitionTarget parent = new State();
280: parent.setId("3");
281:
282: target1.setParent(parent);
283: target2.setParent(parent);
284:
285: TransitionTarget returnValue = SCXMLHelper.getLCA(target1,
286: target2);
287:
288: assertEquals("3", returnValue.getId());
289: }
290: }
|