/** * Get's data from XML file in classpath in specified format.<br> * First search the * * @param className ClassName * @param context The context. */ public HierarchicalConfiguration getData( final String className, HierarchicalConfiguration context) { HierarchicalConfiguration dataForTestCase = null; try { Class<?> clazz = Class.forName(className); String dataFilePath = null; URL dataFileURL = null; boolean packageFile = false; Map<String, HierarchicalConfiguration> dataMap = null; String dataFileName = context.getString("supplier.dataFile", null); log.debug("Checking the data file in argument..."); if (dataFileName == null || dataFileName.equals("")) { log.debug("Data file not given in argument..Using DataFileFinder.."); dataFilePath = DDUtils.findDataFile(className, ".xml", context); } else { log.debug("Got data file in argument"); dataFilePath = dataFileName; } log.debug("Data file path: " + dataFilePath); if (dataFilePath == null) { return null; // No data found, hence it's a normal test case. } dataFileURL = clazz.getResource(dataFilePath); if (packageFile) { // The data file is from package file name so check the cache. log.debug("Cache: " + cache.size()); synchronized (XMLDataSupplier.class) { if (loadedFiles.contains(dataFilePath)) { // get it from cache. log.info("File was loaded before !!!"); dataForTestCase = cache.get(clazz.getName()); } else { // not in cache, so load and put it to cache. log.info("File was not loaded before, loading now..."); if (dataFileURL != null) { cache.putAll(XMLDataParser.load(dataFileURL, clazz)); } else { cache.putAll(XMLDataParser.load(dataFilePath, clazz)); } dataForTestCase = cache.get(clazz.getName()); loadedFiles.add(dataFilePath); } } if ((dataForTestCase == null) || dataForTestCase.isEmpty()) { log.info("Data for '{}' is not available!", className); return null; } } else { // data file not from package file so go ahead and load. log.debug("Loading the xml file..."); if (dataFileURL != null) { dataMap = XMLDataParser.load(dataFileURL, clazz); } else { dataMap = XMLDataParser.load(dataFilePath, clazz); } dataForTestCase = dataMap.get(clazz.getName()); } } catch (Exception ex) { throw new DDException("Error in loading the data file", ex); } return dataForTestCase; }
private List<MetaModule> loadFile(String name) throws IOException { String content = XMLPreprocessor.loadFile(new File("test/xmldataset/" + name), null); assertNotNull(content); assertFalse(content.length() == 0); ///////// List<MetaModule> modules = XMLDataParser.parseModules(content); return modules; }
@Test public void testDataParser() throws Exception { File f = new File("test/xmldataset/datadef.xml"); String content = XMLPreprocessor.loadFile(f, null); assertNotNull(content); assertFalse(content.length() == 0); ///////// List<MetaModule> modules = XMLDataParser.parseModules(content); performBasicChecks(modules); }