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.PropertyKey;
22  
23  import junit.framework.Test;
24  import junit.framework.TestCase;
25  import junit.framework.TestSuite;
26  
27  /**
28   * Tests the PropertyKey class.
29   */
30  public class PropertyKeyTest extends TestCase
31  {
32    /**
33     * Returns a test suite of all PropertyKey tests.
34     */
35    public static final Test suite()
36    {
37      return new TestSuite(PropertyKeyTest.class);
38    }
39    
40  
41    /**
42     * Tests that property keys with primitive types have the corresponding
43     * Java Language Specification default value (boxed).
44     */
45    public void testPrimitiveDefaults()
46    {
47      PropertyKey key;
48      
49      key = new PropertyKey("boolean", boolean.class);
50      assertSame(Boolean.FALSE, key.getDefault());
51      key = new PropertyKey("byte", byte.class);
52      assertEquals(new Byte((byte)0), key.getDefault());
53      key = new PropertyKey("char", char.class);
54      assertEquals(new Character('\0'), key.getDefault());
55      key = new PropertyKey("double", double.class);
56      assertEquals(new Double(0.0), key.getDefault());
57      key = new PropertyKey("float", float.class);
58      assertEquals(new Float(0.0f), key.getDefault());
59      key = new PropertyKey("int", int.class);
60      assertEquals(new Integer(0), key.getDefault());
61      key = new PropertyKey("long", long.class);
62      assertEquals(new Long(0L), key.getDefault());
63      key = new PropertyKey("short", short.class);
64      assertEquals(new Short((short)0), key.getDefault());
65    }
66  
67    /**
68     * Tests that property keys with boxed primitive types still have null
69     * as the default, rather than the boxed primitive default.
70     */
71    public void testBoxedPrimitiveDefaults()
72    {
73      PropertyKey key;
74      
75      key = new PropertyKey("Boolean", Boolean.class);
76      assertNull(key.getDefault());
77      key = new PropertyKey("Byte", Byte.class);
78      assertNull(key.getDefault());
79      key = new PropertyKey("Character", Character.class);
80      assertNull(key.getDefault());
81      key = new PropertyKey("Double", Double.class);
82      assertNull(key.getDefault());
83      key = new PropertyKey("Float", Float.class);
84      assertNull(key.getDefault());
85      key = new PropertyKey("Integer", Integer.class);
86      assertNull(key.getDefault());
87      key = new PropertyKey("Long", Long.class);
88      assertNull(key.getDefault());
89      key = new PropertyKey("Short", Short.class);
90      assertNull(key.getDefault());
91    }
92  
93    public void testDefaultSameType()
94    {
95      new PropertyKey("String", String.class, "default");
96    }
97  
98    public void testDefaultSubType()
99    {
100     new PropertyKey("Number", Number.class, new Integer(101));
101   }
102 
103   public void testDefaultWrongType()
104   {
105     try
106     {
107       new PropertyKey("Long", Long.class, new Integer(101));
108       fail();
109     }
110     catch (IllegalStateException e)
111     {
112       // expected
113     }
114   }
115 }