1 /* 2 * Copyright (c) 2003 3 * Information Desire GmbH 4 * All rights reserved. 5 */ 6 package com.infodesire.infobit; 7 8 import junit.framework.TestCase; 9 import junit.framework.TestSuite; 10 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 14 import java.util.ArrayList; 15 import java.util.HashMap; 16 import java.util.HashSet; 17 import java.util.Iterator; 18 import java.util.List; 19 import java.util.Map; 20 import java.util.Set; 21 import java.util.Properties; 22 import java.util.Collections; 23 24 import com.infodesire.infobit.data.Infobit; 25 import com.infodesire.infobit.data.Acl; 26 import com.infodesire.infobit.data.Version; 27 import com.infodesire.infobit.data.Content; 28 import com.infodesire.infobit.data.Script; 29 import com.infodesire.infobit.data.Template; 30 import com.infodesire.infobit.data.TextContent; 31 32 import com.infodesire.infobit.render.velocity.ServletVelocityRenderer; 33 34 import java.io.StringWriter; 35 import java.io.OutputStreamWriter; 36 import java.io.InputStream; 37 38 /*** 39 * test cases for infobit templates 40 * 41 * @author peter2 42 * @created August 21, 2003 43 * @version $Revision: 1.11 $ 44 */ 45 public class TemplateTest extends TestCase { 46 // Cannot derive from InfobitTestBase because _configuration is needed 47 // in constructor. 48 49 private static Log _log = LogFactory.getLog(TemplateTest.class); 50 private final static String TEMPLATE_VERSION = "template_one"; 51 private final static String TEMPLATE_TEXT = 52 "This text links #fuck #infobit($target_one), #infobit($target_two) and #infobit($target_three)."; 53 private final static String SCRIPT_VERSION = "script_one"; 54 55 private final static String DEEP_TEMPLATE = "{#infobit($one)|#infobit($two)|#infobit($three)|#infobit($four)|#infobit($five)|#infobit($six)|#infobit($seven)|#infobit($eight)|#infobit($nine)|#infobit($ten)}"; 56 private static String[] DEEP_LINKS = {"one", "two", "three", "four", 57 "five", "six", "seven", "eight", "nine", "ten"}; 58 59 /*** 60 * The configuration applicable for the test case 61 */ 62 protected InfobitConfiguration _configuration; 63 64 InfobitPool _pool; 65 Acl _defaultAcl; 66 InfobitManager _manager; 67 68 69 /*** 70 * Constructor for the TemplateTest object 71 * 72 * @param name ID of the testcase 73 * @exception Exception Generic failure 74 */ 75 public TemplateTest(String name) throws Exception { 76 super(name); 77 } 78 79 80 /*** 81 * The JUnit setup method 82 * 83 * @exception Exception Description of Exception 84 */ 85 public void setUp() throws Exception { 86 super.setUp(); 87 _configuration = new InfobitConfiguration("infobit-config.xml"); 88 89 _pool = _configuration.getPool("pool_one"); 90 _defaultAcl = _configuration.getPool("superuser_pool").getAclManager().createAcl("default"); 91 _manager = _pool.getInfobitManager(); 92 93 } 94 95 96 97 /*** 98 * A unit test for JUnit 99 * 100 * @exception Exception Description of Exception 101 */ 102 public void testTemplateRendering() throws Exception { 103 104 String tid = "DE1213434545677"; 105 Infobit templateIbit = _manager.createInfobit(tid, _defaultAcl); 106 _manager.setActualVersion(templateIbit, _manager.createTemplate(templateIbit, TEMPLATE_VERSION, 107 TEMPLATE_TEXT)); 108 Set keywords = new HashSet(); 109 110 Infobit targetOne = _manager.createInfobit("TG001", _defaultAcl); 111 _manager.setActualVersion(targetOne, _manager.createText(targetOne, "1.0", "Target One", keywords)); 112 113 Infobit targetTwo = _manager.createInfobit("TG002", _defaultAcl); 114 _manager.setActualVersion(targetTwo, _manager.createText(targetTwo, "1.0", "Target Two", keywords)); 115 116 Infobit targetThree = _manager.createInfobit("TG003", _defaultAcl); 117 _manager.setActualVersion(targetThree, _manager.createText(targetThree, "1.0", "Target Three", keywords)); 118 119 HashMap links = new HashMap(); 120 links.put("target_one", targetOne); 121 links.put("target_two", targetTwo); 122 links.put("target_three", targetThree); 123 124 String sid = "SI12345"; 125 Infobit scriptInfobit = _manager.createInfobit(sid, _defaultAcl); 126 127 _manager.setActualVersion(scriptInfobit, _manager.createScript(scriptInfobit, SCRIPT_VERSION, templateIbit, 128 links, keywords)); 129 _pool.flush(); 130 131 ServletVelocityRenderer vr = new ServletVelocityRenderer(); 132 133 vr.setManager(_manager); 134 vr.init(new Properties()); 135 136 StringWriter sw = new StringWriter(); 137 vr.render("SI12345", sw, null); 138 sw.flush(); 139 System.out.println("|" + sw.toString() + "|"); 140 assertEquals(sw.toString(), "This text links #fuck Target One, Target Two and Target Three."); 141 } 142 143 144 145 /*** 146 * A unit test for JUnit 147 * 148 * @exception Exception Description of Exception 149 */ 150 public void testBigTemplate() throws Exception { 151 152 Infobit ibitI; 153 Infobit ibitJ; 154 Infobit ibitK; 155 Infobit start; 156 HashSet keywords = new HashSet(); 157 158 long now = System.currentTimeMillis(); 159 160 Infobit template = _manager.createInfobit("temp", _defaultAcl); 161 _manager.setActualVersion(template, _manager.createTemplate(template, "blurge", DEEP_TEMPLATE)); 162 163 start = _manager.createInfobit("start", _defaultAcl); 164 HashMap imap = new HashMap(); 165 for (int i = 0; i < DEEP_LINKS.length; i++) { 166 HashMap jmap = new HashMap(); 167 for (int j = 0; j < DEEP_LINKS.length; j++) { 168 HashMap kmap = new HashMap(); 169 for (int k = 0; k < DEEP_LINKS.length; k++) { 170 ibitK = _manager.createInfobit("ijk" + (i * 100 + j * 10 + k), _defaultAcl); 171 _manager.setActualVersion(ibitK, _manager.createText(ibitK, "blurge", "{ijk" + (i * 100 + j * 10 + k) + "}", keywords)); 172 kmap.put(DEEP_LINKS[k], ibitK); 173 } 174 ibitJ = _manager.createInfobit("ij" + (i * 10 + j), _defaultAcl); 175 _manager.setActualVersion(ibitJ, _manager.createScript(ibitJ, "blam", template, kmap, keywords)); 176 jmap.put(DEEP_LINKS[j], ibitJ); 177 178 } 179 ibitI = _manager.createInfobit("i" + i, _defaultAcl); 180 _manager.setActualVersion(ibitI, _manager.createScript(ibitI, "blam", template, jmap, keywords)); 181 imap.put(DEEP_LINKS[i], ibitI); 182 } 183 _manager.setActualVersion(start, _manager.createScript(start, "blam", template, imap, keywords)); 184 _pool.flush(); 185 now = System.currentTimeMillis() - now; 186 _log.info("creating 1110 infobits took : " + now + "msec"); 187 OutputStreamWriter bw = new OutputStreamWriter(System.out); 188 ServletVelocityRenderer vr = new ServletVelocityRenderer(); 189 vr.setManager(_manager); 190 vr.init(new Properties()); 191 192 now = System.currentTimeMillis(); 193 vr.render("start", bw, null); 194 bw.flush(); 195 now = System.currentTimeMillis() - now; 196 _log.info("rendering 1110 infobits took : " + now + "msec"); 197 } 198 199 200 /*** 201 * test template rendering of binary infobits 202 * 203 * @exception Exception Description of Exception 204 */ 205 public void testBinaryInfobitRendering() throws Exception { 206 Infobit ib = _manager.createInfobit("binary_test", _defaultAcl); 207 String dataPath = "/com/infodesire/infobit/external/undo.pbm"; 208 InputStream data = getClass().getResourceAsStream(dataPath); 209 _manager.setActualVersion(ib, _manager.createBinary(ib, "blurge", data, "image/pnm", Collections.EMPTY_SET)); 210 Content content = ib.getActualVersion().getContent(); 211 _manager.setAttribute(content, "width", "120"); 212 _manager.setAttribute(content, "height", "230"); 213 _pool.flush(); 214 // setup rendering 215 HashMap map = new HashMap(); 216 map.put("_infobit_binary_prefix", "/foo/bar/baz"); 217 map.put("_session_id", "1234567890"); 218 ServletVelocityRenderer vr = new ServletVelocityRenderer(); 219 vr.setManager(_manager); 220 vr.init(new Properties()); 221 222 StringWriter sw = new StringWriter(); 223 224 vr.render("binary_test", sw, map); 225 226 sw.flush(); 227 System.out.println("|" + sw.toString() + "|"); 228 } 229 230 } 231

This page was automatically generated by Maven