1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.velocity.tools

File RegexToolTest.java

 

Code metrics

0
55
9
1
159
103
9
0.16
6.11
9
1

Classes

Class Line # Actions
RegexToolTest 36 55 0% 9 0
1.0100%
 

Contributing tests

This file is covered by 9 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.velocity.tools;
21   
22    import java.util.List;
23    import java.util.regex.Matcher;
24    import java.util.regex.Pattern;
25   
26    import org.junit.Assert;
27    import org.junit.Test;
28    import org.xwiki.velocity.tools.RegexTool.RegexResult;
29   
30    /**
31    * Unit tests for {@link RegexTool}.
32    *
33    * @version $Id: 4d5e273999975544d69b8207c776b2708505e8c6 $
34    * @since 2.0RC1
35    */
 
36    public class RegexToolTest
37    {
 
38  1 toggle @Test
39    public void testFind()
40    {
41  1 RegexTool tool = new RegexTool();
42  1 List<RegexResult> result =
43    tool.find("<h1><span>header</span></h1> whatever", "<[hH][12].*?><span>(.*?)</span></[hH][12]>");
44   
45  1 Assert.assertEquals(2, result.size());
46  1 Assert.assertEquals("<h1><span>header</span></h1>", result.get(0).getGroup());
47  1 Assert.assertEquals(0, result.get(0).getStart());
48  1 Assert.assertEquals(28, result.get(0).getEnd());
49  1 Assert.assertEquals("header", result.get(1).getGroup());
50  1 Assert.assertEquals(10, result.get(1).getStart());
51  1 Assert.assertEquals(16, result.get(1).getEnd());
52    }
53   
 
54  1 toggle @Test
55    public void testFindWithoutMatches()
56    {
57  1 RegexTool tool = new RegexTool();
58  1 List<RegexResult> result = tool.find("nothing here", "something");
59   
60  1 Assert.assertEquals(0, result.size());
61    }
62   
 
63  1 toggle @Test
64    public void findAll()
65    {
66  1 RegexTool tool = new RegexTool();
67  1 List<List<RegexResult>> result =
68    tool.findAll("one :two three (:four) five :six seven=:eight", ":(\\w+) (\\w+)");
69   
70  1 Assert.assertEquals(2, result.size());
71  1 Assert.assertEquals(":two three", result.get(0).get(0).getGroup());
72  1 Assert.assertEquals(":six seven", result.get(1).get(0).getGroup());
73   
74  1 Assert.assertEquals(3, result.get(0).size());
75  1 Assert.assertEquals("two", result.get(0).get(1).getGroup());
76  1 Assert.assertEquals("three", result.get(0).get(2).getGroup());
77   
78  1 Assert.assertEquals(3, result.get(1).size());
79  1 Assert.assertEquals("six", result.get(1).get(1).getGroup());
80  1 Assert.assertEquals("seven", result.get(1).get(2).getGroup());
81    }
82   
 
83  1 toggle @Test
84    public void findAllNoMatch()
85    {
86  1 RegexTool tool = new RegexTool();
87  1 List<List<RegexResult>> result = tool.findAll("nothing here", "something");
88   
89  1 Assert.assertEquals(0, result.size());
90    }
91   
92    /**
93    * Compiling a valid regular expression should work.
94    */
 
95  1 toggle @Test
96    public void testCompileValidRegex()
97    {
98  1 RegexTool tool = new RegexTool();
99  1 Pattern p = tool.compile("ab?");
100  1 Assert.assertNotNull(p);
101    // Try to check that the right regular expression was parsed.
102  1 Matcher m = p.matcher("xyz");
103  1 Assert.assertFalse(m.matches());
104  1 m = p.matcher("a");
105  1 Assert.assertTrue(m.matches());
106  1 m = p.matcher("aba");
107  1 Assert.assertFalse(m.matches());
108  1 m.reset();
109  1 Assert.assertTrue(m.find() && m.find() && m.hitEnd());
110    }
111   
112    /**
113    * Compiling a valid regular expression with internal flags should work.
114    */
 
115  1 toggle @Test
116    public void testCompileRegexWithFlags()
117    {
118  1 RegexTool tool = new RegexTool();
119  1 Pattern p = tool.compile("(?im)^ab?$");
120  1 Assert.assertNotNull(p);
121    // Try to check that the right regular expression was parsed.
122  1 Matcher m = p.matcher("xyz");
123  1 Assert.assertFalse(m.matches());
124  1 m = p.matcher("A");
125  1 Assert.assertTrue(m.matches());
126  1 m = p.matcher("ab\na");
127  1 Assert.assertFalse(m.matches());
128  1 m.reset();
129  1 Assert.assertTrue(m.find() && m.find() && m.hitEnd());
130    }
131   
132    /**
133    * Compiling an invalid regular expression should return null, and not throw an exception.
134    */
 
135  1 toggle @Test
136    public void testCompileInvalidRegex()
137    {
138  1 RegexTool tool = new RegexTool();
139  1 Pattern p = tool.compile("*");
140  1 Assert.assertNull(p);
141    }
142   
143    /**
144    * Escaping a string containing regex syntax characters.
145    */
 
146  1 toggle @Test
147    public void testQuote()
148    {
149  1 RegexTool tool = new RegexTool();
150  1 Assert.assertEquals(Pattern.quote("^(\\)[]"), tool.quote("^(\\)[]"));
151    }
152   
 
153  1 toggle @Test
154    public void testQuoteReplacement()
155    {
156  1 RegexTool tool = new RegexTool();
157  1 Assert.assertEquals(Matcher.quoteReplacement("$1 \\$2"), tool.quoteReplacement("$1 \\$2"));
158    }
159    }