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.proxy.interceptor; 019 020 import org.apache.commons.logging.Log; 021 import org.apache.commons.proxy.factory.cglib.CglibProxyFactory; 022 import org.apache.commons.proxy.util.Echo; 023 import org.apache.commons.proxy.util.EchoImpl; 024 import org.jmock.Mock; 025 import org.jmock.MockObjectTestCase; 026 027 import java.io.IOException; 028 029 public class TestLoggingInterceptor extends MockObjectTestCase 030 { 031 private Mock logMock; 032 private Echo echo; 033 034 protected void setUp() throws Exception 035 { 036 logMock = mock( Log.class ); 037 echo = ( Echo ) new CglibProxyFactory() 038 .createInterceptorProxy( new EchoImpl(), new LoggingInterceptor( ( Log ) logMock.proxy() ), 039 new Class[]{ Echo.class } ); 040 } 041 042 public void testWhenLoggingDisabled() 043 { 044 logMock = mock( Log.class ); 045 echo = ( Echo ) new CglibProxyFactory() 046 .createInterceptorProxy( new EchoImpl(), new LoggingInterceptor( ( Log ) logMock.proxy() ), 047 new Class[]{ Echo.class } ); 048 logMock.expects( once() ).method( "isDebugEnabled" ).will( returnValue( false ) ); 049 echo.echoBack( "Hello" ); 050 051 } 052 053 public void testWithArrayParameter() 054 { 055 logMock.expects( once() ).method( "isDebugEnabled" ).will( returnValue( true ) ); 056 logMock.expects( once() ).method( "debug" ).with( eq( "BEGIN echoBack((java.lang.String[]){Hello, World})" ) ); 057 logMock.expects( once() ).method( "debug" ).with( eq( "END echoBack() [HelloWorld]" ) ); 058 echo.echoBack( new String[] { "Hello", "World" } ); 059 } 060 061 public void testMultipleParameters() 062 { 063 logMock.expects( once() ).method( "isDebugEnabled" ).will( returnValue( true ) ); 064 logMock.expects( once() ).method( "debug" ).with( eq( "BEGIN echoBack(Hello, World)" ) ); 065 logMock.expects( once() ).method( "debug" ).with( eq( "END echoBack() [HelloWorld]" ) ); 066 echo.echoBack( "Hello", "World" ); 067 } 068 069 public void testNullReturnValue() 070 { 071 logMock.expects( once() ).method( "isDebugEnabled" ).will( returnValue( true ) ); 072 logMock.expects( once() ).method( "debug" ).with( eq( "BEGIN echoBack(<null>)" ) ); 073 logMock.expects( once() ).method( "debug" ).with( eq( "END echoBack() [<null>]" ) ); 074 echo.echoBack( ( String )null ); 075 } 076 077 public void testNonVoidMethod() 078 { 079 logMock.expects( once() ).method( "isDebugEnabled" ).will( returnValue( true ) ); 080 logMock.expects( once() ).method( "debug" ).with( eq( "BEGIN echoBack(Hello)" ) ); 081 logMock.expects( once() ).method( "debug" ).with( eq( "END echoBack() [Hello]" ) ); 082 echo.echoBack( "Hello" ); 083 } 084 085 public void testException() 086 { 087 logMock.expects( once() ).method( "isDebugEnabled" ).will( returnValue( true ) ); 088 logMock.expects( once() ).method( "debug" ).with( eq( "BEGIN ioException()" ) ); 089 logMock.expects( once() ).method( "debug" ).with( eq( "EXCEPTION ioException() -- java.io.IOException" ), isA( IOException.class ) ); 090 try 091 { 092 echo.ioException(); 093 fail(); 094 } 095 catch( IOException e ) 096 { 097 098 } 099 } 100 101 public void testRuntimeException() 102 { 103 logMock.expects( once() ).method( "isDebugEnabled" ).will( returnValue( true ) ); 104 logMock.expects( once() ).method( "debug" ).with( eq( "BEGIN illegalArgument()" ) ); 105 logMock.expects( once() ).method( "debug" ).with( eq( "EXCEPTION illegalArgument() -- java.lang.IllegalArgumentException" ), isA( IllegalArgumentException.class ) ); 106 try 107 { 108 echo.illegalArgument(); 109 fail(); 110 } 111 catch( IllegalArgumentException e ) 112 { 113 114 } 115 } 116 117 public void testVoidMethod() 118 { 119 logMock.expects( once() ).method( "isDebugEnabled" ).will( returnValue( true ) ); 120 logMock.expects( once() ).method( "debug" ).with( eq( "BEGIN echo()" ) ); 121 logMock.expects( once() ).method( "debug" ).with( eq( "END echo()" ) ); 122 echo.echo(); 123 } 124 125 }