Skip to content

Commit 40901d9

Browse files
committed
Merge branch 'master' into solutions
Conflicts: src/main/java/de/codecentric/java8examples/streaming/CollectingAndReducing.java src/test/java/de/codecentric/java8examples/streaming/CollectingAndReducingTest.java
2 parents e164a3b + 05b2802 commit 40901d9

File tree

4 files changed

+62
-28
lines changed

4 files changed

+62
-28
lines changed

src/test/java/de/codecentric/java8examples/lambdas/LambdaExampleTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,24 @@ public boolean test(Person person) {
3434
return person.getAge() > 30;
3535
}
3636
}));
37+
}
38+
39+
@Test
40+
public void peterIsOlderThan30WithBlockLambda() throws Exception {
41+
// new: implement the predicate using a block lambda expression
42+
assertTrue(example.matches((Person p) -> {
43+
return p.getAge() > 30;
44+
}));
45+
}
3746

38-
// new: implement the predicate using lambda expression
47+
@Test
48+
public void peterIsOlderThan30WithOneLineLambda() throws Exception {
49+
// implement the predicate using a one line lambda expression
3950
assertTrue(example.matches((Person person) -> person.getAge() > 30));
51+
}
4052

53+
@Test
54+
public void peterIsOlderThan30WithTypeInference() throws Exception {
4155
// even shorter: let the compiler work out the correct type
4256
assertTrue(example.matches(p -> p.getAge() > 30));
4357
}

src/test/java/de/codecentric/java8examples/methodreference/MethodReferenceExampleTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,19 @@ public void setUp() throws Exception {
2020
}
2121

2222
@Test
23-
public void methodReferencToFooMethod() throws Exception {
23+
public void callbackWithoutMethodReference() throws Exception {
24+
Callable<String> callable = new Callable<String>() {
25+
@Override
26+
public String call() throws Exception {
27+
return myFoo.doSomething();
28+
}
29+
};
30+
31+
assertEquals(myFoo.doSomething(), callable.call());
32+
}
33+
34+
@Test
35+
public void callbackWithMethodReference() throws Exception {
2436
Callable<String> callable = myFoo::doSomething;
2537

2638
assertEquals(myFoo.doSomething(), callable.call());

src/test/java/de/codecentric/java8examples/streaming/CollectingAndReducingTest.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package de.codecentric.java8examples.streaming;
22

33
import de.codecentric.java8examples.Invoice;
4+
import de.codecentric.java8examples.InvoiceItem;
45
import de.codecentric.java8examples.Person;
56
import de.codecentric.java8examples.TestData;
67
import org.junit.Test;
78

89
import java.math.BigDecimal;
9-
import java.util.Arrays;
10-
import java.util.IntSummaryStatistics;
11-
import java.util.List;
12-
import java.util.Map;
10+
import java.util.*;
1311

12+
import static junit.framework.Assert.assertNotNull;
1413
import static org.hamcrest.Matchers.*;
1514
import static org.hamcrest.MatcherAssert.assertThat;
1615

@@ -41,6 +40,7 @@ public void testMaxAge() throws Exception {
4140
@Test
4241
public void testAgeStatistics() throws Exception {
4342
IntSummaryStatistics statistic = CollectingAndReducing.ageStatistics(persons);
43+
assertNotNull(statistic);
4444
assertThat(statistic.getAverage(), equalTo(30.571428571428573));
4545
assertThat(statistic.getCount(), equalTo(7l));
4646
assertThat(statistic.getMax(), equalTo(46));
@@ -107,7 +107,24 @@ public void testExpensesByRecipient() throws Exception {
107107

108108
@Test
109109
public void testCountByProduct() throws Exception {
110+
Map<String, Integer> expected = new HashMap<>();
111+
for (Invoice invoice: invoices) {
112+
for (InvoiceItem item: invoice.getItems()) {
113+
String product = item.getProduct();
114+
if (expected.get(product) == null) {
115+
expected.put(product, Integer.valueOf(0));
116+
}
117+
expected.put(
118+
product,
119+
expected.get(product) + item.getQuantity());
120+
}
121+
}
110122

123+
Map<String, Integer> actual = CollectingAndReducing.countByProduct(invoices);
124+
assertThat(actual.keySet(), hasSize(expected.size()));
125+
for (Map.Entry entry: expected.entrySet()) {
126+
assertThat(actual, hasEntry(entry.getKey(), entry.getValue()));
127+
}
111128
}
112129

113130
@Test

src/test/java/de/codecentric/java8examples/streaming/FilteringAndMappingTest.java

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import static org.junit.Assert.assertThat;
1414

1515
/**
16-
* Tests for mapping and filtering feature of the streaming api. Once you have completed the stub-methods in
17-
* {@link FilteringAndMapping}, these tests should pass.
16+
* Tests for mapping and filtering feature of the streaming api. Once you have completed the stub-methods in {@link FilteringAndMapping}, these tests should
17+
* pass.
1818
*/
1919
public class FilteringAndMappingTest {
2020

@@ -24,45 +24,36 @@ public class FilteringAndMappingTest {
2424

2525
@Test
2626
public void collectFirstNamesOfFemales() throws Exception {
27-
// Matchers.contains should really be called "containsExactlyTheseElementsAndNoOtherElementsInExactlyThisOrder"
28-
// I hate hamcrest :(
29-
assertThat(FilteringAndMapping.extractFemaleFirstnames(persons), contains("Jane", "Mary", "Maggie"));
27+
// Matchers.contains should really be called "containsExactlyTheseElementsAndNoOtherElementsInExactlyThisOrder"
28+
// I hate hamcrest :(
29+
assertThat(FilteringAndMapping.extractFemaleFirstnames(persons), contains("Jane", "Mary", "Maggie"));
3030
}
3131

3232
@Test
3333
public void extractNames() {
34-
assertThat(
35-
FilteringAndMapping.extractNames(persons.subList(0, 6)),
36-
contains("Jane Jungle", "Mary Smitch", "John Dole", "Michael Abrams", "Chris Cross", "Peter Power"));
34+
assertThat(FilteringAndMapping.extractNames(persons.subList(0, 6)),
35+
contains("Jane Jungle", "Mary Smith", "John Dole", "Michael Abrahams", "Chris Cross", "Pete Power"));
3736
}
3837

3938
@Test
4039
public void extractNamesSortedByLastname() {
41-
assertThat(
42-
FilteringAndMapping.extractNamesSortedByLastname(persons.subList(0, 6)),
43-
contains("Michael Abrams", "Chris Cross", "John Dole", "Jane Jungle", "Peter Power", "Mary Smitch"));
40+
assertThat(FilteringAndMapping.extractNamesSortedByLastname(persons.subList(0, 6)),
41+
contains("Michael Abrahams", "Chris Cross", "John Dole", "Jane Jungle", "Pete Power", "Mary Smith"));
4442
}
4543

4644
@Test
4745
public void extractAdultWomen() {
48-
// Yes, I know, this test is time-dependent and will break in a few months/years...
49-
assertThat(
50-
FilteringAndMapping.extractAdultWomen(persons),
51-
contains(
52-
persons.get(0),
53-
persons.get(1)));
46+
// Yes, I know, this test is time-dependent and will break in a few months/years...
47+
assertThat(FilteringAndMapping.extractAdultWomen(persons), contains(persons.get(0), persons.get(1)));
5448
}
5549

5650
@Test
5751
public void extractFirstnamesWhereLastnameStartsWith() {
58-
assertThat(
59-
FilteringAndMapping.extractFirstnamesWhereLastnameStartsWith(persons, "S"),
60-
contains("Mary", "Maggie"));
52+
assertThat(FilteringAndMapping.extractFirstnamesWhereLastnameStartsWith(persons, "S"), contains("Maggie", "Mary"));
6153
}
6254

6355
@Test
6456
public void testExtractAllProducts() throws Exception {
65-
MatcherAssert.assertThat(FilteringAndMapping.extractAllProducts(invoices),
66-
containsInAnyOrder("Beer", "Burrito", "Coke"));
57+
MatcherAssert.assertThat(FilteringAndMapping.extractAllProducts(invoices), containsInAnyOrder("Beer", "Burrito", "Coke"));
6758
}
6859
}

0 commit comments

Comments
 (0)