From bbce155a78d28db6d5bf334a01ffce43f309c365 Mon Sep 17 00:00:00 2001 From: "sumit.sg34" Date: Tue, 9 Apr 2019 22:44:45 +0530 Subject: [PATCH 01/12] 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 02/12] 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 03/12] 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 d0a6c5701a6cadc509d92ede72957a8e170b2994 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Sun, 14 Apr 2019 22:02:14 +0300 Subject: [PATCH 04/12] BAEL-2661 - Groovy def keyword --- core-groovy-2/README.md | 5 + core-groovy-2/pom.xml | 123 ++++++++++++++++++ .../baeldung/defkeyword/DefUnitTest.groovy | 73 +++++++++++ pom.xml | 2 + 4 files changed, 203 insertions(+) create mode 100644 core-groovy-2/README.md create mode 100644 core-groovy-2/pom.xml create mode 100644 core-groovy-2/src/test/groovy/com/baeldung/defkeyword/DefUnitTest.groovy diff --git a/core-groovy-2/README.md b/core-groovy-2/README.md new file mode 100644 index 0000000000..0aed5602ab --- /dev/null +++ b/core-groovy-2/README.md @@ -0,0 +1,5 @@ +# Groovy + +## Relevant articles: + +- [Groovy def Keyword] diff --git a/core-groovy-2/pom.xml b/core-groovy-2/pom.xml new file mode 100644 index 0000000000..68382e613b --- /dev/null +++ b/core-groovy-2/pom.xml @@ -0,0 +1,123 @@ + + + 4.0.0 + core-groovy-2 + 1.0-SNAPSHOT + core-groovy-2 + jar + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + org.codehaus.groovy + groovy + ${groovy.version} + + + org.codehaus.groovy + groovy-all + ${groovy-all.version} + pom + + + org.codehaus.groovy + groovy-dateutil + ${groovy.version} + + + org.codehaus.groovy + groovy-sql + ${groovy-sql.version} + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + org.hsqldb + hsqldb + ${hsqldb.version} + test + + + org.spockframework + spock-core + ${spock-core.version} + test + + + + + + + org.codehaus.gmavenplus + gmavenplus-plugin + ${gmavenplus-plugin.version} + + + + addSources + addTestSources + compile + compileTests + + + + + + maven-failsafe-plugin + ${maven-failsafe-plugin.version} + + + org.junit.platform + junit-platform-surefire-provider + ${junit.platform.version} + + + + + junit5 + + integration-test + verify + + + + **/*Test5.java + + + + + + + + + + + central + http://jcenter.bintray.com + + + + + 1.0.0 + + + + 2.5.6 + 2.5.6 + 2.5.6 + 2.4.0 + 1.1-groovy-2.4 + 1.6 + + + diff --git a/core-groovy-2/src/test/groovy/com/baeldung/defkeyword/DefUnitTest.groovy b/core-groovy-2/src/test/groovy/com/baeldung/defkeyword/DefUnitTest.groovy new file mode 100644 index 0000000000..baab7455a5 --- /dev/null +++ b/core-groovy-2/src/test/groovy/com/baeldung/defkeyword/DefUnitTest.groovy @@ -0,0 +1,73 @@ +package com.baeldung.defkeyword + +import org.codehaus.groovy.runtime.NullObject +import org.codehaus.groovy.runtime.typehandling.GroovyCastException + +import groovy.transform.TypeChecked +import groovy.transform.TypeCheckingMode + +@TypeChecked +class DefUnitTest extends GroovyTestCase { + + def id + def firstName = "Samwell" + def listOfCountries = ['USA', 'UK', 'FRANCE', 'INDIA'] + + @TypeChecked(TypeCheckingMode.SKIP) + def multiply(x, y) { + return x*y + } + + @TypeChecked(TypeCheckingMode.SKIP) + void testDef() { + + def list + assert list.getClass() == org.codehaus.groovy.runtime.NullObject + assert list.is(null) + + list = [1,2,4] + assert list instanceof ArrayList + + int sum = 200 + try { + sum = [12] //GroovyCastException + sum = "nill" //GroovyCastException + } catch(GroovyCastException) { + println "Cannot assign anything other than integer" + } + + def rate + assert rate == null + assert rate.getClass() == org.codehaus.groovy.runtime.NullObject + + rate = 12 + assert rate instanceof Integer + + rate = "Not Available" + assert rate instanceof String + + rate = [1, 4] + assert rate instanceof List + + assert divide(12, 3) instanceof BigDecimal + assert divide(1, 0) instanceof String + + } + + def divide(int x, int y) { + if(y==0) { + return "Should not divide by 0" + } else { + return x/y + } + } + + def greetMsg() { + println "Hello! I am Groovy" + } + + void testDefVsType() { + def int count + assert count instanceof Integer + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index bdd8403231..8746f8492b 100644 --- a/pom.xml +++ b/pom.xml @@ -376,6 +376,7 @@ cdi checker-plugin core-groovy + core-groovy-2 @@ -1034,6 +1035,7 @@ cdi checker-plugin core-groovy + core-groovy-2 core-java-8 From 2e99f89775e545c9345ee09d7dcfa84f20f66310 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Tue, 16 Apr 2019 12:07:35 +0300 Subject: [PATCH 05/12] BAEL-2661 - Groovy def keyword - variable names --- .../com/baeldung/defkeyword/DefUnitTest.groovy | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/core-groovy-2/src/test/groovy/com/baeldung/defkeyword/DefUnitTest.groovy b/core-groovy-2/src/test/groovy/com/baeldung/defkeyword/DefUnitTest.groovy index baab7455a5..310d97d3fd 100644 --- a/core-groovy-2/src/test/groovy/com/baeldung/defkeyword/DefUnitTest.groovy +++ b/core-groovy-2/src/test/groovy/com/baeldung/defkeyword/DefUnitTest.groovy @@ -19,7 +19,7 @@ class DefUnitTest extends GroovyTestCase { } @TypeChecked(TypeCheckingMode.SKIP) - void testDef() { + void testDefVariableDeclaration() { def list assert list.getClass() == org.codehaus.groovy.runtime.NullObject @@ -27,15 +27,21 @@ class DefUnitTest extends GroovyTestCase { list = [1,2,4] assert list instanceof ArrayList - - int sum = 200 + } + + @TypeChecked(TypeCheckingMode.SKIP) + void testTypeVariables() { + int rate = 200 try { - sum = [12] //GroovyCastException - sum = "nill" //GroovyCastException + rate = [12] //GroovyCastException + rate = "nill" //GroovyCastException } catch(GroovyCastException) { println "Cannot assign anything other than integer" } - + } + + @TypeChecked(TypeCheckingMode.SKIP) + void testDefVariableMultipleAssignment() { def rate assert rate == null assert rate.getClass() == org.codehaus.groovy.runtime.NullObject From 274cd5b4b142407a3e95cb19419b1ac631927a22 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Wed, 17 Apr 2019 11:01:36 +0300 Subject: [PATCH 06/12] BAEL-2661 - Groovy def keyword - conflicts resolved --- core-groovy-2/pom.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core-groovy-2/pom.xml b/core-groovy-2/pom.xml index 69a390a3a0..9819bc4c16 100644 --- a/core-groovy-2/pom.xml +++ b/core-groovy-2/pom.xml @@ -96,6 +96,8 @@ + + maven-surefire-plugin 2.20.1 From 029ab027edb2d30eb620be5d78dc7388c94c2302 Mon Sep 17 00:00:00 2001 From: "sumit.sg34" Date: Wed, 17 Apr 2019 18:52:51 +0530 Subject: [PATCH 07/12] 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 + From dda58cddc2fa7deadcc0baa50e20f13cb6b06a65 Mon Sep 17 00:00:00 2001 From: amit2103 Date: Sat, 20 Apr 2019 18:59:18 +0530 Subject: [PATCH 08/12] [BAEL-13597] - Added new module : httpclient-simple, updated httpclient with new version and code --- httpclient-simple/.gitignore | 13 ++ httpclient-simple/README.md | 10 ++ httpclient-simple/pom.xml | 133 ++++++++++++++++++ .../src/main/resources/logback.xml | 19 +++ .../org/baeldung/httpclient/ResponseUtil.java | 26 ++++ .../base/HttpClientBasicLiveTest.java | 0 .../src/test/resources/.gitignore | 13 ++ httpclient/README.md | 1 - httpclient/pom.xml | 5 +- .../httpclient/HttpAsyncClientLiveTest.java | 7 +- .../httpclient/HttpsClientSslLiveTest.java | 68 ++++----- ...ttpClientConnectionManagementLiveTest.java | 3 +- pom.xml | 2 + 13 files changed, 259 insertions(+), 41 deletions(-) create mode 100644 httpclient-simple/.gitignore create mode 100644 httpclient-simple/README.md create mode 100644 httpclient-simple/pom.xml create mode 100644 httpclient-simple/src/main/resources/logback.xml create mode 100644 httpclient-simple/src/test/java/org/baeldung/httpclient/ResponseUtil.java rename {httpclient => httpclient-simple}/src/test/java/org/baeldung/httpclient/base/HttpClientBasicLiveTest.java (100%) create mode 100644 httpclient-simple/src/test/resources/.gitignore diff --git a/httpclient-simple/.gitignore b/httpclient-simple/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/httpclient-simple/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/httpclient-simple/README.md b/httpclient-simple/README.md new file mode 100644 index 0000000000..aa66c11b1e --- /dev/null +++ b/httpclient-simple/README.md @@ -0,0 +1,10 @@ +========= +## HttpClient 4.x Cookbooks and Examples + +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring + + +### Relevant Articles: + +- [HttpClient 4 – Get the Status Code](http://www.baeldung.com/httpclient-status-code) \ No newline at end of file diff --git a/httpclient-simple/pom.xml b/httpclient-simple/pom.xml new file mode 100644 index 0000000000..1ad68d2804 --- /dev/null +++ b/httpclient-simple/pom.xml @@ -0,0 +1,133 @@ + + 4.0.0 + com.baeldung + httpclient-simple + 0.1-SNAPSHOT + httpclient-simple + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../parent-java + + + + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + + org.apache.httpcomponents + httpclient + ${httpclient.version} + + + commons-logging + commons-logging + + + + + org.apache.httpcomponents + fluent-hc + ${httpclient.version} + + + commons-logging + commons-logging + + + + + org.apache.httpcomponents + httpmime + ${httpclient.version} + + + commons-codec + commons-codec + ${commons-codec.version} + + + org.apache.httpcomponents + httpasyncclient + ${httpasyncclient.version} + + + commons-logging + commons-logging + + + + + com.github.tomakehurst + wiremock + ${wiremock.version} + test + + + + + httpclient + + + src/main/resources + true + + + + + + + live + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*ManualTest.java + + + **/*LiveTest.java + + + + + + + json + + + + + + + + + + + 19.0 + 3.5 + 1.10 + 4.1.4 + + 2.5.1 + 4.5.8 + + 1.6.1 + + + \ No newline at end of file diff --git a/httpclient-simple/src/main/resources/logback.xml b/httpclient-simple/src/main/resources/logback.xml new file mode 100644 index 0000000000..56af2d397e --- /dev/null +++ b/httpclient-simple/src/main/resources/logback.xml @@ -0,0 +1,19 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + + \ No newline at end of file diff --git a/httpclient-simple/src/test/java/org/baeldung/httpclient/ResponseUtil.java b/httpclient-simple/src/test/java/org/baeldung/httpclient/ResponseUtil.java new file mode 100644 index 0000000000..fd38b95cbe --- /dev/null +++ b/httpclient-simple/src/test/java/org/baeldung/httpclient/ResponseUtil.java @@ -0,0 +1,26 @@ +package org.baeldung.httpclient; + +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; + +import java.io.IOException; + +public final class ResponseUtil { + private ResponseUtil() { + } + + public static void closeResponse(CloseableHttpResponse response) throws IOException { + if (response == null) { + return; + } + + try { + final HttpEntity entity = response.getEntity(); + if (entity != null) { + entity.getContent().close(); + } + } finally { + response.close(); + } + } +} diff --git a/httpclient/src/test/java/org/baeldung/httpclient/base/HttpClientBasicLiveTest.java b/httpclient-simple/src/test/java/org/baeldung/httpclient/base/HttpClientBasicLiveTest.java similarity index 100% rename from httpclient/src/test/java/org/baeldung/httpclient/base/HttpClientBasicLiveTest.java rename to httpclient-simple/src/test/java/org/baeldung/httpclient/base/HttpClientBasicLiveTest.java diff --git a/httpclient-simple/src/test/resources/.gitignore b/httpclient-simple/src/test/resources/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/httpclient-simple/src/test/resources/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/httpclient/README.md b/httpclient/README.md index c5956068c6..ce98d7e72e 100644 --- a/httpclient/README.md +++ b/httpclient/README.md @@ -8,7 +8,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant Articles: - [HttpClient 4 – Send Custom Cookie](http://www.baeldung.com/httpclient-4-cookies) -- [HttpClient 4 – Get the Status Code](http://www.baeldung.com/httpclient-status-code) - [HttpClient 4 – Cancel Request](http://www.baeldung.com/httpclient-cancel-request) - [HttpClient 4 Cookbook](http://www.baeldung.com/httpclient4) - [Unshorten URLs with HttpClient](http://www.baeldung.com/unshorten-url-httpclient) diff --git a/httpclient/pom.xml b/httpclient/pom.xml index c9f9808ede..def3a05816 100644 --- a/httpclient/pom.xml +++ b/httpclient/pom.xml @@ -122,11 +122,10 @@ 19.0 3.5 1.10 - 4.1.2 + 4.1.4 2.5.1 - 4.4.5 - 4.5.3 + 4.5.8 1.6.1 diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpAsyncClientLiveTest.java b/httpclient/src/test/java/org/baeldung/httpclient/HttpAsyncClientLiveTest.java index d39697c0a9..47a587885e 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/HttpAsyncClientLiveTest.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/HttpAsyncClientLiveTest.java @@ -4,7 +4,6 @@ import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertThat; import java.io.IOException; -import java.security.cert.X509Certificate; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; @@ -18,8 +17,7 @@ import org.apache.http.client.CredentialsProvider; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.protocol.HttpClientContext; -import org.apache.http.conn.ssl.SSLConnectionSocketFactory; -import org.apache.http.conn.ssl.SSLContexts; +import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.BasicCredentialsProvider; @@ -31,6 +29,7 @@ import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor; import org.apache.http.nio.reactor.ConnectingIOReactor; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.HttpContext; +import org.apache.http.ssl.SSLContexts; import org.junit.Test; public class HttpAsyncClientLiveTest { @@ -104,7 +103,7 @@ public class HttpAsyncClientLiveTest { final TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true; final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build(); - final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setSSLHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER).setSSLContext(sslContext).build(); + final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).setSSLContext(sslContext).build(); client.start(); final HttpGet request = new HttpGet(HOST_WITH_SSL); diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpsClientSslLiveTest.java b/httpclient/src/test/java/org/baeldung/httpclient/HttpsClientSslLiveTest.java index 4eadfe24d5..9e95905c70 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/HttpsClientSslLiveTest.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/HttpsClientSslLiveTest.java @@ -1,32 +1,31 @@ package org.baeldung.httpclient; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.conn.ClientConnectionManager; -import org.apache.http.conn.scheme.Scheme; -import org.apache.http.conn.scheme.SchemeRegistry; -import org.apache.http.conn.ssl.NoopHostnameVerifier; -import org.apache.http.conn.ssl.SSLConnectionSocketFactory; -import org.apache.http.conn.ssl.SSLSocketFactory; -import org.apache.http.conn.ssl.TrustSelfSignedStrategy; -import org.apache.http.conn.ssl.TrustStrategy; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.impl.conn.PoolingClientConnectionManager; -import org.apache.http.ssl.SSLContextBuilder; -import org.apache.http.ssl.SSLContexts; -import org.junit.Test; - -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLHandshakeException; -import java.io.IOException; -import java.security.GeneralSecurityException; - import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertThat; +import java.io.IOException; +import java.security.GeneralSecurityException; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLHandshakeException; + +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.config.Registry; +import org.apache.http.config.RegistryBuilder; +import org.apache.http.conn.socket.ConnectionSocketFactory; +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.conn.ssl.TrustSelfSignedStrategy; +import org.apache.http.conn.ssl.TrustStrategy; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.ssl.SSLContextBuilder; +import org.apache.http.ssl.SSLContexts; +import org.junit.Test; + /** * This test requires a localhost server over HTTPS
* It should only be manually run, not part of the automated build @@ -50,16 +49,22 @@ public class HttpsClientSslLiveTest { .getStatusCode(), equalTo(200)); } - @SuppressWarnings("deprecation") @Test public final void givenHttpClientPre4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException { final TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true; - final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); - final SchemeRegistry registry = new SchemeRegistry(); - registry.register(new Scheme("https", 443, sf)); - final ClientConnectionManager ccm = new PoolingClientConnectionManager(registry); + + final SSLContext sslContext = SSLContexts.custom() + .loadTrustMaterial(null, acceptingTrustStrategy) + .build(); - final CloseableHttpClient httpClient = new DefaultHttpClient(ccm); + final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); + Registry socketFactoryRegistry = RegistryBuilder. create().register("https", sslsf).build(); + PoolingHttpClientConnectionManager clientConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); + + final CloseableHttpClient httpClient = HttpClients.custom() + .setSSLSocketFactory(sslsf) + .setConnectionManager(clientConnectionManager) + .build(); final HttpGet getMethod = new HttpGet(HOST_WITH_SSL); final HttpResponse response = httpClient.execute(getMethod); @@ -76,10 +81,9 @@ public class HttpsClientSslLiveTest { .loadTrustMaterial(null, acceptingTrustStrategy) .build(); - final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); + final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); final CloseableHttpClient httpClient = HttpClients.custom() - .setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER) .setSSLSocketFactory(sslsf) .build(); diff --git a/httpclient/src/test/java/org/baeldung/httpclient/conn/HttpClientConnectionManagementLiveTest.java b/httpclient/src/test/java/org/baeldung/httpclient/conn/HttpClientConnectionManagementLiveTest.java index 0f8ebefe6c..cf945098db 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/conn/HttpClientConnectionManagementLiveTest.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/conn/HttpClientConnectionManagementLiveTest.java @@ -327,7 +327,8 @@ public class HttpClientConnectionManagementLiveTest { // 8.1 public final void whenHttpClientChecksStaleConns_thenNoExceptions() { poolingConnManager = new PoolingHttpClientConnectionManager(); - client = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().setStaleConnectionCheckEnabled(true).build()).setConnectionManager(poolingConnManager).build(); + poolingConnManager.setValidateAfterInactivity(1000); + client = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().build()).setConnectionManager(poolingConnManager).build(); } @Test diff --git a/pom.xml b/pom.xml index 53ca4291bf..d588dfaba2 100644 --- a/pom.xml +++ b/pom.xml @@ -436,6 +436,7 @@ hazelcast helidon httpclient + httpclient-simple hystrix image-processing @@ -1103,6 +1104,7 @@ hazelcast helidon httpclient + httpclient-simple hystrix image-processing From 1f853d5d515174dd901bba6d42d5620ffb53f4f8 Mon Sep 17 00:00:00 2001 From: Amy DeGregorio Date: Sat, 20 Apr 2019 11:48:39 -0400 Subject: [PATCH 09/12] BAEL 2879 (#6767) * BAEL-2727 Example Code * Example code for BAEL-2879 Jackson YAML --- jackson-2/pom.xml | 14 ++++ .../com/baeldung/jackson/entities/Order.java | 68 +++++++++++++++++++ .../baeldung/jackson/entities/OrderLine.java | 49 +++++++++++++ jackson-2/src/main/resources/orderInput.yaml | 11 +++ .../baeldung/jackson/yaml/YamlUnitTest.java | 63 +++++++++++++++++ 5 files changed, 205 insertions(+) create mode 100644 jackson-2/src/main/java/com/baeldung/jackson/entities/Order.java create mode 100644 jackson-2/src/main/java/com/baeldung/jackson/entities/OrderLine.java create mode 100644 jackson-2/src/main/resources/orderInput.yaml create mode 100644 jackson-2/src/test/java/com/baeldung/jackson/yaml/YamlUnitTest.java diff --git a/jackson-2/pom.xml b/jackson-2/pom.xml index ddbcb81dcc..6b973dd6f5 100644 --- a/jackson-2/pom.xml +++ b/jackson-2/pom.xml @@ -22,6 +22,20 @@ ${jackson.version} + + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + 2.9.8 + + + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + 2.9.8 + + diff --git a/jackson-2/src/main/java/com/baeldung/jackson/entities/Order.java b/jackson-2/src/main/java/com/baeldung/jackson/entities/Order.java new file mode 100644 index 0000000000..2075b7879b --- /dev/null +++ b/jackson-2/src/main/java/com/baeldung/jackson/entities/Order.java @@ -0,0 +1,68 @@ +package com.baeldung.jackson.entities; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; + +public class Order { + private String orderNo; + private LocalDate date; + private String customerName; + private List orderLines; + + public Order() { + + } + + public Order(String orderNo, LocalDate date, String customerName, List orderLines) { + super(); + this.orderNo = orderNo; + this.date = date; + this.customerName = customerName; + this.orderLines = orderLines; + } + + public String getOrderNo() { + return orderNo; + } + + public void setOrderNo(String orderNo) { + this.orderNo = orderNo; + } + + public LocalDate getDate() { + return date; + } + + public void setDate(LocalDate date) { + this.date = date; + } + + public String getCustomerName() { + return customerName; + } + + public void setCustomerName(String customerName) { + this.customerName = customerName; + } + + public List getOrderLines() { + if (orderLines == null) { + orderLines = new ArrayList<>(); + } + return orderLines; + } + + public void setOrderLines(List orderLines) { + if (orderLines == null) { + orderLines = new ArrayList<>(); + } + this.orderLines = orderLines; + } + + @Override + public String toString() { + return "Order [orderNo=" + orderNo + ", date=" + date + ", customerName=" + customerName + ", orderLines=" + orderLines + "]"; + } + +} diff --git a/jackson-2/src/main/java/com/baeldung/jackson/entities/OrderLine.java b/jackson-2/src/main/java/com/baeldung/jackson/entities/OrderLine.java new file mode 100644 index 0000000000..858d094dd1 --- /dev/null +++ b/jackson-2/src/main/java/com/baeldung/jackson/entities/OrderLine.java @@ -0,0 +1,49 @@ +package com.baeldung.jackson.entities; + +import java.math.BigDecimal; + +public class OrderLine { + private String item; + private int quantity; + private BigDecimal unitPrice; + + public OrderLine() { + + } + + public OrderLine(String item, int quantity, BigDecimal unitPrice) { + super(); + this.item = item; + this.quantity = quantity; + this.unitPrice = unitPrice; + } + + public String getItem() { + return item; + } + + public void setItem(String item) { + this.item = item; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public BigDecimal getUnitPrice() { + return unitPrice; + } + + public void setUnitPrice(BigDecimal unitPrice) { + this.unitPrice = unitPrice; + } + + @Override + public String toString() { + return "OrderLine [item=" + item + ", quantity=" + quantity + ", unitPrice=" + unitPrice + "]"; + } +} diff --git a/jackson-2/src/main/resources/orderInput.yaml b/jackson-2/src/main/resources/orderInput.yaml new file mode 100644 index 0000000000..0aaa6186a2 --- /dev/null +++ b/jackson-2/src/main/resources/orderInput.yaml @@ -0,0 +1,11 @@ +orderNo: A001 +date: 2019-04-17 +customerName: Customer, Joe +orderLines: + - item: No. 9 Sprockets + quantity: 12 + unitPrice: 1.23 + - item: Widget (10mm) + quantity: 4 + unitPrice: 3.45 + \ No newline at end of file diff --git a/jackson-2/src/test/java/com/baeldung/jackson/yaml/YamlUnitTest.java b/jackson-2/src/test/java/com/baeldung/jackson/yaml/YamlUnitTest.java new file mode 100644 index 0000000000..3ed84db60e --- /dev/null +++ b/jackson-2/src/test/java/com/baeldung/jackson/yaml/YamlUnitTest.java @@ -0,0 +1,63 @@ +package com.baeldung.jackson.yaml; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.jackson.entities.Order; +import com.baeldung.jackson.entities.OrderLine; +import com.fasterxml.jackson.core.JsonGenerationException; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature; + +public class YamlUnitTest { + private ObjectMapper mapper; + + @Before + public void setup() { + mapper = new ObjectMapper(new YAMLFactory().disable(Feature.WRITE_DOC_START_MARKER)); + mapper.findAndRegisterModules(); + mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + } + + @Test + public void givenYamlInput_ObjectCreated() throws JsonParseException, JsonMappingException, IOException { + Order order = mapper.readValue(new File("src/main/resources/orderInput.yaml"), Order.class); + assertEquals("A001", order.getOrderNo()); + assertEquals(LocalDate.parse("2019-04-17", DateTimeFormatter.ISO_DATE), order.getDate()); + assertEquals("Customer, Joe", order.getCustomerName()); + assertEquals(2, order.getOrderLines() + .size()); + } + + @Test + public void givenYamlObject_FileWritten() throws JsonGenerationException, JsonMappingException, IOException { + List lines = new ArrayList<>(); + lines.add(new OrderLine("Copper Wire (200ft)", 1, new BigDecimal(50.67).setScale(2, RoundingMode.HALF_UP))); + lines.add(new OrderLine("Washers (1/4\")", 24, new BigDecimal(.15).setScale(2, RoundingMode.HALF_UP))); + Order order = new Order( + "B-9910", + LocalDate.parse("2019-04-18", DateTimeFormatter.ISO_DATE), + "Customer, Jane", + lines); + mapper.writeValue(new File("src/main/resources/orderOutput.yaml"), order); + + File outputYaml = new File("src/main/resources/orderOutput.yaml"); + assertTrue(outputYaml.exists()); + } +} From e164ab8af010d5aff19401a560d3d4b281fc5e71 Mon Sep 17 00:00:00 2001 From: maibin Date: Sat, 20 Apr 2019 10:44:01 -0700 Subject: [PATCH 10/12] Fix the Groovy 2 build (#6772) * Fix the name of the artifact * Add Groovy 2 to build --- core-groovy-2/pom.xml | 2 +- pom.xml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/core-groovy-2/pom.xml b/core-groovy-2/pom.xml index bbedbf2157..eb82f5d2e3 100644 --- a/core-groovy-2/pom.xml +++ b/core-groovy-2/pom.xml @@ -2,7 +2,7 @@ 4.0.0 - core-groovy + core-groovy-2 1.0-SNAPSHOT core-groovy-2 jar diff --git a/pom.xml b/pom.xml index d588dfaba2..2e31200652 100644 --- a/pom.xml +++ b/pom.xml @@ -376,6 +376,7 @@ cdi checker-plugin core-groovy + core-groovy-2 From 28f80013cb9561d840efbb757a718c26f51282ad Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Sat, 20 Apr 2019 22:14:27 +0300 Subject: [PATCH 11/12] BAEL-2661 - Groovy def keyword - pr comments resolved --- core-groovy-2/pom.xml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/core-groovy-2/pom.xml b/core-groovy-2/pom.xml index 9819bc4c16..e783533ccc 100644 --- a/core-groovy-2/pom.xml +++ b/core-groovy-2/pom.xml @@ -95,8 +95,8 @@
- -
+ + maven-surefire-plugin 2.20.1 @@ -120,9 +120,6 @@ 1.0.0 - - - 2.5.6 2.5.6 2.5.6 From 743e1b04e12eefca54691f4e4478f8d3230d0d00 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Sat, 20 Apr 2019 22:16:30 +0300 Subject: [PATCH 12/12] BAEL-2661 - Groovy def keyword - pr comments resolved --- core-groovy-2/pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core-groovy-2/pom.xml b/core-groovy-2/pom.xml index e783533ccc..77de9c8fc8 100644 --- a/core-groovy-2/pom.xml +++ b/core-groovy-2/pom.xml @@ -95,9 +95,9 @@ - + - + maven-surefire-plugin 2.20.1