简体   繁体   English

使用Postgres DB用户的Arquillian的Tomee缺少特权或找不到对象

[英]Tomee with Arquillian using Postgres DB user lacks privilege or object not found

I have a problem when running arquillian tests against postgres db using tomee. 使用tomee对postgres db运行Arquillian测试时出现问题。 With all the info on the web I'm still struggling to get the problem solved. 有了网络上的所有信息,我仍在努力解决问题。

javax.ejb.EJBException: The bean encountered a non-application exception; nested exception is: 
    Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: CREDENTIALS
Error Code: -5501
Call: SELECT ID, PASSWORD, USERNAME FROM credentials WHERE (USERNAME = ?)
    bind => [phil]

The DB: 数据库:

Name: registry 名称:注册表

Table: credentials 表:凭据

Sits under manually created Schema: postgres 坐在手动创建的架构下:postgres

persistence.xml under directory src/main/resources src / main / resources目录下的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

  <persistence-unit name="registry" transaction-type="JTA">
    <jta-data-source>RegistryDS</jta-data-source>
    <non-jta-data-source>UnmanagedRegistryDS</non-jta-data-source>
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>za.co.registry.client.login.Credentials</class>
    <properties>
      <property name="eclipselink.debug" value="OFF"/>
      <property name="eclipselink.weaving" value="static"/>
      <property name="eclipselink.logging.level.sql" value="FINE"/>
      <property name="eclipselink.logging.parameters" value="true"/>
      <property name="eclipselink.logging.logger" value="DefaultLogger"/>
    </properties>
  </persistence-unit>
</persistence>

tomee.xml tomee.xml

<Resource id="RegistryDS" type="DataSource">
          jdbcDriver=org.postgresql.Driver
          jdbcUrl=jdbc:postgresql://127.0.0.1:5432/registry
          userName=postgres 
          password=postgres 
          JtaManaged=true
</Resource>
<Resource id="UnmanagedRegistryDS" type="DataSource">
          jdbcDriver=org.postgresql.Driver
          jdbcUrl=jdbc:postgresql://127.0.0.1:5432/registry
          userName=postgres 
          password=postgres 
          JtaManaged=false
</Resource>

pom.xml extract for arquillian tests poml.xml提取以进行Arquillian测试

<dependency>
    <groupId>org.apache.openejb</groupId>
    <artifactId>arquillian-tomee-embedded</artifactId>
    <version>1.6.0</version>
    <scope>test</scope>
</dependency> 

<dependency>
    <groupId>org.jboss.arquillian.junit</groupId>
    <artifactId>arquillian-junit-container</artifactId>
    <version>1.0.3.Final</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
</dependency>  

arquillian.xml extract arquillian.xml提取

<container qualifier="tomee" default="true">
    <configuration>
        <property name="httpPort">-1</property>
        <property name="stopPort">-1</property>
        <property name="dir">target/apache-tomee-remote</property>
        <property name="appWorkingDir">target/arquillian-test-working-dir</property>
        <property name="properties" />
    </configuration>
</container>

The ServiceTest.java file when loading the resources. 加载资源时的ServiceTest.java文件。

    @Deployment
    public static WebArchive createDeployment() {
        WebArchive webArchive = newArchive();
        webArchive.addClasses(Credentials.class);
        webArchive.addAsResource("META-INF/persistence.xml");
        webArchive.addAsResource("META-INF/beans.xml");
        return webArchive;
    }

And last the test findCredentialsByUsernameTest method in ServiceTest.java 最后是ServiceTest.java的测试findCredentialsByUsernameTest方法

@Test
public void findCredentialsByUsernameTest() {
    Credentials login = LoginService.findByUsername("phil");
    Assert.assertNotNull(login);
}

I do not start or end any EntityTransaction 's in the test class. 我没有在测试类中开始或结束任何EntityTransaction

It works injecting a EJB when the DB call in the service is removed. 当删除服务中的数据库调用时,它可以注入EJB。 What am I missing in the config or doing wrong for this not to be working? 我在配置中缺少什么或做错了什么而无法正常工作?

Ok I think I know what I did wrong. 好吧,我想我知道我做错了。

I need to run the tests against a embedded db. 我需要针对嵌入式数据库运行测试。

The steps that I followed to get Arquillian tests to work against a embedded db; 我遵循的步骤使Arquillian测试可以对嵌入式db起作用。

Removed un-managed data source from tomee.xml, I added it because I wanted to use it for my tests. 从tomee.xml中删除了非托管数据源,我添加了它,因为我想在测试中使用它。 The other data source is still there because it is used when deploying to tomee to connect to postgres db. 另一个数据源仍然存在,因为在部署tomee以连接到postgres db时会使用它。

<Resource id="UnmanagedRegistryDS" type="DataSource">
          jdbcDriver=org.postgresql.Driver
          jdbcUrl=jdbc:postgresql://127.0.0.1:5432/registry
          userName=postgres 
          password=postgres 
          JtaManaged=false
</Resource>

I'm going to connect to HSQLDB. 我将连接到HSQLDB。

HSQLDB (HyperSQL DataBase) is the leading SQL relational database software written in Java. It offers a small, fast multithreaded and transactional database engine with in-memory and disk-based tables and supports embedded and server modes

Next I create a second persistence.xml in src/test/resources called test-persistence.xml. 接下来,我在src / test / resources中创建另一个名为test-persistence.xml的persistence.xml。 Keep in mind the persistence-unit name testDatabase it is going to be used in the arquillian.xml file 请记住,持久性单元名称testDatabase将在arquillian.xml文件中使用

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">
    <persistence-unit name="test" transaction-type="JTA">
        <jta-data-source>testDatabase</jta-data-source>
        <class>za.co.registry.client.login.Credentials</class>
        <properties>
            <property name="openejb.jpa.init-entitymanager" value="true" />
            <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
        </properties>
    </persistence-unit>
</persistence>

The arquillian.xml, I'm setting the testDatabase persistence unit properties in the file. arquillian.xml,我在文件中设置了testDatabase持久性单元属性。 See below under properties. 请参阅下面的属性。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<arquillian
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
    <container qualifier="openejb-embedded" default="true">
        <configuration>
            <property name="httpPort">-1</property>
            <property name="stopPort">-1</property>
            <property name="dir">target/apache-tomee-remote</property>
            <property name="appWorkingDir">target/arquillian-test-working-dir</property>
            <property name="properties">
                testDatabase = new://Resource?type=DataSource
                testDatabase.JdbcUrl = jdbc:hsqldb:mem:my-datasource
             </property>
        </configuration>
    </container>
</arquillian>

Configuring my ServiceTest.java file to include the new persistence.xml file. 将我的ServiceTest.java文件配置为包括新的persistence.xml文件。

webArchive.addAsWebInfResource("META-INF/test-persistence.xml", "persistence.xml");
webArchive.addAsResource("META-INF/beans.xml");

Now I can run the findCredentialsByUsernameTest method. 现在,我可以运行findCredentialsByUsernameTest方法。

    @Test
    public void findCredentialsByUsernameTest() {
        //create credentials first
        Credentials newCredentials = loginService.newCredentials(new Credentials("john", "password"));

        //search for credentails
        Credentials login = loginService.findByUsername("john");
        Assert.assertNotNull(login);
        Assert.assertEquals(newCredentials.getUsername(), login.getUsername());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM