KickJava   Java API By Example, From Geeks To Geeks.

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


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.Input;
68 import com.jcorporate.expresso.core.dbobj.ValidValue;
69 import com.jcorporate.expresso.core.misc.StringDOMParser;
70 import com.jcorporate.expresso.kernel.util.FastStringBuffer;
71 import com.jcorporate.expresso.services.test.CommandLineParser;
72 import com.jcorporate.expresso.services.test.ExpressoTestCase;
73 import junit.framework.TestSuite;
74 import org.w3c.dom.Document JavaDoc;
75
76 import java.util.Enumeration JavaDoc;
77 import java.util.Vector JavaDoc;
78
79
80 /**
81  * Unit test case for testing com.jcorporate.expresso.core.controller.Input
82  * objects.
83  *
84  * @author Michael Rimov
85  * @version $Revision: 1.2 $
86  */

87 public class InputTests
88         extends ExpressoTestCase {
89     Input i = null;
90     StringDOMParser parser = null;
91
92     public InputTests(String JavaDoc testName)
93             throws Exception JavaDoc {
94         super(testName);
95     }
96
97     public static void main(String JavaDoc[] args)
98             throws Exception JavaDoc {
99
100         //Set the system properties we need
101
CommandLineParser.parseCommandLine(args);
102         junit.textui.TestRunner.run(suite());
103     }
104
105     public void setUp()
106             throws Exception JavaDoc {
107         i = new Input("Test Input", "This is a test Input");
108         i.setLookup("com.jcorporate.expresso.services.dbobj.MimeTypes");
109         i.setDescription("This is a test input description");
110         i.setDisplayLength(30);
111         i.setType("boolean");
112         i.setAttribute("checkbox", "");
113         parser = new StringDOMParser();
114     }
115
116     /**
117      * Filler Function to conform to log4j test suites.
118      */

119     public static junit.framework.Test suite()
120             throws Exception JavaDoc {
121         return new TestSuite(InputTests.class);
122     }
123
124     /**
125      * Helper function to set up the parser and parse the XML returned
126      * from the input
127      */

128     private Document JavaDoc getXMLDoc()
129             throws Exception JavaDoc {
130         FastStringBuffer fsb = new FastStringBuffer(128);
131         String JavaDoc result = i.toXML(fsb).toString();
132         System.out.println("\nInput Before Serialization:");
133         System.out.println(result);
134
135         Document JavaDoc d = parser.parseString(result);
136
137         if (d == null) {
138             fail("Input returned mal-formed XML document");
139         }
140
141         return d;
142     }
143
144     /**
145      * Helper to rebuild the input from XML
146      */

147     private Input getInput(Document JavaDoc d)
148             throws Exception JavaDoc {
149         Input anotherI = (Input) Input.fromXML(d);
150
151         // System.out.println("\nInput After De-Serialization:");
152
// System.out.println(anotherI.toXML(fsb).toString());
153
return anotherI;
154     }
155
156     /**
157      * Test basic Input->XML->Input Serialization
158      */

159     public void testXML()
160             throws Exception JavaDoc {
161         Document JavaDoc d = getXMLDoc();
162         Input newInput = getInput(d);
163         assertTrue("getInput returned null", newInput != null);
164         assertTrue("Proper Display Length", newInput.getDisplayLength() == 30);
165         assertTrue("Proper Lookup",
166                 newInput.getLookup().equals("com.jcorporate.expresso.services.dbobj.MimeTypes"));
167         assertTrue("Proper Name", newInput.getName().equals("Test Input"));
168         assertTrue("Proper Description",
169                 newInput.getDescription().equals("This is a test input description"));
170         assertTrue("Proper Label",
171                 newInput.getLabel().equals("This is a test Input"));
172         assertTrue("Proper Type", newInput.getType().equals("boolean"));
173         assertTrue("Checkbox Attribute Exists",
174                 newInput.getAttribute("checkbox") != null);
175     }
176
177     /**
178      * Same as testXML, but test MultiValues too
179      */

180     public void testXMLMultiValues()
181             throws Exception JavaDoc {
182         i.addValidValue("one", "One Value");
183         i.addValidValue("two", "Two Values");
184         i.addValidValue("three", "Three Values");
185         i.setType("C");
186
187         Document JavaDoc d = getXMLDoc();
188         Input newInput = getInput(d);
189         assertTrue("getInput returned null", newInput != null);
190         assertTrue("Proper Display Length", newInput.getDisplayLength() == 30);
191         assertTrue("Proper Lookup",
192                 newInput.getLookup().equals("com.jcorporate.expresso.services.dbobj.MimeTypes"));
193         assertTrue("Proper Name", newInput.getName().equals("Test Input"));
194         assertTrue("Proper Description",
195                 newInput.getDescription().equals("This is a test input description"));
196         assertTrue("Proper Label",
197                 newInput.getLabel().equals("This is a test Input"));
198         assertTrue("Proper Type", newInput.getType().equals("C"));
199         assertTrue("Checkbox Attribute Exists",
200                 newInput.getAttribute("checkbox") != null);
201
202         //Now check valid values
203
Vector JavaDoc v = newInput.getValidValues();
204         assertTrue("Returned Vector of valid values",
205                 (v != null && !v.isEmpty()));
206         assertTrue("Returned proper number of valid values", v.size() == 3);
207
208         for (Enumeration JavaDoc e = v.elements(); e.hasMoreElements();) {
209             ValidValue vv = (ValidValue) e.nextElement();
210             assertTrue("Valid Value != null", vv != null);
211
212             String JavaDoc value = vv.getValue();
213             String JavaDoc description = vv.getDescription();
214
215             if (value.equals("one")) {
216                 assertTrue("Proper Description for one",
217                         description.equals("One Value"));
218             } else if (value.equals("two")) {
219                 assertTrue("Proper Description for two",
220                         description.equals("Two Values"));
221             } else if (value.equals("three")) {
222                 assertTrue("Proper Description for three",
223                         description.equals("Three Values"));
224             } else {
225                 fail("Got invalid valid value: " + value);
226             }
227         }
228     }
229
230     /**
231      * Same as testXML, but test with nested inputs
232      */

233     public void testXMLWithNestedInput()
234             throws Exception JavaDoc {
235         Input nested = new Input("nested1", "label1");
236         i.addNested(nested);
237         nested = new Input("nested2", "label2");
238         i.addNested(nested);
239
240         Document JavaDoc d = getXMLDoc();
241         Input newInput = getInput(d);
242         assertTrue("getInput returned null", newInput != null);
243         assertTrue("Proper Display Length", newInput.getDisplayLength() == 30);
244         assertTrue("Proper Lookup",
245                 newInput.getLookup().equals("com.jcorporate.expresso.services.dbobj.MimeTypes"));
246         assertTrue("Proper Name", newInput.getName().equals("Test Input"));
247         assertTrue("Proper Description",
248                 newInput.getDescription().equals("This is a test input description"));
249         assertTrue("Proper Label",
250                 newInput.getLabel().equals("This is a test Input"));
251         assertTrue("Proper Type", newInput.getType().equals("boolean"));
252         assertTrue("Checkbox Attribute Exists",
253                 newInput.getAttribute("checkbox") != null);
254         nested = (Input) newInput.getNested("nested1");
255         assertTrue("Got nested 1", nested != null);
256         assertTrue("Proper Nested 1 Label", nested.getLabel().equals("label1"));
257         nested = (Input) newInput.getNested("nested2");
258         assertTrue("Got nested 2", nested != null);
259         assertTrue("Proper Nested 2 Label", nested.getLabel().equals("label2"));
260     }
261 }
Popular Tags