<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ralf Schäftlein&#039;s Blog &#187; oracle</title>
	<atom:link href="http://ralf.schaeftlein.de/tag/oracle/feed/" rel="self" type="application/rss+xml" />
	<link>http://ralf.schaeftlein.de</link>
	<description>tech stuff, talk,...</description>
	<lastBuildDate>Sat, 27 Mar 2010 11:24:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Dbunit with JUnit 4.x and Spring for testing Oracle DB Application</title>
		<link>http://ralf.schaeftlein.de/2009/01/05/dbunit-with-junit-4x-and-spring-for-testing-oracle-db-application/</link>
		<comments>http://ralf.schaeftlein.de/2009/01/05/dbunit-with-junit-4x-and-spring-for-testing-oracle-db-application/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 17:59:23 +0000</pubDate>
		<dc:creator>ralf</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[dbunit]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://ralf.schaeftlein.de/?p=62</guid>
		<description><![CDATA[DBUnit is very nice for testing database content changes made by an application. You define in XML the data including the structure of your tables (dataset.xml). Simple_Data is the name of the table and each column is a attribute in the xml doc with the content value e.g. id with value 1. The Getting Started [...]]]></description>
			<content:encoded><![CDATA[<p>DBUnit is very nice for testing database content changes made by an application. You define in XML the data including the structure of your tables (dataset.xml).</p>
<p><pre name="code" class='xml'>
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
	<simple_data id="1" content="value_before"/>
</dataset>
</pre>
<p>
Simple_Data is the name of the table and each column is a attribute in the xml doc with the content value e.g. id with value 1.</p>
<p>
The <a href="http://dbunit.sourceforge.net/howto.html">Getting Started</a> of DBUnit work with JUnit 3.8 and self handling of the JDBC Connection.</p>
<p>
JUnit 4.x are more comfortable with annotations based test methods and Spring comes with dependency injection for separating<br />
configuration from implementation code.</p>
<p>
The following approach combines DBUnit with JUnit 4.4 and Spring 2.5.6 to test comfortable a Oracle 10g database.</p>
<p>
I use Maven 2.x to define the depending libraries used by the example (pom.xml):</p>
<p><pre name="code" class='xml'>
<?xml version="1.0" encoding="UTF-8"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<groupId>de.schaeftlein.dev.dbunit</groupId>
	<artifactId>test-dbunit</artifactId>
	<name>test-dbunit</name>
	<version>0.0.1-SNAPSHOT</version>
	<description />
	<dependencies>
		<dependency>
			<groupId>org.dbunit</groupId>
			<artifactId>dbunit</artifactId>
			<version>2.4.2</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring</artifactId>
			<version>2.5.6</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.4</version>
		</dependency>
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.2.2</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>2.5.6</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.5.6</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>log4j-over-slf4j</artifactId>
			<version>1.5.6</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.14</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.5.6</version>
		</dependency>
		<dependency>
			<groupId>com.oracle</groupId>
			<artifactId>ojdbc14</artifactId>
			<version>10.2.0.2.0</version>
		</dependency>
	</dependencies>
</project>
</pre>
<p>
Keep in mind that the Oracle JDBC Driver has to be downloaded <a href="http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/htdocs/jdbc_10201.html">manually</a>.<br />
The public maven repos include only the Pom definition for the oracle driver. Generate with maven command line tool the eclipse project files:
<p>
<code>mvn clean eclipse:clean eclipse:eclipse</code></p>
<p>
The JDBC datasource is defined via Spring (applicationContext.xml):
<p><pre name="code" class='xml'>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
	destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@10.0.0.2:1521:orcl" />
<property name="username" value="your_user_name" />
<property name="password" value="your_password" />
  </bean>
</beans>
</pre>
<p>
Additionally we define the expected data as well in XML for DBUnit (expectedDataSet.xml):
<p>
<P></p>
<pre name="code" class='xml'>
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
	<simple_data id="1" content="value_after"/>
</dataset>
</pre>
<p>
Now we can code our JUnit 4.x Test to
<ol>
<li>load data before the test method
<li>change the data via JDBC to emulate a application
<li>compare the changed data with expected data
<li>clean up the database
</ol>
<p><pre name="code" class='java'>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class TestDBUnitWithSpring {

	@Autowired
	private DataSource dataSource;

	@Before
	public void init() throws Exception{
		// insert data into db
		DatabaseOperation.CLEAN_INSERT.execute(getConnection(), getDataSet());
	}

	@After
	public void after() throws Exception{
		// insert data into db
		DatabaseOperation.DELETE_ALL.execute(getConnection(), getDataSet());
	}

	private IDatabaseConnection getConnection() throws Exception{
	// get connection
		Connection con = dataSource.getConnection();
		DatabaseMetaData  databaseMetaData = con.getMetaData();
		// oracle schema name is the user name
		IDatabaseConnection connection = new DatabaseConnection(con,databaseMetaData.getUserName().toUpperCase());
		DatabaseConfig config = connection.getConfig();
		// oracle 10g
		config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new Oracle10DataTypeFactory());
		// receycle bin
		config.setFeature(DatabaseConfig.FEATURE_SKIP_ORACLE_RECYCLEBIN_TABLES, Boolean.TRUE);
		return connection;
	}

	private IDataSet getDataSet() throws Exception{
		// get insert data
		File file = new File("src/test/resources/dataset.xml");
		return new FlatXmlDataSet(file);
	}

	@Test
	public void testSQLUpdate() throws Exception{
		Connection con = dataSource.getConnection();
		Statement stmt = con.createStatement();
		// get current data
		ResultSet rst = stmt.executeQuery("select * from simple_data where id = 1");
		if(rst.next()){
			// from dataset.xml
			assertEquals("value_before", rst.getString("content"));
			rst.close();

			// update via sql
			int count = stmt.executeUpdate("update simple_data set content='value_after' where id=1");

			stmt.close();
			con.close();

			// expect only one row to be updated
			assertEquals("one row should be updated", 1, count);

			// Fetch database data after executing the code
			QueryDataSet databaseSet = new QueryDataSet(getConnection());
			// filter data
			databaseSet.addTable("simple_data", "select * from simple_data where id = 1");
			ITable actualTable = databaseSet.getTables()[0];

			// Load expected data from an XML dataset
			IDataSet expectedDataSet = new FlatXmlDataSet(new File("src/test/resources/expectedDataSet.xml"));
			ITable expectedTable = expectedDataSet.getTable("simple_data");

			// filter unnecessary columns of current data by xml definition
			actualTable = DefaultColumnFilter.includedColumnsTable(actualTable, expectedTable.getTableMetaData().getColumns());

			// Assert actual database table match expected table
			assertEquals(1,expectedTable.getRowCount());
			assertEquals(expectedTable.getRowCount(), actualTable.getRowCount());
			assertEquals(expectedTable.getValue(0, "content"), actualTable.getValue(0, "content"));

		} else {
			fail("no rows");
			rst.close();
			stmt.close();
			con.close();
		}

	}
}
</pre>
<p>
]]></content:encoded>
			<wfw:commentRss>http://ralf.schaeftlein.de/2009/01/05/dbunit-with-junit-4x-and-spring-for-testing-oracle-db-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jdbc DataBaseMetaData + oracle 10g + dbunit + username..</title>
		<link>http://ralf.schaeftlein.de/2008/04/11/jdbc-databasemetadata-oracle-10g-dbunit-username/</link>
		<comments>http://ralf.schaeftlein.de/2008/04/11/jdbc-databasemetadata-oracle-10g-dbunit-username/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 12:47:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[dbunit]]></category>
		<category><![CDATA[jdbc]]></category>
		<category><![CDATA[oracle]]></category>

		<guid isPermaLink="false">http://ralf.schaeftlein.de/?p=6</guid>
		<description><![CDATA[If you are using DBUnit as tool for set up test environment in your oracle 10g then you have to provide the username of course. Normally it isn&#8217;t case sensitive in most tools. The jdbc driver or sqlplus let you create connection with lower case. DBUnit ask the DataBaseMetaData to get the defined tables in [...]]]></description>
			<content:encoded><![CDATA[<p>If you are using <a href="http://dbunit.sourceforge.net" target="_blank">DBUnit </a> as tool for set up test environment in your oracle 10g then you have to provide the username of course. Normally it isn&#8217;t case sensitive in most tools. The jdbc driver or sqlplus let you create connection with lower case. DBUnit ask the DataBaseMetaData to get the defined tables in the schema. Schema and username are the same in the oracle world. So in DBUnit the source is</p>
<p><pre name="code" class="java">
DatabaseMetaData databaseMetaData = jdbcConnection.getMetaData();
ResultSet resultSet = databaseMetaData.getTables(null, schema, "%", tableType);
</pre>
<p>
where tableType is the String[] {&#8220;table&#8221;}. with schema == username lower case i get in my oracle 10.2 db nothing back. With upper case username et voila! Don&#8217;t know if this is really an standard behaviour&#8230;.</p>
]]></content:encoded>
			<wfw:commentRss>http://ralf.schaeftlein.de/2008/04/11/jdbc-databasemetadata-oracle-10g-dbunit-username/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JDBC Connection + Oracle 10g + recycle bin&#8230;</title>
		<link>http://ralf.schaeftlein.de/2008/04/11/jdbc-connection-oracle-10g-recycle-bin/</link>
		<comments>http://ralf.schaeftlein.de/2008/04/11/jdbc-connection-oracle-10g-recycle-bin/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 12:24:38 +0000</pubDate>
		<dc:creator>ralf</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[jdbc]]></category>
		<category><![CDATA[oracle]]></category>

		<guid isPermaLink="false">http://ralf.schaeftlein.de/?p=5</guid>
		<description><![CDATA[Since Oracle 10g dropped tables goes into a recycle bin: http://orafaq.com/node/968. These tables aren&#8217;t visible inside the admin site under the user but if you are using JDBC to access the schema. Thought first of a bug in SQL Explorer when i saw the huge list of tables starting with BIN$&#8230; Every time you drop [...]]]></description>
			<content:encoded><![CDATA[<p>Since Oracle 10g dropped tables goes into a recycle bin: http://orafaq.com/node/968. These tables aren&#8217;t visible inside the admin site under the user but if you are using JDBC to access the schema. Thought first of a bug in SQL Explorer when i saw the huge list of tables starting with BIN$&#8230; Every time you drop tables more BIN$ you get&#8230; If someone needs such a future&#8230; I&#8217;m more a friend of the &#8220;know what i&#8217;m doing&#8217; approach.  The  good thing  is that this is a feature  which you can disable:</p>
<ol class="dp-sql">
<li class="alt"><span><span>sqlplus sys/</span><span class="keyword">&lt;your password&gt;</span><span> </span><span class="keyword">as</span><span> sysdba </span></span></li>
<li><span>SQL&gt; purge dba_recyclebin; </span></li>
<li class="alt"><span>SQL&gt; <span class="keyword">alter</span><span> system </span><span class="keyword">set</span><span> recyclebin = </span><span class="keyword">OFF</span><span> scope=both;<br />
</span></span></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://ralf.schaeftlein.de/2008/04/11/jdbc-connection-oracle-10g-recycle-bin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
