1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.monitoring.aop;
18
19 import org.apache.commons.monitoring.Role;
20 import org.apache.commons.monitoring.counters.Counter;
21 import org.apache.commons.monitoring.repositories.Repository;
22 import org.junit.Test;
23
24 import java.util.concurrent.TimeUnit;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.fail;
29
30 public class MonitoringProxyFactoryTest {
31 @Test
32 public void test() {
33 final Foo foo = MonitoringProxyFactory.monitor(Foo.class, new FooImpl());
34 foo.haveARest(2000);
35
36 final Counter perf = Repository.INSTANCE.getCounter(new Counter.Key(Role.PERFORMANCES, FooImpl.class.getName() + ".haveARest"));
37 assertNotNull(perf);
38 assertEquals(2000, TimeUnit.NANOSECONDS.toMillis((int) perf.getMax()), 200);
39
40 try {
41 foo.throwSthg();
42 } catch (final Exception e) {
43
44 }
45
46 Counter failures = null;
47 for (final Counter c : Repository.INSTANCE) {
48 if (c.getKey().getName().contains("UnsupportedOperationException")) {
49 if (failures != null) {
50 fail();
51 }
52 failures = c;
53 }
54 }
55
56 assertNotNull(failures);
57 assertEquals(1, failures.getHits());
58 }
59
60 public static interface Foo {
61 void haveARest(long ms);
62 void throwSthg();
63 }
64
65 public static class FooImpl implements Foo {
66 @Override
67 public void haveARest(long ms) {
68 try {
69 Thread.sleep(ms);
70 } catch (final InterruptedException e) {
71
72 }
73 }
74
75 public void throwSthg() {
76 throw new UnsupportedOperationException();
77 }
78 }
79 }