Functional Programming in Java, Second Edition: Functional Programming in Java, Second Edition: JUnit code improvements for Chapter 11, pages 184 ff “Refactoring More Complex loops”

The same remarks apply as for “Refactoring the Traditional for Loop”

  • No init()
  • Classes under test implement a common interface that is called in tests.
  • Negative tests which apply for input less than 1900, resulting in exceptions.
package chapter11;

import org.junit.jupiter.api.Test;

import java.time.Year;
import java.util.stream.IntStream;

import static org.junit.jupiter.api.Assertions.*;

// Everything is inside the MoreComplexLoopTest class for convenience.
// Documentation for java.time.Year.isLeap()
// https://docs.oracle.com/javase/8/docs/api/java/time/Year.html#isLeap--

public class MoreComplexLoopTest {

    interface LeapYears {

        int countFrom1900(int upTo);

    }

    static class LeapYearCountBefore implements LeapYears {

        public int countFrom1900(int upTo) {
            if (upTo < 1900) {
                throw new IllegalArgumentException("The passed value is < 1900: " + upTo);
            }
            int count = 0;
            for (int year = 1900; year <= upTo; year += 4) {
                if (Year.isLeap(year)) {
                    count++;
                }
            }
            return count;
        }

    }

    static class LeapYearCountAfter implements LeapYears {

        public int countFrom1900(int upTo) {
            if (upTo < 1900) {
                throw new IllegalArgumentException("The passed value is < 1900: " + upTo);
            }
            return (int) IntStream.iterate(1900, year -> year <= upTo, year -> year + 4)
                    .filter(Year::isLeap)
                    .count();
        }

    }

    private static void commonLeapYearsTests(final LeapYears leapYears) {
        assertAll(
                () -> assertEquals(0, leapYears.countFrom1900(1900)),
                () -> assertEquals(25, leapYears.countFrom1900(2000)),
                () -> assertEquals(27, leapYears.countFrom1900(2010)),
                () -> assertEquals(31, leapYears.countFrom1900(2025)),
                () -> assertEquals(49, leapYears.countFrom1900(2100)),
                () -> assertEquals(267, leapYears.countFrom1900(3000)),
                () -> assertThrows(IllegalArgumentException.class, () -> leapYears.countFrom1900(1800))
        );
    }

    @Test
    void leapYearCountBefore() {
        commonLeapYearsTests(new LeapYearCountBefore());
    }

    @Test
    void leapYearCountAfter() {
        commonLeapYearsTests(new LeapYearCountAfter());
    }

}