KickJava   Java API By Example, From Geeks To Geeks.

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


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.ControllerException;
69 import com.jcorporate.expresso.core.controller.ControllerRequest;
70 import com.jcorporate.expresso.core.controller.ControllerResponse;
71 import com.jcorporate.expresso.core.controller.Input;
72 import com.jcorporate.expresso.core.controller.Output;
73 import com.jcorporate.expresso.core.controller.Transition;
74 import com.jcorporate.expresso.core.controller.session.SimplePersistentSession;
75 import com.jcorporate.expresso.core.misc.StringDOMParser;
76 import com.jcorporate.expresso.services.test.CommandLineParser;
77 import com.jcorporate.expresso.services.test.ExpressoTestCase;
78 import junit.framework.TestSuite;
79 import org.w3c.dom.Document JavaDoc;
80
81 import java.io.IOException JavaDoc;
82
83
84 /**
85  * Unit test case for testing com.jcorporate.expresso.core.controller.ControllerResponse
86  * objects.
87  *
88  * @author Michael Rimov
89  * @version $Revision: 1.2 $
90  * @since Expresso 5.0
91  */

92 public class ControllerResponseTests
93         extends ExpressoTestCase {
94     ControllerResponse cr = null;
95     StringDOMParser parser = null;
96     static final String JavaDoc controllerName = "com.jcorporate.expresso.services.controller.DBMaint";
97     static final String JavaDoc responseTitle = "Sample Response";
98     static final String JavaDoc responseStyle = "xml??";
99     static final String JavaDoc BlockName = "TestBlock";
100     static final String JavaDoc outputName = "Output1";
101     static final String JavaDoc outputContent = "Sample Content";
102     static final String JavaDoc inputName1 = "Input1";
103     static final String JavaDoc inputLabel1 = "Enter Your Name";
104     static final String JavaDoc inputName2 = "Input2";
105     static final String JavaDoc inputLabel2 = "Enter Your Login Name";
106     static final String JavaDoc transitionName = "Transition1";
107     static final String JavaDoc transitionLabel = "Click Here To Continue";
108     static final String JavaDoc transitionController = "com.jcorporate.expresso.services.controller.DBMaint";
109     static final String JavaDoc block2Name = "Block 2";
110
111     public ControllerResponseTests(String JavaDoc testName)
112             throws Exception JavaDoc {
113         super(testName);
114     }
115
116     public static void main(String JavaDoc[] args)
117             throws Exception JavaDoc {
118
119         //Set the system properties we need
120
CommandLineParser.parseCommandLine(args);
121         junit.textui.TestRunner.run(suite());
122     }
123
124     public void setUp()
125             throws Exception JavaDoc {
126
127
128         cr = new ControllerResponse();
129         cr.setTitle(responseTitle);
130         cr.setControllerClass(controllerName);
131         cr.setStyle(responseStyle);
132         SimplePersistentSession session = new SimplePersistentSession();
133         ControllerRequest request = new ControllerRequest();
134         request.setUid(3);
135         request.setSession(session);
136         cr.setRequest(request);
137
138         Block b = new Block(BlockName);
139         Input i = new Input(inputName1, inputLabel1);
140         b.add(i);
141         i = new Input(inputName2, inputLabel2);
142         b.add(i);
143
144         Output o = new Output(outputName, outputContent);
145         b.add(o);
146
147         Transition t = new Transition(transitionName, transitionLabel,
148                 transitionController);
149         b.add(t);
150         cr.addBlock(b);
151
152         Block b2 = new Block(block2Name);
153         cr.addBlock(b2);
154
155         //
156
//Yes, the data is already in the blocks, but this way we can
157
//test the block retrieval and Input/Output/Transition Cache
158
//
159
cr.addInput(i);
160         cr.addOutput(o);
161         cr.addTransition(t);
162         parser = new StringDOMParser();
163     }
164
165     /**
166      * Filler Function to conform to log4j test suites.
167      */

168     public static junit.framework.Test suite()
169             throws Exception JavaDoc {
170         return new TestSuite(ControllerResponseTests.class);
171     }
172
173     /**
174      * Helper function to set up the parser and parse the XML returned
175      * from the Output
176      */

177     private Document JavaDoc getXMLDoc()
178             throws Exception JavaDoc {
179         String JavaDoc result = cr.toXML();
180
181         // System.out.println("\nController Response Before Serialization:");
182
// System.out.println(result);
183
Document JavaDoc d = parser.parseString(result);
184
185         if (d == null) {
186             fail("ControllerResponse returned mal-formed XML document");
187         }
188
189         return d;
190     }
191
192     /**
193      * Helper to rebuild the Output from XML
194      */

195     private ControllerResponse getControllerResponse(Document JavaDoc d)
196             throws Exception JavaDoc {
197         ControllerResponse cr1 = ControllerResponse.fromXML(d);
198         System.out.println("\nController Response After De-Serialization:");
199         System.out.println(cr1.toXML());
200
201         return cr1;
202     }
203
204     /**
205      * Test basic ControllerReponse->XML->ControllerResponse Serialization
206      * Note, that the serialization of the nested blocks is only determined
207      * to make sure that the blocks exist in the Controller Element. The
208      * well-formdness of the blocks/input/ouputs/transitions is left to their
209      * respective test cases
210      */

211     public void testXML()
212             throws Exception JavaDoc {
213         try {
214             Document JavaDoc d = getXMLDoc();
215             ControllerResponse cr1 = getControllerResponse(d);
216             assertTrue("cr1 returned null", cr1 != null);
217             assertTrue("Block 1 Exists", cr1.getBlock(BlockName) != null);
218             assertTrue("Block 2 Exists", cr1.getBlock(block2Name) != null);
219             assertTrue("Proper Controller Name",
220                     cr1.getControllerClass().equals(controllerName));
221             assertTrue("Proper Style", cr1.getStyle().equals(responseStyle));
222             assertTrue("Proper Title", cr1.getTitle().equals(responseTitle));
223             assertTrue("Input 2 Exists", cr1.getInput(inputName2) != null);
224             assertTrue("Output 1 Exists", cr1.getOutput(outputName) != null);
225             assertTrue("Transition 1 Exists",
226                     cr1.getTransition(transitionName) != null);
227         } catch (Exception JavaDoc ex) {
228             ex.printStackTrace();
229             fail("Caught Exception: " + ex.getMessage());
230         }
231     }
232
233     public void testSerialization() {
234         try {
235             ControllerResponse response = new ControllerResponse();
236             ControllerRequest request = new ControllerRequest();
237             response.setRequest(request);
238             request.setDataContext("default");
239             request.setSession(new com.jcorporate.expresso.core.controller.session.SimplePersistentSession());
240             java.io.ByteArrayOutputStream JavaDoc bos = new java.io.
241
JavaDoc                    ByteArrayOutputStream();
242             java.io.ObjectOutputStream JavaDoc oos = new java.io.ObjectOutputStream JavaDoc(bos);
243             oos.writeObject(response);
244             oos.flush();
245             oos.close();
246             java.io.ByteArrayInputStream JavaDoc bis = new java.io.ByteArrayInputStream JavaDoc(bos.toByteArray());
247             java.io.ObjectInputStream JavaDoc ois = new java.io.ObjectInputStream JavaDoc(bis);
248             response = (ControllerResponse) ois.readObject();
249
250
251         } catch (IOException JavaDoc ex) {
252             ex.printStackTrace();
253             fail("IO Exception testing serialization " + ex.getMessage());
254         } catch (ClassNotFoundException JavaDoc ex) {
255             ex.printStackTrace();
256             fail("ClassNotFoundException testing serialization " + ex.getMessage());
257
258         } catch (ControllerException ex) {
259             ex.printStackTrace();
260             fail("ClassNotFoundException testing serialization " + ex.getMessage());
261         }
262
263     }
264 }
Popular Tags