1   /*
2    * ObjectDescriptorRegistryTest.java
3    * Created on 27 Декабрь 2005 г., 16:01
4    */
5   
6   package org.weda.property;
7   
8   import java.text.SimpleDateFormat;
9   import java.util.Date;
10  import java.util.Iterator;
11  import java.util.List;
12  import org.weda.test.WedaTestCase;
13  import org.weda.domain.Company;
14  import org.weda.property.TestObject.Sex;
15  import org.weda.property.impl.ObjectDescriptor;
16  
17  /**
18   *
19   * @author Mikhail Titov
20   */
21  public class ObjectDescriptorRegistryTest extends WedaTestCase {
22      private ObjectDescriptorRegistry reg;
23      
24      public ObjectDescriptorRegistryTest(String name) throws Exception {
25          super(name);
26      }
27      
28      public void setUp() throws Exception {
29          reg = (ObjectDescriptorRegistry)
30                      registry.getService(ObjectDescriptorRegistry.class);
31      }
32      
33      public void test_get() throws Exception {
34          ObjectDescriptor desc=reg.get(Company.class);
35          assertEquals(Company.class, desc.getObjectClass());
36          assertEquals("s/n", desc.getPropertyDescriptor("id").getDisplayName());
37          assertEquals("dsc", desc.getPropertyDescriptor("dsc").getDisplayName());
38          assertEquals(
39                  "Название компании"
40                  , desc.getPropertyDescriptor("name").getDisplayName());
41          assertEquals("java.lang.String",
42                  desc.getPropertyDescriptor("name").getPropertyClass().getName());
43      }
44      
45      public void test_getPropertyDescriptor() throws Exception {
46          PropertyDescriptor pd = reg.getPropertyDescriptor(Company.class, "name");
47          assertNotNull(pd);
48          assertEquals("Название компании", pd.getDisplayName());
49          assertEquals("java.lang.String", pd.getPropertyClass().getName());
50          
51          pd = reg.getPropertyDescriptor(Company.class, "address.street");
52          assertNotNull(pd);
53          assertEquals("street", pd.getName());
54          assertEquals("Улица", pd.getDisplayName());
55      }
56      
57      public void test_getReadWriteProperties() throws Exception {
58          ObjectDescriptor desc = reg.get(Company.class);
59          List<String> names = desc.getReadWriteProperties();        
60      }
61      
62      public void test_constraint() throws Exception {
63          PropertyDescriptor desc = 
64                  reg.getPropertyDescriptor(TestObject.class, "constaintValue");
65          assertNotNull(desc);
66          List<Constraint> constraints = desc.getConstraints();
67          assertNotNull(constraints);
68          assertEquals(2, constraints.size());
69          try{
70              desc.check(new Integer(3));
71          }catch(Exception e){
72              fail();
73          }
74          try{
75              desc.check(new Integer(0));
76              fail();
77          }catch(Exception e){
78          }
79          try{
80              desc.check(new Integer(1));
81              fail();
82          }catch(Exception e){
83          }
84          try{
85              desc.check(new Integer(4));
86              fail();
87          }catch(Exception e){
88          }
89      }
90      
91      public void test_enum_constraint() throws Exception {
92          PropertyDescriptor desc = 
93                  reg.getPropertyDescriptor(TestObject.class, "sex");
94          assertNotNull(desc);
95          List<Constraint> constraints = desc.getConstraints();
96          assertNotNull(constraints);
97          assertEquals(1, constraints.size());
98          SetConstraint constraint = (SetConstraint)constraints.get(0);
99          boolean containsMale = false;
100         boolean containsFamale = false;
101         Object maleConstant = null;
102         for (Iterator<SetValue> it=constraint.iterator(); it.hasNext();){
103             SetValue value = it.next();
104             if (   Sex.male.name()==value.getAlias()
105                 && Sex.male == value.getValue())
106             {
107                 containsMale = true;
108                 maleConstant = value.getValue();
109             }
110             if (   Sex.famale.name()==value.getAlias()
111                 && Sex.famale == value.getValue())
112             {
113                 containsFamale = true;
114             }
115         }
116         assertTrue(containsFamale);
117         assertTrue(containsMale);
118         //
119         PropertyValue propertyValue = 
120                 (PropertyValue)registry.getService(PropertyValue.class);
121         Integer setterId = propertyValue.compileSetter(TestObject.class, "sex");
122         TestObject obj = new TestObject();
123         propertyValue.setValue(obj, setterId, maleConstant);
124         assertEquals(Sex.male, obj.getSex());
125         //
126         constraint.check(maleConstant, null);
127     }
128 }