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 junit.framework.TestCase;
20  
21  import java.util.Locale;
22  import java.math.BigDecimal;
23  /***
24   * Test Case for PercentValidator.
25   * 
26   * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
27   */
28  public class PercentValidatorTest extends TestCase {
29  
30      protected PercentValidator validator;
31  
32      /***
33       * Main
34       * @param args arguments
35       */
36      public static void main(String[] args) {
37          junit.textui.TestRunner.run(PercentValidatorTest.class);
38      }
39  
40      /***
41       * Constructor
42       * @param name test name
43       */
44      public PercentValidatorTest(String name) {
45          super(name);
46      }
47  
48      protected void setUp() throws Exception {
49          super.setUp();
50          validator = new PercentValidator();
51      }
52  
53      /***
54       * Tear down
55       * @throws Exception
56       */
57      protected void tearDown() throws Exception {
58          super.tearDown();
59          validator = null;
60      }
61  
62      /***
63       * Test Format Type
64       */
65      public void testFormatType() {
66          assertEquals("Format Type A", 2, PercentValidator.getInstance().getFormatType());
67          assertEquals("Format Type B", PercentValidator.PERCENT_FORMAT, PercentValidator.getInstance().getFormatType());
68      }
69  
70      /***
71       * Test Valid percentage values
72       */
73      public void testValid() {
74          // Set the default Locale
75          Locale origDefault = Locale.getDefault();
76          Locale.setDefault(Locale.UK);
77  
78          BigDecimalValidator validator = PercentValidator.getInstance();
79          BigDecimal expected = new BigDecimal("0.12");
80          BigDecimal negative = new BigDecimal("-0.12");
81          BigDecimal hundred  = new BigDecimal("1.00");
82  
83          assertEquals("Default locale", expected, validator.validate("12%"));
84          assertEquals("Default negtve", negative, validator.validate("-12%"));
85  
86          // Invalid UK
87          assertEquals("UK locale",      expected, validator.validate("12%",   Locale.UK));
88          assertEquals("UK negative",    negative, validator.validate("-12%",  Locale.UK));
89          assertEquals("UK No symbol",   expected, validator.validate("12",    Locale.UK));
90  
91          // Invalid US - can't find a Locale with different symbols!
92          assertEquals("US locale",      expected, validator.validate("12%",   Locale.US));
93          assertEquals("US negative",    negative, validator.validate("-12%",  Locale.US));
94          assertEquals("US No symbol",   expected, validator.validate("12",    Locale.US));
95  
96          assertEquals("100%",           hundred, validator.validate("100%"));
97  
98          // Restore the original default
99          Locale.setDefault(origDefault);
100     }
101 
102     /***
103      * Test Invalid percentage values
104      */
105     public void testInvalid() {
106         BigDecimalValidator validator = PercentValidator.getInstance();
107 
108         // Invalid Missing
109         assertFalse("isValid() Null Value",    validator.isValid(null));
110         assertFalse("isValid() Empty Value",   validator.isValid(""));
111         assertNull("validate() Null Value",    validator.validate(null));
112         assertNull("validate() Empty Value",   validator.validate(""));
113 
114         // Invalid UK
115         assertFalse("UK wrong symbol",    validator.isValid("12@",   Locale.UK)); // ???
116         assertFalse("UK wrong negative",  validator.isValid("(12%)", Locale.UK));
117 
118         // Invalid US - can't find a Locale with different symbols!
119         assertFalse("US wrong symbol",    validator.isValid("12@",   Locale.US)); // ???
120         assertFalse("US wrong negative",  validator.isValid("(12%)", Locale.US));
121     }
122 
123 }