1   /*
2    * ObjectSourceSetConstraintTest.java
3    * Created on 7 Июль 2006 г., 14:48
4    */
5   
6   package org.weda.store;
7   
8   import java.util.ArrayList;
9   import java.util.Iterator;
10  import java.util.List;
11  import org.weda.test.WedaTestCase;
12  import org.weda.ObjectStoreTestCase;
13  import org.weda.domain.Employee;
14  import org.weda.domain.Post;
15  import org.weda.property.Constraint;
16  import org.weda.property.ConstraintsRegistry;
17  import org.weda.property.ObjectDescriptorRegistry;
18  import org.weda.property.PropertyDescriptor;
19  import org.weda.property.SetValue;
20  import org.weda.store.impl.ObjectSourceSetConstraint;
21  
22  /**
23   *
24   * @author Mikhail Titov
25   */
26  public class ObjectSourceSetConstraintTest extends ObjectStoreTestCase{
27      
28      public ObjectSourceSetConstraintTest(String name) throws Exception {
29          super(name);
30      }
31      
32      public void test_class() throws Exception {
33          ObjectSourceSetConstraint cons = new ObjectSourceSetConstraint();
34          cons.setId("test-constraint");
35          cons.setObjectSourceName("post");
36          cons.init();
37          check(cons);
38      }
39      
40      public void test_service() throws Exception {
41          ConstraintsRegistry consRegistry = 
42                  (ConstraintsRegistry)registry.getService(
43                      ConstraintsRegistry.class);
44          Constraint cons = consRegistry.getConstraint("post");
45          check(cons);
46      }
47      
48      public void test_property() throws Exception {
49          ObjectDescriptorRegistry objReg = 
50                  (ObjectDescriptorRegistry)
51                      registry.getService(ObjectDescriptorRegistry.class);
52          PropertyDescriptor pdesc = 
53                  objReg.getPropertyDescriptor(Employee.class, "post");
54          assertNotNull(pdesc);
55      }
56      
57      private void check(Constraint constraint) throws Exception {
58          assertNotNull(constraint);
59          assertTrue(constraint instanceof ObjectSourceSetConstraint);
60          ObjectSourceSetConstraint cons = (ObjectSourceSetConstraint)constraint;
61          Iterator<SetValue> it = cons.iterator();
62          assertNotNull(it);
63          assertEquals(0, createList(it).size());
64          //
65          Post post = new Post();
66          post.setName("test-post");
67          store.save(post);
68          //
69          post = new Post();
70          post.setName("test-post2");
71          store.save(post);
72          it = cons.iterator();
73          assertEquals(2, createList(it).size());
74      }
75      
76      private List<SetValue> createList(Iterator<SetValue> it){
77          List<SetValue> values = new ArrayList<SetValue>();
78          for (;it.hasNext();)
79              values.add(it.next());
80          return values;
81      }
82      
83  }