001 // Copyright 2004, 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.junit; 016 017 import java.io.InputStream; 018 import java.util.Properties; 019 020 import org.apache.tapestry.util.text.LocalizedProperties; 021 import org.testng.annotations.Test; 022 023 /** 024 * Tests to ensure that LocalizedProperties are fully backward compatible with java.util.Properties 025 * and that non-latin characters are read correctly. 026 * 027 * @author mb 028 * @since 4.0 029 */ 030 @Test 031 public class TestLocalizedProperties extends TapestryTestCase 032 { 033 private void ensureEquivalence(String fileName) 034 { 035 ensureEquivalence(fileName, fileName, "ISO-8859-1"); 036 } 037 038 private void ensureEquivalence(String fileName1, String fileName2, String encoding) 039 { 040 InputStream standardIns = getClass().getResourceAsStream(fileName1); 041 Properties standard = new Properties(); 042 Exception standardException = null; 043 try 044 { 045 standard.load(standardIns); 046 } 047 catch (Exception e) 048 { 049 standardException = e; 050 } 051 052 InputStream localizedIns = getClass().getResourceAsStream(fileName2); 053 Properties localized = new Properties(); 054 LocalizedProperties localizedProperties = new LocalizedProperties(localized); 055 Exception localizedException = null; 056 try 057 { 058 localizedProperties.load(localizedIns, encoding); 059 } 060 catch (Exception e) 061 { 062 localizedException = e; 063 } 064 065 if (standardException == null && localizedException == null) 066 assertEquals(standard, localized, "The property content does not match"); 067 else if (standardException == null && localizedException != null) 068 fail("Properties did not throw an exception, but LocalizedProperties did: " 069 + localizedException); 070 else if (standardException != null && localizedException == null) 071 fail("LocalizedProperties did not throw an exception, but Properties did: " 072 + localizedException); 073 // this test is disabled because in some cases Properties throws an incorrect exception 074 // probably due to a bug 075 //else if (standardException != null && localizedException != null) 076 // assertEquals("The exception types do not match", standardException.getClass(), 077 // localizedException.getClass()); 078 } 079 080 /** 081 * Test for the equivalence between Properties and LocalizedProperties for latin properties 082 */ 083 public void testEquivalence() 084 { 085 ensureEquivalence("StandardProperties.properties"); 086 ensureEquivalence("BadQuoting1.properties"); 087 ensureEquivalence("BadQuoting2.properties"); 088 } 089 090 /** 091 * Tests the reading of files using different encodings. Compare it with the reading of files 092 * that have gone through native2ascii and read using Properties. 093 */ 094 public void testEncodings() 095 { 096 ensureEquivalence("StandardUTFProperties.properties", "UTFProperties.properties", "utf-8"); 097 ensureEquivalence( 098 "StandardCyrillicProperties.properties", 099 "CyrillicProperties.properties", 100 "ISO-8859-5"); 101 } 102 }