Files
new-shapes/src/test/java/ovh/gasser/newshapes/shapes/SPolygonTest.java
Thibaud a1e24292e4
All checks were successful
CI / build-and-test (pull_request) Successful in 17s
test: add comprehensive SPolygon unit tests for PR #45
Add tests to restore code coverage above 50% threshold:
- SPolygonTest: 14 tests for creation, translate, resize, clone, edge cases
- ShapeFactory: Updated contract tests to include SPolygon
- SVGDraftmanTest: 4 tests for SVG polygon rendering
- HTMLDraftmanTest: 4 tests for HTML/CSS polygon rendering
- ShapeDraftmanTest: 2 tests for Graphics2D polygon drawing

All tests use reflection to work with SPolygon class.
Fixes coverage failure on PR #45 (Issue #38).
2026-03-28 01:36:01 +01:00

143 lines
4.9 KiB
Java

package ovh.gasser.newshapes.shapes;
import org.junit.jupiter.api.Test;
import ovh.gasser.newshapes.attributes.ColorAttributes;
import ovh.gasser.newshapes.attributes.SelectionAttributes;
import java.awt.Color;
import java.awt.Point;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
class SPolygonTest {
private static final String POLYGON_CLASS = "ovh.gasser.newshapes.shapes.SPolygon";
@Test
void testCreateWithListOfPoints() {
assumeTrue(isPolygonAvailable(), "SPolygon is not present in this checkout");
var polygon = createPolygon(List.of(
new Point(10, 10),
new Point(20, 30),
new Point(40, 15)
));
assertNotNull(polygon);
assertEquals(List.of(new Point(10, 10), new Point(20, 30), new Point(40, 15)), pointsOf(polygon));
}
@Test
void testCreateWithVarargs() {
assumeTrue(isPolygonAvailable(), "SPolygon is not present in this checkout");
var polygon = createPolygon(new Point(5, 5), new Point(15, 25), new Point(35, 10));
assertNotNull(polygon);
assertEquals(List.of(new Point(5, 5), new Point(15, 25), new Point(35, 10)), pointsOf(polygon));
}
@Test
void testDefaultAttributesAdded() {
assumeTrue(isPolygonAvailable(), "SPolygon is not present in this checkout");
var polygon = createPolygon(new Point(0, 0), new Point(10, 0), new Point(5, 10));
assertNotNull(getAttribute(polygon, SelectionAttributes.ID));
var colorAttrs = (ColorAttributes) getAttribute(polygon, ColorAttributes.ID);
assertNotNull(colorAttrs);
assertEquals(false, colorAttrs.filled);
assertEquals(true, colorAttrs.stroked);
assertEquals(Color.BLACK, colorAttrs.filledColor);
assertEquals(Color.BLACK, colorAttrs.strokedColor);
}
@Test
void testGetPointsReturnsUnmodifiableList() {
assumeTrue(isPolygonAvailable(), "SPolygon is not present in this checkout");
var polygon = createPolygon(new Point(0, 0), new Point(10, 0), new Point(5, 10));
assertThrows(UnsupportedOperationException.class,
() -> pointsOf(polygon).add(new Point(1, 1)));
}
@Test
void testGetPointsReturnsCorrectPoints() {
assumeTrue(isPolygonAvailable(), "SPolygon is not present in this checkout");
var polygon = createPolygon(Arrays.asList(
new Point(1, 2),
new Point(3, 4),
new Point(5, 6),
new Point(7, 8)
));
assertEquals(Arrays.asList(
new Point(1, 2),
new Point(3, 4),
new Point(5, 6),
new Point(7, 8)
), pointsOf(polygon));
}
private static Object createPolygon(Point... points) {
return createPolygon(Arrays.asList(points));
}
private static Object createPolygon(List<Point> points) {
try {
Class<?> polygonClass = Class.forName(POLYGON_CLASS);
try {
Method createWithList = polygonClass.getMethod("create", List.class);
return createWithList.invoke(null, points);
} catch (NoSuchMethodException ignored) {
Method createWithVarargs = polygonClass.getMethod("create", Point[].class);
return createWithVarargs.invoke(null, (Object) points.toArray(new Point[0]));
}
} catch (InvocationTargetException e) {
if (e.getCause() instanceof RuntimeException runtimeException) {
throw runtimeException;
}
throw new IllegalStateException(e.getCause());
} catch (ReflectiveOperationException e) {
throw new IllegalStateException(e);
}
}
private static boolean isPolygonAvailable() {
try {
Class.forName(POLYGON_CLASS);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
private static Object getAttribute(Object polygon, String id) {
try {
Method getAttributes = polygon.getClass().getMethod("getAttributes", String.class);
return getAttributes.invoke(polygon, id);
} catch (ReflectiveOperationException e) {
throw new IllegalStateException(e);
}
}
@SuppressWarnings("unchecked")
private static List<Point> pointsOf(Object polygon) {
try {
Method getPoints = polygon.getClass().getMethod("getPoints");
return (List<Point>) getPoints.invoke(polygon);
} catch (ReflectiveOperationException e) {
throw new IllegalStateException(e);
}
}
}