Dashboard > DbFixture > ... > One Minute Tutorial > DbFixtureOneMinuteTutorialTest.java
DbFixtureOneMinuteTutorialTest.java Log In | Sign Up   View a printable version of the current page.

Added by Shane Duan , last edited by Shane Duan on Feb 09, 2006  (view change)
Labels: 
(None)

DbFixtureOneMinuteTutorialTest.java
package org.thoughtworks.dbfixture.api;

import java.sql.*;

import org.thoughtworks.dbfixture.sample.*;
import org.thoughtworks.dbfixture.*;
import org.thoughtworks.dbfixture.proxy.ProxyDriver;
import junit.framework.*;

/**
 * A One minute tutorial test case for DbFixture Test.
 * This file is to be delivered as part of the document.
 * A proxy driver JDBC URL in the form of "jdbc:dbfixture:proxy:<database-name>" will connect
 * to an HSQL DB using URL "jdbc:hsqldb:mem:<database-name>", after the database is set up
 * through registered schema.
 *
 * Database class is to be used for data setup and verification.
 *
 * Resources are being monitored and collected.
 * The ones that being loaded through Database API can be cleaned by calling
 * DatabaseRegistry.instance().cleanResources()
 * The ones that being loaded through ProxyDriver can be verified by calling
 * DatabaseRegistry.instance().resourceVerifier() (which can be registered if
 * the current test case is extending org.jmock.core.VerifyingTestCase
 */
public class DbFixtureOneMinuteTutorialTest extends TestCase {
  // Registered schema will be loaded lazily.
  static {
    DatabaseRegistry.instance().registerSchema(new Schema() {

      public String getDbName() {
        return databaseName();
      }

      public void setupSchema(HsqlDb database) {
        database.execute(
            "create user user password password\n" +
            "create table person (\n" +
            "  person_key integer IDENTITY,\n" +
            "  ssn varchar(10) not null,\n" +
            "  first_name varchar(64) null,\n" +
            "  last_name varchar(64) null,\n" +
            "  birthdate datetime null,\n" +
            "  CONSTRAINT person_primary_key PRIMARY KEY (person_key)\n" +
            ")\n" +
            ";\n" +
            "grant all on person to public;");
      }
    });
  }

  protected void tearDown() throws Exception {
    DatabaseRegistry.instance().cleanResources();
    DatabaseRegistry.instance().resourceVerifier().verify();
  }

  private static String databaseName() {
    return "tutorial";
  }

  private Database database() {
    return DatabaseRegistry.instance().getDatabase(databaseName());
  }

  public void testApi() throws SQLException {
    ProductionCode testedObject = new ProductionCode(ProxyDriver.jdbcUrl(databaseName()), "user", "password");
    testedObject.insertSomeData("383-23-2888", "John", "Smith");
    ResultRow row = database().table("person").row("ssn='383-23-2888'");
    assertEquals("John", row.column("first_name"));
    assertEquals("Smith", row.column("last_name"));
  }

}
Powered by a free Atlassian Confluence Open Source Project License granted to ThoughtWorks, Inc.. Evaluate Confluence today.
Powered by Atlassian Confluence 2.7.1, the Enterprise Wiki. Bug/feature request - Atlassian news - Contact administrators