1 16 17 package org.apache.cocoon.components.treeprocessor.variables; 18 19 import java.util.HashMap ; 20 import java.util.Map ; 21 22 import org.apache.cocoon.SitemapComponentTestCase; 23 import org.apache.cocoon.components.treeprocessor.InvokeContext; 24 import org.apache.cocoon.environment.mock.MockRequest; 25 import org.apache.cocoon.sitemap.PatternException; 26 27 33 public class PreparedVariableResolverTestCase extends SitemapComponentTestCase { 34 35 public void testNestedExpressions() throws Exception { 36 String expr = "{request-param:{request-param:foo}}"; 37 MockRequest request = getRequest(); 38 request.reset(); 39 request.addParameter("foo", "bar"); 40 request.addParameter("bar", "123"); 41 InvokeContext context = new InvokeContext(true); 42 context.enableLogging(getLogger()); 43 44 Map sitemapElements = new HashMap (); 45 context.pushMap("sitemap", sitemapElements); 46 PreparedVariableResolver resolver = new PreparedVariableResolver(expr, getManager()); 47 assertEquals("123", resolver.resolve(context, getObjectModel())); 48 } 49 50 public void testNestedModuleAndSitemapExpressions() throws Exception { 51 String expr = "{request-param:f{1}}"; 52 MockRequest request = getRequest(); 53 request.reset(); 54 request.addParameter("foo", "123"); 55 InvokeContext context = new InvokeContext(true); 56 context.enableLogging(getLogger()); 57 58 Map sitemapElements = new HashMap (); 59 sitemapElements.put("1", "oo"); 60 context.pushMap("sitemap", sitemapElements); 61 PreparedVariableResolver resolver = new PreparedVariableResolver(expr, getManager()); 62 assertEquals("123", resolver.resolve(context, getObjectModel())); 63 } 64 65 public void testAnchors() throws PatternException { 66 String expr = "{#label:name}"; 67 68 InvokeContext context = new InvokeContext(true); 69 context.enableLogging(getLogger()); 70 71 Map sitemapElements = new HashMap (); 72 sitemapElements.put("name", "123"); 73 context.pushMap("label", sitemapElements); 74 PreparedVariableResolver resolver = new PreparedVariableResolver(expr, getManager()); 75 assertEquals("123", resolver.resolve(context, getObjectModel())); 76 } 77 78 public void testSitemapVariables() throws PatternException { 79 String expr = "123{1}"; 80 81 InvokeContext context = new InvokeContext(true); 82 context.enableLogging(getLogger()); 83 84 Map sitemapElements = new HashMap (); 85 sitemapElements.put("1", "abc"); 86 context.pushMap("label", sitemapElements); 87 PreparedVariableResolver resolver = new PreparedVariableResolver(expr, getManager()); 88 assertEquals("123abc", resolver.resolve(context, getObjectModel())); 89 } 90 91 public void testSitemapVariablesWithText() throws PatternException { 92 String expr = "123{1}/def"; 93 94 InvokeContext context = new InvokeContext(true); 95 context.enableLogging(getLogger()); 96 97 Map sitemapElements = new HashMap (); 98 sitemapElements.put("1", "abc"); 99 context.pushMap("label", sitemapElements); 100 PreparedVariableResolver resolver = new PreparedVariableResolver(expr, getManager()); 101 assertEquals("123abc/def", resolver.resolve(context, getObjectModel())); 102 } 103 104 public void testPrefixedSitemapVariable() throws PatternException { 105 String expr = "123{sitemap:1}/def"; 106 107 InvokeContext context = new InvokeContext(true); 108 context.enableLogging(getLogger()); 109 110 Map sitemapElements = new HashMap (); 111 sitemapElements.put("1", "abc"); 112 context.pushMap("label", sitemapElements); 113 PreparedVariableResolver resolver = new PreparedVariableResolver(expr, getManager()); 114 assertEquals("123abc/def", resolver.resolve(context, getObjectModel())); 115 } 116 117 public void testMultilevelSitemapVariables() throws PatternException { 118 String expr = "from {../1} to {1}"; 119 120 InvokeContext context = new InvokeContext(true); 121 context.enableLogging(getLogger()); 122 123 Map sitemapElements; 124 sitemapElements = new HashMap (); 125 sitemapElements.put("1", "juliet"); 126 context.pushMap("label1", sitemapElements); 127 128 sitemapElements = new HashMap (); 129 sitemapElements.put("1", "oscar"); 130 context.pushMap("label2", sitemapElements); 131 132 PreparedVariableResolver resolver = new PreparedVariableResolver(expr, getManager()); 133 assertEquals("from juliet to oscar", resolver.resolve(context, getObjectModel())); 134 } 135 136 public void testRootSitemapVariables() throws PatternException { 137 String expr = "from {/1} to {1}"; 138 139 InvokeContext context = new InvokeContext(true); 140 context.enableLogging(getLogger()); 141 142 Map sitemapElements; 143 sitemapElements = new HashMap (); 144 sitemapElements.put("1", "juliet"); 145 context.pushMap("label1", sitemapElements); 146 147 sitemapElements = new HashMap (); 148 sitemapElements.put("1", "oscar"); 149 context.pushMap("label2", sitemapElements); 150 151 PreparedVariableResolver resolver = new PreparedVariableResolver(expr, getManager()); 152 assertEquals("from juliet to oscar", resolver.resolve(context, getObjectModel())); 153 } 154 155 public void testColonInTextContent() throws PatternException { 156 String expr = "http://cocoon.apache.org"; 157 158 InvokeContext context = new InvokeContext(true); 159 context.enableLogging(getLogger()); 160 161 Map sitemapElements; 162 sitemapElements = new HashMap (); 163 context.pushMap("label", sitemapElements); 164 165 PreparedVariableResolver resolver = new PreparedVariableResolver(expr, getManager()); 166 assertEquals("http://cocoon.apache.org", resolver.resolve(context, getObjectModel())); 167 } 168 169 public void testColonBeginningTextContent() throws PatternException { 170 String expr = ":colon-starts-this"; 171 172 InvokeContext context = new InvokeContext(true); 173 context.enableLogging(getLogger()); 174 175 Map sitemapElements; 176 sitemapElements = new HashMap (); 177 context.pushMap("label", sitemapElements); 178 179 PreparedVariableResolver resolver = new PreparedVariableResolver(expr, getManager()); 180 assertEquals(":colon-starts-this", resolver.resolve(context, getObjectModel())); 181 } 182 183 public void testEmbeddedColon() throws PatternException { 184 String expr = "{1}:{1}"; 185 186 InvokeContext context = new InvokeContext(true); 187 context.enableLogging(getLogger()); 188 189 Map sitemapElements; 190 sitemapElements = new HashMap (); 191 sitemapElements.put("1", "abc"); 192 context.pushMap("label", sitemapElements); 193 194 PreparedVariableResolver resolver = new PreparedVariableResolver(expr, getManager()); 195 assertEquals("abc:abc", resolver.resolve(context, getObjectModel())); 196 } 197 198 public void testEscapedBraces() throws PatternException { 199 String expr = "This is a \\{brace\\}"; 200 201 InvokeContext context = new InvokeContext(true); 202 context.enableLogging(getLogger()); 203 204 Map sitemapElements; 205 sitemapElements = new HashMap (); 206 context.pushMap("label", sitemapElements); 207 208 PreparedVariableResolver resolver = new PreparedVariableResolver(expr, getManager()); 209 assertEquals("This is a {brace}", resolver.resolve(context, getObjectModel())); 210 } 211 212 public void testModuleWithoutOption() throws PatternException { 213 String expr = "{baselink:}"; 214 InvokeContext context = new InvokeContext(true); 215 context.enableLogging(getLogger()); 216 217 Map sitemapElements = new HashMap (); 218 context.pushMap("sitemap", sitemapElements); 219 PreparedVariableResolver resolver = new PreparedVariableResolver(expr, getManager()); 220 assertEquals("", resolver.resolve(context, getObjectModel())); 221 } 222 } 223 | Popular Tags |