From d0a6c5701a6cadc509d92ede72957a8e170b2994 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Sun, 14 Apr 2019 22:02:14 +0300 Subject: [PATCH 1/5] 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 2/5] 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 3/5] 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 28f80013cb9561d840efbb757a718c26f51282ad Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Sat, 20 Apr 2019 22:14:27 +0300 Subject: [PATCH 4/5] 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 5/5] 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