001
014
015 package com.liferay.portal.test.rule.callback;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncPrintWriter;
018 import com.liferay.portal.kernel.test.rule.callback.BaseTestCallback;
019 import com.liferay.portal.kernel.util.ReflectionUtil;
020 import com.liferay.portal.util.PropsValues;
021
022 import java.io.File;
023 import java.io.PrintWriter;
024
025 import java.sql.Connection;
026 import java.sql.DriverManager;
027 import java.sql.SQLException;
028 import java.sql.Statement;
029
030 import java.util.concurrent.CountDownLatch;
031 import java.util.concurrent.TimeUnit;
032
033 import org.hsqldb.server.Server;
034 import org.hsqldb.server.ServerConstants;
035
036 import org.junit.runner.Description;
037
038
042 public class HypersonicServerTestCallback
043 extends BaseTestCallback<Server, Object> {
044
045 public static final String DATABASE_URL_BASE =
046 "jdbc:hsqldb:hsql:
047
048 public HypersonicServerTestCallback(String databaseName) {
049 _databaseName = databaseName;
050 }
051
052 @Override
053 public void doAfterClass(Description description, Server server)
054 throws SQLException {
055
056 try (Connection connection = DriverManager.getConnection(
057 DATABASE_URL_BASE + _databaseName, "sa", "");
058 Statement statement = connection.createStatement()) {
059
060 statement.execute("SHUTDOWN COMPACT");
061 }
062
063 server.stop();
064 }
065
066 @Override
067 public Server doBeforeClass(Description description) throws Exception {
068 final CountDownLatch startCountDownLatch = new CountDownLatch(1);
069
070 Server server = new Server() {
071
072 @Override
073 public int stop() {
074 try (PrintWriter logPrintWriter = getLogWriter();
075 PrintWriter errPrintWriter = getErrWriter()) {
076
077 int state = super.stop();
078
079 if (!_shutdownCountDownLatch.await(1, TimeUnit.MINUTES)) {
080 throw new IllegalStateException(
081 "Unable to shut down Hypersonic " + _databaseName);
082 }
083
084 return state;
085 }
086 catch (InterruptedException ie) {
087 return ReflectionUtil.throwException(ie);
088 }
089 }
090
091 @Override
092 protected synchronized void setState(int state) {
093 super.setState(state);
094
095 if (state == ServerConstants.SERVER_STATE_ONLINE) {
096 startCountDownLatch.countDown();
097 }
098 else if (state == ServerConstants.SERVER_STATE_SHUTDOWN) {
099 _shutdownCountDownLatch.countDown();
100 }
101 }
102
103 private final CountDownLatch _shutdownCountDownLatch =
104 new CountDownLatch(1);
105
106 };
107
108 File hsqlHomeDir = new File(_HSQL_HOME);
109
110 hsqlHomeDir.mkdirs();
111
112 server.setErrWriter(
113 new UnsyncPrintWriter(
114 new File(hsqlHomeDir, _databaseName + ".err.log")));
115 server.setLogWriter(
116 new UnsyncPrintWriter(
117 new File(hsqlHomeDir, _databaseName + ".std.log")));
118
119 server.setDatabaseName(0, _databaseName);
120 server.setDatabasePath(0, _HSQL_HOME + _databaseName);
121
122 server.start();
123
124 if (!startCountDownLatch.await(1, TimeUnit.MINUTES)) {
125 throw new IllegalStateException(
126 "Unable to start up Hypersonic " + _databaseName);
127 }
128
129 return server;
130 }
131
132 private static final String _HSQL_HOME =
133 PropsValues.LIFERAY_HOME + "/data/hypersonic/";
134
135 private final String _databaseName;
136
137 }