From dd5a432614153e57326b58afef3c1624385cd132 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Sun, 15 Sep 2019 16:54:50 +0300 Subject: [PATCH 1/2] BAEL-3191 - Categories in Groovy --- .../baeldung/category/BaeldungCategory.groovy | 17 ++++ .../baeldung/category/NumberCategory.groovy | 16 +++ .../baeldung/category/CategoryUnitTest.groovy | 99 +++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 core-groovy-2/src/main/groovy/com/baeldung/category/BaeldungCategory.groovy create mode 100644 core-groovy-2/src/main/groovy/com/baeldung/category/NumberCategory.groovy create mode 100644 core-groovy-2/src/test/groovy/com/baeldung/category/CategoryUnitTest.groovy diff --git a/core-groovy-2/src/main/groovy/com/baeldung/category/BaeldungCategory.groovy b/core-groovy-2/src/main/groovy/com/baeldung/category/BaeldungCategory.groovy new file mode 100644 index 0000000000..366dd182c8 --- /dev/null +++ b/core-groovy-2/src/main/groovy/com/baeldung/category/BaeldungCategory.groovy @@ -0,0 +1,17 @@ +package com.baeldung.category; + +class BaeldungCategory { + + public static String capitalize(String self) { + String capitalizedStr = self; + if (self.size() > 0) { + capitalizedStr = self.substring(0, 1).toUpperCase() + self.substring(1); + } + return capitalizedStr + } + + public static Number square(Number self) { + return self*self; + } + +} diff --git a/core-groovy-2/src/main/groovy/com/baeldung/category/NumberCategory.groovy b/core-groovy-2/src/main/groovy/com/baeldung/category/NumberCategory.groovy new file mode 100644 index 0000000000..cf0e5282fc --- /dev/null +++ b/core-groovy-2/src/main/groovy/com/baeldung/category/NumberCategory.groovy @@ -0,0 +1,16 @@ +package com.baeldung.category; + +import groovy.lang.Category + +@Category(Number) +class NumberCategory { + + public Number cube() { + return this*this*this + } + + public Number toThePower(Number exponent) { + return Math.pow(this, exponent) + } + +} diff --git a/core-groovy-2/src/test/groovy/com/baeldung/category/CategoryUnitTest.groovy b/core-groovy-2/src/test/groovy/com/baeldung/category/CategoryUnitTest.groovy new file mode 100644 index 0000000000..2e10efbe03 --- /dev/null +++ b/core-groovy-2/src/test/groovy/com/baeldung/category/CategoryUnitTest.groovy @@ -0,0 +1,99 @@ +package com.baeldung.category + +import groovy.time.* +import java.text.SimpleDateFormat +import groovy.xml.* +import groovy.xml.dom.* +import com.baeldung.category.BaeldungCategory +import com.baeldung.category.NumberCategory + +class CategoryUnitTest extends GroovyTestCase { + + void test_whenUsingTimeCategory_thenOperationOnDate() { + def jan_1_2019 = new Date("01/01/2019") + use (TimeCategory) { + assert jan_1_2019 + 10.seconds == new Date("01/01/2019 00:00:10") + + assert jan_1_2019 + 20.minutes == new Date("01/01/2019 00:20:00") + + assert jan_1_2019 + 2.hours == new Date("01/01/2019 02:00:00") + + assert jan_1_2019 - 1.day == new Date("12/31/2018") + + assert jan_1_2019 + 2.weeks == new Date("01/15/2019") + + assert jan_1_2019 - 2.months == new Date("11/01/2018") + + assert jan_1_2019 + 3.years == new Date("01/01/2022") + } + } + + void test_whenUsingTimeCategory_thenOperationOnNumber() { + SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy") + use (TimeCategory) { + assert sdf.format(5.days.from.now) == sdf.format(new Date() + 5.days) + + sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss") + assert sdf.format(10.minutes.from.now) == sdf.format(new Date() + 10.minutes) + assert sdf.format(2.hours.ago) == sdf.format(new Date() - 2.hours) + } + } + + void test_whenUsingDOMCategory_thenOperationOnXML() { + + def baeldungArticlesText = """ + +
+ An Intro to the Java Debug Interface (JDI) + A quick and practical overview of Java Debug Interface. +
+
+ A Quick Guide to Working with Web Services in Groovy + Learn how to work with Web Services in Groovy. +
+
+""" + def baeldungArticlesDom = DOMBuilder.newInstance().parseText(baeldungArticlesText) + + def root = baeldungArticlesDom.documentElement + + use (DOMCategory) { + assert root.article.size() == 2 + + def articles = root.article + + assert articles[0].title.text() == "An Intro to the Java Debug Interface (JDI)" + assert articles[1].desc.text() == "Learn how to work with Web Services in Groovy." + + def articleNode3 = root.appendNode(new QName("article"), ["core-java": "false"]) + + articleNode3.appendNode("title", "Metaprogramming in Groovy") + articleNode3.appendNode("desc", "Explore the concept of runtime and compile-time metaprogramming in Groovy") + + assert root.article.size() == 3 + + assert root.article[2].title.text() == "Metaprogramming in Groovy" + } + } + + void test_whenUsingBaeldungCategory_thenCapitalizeString() { + use (BaeldungCategory) { + assert "norman".capitalize() == "Norman" + } + } + + void test_whenUsingBaeldungCategory_thenSquareNumber() { + use (BaeldungCategory) { + assert 50.square() == 2500 + assert 20.01.square() == 400.4001 + } + } + + void test_whenUsingNumberUtils_thenCubeNumber() { + use (NumberCategory) { + assert 3.cube() == 27 + assert 2.4.toThePower(4) == 33.1776 + } + } + +} From b7368cb6dbca187efaf136137128e11e052d0ef8 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Sun, 22 Sep 2019 22:26:47 +0300 Subject: [PATCH 2/2] BAEL-3191 - review changes --- .../com/baeldung/category/BaeldungCategory.groovy | 4 ++-- .../com/baeldung/category/NumberCategory.groovy | 9 +++++---- .../com/baeldung/category/CategoryUnitTest.groovy | 12 +++++++----- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/core-groovy-2/src/main/groovy/com/baeldung/category/BaeldungCategory.groovy b/core-groovy-2/src/main/groovy/com/baeldung/category/BaeldungCategory.groovy index 366dd182c8..479c39699f 100644 --- a/core-groovy-2/src/main/groovy/com/baeldung/category/BaeldungCategory.groovy +++ b/core-groovy-2/src/main/groovy/com/baeldung/category/BaeldungCategory.groovy @@ -10,8 +10,8 @@ class BaeldungCategory { return capitalizedStr } - public static Number square(Number self) { - return self*self; + public static double toThePower(Number self, Number exponent) { + return Math.pow(self, exponent); } } diff --git a/core-groovy-2/src/main/groovy/com/baeldung/category/NumberCategory.groovy b/core-groovy-2/src/main/groovy/com/baeldung/category/NumberCategory.groovy index cf0e5282fc..ccf2ed519b 100644 --- a/core-groovy-2/src/main/groovy/com/baeldung/category/NumberCategory.groovy +++ b/core-groovy-2/src/main/groovy/com/baeldung/category/NumberCategory.groovy @@ -4,13 +4,14 @@ import groovy.lang.Category @Category(Number) class NumberCategory { - + public Number cube() { return this*this*this } - - public Number toThePower(Number exponent) { - return Math.pow(this, exponent) + + public int divideWithRoundUp(BigDecimal divisor, boolean isRoundUp) { + def mathRound = isRoundUp ? BigDecimal.ROUND_UP : BigDecimal.ROUND_DOWN + return (int)new BigDecimal(this).divide(divisor, 0, mathRound) } } diff --git a/core-groovy-2/src/test/groovy/com/baeldung/category/CategoryUnitTest.groovy b/core-groovy-2/src/test/groovy/com/baeldung/category/CategoryUnitTest.groovy index 2e10efbe03..a1f67b1e2e 100644 --- a/core-groovy-2/src/test/groovy/com/baeldung/category/CategoryUnitTest.groovy +++ b/core-groovy-2/src/test/groovy/com/baeldung/category/CategoryUnitTest.groovy @@ -82,17 +82,19 @@ class CategoryUnitTest extends GroovyTestCase { } } - void test_whenUsingBaeldungCategory_thenSquareNumber() { + void test_whenUsingBaeldungCategory_thenOperationsOnNumber() { use (BaeldungCategory) { - assert 50.square() == 2500 - assert 20.01.square() == 400.4001 + assert 50.toThePower(2) == 2500 + assert 2.4.toThePower(4) == 33.1776 } } - void test_whenUsingNumberUtils_thenCubeNumber() { + void test_whenUsingNumberCategory_thenOperationsOnNumber() { use (NumberCategory) { assert 3.cube() == 27 - assert 2.4.toThePower(4) == 33.1776 + assert 25.divideWithRoundUp(6, true) == 5 + assert 120.23.divideWithRoundUp(6.1, true) == 20 + assert 150.9.divideWithRoundUp(12.1, false) == 12 } }