Created
December 6, 2013 14:24
-
-
Save deckerego/7825342 to your computer and use it in GitHub Desktop.
DBUnit is oooooooooooooooold and hasn't been updated in forever, but still quasi-useful. It never had an official way of loading spreadsheets based on the absolute path of a flie name; paths were always relative to its own class context. Snippets load a spreadsheet using DBUnit specified by an absolute file path, and then demonstrate what a setu…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static IDatabaseTester databaseTester; | |
@Resource | |
protected ComboPooledDataSource dataSource; | |
@Before //You can use @BeforeClass if dataSource isn't loaded by dependency injection | |
public void setupDatabase() throws Exception { | |
if(databaseTester == null) { //Only load once, this can be removed if using @BeforeClass | |
databaseTester = new DataSourceDatabaseTester(dataSource); | |
IDataSet dataSet = new XlsDataFileLoader().load("TestDAO-data.xls"); | |
databaseTester.setDataSet(dataSet); | |
databaseTester.onSetup(); | |
} | |
} | |
@AfterClass | |
public static void shutdownDatabase() throws Exception { | |
databaseTester.onTearDown(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class XlsDataFileLoader extends org.dbunit.util.fileloader.XlsDataFileLoader { | |
public IDataSet load(String path) throws DatabaseUnitRuntimeException { | |
URL url = this.getClass().getResource(path); | |
if (url == null) | |
throw new DatabaseUnitRuntimeException("Could not find file " + path); | |
IDataSet dataSet = new DefaultDataSet(); | |
try { | |
dataSet = loadDataSet(url); | |
dataSet = processReplacementTokens(dataSet); | |
} catch (DataSetException ex) { | |
throw new DatabaseUnitRuntimeException(String.format("Error loading data from %s: %s", path, ex.getLocalizedMessage()), ex); | |
} catch (IOException ex) { | |
throw new DatabaseUnitRuntimeException("Error reading " + path, ex); | |
} | |
return dataSet; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment