View Javadoc
1   package fr.ifremer.quadrige3.core.dao;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Client Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import fr.ifremer.quadrige3.core.dao.technical.Daos;
27  import org.apache.commons.io.IOUtils;
28  import org.dbunit.DatabaseUnitException;
29  import org.dbunit.database.DatabaseConnection;
30  import org.dbunit.database.IDatabaseConnection;
31  import org.dbunit.dataset.IDataSet;
32  import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
33  import org.dbunit.ext.hsqldb.HsqldbDataTypeFactory;
34  import org.dbunit.operation.DatabaseOperation;
35  
36  import java.io.File;
37  import java.io.IOException;
38  import java.io.InputStream;
39  import java.sql.Connection;
40  import java.sql.SQLException;
41  import java.util.Properties;
42  
43  /**
44   * Created by blavenie on 16/12/15.
45   */
46  public class DBUnitUtils {
47  
48  	public static void loadXmlFile(Connection jdbcConnection, File file, boolean cleanBeforeInsert)
49  			throws IOException, SQLException, DatabaseUnitException {
50  
51  		IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
52  		connection.getConfig().setProperty("http://www.dbunit.org/properties/datatypeFactory", new HsqldbDataTypeFactory());
53  		IDataSet dataSet = new FlatXmlDataSetBuilder().setColumnSensing(true).build(file);
54  		if (cleanBeforeInsert) {
55  			DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
56  		} else {
57  			DatabaseOperation.INSERT.execute(connection, dataSet);
58  		}
59  	}
60  
61  	public static void loadXmlClasspathFile(Properties connectionProperties, ClassLoader classLoader, String classpathFile, boolean cleanBeforeInsert)
62  			throws SQLException, DatabaseUnitException {
63  
64  		Connection jdbcConnection = null;
65  		InputStream is = null;
66  		try {
67  			jdbcConnection = Daos.createConnection(connectionProperties);
68  
69  			is = classLoader.getResourceAsStream(classpathFile);
70  
71  			IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
72  			connection.getConfig().setProperty("http://www.dbunit.org/properties/datatypeFactory", new HsqldbDataTypeFactory());
73  			IDataSet dataSet = new FlatXmlDataSetBuilder().setColumnSensing(true).build(is);
74  			if (cleanBeforeInsert) {
75  				DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
76  			} else {
77  				DatabaseOperation.INSERT.execute(connection, dataSet);
78  			}
79  
80  			Daos.sqlUpdate(jdbcConnection, "COMMIT");
81  
82  		} finally {
83  			Daos.closeSilently(jdbcConnection);
84  			IOUtils.closeQuietly(is);
85  		}
86  	}
87  }