You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ni...@apache.org on 2006/01/19 07:38:52 UTC
svn commit: r370397 [4/4] - in /jakarta/commons/proper/validator/trunk: ./
src/share/org/apache/commons/validator/routines/
src/test/org/apache/commons/validator/routines/
Added: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/LongValidatorTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/LongValidatorTest.java?rev=370397&view=auto
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/LongValidatorTest.java (added)
+++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/LongValidatorTest.java Wed Jan 18 22:38:24 2006
@@ -0,0 +1,144 @@
+/*
+ * $Id$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.validator.routines;
+
+import java.util.Locale;
+
+/**
+ * Test Case for LongValidator.
+ *
+ * @version $Revision$ $Date$
+ */
+public class LongValidatorTest extends BaseNumberValidatorTest {
+
+ /**
+ * Main
+ * @param args arguments
+ */
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(LongValidatorTest.class);
+ }
+
+ /**
+ * Constructor
+ * @param name test name
+ */
+ public LongValidatorTest(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ validator = new LongValidator(false);
+ strictValidator = new LongValidator(true);
+
+ testPattern = "#,###";
+
+ // testValidateMinMax()
+ max = null;
+ maxPlusOne = null;
+ min = null;
+ minMinusOne = null;
+
+ // testInvalidStrict()
+ invalidStrict = new String[] {null, "", "X", "X12", "12X", "1X2", "1.2"};
+
+ // testInvalidNotStrict()
+ invalid = new String[] {null, "", "X", "X12"};
+
+ // testValid()
+ testNumber = new Long(1234);
+ testZero = new Long(0);
+ validStrict = new String[] {"0", "1234", "1,234"};
+ validStrictCompare = new Number[] {testZero, testNumber, testNumber};
+ valid = new String[] {"0", "1234", "1,234", "1,234.5", "1234X"};
+ validCompare = new Number[] {testZero, testNumber, testNumber, testNumber, testNumber};
+
+ testStringUS = "1,234";
+ testStringDE = "1.234";
+
+ // Localized Pattern test
+ localeValue = testStringDE;
+ localePattern = "#.###";
+ testLocale = Locale.GERMANY;
+ localeExpected = testNumber;
+
+ }
+
+ /**
+ * Test LongValidator validate Methods
+ */
+ public void testLongValidatorMethods() {
+ Locale locale = Locale.GERMAN;
+ String pattern = "0,00,00";
+ String patternVal = "1,23,45";
+ String localeVal = "12.345";
+ String defaultVal = "12,345";
+ String XXXX = "XXXX";
+ Long expected = new Long(12345);
+ assertEquals("validate(A) default", expected, LongValidator.getInstance().validate(defaultVal));
+ assertEquals("validate(A) locale ", expected, LongValidator.getInstance().validate(localeVal, locale));
+ assertEquals("validate(A) pattern", expected, LongValidator.getInstance().validate(patternVal, pattern));
+
+ assertTrue("isValid(A) default", LongValidator.getInstance().isValid(defaultVal));
+ assertTrue("isValid(A) locale ", LongValidator.getInstance().isValid(localeVal, locale));
+ assertTrue("isValid(A) pattern", LongValidator.getInstance().isValid(patternVal, pattern));
+
+ assertNull("validate(B) default", LongValidator.getInstance().validate(XXXX));
+ assertNull("validate(B) locale ", LongValidator.getInstance().validate(XXXX, locale));
+ assertNull("validate(B) pattern", LongValidator.getInstance().validate(XXXX, pattern));
+
+ assertFalse("isValid(B) default", LongValidator.getInstance().isValid(XXXX));
+ assertFalse("isValid(B) locale ", LongValidator.getInstance().isValid(XXXX, locale));
+ assertFalse("isValid(B) pattern", LongValidator.getInstance().isValid(XXXX, pattern));
+ }
+
+ /**
+ * Test Long Range/Min/Max
+ */
+ public void testLongRangeMinMax() {
+ LongValidator validator = (LongValidator)strictValidator;
+ Long number9 = validator.validate("9", "#");
+ Long number10 = validator.validate("10", "#");
+ Long number11 = validator.validate("11", "#");
+ Long number19 = validator.validate("19", "#");
+ Long number20 = validator.validate("20", "#");
+ Long number21 = validator.validate("21", "#");
+
+ // Test isInRange()
+ assertFalse("isInRange() < min", validator.isInRange(number9, 10, 20));
+ assertTrue("isInRange() = min", validator.isInRange(number10, 10, 20));
+ assertTrue("isInRange() in range", validator.isInRange(number11, 10, 20));
+ assertTrue("isInRange() = max", validator.isInRange(number20, 10, 20));
+ assertFalse("isInRange() > max", validator.isInRange(number21, 10, 20));
+
+ // Test minValue()
+ assertFalse("minValue() < min", validator.minValue(number9, 10));
+ assertTrue("minValue() = min", validator.minValue(number10, 10));
+ assertTrue("minValue() > min", validator.minValue(number11, 10));
+
+ // Test minValue()
+ assertTrue("maxValue() < max", validator.maxValue(number19, 20));
+ assertTrue("maxValue() = max", validator.maxValue(number20, 20));
+ assertFalse("maxValue() > max", validator.maxValue(number21, 20));
+ }
+}
Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/LongValidatorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/LongValidatorTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/PercentValidatorTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/PercentValidatorTest.java?rev=370397&view=auto
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/PercentValidatorTest.java (added)
+++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/PercentValidatorTest.java Wed Jan 18 22:38:24 2006
@@ -0,0 +1,83 @@
+/*
+ * $Id$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.validator.routines;
+
+import junit.framework.TestCase;
+
+import java.util.Locale;
+import java.math.BigDecimal;
+/**
+ * Test Case for PercentValidator.
+ *
+ * @version $Revision$ $Date$
+ */
+public class PercentValidatorTest extends TestCase {
+
+ protected PercentValidator validator;
+
+ /**
+ * Main
+ * @param args arguments
+ */
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(PercentValidatorTest.class);
+ }
+
+ /**
+ * Constructor
+ * @param name test name
+ */
+ public PercentValidatorTest(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ validator = new PercentValidator();
+ }
+
+ /**
+ * Tear down
+ * @throws Exception
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ validator = null;
+ }
+
+ /**
+ * Test Invalid, strict=false
+ */
+ public void testLocalePercentage() {
+ String inputString = null;
+ BigDecimal expected = new BigDecimal("0.12");
+ BigDecimal result = null;
+
+ inputString = "12%";
+ result = validator.validate(inputString, Locale.US);
+ assertEquals("US percentage", expected, result);
+
+ inputString = "12%";
+ result = validator.validate(inputString, Locale.GERMANY);
+ assertEquals("German Percentage", expected, result);
+ }
+
+}
Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/PercentValidatorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/PercentValidatorTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/RoutinesTestSuite.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/RoutinesTestSuite.java?rev=370397&view=auto
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/RoutinesTestSuite.java (added)
+++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/RoutinesTestSuite.java Wed Jan 18 22:38:24 2006
@@ -0,0 +1,62 @@
+/*
+ * $Id$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.validator.routines;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Test suite for <code>org.apache.commons.validator.routines</code>
+ * package.
+ */
+public class RoutinesTestSuite extends TestCase {
+
+ public RoutinesTestSuite(String name) {
+ super(name);
+ }
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite();
+
+ suite.addTestSuite(BigDecimalValidatorTest.class);
+ suite.addTestSuite(BigIntegerValidatorTest.class);
+ suite.addTestSuite(ByteValidatorTest.class);
+ suite.addTestSuite(CalendarValidatorTest.class);
+ suite.addTestSuite(CurrencyValidatorTest.class);
+ suite.addTestSuite(DateValidatorTest.class);
+ suite.addTestSuite(DoubleValidatorTest.class);
+ suite.addTestSuite(FloatValidatorTest.class);
+ suite.addTestSuite(IntegerValidatorTest.class);
+ suite.addTestSuite(LongValidatorTest.class);
+ suite.addTestSuite(PercentValidatorTest.class);
+ suite.addTestSuite(ShortValidatorTest.class);
+ suite.addTestSuite(TimeValidatorTest.class);
+
+ return suite;
+ }
+
+ public static void main(String args[]) {
+ junit.textui.TestRunner.run(suite());
+ }
+
+}
Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/RoutinesTestSuite.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/RoutinesTestSuite.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/ShortValidatorTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/ShortValidatorTest.java?rev=370397&view=auto
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/ShortValidatorTest.java (added)
+++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/ShortValidatorTest.java Wed Jan 18 22:38:24 2006
@@ -0,0 +1,146 @@
+/*
+ * $Id$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.validator.routines;
+
+import java.util.Locale;
+
+/**
+ * Test Case for ShortValidator.
+ *
+ * @version $Revision$ $Date$
+ */
+public class ShortValidatorTest extends BaseNumberValidatorTest {
+
+ /**
+ * Main
+ * @param args arguments
+ */
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(ShortValidatorTest.class);
+ }
+
+ /**
+ * Constructor
+ * @param name test name
+ */
+ public ShortValidatorTest(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ validator = new ShortValidator(false);
+ strictValidator = new ShortValidator(true);
+
+ testPattern = "#,###";
+
+ // testValidateMinMax()
+ max = new Short(Short.MAX_VALUE);
+ maxPlusOne = new Long(max.longValue() + 1);
+ min = new Short(Short.MIN_VALUE);
+ minMinusOne = new Long(min.longValue() - 1);
+
+ // testInvalidStrict()
+ invalidStrict = new String[] {null, "", "X", "X12", "12X", "1X2", "1.2"};
+
+ // testInvalidNotStrict()
+ invalid = new String[] {null, "", "X", "X12"};
+
+ // testValid()
+ testNumber = new Short((short)1234);
+ testZero = new Short((short)0);
+ validStrict = new String[] {"0", "1234", "1,234"};
+ validStrictCompare = new Number[] {testZero, testNumber, testNumber};
+ valid = new String[] {"0", "1234", "1,234", "1,234.5", "1234X"};
+ validCompare = new Number[] {testZero, testNumber, testNumber, testNumber, testNumber};
+
+ testStringUS = "1,234";
+ testStringDE = "1.234";
+
+ // Localized Pattern test
+ localeValue = testStringDE;
+ localePattern = "#.###";
+ testLocale = Locale.GERMANY;
+ localeExpected = testNumber;
+
+ }
+
+ /**
+ * Test ShortValidator validate Methods
+ */
+ public void testShortValidatorMethods() {
+ Locale locale = Locale.GERMAN;
+ String pattern = "0,00,00";
+ String patternVal = "1,23,45";
+ String localeVal = "12.345";
+ String defaultVal = "12,345";
+ String XXXX = "XXXX";
+ Short expected = new Short((short)12345);
+ assertEquals("validate(A) default", expected, ShortValidator.getInstance().validate(defaultVal));
+ assertEquals("validate(A) locale ", expected, ShortValidator.getInstance().validate(localeVal, locale));
+ assertEquals("validate(A) pattern", expected, ShortValidator.getInstance().validate(patternVal, pattern));
+
+ assertTrue("isValid(A) default", ShortValidator.getInstance().isValid(defaultVal));
+ assertTrue("isValid(A) locale ", ShortValidator.getInstance().isValid(localeVal, locale));
+ assertTrue("isValid(A) pattern", ShortValidator.getInstance().isValid(patternVal, pattern));
+
+ assertNull("validate(B) default", ShortValidator.getInstance().validate(XXXX));
+ assertNull("validate(B) locale ", ShortValidator.getInstance().validate(XXXX, locale));
+ assertNull("validate(B) pattern", ShortValidator.getInstance().validate(XXXX, pattern));
+
+ assertFalse("isValid(B) default", ShortValidator.getInstance().isValid(XXXX));
+ assertFalse("isValid(B) locale ", ShortValidator.getInstance().isValid(XXXX, locale));
+ assertFalse("isValid(B) pattern", ShortValidator.getInstance().isValid(XXXX, pattern));
+ }
+
+ /**
+ * Test Short Range/Min/Max
+ */
+ public void testShortRangeMinMax() {
+ ShortValidator validator = (ShortValidator)strictValidator;
+ Short number9 = validator.validate("9", "#");
+ Short number10 = validator.validate("10", "#");
+ Short number11 = validator.validate("11", "#");
+ Short number19 = validator.validate("19", "#");
+ Short number20 = validator.validate("20", "#");
+ Short number21 = validator.validate("21", "#");
+ short min = (short)10;
+ short max = (short)20;
+
+ // Test isInRange()
+ assertFalse("isInRange() < min", validator.isInRange(number9, min, max));
+ assertTrue("isInRange() = min", validator.isInRange(number10, min, max));
+ assertTrue("isInRange() in range", validator.isInRange(number11, min, max));
+ assertTrue("isInRange() = max", validator.isInRange(number20, min, max));
+ assertFalse("isInRange() > max", validator.isInRange(number21, min, max));
+
+ // Test minValue()
+ assertFalse("minValue() < min", validator.minValue(number9, min));
+ assertTrue("minValue() = min", validator.minValue(number10, min));
+ assertTrue("minValue() > min", validator.minValue(number11, min));
+
+ // Test minValue()
+ assertTrue("maxValue() < max", validator.maxValue(number19, max));
+ assertTrue("maxValue() = max", validator.maxValue(number20, max));
+ assertFalse("maxValue() > max", validator.maxValue(number21, max));
+ }
+}
Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/ShortValidatorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/ShortValidatorTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/TimeValidatorTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/TimeValidatorTest.java?rev=370397&view=auto
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/TimeValidatorTest.java (added)
+++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/TimeValidatorTest.java Wed Jan 18 22:38:24 2006
@@ -0,0 +1,303 @@
+/*
+ * $Id$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.validator.routines;
+
+import junit.framework.TestCase;
+import java.util.Date;
+import java.util.Calendar;
+import java.util.Locale;
+import java.util.TimeZone;
+
+/**
+ * Test Case for TimeValidator.
+ *
+ * @version $Revision$ $Date$
+ */
+public class TimeValidatorTest extends TestCase {
+
+ protected static final TimeZone GMT = TimeZone.getTimeZone("GMT"); // 0 offset
+
+ protected TimeValidator validator;
+
+ protected String[] patternValid = new String[] {
+ "23-59-59"
+ ,"00-00-00"
+ ,"00-00-01"
+ ,"0-0-0"
+ ,"1-12-1"
+ ,"10-49-18"
+ ,"16-23-46"};
+ protected Date[] patternExpect = new Date[] {
+ createDate(null, 235959, 0)
+ ,createDate(null, 0, 0)
+ ,createDate(null, 1, 0)
+ ,createDate(null, 0, 0)
+ ,createDate(null, 11201, 0)
+ ,createDate(null, 104918, 0)
+ ,createDate(null, 162346, 0)};
+ protected String[] localeValid = new String[] {
+ "23:59"
+ ,"00:00"
+ ,"00:01"
+ ,"0:0"
+ ,"1:12"
+ ,"10:49"
+ ,"16:23"};
+ protected Date[] localeExpect = new Date[] {
+ createDate(null, 235900, 0)
+ ,createDate(null, 0, 0)
+ ,createDate(null, 100, 0)
+ ,createDate(null, 0, 0)
+ ,createDate(null, 11200, 0)
+ ,createDate(null, 104900, 0)
+ ,createDate(null, 162300, 0)};
+ protected String[] patternInvalid = new String[] {
+ "24-00-00" // midnight
+ ,"24-00-01" // past midnight
+ ,"25-02-03" // invalid hour
+ ,"10-61-31" // invalid minute
+ ,"10-01-61" // invalid second
+ ,"05:02-29" // invalid sep
+ ,"0X-01:01" // invalid sep
+ ,"05-0X-01" // invalid char
+ ,"10-01-0X" // invalid char
+ ,"01:01:05" // invalid pattern
+ ,"10-10" // invalid pattern
+ ,"10--10" // invalid pattern
+ ,"10-10-"}; // invalid pattern
+ protected String[] localeInvalid = new String[] {
+ "24:00" // midnight
+ ,"24:00" // past midnight
+ ,"25:02" // invalid hour
+ ,"10:61" // invalid minute
+ ,"05-02" // invalid sep
+ ,"0X:01" // invalid sep
+ ,"05:0X" // invalid char
+ ,"01-01" // invalid pattern
+ ,"10:" // invalid pattern
+ ,"10::1" // invalid pattern
+ ,"10:1:"}; // invalid pattern
+
+ /**
+ * Main
+ * @param args arguments
+ */
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(TimeValidatorTest.class);
+ }
+
+ /**
+ * Constructor
+ * @param name test name
+ */
+ public TimeValidatorTest(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ validator = new TimeValidator();
+ }
+
+ /**
+ * Tear down
+ * @throws Exception
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ validator = null;
+ }
+
+ /**
+ * Test Valid Dates with "pattern" validation
+ */
+ public void testPatternValid() {
+ for (int i = 0; i < patternValid.length; i++) {
+ String text = i + " value=[" +patternValid[i]+"] failed ";
+ Calendar calendar = validator.validate(patternValid[i], "HH-mm-ss");
+ assertNotNull("validateObj() " + text, calendar);
+ Date date = calendar.getTime();
+ assertTrue("isValid() " + text, validator.isValid(patternValid[i], "HH-mm-ss"));
+ assertEquals("compare " + text, patternExpect[i], date);
+ }
+ }
+
+ /**
+ * Test Invalid Dates with "pattern" validation
+ */
+ public void testPatternInvalid() {
+ for (int i = 0; i < patternInvalid.length; i++) {
+ String text = i + " value=[" +patternInvalid[i]+"] passed ";
+ Object date = validator.validate(patternInvalid[i], "HH-mm-ss");
+ assertNull("validate() " + text + date, date);
+ assertFalse("isValid() " + text, validator.isValid(patternInvalid[i], "HH-mm-ss"));
+ }
+ }
+
+ /**
+ * Test Valid Dates with "locale" validation
+ */
+ public void testLocaleValid() {
+ for (int i = 0; i < localeValid.length; i++) {
+ String text = i + " value=[" +localeValid[i]+"] failed ";
+ Calendar calendar = validator.validate(localeValid[i], Locale.UK);
+ assertNotNull("validate() " + text, calendar);
+ Date date = calendar.getTime();
+ assertTrue("isValid() " + text, validator.isValid(localeValid[i], Locale.UK));
+ assertEquals("compare " + text, localeExpect[i], date);
+ }
+ }
+
+ /**
+ * Test Invalid Dates with "locale" validation
+ */
+ public void testLocaleInvalid() {
+ for (int i = 0; i < localeInvalid.length; i++) {
+ String text = i + " value=[" +localeInvalid[i]+"] passed ";
+ Object date = validator.validate(localeInvalid[i], Locale.US);
+ assertNull("validate() " + text + date, date);
+ assertFalse("isValid() " + text, validator.isValid(localeInvalid[i], Locale.UK));
+ }
+ }
+
+// /**
+// * Test Valid Dates with "localized pattern" validation
+// */
+// public void testLocalizedPatternValid() {
+// for (int i = 0; i < patternValid.length; i++) {
+// String text = i + " value=[" +patternValid[i]+"] failed ";
+// Object date = validator.validate(patternValid[i], "HH-mm-ss", Locale.GERMAN);
+// assertNotNull("validate() " + text + date, date);
+// assertTrue("isValid() " + text, validator.isValid(patternValid[i], "HH-mm-ss", Locale.GERMAN));
+// if (date instanceof Calendar) {
+// date = ((Calendar)date).getTime();
+// }
+// assertEquals("compare " + text, patternExpect[i], date);
+// }
+// }
+//
+// /**
+// * Test Invalid Dates with "localized pattern" validation
+// */
+// public void testLocalizedPatternInvalid() {
+// for (int i = 0; i < patternInvalid.length; i++) {
+// String text = i + " value=[" +patternInvalid[i]+"] passed ";
+// Object date = validator.validate(patternInvalid[i], "HH-mm-ss", Locale.GERMAN);
+// assertNull("validate() " + text + date, date);
+// assertFalse("isValid() " + text, validator.isValid(patternInvalid[i], "HH-mm-ss", Locale.GERMAN));
+// }
+// }
+
+ /**
+ * Test Invalid Dates with "locale" validation
+ */
+ public void testFormat() {
+
+ // Create a Date or Calendar
+ Object test = validator.validate("16:49:23", "HH:mm:ss");
+ assertNotNull("Test Date ", test);
+ assertEquals("Format pattern", "16-49-23", validator.format(test, "HH-mm-ss"));
+ assertEquals("Format locale", "4:49 PM", validator.format(test, Locale.US));
+// assertEquals("Locale Pattern", "23 49 16", validator.format(test, "ss mm HH", Locale.GERMAN));
+ }
+
+ /**
+ * Test compare date methods
+ */
+ public void testCompare() {
+ int testTime = 154523;
+ int min = 100;
+ int hour = 10000;
+
+ Calendar milliGreater = createTime(GMT, testTime, 500); // > milli sec
+ Calendar value = createTime(GMT, testTime, 400); // test value
+ Calendar milliLess = createTime(GMT, testTime, 300); // < milli sec
+
+ Calendar secGreater = createTime(GMT, testTime + 1, 100); // +1 sec
+ Calendar secLess = createTime(GMT, testTime - 1, 100); // -1 sec
+
+ Calendar minGreater = createTime(GMT, testTime + min, 100); // +1 min
+ Calendar minLess = createTime(GMT, testTime - min, 100); // -1 min
+
+ Calendar hourGreater = createTime(GMT, testTime + hour, 100); // +1 hour
+ Calendar hourLess = createTime(GMT, testTime - hour, 100); // -1 hour
+
+ assertEquals("mili LT", -1, validator.compareTime(value, milliGreater)); // > milli
+ assertEquals("mili EQ", 0, validator.compareTime(value, value)); // same time
+ assertEquals("mili GT", 1, validator.compareTime(value, milliLess)); // < milli
+
+ assertEquals("secs LT", -1, validator.compareSeconds(value, secGreater)); // +1 sec
+ assertEquals("secs =1", 0, validator.compareSeconds(value, milliGreater)); // > milli
+ assertEquals("secs =2", 0, validator.compareSeconds(value, value)); // same time
+ assertEquals("secs =3", 0, validator.compareSeconds(value, milliLess)); // < milli
+ assertEquals("secs GT", 1, validator.compareSeconds(value, secLess)); // -1 sec
+
+ assertEquals("mins LT", -1, validator.compareMinutes(value, minGreater)); // +1 min
+ assertEquals("mins =1", 0, validator.compareMinutes(value, secGreater)); // +1 sec
+ assertEquals("mins =2", 0, validator.compareMinutes(value, value)); // same time
+ assertEquals("mins =3", 0, validator.compareMinutes(value, secLess)); // -1 sec
+ assertEquals("mins GT", 1, validator.compareMinutes(value, minLess)); // -1 min
+
+ assertEquals("hour LT", -1, validator.compareHours(value, hourGreater)); // +1 hour
+ assertEquals("hour =1", 0, validator.compareHours(value, minGreater)); // +1 min
+ assertEquals("hour =2", 0, validator.compareHours(value, value)); // same time
+ assertEquals("hour =3", 0, validator.compareHours(value, minLess)); // -1 min
+ assertEquals("hour GT", 1, validator.compareHours(value, hourLess)); // -1 hour
+
+ }
+
+ /**
+ * Create a calendar instance for a specified time zone, date and time.
+ *
+ * @param zone The time zone
+ * @param time the time in HH:mm:ss format
+ * @param millisecond the milliseconds
+ * @return the new Calendar instance.
+ */
+ protected static Calendar createTime(TimeZone zone, int time, int millisecond) {
+ Calendar calendar = zone == null ? Calendar.getInstance()
+ : Calendar.getInstance(zone);
+ int hour = ((time / 10000) * 10000);
+ int min = ((time / 100) * 100) - hour;
+ int sec = time - (hour + min);
+ calendar.set(Calendar.YEAR, 1970);
+ calendar.set(Calendar.MONTH, 0);
+ calendar.set(Calendar.DATE, 1);
+ calendar.set(Calendar.HOUR_OF_DAY, (hour / 10000));
+ calendar.set(Calendar.MINUTE, (min / 100));
+ calendar.set(Calendar.SECOND, sec);
+ calendar.set(Calendar.MILLISECOND, millisecond);
+ return calendar;
+ }
+
+ /**
+ * Create a date instance for a specified time zone, date and time.
+ *
+ * @param zone The time zone
+ * @param time the time in HH:mm:ss format
+ * @param millisecond the milliseconds
+ * @return the new Date instance.
+ */
+ protected static Date createDate(TimeZone zone, int time, int millisecond) {
+ Calendar calendar = createTime(zone, time, millisecond);
+ return calendar.getTime();
+ }
+}
Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/TimeValidatorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/TimeValidatorTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org