View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.text.matcher;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * Tests {@link StringMatcherFactory}.
27   */
28  class StringMatcherFactoryTest {
29  
30      private static final class StringMatcherDefaults implements StringMatcher {
31  
32          @Override
33          public int isMatch(final char[] buffer, final int start, final int bufferStart, final int bufferEnd) {
34              return 2;
35          }
36  
37      }
38  
39      @Test
40      void test_andMatcher() {
41          assertNotNull(StringMatcherFactory.INSTANCE.andMatcher(StringMatcherFactory.INSTANCE.charMatcher('1'),
42              StringMatcherFactory.INSTANCE.stringMatcher("2")));
43          assertNotNull(StringMatcherFactory.INSTANCE.andMatcher(null, StringMatcherFactory.INSTANCE.stringMatcher("2")));
44          assertNotNull(StringMatcherFactory.INSTANCE.andMatcher(null, null));
45          StringMatcher andMatcher = StringMatcherFactory.INSTANCE.andMatcher();
46          assertNotNull(andMatcher);
47          assertEquals(0, andMatcher.size());
48          andMatcher = StringMatcherFactory.INSTANCE.andMatcher(StringMatcherFactory.INSTANCE.charMatcher('1'));
49          assertNotNull(andMatcher);
50          assertEquals(1, andMatcher.size());
51      }
52  
53      @Test
54      void test_charMatcher() {
55          final StringMatcher charMatcher = StringMatcherFactory.INSTANCE.charMatcher('1');
56          assertNotNull(charMatcher);
57          assertNotNull(charMatcher.toString());
58          assertEquals(1, charMatcher.size());
59      }
60  
61      @Test
62      void test_charSetMatcher_char() {
63          final StringMatcher charSetMatcher = StringMatcherFactory.INSTANCE.charSetMatcher('1');
64          assertNotNull(charSetMatcher);
65          assertNotNull(charSetMatcher.toString());
66          assertEquals(1, charSetMatcher.size());
67      }
68  
69      @Test
70      void test_charSetMatcher_String() {
71          final StringMatcher charSetMatcher = StringMatcherFactory.INSTANCE.charSetMatcher("1");
72          assertNotNull(charSetMatcher);
73          assertNotNull(charSetMatcher.toString());
74          assertEquals(1, charSetMatcher.size());
75      }
76  
77      @Test
78      void test_commaMatcher() {
79          final StringMatcher commaMatcher = StringMatcherFactory.INSTANCE.commaMatcher();
80          assertNotNull(commaMatcher);
81          assertNotNull(commaMatcher.toString());
82          assertEquals(1, commaMatcher.size());
83      }
84  
85      @Test
86      void test_doubleQuoteMatcher() {
87          final StringMatcher doubleQuoteMatcher = StringMatcherFactory.INSTANCE.doubleQuoteMatcher();
88          assertNotNull(doubleQuoteMatcher);
89          assertNotNull(doubleQuoteMatcher.toString());
90          assertEquals(1, doubleQuoteMatcher.size());
91      }
92  
93      @Test
94      void test_noneMatcher() {
95          final StringMatcher noneMatcher = StringMatcherFactory.INSTANCE.noneMatcher();
96          assertNotNull(noneMatcher);
97          assertNotNull(noneMatcher.toString());
98          assertEquals(0, noneMatcher.size());
99      }
100 
101     @Test
102     void test_quoteMatcher() {
103         final StringMatcher quoteMatcher = StringMatcherFactory.INSTANCE.quoteMatcher();
104         assertNotNull(quoteMatcher);
105         assertNotNull(quoteMatcher.toString());
106         assertEquals(1, quoteMatcher.size());
107     }
108 
109     @Test
110     void test_singleQuoteMatcher() {
111         final StringMatcher singleQuoteMatcher = StringMatcherFactory.INSTANCE.singleQuoteMatcher();
112         assertNotNull(singleQuoteMatcher);
113         assertNotNull(singleQuoteMatcher.toString());
114         assertEquals(1, singleQuoteMatcher.size());
115     }
116 
117     @Test
118     void test_spaceMatcher() {
119         final StringMatcher spaceMatcher = StringMatcherFactory.INSTANCE.spaceMatcher();
120         assertNotNull(spaceMatcher);
121         assertNotNull(spaceMatcher.toString());
122         assertEquals(1, spaceMatcher.size());
123     }
124 
125     @Test
126     void test_splitMatcher() {
127         final StringMatcher splitMatcher = StringMatcherFactory.INSTANCE.splitMatcher();
128         assertNotNull(splitMatcher);
129         assertNotNull(splitMatcher.toString());
130         assertEquals(1, splitMatcher.size());
131     }
132 
133     @Test
134     void test_stringMatcher() {
135         StringMatcher stringMatcher = StringMatcherFactory.INSTANCE.stringMatcher("1");
136         assertNotNull(stringMatcher);
137         assertNotNull(stringMatcher.toString());
138         assertEquals(1, stringMatcher.size());
139         //
140         stringMatcher = StringMatcherFactory.INSTANCE.stringMatcher();
141         assertNotNull(stringMatcher);
142         assertNotNull(stringMatcher.toString());
143         assertEquals(0, stringMatcher.size());
144     }
145 
146     @Test
147     void test_stringMatcherChars() {
148         StringMatcher stringMatcher = StringMatcherFactory.INSTANCE.stringMatcher('1', '2');
149         assertNotNull(stringMatcher);
150         assertNotNull(stringMatcher.toString());
151         assertEquals(2, stringMatcher.size());
152         //
153         stringMatcher = StringMatcherFactory.INSTANCE.stringMatcher('1');
154         assertNotNull(stringMatcher);
155         assertNotNull(stringMatcher.toString());
156         assertEquals(1, stringMatcher.size());
157         //
158         stringMatcher = StringMatcherFactory.INSTANCE.stringMatcher();
159         assertNotNull(stringMatcher);
160         assertNotNull(stringMatcher.toString());
161         assertEquals(0, stringMatcher.size());
162     }
163 
164     @Test
165     void test_tabMatcher() {
166         final StringMatcher charMatcher = StringMatcherFactory.INSTANCE.charMatcher('1');
167         assertNotNull(charMatcher);
168         assertNotNull(charMatcher.toString());
169         assertEquals(1, charMatcher.size());
170     }
171 
172     @Test
173     void test_trimMatcher() {
174         final StringMatcher charMatcher = StringMatcherFactory.INSTANCE.charMatcher('1');
175         assertNotNull(charMatcher);
176         assertNotNull(charMatcher.toString());
177         assertEquals(1, charMatcher.size());
178     }
179 
180     @Test
181     void testDefaultMethods() {
182         final StringMatcherDefaults stringMatcher = new StringMatcherDefaults();
183         assertEquals(0, stringMatcher.size());
184         assertEquals(2, stringMatcher.isMatch("1", 0));
185     }
186 
187 }