[hibernate-commits] Hibernate SVN: r18982 - in validator/trunk/hibernate-validator/src: main/java/org/hibernate/validator/constraints/impl and 2 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Mar 11 14:31:30 EST 2010


Author: hardy.ferentschik
Date: 2010-03-11 14:31:30 -0500 (Thu, 11 Mar 2010)
New Revision: 18982

Added:
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/CreditCardNumber.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/CreditCardNumberValidator.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/LuhnValidator.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/impl/CreditCardNumberValidatorTest.java
Modified:
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/URL.java
   validator/trunk/hibernate-validator/src/main/resources/org/hibernate/validator/ValidationMessages.properties
   validator/trunk/hibernate-validator/src/main/resources/org/hibernate/validator/ValidationMessages_de.properties
Log:
HV-282 - Create CreditCardValidator


Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/CreditCardNumber.java (from rev 18807, validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/Email.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/CreditCardNumber.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/CreditCardNumber.java	2010-03-11 19:31:30 UTC (rev 18982)
@@ -0,0 +1,52 @@
+//$Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* 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.hibernate.validator.constraints;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.Payload;
+
+import org.hibernate.validator.constraints.impl.CreditCardNumberValidator;
+
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import static java.lang.annotation.ElementType.CONSTRUCTOR;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+/**
+ * The annotated element has to represent a valid
+ * credit card number. This is the Luhn algorithm implementation
+ * which aims to check for user mistake, not credit card validity!
+ *
+ * @author Emmanuel Bernard
+ */
+ at Documented
+ at Constraint(validatedBy = CreditCardNumberValidator.class)
+ at Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
+ at Retention(RUNTIME)
+public @interface CreditCardNumber {
+	public abstract String message() default "{org.hibernate.validator.constraints.CreditCardNumber.message}";
+
+	public abstract Class<?>[] groups() default { };
+
+	public abstract Class<? extends Payload>[] payload() default { };
+}
\ No newline at end of file

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/URL.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/URL.java	2010-03-10 22:20:03 UTC (rev 18981)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/URL.java	2010-03-11 19:31:30 UTC (rev 18982)
@@ -25,9 +25,11 @@
 
 import org.hibernate.validator.constraints.impl.URLValidator;
 
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import static java.lang.annotation.ElementType.CONSTRUCTOR;
 import static java.lang.annotation.ElementType.FIELD;
 import static java.lang.annotation.ElementType.METHOD;
-import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.ElementType.PARAMETER;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 
 /**
@@ -37,7 +39,7 @@
  */
 @Documented
 @Constraint(validatedBy = URLValidator.class)
- at Target({ METHOD, FIELD, TYPE })
+ at Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
 @Retention(RUNTIME)
 public @interface URL {
 	public abstract String protocol() default "";

Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/CreditCardNumberValidator.java (from rev 18807, validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/EmailValidator.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/CreditCardNumberValidator.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/CreditCardNumberValidator.java	2010-03-11 19:31:30 UTC (rev 18982)
@@ -0,0 +1,31 @@
+//$Id$
+package org.hibernate.validator.constraints.impl;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+import org.hibernate.validator.constraints.CreditCardNumber;
+
+/**
+ * Check a credit card number through the Luhn algorithm.
+ *
+ * @author Emmanuel Bernard
+ * @author Hardy Ferentschik
+ */
+public class CreditCardNumberValidator implements ConstraintValidator<CreditCardNumber, String> {
+	private LuhnValidator luhnValidator;
+
+	public CreditCardNumberValidator() {
+		luhnValidator = new LuhnValidator( 2 );
+	}
+
+	public void initialize(CreditCardNumber annotation) {
+	}
+
+	public boolean isValid(String value, ConstraintValidatorContext context) {
+		if ( value == null ) {
+			return true;
+		}
+		return luhnValidator.passesLuhnTest( value );
+	}
+}
\ No newline at end of file

Added: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/LuhnValidator.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/LuhnValidator.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/LuhnValidator.java	2010-03-11 19:31:30 UTC (rev 18982)
@@ -0,0 +1,63 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* 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.hibernate.validator.constraints.impl;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Implement the Luhn algorithm (with Luhn key on the last digit). See also {@link http://en.wikipedia.org/wiki/Luhn_algorithm}
+ * and {@link http://www.merriampark.com/anatomycc.htm}.
+ *
+ * @author Hardy Ferentschik
+ */
+public class LuhnValidator {
+	private final int multiplicator;
+
+	public LuhnValidator(int multiplicator) {
+		this.multiplicator = multiplicator;
+	}
+
+	public boolean passesLuhnTest(String value) {
+		char[] chars = value.toCharArray();
+
+		List<Integer> digits = new ArrayList<Integer>();
+		for ( char c : chars ) {
+			if ( Character.isDigit( c ) ) {
+				digits.add( c - '0' );
+			}
+		}
+		int length = digits.size();
+		int sum = 0;
+		boolean even = false;
+		for ( int index = length - 1; index >= 0; index-- ) {
+			int digit = digits.get( index );
+			if ( even ) {
+				digit *= multiplicator;
+			}
+			if ( digit > 9 ) {
+				digit = digit / 10 + digit % 10;
+			}
+			sum += digit;
+			even = !even;
+		}
+		return sum % 10 == 0;
+	}
+}
+
+

Modified: validator/trunk/hibernate-validator/src/main/resources/org/hibernate/validator/ValidationMessages.properties
===================================================================
--- validator/trunk/hibernate-validator/src/main/resources/org/hibernate/validator/ValidationMessages.properties	2010-03-10 22:20:03 UTC (rev 18981)
+++ validator/trunk/hibernate-validator/src/main/resources/org/hibernate/validator/ValidationMessages.properties	2010-03-11 19:31:30 UTC (rev 18982)
@@ -17,4 +17,5 @@
 org.hibernate.validator.constraints.NotEmpty.message=may not be empty
 org.hibernate.validator.constraints.Range.message=must be between {min} and {max}
 org.hibernate.validator.constraints.URL.message=must be a valid URL
+org.hibernate.validator.constraints.CreditCardNumber.message=Invalid credit card number
 

Modified: validator/trunk/hibernate-validator/src/main/resources/org/hibernate/validator/ValidationMessages_de.properties
===================================================================
--- validator/trunk/hibernate-validator/src/main/resources/org/hibernate/validator/ValidationMessages_de.properties	2010-03-10 22:20:03 UTC (rev 18981)
+++ validator/trunk/hibernate-validator/src/main/resources/org/hibernate/validator/ValidationMessages_de.properties	2010-03-11 19:31:30 UTC (rev 18982)
@@ -16,4 +16,5 @@
 javax.validation.constraints.DecimalMax.message=muss kleinergleich {value} sein
 org.hibernate.validator.constraints.Email.message=keine g\u00FCltige E-Mail-Adresse
 org.hibernate.validator.constraints.Range.message=muss zwischen {min} und {max} liegen
-org.hibernate.validator.constraints.URL.message=muss eine g\u00FCltige URL sein
\ No newline at end of file
+org.hibernate.validator.constraints.URL.message=muss eine g\u00FCltige URL sein
+org.hibernate.validator.constraints.CreditCardNumber.message=Ung\u00FCltige Kreditkartennummer
\ No newline at end of file

Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/impl/CreditCardNumberValidatorTest.java (from rev 18807, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/impl/EmailValidatorTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/impl/CreditCardNumberValidatorTest.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/impl/CreditCardNumberValidatorTest.java	2010-03-11 19:31:30 UTC (rev 18982)
@@ -0,0 +1,52 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* 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.hibernate.validator.constraints.impl;
+
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class CreditCardNumberValidatorTest {
+
+	private static CreditCardNumberValidator validator;
+
+	@BeforeClass
+	public static void init() {
+		validator = new CreditCardNumberValidator();
+	}
+
+	@Test
+	public void testInvalidCreditCardNumber() throws Exception {
+		assertFalse( validator.isValid( "1234567890123456", null ) );
+	}
+
+	@Test
+	public void testValidCreditCardNumber() throws Exception {
+		assertTrue( validator.isValid( "541234567890125", null ) );
+	}
+
+	@Test
+	public void testNullValue() throws Exception {
+		assertTrue( validator.isValid( null, null ) );
+	}
+}
\ No newline at end of file



More information about the hibernate-commits mailing list