@BeforeClass
  public static void setUpBeforeClass() throws Exception {
    // initialize Log4j
    Properties props = new Properties();
    props.load(
        (new ClassPathResource("confs/log4j-exercise.properties", CompanyService.class))
            .getInputStream());
    org.apache.log4j.PropertyConfigurator.configure(props);

    // load springframework context
    applContext =
        new ClassPathXmlApplicationContext(
            "confs/spring-exercise-hibernate.xml", CompanyService.class);

    // create DbUnit core components
    IDatabaseConnection conn =
        new DatabaseDataSourceConnection((DataSource) applContext.getBean("dataSource"));
    dataSet =
        new FlatXmlDataSet(
            (new ClassPathResource(
                    "thirdstage/exercise/spring/transaction/case2/test/dataset/company-building-factory-seed-01.xml"))
                .getFile());

    // clean target tables and load test data
    try {
      DatabaseOperation.CLEAN_INSERT.execute(conn, dataSet);
    } finally {
      conn.close();
    }
  }
  protected final void setupData(InputStream... dataSetStream) {
    try {
      IDataSet[] dataSets = new IDataSet[dataSetStream.length];
      for (int i = 0; i < dataSetStream.length; i++) {
        ReplacementDataSet dataSet = new ReplacementDataSet(new FlatXmlDataSet(dataSetStream[i]));
        dataSet.addReplacementObject("[null]", null);
        dataSets[i] = dataSet;
      }
      CompositeDataSet compositeDataSet = new CompositeDataSet(dataSets);

      databaseTester.setDataSet(compositeDataSet);
      connection = databaseTester.getConnection();
      connection
          .getConnection()
          .prepareStatement("set referential_integrity FALSE")
          .execute(); // HSQL DB
      DatabaseConfig config = connection.getConfig();
      config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());

      DatabaseOperation.CLEAN_INSERT.execute(connection, databaseTester.getDataSet());

      connection
          .getConnection()
          .prepareStatement("set referential_integrity TRUE")
          .execute(); // HSQL DB
    } catch (Exception e) {
      throw translateException("Could not setup DBUnit data", e);
    }
  }
  /**
   * Import data into database.
   *
   * @param connection database connection
   * @throws Exception throws various exceptions
   */
  public void importData(Connection connection) throws Exception {
    InputStream inStream;
    IDataSet dataSet;

    DatabaseConnection dbconnection = new DatabaseConnection(connection);

    // initialize the data set
    DatabaseConfig config = dbconnection.getConfig();
    config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());

    inStream = this.getClass().getResourceAsStream(getDataFile());

    try {

      // put check here to ensure, finally is always executed
      if (inStream == null) {
        throw new FileNotFoundException("Datafile: " + getDataFile() + " not found");
      }

      // if no modified data set is available the test data set is loaded
      dataSet = new FlatXmlDataSet(inStream);

      DatabaseOperation.CLEAN_INSERT.execute(dbconnection, dataSet);
    } finally {
      dbconnection.close();
      connection.close();
    }
  }
  private void populateData() throws Exception {

    // skip db stuff of
    if (!isLocalTestEnvironment()) {
      // Once we've got the tables created populate them with data - clean insert will delete all
      // data first
      DatabaseDataSourceConnection databaseDataSourceConnection =
          new DatabaseDataSourceConnection(dataSource);

      // Set the database factory as in http://www.dbunit.org/faq.html#DefaultDataTypeFactory
      DatabaseConfig config = databaseDataSourceConnection.getConfig();

      config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
      config.setProperty(DatabaseConfig.PROPERTY_ESCAPE_PATTERN, "`?`");

      // Construct dataset
      XmlDataSet dataSet = new XmlDataSet(readFileFromClasspath("dataset.xml"));

      // Insert, cleanly (remove everything first)
      DatabaseOperation.CLEAN_INSERT.execute(databaseDataSourceConnection, dataSet);

      // Have to close the database connection
      databaseDataSourceConnection.close();
    }
  }
示例#5
0
 private void populateDataBase(String initialContents) throws SQLException, DatabaseUnitException {
   createFreshConnection();
   try {
     IDataSet template = loadDataSetFromClassPath(initialContents);
     DatabaseOperation.CLEAN_INSERT.execute(connection, template);
   } finally {
     disconnect();
   }
 }
 protected void runTemplateTest(TemplateHandler worker) throws Exception {
   IDataSet setupDataSet = getReplacedDataSet(worker.getSetupDataSet(), worker.getId());
   DatabaseOperation.CLEAN_INSERT.execute(dbunitConnection, setupDataSet);
   worker.doIt();
   String comparisonDataSetName = worker.getAssertDataSet();
   if (comparisonDataSetName != null) {
     IDataSet expectedDataSet = getReplacedDataSet(comparisonDataSetName, worker.getId());
     IDataSet actualDataSet = dbunitConnection.createDataSet();
     Assertion.assertEquals(expectedDataSet, actualDataSet);
   }
 }
 public void fill() throws Exception {
   Connection connection = null;
   try {
     connection = DataSourceUtils.getConnection(dataSource);
     IDatabaseConnection dbUnitConnection = new DatabaseConnection(connection);
     FullXmlDataFileLoader loader = new FullXmlDataFileLoader();
     IDataSet xmlDataSet = loader.load(xmlFilename);
     DatabaseOperation.CLEAN_INSERT.execute(dbUnitConnection, xmlDataSet);
   } finally {
     DataSourceUtils.releaseConnection(connection, dataSource);
   }
 }
 @Before
 public void generalSetUp() throws Exception {
   ReplacementDataSet dataSet =
       new ReplacementDataSet(
           new FlatXmlDataSetBuilder()
               .build(
                   PdcAxisValueDAOTest.class
                       .getClassLoader()
                       .getResourceAsStream("com/silverpeas/pdc/dao/pdc-dataset.xml")));
   dataSet.addReplacementObject("[NULL]", null);
   IDatabaseConnection connection = new DatabaseConnection(dataSource.getConnection());
   DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
 }
示例#9
0
  public static void executeDataFile(String path, Connection conn) {

    try {

      IDatabaseConnection iConn = new DatabaseConnection(conn);

      IDataSet dataSet =
          new FlatXmlDataSetBuilder().build(JdbcUtilTest.class.getResourceAsStream(path));

      DatabaseOperation.CLEAN_INSERT.execute(iConn, dataSet);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  protected void executeDataFile(String path) {

    try {
      IDataSet dataSet =
          new FlatXmlDataSet(this.getClass().getClassLoader().getResourceAsStream(path));

      Assert.assertNotNull(connection);
      Assert.assertNotNull(dataSet);

      DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  @Before
  public void setUp() throws Exception {
    DatabaseOperation.CLEAN_INSERT.execute(getConnection(), getDataSet());
    loginServiceImpl = new LoginServiceImpl();
    hibernateTemplate = new HibernateTemplate<Customer>();

    directFieldAccessor = new DirectFieldAccessor(loginServiceImpl);
    directFieldAccessor.setPropertyValue("hibernateTemplate", hibernateTemplate);

    directFieldAccessor = new DirectFieldAccessor(hibernateTemplate);
    directFieldAccessor.setPropertyValue("session", hibernateSession.openSession());

    username = "******";
    password = "******";
  }
 protected void complexSetUp() {
   IDatabaseConnection connection = null;
   try {
     connection = getConnection();
     DatabaseOperation.DELETE_ALL.execute(connection, getDataSet());
     DatabaseOperation.CLEAN_INSERT.execute(connection, getComplexDataSet());
   } catch (Exception ex) {
     ex.printStackTrace();
   } finally {
     if (connection != null) {
       try {
         connection.getConnection().close();
       } catch (SQLException e) {
         e.printStackTrace();
       }
     }
   }
 }
 @Override
 protected void onSetUp() {
   registerDatasource();
   IDatabaseConnection connection = null;
   try {
     connection = getConnection();
     DatabaseOperation.CLEAN_INSERT.execute(connection, getDataSet());
   } catch (Exception ex) {
     ex.printStackTrace();
   } finally {
     if (connection != null) {
       try {
         connection.getConnection().close();
       } catch (SQLException e) {
         e.printStackTrace();
       }
     }
   }
 }
 @Before
 public void setUpDatabase() throws Exception {
   System.getProperties()
       .put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
   IDatabaseConnection connection = null;
   try {
     connection = new DatabaseConnection(datasource.getConnection());
     DatabaseOperation.CLEAN_INSERT.execute(connection, getDataSet());
   } catch (Exception ex) {
     throw ex;
   } finally {
     if (connection != null) {
       try {
         connection.getConnection().close();
       } catch (SQLException e) {
         throw e;
       }
     }
   }
 }
  @Override
  public void beforeTestMethod(TestContext testContext) {
    DataSetLocation annotation = testContext.getTestMethod().getAnnotation(DataSetLocation.class);
    if (annotation == null || annotation.value() == null) return;
    String dataSetLocation = annotation.value();

    Resource dataSetResource = testContext.getApplicationContext().getResource(dataSetLocation);
    if (!dataSetResource.exists()) {
      log.error("data set " + dataSetLocation + " does not exist");
      return;
    }
    try {
      IDataSet dataSet = new FlatXmlDataSetBuilder().build(dataSetResource.getInputStream());
      IDatabaseConnection dbUnit = initialiseDbUnitConnection(testContext.getApplicationContext());
      DatabaseOperation.CLEAN_INSERT.execute(dbUnit, dataSet);

    } catch (Exception e) {
      log.error("dataSet import error for {}", dataSetLocation, e);
    }
  }
 @Before
 public void onSetUp() {
   super.onSetUp();
   Mailbox.clearAll();
   IDatabaseConnection connection = null;
   try {
     connection = getConnection();
     DatabaseOperation.DELETE_ALL.execute(connection, getDataSet());
     DatabaseOperation.CLEAN_INSERT.execute(connection, getDataSet());
   } catch (Exception ex) {
     ex.printStackTrace();
   } finally {
     if (connection != null) {
       try {
         connection.getConnection().close();
       } catch (SQLException e) {
         e.printStackTrace();
       }
     }
   }
   ServicesFactory.getInstance().setApplicationContext(applicationContext);
 }
 @Before
 public void generalSetUp() throws Exception {
   InitialContext ic = new InitialContext();
   ic.rebind(JNDINames.ATTACHMENT_DATASOURCE, dataSource);
   IDatabaseConnection connection = new DatabaseConnection(dataSource.getConnection());
   DatabaseOperation.DELETE_ALL.execute(connection, dataSet);
   DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
   DBUtil.getInstanceForTest(dataSource.getConnection());
   if (!registred) {
     Reader reader = null;
     try {
       reader =
           new InputStreamReader(
               SimpleFileAccessControlTest.class
                   .getClassLoader()
                   .getResourceAsStream("silverpeas-jcr.txt"),
               CharEncoding.UTF_8);
       SilverpeasRegister.registerNodeTypes(reader);
     } finally {
       IOUtils.closeQuietly(reader);
     }
     registred = true;
     DBUtil.getInstanceForTest(dataSource.getConnection());
   }
   Session session = null;
   try {
     session = getRepository().login(new SilverpeasSystemCredentials());
     if (!session.getRootNode().hasNode(instanceId)) {
       session.getRootNode().addNode(instanceId, NT_FOLDER);
     }
     session.save();
   } finally {
     if (session != null) {
       session.logout();
     }
   }
 }
 protected void onSetUp() {
   super.onSetUp();
   HttpUnitOptions.setExceptionsThrownOnErrorStatus(true);
   HttpUnitOptions.setExceptionsThrownOnScriptError(false);
   HttpUnitOptions.setScriptingEnabled(true);
   ClientProperties.getDefaultProperties().setAcceptCookies(true);
   ClientProperties.getDefaultProperties().setAutoRedirect(true);
   IDatabaseConnection connection = null;
   try {
     connection = getConnection();
     DatabaseOperation.DELETE_ALL.execute(connection, getDataSet());
     DatabaseOperation.CLEAN_INSERT.execute(connection, getDataSet());
   } catch (Exception ex) {
     ex.printStackTrace();
   } finally {
     if (connection != null) {
       try {
         connection.getConnection().close();
       } catch (SQLException e) {
         e.printStackTrace();
       }
     }
   }
 }
  @Before
  public void initTest() throws Exception {
    // Load Spring application context and get beans
    if (context == null) {
      context = new ClassPathXmlApplicationContext("/spring-domain.xml");
    }

    dataSource = (DataSource) context.getBean("jpaDataSource");
    dao =
        (SQLDomainRepository)
            context.getAutowireCapableBeanFactory().getBean("sqlInternalDomainRepository");

    // Populate database
    ReplacementDataSet dataSet =
        new ReplacementDataSet(
            new FlatXmlDataSet(
                SQLInternalDomainRepositoryTest.class
                    .getClassLoader()
                    .getResourceAsStream(
                        "org/silverpeas/admin/domain/repository/domain-dataset.xml")));
    dataSet.addReplacementObject("[NULL]", null);
    IDatabaseConnection connection = new DatabaseConnection(dataSource.getConnection());
    DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
  }
 @Before
 public void setUp() throws Exception {
   DatabaseOperation.CLEAN_INSERT.execute(this.getDatabaseTester().getConnection(), getBlank());
   DatabaseOperation.CLEAN_INSERT.execute(this.getDatabaseTester().getConnection(), getDataSet());
   DatabaseOperation.INSERT.execute(this.getDatabaseTester().getConnection(), getStudent());
 }
 @After
 public void tearDown() throws Exception {
   DatabaseOperation.DELETE_ALL.execute(this.getDatabaseTester().getConnection(), getStudent());
   DatabaseOperation.DELETE_ALL.execute(this.getDatabaseTester().getConnection(), getDataSet());
   DatabaseOperation.CLEAN_INSERT.execute(this.getDatabaseTester().getConnection(), getBlank());
 }
  /**
   * 测试启动前方法.
   *
   * @throws java.lang.Exception 普通异常.
   */
  @BeforeClass
  public static void setUpBeforeClass() throws Exception {
    // 获取原数据库内容
    IDataSet backupDataset = TestDBHelper.backupDataBase();
    // 构建测试用的数据
    // 指定测试用到的表
    DefaultTable userTable =
        new DefaultTable(
            "t_user", backupDataset.getTable("t_user").getTableMetaData().getColumns());
    DefaultTable roleTable =
        new DefaultTable(
            "t_role", backupDataset.getTable("t_role").getTableMetaData().getColumns());
    DefaultTable menuItemTable =
        new DefaultTable(
            "t_menuitem", backupDataset.getTable("t_menuitem").getTableMetaData().getColumns());
    DefaultTable rolemenuitemmapTable =
        new DefaultTable(
            "t_rolemenuitemmap",
            backupDataset.getTable("t_rolemenuitemmap").getTableMetaData().getColumns());

    // 写入数据
    Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-01-12 23:30:20");
    Object[] userData1 = {
      Long.valueOf("1"),
      "user1",
      "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
      date,
      Long.valueOf("1"),
      null,
      null,
      Byte.valueOf("0"),
      Byte.valueOf("0"),
      Long.valueOf("0")
    };
    userTable.addRow(userData1);

    Object[] roleData1 = {
      Long.valueOf("1"),
      "访客",
      date,
      Long.valueOf("1"),
      null,
      null,
      Byte.valueOf("0"),
      Byte.valueOf("0"),
      Long.valueOf("0")
    };
    roleTable.addRow(roleData1);
    Object[] roleData2 = {
      Long.valueOf("2"),
      "超级管理员",
      date,
      Long.valueOf("1"),
      null,
      null,
      Byte.valueOf("0"),
      Byte.valueOf("0"),
      Long.valueOf("0")
    };
    roleTable.addRow(roleData2);

    Object[] roleData4 = {
      Long.valueOf("4"),
      "role4",
      date,
      Long.valueOf("1"),
      null,
      null,
      Byte.valueOf("0"),
      Byte.valueOf("0"),
      Long.valueOf("0")
    };
    roleTable.addRow(roleData4);
    // testEditRole使用
    Object[] roleData5 = {
      Long.valueOf("5"),
      "role5",
      date,
      Long.valueOf("1"),
      null,
      null,
      Byte.valueOf("0"),
      Byte.valueOf("0"),
      Long.valueOf("0")
    };
    roleTable.addRow(roleData5);
    // testPageRole使用
    Object[] roleData6 = {
      Long.valueOf("6"),
      "page",
      date,
      Long.valueOf("1"),
      null,
      null,
      Byte.valueOf("0"),
      Byte.valueOf("0"),
      Long.valueOf("0")
    };
    roleTable.addRow(roleData6);

    Object[] menuItemData1 = {
      1,
      "MenuName",
      "menuItemName1",
      "icon1",
      1,
      "sfds",
      "sdf",
      "sdf",
      0,
      "2015-06-15 15:15:27",
      "1",
      "2015-06-15 15:15:27",
      null,
      '0',
      '0',
      0
    };
    menuItemTable.addRow(menuItemData1);
    Object[] menuItemData2 = {
      2,
      "MenuName",
      "menuItemName2",
      "icon1",
      1,
      "sfds",
      "sdf",
      "sdf",
      0,
      "2015-06-15 15:15:27",
      "1",
      "2015-06-15 15:15:27",
      null,
      '0',
      '0',
      0
    };
    menuItemTable.addRow(menuItemData2);
    Object[] menuItemData3 = {
      Long.valueOf("3"),
      "MenuName",
      "menuItemName3",
      "icon1",
      1,
      "sfds",
      "sdf",
      "sdf",
      0,
      "2015-06-15 15:15:27",
      "1",
      "2015-06-15 15:15:27",
      null,
      '0',
      '0',
      0
    };
    menuItemTable.addRow(menuItemData3);
    Object[] menuItemData4 = {
      Long.valueOf("4"),
      "MenuName",
      "menuItemName4",
      "icon1",
      1,
      "sfds",
      "sdf",
      "sdf",
      0,
      "2015-06-15 15:15:27",
      "1",
      "2015-06-15 15:15:27",
      null,
      '0',
      '0',
      0
    };
    menuItemTable.addRow(menuItemData4);

    Object[] rolemenuitemmapData1 = {
      1, 1, 1, "2015-06-15 15:16:40", 1, "2015-06-15 15:15:27", null, 0, 0, 0
    };
    rolemenuitemmapTable.addRow(rolemenuitemmapData1);
    Object[] rolemenuitemmapData2 = {
      2, 2, 1, "2015-06-15 15:16:40", 1, "2015-06-15 15:15:27", null, 0, 0, 0
    };
    rolemenuitemmapTable.addRow(rolemenuitemmapData2);
    Object[] rolemenuitemmapData3 = {
      Long.valueOf("3"), 1, 2, "2015-06-15 15:16:40", 1, "2015-06-15 15:15:27", 0, 0, 0, null
    };
    rolemenuitemmapTable.addRow(rolemenuitemmapData3);
    Object[] rolemenuitemmapData4 = {
      Long.valueOf("4"),
      2,
      Long.valueOf("3"),
      "2015-06-15 15:16:40",
      1,
      "2015-06-15 15:15:27",
      null,
      0,
      0,
      0
    };
    rolemenuitemmapTable.addRow(rolemenuitemmapData4);
    Object[] rolemenuitemmapData5 = {
      Long.valueOf("5"),
      2,
      Long.valueOf("4"),
      "2015-06-15 15:16:40",
      1,
      "2015-06-15 15:15:27",
      null,
      0,
      0,
      0
    };
    rolemenuitemmapTable.addRow(rolemenuitemmapData5);

    // 组合表
    DefaultTable[] tables = {userTable, roleTable, menuItemTable, rolemenuitemmapTable};
    // 创建测试数据库的引用以准备写入数据库中
    IDataSet tempDataSet = new DefaultDataSet(tables);
    // 写入数据库
    DatabaseOperation.CLEAN_INSERT.execute(TestDBHelper.getConn(), tempDataSet);
  }
 @Before
 public void setUp() throws Exception {
   // Clean the data from previous test and insert new data test.
   DatabaseOperation.CLEAN_INSERT.execute(dbUnitConnection, dataset);
 }