1 package atg.test.util;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.sql.SQLException;
6 import java.sql.Statement;
7 import java.util.Map;
8 import java.util.Properties;
9
10 import org.apache.commons.dbcp.BasicDataSource;
11 import org.apache.log4j.Logger;
12
13 /***
14 * This class is a merger of atg.test.util.DBUtils and
15 * atg.adapter.gsa.GSATestUtils. The result will hopefully be a class that just
16 * has the bare minimums needed for testing against an existing and/or in-memory
17 * database.
18 *
19 * <p>
20 * TODO: re-enable versioned repositories
21 * </p>
22 *
23 * @author robert
24 *
25 */
26 public class RepositoryManager {
27
28 private static Logger log = Logger.getLogger(RepositoryManager.class);
29
30 private boolean isDefaultInMemoryDb;
31
32 private final BasicDataSource dataSource = new BasicDataSource();
33
34 /***
35 *
36 * @param configRoot
37 * @param repositoryPath
38 * The the repository to be tested, specified as nucleus component
39 * path.
40 * @param settings
41 *
42 * A {@link Properties} instance with the following values (in this example
43 * the properties are geared towards an mysql database):
44 *
45 * <pre>
46 * final Properties properties = new Properties();
47 * properties.put("driver", "com.mysql.jdbc.Driver");
48 * properties.put("url", "jdbc:mysql://localhost:3306/someDb");
49 * properties.put("user", "someUserName");
50 * properties.put("password", "somePassword");
51 * </pre>
52 *
53 * @param dropTables
54 * If <code>true</code> then existing tables will be dropped and
55 * re-created, if set to <code>false</code> the existing tables
56 * will be used.
57 * @param isDebug
58 * Enables or disables debugging.
59 * @param definitionFiles
60 * One or more needed repository definition files.
61 * @throws SQLException
62 * @throws IOException
63 */
64 public void initializeMinimalRepositoryConfiguration(File configRoot,
65 String repositoryPath, Map<String, String> settings,
66 final boolean dropTables, final boolean isDebug,
67 String... definitionFiles) throws SQLException, IOException {
68
69 dataSource.setDriverClassName(settings.get("driver"));
70 dataSource.setUsername(settings.get("user"));
71 dataSource.setPassword(settings.get("password"));
72 dataSource.setUrl(settings.get("url"));
73
74 log.info(String.format("Connected to '%s' using driver '%s'", dataSource
75 .getUrl(), dataSource.getDriverClassName()));
76
77 if (dropTables) {
78 createIdGeneratorTables();
79 }
80 else {
81 log.info("Existing tables will be used.");
82 }
83
84 isDefaultInMemoryDb = settings.get("url")==null?false:
85 settings.get("url").contains("jdbc:hsqldb:mem:testDb");
86
87 }
88
89 /***
90 *
91 * @throws SQLException
92 */
93 public void shutdownInMemoryDbAndCloseConnections() throws SQLException {
94 if (isDefaultInMemoryDb) {
95 dataSource.getConnection().createStatement().execute("SHUTDOWN");
96 }
97 dataSource.close();
98 }
99
100 /***
101 *
102 * @throws SQLException
103 */
104 private void createIdGeneratorTables() throws SQLException {
105
106 log.info("Re-creating (drop and create) DAS_ID_GENERATOR");
107
108 final Statement statement = dataSource.getConnection().createStatement();
109 try {
110 statement.executeUpdate("DROP TABLE DAS_ID_GENERATOR");
111 }
112 catch (SQLException e) {
113
114 }
115
116 statement
117 .executeUpdate("CREATE TABLE DAS_ID_GENERATOR(ID_SPACE_NAME VARCHAR(60) NOT NULL, "
118 + "SEED NUMERIC(19, 0) NOT NULL, BATCH_SIZE INTEGER NOT NULL, "
119 + "PREFIX VARCHAR(10) DEFAULT NULL, SUFFIX VARCHAR(10) DEFAULT NULL, "
120 + "PRIMARY KEY(ID_SPACE_NAME))");
121 statement.close();
122 }
123
124 }