/** * Checks whether {@code method} is thread-safe, as indicated by the presence of the {@link * AllowConcurrentEvents} annotation. */ private static boolean isDeclaredThreadSafe(Method method) { return method.getAnnotation(AllowConcurrentEvents.class) != null; }
protected <A extends Annotation> CacheMethodDetails<A> create(Class<A> annotationType, Class<?> targetType, String methodName, Class<?>... parameterTypes) { Method method = ReflectionUtils.findMethod(targetType, methodName, parameterTypes); Assert.notNull(method, "requested method '" + methodName + "'does not exist"); A cacheAnnotation = method.getAnnotation(annotationType); return new DefaultCacheMethodDetails<>(method, cacheAnnotation, getCacheName(cacheAnnotation)); }
@Test public void supportsAllDefaultHandlerExceptionResolverExceptionTypes() throws Exception { Class<ResponseEntityExceptionHandler> clazz = ResponseEntityExceptionHandler.class; Method handleExceptionMethod = clazz.getMethod("handleException", Exception.class, WebRequest.class); ExceptionHandler annotation = handleExceptionMethod.getAnnotation(ExceptionHandler.class); List<Class<?>> exceptionTypes = Arrays.asList(annotation.value()); for (Method method : DefaultHandlerExceptionResolver.class.getDeclaredMethods()) { Class<?>[] paramTypes = method.getParameterTypes(); if (method.getName().startsWith("handle") && (paramTypes.length == 4)) { String name = paramTypes[0].getSimpleName(); assertTrue("@ExceptionHandler is missing " + name, exceptionTypes.contains(paramTypes[0])); } } }
@Test public void localConfigWithDefaults() throws Exception { Method method = getClass().getMethod("localConfigMethodWithDefaults"); SqlConfig localSqlConfig = method.getAnnotation(Sql.class).config(); MergedSqlConfig cfg = new MergedSqlConfig(localSqlConfig, getClass()); assertDefaults(cfg); }
@Test public void getAnnotationAttributesWithAttributeAliasesWithDifferentValues() throws Exception { exception.expect(AnnotationConfigurationException.class); exception.expectMessage(containsString("attribute 'value' and its alias 'path'")); exception.expectMessage(containsString("values of [{/enigma}] and [{/test}]")); Method method = WebController.class.getMethod("handleMappedWithDifferentPathAndValueAttributes"); WebMapping webMapping = method.getAnnotation(WebMapping.class); getAnnotationAttributes(webMapping); }
@Test public void findMethodAnnotationOnLeaf() throws Exception { Method m = Leaf.class.getMethod("annotatedOnLeaf"); assertNotNull(m.getAnnotation(Order.class)); assertNotNull(getAnnotation(m, Order.class)); assertNotNull(findAnnotation(m, Order.class)); }
@Test public void findMethodAnnotationWithMetaAnnotationOnLeaf() throws Exception { Method m = Leaf.class.getMethod("metaAnnotatedOnLeaf"); assertNull(m.getAnnotation(Order.class)); assertNotNull(getAnnotation(m, Order.class)); assertNotNull(findAnnotation(m, Order.class)); }
@Test public void findMethodAnnotationWithMetaMetaAnnotationOnLeaf() throws Exception { Method m = Leaf.class.getMethod("metaMetaAnnotatedOnLeaf"); assertNull(m.getAnnotation(Component.class)); assertNull(getAnnotation(m, Component.class)); assertNotNull(findAnnotation(m, Component.class)); }
@Test public void findMethodAnnotationWithMetaAnnotationOnRoot() throws Exception { Method m = Leaf.class.getMethod("metaAnnotatedOnRoot"); assertNull(m.getAnnotation(Order.class)); assertNotNull(getAnnotation(m, Order.class)); assertNotNull(findAnnotation(m, Order.class)); }
@Test public void findMethodAnnotationOnBridgedMethod() throws Exception { Method bridgedMethod = SimpleFoo.class.getMethod("something", String.class); assertFalse(bridgedMethod.isBridge()); assertNull(bridgedMethod.getAnnotation(Order.class)); assertNull(getAnnotation(bridgedMethod, Order.class)); assertNotNull(findAnnotation(bridgedMethod, Order.class)); assertNotNull(bridgedMethod.getAnnotation(Transactional.class)); assertNotNull(getAnnotation(bridgedMethod, Transactional.class)); assertNotNull(findAnnotation(bridgedMethod, Transactional.class)); }
@Test public void globalConfigWithDefaults() throws Exception { Method method = GlobalConfigWithDefaultsClass.class.getMethod("globalConfigMethod"); SqlConfig localSqlConfig = method.getAnnotation(Sql.class).config(); MergedSqlConfig cfg = new MergedSqlConfig(localSqlConfig, GlobalConfigWithDefaultsClass.class); assertDefaults(cfg); }
@Test public void localConfigWithContinueOnError() throws Exception { Method method = getClass().getMethod("localConfigMethodWithContinueOnError"); SqlConfig localSqlConfig = method.getAnnotation(Sql.class).config(); MergedSqlConfig cfg = new MergedSqlConfig(localSqlConfig, getClass()); assertNotNull(cfg); assertEquals("errorMode", CONTINUE_ON_ERROR, cfg.getErrorMode()); }
@Test public void findMethodAnnotationWithAnnotationOnMethodInInterface() throws Exception { Method m = Leaf.class.getMethod("fromInterfaceImplementedByRoot"); // @Order is not @Inherited assertNull(m.getAnnotation(Order.class)); // getAnnotation() does not search on interfaces assertNull(getAnnotation(m, Order.class)); // findAnnotation() does search on interfaces assertNotNull(findAnnotation(m, Order.class)); }
@Test public void findMethodAnnotationOnRoot() throws Exception { Method m = Leaf.class.getMethod("annotatedOnRoot"); assertNotNull(m.getAnnotation(Order.class)); assertNotNull(getAnnotation(m, Order.class)); assertNotNull(findAnnotation(m, Order.class)); }
@Test public void findMethodAnnotationOnRootButOverridden() throws Exception { Method m = Leaf.class.getMethod("overrideWithoutNewAnnotation"); assertNull(m.getAnnotation(Order.class)); assertNull(getAnnotation(m, Order.class)); assertNotNull(findAnnotation(m, Order.class)); }
@Test public void localConfigWithIgnoreFailedDrops() throws Exception { Method method = getClass().getMethod("localConfigMethodWithIgnoreFailedDrops"); SqlConfig localSqlConfig = method.getAnnotation(Sql.class).config(); MergedSqlConfig cfg = new MergedSqlConfig(localSqlConfig, getClass()); assertNotNull(cfg); assertEquals("errorMode", IGNORE_FAILED_DROPS, cfg.getErrorMode()); }
@Test public void toStringForSynthesizedAnnotations() throws Exception { Method methodWithPath = WebController.class.getMethod("handleMappedWithPathAttribute"); WebMapping webMappingWithAliases = methodWithPath.getAnnotation(WebMapping.class); assertNotNull(webMappingWithAliases); Method methodWithPathAndValue = WebController.class.getMethod("handleMappedWithSamePathAndValueAttributes"); WebMapping webMappingWithPathAndValue = methodWithPathAndValue.getAnnotation(WebMapping.class); assertNotNull(webMappingWithPathAndValue); WebMapping synthesizedWebMapping1 = synthesizeAnnotation(webMappingWithAliases); assertNotNull(synthesizedWebMapping1); WebMapping synthesizedWebMapping2 = synthesizeAnnotation(webMappingWithAliases); assertNotNull(synthesizedWebMapping2); assertThat(webMappingWithAliases.toString(), is(not(synthesizedWebMapping1.toString()))); assertToStringForWebMappingWithPathAndValue(synthesizedWebMapping1); assertToStringForWebMappingWithPathAndValue(synthesizedWebMapping2); }
protected DefaultCacheInvocationContext<?> createDummyContext() throws Exception { Method method = Sample.class.getMethod("get", String.class); CacheResult cacheAnnotation = method.getAnnotation(CacheResult.class); CacheMethodDetails<CacheResult> methodDetails = new DefaultCacheMethodDetails<>(method, cacheAnnotation, "test"); CacheResultOperation operation = new CacheResultOperation(methodDetails, defaultCacheResolver, defaultKeyGenerator, defaultExceptionCacheResolver); return new DefaultCacheInvocationContext<>(operation, new Sample(), new Object[] {"id"}); }
@Test public void synthesizeAlreadySynthesizedAnnotation() throws Exception { Method method = WebController.class.getMethod("handleMappedWithValueAttribute"); WebMapping webMapping = method.getAnnotation(WebMapping.class); assertNotNull(webMapping); WebMapping synthesizedWebMapping = synthesizeAnnotation(webMapping); assertNotSame(webMapping, synthesizedWebMapping); WebMapping synthesizedAgainWebMapping = synthesizeAnnotation(synthesizedWebMapping); assertThat(synthesizedAgainWebMapping, instanceOf(SynthesizedAnnotation.class)); assertSame(synthesizedWebMapping, synthesizedAgainWebMapping); assertEquals("name attribute: ", "foo", synthesizedAgainWebMapping.name()); assertArrayEquals("aliased path attribute: ", asArray("/test"), synthesizedAgainWebMapping.path()); assertArrayEquals("actual value attribute: ", asArray("/test"), synthesizedAgainWebMapping.value()); }
@Test public void localConfigWithCustomValues() throws Exception { Method method = getClass().getMethod("localConfigMethodWithCustomValues"); SqlConfig localSqlConfig = method.getAnnotation(Sql.class).config(); MergedSqlConfig cfg = new MergedSqlConfig(localSqlConfig, getClass()); assertNotNull(cfg); assertEquals("dataSource", "ds", cfg.getDataSource()); assertEquals("transactionManager", "txMgr", cfg.getTransactionManager()); assertEquals("transactionMode", ISOLATED, cfg.getTransactionMode()); assertEquals("encoding", "enigma", cfg.getEncoding()); assertEquals("separator", "\n", cfg.getSeparator()); assertEquals("commentPrefix", "`", cfg.getCommentPrefix()); assertEquals("blockCommentStartDelimiter", "<<", cfg.getBlockCommentStartDelimiter()); assertEquals("blockCommentEndDelimiter", ">>", cfg.getBlockCommentEndDelimiter()); assertEquals("errorMode", IGNORE_FAILED_DROPS, cfg.getErrorMode()); }