View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.trinidad.bean;
20  
21  import org.apache.myfaces.trinidad.bean.FacesBean;
22  import org.apache.myfaces.trinidad.bean.PropertyKey;
23  
24  import junit.framework.Test;
25  import junit.framework.TestCase;
26  import junit.framework.TestSuite;
27  
28  public class TypeTest extends TestCase
29  {
30    public static final Test suite()
31    {
32      return new TestSuite(TypeTest.class);
33    }
34    
35    public static void main(String[] args) throws Throwable
36    {
37      junit.textui.TestRunner.run(suite());
38    }
39  
40    public TypeTest(
41      String testName)
42    {
43      super(testName);
44    }
45  
46    @Override
47    public void setUp()
48    {
49      _type = new FacesBean.Type();
50      _fooKey = _type.registerKey("foo",
51                                  PropertyKey.CAP_TRANSIENT);
52      _barKey = _type.registerKey("bar",
53                                  PropertyKey.CAP_LIST);
54  
55      _fooAliasKey = _type.registerAlias(_fooKey, "fooAlias");
56      
57      _subtype = new FacesBean.Type(_type);
58      _bazKey = _subtype.registerKey("baz");
59    }
60  
61    @Override
62    public void tearDown()
63    {
64      _type = null;
65    }
66  
67    public void testKeyEquality()
68    {
69      assertTrue(_fooKey.equals(_fooKey));
70      assertTrue(_barKey.equals(_barKey));
71      assertTrue(!_fooKey.equals(_barKey));
72      assertTrue(!_barKey.equals(_fooKey));
73      assertTrue(!_fooKey.equals(null));
74      assertTrue(!_fooKey.equals("foo"));
75      assertTrue(!"foo".equals(_fooKey));
76    }
77  
78    public void testKeyCapabilities()
79    {
80      assertTrue(_fooKey.isTransient());
81      assertTrue(_fooKey.getSupportsBinding());
82      assertTrue(!_fooKey.isList());
83  
84      assertTrue(!_barKey.isTransient());
85      assertTrue(!_barKey.getSupportsBinding());
86      assertTrue(_barKey.isList());
87    }
88  
89    public void testFindKeyByName()
90    {
91      PropertyKey foo = _type.findKey("foo");
92      assertSame(foo, _fooKey);
93      assertEquals("foo", foo.getName());
94      
95      PropertyKey bar = _type.findKey("bar");
96      assertSame(bar, _barKey);
97      assertEquals("bar", bar.getName());
98  
99      PropertyKey baz = _type.findKey("baz");
100     assertNull(baz);
101 
102     baz = _subtype.findKey("baz");
103     assertSame(baz, _bazKey);
104   }
105 
106   public void testFindKeyByIndex()
107   {
108     PropertyKey foo = _type.findKey(0);
109     assertSame(foo, _fooKey);
110     assertEquals(0, foo.getIndex());
111     
112     PropertyKey bar = _type.findKey(1);
113     assertSame(bar, _barKey);
114     assertEquals(1, bar.getIndex());
115 
116     PropertyKey baz = _type.findKey(2);
117     assertNull(baz);
118 
119     baz = _type.findKey(-1);
120     assertNull(baz);
121   }
122 
123   public void testTypeModifications()
124   {
125     PropertyKey newKey = _subtype.registerKey("new");
126     assertNotNull(newKey);
127 
128     assertEquals(3, newKey.getIndex());
129     assertEquals("new", newKey.getName());
130 
131     // Verify that re-registering a key fails
132     try
133     {
134       _subtype.registerKey("new");
135       fail();
136     }
137     catch (IllegalStateException ise)
138     {
139     }
140 
141     
142     // Verify that we didn't overwrite anything
143     assertSame(newKey, _subtype.findKey("new"));
144     assertSame(newKey, _subtype.findKey(3));
145     
146     // Lock the type
147     _subtype.lock();
148 
149     // And register a "reallyNew" key
150     try
151     {
152       _subtype.registerKey("reallyNew");
153       fail();
154     }
155     catch (IllegalStateException ise)
156     {
157     }
158 
159     // The key should not be be present
160     assertNull(_subtype.findKey("reallyNew"));
161     assertNull(_subtype.findKey(4));
162   }
163 
164   public void testAlias()
165   {
166     assertSame(_fooKey, _fooAliasKey);
167     assertSame(_fooKey, _type.findKey("fooAlias"));
168   }
169 
170   private FacesBean.Type _type;
171   private FacesBean.Type _subtype;
172   private PropertyKey  _fooKey;
173   private PropertyKey  _barKey;
174   private PropertyKey  _bazKey;
175   private PropertyKey  _fooAliasKey;
176 }