001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.core.controller.tests;
066:
067: import com.jcorporate.expresso.core.controller.Block;
068: import com.jcorporate.expresso.core.controller.Input;
069: import com.jcorporate.expresso.core.controller.Output;
070: import com.jcorporate.expresso.core.controller.Transition;
071: import com.jcorporate.expresso.core.misc.StringDOMParser;
072: import com.jcorporate.expresso.kernel.util.FastStringBuffer;
073: import com.jcorporate.expresso.services.test.ExpressoTestCase;
074: import junit.framework.TestSuite;
075: import org.w3c.dom.Document;
076:
077: /**
078: * Unit test for com.jcorporate.expresso.core.controller.Block objects.
079: *
080: * @author Michael Rimov
081: * @version $Revision: 1.2 $
082: */
083: public class BlockTests extends ExpressoTestCase {
084: Block b = null;
085: StringDOMParser parser = null;
086: static final String BlockName = "TestBlock";
087: static final String outputName = "Output1";
088: static final String outputContent = "Sample Content";
089: static final String inputName1 = "Input1";
090: static final String inputLabel1 = "Enter Your Name";
091: static final String inputName2 = "Input2";
092: static final String inputLabel2 = "Enter Your Login Name";
093: static final String transitionName = "Transition1";
094: static final String transitionLabel = "Click Here To Continue";
095: static final String transitionController = "com.jcorporate.expresso.services.controller.DBMaint";
096: static final String block2Name = "Block 2";
097:
098: public BlockTests(String testName) throws Exception {
099: super (testName);
100: }
101:
102: public static void main(String[] args) throws Exception {
103:
104: //Set the system properties we need
105: junit.textui.TestRunner.run(suite());
106: }
107:
108: public void setUp() throws Exception {
109: b = new Block(BlockName);
110:
111: Input i = new Input(inputName1, inputLabel1);
112: b.add(i);
113: i = new Input(inputName2, inputLabel2);
114: b.add(i);
115:
116: Output o = new Output(outputName, outputContent);
117: b.add(o);
118:
119: Transition t = new Transition(transitionName, transitionLabel,
120: transitionController);
121: b.add(t);
122:
123: Block b2 = new Block(block2Name);
124: b.add(b2);
125: parser = new StringDOMParser();
126: }
127:
128: /**
129: * Filler Function to conform to log4j test suites.
130: */
131: public static junit.framework.Test suite() throws Exception {
132: return new TestSuite(BlockTests.class);
133: }
134:
135: /**
136: * Helper function to set up the parser and parse the XML returned
137: * from the Block
138: */
139: private Document getXMLDoc(Block block) throws Exception {
140: FastStringBuffer fsb = new FastStringBuffer(128);
141: String result = block.toXML(fsb).toString();
142: System.out.println("\nBlock Before Serialization:");
143: System.out.println(result);
144:
145: Document d = parser.parseString(result);
146:
147: if (d == null) {
148: fail("Block returned mal-formed XML document");
149: }
150:
151: return d;
152: }
153:
154: /**
155: * Helper to rebuild the Block from XML
156: */
157: private Block getBlock(Document d) throws Exception {
158: try {
159: Block anotherBlock = (Block) Block.fromXML(d);
160: FastStringBuffer fsb = new FastStringBuffer(128);
161: System.out.println("\nBlock After De-Serialization:");
162: System.out.println(anotherBlock.toXML(fsb).toString());
163:
164: return anotherBlock;
165: } catch (NullPointerException npe) {
166: npe.printStackTrace();
167: fail("Null Pointer Exception: " + npe);
168:
169: return null;
170: }
171: }
172:
173: /**
174: * Tests a very simple block XML Serialization
175: */
176: public void testSimpleXML() throws Exception {
177: Block simpleBlock = new Block(BlockName);
178: Document d = getXMLDoc(simpleBlock);
179: Block newBlock = getBlock(d);
180: assertTrue("getBlock returned null", newBlock != null);
181: assertTrue("Proper Name", newBlock.getName().equals(BlockName));
182: }
183:
184: /**
185: * Test basic Block->XML->Block Serialization
186: */
187: public void testXML() throws Exception {
188: Document d = getXMLDoc(b);
189: Block newBlock = getBlock(d);
190: assertTrue("getBlock returned null", newBlock != null);
191: assertTrue("Proper Name", newBlock.getName().equals(BlockName));
192:
193: try {
194: Input i;
195: Output o;
196: Transition t;
197: Block b2;
198: i = (Input) newBlock.getContent(inputName1);
199: assertTrue("i != null for " + inputName1, i != null);
200: assertTrue("i.getLabel() == " + inputLabel1, i.getLabel()
201: .equals(inputLabel1));
202: i = (Input) newBlock.getContent(inputName2);
203: assertTrue("i != null for " + inputName2, i != null);
204: assertTrue("i.getLabel() == " + inputLabel2, i.getLabel()
205: .equals(inputLabel2));
206: o = (Output) newBlock.getContent(outputName);
207: assertTrue("o != null for " + outputName, o != null);
208: assertTrue("o.getContent() == " + outputContent, o
209: .getContent().equals(outputContent));
210: t = (Transition) newBlock.getContent(transitionName);
211: assertTrue("t != null for " + transitionName, t != null);
212: assertTrue("t.getControllerObject() == "
213: + transitionController, t.getControllerObject()
214: .equals(transitionController));
215: assertTrue("t.getLabel() == " + transitionLabel, t
216: .getLabel().equals(transitionLabel));
217: b2 = (Block) newBlock.getContent(block2Name);
218: assertTrue("b2 != null for " + block2Name, b2 != null);
219: } catch (ClassCastException cce) {
220: cce.printStackTrace();
221: fail("Class Cast Exception: " + cce.getMessage());
222: }
223: }
224: }
|