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