2013-09-26 75 views
3

我試圖使用flatXmlDataSetBuilder爲我的junit測試填充數據庫MS-SQLSERVER。問題是表名包含' - '。填充flatXmlDataSetBuilder SQL-SERVER表名包含' - '

的XML我使用的是這樣的:

<?xml version='1.0' encoding='UTF-8'?> 
<dataset> 
    <TABLE-NAME ID="1111" NAME="TEST"/> 
</dataset> 

的代碼是:

FlatXmlDataSetBuilder flatXmlDataSetBuilder = new FlatXmlDataSetBuilder(); 
      flatXmlDataSetBuilder.setColumnSensing(true); 
      dataset = flatXmlDataSetBuilder.build(Thread.currentThread() 
        .getContextClassLoader() 
        .getResourceAsStream("populate.xml")); 

和錯誤,我得到「com.microsoft.sqlserver.jdbc.SQLServerException:不正確的語法靠近' - '。「

我知道我有這樣的[TABLE-NAME]括號sorround但在這種情況下,我從XML得到一個錯誤:

<?xml version='1.0' encoding='UTF-8'?> 
<dataset> 
    <[TABLE-NAME] ID="1111" NAME="TEST"/> 
</dataset> 

org.dbunit.dataset.DataSetException:線路3:元素內容必須包含格式正確的字符數據或標記。

任何想法如何解決它?

感謝

回答

2

或者您可以使用XmlDataSet到位FlatXmlDataSet的,以避免問題「 - 」字符。下面你可以看到一個DbUnit的完整的例子:

package yourPackage; 

import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import org.dbunit.DBTestCase; 
import org.dbunit.PropertiesBasedJdbcDatabaseTester; 
import org.dbunit.database.DatabaseConnection; 
import org.dbunit.database.IDatabaseConnection; 
import org.dbunit.database.QueryDataSet; 
import org.dbunit.dataset.xml.XmlDataSet; 
import org.junit.Test; 
import org.dbunit.dataset.IDataSet; 
import org.dbunit.dataset.xml.FlatXmlDataSet; 

public class TestDBUnitDummy extends DBTestCase 
{ 

    public TestDBUnitDummy(String name) throws Exception 
    { 
     super(name); 
     System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, "com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
     System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, "jdbc:sqlserver://MyServer;databaseName=MyDatabase"); 
     System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, "sa"); 
     System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, ""); 
    } 

    public static void Export() throws Exception 
    { 
     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
     Connection jdbcConnection = DriverManager.getConnection("jdbc:sqlserver://MySourceServer;databaseName=MyDatabase", "sa", ""); 
     IDatabaseConnection connection = new DatabaseConnection(jdbcConnection); 

     QueryDataSet partialDataSet = new QueryDataSet(connection); 
     partialDataSet.addTable("TABLE-NAME", "SELECT * FROM [TABLE-NAME]"); 
     XmlDataSet.write(partialDataSet, new FileOutputStream("table.xml")); 
     FlatXmlDataSet.write(partialDataSet, new FileOutputStream("table_flat.xml")); 
    } 

    protected void setUpDatabaseConfig(DatabaseConfig config) 
    { 
    config.setProperty(DatabaseConfig.PROPERTY_ESCAPE_PATTERN , "[?]"); 
    } 

    protected IDataSet getDataSet() throws Exception 
    { 
     Export(); 
     return new XmlDataSet(new FileInputStream("table.xml")); 
    } 


    @Test 
    public void test_001() 
    { 
     assertEquals("Dummy test", true, true);  
    } 

} 

[TABLE-NAME]與這些命令的SQL創建:

CREATE TABLE [TABLE-NAME](
    [ID] [int] NULL, 
    [DESCRIPTION] [nvarchar](50) NULL 
) ON [PRIMARY] 
GO 
INSERT INTO [TABLE-NAME] VALUES (1,'ONE') 
INSERT INTO [TABLE-NAME] VALUES (2,'TWO') 
INSERT INTO [TABLE-NAME] VALUES (3,'THREE') 
GO 

XmlDataSet看起來是這樣的:

<?xml version='1.0' encoding='UTF-8'?> 
<dataset> 
    <table name="TABLE-NAME"> 
    <column>ID</column> 
    <column>DESCRIPTION</column> 
    <row> 
    <value>1</value> 
    <value>ONE</value> 
    </row> 
    <row> 
    <value>2</value> 
    <value>TWO</value> 
    </row> 
    <row> 
    <value>3</value> 
    <value>THREE</value> 
    </row> 
    </table> 
</dataset> 

FlatXmlDataSet外觀像這樣:

<?xml version='1.0' encoding='UTF-8'?> 
<dataset> 
    <[TABLE-NAME] ID="1" DESCRIPTION="ONE"/> 
    <[TABLE-NAME] ID="2" DESCRIPTION="TWO"/> 
    <[TABLE-NAME] ID="3" DESCRIPTION="THREE"/> 
</dataset> 

由於「 - 」字符,此xml文件格式不正確。