import student.TestCase; /** * * @author nickgrifasi * @version 9/20/21 * Tests the Rectangle class and its methods. Some of these include * the getter methods, equals(), and findIntersection() */ public class RectangleTest extends TestCase { /** * Sets up test cases */ public void setUp() { //left intentionally empty } /** * Tests all the getter methods of the Rectangles */ public void testGetters() { Rectangle getterR = new Rectangle("getter", 5, 2, 1, 3); assertEquals(5, getterR.getX()); assertEquals(2, getterR.getY()); assertEquals(1, getterR.getWidth()); assertEquals(3, getterR.getHeight()); assertEquals("getter", getterR.getName()); } /** * tests the equals() method */ @SuppressWarnings("unlikely-arg-type") public void testEquals() { Rectangle shape = new Rectangle("getter", 5, 2, 1, 3); Rectangle shape2 = new Rectangle("nicksRectangle", 5, 2, 1, 3); Rectangle shape3 = new Rectangle("rectangle", 6, 1, 2, 6); Rectangle shape4 = new Rectangle("nicksRectangle", 5, 2, 1, 20); Rectangle shape5 = null; assertFalse(shape.equals(shape3)); assertTrue(shape.equals(shape2)); assertFalse(shape.equals(shape4)); assertTrue(shape.equals(shape)); assertFalse(shape.equals(shape5)); String testString = "testString"; Rectangle shape6 = new Rectangle("Shape", 5, 2, 20, 3); assertFalse(shape.equals(shape6)); assertFalse(shape.equals(testString)); Rectangle shape7 = new Rectangle("nicksRectangle", 100, 2, 1, 20); assertFalse(shape.equals(shape7)); Rectangle shape8 = new Rectangle("nicksRectangle", 5, 100, 1, 20); assertFalse(shape.equals(shape8)); Rectangle shape9 = new Rectangle("Shape", 5, 20, 1, 3); Rectangle shape10 = new Rectangle("Shape", 50, 2, 1, 3); assertFalse(shape.equals(shape9)); assertFalse(shape.equals(shape10)); } /** * tests the toString() method */ public void testToString() { Rectangle shape2 = new Rectangle("nicksRectangle", 5, 2, 1, 3); assertEquals("5, 2, 1, 3", shape2.toString()); } /** * tests the findIntersection() method */ public void testFindIntersection() { Rectangle shape = new Rectangle("robbysRectangle", 5, 2, 1, 3); Rectangle shape2 = new Rectangle("nicksRectangle", 5, 2, 1, 3); Rectangle shape3 = new Rectangle("ricksRectangle", 3, 2, 2, 4); Rectangle shape4 = new Rectangle("marysRectangle", 5, 5, 10, 1); Rectangle shape5 = new Rectangle("bethsRectangle", 4, 1, 2, 4); assertTrue(shape.findIntersection(shape)); assertTrue(shape.findIntersection(shape2)); assertFalse(shape.findIntersection(shape4)); assertFalse(shape.findIntersection(shape3)); assertFalse(shape.findIntersection(shape4)); assertTrue(shape.findIntersection(shape5)); Rectangle shape6 = new Rectangle("bethsRectangle", 30, 1, 2, 4); assertFalse(shape.findIntersection(shape6)); Rectangle shape7 = new Rectangle("bethsRectangle", 1, 1, 1, 1); assertFalse(shape4.findIntersection(shape7)); } }