KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > controller > tests > BlockTests


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.controller.tests;
66
67 import com.jcorporate.expresso.core.controller.Block;
68 import com.jcorporate.expresso.core.controller.Input;
69 import com.jcorporate.expresso.core.controller.Output;
70 import com.jcorporate.expresso.core.controller.Transition;
71 import com.jcorporate.expresso.core.misc.StringDOMParser;
72 import com.jcorporate.expresso.kernel.util.FastStringBuffer;
73 import com.jcorporate.expresso.services.test.ExpressoTestCase;
74 import junit.framework.TestSuite;
75 import org.w3c.dom.Document JavaDoc;
76
77
78 /**
79  * Unit test for com.jcorporate.expresso.core.controller.Block objects.
80  *
81  * @author Michael Rimov
82  * @version $Revision: 1.2 $
83  */

84 public class BlockTests
85         extends ExpressoTestCase {
86     Block b = null;
87     StringDOMParser parser = null;
88     static final String JavaDoc BlockName = "TestBlock";
89     static final String JavaDoc outputName = "Output1";
90     static final String JavaDoc outputContent = "Sample Content";
91     static final String JavaDoc inputName1 = "Input1";
92     static final String JavaDoc inputLabel1 = "Enter Your Name";
93     static final String JavaDoc inputName2 = "Input2";
94     static final String JavaDoc inputLabel2 = "Enter Your Login Name";
95     static final String JavaDoc transitionName = "Transition1";
96     static final String JavaDoc transitionLabel = "Click Here To Continue";
97     static final String JavaDoc transitionController = "com.jcorporate.expresso.services.controller.DBMaint";
98     static final String JavaDoc block2Name = "Block 2";
99
100     public BlockTests(String JavaDoc testName)
101             throws Exception JavaDoc {
102         super(testName);
103     }
104
105     public static void main(String JavaDoc[] args)
106             throws Exception JavaDoc {
107
108         //Set the system properties we need
109
junit.textui.TestRunner.run(suite());
110     }
111
112     public void setUp()
113             throws Exception JavaDoc {
114         b = new Block(BlockName);
115
116         Input i = new Input(inputName1, inputLabel1);
117         b.add(i);
118         i = new Input(inputName2, inputLabel2);
119         b.add(i);
120
121         Output o = new Output(outputName, outputContent);
122         b.add(o);
123
124         Transition t = new Transition(transitionName, transitionLabel,
125                 transitionController);
126         b.add(t);
127
128         Block b2 = new Block(block2Name);
129         b.add(b2);
130         parser = new StringDOMParser();
131     }
132
133     /**
134      * Filler Function to conform to log4j test suites.
135      */

136     public static junit.framework.Test suite()
137             throws Exception JavaDoc {
138         return new TestSuite(BlockTests.class);
139     }
140
141     /**
142      * Helper function to set up the parser and parse the XML returned
143      * from the Block
144      */

145     private Document JavaDoc getXMLDoc(Block block)
146             throws Exception JavaDoc {
147         FastStringBuffer fsb = new FastStringBuffer(128);
148         String JavaDoc result = block.toXML(fsb).toString();
149         System.out.println("\nBlock Before Serialization:");
150         System.out.println(result);
151
152         Document JavaDoc d = parser.parseString(result);
153
154         if (d == null) {
155             fail("Block returned mal-formed XML document");
156         }
157
158         return d;
159     }
160
161     /**
162      * Helper to rebuild the Block from XML
163      */

164     private Block getBlock(Document JavaDoc d)
165             throws Exception JavaDoc {
166         try {
167             Block anotherBlock = (Block) Block.fromXML(d);
168             FastStringBuffer fsb = new FastStringBuffer(128);
169             System.out.println("\nBlock After De-Serialization:");
170             System.out.println(anotherBlock.toXML(fsb).toString());
171
172             return anotherBlock;
173         } catch (NullPointerException JavaDoc npe) {
174             npe.printStackTrace();
175             fail("Null Pointer Exception: " + npe);
176
177             return null;
178         }
179     }
180
181     /**
182      * Tests a very simple block XML Serialization
183      */

184     public void testSimpleXML()
185             throws Exception JavaDoc {
186         Block simpleBlock = new Block(BlockName);
187         Document JavaDoc d = getXMLDoc(simpleBlock);
188         Block newBlock = getBlock(d);
189         assertTrue("getBlock returned null", newBlock != null);
190         assertTrue("Proper Name", newBlock.getName().equals(BlockName));
191     }
192
193     /**
194      * Test basic Block->XML->Block Serialization
195      */

196     public void testXML()
197             throws Exception JavaDoc {
198         Document JavaDoc d = getXMLDoc(b);
199         Block newBlock = getBlock(d);
200         assertTrue("getBlock returned null", newBlock != null);
201         assertTrue("Proper Name", newBlock.getName().equals(BlockName));
202
203         try {
204             Input i;
205             Output o;
206             Transition t;
207             Block b2;
208             i = (Input) newBlock.getContent(inputName1);
209             assertTrue("i != null for " + inputName1, i != null);
210             assertTrue("i.getLabel() == " + inputLabel1,
211                     i.getLabel().equals(inputLabel1));
212             i = (Input) newBlock.getContent(inputName2);
213             assertTrue("i != null for " + inputName2, i != null);
214             assertTrue("i.getLabel() == " + inputLabel2,
215                     i.getLabel().equals(inputLabel2));
216             o = (Output) newBlock.getContent(outputName);
217             assertTrue("o != null for " + outputName, o != null);
218             assertTrue("o.getContent() == " + outputContent,
219                     o.getContent().equals(outputContent));
220             t = (Transition) newBlock.getContent(transitionName);
221             assertTrue("t != null for " + transitionName, t != null);
222             assertTrue("t.getControllerObject() == " + transitionController,
223                     t.getControllerObject().equals(transitionController));
224             assertTrue("t.getLabel() == " + transitionLabel,
225                     t.getLabel().equals(transitionLabel));
226             b2 = (Block) newBlock.getContent(block2Name);
227             assertTrue("b2 != null for " + block2Name, b2 != null);
228         } catch (ClassCastException JavaDoc cce) {
229             cce.printStackTrace();
230             fail("Class Cast Exception: " + cce.getMessage());
231         }
232     }
233 }
Popular Tags