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

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon Jan 18 11:32:46 EST 2010


Author: hardy.ferentschik
Date: 2010-01-18 11:32:46 -0500 (Mon, 18 Jan 2010)
New Revision: 18572

Added:
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/URL.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/URLValidator.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/impl/URLValidatorTest.java
Log:
HV-229 -first cut of @URL using java.net.URL to implemenet its logic


Added: 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	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/URL.java	2010-01-18 16:32:46 UTC (rev 18572)
@@ -0,0 +1,48 @@
+// $Id: Length.java 17427 2009-08-27 09:47:28Z hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.URLValidator;
+
+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.RetentionPolicy.RUNTIME;
+
+/**
+ * Validate that the string is a valid URL.
+ *
+ * @author Hardy Ferentschik
+ */
+ at Documented
+ at Constraint(validatedBy = URLValidator.class)
+ at Target({ METHOD, FIELD, TYPE })
+ at Retention(RUNTIME)
+public @interface URL {
+	public abstract String message() default "{org.hibernate.validator.constraints.URL.message}";
+
+	public abstract Class<?>[] groups() default { };
+
+	public abstract Class<? extends Payload>[] payload() default { };
+}
\ No newline at end of file

Added: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/URLValidator.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/URLValidator.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/URLValidator.java	2010-01-18 16:32:46 UTC (rev 18572)
@@ -0,0 +1,47 @@
+// $Id: LengthValidator.java 17521 2009-09-16 12:50:41Z hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.net.MalformedURLException;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+import org.hibernate.validator.constraints.URL;
+
+/**
+ * Validate that the string is a valid URL.
+ *
+ * @author Hardy Ferentschik
+ */
+public class URLValidator implements ConstraintValidator<URL, String> {
+	public void initialize(URL url) {
+	}
+
+	public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
+		if ( value == null ) {
+			return true;
+		}
+		try {
+			new java.net.URL( value );
+			return true;
+		}
+		catch ( MalformedURLException e ) {
+			return false;
+		}
+	}
+}
\ No newline at end of file

Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/impl/URLValidatorTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/impl/URLValidatorTest.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/impl/URLValidatorTest.java	2010-01-18 16:32:46 UTC (rev 18572)
@@ -0,0 +1,48 @@
+// $Id: LengthValidatorTest.java 17521 2009-09-16 12:50:41Z hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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 static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.Test;
+
+import org.hibernate.validator.constraints.URL;
+import org.hibernate.validator.util.annotationfactory.AnnotationDescriptor;
+import org.hibernate.validator.util.annotationfactory.AnnotationFactory;
+
+/**
+ * Tests the {@code Url} constraint.
+ *
+ * @author Hardy Ferentschik
+ */
+public class URLValidatorTest {
+
+	@Test
+	public void testIsValidUrl() {
+		AnnotationDescriptor<URL> descriptor = new AnnotationDescriptor<URL>( URL.class );
+		descriptor.setValue( "message", "{org.hibernate.validator.constraints.URL.message}" );
+		URL url = AnnotationFactory.create( descriptor );
+		URLValidator validator = new URLValidator();
+		validator.initialize( url );
+		assertTrue( validator.isValid( null, null ) );
+		assertFalse( validator.isValid( "", null ) );
+		assertFalse( validator.isValid( "http", null ) );
+		assertFalse( validator.isValid( "ftp//abc.de", null ) );
+		assertTrue( validator.isValid( "ftp://abc.de", null ) );
+	}
+}
\ No newline at end of file



More information about the hibernate-commits mailing list