[hibernate-commits] Hibernate SVN: r16333 - in validator/trunk: hibernate-validator and 14 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue Apr 14 13:34:43 EDT 2009


Author: hardy.ferentschik
Date: 2009-04-14 13:34:43 -0400 (Tue, 14 Apr 2009)
New Revision: 16333

Added:
   validator/trunk/hibernate-validator-archetype/pom.xml
   validator/trunk/hibernate-validator-archetype/src/
   validator/trunk/hibernate-validator-archetype/src/main/
   validator/trunk/hibernate-validator-archetype/src/main/java/
   validator/trunk/hibernate-validator-archetype/src/main/java/org/
   validator/trunk/hibernate-validator-archetype/src/main/java/org/hibernate/
   validator/trunk/hibernate-validator-archetype/src/main/java/org/hibernate/validator/
   validator/trunk/hibernate-validator-archetype/src/main/java/org/hibernate/validator/quickstart/
   validator/trunk/hibernate-validator-archetype/src/main/java/org/hibernate/validator/quickstart/Car.java
   validator/trunk/hibernate-validator-archetype/src/test/
   validator/trunk/hibernate-validator-archetype/src/test/java/
   validator/trunk/hibernate-validator-archetype/src/test/java/org/
   validator/trunk/hibernate-validator-archetype/src/test/java/org/hibernate/
   validator/trunk/hibernate-validator-archetype/src/test/java/org/hibernate/validator/
   validator/trunk/hibernate-validator-archetype/src/test/java/org/hibernate/validator/quickstart/
   validator/trunk/hibernate-validator-archetype/src/test/java/org/hibernate/validator/quickstart/CarTest.java
Modified:
   validator/trunk/hibernate-validator/pom.xml
   validator/trunk/pom.xml
Log:
HV-137

Modified: validator/trunk/hibernate-validator/pom.xml
===================================================================
--- validator/trunk/hibernate-validator/pom.xml	2009-04-14 17:12:42 UTC (rev 16332)
+++ validator/trunk/hibernate-validator/pom.xml	2009-04-14 17:34:43 UTC (rev 16333)
@@ -84,6 +84,19 @@
         </resources>
         <plugins>
             <plugin>
+                <inherited>true</inherited>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-source-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>attach-sources</id>
+                        <goals>
+                            <goal>jar</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
                 <groupId>org.codehaus.mojo</groupId>
                 <artifactId>jaxb2-maven-plugin</artifactId>
                 <executions>

Added: validator/trunk/hibernate-validator-archetype/pom.xml
===================================================================
--- validator/trunk/hibernate-validator-archetype/pom.xml	                        (rev 0)
+++ validator/trunk/hibernate-validator-archetype/pom.xml	2009-04-14 17:34:43 UTC (rev 16333)
@@ -0,0 +1,58 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project 
+	xmlns="http://maven.apache.org/POM/4.0.0" 
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+   <parent>
+        <artifactId>hibernate-validator-parent</artifactId>
+        <groupId>org.hibernate</groupId>
+        <version>4.0.0.Beta1-SNAPSHOT</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>  
+    <groupId>org.hibernate</groupId>
+    <artifactId>hibernate-validator-quickstart</artifactId>
+    <name>Hibernate Validator Quickstart Archetype</name>
+	<dependencies>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.5</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.slf4j</groupId>
+			<artifactId>slf4j-log4j12</artifactId>
+			<version>1.4.2</version>
+			<scope>runtime</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.hibernate</groupId>
+			<artifactId>hibernate-validator</artifactId>
+			<version>4.0.0.Beta1-SNAPSHOT</version>
+		</dependency>
+	</dependencies>
+	<build>
+		<plugins>
+			<plugin>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<version>RELEASE</version>
+				<configuration>
+					<source>1.5</source>
+					<target>1.5</target>
+				</configuration>
+			</plugin>
+		</plugins>
+	</build>
+	<repositories>
+		<repository>
+			<id>jboss</id>
+			<url>http://repository.jboss.com/maven2</url>
+			<releases>
+			</releases>
+			<snapshots>
+				<enabled>false</enabled>
+			</snapshots>
+		</repository>
+	</repositories>
+</project>
\ No newline at end of file

Added: validator/trunk/hibernate-validator-archetype/src/main/java/org/hibernate/validator/quickstart/Car.java
===================================================================
--- validator/trunk/hibernate-validator-archetype/src/main/java/org/hibernate/validator/quickstart/Car.java	                        (rev 0)
+++ validator/trunk/hibernate-validator-archetype/src/main/java/org/hibernate/validator/quickstart/Car.java	2009-04-14 17:34:43 UTC (rev 16333)
@@ -0,0 +1,85 @@
+/**
+ * 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.quickstart;
+
+import javax.validation.constraints.Min;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
+
+/**
+ * An exemplary model class, which is enriched with constraint annotations from
+ * the Bean Validation API (<a href="http://jcp.org/en/jsr/detail?id=303">JSR
+ * 303</a>). Have a look at {@link CarTest} to learn, how the Bean Validation
+ * API can be used to validate Car instances.
+ * 
+ * @author Gunnar Morling
+ */
+public class Car {
+
+	/**
+	 * By annotating the field with @NotNull we specify, that null is not a valid
+	 * value.
+	 */
+    @NotNull
+    private String manufacturer;
+
+    /**
+     * This String field shall not only not allowed to be null, it shall also between
+     * 2 and 14 characters long. 
+     */
+    @NotNull
+    @Size(min = 2, max = 14)
+    private String licensePlate;
+
+    /**
+     * This int field shall have a value of at least 2.
+     */
+    @Min(2)
+    private int seatCount;
+    
+    public Car(String manufacturer, String licencePlate, int seatCount) {
+
+        this.manufacturer = manufacturer;
+        this.licensePlate = licencePlate;
+        this.seatCount = seatCount;
+    }
+
+	public String getManufacturer() {
+		return manufacturer;
+	}
+
+	public void setManufacturer(String manufacturer) {
+		this.manufacturer = manufacturer;
+	}
+
+	public String getLicensePlate() {
+		return licensePlate;
+	}
+
+	public void setLicensePlate(String licensePlate) {
+		this.licensePlate = licensePlate;
+	}
+
+	public int getSeatCount() {
+		return seatCount;
+	}
+
+	public void setSeatCount(int seatCount) {
+		this.seatCount = seatCount;
+	}
+
+}
\ No newline at end of file

Added: validator/trunk/hibernate-validator-archetype/src/test/java/org/hibernate/validator/quickstart/CarTest.java
===================================================================
--- validator/trunk/hibernate-validator-archetype/src/test/java/org/hibernate/validator/quickstart/CarTest.java	                        (rev 0)
+++ validator/trunk/hibernate-validator-archetype/src/test/java/org/hibernate/validator/quickstart/CarTest.java	2009-04-14 17:34:43 UTC (rev 16333)
@@ -0,0 +1,131 @@
+/**
+ * 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.quickstart;
+
+import static org.junit.Assert.*;
+
+import java.util.Set;
+
+import javax.validation.ConstraintViolation;
+import javax.validation.Validation;
+import javax.validation.Validator;
+import javax.validation.ValidatorFactory;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * <p>
+ * A module test that shows how to use the Bean Validation (BV) API to validate
+ * the constraint annotations at the exemplary {@link Car} model class.
+ * </p>
+ * <p>
+ * The interface {@link Validator} is the main entry point the BV API. The
+ * test makes use of the <code>validate()</code> method of that interface, which
+ * returns a set of <code>ConstraintViolation</code>s, that describe the
+ * problems occurred during validation.
+ * </p>
+ * <p>
+ * In case the object in question could be validated successfully this set will
+ * be empty.
+ * </p>
+ * 
+ * @author Gunnar Morling
+ */
+public class CarTest {
+
+	/**
+	 * The validator to be used for object validation. Will be retrieved once
+	 * for all test methods.
+	 */
+    private static Validator validator;
+
+    /**
+     * Retrieves the validator instance.
+     */
+    @BeforeClass
+    public static void setUp() {
+        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
+        validator = factory.getValidator();
+    }
+
+    /**
+	 * One constraint violation due to the manufacturer field being null
+	 * expected.
+	 */
+    @Test
+    public void manufacturerIsNull() {
+
+        Car car = new Car(null, "DD-AB-123", 4);
+
+        Set<ConstraintViolation<Car>> constraintViolations =
+            validator.validate(car);
+
+        assertEquals(1, constraintViolations.size());
+        assertEquals(
+            "may not be null", constraintViolations.iterator().next().getMessage());
+    }
+
+    /**
+	 * One constraint violation due to the licensePlate field being too short
+	 * expected.
+	 */
+    @Test
+    public void licensePlateTooShort() {
+
+        Car car = new Car("Morris", "D", 4);
+
+        Set<ConstraintViolation<Car>> constraintViolations = 
+            validator.validate(car);
+
+        assertEquals(1, constraintViolations.size());
+        assertEquals(
+            "size must be between 2 and 14", constraintViolations.iterator().next().getMessage());
+    }
+    
+    /**
+	 * One constraint violation due to the seatCount field being too low
+	 * expected.
+	 */
+    @Test
+    public void seatCountTooLow() {
+
+        Car car = new Car("Morris", "DD-AB-123", 1);
+
+        Set<ConstraintViolation<Car>> constraintViolations =
+            validator.validate(car);
+
+        assertEquals(1, constraintViolations.size());
+        assertEquals(
+            "must be greater than or equal to 2", constraintViolations.iterator().next().getMessage());
+    }
+
+    /**
+	 * No constraint violation expected, as all fields of the validated Car
+	 * instance have proper values.
+	 */
+    @Test
+    public void carIsValid() {
+
+        Car car = new Car("Morris", "DD-AB-123", 2);
+
+        Set<ConstraintViolation<Car>> constraintViolations =
+            validator.validate(car);
+
+        assertEquals(0, constraintViolations.size());
+    }
+}
\ No newline at end of file

Modified: validator/trunk/pom.xml
===================================================================
--- validator/trunk/pom.xml	2009-04-14 17:12:42 UTC (rev 16332)
+++ validator/trunk/pom.xml	2009-04-14 17:34:43 UTC (rev 16333)
@@ -37,6 +37,7 @@
     
     <modules>
         <module>hibernate-validator</module>
+        <module>hibernate-validator-archetype</module>
         <module>hibernate-validator-legacy</module>
     </modules>
 




More information about the hibernate-commits mailing list