KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > xslt > FormatHelperTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.servlet.view.xslt;
18
19 import java.util.Locale JavaDoc;
20 import java.util.TimeZone JavaDoc;
21
22 import junit.framework.TestCase;
23 import org.w3c.dom.Element JavaDoc;
24
25 /**
26  * Test the FormatHelper methods.
27  *
28  * @author Rod Johnson
29  * @author Darren Davison
30  * @since 26.07.2003
31  */

32 public class FormatHelperTests extends TestCase {
33
34     private static final long testTime = 1064359582063L; //appx 00:26 on 24/9/2003 in the UK (GMT +1)
35

36     /**
37      * Test null params for Locale.
38      */

39     public void testNullParamsForLocale() {
40         try {
41             Element JavaDoc e = (Element JavaDoc) FormatHelper.dateTimeElement(testTime, null, null);
42             e = (Element JavaDoc) FormatHelper.dateTimeElement(testTime, "", null);
43             e = (Element JavaDoc) FormatHelper.dateTimeElement(testTime, null, "");
44         }
45         catch (Throwable JavaDoc ex) {
46             fail( "Passing null params to dateTimeElement(long, String, String) throws " + ex.getClass().getName());
47         }
48
49         try {
50             String JavaDoc s = FormatHelper.currency(50d, null, null);
51             s = FormatHelper.currency(50d, "", null);
52             s = FormatHelper.currency(50d, null, "");
53         }
54         catch (Throwable JavaDoc ex) {
55             fail( "Passing null params to currency(long, String, String) throws " + ex.getClass().getName());
56         }
57     }
58     
59     public void testDateTimeElement() {
60         // save the current default TZ
61
TimeZone JavaDoc curr = TimeZone.getDefault();
62         // force the right TZ to ensure parsed Date works
63
TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
64
65         Element JavaDoc e = (Element JavaDoc) FormatHelper.dateTimeElement(testTime, Locale.UK);
66         assertTrue(e.getTagName().equals("formatted-date"));
67         Element JavaDoc el = (Element JavaDoc) e.getElementsByTagName("year").item(0);
68         assertTrue("2003".equals(el.getFirstChild().getNodeValue()));
69         el = (Element JavaDoc) e.getElementsByTagName("month").item(0);
70         assertTrue("September".equals(el.getFirstChild().getNodeValue()));
71         el = (Element JavaDoc) e.getElementsByTagName("day-of-week").item(0);
72         assertTrue("Wednesday".equals(el.getFirstChild().getNodeValue()));
73         el = (Element JavaDoc) e.getElementsByTagName("day-of-month").item(0);
74         assertTrue("24".equals(el.getFirstChild().getNodeValue()));
75         el = (Element JavaDoc) e.getElementsByTagName("hours").item(0);
76         assertEquals( "12", el.getFirstChild().getNodeValue() );
77         el = (Element JavaDoc) e.getElementsByTagName("minutes").item(0);
78         assertTrue("26".equals(el.getFirstChild().getNodeValue()));
79         el = (Element JavaDoc) e.getElementsByTagName("am-pm").item(0);
80         assertTrue("AM".equals(el.getFirstChild().getNodeValue()));
81
82         // prove a different locale changes the output
83
e = (Element JavaDoc) FormatHelper.dateTimeElement(testTime, Locale.FRANCE);
84         el = (Element JavaDoc) e.getElementsByTagName("day-of-week").item(0);
85         assertTrue("mercredi".equals(el.getFirstChild().getNodeValue()));
86
87         // reset TZ in case later tests have a problem
88
TimeZone.setDefault(curr);
89     }
90     
91     public void testCurrency() {
92         String JavaDoc s = FormatHelper.currency( 50.0d, Locale.US);
93         assertTrue( "$50.00".equals(s));
94         
95         // pound sign (#163)
96
s = FormatHelper.currency( 50.0d, Locale.UK);
97         assertEquals( 163, s.charAt(0));
98         assertTrue( "50.00".equals(s.substring(1)));
99         
100         // comma replace dec.point
101
s = FormatHelper.currency( 50.0d, Locale.FRANCE);
102         assertTrue( "50,00".equals(s.substring(0,5)));
103         
104     }
105
106 }
107
Popular Tags