1 /* 2 * Copyright (c) 2003 3 * Information Desire GmbH 4 * All rights reserved. 5 */ 6 package com.infodesire.infobit; 7 8 import com.infodesire.infobit.data.Acl; 9 import com.infodesire.infobit.data.Infobit; 10 11 import com.infodesire.infobit.external.InfobitExporter; 12 import com.infodesire.infobit.external.InfobitImporter; 13 14 import com.infodesire.infobit.external.impl.ImporterImpl; 15 // import com.infodesire.infobit.external.impl.VelocityExporter; 16 import com.infodesire.infobit.external.impl.SimpleExporter; 17 18 import junit.framework.TestCase; 19 import junit.framework.TestSuite; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import java.io.BufferedReader; 25 import java.io.IOException; 26 import java.io.InputStream; 27 import java.io.InputStreamReader; 28 import java.io.Reader; 29 import java.io.StringReader; 30 import java.io.StringWriter; 31 32 import java.util.ArrayList; 33 import java.util.Collection; 34 import java.util.HashMap; 35 import java.util.HashSet; 36 import java.util.List; 37 import java.util.Map; 38 import java.util.Set; 39 import java.util.TreeMap; 40 41 /*** 42 * Test cases asserting the XML representation of infobits 43 * 44 * @author peter 45 * @created August 27, 2003 46 * @version $Revision: 1.2 $ 47 */ 48 public class XMLExportTest extends XMLTestBase { 49 50 /*** 51 * Initializes the test cases. 52 * 53 * @param name Test name 54 */ 55 public XMLExportTest(String name) { 56 super(name); 57 } 58 59 60 /*** 61 * Common test case fixture. 62 * 63 * @exception Exception Description of Exception 64 */ 65 public void setUp() throws Exception { 66 super.setUp(); 67 } 68 69 70 /*** 71 * Common teardown of test case fixtures. 72 * 73 * @exception Exception Description of Exception 74 */ 75 public void tearDown() throws Exception { 76 } 77 78 79 /*** 80 * Checks the externalization of an infobit with no version attached. 81 * 82 * @exception Exception Generic error 83 */ 84 public void testEmptyInfobitOutput() throws Exception { 85 Infobit ib = prepareEmptyInfobit("EM001"); 86 List infobits = new ArrayList(); 87 infobits.add(ib); 88 89 String expPath = "/com/infodesire/infobit/external/emptyInfobit.xml"; 90 InputStream exp = getClass().getResourceAsStream(expPath); 91 performExportTest("testEmptyInfobitOutput", infobits, exp); 92 } 93 94 95 /*** 96 * Checks the externalization of an infobit with multiple (text content) 97 * versions attached. 98 * 99 * @exception Exception Description of Exception 100 */ 101 public void testMultiVersionInfobitOutput() throws Exception { 102 Infobit ib = prepareMultiVersionTextInfobit("IB011"); 103 List export = new ArrayList(); 104 export.add(ib); 105 106 String expPath = 107 "/com/infodesire/infobit/external/multiVersionInfobit.xml"; 108 InputStream exp = getClass().getResourceAsStream(expPath); 109 performExportTest("testMultiVersionInfobitOutput", export, exp); 110 } 111 112 113 /*** 114 * Checks the externalization of an infobit with a binary content version 115 * attached. 116 * 117 * @exception Exception Description of Exception 118 */ 119 public void xxtestBinaryContentInfobitOutput() throws Exception { 120 Infobit ib = prepareBinaryInfobit("IB023"); 121 List export = new ArrayList(); 122 export.add(ib); 123 124 String expPath = 125 "/com/infodesire/infobit/external/binaryContentInfobit.xml"; 126 InputStream expected = getClass().getResourceAsStream(expPath); 127 performExportTest("testBinaryContentInfobitOutput", export, expected); 128 } 129 130 131 /*** 132 * Checks the XML representation of multiple unrelated infobits. 133 * 134 * @exception Exception Description of Exception 135 */ 136 public void xxtestMultipleInfobitsOutput() throws Exception { 137 Infobit eib = prepareEmptyInfobit("EM001"); 138 Infobit tib = prepareMultiVersionTextInfobit("TX001"); 139 Infobit bib = prepareBinaryInfobit("BI001"); 140 141 List export = new ArrayList(); 142 export.add(eib); 143 export.add(tib); 144 export.add(bib); 145 146 String expPath = 147 "/com/infodesire/infobit/external/multipleInfobits.xml"; 148 InputStream expected = getClass().getResourceAsStream(expPath); 149 performExportTest("testMulitpleInfobitOutput", export, expected); 150 } 151 152 153 /*** 154 * Checks the XML representation of a template content infobit. 155 * 156 * @exception Exception Description of Exception 157 */ 158 public void testTemplateInfobitOutput() throws Exception { 159 Infobit ib = prepareTemplateInfobit("TM001"); 160 List export = new ArrayList(); 161 export.add(ib); 162 163 String expPath = 164 "/com/infodesire/infobit/external/templateInfobit.xml"; 165 InputStream expected = getClass().getResourceAsStream(expPath); 166 performExportTest("testTemplateInfobitOutput", export, expected); 167 } 168 169 170 /*** 171 * Checks the XML representation of a set of script with its referred 172 * template infobit. 173 * 174 * @exception Exception Description of Exception 175 */ 176 public void testScriptSetOutput() throws Exception { 177 Map links = new TreeMap(); 178 // keep order stable to enable text-based testing 179 180 Infobit ref01 = prepareMultiVersionTextInfobit("TX011"); 181 Infobit ref02 = prepareEmptyInfobit("EM011"); 182 links.put("ref01", ref01); 183 links.put("ref02", ref02); 184 185 Infobit tib = prepareTemplateInfobit("TM002"); 186 Infobit sib = prepareScriptInfobit("SC001", tib, links); 187 188 List export = new ArrayList(); 189 export.add(sib); 190 export.add(tib); 191 export.add(ref01); 192 export.add(ref02); 193 194 String expPath = "/com/infodesire/infobit/external/scriptSet.xml"; 195 InputStream expected = getClass().getResourceAsStream(expPath); 196 performExportTest("testScriptOutput", export, expected); 197 } 198 199 200 /*** 201 * Creates an infobit with no versions defined. 202 * 203 * @param name The name of the infobit to create 204 * @return Description of the Returned Value 205 * @exception Exception Description of Exception 206 */ 207 private Infobit prepareEmptyInfobit(String name) throws Exception { 208 Infobit ib = _manager.createInfobit(name, _defaultAcl); 209 return ib; 210 } 211 212 213 /*** 214 * Creates an infobit with multiple text versions set. 215 * 216 * @param name Description of Parameter 217 * @return Description of the Returned Value 218 * @exception Exception Description of Exception 219 */ 220 private Infobit prepareMultiVersionTextInfobit(String name) 221 throws Exception { 222 223 Infobit ib = _manager.createInfobit(name, _defaultAcl); 224 Set keywords = new HashSet(); 225 226 String text = "This is the first version of the content" + 227 System.getProperty("line.separator"); 228 _manager.createText(ib, "1", text, keywords); 229 230 text = "This is the second version of the content" + 231 System.getProperty("line.separator"); 232 _manager.createText(ib, "2", text, keywords); 233 234 return ib; 235 } 236 237 238 /*** 239 * Creates an infobit of binary content. 240 * 241 * @param name Description of Parameter 242 * @return Description of the Returned Value 243 * @exception Exception Description of Exception 244 */ 245 private Infobit prepareBinaryInfobit(String name) throws Exception { 246 Infobit ib = _manager.createInfobit(name, _defaultAcl); 247 String dataPath = "/com/infodesire/infobit/external/undo.pbm"; 248 InputStream data = getClass().getResourceAsStream(dataPath); 249 Set keywords = new HashSet(); 250 _manager.createBinary(ib, "V1.0", data, "image/pnm", keywords); 251 252 return ib; 253 } 254 255 256 /*** 257 * Creates an infobit of tmeplate content. 258 * 259 * @param name Description of Parameter 260 * @return Description of the Returned Value 261 * @exception Exception Description of Exception 262 */ 263 private Infobit prepareTemplateInfobit(String name) throws Exception { 264 Infobit ib = _manager.createInfobit(name, _defaultAcl); 265 266 String templPath = "/com/infodesire/infobit/external/template.xml"; 267 InputStream is = getClass().getResourceAsStream(templPath); 268 InputStreamReader r = new InputStreamReader(is); 269 StringBuffer tmpl = new StringBuffer(); 270 char[] buf = new char[512]; 271 int n; 272 while ((n = r.read(buf)) > -1) { 273 tmpl.append(buf, 0, n); 274 } 275 r.close(); 276 277 _manager.createTemplate(ib, "1.0", tmpl.toString()); 278 279 return ib; 280 } 281 282 283 /*** 284 * Creates an infobit with a template bound as most recent version. 285 * 286 * @param name Name of the infobit to create 287 * @param tmpl The bound template 288 * @param links The links to pass... 289 * @return Description of the Returned Value 290 * @exception Exception Description of Exception 291 */ 292 private Infobit prepareScriptInfobit(String name, Infobit tmpl, 293 Map links) throws Exception { 294 295 Infobit sib = _manager.createInfobit(name, _defaultAcl); 296 297 Set keywords = new HashSet(); 298 _manager.createScript(sib, "Version 1.1", tmpl, links, keywords); 299 300 return sib; 301 } 302 303 304 /*** 305 * Drives a test consisting in the export of the specified set of infobits. 306 * The test is validated by comparing the resulting XML document to a 307 * particular pattern (thereby ignoring leading and trailing whitespace and 308 * empty lines). 309 * 310 * @param tag Some tag added to diagnositc messages 311 * @param infobits The infobits to export. Collection of {@link 312 * Infobit infobits}. 313 * @param expected The XML docuement expected as as external 314 * representation of <code>infobits</code>. 315 * @exception Exception Description of Exception 316 */ 317 private void performExportTest(String tag, 318 List infobits, InputStream expected) 319 throws Exception { 320 321 StringWriter sw = new StringWriter(); 322 _exporter.export(infobits, sw); 323 sw.close(); 324 String doc = sw.toString(); 325 326 // DEBUG 327 System.out.println(tag); 328 System.out.println(doc); 329 330 Reader srd = new StringReader(doc); 331 BufferedReader brd = new BufferedReader(srd); 332 333 Reader sre = new InputStreamReader(expected); 334 BufferedReader bre = new BufferedReader(sre); 335 checkDocument(brd, bre); 336 } 337 }

This page was automatically generated by Maven