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 junit.framework.TestCase;
16  import net.sf.oval.constraint.MaxLength;
17  import net.sf.oval.constraint.NotNull;
18  import net.sf.oval.exception.ConstraintsViolatedException;
19  import net.sf.oval.guard.Guarded;
20  import net.sf.oval.guard.SuppressOValWarnings;
21  
22  import org.springframework.context.support.ClassPathXmlApplicationContext;
23  
24  /**
25  * @author Sebastian Thomschke
26  */
27  public class SpringAOPAllianceTest extends TestCase
28  {
29  	public static interface TestServiceInterface
30  	{
31  		@MaxLength(value = 5, message = "MAX_LENGTH")
32  		String getSomething(@NotNull(message = "NOT_NULL") final String input);
33  	}
34  
35  	/**
36  	 * interface based service
37  	 */
38  	@Guarded(inspectInterfaces = true)
39  	public static class TestServiceWithInterface implements TestServiceInterface
40  	{
41  		public String getSomething(final String input)
42  		{
43  			return input;
44  		}
45  	}
46  
47  	/**
48  	 * class based service
49  	 */
50  	public static class TestServiceWithoutInterface
51  	{
52  		@SuppressOValWarnings
53  		@MaxLength(value = 5, message = "MAX_LENGTH")
54  		public String getSomething(@NotNull(message = "NOT_NULL") final String input)
55  		{
56  			return input;
57  		}
58  	}
59  
60  	public void testCGLIBProxying()
61  	{
62  		final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
63  				"SpringAOPAllianceTestCGLIBProxy.xml", SpringAOPAllianceTest.class);
64  
65  		{
66  			final TestServiceWithoutInterface testServiceWithoutInterface = (TestServiceWithoutInterface) ctx
67  					.getBean("testServiceWithoutInterface");
68  
69  			try
70  			{
71  				testServiceWithoutInterface.getSomething(null);
72  				fail();
73  			}
74  			catch (final ConstraintsViolatedException ex)
75  			{
76  				assertEquals("NOT_NULL", ex.getConstraintViolations()[0].getMessage());
77  			}
78  
79  			try
80  			{
81  				testServiceWithoutInterface.getSomething("123456");
82  				fail();
83  			}
84  			catch (final ConstraintsViolatedException ex)
85  			{
86  				assertEquals("MAX_LENGTH", ex.getConstraintViolations()[0].getMessage());
87  			}
88  		}
89  
90  		{
91  			final TestServiceInterface testServiceWithInterface = ctx.getBean("testServiceWithInterface",
92  					TestServiceInterface.class);
93  
94  			try
95  			{
96  				testServiceWithInterface.getSomething(null);
97  				fail();
98  			}
99  			catch (final ConstraintsViolatedException ex)
100 			{
101 				assertEquals("NOT_NULL", ex.getConstraintViolations()[0].getMessage());
102 			}
103 
104 			try
105 			{
106 				testServiceWithInterface.getSomething("123456");
107 				fail();
108 			}
109 			catch (final ConstraintsViolatedException ex)
110 			{
111 				assertEquals("MAX_LENGTH", ex.getConstraintViolations()[0].getMessage());
112 			}
113 		}
114 	}
115 
116 	public void testJDKProxying()
117 	{
118 		final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
119 				"SpringAOPAllianceTestJDKProxy.xml", SpringAOPAllianceTest.class);
120 		final TestServiceInterface testServiceWithInterface = ctx.getBean("testServiceWithInterface",
121 				TestServiceInterface.class);
122 
123 		try
124 		{
125 			testServiceWithInterface.getSomething(null);
126 			fail();
127 		}
128 		catch (final ConstraintsViolatedException ex)
129 		{
130 			assertEquals("NOT_NULL", ex.getConstraintViolations()[0].getMessage());
131 		}
132 
133 		try
134 		{
135 			testServiceWithInterface.getSomething("123456");
136 			fail();
137 		}
138 		catch (final ConstraintsViolatedException ex)
139 		{
140 			assertEquals("MAX_LENGTH", ex.getConstraintViolations()[0].getMessage());
141 		}
142 	}
143 }