XML dataset file and adding entries to a DB -> XML file entries are not stored into the DB

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

XML dataset file and adding entries to a DB -> XML file entries are not stored into the DB

phantomiaser
Hello!

I am using the latest version of DBUnit 2.4.7. and I have a problem  
understanding what's happening:

I have a database table named User and I created a xml file full.xml  
with this source code:
http://www.dbunit.org/faq.html#extract
The result are ten entries (rows for the User table).

Then I have this Test class for adding ten new users to the database:

import org.dbunit.DBTestCase;
import org.dbunit.PropertiesBasedJdbcDatabaseTester;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.junit.Test;

import java.io.FileInputStream;
import java.util.Date;

public class UserTest extends DBTestCase {
     IUserService service = new UserServiceImplement();

     public UserTest(String name) {
         super(name);
         
System
.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS,  
"com.mysql.jdbc.Driver");
         
System
.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL,  
"jdbc:mysql://localhost/mytestdb");
         
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME,  
"root");
         
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD,  
"");
     }

     protected IDataSet getDataSet() throws Exception {
         return new FlatXmlDataSetBuilder().build(new  
FileInputStream("full.xml"));
     }

     @Test
     public void testAddUser() {
         for (int i=0; i < 10; i++) {
             User u = new User();
             u.setName("test name - " + i);
             u.setDesc("test desc - " +i);
             u.setCreateDate(new Date());
             service.addUser(u);
         }
     }
}

Now, everything is okay, the Unit test was running: I expect that I  
have 20 users in my User table:
10 user because of the for loop, and 10 users because of my full.xml =  
20.
But, I only have 10 users in the database table User. And those  
entries are from the for-loop.

Where are the entries of the xml file?

If I write instead of
     @Test
     public void testAddUser() {
         for (int i=0; i < 10; i++) {
             User u = new User();
             u.setName("test name - " + i);
             u.setDesc("test desc - " +i);
             u.setCreateDate(new Date());
             service.addUser(u);
         }
     }

this:

     @Test
     public void testAddUser() {
     }
(an empty test method)
I will get 10 entries, the entries of the xml file.

So I am totally confused what's happening.
Perhaps someone can explain this to me?!

Thanks a lot in advance & Best Regards.

------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the
lucky parental unit.  See the prize list and enter to win:
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: XML dataset file and adding entries to a DB -> XML file entries are not stored into the DB

John Hurst-2
I'm not sure about how that behaviour would result, and haven't had time to test it.

But at a guess, perhaps it relates to combining JUnit 4-style annotated tests with DbUnit's testing base class DBTestCase, which is of course a JUnit 3 test class.

Could you try reworking your test to JUnit 3 style, just to see if that works?

Note: I use DbUnit with JUnit 4 all the time, but I do not extend a JUnit base class in my usage.

Hope this helps.

John Hurst

On Wed, Jun 9, 2010 at 4:50 AM, <[hidden email]> wrote:
Hello!

I am using the latest version of DBUnit 2.4.7. and I have a problem
understanding what's happening:

I have a database table named User and I created a xml file full.xml
with this source code:
http://www.dbunit.org/faq.html#extract
The result are ten entries (rows for the User table).

Then I have this Test class for adding ten new users to the database:

import org.dbunit.DBTestCase;
import org.dbunit.PropertiesBasedJdbcDatabaseTester;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.junit.Test;

import java.io.FileInputStream;
import java.util.Date;

public class UserTest extends DBTestCase {
    IUserService service = new UserServiceImplement();

    public UserTest(String name) {
        super(name);

System
.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS,
"com.mysql.jdbc.Driver");

System
.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL,
"jdbc:mysql://localhost/mytestdb");

System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME,
"root");

System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD,
"");
    }

    protected IDataSet getDataSet() throws Exception {
        return new FlatXmlDataSetBuilder().build(new
FileInputStream("full.xml"));
    }

    @Test
    public void testAddUser() {
        for (int i=0; i < 10; i++) {
            User u = new User();
            u.setName("test name - " + i);
            u.setDesc("test desc - " +i);
            u.setCreateDate(new Date());
            service.addUser(u);
        }
    }
}

Now, everything is okay, the Unit test was running: I expect that I
have 20 users in my User table:
10 user because of the for loop, and 10 users because of my full.xml =
20.
But, I only have 10 users in the database table User. And those
entries are from the for-loop.

Where are the entries of the xml file?

If I write instead of
    @Test
    public void testAddUser() {
        for (int i=0; i < 10; i++) {
            User u = new User();
            u.setName("test name - " + i);
            u.setDesc("test desc - " +i);
            u.setCreateDate(new Date());
            service.addUser(u);
        }
    }

this:

    @Test
    public void testAddUser() {
    }
(an empty test method)
I will get 10 entries, the entries of the xml file.

So I am totally confused what's happening.
Perhaps someone can explain this to me?!

Thanks a lot in advance & Best Regards.

------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the
lucky parental unit.  See the prize list and enter to win:
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user



--
Life is interfering with my game

------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the
lucky parental unit.  See the prize list and enter to win:
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: XML dataset file and adding entries to a DB -> XML file entries are not stored into the DB

Oli Tree
In reply to this post by phantomiaser
Hi,

can you show me the full.xml? And can you check the primary key?

Regards

------
WWK Lebensversicherung a. G., Vorstand: Jürgen Schrameier (V.), Rainer Gebhart, Dr. Friedrich Hebeisen; Vorsitzender des Aufsichtsrats: Prof. Dr.-Ing. habil. Kurt Rößner, Sitz München, Registergericht München HR B 211; WWK Allgemeine Versicherung AG, Vorstand: Jürgen Schrameier (V), Rainer Gebhart, Dr. Friedrich Hebeisen; Vorsitzender des Aufsichtsrats: Dr. Klaus Otto, Sitz München, Registergericht München HR B 5553; WWK Vermögensverwaltungs und Dienstleistungs GmbH, Geschäftsführer: Dirk Fassott, Franz Mastera, Sitz München, Registergericht München HR B 76323; WWK Pensionsfonds AG, Vorstand: Ansgar Eckert, Dirk Fassott, Karl Ruffing, Heinrich Schüppert; Vorsitzender des Aufsichtsrats: Dr. Friedrich Hebeisen, Sitz München, Registergericht München HR B 146295; Hausanschrift: Marsstraße 37, 80335 München; WWK Investment S.A., Verwaltungsrat: Karl Ruffing (V.), Ansgar Eckert, Stefan Schneider (Hauck & Aufhäuser), Handelsregister: R.C. Luxembourg Nr. B 81 270, Sitz der Gesellschaft: 2, rue Heine, L-1720 Luxembourg
------

-----Ursprüngliche Nachricht-----
Von: [hidden email] [mailto:[hidden email]]
Gesendet: Dienstag, 8. Juni 2010 18:50
An: [hidden email]
Betreff: [dbunit-user] XML dataset file and adding entries to a DB -> XML file entries are not stored into the DB


Hello!

I am using the latest version of DBUnit 2.4.7. and I have a problem
understanding what's happening:

I have a database table named User and I created a xml file full.xml
with this source code:
http://www.dbunit.org/faq.html#extract
The result are ten entries (rows for the User table).

Then I have this Test class for adding ten new users to the database:

import org.dbunit.DBTestCase;
import org.dbunit.PropertiesBasedJdbcDatabaseTester;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.junit.Test;

import java.io.FileInputStream;
import java.util.Date;

public class UserTest extends DBTestCase {
     IUserService service = new UserServiceImplement();

     public UserTest(String name) {
         super(name);

System
.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS,
"com.mysql.jdbc.Driver");

System
.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL,
"jdbc:mysql://localhost/mytestdb");

System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME,
"root");

System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD,
"");
     }

     protected IDataSet getDataSet() throws Exception {
         return new FlatXmlDataSetBuilder().build(new
FileInputStream("full.xml"));
     }

     @Test
     public void testAddUser() {
         for (int i=0; i < 10; i++) {
             User u = new User();
             u.setName("test name - " + i);
             u.setDesc("test desc - " +i);
             u.setCreateDate(new Date());
             service.addUser(u);
         }
     }
}

Now, everything is okay, the Unit test was running: I expect that I
have 20 users in my User table:
10 user because of the for loop, and 10 users because of my full.xml =
20.
But, I only have 10 users in the database table User. And those
entries are from the for-loop.

Where are the entries of the xml file?

If I write instead of
     @Test
     public void testAddUser() {
         for (int i=0; i < 10; i++) {
             User u = new User();
             u.setName("test name - " + i);
             u.setDesc("test desc - " +i);
             u.setCreateDate(new Date());
             service.addUser(u);
         }
     }

this:

     @Test
     public void testAddUser() {
     }
(an empty test method)
I will get 10 entries, the entries of the xml file.

So I am totally confused what's happening.
Perhaps someone can explain this to me?!

Thanks a lot in advance & Best Regards.

------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the
lucky parental unit.  See the prize list and enter to win:
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
dbunit-user mailing list
[hidden email] https://lists.sourceforge.net/lists/listinfo/dbunit-user


------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the
lucky parental unit.  See the prize list and enter to win:
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: XML dataset file and adding entries to a DB -> XML file entries are not stored into the DB

phantomiaser
In reply to this post by phantomiaser
Thanks a lot for your reply.

I got the problem: It was because the database was refreshed every  
time by the Hibernate configuration "create" at hbm2ddl.auto.

Best Regards.

------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the
lucky parental unit.  See the prize list and enter to win:
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user