1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.commons.validator.routines;
22
23 import junit.framework.TestCase;
24
25 import java.util.Locale;
26 import java.math.BigDecimal;
27 /***
28 * Test Case for PercentValidator.
29 *
30 * @version $Revision: 371174 $ $Date: 2006-01-22 03:24:40 +0000 (Sun, 22 Jan 2006) $
31 */
32 public class PercentValidatorTest extends TestCase {
33
34 protected PercentValidator validator;
35
36 /***
37 * Main
38 * @param args arguments
39 */
40 public static void main(String[] args) {
41 junit.textui.TestRunner.run(PercentValidatorTest.class);
42 }
43
44 /***
45 * Constructor
46 * @param name test name
47 */
48 public PercentValidatorTest(String name) {
49 super(name);
50 }
51
52 protected void setUp() throws Exception {
53 super.setUp();
54 validator = new PercentValidator();
55 }
56
57 /***
58 * Tear down
59 * @throws Exception
60 */
61 protected void tearDown() throws Exception {
62 super.tearDown();
63 validator = null;
64 }
65
66 /***
67 * Test Format Type
68 */
69 public void testFormatType() {
70 assertEquals("Format Type A", 2, PercentValidator.getInstance().getFormatType());
71 assertEquals("Format Type B", PercentValidator.PERCENT_FORMAT, PercentValidator.getInstance().getFormatType());
72 }
73
74 /***
75 * Test Valid percentage values
76 */
77 public void testValid() {
78
79 Locale origDefault = Locale.getDefault();
80 Locale.setDefault(Locale.UK);
81
82 BigDecimalValidator validator = PercentValidator.getInstance();
83 BigDecimal expected = new BigDecimal("0.12");
84 BigDecimal negative = new BigDecimal("-0.12");
85 BigDecimal hundred = new BigDecimal("1.00");
86
87 assertEquals("Default locale", expected, validator.validate("12%"));
88 assertEquals("Default negtve", negative, validator.validate("-12%"));
89
90
91 assertEquals("UK locale", expected, validator.validate("12%", Locale.UK));
92 assertEquals("UK negative", negative, validator.validate("-12%", Locale.UK));
93 assertEquals("UK No symbol", expected, validator.validate("12", Locale.UK));
94
95
96 assertEquals("US locale", expected, validator.validate("12%", Locale.US));
97 assertEquals("US negative", negative, validator.validate("-12%", Locale.US));
98 assertEquals("US No symbol", expected, validator.validate("12", Locale.US));
99
100 assertEquals("100%", hundred, validator.validate("100%"));
101
102
103 Locale.setDefault(origDefault);
104 }
105
106 /***
107 * Test Invalid percentage values
108 */
109 public void testInvalid() {
110 BigDecimalValidator validator = PercentValidator.getInstance();
111
112
113 assertFalse("isValid() Null Value", validator.isValid(null));
114 assertFalse("isValid() Empty Value", validator.isValid(""));
115 assertNull("validate() Null Value", validator.validate(null));
116 assertNull("validate() Empty Value", validator.validate(""));
117
118
119 assertFalse("UK wrong symbol", validator.isValid("12@", Locale.UK));
120 assertFalse("UK wrong negative", validator.isValid("(12%)", Locale.UK));
121
122
123 assertFalse("US wrong symbol", validator.isValid("12@", Locale.US));
124 assertFalse("US wrong negative", validator.isValid("(12%)", Locale.US));
125 }
126
127 }