/*
 * Copyright 2015, TeamDev Ltd. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.spine3.server.storage.hsqldb;

import com.google.protobuf.StringValue;
import com.zaxxer.hikari.HikariConfig;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.spine3.server.storage.EntityStorageShould;

import static org.spine3.server.storage.hsqldb.HsqlEntityStorage.*;

/**
 * @author Alexander Litus
 */
public class HsqlEntityStorageShould extends EntityStorageShould {

    /**
     * The URL of in-memory database.
     */
    private static final String DB_URL = "jdbc:hsqldb:mem:entitytests";

    private static final HsqlDb DATABASE = newHsqlDb();

    private static final String CREATE_TABLE_SQL =
            "CREATE TABLE " + ENTITIES + '(' +
                ID + " VARCHAR(999)," +
                ENTITY + " BLOB," +
                "PRIMARY KEY(" + ID + ')' +
            ");";

    private static final String DROP_TABLE_SQL = "DROP TABLE " + ENTITIES;

    private static final String SHUTDOWN_SQL = "SHUTDOWN";

    public HsqlEntityStorageShould() {
        super(newStorage());
    }

    private static HsqlEntityStorage<String, StringValue> newStorage() {
        return HsqlEntityStorage.newInstance(DATABASE, StringValue.getDescriptor());
    }

    private static HsqlDb newHsqlDb() {
        final HikariConfig config = new HikariConfig();
        config.setJdbcUrl(DB_URL);
        return HsqlDb.newInstance(config);
    }

    @Before
    public void setUpTest() {
        DATABASE.execute(CREATE_TABLE_SQL);
    }

    @After
    public void tearDownTest() {
        DATABASE.execute(DROP_TABLE_SQL);
    }

    @AfterClass
    public static void tearDownClass() {
        DATABASE.execute(SHUTDOWN_SQL);
        DATABASE.close();
    }
}

@@ -1210,16 +1210,39 @@
 Before;%0A
+import org.junit.Test;%0A
 import o
@@ -1288,16 +1288,53 @@
 hould;%0A%0A
+import static org.junit.Assert.fail;%0A
 import s
@@ -1429,16 +1429,68 @@
 tus%0A */%0A
+@SuppressWarnings(%22InstanceMethodNamingConvention%22)%0A
 public c
@@ -1733,16 +1733,113 @@
 lDb();%0A%0A
+    private static final HsqlEntityStorage%3CString, StringValue%3E STORAGE = newStorage(DATABASE);%0A%0A
     priv
@@ -2260,185 +2260,15 @@
 per(
-newStorage());%0A    %7D%0A%0A    private static HsqlEntityStorage%3CString, StringValue%3E newStorage() %7B%0A        return HsqlEntityStorage.newInstance(DATABASE, StringValue.getDescriptor()
+STORAGE
 );%0A 
@@ -2450,24 +2450,192 @@
 ig);%0A    %7D%0A%0A
+    private static HsqlEntityStorage%3CString, StringValue%3E newStorage(HsqlDb db) %7B%0A        return HsqlEntityStorage.newInstance(db, StringValue.getDescriptor());%0A    %7D%0A%0A
     @Before%0A
@@ -2936,11 +2936,385 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void close() %7B%0A        final HsqlEntityStorage%3CString, StringValue%3E storage = newStorage(newHsqlDb());%0A        storage.close();%0A        try %7B%0A            storage.read(%22any-id%22);%0A        %7D catch (RuntimeException ignored) %7B%0A            // is OK because storage is closed%0A            return;%0A        %7D%0A        fail(%22Storage should close itself.%22);%0A    %7D
 %0A%7D%0A

Add a test.