1   /*
2    * Copyright 2002-2006 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
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   * @author Mathias Broekelmann
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  		 * @return the collectionvalues
74  		 */
75  		public Collection getCollectionvalues() {
76  			return collectionvalues;
77  		}
78  
79  		/**
80  		 * @param collectionvalues the collectionvalues to set
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 }