View Javadoc

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  package org.apache.commons.monitoring.cdi;
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.apache.webbeans.cditest.CdiTestContainer;
23  import org.apache.webbeans.cditest.CdiTestContainerLoader;
24  import org.junit.Test;
25  
26  import javax.enterprise.context.ApplicationScoped;
27  import javax.enterprise.inject.spi.BeanManager;
28  import java.util.concurrent.TimeUnit;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertNotNull;
32  
33  public class CommonsMonitoringInterceptorTest {
34      @Test
35      public void checkMeasures() throws Exception {
36          final CdiTestContainer container = CdiTestContainerLoader.getCdiContainer();
37          container.bootContainer();
38          container.startApplicationScope();
39  
40          final BeanManager beanManager = container.getBeanManager();
41          final MonitoredBean bean = MonitoredBean.class.cast(beanManager.getReference(beanManager.resolve(beanManager.getBeans(MonitoredBean.class)), MonitoredBean.class, null));
42  
43          bean.twoSeconds();
44  
45          container.stopApplicationScope();
46          container.shutdownContainer();
47  
48          final Counter perf = Repository.INSTANCE.getCounter(new Counter.Key(Role.PERFORMANCES, MonitoredBean.class.getName() + ".twoSeconds"));
49          assertNotNull(perf);
50          assertEquals(2000, TimeUnit.NANOSECONDS.toMillis((int) perf.getMax()), 200);
51      }
52  
53      @Monitored
54      @ApplicationScoped
55      public static class MonitoredBean {
56          public void twoSeconds() {
57              try {
58                  Thread.sleep(2000);
59              } catch (final InterruptedException e) {
60                  // no-op
61              }
62          }
63      }
64  }