1// © 2016 and later: Unicode, Inc. and others. 2// License & terms of use: http://www.unicode.org/copyright.html#License 3/** 4 ******************************************************************************* 5 * Copyright (C) 2001-2007, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ******************************************************************************* 8 */ 9package com.ibm.icu.dev.test; 10 11import java.util.ArrayList; 12import java.util.Iterator; 13import java.util.List; 14 15import com.ibm.icu.dev.test.TestDataModule.DataMap; 16import com.ibm.icu.dev.test.TestDataModule.DataModuleFormatError; 17import com.ibm.icu.dev.test.TestDataModule.Factory; 18import com.ibm.icu.dev.test.TestDataModule.TestData; 19 20/** 21 * Ray: An adapter class for TestDataMoule to make it like TestFmwk 22 * 23 * A convenience extension of TestFmwk for use by data module-driven tests. 24 * 25 * Tests can implement this if they make extensive use of information in a 26 * TestDataModule. 27 * 28 * Subclasses can allow for test methods that don't use data from the module by 29 * overriding validateMethod to return true for these methods. Tests are also 30 * free to instantiate their own modules and run from them, though care should 31 * be taken not to interfere with the methods in this class. 32 * 33 * See CollationTest for an example. 34 */ 35public class ModuleTest { 36 private ModuleTest() { 37 // prevent construction 38 } 39 40 /** 41 * 42 * TestFmwk calls this before trying to run a suite of tests. The test suite 43 * if valid if a module whose name is the name of this class + "Data" can be 44 * opened. Subclasses can override this if there are different or additional 45 * data required. 46 */ 47 48 public static TestDataModule loadTestData(String baseName, String testName) throws DataModuleFormatError { 49 return Factory.get(baseName, testName); 50 } 51 52 static TestData openTestData(TestDataModule module, String name) throws DataModuleFormatError { 53 return module.getTestData(name); 54 } 55 56 public static class TestDataPair { 57 public TestData td; 58 public DataMap dm; 59 60 public TestDataPair(TestData td, DataMap dm) { 61 this.td = td; 62 this.dm = dm; 63 } 64 } 65 66 public static List<TestDataPair> getTestData(String moduleLocation, String moduleName) throws Exception { 67 List<TestDataPair> list = new ArrayList<TestDataPair>(); 68 69 TestDataModule m = ModuleTest.loadTestData(moduleLocation, moduleName); 70 Iterator<TestData> tIter = m.getTestDataIterator(); 71 while (tIter.hasNext()) { 72 TestData t = tIter.next(); 73 for (Iterator siter = t.getSettingsIterator(); siter.hasNext();) { 74 DataMap settings = (DataMap) siter.next(); 75 list.add(new TestDataPair(t, settings)); 76 } 77 } 78 return list; 79 } 80} 81