001    /*
002     * Copyright 2002-2004 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.metadata;
017    
018    import junit.framework.TestCase;
019    
020    /**
021     * @author Oliver Hutchison
022     */    
023    public class AttributesRulesSourceTests extends TestCase {
024            
025        public void testLoadsAttributes() {
026    //        Attributes attributes = new CommonsAttributes();        
027    //        assertTrue("You must compile the attributes to run this test", 
028    //                attributes.getAttributes(Foo.class).size() == 1);        
029    //        AttributesRulesSource ars = new AttributesRulesSource(attributes);
030    // 
031    //        Rules rules = ars.getRules(Foo.class);
032    //
033    //        CompoundPropertyConstraint pc1 = (CompoundPropertyConstraint) rules.getPropertyConstraint("property1");
034    //        assertNotNull(pc1);
035    //        assertEquals(3, ((CompoundConstraint) pc1.getPredicate()).size());
036    //        assertContains(pc1, Required.class);
037    //        assertContains(pc1, StringLengthConstraint.class);
038    //        assertContains(pc1, Not.class);
039    //        
040    //        PropertyConstraint pc2 = rules.getPropertyConstraint("property2");
041    //        assertNull(pc2);
042        }
043        
044    //    private void assertContains(CompoundPropertyConstraint cpc, Class constraintClass) {
045    //        CompoundConstraint cc = (CompoundConstraint) cpc.getPredicate();
046    //        for (int i = 0; i < cc.size(); i++) {
047    //            Constraint c = cc.get(i);
048    //            
049    //            if (c.getClass().equals(constraintClass)) {
050    //                return; 
051    //            }          
052    //            if (c instanceof PropertyValueConstraint) {                    
053    //                c = ((PropertyValueConstraint) c).getConstraint();
054    //                if (c.getClass().equals(constraintClass)) {
055    //                    return;
056    //                } 
057    //            }
058    //        }
059    //        fail("Could not find constraint with class [" + constraintClass + "]");
060    //    }
061    
062        /**
063         * @@Object()
064         */
065        public static class Foo {
066    
067            private String property1;
068    
069            /**
070             * @@Object()
071             * 
072             * @@StringLengthConstraint(255)
073             * @@Not(new PropertiesConstraint("property1", new EqualTo(),
074             *           "property2"))
075             */
076            public String getProperty1() {
077                return property1;
078            }
079    
080            /**
081             * @@Required()
082             */
083            public void setProperty1(String property1) {
084                this.property1 = property1;
085            }
086    
087            public String getProperty2() {
088                return "";
089            }
090    
091            public void setProperty2(String property2) {
092            }
093        }
094    }