View Javadoc
1   package fr.ifremer.quadrige2.core.dao;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 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 com.google.common.io.CharStreams;
27  import fr.ifremer.quadrige2.core.dao.technical.Daos;
28  import org.apache.commons.io.IOUtils;
29  import org.dbunit.DatabaseUnitException;
30  import org.dbunit.database.DatabaseConnection;
31  import org.dbunit.database.IDatabaseConnection;
32  import org.dbunit.dataset.IDataSet;
33  import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
34  import org.dbunit.ext.hsqldb.HsqldbDataTypeFactory;
35  import org.dbunit.operation.DatabaseOperation;
36  
37  import java.io.*;
38  import java.sql.Connection;
39  import java.sql.SQLException;
40  import java.util.Properties;
41  
42  /**
43   * Created by blavenie on 16/12/15.
44   */
45  public class DBUnitUtils {
46  
47  	public static void loadXmlFile(Connection jdbcConnection, File file, boolean cleanBeforeInsert)
48  			throws ClassNotFoundException, IOException, SQLException, DatabaseUnitException {
49  
50  		IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
51  		connection.getConfig().setProperty("http://www.dbunit.org/properties/datatypeFactory", new HsqldbDataTypeFactory());
52  		IDataSet dataSet = new FlatXmlDataSetBuilder().setColumnSensing(true).build(file);
53  		if (cleanBeforeInsert) {
54  			DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
55  		} else {
56  			DatabaseOperation.INSERT.execute(connection, dataSet);
57  		}
58  	}
59  
60  	public static void loadXmlClasspathFile(Properties connectionProperties, ClassLoader classLoader, String classpathFile, boolean cleanBeforeInsert)
61  			throws ClassNotFoundException, IOException, SQLException, DatabaseUnitException {
62  
63  		Connection jdbcConnection = null;
64  		InputStream is = null;
65  		try {
66  			jdbcConnection = Daos.createConnection(connectionProperties);
67  
68  			is = classLoader.getResourceAsStream(classpathFile);
69  
70  			IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
71  			connection.getConfig().setProperty("http://www.dbunit.org/properties/datatypeFactory", new HsqldbDataTypeFactory());
72  			IDataSet dataSet = new FlatXmlDataSetBuilder().setColumnSensing(true).build(is);
73  			if (cleanBeforeInsert) {
74  				DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
75  			} else {
76  				DatabaseOperation.INSERT.execute(connection, dataSet);
77  			}
78  
79  			Daos.sqlUpdate(jdbcConnection, "COMMIT");
80  
81  		} finally {
82  			Daos.closeSilently(jdbcConnection);
83  			IOUtils.closeQuietly(is);
84  		}
85  	}
86  }