starting and stopping hsqldb from unit tests

后端 未结 6 778
清歌不尽
清歌不尽 2021-01-21 04:07

I\'m trying to create integration tests using hsqldb in an in memory mode. At the moment, I have to start the hsqldb server from the command line before running the unit tests.

相关标签:
6条回答
  • 2021-01-21 04:46

    check my hsqldb maven plugin : https://github.com/avianey/hsqldb-maven-plugin

    You can just start/stop it like jetty-maven-plugin or tomee-maven-plugin for your tests :

    <plugin>
    
        <!-- current version -->
        <groupId>fr.avianey.mojo</groupId>
        <artifactId>hsqldb-maven-plugin</artifactId>
        <version>1.0.0</version>
    
        <!-- 
            default value for in memory jdbc:hsqldb:hsql://localhost/xdb
            override only values you want to change
        -->
        <configuration>
            <driver>org.hsqldb.jdbcDriver</driver>
            <path>mem:test</path>
            <address>localhost</address>
            <name>xdb</name>
            <username>sa</username>
            <password></password>
            <validationQuery>SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS</validationQuery>
        </configuration>
    
        <!-- call start and stop -->
        <executions>
            <execution>
                <id>start-hsqldb</id>
                <phase>pre-integration-test</phase>
                <goals>
                    <goal>start</goal>
                </goals>
            </execution>
            <execution>
                <id>stop-hsqldb</id>
                <phase>post-integration-test</phase>
                <goals>
                    <goal>stop</goal>
                </goals>
            </execution>
        </executions>
    
    </plugin>
    
    0 讨论(0)
  • 2021-01-21 04:48

    What about starting server through Runtime.getRuntime().exec("shell command here")? You have to do it only once for all tests, so it won't add too big lag.

    Update
    Ok, looks like you've solved it yourself :)

    Update 2
    To execute some code once before (or after) unit tests, you can

    static class TestWrapper extends TestSetup {
        TestWrapper(TestSuite suite) {
            super(suite);
        }
    
        protected void setUp() throws Exception {
            // start db
        }
    
        protected void tearDown() throws Exception {
            // kill db
        }
    }
    

    Then, just wrap your test set in it: new TestWrapper(suite)

    0 讨论(0)
  • 2021-01-21 04:58

    Maybe this might help to start HSQL in server mode in a Unit test, but in the same JVM. Sample code runs org.hsqldb.server.WebServer (i.e. port 80) but you may use org.hsqldb.server.Server. You may call setPort on either to override default port.

    https://stackoverflow.com/a/37784679/15789

    0 讨论(0)
  • 2021-01-21 05:01

    I use the following configuration (directly inspired by the Hibernate tutorial) without any problem:

    <hibernate-configuration>
      <session-factory>
    
        <!-- Database connection settings -->
        <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
        <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:foobar"/>
        <property name="hibernate.connection.username" value="sa"/>
        <property name="hibernate.connection.password" value=""/>
    
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>
    
        <!-- SQL dialect -->
        <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
    
        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>
    
        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
    
        <!-- Drop and re-create the database schema on startup -->
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    
        <mapping resource="..."/>
    
      </session-factory>
    </hibernate-configuration>
    

    When using an in-memory HSQLDB, there is no need to start anything explicitly. Just use the mem: protocol and the in-memory database will get started from JDBC.

    See also

    • Unit-Testing Hibernate With HSQLDB
    0 讨论(0)
  • 2021-01-21 05:04

    Try appending this to the jdbc url:

    ;ifexists=true;shutdown=true;
    
    0 讨论(0)
  • 2021-01-21 05:07

    In your shutdown method just do

    Statement st = conn.createStatement();
    st.execute("SHUTDOWN");
    conn.close();
    
    0 讨论(0)
提交回复
热议问题