TimedInterface { @SuppressWarnings("unchecked") public static <T> T newProxy(Class<T> ctype, T concrete, String id) { final InvocationHandler handler = new TimedHandler<>(ctype, concrete, id); final Class<?>[] types = new Class[]{ctype, CompositeMonitor.class}; return (T) Proxy.newProxyInstance(ctype.getClassLoader(), types, handler); } private  TimedInterface(); @SuppressWarnings("unchecked") static T newProxy(Class<T> ctype, T concrete, String id); static T newProxy(Class<T> ctype, T concrete);  }
@SuppressWarnings("unchecked") @Test public void testTimedInterfaceNoId() { final IDummy dummy = TimedInterface.newProxy(IDummy.class, new DummyImpl()); DefaultMonitorRegistry.getInstance().register((CompositeMonitor) dummy); final CompositeMonitor<Long> compositeMonitor = (CompositeMonitor<Long>) dummy; final TagList tagList = BasicTagList.of( TimedInterface.CLASS_TAG, "DummyImpl", TimedInterface.INTERFACE_TAG, "IDummy"); final MonitorConfig expectedConfig = MonitorConfig.builder(TimedInterface.TIMED_INTERFACE) .withTags(tagList).build(); assertEquals(compositeMonitor.getConfig(), expectedConfig); }
@SuppressWarnings("unchecked") @Test public void testInterfaceInheritence() { final List<String> expectedNames = Arrays.asList("method1", "method2", "method3", "method4"); final IDummyExtended extendedDummy = TimedInterface.newProxy(IDummyExtended.class, new ExtendedDummy()); final CompositeMonitor<Long> compositeMonitor = (CompositeMonitor<Long>) extendedDummy; DefaultMonitorRegistry.getInstance().register(compositeMonitor); assertEquals(compositeMonitor.getMonitors().size(), 4); assertEquals(compositeMonitor.getValue().longValue(), 4L); assertEquals(compositeMonitor.getValue(100).longValue(), 4L); final List<Monitor<?>> monitors = compositeMonitor.getMonitors(); for (Monitor<?> monitor : monitors) { assertTrue(expectedNames.contains(monitor.getConfig().getName())); } }