001: /*
002: * Copyright 2007 Gerd Ziegler (www.gerdziegler.de)
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: * http://www.apache.org/licenses/LICENSE-2.0
007: * Unless required by applicable law or agreed to in writing,
008: * software distributed under the License is distributed on an
009: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
010: * either express or implied. See the License for the specific
011: * language governing permissions and limitations under the License.
012: * 22.09.2007
013: * @author www.gerdziegler.de
014: */
015: package org.ztemplates.test.actions.expression.test1;
016:
017: import junit.framework.TestCase;
018:
019: import org.apache.log4j.Logger;
020: import org.ztemplates.actions.expression.ZExpression;
021: import org.ztemplates.actions.expression.ZParserException;
022: import org.ztemplates.actions.expression.ZTail;
023: import org.ztemplates.actions.expression.ZTerm;
024:
025: public class ExpressionTest extends TestCase {
026: static Logger log = Logger.getLogger(ExpressionTest.class);
027:
028: protected void setUp() throws Exception {
029: super .setUp();
030: }
031:
032: protected void tearDown() throws Exception {
033: super .tearDown();
034: }
035:
036: public void test1() throws Exception {
037: doit("audiobooks/category/${title}_${categoryId}[/page/${pageNum}][/sortby-${sortBy}]");
038: }
039:
040: public void test2() throws Exception {
041: doit("audiobooks/category/${title}_${categoryId}[/page/${pageNum}[/sortby-${sortBy}]katze]");
042: }
043:
044: public void test3() throws Exception {
045: doit("${title}_${categoryId}[/page/${pageNum}[/sortby-${sortBy}]katze]");
046: }
047:
048: public void test4() throws Exception {
049: doit("[]");
050: }
051:
052: public void test5() throws Exception {
053: doit("[katze]");
054: }
055:
056: public void test6() throws Exception {
057: doit("${i}");
058: }
059:
060: public void test7() throws Exception {
061: doit("${i}");
062: }
063:
064: public void test8() throws Exception {
065: doit("[${i}]");
066: }
067:
068: public void test9() throws Exception {
069: doit("[[[[[[a]a]a]a]a]a]");
070: }
071:
072: public void test10() throws Exception {
073: doit("[[[[[[a]a]a]a]a]a]/*{tail}");
074: }
075:
076: public void test11() throws Exception {
077: ZExpression exp = doit("katze*{tail}");
078: ZTerm term = exp.getContent().get(exp.getContent().size() - 1);
079: assertTrue(term instanceof ZTail);
080: }
081:
082: public void testFail1() throws Exception {
083: try {
084: ZExpression p = new ZExpression("${}");
085: fail("empty variable not allowed");
086: } catch (ZParserException e) {
087: }
088: }
089:
090: public void testFail2() throws Exception {
091: try {
092: ZExpression p = new ZExpression("${");
093: fail("missing parantheses");
094: } catch (ZParserException e) {
095: }
096: }
097:
098: public void testFail3() throws Exception {
099: try {
100: ZExpression p = new ZExpression("[${i$}]");
101: fail("misplaced '$'");
102: } catch (ZParserException e) {
103: }
104: }
105:
106: public void testFail4() throws Exception {
107: try {
108: ZExpression p = new ZExpression("[${i[}]");
109: fail("misplaced '['");
110: } catch (ZParserException e) {
111: }
112: }
113:
114: public void testFail5() throws Exception {
115: try {
116: ZExpression p = new ZExpression("]");
117: fail("misplaced ']'");
118: } catch (ZParserException e) {
119: }
120: }
121:
122: public void testFail6() throws Exception {
123: try {
124: ZExpression p = new ZExpression("[test]ka]tze");
125: fail("misplaced ']'");
126: } catch (ZParserException e) {
127: }
128: }
129:
130: public void testFail7() throws Exception {
131: try {
132: ZExpression p = new ZExpression("[test]ka[tze");
133: fail("misplaced '['");
134: } catch (ZParserException e) {
135: }
136: }
137:
138: public void testFail8() throws Exception {
139: try {
140: ZExpression p = new ZExpression("[test]ka$tze");
141: fail("misplaced '$'");
142: } catch (ZParserException e) {
143: }
144: }
145:
146: public void testFail9() throws Exception {
147: try {
148: ZExpression p = new ZExpression("*{tail}-");
149: fail("tail must be last");
150: } catch (ZParserException e) {
151: }
152: }
153:
154: public void testFail10() throws Exception {
155: try {
156: ZExpression exp = new ZExpression("${var1}${var2}");
157: fail("variables without literal in between");
158: } catch (ZParserException e) {
159: }
160: }
161:
162: public void testFail11() throws Exception {
163: try {
164: ZExpression exp = new ZExpression("${var1}*{var2}");
165: fail("variables without literal in between");
166: } catch (ZParserException e) {
167: }
168: }
169:
170: public void testFail12() throws Exception {
171: try {
172: ZExpression exp = new ZExpression("${var1}#{var2}");
173: fail("variables without literal in between");
174: } catch (ZParserException e) {
175: }
176: }
177:
178: private ZExpression doit(String pattern) throws Exception {
179: ZExpression exp = new ZExpression(pattern);
180: log.debug(exp.toXml());
181: return exp;
182: // String regex = exp.toRegex();
183: // log.debug(regex);
184: // List<String> samples = exp.createSamples();
185: // for (String crt : samples)
186: // {
187: // log.debug("sample: '" + crt + "'");
188: // assertTrue(Pattern.matches(regex, crt));
189: // }
190: }
191:
192: // private void doit(String pattern, String match) throws Exception
193: // {
194: // Expression exp = new Expression(pattern);
195: // log.debug(exp.toXml());
196: // String regex = exp.toRegex();
197: // log.debug(regex);
198: // assertTrue(Pattern.matches(regex, match));
199: // }
200:
201: }
|