1   /*
2    * ConstraintsFactoryTest.java
3    *
4    * Created on 30 Июнь 2006 г., 14:52
5    */
6   package org.weda.property;
7   
8   import java.text.SimpleDateFormat;
9   import java.util.ArrayList;
10  import java.util.Arrays;
11  import java.util.HashMap;
12  import java.util.HashSet;
13  import java.util.Iterator;
14  import java.util.List;
15  import java.util.Map;
16  import java.util.Set;
17  import org.weda.test.WedaTestCase;
18  import org.weda.property.impl.ConstraintInfo;
19  import org.weda.property.impl.NotNullConstraint;
20  
21  /**
22   *
23   * @author Mikhail Titov
24   */
25  public class ConstraintsFactoryTest extends WedaTestCase{
26      
27      public ConstraintsFactoryTest(String name) throws Exception {
28          super(name);
29      }
30      
31      public void test_config() throws Exception {
32          List<Constraint> constraints = 
33                  registry.getConfiguration("org.weda.property.Constraints");
34          assertNotNull(constraints);
35          assertTrue(constraints.size()>0);
36          Constraint constraint = 
37                  getConstraintById("test-values-set", constraints);
38          assertNotNull(constraint);
39      }
40      
41      public void test_service() throws Exception {
42          ConstraintsFactory fact = 
43                  (ConstraintsFactory)
44                      registry.getService(ConstraintsFactory.class);
45          assertNotNull(fact);
46          List<Constraint> constraints = fact.getConstraints();
47          assertNotNull(constraints);
48          assertTrue(constraints.size()>0);
49      }
50      
51      public void test_notNull() throws Exception {
52          Constraint constraint = getConstraint("notNull");
53          checkNotNullConstraint(constraint);
54      }
55      
56      public void test_valuesSet() throws Exception {        
57          Constraint constraint = getConstraint("test-values-set");
58          checkTestValuesSetConstraint(constraint);
59          SetConstraint setConstraint = (SetConstraint)constraint;
60          assertFalse(setConstraint.isValuesAliased());
61      }
62      
63      public void test_aliasedValuesSet() throws Exception {
64          Constraint constraint = getConstraint("test-aliased-values-set");
65          checkTestAliasedValuesSetConstraint(constraint);
66      }
67      
68      public void test_valuesSet_conversionPattern() throws Exception {
69          Constraint constraint = 
70                  getConstraint("test-values-set-conversion-pattern");
71          checkConversionPattern(constraint);
72      }
73      
74      public void test_boolean_constraint() throws Exception {
75          Constraint constraint = 
76                  getConstraint("boolean");        
77          checkBooleanConstraint(constraint);
78      }
79      
80      public void test_between_constraint() throws Exception {
81          Constraint constraint = 
82                  getConstraint("between");        
83          assertNotNull(constraint);
84          constraint.check(2., new Object[]{1., 3.});
85          constraint.check(2., new Object[]{2., 2.});
86          try{
87              constraint.check(2., new Object[]{2.1, 3.});
88              fail();
89          }catch(ConstraintException e){
90              log.debug(e.getMessage());
91          }
92      }
93      
94      private Constraint getConstraint(String id) throws Exception {
95          ConstraintsFactory fact = 
96                  (ConstraintsFactory)
97                      registry.getService(ConstraintsFactory.class);
98          Constraint constraint = getConstraintById(id, fact.getConstraints());
99          assertNotNull(constraint);
100         return constraint;
101     }
102     
103     public void checkBooleanConstraint(Constraint constraint)
104         throws Exception 
105     {
106         assertTrue(constraint instanceof SetConstraint);
107         SetConstraint setConstraint = (SetConstraint)constraint;
108         List<SetValue> setValues = new ArrayList<SetValue>();
109         for (Iterator<SetValue> it=setConstraint.iterator(); it.hasNext();)
110             setValues.add(it.next());
111         assertEquals(2, setValues.size());
112         constraint.check(true, null);
113         constraint.check(false, null);
114     }
115     
116     public void checkNotNullConstraint(Constraint constraint)
117         throws Exception
118     {
119         assertTrue(constraint instanceof NotNullConstraint);
120         try{
121             constraint.check(null, null);
122             fail();
123         }catch(ConstraintException e){            
124         }
125         try{
126             constraint.check("not null value", null);
127         }catch(ConstraintException e){            
128             fail();
129         }
130     }
131     
132     public void checkTestValuesSetConstraint(Constraint constraint)
133         throws Exception
134     {
135         assertTrue(constraint instanceof SetConstraint);
136         SetConstraint setConstraint = (SetConstraint)constraint;
137         //assertFalse(setConstraint.isValuesAliased());
138         List<SetValue> setValues = new ArrayList<SetValue>();
139         for (Iterator<SetValue> it=setConstraint.iterator(); it.hasNext();)
140             setValues.add(it.next());
141         assertEquals(3, setValues.size());
142         Object[] values = new Object[setValues.size()];
143         int i=0;
144         for (Iterator<SetValue> it=setConstraint.iterator(); it.hasNext();)
145             values[i++]=it.next().getValue();
146         assertTrue(Arrays.equals(new Integer[]{1, 2, 3}, values));
147         try{
148             setConstraint.check(new Integer(1), null);
149         }catch(Exception e){
150             fail();
151         }
152         try{
153             setConstraint.check(new Integer(0), null);
154             fail();
155         }catch(Exception e){            
156         }
157     }
158     
159     private void checkTestAliasedValuesSetConstraint(Constraint constraint)
160         throws Exception
161     {
162         checkTestValuesSetConstraint(constraint);
163         SetConstraint setConstraint = (SetConstraint)constraint;
164         assertTrue(setConstraint.isValuesAliased());
165         Map<String, Object> aliases = new HashMap<String, Object>();
166         for (Iterator<SetValue> it=setConstraint.iterator(); it.hasNext();){
167             SetValue setValue = it.next();
168             aliases.put(setValue.getAlias(), setValue.getValue());
169         }
170         assertEquals(new Integer(1), aliases.get("one"));
171         assertEquals(new Integer(2), aliases.get("two"));
172         assertEquals(new Integer(3), aliases.get("l18n_three"));        
173     }
174     
175     private void checkConversionPattern(Constraint constraint)
176         throws Exception
177     {
178         SetConstraint setConstraint = (SetConstraint)constraint;
179         Set values = new HashSet();
180         for (Iterator<SetValue> it=setConstraint.iterator(); it.hasNext();)
181             values.add(it.next().getValue());
182         SimpleDateFormat fmt = new SimpleDateFormat("dd.MM.yyyy");
183         assertTrue(values.contains(fmt.parse("01.01.2000")));
184         assertTrue(values.contains(fmt.parse("01.01.2001")));
185         //
186         try{
187             setConstraint.check(fmt.parse("01.01.2000"), null);
188         }catch(Exception e){
189             fail();
190         }
191         try{
192             setConstraint.check(fmt.parse("01.01.2002"), null);
193             fail();
194         }catch(Exception e){
195         }
196     }
197     
198     private ConstraintInfo getConstraintInfoById(
199             String id, List<ConstraintInfo> constraints)
200     {
201         for (ConstraintInfo constraint: constraints)
202             if (id.equals(constraint.getId()))
203                 return constraint;
204         return null;
205     }
206     
207     private Constraint getConstraintById(
208             String id, List constraints)
209     {
210         for (Object constraint: constraints)
211             if (   constraint instanceof Constraint 
212                 && id.equals(((Constraint)constraint).getId()))
213             {
214                 return (Constraint)constraint;
215             }
216         return null;
217     }
218 }