diff --git a/gradle-6/fibonacci-recursive/build.gradle.kts b/gradle-6/fibonacci-recursive/build.gradle.kts new file mode 100644 index 000000000000..0872a524721f --- /dev/null +++ b/gradle-6/fibonacci-recursive/build.gradle.kts @@ -0,0 +1,17 @@ +plugins { + `java-library` +} + +dependencies { + api(project(":fibonacci-spi")) + compileOnly("com.google.auto.service:auto-service-annotations:1.0-rc6") + annotationProcessor("com.google.auto.service:auto-service:1.0-rc6") + + testImplementation(testFixtures(project(":fibonacci-spi"))) + testImplementation("org.junit.jupiter:junit-jupiter-api:5.5.2") + testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.5.2") +} + +tasks.test { + useJUnitPlatform() +} \ No newline at end of file diff --git a/gradle-6/fibonacci-recursive/src/main/java/com/baeldung/fibonacci/impl/RecursiveFibonacci.java b/gradle-6/fibonacci-recursive/src/main/java/com/baeldung/fibonacci/impl/RecursiveFibonacci.java new file mode 100644 index 000000000000..97eccde0548b --- /dev/null +++ b/gradle-6/fibonacci-recursive/src/main/java/com/baeldung/fibonacci/impl/RecursiveFibonacci.java @@ -0,0 +1,20 @@ +package com.baeldung.fibonacci.impl; + +import com.baeldung.fibonacci.FibonacciSequenceGenerator; +import com.google.auto.service.AutoService; + +/** Recursive implementation of the {@link FibonacciSequenceGenerator}. */ +@AutoService(FibonacciSequenceGenerator.class) +public final class RecursiveFibonacci implements FibonacciSequenceGenerator { + + @Override + public int generate(int nth) { + if (nth < 0) { + throw new IllegalArgumentException("sequence number must be 0 or greater"); + } + if (nth <= 1) { + return nth; + } + return generate(nth - 1) + generate(nth - 2); + } +} diff --git a/gradle-6/fibonacci-recursive/src/test/java/com/baeldung/fibonacci/impl/RecursiveFibonacciUnitTest.java b/gradle-6/fibonacci-recursive/src/test/java/com/baeldung/fibonacci/impl/RecursiveFibonacciUnitTest.java new file mode 100644 index 000000000000..1e46e7510611 --- /dev/null +++ b/gradle-6/fibonacci-recursive/src/test/java/com/baeldung/fibonacci/impl/RecursiveFibonacciUnitTest.java @@ -0,0 +1,16 @@ +package com.baeldung.fibonacci.impl; + +import com.baeldung.fibonacci.FibonacciSequenceGenerator; +import com.baeldung.fibonacci.FibonacciSequenceGeneratorFixture; + +/** + * Unit test which reuses the {@link FibonacciSequenceGeneratorFixture} test mix-in exported from + * the fibonacci-spi project. + */ +final class RecursiveFibonacciUnitTest implements FibonacciSequenceGeneratorFixture { + + @Override + public FibonacciSequenceGenerator provide() { + return new RecursiveFibonacci(); + } +} diff --git a/gradle-6/fibonacci-spi/build.gradle.kts b/gradle-6/fibonacci-spi/build.gradle.kts new file mode 100644 index 000000000000..e571f329a956 --- /dev/null +++ b/gradle-6/fibonacci-spi/build.gradle.kts @@ -0,0 +1,13 @@ +plugins { + `java-library` + `java-test-fixtures` +} + +dependencies { + testFixturesApi("org.junit.jupiter:junit-jupiter-api:5.5.2") + testFixturesImplementation("org.junit.jupiter:junit-jupiter-engine:5.5.2") +} + +tasks.test { + useJUnitPlatform() +} \ No newline at end of file diff --git a/gradle-6/fibonacci-spi/src/main/java/com/baeldung/fibonacci/FibonacciSequenceGenerator.java b/gradle-6/fibonacci-spi/src/main/java/com/baeldung/fibonacci/FibonacciSequenceGenerator.java new file mode 100644 index 000000000000..1e9de212576a --- /dev/null +++ b/gradle-6/fibonacci-spi/src/main/java/com/baeldung/fibonacci/FibonacciSequenceGenerator.java @@ -0,0 +1,11 @@ +package com.baeldung.fibonacci; + +/** Describes an SPI for a Fibonacci sequence generator function. */ +public interface FibonacciSequenceGenerator { + + /** + * @param nth the index of the number in the fibonacci sequence + * @return the nth number in the fibonacci sequence + */ + int generate(int nth); +} diff --git a/gradle-6/fibonacci-spi/src/testFixtures/java/com/baeldung/fibonacci/FibonacciSequenceGeneratorFixture.java b/gradle-6/fibonacci-spi/src/testFixtures/java/com/baeldung/fibonacci/FibonacciSequenceGeneratorFixture.java new file mode 100644 index 000000000000..0b7988f64047 --- /dev/null +++ b/gradle-6/fibonacci-spi/src/testFixtures/java/com/baeldung/fibonacci/FibonacciSequenceGeneratorFixture.java @@ -0,0 +1,31 @@ +package com.baeldung.fibonacci; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +/** + * Reusable test fixture for {@link FibonacciSequenceGenerator} implementations. Tests will be + * skipped if no such implementation exists. + */ +public interface FibonacciSequenceGeneratorFixture { + + /** @return the implementation of {@link FibonacciSequenceGenerator} to test. Must not be null */ + FibonacciSequenceGenerator provide(); + + @Test + default void when_sequence_index_is_negative_then_throws() { + final FibonacciSequenceGenerator generator = provide(); + assertThrows(IllegalArgumentException.class, () -> generator.generate(-1)); + } + + @Test + default void when_given_index_then_generates_fibonacci_number() { + final FibonacciSequenceGenerator generator = provide(); + final int[] sequence = {0, 1, 1, 2, 3, 5, 8}; + for (int i = 0; i < sequence.length; i++) { + assertEquals(sequence[i], generator.generate(i)); + } + } +} diff --git a/gradle-6/gradle.properties b/gradle-6/gradle.properties new file mode 100644 index 000000000000..f97ebb7d3349 --- /dev/null +++ b/gradle-6/gradle.properties @@ -0,0 +1 @@ +org.gradle.parallel=true diff --git a/gradle-6/settings.gradle.kts b/gradle-6/settings.gradle.kts index d84d0ff54e1b..babe43117558 100644 --- a/gradle-6/settings.gradle.kts +++ b/gradle-6/settings.gradle.kts @@ -1,6 +1,8 @@ rootProject.name = "gradle-6" include("dependency-constraints") +include("fibonacci-spi") +include("fibonacci-recursive") include("httpclient-platform") include("module-metadata-publishing") include("person-rest-client")