protected final IDataSet getCurrentDataSet() {
   try {
     return connection.createDataSet();
   } catch (SQLException e) {
     throw translateException("Could not create the current dataset", e);
   }
 }
Example #2
0
  protected void extractFullDataset(final String datasetFileName) throws Exception {
    // database connection
    IDatabaseConnection connection = getDbUnitConnection();

    // full database export
    IDataSet fullDataSet = connection.createDataSet();
    FlatXmlDataSet.write(fullDataSet, new FileOutputStream(datasetFileName));
  }
 public void dump(String dbUnitXmlPath) {
   try {
     IDatabaseConnection dbconn = getDbUnitConnection();
     IDataSet iDataSet = dbconn.createDataSet();
     // se for necessário ordenar devido as constraints - demora pacas
     // iDataSet = new FilteredDataSet(new
     // DatabaseSequenceFilter(dbconn), iDataSet);
     FlatXmlDataSet.write(iDataSet, new FileOutputStream(dbUnitXmlPath));
   } catch (Exception e) {
     e.printStackTrace();
     throw new RuntimeException(e);
   }
 }
  /**
   * Verify that we can start the server as a daemon.
   *
   * @throws Exception If there was an error.
   */
  @Test
  public void testRunDaemon() throws Exception {
    ReflectionUtils.setVariableValueInObject(mojo, "daemon", Boolean.TRUE);
    try {
      mojo.execute();
      Thread.sleep(5000L);
      final Connection jdbcConnection = DriverManager.getConnection(getConnectionString());
      final IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
      IDataSet databaseDataSet = connection.createDataSet();
      assertNotNull(databaseDataSet.getTable(getTableName()));

      connection.close();
      jdbcConnection.close();
    } finally {
      signalStop();
    }
  }
Example #5
0
  public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection =
        DriverManager.getConnection("jdbc:mysql://localhost:3306/dbunit", "root", "1234");
    IDatabaseConnection databaseConnection = new DatabaseConnection(connection);

    // 根据SQL导出部分数据
    QueryDataSet queryDataSet = new QueryDataSet(databaseConnection);
    queryDataSet.addTable("users", "select password from users where id = 10");
    FlatXmlDataSet.write(queryDataSet, new FileOutputStream("dbunitXMLConditation.xml"));

    // 导出整个库的数据
    IDataSet dataSet = databaseConnection.createDataSet();
    // 将dbunit表中的数据写入到dbunitXML.xml文件中
    FlatXmlDataSet.write(dataSet, new FileOutputStream("dbunitXML.xml"));
    FlatDtdDataSet.write(dataSet, new FileOutputStream("dbunitXML.dtd"));
  }
Example #6
0
  private static void oldMain() throws Exception {

    //        System.setProperty("dbunit.name.escapePattern", "\"?\"");
    IDatabaseConnection connection = DatabaseEnvironment.getInstance().getConnection();
    //        IDataSet dataSet = new XmlDataSet(new FileReader("dataSetTest.xml"));
    //        DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);

    //        String[] tableNames = connection.createDataSet().getTableNames();
    //        Arrays.sort(tableNames);
    //        FlatXmlDataSet.writeDtd(new FilteredDataSet(tableNames,
    //                connection.createDataSet()),
    //                new FileOutputStream("test.dtd"));
    //
    //
    Writer out = new FileWriter("test.xml");
    //        FlatXmlDataSet.write(connection.createDataSet(), out, "ISO-8859-1");
    FlatXmlDataSet.write(connection.createDataSet(), out);
    //        out.flush();
    //        out.close();

    //        ////////////////////////////////
    //        Document document = new Document(TestUtils.getFile("xml/flatXmlDataSetTest.xml"));
    //        DocType docType = document.getDocType();
    //        System.out.println(docType);
    //
    //        // display children of DocType
    //        for (Children decls = docType.getChildren(); decls.hasMoreElements();)
    //        {
    //            Child decl = decls.next();
    //            String type = decl.getClass().getName();
    //            System.out.println("decl = " + decl + ", class: " + type);
    //        }

    //        IDataSet dataSet = new FlatXmlDataSet(
    //                new FileInputStream("flatXmlDataSetTest.xml"));
    //        FlatDtdDataSet.write(new FlatXmlDataSet(
    //                TestUtils.getFileInputStream("xml/flatXmlDataSetTest.xml")),
    //                new FileOutputStream("src/dtd/flatXmlDataSetTest.dtd"));
  }
  /**
   * Verify that we can start the server.
   *
   * @throws Exception If there was an error.
   */
  @Test
  public void testRun() throws Exception {
    ReflectionUtils.setVariableValueInObject(mojo, "daemon", Boolean.FALSE);

    final Thread mojoThread =
        new Thread(
            new Runnable() {
              public void run() {
                try {
                  mojo.execute();
                } catch (final Exception e) {
                }
              }
            });
    mojoThread.start();
    try {
      Thread.sleep(5000);

      final Connection jdbcConnection = DriverManager.getConnection(getConnectionString());
      final IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
      IDataSet databaseDataSet = connection.createDataSet();
      assertNotNull(databaseDataSet.getTable(getTableName()));

      connection.close();
      jdbcConnection.close();
    } finally {
      final Timer timer = new Timer();
      timer.schedule(
          new TimerTask() {
            @Override
            public void run() {
              signalStop();
            }
          },
          5000L);
      mojoThread.join(15000L);
    }
  }
 private void verifyExpected(
     DbUnitTestContext testContext, Collection<ExpectedDatabase> annotations) throws Exception {
   if (testContext.getTestException() != null) {
     if (logger.isDebugEnabled()) {
       logger.debug(
           "Skipping @DatabaseTest expectation due to test exception "
               + testContext.getTestException().getClass());
     }
     return;
   }
   IDatabaseConnection connection = testContext.getConnection();
   IDataSet actualDataSet = connection.createDataSet();
   for (ExpectedDatabase annotation : annotations) {
     IDataSet expectedDataSet = loadDataset(testContext, annotation.value());
     if (expectedDataSet != null) {
       if (logger.isDebugEnabled()) {
         logger.debug("Veriftying @DatabaseTest expectation using " + annotation.value());
       }
       DatabaseAssertion assertion = annotation.assertionMode().getDatabaseAssertion();
       assertion.assertEquals(expectedDataSet, actualDataSet);
     }
   }
 }
Example #9
0
  private void expectDataSetFromDb(final Table table) throws DataSetException, SQLException {
    final IDataSet dataSetFromDb = dataSetFactory.create(toTableData(table));

    expectConnection();
    expect(connection.createDataSet()).andReturn(dataSetFromDb);
  }
Example #10
0
 private void dataSetFactoryFailsWith(final Exception expected) throws SQLException {
   final Table table = aTable(MyTable.class).withRowsBetween(0, 10);
   expectConnection();
   expect(connection.createDataSet()).andThrow(expected);
   doAssert(table);
 }