问题描述
好吧,我相信我不会以最有效的方式去处理这个问题,而且我正在寻找一些有关如何更有效地做这些的帮助。- config.txt文件包含键/值对,其中key = test和value的名称=是否执行测试
- 通过配置文件解析并创建一个运行测试列表
- 运行这些测试
m目前正在进行这个
- 通过传递给一个帮助函数parseConfig,通过我的配置文件的BufferedReader创建一个ArrayList。 parseConfig返回一个TreeSet,我在构造方法中使用我的ArrayList
- parseConfig遍历配置文件中的文本行。如果值指示执行测试,则将测试名称添加到TreeSet。返回TreeSet。
- 使用增强型for循环迭代ArrayList。如果key.equals(thisTest),则执行thisTest,否则,如果key.equals(thatTest),执行theTest ... etc
这是我真的不喜欢的最后一部分。它工作得很好,但似乎很笨拙,效率低下。由于我的ArrayList是使用TreeSet构造的,所以它按照排序顺序排列。我想使用一个更优雅和确定性的方法将我的键映射到要执行的测试。任何人都可以帮助我?
答案是创建一个 HashMap< String,Method> / code>对象。
Okay, I'm sure that I'm not going about this in the most efficient way and I'm looking for some help regarding how to do this more efficiently...
- config.txt file contains key/value pairs, where key = name of test and value = whether to execute test
- parse through config file and create a list of tests to run
- run those tests
Here is how I'm currently going about this
- create an ArrayList by passing to a helper function, parseConfig, a BufferedReader over my config file. parseConfig returns a TreeSet , which I use in the constructor method for my ArrayList
- parseConfig iterates over lines of text in config file. If value indicates to perform test, add name of test to TreeSet. Return TreeSet.
- Iterate over ArrayList with enhanced for loop. Body of enhanced for loop is basically a long if/else statement...if key.equals ("thisTest"), perform thisTest, else if key.equals (thatTest), perform thatTest...etc
It's that last part that I really don't like. It works well enough, but it seems clumsy and inefficient. Since my ArrayList is constructed using a TreeSet, it is in sorted order. I would like to use a more elegant and deterministic method for mapping my keys to tests to perform. Can anyone help me?
The answer was to create a HashMap <String, Method>
object.
这篇关于Java:从配置文件&基于价值观有效地进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!