1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.logging.config; 19 20 21 import java.net.URL; 22 23 import junit.framework.Test; 24 import junit.framework.TestCase; 25 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.commons.logging.PathableClassLoader; 28 import org.apache.commons.logging.PathableTestSuite; 29 30 31 /** 32 * Tests that verify that the process of configuring logging on startup 33 * works correctly by selecting the file with the highest priority. 34 * <p> 35 * This test sets up a classpath where: 36 * <ul> 37 * <li> first file (in parent loader) has priority=10 (parentFirst=true) 38 * <li> second file found has no priority set 39 * <li> third file found has priority=20 40 * <li> fourth file found also has priority=20 41 * </ul> 42 * The result should be that the third file is used. 43 * <p> 44 * Note that parentFirst=true is used in this test because method 45 * <code>PathableClassLoader.getResources</code> always behaves as if 46 * parentFirst=true; see the PathableClassLoader javadoc for details. 47 */ 48 49 public class PriorityConfigTestCase extends TestCase { 50 51 // ------------------------------------------- JUnit Infrastructure Methods 52 53 54 /** 55 * Return the tests included in this test suite. 56 */ 57 public static Test suite() throws Exception { 58 Class thisClass = PriorityConfigTestCase.class; 59 60 // Determine the URL to this .class file, so that we can then 61 // append the priority dirs to it. For tidiness, load this 62 // class through a dummy loader though this is not absolutely 63 // necessary... 64 PathableClassLoader dummy = new PathableClassLoader(null); 65 dummy.useExplicitLoader("junit.", Test.class.getClassLoader()); 66 dummy.addLogicalLib("testclasses"); 67 dummy.addLogicalLib("commons-logging"); 68 69 String thisClassPath = thisClass.getName().replace('.', '/') + ".class"; 70 URL baseUrl = dummy.findResource(thisClassPath); 71 72 // Now set up the desired classloader hierarchy. We'll put a config 73 // file of priority=10 in the container path, and ones of both 74 // "no priority" and priority=20 in the webapp path. 75 // 76 // A second properties file with priority=20 is also added, 77 // so we can check that the first one in the classpath is 78 // used. 79 PathableClassLoader containerLoader = new PathableClassLoader(null); 80 containerLoader.useExplicitLoader("junit.", Test.class.getClassLoader()); 81 containerLoader.addLogicalLib("commons-logging"); 82 83 URL pri10URL = new URL(baseUrl, "priority10/"); 84 containerLoader.addURL(pri10URL); 85 86 PathableClassLoader webappLoader = new PathableClassLoader(containerLoader); 87 webappLoader.setParentFirst(true); 88 webappLoader.addLogicalLib("testclasses"); 89 90 URL noPriorityURL = new URL(baseUrl, "nopriority/"); 91 webappLoader.addURL(noPriorityURL); 92 93 URL pri20URL = new URL(baseUrl, "priority20/"); 94 webappLoader.addURL(pri20URL); 95 96 URL pri20aURL = new URL(baseUrl, "priority20a/"); 97 webappLoader.addURL(pri20aURL); 98 99 // load the test class via webapp loader, and use the webapp loader 100 // as the tccl loader too. 101 Class testClass = webappLoader.loadClass(thisClass.getName()); 102 return new PathableTestSuite(testClass, webappLoader); 103 } 104 105 /** 106 * Set up instance variables required by this test case. 107 */ 108 public void setUp() throws Exception { 109 LogFactory.releaseAll(); 110 } 111 112 /** 113 * Tear down instance variables required by this test case. 114 */ 115 public void tearDown() { 116 LogFactory.releaseAll(); 117 } 118 119 // ----------------------------------------------------------- Test Methods 120 121 /** 122 * Verify that the config file being used is the one containing 123 * the desired configId value. 124 */ 125 public void testPriority() throws Exception { 126 LogFactory instance = LogFactory.getFactory(); 127 String id = (String) instance.getAttribute("configId"); 128 assertEquals("Correct config file loaded", "priority20", id ); 129 } 130 }