1 64 65 package com.jcorporate.expresso.core.controller.tests; 66 67 import com.jcorporate.expresso.core.controller.Output; 68 import com.jcorporate.expresso.core.misc.StringDOMParser; 69 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 70 import com.jcorporate.expresso.services.test.CommandLineParser; 71 import com.jcorporate.expresso.services.test.ExpressoTestCase; 72 import junit.framework.TestSuite; 73 import org.w3c.dom.Document ; 74 75 76 83 public class OutputTests 84 extends ExpressoTestCase { 85 Output o = null; 86 StringDOMParser parser = null; 87 static final String outputName = "Test Output"; 88 static final String outputContent = "This is a test output description"; 89 static final String escapedOutputContent = "This & That \" And \" something else too;"; 90 static final int outputDisplayLength = 30; 91 92 public OutputTests(String testName) 93 throws Exception { 94 super(testName); 95 } 96 97 public static void main(String [] args) 98 throws Exception { 99 100 CommandLineParser.parseCommandLine(args); 102 junit.textui.TestRunner.run(suite()); 103 } 104 105 public void setUp() 106 throws Exception { 107 o = new Output(outputName, outputContent); 108 o.setDisplayLength(outputDisplayLength); 109 o.setAlignment("left"); 110 parser = new StringDOMParser(); 111 } 112 113 116 public static junit.framework.Test suite() 117 throws Exception { 118 return new TestSuite(OutputTests.class); 119 } 120 121 125 private Document getXMLDoc() 126 throws Exception { 127 FastStringBuffer fsb = new FastStringBuffer(128); 128 String result = o.toXML(fsb).toString(); 129 System.out.println("\nOutput Before Serialization:"); 130 System.out.println(result); 131 132 Document d = parser.parseString(result); 133 134 if (d == null) { 135 fail("Output returned mal-formed XML document"); 136 } 137 138 return d; 139 } 140 141 144 private Output getOutput(Document d) 145 throws Exception { 146 Output anotherOutput = (Output) Output.fromXML(d); 147 FastStringBuffer fsb = new FastStringBuffer(128); 148 System.out.println("\nOutput After De-Serialization:"); 149 System.out.println(anotherOutput.toXML(fsb).toString()); 150 151 return anotherOutput; 152 } 153 154 157 public void testXML() 158 throws Exception { 159 Document d = getXMLDoc(); 160 Output newOutput = getOutput(d); 161 assertTrue("getOutput returned null", newOutput != null); 162 assertTrue("Proper Display Length", newOutput.getDisplayLength() == 30); 163 assertTrue("Proper Name", newOutput.getName().equals(outputName)); 164 assertTrue("Proper Content", 165 newOutput.getContent().equals(outputContent)); 166 } 167 168 171 public void testXmlWithSpecialCharacters() 172 throws Exception { 173 o.setContent(escapedOutputContent); 174 175 Document d = getXMLDoc(); 176 Output newOutput = getOutput(d); 177 assertTrue("getOutput returned null", newOutput != null); 178 assertTrue("Proper Display Length", newOutput.getDisplayLength() == 30); 179 assertTrue("Proper Name", newOutput.getName().equals(outputName)); 180 assertTrue("Proper Content", 181 newOutput.getContent().equals(escapedOutputContent)); 182 } 183 184 187 public void testXMLWithNestedOutput() 188 throws Exception { 189 Output nested = new Output("nested1", "label1"); 190 o.addNested(nested); 191 nested = new Output("nested2", "label2"); 192 o.addNested(nested); 193 194 Document d = getXMLDoc(); 195 Output newOutput = getOutput(d); 196 assertTrue("getOutput returned null", newOutput != null); 197 assertTrue("Proper Display Length", newOutput.getDisplayLength() == 30); 198 assertTrue("Proper Name", newOutput.getName().equals(outputName)); 199 assertTrue("Proper Content", 200 newOutput.getContent().equals(outputContent)); 201 nested = (Output) newOutput.getNested("nested1"); 202 assertTrue("Got nested 1", nested != null); 203 assertTrue("Proper Nested 1 Content", 204 nested.getContent().equals("label1")); 205 nested = (Output) newOutput.getNested("nested2"); 206 assertTrue("Got nested 2", nested != null); 207 assertTrue("Proper Nested 2 Content", 208 nested.getContent().equals("label2")); 209 } 210 } | Popular Tags |