/** * Print the contents of the given tableName to system.out<br> * <br> * Call this from any {@link BaseContextSensitiveTest} child by: * TestUtil.printOutTableContents(getConnection(), "encounter"); * * @param sqlConnection the connection to use * @param tableNames the name(s) of the table(s) to print out * @throws Exception */ public static void printOutTableContents(Connection sqlConnection, String... tableNames) throws Exception { for (String tableName : tableNames) { System.out.println("The contents of table: " + tableName); IDatabaseConnection connection = new DatabaseConnection(sqlConnection); DatabaseConfig config = connection.getConfig(); config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory()); QueryDataSet outputSet = new QueryDataSet(connection); outputSet.addTable(tableName); FlatXmlDataSet.write(outputSet, System.out); } }
@Test @SkipBaseSetup public void createConceptDictionaryDataSet() throws Exception { IDatabaseConnection connection = new DatabaseConnection(getConnection()); DatabaseConfig config = connection.getConfig(); config.setProperty(DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY, new ForwardOnlyResultSetTableFactory()); String[] tableNames = new String[] { "concept_class", "concept_datatype", "concept_map_type", "concept_reference_source", "concept", "concept_numeric", "concept_description", "concept_name", "concept_reference_term", "concept_reference_map", "concept_reference_term_map", "concept_set", "concept_complex", "concept_answer", "concept_stop_word" }; IDataSet dataSet = connection.createDataSet(tableNames); FlatXmlDataSet.write(dataSet, new FileOutputStream("target/conceptDictionaryDataSet.xml")); } }
public static void exportDataSet(IDataSet dataSet, String fileName) throws DataSetException, FileNotFoundException, IOException { FlatXmlDataSet.write(dataSet, new FileOutputStream(fileName)); }
public void exportDbUnitDataSetToFile(IDataSet dataset, File file) throws IOException, DataSetException { FlatXmlDataSet.write( dataset, new FileOutputStream(file)); } }
private DataDump createDataDump(final String path, final IDataSet dbContent) throws IOException, DataSetException { StringWriter stringWriter = new StringWriter(); FlatXmlDataSet.write(dbContent, stringWriter); DataDump dumpData = new DataDump(stringWriter.toString(), path); stringWriter.close(); return dumpData; }
private DataDump createDataDump(final String path, final IDataSet dbContent) throws IOException, DataSetException { StringWriter stringWriter = new StringWriter(); FlatXmlDataSet.write(dbContent, stringWriter); DataDump dumpData = new DataDump(stringWriter.toString(), path); stringWriter.close(); return dumpData; }
private DataDump createDataDump(final String path, final IDataSet dbContent) throws IOException, DataSetException { StringWriter stringWriter = new StringWriter(); FlatXmlDataSet.write(dbContent, stringWriter); DataDump dumpData = new DataDump(stringWriter.toString(), path); stringWriter.close(); return dumpData; }
private DataDump createDataDump(final String path, final IDataSet dbContent) throws IOException, DataSetException { StringWriter stringWriter = new StringWriter(); FlatXmlDataSet.write(dbContent, stringWriter); DataDump dumpData = new DataDump(stringWriter.toString(), path); stringWriter.close(); return dumpData; }
/** * Converts the supplied dataSet to a FlatXmlDataSet which, in turn, is written to a String. * * @param dataSet The IDataSet to convert. * @return The string representation of the supplied IDataSet. */ protected final String extractFlatXmlDataSet(@NotNull final IDataSet dataSet) { // Check sanity Validate.notNull(dataSet, "dataset"); // Convert and return. final StringWriter out = new StringWriter(); try { FlatXmlDataSet.write(dataSet, out); } catch (Exception e) { throw new IllegalArgumentException("Could not write FlatXmlDataSet.", e); } // All done. return out.toString(); }
public static void logDb(IDatabaseConnection connection) { try { ITableFilter filter = new DatabaseSequenceFilter(connection); IDataSet dataset = new FilteredDataSet(filter, connection.createDataSet()); StringWriter out = new StringWriter(); FlatXmlDataSet.write(dataset, out); log.info(out.getBuffer().toString()); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } }
@Override public void doInDbUnit(IDatabaseConnection connection) throws Exception { FilteredDataSet dataSet = new FilteredDataSet( new DatabaseSequenceFilter(connection), connection.createDataSet()); File parent = new File(dir); if (!parent.exists()) { parent.mkdirs(); } Writer out = new FileWriter(new File(dir, fileName)); //OutputStream out = new FileOutputStream(new File(dir, fileName)); FlatXmlDataSet.write(new SortedDataSet(dataSet), out, "UTF-8"); } });
/** * 把数据集写入到文件中 * @param dataSet * @param destFilePath */ public static void writeToFileFromDataSet(IDataSet dataSet,String destFilePath) { File destFile = new File(destFilePath); try { String destFileName = destFile.getName(); //转换为excel格式 if(destFileName.endsWith(".xls")){ XlsDataSet.write(dataSet, new FileOutputStream(destFile)); }else if(destFileName.endsWith(".xml")){ FlatXmlDataSet.write(dataSet, new FileOutputStream(destFile)); }else{ log.error("文件格式不是xls或者xml,不支持"); return; } log.info("写入数据到文件 : "+destFile.getAbsolutePath()); } catch (Exception e) { log.error("写入数据到文件失败"); log.error(e.getMessage()); } }
/** * 将表的数据根据query导出到文件,文件格式为flatXMLFile * * @param tableName 表名 * @param sqlQuery SQL查询语句 * @param flatXMLFile 导出的文件 * @throws DataSetException * @throws FileNotFoundException * @throws SQLException * @throws IOException */ public void exportFromDBToFile(String tableName,String sqlQuery,File flatXMLFile) throws DataSetException, FileNotFoundException, SQLException, IOException { QueryDataSet partialDataSet = new QueryDataSet(getDatabaseConnection()); partialDataSet.addTable(tableName,sqlQuery); FlatXmlDataSet.write(partialDataSet, new FileOutputStream(flatXMLFile)); }
public void export() { try { Connection dbCon = getConnection(); IDatabaseConnection connection = new DatabaseConnection(dbCon); List<String> tables = getTables(dbCon); // partial database export QueryDataSet partialDataSet = new QueryDataSet(connection); for (String table : tables) { partialDataSet.addTable(table); } FlatXmlDataSet.write(partialDataSet, new BufferedOutputStream(new FileOutputStream("full.xml"))); } catch (Exception e) { e.printStackTrace(); } }
/** * 将多张表的数据导出为一个DBUnit的FlatXMLFile * * @see insertTestData() * @param tableNames 需要导入数据的表名s * @param flatXMLFile 导出的文件 * @throws SQLException * @throws DataSetException * @throws FileNotFoundException * @throws IOException */ public void exportFromDBToFile(String[] tableNames,File flatXMLFile) throws SQLException, DataSetException, FileNotFoundException, IOException { QueryDataSet partialDataSet = new QueryDataSet(getDatabaseConnection()); for(int i = 0; i < tableNames.length; i++ ) { partialDataSet.addTable(tableNames[i]); } FlatXmlDataSet.write(partialDataSet, new FileOutputStream(flatXMLFile)); }
private String printTables() { try { StringWriter stringWriter = new StringWriter(); PrintWriter out = new PrintWriter(stringWriter); Connection dbCon = getConnection(); IDatabaseConnection connection = new DatabaseConnection(dbCon); IDataTypeFactory dataTypeFactory = environmentStrategy.getDataTypeFactory(); if (dataTypeFactory != null) { DatabaseConfig config = connection.getConfig(); config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqlDataTypeFactory()); } List<String> tables = getTables(dbCon); for (String table : tables) { out.println("------------ " + table + "------------ "); QueryDataSet partialDataSet = new QueryDataSet(connection); partialDataSet.addTable(table); FlatXmlDataSet.write(partialDataSet, out); } return stringWriter.toString(); } catch (Exception e) { e.printStackTrace(); return ""; } }
/** * Utility to create a dbunit xml file from a local mysql database * * @param args * @throws Exception */ public static void main(String[] args) throws Exception { Connection jdbcConnection = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PW); IDatabaseConnection connection = new DatabaseConnection(jdbcConnection); String currentDir = System.getProperty("user.dir"); String pathname = filepath(currentDir, "server", "src", "test", "resources", "dbunit", XML_FILE_NAME); System.out.println("Current directory: " + currentDir); System.out.println("File: " + pathname); File file = new File(pathname); if (!file.exists() && !file.createNewFile()) { throw new RuntimeException("Unable to create file: " + file.getAbsolutePath()); } // full database export IDataSet fullDataSet = connection.createDataSet(); FlatXmlDataSet.write(fullDataSet, new FileOutputStream(file, false)); }
private static void exportPartialDataSet() throws Exception { IDatabaseConnection connection = new DatabaseConnection(_conn); QueryDataSet dataSet = new QueryDataSet(connection); for (Iterator i=_tables.iterator(); i.hasNext(); ) { String table = (String) i.next(); dataSet.addTable(table); } if (!_destFilename.endsWith(".xml.gz")) { _destFilename = _destFilename + ".xml.gz"; } GZIPOutputStream gstream = new GZIPOutputStream(new FileOutputStream( _destFilename)); long start = System.currentTimeMillis(); System.out.print("writing " + _destFilename + "..."); FlatXmlDataSet.write(dataSet, gstream); gstream.finish(); System.out.println("done " + (System.currentTimeMillis() - start) + " ms"); }
protected void dumpDataBase(final String prefix, final String ... tables) { if (!enabled) { return; // no dumps } if (tables != null && tables.length > 0) { final File dump = new File("target" + File.separator + prefix + "_dataset.xml"); try { QueryDataSet queryDataSet = new QueryDataSet(dbTester.getConnection()); for (String tableName : tables) { queryDataSet.addTable(tableName); } FlatXmlDataSet.write(queryDataSet, new FileOutputStream(dump)); System.out.println("DUMP: " + dump.getAbsoluteFile()); } catch (Exception exp) { System.out.println("Unable to create dump file: " + dump.getAbsoluteFile()); exp.printStackTrace(); } } } }
FlatXmlDataSet.write(initialDataSet, new FileOutputStream("test/api/org/openmrs/logic/include/LogicBasicTest.xml"));