View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.bean_validation.test;
18  
19  import java.util.Set;
20  
21  import javax.inject.Inject;
22  import javax.validation.ConstraintViolation;
23  import javax.validation.Validator;
24  
25  import org.jboss.arquillian.container.test.api.Deployment;
26  import org.jboss.arquillian.junit.Arquillian;
27  import org.jboss.as.quickstarts.bean_validation.model.Member;
28  import org.jboss.shrinkwrap.api.Archive;
29  import org.jboss.shrinkwrap.api.ShrinkWrap;
30  import org.jboss.shrinkwrap.api.asset.EmptyAsset;
31  import org.jboss.shrinkwrap.api.spec.WebArchive;
32  import org.junit.Assert;
33  import org.junit.Test;
34  import org.junit.runner.RunWith;
35  
36  /**
37   * Simple tests for Bean Validator. Arquillian deploys an WAR archive to the application server, which constructs Validator
38   * object.
39   * 
40   * This object is injected into the tests so user can verify the validators are working. This example does not touch validation
41   * on database layer, e.g. it is not validating uniqueness constraint for email field.
42   * 
43   * 
44   * @author <a href="kpiwko@redhat.com>Karel Piwko</a>
45   * 
46   */
47  @RunWith(Arquillian.class)
48  public class MemberValidationTest {
49  
50      /**
51       * Constructs a deployment archive
52       * 
53       * @return the deployment archive
54       */
55      @Deployment
56      public static Archive<?> createTestArchive() {
57          return ShrinkWrap.create(WebArchive.class, "test.war").addClasses(Member.class)
58          // enable JPA
59                  .addAsResource("META-INF/test-persistence.xml", "META-INF/persistence.xml")
60                  // add sample data
61                  .addAsResource("import.sql")
62                  // enable CDI
63                  .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
64                  // Deploy our test datasource
65                  .addAsWebInfResource("test-ds.xml", "test-ds.xml");
66      }
67  
68      // Get configured validator directly from JBoss EAP 6 environment
69      @Inject
70      Validator validator;
71  
72      /**
73       * Tests an empty member registration, e.g. violation of:
74       * 
75       * <ul>
76       * <li>@NotNull</li>
77       * <li>@NotNull</li>
78       * <li>@Email</li>
79       * <li>@Size</li>
80       * </ul>
81       */
82      @Test
83      public void testRegisterEmptyMember() {
84  
85          Member member = new Member();
86          Set<ConstraintViolation<Member>> violations = validator.validate(member);
87  
88          Assert.assertEquals("Four violations were found", 4, violations.size());
89      }
90  
91      /**
92       * Tests an valid member registration
93       */
94      @Test
95      public void testRegisterValidMember() {
96          Set<ConstraintViolation<Member>> violations = validator.validate(createValidMember());
97  
98          Assert.assertEquals("No violations were found", 0, violations.size());
99      }
100 
101     /**
102      * Tests {@code @Pattern} constraint
103      */
104     @Test
105     public void testNameViolation() {
106         Member member = createValidMember();
107         member.setName("Joe Doe-Dah");
108         Set<ConstraintViolation<Member>> violations = validator.validate(member);
109 
110         Assert.assertEquals("One violation was found", 1, violations.size());
111         Assert.assertEquals("Name was invalid", "must contain only letters and spaces", violations.iterator().next()
112                 .getMessage());
113     }
114 
115     /**
116      * Tests {@code @Email} constraint
117      */
118     @Test
119     public void testEmailViolation() {
120         Member member = createValidMember();
121         member.setEmail("invalid-email");
122         Set<ConstraintViolation<Member>> violations = validator.validate(member);
123 
124         Assert.assertEquals("One violation was found", 1, violations.size());
125         Assert.assertEquals("Email was invalid", "not a well-formed email address", violations.iterator().next().getMessage());
126     }
127 
128     /**
129      * Tests {@code @Size} constraint
130      */
131     @Test
132     public void testPhoneViolation() {
133         Member member = createValidMember();
134         member.setPhoneNumber("123");
135         Set<ConstraintViolation<Member>> violations = validator.validate(member);
136 
137         Assert.assertEquals("One violation was found", 1, violations.size());
138         Assert.assertEquals("Phone number was invalid", "size must be between 10 and 12", violations.iterator().next()
139                 .getMessage());
140     }
141 
142     private Member createValidMember() {
143         Member member = new Member();
144         member.setEmail("jdoe@test.org");
145         member.setName("John Doe");
146         member.setPhoneNumber("1234567890");
147         return member;
148     }
149 
150 }