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.ControllerException;
069: import com.jcorporate.expresso.core.controller.ControllerRequest;
070: import com.jcorporate.expresso.core.controller.ControllerResponse;
071: import com.jcorporate.expresso.core.controller.Input;
072: import com.jcorporate.expresso.core.controller.Output;
073: import com.jcorporate.expresso.core.controller.Transition;
074: import com.jcorporate.expresso.core.controller.session.SimplePersistentSession;
075: import com.jcorporate.expresso.core.misc.StringDOMParser;
076: import com.jcorporate.expresso.services.test.CommandLineParser;
077: import com.jcorporate.expresso.services.test.ExpressoTestCase;
078: import junit.framework.TestSuite;
079: import org.w3c.dom.Document;
080:
081: import java.io.IOException;
082:
083: /**
084: * Unit test case for testing com.jcorporate.expresso.core.controller.ControllerResponse
085: * objects.
086: *
087: * @author Michael Rimov
088: * @version $Revision: 1.2 $
089: * @since Expresso 5.0
090: */
091: public class ControllerResponseTests extends ExpressoTestCase {
092: ControllerResponse cr = null;
093: StringDOMParser parser = null;
094: static final String controllerName = "com.jcorporate.expresso.services.controller.DBMaint";
095: static final String responseTitle = "Sample Response";
096: static final String responseStyle = "xml??";
097: static final String BlockName = "TestBlock";
098: static final String outputName = "Output1";
099: static final String outputContent = "Sample Content";
100: static final String inputName1 = "Input1";
101: static final String inputLabel1 = "Enter Your Name";
102: static final String inputName2 = "Input2";
103: static final String inputLabel2 = "Enter Your Login Name";
104: static final String transitionName = "Transition1";
105: static final String transitionLabel = "Click Here To Continue";
106: static final String transitionController = "com.jcorporate.expresso.services.controller.DBMaint";
107: static final String block2Name = "Block 2";
108:
109: public ControllerResponseTests(String testName) throws Exception {
110: super (testName);
111: }
112:
113: public static void main(String[] args) throws Exception {
114:
115: //Set the system properties we need
116: CommandLineParser.parseCommandLine(args);
117: junit.textui.TestRunner.run(suite());
118: }
119:
120: public void setUp() throws Exception {
121:
122: cr = new ControllerResponse();
123: cr.setTitle(responseTitle);
124: cr.setControllerClass(controllerName);
125: cr.setStyle(responseStyle);
126: SimplePersistentSession session = new SimplePersistentSession();
127: ControllerRequest request = new ControllerRequest();
128: request.setUid(3);
129: request.setSession(session);
130: cr.setRequest(request);
131:
132: Block b = new Block(BlockName);
133: Input i = new Input(inputName1, inputLabel1);
134: b.add(i);
135: i = new Input(inputName2, inputLabel2);
136: b.add(i);
137:
138: Output o = new Output(outputName, outputContent);
139: b.add(o);
140:
141: Transition t = new Transition(transitionName, transitionLabel,
142: transitionController);
143: b.add(t);
144: cr.addBlock(b);
145:
146: Block b2 = new Block(block2Name);
147: cr.addBlock(b2);
148:
149: //
150: //Yes, the data is already in the blocks, but this way we can
151: //test the block retrieval and Input/Output/Transition Cache
152: //
153: cr.addInput(i);
154: cr.addOutput(o);
155: cr.addTransition(t);
156: parser = new StringDOMParser();
157: }
158:
159: /**
160: * Filler Function to conform to log4j test suites.
161: */
162: public static junit.framework.Test suite() throws Exception {
163: return new TestSuite(ControllerResponseTests.class);
164: }
165:
166: /**
167: * Helper function to set up the parser and parse the XML returned
168: * from the Output
169: */
170: private Document getXMLDoc() throws Exception {
171: String result = cr.toXML();
172:
173: // System.out.println("\nController Response Before Serialization:");
174: // System.out.println(result);
175: Document d = parser.parseString(result);
176:
177: if (d == null) {
178: fail("ControllerResponse returned mal-formed XML document");
179: }
180:
181: return d;
182: }
183:
184: /**
185: * Helper to rebuild the Output from XML
186: */
187: private ControllerResponse getControllerResponse(Document d)
188: throws Exception {
189: ControllerResponse cr1 = ControllerResponse.fromXML(d);
190: System.out
191: .println("\nController Response After De-Serialization:");
192: System.out.println(cr1.toXML());
193:
194: return cr1;
195: }
196:
197: /**
198: * Test basic ControllerReponse->XML->ControllerResponse Serialization
199: * Note, that the serialization of the nested blocks is only determined
200: * to make sure that the blocks exist in the Controller Element. The
201: * well-formdness of the blocks/input/ouputs/transitions is left to their
202: * respective test cases
203: */
204: public void testXML() throws Exception {
205: try {
206: Document d = getXMLDoc();
207: ControllerResponse cr1 = getControllerResponse(d);
208: assertTrue("cr1 returned null", cr1 != null);
209: assertTrue("Block 1 Exists",
210: cr1.getBlock(BlockName) != null);
211: assertTrue("Block 2 Exists",
212: cr1.getBlock(block2Name) != null);
213: assertTrue("Proper Controller Name", cr1
214: .getControllerClass().equals(controllerName));
215: assertTrue("Proper Style", cr1.getStyle().equals(
216: responseStyle));
217: assertTrue("Proper Title", cr1.getTitle().equals(
218: responseTitle));
219: assertTrue("Input 2 Exists",
220: cr1.getInput(inputName2) != null);
221: assertTrue("Output 1 Exists",
222: cr1.getOutput(outputName) != null);
223: assertTrue("Transition 1 Exists", cr1
224: .getTransition(transitionName) != null);
225: } catch (Exception ex) {
226: ex.printStackTrace();
227: fail("Caught Exception: " + ex.getMessage());
228: }
229: }
230:
231: public void testSerialization() {
232: try {
233: ControllerResponse response = new ControllerResponse();
234: ControllerRequest request = new ControllerRequest();
235: response.setRequest(request);
236: request.setDataContext("default");
237: request
238: .setSession(new com.jcorporate.expresso.core.controller.session.SimplePersistentSession());
239: java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
240: java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(
241: bos);
242: oos.writeObject(response);
243: oos.flush();
244: oos.close();
245: java.io.ByteArrayInputStream bis = new java.io.ByteArrayInputStream(
246: bos.toByteArray());
247: java.io.ObjectInputStream ois = new java.io.ObjectInputStream(
248: bis);
249: response = (ControllerResponse) ois.readObject();
250:
251: } catch (IOException ex) {
252: ex.printStackTrace();
253: fail("IO Exception testing serialization "
254: + ex.getMessage());
255: } catch (ClassNotFoundException ex) {
256: ex.printStackTrace();
257: fail("ClassNotFoundException testing serialization "
258: + ex.getMessage());
259:
260: } catch (ControllerException ex) {
261: ex.printStackTrace();
262: fail("ClassNotFoundException testing serialization "
263: + ex.getMessage());
264: }
265:
266: }
267: }
|