/* ====================================================================
* The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
*
* Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by Jcorporate Ltd.
* (http://www.jcorporate.com/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. "Jcorporate" and product names such as "Expresso" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written permission,
* please contact info@jcorporate.com.
*
* 5. Products derived from this software may not be called "Expresso",
* or other Jcorporate product names; nor may "Expresso" or other
* Jcorporate product names appear in their name, without prior
* written permission of Jcorporate Ltd.
*
* 6. No product derived from this software may compete in the same
* market space, i.e. framework, without prior written permission
* of Jcorporate Ltd. For written permission, please contact
* partners@jcorporate.com.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Jcorporate Ltd. Contributions back
* to the project(s) are encouraged when you make modifications.
* Please send them to support@jcorporate.com. For more information
* on Jcorporate Ltd. and its products, please see
* <http://www.jcorporate.com/>.
*
* Portions of this software are based upon other open source
* products and are subject to their respective licenses.
*/
package com.jcorporate.expresso.core.dataobjects.test;
import com.jcorporate.expresso.core.dataobjects.DataTransferObject;
import com.jcorporate.expresso.core.db.DBException;
import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
import com.jcorporate.expresso.services.dbobj.MimeTypes;
import com.jcorporate.expresso.services.test.ExpressoTestCase;
import com.jcorporate.expresso.services.test.TestSystemInitializer;
import junit.framework.TestSuite;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Map;
/**
* Tests various aspects of the DataTransferObject
*
* @author Michael Rimov
* @version 1.0
*/
public class TestDataTransferObject extends ExpressoTestCase {
DataTransferObject dataTransferObject = null;
MimeTypes mt = null;
/**
* Creates a new TestDataTransferObject object.
*
* @param testName The name of the test
* @throws Exception Upon initialization error
*/
public TestDataTransferObject(String testName) throws Exception {
super(testName);
}
/**
* A main function to allow testing of this class only
*
* @param args Unused
* @throws Exception If an error occurs during testing
*/
public static void main(String[] args) throws Exception {
junit.textui.TestRunner.run(suite());
}
/**
* Constructs the test suite
*
* @return An instantiated Junit test suite
* @throws Exception Upon error
*/
public static junit.framework.Test suite() throws Exception {
return new TestSuite(TestDataTransferObject.class);
}
/**
* Checks the data transfer object by verifying that the values
* set in the data object are consistent with those that appear in the data
* transfer object
*/
public void testDTOValues() {
try {
DataTransferObject dto = mt.getDataTransferObject();
assertTrue(TestSystemInitializer.getTestContext().equals(dto.getDataContext()));
assertTrue(MimeTypes.class.getName().equals(dto.getObjectClassName()));
Map fields = dto.getTableFields();
checkFields(fields);
} catch (DBException ex) {
ex.printStackTrace();
fail("Exception testing field values");
}
}
/**
* Performs similar test to testDTOValues except that the data transfer object
* is serialized to memory, deserialized, and the values in the deserialized
* data transfer object are checked against the original data object.
*/
public void testDTOSerialization() {
try {
DataTransferObject dto = mt.getDataTransferObject();
assertTrue(TestSystemInitializer.getTestContext().equals(dto.getDataContext()));
assertTrue(MimeTypes.class.getName().equals(dto.getObjectClassName()));
DataTransferObject dto2 = runThroughSerializerTransfer(dto);
Map fields = dto.getTableFields();
checkFields(fields);
} catch (Exception ex) {
ex.printStackTrace();
fail("Exception testing field values");
}
}
/**
* Sets up the test suite by creating a mime type data object.
*
* @throws Exception upon setup error
*/
protected void setUp() throws Exception {
super.setUp();
dataTransferObject = new DataTransferObject();
mt = new MimeTypes(SecuredDBObject.SYSTEM_ACCOUNT);
mt.setDataContext(TestSystemInitializer.getTestContext());
mt.setField(MimeTypes.FLD_MIMENUMBER, 1);
mt.setField(MimeTypes.FLD_DESCRIPTION, "Image File");
mt.setField(MimeTypes.FLD_FILE_EXTENSIONS, ".jpg,.png,.gif");
}
/**
* Checks to make sure field values are consistent
*
* @param fields
*/
protected void checkFields(Map fields) {
assertTrue(fields.containsKey(MimeTypes.FLD_MIMENUMBER));
assertTrue(fields.containsKey(MimeTypes.FLD_DESCRIPTION));
assertTrue(fields.containsKey(MimeTypes.FLD_FILE_EXTENSIONS));
assertTrue("1".equals(fields.get(MimeTypes.FLD_MIMENUMBER)));
assertTrue("Image File".equals(fields.get(MimeTypes.FLD_DESCRIPTION)));
assertTrue(".jpg,.png,.gif".equals(fields.get(MimeTypes.FLD_FILE_EXTENSIONS)));
}
/**
* Serializes a test object to memory, a serializes into a new object
* and returns that new object
*
* @param testObject The object to serialize
* @return The deserialized object
* @throws java.io.IOException Upon I/O error
* @throws java.lang.ClassNotFoundException
* If one of the sub classes could not be found
*/
protected DataTransferObject runThroughSerializerTransfer(DataTransferObject testObject)
throws java.io.IOException, java.lang.ClassNotFoundException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(testObject);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
DataTransferObject dto2 = (DataTransferObject) ois.readObject();
return dto2;
}
/**
* Run when every test completes
*
* @throws Exception upon error
*/
protected void tearDown() throws Exception {
dataTransferObject = null;
mt = null;
super.tearDown();
}
}
|