Pregunta

I have defined an Ant target containing in-memory HSQLDB:

<target name="create.tables" depends="-prepare.test">
  <property name="db.connection.url" value="jdbc:hsqldb:mem:adb"/>
  <property name="db.driver" value="org.hsqldb.jdbcDriver"/>
  <property name="db.username" value="a"/>
  <property name="db.password" value="a"/>

  <echo>Creating tables using: ${db.driver} ${db.connection.url} ${product.mysql-connector.jar}</echo>
  <sql driver="${db.driver}"
      url="${db.connection.url}"
      userid="${db.username}"
      password="${db.password}"
      onerror="stop"
      src="/create-schema.sql">
      <classpath refid="test.classpath" />
  </sql>
</target>

The prepare.test only contains basic things such as fileset etc. When I print the SQL inserts everything is OK. But how can I access the in-memory HSQLDB from a JUnit testcase?

I tried:

Class.forName("org.hsqldb.jdbcDriver");
connection = DriverManager.getConnection("jdbc:hsqldb:mem:adb",
           "a", "a");

but there was only an empty database. Is it possible to use HSQLDB with the insert statements from the Ant target in JUnit testcases?

The exception in the junit test is as follows. The table exists because it is created in the ant task:

 [junit] java.lang.Exception: user lacks privilege or object not found: MYTABLE
[junit]     at com.inmemorytests.InMemoryTest.testSomething(InMemoryTest.java:116)
[junit]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[junit]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
[junit]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[junit]     at java.lang.reflect.Method.invoke(Method.java:601)
[junit]     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
[junit]     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
[junit]     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
[junit]     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
[junit]     at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
[junit]     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
[junit]     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
[junit]     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
[junit]     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
[junit]     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
[junit]     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
[junit]     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
[junit]     at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
[junit]     at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39)
[junit]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:518)
[junit]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1052)
[junit]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:906)
¿Fue útil?

Solución

This is your connection URL in Ant:

 <property name="db.connection.url" value="jdbc:hsqldb:mem:adb"/>

You must use the same URL and the same username and password for a connection:

 connection = DriverManager.getConnection("jdbc:hsqldb:mem:adb", "a", "a");

Update: When using a memory database, the Ant target and the JUnit tests must run in the same JVM. If you are running the two in separate JVM's, start an HSQLDB server before the tests and connect to this server from each test. The server connection URL's are different and described in the HSQLDB Guide.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top