View Javadoc
1   package org.apache.commons.jcs3.engine.match;
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 junit.framework.TestCase;
23  
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  /** Unit tests for the key matcher. */
28  public class KeyMatcherPatternImpllUnitTest
29      extends TestCase
30  {
31      /**
32       * Verify that the matching method works.
33       */
34      public void testGetMatchingKeysFromArray_AllMatch()
35      {
36          // SETUP
37          final int numToInsertPrefix1 = 10;
38          final Set<String> keyArray = new HashSet<>();
39  
40          final String keyprefix1 = "MyPrefixC";
41  
42          // insert with prefix1
43          for ( int i = 0; i < numToInsertPrefix1; i++ )
44          {
45              keyArray.add(keyprefix1 + String.valueOf( i ));
46          }
47  
48          final KeyMatcherPatternImpl<String> keyMatcher = new KeyMatcherPatternImpl<>();
49  
50          // DO WORK
51          final Set<String> result1 = keyMatcher.getMatchingKeysFromArray( keyprefix1 + ".", keyArray );
52  
53          // VERIFY
54          assertEquals( "Wrong number returned 1: " + result1, numToInsertPrefix1, result1.size() );
55      }
56  
57      /**
58       * Verify that the matching method works.
59       */
60      public void testGetMatchingKeysFromArray_AllMatchFirstNull()
61      {
62          // SETUP
63          final int numToInsertPrefix1 = 10;
64          final Set<String> keyArray = new HashSet<>();
65  
66          final String keyprefix1 = "MyPrefixC";
67  
68          // insert with prefix1
69          for ( int i = 1; i < numToInsertPrefix1 + 1; i++ )
70          {
71              keyArray.add(keyprefix1 + String.valueOf( i ));
72          }
73  
74          final KeyMatcherPatternImpl<String> keyMatcher = new KeyMatcherPatternImpl<>();
75  
76          // DO WORK
77          final Set<String> result1 = keyMatcher.getMatchingKeysFromArray( keyprefix1 + "\\S+", keyArray );
78  
79          // VERIFY
80          assertEquals( "Wrong number returned 1: " + result1, numToInsertPrefix1, result1.size() );
81      }
82  
83      /**
84       * Verify that the matching method works.
85       */
86      public void testGetMatchingKeysFromArray_TwoTypes()
87      {
88          // SETUP
89          final int numToInsertPrefix1 = 10;
90          final int numToInsertPrefix2 = 50;
91          final Set<String> keyArray = new HashSet<>();
92  
93          final String keyprefix1 = "MyPrefixA";
94          final String keyprefix2 = "MyPrefixB";
95  
96          // insert with prefix1
97          for ( int i = 0; i < numToInsertPrefix1; i++ )
98          {
99              keyArray.add(keyprefix1 + String.valueOf( i ));
100         }
101 
102         // insert with prefix2
103         for ( int i = numToInsertPrefix1; i < numToInsertPrefix2 + numToInsertPrefix1; i++ )
104         {
105             keyArray.add(keyprefix2 + String.valueOf( i ));
106         }
107 
108         final KeyMatcherPatternImpl<String> keyMatcher = new KeyMatcherPatternImpl<>();
109 
110         // DO WORK
111         final Set<String> result1 = keyMatcher.getMatchingKeysFromArray( keyprefix1 + ".+", keyArray );
112         final Set<String> result2 = keyMatcher.getMatchingKeysFromArray( keyprefix2 + ".+", keyArray );
113 
114         // VERIFY
115         assertEquals( "Wrong number returned 1: " + result1, numToInsertPrefix1, result1.size() );
116         assertEquals( "Wrong number returned 2: " + result2, numToInsertPrefix2, result2.size() );
117     }
118 }