001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.logging.config; 019 020 021 import java.net.URL; 022 023 import junit.framework.Test; 024 import junit.framework.TestCase; 025 026 import org.apache.commons.logging.LogFactory; 027 import org.apache.commons.logging.PathableClassLoader; 028 import org.apache.commons.logging.PathableTestSuite; 029 030 031 /** 032 * Tests that verify that the process of configuring logging on startup 033 * works correctly by selecting the file with the highest priority. 034 * <p> 035 * This test sets up a classpath where: 036 * <ul> 037 * <li> first file (in parent loader) has priority=10 (parentFirst=true) 038 * <li> second file found has no priority set 039 * <li> third file found has priority=20 040 * <li> fourth file found also has priority=20 041 * </ul> 042 * The result should be that the third file is used. 043 * <p> 044 * Note that parentFirst=true is used in this test because method 045 * <code>PathableClassLoader.getResources</code> always behaves as if 046 * parentFirst=true; see the PathableClassLoader javadoc for details. 047 */ 048 049 public class PriorityConfigTestCase extends TestCase { 050 051 // ------------------------------------------- JUnit Infrastructure Methods 052 053 054 /** 055 * Return the tests included in this test suite. 056 */ 057 public static Test suite() throws Exception { 058 Class thisClass = PriorityConfigTestCase.class; 059 060 // Determine the URL to this .class file, so that we can then 061 // append the priority dirs to it. For tidiness, load this 062 // class through a dummy loader though this is not absolutely 063 // necessary... 064 PathableClassLoader dummy = new PathableClassLoader(null); 065 dummy.useExplicitLoader("junit.", Test.class.getClassLoader()); 066 dummy.addLogicalLib("testclasses"); 067 dummy.addLogicalLib("commons-logging"); 068 069 String thisClassPath = thisClass.getName().replace('.', '/') + ".class"; 070 URL baseUrl = dummy.findResource(thisClassPath); 071 072 // Now set up the desired classloader hierarchy. We'll put a config 073 // file of priority=10 in the container path, and ones of both 074 // "no priority" and priority=20 in the webapp path. 075 // 076 // A second properties file with priority=20 is also added, 077 // so we can check that the first one in the classpath is 078 // used. 079 PathableClassLoader containerLoader = new PathableClassLoader(null); 080 containerLoader.useExplicitLoader("junit.", Test.class.getClassLoader()); 081 containerLoader.addLogicalLib("commons-logging"); 082 083 URL pri10URL = new URL(baseUrl, "priority10/"); 084 containerLoader.addURL(pri10URL); 085 086 PathableClassLoader webappLoader = new PathableClassLoader(containerLoader); 087 webappLoader.setParentFirst(true); 088 webappLoader.addLogicalLib("testclasses"); 089 090 URL noPriorityURL = new URL(baseUrl, "nopriority/"); 091 webappLoader.addURL(noPriorityURL); 092 093 URL pri20URL = new URL(baseUrl, "priority20/"); 094 webappLoader.addURL(pri20URL); 095 096 URL pri20aURL = new URL(baseUrl, "priority20a/"); 097 webappLoader.addURL(pri20aURL); 098 099 // 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 }