1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
25
26
27
28 public class PercentValidatorTest extends TestCase {
29
30 protected PercentValidator validator;
31
32
33
34
35
36 public PercentValidatorTest(String name) {
37 super(name);
38 }
39
40 @Override
41 protected void setUp() throws Exception {
42 super.setUp();
43 validator = new PercentValidator();
44 }
45
46
47
48
49
50 @Override
51 protected void tearDown() throws Exception {
52 super.tearDown();
53 validator = null;
54 }
55
56
57
58
59 public void testFormatType() {
60 assertEquals("Format Type A", 2, PercentValidator.getInstance().getFormatType());
61 assertEquals("Format Type B", AbstractNumberValidator.PERCENT_FORMAT, PercentValidator.getInstance().getFormatType());
62 }
63
64
65
66
67 public void testValid() {
68
69 Locale origDefault = Locale.getDefault();
70 Locale.setDefault(Locale.UK);
71
72 BigDecimalValidator validator = PercentValidator.getInstance();
73 BigDecimal expected = new BigDecimal("0.12");
74 BigDecimal negative = new BigDecimal("-0.12");
75 BigDecimal hundred = new BigDecimal("1.00");
76
77 assertEquals("Default locale", expected, validator.validate("12%"));
78 assertEquals("Default negtve", negative, validator.validate("-12%"));
79
80
81 assertEquals("UK locale", expected, validator.validate("12%", Locale.UK));
82 assertEquals("UK negative", negative, validator.validate("-12%", Locale.UK));
83 assertEquals("UK No symbol", expected, validator.validate("12", Locale.UK));
84
85
86 assertEquals("US locale", expected, validator.validate("12%", Locale.US));
87 assertEquals("US negative", negative, validator.validate("-12%", Locale.US));
88 assertEquals("US No symbol", expected, validator.validate("12", Locale.US));
89
90 assertEquals("100%", hundred, validator.validate("100%"));
91
92
93 Locale.setDefault(origDefault);
94 }
95
96
97
98
99 public void testInvalid() {
100 BigDecimalValidator validator = PercentValidator.getInstance();
101
102
103 assertFalse("isValid() Null Value", validator.isValid(null));
104 assertFalse("isValid() Empty Value", validator.isValid(""));
105 assertNull("validate() Null Value", validator.validate(null));
106 assertNull("validate() Empty Value", validator.validate(""));
107
108
109 assertFalse("UK wrong symbol", validator.isValid("12@", Locale.UK));
110 assertFalse("UK wrong negative", validator.isValid("(12%)", Locale.UK));
111
112
113 assertFalse("US wrong symbol", validator.isValid("12@", Locale.US));
114 assertFalse("US wrong negative", validator.isValid("(12%)", Locale.US));
115 }
116
117 }