1   /*
2    * Copyright 2002-2004 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.metadata;
17  
18  import junit.framework.TestCase;
19  
20  /**
21   * @author Oliver Hutchison
22   */    
23  public class AttributesRulesSourceTests extends TestCase {
24          
25      public void testLoadsAttributes() {
26  //        Attributes attributes = new CommonsAttributes();        
27  //        assertTrue("You must compile the attributes to run this test", 
28  //                attributes.getAttributes(Foo.class).size() == 1);        
29  //        AttributesRulesSource ars = new AttributesRulesSource(attributes);
30  // 
31  //        Rules rules = ars.getRules(Foo.class);
32  //
33  //        CompoundPropertyConstraint pc1 = (CompoundPropertyConstraint) rules.getPropertyConstraint("property1");
34  //        assertNotNull(pc1);
35  //        assertEquals(3, ((CompoundConstraint) pc1.getPredicate()).size());
36  //        assertContains(pc1, Required.class);
37  //        assertContains(pc1, StringLengthConstraint.class);
38  //        assertContains(pc1, Not.class);
39  //        
40  //        PropertyConstraint pc2 = rules.getPropertyConstraint("property2");
41  //        assertNull(pc2);
42      }
43      
44  //    private void assertContains(CompoundPropertyConstraint cpc, Class constraintClass) {
45  //        CompoundConstraint cc = (CompoundConstraint) cpc.getPredicate();
46  //        for (int i = 0; i < cc.size(); i++) {
47  //            Constraint c = cc.get(i);
48  //            
49  //            if (c.getClass().equals(constraintClass)) {
50  //                return; 
51  //            }          
52  //            if (c instanceof PropertyValueConstraint) {                    
53  //                c = ((PropertyValueConstraint) c).getConstraint();
54  //                if (c.getClass().equals(constraintClass)) {
55  //                    return;
56  //                } 
57  //            }
58  //        }
59  //        fail("Could not find constraint with class [" + constraintClass + "]");
60  //    }
61  
62      /**
63       * @@Object()
64       */
65      public static class Foo {
66  
67          private String property1;
68  
69          /**
70           * @@Object()
71           * 
72           * @@StringLengthConstraint(255)
73           * @@Not(new PropertiesConstraint("property1", new EqualTo(),
74           *           "property2"))
75           */
76          public String getProperty1() {
77              return property1;
78          }
79  
80          /**
81           * @@Required()
82           */
83          public void setProperty1(String property1) {
84              this.property1 = property1;
85          }
86  
87          public String getProperty2() {
88              return "";
89          }
90  
91          public void setProperty2(String property2) {
92          }
93      }
94  }