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.ProceedingJoinPoint;
9 import org.aspectj.lang.Signature;
10 import org.easymock.Capture;
11 import org.easymock.classextension.EasyMock;
12 import org.junit.Test;
13 import org.junit.matchers.JUnitMatchers;
14 import org.mod4j.runtime.test.AbstractLoggingTest;
15
16 import static org.junit.Assert.assertThat;
17 import static org.junit.Assert.fail;
18
19 import static org.easymock.EasyMock.capture;
20 import static org.easymock.EasyMock.createMock;
21 import static org.easymock.EasyMock.expect;
22 import static org.easymock.EasyMock.replay;
23 import static org.easymock.EasyMock.verify;
24
25
26
27
28 public class TimingAspectTest extends AbstractLoggingTest {
29
30
31 private static final String DUMMY_METHOD_NAME = "dummy";
32
33
34
35
36
37
38 @Test
39 public void testTiming() throws Throwable {
40
41 final TimingAspect timingAspect = new TimingAspect();
42
43
44
45 final ProceedingJoinPoint call = createMock(ProceedingJoinPoint.class);
46
47 expect(call.getTarget()).andReturn(this);
48 final Signature signature = createMock(Signature.class);
49 expect(call.getSignature()).andReturn(signature);
50 expect(signature.getName()).andReturn(DUMMY_METHOD_NAME);
51
52 expect(call.proceed()).andThrow(new IllegalArgumentException());
53
54
55 final Capture<LogRecord> capturedLogRecord = new Capture<LogRecord>();
56 mockHandler.publish(capture(capturedLogRecord));
57
58 EasyMock.replay(mockHandler);
59 replay(call, signature);
60 try {
61 timingAspect.time(call);
62 fail("Should have thrown an exception here");
63 } catch (final IllegalArgumentException illegalArgumentException) {
64
65 }
66 EasyMock.verify(mockHandler);
67 verify(call, signature);
68
69
70
71 final String expectedMessagePrefix = "Execution of method [" + DUMMY_METHOD_NAME + "] in";
72 assertThat(capturedLogRecord.getValue().getMessage(), JUnitMatchers.containsString(expectedMessagePrefix));
73 }
74
75
76
77
78
79
80
81 @Test
82 public void testNoLogging() throws Throwable {
83
84 final TimingAspect timingAspect = new TimingAspect();
85
86
87 logger.setLevel(Level.INFO);
88
89
90 final ProceedingJoinPoint call = createMock(ProceedingJoinPoint.class);
91 expect(call.getTarget()).andReturn(this);
92 expect(call.proceed()).andReturn(null);
93
94 EasyMock.replay(mockHandler);
95 replay(call);
96 timingAspect.time(call);
97 EasyMock.verify(mockHandler);
98 verify(call);
99 }
100
101
102
103
104 @Override
105 protected Log getLog() {
106 return LogFactory.getLog(this.getClass());
107 }
108
109 }