1 64 65 package com.jcorporate.expresso.core.controller.tests; 66 67 import com.jcorporate.expresso.core.controller.Block; 68 import com.jcorporate.expresso.core.controller.ControllerException; 69 import com.jcorporate.expresso.core.controller.ControllerRequest; 70 import com.jcorporate.expresso.core.controller.ControllerResponse; 71 import com.jcorporate.expresso.core.controller.Input; 72 import com.jcorporate.expresso.core.controller.Output; 73 import com.jcorporate.expresso.core.controller.Transition; 74 import com.jcorporate.expresso.core.controller.session.SimplePersistentSession; 75 import com.jcorporate.expresso.core.misc.StringDOMParser; 76 import com.jcorporate.expresso.services.test.CommandLineParser; 77 import com.jcorporate.expresso.services.test.ExpressoTestCase; 78 import junit.framework.TestSuite; 79 import org.w3c.dom.Document ; 80 81 import java.io.IOException ; 82 83 84 92 public class ControllerResponseTests 93 extends ExpressoTestCase { 94 ControllerResponse cr = null; 95 StringDOMParser parser = null; 96 static final String controllerName = "com.jcorporate.expresso.services.controller.DBMaint"; 97 static final String responseTitle = "Sample Response"; 98 static final String responseStyle = "xml??"; 99 static final String BlockName = "TestBlock"; 100 static final String outputName = "Output1"; 101 static final String outputContent = "Sample Content"; 102 static final String inputName1 = "Input1"; 103 static final String inputLabel1 = "Enter Your Name"; 104 static final String inputName2 = "Input2"; 105 static final String inputLabel2 = "Enter Your Login Name"; 106 static final String transitionName = "Transition1"; 107 static final String transitionLabel = "Click Here To Continue"; 108 static final String transitionController = "com.jcorporate.expresso.services.controller.DBMaint"; 109 static final String block2Name = "Block 2"; 110 111 public ControllerResponseTests(String testName) 112 throws Exception { 113 super(testName); 114 } 115 116 public static void main(String [] args) 117 throws Exception { 118 119 CommandLineParser.parseCommandLine(args); 121 junit.textui.TestRunner.run(suite()); 122 } 123 124 public void setUp() 125 throws Exception { 126 127 128 cr = new ControllerResponse(); 129 cr.setTitle(responseTitle); 130 cr.setControllerClass(controllerName); 131 cr.setStyle(responseStyle); 132 SimplePersistentSession session = new SimplePersistentSession(); 133 ControllerRequest request = new ControllerRequest(); 134 request.setUid(3); 135 request.setSession(session); 136 cr.setRequest(request); 137 138 Block b = new Block(BlockName); 139 Input i = new Input(inputName1, inputLabel1); 140 b.add(i); 141 i = new Input(inputName2, inputLabel2); 142 b.add(i); 143 144 Output o = new Output(outputName, outputContent); 145 b.add(o); 146 147 Transition t = new Transition(transitionName, transitionLabel, 148 transitionController); 149 b.add(t); 150 cr.addBlock(b); 151 152 Block b2 = new Block(block2Name); 153 cr.addBlock(b2); 154 155 cr.addInput(i); 160 cr.addOutput(o); 161 cr.addTransition(t); 162 parser = new StringDOMParser(); 163 } 164 165 168 public static junit.framework.Test suite() 169 throws Exception { 170 return new TestSuite(ControllerResponseTests.class); 171 } 172 173 177 private Document getXMLDoc() 178 throws Exception { 179 String result = cr.toXML(); 180 181 Document d = parser.parseString(result); 184 185 if (d == null) { 186 fail("ControllerResponse returned mal-formed XML document"); 187 } 188 189 return d; 190 } 191 192 195 private ControllerResponse getControllerResponse(Document d) 196 throws Exception { 197 ControllerResponse cr1 = ControllerResponse.fromXML(d); 198 System.out.println("\nController Response After De-Serialization:"); 199 System.out.println(cr1.toXML()); 200 201 return cr1; 202 } 203 204 211 public void testXML() 212 throws Exception { 213 try { 214 Document d = getXMLDoc(); 215 ControllerResponse cr1 = getControllerResponse(d); 216 assertTrue("cr1 returned null", cr1 != null); 217 assertTrue("Block 1 Exists", cr1.getBlock(BlockName) != null); 218 assertTrue("Block 2 Exists", cr1.getBlock(block2Name) != null); 219 assertTrue("Proper Controller Name", 220 cr1.getControllerClass().equals(controllerName)); 221 assertTrue("Proper Style", cr1.getStyle().equals(responseStyle)); 222 assertTrue("Proper Title", cr1.getTitle().equals(responseTitle)); 223 assertTrue("Input 2 Exists", cr1.getInput(inputName2) != null); 224 assertTrue("Output 1 Exists", cr1.getOutput(outputName) != null); 225 assertTrue("Transition 1 Exists", 226 cr1.getTransition(transitionName) != null); 227 } catch (Exception ex) { 228 ex.printStackTrace(); 229 fail("Caught Exception: " + ex.getMessage()); 230 } 231 } 232 233 public void testSerialization() { 234 try { 235 ControllerResponse response = new ControllerResponse(); 236 ControllerRequest request = new ControllerRequest(); 237 response.setRequest(request); 238 request.setDataContext("default"); 239 request.setSession(new com.jcorporate.expresso.core.controller.session.SimplePersistentSession()); 240 java.io.ByteArrayOutputStream bos = new java.io. 241 ByteArrayOutputStream(); 242 java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream (bos); 243 oos.writeObject(response); 244 oos.flush(); 245 oos.close(); 246 java.io.ByteArrayInputStream bis = new java.io.ByteArrayInputStream (bos.toByteArray()); 247 java.io.ObjectInputStream ois = new java.io.ObjectInputStream (bis); 248 response = (ControllerResponse) ois.readObject(); 249 250 251 } catch (IOException ex) { 252 ex.printStackTrace(); 253 fail("IO Exception testing serialization " + ex.getMessage()); 254 } catch (ClassNotFoundException ex) { 255 ex.printStackTrace(); 256 fail("ClassNotFoundException testing serialization " + ex.getMessage()); 257 258 } catch (ControllerException ex) { 259 ex.printStackTrace(); 260 fail("ClassNotFoundException testing serialization " + ex.getMessage()); 261 } 262 263 } 264 } | Popular Tags |