コード例 #1
0
 public static void main(String[] args) throws Exception {
   Class.forName("com.mysql.jdbc.Driver");
   Connection connection =
       DriverManager.getConnection("jdbc:mysql://localhost:3306/youxigu", "root", "root");
   //		DatabaseConnection conn = new DatabaseConnection(connection);
   //		DatabaseConfig config = conn.getConfig();
   //		config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
   //		QueryDataSet set = new QueryDataSet(conn);
   //		set.addTable("person");// 作用相当于SELECT * FROM tableName
   //		// set.addTable("animals", "select * from animals limit
   //		// 4");//相当于增加了查找的条件
   //		FlatXmlDataSet.write(set, new FileWriter(new File("dbunit.xml")), "UTF-8");
   //		new AnimalsTest().getDataSet();
   //		boolean enableColumnSensing = true;
   //		FlatXmlDataSet dataSet = new FlatXmlDataSet(new
   // File("D:/Develop/Eclipse_Mars/workspace/7q_mobile_2.7/data/zh_CN/Caddy.xml"), false,
   // enableColumnSensing);
   XmlProducer producer =
       new XmlProducer(
           FileHelper.createInputSource(
               new File("D:/Develop/Eclipse_Mars/workspace/7q_mobile_2.7/data/zh_CN/Caddy.xml")));
   CachedDataSet set = new CachedDataSet(producer);
   DatabaseConnection databaseConnection = new DatabaseConnection(connection);
   DatabaseOperation cleanInsert = DatabaseOperation.CLEAN_INSERT;
   DatabaseOperation.TRANSACTION(cleanInsert);
   cleanInsert.execute(databaseConnection, set);
   DatabaseOperation.CLOSE_CONNECTION(cleanInsert);
 }
コード例 #2
0
 private void setupOrTeardown(
     DbUnitTestContext testContext, boolean isSetup, Collection<AnnotationAttributes> annotations)
     throws Exception {
   IDatabaseConnection connection = testContext.getConnection();
   DatabaseOperation lastOperation = null;
   for (AnnotationAttributes annotation : annotations) {
     for (String dataSetLocation : annotation.getValue()) {
       DatabaseOperation operation = annotation.getType();
       org.dbunit.operation.DatabaseOperation dbUnitDatabaseOperation =
           getDbUnitDatabaseOperation(testContext, operation, lastOperation);
       IDataSet dataSet = loadDataset(testContext, dataSetLocation);
       if (dataSet != null) {
         if (logger.isDebugEnabled()) {
           logger.debug(
               "Executing "
                   + (isSetup ? "Setup" : "Teardown")
                   + " of @DatabaseTest using "
                   + operation
                   + " on "
                   + dataSetLocation);
         }
         dbUnitDatabaseOperation.execute(connection, dataSet);
         lastOperation = operation;
       }
     }
   }
 }
コード例 #3
0
  /** @throws Exception */
  @AfterClass
  protected void afterTestClass() throws Exception {
    logger.entry();
    for (TestDataHelper helper : testDataHelpers) {
      for (DatabaseOperation op : helper.getAfterTestClassOperations()) {
        op.execute(dbUnitConnection, helper.getTranslatedDataSet());
      }
    }

    logger.exit();
  }
コード例 #4
0
 /** @throws Exception */
 @AfterMethod(alwaysRun = true)
 protected void afterTestMethod() throws Exception {
   logger.entry();
   if (dbUnitConnection != null) {
     for (TestDataHelper helper : testDataHelpers) {
       for (DatabaseOperation op : helper.getAfterTestMethodOperations()) {
         op.execute(dbUnitConnection, helper.getTranslatedDataSet());
       }
     }
   }
   logger.exit();
 }
コード例 #5
0
  /** @throws Exception */
  @BeforeMethod(alwaysRun = true)
  protected void beforeTestMethod() throws Exception {
    logger.entry();

    if (dbUnitConnection != null) {
      for (TestDataHelper helper : testDataHelpers) {
        for (DatabaseOperation op : helper.getBeforeTestMethodOperations()) {
          logger.debug("RUNNING DB OPERATIONS FOR: " + helper.getRawDataFile());
          op.execute(dbUnitConnection, helper.getTranslatedDataSet());
        }
      }
    }
    logger.exit();
  }
コード例 #6
0
  /** @throws Exception */
  @BeforeClass(
      alwaysRun = true,
      dependsOnMethods = {"prepareDataSet"})
  protected void beforeTestClass() throws Exception {
    logger.entry();

    if (dbUnitConnection != null) {
      for (TestDataHelper helper : testDataHelpers) {
        for (DatabaseOperation op : helper.getBeforeTestClassOperations()) {
          logger.debug("RUNNING DB OPERATIONS FOR: " + helper.getRawDataFile());
          op.execute(dbUnitConnection, helper.getTranslatedDataSet());
        }
      }
    } else {
      logger.error("DB CONNECTION IS NULL, NOT RUNNING beforeTestClass operations");
    }
    logger.exit();
  }
コード例 #7
0
 /** Executa operações do DBUnit no dataset <code>dbUnitXmlPath</code>. */
 protected void execute(DatabaseOperation operation, String dbUnitXmlPath) {
   try {
     IDatabaseConnection dbconn = this.getDbUnitConnection();
     operation.execute(dbconn, this.getDataSetFrom(dbUnitXmlPath));
     dbconn.close();
   } catch (Exception e) {
     e.printStackTrace();
     throw new RuntimeException(e);
   }
 }
コード例 #8
0
  public static void reload() throws Exception {
    if (dbReloaded) {
      return;
    }

    Connection jdbcConn = null;
    IDatabaseConnection dbunitConn = null;
    try {
      jdbcConn = DriverManager.getConnection(URL, USERNAME, PASSWORD);

      boolean caseSensitiveTableNames = false;
      if (URL.contains("postgres")) {
        dbunitConn = new DatabaseConnection(jdbcConn, SCHEMA);
        dbunitConn
            .getConfig()
            .setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new PostgresqlDataTypeFactory());
        caseSensitiveTableNames = true;
      } else if (URL.contains("mysql")) {
        dbunitConn = new MySqlConnection(jdbcConn, null);
        if (System.getProperty("os.name").contains("Linux")) {
          caseSensitiveTableNames = true;
        }
      }
      // else if (URL.contains("oracle")) {
      //	dbunitConn = new OracleConnection(jdbcConn, SCHEMA);
      // }
      // else if (URL.contains("sqlserver")) {
      //	dbunitConn = new MsSqlConnection(jdbcConn, SCHEMA);
      // }
      // else if (URL.contains("db2")) {
      //	dbunitConn = new Db2Connection(jdbcConn, SCHEMA);
      //	if (System.getProperty("os.name").contains("Linux")) {
      //		caseSensitiveTableNames = true;
      //	}
      // }
      else {
        throw new IllegalStateException("Die Datenbank-URL " + URL + " wird nicht unterstuetzt");
      }

      final ClassLoader cl = Thread.currentThread().getContextClassLoader();
      final FlatXmlDataSetBuilder flatXmlDataSetBuilder = new FlatXmlDataSetBuilder();
      flatXmlDataSetBuilder.setCaseSensitiveTableNames(caseSensitiveTableNames);
      FlatXmlDataSet xmlDataset = flatXmlDataSetBuilder.build(cl.getResource(XML_FLAT_DATASET));

      DatabaseOperation dbOp = CLEAN_INSERT;
      // if (URL != null && URL.contains("sqlserver")) {
      //	// Fuer SQL Server ist ein spezieller INSERT-Modus notwendig,
      //	// damit IDENTITY-Spalten eingefuegt werden koennen
      //	dbOp = InsertIdentityOperation.CLEAN_INSERT;
      // }

      dbOp.execute(dbunitConn, xmlDataset);
    } finally {
      if (dbunitConn != null) dbunitConn.close();

      if (jdbcConn != null) jdbcConn.close();
    }

    dbReloaded = true;

    System.out.println("Die Datenbank " + URL + " wurde neu geladen");
  }
コード例 #9
0
ファイル: DBUnitSeamTest.java プロジェクト: nuxeo/jboss-seam
 public String toString() {
   // TODO: This is not pretty because DBUnit's DatabaseOperation doesn't implement toString()
   // properly
   return operation.getClass() + " with dataset: " + dataSetLocation;
 }