编写 TestSuite 类
由于我们构造了自己的 TestCase, TestSuite 常用的组织 TestCase 的方法需要做一点小小的改动。在我们的 TestSuite 中,提供了一个方法“ addTestCase ”。这个方法可以将 TestCase 添加到 TestSuite 中。参见清单 6。
清单 6. addTestCase method
1 protected void addTestCase(IRateable testObject){
2 Test tp = null;
3 try{
4 // 获得 SettersMapFactory
5 Class factoryClass = Class.forName(factory);
6 SettersMapFactory settersMapFactory=
7 (SettersMapFactory)factoryClass.newInstance();
8
9 // 从 SettersMapFactory 获得 SettersMap
10 File file = null;
11 file = getSetterResourceFile(testObject,settersMapFactory);
12 SettersMap settersMap = settersMapFactory.createSettersMap(file);
13
14 // 创建 TestCase
15 tp = new AttributeRatingTestCase(testObject,settersMap);
16 }catch (Exception e){
17 e.printStackTrace();
18 }
19 // 添加 TestCase 到 TestSuite;
20 this.addTest(tp);
21 }
2 Test tp = null;
3 try{
4 // 获得 SettersMapFactory
5 Class factoryClass = Class.forName(factory);
6 SettersMapFactory settersMapFactory=
7 (SettersMapFactory)factoryClass.newInstance();
8
9 // 从 SettersMapFactory 获得 SettersMap
10 File file = null;
11 file = getSetterResourceFile(testObject,settersMapFactory);
12 SettersMap settersMap = settersMapFactory.createSettersMap(file);
13
14 // 创建 TestCase
15 tp = new AttributeRatingTestCase(testObject,settersMap);
16 }catch (Exception e){
17 e.printStackTrace();
18 }
19 // 添加 TestCase 到 TestSuite;
20 this.addTest(tp);
21 }
有了 addTestCase 方法 , 我们就可以轻易的把 TestCase 添加到 TestSuite 中了。参见清单 7。
清单 7. LWIAttributesRatingTestSuite.java
1 public LWIAttributesRatingTestSuite() {
2 // 获取 Logger.
3 AttributeLogger.getLogger();
4
5 // 获取 SettersMapFactory 的名字
6 Properties confProps = new Properties();
7 try{
8 confProps.load(new FileInputStream(CONFIG_FILE));
9 factory = confProps.getProperty("SettersFactory");
10 }catch(Exception e){
11 e.printStackTrace();
12 }
13
14 // 添加 TestCase
15 System.out.println("LWIAttributesRatingTestSuite...");
16 addTestCase(new MockLWITracing());
17 addTestCase(new MockInitialHeapSize());
18 addTestCase(new PoolPreparedStatements(null));
19 addTestCase(new PoolMaxConnections(null));
20 addTestCase(new MaxOpenPreparedStatements(null));
21
22 }
2 // 获取 Logger.
3 AttributeLogger.getLogger();
4
5 // 获取 SettersMapFactory 的名字
6 Properties confProps = new Properties();
7 try{
8 confProps.load(new FileInputStream(CONFIG_FILE));
9 factory = confProps.getProperty("SettersFactory");
10 }catch(Exception e){
11 e.printStackTrace();
12 }
13
14 // 添加 TestCase
15 System.out.println("LWIAttributesRatingTestSuite...");
16 addTestCase(new MockLWITracing());
17 addTestCase(new MockInitialHeapSize());
18 addTestCase(new PoolPreparedStatements(null));
19 addTestCase(new PoolMaxConnections(null));
20 addTestCase(new MaxOpenPreparedStatements(null));
21
22 }