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  package org.apache.jsieve.comparators;
20  
21  import junit.framework.TestCase;
22  
23  import org.apache.jsieve.exception.FeatureException;
24  import org.apache.jsieve.parser.generated.ParseException;
25  import org.apache.jsieve.utils.JUnitUtils;
26  
27  public class AsciiNumericTest extends TestCase {
28      
29      AsciiNumeric subject;
30  
31      protected void setUp() throws Exception {
32          super.setUp();
33          subject = new AsciiNumeric();
34      }
35  
36      protected void tearDown() throws Exception {
37          super.tearDown();
38      }
39  
40      public void testVerificationFailsWhenAsciiNumericIsNotRequired() throws Exception {
41          String script = "if header :contains :comparator \"i;ascii-numeric\" \"Subject\" \"69\" {stop;}";
42          try {
43              JUnitUtils.interpret(JUnitUtils.createMail(), script);
44              fail("Comparator must be declared in require statement");
45          } catch (ParseException e) {
46              // Expected
47          }
48      }
49      
50      public void testVerificationPassesWhenAsciiNumericIsRequired() throws Exception {
51          String script = "require [\"comparator-i;ascii-numeric\"]; if header :is :comparator \"i;ascii-numeric\" \"Subject\" \"69\" {stop;}";
52          JUnitUtils.interpret(JUnitUtils.createMail(), script);
53      }
54      
55      public void testBasicNumbericEquality() throws Exception {
56          assertFalse(subject.equals("1", "2"));
57          assertTrue(subject.equals("1", "1"));
58      }
59      
60      public void testEqualityShouldIgnoreTrailingCharacters() throws Exception {
61          assertTrue(subject.equals("01", "1A"));
62          assertTrue(subject.equals("1", "00000000000000001A"));
63          assertTrue(subject.equals("234S", "234YTGSDBBSD"));
64      }
65      
66      public void testEqualityShouldIgnoreLeadingZeros() throws Exception {
67          assertTrue(subject.equals("01", "1"));
68          assertTrue(subject.equals("000001", "1"));
69          assertFalse(subject.equals("000001", "10"));
70      }
71      
72      public void testStingsThatDoNotStartWithADigitRepresentPositiveInfinityWhenUsedInEquality() throws Exception {
73          assertFalse(subject.equals("1", "A4"));
74          assertFalse(subject.equals("x", "4"));
75          assertTrue(subject.equals("GT", "A4"));
76      }
77      
78      public void testSubstringIsNotSupported() throws Exception {
79          try {
80              subject.contains("234234", "34");
81              fail("Substring is unsupported");
82          } catch (FeatureException e) {
83              // Expected
84          }
85      }
86      
87      public void testMatchNotSupported() throws Exception {
88          try {
89              subject.matches("234234", "34");
90              fail("Substring is unsupported");
91          } catch (FeatureException e) {
92              // Expected
93          }
94      }
95  }