KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > dataobjects > test > TestDataTransferObject


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.dataobjects.test;
66
67 import com.jcorporate.expresso.core.dataobjects.DataTransferObject;
68 import com.jcorporate.expresso.core.db.DBException;
69 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
70 import com.jcorporate.expresso.services.dbobj.MimeTypes;
71 import com.jcorporate.expresso.services.test.ExpressoTestCase;
72 import com.jcorporate.expresso.services.test.TestSystemInitializer;
73 import junit.framework.TestSuite;
74
75 import java.io.ByteArrayInputStream JavaDoc;
76 import java.io.ByteArrayOutputStream JavaDoc;
77 import java.io.ObjectInputStream JavaDoc;
78 import java.io.ObjectOutputStream JavaDoc;
79 import java.util.Map JavaDoc;
80
81
82 /**
83  * Tests various aspects of the DataTransferObject
84  *
85  * @author Michael Rimov
86  * @version 1.0
87  */

88 public class TestDataTransferObject extends ExpressoTestCase {
89     DataTransferObject dataTransferObject = null;
90     MimeTypes mt = null;
91
92     /**
93      * Creates a new TestDataTransferObject object.
94      *
95      * @param testName The name of the test
96      * @throws Exception Upon initialization error
97      */

98     public TestDataTransferObject(String JavaDoc testName) throws Exception JavaDoc {
99         super(testName);
100     }
101
102     /**
103      * A main function to allow testing of this class only
104      *
105      * @param args Unused
106      * @throws Exception If an error occurs during testing
107      */

108     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
109         junit.textui.TestRunner.run(suite());
110     }
111
112     /**
113      * Constructs the test suite
114      *
115      * @return An instantiated Junit test suite
116      * @throws Exception Upon error
117      */

118     public static junit.framework.Test suite() throws Exception JavaDoc {
119         return new TestSuite(TestDataTransferObject.class);
120     }
121
122     /**
123      * Checks the data transfer object by verifying that the values
124      * set in the data object are consistent with those that appear in the data
125      * transfer object
126      */

127     public void testDTOValues() {
128         try {
129             DataTransferObject dto = mt.getDataTransferObject();
130             assertTrue(TestSystemInitializer.getTestContext().equals(dto.getDataContext()));
131             assertTrue(MimeTypes.class.getName().equals(dto.getObjectClassName()));
132
133             Map JavaDoc fields = dto.getTableFields();
134             checkFields(fields);
135         } catch (DBException ex) {
136             ex.printStackTrace();
137             fail("Exception testing field values");
138         }
139     }
140
141
142     /**
143      * Performs similar test to testDTOValues except that the data transfer object
144      * is serialized to memory, deserialized, and the values in the deserialized
145      * data transfer object are checked against the original data object.
146      */

147     public void testDTOSerialization() {
148         try {
149             DataTransferObject dto = mt.getDataTransferObject();
150             assertTrue(TestSystemInitializer.getTestContext().equals(dto.getDataContext()));
151             assertTrue(MimeTypes.class.getName().equals(dto.getObjectClassName()));
152
153             DataTransferObject dto2 = runThroughSerializerTransfer(dto);
154
155             Map JavaDoc fields = dto.getTableFields();
156             checkFields(fields);
157         } catch (Exception JavaDoc ex) {
158             ex.printStackTrace();
159             fail("Exception testing field values");
160         }
161     }
162
163
164     /**
165      * Sets up the test suite by creating a mime type data object.
166      *
167      * @throws Exception upon setup error
168      */

169     protected void setUp() throws Exception JavaDoc {
170         super.setUp();
171         dataTransferObject = new DataTransferObject();
172         mt = new MimeTypes(SecuredDBObject.SYSTEM_ACCOUNT);
173         mt.setDataContext(TestSystemInitializer.getTestContext());
174         mt.setField(MimeTypes.FLD_MIMENUMBER, 1);
175         mt.setField(MimeTypes.FLD_DESCRIPTION, "Image File");
176         mt.setField(MimeTypes.FLD_FILE_EXTENSIONS, ".jpg,.png,.gif");
177     }
178
179     /**
180      * Checks to make sure field values are consistent
181      *
182      * @param fields
183      */

184     protected void checkFields(Map JavaDoc fields) {
185         assertTrue(fields.containsKey(MimeTypes.FLD_MIMENUMBER));
186         assertTrue(fields.containsKey(MimeTypes.FLD_DESCRIPTION));
187         assertTrue(fields.containsKey(MimeTypes.FLD_FILE_EXTENSIONS));
188
189         assertTrue("1".equals(fields.get(MimeTypes.FLD_MIMENUMBER)));
190         assertTrue("Image File".equals(fields.get(MimeTypes.FLD_DESCRIPTION)));
191         assertTrue(".jpg,.png,.gif".equals(fields.get(MimeTypes.FLD_FILE_EXTENSIONS)));
192     }
193
194     /**
195      * Serializes a test object to memory, a serializes into a new object
196      * and returns that new object
197      *
198      * @param testObject The object to serialize
199      * @return The deserialized object
200      * @throws java.io.IOException Upon I/O error
201      * @throws java.lang.ClassNotFoundException
202      * If one of the sub classes could not be found
203      */

204     protected DataTransferObject runThroughSerializerTransfer(DataTransferObject testObject)
205             throws java.io.IOException JavaDoc, java.lang.ClassNotFoundException JavaDoc {
206         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
207         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(bos);
208         oos.writeObject(testObject);
209
210         ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(bos.toByteArray());
211         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bis);
212
213         DataTransferObject dto2 = (DataTransferObject) ois.readObject();
214
215         return dto2;
216     }
217
218     /**
219      * Run when every test completes
220      *
221      * @throws Exception upon error
222      */

223     protected void tearDown() throws Exception JavaDoc {
224         dataTransferObject = null;
225         mt = null;
226         super.tearDown();
227     }
228 }
229
Popular Tags