View Javadoc
1   package fr.ifremer.dali;
2   
3   /*
4    * #%L
5    * Dali :: Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 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  
27  import fr.ifremer.common.synchro.dao.Daos;
28  import fr.ifremer.quadrige3.core.config.QuadrigeConfiguration;
29  import fr.ifremer.dali.dao.DaliDatabaseResource;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.dbunit.DatabaseUnitException;
33  import org.dbunit.database.DatabaseConnection;
34  import org.dbunit.database.DatabaseSequenceFilter;
35  import org.dbunit.database.IDatabaseConnection;
36  import org.dbunit.dataset.FilteredDataSet;
37  import org.dbunit.dataset.IDataSet;
38  import org.dbunit.dataset.filter.ITableFilter;
39  import org.dbunit.dataset.xml.FlatXmlDataSet;
40  import org.dbunit.ext.hsqldb.HsqldbDataTypeFactory;
41  import org.junit.Assert;
42  import org.junit.ClassRule;
43  import org.junit.Test;
44  
45  import java.io.File;
46  import java.io.FileOutputStream;
47  import java.io.IOException;
48  import java.sql.Connection;
49  import java.sql.SQLException;
50  
51  /**
52   *
53   * Use this test to export configured database in XML format
54   *
55   * @author Ludovic
56   */
57  public class ExportDbTest {
58  
59      private static final Log log = LogFactory.getLog(ExportDbTest.class);
60  
61      @ClassRule
62      public static final DaliDatabaseResource dbResource = DaliDatabaseResource.readDb();
63  
64      @Test
65      public void fullDatabaseExport() {
66          File file = new File("target", "dbDump" + System.currentTimeMillis() + ".xml");
67          log.info("Exporting DB data... [" + file.getAbsolutePath() + "]");
68  
69          try {
70              file.createNewFile();
71              Connection jdbcConnection = Daos.createConnection(QuadrigeConfiguration.getInstance().getConnectionProperties());
72              IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
73              connection.getConfig().setProperty("http://www.dbunit.org/properties/datatypeFactory", new HsqldbDataTypeFactory());
74              ITableFilter filter = new DatabaseSequenceFilter(connection);
75              IDataSet dataset = new FilteredDataSet(filter, connection.createDataSet());
76              FlatXmlDataSet.write(dataset, new FileOutputStream(file));
77          } catch (DatabaseUnitException | IOException | SQLException ex) {
78              log.error(ex.getLocalizedMessage(), ex);
79              Assert.fail("Export Failed");
80          }
81  
82          log.info("DB data successfully exported to [" + file.getAbsolutePath() + "]");
83      }
84  
85  }