From bbce155a78d28db6d5bf334a01ffce43f309c365 Mon Sep 17 00:00:00 2001 From: "sumit.sg34" Date: Tue, 9 Apr 2019 22:44:45 +0530 Subject: [PATCH 1/4] BAEL-2841 spring data jpa populators --- persistence-modules/spring-data-jpa-2/pom.xml | 56 +++++++++++-------- .../com/baeldung/config/JpaPopulators.java | 33 +++++++++++ .../main/java/com/baeldung/entity/Fruit.java | 2 + .../src/main/resources/fruit-data.json | 14 +++++ .../src/main/resources/fruit-data.xml | 7 +++ .../repository/FruitPopulatorTest.java | 53 ++++++++++++++++++ 6 files changed, 142 insertions(+), 23 deletions(-) create mode 100644 persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/config/JpaPopulators.java create mode 100644 persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.json create mode 100644 persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.xml create mode 100644 persistence-modules/spring-data-jpa-2/src/test/java/com/baeldung/repository/FruitPopulatorTest.java diff --git a/persistence-modules/spring-data-jpa-2/pom.xml b/persistence-modules/spring-data-jpa-2/pom.xml index 8e46112659..5080ad9bd3 100644 --- a/persistence-modules/spring-data-jpa-2/pom.xml +++ b/persistence-modules/spring-data-jpa-2/pom.xml @@ -1,30 +1,40 @@ - 4.0.0 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 com.baeldung - spring-data-jpa-2 + spring-data-jpa-2 spring-data-jpa - - - parent-boot-2 - com.baeldung - 0.0.1-SNAPSHOT - ../../parent-boot-2 - - - - org.springframework.boot - spring-boot-starter-data-jpa - - - - com.h2database - h2 - - - + + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + com.h2database + h2 + + + + com.fasterxml.jackson.core + jackson-databind + + + + org.springframework + spring-oxm + + + \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/config/JpaPopulators.java b/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/config/JpaPopulators.java new file mode 100644 index 0000000000..cb4b32aabc --- /dev/null +++ b/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/config/JpaPopulators.java @@ -0,0 +1,33 @@ +package com.baeldung.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.data.repository.init.Jackson2RepositoryPopulatorFactoryBean; +import org.springframework.data.repository.init.UnmarshallerRepositoryPopulatorFactoryBean; +import org.springframework.oxm.jaxb.Jaxb2Marshaller; + +import com.baeldung.entity.Fruit; + +@Configuration +public class JpaPopulators { + + @Bean + public Jackson2RepositoryPopulatorFactoryBean getRespositoryPopulator() throws Exception { + Jackson2RepositoryPopulatorFactoryBean factory = new Jackson2RepositoryPopulatorFactoryBean(); + factory.setResources(new Resource[] { (Resource) new ClassPathResource("fruit-data.json") }); + return factory; + } + + @Bean + public UnmarshallerRepositoryPopulatorFactoryBean repositoryPopulator() { + Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); + marshaller.setClassesToBeBound(Fruit.class); + UnmarshallerRepositoryPopulatorFactoryBean factory = new UnmarshallerRepositoryPopulatorFactoryBean(); + factory.setUnmarshaller(marshaller); + factory.setResources(new Resource[] { new ClassPathResource("fruit-data.xml") }); + return factory; + } + +} diff --git a/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/entity/Fruit.java b/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/entity/Fruit.java index f82022e67e..d45ac33db8 100644 --- a/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/entity/Fruit.java +++ b/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/entity/Fruit.java @@ -2,7 +2,9 @@ package com.baeldung.entity; import javax.persistence.Entity; import javax.persistence.Id; +import javax.xml.bind.annotation.XmlRootElement; +@XmlRootElement @Entity public class Fruit { diff --git a/persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.json b/persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.json new file mode 100644 index 0000000000..214ea18e4d --- /dev/null +++ b/persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.json @@ -0,0 +1,14 @@ +[ + { + "_class": "com.baeldung.entity.Fruit", + "name": "apple", + "color": "red", + "id": 1 + }, + { + "_class": "com.baeldung.entity.Fruit", + "name": "guava", + "color": "green", + "id": 2 + } +] \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.xml b/persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.xml new file mode 100644 index 0000000000..fe6eb71faf --- /dev/null +++ b/persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.xml @@ -0,0 +1,7 @@ + + + + 3 + mango + yellow + diff --git a/persistence-modules/spring-data-jpa-2/src/test/java/com/baeldung/repository/FruitPopulatorTest.java b/persistence-modules/spring-data-jpa-2/src/test/java/com/baeldung/repository/FruitPopulatorTest.java new file mode 100644 index 0000000000..8760c69ce8 --- /dev/null +++ b/persistence-modules/spring-data-jpa-2/src/test/java/com/baeldung/repository/FruitPopulatorTest.java @@ -0,0 +1,53 @@ +package com.baeldung.repository; + +import static org.junit.Assert.assertEquals; + +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.entity.Fruit; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class FruitPopulatorTest { + + @Autowired + private FruitRepository fruitRepository; + + @Test + public void givenFruitJsonPopulatorThenShouldInsertRecordOnStart() { + + List fruits = fruitRepository.findAll(); + assertEquals("record count is not matching", 3, fruits.size()); + + fruits.forEach(fruit -> { + if (1 == fruit.getId()) { + assertEquals("This is not an apple", "apple", fruit.getName()); + assertEquals("It is not a red colored fruit", "red", fruit.getColor()); + } else if (2 == fruit.getId()) { + assertEquals("This is not a guava", "guava", fruit.getName()); + assertEquals("It is not a green colored fruit", "green", fruit.getColor()); + } + }); + } + + @Test + public void givenFruitXmlPopulatorThenShouldInsertRecordOnStart() { + + List fruits = fruitRepository.findAll(); + assertEquals("record count is not matching", 3, fruits.size()); + + fruits.forEach(fruit -> { + if (3 == fruit.getId()) { + assertEquals("This is not a mango", "mango", fruit.getName()); + assertEquals("It is not a yellow colored fruit", "yellow", fruit.getColor()); + } + }); + } + +} From 7bb364a2f20c059ff53ec38e0b11e5d2174b0b76 Mon Sep 17 00:00:00 2001 From: "sumit.sg34" Date: Thu, 11 Apr 2019 23:01:50 +0530 Subject: [PATCH 2/4] BAEL-2841 updated test cases and test data --- .../com/baeldung/config/JpaPopulators.java | 66 +++++++------- .../src/main/resources/fruit-data.xml | 14 +-- .../repository/FruitPopulatorTest.java | 91 ++++++++----------- 3 files changed, 78 insertions(+), 93 deletions(-) diff --git a/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/config/JpaPopulators.java b/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/config/JpaPopulators.java index cb4b32aabc..0791df85f4 100644 --- a/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/config/JpaPopulators.java +++ b/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/config/JpaPopulators.java @@ -1,33 +1,33 @@ -package com.baeldung.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.Resource; -import org.springframework.data.repository.init.Jackson2RepositoryPopulatorFactoryBean; -import org.springframework.data.repository.init.UnmarshallerRepositoryPopulatorFactoryBean; -import org.springframework.oxm.jaxb.Jaxb2Marshaller; - -import com.baeldung.entity.Fruit; - -@Configuration -public class JpaPopulators { - - @Bean - public Jackson2RepositoryPopulatorFactoryBean getRespositoryPopulator() throws Exception { - Jackson2RepositoryPopulatorFactoryBean factory = new Jackson2RepositoryPopulatorFactoryBean(); - factory.setResources(new Resource[] { (Resource) new ClassPathResource("fruit-data.json") }); - return factory; - } - - @Bean - public UnmarshallerRepositoryPopulatorFactoryBean repositoryPopulator() { - Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); - marshaller.setClassesToBeBound(Fruit.class); - UnmarshallerRepositoryPopulatorFactoryBean factory = new UnmarshallerRepositoryPopulatorFactoryBean(); - factory.setUnmarshaller(marshaller); - factory.setResources(new Resource[] { new ClassPathResource("fruit-data.xml") }); - return factory; - } - -} +package com.baeldung.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.data.repository.init.Jackson2RepositoryPopulatorFactoryBean; +import org.springframework.data.repository.init.UnmarshallerRepositoryPopulatorFactoryBean; +import org.springframework.oxm.jaxb.Jaxb2Marshaller; + +import com.baeldung.entity.Fruit; + +@Configuration +public class JpaPopulators { + + @Bean + public Jackson2RepositoryPopulatorFactoryBean getRespositoryPopulator() throws Exception { + Jackson2RepositoryPopulatorFactoryBean factory = new Jackson2RepositoryPopulatorFactoryBean(); + factory.setResources(new Resource[] { new ClassPathResource("fruit-data.json") }); + return factory; + } + + @Bean + public UnmarshallerRepositoryPopulatorFactoryBean repositoryPopulator() { + Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); + marshaller.setClassesToBeBound(Fruit.class); + UnmarshallerRepositoryPopulatorFactoryBean factory = new UnmarshallerRepositoryPopulatorFactoryBean(); + factory.setUnmarshaller(marshaller); + factory.setResources(new Resource[] { new ClassPathResource("fruit-data.xml") }); + return factory; + } + +} diff --git a/persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.xml b/persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.xml index fe6eb71faf..d0101e3f4d 100644 --- a/persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.xml +++ b/persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.xml @@ -1,7 +1,7 @@ - - - - 3 - mango - yellow - + + + + 1 + apple + red + diff --git a/persistence-modules/spring-data-jpa-2/src/test/java/com/baeldung/repository/FruitPopulatorTest.java b/persistence-modules/spring-data-jpa-2/src/test/java/com/baeldung/repository/FruitPopulatorTest.java index 8760c69ce8..29ef52dcef 100644 --- a/persistence-modules/spring-data-jpa-2/src/test/java/com/baeldung/repository/FruitPopulatorTest.java +++ b/persistence-modules/spring-data-jpa-2/src/test/java/com/baeldung/repository/FruitPopulatorTest.java @@ -1,53 +1,38 @@ -package com.baeldung.repository; - -import static org.junit.Assert.assertEquals; - -import java.util.List; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -import com.baeldung.entity.Fruit; - -@RunWith(SpringRunner.class) -@SpringBootTest -public class FruitPopulatorTest { - - @Autowired - private FruitRepository fruitRepository; - - @Test - public void givenFruitJsonPopulatorThenShouldInsertRecordOnStart() { - - List fruits = fruitRepository.findAll(); - assertEquals("record count is not matching", 3, fruits.size()); - - fruits.forEach(fruit -> { - if (1 == fruit.getId()) { - assertEquals("This is not an apple", "apple", fruit.getName()); - assertEquals("It is not a red colored fruit", "red", fruit.getColor()); - } else if (2 == fruit.getId()) { - assertEquals("This is not a guava", "guava", fruit.getName()); - assertEquals("It is not a green colored fruit", "green", fruit.getColor()); - } - }); - } - - @Test - public void givenFruitXmlPopulatorThenShouldInsertRecordOnStart() { - - List fruits = fruitRepository.findAll(); - assertEquals("record count is not matching", 3, fruits.size()); - - fruits.forEach(fruit -> { - if (3 == fruit.getId()) { - assertEquals("This is not a mango", "mango", fruit.getName()); - assertEquals("It is not a yellow colored fruit", "yellow", fruit.getColor()); - } - }); - } - -} +package com.baeldung.repository; + +import static org.junit.Assert.assertEquals; + +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.entity.Fruit; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class FruitPopulatorTest { + + @Autowired + private FruitRepository fruitRepository; + + @Test + public void givenFruitJsonPopulatorThenShouldInsertRecordOnStart() { + + List fruits = fruitRepository.findAll(); + assertEquals("record count is not matching", 2, fruits.size()); + + fruits.forEach(fruit -> { + if (1 == fruit.getId()) { + assertEquals("apple", fruit.getName()); + assertEquals("red", fruit.getColor()); + } else if (2 == fruit.getId()) { + assertEquals("guava", fruit.getName()); + assertEquals("green", fruit.getColor()); + } + }); + } +} From 93d6d243ac16562b3ff557d8dbeb88ab0ee64ec2 Mon Sep 17 00:00:00 2001 From: "sumit.sg34" Date: Sun, 14 Apr 2019 20:56:40 +0530 Subject: [PATCH 3/4] formatting done : removing tabs --- persistence-modules/spring-data-jpa-2/pom.xml | 61 ++++++++++--------- .../src/main/resources/fruit-data.json | 26 ++++---- .../src/main/resources/fruit-data.xml | 6 +- 3 files changed, 47 insertions(+), 46 deletions(-) diff --git a/persistence-modules/spring-data-jpa-2/pom.xml b/persistence-modules/spring-data-jpa-2/pom.xml index 5080ad9bd3..251007ba6d 100644 --- a/persistence-modules/spring-data-jpa-2/pom.xml +++ b/persistence-modules/spring-data-jpa-2/pom.xml @@ -1,40 +1,41 @@ - 4.0.0 - com.baeldung - spring-data-jpa-2 - spring-data-jpa + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - - parent-boot-2 - com.baeldung - 0.0.1-SNAPSHOT - ../../parent-boot-2 - + com.baeldung + spring-data-jpa-2 + spring-data-jpa - - - org.springframework.boot - spring-boot-starter-data-jpa - + + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../../parent-boot-2 + - - com.h2database - h2 - + + + org.springframework.boot + spring-boot-starter-data-jpa + - - com.fasterxml.jackson.core - jackson-databind - + + com.h2database + h2 + - - org.springframework - spring-oxm - + + com.fasterxml.jackson.core + jackson-databind + - + + org.springframework + spring-oxm + + + \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.json b/persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.json index 214ea18e4d..6dc44e2586 100644 --- a/persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.json +++ b/persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.json @@ -1,14 +1,14 @@ -[ - { - "_class": "com.baeldung.entity.Fruit", - "name": "apple", - "color": "red", - "id": 1 - }, - { - "_class": "com.baeldung.entity.Fruit", - "name": "guava", - "color": "green", - "id": 2 - } +[ + { + "_class": "com.baeldung.entity.Fruit", + "name": "apple", + "color": "red", + "id": 1 + }, + { + "_class": "com.baeldung.entity.Fruit", + "name": "guava", + "color": "green", + "id": 2 + } ] \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.xml b/persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.xml index d0101e3f4d..d87ae28f1e 100644 --- a/persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.xml +++ b/persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.xml @@ -1,7 +1,7 @@ - 1 - apple - red + 1 + apple + red From 029ab027edb2d30eb620be5d78dc7388c94c2302 Mon Sep 17 00:00:00 2001 From: "sumit.sg34" Date: Wed, 17 Apr 2019 18:52:51 +0530 Subject: [PATCH 4/4] BAEL-2841 updated code to use multiple xml files --- .../main/java/com/baeldung/config/JpaPopulators.java | 10 ++++++---- .../resources/{fruit-data.xml => apple-fruit-data.xml} | 0 .../src/main/resources/guava-fruit-data.xml | 7 +++++++ 3 files changed, 13 insertions(+), 4 deletions(-) rename persistence-modules/spring-data-jpa-2/src/main/resources/{fruit-data.xml => apple-fruit-data.xml} (100%) create mode 100644 persistence-modules/spring-data-jpa-2/src/main/resources/guava-fruit-data.xml diff --git a/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/config/JpaPopulators.java b/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/config/JpaPopulators.java index 0791df85f4..24348d31c5 100644 --- a/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/config/JpaPopulators.java +++ b/persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/config/JpaPopulators.java @@ -22,11 +22,13 @@ public class JpaPopulators { @Bean public UnmarshallerRepositoryPopulatorFactoryBean repositoryPopulator() { - Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); - marshaller.setClassesToBeBound(Fruit.class); + + Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller(); + unmarshaller.setClassesToBeBound(Fruit.class); + UnmarshallerRepositoryPopulatorFactoryBean factory = new UnmarshallerRepositoryPopulatorFactoryBean(); - factory.setUnmarshaller(marshaller); - factory.setResources(new Resource[] { new ClassPathResource("fruit-data.xml") }); + factory.setUnmarshaller(unmarshaller); + factory.setResources(new Resource[] { new ClassPathResource("apple-fruit-data.xml"), new ClassPathResource("guava-fruit-data.xml") }); return factory; } diff --git a/persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.xml b/persistence-modules/spring-data-jpa-2/src/main/resources/apple-fruit-data.xml similarity index 100% rename from persistence-modules/spring-data-jpa-2/src/main/resources/fruit-data.xml rename to persistence-modules/spring-data-jpa-2/src/main/resources/apple-fruit-data.xml diff --git a/persistence-modules/spring-data-jpa-2/src/main/resources/guava-fruit-data.xml b/persistence-modules/spring-data-jpa-2/src/main/resources/guava-fruit-data.xml new file mode 100644 index 0000000000..ffd75bb4bb --- /dev/null +++ b/persistence-modules/spring-data-jpa-2/src/main/resources/guava-fruit-data.xml @@ -0,0 +1,7 @@ + + + + 2 + guava + green +