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.Input; 69 import com.jcorporate.expresso.core.controller.Output; 70 import com.jcorporate.expresso.core.controller.Transition; 71 import com.jcorporate.expresso.core.misc.StringDOMParser; 72 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 73 import com.jcorporate.expresso.services.test.ExpressoTestCase; 74 import junit.framework.TestSuite; 75 import org.w3c.dom.Document ; 76 77 78 84 public class BlockTests 85 extends ExpressoTestCase { 86 Block b = null; 87 StringDOMParser parser = null; 88 static final String BlockName = "TestBlock"; 89 static final String outputName = "Output1"; 90 static final String outputContent = "Sample Content"; 91 static final String inputName1 = "Input1"; 92 static final String inputLabel1 = "Enter Your Name"; 93 static final String inputName2 = "Input2"; 94 static final String inputLabel2 = "Enter Your Login Name"; 95 static final String transitionName = "Transition1"; 96 static final String transitionLabel = "Click Here To Continue"; 97 static final String transitionController = "com.jcorporate.expresso.services.controller.DBMaint"; 98 static final String block2Name = "Block 2"; 99 100 public BlockTests(String testName) 101 throws Exception { 102 super(testName); 103 } 104 105 public static void main(String [] args) 106 throws Exception { 107 108 junit.textui.TestRunner.run(suite()); 110 } 111 112 public void setUp() 113 throws Exception { 114 b = new Block(BlockName); 115 116 Input i = new Input(inputName1, inputLabel1); 117 b.add(i); 118 i = new Input(inputName2, inputLabel2); 119 b.add(i); 120 121 Output o = new Output(outputName, outputContent); 122 b.add(o); 123 124 Transition t = new Transition(transitionName, transitionLabel, 125 transitionController); 126 b.add(t); 127 128 Block b2 = new Block(block2Name); 129 b.add(b2); 130 parser = new StringDOMParser(); 131 } 132 133 136 public static junit.framework.Test suite() 137 throws Exception { 138 return new TestSuite(BlockTests.class); 139 } 140 141 145 private Document getXMLDoc(Block block) 146 throws Exception { 147 FastStringBuffer fsb = new FastStringBuffer(128); 148 String result = block.toXML(fsb).toString(); 149 System.out.println("\nBlock Before Serialization:"); 150 System.out.println(result); 151 152 Document d = parser.parseString(result); 153 154 if (d == null) { 155 fail("Block returned mal-formed XML document"); 156 } 157 158 return d; 159 } 160 161 164 private Block getBlock(Document d) 165 throws Exception { 166 try { 167 Block anotherBlock = (Block) Block.fromXML(d); 168 FastStringBuffer fsb = new FastStringBuffer(128); 169 System.out.println("\nBlock After De-Serialization:"); 170 System.out.println(anotherBlock.toXML(fsb).toString()); 171 172 return anotherBlock; 173 } catch (NullPointerException npe) { 174 npe.printStackTrace(); 175 fail("Null Pointer Exception: " + npe); 176 177 return null; 178 } 179 } 180 181 184 public void testSimpleXML() 185 throws Exception { 186 Block simpleBlock = new Block(BlockName); 187 Document d = getXMLDoc(simpleBlock); 188 Block newBlock = getBlock(d); 189 assertTrue("getBlock returned null", newBlock != null); 190 assertTrue("Proper Name", newBlock.getName().equals(BlockName)); 191 } 192 193 196 public void testXML() 197 throws Exception { 198 Document d = getXMLDoc(b); 199 Block newBlock = getBlock(d); 200 assertTrue("getBlock returned null", newBlock != null); 201 assertTrue("Proper Name", newBlock.getName().equals(BlockName)); 202 203 try { 204 Input i; 205 Output o; 206 Transition t; 207 Block b2; 208 i = (Input) newBlock.getContent(inputName1); 209 assertTrue("i != null for " + inputName1, i != null); 210 assertTrue("i.getLabel() == " + inputLabel1, 211 i.getLabel().equals(inputLabel1)); 212 i = (Input) newBlock.getContent(inputName2); 213 assertTrue("i != null for " + inputName2, i != null); 214 assertTrue("i.getLabel() == " + inputLabel2, 215 i.getLabel().equals(inputLabel2)); 216 o = (Output) newBlock.getContent(outputName); 217 assertTrue("o != null for " + outputName, o != null); 218 assertTrue("o.getContent() == " + outputContent, 219 o.getContent().equals(outputContent)); 220 t = (Transition) newBlock.getContent(transitionName); 221 assertTrue("t != null for " + transitionName, t != null); 222 assertTrue("t.getControllerObject() == " + transitionController, 223 t.getControllerObject().equals(transitionController)); 224 assertTrue("t.getLabel() == " + transitionLabel, 225 t.getLabel().equals(transitionLabel)); 226 b2 = (Block) newBlock.getContent(block2Name); 227 assertTrue("b2 != null for " + block2Name, b2 != null); 228 } catch (ClassCastException cce) { 229 cce.printStackTrace(); 230 fail("Class Cast Exception: " + cce.getMessage()); 231 } 232 } 233 } | Popular Tags |