|
| 1 | +package src.test.java.com.designpatterns.creational.abstractfactory; |
| 2 | + |
| 3 | +import org.junit.Assert; |
| 4 | +import org.junit.Test; |
| 5 | +import src.main.java.com.designpatterns.creational.abstractfactory.*; |
| 6 | + |
| 7 | +public class AbstractShapeFactoryTest { |
| 8 | + @Test |
| 9 | + public void testAbstractShapeFactory() { |
| 10 | + String failReason = ""; |
| 11 | + // Tests for 2-D shape factory |
| 12 | + // Test for Line |
| 13 | + AbstractShapeFactory shapeFactory = FactoryProvider.getShapeFactory(FactoryType.TWO_D_FACTORY); |
| 14 | + Shape shape = shapeFactory.getShape(ShapeType.LINE); |
| 15 | + if (shape.getShapeType() != ShapeType.LINE) { |
| 16 | + failReason += "Could not create an object for LINE.\n"; |
| 17 | + } |
| 18 | + if (shape.surfaceArea(5) != 0) { |
| 19 | + failReason += "Surface area of Line is incorrect!.\n"; |
| 20 | + } |
| 21 | + |
| 22 | + // Test for circle |
| 23 | + shape = shapeFactory.getShape(ShapeType.CIRCLE); |
| 24 | + if (shape.getShapeType() != ShapeType.CIRCLE) { |
| 25 | + failReason += "Could not create an object for CIRCLE.\n"; |
| 26 | + } |
| 27 | + if (shape.surfaceArea(9) != 254.46900494077323) { |
| 28 | + failReason += "Surface area of Circle is incorrect!.\n"; |
| 29 | + } |
| 30 | + |
| 31 | + // Test for 3-D shape factory |
| 32 | + // Test for Sphere |
| 33 | + shapeFactory = FactoryProvider.getShapeFactory(FactoryType.THREE_D_FACTORY); |
| 34 | + shape = shapeFactory.getShape(ShapeType.SPHERE); |
| 35 | + |
| 36 | + if (shape.getShapeType() != ShapeType.SPHERE) { |
| 37 | + failReason += "Could not create and object for SPHERE.\n"; |
| 38 | + } |
| 39 | + if (shape.surfaceArea(6) != 452.3893421169302) { |
| 40 | + failReason += "Surface area of Sphere is incorrect!.\n"; |
| 41 | + } |
| 42 | + |
| 43 | + Assert.assertEquals(failReason, "", failReason); |
| 44 | + |
| 45 | + } |
| 46 | +} |
0 commit comments