Advertisement
Guest User

Untitled

a guest
Jul 25th, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.69 KB | None | 0 0
  1.  
  2. import static org.junit.Assert.*;
  3. import org.junit.After;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6. import java.util.ArrayList;
  7. import java.io.ByteArrayOutputStream;
  8. import java.io.PrintStream;
  9. import java.util.Arrays;
  10.  
  11. /**
  12.  * The test class Assignment2Test.
  13.  */
  14. public class Assignment2Test
  15. {
  16.     public static final int EXPECTED_NUMBER_OF_EQUAL_CARS = 9;
  17.     public static final int EXPECTED_NUMBER_OF_EQUAL_BOATS = 4;
  18.     public static final int EXPECTED_NUMBER_OF_EQUAL_AIRPLANES = 0;
  19.     private static final String NEWLINE = System.getProperty("line.separator");//"\r\n";
  20.     private static final String TESTCODE_SUFFIX = "\";" + NEWLINE;
  21.     private static final String MINOR_ERROR_MESSAGE = "-1 point.";
  22.     private static final boolean PRODUCE_JUNIT_CODE = false;
  23.     private static final String TESTCODE_PREFIX = "final String EXPECTED = \"";
  24.     private static final String MAJOR_ERROR_MESSAGE = MINOR_ERROR_MESSAGE + "  The amount of lines in your solution is not the same as the amount of lines in my solution.";
  25.     private static PrintStream stdOut = System.out;
  26.     private static final String TESTCODE_NEWLINE = "\"" + NEWLINE + "\t+ NEWLINE + \"";
  27.     private ByteArrayOutputStream outContent;
  28.  
  29.     private ArrayList<Vehicle> stable;
  30.     /**
  31.      * Default constructor for test class Assignment2Test
  32.      */
  33.     public Assignment2Test()
  34.     {
  35.         stable = new ArrayList<Vehicle>();
  36.        
  37.        
  38.     }
  39.  
  40.     /**
  41.      * Sets up the test fixture.
  42.      *
  43.      * Called before every test case method.
  44.      */
  45.     @Before
  46.     public void setUp()
  47.     {
  48.         outContent = new ByteArrayOutputStream();
  49.         stable.add(new Car(2000, "Lamborghini", "Diablo", 700));
  50.         stable.add(new Car(1997, "Dodge", "Ram", 175));
  51.         stable.add(new Car(1940, "Buggati", "Vehron", 135));
  52.         stable.add(new Car(2014, "Honda", "Civic", 143));
  53.         stable.add(new Car(2011, "Honda", "Civic", 143));
  54.         stable.add(new Car(1999, "Toyota", "Corrola", 140));
  55.  
  56.         stable.add(new Boat(1980, "Bayliner", "Extreme", true));
  57.         stable.add(new Boat(2014, "Bayliner", "Extreme II", true));
  58.         stable.add(new Boat(2000, "American Skier", "Skier Supreme", false));
  59.         stable.add(new Boat(2010, "Boesch", "Journey", false));
  60.  
  61.         stable.add(new Airplane(1998, "ABC Motors", "Comac", 10000));
  62.         stable.add(new Airplane(1940, "Boeing", "84", 45000));
  63.         stable.add(new Airplane(2012, "Boeing", "737", 80000));
  64.         stable.add(new Airplane(2014, "Abrams", "Motorhead", 70000));
  65.        
  66.        
  67.        
  68.     }
  69.  
  70.  
  71.     /**
  72.      * Tears down the test fixture.
  73.      *
  74.      * Called after every test case method.
  75.      */
  76.     @After
  77.     public void tearDown()
  78.     {
  79.         stable.clear();
  80.     }
  81.  
  82.         private void verify(String expected)
  83.     {
  84.         String actual = outContent.toString(); // for debug purposes
  85.         stdOut.println(PRODUCE_JUNIT_CODE
  86.             ? (TESTCODE_PREFIX
  87.                     + actual.replaceAll(NEWLINE, TESTCODE_NEWLINE)
  88.                     + TESTCODE_SUFFIX
  89.                 ) : actual
  90.         );
  91.  
  92.         assertEquals(MINOR_ERROR_MESSAGE, expected, actual);        
  93.     } // end method
  94.    
  95.    
  96.     private void verifyAnyOrder(String expected)
  97.     {
  98.         String actual = outContent.toString(); // for debug purposes
  99.         stdOut.println(actual);
  100.  
  101.         String[] actualStrings = actual.split(NEWLINE);
  102.         String[] expectedStrings = expected.split(NEWLINE);
  103.  
  104.         int i = actualStrings.length;
  105.         assertEquals(MAJOR_ERROR_MESSAGE, expectedStrings.length, i);
  106.  
  107.         Arrays.sort(actualStrings);
  108.         Arrays.sort(expectedStrings);
  109.  
  110.         while (0 != i--)
  111.             assertEquals(MINOR_ERROR_MESSAGE, expectedStrings[i], actualStrings[i]);
  112.     } // end method
  113.  
  114.     @Test
  115.     public void testEqualsCar()
  116.     {
  117.         int numberOfEqualCars = 0;
  118.         for (int i = 0; i < stable.size(); i++)
  119.         {
  120.             Vehicle c = stable.get(i);
  121.             if (c instanceof Car)
  122.             {
  123.                 Car car1 = (Car) c;
  124.                 for (int j = 0; j < stable.size(); j++)
  125.                 {  
  126.                     if (j != i)
  127.                     {
  128.                         Vehicle b = stable.get(j);
  129.                         if (b instanceof Car)
  130.                         {
  131.                             Car car2 = (Car) b;
  132.                             if(car1.equals(car2))
  133.                             {
  134.                                 numberOfEqualCars++;
  135.                             }
  136.                         }
  137.                     }
  138.                 }
  139.             }
  140.         }
  141.  
  142.         assertEquals(EXPECTED_NUMBER_OF_EQUAL_CARS, numberOfEqualCars, 0.1);
  143.     }
  144.  
  145.     @Test
  146.     public void testEqualsBoats()
  147.     {
  148.         int numberOfEqualBoats = 0;
  149.         for (int i = 0; i < stable.size(); i++)
  150.         {
  151.             Vehicle c = stable.get(i);
  152.             if (c instanceof Boat)
  153.             {
  154.                 Boat boat1 = (Boat) c;
  155.                 for (int j = 0; j < stable.size(); j++)
  156.                 {  
  157.                     if (j != i)
  158.                     {
  159.                         Vehicle b = stable.get(j);
  160.                         if (b instanceof Boat)
  161.                         {
  162.                             Boat boat2 = (Boat) b;
  163.                             if(boat1.equals(boat2))
  164.                             {
  165.                                 numberOfEqualBoats++;
  166.                             }
  167.                         }
  168.                     }
  169.                 }
  170.             }
  171.         }
  172.  
  173.         assertEquals(EXPECTED_NUMBER_OF_EQUAL_BOATS, numberOfEqualBoats, 0.1);
  174.     }
  175.  
  176.     @Test
  177.     public void testEqualsAirplane()
  178.     {
  179.         int numberOfEqualAirplanes = 0;
  180.         for (int i = 0; i < stable.size(); i++)
  181.         {
  182.             Vehicle c = stable.get(i);
  183.             if (c instanceof Airplane)
  184.             {
  185.                 Airplane airplane1 = (Airplane) c;
  186.                 for (int j = 0; j < stable.size(); j++)
  187.                 {  
  188.                     if (j != i)
  189.                     {
  190.                         Vehicle b = stable.get(j);
  191.                         if (b instanceof Airplane)
  192.                         {
  193.                             Airplane airplane2 = (Airplane) b;
  194.                             if(airplane1.equals(airplane2))
  195.                             {
  196.                                 numberOfEqualAirplanes++;
  197.                             }
  198.                         }
  199.                     }
  200.                 }
  201.             }
  202.         }
  203.  
  204.         assertEquals(EXPECTED_NUMBER_OF_EQUAL_AIRPLANES, numberOfEqualAirplanes, 0.1);
  205.     }
  206.  
  207.     @Test
  208.     public void testToStringAirplane()
  209.     {
  210.         final String EXPECTED = "This airplane is a 1998 ABC Motors Comac that can reach 10000"
  211.             + NEWLINE + "This airplane is a 1940 Boeing 84 that can reach 45000"
  212.             + NEWLINE + "This airplane is a 2012 Boeing 737 that can reach 80000"
  213.             + NEWLINE + "This airplane is a 2014 Abrams Motorhead that can reach 70000"
  214.             + NEWLINE + "";
  215.  
  216.         for (Vehicle v : stable)
  217.         {
  218.             if (v instanceof Airplane)
  219.             {
  220.                 Airplane a = (Airplane) v;
  221.                 System.out.println(a);
  222.             }
  223.         }
  224.  
  225.         verifyAnyOrder(EXPECTED);
  226.     }
  227.  
  228.     @Test
  229.     public void testToStringCar()
  230.     {
  231.         final String EXPECTED = "This car is a 2000 Lamborghini Diablo that with 700 hp"
  232.             + NEWLINE + "This car is a 1998 Dodge Ram with 175 hp"
  233.             + NEWLINE + "This car is a 1940 Buggati Veryon with 135 hp"
  234.             + NEWLINE + "This car is a 2014 Honda Civic with 143 hp"
  235.             + NEWLINE + "This car is a 2011 Honda Civic with 143 hp"
  236.             + NEWLINE + "This car is a 1999 Toyota Corolla with 140 hp"
  237.             + NEWLINE + "";
  238.  
  239.         for (Vehicle v : stable)
  240.         {
  241.             if (v instanceof Car)
  242.             {
  243.                 Car c = (Car) v;
  244.                 System.out.println(c);
  245.             }
  246.         }
  247.  
  248.         verify(EXPECTED);
  249.     }
  250.  
  251.     @Test
  252.     public void testToStringBoat()
  253.     {
  254.         final String EXPECTED = "This boat is a 1980 Bayliner Extreme (motorized)"
  255.             + NEWLINE + "This boat is a 2014 Bayliner Extreme II (motorized)"
  256.             + NEWLINE + "This boat is a 2000 American Skier Supreme (not motorized)"
  257.             + NEWLINE + "This boat is a 2010 Boesch Journey (not motorized)"
  258.             + NEWLINE + "";
  259.  
  260.         for (Vehicle v : stable)
  261.         {
  262.             if (v instanceof Boat)
  263.             {
  264.                 Boat b = (Boat) v;
  265.                 System.out.println(b);
  266.             }
  267.         }
  268.  
  269.         verify(EXPECTED);
  270.  
  271.     }
  272.  
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement