1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.jsieve.comparator;
21  
22  import junit.framework.TestCase;
23  
24  import org.apache.jsieve.comparators.ComparatorUtils;
25  import org.apache.jsieve.exception.SievePatternException;
26  
27  public class ComparatorUtilsTest extends TestCase {
28  
29      public void testMatchesStringString() throws SievePatternException {
30          String sievematch = "[test] ?\\\\?\\?*\\\\*\\*\\";
31          assertTrue(ComparatorUtils.matches("[test] a\\x?foo\\bar*\\",
32                  sievematch));
33          assertFalse(ComparatorUtils.matches("[test] ab\\x?foo\\bar*\\",
34                  sievematch));
35          assertFalse(ComparatorUtils.matches("[test]a\\x?foo\\bar*\\",
36                  sievematch));
37          assertFalse(ComparatorUtils.matches("[tst] a\\x?foo\\bar*\\",
38                  sievematch));
39          assertFalse(ComparatorUtils.matches("[test] a\\\\x?foo\\bar*\\",
40                  sievematch));
41          assertFalse(ComparatorUtils.matches("[test] a\\?foo\\bar*\\",
42                  sievematch));
43          assertFalse(ComparatorUtils.matches("[test] a\\xafoo\\bar*\\",
44                  sievematch));
45          assertTrue(ComparatorUtils.matches("[test] a\\x?\\bar*\\", sievematch));
46          assertTrue(ComparatorUtils.matches("[test] a\\x?foo\\\\bar*\\",
47                  sievematch));
48          assertFalse(ComparatorUtils
49                  .matches("[test] a\\x?foobar*\\", sievematch));
50          assertFalse(ComparatorUtils.matches("[test] a\\x?foo\\bar.\\",
51                  sievematch));
52          assertFalse(ComparatorUtils.matches("[test] a\\x?foo\\bar*\\\\",
53                  sievematch));
54          assertFalse(ComparatorUtils
55                  .matches("[test] a\\x?foo\\bar*", sievematch));
56      }
57  
58      /**
59       * The ":matches" version specifies a wildcard match using the characters
60       * "*" and "?". "*" matches zero or more characters, and "?" matches a
61       * single character. "?" and "*" may be escaped as "\\?" and "\\*" in
62       * strings to match against themselves. The first backslash escapes the
63       * second backslash; together, they escape the "*". This is awkward, but it
64       * is commonplace in several programming languages that use globs and
65       * regular expressions.
66       */
67      public void testSieveToJavaRegex() {
68          String sievematch = "[test] ?\\\\?\\?*\\\\*\\*\\";
69          String res = ComparatorUtils.sieveToJavaRegex(sievematch);
70          String expected = "\\[test\\] .\\\\.\\?.*\\\\.*\\*\\\\";
71          assertEquals(expected, res);
72      }
73  
74  }