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.proxy.interceptor;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.proxy.factory.cglib.CglibProxyFactory;
22  import org.apache.commons.proxy.util.Echo;
23  import org.apache.commons.proxy.util.EchoImpl;
24  import org.jmock.Mock;
25  import org.jmock.MockObjectTestCase;
26  
27  import java.io.IOException;
28  
29  public class TestLoggingInterceptor extends MockObjectTestCase
30  {
31      private Mock logMock;
32      private Echo echo;
33  
34      protected void setUp() throws Exception
35      {
36          logMock = mock( Log.class );
37          echo = ( Echo ) new CglibProxyFactory()
38                  .createInterceptorProxy( new EchoImpl(), new LoggingInterceptor( ( Log ) logMock.proxy() ),
39                                           new Class[]{ Echo.class } );
40      }
41  
42      public void testWhenLoggingDisabled()
43      {
44          logMock = mock( Log.class );
45          echo = ( Echo ) new CglibProxyFactory()
46                  .createInterceptorProxy( new EchoImpl(), new LoggingInterceptor( ( Log ) logMock.proxy() ),
47                                           new Class[]{ Echo.class } );
48          logMock.expects( once() ).method( "isDebugEnabled" ).will( returnValue( false ) );
49          echo.echoBack( "Hello" );
50  
51      }
52  
53      public void testWithArrayParameter()
54      {
55          logMock.expects( once() ).method( "isDebugEnabled" ).will( returnValue( true ) );
56          logMock.expects( once() ).method( "debug" ).with( eq( "BEGIN echoBack((java.lang.String[]){Hello, World})" ) );
57          logMock.expects( once() ).method( "debug" ).with( eq( "END echoBack() [HelloWorld]" ) );
58          echo.echoBack( new String[] { "Hello", "World" } );
59      }
60  
61      public void testMultipleParameters()
62      {
63          logMock.expects( once() ).method( "isDebugEnabled" ).will( returnValue( true ) );
64          logMock.expects( once() ).method( "debug" ).with( eq( "BEGIN echoBack(Hello, World)" ) );
65          logMock.expects( once() ).method( "debug" ).with( eq( "END echoBack() [HelloWorld]" ) );
66          echo.echoBack( "Hello", "World" );
67      }
68  
69      public void testNullReturnValue()
70      {
71          logMock.expects( once() ).method( "isDebugEnabled" ).will( returnValue( true ) );
72          logMock.expects( once() ).method( "debug" ).with( eq( "BEGIN echoBack(<null>)" ) );
73          logMock.expects( once() ).method( "debug" ).with( eq( "END echoBack() [<null>]" ) );
74          echo.echoBack( ( String )null );
75      }
76  
77      public void testNonVoidMethod()
78      {
79          logMock.expects( once() ).method( "isDebugEnabled" ).will( returnValue( true ) );
80          logMock.expects( once() ).method( "debug" ).with( eq( "BEGIN echoBack(Hello)" ) );
81          logMock.expects( once() ).method( "debug" ).with( eq( "END echoBack() [Hello]" ) );
82          echo.echoBack( "Hello" );
83      }
84  
85      public void testException()
86      {
87          logMock.expects( once() ).method( "isDebugEnabled" ).will( returnValue( true ) );
88          logMock.expects( once() ).method( "debug" ).with( eq( "BEGIN ioException()" ) );
89          logMock.expects( once() ).method( "debug" ).with( eq( "EXCEPTION ioException() -- java.io.IOException" ), isA( IOException.class ) );
90          try
91          {
92              echo.ioException();
93              fail();
94          }
95          catch( IOException e )
96          {
97  
98          }
99      }
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 }