Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Automated tests JPA 2.1使用Arquillian&;从persistence.xml中删除脚本执行;收缩膜_Automated Tests_Sql Scripts_Jpa 2.1_Jboss Arquillian_Shrinkwrap - Fatal编程技术网

Automated tests JPA 2.1使用Arquillian&;从persistence.xml中删除脚本执行;收缩膜

Automated tests JPA 2.1使用Arquillian&;从persistence.xml中删除脚本执行;收缩膜,automated-tests,sql-scripts,jpa-2.1,jboss-arquillian,shrinkwrap,Automated Tests,Sql Scripts,Jpa 2.1,Jboss Arquillian,Shrinkwrap,我正在编写一些Arquillian(使用Shrinkwrap)测试,我目前通过persistence.xml使用JPA 2.1创建和删除脚本自动化: // src/test/resources-wildfly-remote/test-persistence.xml // runs fine, configured as resource in pom.xml <?xml version="1.0" encoding="UTF-8"?> <persistence vers

我正在编写一些Arquillian(使用Shrinkwrap)测试,我目前通过persistence.xml使用JPA 2.1创建和删除脚本自动化:

// src/test/resources-wildfly-remote/test-persistence.xml
// runs fine, configured as resource in pom.xml
<?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="testPU" transaction-type="JTA">
        <!-- Wildfly JPA specific implementation config -->
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>java:/jdbc/Test</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.archive.autodetection" value="class" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="javax.persistence.schema-generation.database.action"
            value="drop-and-create" />
            <property name="javax.persistence.schema-generation.create-source"
            value="script" />
            <property name="javax.persistence.schema-generation.drop-source"
            value="script" />
            <property name="javax.persistence.schema-generation.create-script-source"
            value="META-INF/create-test.sql" />
            <property name="javax.persistence.schema-generation.drop-script-source"
            value="META-INF/drop-test.sql" />
        </properties>
    </persistence-unit>
</persistence>
测试和创建脚本运行良好,但不会发生删除,是否有人有相同的问题

编辑(添加日志):

我会继续努力,任何进展都会在这里报告

编辑在@After测试方法下手动执行脚本:

protected static void dropAllTables(UserTransaction utx, EntityManager em)
        throws SystemException {
    // Temporary solution for table dropping after tests
    try {
        InputStream fis = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("META-INF/drop-teste.sql");

        StringBuilder builder = new StringBuilder();
        int ch;
        while ((ch = fis.read()) != -1) {
            builder.append((char) ch);
        }

        String[] dropCommands = builder.toString().split(";");

        utx.begin();
        for (String command : dropCommands) {
            em.createNativeQuery(command).executeUpdate();
        }
        utx.commit();
    } catch (Exception e) {
        utx.rollback();
    }
}

看起来Hibernate正在尝试执行您的drop命令,但没有成功

这可能是由于表之间的约束造成的。因此,请仔细检查删除顺序是否正确:

  • 首先:删除没有ForeignKey引用的表
  • 第二:删除下一个这样的表
  • 依此类推-直到最基本的表,其中没有任何FKs内

Regars

Hi Grzesiek,奇怪的是,我使用完全相同的脚本来删除表,我甚至手动执行drop命令,读取文件,作为一个临时解决方案,我将用代码段编辑问题。至于约束错误,由于我手动执行,已经碰到了,并且已经修复了。无论如何,thnx用于双重检查:)哦,对于drop脚本,它的编写尽可能简单,比如:
drop TABLE TABLE 1;下表2
--project
    --src/test
        --java
            --com/example/MyTest.java
        --resources
            --create-test.sql
            --drop-test.sql
            --example-ds.xml
        --resources-wildfly-remote
            --test-persistence.xml
// log snippet discriminating that the drop command wasnt executed
2015-01-08 15:36:21,781 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table1]
2015-01-08 15:36:21,781 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table2]
2015-01-08 15:36:21,781 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table3]
2015-01-08 15:36:21,791 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table4]
2015-01-08 15:36:21,791 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table5]
2015-01-08 15:36:21,801 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table6]
2015-01-08 15:36:21,801 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table7]
2015-01-08 15:36:21,801 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table8]
2015-01-08 15:36:21,801 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table9]
2015-01-08 15:36:21,801 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table10]
2015-01-08 15:36:21,811 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table11]
2015-01-08 15:36:21,811 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table12]
2015-01-08 15:36:21,811 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table13]
2015-01-08 15:36:21,811 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table14]
2015-01-08 15:36:21,811 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table15]
2015-01-08 15:36:21,811 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table16]
2015-01-08 15:36:21,821 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table17]
protected static void dropAllTables(UserTransaction utx, EntityManager em)
        throws SystemException {
    // Temporary solution for table dropping after tests
    try {
        InputStream fis = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("META-INF/drop-teste.sql");

        StringBuilder builder = new StringBuilder();
        int ch;
        while ((ch = fis.read()) != -1) {
            builder.append((char) ch);
        }

        String[] dropCommands = builder.toString().split(";");

        utx.begin();
        for (String command : dropCommands) {
            em.createNativeQuery(command).executeUpdate();
        }
        utx.commit();
    } catch (Exception e) {
        utx.rollback();
    }
}