001/*
002 * Copyright (c) 2006-2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Florent Guillaume
011 */
012package org.nuxeo.ecm.core.test;
013
014import org.nuxeo.ecm.core.event.EventService;
015import org.nuxeo.ecm.core.storage.sql.DatabaseDB2;
016import org.nuxeo.ecm.core.storage.sql.DatabaseDerby;
017import org.nuxeo.ecm.core.storage.sql.DatabaseH2;
018import org.nuxeo.ecm.core.storage.sql.DatabaseHelper;
019import org.nuxeo.ecm.core.storage.sql.DatabaseMySQL;
020import org.nuxeo.ecm.core.storage.sql.DatabaseOracle;
021import org.nuxeo.ecm.core.storage.sql.DatabasePostgreSQL;
022import org.nuxeo.ecm.core.storage.sql.DatabaseSQLServer;
023import org.nuxeo.runtime.api.Framework;
024
025/**
026 * Description of the specific capabilities of a repository for tests, and helper methods.
027 *
028 * @since 7.3
029 */
030public class StorageConfiguration {
031
032    public static final String CORE_PROPERTY = "nuxeo.test.core";
033
034    public static final String CORE_VCS = "vcs";
035
036    public static final String CORE_DBS_MEM = "mem";
037
038    public static final String CORE_DBS_MONGODB = "mongodb";
039
040    public static final String CORE_DEFAULT = CORE_VCS;
041
042    private DatabaseHelper vcsDatabaseHelper;
043
044    private String dbsBackend;
045
046    public StorageConfiguration() {
047        String core = defaultSystemProperty(CORE_PROPERTY, CORE_DEFAULT);
048        switch (core) {
049        case CORE_VCS:
050            initVCS();
051            break;
052        case CORE_DBS_MEM:
053        case CORE_DBS_MONGODB:
054            dbsBackend = core;
055            break;
056        default:
057            throw new ExceptionInInitializerError("Unknown test core mode: " + core);
058        }
059    }
060
061    protected static String defaultSystemProperty(String name, String def) {
062        String value = System.getProperty(name);
063        if (value == null || value.equals("") || value.equals("${" + name + "}")) {
064            System.setProperty(name, value = def);
065        }
066        return value;
067    }
068
069    protected void initVCS() {
070        vcsDatabaseHelper = DatabaseHelper.DATABASE;
071    }
072
073    // used only for datasource contrib in TestSQLBinaryManager
074    public String getVCSName() {
075        String db = vcsDatabaseHelper.getClass().getSimpleName();
076        if (db.startsWith("Database")) {
077            db = db.substring("Database".length());
078        }
079        return db;
080    }
081
082    public boolean isVCS() {
083        return vcsDatabaseHelper != null;
084    }
085
086    public boolean isVCSH2() {
087        return vcsDatabaseHelper instanceof DatabaseH2;
088    }
089
090    public boolean isVCSDerby() {
091        return vcsDatabaseHelper instanceof DatabaseDerby;
092    }
093
094    public boolean isVCSPostgreSQL() {
095        return vcsDatabaseHelper instanceof DatabasePostgreSQL;
096    }
097
098    public boolean isVCSMySQL() {
099        return vcsDatabaseHelper instanceof DatabaseMySQL;
100    }
101
102    public boolean isVCSOracle() {
103        return vcsDatabaseHelper instanceof DatabaseOracle;
104    }
105
106    public boolean isVCSSQLServer() {
107        return vcsDatabaseHelper instanceof DatabaseSQLServer;
108    }
109
110    public boolean isVCSDB2() {
111        return vcsDatabaseHelper instanceof DatabaseDB2;
112    }
113
114    public boolean isDBS() {
115        return dbsBackend != null;
116    }
117
118    public boolean isDBSMem() {
119        return CORE_DBS_MEM.equals(dbsBackend);
120    }
121
122    public boolean isDBSMongoDB() {
123        return CORE_DBS_MONGODB.equals(dbsBackend);
124    }
125
126    public String getRepositoryName() {
127        if (isVCS()) {
128            return vcsDatabaseHelper.repositoryName;
129        } else {
130            return "test"; // DBS
131        }
132    }
133
134    /**
135     * For databases that do asynchronous fulltext indexing, sleep a bit.
136     */
137    public void sleepForFulltext() {
138        if (isVCS()) {
139            vcsDatabaseHelper.sleepForFulltext();
140        } else {
141            // DBS
142        }
143    }
144
145    /**
146     * For databases that don't have sub-second resolution, sleep a bit to get to the next second.
147     */
148    public void maybeSleepToNextSecond() {
149        if (isVCS()) {
150            vcsDatabaseHelper.maybeSleepToNextSecond();
151        } else {
152            // DBS
153        }
154    }
155
156    /**
157     * Checks if the database has sub-second resolution.
158     */
159    public boolean hasSubSecondResolution() {
160        if (isVCS()) {
161            return vcsDatabaseHelper.hasSubSecondResolution();
162        } else {
163            return true; // DBS
164        }
165    }
166
167    public void waitForAsyncCompletion() {
168        Framework.getService(EventService.class).waitForAsyncCompletion();
169    }
170
171    public void waitForFulltextIndexing() {
172        waitForAsyncCompletion();
173        sleepForFulltext();
174    }
175
176    /**
177     * Checks if the database supports multiple fulltext indexes.
178     */
179    public boolean supportsMultipleFulltextIndexes() {
180        if (isVCS()) {
181            return vcsDatabaseHelper.supportsMultipleFulltextIndexes();
182        } else {
183            return false; // DBS
184        }
185    }
186
187}