Advertisement
nazar_art

AbsFigureTest now

Jan 16th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. package task.to.soft;
  2.  
  3. import task.to.soft.AbsFigure.*;
  4. import java.io.File;
  5. import java.util.*;
  6. import static org.junit.Assert.*;
  7. import org.junit.*;
  8.  
  9. public class AbsFigureTest {
  10.  
  11. public static final String defaultTestPath = "unittest.tmp";
  12. AreaCompare areaCompare;
  13. AbsFigure absFigure;
  14. List<FigureGeneral> inputFigures;
  15. List<FigureGeneral> outputFigures;
  16. DeepCompare deepCompare;// ==null
  17.  
  18. @Before
  19. public void before() throws FigureGeneralFilesFoundException,
  20. FigureGeneralFormatException {
  21. absFigure = new AbsFigure();
  22. inputFigures = absFigure.generateRandomFigures(5);
  23. absFigure.write(defaultTestPath, inputFigures);
  24. outputFigures = absFigure.figureReader(defaultTestPath);
  25. areaCompare = new AreaCompare();
  26. deepCompare = new DeepCompare();
  27. }
  28.  
  29. @After
  30. public void releaseResources() {
  31. new File(defaultTestPath).delete();
  32. }
  33.  
  34. @Test
  35. public void testRectArea() {
  36. Rectangle rectangle = new Rectangle(2.0, 2.0, "rectangle");
  37. double testResult = 4;
  38. double actualResult = rectangle.area();
  39. float delta = 0.0001f;
  40. assertEquals(testResult, actualResult, delta);
  41. }
  42.  
  43. @Test
  44. public void testTriangleArea() {
  45. Triangle triangle = new Triangle(2.0, 2.0, "triangle");
  46. double testResult = 2;
  47. double actualResult = triangle.area();
  48. float delta = 0.0001f;
  49. assertEquals(testResult, actualResult, delta);
  50. }
  51.  
  52. @Test
  53. public void testToStringFormat() {
  54. Triangle triangle = new Triangle(2.0, 2.0, "triangle");
  55. String testResult = "triangle 2.0 2.0";
  56. assertEquals(testResult, triangle.toString());
  57. }
  58.  
  59. @Test
  60. public void testEqual() {
  61. FigureGeneral oneFigure = new Rectangle(2.0, 2.0, "rectangle");
  62. FigureGeneral twoFigure = new Rectangle(2.0, 2.0, "rectangle");
  63. int result = areaCompare.compare(oneFigure, twoFigure);
  64. assertTrue("expected to be equal", result == 0);
  65. }
  66.  
  67. @Test
  68. public void testGreaterThan() {
  69. FigureGeneral oneFigure = new Triangle(2.0, 2.0, "triangle");
  70. FigureGeneral twoFigure = new Rectangle(1.0, 1.0, "rectangle");
  71. int result = areaCompare.compare(oneFigure, twoFigure);
  72. assertTrue("expected to be greater than", result >= 1);
  73. }
  74.  
  75. @Test
  76. public void testLessThan() {
  77. FigureGeneral oneFigure = new Rectangle(1.0, 1.0, "rectangle");
  78. FigureGeneral twoFigure = new Triangle(2.0, 2.0, "triangle");
  79. int result = areaCompare.compare(oneFigure, twoFigure);
  80. assertTrue("expected to be less than", result <= -1);
  81. }
  82.  
  83. @Test
  84. public void assertEqualsFigureGeneralLists() {
  85. for (int i = 0; i < inputFigures.size(); i++) {
  86. FigureGeneral inputValue = inputFigures.get(i);
  87. FigureGeneral outputValue = outputFigures.get(i);
  88.  
  89. if (deepCompare.compare(inputValue, outputValue) != 0)
  90. throw new RuntimeException("not equal");
  91. }
  92.  
  93. }
  94.  
  95. @Test
  96. public void testEqualDeepCompareRectangle() {
  97. FigureGeneral oneFigure = new Rectangle(2.0, 2.0, "rectangle");
  98. FigureGeneral twoFigure = new Rectangle(2.0, 2.0, "rectangle");
  99. int result = deepCompare.compare(oneFigure, twoFigure);
  100. assertTrue("expected to be equal", result == 0);
  101. }
  102.  
  103. @Test
  104. public void testEqualDeepCompareTriangle() {
  105. FigureGeneral oneFigure = new Triangle(2.0, 2.0, "rectangle");
  106. FigureGeneral twoFigure = new Triangle(2.0, 2.0, "rectangle");
  107. int result = deepCompare.compare(oneFigure, twoFigure);
  108. assertTrue("expected to be equal", result == 0);
  109. }
  110.  
  111. @Test
  112. public void testDeepCompareGreaterThan() {
  113. FigureGeneral oneFigure = new Triangle(2.0, 2.0, "triangle");
  114. FigureGeneral twoFigure = new Rectangle(1.0, 1.0, "rectangle");
  115. int result = deepCompare.compare(oneFigure, twoFigure);
  116. assertTrue("expected to be greater than", result >= 1);
  117.  
  118. if (deepCompare.compare(oneFigure, twoFigure) != 0)
  119. throw new RuntimeException("not equal");
  120. }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement