Skip to content

Commit 4151e55

Browse files
jfrankowskiashleyfrieze
authored andcommitted
[BAEL-2664] Groovy collections find
1 parent d05ce3a commit 4151e55

5 files changed

Lines changed: 186 additions & 6 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung
2+
3+
class Person {
4+
private String firstname
5+
private String lastname
6+
private Integer age
7+
8+
Person(String firstname, String lastname, Integer age) {
9+
this.firstname = firstname
10+
this.lastname = lastname
11+
this.age = age
12+
}
13+
14+
String getFirstname() {
15+
return firstname
16+
}
17+
18+
void setFirstname(String firstname) {
19+
this.firstname = firstname
20+
}
21+
22+
String getLastname() {
23+
return lastname
24+
}
25+
26+
void setLastname(String lastname) {
27+
this.lastname = lastname
28+
}
29+
30+
Integer getAge() {
31+
return age
32+
}
33+
34+
void setAge(Integer age) {
35+
this.age = age
36+
}
37+
}

core-groovy/src/test/groovy/com/baeldung/lists/ListTest.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ class ListTest{
129129
assertTrue(filterList.findAll{it > 3} == [4, 5, 6, 76])
130130

131131
assertTrue(filterList.findAll{ it instanceof Number} == [2, 1, 3, 4, 5, 6, 76])
132-
132+
133133
assertTrue(filterList.grep( Number )== [2, 1, 3, 4, 5, 6, 76])
134-
134+
135135
assertTrue(filterList.grep{ it> 6 }== [76])
136136

137137
def conditionList = [2, 1, 3, 4, 5, 6, 76]
138-
138+
139139
assertFalse(conditionList.every{ it < 6})
140140

141141
assertTrue(conditionList.any{ it%2 == 0})
@@ -165,7 +165,7 @@ class ListTest{
165165

166166
def strList = ["na", "ppp", "as"]
167167
assertTrue(strList.max() == "ppp")
168-
168+
169169
Comparator minc = {a,b -> a == b? 0: a < b? -1 : 1}
170170
def numberList = [3, 2, 0, 7]
171171
assertTrue(numberList.min(minc) == 0)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.lists
2+
3+
import com.baeldung.Person
4+
import org.junit.Test
5+
6+
import static org.junit.Assert.*
7+
8+
class ListUnitTest {
9+
10+
private final personList = [
11+
new Person("Regina", "Fitzpatrick", 25),
12+
new Person("Abagail", "Ballard", 26),
13+
new Person("Lucian", "Walter", 30),
14+
]
15+
16+
@Test
17+
void whenListContainsElement_thenCheckReturnsTrue() {
18+
def list = ['a', 'b', 'c']
19+
20+
assertTrue(list.indexOf('a') > -1)
21+
assertTrue(list.contains('a'))
22+
}
23+
24+
@Test
25+
void whenListContainsElement_thenCheckWithMembershipOperatorReturnsTrue() {
26+
def list = ['a', 'b', 'c']
27+
28+
assertTrue('a' in list)
29+
}
30+
31+
@Test
32+
void givenListOfPerson_whenUsingStreamMatching_thenShouldEvaluateList() {
33+
assertTrue(personList.stream().anyMatch {it.age > 20})
34+
assertFalse(personList.stream().allMatch {it.age < 30})
35+
}
36+
37+
@Test
38+
void givenListOfPerson_whenUsingCollectionMatching_thenShouldEvaluateList() {
39+
assertTrue(personList.any {it.age > 20})
40+
assertFalse(personList.every {it.age < 30})
41+
}
42+
43+
@Test
44+
void givenListOfPerson_whenUsingStreamFind_thenShouldReturnMatchingElements() {
45+
assertTrue(personList.stream().filter {it.age > 20}.findAny().isPresent())
46+
assertFalse(personList.stream().filter {it.age > 30}.findAny().isPresent())
47+
assertTrue(personList.stream().filter {it.age > 20}.findAll().size() == 3)
48+
assertTrue(personList.stream().filter {it.age > 30}.findAll().isEmpty())
49+
}
50+
51+
@Test
52+
void givenListOfPerson_whenUsingCollectionFind_thenShouldReturnMatchingElements() {
53+
assertNotNull(personList.find {it.age > 20})
54+
assertNull(personList.find {it.age > 30})
55+
assertTrue(personList.findAll {it.age > 20}.size() == 3)
56+
assertTrue(personList.findAll {it.age > 30}.isEmpty())
57+
}
58+
}

core-groovy/src/test/groovy/com/baeldung/map/MapUnitTest.groovy

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package com.baeldung.map
22

3-
import static org.junit.Assert.*
3+
import com.baeldung.Person
44
import org.junit.Test
55

6+
import static org.junit.Assert.*
7+
68
class MapUnitTest {
79

10+
private final personMap = [
11+
Regina : new Person("Regina", "Fitzpatrick", 25),
12+
Abagail: new Person("Abagail", "Ballard", 26),
13+
Lucian : new Person("Lucian", "Walter", 30)
14+
]
15+
816
@Test
917
void whenUsingEach_thenMapIsIterated() {
1018
def map = [
@@ -63,7 +71,7 @@ class MapUnitTest {
6371
'FF6347' : 'Tomato',
6472
'FF4500' : 'Orange Red'
6573
]
66-
74+
6775
map.eachWithIndex { key, val, index ->
6876
def indent = ((index == 0 || index % 2 == 0) ? " " : "")
6977
println "$indent Hex Code: $key = Color Name: $val"
@@ -82,4 +90,65 @@ class MapUnitTest {
8290
println "Hex Code: $entry.key = Color Name: $entry.value"
8391
}
8492
}
93+
94+
@Test
95+
void whenMapContainsKeyElement_thenCheckReturnsTrue() {
96+
def map = [a: 'd', b: 'e', c: 'f']
97+
98+
assertTrue(map.containsKey('a'))
99+
assertFalse(map.containsKey('e'))
100+
assertTrue(map.containsValue('e'))
101+
}
102+
103+
@Test
104+
void whenMapContainsKeyElement_thenCheckByMembershipReturnsTrue() {
105+
def map = [a: 'd', b: 'e', c: 'f']
106+
107+
assertTrue('a' in map)
108+
assertFalse('f' in map)
109+
}
110+
111+
@Test
112+
void whenMapContainsFalseBooleanValues_thenCheckReturnsFalse() {
113+
def map = [a: true, b: false, c: null]
114+
115+
assertTrue(map.containsKey('b'))
116+
assertTrue('a' in map)
117+
assertFalse('b' in map)
118+
assertFalse('c' in map)
119+
}
120+
121+
@Test
122+
void givenMapOfPerson_whenUsingStreamMatching_thenShouldEvaluateMap() {
123+
assertTrue(personMap.keySet().stream().anyMatch {it == "Regina"})
124+
assertFalse(personMap.keySet().stream().allMatch {it == "Albert"})
125+
assertFalse(personMap.values().stream().allMatch {it.age < 30})
126+
assertTrue(personMap.entrySet().stream().anyMatch {it.key == "Abagail" && it.value.lastname == "Ballard"})
127+
}
128+
129+
@Test
130+
void givenMapOfPerson_whenUsingCollectionMatching_thenShouldEvaluateMap() {
131+
assertTrue(personMap.keySet().any {it == "Regina"})
132+
assertFalse(personMap.keySet().every {it == "Albert"})
133+
assertFalse(personMap.values().every {it.age < 30})
134+
assertTrue(personMap.any {firstname, person -> firstname == "Abagail" && person.lastname == "Ballard"})
135+
}
136+
137+
@Test
138+
void givenMapOfPerson_whenUsingCollectionFind_thenShouldReturnElements() {
139+
assertNotNull(personMap.find {it.key == "Abagail" && it.value.lastname == "Ballard"})
140+
assertTrue(personMap.findAll {it.value.age > 20}.size() == 3)
141+
}
142+
143+
@Test
144+
void givenMapOfPerson_whenUsingStreamFind_thenShouldReturnElements() {
145+
assertTrue(
146+
personMap.entrySet().stream()
147+
.filter {it.key == "Abagail" && it.value.lastname == "Ballard"}
148+
.findAny().isPresent())
149+
assertTrue(
150+
personMap.entrySet().stream()
151+
.filter {it.value.age > 20}
152+
.findAll().size() == 3)
153+
}
85154
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.set
2+
3+
import org.junit.Test
4+
5+
import static org.junit.Assert.assertTrue
6+
7+
class SetUnitTest {
8+
9+
@Test
10+
void whenSetContainsElement_thenCheckReturnsTrue() {
11+
def set = ['a', 'b', 'c'] as Set
12+
13+
assertTrue(set.contains('a'))
14+
assertTrue('a' in set)
15+
}
16+
}

0 commit comments

Comments
 (0)