使用JUnit进行测试时出现HSQL错误

时间:2014-01-27 11:47:08

标签: java spring junit hsqldb

我是Spring新手并尝试将单元测试添加到简单的Web应用程序中。我有一个文件内的HSQL数据库在我的webapp和Netbeans IDE中运行良好,但是我无法通过JUnit连接它来运行测试。我得到“用户缺少权限或找不到对象”。

错误

Internal Exception: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: MICOM_CHARACTERISTICS
Error Code: -5501
Call: SELECT ID, NAME, RANGE_IDS, RANGE_NAMES, FUNCTIONALITY FROM MICOM_CHARACTERISTICS ORDER BY ID ASC
Query: ReadAllQuery(name="Characteristic.findAll" referenceClass=Characteristic sql="SELECT ID, NAME, RANGE_IDS, RANGE_NAMES, FUNCTIONALITY FROM MICOM_CHARACTERISTICS ORDER BY ID ASC")
    at org.hsqldb.error.Error.error(Unknown Source)

测试-context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p" 
       xmlns:context="http://www.springframework.org/schema/context" 
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="classpath:jdbc.properties" />

    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"
          p:driverClassName="${jdbc.driverClassName}"
          p:url="${jdbc.url}"
          p:username="${jdbc.username}"
          p:password="${jdbc.password}"
          />

          <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
              <property name="persistenceXmlLocation" value="classpath:test-persistence.xml" />
              <property name="persistenceUnitName" value="micomPU" /> 
              <property name="dataSource" ref="dataSource"/>
              <property name="jpaPropertyMap">
                  <props>
                      <prop key="eclipselink.weaving">false</prop>
                  </props>
              </property>
          </bean>

          <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
              <property name="entityManagerFactory" ref="entityManagerFactory"/>
          </bean>

          <tx:annotation-driven transaction-manager="transactionManager"  proxy-target-class="true" />

          <context:component-scan base-package="com.se.micom">
              <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Service" />
              <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Repository" />
              <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Controller" />
          </context:component-scan>

</beans>

Characteristic.java

@Entity
@Table(name = "MICOM_CHARACTERISTICS")
@NamedQueries({
    @NamedQuery(name = "Characteristic.findAll", query = "SELECT c FROM Characteristic c ORDER BY c.id ASC"),    
    @NamedQuery(name = "Characteristic.findById", query = "SELECT c FROM Characteristic c WHERE c.id = :id"),
    @NamedQuery(name = "Characteristic.findByName", query = "SELECT c FROM Characteristic c WHERE c.name = :name"),
    @NamedQuery(name = "Characteristic.findByRangeIds", query = "SELECT c FROM Characteristic c WHERE c.rangeIds = :rangeIds"),
    @NamedQuery(name = "Characteristic.findByRangeNames", query = "SELECT c FROM Characteristic c WHERE c.rangeNames = :rangeNames")})
public class Characteristic extends AbstractEntity implements EntityItem<Integer> {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "ID")
    private Integer id;
    @Column(name = "NAME")
    private String name;
    @Column(name = "RANGE_IDS")
    private String rangeIds;
    @Column(name = "RANGE_NAMES")
    private String rangeNames;
    @JoinColumn(name = "FUNCTIONALITY", referencedColumnName = "ID")
    @ManyToOne
    private Functionality functionality;

jdbc.properties

jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:file:D:\workspace_java\Micom2\db\micom;shutdown=true;
jdbc.username=sa
jdbc.password=

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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_1.xsd">
    <persistence-unit name="micomPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
            <class>com.se.micom.domain.Characteristic</class>
            <class>com.se.micom.domain.Functionality</class>
            <class>com.se.micom.domain.Segment</class>
            <exclude-unlisted-classes>true</exclude-unlisted-classes>
            <properties>
                <property name="eclipselink.logging.level" value="WARNING"/>    
            </properties>
    </persistence-unit>
</persistence>

micom.script

SET DATABASE UNIQUE NAME HSQLDB4253B236F1
SET DATABASE GC 0
SET DATABASE DEFAULT RESULT MEMORY ROWS 0
SET DATABASE EVENT LOG LEVEL 3
SET DATABASE SQL NAMES FALSE
SET DATABASE SQL REFERENCES FALSE
SET DATABASE SQL SIZE TRUE
SET DATABASE SQL TYPES FALSE
SET DATABASE SQL TDC DELETE TRUE
SET DATABASE SQL TDC UPDATE TRUE
SET DATABASE SQL TRANSLATE TTI TYPES TRUE
SET DATABASE SQL CONCAT NULLS TRUE
SET DATABASE SQL UNIQUE NULLS TRUE
SET DATABASE SQL CONVERT TRUNCATE TRUE
SET DATABASE SQL AVG SCALE 0
SET DATABASE SQL DOUBLE NAN TRUE
SET DATABASE TRANSACTION CONTROL LOCKS
SET DATABASE DEFAULT ISOLATION LEVEL READ COMMITTED
SET DATABASE TRANSACTION ROLLBACK ON CONFLICT TRUE
SET DATABASE TEXT TABLE DEFAULTS ''
SET FILES WRITE DELAY 500 MILLIS
SET FILES BACKUP INCREMENT TRUE
SET FILES CACHE SIZE 10000
SET FILES CACHE ROWS 50000
SET FILES SCALE 32
SET FILES LOB SCALE 32
SET FILES DEFRAG 0
SET FILES NIO TRUE
SET FILES NIO SIZE 256
SET FILES LOG TRUE
SET FILES LOG SIZE 50
CREATE USER SA PASSWORD DIGEST 'd41d8cd98f00b204e9800998ecf8427e'
ALTER USER SA SET LOCAL TRUE
CREATE SCHEMA PUBLIC AUTHORIZATION DBA
SET SCHEMA PUBLIC
CREATE MEMORY TABLE PUBLIC.UPDATES(DATA_VERSION VARCHAR(10),LAST_UPDATE TIMESTAMP)
CREATE MEMORY TABLE PUBLIC.MICOM_SEGMENTS(ID INTEGER PRIMARY KEY,NAME VARCHAR(50))
CREATE MEMORY TABLE PUBLIC.MICOM_FUNCTIONALITIES(ID INTEGER PRIMARY KEY,NAME VARCHAR(100),SEGMENT INTEGER,FOREIGN KEY(SEGMENT) REFERENCES PUBLIC.MICOM_SEGMENTS(ID))
CREATE MEMORY TABLE PUBLIC.MICOM_CHARACTERISTICS(ID INTEGER PRIMARY KEY,NAME VARCHAR(100),RANGE_IDS VARCHAR(100),RANGE_NAMES VARCHAR(100),FUNCTIONALITY INTEGER,FOREIGN KEY(FUNCTIONALITY) REFERENCES PUBLIC.MICOM_FUNCTIONALITIES(ID))
ALTER SEQUENCE SYSTEM_LOBS.LOB_ID RESTART WITH 1
SET DATABASE DEFAULT INITIAL SCHEMA PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.SQL_IDENTIFIER TO PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.YES_OR_NO TO PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.TIME_STAMP TO PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.CARDINAL_NUMBER TO PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.CHARACTER_DATA TO PUBLIC
GRANT DBA TO SA
SET SCHEMA SYSTEM_LOBS
INSERT INTO BLOCKS VALUES(0,2147483647,0)
SET SCHEMA PUBLIC
INSERT INTO MICOM_CHARACTERISTICS VALUES(4,'Current transformer powered','60755','MiCOM P115',1)
INSERT INTO MICOM_CHARACTERISTICS VALUES(5,'Dual powered','60755','MiCOM P116',1)
...

0 个答案:

没有答案