1 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 ; 76 import java.io.ByteArrayOutputStream ; 77 import java.io.ObjectInputStream ; 78 import java.io.ObjectOutputStream ; 79 import java.util.Map ; 80 81 82 88 public class TestDataTransferObject extends ExpressoTestCase { 89 DataTransferObject dataTransferObject = null; 90 MimeTypes mt = null; 91 92 98 public TestDataTransferObject(String testName) throws Exception { 99 super(testName); 100 } 101 102 108 public static void main(String [] args) throws Exception { 109 junit.textui.TestRunner.run(suite()); 110 } 111 112 118 public static junit.framework.Test suite() throws Exception { 119 return new TestSuite(TestDataTransferObject.class); 120 } 121 122 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 fields = dto.getTableFields(); 134 checkFields(fields); 135 } catch (DBException ex) { 136 ex.printStackTrace(); 137 fail("Exception testing field values"); 138 } 139 } 140 141 142 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 fields = dto.getTableFields(); 156 checkFields(fields); 157 } catch (Exception ex) { 158 ex.printStackTrace(); 159 fail("Exception testing field values"); 160 } 161 } 162 163 164 169 protected void setUp() throws Exception { 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 184 protected void checkFields(Map 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 204 protected DataTransferObject runThroughSerializerTransfer(DataTransferObject testObject) 205 throws java.io.IOException , java.lang.ClassNotFoundException { 206 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 207 ObjectOutputStream oos = new ObjectOutputStream (bos); 208 oos.writeObject(testObject); 209 210 ByteArrayInputStream bis = new ByteArrayInputStream (bos.toByteArray()); 211 ObjectInputStream ois = new ObjectInputStream (bis); 212 213 DataTransferObject dto2 = (DataTransferObject) ois.readObject(); 214 215 return dto2; 216 } 217 218 223 protected void tearDown() throws Exception { 224 dataTransferObject = null; 225 mt = null; 226 super.tearDown(); 227 } 228 } 229 | Popular Tags |