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    *      http://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  package org.apache.commons.validator.routines;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  
23  import java.math.BigDecimal;
24  import java.util.Locale;
25  
26  import org.junit.jupiter.api.AfterEach;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Test Case for PercentValidator.
32   */
33  public class PercentValidatorTest {
34  
35      protected PercentValidator validator;
36  
37      @BeforeEach
38      protected void setUp() {
39          validator = new PercentValidator();
40      }
41  
42      /**
43       * Tear down
44       */
45      @AfterEach
46      protected void tearDown() {
47          validator = null;
48      }
49  
50      /**
51       * Test Format Type
52       */
53      @Test
54      public void testFormatType() {
55          assertEquals(2, PercentValidator.getInstance().getFormatType(), "Format Type A");
56          assertEquals(AbstractNumberValidator.PERCENT_FORMAT, PercentValidator.getInstance().getFormatType(), "Format Type B");
57      }
58  
59      /**
60       * Test Invalid percentage values
61       */
62      @Test
63      public void testInvalid() {
64          final BigDecimalValidator validator = PercentValidator.getInstance();
65  
66          // Invalid Missing
67          assertFalse(validator.isValid(null), "isValid() Null Value");
68          assertFalse(validator.isValid(""), "isValid() Empty Value");
69          assertNull(validator.validate(null), "validate() Null Value");
70          assertNull(validator.validate(""), "validate() Empty Value");
71  
72          // Invalid UK
73          assertFalse(validator.isValid("12@", Locale.UK), "UK wrong symbol"); // ???
74          assertFalse(validator.isValid("(12%)", Locale.UK), "UK wrong negative");
75  
76          // Invalid US - can't find a Locale with different symbols!
77          assertFalse(validator.isValid("12@", Locale.US), "US wrong symbol"); // ???
78          assertFalse(validator.isValid("(12%)", Locale.US), "US wrong negative");
79      }
80  
81      /**
82       * Test Valid percentage values
83       */
84      @Test
85      public void testValid() {
86          // Set the default Locale
87          final Locale origDefault = Locale.getDefault();
88          Locale.setDefault(Locale.UK);
89  
90          final BigDecimalValidator validator = PercentValidator.getInstance();
91          final BigDecimal expected = new BigDecimal("0.12");
92          final BigDecimal negative = new BigDecimal("-0.12");
93          final BigDecimal hundred = new BigDecimal("1.00");
94  
95          assertEquals(expected, validator.validate("12%"), "Default locale");
96          assertEquals(negative, validator.validate("-12%"), "Default negtve");
97  
98          // Invalid UK
99          assertEquals(expected, validator.validate("12%", Locale.UK), "UK locale");
100         assertEquals(negative, validator.validate("-12%", Locale.UK), "UK negative");
101         assertEquals(expected, validator.validate("12", Locale.UK), "UK No symbol");
102 
103         // Invalid US - can't find a Locale with different symbols!
104         assertEquals(expected, validator.validate("12%", Locale.US), "US locale");
105         assertEquals(negative, validator.validate("-12%", Locale.US), "US negative");
106         assertEquals(expected, validator.validate("12", Locale.US), "US No symbol");
107 
108         assertEquals(hundred, validator.validate("100%"), "100%");
109 
110         // Restore the original default
111         Locale.setDefault(origDefault);
112     }
113 
114 }