Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static org.junit.Assert.*;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- import java.util.ArrayList;
- import java.io.ByteArrayOutputStream;
- import java.io.PrintStream;
- import java.util.Arrays;
- /**
- * The test class Assignment2Test.
- */
- public class Assignment2Test
- {
- public static final int EXPECTED_NUMBER_OF_EQUAL_CARS = 9;
- public static final int EXPECTED_NUMBER_OF_EQUAL_BOATS = 4;
- public static final int EXPECTED_NUMBER_OF_EQUAL_AIRPLANES = 0;
- private static final String NEWLINE = System.getProperty("line.separator");//"\r\n";
- private static final String TESTCODE_SUFFIX = "\";" + NEWLINE;
- private static final String MINOR_ERROR_MESSAGE = "-1 point.";
- private static final boolean PRODUCE_JUNIT_CODE = false;
- private static final String TESTCODE_PREFIX = "final String EXPECTED = \"";
- 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.";
- private static PrintStream stdOut = System.out;
- private static final String TESTCODE_NEWLINE = "\"" + NEWLINE + "\t+ NEWLINE + \"";
- private ByteArrayOutputStream outContent;
- private ArrayList<Vehicle> stable;
- /**
- * Default constructor for test class Assignment2Test
- */
- public Assignment2Test()
- {
- stable = new ArrayList<Vehicle>();
- }
- /**
- * Sets up the test fixture.
- *
- * Called before every test case method.
- */
- @Before
- public void setUp()
- {
- outContent = new ByteArrayOutputStream();
- stable.add(new Car(2000, "Lamborghini", "Diablo", 700));
- stable.add(new Car(1997, "Dodge", "Ram", 175));
- stable.add(new Car(1940, "Buggati", "Vehron", 135));
- stable.add(new Car(2014, "Honda", "Civic", 143));
- stable.add(new Car(2011, "Honda", "Civic", 143));
- stable.add(new Car(1999, "Toyota", "Corrola", 140));
- stable.add(new Boat(1980, "Bayliner", "Extreme", true));
- stable.add(new Boat(2014, "Bayliner", "Extreme II", true));
- stable.add(new Boat(2000, "American Skier", "Skier Supreme", false));
- stable.add(new Boat(2010, "Boesch", "Journey", false));
- stable.add(new Airplane(1998, "ABC Motors", "Comac", 10000));
- stable.add(new Airplane(1940, "Boeing", "84", 45000));
- stable.add(new Airplane(2012, "Boeing", "737", 80000));
- stable.add(new Airplane(2014, "Abrams", "Motorhead", 70000));
- }
- /**
- * Tears down the test fixture.
- *
- * Called after every test case method.
- */
- @After
- public void tearDown()
- {
- stable.clear();
- }
- private void verify(String expected)
- {
- String actual = outContent.toString(); // for debug purposes
- stdOut.println(PRODUCE_JUNIT_CODE
- ? (TESTCODE_PREFIX
- + actual.replaceAll(NEWLINE, TESTCODE_NEWLINE)
- + TESTCODE_SUFFIX
- ) : actual
- );
- assertEquals(MINOR_ERROR_MESSAGE, expected, actual);
- } // end method
- private void verifyAnyOrder(String expected)
- {
- String actual = outContent.toString(); // for debug purposes
- stdOut.println(actual);
- String[] actualStrings = actual.split(NEWLINE);
- String[] expectedStrings = expected.split(NEWLINE);
- int i = actualStrings.length;
- assertEquals(MAJOR_ERROR_MESSAGE, expectedStrings.length, i);
- Arrays.sort(actualStrings);
- Arrays.sort(expectedStrings);
- while (0 != i--)
- assertEquals(MINOR_ERROR_MESSAGE, expectedStrings[i], actualStrings[i]);
- } // end method
- @Test
- public void testEqualsCar()
- {
- int numberOfEqualCars = 0;
- for (int i = 0; i < stable.size(); i++)
- {
- Vehicle c = stable.get(i);
- if (c instanceof Car)
- {
- Car car1 = (Car) c;
- for (int j = 0; j < stable.size(); j++)
- {
- if (j != i)
- {
- Vehicle b = stable.get(j);
- if (b instanceof Car)
- {
- Car car2 = (Car) b;
- if(car1.equals(car2))
- {
- numberOfEqualCars++;
- }
- }
- }
- }
- }
- }
- assertEquals(EXPECTED_NUMBER_OF_EQUAL_CARS, numberOfEqualCars, 0.1);
- }
- @Test
- public void testEqualsBoats()
- {
- int numberOfEqualBoats = 0;
- for (int i = 0; i < stable.size(); i++)
- {
- Vehicle c = stable.get(i);
- if (c instanceof Boat)
- {
- Boat boat1 = (Boat) c;
- for (int j = 0; j < stable.size(); j++)
- {
- if (j != i)
- {
- Vehicle b = stable.get(j);
- if (b instanceof Boat)
- {
- Boat boat2 = (Boat) b;
- if(boat1.equals(boat2))
- {
- numberOfEqualBoats++;
- }
- }
- }
- }
- }
- }
- assertEquals(EXPECTED_NUMBER_OF_EQUAL_BOATS, numberOfEqualBoats, 0.1);
- }
- @Test
- public void testEqualsAirplane()
- {
- int numberOfEqualAirplanes = 0;
- for (int i = 0; i < stable.size(); i++)
- {
- Vehicle c = stable.get(i);
- if (c instanceof Airplane)
- {
- Airplane airplane1 = (Airplane) c;
- for (int j = 0; j < stable.size(); j++)
- {
- if (j != i)
- {
- Vehicle b = stable.get(j);
- if (b instanceof Airplane)
- {
- Airplane airplane2 = (Airplane) b;
- if(airplane1.equals(airplane2))
- {
- numberOfEqualAirplanes++;
- }
- }
- }
- }
- }
- }
- assertEquals(EXPECTED_NUMBER_OF_EQUAL_AIRPLANES, numberOfEqualAirplanes, 0.1);
- }
- @Test
- public void testToStringAirplane()
- {
- final String EXPECTED = "This airplane is a 1998 ABC Motors Comac that can reach 10000"
- + NEWLINE + "This airplane is a 1940 Boeing 84 that can reach 45000"
- + NEWLINE + "This airplane is a 2012 Boeing 737 that can reach 80000"
- + NEWLINE + "This airplane is a 2014 Abrams Motorhead that can reach 70000"
- + NEWLINE + "";
- for (Vehicle v : stable)
- {
- if (v instanceof Airplane)
- {
- Airplane a = (Airplane) v;
- System.out.println(a);
- }
- }
- verifyAnyOrder(EXPECTED);
- }
- @Test
- public void testToStringCar()
- {
- final String EXPECTED = "This car is a 2000 Lamborghini Diablo that with 700 hp"
- + NEWLINE + "This car is a 1998 Dodge Ram with 175 hp"
- + NEWLINE + "This car is a 1940 Buggati Veryon with 135 hp"
- + NEWLINE + "This car is a 2014 Honda Civic with 143 hp"
- + NEWLINE + "This car is a 2011 Honda Civic with 143 hp"
- + NEWLINE + "This car is a 1999 Toyota Corolla with 140 hp"
- + NEWLINE + "";
- for (Vehicle v : stable)
- {
- if (v instanceof Car)
- {
- Car c = (Car) v;
- System.out.println(c);
- }
- }
- verify(EXPECTED);
- }
- @Test
- public void testToStringBoat()
- {
- final String EXPECTED = "This boat is a 1980 Bayliner Extreme (motorized)"
- + NEWLINE + "This boat is a 2014 Bayliner Extreme II (motorized)"
- + NEWLINE + "This boat is a 2000 American Skier Supreme (not motorized)"
- + NEWLINE + "This boat is a 2010 Boesch Journey (not motorized)"
- + NEWLINE + "";
- for (Vehicle v : stable)
- {
- if (v instanceof Boat)
- {
- Boat b = (Boat) v;
- System.out.println(b);
- }
- }
- verify(EXPECTED);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement