001: /*******************************************************************************
002: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
003: * Thomschke.
004: *
005: * All Rights Reserved. This program and the accompanying materials
006: * are made available under the terms of the Eclipse Public License v1.0
007: * which accompanies this distribution, and is available at
008: * http://www.eclipse.org/legal/epl-v10.html
009: *
010: * Contributors:
011: * Sebastian Thomschke - initial implementation.
012: *******************************************************************************/package net.sf.oval.test.validator;
013:
014: import java.util.ArrayList;
015: import java.util.Collection;
016: import java.util.List;
017:
018: import javax.persistence.Basic;
019: import javax.persistence.Column;
020: import javax.persistence.Entity;
021: import javax.persistence.ManyToOne;
022: import javax.persistence.OneToMany;
023: import javax.persistence.OneToOne;
024:
025: import junit.framework.TestCase;
026: import net.sf.oval.ConstraintViolation;
027: import net.sf.oval.Validator;
028: import net.sf.oval.configuration.annotation.JPAAnnotationsConfigurer;
029:
030: /**
031: * @author Sebastian Thomschke
032: *
033: */
034: public class JPAAnnotationsConfigurerTest extends TestCase {
035: @Entity
036: protected static class TestEntity {
037: // -> @NotNull
038: @Basic(optional=false)
039: // -> @MaxLength(4)
040: @Column(length=4)
041: public String code;
042:
043: // -> @NotNull
044: @Column(nullable=false)
045: public String description;
046:
047: // -> @NotNull & @AssertValid
048: @ManyToOne(optional=false)
049: public TestEntity ref1;
050:
051: // -> @AssertValid
052: @OneToOne(optional=true)
053: public TestEntity ref2;
054:
055: // -> @AssertValid
056: @OneToMany
057: public Collection<TestEntity> refs;
058: }
059:
060: public void testJPAAnnotationsConfigurer() {
061: Validator v = new Validator(new JPAAnnotationsConfigurer());
062: List<ConstraintViolation> violations;
063:
064: TestEntity entity;
065:
066: {
067: entity = new TestEntity();
068:
069: violations = v.validate(entity);
070: // code is null
071: // description is null
072: // ref1 is null
073: assertEquals(3, violations.size());
074: assertNull(violations.get(0).getInvalidValue());
075: assertNull(violations.get(1).getInvalidValue());
076: assertNull(violations.get(2).getInvalidValue());
077: }
078:
079: {
080: entity.code = "";
081: entity.description = "";
082: entity.ref1 = new TestEntity();
083:
084: violations = v.validate(entity);
085: // ref1 is invalid
086: assertEquals(1, violations.size());
087: }
088:
089: {
090: entity.ref1.code = "";
091: entity.ref1.description = "";
092: entity.ref1.ref1 = entity;
093:
094: violations = v.validate(entity);
095: assertEquals(0, violations.size());
096: }
097:
098: {
099: entity.ref2 = new TestEntity();
100:
101: violations = v.validate(entity);
102: // ref2 is invalid
103: assertEquals(1, violations.size());
104: }
105:
106: {
107: entity.ref2.code = "";
108: entity.ref2.description = "";
109: entity.ref2.ref1 = entity;
110:
111: violations = v.validate(entity);
112: assertEquals(0, violations.size());
113: }
114:
115: // Column length test
116: {
117: entity.code = "12345";
118: violations = v.validate(entity);
119: // code is too long
120: assertEquals(1, violations.size());
121:
122: entity.code = "";
123: }
124:
125: // OneToMany test
126: {
127: entity.refs = new ArrayList<TestEntity>();
128: TestEntity d = new TestEntity();
129: entity.refs.add(d);
130:
131: violations = v.validate(entity);
132: assertEquals(1, violations.size());
133:
134: d.code = "";
135: d.description = "";
136: d.ref1 = entity;
137:
138: violations = v.validate(entity);
139: assertEquals(0, violations.size());
140: }
141: }
142: }
|