1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.rules.constraint.property;
17
18 import java.util.Arrays;
19 import java.util.Collection;
20
21 import junit.framework.TestCase;
22
23
24
25
26
27 public class PropertyInGroupConstraintTests extends TestCase {
28
29 public void testConstraintWithObjectArray() {
30 MyTestBean bean = new MyTestBean();
31 AbstractPropertyConstraint constraint = new PropertyInGroupConstraint("property", "arrayvalues");
32 assertFalse(constraint.test(bean));
33 bean.setProperty("value1");
34 assertFalse(constraint.test(bean));
35 bean.setArrayvalues(new String[] { "value1", "value2" });
36 assertTrue(constraint.test(bean));
37 bean.setArrayvalues(new String[] { "value2" });
38 assertFalse(constraint.test(bean));
39 bean.setProperty("value3");
40 assertFalse(constraint.test(bean));
41 bean.setArrayvalues(new String[] { "value3" });
42 assertTrue(constraint.test(bean));
43 bean.setArrayvalues(null);
44 assertFalse(constraint.test(bean));
45 }
46
47 public void testConstraintWithObjectCollection() {
48 MyTestBean bean = new MyTestBean();
49 AbstractPropertyConstraint constraint = new PropertyInGroupConstraint("property", "collectionvalues");
50 assertFalse(constraint.test(bean));
51 bean.setProperty("value1");
52 assertFalse(constraint.test(bean));
53 bean.setCollectionvalues(Arrays.asList(new String[] { "value1", "value2" }));
54 assertTrue(constraint.test(bean));
55 bean.setCollectionvalues(Arrays.asList(new String[] { "value2" }));
56 assertFalse(constraint.test(bean));
57 bean.setProperty("value3");
58 assertFalse(constraint.test(bean));
59 bean.setCollectionvalues(Arrays.asList(new String[] { "value3" }));
60 assertTrue(constraint.test(bean));
61 bean.setCollectionvalues(null);
62 assertFalse(constraint.test(bean));
63 }
64
65 private static class MyTestBean {
66 private String property;
67
68 private String[] arrayvalues;
69
70 private Collection collectionvalues;
71
72
73
74
75 public Collection getCollectionvalues() {
76 return collectionvalues;
77 }
78
79
80
81
82 public void setCollectionvalues(Collection collectionvalues) {
83 this.collectionvalues = collectionvalues;
84 }
85
86 public String getProperty() {
87 return property;
88 }
89
90 public void setProperty(String property) {
91 this.property = property;
92 }
93
94 public String[] getArrayvalues() {
95 return arrayvalues;
96 }
97
98 public void setArrayvalues(String[] values) {
99 this.arrayvalues = values;
100 }
101 }
102 }