1 package org.apache.maven.surefire.its.fixture;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.hamcrest.BaseMatcher;
23 import org.hamcrest.Description;
24 import org.hamcrest.Matcher;
25
26 import java.util.Collections;
27 import java.util.Iterator;
28 import java.util.Set;
29
30 import static java.util.Collections.singleton;
31
32
33
34
35 public final class IsRegex
36 extends BaseMatcher<Set<String>>
37 {
38 public static Matcher<Set<String>> regex( Set<String> expectedRegex )
39 {
40 return new IsRegex( expectedRegex );
41 }
42
43 public static Matcher<Set<String>> regex( String expectedRegex )
44 {
45 return new IsRegex( expectedRegex );
46 }
47
48 private final Set<String> expectedRegex;
49
50 private IsRegex( String expectedRegex )
51 {
52 this.expectedRegex = singleton( expectedRegex );
53 }
54
55 private IsRegex( Set<String> expectedRegex )
56 {
57 this.expectedRegex = expectedRegex;
58 }
59
60 @Override
61 public boolean matches( Object o )
62 {
63 if ( o != null
64 && expectedRegex.size() == 1 ? isStringOrSet( o ) : isSet( o ) )
65 {
66
67 Set<String> actual = isSet( o ) ? ( Set<String> ) o : singleton( ( String ) o );
68 boolean matches = actual.size() == expectedRegex.size();
69 Iterator<String> regex = expectedRegex.iterator();
70 for ( String s : actual )
71 {
72 if ( s == null || !regex.hasNext() || !s.matches( regex.next() ) )
73 {
74 matches = false;
75 }
76 }
77 return matches;
78 }
79 else
80 {
81 return false;
82 }
83 }
84
85 @Override
86 public void describeTo( Description description )
87 {
88 description.appendValue( expectedRegex );
89 }
90
91 private static boolean isStringOrSet( Object o )
92 {
93 return o instanceof String || o instanceof Set;
94 }
95
96 private static boolean isSet( Object o )
97 {
98 return o instanceof Set;
99 }
100 }