• 测试技术
  • 博客
  • 视频
  • 开源
  • 论坛
  • 沙龙
  • 下载
  • 杂志
  • 招聘

字号: | 推荐给好友 上一篇 | 下一篇

Mock Objects:缺点和用例

发布: 2008-6-13 18:23 | 作者: 不详 | 来源: 领测软件测试网编辑整理 | 查看: 163次 | 进入领测软件测试网论坛讨论

领测软件测试网

'I^*hr#T$i-F
public final class IgnoreExceptionsCacheManagerDecorator implements CacheManager {
 
  private static final Object NULL = new Object();
  private static Logger logger = Logger.getAnonymousLogger(); 
 
  private final CacheManager decorated;
  
  public IgnoreExceptionsCacheManagerDecorator(CacheManager decorated) {
    this.decorated = decorated;
  }
  
  public Object getFromCache(String key) {
    try {
      return decorated.getFromCache(key);
    } catch (Exception e) {
      logger.log(SEVERE, "Unable to retrieve an object using key \"" + key + "\"", e);            
    }
    return NULL;
  }
 
  public void putInCache(String key, Object o) {
    try {
      decorated.putInCache(key, o);
    } catch (Exception e) {
      logger.log(SEVERE, "Unable to store the object " + o + " using key \"" + key + "\"", e);            
    }
  }
}
  为了避免缓存系统中的任何错误导致产品中某些应用程序停止运行,我们仅需要使用IgnoreExceptionsCacheManagerDecorator :

X)V$uM8o:H9|  CacheManager cacheManager = new IgnoreExceptionsCacheManagerDecorator(new DistributedCacheManager());

K