001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.validator.routines;
018:
019: import junit.framework.TestCase;
020:
021: import java.util.Locale;
022: import java.math.BigDecimal;
023:
024: /**
025: * Test Case for PercentValidator.
026: *
027: * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
028: */
029: public class PercentValidatorTest extends TestCase {
030:
031: protected PercentValidator validator;
032:
033: /**
034: * Main
035: * @param args arguments
036: */
037: public static void main(String[] args) {
038: junit.textui.TestRunner.run(PercentValidatorTest.class);
039: }
040:
041: /**
042: * Constructor
043: * @param name test name
044: */
045: public PercentValidatorTest(String name) {
046: super (name);
047: }
048:
049: protected void setUp() throws Exception {
050: super .setUp();
051: validator = new PercentValidator();
052: }
053:
054: /**
055: * Tear down
056: * @throws Exception
057: */
058: protected void tearDown() throws Exception {
059: super .tearDown();
060: validator = null;
061: }
062:
063: /**
064: * Test Format Type
065: */
066: public void testFormatType() {
067: assertEquals("Format Type A", 2, PercentValidator.getInstance()
068: .getFormatType());
069: assertEquals("Format Type B", PercentValidator.PERCENT_FORMAT,
070: PercentValidator.getInstance().getFormatType());
071: }
072:
073: /**
074: * Test Valid percentage values
075: */
076: public void testValid() {
077: // Set the default Locale
078: Locale origDefault = Locale.getDefault();
079: Locale.setDefault(Locale.UK);
080:
081: BigDecimalValidator validator = PercentValidator.getInstance();
082: BigDecimal expected = new BigDecimal("0.12");
083: BigDecimal negative = new BigDecimal("-0.12");
084: BigDecimal hundred = new BigDecimal("1.00");
085:
086: assertEquals("Default locale", expected, validator
087: .validate("12%"));
088: assertEquals("Default negtve", negative, validator
089: .validate("-12%"));
090:
091: // Invalid UK
092: assertEquals("UK locale", expected, validator.validate("12%",
093: Locale.UK));
094: assertEquals("UK negative", negative, validator.validate(
095: "-12%", Locale.UK));
096: assertEquals("UK No symbol", expected, validator.validate("12",
097: Locale.UK));
098:
099: // Invalid US - can't find a Locale with different symbols!
100: assertEquals("US locale", expected, validator.validate("12%",
101: Locale.US));
102: assertEquals("US negative", negative, validator.validate(
103: "-12%", Locale.US));
104: assertEquals("US No symbol", expected, validator.validate("12",
105: Locale.US));
106:
107: assertEquals("100%", hundred, validator.validate("100%"));
108:
109: // Restore the original default
110: Locale.setDefault(origDefault);
111: }
112:
113: /**
114: * Test Invalid percentage values
115: */
116: public void testInvalid() {
117: BigDecimalValidator validator = PercentValidator.getInstance();
118:
119: // Invalid Missing
120: assertFalse("isValid() Null Value", validator.isValid(null));
121: assertFalse("isValid() Empty Value", validator.isValid(""));
122: assertNull("validate() Null Value", validator.validate(null));
123: assertNull("validate() Empty Value", validator.validate(""));
124:
125: // Invalid UK
126: assertFalse("UK wrong symbol", validator.isValid("12@",
127: Locale.UK)); // ???
128: assertFalse("UK wrong negative", validator.isValid("(12%)",
129: Locale.UK));
130:
131: // Invalid US - can't find a Locale with different symbols!
132: assertFalse("US wrong symbol", validator.isValid("12@",
133: Locale.US)); // ???
134: assertFalse("US wrong negative", validator.isValid("(12%)",
135: Locale.US));
136: }
137:
138: }
|