001/* $Id: RegexRulesTestCase.java 1212599 2011-12-09 19:46:42Z simonetripodi $
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements.  See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.commons.digester3;
020
021import static org.junit.Assert.assertEquals;
022
023import java.util.List;
024
025import org.apache.commons.digester3.RegexMatcher;
026import org.apache.commons.digester3.RegexRules;
027import org.apache.commons.digester3.Rule;
028import org.apache.commons.digester3.SimpleRegexMatcher;
029import org.junit.Test;
030
031/**
032 * Test case for RegexRules
033 *
034 * @author Robert Burrell Donkin
035 */
036public class RegexRulesTestCase
037{
038
039    /** Test regex that matches everything */
040    @Test
041    public void testMatchAll()
042    {
043        // set up which should match every rule
044        RegexRules rules = new RegexRules( new RegexMatcher()
045        {
046            @Override
047            public boolean match( String pathPattern, String rulePattern )
048            {
049                return true;
050            }
051        } );
052
053        rules.add( "/a/b/b", new TestRule( "alpha" ) );
054        rules.add( "/a/d", new TestRule( "beta" ) );
055        rules.add( "/b", new TestRule( "gamma" ) );
056
057        // now test a few patterns
058        // check that all are return in the order which they were added
059        List<Rule> matches = rules.match( "", "x/g/e", null, null );
060        assertEquals( "Wrong number of rules returned (1)", 3, matches.size() );
061        assertEquals( "Rule Out Of Order (1)", "alpha", ( (TestRule) matches.get( 0 ) ).getIdentifier() );
062        assertEquals( "Rule Out Of Order (2)", "beta", ( (TestRule) matches.get( 1 ) ).getIdentifier() );
063        assertEquals( "Rule Out Of Order (3)", "gamma", ( (TestRule) matches.get( 2 ) ).getIdentifier() );
064
065        matches = rules.match( "", "/a", null, null );
066        assertEquals( "Wrong number of rules returned (2)", 3, matches.size() );
067        assertEquals( "Rule Out Of Order (4)", "alpha", ( (TestRule) matches.get( 0 ) ).getIdentifier() );
068        assertEquals( "Rule Out Of Order (5)", "beta", ( (TestRule) matches.get( 1 ) ).getIdentifier() );
069        assertEquals( "Rule Out Of Order (6)", "gamma", ( (TestRule) matches.get( 2 ) ).getIdentifier() );
070    }
071
072    /** Test regex matcher that matches nothing */
073    @Test
074    public void testMatchNothing()
075    {
076        // set up which should match every rule
077        RegexRules rules = new RegexRules( new RegexMatcher()
078        {
079            @Override
080            public boolean match( String pathPattern, String rulePattern )
081            {
082                return false;
083            }
084        } );
085
086        rules.add( "/b/c/f", new TestRule( "alpha" ) );
087        rules.add( "/c/f", new TestRule( "beta" ) );
088        rules.add( "/b", new TestRule( "gamma" ) );
089
090        // now test a few patterns
091        // check that all are return in the order which they were added
092        List<Rule> matches = rules.match( "", "/b/c", null, null );
093        assertEquals( "Wrong number of rules returned (1)", 0, matches.size() );
094
095        matches = rules.match( "", "/b/c/f", null, null );
096        assertEquals( "Wrong number of rules returned (2)", 0, matches.size() );
097    }
098
099    /** Test a mixed regex - in other words, one that sometimes returns true and sometimes false */
100    @Test
101    public void testMatchMixed()
102    {
103        // set up which should match every rule
104        RegexRules rules = new RegexRules( new RegexMatcher()
105        {
106            @Override
107            public boolean match( String pathPattern, String rulePattern )
108            {
109                return ( rulePattern.equals( "/match/me" ) );
110            }
111        } );
112
113        rules.add( "/match", new TestRule( "alpha" ) );
114        rules.add( "/match/me", new TestRule( "beta" ) );
115        rules.add( "/match", new TestRule( "gamma" ) );
116
117        // now test a few patterns
118        // check that all are return in the order which they were added
119        List<Rule> matches = rules.match( "", "/match", null, null );
120        assertEquals( "Wrong number of rules returned (1)", 1, matches.size() );
121        assertEquals( "Wrong Rule (1)", "beta", ( (TestRule) matches.get( 0 ) ).getIdentifier() );
122
123        matches = rules.match( "", "/a/match", null, null );
124        assertEquals( "Wrong Rule (2)", "beta", ( (TestRule) matches.get( 0 ) ).getIdentifier() );
125    }
126
127    /** Test rules and clear methods */
128    @Test
129    public void testClear()
130    {
131        // set up which should match every rule
132        RegexRules rules = new RegexRules( new RegexMatcher()
133        {
134            @Override
135            public boolean match( String pathPattern, String rulePattern )
136            {
137                return true;
138            }
139        } );
140
141        rules.add( "/abba", new TestRule( "alpha" ) );
142        rules.add( "/ad/ma", new TestRule( "beta" ) );
143        rules.add( "/gamma", new TestRule( "gamma" ) );
144
145        // check that rules returns all rules in the order which they were added
146        List<Rule> matches = rules.rules();
147        assertEquals( "Wrong number of rules returned (1)", 3, matches.size() );
148        assertEquals( "Rule Out Of Order (1)", "alpha", ( (TestRule) matches.get( 0 ) ).getIdentifier() );
149        assertEquals( "Rule Out Of Order (2)", "beta", ( (TestRule) matches.get( 1 ) ).getIdentifier() );
150        assertEquals( "Rule Out Of Order (3)", "gamma", ( (TestRule) matches.get( 2 ) ).getIdentifier() );
151
152        matches = rules.match( "", "/eggs", null, null );
153        assertEquals( "Wrong number of rules returned (2)", 3, matches.size() );
154        assertEquals( "Rule Out Of Order (4)", "alpha", ( (TestRule) matches.get( 0 ) ).getIdentifier() );
155        assertEquals( "Rule Out Of Order (5)", "beta", ( (TestRule) matches.get( 1 ) ).getIdentifier() );
156        assertEquals( "Rule Out Of Order (6)", "gamma", ( (TestRule) matches.get( 2 ) ).getIdentifier() );
157
158        rules.clear();
159        matches = rules.rules();
160        assertEquals( "Wrong number of rules returned (3)", 0, matches.size() );
161
162        matches = rules.match( "", "/eggs", null, null );
163        assertEquals( "Wrong number of rules returned (4)", 0, matches.size() );
164    }
165
166    @Test
167    public void testSimpleRegexMatch()
168    {
169
170        SimpleRegexMatcher matcher = new SimpleRegexMatcher();
171
172        // SimpleLog log = new SimpleLog("{testSimpleRegexMatch:SimpleRegexMatcher]");
173        // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
174
175        assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '/alpha/beta/gamma' ", true,
176                      matcher.match( "/alpha/beta/gamma", "/alpha/beta/gamma" ) );
177        assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '/alpha/beta/gamma/epsilon' ", false,
178                      matcher.match( "/alpha/beta/gamma", "/alpha/beta/gamma/epsilon" ) );
179        assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '/alpha/*' ", true,
180                      matcher.match( "/alpha/beta/gamma", "/alpha/*" ) );
181        assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '/alpha/*/gamma' ", true,
182                      matcher.match( "/alpha/beta/gamma", "/alpha/*/gamma" ) );
183        assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '/alpha/*me' ", false,
184                      matcher.match( "/alpha/beta/gamma", "/alpha/*me" ) );
185        assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '*/beta/gamma' ", true,
186                      matcher.match( "/alpha/beta/gamma", "*/beta/gamma" ) );
187        assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '*/alpha/beta/gamma' ", true,
188                      matcher.match( "/alpha/beta/gamma", "*/alpha/beta/gamma" ) );
189        assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '*/bet/gamma' ", false,
190                      matcher.match( "/alpha/beta/gamma", "*/bet/gamma" ) );
191        assertEquals( "Simple Regex Match '/alpha/beta/gamma' to 'alph?/beta/gamma' ", true,
192                      matcher.match( "/alpha/beta/gamma", "/alph?/beta/gamma" ) );
193        assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '/?lpha/beta/gamma' ", true,
194                      matcher.match( "/alpha/beta/gamma", "/?lpha/beta/gamma" ) );
195        assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '/alpha/?beta/gamma' ", false,
196                      matcher.match( "/alpha/beta/gamma", "/alpha/?beta/gamma" ) );
197        assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '/alpha/?eta/*' ", true,
198                      matcher.match( "/alpha/beta/gamma", "/alpha/?eta/*" ) );
199        assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '/alpha/?eta/*e' ", false,
200                      matcher.match( "/alpha/beta/gamma", "/alpha/?eta/*e" ) );
201        assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '*/?et?/?amma' ", true,
202                      matcher.match( "/alpha/beta/gamma", "*/?et?/?amma" ) );
203        assertEquals( "Simple Regex Match '/alpha/beta/gamma/beta/epsilon/beta/gamma/epsilon' to "
204                          + " '*/beta/gamma/?p*n' ", true,
205                      matcher.match( "/alpha/beta/gamma/beta/epsilon/beta/gamma/epsilon", "*/beta/gamma/?p*n" ) );
206        assertEquals( "Simple Regex Match '/alpha/beta/gamma/beta/epsilon/beta/gamma/epsilon' to "
207            + " '*/beta/gamma/?p*no' ", false, matcher.match( "/alpha/beta/gamma", "*/beta/gamma/?p*no" ) );
208    }
209}