Error when Testing a datasource

Hi, I am following this tutorial

In my pom.xml, add the dependency:

<dependency>
      <groupId>org.nuxeo.runtime</groupId>
      <artifactId>nuxeo-runtime-datasource</artifactId>
      <scope>test</scope>
    </dependency>
In some circumstances, if your test dependencies aren't followed correctly, you may have to add as well:

<dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <scope>test</scope>
    </dependency>

My contrib:

<?xml version="1.0"?>
<component name="com.example.datasource.tests">
  <extension target="org.nuxeo.runtime.datasource" point="datasources">
    <datasource name="jdbc/foo" driverClassName="org.h2.Driver"
      maxActive="20" maxIdle="5">
      <property name="url">jdbc:h2:${ds.test.home}/db</property>
    </datasource>
  </extension>
</component>

Junit4 config test:

@Features(CoreFeature.class)
@Deploy("org.nuxeo.runtime.datasource")
@LocalDeploy("myplugin:OSGI-INF/datasource-contrib.xml")
public class MyDataSourceFeature extends SimpleFeature {

    private static final String DIRECTORY = "target/test/h2";
    private static final String PROP_NAME = "ds.test.home";

    @Override
    public void initialize(FeaturesRunner runner) throws Exception {
        File dir = new File(DIRECTORY);
        FileUtils.deleteTree(dir);
        dir.mkdirs();
        Framework.getProperties().put(PROP_NAME, dir.getPath());
        super.initialize(runner);
    }
}

myplugin I got from MANIFEST.MF file

Bundle-SymbolicName: myplugin

My test

@Test
    public void test() throws Exception {
        // look up jdbc/foo
        DataSource ds = DataSourceHelper.getDataSource("foo");
        Connection conn = ds.getConnection();
        try {
            ... use the connection ...
        } finally {
            conn.close();
        }
    }

but I am having this error when run test

javax.naming.NameNotFoundException; remaining name 'env/jdbc/foo'
    at org.mortbay.naming.NamingContext.lookup(NamingContext.java:634)
    at org.mortbay.naming.NamingContext.lookup(NamingContext.java:665)
    at org.mortbay.naming.NamingContext.lookup(NamingContext.java:680)
    at org.mortbay.naming.java.javaRootURLContext.lookup(javaRootURLContext.java:112)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at org.nuxeo.runtime.api.DataSourceHelper.getDataSource(DataSourceHelper.java:125)
    at org.example.test.MyDataSourceFeature.test(MyDataSourceFeature.java:49)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.jdt.internal.launching.LongCommandLineLauncher.main(LongCommandLineLauncher.java:43)
0 votes

1 answers

1008 views

ANSWER



Hi,

Your code is missing the TransactionalFeature (required since 5.6). I've updated the tutorial and added the sample tests in the nuxeo-sample-project.

Regards,

0 votes