本文整理汇总了Java中org.dbunit.database.DatabaseConnection类的典型用法代码示例。如果您正苦于以下问题:Java DatabaseConnection类的具体用法?Java DatabaseConnection怎么用?Java DatabaseConnection使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DatabaseConnection类属于org.dbunit.database包,在下文中一共展示了DatabaseConnection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.dbunit.database.DatabaseConnection; //导入依赖的package包/类
public static void main(String[] args)
throws Exception
{
//Connect to the database
Class.forName( "com.mysql.jdbc.Driver" );
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/easyrec_test", "root", "root");
IDatabaseConnection connection = new DatabaseConnection( conn );
QueryDataSet partialDataSet = new QueryDataSet(connection);
//Specify the SQL to run to retrieve the data
partialDataSet.addTable("actionarchive1", " SELECT * FROM actionarchive1");
//Specify the location of the flat file(XML)
// file is stored in /target folder
FlatXmlWriter datasetWriter = new FlatXmlWriter(new FileOutputStream("easyrec-testutils/target/temp.xml"));
//Export the data
datasetWriter.write( partialDataSet );
}
示例2: initEntityManager
import org.dbunit.database.DatabaseConnection; //导入依赖的package包/类
/**
* Set up memory database and insert data from test-dataset.xml
*
* @throws DatabaseUnitException
* @throws HibernateException
* @throws SQLException
*/
@BeforeClass
public static void initEntityManager() throws HibernateException, DatabaseUnitException, SQLException {
entityManagerFactory = Persistence.createEntityManagerFactory("listing-test-db");
entityManager = entityManagerFactory.createEntityManager();
connection = new DatabaseConnection(((SessionImpl) (entityManager.getDelegate())).connection());
connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(datasetXml);
if (inputStream != null) {
FlatXmlDataSetBuilder flatXmlDataSetBuilder = new FlatXmlDataSetBuilder();
flatXmlDataSetBuilder.setColumnSensing(true);
dataset = flatXmlDataSetBuilder.build(inputStream);
DatabaseOperation.CLEAN_INSERT.execute(connection, dataset);
}
}
示例3: shouldExportDataSetUsingSubSelectToFilterRows
import org.dbunit.database.DatabaseConnection; //导入依赖的package包/类
@Test
@DataSet("datasets/yml/users.yml")
public void shouldExportDataSetUsingSubSelectToFilterRows() throws SQLException, DatabaseUnitException {
DataSetExporter.getInstance().export(new DatabaseConnection(emProvider.connection()),
new DataSetExportConfig().
outputFileName("target/querySubselect.yml").
//export only users that have tweets
queryList(new String[]{"select * from user u where u.id in" +
" (select t.user_id from tweet t)"})
);
File ymlDataSet = new File("target/querySubselect.yml");
assertThat(ymlDataSet).exists();
assertThat(contentOf(ymlDataSet)).
isEqualTo("user:"+NEW_LINE +
" - ID: 1"+NEW_LINE +
" NAME: \"@realpestano\""+NEW_LINE+NEW_LINE);
}
示例4: shouldNotExportNullColumnsInYMLDataSet
import org.dbunit.database.DatabaseConnection; //导入依赖的package包/类
@Test
public void shouldNotExportNullColumnsInYMLDataSet() throws SQLException, DatabaseUnitException {
DataSetExporter.getInstance().export(new DatabaseConnection(emProvider.connection()), new DataSetExportConfig().outputFileName("target/userWithNullProperty.yml"));
File ymlDataSet = new File("target/userWithNullProperty.yml");
assertThat(ymlDataSet).exists();
assertThat(contentOf(ymlDataSet)).
contains("FOLLOWER:" + NEW_LINE +
"" + NEW_LINE +
"SEQUENCE:" + NEW_LINE +
" - SEQ_NAME: \"SEQ_GEN\"" + NEW_LINE +
" SEQ_COUNT: 50" + NEW_LINE +
"" + NEW_LINE +
"TWEET:" + NEW_LINE +
"" + NEW_LINE +
"USER:" + NEW_LINE +
" - ID: 1" + NEW_LINE + NEW_LINE
);
}
示例5: shouldNotExportNullColumnsInJSONDataSet
import org.dbunit.database.DatabaseConnection; //导入依赖的package包/类
@Test
public void shouldNotExportNullColumnsInJSONDataSet() throws SQLException, DatabaseUnitException{
DataSetExporter.getInstance().export(new DatabaseConnection(emProvider.connection()), new DataSetExportConfig().
dataSetFormat(DataSetFormat.JSON).outputFileName("target/userWithNullProperty.json"));
File xmlDataSet = new File("target/userWithNullProperty.json");
assertThat(xmlDataSet).exists();
assertThat(contentOf(xmlDataSet).replaceAll("\r","")).isEqualTo(("{"+NEW_LINE +
" \"FOLLOWER\": ["+NEW_LINE +
" ],"+NEW_LINE +
" \"SEQUENCE\": ["+NEW_LINE +
" {"+NEW_LINE +
" \"SEQ_NAME\": \"SEQ_GEN\","+NEW_LINE +
" \"SEQ_COUNT\": 50"+NEW_LINE +
" }"+NEW_LINE +
" ],"+NEW_LINE +
" \"TWEET\": ["+NEW_LINE +
" ],"+NEW_LINE +
" \"USER\": ["+NEW_LINE +
" {"+NEW_LINE +
" \"ID\": 1,"+NEW_LINE +
" }"+NEW_LINE +
" ]"+NEW_LINE +
"}").replaceAll("\r",""));
}
示例6: openConnection
import org.dbunit.database.DatabaseConnection; //导入依赖的package包/类
public static IDatabaseConnection openConnection(final BasicDataSource ds) {
try {
final Connection connection = ds.getConnection();
for (final DbUnitConnectionFactory impl : SERVICE_LOADER) {
if (impl.supportsDriver(ds.getDriverClassName())) {
return impl.createConnection(connection);
}
}
// fall back if no specific implementation is available
return new DatabaseConnection(connection);
} catch (final DatabaseUnitException | SQLException e) {
throw new JpaUnitException(e);
}
}
示例7: createConnection
import org.dbunit.database.DatabaseConnection; //导入依赖的package包/类
@Override
public IDatabaseConnection createConnection(final Connection connection) throws DatabaseUnitException {
final IDatabaseConnection dbUnitConnection = new DatabaseConnection(connection);
dbUnitConnection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new PostgresqlDataTypeFactory());
return dbUnitConnection;
}
示例8: testOpenConnectionToSqliteDbHavingAllSupportedPersistenceProperties
import org.dbunit.database.DatabaseConnection; //导入依赖的package包/类
@Test
public void testOpenConnectionToSqliteDbHavingAllSupportedPersistenceProperties() throws Exception {
// This test is about a fall back functionality
// GIVEN
final File dbFile = folder.newFile("test.db");
final BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(SQLITE_DRIVER_CLASS_PROP_VALUE);
ds.setUsername(USERNAME_PROP_VALUE);
ds.setPassword(PASSWORD_PROP_VALUE);
ds.setUrl(SQLITE_CONNECTION_URL_PROP_PREFIX + dbFile.getAbsolutePath());
// WHEN
connection = DatabaseConnectionFactory.openConnection(ds);
// THEN
assertThat(connection, notNullValue());
assertThat(connection.getClass(), equalTo(DatabaseConnection.class));
}
示例9: testOpenConnectionToSqliteDbWithoutHavingUsernameAndPasswordProperties
import org.dbunit.database.DatabaseConnection; //导入依赖的package包/类
@Test
public void testOpenConnectionToSqliteDbWithoutHavingUsernameAndPasswordProperties() throws Exception {
// GIVEN
final File dbFile = folder.newFile("test.db");
final BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(SQLITE_DRIVER_CLASS_PROP_VALUE);
ds.setUrl(SQLITE_CONNECTION_URL_PROP_PREFIX + dbFile.getAbsolutePath());
// WHEN
connection = DatabaseConnectionFactory.openConnection(ds);
// THEN
assertThat(connection, notNullValue());
assertThat(connection.getClass(), equalTo(DatabaseConnection.class));
}
示例10: setUp
import org.dbunit.database.DatabaseConnection; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
initialDataSet = new FlatXmlDataSetBuilder().build(new File("src/test/resources/test-data.xml"));
connection = new DatabaseConnection(DriverManager.getConnection(CONNECTION_URL, USER_NAME, PASSWORD));
final DatabaseOperation operation = DatabaseOperation.CLEAN_INSERT;
operation.execute(connection, initialDataSet);
connection.getConnection().createStatement().execute(
"insert into XML_TABLE_1(id, version, value_1, value_2, value_3, value_4, value_5) values(10, 'Record 10 version', 'Record 10 Value 1', 'Record 10 Value 2', 'Record 10 Value 3', 'Record 10 Value 4', 'Record 10 Value 5');");
connection.getConnection().commit();
connection.getConnection().createStatement().execute(
"merge into XML_TABLE_3(id, version, value_8, value_9) values(11, 'Record 11 version', 'Record 11 Value 8', 'Record 11 Value 9');");
connection.getConnection().commit();
assertThat(getRecordCountFromTable(connection, "XML_TABLE_1"), equalTo(4));
assertThat(getRecordCountFromTable(connection, "XML_TABLE_2"), equalTo(1));
assertThat(getRecordCountFromTable(connection, "XML_TABLE_3"), equalTo(1));
}
示例11: insertDbUnitTestdata
import org.dbunit.database.DatabaseConnection; //导入依赖的package包/类
/**
* Inserts test data in DbUnit XML format.
*
* @param entityManager
* the EntityManager
* @param dbUnitTestdata
* test file stream
*/
public static void insertDbUnitTestdata(EntityManager entityManager, final InputStream dbUnitTestdata) {
entityManager.unwrap(Session.class).doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
// Insert Testdata
try {
LOGGER.debug("INSERTing testdata");
DatabaseConnection databaseConnection = new DatabaseConnection(connection);
databaseConnection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
new MySqlDataTypeFactory());
FlatXmlDataSet dataSet = new FlatXmlDataSet(
new FlatXmlProducer(new InputSource(dbUnitTestdata), false, true));
DatabaseOperation.CLEAN_INSERT.execute(databaseConnection, dataSet);
LOGGER.debug("INSERTing testdata ... done");
} catch (DatabaseUnitException e) {
throw new SQLException(e);
}
}
});
}
示例12: getConnection
import org.dbunit.database.DatabaseConnection; //导入依赖的package包/类
protected IDatabaseConnection getConnection() throws Exception
{
Configuration cfg = getHibernateConfiguration();
String username = cfg.getProperty( "hibernate.connection.username" );
String password = cfg.getProperty( "hibernate.connection.password" );
String driver = cfg.getProperty( "hibernate.connection.driver_class" );
String url = cfg.getProperty( "hibernate.connection.url" );
Class.forName( driver );
Connection jdbcConnection = DriverManager.getConnection( url, username, password );
IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
DatabaseConfig config = connection.getConfig();
// config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new H2DataTypeFactory());
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());
return connection;
}
示例13: getConnection
import org.dbunit.database.DatabaseConnection; //导入依赖的package包/类
protected IDatabaseConnection getConnection() throws Exception {
Properties cfg = getHibernateConfiguration();
String username = cfg.getProperty("hibernate.connection.username");
String password = cfg.getProperty("hibernate.connection.password");
String driver = cfg.getProperty("hibernate.connection.driver_class");
String url = cfg.getProperty("hibernate.connection.url");
Class.forName(driver);
Connection jdbcConnection = DriverManager.getConnection(url, username, password);
IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
DatabaseConfig config = connection.getConfig();
// config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new
// H2DataTypeFactory());
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());
return connection;
}
开发者ID:testIT-LivingDoc,项目名称:livingdoc-confluence,代码行数:20,代码来源:AbstractDBUnitHibernateMemoryTest.java
示例14: testInvalidQuery
import org.dbunit.database.DatabaseConnection; //导入依赖的package包/类
@Test
public void testInvalidQuery() throws Exception {
logger.info("-------------------- Test Invalid Query --------------------");
String queryFolder = getQueryFolderPrefix() + "src/test/resources/query/sql_invalid";
List<File> sqlFiles = getFilesFromFolder(new File(queryFolder), ".sql");
for (File sqlFile : sqlFiles) {
String queryName = StringUtils.split(sqlFile.getName(), '.')[0];
logger.info("Testing Query " + queryName);
String sql = getTextFromFile(sqlFile);
IDatabaseConnection cubeConn = new DatabaseConnection(cubeConnection);
try {
cubeConn.createQueryTable(queryName, sql);
} catch (Throwable t) {
continue;
} finally {
cubeConn.close();
}
throw new IllegalStateException(queryName + " should be error!");
}
}
示例15: testVersionQuery
import org.dbunit.database.DatabaseConnection; //导入依赖的package包/类
@Test
public void testVersionQuery() throws Exception {
String expectVersion = KylinVersion.getCurrentVersion().toString();
logger.info("---------- verify expect version: " + expectVersion);
String queryName = "QueryKylinVersion";
String sql = "SELECT VERSION() AS version";
// execute Kylin
logger.info("Query Result from Kylin - " + queryName);
IDatabaseConnection kylinConn = new DatabaseConnection(cubeConnection);
ITable kylinTable = executeQuery(kylinConn, queryName, sql, false);
String queriedVersion = String.valueOf(kylinTable.getValue(0, "version"));
// compare the result
Assert.assertEquals(expectVersion, queriedVersion);
}