1 /* ==================================================================== 2 * License: 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in 13 * the documentation and/or other materials provided with the 14 * distribution. 15 * 16 * 3. The end-user documentation included with the redistribution, 17 * if any, must include the following acknowledgment: 18 * "This product includes software developed by 19 * Robert Half International (http://www.rhi.com/)." 20 * Alternately, this acknowledgment may appear in the software itself, 21 * if and wherever such third-party acknowledgments normally appear. 22 * 23 * 4. The names "Parc", "RHI", and "Robert Half International" must 24 * not be used to endorse or promote products derived from this 25 * software without prior written permission. For written 26 * permission, please contact pete.mckinstry@rhi.com. 27 * 28 * 5. Products derived from this software may not be called "PARC", 29 * nor may "PARC" appear in their name, without prior written 30 * permission of Robert Half International. 31 * 32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 35 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 36 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 39 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 40 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 41 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 42 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 43 * SUCH DAMAGE. 44 * ==================================================================== 45 * 46 */ 47 package com.rhi.architecture.resource; 48 49 import junit.framework.*; 50 import java.util.*; 51 52 53 /*** 54 * ResourceParserTest.java 55 * 56 * @author <a href="mailto:pete_mckinstry@yahoo.com">Pete McKinstry</a> 57 * @copyright 2002, Robert Half International, All rights reserved. 58 * 59 * @version 1.0 60 */ 61 public class ResourceParserTest extends TestCase { 62 63 ResourceParserImpl parser = null; 64 65 /*** 66 * Constructor for ResourceParserTest. 67 * @param name 68 */ 69 public ResourceParserTest(String name) { 70 super(name); 71 } 72 73 /*** 74 * setup 75 * @throws Exception 76 */ 77 protected void setUp() throws Exception { 78 super.setUp(); 79 parser = new ResourceParserImpl(); 80 } 81 82 /*** 83 * tear down 84 * @throws Exception 85 */ 86 protected void tearDown() throws Exception { 87 super.tearDown(); 88 parser = null; 89 } 90 91 /*** 92 * test single resource. 93 * @throws Exception 94 */ 95 public void testSingleResource() throws Exception { 96 Properties p = new Properties(); 97 p.setProperty("Resource.0.LogFactory", 98 "org.mckinstry.logging.DefaultFactory"); 99 100 List resources = parser.getResources(p); 101 assertTrue(resources.size()==1); 102 ListIterator iter = resources.listIterator(); 103 ResourceEntry entry = (ResourceEntry)iter.next(); 104 assertTrue(entry.getPriority()==0); 105 assertTrue(entry.getKey().equals("Resource.LogFactory")); 106 assertTrue(entry.getType().equals("org.mckinstry.logging.DefaultFactory")); 107 } 108 109 /*** 110 * test multiple 111 * @throws Exception 112 */ 113 public void testMultipleResources() throws Exception { 114 Properties p = new Properties(); 115 p.setProperty("Resource.2.ConfigFacility", 116 "org.mckinstry.config.ConfigFacility"); 117 p.setProperty("Resource.1.DataSourceFactory", 118 "org.mckinstry.db.DataSourceFactory"); 119 p.setProperty("Resource.0.LogFactory", 120 "org.mckinstry.logging.DefaultFactory"); 121 122 List resources = parser.getResources(p); 123 assertTrue(resources.size()==3); 124 ListIterator iter = resources.listIterator(); 125 // i know there are 3 entries, so check order for each. 126 // 1) 127 ResourceEntry entry = (ResourceEntry)iter.next(); 128 assertTrue(entry.getPriority()==0); 129 assertTrue(entry.getKey().equals("Resource.LogFactory")); 130 assertTrue(entry.getType().equals("org.mckinstry.logging.DefaultFactory")); 131 // 2) 132 entry = (ResourceEntry)iter.next(); 133 assertTrue(entry.getPriority()==1); 134 assertTrue(entry.getKey().equals("Resource.DataSourceFactory")); 135 assertTrue(entry.getType().equals("org.mckinstry.db.DataSourceFactory")); 136 // 3) 137 entry = (ResourceEntry)iter.next(); 138 assertTrue(entry.getPriority()==2); 139 assertTrue(entry.getKey().equals("Resource.ConfigFacility")); 140 assertTrue(entry.getType().equals("org.mckinstry.config.ConfigFacility")); 141 142 } 143 144 /*** 145 * test old mode. 146 * @throws Exception 147 */ 148 public void testOldModel() throws Exception { 149 Properties p = new Properties(); 150 p.setProperty("Resource.LogFactory", 151 "org.mckinstry.logging.DefaultFactory"); 152 153 List resources = parser.getResources(p); 154 assertTrue(resources.size()==1); 155 ListIterator iter = resources.listIterator(); 156 ResourceEntry entry = (ResourceEntry)iter.next(); 157 assertTrue(entry.getPriority()==0); 158 assertTrue(entry.getKey().equals("Resource.LogFactory")); 159 assertTrue(entry.getType().equals("org.mckinstry.logging.DefaultFactory")); 160 } 161 162 /*** 163 * test old w/ multiple resources. 164 * @throws Exception 165 */ 166 public void testOldModelWithMultipleResources() throws Exception { 167 Properties p = new Properties(); 168 p.setProperty("Resource.LogFactory", 169 "org.mckinstry.logging.DefaultFactory"); 170 p.setProperty("Resource.DataSourceFactory", 171 "org.mckinstry.db.DataSourceFactory"); 172 173 List resources = parser.getResources(p); 174 assertTrue(resources.size()==2); 175 ListIterator iter = resources.listIterator(); 176 ResourceEntry entry = (ResourceEntry)iter.next(); 177 assertTrue(entry.getPriority()==0); 178 assertTrue(entry.getKey().equals("Resource.LogFactory") || 179 entry.getKey().equals("Resource.DataSourceFactory")); 180 entry = (ResourceEntry)iter.next(); 181 assertTrue(entry.getPriority()==0); 182 assertTrue(entry.getKey().equals("Resource.LogFactory") || 183 entry.getKey().equals("Resource.DataSourceFactory")); 184 } 185 186 /*** 187 * test non-resource entries. 188 * @throws Exception 189 */ 190 public void testNonResources() throws Exception { 191 Properties p = new Properties(); 192 p.setProperty("Random.Entry", "Configuration entry should be ignored."); 193 194 List resources = parser.getResources(p); 195 assertTrue(resources.size()==0); 196 } 197 198 /*** 199 * test 2 digit priorities for resources. 200 * @throws Exception 201 */ 202 public void testTwoDigitPriorities() throws Exception { 203 Properties p = new Properties(); 204 for (int i=0; i < 15; ++i) { 205 String key = "Resource." + i + "." + i; 206 p.setProperty(key, "com.rhi.architecture.resource.TestResource"); 207 } 208 209 List resources = parser.getResources(p); 210 assertTrue(resources.size()==15); 211 } 212 213 /*** 214 * print list. (debug) 215 * @param list 216 */ 217 public static void printList(List list) { 218 ListIterator iter = list.listIterator(); 219 int position=0; 220 while (iter.hasNext()) { 221 ResourceEntry entry = (ResourceEntry)iter.next(); 222 System.out.println("element["+ position + "].key= " + 223 entry.getKey()); 224 System.out.println("element["+ position + "].type= " + 225 entry.getType()); 226 System.out.println("element["+ position + "].priority= " + 227 entry.getPriority()); 228 ++position; 229 } 230 } 231 232 /*** 233 * log messages. 234 * @param msg 235 */ 236 public static void log( String msg ) { 237 System.out.println(msg); 238 } 239 240 241 /*** 242 * Suite method. 243 * @return Test 244 */ 245 public static Test suite() { 246 return new TestSuite( ResourceParserTest.class ); 247 } 248 249 /*** 250 * Convenience method 251 * @param args 252 */ 253 public static void main(String args[]) { 254 junit.textui.TestRunner.run(suite()); 255 } 256 257 }

This page was automatically generated by Maven