1   package com.sri.emo;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.FileOutputStream;
6   import java.io.InputStream;
7   import java.sql.Connection;
8   import java.sql.DriverManager;
9   import com.sri.emo.dbobj.NodeType;
10  import com.sri.emo.dbobj.Part;
11  import org.dbunit.database.DatabaseConnection;
12  import org.dbunit.database.IDatabaseConnection;
13  import org.dbunit.database.QueryDataSet;
14  import org.dbunit.dataset.xml.FlatXmlDataSet;
15  import org.dbunit.operation.DatabaseOperation;
16  
17  /***
18   * Extracts EMO test data to a particular file.
19   *
20   * Note to self: This is the proper test URL for performance
21   * http://localhost:8080/emo/AddNodeAction.do?NODE_ID=83&state=viewNode
22   * @author Michael Rimov
23   */
24  public class ExtractEmoTestData {
25      final String fileName;
26  
27      IDatabaseConnection connection;
28  
29      Connection jdbcConnection;
30  
31      Class driverClass;
32  
33      public ExtractEmoTestData(String exportFileName) throws Exception{
34          super();
35          fileName = exportFileName;
36      }
37  
38      public void initialize() throws Exception {
39          // database connection
40  //PADI MYSQL Config
41  //        driverClass = Class.forName("org.gjt.mm.mysql.Driver");
42  //        jdbcConnection = DriverManager.getConnection("jdbc:mysql://localhost/padi", "padi_admin", "padi_admin2");
43  //        connection = new DatabaseConnection(jdbcConnection);
44          //HSqlConfig
45          driverClass = Class.forName("org.hsqldb.jdbcDriver");
46          jdbcConnection = DriverManager.getConnection("jdbc:hsqldb:webapps/emo/WEB-INF/db/emo/emo", "sa", "");
47          connection = new DatabaseConnection(jdbcConnection);
48      }
49  
50      public void finish() throws Exception {
51          connection.close();
52          jdbcConnection.close();
53      }
54  
55  
56  
57      public void extract() throws Exception {
58          initialize();
59  
60          try {
61              // partial database export
62              QueryDataSet partialDataSet = new QueryDataSet(connection);
63              partialDataSet.addTable(NodeType.NODE_TYPE_TABLE_NAME);
64              partialDataSet.addTable("node");
65              partialDataSet.addTable(Part.PART_TYPE_TABLE_NAME);
66              partialDataSet.addTable("attribute");
67              partialDataSet.addTable("relation");
68              partialDataSet.addTable("relation_type");
69              partialDataSet.addTable("picklist");
70              partialDataSet.addTable("ROW_PERMISSIONS");
71              FlatXmlDataSet.write(partialDataSet, new FileOutputStream(fileName));
72          } finally {
73              finish();
74          }
75  
76      }
77  
78      public void importFile() throws Exception {
79          initialize();
80  
81          try {
82              File f = new File(fileName);
83              assert f != null;
84              assert f.exists() : "File " + fileName + " doesn't exist";
85              InputStream fis = new FileInputStream(fileName);
86              assert fis != null;
87  
88              FlatXmlDataSet dataSet = new FlatXmlDataSet(fis,false);
89              DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
90          } finally {
91              finish();
92          }
93      }
94  
95      public static void main(String[] args) throws Exception {
96          final String USAGE = "Usage: java -jar ExtractTestData.jar <filename> [import]";
97          if (args.length == 0) {
98              System.out.println(USAGE);
99              return;
100         }
101 
102         ExtractEmoTestData extractemotestdata = new ExtractEmoTestData(args[0]);
103 
104 
105         if (args.length == 2) {
106             if (!"import".equalsIgnoreCase(args[1])) {
107                 System.out.println(USAGE);
108                 return;
109             }
110             extractemotestdata.importFile();
111         } else {
112             extractemotestdata.extract();
113         }
114 
115     }
116 }