From 2468c93e64aa7eebb47ce4c90252318c529ff6c8 Mon Sep 17 00:00:00 2001 From: catalin-burcea Date: Tue, 22 Oct 2019 16:06:15 +0300 Subject: [PATCH] Move Existing Articles to core-groovy-collections Module --- core-groovy-collections/README.md | 4 +- .../com/baeldung/find/ListFindUnitTest.groovy | 6 +- .../com/baeldung/find/MapFindUnitTest.groovy | 84 +--------- .../groovy/com/baeldung/find}/Person.groovy | 2 +- .../com/baeldung/find/SetFindUnitTest.groovy | 4 +- .../iteratemap/IterateMapUnitTest.groovy | 87 ++++++++++ .../com/baeldung/lists/ListUnitTest.groovy | 4 +- .../com/baeldung/{map => maps}/MapTest.groovy | 2 +- core-groovy/README.md | 3 - .../groovy/com/baeldung/map/MapTest.groovy | 148 ------------------ 10 files changed, 102 insertions(+), 242 deletions(-) rename core-groovy/src/test/groovy/com/baeldung/lists/ListUnitTest.groovy => core-groovy-collections/src/test/groovy/com/baeldung/find/ListFindUnitTest.groovy (95%) rename core-groovy/src/test/groovy/com/baeldung/map/MapUnitTest.groovy => core-groovy-collections/src/test/groovy/com/baeldung/find/MapFindUnitTest.groovy (53%) rename {core-groovy/src/main/groovy/com/baeldung => core-groovy-collections/src/test/groovy/com/baeldung/find}/Person.groovy (96%) rename core-groovy/src/test/groovy/com/baeldung/set/SetUnitTest.groovy => core-groovy-collections/src/test/groovy/com/baeldung/find/SetFindUnitTest.groovy (83%) create mode 100644 core-groovy-collections/src/test/groovy/com/baeldung/iteratemap/IterateMapUnitTest.groovy rename core-groovy/src/test/groovy/com/baeldung/lists/ListTest.groovy => core-groovy-collections/src/test/groovy/com/baeldung/lists/ListUnitTest.groovy (98%) rename core-groovy-collections/src/test/groovy/com/baeldung/{map => maps}/MapTest.groovy (99%) delete mode 100644 core-groovy/src/test/groovy/com/baeldung/map/MapTest.groovy diff --git a/core-groovy-collections/README.md b/core-groovy-collections/README.md index 4afd214e7d..aae8be508e 100644 --- a/core-groovy-collections/README.md +++ b/core-groovy-collections/README.md @@ -5,4 +5,6 @@ This module contains articles about Groovy core collections ## Relevant articles: - [Maps in Groovy](https://www.baeldung.com/groovy-maps) - +- [Finding Elements in Collections in Groovy](https://www.baeldung.com/groovy-collections-find-elements) +- [Lists in Groovy](https://www.baeldung.com/groovy-lists) +- [A Quick Guide to Iterating a Map in Groovy](https://www.baeldung.com/groovy-map-iterating) diff --git a/core-groovy/src/test/groovy/com/baeldung/lists/ListUnitTest.groovy b/core-groovy-collections/src/test/groovy/com/baeldung/find/ListFindUnitTest.groovy similarity index 95% rename from core-groovy/src/test/groovy/com/baeldung/lists/ListUnitTest.groovy rename to core-groovy-collections/src/test/groovy/com/baeldung/find/ListFindUnitTest.groovy index 9617c099ce..82a2138be4 100644 --- a/core-groovy/src/test/groovy/com/baeldung/lists/ListUnitTest.groovy +++ b/core-groovy-collections/src/test/groovy/com/baeldung/find/ListFindUnitTest.groovy @@ -1,11 +1,11 @@ -package com.baeldung.lists +package com.baeldung.find -import com.baeldung.Person +import com.baeldung.find.Person import org.junit.Test import static org.junit.Assert.* -class ListUnitTest { +class ListFindUnitTest { private final personList = [ new Person("Regina", "Fitzpatrick", 25), diff --git a/core-groovy/src/test/groovy/com/baeldung/map/MapUnitTest.groovy b/core-groovy-collections/src/test/groovy/com/baeldung/find/MapFindUnitTest.groovy similarity index 53% rename from core-groovy/src/test/groovy/com/baeldung/map/MapUnitTest.groovy rename to core-groovy-collections/src/test/groovy/com/baeldung/find/MapFindUnitTest.groovy index 0d6bbed04b..16e231182b 100644 --- a/core-groovy/src/test/groovy/com/baeldung/map/MapUnitTest.groovy +++ b/core-groovy-collections/src/test/groovy/com/baeldung/find/MapFindUnitTest.groovy @@ -1,11 +1,11 @@ -package com.baeldung.map +package com.baeldung.find -import com.baeldung.Person +import com.baeldung.find.Person import org.junit.Test import static org.junit.Assert.* -class MapUnitTest { +class MapFindUnitTest { private final personMap = [ Regina : new Person("Regina", "Fitzpatrick", 25), @@ -13,84 +13,6 @@ class MapUnitTest { Lucian : new Person("Lucian", "Walter", 30) ] - @Test - void whenUsingEach_thenMapIsIterated() { - def map = [ - 'FF0000' : 'Red', - '00FF00' : 'Lime', - '0000FF' : 'Blue', - 'FFFF00' : 'Yellow' - ] - - map.each { println "Hex Code: $it.key = Color Name: $it.value" } - } - - @Test - void whenUsingEachWithEntry_thenMapIsIterated() { - def map = [ - 'E6E6FA' : 'Lavender', - 'D8BFD8' : 'Thistle', - 'DDA0DD' : 'Plum', - ] - - map.each { entry -> println "Hex Code: $entry.key = Color Name: $entry.value" } - } - - @Test - void whenUsingEachWithKeyAndValue_thenMapIsIterated() { - def map = [ - '000000' : 'Black', - 'FFFFFF' : 'White', - '808080' : 'Gray' - ] - - map.each { key, val -> - println "Hex Code: $key = Color Name $val" - } - } - - @Test - void whenUsingEachWithIndexAndEntry_thenMapIsIterated() { - def map = [ - '800080' : 'Purple', - '4B0082' : 'Indigo', - '6A5ACD' : 'Slate Blue' - ] - - map.eachWithIndex { entry, index -> - def indent = ((index == 0 || index % 2 == 0) ? " " : "") - println "$indent Hex Code: $entry.key = Color Name: $entry.value" - } - } - - @Test - void whenUsingEachWithIndexAndKeyAndValue_thenMapIsIterated() { - def map = [ - 'FFA07A' : 'Light Salmon', - 'FF7F50' : 'Coral', - 'FF6347' : 'Tomato', - 'FF4500' : 'Orange Red' - ] - - map.eachWithIndex { key, val, index -> - def indent = ((index == 0 || index % 2 == 0) ? " " : "") - println "$indent Hex Code: $key = Color Name: $val" - } - } - - @Test - void whenUsingForLoop_thenMapIsIterated() { - def map = [ - '2E8B57' : 'Seagreen', - '228B22' : 'Forest Green', - '008000' : 'Green' - ] - - for (entry in map) { - println "Hex Code: $entry.key = Color Name: $entry.value" - } - } - @Test void whenMapContainsKeyElement_thenCheckReturnsTrue() { def map = [a: 'd', b: 'e', c: 'f'] diff --git a/core-groovy/src/main/groovy/com/baeldung/Person.groovy b/core-groovy-collections/src/test/groovy/com/baeldung/find/Person.groovy similarity index 96% rename from core-groovy/src/main/groovy/com/baeldung/Person.groovy rename to core-groovy-collections/src/test/groovy/com/baeldung/find/Person.groovy index 6a009aeee0..e65826363a 100644 --- a/core-groovy/src/main/groovy/com/baeldung/Person.groovy +++ b/core-groovy-collections/src/test/groovy/com/baeldung/find/Person.groovy @@ -1,4 +1,4 @@ -package com.baeldung +package com.baeldung.find class Person { private String firstname diff --git a/core-groovy/src/test/groovy/com/baeldung/set/SetUnitTest.groovy b/core-groovy-collections/src/test/groovy/com/baeldung/find/SetFindUnitTest.groovy similarity index 83% rename from core-groovy/src/test/groovy/com/baeldung/set/SetUnitTest.groovy rename to core-groovy-collections/src/test/groovy/com/baeldung/find/SetFindUnitTest.groovy index 1248c9ac91..d2d03d5427 100644 --- a/core-groovy/src/test/groovy/com/baeldung/set/SetUnitTest.groovy +++ b/core-groovy-collections/src/test/groovy/com/baeldung/find/SetFindUnitTest.groovy @@ -1,10 +1,10 @@ -package com.baeldung.set +package com.baeldung.find import org.junit.Test import static org.junit.Assert.assertTrue -class SetUnitTest { +class SetFindUnitTest { @Test void whenSetContainsElement_thenCheckReturnsTrue() { diff --git a/core-groovy-collections/src/test/groovy/com/baeldung/iteratemap/IterateMapUnitTest.groovy b/core-groovy-collections/src/test/groovy/com/baeldung/iteratemap/IterateMapUnitTest.groovy new file mode 100644 index 0000000000..970203ce85 --- /dev/null +++ b/core-groovy-collections/src/test/groovy/com/baeldung/iteratemap/IterateMapUnitTest.groovy @@ -0,0 +1,87 @@ +package com.baeldung.iteratemap + +import com.baeldung.find.Person +import org.junit.Test + +import static org.junit.Assert.* + +class IterateMapUnitTest { + + @Test + void whenUsingEach_thenMapIsIterated() { + def map = [ + 'FF0000' : 'Red', + '00FF00' : 'Lime', + '0000FF' : 'Blue', + 'FFFF00' : 'Yellow' + ] + + map.each { println "Hex Code: $it.key = Color Name: $it.value" } + } + + @Test + void whenUsingEachWithEntry_thenMapIsIterated() { + def map = [ + 'E6E6FA' : 'Lavender', + 'D8BFD8' : 'Thistle', + 'DDA0DD' : 'Plum', + ] + + map.each { entry -> println "Hex Code: $entry.key = Color Name: $entry.value" } + } + + @Test + void whenUsingEachWithKeyAndValue_thenMapIsIterated() { + def map = [ + '000000' : 'Black', + 'FFFFFF' : 'White', + '808080' : 'Gray' + ] + + map.each { key, val -> + println "Hex Code: $key = Color Name $val" + } + } + + @Test + void whenUsingEachWithIndexAndEntry_thenMapIsIterated() { + def map = [ + '800080' : 'Purple', + '4B0082' : 'Indigo', + '6A5ACD' : 'Slate Blue' + ] + + map.eachWithIndex { entry, index -> + def indent = ((index == 0 || index % 2 == 0) ? " " : "") + println "$indent Hex Code: $entry.key = Color Name: $entry.value" + } + } + + @Test + void whenUsingEachWithIndexAndKeyAndValue_thenMapIsIterated() { + def map = [ + 'FFA07A' : 'Light Salmon', + 'FF7F50' : 'Coral', + 'FF6347' : 'Tomato', + 'FF4500' : 'Orange Red' + ] + + map.eachWithIndex { key, val, index -> + def indent = ((index == 0 || index % 2 == 0) ? " " : "") + println "$indent Hex Code: $key = Color Name: $val" + } + } + + @Test + void whenUsingForLoop_thenMapIsIterated() { + def map = [ + '2E8B57' : 'Seagreen', + '228B22' : 'Forest Green', + '008000' : 'Green' + ] + + for (entry in map) { + println "Hex Code: $entry.key = Color Name: $entry.value" + } + } +} diff --git a/core-groovy/src/test/groovy/com/baeldung/lists/ListTest.groovy b/core-groovy-collections/src/test/groovy/com/baeldung/lists/ListUnitTest.groovy similarity index 98% rename from core-groovy/src/test/groovy/com/baeldung/lists/ListTest.groovy rename to core-groovy-collections/src/test/groovy/com/baeldung/lists/ListUnitTest.groovy index 7771028132..e4c0a0c177 100644 --- a/core-groovy/src/test/groovy/com/baeldung/lists/ListTest.groovy +++ b/core-groovy-collections/src/test/groovy/com/baeldung/lists/ListUnitTest.groovy @@ -1,9 +1,9 @@ -package com.baeldung.groovy.lists +package com.baeldung.lists import static groovy.test.GroovyAssert.* import org.junit.Test -class ListTest{ +class ListUnitTest { @Test void testCreateList() { diff --git a/core-groovy-collections/src/test/groovy/com/baeldung/map/MapTest.groovy b/core-groovy-collections/src/test/groovy/com/baeldung/maps/MapTest.groovy similarity index 99% rename from core-groovy-collections/src/test/groovy/com/baeldung/map/MapTest.groovy rename to core-groovy-collections/src/test/groovy/com/baeldung/maps/MapTest.groovy index c6105eb1c4..deb552c420 100644 --- a/core-groovy-collections/src/test/groovy/com/baeldung/map/MapTest.groovy +++ b/core-groovy-collections/src/test/groovy/com/baeldung/maps/MapTest.groovy @@ -1,4 +1,4 @@ -package com.baeldung.map; +package com.baeldung.maps; import static groovy.test.GroovyAssert.* import org.junit.Test diff --git a/core-groovy/README.md b/core-groovy/README.md index 0f45eed879..25a0aece3a 100644 --- a/core-groovy/README.md +++ b/core-groovy/README.md @@ -8,11 +8,8 @@ This module contains articles about core Groovy concepts - [Working with JSON in Groovy](https://www.baeldung.com/groovy-json) - [Reading a File in Groovy](https://www.baeldung.com/groovy-file-read) - [Types of Strings in Groovy](https://www.baeldung.com/groovy-strings) -- [A Quick Guide to Iterating a Map in Groovy](https://www.baeldung.com/groovy-map-iterating) - [An Introduction to Traits in Groovy](https://www.baeldung.com/groovy-traits) - [Closures in Groovy](https://www.baeldung.com/groovy-closures) -- [Finding Elements in Collections in Groovy](https://www.baeldung.com/groovy-collections-find-elements) -- [Lists in Groovy](https://www.baeldung.com/groovy-lists) - [Converting a String to a Date in Groovy](https://www.baeldung.com/groovy-string-to-date) - [Guide to I/O in Groovy](https://www.baeldung.com/groovy-io) - [[More -->]](/core-groovy-2) \ No newline at end of file diff --git a/core-groovy/src/test/groovy/com/baeldung/map/MapTest.groovy b/core-groovy/src/test/groovy/com/baeldung/map/MapTest.groovy deleted file mode 100644 index f1d528207f..0000000000 --- a/core-groovy/src/test/groovy/com/baeldung/map/MapTest.groovy +++ /dev/null @@ -1,148 +0,0 @@ -package com.baeldung.groovy.map; - -import static groovy.test.GroovyAssert.* -import org.junit.Test - -class MapTest{ - - @Test - void createMap() { - - def emptyMap = [:] - assertNotNull(emptyMap) - - assertTrue(emptyMap instanceof java.util.LinkedHashMap) - - def map = [name:"Jerry", age: 42, city: "New York"] - assertTrue(map.size() == 3) - } - - @Test - void addItemsToMap() { - - def map = [name:"Jerry"] - - map["age"] = 42 - - map.city = "New York" - - def hobbyLiteral = "hobby" - def hobbyMap = [(hobbyLiteral): "Singing"] - map.putAll(hobbyMap) - - assertTrue(map == [name:"Jerry", age: 42, city: "New York", hobby:"Singing"]) - assertTrue(hobbyMap.hobby == "Singing") - assertTrue(hobbyMap[hobbyLiteral] == "Singing") - - map.plus([1:20]) // returns new map - - map << [2:30] - - } - - @Test - void getItemsFromMap() { - - def map = [name:"Jerry", age: 42, city: "New York", hobby:"Singing"] - - assertTrue(map["name"] == "Jerry") - - assertTrue(map.name == "Jerry") - - def propertyAge = "age" - assertTrue(map[propertyAge] == 42) - } - - @Test - void removeItemsFromMap() { - - def map = [1:20, a:30, 2:42, 4:34, ba:67, 6:39, 7:49] - - def minusMap = map.minus([2:42, 4:34]); - assertTrue(minusMap == [1:20, a:30, ba:67, 6:39, 7:49]) - - minusMap.removeAll{it -> it.key instanceof String} - assertTrue( minusMap == [ 1:20, 6:39, 7:49]) - - minusMap.retainAll{it -> it.value %2 == 0} - assertTrue( minusMap == [1:20]) - } - - @Test - void iteratingOnMaps(){ - def map = [name:"Jerry", age: 42, city: "New York", hobby:"Singing"] - - map.each{ entry -> println "$entry.key: $entry.value" } - - map.eachWithIndex{ entry, i -> println "$i $entry.key: $entry.value" } - - map.eachWithIndex{ key, value, i -> println "$i $key: $value" } - } - - @Test - void filteringAndSearchingMaps(){ - def map = [name:"Jerry", age: 42, city: "New York", hobby:"Singing"] - - assertTrue(map.find{ it.value == "New York"}.key == "city") - - assertTrue(map.findAll{ it.value == "New York"} == [city : "New York"]) - - map.grep{it.value == "New York"}.each{ it -> assertTrue(it.key == "city" && it.value == "New York")} - - assertTrue(map.every{it -> it.value instanceof String} == false) - - assertTrue(map.any{it -> it.value instanceof String} == true) - } - - @Test - void collect(){ - - def map = [1: [name:"Jerry", age: 42, city: "New York"], - 2: [name:"Long", age: 25, city: "New York"], - 3: [name:"Dustin", age: 29, city: "New York"], - 4: [name:"Dustin", age: 34, city: "New York"]] - - def names = map.collect{entry -> entry.value.name} // returns only list - assertTrue(names == ["Jerry", "Long", "Dustin", "Dustin"]) - - def uniqueNames = map.collect([] as HashSet){entry -> entry.value.name} - assertTrue(uniqueNames == ["Jerry", "Long", "Dustin"] as Set) - - def idNames = map.collectEntries{key, value -> [key, value.name]} - assertTrue(idNames == [1:"Jerry", 2: "Long", 3:"Dustin", 4: "Dustin"]) - - def below30Names = map.findAll{it.value.age < 30}.collect{key, value -> value.name} - assertTrue(below30Names == ["Long", "Dustin"]) - - - } - - @Test - void group(){ - def map = [1:20, 2: 40, 3: 11, 4: 93] - - def subMap = map.groupBy{it.value % 2} - println subMap - assertTrue(subMap == [0:[1:20, 2:40 ], 1:[3:11, 4:93]]) - - def keySubMap = map.subMap([1, 2]) - assertTrue(keySubMap == [1:20, 2:40]) - - } - - @Test - void sorting(){ - def map = [ab:20, a: 40, cb: 11, ba: 93] - - def naturallyOrderedMap = map.sort() - assertTrue([a:40, ab:20, ba:93, cb:11] == naturallyOrderedMap) - - def compSortedMap = map.sort({ k1, k2 -> k1 <=> k2 } as Comparator) - assertTrue([a:40, ab:20, ba:93, cb:11] == compSortedMap) - - def cloSortedMap = map.sort({ it1, it2 -> it1.value <=> it1.value }) - assertTrue([cb:11, ab:20, a:40, ba:93] == cloSortedMap) - - } - -}