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 package org.apache.commons.inject.impl; 018 019 import java.util.ArrayList; 020 import java.util.List; 021 022 import org.apache.commons.inject.api.CommonsInject; 023 import org.apache.commons.inject.api.IInjector; 024 import org.apache.commons.inject.api.ILifecycleController; 025 import org.apache.commons.inject.api.Log4jLoggerModule; 026 import org.apache.commons.inject.api.PostConstructModule; 027 import org.apache.commons.inject.api.bind.IBinder; 028 import org.apache.commons.inject.api.bind.IModule; 029 import org.apache.commons.inject.api.bind.Scopes; 030 import org.apache.log4j.AppenderSkeleton; 031 import org.apache.log4j.BasicConfigurator; 032 import org.apache.log4j.Level; 033 import org.apache.log4j.Logger; 034 import org.apache.log4j.spi.LoggingEvent; 035 import org.junit.Assert; 036 import org.junit.Test; 037 038 public class Log4jLoggerModuleTest { 039 private final List<String> messages = new ArrayList<String>(); 040 private void configureLog4j() { 041 BasicConfigurator.configure(); 042 final Logger root = Logger.getRootLogger(); 043 root.removeAllAppenders(); 044 root.addAppender(new AppenderSkeleton(true) { 045 046 @Override 047 public boolean requiresLayout() { 048 return false; 049 } 050 051 @Override 052 public void close() { 053 // Does nothing. 054 } 055 056 @Override 057 protected void append(LoggingEvent pEvent) { 058 final Level level = pEvent.getLevel(); 059 messages.add(level.toString() + "; " + pEvent.getMessage()); 060 } 061 }); 062 } 063 064 @Test 065 public void testLoggerModule() { 066 configureLog4j(); 067 final PostConstructModule module0 = new PostConstructModule(); 068 final IModule module1 = new Log4jLoggerModule(); 069 final IModule module2 = new IModule(){ 070 @Override 071 public void configure(IBinder pBinder) { 072 pBinder.bind(InitializableObject.class).scope(Scopes.PER_CALL); 073 } 074 }; 075 final ILifecycleController controller = module0.getLifecycleController(); 076 final IInjector injector = CommonsInject.build(module0, module1, module2); 077 final InitializableObject io = injector.getInstance(InitializableObject.class); 078 controller.start(); 079 io.run(); 080 controller.shutdown(); 081 Assert.assertEquals(5, messages.size()); 082 Assert.assertEquals("DEBUG; start: ->", messages.get(0)); 083 Assert.assertEquals("DEBUG; start: <-", messages.get(1)); 084 Assert.assertEquals("DEBUG; run: Running", messages.get(2)); 085 Assert.assertEquals("DEBUG; shutdown: ->", messages.get(3)); 086 Assert.assertEquals("DEBUG; shutdown: <-", messages.get(4)); 087 } 088 }