001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.test.rule;
016    
017    import com.liferay.portal.kernel.test.rule.BaseTestRule;
018    import com.liferay.portal.kernel.test.rule.callback.BaseTestCallback;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.Props;
021    import com.liferay.portal.test.rule.callback.HypersonicServerTestCallback;
022    import com.liferay.portal.util.PropsImpl;
023    
024    import java.util.Arrays;
025    import java.util.Collections;
026    import java.util.List;
027    
028    import org.hsqldb.jdbc.JDBCDriver;
029    import org.hsqldb.server.Server;
030    
031    /**
032     * @author William Newbury
033     * @author Shuyang Zhou
034     */
035    public class HypersonicServerTestRule extends BaseTestRule<Server, Object> {
036    
037            public static final HypersonicServerTestRule INSTANCE;
038    
039            public HypersonicServerTestRule() {
040                    super(_getTestCallback());
041            }
042    
043            public List<String> getJdbcProperties() {
044                    if (_HYPERSONIC) {
045                            return Arrays.asList(
046                                    new String[] {
047                                            "portal:jdbc.default.url=" + _DATABASE_URL,
048                                            "portal:jdbc.default.username=sa",
049                                            "portal:jdbc.default.password="
050                                    });
051                    }
052    
053                    return Collections.emptyList();
054            }
055    
056            private static BaseTestCallback<Server, Object> _getTestCallback() {
057                    if (_HYPERSONIC) {
058                            return new HypersonicServerTestCallback(_DATABASE_NAME);
059                    }
060    
061                    return new BaseTestCallback<>();
062            }
063    
064            private static final String _DATABASE_NAME;
065    
066            private static final String _DATABASE_URL;
067    
068            private static final boolean _HYPERSONIC;
069    
070            static {
071                    Props props = new PropsImpl();
072    
073                    String className = props.get("jdbc.default.driverClassName");
074    
075                    _HYPERSONIC = className.equals(JDBCDriver.class.getName());
076    
077                    if (_HYPERSONIC) {
078                            String jdbcURL = props.get("jdbc.default.url");
079    
080                            int index = jdbcURL.lastIndexOf(CharPool.SLASH);
081    
082                            if (index < 0) {
083                                    throw new ExceptionInInitializerError(
084                                            "Invalid Hypersonic JDBC URL " + jdbcURL);
085                            }
086    
087                            String databaseName = jdbcURL.substring(index + 1);
088    
089                            index = databaseName.indexOf(CharPool.SEMICOLON);
090    
091                            if (index >= 0) {
092                                    databaseName = databaseName.substring(0, index);
093                            }
094    
095                            _DATABASE_NAME = databaseName;
096                            _DATABASE_URL =
097                                    HypersonicServerTestCallback.DATABASE_URL_BASE + _DATABASE_NAME;
098                    }
099                    else {
100                            _DATABASE_NAME = null;
101                            _DATABASE_URL = null;
102                    }
103    
104                    INSTANCE = new HypersonicServerTestRule();
105            }
106    
107    }