CacheKeyMethodStoreImpl implements CacheKeyMethodStore { @Override public Method getKeyMethod(final Class<?> keyClass) throws NoSuchMethodException { final Method storedMethod = find(keyClass); if (storedMethod != null) { return storedMethod; } Method targetMethod = getFromClass(keyClass); if (targetMethod == null) { Class<?> superKeyClass = keyClass.getSuperclass(); if (superKeyClass != null) { targetMethod = getKeyMethod(superKeyClass); } if (targetMethod == null || DEFAULT_KEY_METHOD_NAME.equals(targetMethod.getName())) { targetMethod = keyClass.getMethod(DEFAULT_KEY_METHOD_NAME, (Class<?>[]) null); } } add(keyClass, targetMethod); return targetMethod; }  @Override Method getKeyMethod(final Class<?> keyClass); static final String DEFAULT_KEY_METHOD_NAME; }
@Test public void testInheritance() throws NoSuchMethodException { Method cacheKeyMethod = cacheKeyMethodStoreImpl.getKeyMethod(AClass.class); assertEquals("getAKey", cacheKeyMethod.getName()); cacheKeyMethod = cacheKeyMethodStoreImpl.getKeyMethod(BClass.class); assertEquals("getBKey", cacheKeyMethod.getName()); cacheKeyMethod = cacheKeyMethodStoreImpl.getKeyMethod(CClass.class); assertEquals("getCKey", cacheKeyMethod.getName()); cacheKeyMethod = cacheKeyMethodStoreImpl.getKeyMethod(DClass.class); assertEquals("getBKey", cacheKeyMethod.getName()); }
@Test public void testToStringOwerwrite() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Method cacheKeyMethod = cacheKeyMethodStoreImpl.getKeyMethod(YClass.class); assertEquals("toString", cacheKeyMethod.getName()); Object result = cacheKeyMethod.invoke(new YClass()); assertEquals("Y", result.toString()); cacheKeyMethod = cacheKeyMethodStoreImpl.getKeyMethod(XClass.class); assertEquals("toString", cacheKeyMethod.getName()); result = cacheKeyMethod.invoke(new XClass()); assertEquals("X", result.toString()); }
@Test(expected = InvalidAnnotationException.class) public void invalidTwoCacheKeyMethods() throws NoSuchMethodException { cacheKeyMethodStoreImpl.getKeyMethod(TwoCacheKeyMethodsModel.class); }
@Test(expected = InvalidAnnotationException.class) public void invalidMethodSignature() throws NoSuchMethodException { cacheKeyMethodStoreImpl.getKeyMethod(InvalidCacheKeyMethodSignatureModel.class); }
@Test(expected = InvalidAnnotationException.class) public void invalidMethodSignature2() throws NoSuchMethodException { cacheKeyMethodStoreImpl.getKeyMethod(InvalidCacheKeyMethodSignatureModel2.class); }
@Test public void testNoInheritance() throws NoSuchMethodException { Method cacheKeyMethod = cacheKeyMethodStoreImpl.getKeyMethod(Object.class); assertNotNull(cacheKeyMethod); assertEquals("toString", cacheKeyMethod.getName()); cacheKeyMethod = cacheKeyMethodStoreImpl.getKeyMethod(Point.class); assertNotNull(cacheKeyMethod); assertEquals("getKey", cacheKeyMethod.getName()); }