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)
This commit is contained in:
@@ -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"));
|
||||
}
|
||||
}
|
||||
58
src/test/java/ovh/gasser/newshapes/shapes/SCircleTest.java
Normal file
58
src/test/java/ovh/gasser/newshapes/shapes/SCircleTest.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
47
src/test/java/ovh/gasser/newshapes/shapes/STextTest.java
Normal file
47
src/test/java/ovh/gasser/newshapes/shapes/STextTest.java
Normal file
@@ -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());
|
||||
}
|
||||
}
|
||||
37
src/test/java/ovh/gasser/newshapes/shapes/STriangleTest.java
Normal file
37
src/test/java/ovh/gasser/newshapes/shapes/STriangleTest.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user