001 /* 002 * $Id: TestResources.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.HashMap; 028 import java.util.Iterator; 029 import java.util.List; 030 import java.util.Locale; 031 import java.util.Map; 032 033 import org.apache.commons.resources.ResourcesException; 034 import org.apache.commons.resources.ResourcesKeyException; 035 036 /** 037 * <p>Concrete implementation of {@link org.apache.commons.resources.Resources} 038 * for unit tests.</p> 039 */ 040 public class TestResources extends ResourcesBase { 041 042 043 // ----------------------------------------------------------- Constructors 044 045 046 // Construct a test instance with the specified name 047 public TestResources(String name) { 048 super(name); 049 } 050 051 052 // ----------------------------------------------------- Instance Variables 053 054 055 // Has this instance been initialized? 056 protected boolean initialized = false; 057 058 059 // Map of resource String values 060 protected Map map = new HashMap(); 061 062 063 // ------------------------------------------------------ Lifecycle Methods 064 065 066 // Initialize this Resources instance 067 public void init() throws ResourcesException { 068 069 super.init(); 070 map.put("test.specific", "[test] SPECIFIC"); 071 map.put("test.inherit", "[test] INHERIT"); 072 initialized = true; 073 074 } 075 076 077 // Destroy this Resources instance 078 public void destroy() throws ResourcesException { 079 080 super.destroy(); 081 map.clear(); 082 initialized = false; 083 084 } 085 086 087 // ------------------------------------------------------------- Properties 088 089 090 // Return the keys for all defined Resources 091 public Iterator getKeys() { 092 093 List results = new ArrayList(); 094 results.add("test.base"); 095 results.add("test.specific"); 096 results.add("test.inherit"); 097 results.add("test.message"); 098 results.add("test.message.single"); 099 return (results.iterator()); 100 101 } 102 103 104 // Has this instance been initialized? 105 public boolean isInitialized() { 106 return (this.initialized); 107 } 108 109 110 // ---------------------------------------------- Content Retrieval Methods 111 112 113 /** 114 * <p>Return the content for the specified <code>key</code> as an 115 * Object, localized based on the specified <code>locale</code>. 116 * </p> 117 * 118 * @param key Identifier for the requested content 119 * @param locale Locale with which to localize retrieval, 120 * or <code>null</code> for the default Locale 121 * 122 * @exception ResourcesException if an error occurs retrieving or 123 * returning the requested content 124 */ 125 public Object getObject(String key){ 126 return getObject(key, null); 127 } 128 public Object getObject(String key, Locale locale) 129 throws ResourcesException { 130 131 Object value = map.get(key); 132 if (value == null) { 133 if (isReturnNull()) { 134 return (null); 135 } else { 136 throw new ResourcesKeyException(key); 137 } 138 } 139 return (value); 140 141 } 142 143 144 }