001    /*
002     * Copyright 2002-2006 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.rules.constraint.property;
017    
018    import java.util.Arrays;
019    import java.util.Collection;
020    
021    import junit.framework.TestCase;
022    
023    /**
024     * @author Mathias Broekelmann
025     * 
026     */
027    public class PropertyInGroupConstraintTests extends TestCase {
028    
029            public void testConstraintWithObjectArray() {
030                    MyTestBean bean = new MyTestBean();
031                    AbstractPropertyConstraint constraint = new PropertyInGroupConstraint("property", "arrayvalues");
032                    assertFalse(constraint.test(bean));
033                    bean.setProperty("value1");
034                    assertFalse(constraint.test(bean));
035                    bean.setArrayvalues(new String[] { "value1", "value2" });
036                    assertTrue(constraint.test(bean));
037                    bean.setArrayvalues(new String[] { "value2" });
038                    assertFalse(constraint.test(bean));
039                    bean.setProperty("value3");
040                    assertFalse(constraint.test(bean));
041                    bean.setArrayvalues(new String[] { "value3" });
042                    assertTrue(constraint.test(bean));
043                    bean.setArrayvalues(null);
044                    assertFalse(constraint.test(bean));
045            }
046    
047            public void testConstraintWithObjectCollection() {
048                    MyTestBean bean = new MyTestBean();
049                    AbstractPropertyConstraint constraint = new PropertyInGroupConstraint("property", "collectionvalues");
050                    assertFalse(constraint.test(bean));
051                    bean.setProperty("value1");
052                    assertFalse(constraint.test(bean));
053                    bean.setCollectionvalues(Arrays.asList(new String[] { "value1", "value2" }));
054                    assertTrue(constraint.test(bean));
055                    bean.setCollectionvalues(Arrays.asList(new String[] { "value2" }));
056                    assertFalse(constraint.test(bean));
057                    bean.setProperty("value3");
058                    assertFalse(constraint.test(bean));
059                    bean.setCollectionvalues(Arrays.asList(new String[] { "value3" }));
060                    assertTrue(constraint.test(bean));
061                    bean.setCollectionvalues(null);
062                    assertFalse(constraint.test(bean));
063            }
064    
065            private static class MyTestBean {
066                    private String property;
067    
068                    private String[] arrayvalues;
069    
070                    private Collection collectionvalues;
071    
072                    /**
073                     * @return the collectionvalues
074                     */
075                    public Collection getCollectionvalues() {
076                            return collectionvalues;
077                    }
078    
079                    /**
080                     * @param collectionvalues the collectionvalues to set
081                     */
082                    public void setCollectionvalues(Collection collectionvalues) {
083                            this.collectionvalues = collectionvalues;
084                    }
085    
086                    public String getProperty() {
087                            return property;
088                    }
089    
090                    public void setProperty(String property) {
091                            this.property = property;
092                    }
093    
094                    public String[] getArrayvalues() {
095                            return arrayvalues;
096                    }
097    
098                    public void setArrayvalues(String[] values) {
099                            this.arrayvalues = values;
100                    }
101            }
102    }