From 211f15658b12e049a756a84e09551fb46a8966ca Mon Sep 17 00:00:00 2001 From: Thibaud Date: Fri, 27 Mar 2026 00:16:09 +0100 Subject: [PATCH] test: add core unit tests - Add JUnit 5 Jupiter dependencies to pom.xml - Add maven-surefire-plugin for test execution - Add AbstractShapeTest for base class methods - Add SCircleTest, SRectangleTest, STriangleTest, STextTest Tests cover: creation, bounds, clone, resize (AbstractShape) --- pom.xml | 26 ++++++- .../newshapes/shapes/AbstractShapeTest.java | 68 +++++++++++++++++++ .../gasser/newshapes/shapes/SCircleTest.java | 58 ++++++++++++++++ .../newshapes/shapes/SRectangleTest.java | 45 ++++++++++++ .../gasser/newshapes/shapes/STextTest.java | 47 +++++++++++++ .../newshapes/shapes/STriangleTest.java | 37 ++++++++++ 6 files changed, 278 insertions(+), 3 deletions(-) create mode 100644 src/test/java/ovh/gasser/newshapes/shapes/AbstractShapeTest.java create mode 100644 src/test/java/ovh/gasser/newshapes/shapes/SCircleTest.java create mode 100644 src/test/java/ovh/gasser/newshapes/shapes/SRectangleTest.java create mode 100644 src/test/java/ovh/gasser/newshapes/shapes/STextTest.java create mode 100644 src/test/java/ovh/gasser/newshapes/shapes/STriangleTest.java diff --git a/pom.xml b/pom.xml index 17a30b9..2ef5780 100644 --- a/pom.xml +++ b/pom.xml @@ -6,8 +6,9 @@ UTF-8 - 1.10 - 1.10 + 16 + 16 + test/java @@ -19,6 +20,11 @@ 16 + + org.apache.maven.plugins + maven-surefire-plugin + 3.2.5 + @@ -38,5 +44,19 @@ logback-classic RELEASE + + + org.junit.jupiter + junit-jupiter-api + 5.10.0 + test + + + + org.junit.jupiter + junit-jupiter-engine + 5.10.0 + test + - \ No newline at end of file + diff --git a/src/test/java/ovh/gasser/newshapes/shapes/AbstractShapeTest.java b/src/test/java/ovh/gasser/newshapes/shapes/AbstractShapeTest.java new file mode 100644 index 0000000..33fd40a --- /dev/null +++ b/src/test/java/ovh/gasser/newshapes/shapes/AbstractShapeTest.java @@ -0,0 +1,68 @@ +package ovh.gasser.newshapes.shapes; + +import ovh.gasser.newshapes.attributes.Attributes; +import ovh.gasser.newshapes.attributes.SelectionAttributes; +import org.junit.jupiter.api.Test; +import java.awt.Rectangle; + +import static org.junit.jupiter.api.Assertions.*; + +class AbstractShapeTest { + + @Test + void testGetBoundsReturnsCopy() { + SRectangle rect = SRectangle.create(10, 20, 100, 50); + Rectangle bounds = rect.getBounds(); + + // Modify the returned bounds + bounds.x = 999; + + // Original should be unchanged + assertEquals(10, rect.getBounds().x); + } + + @Test + void testAddAndGetAttributes() { + SRectangle rect = SRectangle.create(0, 0, 10, 10); + Attributes attrs = new SelectionAttributes(); + rect.addAttributes(attrs); + + assertSame(attrs, rect.getAttributes(SelectionAttributes.ID)); + } + + @Test + void testTranslate() { + SRectangle rect = SRectangle.create(10, 20, 100, 50); + rect.translate(5, 10); + + assertEquals(15, rect.getBounds().x); + assertEquals(30, rect.getBounds().y); + } + + @Test + void testResizeEast() { + SRectangle rect = SRectangle.create(0, 0, 100, 50); + rect.resize(ResizeHandle.E, 20, 0); + + assertEquals(120, rect.getBounds().width); + } + + @Test + void testResizeSouth() { + SRectangle rect = SRectangle.create(0, 0, 100, 50); + rect.resize(ResizeHandle.S, 0, 30); + + assertEquals(80, rect.getBounds().height); + } + + @Test + void testToString() { + SRectangle rect = SRectangle.create(10, 20, 100, 50); + String str = rect.toString(); + + assertTrue(str.contains("x=10")); + assertTrue(str.contains("y=20")); + assertTrue(str.contains("width=100")); + assertTrue(str.contains("height=50")); + } +} diff --git a/src/test/java/ovh/gasser/newshapes/shapes/SCircleTest.java b/src/test/java/ovh/gasser/newshapes/shapes/SCircleTest.java new file mode 100644 index 0000000..7ef674c --- /dev/null +++ b/src/test/java/ovh/gasser/newshapes/shapes/SCircleTest.java @@ -0,0 +1,58 @@ +package ovh.gasser.newshapes.shapes; + +import org.junit.jupiter.api.Test; +import java.awt.Color; +import java.awt.Rectangle; +import java.awt.Shape; + +import static org.junit.jupiter.api.Assertions.*; + +class SCircleTest { + @Test + void testCreateWithDefaultColor() { + SCircle circle = SCircle.create(10, 20, 50); + assertNotNull(circle); + assertEquals(50, circle.getRadius()); + } + + @Test + void testCreateWithCustomColor() { + SCircle circle = SCircle.create(0, 0, 10, Color.RED); + assertNotNull(circle); + assertEquals(10, circle.getRadius()); + try { + Object color = circle.getClass().getMethod("getColor").invoke(circle); + assertEquals(Color.RED, color); + } catch (NoSuchMethodException e) { + // Method not available; skip color check + } catch (Exception e) { + fail("Unexpected exception while verifying color: " + e); + } + } + + @Test + void testGetRadius() { + SCircle circle = SCircle.create(5, 5, 15); + assertEquals(15, circle.getRadius()); + } + + @Test + void testCloneCreatesIndependentCopy() { + SCircle original = SCircle.create(12, 34, 7); + Object cloneObj = original.clone(); + assertNotSame(original, cloneObj); + assertTrue(cloneObj instanceof SCircle); + SCircle clone = (SCircle) cloneObj; + assertEquals(original.getRadius(), clone.getRadius()); + } + + @Test + void testBoundsAreCalculatedFromRadius() { + SCircle circle = SCircle.create(10, 20, 5); + Rectangle bounds = circle.getBounds(); + assertEquals(10, bounds.x); + assertEquals(20, bounds.y); + assertEquals(10, bounds.width); + assertEquals(10, bounds.height); + } +} diff --git a/src/test/java/ovh/gasser/newshapes/shapes/SRectangleTest.java b/src/test/java/ovh/gasser/newshapes/shapes/SRectangleTest.java new file mode 100644 index 0000000..b0f94bb --- /dev/null +++ b/src/test/java/ovh/gasser/newshapes/shapes/SRectangleTest.java @@ -0,0 +1,45 @@ +package ovh.gasser.newshapes.shapes; + +import org.junit.jupiter.api.Test; +import java.awt.Color; +import java.awt.Rectangle; + +import static org.junit.jupiter.api.Assertions.*; + +class SRectangleTest { + @Test + void testCreateWithDefaultColor() { + SRectangle rect = SRectangle.create(10, 20, 100, 50); + assertNotNull(rect); + assertEquals(100, rect.getBounds().width); + assertEquals(50, rect.getBounds().height); + } + + @Test + void testCreateWithCustomColor() { + SRectangle rect = SRectangle.create(5, 5, 30, 40, Color.RED); + assertNotNull(rect); + assertEquals(30, rect.getBounds().width); + assertEquals(40, rect.getBounds().height); + } + + @Test + void testCloneCreatesIndependentCopy() { + SRectangle original = SRectangle.create(0, 0, 10, 10, Color.BLUE); + Object cloneObj = original.clone(); + assertNotSame(original, cloneObj); + assertTrue(cloneObj instanceof SRectangle); + SRectangle clone = (SRectangle) cloneObj; + assertEquals(original.getBounds(), clone.getBounds()); + } + + @Test + void testBoundsAreSetCorrectly() { + SRectangle rect = SRectangle.create(15, 25, 80, 60); + Rectangle bounds = rect.getBounds(); + assertEquals(15, bounds.x); + assertEquals(25, bounds.y); + assertEquals(80, bounds.width); + assertEquals(60, bounds.height); + } +} diff --git a/src/test/java/ovh/gasser/newshapes/shapes/STextTest.java b/src/test/java/ovh/gasser/newshapes/shapes/STextTest.java new file mode 100644 index 0000000..5a49c7c --- /dev/null +++ b/src/test/java/ovh/gasser/newshapes/shapes/STextTest.java @@ -0,0 +1,47 @@ +package ovh.gasser.newshapes.shapes; + +import org.junit.jupiter.api.Test; +import java.awt.Font; +import java.awt.Rectangle; + +import static org.junit.jupiter.api.Assertions.*; + +class STextTest { + @Test + void testCreate() { + SText text = SText.create(10, 20, "Hello"); + assertNotNull(text); + assertEquals("Hello", text.getText()); + } + + @Test + void testCreateWithDefaultValues() { + SText text = SText.create(0, 0, "Test"); + assertEquals("Test", text.getText()); + assertEquals(SText.DEFAULT_FONT_SIZE, text.getFontSize()); + assertEquals(SText.DEFAULT_FONT_NAME, text.getFontName()); + assertEquals(SText.DEFAULT_FONT_STYLE, text.getFontStyle()); + } + + @Test + void testCloneCreatesIndependentCopy() { + SText original = SText.create(5, 5, "Copy"); + Object cloneObj = original.clone(); + assertNotSame(original, cloneObj); + assertTrue(cloneObj instanceof SText); + SText clone = (SText) cloneObj; + assertEquals(original.getText(), clone.getText()); + } + + @Test + void testPlaceholderTextForEmpty() { + SText text = SText.create(0, 0, ""); + assertEquals(SText.PLACEHOLDER_TEXT, text.getText()); + } + + @Test + void testPlaceholderTextForNull() { + SText text = SText.create(0, 0, null); + assertEquals(SText.PLACEHOLDER_TEXT, text.getText()); + } +} diff --git a/src/test/java/ovh/gasser/newshapes/shapes/STriangleTest.java b/src/test/java/ovh/gasser/newshapes/shapes/STriangleTest.java new file mode 100644 index 0000000..fd11fee --- /dev/null +++ b/src/test/java/ovh/gasser/newshapes/shapes/STriangleTest.java @@ -0,0 +1,37 @@ +package ovh.gasser.newshapes.shapes; + +import org.junit.jupiter.api.Test; +import java.awt.Color; +import java.awt.Rectangle; + +import static org.junit.jupiter.api.Assertions.*; + +class STriangleTest { + @Test + void testCreate() { + STriangle triangle = STriangle.create(10, 20, 50, Color.RED, Color.BLACK); + assertNotNull(triangle); + assertEquals(50, triangle.getBounds().width); + assertEquals(50, triangle.getBounds().height); + } + + @Test + void testCloneCreatesIndependentCopy() { + STriangle original = STriangle.create(0, 0, 30, Color.BLUE, Color.BLACK); + Object cloneObj = original.clone(); + assertNotSame(original, cloneObj); + assertTrue(cloneObj instanceof STriangle); + STriangle clone = (STriangle) cloneObj; + assertEquals(original.getBounds(), clone.getBounds()); + } + + @Test + void testBoundsAreSetCorrectly() { + STriangle triangle = STriangle.create(5, 10, 25, Color.GREEN, Color.BLACK); + Rectangle bounds = triangle.getBounds(); + assertEquals(5, bounds.x); + assertEquals(10, bounds.y); + assertEquals(25, bounds.width); + assertEquals(25, bounds.height); + } +}