001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
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    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.junit.utils;
016    
017    import org.apache.hivemind.ApplicationRuntimeException;
018    import org.apache.tapestry.junit.TapestryTestCase;
019    import org.apache.tapestry.util.RegexpMatch;
020    import org.apache.tapestry.util.RegexpMatcher;
021    import org.testng.annotations.Test;
022    
023    /**
024     * Simple test case for {@link org.apache.tapestry.util.RegexpMatcher}.
025     * 
026     * @author Howard Lewis Ship
027     * @since 3.0
028     */
029    @Test
030    public class TestRegexpMatcher extends TapestryTestCase
031    {
032    
033        public void testMatch()
034        {
035            RegexpMatcher m = new RegexpMatcher();
036    
037            assertTrue(m.matches("[a-z]+", "c"));
038            assertTrue(m.matches("foo|foot", "foo"));
039        }
040    
041        public void test_Non_Match()
042        {
043            RegexpMatcher m = new RegexpMatcher();
044    
045            assert !m.matches("[0-9]+", "q");
046            assert !m.matches("foo|efoot", "foot");
047        }
048    
049        public void test_Bad_Pattern()
050        {
051            RegexpMatcher m = new RegexpMatcher();
052    
053            try
054            {
055                m.matches("[[[", "x");
056    
057                unreachable();
058            }
059            catch (ApplicationRuntimeException ex)
060            {
061                checkException(ex, "Unclosed character class near index 2");
062            }
063        }
064    
065        public void testClear()
066        {
067            RegexpMatcher m = new RegexpMatcher();
068    
069            m.clear();
070        }
071    
072        public void testContains()
073        {
074            RegexpMatcher m = new RegexpMatcher();
075    
076            assertTrue(m.contains("[a-z]+", "c"));
077            assertTrue(m.contains("^(\\d{5}(-\\d{4})?)$", "06514"));
078            assertTrue(m.contains("^(\\d{5}(-\\d{4})?)$", "06514-3149"));
079            assertTrue(m.contains("foo|foot", "12foot12"));
080        }
081    
082        public void testNotContains()
083        {
084            RegexpMatcher m = new RegexpMatcher();
085    
086            assertTrue(!m.contains("[0-9]+", "q"));
087            assertTrue(!m.contains("^(\\d{5}(-\\d{4})?)$", "0651"));
088            assertTrue(!m.contains("^(\\d{5}(-\\d{4})?)$", "065147"));
089            assertTrue(!m.contains("^(\\d{5}(-\\d{4})?)$", "06514-314"));
090            assertTrue(!m.contains("^(\\d{5}(-\\d{4})?)$", "06514-31497"));
091            assertTrue(!m.contains("^(foo|foot)$", "12foot12"));
092        }
093    
094        public void testGetEscapedPatternStrings()
095        {
096            RegexpMatcher m = new RegexpMatcher();
097    
098            assertEquals(m.getEscapedPatternString("^\\d$"), "\\^\\\\d\\$");
099        }
100    
101        /** @since 4.0 */
102    
103        public void test_Get_Matches()
104        {
105            RegexpMatcher m = new RegexpMatcher();
106    
107            String[] matches = m.getMatches("\\d+", "57,232 89 147", 0);
108    
109            assertEquals(new String[] { "57", "232", "89", "147" }, matches);
110        }
111    
112        /** @since 4.0 */
113    
114        public void testGetMatchesAsObjects()
115        {
116            RegexpMatcher m = new RegexpMatcher();
117    
118            RegexpMatch[] matches = m.getMatches("\\w+(=(\\w+))?", "fred,barney=rubble;wilma=flintstone");
119    
120            assertEquals(3, matches.length);
121            
122            assertEquals("fred", matches[0].getInput());
123            assertEquals("fred", matches[0].getGroup(0));
124            
125            assertEquals("barney=rubble", matches[1].getInput());
126            assertEquals("rubble", matches[1].getGroup(2));
127            
128            assertEquals("wilma=flintstone", matches[2].getInput());
129            assertEquals("flintstone", matches[2].getGroup(2));        
130        }
131    
132        /** @since 4.0 */
133    
134        public void testGetMatchesNoMatch()
135        {
136            RegexpMatcher m = new RegexpMatcher();
137    
138            String[] matches = m.getMatches("A(B|C)", "aBCAaBA", 0);
139    
140            assertEquals(0, matches.length);
141        }
142    
143        /** @since 4.0 */
144    
145        public void testGetMatchesSubgroup()
146        {
147            RegexpMatcher m = new RegexpMatcher();
148    
149            String matches[] = m.getMatches("A(B|C|fred)", "AA AC AB Afred AA AC", 1);
150    
151            assertListEquals(new String[]
152            { "C", "B", "fred", "C" }, matches);
153        }
154    
155    }