1   package org.mod4j.runtime.aspects;
2   
3   import java.util.logging.Level;
4   import java.util.logging.LogRecord;
5   
6   import org.apache.commons.logging.Log;
7   import org.apache.commons.logging.LogFactory;
8   import org.aspectj.lang.JoinPoint;
9   import org.aspectj.lang.Signature;
10  import org.easymock.Capture;
11  import org.easymock.classextension.EasyMock;
12  import org.junit.Before;
13  import org.junit.Test;
14  import org.mod4j.runtime.test.AbstractLoggingTest;
15  
16  import static org.junit.Assert.assertThat;
17  
18  import static org.easymock.EasyMock.capture;
19  import static org.easymock.EasyMock.createMock;
20  import static org.easymock.EasyMock.expect;
21  import static org.easymock.EasyMock.replay;
22  import static org.easymock.EasyMock.verify;
23  
24  import static org.hamcrest.CoreMatchers.is;
25  
26  /**
27   * Test for {@link LoggingAspect}.
28   */
29  public class LoggingAspectTest extends AbstractLoggingTest {
30  
31      /** Used to return as method name */
32      private static final String DUMMY_METHOD_NAME = "dummy";
33      private LoggingAspect loggingAspect;
34      private JoinPoint call;
35  
36      /**
37       * {@inheritDoc}
38       */
39      @Override
40      @Before
41      public void setUp() {
42          loggingAspect = new LoggingAspect();
43          call = createMock(JoinPoint.class);
44  
45          super.setUp();
46      }
47  
48      /**
49       * Test the {@link LoggingAspect#logAfter(JoinPoint)} method with loglevel set to 'all'.
50       */
51      @Test
52      public void testLogAfter() {
53          // Return this class as target. That means this classname will be written to the log.
54          expect(call.getTarget()).andReturn(this);
55          final Signature signature = expectGetName(call);
56  
57          // This should be the 'exiting' log message
58          final Capture<LogRecord> capturedLogRecord = new Capture<LogRecord>();
59          mockHandler.publish(capture(capturedLogRecord));
60  
61          EasyMock.replay(mockHandler);
62          replay(call, signature);
63          loggingAspect.logAfter(call);
64          EasyMock.verify(mockHandler);
65          verify(call, signature);
66  
67          // Validate the written logline
68          final String expectedMessage = "Exiting method [" + DUMMY_METHOD_NAME + "]";
69          assertThat(capturedLogRecord.getValue().getMessage(), is(expectedMessage));
70      }
71  
72      /**
73       * Test the {@link LoggingAspect#logBefore(JoinPoint)} method with loglevel set to 'all'.
74       * 
75       * @throws Throwable Not expected.
76       */
77      @Test
78      public void testLogBefore() throws Throwable {
79          // Return this class as target. That means this classname will be written to the log.
80          expect(call.getTarget()).andReturn(this);
81          final Signature signature = expectGetName(call);
82  
83          // This should be the 'entering' log message
84          final Capture<LogRecord> capturedLogRecord = new Capture<LogRecord>();
85          mockHandler.publish(capture(capturedLogRecord));
86  
87          EasyMock.replay(mockHandler);
88          replay(call, signature);
89          loggingAspect.logBefore(call);
90          EasyMock.verify(mockHandler);
91          verify(call, signature);
92  
93          // Validate the written logline
94          final String expectedMessage = "Entering method [" + DUMMY_METHOD_NAME + "]";
95          assertThat(capturedLogRecord.getValue().getMessage(), is(expectedMessage));
96      }
97  
98      /**
99       * Test the {@link LoggingAspect#logBefore(JoinPoint)} AND {@link LoggingAspect#logAfter(JoinPoint)} methods with
100      * loglevel set to 'info', which should in nothing.
101      */
102     @Test
103     public void testNoLogging() {
104         // Override the loglevel: set it do info (which is not fine enough to log trace level).
105         logger.setLevel(Level.INFO);
106 
107         // Two times, since we're testing both logBefore and logAfter
108         expect(call.getTarget()).andReturn(this).times(2);
109 
110         EasyMock.replay(mockHandler);
111         replay(call);
112         loggingAspect.logBefore(call);
113         loggingAspect.logAfter(call);
114         EasyMock.verify(mockHandler);
115         verify(call);
116     }
117 
118     /**
119      * @return The {@link Log} for the target (which is this class).
120      */
121     @Override
122     protected Log getLog() {
123         return LogFactory.getLog(this.getClass());
124     }
125 
126     /**
127      * Expect the steps to get the method name from the {@link JoinPoint}.
128      * 
129      * @param call The {@link JoinPoint} of which the {@link Signature} is retrieved.
130      * @return The {@link Signature} with set behaviour, calling method should do replay / verify.
131      */
132     private static Signature expectGetName(final JoinPoint call) {
133         final Signature signature = createMock(Signature.class);
134         expect(call.getSignature()).andReturn(signature);
135         expect(signature.getName()).andReturn(DUMMY_METHOD_NAME);
136         return signature;
137     }
138 
139 }