001 /* 002 * $Id: ResourcesBaseTestCase.java 354330 2005-12-06 06:05:19Z niallp $ 003 * $Revision: 354330 $ 004 * $Date: 2005-12-06 06:05:19 +0000 (Tue, 06 Dec 2005) $ 005 * 006 * ==================================================================== 007 * 008 * Copyright 2003-2005 The Apache Software Foundation 009 * 010 * Licensed under the Apache License, Version 2.0 (the "License"); 011 * you may not use this file except in compliance with the License. 012 * You may obtain a copy of the License at 013 * 014 * http://www.apache.org/licenses/LICENSE-2.0 015 * 016 * Unless required by applicable law or agreed to in writing, software 017 * distributed under the License is distributed on an "AS IS" BASIS, 018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 019 * See the License for the specific language governing permissions and 020 * limitations under the License. 021 * 022 */ 023 024 package org.apache.commons.resources.impl; 025 026 import java.util.ArrayList; 027 import java.util.Iterator; 028 import java.util.List; 029 import java.util.Locale; 030 031 import junit.framework.Test; 032 import junit.framework.TestSuite; 033 034 import org.apache.commons.resources.Resources; 035 import org.apache.commons.resources.ResourcesException; 036 import org.apache.commons.resources.ResourcesKeyException; 037 038 /** 039 * <p>Unit tests for 040 * <code>org.apache.commons.resources.impl.ResourcesBase</code>. 041 * </p> 042 */ 043 public class ResourcesBaseTestCase extends ResourcesFactoryBaseTestCase { 044 045 046 // ----------------------------------------------------- Instance Variables 047 048 049 // The Resources instance to be tested 050 protected Resources resources = null; 051 052 053 // ----------------------------------------------------------- Constructors 054 055 056 public ResourcesBaseTestCase(String name) { 057 super(name); 058 } 059 060 061 // --------------------------------------------------- Overall Test Methods 062 063 064 // Set up instance variables required by this test case 065 public void setUp() throws Exception { 066 super.setUp(); 067 resources = factory.getResources(NAME); 068 } 069 070 // Return the tests included in this test suite 071 public static Test suite() { 072 return (new TestSuite(ResourcesBaseTestCase.class)); 073 } 074 075 // Tear down the instance variables required by this test case 076 public void tearDown() { 077 resources = null; 078 super.tearDown(); 079 } 080 081 082 // ------------------------------------------------ Individual Test Methods 083 084 085 // Test retrieving Strings that should be inherited from parent Locales 086 public void testInherit() throws Exception { 087 088 Locale locale = null; 089 090 locale = new Locale("en", "US"); 091 assertEquals("en_US inherit value", 092 "[test] INHERIT", 093 resources.getString("test.inherit", locale)); 094 095 locale = new Locale("fr", "FR"); 096 assertEquals("fr_FR inherit value", 097 "[test] INHERIT", 098 resources.getString("test.inherit", locale)); 099 100 } 101 102 103 // Test retrieving all of the defined keys 104 public void testKeys() throws Exception { 105 106 // Pre-seed all of the keys we expect to find 107 Locale locale = new Locale("en", "US"); 108 resources.getString("test.base", locale); 109 resources.getString("test.specific", locale); 110 resources.getString("test.inherit", locale); 111 resources.getString("test.message", locale); 112 113 // Accumulate all of the keys that are actually returned 114 List results = new ArrayList(); 115 Iterator keys = resources.getKeys(); 116 while (keys.hasNext()) { 117 results.add(keys.next()); 118 } 119 120 // Validate the keys that were actually returned 121 assertEquals(5, results.size()); 122 assertTrue(results.contains("test.base")); 123 assertTrue(results.contains("test.specific")); 124 assertTrue(results.contains("test.inherit")); 125 assertTrue(results.contains("test.message")); 126 127 } 128 129 130 // Test retrieving missing resource keys 131 public void testMissing() throws Exception { 132 133 Locale locale = new Locale("en",""); 134 try { 135 resources.setReturnNull(false); 136 resources.getString("test.missing", locale); 137 fail("Should have thrown ResourcesException"); 138 } catch (ResourcesKeyException e) { 139 // Expected result 140 } 141 try { 142 resources.setReturnNull(true); 143 String value = resources.getString("test.missing", locale); 144 assertTrue("Should have returned null", value == null); 145 } catch (ResourcesException e) { 146 fail("Should have returned null"); 147 } 148 149 } 150 151 152 // Test the characteristics of a newly created instance 153 public void testPristine() { 154 assertNotNull(resources); 155 assertEquals("Correct name", NAME, resources.getName()); 156 if (resources instanceof TestResources) { 157 assertTrue("Resources initialized", 158 ((TestResources) resources).isInitialized()); 159 } 160 } 161 162 163 // Test that releasing should destroy instances 164 public void testRelease() throws Exception { 165 factory.release(); 166 if (resources instanceof TestResources) { 167 assertTrue("Resources destroyed", 168 !((TestResources) resources).isInitialized()); 169 } 170 } 171 172 173 // Test retrieving Locale-specific Strings explicitly 174 public void testSpecific() throws Exception { 175 176 Locale locale = null; 177 178 locale = new Locale("en",""); 179 assertEquals("en specific value", 180 "[test] SPECIFIC", 181 resources.getString("test.specific", locale)); 182 183 locale = new Locale("en", "US"); 184 assertEquals("en_US specific value", 185 "[test] SPECIFIC", 186 resources.getString("test.specific", locale)); 187 188 locale = new Locale("fr",""); 189 assertEquals("fr specific value", 190 "[test] SPECIFIC", 191 resources.getString("test.specific", locale)); 192 193 } 194 195 196 }