View Javadoc

1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2010 Sebastian
3    * Thomschke.
4    * 
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    * 
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.test.integration.spring;
14  
15  import java.lang.annotation.ElementType;
16  import java.lang.annotation.Retention;
17  import java.lang.annotation.RetentionPolicy;
18  import java.lang.annotation.Target;
19  
20  import junit.framework.TestCase;
21  import net.sf.oval.Validator;
22  import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
23  import net.sf.oval.configuration.annotation.AnnotationsConfigurer;
24  import net.sf.oval.configuration.annotation.Constraint;
25  import net.sf.oval.context.OValContext;
26  import net.sf.oval.exception.OValException;
27  import net.sf.oval.integration.spring.BeanInjectingCheckInitializationListener;
28  
29  import org.springframework.beans.factory.annotation.Autowired;
30  import org.springframework.context.support.ClassPathXmlApplicationContext;
31  
32  /**
33  * @author Sebastian Thomschke
34  */
35  public class SpringInjectorTest extends TestCase
36  {
37  	public static class Entity
38  	{
39  		@SuppressWarnings("unused")
40  		@SpringNullContraint
41  		private String field;
42  	}
43  
44  	@Retention(RetentionPolicy.RUNTIME)
45  	@Target({ElementType.FIELD})
46  	@Constraint(checkWith = SpringNullContraintCheck.class)
47  	public @interface SpringNullContraint
48  	{
49  		//nothing
50  	}
51  
52  	public static class SpringNullContraintCheck extends AbstractAnnotationCheck<SpringNullContraint>
53  	{
54  		private static final long serialVersionUID = 1L;
55  
56  		@Autowired()
57  		private Object springConstraintBean;
58  
59  		public boolean isSatisfied(final Object validatedObject, final Object valueToValidate,
60  				final OValContext context, final Validator validator) throws OValException
61  		{
62  			return springConstraintBean != null && valueToValidate != null;
63  		}
64  	}
65  
66  	@SuppressWarnings("synthetic-access")
67  	public void testSpringInjector()
68  	{
69  		@SuppressWarnings("unused")
70  		final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("SpringInjectorTest.xml",
71  				SpringInjectorTest.class);
72  		final AnnotationsConfigurer myConfigurer = new AnnotationsConfigurer();
73  		myConfigurer.addCheckInitializationListener(BeanInjectingCheckInitializationListener.INSTANCE);
74  		final Validator v = new Validator(myConfigurer);
75  
76  		final Entity e = new Entity();
77  		assertEquals(1, v.validate(e).size());
78  		e.field = "whatever";
79  		assertEquals(0, v.validate(e).size());
80  	}
81  }