Question

I trying to use flatXmlDataSetBuilder to populate a Database MS-SQLSERVER for my junit test. The problem is the table name contain a '-'.

the xml I'm using look like this:

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

The code is:

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

And the error I get "com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '-'."

I know I have to sorround with brackets like this [TABLE-NAME] but in that case I get an error from the xml:

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

org.dbunit.dataset.DataSetException: Line 3: The content of elements must consist of well-formed character data or markup.

any idea how to solve it?

thanks

Was it helpful?

Solution

Alternatively you can use XmlDataSet in place of FlatXmlDataSet to avoid the problem with "-" character. Below you can see a dbunit complete example:

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] is created with SQL with these commands:

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 looks like this:

<?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 looks like this:

<?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>

This xml file is not well formed due to "-" character.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top