001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.cocoon.components.treeprocessor.variables;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.apache.cocoon.SitemapComponentTestCase;
024: import org.apache.cocoon.components.treeprocessor.InvokeContext;
025: import org.apache.cocoon.environment.mock.MockRequest;
026: import org.apache.cocoon.sitemap.PatternException;
027:
028: /**
029: * Test case for the nested variant of the PreparedVariableResolver
030: *
031: * @author <a href="mailto:uv@upaya.co.uk">Upayavira</a>
032: * @version CVS $Id: PreparedVariableResolverTestCase.java 433543 2006-08-22 06:22:54Z crossley $
033: */
034: public class PreparedVariableResolverTestCase extends
035: SitemapComponentTestCase {
036:
037: public void testNestedExpressions() throws Exception {
038: String expr = "{request-param:{request-param:foo}}";
039: MockRequest request = getRequest();
040: request.reset();
041: request.addParameter("foo", "bar");
042: request.addParameter("bar", "123");
043: InvokeContext context = new InvokeContext(true);
044: context.enableLogging(getLogger());
045:
046: Map sitemapElements = new HashMap();
047: context.pushMap("sitemap", sitemapElements);
048: PreparedVariableResolver resolver = new PreparedVariableResolver(
049: expr, getManager());
050: assertEquals("123", resolver.resolve(context, getObjectModel()));
051: }
052:
053: public void testNestedModuleAndSitemapExpressions()
054: throws Exception {
055: String expr = "{request-param:f{1}}";
056: MockRequest request = getRequest();
057: request.reset();
058: request.addParameter("foo", "123");
059: InvokeContext context = new InvokeContext(true);
060: context.enableLogging(getLogger());
061:
062: Map sitemapElements = new HashMap();
063: sitemapElements.put("1", "oo");
064: context.pushMap("sitemap", sitemapElements);
065: PreparedVariableResolver resolver = new PreparedVariableResolver(
066: expr, getManager());
067: assertEquals("123", resolver.resolve(context, getObjectModel()));
068: }
069:
070: public void testAnchors() throws PatternException {
071: String expr = "{#label:name}";
072:
073: InvokeContext context = new InvokeContext(true);
074: context.enableLogging(getLogger());
075:
076: Map sitemapElements = new HashMap();
077: sitemapElements.put("name", "123");
078: context.pushMap("label", sitemapElements);
079: PreparedVariableResolver resolver = new PreparedVariableResolver(
080: expr, getManager());
081: assertEquals("123", resolver.resolve(context, getObjectModel()));
082: }
083:
084: public void testSitemapVariables() throws PatternException {
085: String expr = "123{1}";
086:
087: InvokeContext context = new InvokeContext(true);
088: context.enableLogging(getLogger());
089:
090: Map sitemapElements = new HashMap();
091: sitemapElements.put("1", "abc");
092: context.pushMap("label", sitemapElements);
093: PreparedVariableResolver resolver = new PreparedVariableResolver(
094: expr, getManager());
095: assertEquals("123abc", resolver.resolve(context,
096: getObjectModel()));
097: }
098:
099: public void testSitemapVariablesWithText() throws PatternException {
100: String expr = "123{1}/def";
101:
102: InvokeContext context = new InvokeContext(true);
103: context.enableLogging(getLogger());
104:
105: Map sitemapElements = new HashMap();
106: sitemapElements.put("1", "abc");
107: context.pushMap("label", sitemapElements);
108: PreparedVariableResolver resolver = new PreparedVariableResolver(
109: expr, getManager());
110: assertEquals("123abc/def", resolver.resolve(context,
111: getObjectModel()));
112: }
113:
114: public void testPrefixedSitemapVariable() throws PatternException {
115: String expr = "123{sitemap:1}/def";
116:
117: InvokeContext context = new InvokeContext(true);
118: context.enableLogging(getLogger());
119:
120: Map sitemapElements = new HashMap();
121: sitemapElements.put("1", "abc");
122: context.pushMap("label", sitemapElements);
123: PreparedVariableResolver resolver = new PreparedVariableResolver(
124: expr, getManager());
125: assertEquals("123abc/def", resolver.resolve(context,
126: getObjectModel()));
127: }
128:
129: public void testMultilevelSitemapVariables()
130: throws PatternException {
131: String expr = "from {../1} to {1}";
132:
133: InvokeContext context = new InvokeContext(true);
134: context.enableLogging(getLogger());
135:
136: Map sitemapElements;
137: sitemapElements = new HashMap();
138: sitemapElements.put("1", "juliet");
139: context.pushMap("label1", sitemapElements);
140:
141: sitemapElements = new HashMap();
142: sitemapElements.put("1", "oscar");
143: context.pushMap("label2", sitemapElements);
144:
145: PreparedVariableResolver resolver = new PreparedVariableResolver(
146: expr, getManager());
147: assertEquals("from juliet to oscar", resolver.resolve(context,
148: getObjectModel()));
149: }
150:
151: public void testRootSitemapVariables() throws PatternException {
152: String expr = "from {/1} to {1}";
153:
154: InvokeContext context = new InvokeContext(true);
155: context.enableLogging(getLogger());
156:
157: Map sitemapElements;
158: sitemapElements = new HashMap();
159: sitemapElements.put("1", "juliet");
160: context.pushMap("label1", sitemapElements);
161:
162: sitemapElements = new HashMap();
163: sitemapElements.put("1", "oscar");
164: context.pushMap("label2", sitemapElements);
165:
166: PreparedVariableResolver resolver = new PreparedVariableResolver(
167: expr, getManager());
168: assertEquals("from juliet to oscar", resolver.resolve(context,
169: getObjectModel()));
170: }
171:
172: public void testColonInTextContent() throws PatternException {
173: String expr = "http://cocoon.apache.org";
174:
175: InvokeContext context = new InvokeContext(true);
176: context.enableLogging(getLogger());
177:
178: Map sitemapElements;
179: sitemapElements = new HashMap();
180: context.pushMap("label", sitemapElements);
181:
182: PreparedVariableResolver resolver = new PreparedVariableResolver(
183: expr, getManager());
184: assertEquals("http://cocoon.apache.org", resolver.resolve(
185: context, getObjectModel()));
186: }
187:
188: public void testColonBeginningTextContent() throws PatternException {
189: String expr = ":colon-starts-this";
190:
191: InvokeContext context = new InvokeContext(true);
192: context.enableLogging(getLogger());
193:
194: Map sitemapElements;
195: sitemapElements = new HashMap();
196: context.pushMap("label", sitemapElements);
197:
198: PreparedVariableResolver resolver = new PreparedVariableResolver(
199: expr, getManager());
200: assertEquals(":colon-starts-this", resolver.resolve(context,
201: getObjectModel()));
202: }
203:
204: public void testEmbeddedColon() throws PatternException {
205: String expr = "{1}:{1}";
206:
207: InvokeContext context = new InvokeContext(true);
208: context.enableLogging(getLogger());
209:
210: Map sitemapElements;
211: sitemapElements = new HashMap();
212: sitemapElements.put("1", "abc");
213: context.pushMap("label", sitemapElements);
214:
215: PreparedVariableResolver resolver = new PreparedVariableResolver(
216: expr, getManager());
217: assertEquals("abc:abc", resolver.resolve(context,
218: getObjectModel()));
219: }
220:
221: public void testEscapedBraces() throws PatternException {
222: String expr = "This is a \\{brace\\}";
223:
224: InvokeContext context = new InvokeContext(true);
225: context.enableLogging(getLogger());
226:
227: Map sitemapElements;
228: sitemapElements = new HashMap();
229: context.pushMap("label", sitemapElements);
230:
231: PreparedVariableResolver resolver = new PreparedVariableResolver(
232: expr, getManager());
233: assertEquals("This is a {brace}", resolver.resolve(context,
234: getObjectModel()));
235: }
236:
237: public void testModuleWithoutOption() throws PatternException {
238: String expr = "{baselink:}";
239: InvokeContext context = new InvokeContext(true);
240: context.enableLogging(getLogger());
241:
242: Map sitemapElements = new HashMap();
243: context.pushMap("sitemap", sitemapElements);
244: PreparedVariableResolver resolver = new PreparedVariableResolver(
245: expr, getManager());
246: assertEquals("", resolver.resolve(context, getObjectModel()));
247: }
248: }
|