简体   繁体   English

java.util.Map的通用测试工具?

[英]Generic Test harness for java.util.Map?

I have a custom implementation of the Map interface which does some fancy stuff, like lazy evaluation of functions. 我有一个Map接口的自定义实现,它做了一些奇特的东西,比如懒惰的函数评估。 the implementation should appear immutable after construction from outside (eg no put() and putAll() methods are supported) 从外部构造后,实现应该是不可变的(例如,不支持put()和putAll()方法)

I it looks like it works in the most basic conditions. 我看起来它在最基本的条件下工作。 Since it is quite complex, i am sure there must be tons of lurking bugs wrt thread safety, irregular order of operations and much more.. 由于它非常复杂,我相信必须有大量潜在的错误,线程安全,不规则的操作顺序等等。

Since the contract of the Map interface is well-defined i am sure there must exist a generic test collection which checks corner-cases, thread safety etc. 由于Map接口的合约定义明确,我确信必须存在一个通用的测试集合,用于检查角落情况,线程安全等。

I have heard that Google Collections runs about 25000 unit tests for their library. 我听说Google Collections为他们的库运行了大约25000个单元测试。 Is it possible to download them somewhere? 可以在某处下载它们吗?

The Google Collections zip contains their tests. Google Collections zip包含他们的测试。 There should be a google-collect-testfw jar in there. 那里应该有一个google-collect-testfw jar。

Specifically, there's an abstract test for the general contract of Map. 具体来说,有一个Map的一般合同的抽象测试。

You might want to see if Google Collections has something that meets your needs so you don't have to support your own Map. 您可能想知道Google Collections是否有满足您需求的内容,因此您无需支持自己的地图。 See, for instance, MapMaker 例如,请参阅MapMaker

private Map<Key, Graph> createMap() {
  ConcurrentMap<Key, Graph> graphs = new MapMaker()
     .concurrencyLevel(32)
     .softKeys()
     .weakValues()
     .expiration(30, TimeUnit.MINUTES)
     .makeComputingMap(
        new Function<Key, Graph>() {
          public Graph apply(Key key) {
            return createExpensiveGraph(key);
          }
        });
  return Collections.unmodifiableMap(graphs);
}

Note that the map won't appear completely immutable after construction, since doing a map.get(key) with a previously-unseen key will change what is seen by Map.entrySet() 请注意,构造后映射不会显示为完全不可变,因为使用以前看不见的键执行map.get(key)将更改Map.entrySet()所看到的Map.entrySet()

If you need to write a custom Map implementation and want to good place to start for your tests, Adam's suggestion of using Google Collections's MapInterfaceTest is a good one. 如果您需要编写自定义Map实现并想要开始测试的好地方,Adam建议使用Google Collections的MapInterfaceTest是一个很好的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM