001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.monitoring.cdi; 018 019 import org.apache.commons.monitoring.Role; 020 import org.apache.commons.monitoring.counters.Counter; 021 import org.apache.commons.monitoring.repositories.Repository; 022 import org.apache.webbeans.cditest.CdiTestContainer; 023 import org.apache.webbeans.cditest.CdiTestContainerLoader; 024 import org.junit.Test; 025 026 import javax.enterprise.context.ApplicationScoped; 027 import javax.enterprise.inject.spi.BeanManager; 028 import java.util.concurrent.TimeUnit; 029 030 import static org.junit.Assert.assertEquals; 031 import static org.junit.Assert.assertNotNull; 032 033 public class CommonsMonitoringInterceptorTest { 034 @Test 035 public void checkMeasures() throws Exception { 036 final CdiTestContainer container = CdiTestContainerLoader.getCdiContainer(); 037 container.bootContainer(); 038 container.startApplicationScope(); 039 040 final BeanManager beanManager = container.getBeanManager(); 041 final MonitoredBean bean = MonitoredBean.class.cast(beanManager.getReference(beanManager.resolve(beanManager.getBeans(MonitoredBean.class)), MonitoredBean.class, null)); 042 043 bean.twoSeconds(); 044 045 container.stopApplicationScope(); 046 container.shutdownContainer(); 047 048 final Counter perf = Repository.INSTANCE.getCounter(new Counter.Key(Role.PERFORMANCES, MonitoredBean.class.getName() + ".twoSeconds")); 049 assertNotNull(perf); 050 assertEquals(2000, TimeUnit.NANOSECONDS.toMillis((int) perf.getMax()), 200); 051 } 052 053 @Monitored 054 @ApplicationScoped 055 public static class MonitoredBean { 056 public void twoSeconds() { 057 try { 058 Thread.sleep(2000); 059 } catch (final InterruptedException e) { 060 // no-op 061 } 062 } 063 } 064 }