View Javadoc
1   package com.atlassian.plugin.util;
2   
3   import org.junit.Test;
4   
5   import java.util.Arrays;
6   import java.util.regex.Pattern;
7   
8   import static org.hamcrest.MatcherAssert.assertThat;
9   import static org.hamcrest.core.Is.is;
10  
11  public class TestRegularExpressions {
12      @Test
13      public void anyOfEmptyDoesntMatchAnything() {
14          final String compound = RegularExpressions.anyOf(Arrays.<String>asList());
15          final Pattern pattern = Pattern.compile(compound);
16          assertThat(pattern.matcher("").matches(), is(false));
17          assertThat(pattern.matcher("a").matches(), is(false));
18          // Yes, the next few are a little whitebox, but they're designed to pick up likely implementation faults
19          assertThat(pattern.matcher("a/A").matches(), is(false));
20          assertThat(pattern.matcher("a\\A").matches(), is(false));
21          assertThat(pattern.matcher("a\\\\A").matches(), is(false));
22      }
23  
24      @Test
25      public void anyOfOneMatchesIt() {
26          final String compound = RegularExpressions.anyOf(Arrays.asList("a+"));
27          final Pattern pattern = Pattern.compile(compound);
28          assertThat(pattern.matcher("").matches(), is(false));
29          assertThat(pattern.matcher("a").matches(), is(true));
30          assertThat(pattern.matcher("aa").matches(), is(true));
31          assertThat(pattern.matcher("b").matches(), is(false));
32          assertThat(pattern.matcher("bb").matches(), is(false));
33          assertThat(pattern.matcher("ab").matches(), is(false));
34      }
35  
36      @Test
37      public void anyOfTwoMatchesEither() {
38          final String compound = RegularExpressions.anyOf(Arrays.asList("a+", "b+"));
39          final Pattern pattern = Pattern.compile(compound);
40          assertThat(pattern.matcher("").matches(), is(false));
41          assertThat(pattern.matcher("a").matches(), is(true));
42          assertThat(pattern.matcher("aa").matches(), is(true));
43          assertThat(pattern.matcher("b").matches(), is(true));
44          assertThat(pattern.matcher("bb").matches(), is(true));
45          assertThat(pattern.matcher("ab").matches(), is(false));
46          assertThat(pattern.matcher("c").matches(), is(false));
47          assertThat(pattern.matcher("cc").matches(), is(false));
48      }
49  
50      @Test
51      public void anyOfWithOverlap() {
52          final String compound = RegularExpressions.anyOf(Arrays.asList("a(a|b)+b", "a(a|b)+b"));
53          final Pattern pattern = Pattern.compile(compound);
54          assertThat(pattern.matcher("").matches(), is(false));
55          assertThat(pattern.matcher("ab").matches(), is(false));
56          assertThat(pattern.matcher("aab").matches(), is(true));
57          assertThat(pattern.matcher("abb").matches(), is(true));
58          assertThat(pattern.matcher("acb").matches(), is(false));
59      }
60  
61  
62      @Test
63      public void naiveComplexExample() {
64          final String compound = RegularExpressions.anyOf(Arrays.asList("ab", "c(d|e)f", "gh*g+"));
65          final Pattern pattern = Pattern.compile(compound);
66          assertThat(pattern.matcher("").matches(), is(false));
67          assertThat(pattern.matcher("a").matches(), is(false));
68          assertThat(pattern.matcher("ab").matches(), is(true));
69          assertThat(pattern.matcher("cf").matches(), is(false));
70          assertThat(pattern.matcher("cdf").matches(), is(true));
71          assertThat(pattern.matcher("cef").matches(), is(true));
72          assertThat(pattern.matcher("cdef").matches(), is(false));
73          assertThat(pattern.matcher("g").matches(), is(false));
74          assertThat(pattern.matcher("gg").matches(), is(true));
75          assertThat(pattern.matcher("ghg").matches(), is(true));
76          assertThat(pattern.matcher("ghgh").matches(), is(false));
77      }
78  }