AbstractTransactionalDataSourceSpringContextTests.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 2k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright 2002-2004 the original author or authors.
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.springframework.test;
  17. import javax.sql.DataSource;
  18. import org.springframework.jdbc.core.JdbcTemplate;
  19. /**
  20.  * Expects a DataSource, exposes a JdbcTemplate, and provides an easy
  21.  * way to delete from the database in a new transaction.
  22.  * @author Rod Johnson
  23.  * @since 1.1.1
  24.  */
  25. public abstract class AbstractTransactionalDataSourceSpringContextTests
  26.     extends AbstractTransactionalSpringContextTests {
  27. protected JdbcTemplate jdbcTemplate;
  28. private boolean zappedTables;
  29. public void setDataSource(DataSource dataSource) {
  30. // TODO what if you want to use a JdbcTemplate by preference,
  31. // for a native extractor?
  32. this.jdbcTemplate = new JdbcTemplate(dataSource);
  33. }
  34. /**
  35.  * Convenient method to delete all rows from these tables.
  36.  */
  37. protected void deleteFromTables(String[] names) {
  38. for (int i = 0; i < names.length; i++) {
  39. logger.info("Deleted " +
  40.     this.jdbcTemplate.update("DELETE FROM " + names[i]) + " rows from table " + names[i]);
  41. }
  42. this.zappedTables = true;
  43. }
  44. protected final void setComplete() {
  45. if (this.zappedTables) {
  46. throw new IllegalStateException("Cannot set complete after deleting tables");
  47. }
  48. super.setComplete();
  49. }
  50. }