KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > junit > TestLocalizedProperties


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

15 package org.apache.tapestry.junit;
16
17 import java.io.InputStream JavaDoc;
18 import java.util.Properties JavaDoc;
19
20 import org.apache.tapestry.util.text.LocalizedProperties;
21
22 /**
23  * Tests to ensure that LocalizedProperties are fully backward compatible with java.util.Properties
24  * and that non-latin characters are read correctly.
25  *
26  * @author mb
27  * @since 4.0
28  */

29 public class TestLocalizedProperties extends TapestryTestCase
30 {
31     private void ensureEquivalence(String JavaDoc fileName)
32     {
33         ensureEquivalence(fileName, fileName, "ISO-8859-1");
34     }
35
36     private void ensureEquivalence(String JavaDoc fileName1, String JavaDoc fileName2, String JavaDoc encoding)
37     {
38         InputStream JavaDoc standardIns = getClass().getResourceAsStream(fileName1);
39         Properties JavaDoc standard = new Properties JavaDoc();
40         Exception JavaDoc standardException = null;
41         try
42         {
43             standard.load(standardIns);
44         }
45         catch (Exception JavaDoc e)
46         {
47             standardException = e;
48         }
49
50         InputStream JavaDoc localizedIns = getClass().getResourceAsStream(fileName2);
51         Properties JavaDoc localized = new Properties JavaDoc();
52         LocalizedProperties localizedProperties = new LocalizedProperties(localized);
53         Exception JavaDoc localizedException = null;
54         try
55         {
56             localizedProperties.load(localizedIns, encoding);
57         }
58         catch (Exception JavaDoc e)
59         {
60             localizedException = e;
61         }
62
63         if (standardException == null && localizedException == null)
64             assertEquals("The property content does not match", standard, localized);
65         else if (standardException == null && localizedException != null)
66             fail("Properties did not throw an exception, but LocalizedProperties did: "
67                     + localizedException);
68         else if (standardException != null && localizedException == null)
69             fail("LocalizedProperties did not throw an exception, but Properties did: "
70                     + localizedException);
71         // this test is disabled because in some cases Properties throws an incorrect exception
72
// probably due to a bug
73
//else if (standardException != null && localizedException != null)
74
// assertEquals("The exception types do not match", standardException.getClass(),
75
// localizedException.getClass());
76
}
77
78     /**
79      * Test for the equivalence between Properties and LocalizedProperties for latin properties
80      */

81     public void testEquivalence()
82     {
83         ensureEquivalence("StandardProperties.properties");
84         ensureEquivalence("BadQuoting1.properties");
85         ensureEquivalence("BadQuoting2.properties");
86     }
87
88     /**
89      * Tests the reading of files using different encodings. Compare it with the reading of files
90      * that have gone through native2ascii and read using Properties.
91      */

92     public void testEncodings()
93     {
94         ensureEquivalence("StandardUTFProperties.properties", "UTFProperties.properties", "utf-8");
95         ensureEquivalence(
96                 "StandardCyrillicProperties.properties",
97                 "CyrillicProperties.properties",
98                 "ISO-8859-5");
99     }
100 }
101
Popular Tags