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.io;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025:
026: import org.apache.commons.scxml.model.Assign;
027: import org.apache.commons.scxml.model.Cancel;
028: import org.apache.commons.scxml.model.Else;
029: import org.apache.commons.scxml.model.ElseIf;
030: import org.apache.commons.scxml.model.Exit;
031: import org.apache.commons.scxml.model.If;
032: import org.apache.commons.scxml.model.Log;
033: import org.apache.commons.scxml.model.OnEntry;
034: import org.apache.commons.scxml.model.OnExit;
035: import org.apache.commons.scxml.model.SCXML;
036: import org.apache.commons.scxml.model.Send;
037: import org.apache.commons.scxml.model.State;
038: import org.apache.commons.scxml.model.TransitionTarget;
039: import org.apache.commons.scxml.model.Var;
040:
041: public class SCXMLSerializerTest extends TestCase {
042:
043: public SCXMLSerializerTest(String testName) {
044: super (testName);
045: }
046:
047: public static Test suite() {
048: return new TestSuite(SCXMLSerializerTest.class);
049: }
050:
051: public static void main(String args[]) {
052: String[] testCaseName = { SCXMLSerializerTest.class.getName() };
053: junit.textui.TestRunner.main(testCaseName);
054: }
055:
056: public void testSerializeSCXMLNoStates() {
057: SCXML scxml = new SCXML();
058: scxml.setXmlns("namespace");
059: scxml.setVersion("version1");
060: scxml.setInitialstate("off");
061: scxml.addState(new State());
062:
063: String assertValue = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
064: + "<scxml xmlns=\"namespace\" version=\"version1\" "
065: + "initialstate=\"off\">\n <state>\n </state>\n</scxml>\n";
066:
067: assertEquals(assertValue, SCXMLSerializer.serialize(scxml));
068: }
069:
070: public void testSerializeSend() {
071: Send send = new Send();
072: send.setSendid("1");
073: send.setTarget("newTarget");
074: send.setTargettype("newTargetType");
075: send.setNamelist("names");
076: send.setDelay("4 secs");
077: send.setEvent("turnoff");
078: send.setHints("guess");
079:
080: String assertValue = " <send sendid=\"1\" "
081: + "target=\"newTarget\" "
082: + "targetType=\"newTargetType\" "
083: + "namelist=\"names\" " + "delay=\"4 secs\" "
084: + "events=\"turnoff\" "
085: + "hints=\"guess\">\n </send>\n";
086:
087: StringBuffer returnValue = new StringBuffer();
088: SCXMLSerializer.serializeSend(returnValue, send, " ");
089:
090: assertEquals(assertValue.toString(), returnValue.toString());
091: }
092:
093: public void testSerializeActionsListNull() {
094: TransitionTarget target = new State();
095: target.setId("1");
096:
097: StringBuffer returnValue = new StringBuffer();
098: boolean returnBoolean = SCXMLSerializer.serializeActions(
099: returnValue, null, " ");
100:
101: assertFalse(returnBoolean);
102: assertEquals(0, returnValue.length());
103: }
104:
105: public void testSerializeActionsVar() {
106: Var var = new Var();
107: var.setName("newName");
108: var.setExpr("newExpression");
109:
110: List values = new ArrayList();
111: values.add(var);
112:
113: String actualValue = " <var name=\"newName\" expr=\"newExpression\"/>\n";
114:
115: StringBuffer returnValue = new StringBuffer();
116: boolean returnBoolean = SCXMLSerializer.serializeActions(
117: returnValue, values, " ");
118:
119: assertFalse(returnBoolean);
120: assertEquals(actualValue, returnValue.toString());
121: }
122:
123: public void testSerializeActionsAssign() {
124: Assign assign = new Assign();
125: assign.setName("newName");
126: assign.setExpr("newExpression");
127:
128: List values = new ArrayList();
129: values.add(assign);
130:
131: String actualValue = " <assign name=\"newName\" expr=\"newExpression\"/>\n";
132:
133: StringBuffer returnValue = new StringBuffer();
134: boolean returnBoolean = SCXMLSerializer.serializeActions(
135: returnValue, values, " ");
136:
137: assertFalse(returnBoolean);
138: assertEquals(actualValue, returnValue.toString());
139: }
140:
141: public void testSerializeActionsCancel() {
142: Cancel cancel = new Cancel();
143: cancel.setSendid("1");
144:
145: List values = new ArrayList();
146: values.add(cancel);
147:
148: String actualValue = " <cancel sendid=\"1\"/>\n";
149:
150: StringBuffer returnValue = new StringBuffer();
151: boolean returnBoolean = SCXMLSerializer.serializeActions(
152: returnValue, values, " ");
153:
154: assertFalse(returnBoolean);
155: assertEquals(actualValue, returnValue.toString());
156: }
157:
158: public void testSerializeActionsLog() {
159: Log log = new Log();
160: log.setExpr("newExpression");
161:
162: List values = new ArrayList();
163: values.add(log);
164:
165: String actualValue = " <log expr=\"newExpression\"/>\n";
166:
167: StringBuffer returnValue = new StringBuffer();
168: boolean returnBoolean = SCXMLSerializer.serializeActions(
169: returnValue, values, " ");
170:
171: assertFalse(returnBoolean);
172: assertEquals(actualValue, returnValue.toString());
173: }
174:
175: public void testSerializeActionsExit() {
176: Exit exit = new Exit();
177: exit.setExpr("newExpression");
178: exit.setNamelist("names");
179:
180: List values = new ArrayList();
181: values.add(exit);
182:
183: String actualValue = " <exit expr=\"newExpression\" namelist=\"names\"/>\n";
184:
185: StringBuffer returnValue = new StringBuffer();
186: boolean returnBoolean = SCXMLSerializer.serializeActions(
187: returnValue, values, " ");
188:
189: assertTrue(returnBoolean);
190: assertEquals(actualValue, returnValue.toString());
191: }
192:
193: public void testSerializeActionsElse() {
194: Else elseValue = new Else();
195:
196: List values = new ArrayList();
197: values.add(elseValue);
198:
199: String actualValue = " <else/>\n";
200:
201: StringBuffer returnValue = new StringBuffer();
202: boolean returnBoolean = SCXMLSerializer.serializeActions(
203: returnValue, values, " ");
204:
205: assertFalse(returnBoolean);
206: assertEquals(actualValue, returnValue.toString());
207: }
208:
209: public void testSerializeActionsElseIf() {
210: ElseIf elseIf = new ElseIf();
211: elseIf.setCond("newCondition");
212:
213: List values = new ArrayList();
214: values.add(elseIf);
215:
216: String actualValue = " <elseif cond=\"newCondition\" />\n";
217:
218: StringBuffer returnValue = new StringBuffer();
219: boolean returnBoolean = SCXMLSerializer.serializeActions(
220: returnValue, values, " ");
221:
222: assertFalse(returnBoolean);
223: assertEquals(actualValue, returnValue.toString());
224: }
225:
226: public void testSerializeIf() {
227: If ifValue = new If();
228: ifValue.setCond("newCondition");
229:
230: List values = new ArrayList();
231: values.add(ifValue);
232:
233: String actualValue = " <if cond=\"newCondition\">\n </if>\n";
234:
235: StringBuffer returnValue = new StringBuffer();
236: boolean returnBoolean = SCXMLSerializer.serializeActions(
237: returnValue, values, " ");
238:
239: assertFalse(returnBoolean);
240: assertEquals(actualValue, returnValue.toString());
241: }
242:
243: public void testSerializeOnEntrySizeZero() {
244: TransitionTarget target = new State();
245: target.setOnEntry(new OnEntry());
246:
247: String actualValue = "";
248:
249: StringBuffer returnValue = new StringBuffer();
250: SCXMLSerializer.serializeOnEntry(returnValue, target, " ");
251:
252: assertEquals(actualValue, returnValue.toString());
253: }
254:
255: public void testSerializeOnEntry() {
256: TransitionTarget target = new State();
257:
258: OnEntry onEntry = new OnEntry();
259: onEntry.addAction(new Else());
260:
261: target.setOnEntry(onEntry);
262:
263: String actualValue = " <onentry>\n <else/>\n </onentry>\n";
264:
265: StringBuffer returnValue = new StringBuffer();
266: SCXMLSerializer.serializeOnEntry(returnValue, target, " ");
267:
268: assertEquals(actualValue, returnValue.toString());
269: }
270:
271: public void testSerializeOnExitSizeZero() {
272: TransitionTarget target = new State();
273: target.setOnExit(new OnExit());
274:
275: String actualValue = "";
276:
277: StringBuffer returnValue = new StringBuffer();
278: SCXMLSerializer.serializeOnExit(returnValue, target, " ");
279:
280: assertEquals(actualValue, returnValue.toString());
281: }
282:
283: public void testSerializeOnExit() {
284: TransitionTarget target = new State();
285:
286: OnExit onExit = new OnExit();
287: onExit.addAction(new Else());
288:
289: target.setOnExit(onExit);
290:
291: String actualValue = " <onexit>\n <else/>\n </onexit>\n";
292:
293: StringBuffer returnValue = new StringBuffer();
294: SCXMLSerializer.serializeOnExit(returnValue, target, " ");
295:
296: assertEquals(actualValue, returnValue.toString());
297: }
298:
299: }
|