From db45700a561d0fb0eaddb2d99fea4391529d3f2e Mon Sep 17 00:00:00 2001 From: William Billingsley <wbillingsley@cantab.net> Date: Wed, 7 Jul 2021 16:12:33 +1000 Subject: [PATCH] Added log4j and a basic test class --- build.gradle | 4 +++ .../java/dotsandboxes/DotsAndBoxesUI.java | 4 +++ src/main/java/module-info.java | 1 + .../dotsandboxes/DotsAndBoxesGridTest.java | 31 +++++++++++++++++++ src/test/resources/log4j2.xml | 13 ++++++++ 5 files changed, 53 insertions(+) create mode 100644 src/test/java/dotsandboxes/DotsAndBoxesGridTest.java create mode 100644 src/test/resources/log4j2.xml diff --git a/build.gradle b/build.gradle index fd455a2..5705373 100644 --- a/build.gradle +++ b/build.gradle @@ -25,6 +25,10 @@ repositories { } dependencies { + // Log4J does logging. We'll meet it properly in a later week... + implementation 'org.apache.logging.log4j:log4j-api:2.12.0' + implementation 'org.apache.logging.log4j:log4j-core:2.12.0' + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0' } diff --git a/src/main/java/dotsandboxes/DotsAndBoxesUI.java b/src/main/java/dotsandboxes/DotsAndBoxesUI.java index 154cb88..66010e3 100644 --- a/src/main/java/dotsandboxes/DotsAndBoxesUI.java +++ b/src/main/java/dotsandboxes/DotsAndBoxesUI.java @@ -79,6 +79,10 @@ public class DotsAndBoxesUI { grid.drawHorizontal(x, y, grid.getPlayer()); } catch (IllegalStateException ex) { // do nothing + // This is a little artificial, as normally we'd implement this with a check that the line isn't + // already "drawn" and then not calling the function. But for the exercise, we wanted students + // to write a test that would ensure an exception is thrown, so we're relying on an exception + // being thrown! }}); AnchorPane.setLeftAnchor(line, 0.0 + gap + dotDiameter + col * (gap + lineLength + gap + dotDiameter)); diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index c99b9f2..eb9361f 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -14,5 +14,6 @@ module dotsAndBoxes { requires javafx.graphics; requires javafx.controls; + requires org.apache.logging.log4j; exports dotsandboxes; } \ No newline at end of file diff --git a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java new file mode 100644 index 0000000..1946bed --- /dev/null +++ b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java @@ -0,0 +1,31 @@ +package dotsandboxes; + +import org.junit.jupiter.api.*; +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assumptions.*; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class DotsAndBoxesGridTest { + /* + * Because Test classes are classes, they can have fields, and can have static fields. + * This field is a logger. Loggers are like a more advanced println, for writing messages out to the console or a log file. + */ + private static final Logger logger = LogManager.getLogger(DotsAndBoxesGridTest.class); + + /* + * Tests are functions that have an @Test annotation before them. + * The typical format of a test is that it contains some code that does something, and then one + * or more assertions to check that a condition holds. + * + * This is a dummy test just to show that the test suite itself runs + */ + @Test + public void testTestSuiteRuns() { + logger.info("Dummy test to show the test suite runs"); + assertTrue(true); + } + + // FIXME: You need to write tests for the two known bugs in the code. +} diff --git a/src/test/resources/log4j2.xml b/src/test/resources/log4j2.xml new file mode 100644 index 0000000..32c8f6b --- /dev/null +++ b/src/test/resources/log4j2.xml @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="UTF-8"?> +<Configuration status="WARN"> + <Appenders> + <Console name="Console" target="SYSTEM_OUT"> + <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> + </Console> + </Appenders> + <Loggers> + <Root level="debug"> + <AppenderRef ref="Console"/> + </Root> + </Loggers> +</Configuration> \ No newline at end of file -- GitLab