From hibernate-commits at lists.jboss.org Wed Mar 10 17:13:10 2010
Content-Type: multipart/mixed; boundary="===============4312351118631570783=="
MIME-Version: 1.0
From: hibernate-commits at lists.jboss.org
To: hibernate-commits at lists.jboss.org
Subject: [hibernate-commits] Hibernate SVN: r18980 - in
validator/trunk/hibernate-validator: src/main/docbook/en-US and 5 other
directories.
Date: Wed, 10 Mar 2010 17:13:09 -0500
Message-ID: <201003102213.o2AMD9c5003752@svn01.web.mwc.hst.phx2.redhat.com>
--===============4312351118631570783==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Author: hardy.ferentschik
Date: 2010-03-10 17:13:09 -0500 (Wed, 10 Mar 2010)
New Revision: 18980
Modified:
validator/trunk/hibernate-validator/pom.xml
validator/trunk/hibernate-validator/src/main/docbook/en-US/master.xml
validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/boots=
trapping.xml
validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/custo=
mconstraints.xml
validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/defin=
econstraints.xml
validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/getti=
ngstarted.xml
validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/integ=
ration.xml
validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/using=
validator.xml
validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/xmlco=
nfiguration.xml
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validato=
r/constraints/URL.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validato=
r/constraints/impl/URLValidator.java
validator/trunk/hibernate-validator/src/main/resources/org/hibernate/val=
idator/ValidationMessages.properties
validator/trunk/hibernate-validator/src/main/resources/org/hibernate/val=
idator/ValidationMessages_de.properties
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validato=
r/constraints/impl/URLValidatorTest.java
Log:
HV-229 - Added URL constraint and updated docs. Fixed some doc styles as we=
ll.
Modified: validator/trunk/hibernate-validator/pom.xml
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- validator/trunk/hibernate-validator/pom.xml 2010-03-10 18:59:19 UTC (re=
v 18979)
+++ validator/trunk/hibernate-validator/pom.xml 2010-03-10 22:13:09 UTC (re=
v 18980)
@@ -176,7 +176,7 @@
pdf
classpath:/xslt/org/hibern=
ate/jdocbook/xslt/pdf.xsl
- hibernate_reference.pdf
+ hibernate_validator_reference.pdf=
finalName>
html_single
Modified: validator/trunk/hibernate-validator/src/main/docbook/en-US/master=
.xml
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- validator/trunk/hibernate-validator/src/main/docbook/en-US/master.xml 2=
010-03-10 18:59:19 UTC (rev 18979)
+++ validator/trunk/hibernate-validator/src/main/docbook/en-US/master.xml 2=
010-03-10 22:13:09 UTC (rev 18980)
@@ -19,26 +19,32 @@
+
]>
Hibernate Validator
-
JSR 303 Reference Implementation
-
Reference Guide
-
&version;
-
+ &today; =
&version;
-
©rightYear;
-
©rightHolder;
+
+
+ Hardy
+ Ferentschik
+
+
+ Gunnar
+ Morling
+ =
+ =
=
Modified: validator/trunk/hibernate-validator/src/main/docbook/en-US/module=
s/bootstrapping.xml
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/boot=
strapping.xml 2010-03-10 18:59:19 UTC (rev 18979)
+++ validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/boot=
strapping.xml 2010-03-10 22:13:09 UTC (rev 18980)
@@ -40,14 +40,12 @@
implementation. In the case of Hibernate Validator this is
org.hibernate.validator.HibernateValidator.
=
-
- If there are more than one Bean Validation implementation
- providers in the classpath and
- Validation.buildDefaultValidatorFactory() is
- used, there is no guarantee which provider will be chosen. To enforce
- the provider Validation.byProvider() should=
be
- used.
-
+
+ If there are more than one Bean Validation implementation pr=
oviders in the classpath
+ and Validation.buildDefaultValidatorFactory() is used, there is
+ no guarantee which provider will be chosen. To enforce the pro=
vider
+ Validation.byProvider() should be=
used.
+
=
Configuration and
@@ -55,19 +53,20 @@
=
There are three different methods in the Validation class to cre=
ate
a Validator instance. The easiest in shown in ..
+
+
Validation.buildDefaultValidatorFactory()
=
- ValidatorFactory factory =3D Validation.buildDefaultValidatorFactory();
+ ValidatorFactory f=
actory =3D Validation.buildDefaultValidatorFactory();
Validator validator =3D factory.getValidator();
- You can also use the method
- Validation.byDefaultProvider() which will all=
ow
- you to configure several aspects of the created Validator
- instance:
- Validation.byDefaultProvider()
+
+ You can also use the method Validation.byDefaul=
tProvider()
+ which will allow you to configure several aspects of the creat=
ed Validator instance:
+ Validation.byDefaultProvider()
=
- Configuration<?> config =3D Validation.byDefaultProvider().configure();
+ Configurat=
ion<?> config =3D Validation.byDefaultProvider().configure();
config.messageInterpolator(new MyMessageInterpolator())
.traversableResolver( new MyTraversableResolver())
.constraintValidatorFactory(new MyConstraintValidatorFactory());
@@ -75,29 +74,27 @@
ValidatorFactory factory =3D config.buildValidatorFactory();
Validator validator =3D factory.getValidator();
- We will learn more about
- MessageInterpolator,
- TraversableResolver and
- ConstraintValidatorFactory in the following
- sections.
+ We will learn more about MessageInt=
erpolator,
+ TraversableResolver and
+ ConstraintValidatorFactory in the f=
ollowing sections.
=
Last but not least you can ask for a Configuration object of a
specific Bean Validation provider. This is useful if you have more than
one Bean Validation provider in your classpath. In this situation you =
can
make an explicit choice about which implementation to use. In the case=
of
Hibernate Validator the Validator creation looks
- like:
+ like:
+
Validation.byProvider( HibernateValidator.class )
=
- ValidatorConfiguration config =3D Validation.byProvider( HibernateValidator.class ).conf=
igure();
+ ValidatorConfigura=
tion config =3D Validation.byProvider( HibernateValidator.class ).configure=
();
config.messageInterpolator(new MyMessageInterpolator())
.traversableResolver( new MyTraversableResolver())
.constraintValidatorFactory(new MyConstraintValidatorFactory());
=
ValidatorFactory factory =3D config.buildValidatorFactory();
Validator validator =3D factory.getValidator();
-
-
+
The generated Validator instance is
thread safe and can be cached.
@@ -113,24 +110,24 @@
example in an OSGi environment you could plug your custom provider
resolver like seen in .<=
/para>
=
-
+
Providing a custom ValidationProviderResolver
=
- Configuration<?> config =3D Validation.byDef=
aultProvider()
- .providerResolver( new OSGiServiceDiscoverer()=
)
+ Configuration<?=
> config =3D Validation.byDefaultProvider()
+ .providerResolver( new OSGiServiceDiscoverer() )
.configure();
=
ValidatorFactory factory =3D config.buildValidatorFactory();
Validator validator =3D factory.getValidator();
- Your OSGiServiceDiscoverer must in =
this
+ Your OSGiServiceDiscoverer must=
in this
case implement the interface
- ValidationProviderResolver:
-
+ ValidationProviderResolver:
+
ValidationProviderResolver interface
=
- public interface ValidationProviderResolver {
+ public interface Val=
idationProviderResolver {
/**
* Returns a list of ValidationProviders available in the runtime envi=
ronment.
*
@@ -158,9 +155,9 @@
Providing a custom MessageInterpolator
=
- Configuration<?> configuration =3D Validation.=
byDefaultProvider().configure();
+ Configuration<?&g=
t; configuration =3D Validation.byDefaultProvider().configure();
ValidatorFactory factory =3D configuration
- .messageInterpolator(new ContextualMessageInte=
rpolator(configuration.getDefaultMessageInterpolator()))
+ .messageInterpolator(new ContextualMessageInterpolator(configuration.g=
etDefaultMessageInterpolator()))
.buildValidatorFactory();
=
Validator validator =3D factory.getValidator();
@@ -188,11 +185,11 @@
would have to be accessed triggering a load from the database. Bean
Validation controls which property can and cannot be accessed via the
TraversableResolver interface (see ).).
TraversableResolver interface
=
- /**
+ /**
* Contract determining if a property can be accessed by the Bean Validati=
on provider
* This contract is called for each property that is being either validate=
d or cascaded.
*
@@ -247,7 +244,7 @@
ElementType elementType);
}
- Hibernate Validator provides two
+ Hibernate Validator provides two
TraversableResolvers out of the box which will =
be
enabled automatically depending on your environment. The first is the
DefaultTraversableResolver which will always re=
turn
@@ -257,18 +254,18 @@
Hibernate Validator gets used in combination with JPA 2. In case you h=
ave
to provide your own resolver you can do so again using the
Configuration object as seen in ..
Providing a custom TraversableResolver
=
- Configuration<?> configuration =3D Validatio=
n.byDefaultProvider().configure();
+ Configuration<?=
> configuration =3D Validation.byDefaultProvider().configure();
ValidatorFactory factory =3D configuration
- .traversableResolver(new MyTraversableResolver=
())
+ .traversableResolver(new MyTraversableResolver())
.buildValidatorFactory();
=
Validator validator =3D factory.getValidator();
-
+
=
@@ -286,22 +283,22 @@
Configuration ().
=
-
+
Providing a custom ConstraintValidatorFactory
=
- Configuration<?> configuration =3D Validatio=
n.byDefaultProvider().configure();
+ Configuration<?=
> configuration =3D Validation.byDefaultProvider().configure();
ValidatorFactory factory =3D configuration
- .constraintValidatorFactory(new IOCConstraintV=
alidatorFactory())
+ .constraintValidatorFactory(new IOCConstraintValidatorFactory())
.buildValidatorFactory();
=
Validator validator =3D factory.getValidator();
- The interface you have to implement is:
+ The interface you have to implement is:
=
-
+
ConstraintValidatorFactory interface
=
- public interface ConstraintValidatorFactory {
+ public interface C=
onstraintValidatorFactory {
/**
* @param key The class of the constraint validator to instantiate.
*
@@ -318,6 +315,6 @@
ConstraintValidatorFactory should not cache instances as the
state of each instance can be altered in the initialize method.
-
+
Modified: validator/trunk/hibernate-validator/src/main/docbook/en-US/module=
s/customconstraints.xml
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/cust=
omconstraints.xml 2010-03-10 18:59:19 UTC (rev 18979)
+++ validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/cust=
omconstraints.xml 2010-03-10 22:13:09 UTC (rev 18980)
@@ -65,7 +65,7 @@
Enum CaseMode to express upper vs. l=
ower
case
=
- package com.mycompany;
+ package com.mycomp=
any;
=
public enum CaseMode {
UPPER, =
@@ -80,7 +80,7 @@
Defining CheckCase constraint annotation
=
- package com.mycompany;
+ package com.mycomp=
any;
=
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
@@ -135,7 +135,7 @@
objects to a constraint. This attribute is not used by the API
itself.
An examle for a custom payload could be the definition=
of
- a severity. public class Severity {
+ a severity. public class Severity {
public static class Info extends ConstraintPayload {};
public static class Error extends ConstraintPayload {};
}
@@ -148,7 +148,7 @@
private String phoneNumber;
=
// ...
-}Now a client can after the validation of a
+}Now a client can after the validation of a
ContactDetails instance access the
severity of a constraint using
ConstraintViolation.getConstraintDescriptor().ge=
tPayload()
@@ -207,7 +207,7 @@
Implementing a constraint validator for the constraint
CheckCase
=
- package com.mycompany;
+ package com.mycomp=
any;
=
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
@@ -301,7 +301,7 @@
Applying the CheckCase
constraint
=
- package com.mycompany;
+ package com.mycomp=
any;
=
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
@@ -340,7 +340,7 @@
Testcase demonstrating the CheckCase
validation
=
- package com.mycompany;
+ package com.mycomp=
any;
=
import static org.junit.Assert.*;
=
@@ -414,7 +414,7 @@
Creating a composing constraint
ValidLicensePlate
=
- package com.mycompany;
+ package com.mycompan=
y;
=
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
@@ -463,7 +463,7 @@
Application of composing constraint
ValidLicensePlate
=
- package com.mycompany;
+ package com.mycompan=
y;
=
public class Car {
=
@@ -487,7 +487,7 @@
Usage of @ReportAsSingleViolation
=
- //...
+ //...
@ReportAsSingleViolation
public @interface ValidLicensePlate {
=
Modified: validator/trunk/hibernate-validator/src/main/docbook/en-US/module=
s/defineconstraints.xml
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/defi=
neconstraints.xml 2010-03-10 18:59:19 UTC (rev 18979)
+++ validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/defi=
neconstraints.xml 2010-03-10 22:13:09 UTC (rev 18980)
@@ -281,7 +281,7 @@
validator (the implementation class). Here is a
simple user-defined descriptor:
=
- @ValidatorClass(CapitalizedValidator.class)
+ @ValidatorClass(Capita=
lizedValidator.class)
@Target(METHOD)
@Retention(RUNTIME)
@Documented
@@ -305,7 +305,7 @@
good practice. See
.
=
- @ValidatorClass(CapitalizedValidator.class)
+ @ValidatorClass(Capita=
lizedValidator.class)
@Target(METHOD)
@Retention(RUNTIME)
@Documented
@@ -333,7 +333,7 @@
database level (by implementing
PersistentClassConstraint )
=
- public class CapitalizedValidator
+ public class Capitaliz=
edValidator
implements Validator<Capitalized>, PropertyConstraint {
private CapitalizeType type;
=
@@ -372,7 +372,7 @@
parameters) on the same property or type, you can use the following
annotation form:
=
- @Target(METHOD)
+ @Target(METHOD)
@Retention(RUNTIME)
@Documented
public @interface Patterns {
@@ -397,7 +397,7 @@
Since you are already familiar with annotations now, the syntax
should be very familiar
=
- public class Address {
+ public class Address {
private String line1;
private String line2;
private String zip;
@@ -456,7 +456,7 @@
While the example only shows public property validation, you can
also annotate fields of any kind of visibility
=
- @MyBeanConstraint(max=3D45
+ @MyBeanConstraint(max=
=3D45
public class Dog {
@AssertTrue private boolean isMale;
@NotNull protected String getName() { ... };
@@ -467,7 +467,7 @@
superclasses and interfaces extended or implemented by a given bean to
read the appropriate validator annotations.
=
- public interface Named {
+ public interface Named=
{
@NotNull String getName();
...
}
Modified: validator/trunk/hibernate-validator/src/main/docbook/en-US/module=
s/gettingstarted.xml
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/gett=
ingstarted.xml 2010-03-10 18:59:19 UTC (rev 18979)
+++ validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/gett=
ingstarted.xml 2010-03-10 22:13:09 UTC (rev 18980)
@@ -101,10 +101,10 @@
Open the project in the IDE of your choice and have a look at the
class Car:
=
-
+
Class Car annotated with constraints
=
- package com.mycompany;
+ package com.mycomp=
any;
=
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
@@ -112,14 +112,14 @@
=
public class Car {
=
- @NotNull
+ @NotNull
private String manufacturer;
=
- @NotNull
- @Size(min =3D 2, max =3D 14)
+ @NotNull
+ @Size(min =3D 2, max =3D 14)
private String licensePlate;
=
- @Min(2)
+ @Min(2)
private int seatCount;
=
public Car(String manufacturer, String licencePlate, int seatCount) {
@@ -130,7 +130,7 @@
=
//getters and setters ...
}
- @NotNull, @Size
+ @NotNull, @Size
and @Min are so-called constraint annotations, =
that
we use to declare constraints, which shall be applied to the fields of=
a
Car instance:
@@ -161,7 +161,7 @@
Class CarTest showing validation examples
=
- package com.mycompany;
+ package com.mycompan=
y;
=
import static org.junit.Assert.*;
=
Modified: validator/trunk/hibernate-validator/src/main/docbook/en-US/module=
s/integration.xml
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/inte=
gration.xml 2010-03-10 18:59:19 UTC (rev 18979)
+++ validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/inte=
gration.xml 2010-03-10 22:13:09 UTC (rev 18980)
@@ -102,7 +102,7 @@
Manual configuration of
BeanValidationEvenListener
=
- <hibernate-configuration>
+ <hibernate-config=
uration>
<session-factory>
...
<property name=3D"javax.persistence.validation.group.pre-persist=
">javax.validation.groups.Default</property>
@@ -110,13 +110,13 @@
<property name=3D"javax.persistence.validation.group.pre-remove"=
></property>
...
<event type=3D"pre-update">
- <listener class=3D"org.hibernate.cfg.beanvalidation=
.BeanValidationEventListener"/>
+ <listener class=3D"org.hibernate.cfg.beanvalidation.BeanValida=
tionEventListener"/>
</event>
<event type=3D"pre-insert">
- <listener class=3D"org.hibernate.cfg.beanvalidation=
.BeanValidationEventListener"/>
+ <listener class=3D"org.hibernate.cfg.beanvalidation.BeanValida=
tionEventListener"/>
</event>
<event type=3D"pre-delete">
- <listener class=3D"org.hibernate.cfg.beanvalidation=
.BeanValidationEventListener"/>
+ <listener class=3D"org.hibernate.cfg.beanvalidation.BeanValida=
tionEventListener"/>
</event>
</session-factory>
</hibernate-configuration>
Modified: validator/trunk/hibernate-validator/src/main/docbook/en-US/module=
s/usingvalidator.xml
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/usin=
gvalidator.xml 2010-03-10 18:59:19 UTC (rev 18979)
+++ validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/usin=
gvalidator.xml 2010-03-10 22:13:09 UTC (rev 18980)
@@ -55,16 +55,16 @@
Field level constraint
=
- package com.mycompany;
+ package com.mycomp=
any;
=
import javax.validation.constraints.NotNull;
=
public class Car {
=
- @NotNull
+ @NotNull
private String manufacturer;
=
- @AssertTrue
+ @AssertTrue
private boolean isRegistered;
=
public Car(String manufacturer, boolean isRegistered) {
@@ -105,7 +105,7 @@
Property level constraint
=
- package com.mycompany;
+ package com.mycomp=
any;
=
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.NotNull;
@@ -122,7 +122,7 @@
this.isRegistered =3D isRegistered;
}
=
- @NotNull
+ @NotNull
public String getManufacturer() {
return manufacturer;
}
@@ -131,7 +131,7 @@
this.manufacturer =3D manufacturer;
}
=
- @AssertTrue
+ @AssertTrue
public boolean isRegistered() {
return isRegistered;
}
@@ -174,16 +174,16 @@
know that PassengerCount will ensure that the=
re
cannot be more passengers in a car than there are seats.
=
-
+
Class level constraint
=
- package com.mycompany;
+ package com.myco=
mpany;
=
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
=
-@PassengerCount
+(a)PassengerCount
public class Car {
=
@NotNull
@@ -206,7 +206,7 @@
=
//getters and setters ...
}
-
+
=
@@ -221,7 +221,7 @@
Constraint inheritance using RentalCar
=
- package com.mycompany;
+ package com.mycomp=
any;
=
import javax.validation.constraints.NotNull;
=
@@ -234,7 +234,7 @@
this.rentalStation =3D rentalStation;
}
=
- @NotNull
+ @NotNull
public String getRentalStation() {
return rentalStation;
}
@@ -246,7 +246,7 @@
=
Our well-known class Car from is now extended by
+ linkend=3D"example-class-car" /> is now extended by
RentalCar with the additional property
rentalStation. If an instance of
RentalCar is validated, not only the
@@ -279,13 +279,13 @@
Class Person
=
- package com.mycompany;
+ package com.mycomp=
any;
=
import javax.validation.constraints.NotNull;
=
public class Person {
=
- @NotNull
+ @NotNull
private String name;
=
public Person(String name) {
@@ -306,15 +306,15 @@
Adding a driver to the car
=
- package com.mycompany;
+ package com.mycomp=
any;
=
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
=
public class Car {
=
- @NotNull
- @Valid
+ @NotNull
+ @Valid
private Person driver;
=
public Car(Person driver) {
@@ -359,7 +359,7 @@
Car with a list of passengers
=
- package com.mycompany;
+ package com.mycomp=
any;
=
import java.util.ArrayList;
import java.util.List;
@@ -370,8 +370,8 @@
public class Car {
=
@NotNull
- @Valid
- private List<Person> passengers =3D new ArrayList<Person>(=
);
+ @Valid
+ private List<Person> passengers =3D new ArrayList<Person>(=
);
=
public Car(List<Person> passengers) {
this.passengers =3D passengers;
@@ -409,12 +409,12 @@
ValidatorFactory. The easiest way is to use t=
he
static
Validation.buildDefaultValidatorFactory()
- method:
+ method:
Validation.buildDefaultValidatorFactory()
=
- ValidatorFactory factory =3D Validation.buildDefaultValidatorFactory();
+ ValidatorFactory=
factory =3D Validation.buildDefaultValidatorFactory();
Validator validator =3D factory.getValidator();
- For other ways of obtaining a Validator instance see For other ways of obtaining a Validator instance s=
ee . For now we just want to see =
how we
can use the Validator instance to validate en=
tity
instances.
@@ -436,9 +436,10 @@
All the validation methods have a var-args parameter which can=
be
used to specify, which validation groups shall be considered when
performing the validation. If the parameter is not specified the def=
ault
- validation group (javax.validation.groups.Default) will
- be used. We will go into more detail on the topic of validation grou=
ps
- in
+ validation group
+ (javax.validation.groups.Default) will be use=
d.
+ We will go into more detail on the topic of validation groups in
=
validate
@@ -451,13 +452,12 @@
Usage of
Validator.validate()
=
- ValidatorFactory factory =3D Validation.buildDef=
aultValidatorFactory();
+ ValidatorFactory=
factory =3D Validation.buildDefaultValidatorFactory();
Validator validator =3D factory.getValidator();
=
Car car =3D new Car(null);
=
-Set<ConstraintViolation<Car>> constraintViolations =3D validator.validate(car);
+Set<ConstraintViolation<Car>> constraintViolations =3D validat=
or.validate(car);
=
assertEquals(1, constraintViolations.size());
assertEquals("may not be null", constraintViolations.iterator().next().get=
Message());
@@ -475,12 +475,11 @@
Usage of
Validator.validateProperty()
=
- Validator validator =3D Validation.buildDefaultV=
alidatorFactory().getValidator();
+ Validator valida=
tor =3D Validation.buildDefaultValidatorFactory().getValidator();
=
Car car =3D new Car(null);
=
-Set<ConstraintViolation<Car>> constraintViolations =3D validator.validateProperty(car, "manufacturer"=
);
+Set<ConstraintViolation<Car>> constraintViolations =3D validat=
or.validateProperty(car, "manufacturer");
=
assertEquals(1, constraintViolations.size());
assertEquals("may not be null", constraintViolations.iterator().next().get=
Message());
@@ -502,10 +501,9 @@
Usage of
Validator.validateValue()
=
- Validator validator =3D Validation.buildDefaultV=
alidatorFactory().getValidator();
+ Validator valida=
tor =3D Validation.buildDefaultValidatorFactory().getValidator();
=
-Set<ConstraintViolation<Car>> constraintViolations =3D validator.validateValue(Car.class, "manufactur=
er", null);
+Set<ConstraintViolation<Car>> constraintViolations =3D validat=
or.validateValue(Car.class, "manufacturer", null);
=
assertEquals(1, constraintViolations.size());
assertEquals("may not be null", constraintViolations.iterator().next().get=
Message());
@@ -668,7 +666,6 @@
=
Validating groups
-
Groups allow you to restrict the set of constraints applied duri=
ng
validation. This makes for example wizard like validation possible whe=
re
in each step only a specified subset of constraints get validated. The
@@ -682,19 +679,16 @@
constraint on name. Since no group is
specified for this annotation its default group is
javax.validation.groups.Default.
-
When more than one group is requested, the order in which the
groups are evaluated is not deterministic. If no group is specified =
the
default group javax.validation.groups.Default=
is
assumed.
-
-
+
Person
-
- public class Person {
- @NotNull
+ public class Perso=
n {
+ @NotNull
private String name;
=
public Person(String name) {
@@ -702,7 +696,9 @@
}
// getters and setters ...
}
- Next we have the class Driver (
+ =
+ Next we have the class Driver () extending Person=
. Here
we are adding the properties age and
hasDrivingLicense. In order to drive you must be =
at
@@ -713,18 +709,20 @@
DriverChecks is just a simple tagging interface.
Using interfaces makes the usage of groups type safe and allows for ea=
sy
refactoring. It also means that groups can inherit from each other via
- class inheritance.
+ class inheritance.
+
The Bean Validation specification does not enforce that grou=
ps
have to be interfaces. Non interface classes could be used as well,
- but we recommend to stick to interfaces.
-
+ but we recommend to stick to interfaces.
+
+
+
Driver
-
- public class Driver extends Person {
- @Min(value =3D 18, message =3D "You have to be=
18 to drive a car", groups =3D DriverChecks.class)
+ public class Drive=
r extends Person {
+ @Min(value =3D 18, message =3D "You have to be 18 to drive a car", gro=
ups =3D DriverChecks.class)
public int age;
=
- @AssertTrue(message =3D "You first have to pas=
s the driving test", groups =3D DriverChecks.class)
+ @AssertTrue(message =3D "You first have to pass the driving test", gro=
ups =3D DriverChecks.class)
public boolean hasDrivingLicense;
=
public Driver(String name) {
@@ -743,22 +741,25 @@
this.age =3D age;
}
}
-
+
+
Group interfaces
-
- public interface DriverChecks {
+ public interface D=
riverChecks {
}
=
public interface CarChecks {
}
- Last but not least we add the property
+
+ Last but not least we add the property
passedVehicleInspection to the
Car class ()
- indicating whether a car passed the road worthy tests.
+
Car
=
- public class Car {
+ public class Car {
@NotNull
private String manufacturer;
=
@@ -769,7 +770,7 @@
@Min(2)
private int seatCount;
=
- @AssertTrue(message =3D "The car has to pass t=
he vehicle inspection first", groups =3D CarChecks.class)
+ @AssertTrue(message =3D "The car has to pass the vehicle inspection fi=
rst", groups =3D CarChecks.class)
private boolean passedVehicleInspection;
=
@Valid
@@ -781,7 +782,8 @@
this.seatCount =3D seatCount;
}
}
- Overall three different groups are used in our example.
+
+ Overall three different groups are used in our example.
Person.name, Car.manufacturer,
Car.licensePlate and
Car.seatCount all belong to the
@@ -792,11 +794,12 @@
CarChecks.
shows how passing different group combinations to the
Validator.validate method result in different
- validation results.
+ validation results.
+
+
Drive away
+ public class Group=
Test {
=
- public class GroupTest {
-
private static Validator validator;
=
@BeforeClass
@@ -837,24 +840,26 @@
assertEquals( 0, validator.validate( car, Default.class, CarChecks=
.class, DriverChecks.class ).size() );
}
}
- First we create a car and validate it using no explicit gr=
oup.
+
+ First we create a car and validate it using no explicit group.
There are no validation errors, even though the property
passedVehicleInspection is per default
false. However, the constraint defined on this
- property does not belong to the default group.
+ property does not belong to the default group.
=
- Next we just validate the CarChecks group
+ Next we just validate the CarChecks group
which will fail until we make sure that the car passes the vehicle
- inspection.
+ inspection.
=
- When we then add a driver to the car and validate against
+ When we then add a driver to the car and validate against
DriverChecks we get again a constraint violation
due to the fact that the driver has not yet passed the driving test. O=
nly
after setting passedDrivingTest to true the
validation against DriverChecks will pass.
=
Last but not least, we show that all constraints are passing by
- validating against all defined groups.
+ validating against all defined groups.
+
=
Group sequences
@@ -877,13 +882,13 @@
validated.
=
-
+
Interface with @GroupSequence
=
- @GroupSequence({Default.=
class, CarChecks.class, DriverChecks.class})
+ @GroupSequence({=
Default.class, CarChecks.class, DriverChecks.class})
public interface OrderedChecks {
}
-
+
=
Groups defining a sequence and groups composing a sequence
@@ -897,7 +902,7 @@
Usage of a group sequence
=
- @Test
+ @Test
public void testOrderedChecks() {
Car car =3D new Car( "Morris", "DD-AB-123", 2 );
car.setPassedVehicleInspection( true );
@@ -929,7 +934,7 @@
RentalCar
=
- @GroupSequence({ RentalCar.class, CarChecks.class =
})
+ @GroupSequence({ R=
entalCar.class, CarChecks.class })
public class RentalCar extends Car {
public RentalCar(String manufacturer, String licencePlate, int seatCou=
nt) {
super( manufacturer, licencePlate, seatCount );
@@ -940,7 +945,7 @@
testOrderedChecksWithRedefinedDefault
=
- @Test
+ @Test
public void testOrderedChecksWithRedefinedDefault() {
RentalCar rentalCar =3D new RentalCar( "Morris", "DD-AB-123", 2 );
rentalCar.setPassedVehicleInspection( true );
@@ -1256,6 +1261,23 @@
=
+ @URL(protocol=3D, host=3D, port=3D)
+
+ yes
+
+ field/property. Supported types are
+ String.
+
+ Check if the annotated string is a valid URL. If any of
+ parameters protocol,
+ host or port is
+ specified the URL must match the specified values in the accor=
ding
+ part.
+
+ none
+
+
+
@Valid
=
yes
Modified: validator/trunk/hibernate-validator/src/main/docbook/en-US/module=
s/xmlconfiguration.xml
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/xmlc=
onfiguration.xml 2010-03-10 18:59:19 UTC (rev 18979)
+++ validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/xmlc=
onfiguration.xml 2010-03-10 22:13:09 UTC (rev 18980)
@@ -52,7 +52,7 @@
validation.xml
=
- <validation-config xmlns=3D"http://jboss.org/xml/=
ns/javax/validation/configuration"
+ <validation-config =
xmlns=3D"http://jboss.org/xml/ns/javax/validation/configuration"
xmlns:xsi=3D"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=3D"http://jboss.org/xml/ns/javax/validation/configurat=
ion">
<default-provider>org.hibernate.validator.HibernateValidator<=
/default-provider>
@@ -129,7 +129,7 @@
constraints-car.xml
=
- <constraint-mappings xmlns:xsi=3D"http://www.w3.o=
rg/2001/XMLSchema-instance"
+ <constraint-mapping=
s xmlns:xsi=3D"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=3D"http://jboss.org/xml/ns/javax/v=
alidation/mapping validation-mapping-1.0.xsd"
xmlns=3D"http://jboss.org/xml/ns/javax/validation/map=
ping">
<default-package>org.hibernate.validator.quickstart</default-=
package>
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/v=
alidator/constraints/URL.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validat=
or/constraints/URL.java 2010-03-10 18:59:19 UTC (rev 18979)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validat=
or/constraints/URL.java 2010-03-10 22:13:09 UTC (rev 18980)
@@ -40,6 +40,12 @@
@Target({ METHOD, FIELD, TYPE })
@Retention(RUNTIME)
public @interface URL {
+ public abstract String protocol() default "";
+
+ public abstract String host() default "";
+
+ public abstract int port() default -1;
+
public abstract String message() default "{org.hibernate.validator.constr=
aints.URL.message}";
=
public abstract Class>[] groups() default { };
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/v=
alidator/constraints/impl/URLValidator.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validat=
or/constraints/impl/URLValidator.java 2010-03-10 18:59:19 UTC (rev 18979)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validat=
or/constraints/impl/URLValidator.java 2010-03-10 22:13:09 UTC (rev 18980)
@@ -29,19 +29,41 @@
* @author Hardy Ferentschik
*/
public class URLValidator implements ConstraintValidator {
+ private String protocol;
+ private String host;
+ private int port;
+
public void initialize(URL url) {
+ this.protocol =3D url.protocol();
+ this.host =3D url.host();
+ this.port =3D url.port();
}
=
public boolean isValid(String value, ConstraintValidatorContext constrain=
tValidatorContext) {
if ( value =3D=3D null ) {
return true;
}
+
+ java.net.URL url;
try {
- new java.net.URL( value );
- return true;
+ url =3D new java.net.URL( value );
}
catch ( MalformedURLException e ) {
return false;
}
+
+ if ( protocol !=3D null && protocol.length() > 0 && !url.getProtocol().e=
quals( protocol ) ) {
+ return false;
+ }
+
+ if ( host !=3D null && host.length() > 0 && !url.getHost().equals( host =
) ) {
+ return false;
+ }
+
+ if ( port !=3D -1 && url.getPort() !=3D port ) {
+ return false;
+ }
+
+ return true;
}
}
\ No newline at end of file
Modified: validator/trunk/hibernate-validator/src/main/resources/org/hibern=
ate/validator/ValidationMessages.properties
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- validator/trunk/hibernate-validator/src/main/resources/org/hibernate/va=
lidator/ValidationMessages.properties 2010-03-10 18:59:19 UTC (rev 18979)
+++ validator/trunk/hibernate-validator/src/main/resources/org/hibernate/va=
lidator/ValidationMessages.properties 2010-03-10 22:13:09 UTC (rev 18980)
@@ -16,4 +16,5 @@
org.hibernate.validator.constraints.Length.message=3Dlength must be betwee=
n {min} and {max}
org.hibernate.validator.constraints.NotEmpty.message=3Dmay not be empty
org.hibernate.validator.constraints.Range.message=3Dmust be between {min} =
and {max}
+org.hibernate.validator.constraints.URL.message=3Dmust be a valid URL
=
Modified: validator/trunk/hibernate-validator/src/main/resources/org/hibern=
ate/validator/ValidationMessages_de.properties
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- validator/trunk/hibernate-validator/src/main/resources/org/hibernate/va=
lidator/ValidationMessages_de.properties 2010-03-10 18:59:19 UTC (rev 18979)
+++ validator/trunk/hibernate-validator/src/main/resources/org/hibernate/va=
lidator/ValidationMessages_de.properties 2010-03-10 22:13:09 UTC (rev 18980)
@@ -15,4 +15,5 @@
javax.validation.constraints.DecimalMin.message=3Dmuss gr\u00F6ssergleich =
{value} sein
javax.validation.constraints.DecimalMax.message=3Dmuss kleinergleich {valu=
e} sein
org.hibernate.validator.constraints.Email.message=3Dkeine g\u00FCltige E-M=
ail-Adresse
-org.hibernate.validator.constraints.Range.message=3Dmuss zwischen {min} un=
d {max} liegen
\ No newline at end of file
+org.hibernate.validator.constraints.Range.message=3Dmuss zwischen {min} un=
d {max} liegen
+org.hibernate.validator.constraints.URL.message=3Dmuss eine g\u00FCltige U=
RL sein
\ No newline at end of file
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/v=
alidator/constraints/impl/URLValidatorTest.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validat=
or/constraints/impl/URLValidatorTest.java 2010-03-10 18:59:19 UTC (rev 1897=
9)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validat=
or/constraints/impl/URLValidatorTest.java 2010-03-10 22:13:09 UTC (rev 1898=
0)
@@ -17,16 +17,17 @@
*/
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;
=
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+
/**
- * Tests the {@code Url} constraint.
+ * Tests the {@code URL} constraint. See HV-229
*
* @author Hardy Ferentschik
*/
@@ -35,14 +36,73 @@
@Test
public void testIsValidUrl() {
AnnotationDescriptor descriptor =3D new AnnotationDescriptor( =
URL.class );
- descriptor.setValue( "message", "{org.hibernate.validator.constraints.UR=
L.message}" );
URL url =3D AnnotationFactory.create( descriptor );
URLValidator validator =3D 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 ) );
}
+
+
+ @Test
+ public void testIsValidUrlWithProtocolSpecified() {
+ AnnotationDescriptor descriptor =3D new AnnotationDescriptor( =
URL.class );
+ descriptor.setValue( "protocol", "http" );
+ URL url =3D AnnotationFactory.create( descriptor );
+ URLValidator validator =3D new URLValidator();
+ validator.initialize( url );
+
+ assertFalse( validator.isValid( "ftp://abc.de", null ) );
+ assertTrue( validator.isValid( "http://abc.de", null ) );
+
+ descriptor =3D new AnnotationDescriptor( URL.class );
+ descriptor.setValue( "protocol", "file" );
+ url =3D AnnotationFactory.create( descriptor );
+ validator =3D new URLValidator(); =
+ validator.initialize( url );
+ assertFalse( validator.isValid( "http://abc.de", null ) );
+ assertTrue( validator.isValid( "file://Users/foobar/tmp", null ) );
+ }
+
+ @Test
+ public void testIsValidUrlWithPortSpecified() {
+ AnnotationDescriptor descriptor =3D new AnnotationDescriptor( =
URL.class );
+ descriptor.setValue( "port", 21 );
+ URL url =3D AnnotationFactory.create( descriptor );
+ URLValidator validator =3D new URLValidator();
+ validator.initialize( url );
+
+ assertFalse( validator.isValid( "ftp://abc.de", null ) );
+ assertTrue( validator.isValid( "ftp://abc.de:21", null ) );
+ }
+
+ @Test
+ public void testIsValidUrlWithHostSpecified() {
+ AnnotationDescriptor descriptor =3D new AnnotationDescriptor( =
URL.class );
+ descriptor.setValue( "host", "foobar.com" );
+ URL url =3D AnnotationFactory.create( descriptor );
+ URLValidator validator =3D new URLValidator();
+ validator.initialize( url );
+
+ assertFalse( validator.isValid( "http://fubar.com/this/is/foobar.html", =
null ) );
+ assertTrue( validator.isValid( "http://foobar.com/this/is/foobar.html", =
null ) );
+ }
+
+ @Test
+ public void testIsValidUrlWithProtocolHostAndPort() {
+ AnnotationDescriptor descriptor =3D new AnnotationDescriptor( =
URL.class );
+ descriptor.setValue( "protocol", "http" );
+ descriptor.setValue( "host", "www.hibernate.org" );
+ descriptor.setValue( "port", 80 );
+ URL url =3D AnnotationFactory.create( descriptor );
+ URLValidator validator =3D new URLValidator();
+ validator.initialize( url );
+
+ assertFalse( validator.isValid( "ftp://www#hibernate#org:80", null ) );
+ assertTrue( validator.isValid( "http://www.hibernate.org:80", null ) );
+ }
}
\ No newline at end of file
--===============4312351118631570783==--