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: * 20.09.2007
013: * @author www.gerdziegler.de
014: */
015: package org.ztemplates.test.actions.urlhandler.match.matchtree;
016:
017: import junit.framework.TestCase;
018:
019: import org.apache.log4j.Logger;
020: import org.ztemplates.actions.urlhandler.tree.match.ZMatchTree;
021: import org.ztemplates.actions.urlhandler.tree.match.ZMatchedUrl;
022: import org.ztemplates.actions.urlhandler.tree.term.ZTreeLiteral;
023: import org.ztemplates.actions.urlhandler.tree.term.ZTreeSlash;
024: import org.ztemplates.actions.urlhandler.tree.term.ZTreeTermList;
025: import org.ztemplates.actions.urlhandler.tree.term.ZTreeVariable;
026: import org.ztemplates.classpath.ZClassRepository;
027: import org.ztemplates.classpath.ZIClassRepository;
028:
029: public class MatchTreeTest extends TestCase {
030: static Logger log = Logger.getLogger(MatchTreeTest.class);
031:
032: ZIClassRepository classRepo;
033:
034: protected void setUp() throws Exception {
035: super .setUp();
036: classRepo = new ZClassRepository(MatchTreeTest.class
037: .getPackage().getName());
038: }
039:
040: protected void tearDown() throws Exception {
041: classRepo = null;
042: super .tearDown();
043: }
044:
045: public void testMatchLiteralFirst() throws Exception {
046: ZMatchTree tree = new ZMatchTree();
047:
048: ZTreeTermList tl1 = new ZTreeTermList();
049: tl1.getTerms().add(new ZTreeSlash(this .getClass()));
050: tl1.getTerms().add(new ZTreeVariable(this .getClass(), "var"));
051:
052: ZTreeTermList tl2 = new ZTreeTermList();
053: tl2.getTerms().add(new ZTreeSlash(this .getClass()));
054: tl2.getTerms().add(new ZTreeLiteral(this .getClass(), "123"));
055:
056: tree.add(tl1);
057: tree.add(tl2);
058: tree.prepare();
059: log.info(tree.toConsoleString());
060: ZMatchedUrl mu = tree.match("/123");
061: assertEquals(tree.toConsoleString() + "\n " + mu.getTermList(),
062: tl2.toString(), mu.getTermList().toString());
063: }
064:
065: public void testMatchOrder2() throws Exception {
066: ZMatchTree tree = new ZMatchTree();
067:
068: ZTreeTermList tl1 = new ZTreeTermList();
069: tl1.getTerms().add(new ZTreeSlash(this .getClass()));
070: tl1.getTerms().add(new ZTreeVariable(this .getClass(), "var"));
071: tl1.getTerms().add(new ZTreeLiteral(this .getClass(), "_"));
072: tl1.getTerms().add(new ZTreeVariable(this .getClass(), "var"));
073:
074: ZTreeTermList tl2 = new ZTreeTermList();
075: tl2.getTerms().add(new ZTreeSlash(this .getClass()));
076: tl2.getTerms().add(new ZTreeLiteral(this .getClass(), "123_"));
077: tl2.getTerms().add(new ZTreeVariable(this .getClass(), "var"));
078:
079: tree.add(tl1);
080: tree.add(tl2);
081: tree.prepare();
082: log.info(tree.toConsoleString());
083: ZMatchedUrl mu = tree.match("/123_456");
084: assertEquals(tree.toConsoleString() + "\n " + mu.getTermList(),
085: tl2.toString(), mu.getTermList().toString());
086: }
087:
088: public void testMatchOrder3() throws Exception {
089: ZMatchTree tree = new ZMatchTree();
090:
091: ZTreeTermList tl1 = new ZTreeTermList();
092: tl1.getTerms().add(new ZTreeSlash(this .getClass()));
093: tl1.getTerms().add(new ZTreeVariable(this .getClass(), "12"));
094: tl1.getTerms().add(new ZTreeVariable(this .getClass(), "var"));
095: tl1.getTerms().add(new ZTreeVariable(this .getClass(), "45"));
096: tl1.getTerms().add(new ZTreeVariable(this .getClass(), "var1"));
097: tl1.getTerms().add(new ZTreeVariable(this .getClass(), "78"));
098: tl1.getTerms().add(new ZTreeVariable(this .getClass(), "var2"));
099:
100: ZTreeTermList tl2 = new ZTreeTermList();
101: tl2.getTerms().add(new ZTreeSlash(this .getClass()));
102: tl2.getTerms().add(
103: new ZTreeLiteral(this .getClass(), "12345678"));
104: tl2.getTerms().add(new ZTreeVariable(this .getClass(), "var"));
105:
106: tree.add(tl1);
107: tree.add(tl2);
108: tree.prepare();
109: log.info(tree.toConsoleString());
110: ZMatchedUrl mu = tree.match("/123456789");
111: assertEquals(tree.toConsoleString() + "\n " + mu.getTermList(),
112: tl2.toString(), mu.getTermList().toString());
113: }
114: }
|