KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > tagutil > TaglibPropertiesTest


1 /**
2  * Nov 14, 2004
3  *
4  * Copyright 2004 uitags
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package net.sf.uitags.tagutil;
19
20 import java.util.Arrays JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import junit.framework.TestCase;
24 import net.sf.uitags.testutil.PrivilegedAccessor;
25
26 /**
27  * Tests {@link net.sf.uitags.tagutil.TaglibProperties}.
28  *
29  * @author jonni
30  * @version $Id: TaglibPropertiesTest.java,v 1.5 2006/08/16 12:45:13 hgani Exp $
31  */

32 public final class TaglibPropertiesTest extends TestCase {
33   /**
34    * Test fixture
35    */

36   private TaglibProperties props;
37
38   /**
39    * Main method for the tests.
40    *
41    * @param args main arguments
42    */

43   public static void main(String JavaDoc[] args) {
44     junit.textui.TestRunner.run(TaglibPropertiesTest.class);
45   }
46
47   /** {@inheritDoc} */
48   protected void setUp() throws Exception JavaDoc {
49     super.setUp();
50     this.props = TaglibProperties.getInstance();
51   }
52
53   /** {@inheritDoc} */
54   protected void tearDown() throws Exception JavaDoc {
55     super.tearDown();
56     this.props = null;
57   }
58
59   /**
60    * Tests {@link TaglibProperties#getInstance()} to make sure that
61    * <code>siteWideProps</code> is initialized once.
62    *
63    * @throws IllegalAccessException if reflection failed
64    * @throws NoSuchFieldException if reflection failed
65    */

66   public void testGetInstance()
67       throws IllegalAccessException JavaDoc, NoSuchFieldException JavaDoc {
68     TaglibProperties props1 = TaglibProperties.getInstance();
69     Properties JavaDoc siteWide1 =
70         (Properties JavaDoc) PrivilegedAccessor.getValue(props1, "mergedProps");
71
72     TaglibProperties props2 = TaglibProperties.getInstance();
73     Properties JavaDoc siteWide2 =
74         (Properties JavaDoc) PrivilegedAccessor.getValue(props2, "mergedProps");
75
76     assertSame(siteWide1, siteWide2);
77   }
78
79   /**
80    * Tests using {@link TaglibProperties#setRuntimeProperty(String, String)}
81    * and {@link TaglibProperties#get(String)} together to make
82    * sure runtime properties are set and retrieved correctly.
83    */

84   public void testSetAndGetStringProperty() {
85     String JavaDoc testValue = "dummy test value";
86     this.props.setRuntimeProperty("testKey", testValue);
87     String JavaDoc value = this.props.get("testKey");
88     assertEquals(testValue, value);
89
90     // null value should not override previous value
91
this.props.setRuntimeProperty("testKey", (String JavaDoc) null);
92     assertEquals(testValue, this.props.get("testKey"));
93   }
94
95   /**
96    * Tests {@link TaglibProperties#get(String)} to make
97    * sure returned value is never <code>null</code>.
98    */

99   public void testGetAsNonNullString() {
100     String JavaDoc nonExistantKey = "This is so ridiculous it can never be a key";
101     try {
102       this.props.get(nonExistantKey);
103       fail("Searching for a non-existant key should throw an exception");
104     }
105     catch (IllegalStateException JavaDoc e) {
106       // This is expected.
107
}
108   }
109
110   /**
111    * Tests using {@link TaglibProperties#setRuntimeProperty(String, String[])}
112    * and {@link TaglibProperties#getAsArray(String)} together to make
113    * sure runtime properties are set and retrieved correctly.
114    */

115   public void testSetAndGetStringArrayProperty() {
116     String JavaDoc[] testValues = new String JavaDoc[] { "dummy", "test", "values" };
117     this.props.setRuntimeProperty(
118         "calendar.listMonths.monthLabels", testValues);
119     assertEquals("dummy,test,values",
120         this.props.get("calendar.listMonths.monthLabels"));
121     assertEquals(Arrays.asList(testValues), Arrays.asList(
122         this.props.getAsArray("calendar.listMonths.monthLabels")));
123
124     // Null value should not override previous value
125
this.props.setRuntimeProperty(
126         "calendar.listMonths.monthLabels", (String JavaDoc[]) null);
127     assertEquals(Arrays.asList(testValues), Arrays.asList(
128         this.props.getAsArray("calendar.listMonths.monthLabels")));
129   }
130
131   /**
132    * Tests {@link TaglibProperties#getAsArray(String)} to make
133    * sure returned string is never <code>null</code>. Instead an empty array
134    * should be returned.
135    */

136   public void testGetAsEmptyStringArray() {
137     String JavaDoc nonExistantKey = "This is so ridiculous it can never be a key";
138     try {
139       this.props.getAsArray(nonExistantKey);
140       fail("Searching for a non-existant key should throw an exception");
141     }
142     catch (IllegalStateException JavaDoc e) {
143       // This is expected.
144
}
145   }
146 }
Popular Tags