TimedAnnotationNameStrategy implements StatementNameStrategy { @Override public String getStatementName(StatementContext statementContext) { final ExtensionMethod extensionMethod = statementContext.getExtensionMethod(); if (extensionMethod == null) { return null; } final Class<?> clazz = extensionMethod.getType(); final Timed classTimed = clazz.getAnnotation(Timed.class); final Method method = extensionMethod.getMethod(); final Timed methodTimed = method.getAnnotation(Timed.class); if (methodTimed != null) { String methodName = methodTimed.name().isEmpty() ? method.getName() : methodTimed.name(); if (methodTimed.absolute()) { return methodName; } else { return classTimed == null || classTimed.name().isEmpty() ? MetricRegistry.name(clazz, methodName) : MetricRegistry.name(classTimed.name(), methodName); } } else if (classTimed != null) { return classTimed.name().isEmpty() ? MetricRegistry.name(clazz, method.getName()) : MetricRegistry.name(classTimed.name(), method.getName()); } else { return null; } }  @Override String getStatementName(StatementContext statementContext);  }
@Test public void testAnnotationOnMethod() throws Exception { when(ctx.getExtensionMethod()).thenReturn(new ExtensionMethod(Foo.class, Foo.class.getMethod("update"))); assertThat(timedAnnotationNameStrategy.getStatementName(ctx)) .isEqualTo("com.codahale.metrics.jdbi3.strategies.TimedAnnotationNameStrategyTest$Foo.update"); }
@Test public void testAnnotationOnMethodWithCustomName() throws Exception { when(ctx.getExtensionMethod()).thenReturn(new ExtensionMethod(Foo.class, Foo.class.getMethod("customUpdate"))); assertThat(timedAnnotationNameStrategy.getStatementName(ctx)) .isEqualTo("com.codahale.metrics.jdbi3.strategies.TimedAnnotationNameStrategyTest$Foo.custom-update"); }
@Test public void testAnnotationOnMethodWithCustomAbsoluteName() throws Exception { when(ctx.getExtensionMethod()).thenReturn(new ExtensionMethod(Foo.class, Foo.class.getMethod("absoluteUpdate"))); assertThat(timedAnnotationNameStrategy.getStatementName(ctx)).isEqualTo("absolute-update"); }
@Test public void testAnnotationOnClass() throws Exception { when(ctx.getExtensionMethod()).thenReturn(new ExtensionMethod(Bar.class, Bar.class.getMethod("update"))); assertThat(timedAnnotationNameStrategy.getStatementName(ctx)) .isEqualTo("com.codahale.metrics.jdbi3.strategies.TimedAnnotationNameStrategyTest$Bar.update"); }
@Test public void testAnnotationOnMethodAndClassWithCustomNames() throws Exception { when(ctx.getExtensionMethod()).thenReturn(new ExtensionMethod(CustomBar.class, CustomBar.class.getMethod("find", String.class))); assertThat(timedAnnotationNameStrategy.getStatementName(ctx)).isEqualTo("custom-bar.find-by-id"); }
@Test public void testNoAnnotations() throws Exception { when(ctx.getExtensionMethod()).thenReturn(new ExtensionMethod(Dummy.class, Dummy.class.getMethod("show"))); assertThat(timedAnnotationNameStrategy.getStatementName(ctx)).isNull(); }
@Test public void testNoMethod() { assertThat(timedAnnotationNameStrategy.getStatementName(ctx)).isNull(); }