dataset based on JPA entities

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

dataset based on JPA entities

Piotr Jagielski-3
Hello,

I'm a new user of DBUnit and I enjoy using it very much except for the
fact that it forces me to externalize my test data in XML. I try to keep
my data sets small but their number is still growing and getting harder
and harder to maintain. I was wondering whether any of you tried
alternative approaches. What I would prefer to do is to create my domain
objects in tests using builder pattern and have DBUnit data set
generated based on them. I already experimented with this approach and
it results in more readable and maintainable tests in my opinion. Below
is an example of functional test using this approach:

@Test
public void shouldHaveTitleEqualToUserName() throws Exception {
    // given
    User user = aUser().withId(1).withUsername("John").build();
    database.add(user).prepare();

    // when
    userPage.open(1);

    // then
    assertTitleEquals("User: John", userPage);
}

Calling database.add(user).prepare() creates a dataset with given user
and calls onSetup() method of underlying databaseTester. This way I
don't have to go to external file in order to understand the test and
details that are not important in this case like user's e-mail are
hidden in the builder. Changes in the database schema are easier because
I only need to do them in one place instead of multiple XML data

Right now I have a separate data set builder for each entity and I'm
using flat XML data set representation internally because I'm most
familiar with it. It doesn't have to work this way. What I was thinking
I could do is to create generic mechanism based on JPA annotations that
could convert any set of JPA entities to IDataSet without using flat XML
representation internally.

I'm wondering what you think. Have any of you tried to do something
similar? Do you see any possible issues that may come up?

Regards,
Piotr Jagielski

------------------------------------------------------------------------------
BlackBerry® DevCon Americas, Oct. 18-20, San Francisco, CA
The must-attend event for mobile developers. Connect with experts.
Get tools for creating Super Apps. See the latest technologies.
Sessions, hands-on labs, demos & much more. Register early & save!
http://p.sf.net/sfu/rim-blackberry-1
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: dataset based on JPA entities

Marc Philipp-2
Hi Piotr,

I came up with something very similar a while ago. It can be found on
GitHub: https://github.com/marcphilipp/dbunit-datasetbuilder

Unfortunately, nobody seemed to be interested at that time.

Here's an example:

DataSetBuilder builder = new DataSetBuilder();

// Using strings as column names, not type-safe
builder.newRow("PERSON").with("NAME", "Bob").with("AGE", 18).add();

// Using ColumnSpecs to identify columns, type-safe!
ColumnSpec<String> name = ColumnSpec.newColumn("NAME")
ColumnSpec<Integer> age = ColumnSpec.newColumn("AGE");
builder.newRow("PERSON").with(name, "Alice").with(age, 23).add();

// New columns are added on the fly
builder.newRow("PERSON").with(NAME, "Charlie").with("LAST_NAME", "Brown").add();

IDataSet dataSet = builder.build();


Best regards,

Marc




2011/8/7 Piotr Jagielski <[hidden email]>:

> Hello,
>
> I'm a new user of DBUnit and I enjoy using it very much except for the
> fact that it forces me to externalize my test data in XML. I try to keep
> my data sets small but their number is still growing and getting harder
> and harder to maintain. I was wondering whether any of you tried
> alternative approaches. What I would prefer to do is to create my domain
> objects in tests using builder pattern and have DBUnit data set
> generated based on them. I already experimented with this approach and
> it results in more readable and maintainable tests in my opinion. Below
> is an example of functional test using this approach:
>
> @Test
> public void shouldHaveTitleEqualToUserName() throws Exception {
>    // given
>    User user = aUser().withId(1).withUsername("John").build();
>    database.add(user).prepare();
>
>    // when
>    userPage.open(1);
>
>    // then
>    assertTitleEquals("User: John", userPage);
> }
>
> Calling database.add(user).prepare() creates a dataset with given user
> and calls onSetup() method of underlying databaseTester. This way I
> don't have to go to external file in order to understand the test and
> details that are not important in this case like user's e-mail are
> hidden in the builder. Changes in the database schema are easier because
> I only need to do them in one place instead of multiple XML data
>
> Right now I have a separate data set builder for each entity and I'm
> using flat XML data set representation internally because I'm most
> familiar with it. It doesn't have to work this way. What I was thinking
> I could do is to create generic mechanism based on JPA annotations that
> could convert any set of JPA entities to IDataSet without using flat XML
> representation internally.
>
> I'm wondering what you think. Have any of you tried to do something
> similar? Do you see any possible issues that may come up?
>
> Regards,
> Piotr Jagielski
>
> ------------------------------------------------------------------------------
> BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
> The must-attend event for mobile developers. Connect with experts.
> Get tools for creating Super Apps. See the latest technologies.
> Sessions, hands-on labs, demos & much more. Register early & save!
> http://p.sf.net/sfu/rim-blackberry-1
> _______________________________________________
> dbunit-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/dbunit-user
>

------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
The must-attend event for mobile developers. Connect with experts.
Get tools for creating Super Apps. See the latest technologies.
Sessions, hands-on labs, demos & much more. Register early & save!
http://p.sf.net/sfu/rim-blackberry-1
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: dataset based on JPA entities

Piotr Jagielski-3
Marc,

Actually, I found your project a while ago. Good work! My idea is to
build JPA entities and generate data sets based on them as opposed to
building data sets directly. Perhaps what you created could be useful to
accomplish that.

Regards,
Piotr

On 2011-08-07 21:07, Marc Philipp wrote:

> Hi Piotr,
>
> I came up with something very similar a while ago. It can be found on
> GitHub: https://github.com/marcphilipp/dbunit-datasetbuilder
>
> Unfortunately, nobody seemed to be interested at that time.
>
> Here's an example:
>
> DataSetBuilder builder = new DataSetBuilder();
>
> // Using strings as column names, not type-safe
> builder.newRow("PERSON").with("NAME", "Bob").with("AGE", 18).add();
>
> // Using ColumnSpecs to identify columns, type-safe!
> ColumnSpec<String>  name = ColumnSpec.newColumn("NAME")
> ColumnSpec<Integer>  age = ColumnSpec.newColumn("AGE");
> builder.newRow("PERSON").with(name, "Alice").with(age, 23).add();
>
> // New columns are added on the fly
> builder.newRow("PERSON").with(NAME, "Charlie").with("LAST_NAME", "Brown").add();
>
> IDataSet dataSet = builder.build();
>
>
> Best regards,
>
> Marc
>
>
>
>
> 2011/8/7 Piotr Jagielski<[hidden email]>:
>> Hello,
>>
>> I'm a new user of DBUnit and I enjoy using it very much except for the
>> fact that it forces me to externalize my test data in XML. I try to keep
>> my data sets small but their number is still growing and getting harder
>> and harder to maintain. I was wondering whether any of you tried
>> alternative approaches. What I would prefer to do is to create my domain
>> objects in tests using builder pattern and have DBUnit data set
>> generated based on them. I already experimented with this approach and
>> it results in more readable and maintainable tests in my opinion. Below
>> is an example of functional test using this approach:
>>
>> @Test
>> public void shouldHaveTitleEqualToUserName() throws Exception {
>>     // given
>>     User user = aUser().withId(1).withUsername("John").build();
>>     database.add(user).prepare();
>>
>>     // when
>>     userPage.open(1);
>>
>>     // then
>>     assertTitleEquals("User: John", userPage);
>> }
>>
>> Calling database.add(user).prepare() creates a dataset with given user
>> and calls onSetup() method of underlying databaseTester. This way I
>> don't have to go to external file in order to understand the test and
>> details that are not important in this case like user's e-mail are
>> hidden in the builder. Changes in the database schema are easier because
>> I only need to do them in one place instead of multiple XML data
>>
>> Right now I have a separate data set builder for each entity and I'm
>> using flat XML data set representation internally because I'm most
>> familiar with it. It doesn't have to work this way. What I was thinking
>> I could do is to create generic mechanism based on JPA annotations that
>> could convert any set of JPA entities to IDataSet without using flat XML
>> representation internally.
>>
>> I'm wondering what you think. Have any of you tried to do something
>> similar? Do you see any possible issues that may come up?
>>
>> Regards,
>> Piotr Jagielski
>>
>> ------------------------------------------------------------------------------
>> BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
>> The must-attend event for mobile developers. Connect with experts.
>> Get tools for creating Super Apps. See the latest technologies.
>> Sessions, hands-on labs, demos&  much more. Register early&  save!
>> http://p.sf.net/sfu/rim-blackberry-1
>> _______________________________________________
>> dbunit-user mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/dbunit-user
>>
> ------------------------------------------------------------------------------
> BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
> The must-attend event for mobile developers. Connect with experts.
> Get tools for creating Super Apps. See the latest technologies.
> Sessions, hands-on labs, demos&  much more. Register early&  save!
> http://p.sf.net/sfu/rim-blackberry-1
> _______________________________________________
> dbunit-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/dbunit-user
>


------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
The must-attend event for mobile developers. Connect with experts.
Get tools for creating Super Apps. See the latest technologies.
Sessions, hands-on labs, demos & much more. Register early & save!
http://p.sf.net/sfu/rim-blackberry-1
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: dataset based on JPA entities

Marc Philipp-2
Piotr,

if I understand you correctly, you would like your database.add() method to accept an JPA entity and use the information from JPA annotations on the corresponding class to find out table and column names, and use the values stored in the class instance to fill it. Right?

Marc


Am 07.08.2011 um 21:18 schrieb Piotr Jagielski:

> Marc,
>
> Actually, I found your project a while ago. Good work! My idea is to
> build JPA entities and generate data sets based on them as opposed to
> building data sets directly. Perhaps what you created could be useful to
> accomplish that.
>
> Regards,
> Piotr
>
> On 2011-08-07 21:07, Marc Philipp wrote:
>> Hi Piotr,
>>
>> I came up with something very similar a while ago. It can be found on
>> GitHub: https://github.com/marcphilipp/dbunit-datasetbuilder
>>
>> Unfortunately, nobody seemed to be interested at that time.
>>
>> Here's an example:
>>
>> DataSetBuilder builder = new DataSetBuilder();
>>
>> // Using strings as column names, not type-safe
>> builder.newRow("PERSON").with("NAME", "Bob").with("AGE", 18).add();
>>
>> // Using ColumnSpecs to identify columns, type-safe!
>> ColumnSpec<String>  name = ColumnSpec.newColumn("NAME")
>> ColumnSpec<Integer>  age = ColumnSpec.newColumn("AGE");
>> builder.newRow("PERSON").with(name, "Alice").with(age, 23).add();
>>
>> // New columns are added on the fly
>> builder.newRow("PERSON").with(NAME, "Charlie").with("LAST_NAME", "Brown").add();
>>
>> IDataSet dataSet = builder.build();
>>
>>
>> Best regards,
>>
>> Marc
>>
>>
>>
>>
>> 2011/8/7 Piotr Jagielski<[hidden email]>:
>>> Hello,
>>>
>>> I'm a new user of DBUnit and I enjoy using it very much except for the
>>> fact that it forces me to externalize my test data in XML. I try to keep
>>> my data sets small but their number is still growing and getting harder
>>> and harder to maintain. I was wondering whether any of you tried
>>> alternative approaches. What I would prefer to do is to create my domain
>>> objects in tests using builder pattern and have DBUnit data set
>>> generated based on them. I already experimented with this approach and
>>> it results in more readable and maintainable tests in my opinion. Below
>>> is an example of functional test using this approach:
>>>
>>> @Test
>>> public void shouldHaveTitleEqualToUserName() throws Exception {
>>>    // given
>>>    User user = aUser().withId(1).withUsername("John").build();
>>>    database.add(user).prepare();
>>>
>>>    // when
>>>    userPage.open(1);
>>>
>>>    // then
>>>    assertTitleEquals("User: John", userPage);
>>> }
>>>
>>> Calling database.add(user).prepare() creates a dataset with given user
>>> and calls onSetup() method of underlying databaseTester. This way I
>>> don't have to go to external file in order to understand the test and
>>> details that are not important in this case like user's e-mail are
>>> hidden in the builder. Changes in the database schema are easier because
>>> I only need to do them in one place instead of multiple XML data
>>>
>>> Right now I have a separate data set builder for each entity and I'm
>>> using flat XML data set representation internally because I'm most
>>> familiar with it. It doesn't have to work this way. What I was thinking
>>> I could do is to create generic mechanism based on JPA annotations that
>>> could convert any set of JPA entities to IDataSet without using flat XML
>>> representation internally.
>>>
>>> I'm wondering what you think. Have any of you tried to do something
>>> similar? Do you see any possible issues that may come up?
>>>
>>> Regards,
>>> Piotr Jagielski
>>>
>>> ------------------------------------------------------------------------------
>>> BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
>>> The must-attend event for mobile developers. Connect with experts.
>>> Get tools for creating Super Apps. See the latest technologies.
>>> Sessions, hands-on labs, demos&  much more. Register early&  save!
>>> http://p.sf.net/sfu/rim-blackberry-1
>>> _______________________________________________
>>> dbunit-user mailing list
>>> [hidden email]
>>> https://lists.sourceforge.net/lists/listinfo/dbunit-user
>>>
>> ------------------------------------------------------------------------------
>> BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
>> The must-attend event for mobile developers. Connect with experts.
>> Get tools for creating Super Apps. See the latest technologies.
>> Sessions, hands-on labs, demos&  much more. Register early&  save!
>> http://p.sf.net/sfu/rim-blackberry-1
>> _______________________________________________
>> dbunit-user mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/dbunit-user
>>
>
>
> ------------------------------------------------------------------------------
> BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
> The must-attend event for mobile developers. Connect with experts.
> Get tools for creating Super Apps. See the latest technologies.
> Sessions, hands-on labs, demos & much more. Register early & save!
> http://p.sf.net/sfu/rim-blackberry-1
> _______________________________________________
> dbunit-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/dbunit-user


------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
The must-attend event for mobile developers. Connect with experts.
Get tools for creating Super Apps. See the latest technologies.
Sessions, hands-on labs, demos & much more. Register early & save!
http://p.sf.net/sfu/rim-blackberry-1
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: dataset based on JPA entities

Piotr Jagielski-3
Exactly!

Regards,
Piotr

On 2011-08-07 21:27, Marc Philipp wrote:

> Piotr,
>
> if I understand you correctly, you would like your database.add() method to accept an JPA entity and use the information from JPA annotations on the corresponding class to find out table and column names, and use the values stored in the class instance to fill it. Right?
>
> Marc
>
>
> Am 07.08.2011 um 21:18 schrieb Piotr Jagielski:
>
>> Marc,
>>
>> Actually, I found your project a while ago. Good work! My idea is to
>> build JPA entities and generate data sets based on them as opposed to
>> building data sets directly. Perhaps what you created could be useful to
>> accomplish that.
>>
>> Regards,
>> Piotr
>>
>> On 2011-08-07 21:07, Marc Philipp wrote:
>>> Hi Piotr,
>>>
>>> I came up with something very similar a while ago. It can be found on
>>> GitHub: https://github.com/marcphilipp/dbunit-datasetbuilder
>>>
>>> Unfortunately, nobody seemed to be interested at that time.
>>>
>>> Here's an example:
>>>
>>> DataSetBuilder builder = new DataSetBuilder();
>>>
>>> // Using strings as column names, not type-safe
>>> builder.newRow("PERSON").with("NAME", "Bob").with("AGE", 18).add();
>>>
>>> // Using ColumnSpecs to identify columns, type-safe!
>>> ColumnSpec<String>   name = ColumnSpec.newColumn("NAME")
>>> ColumnSpec<Integer>   age = ColumnSpec.newColumn("AGE");
>>> builder.newRow("PERSON").with(name, "Alice").with(age, 23).add();
>>>
>>> // New columns are added on the fly
>>> builder.newRow("PERSON").with(NAME, "Charlie").with("LAST_NAME", "Brown").add();
>>>
>>> IDataSet dataSet = builder.build();
>>>
>>>
>>> Best regards,
>>>
>>> Marc
>>>
>>>
>>>
>>>
>>> 2011/8/7 Piotr Jagielski<[hidden email]>:
>>>> Hello,
>>>>
>>>> I'm a new user of DBUnit and I enjoy using it very much except for the
>>>> fact that it forces me to externalize my test data in XML. I try to keep
>>>> my data sets small but their number is still growing and getting harder
>>>> and harder to maintain. I was wondering whether any of you tried
>>>> alternative approaches. What I would prefer to do is to create my domain
>>>> objects in tests using builder pattern and have DBUnit data set
>>>> generated based on them. I already experimented with this approach and
>>>> it results in more readable and maintainable tests in my opinion. Below
>>>> is an example of functional test using this approach:
>>>>
>>>> @Test
>>>> public void shouldHaveTitleEqualToUserName() throws Exception {
>>>>     // given
>>>>     User user = aUser().withId(1).withUsername("John").build();
>>>>     database.add(user).prepare();
>>>>
>>>>     // when
>>>>     userPage.open(1);
>>>>
>>>>     // then
>>>>     assertTitleEquals("User: John", userPage);
>>>> }
>>>>
>>>> Calling database.add(user).prepare() creates a dataset with given user
>>>> and calls onSetup() method of underlying databaseTester. This way I
>>>> don't have to go to external file in order to understand the test and
>>>> details that are not important in this case like user's e-mail are
>>>> hidden in the builder. Changes in the database schema are easier because
>>>> I only need to do them in one place instead of multiple XML data
>>>>
>>>> Right now I have a separate data set builder for each entity and I'm
>>>> using flat XML data set representation internally because I'm most
>>>> familiar with it. It doesn't have to work this way. What I was thinking
>>>> I could do is to create generic mechanism based on JPA annotations that
>>>> could convert any set of JPA entities to IDataSet without using flat XML
>>>> representation internally.
>>>>
>>>> I'm wondering what you think. Have any of you tried to do something
>>>> similar? Do you see any possible issues that may come up?
>>>>
>>>> Regards,
>>>> Piotr Jagielski
>>>>
>>>> ------------------------------------------------------------------------------
>>>> BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
>>>> The must-attend event for mobile developers. Connect with experts.
>>>> Get tools for creating Super Apps. See the latest technologies.
>>>> Sessions, hands-on labs, demos&   much more. Register early&   save!
>>>> http://p.sf.net/sfu/rim-blackberry-1
>>>> _______________________________________________
>>>> dbunit-user mailing list
>>>> [hidden email]
>>>> https://lists.sourceforge.net/lists/listinfo/dbunit-user
>>>>
>>> ------------------------------------------------------------------------------
>>> BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
>>> The must-attend event for mobile developers. Connect with experts.
>>> Get tools for creating Super Apps. See the latest technologies.
>>> Sessions, hands-on labs, demos&   much more. Register early&   save!
>>> http://p.sf.net/sfu/rim-blackberry-1
>>> _______________________________________________
>>> dbunit-user mailing list
>>> [hidden email]
>>> https://lists.sourceforge.net/lists/listinfo/dbunit-user
>>>
>>
>> ------------------------------------------------------------------------------
>> BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
>> The must-attend event for mobile developers. Connect with experts.
>> Get tools for creating Super Apps. See the latest technologies.
>> Sessions, hands-on labs, demos&  much more. Register early&  save!
>> http://p.sf.net/sfu/rim-blackberry-1
>> _______________________________________________
>> dbunit-user mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/dbunit-user
>
> ------------------------------------------------------------------------------
> BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
> The must-attend event for mobile developers. Connect with experts.
> Get tools for creating Super Apps. See the latest technologies.
> Sessions, hands-on labs, demos&  much more. Register early&  save!
> http://p.sf.net/sfu/rim-blackberry-1
> _______________________________________________
> dbunit-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/dbunit-user
>


------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
The must-attend event for mobile developers. Connect with experts.
Get tools for creating Super Apps. See the latest technologies.
Sessions, hands-on labs, demos & much more. Register early & save!
http://p.sf.net/sfu/rim-blackberry-1
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user