View Javadoc
1   package org.apache.maven.surefire.its.fixture;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Java Hamcrest Matcher with Regex.
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              //noinspection unchecked
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 }