1
2
3
4
5
6
7
8
9
10
11
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
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
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
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 }