Starting and stopping hsqldb from unit tests

I am trying to create integration tests using hsqldb in memory mode. For now, I have to start the hsqldb server from the command line before running unit tests. I would like to be able to control the hsqldb server from my integration tests. I can't seem to understand all this, but from the code.

Update:

This is similar to working with the hibernate.cfg.xml file in the classpath:

org.hsqldb.Server.main(new String[]{}); 

and in my hibernate.cfg.xml file:

 <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> <property name="connection.url">jdbc:hsqldb:mem:ww</property> <property name="connection.username">sa</property> <property name="connection.password"></property> <property name="connection.pool_size">1</property> <property name="dialect">org.hibernate.dialect.HSQLDialect</property> <property name="current_session_context_class">thread</property> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <property name="hbm2ddl.auto">update</property> 

Update It seems that this is only a problem when running unit tests from Eclipse using jUnit and the built-in test runner. If I run

  mvn test 

they are executed correctly and there are no exceptions. Am I missing something, how much does addiction depend? I created an eclipse project using

 mvn eclipse:eclipse 

and my pom:

 <?xml version="1.0" encoding="UTF-8"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>com.myproject</groupId> <artifactId>myproject</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>myproject</name> <dependencies> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.3.2.GA</version> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> <version>10.1.0.4.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.6.0</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.8.0.GA</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> <version>3.4.0.GA</version> </dependency> <!-- Test Dependencies --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.easymock</groupId> <artifactId>easymock</artifactId> <version>3.0</version> <scope>test</scope> </dependency> <dependency> <groupId>hsqldb</groupId> <artifactId>hsqldb</artifactId> <version>1.8.0.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-servlet-tester</artifactId> <version>6.1.24</version> <scope>test</scope> </dependency> <!-- Provided --> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>servlet-api</artifactId> <version>6.0.26</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>jsp-api</artifactId> <version>6.0.26</version> </dependency> </dependencies> <build> <finalName>ww_main</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> </plugin> </plugins> </build> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> 

Update Good, but not sure what is going wrong here, but I seem to have fixed it. I deleted all files created by HSQLDB, as well as all created files in my Maven destination folder, made a clean, updated eclipse.project project using maven and updated the project in eclipse. I think that I may have something left over from the previous configuration, which discarded it.

Thanks for the help!

+3
source share
6 answers

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

 <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 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 the embedded HSQLDB, there is no need to explicitly run anything. Just use the mem: protocol and the in-memory database will start with JDBC.

see also

+6
source

Try adding this to the jdbc url:

 ;ifexists=true;shutdown=true; 
+2
source

In your shutdown method just

 Statement st = conn.createStatement(); st.execute("SHUTDOWN"); conn.close(); 
+2
source

How about starting the server through Runtime.getRuntime().exec("shell command here") ? You should only do this once for all tests, so it won’t add too much delay.

Update
Ok, it seems you yourself decided it :)

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 insert your test suite into it: new TestWrapper(suite)

0
source

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

You can just start / stop it, like the berth-maven-plugin or tomee-maven-plugin 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
source

Perhaps this will help run HSQL in server mode in Unit test, but in the same JVM. The sample code starts org.hsqldb.server.WebServer (i.e., port 80), but you can use org.hsqldb.server.Server. You can call setPort either to override the default port.

fooobar.com/questions/1312599 / ...

0
source

Source: https://habr.com/ru/post/1312582/


All Articles