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