thecoderenroute

Bird Database

Jun 19th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.98 KB | None | 0 0
  1.  
  2. import fi.helsinki.cs.tmc.edutestutils.MockStdio;
  3. import fi.helsinki.cs.tmc.edutestutils.Points;
  4. import java.util.NoSuchElementException;
  5. import org.junit.*;
  6. import static org.junit.Assert.*;
  7.  
  8. @Points("07-08.1 07-08.2 07-08.3")
  9. public class BirdDatabaseTest {
  10.  
  11.     @Rule
  12.     public MockStdio io = new MockStdio();
  13.  
  14.     String test(String in) {
  15.  
  16.         String s;
  17.         try {
  18.             io.setSysIn(in);
  19.             mainProgram.main(new String[0]);
  20.             s = io.getSysOut();
  21.         } catch (NoSuchElementException e) {
  22.             fail("Error reading the input. Try:\n" + in);
  23.             return null;
  24.         } catch (Throwable t) {
  25.             fail("Something went wrong. Try:\n" + in + "\n\nMore information:\n" + t);
  26.             return null;
  27.         }
  28.         return s;
  29.  
  30.     }
  31.  
  32.     @Test
  33.     public void testQuit1() {
  34.         test("Quit\n");
  35.     }
  36.  
  37.     @Test
  38.     public void testEmptyAll() {
  39.         test("All\nQuit\n");
  40.     }
  41.  
  42.     @Test
  43.     public void testOneNotExisting() {
  44.         test("One\nPidgeon\nQuit\n");
  45.     }
  46.  
  47.     @Test
  48.     public void testObservationNotExisting() {
  49.         test("Observation\nBigBird\nQuit\n");
  50.     }
  51.  
  52.     @Test
  53.     public void testAddOne() {
  54.         String in = "Add\nXXX\nYYY\nOne\nXXX\nQuit\n";
  55.         String out = test(in);
  56.         String message = "Try:\n" + in;
  57.  
  58.         assertTrue("Program did not print birds name with the One-command. " + message,
  59.                 out.contains("XXX"));
  60.         assertTrue("Program did not print birds name in Latin with the One-command. " + message,
  61.                 out.contains("YYY"));
  62.         assertTrue("Program did not print number of observations with the One-command. " + message,
  63.                 out.contains("0"));
  64.     }
  65.  
  66.     @Test
  67.     public void testAddObservationObservationOne() {
  68.         String in = "Add\nXXX\nYYY\nObservation\nXXX\nObservation\nXXX\nOne\nXXX\nQuit\n";
  69.         String out = test(in);
  70.         String message = "Try:\n" + in;
  71.  
  72.         assertTrue("Program did not print birds name with the One-command. " + message,
  73.                 out.contains("XXX"));
  74.         assertTrue("Program did not print birds name in Latin with the One-command. " + message,
  75.                 out.contains("YYY"));
  76.         assertTrue("Program did not print number of observations with the One-command. " + message,
  77.                 out.contains("2"));
  78.  
  79.     }
  80.  
  81.     public void bird(String message, String out, String a, String b, int no) {
  82.         String[] lines = out.split("\n");
  83.         boolean ok = false;
  84.         for (String line : lines) {
  85.             if (line.contains(a) && line.contains(b) && line.contains("" + no)) {
  86.                 ok = true;
  87.             }
  88.         }
  89.         assertTrue("Program did not print birds " + a + " (" + b + ") number of observations correctly. " + message,
  90.                 ok);
  91.     }
  92.  
  93.     @Test
  94.     public void testWithTwoBirds() {
  95.         String in = "Add\nXXX\nYYY\nObservation\nXXX\n"
  96.                 + "Add\nWWW\nZZZ\n"
  97.                 + "Observation\nWWW\n"
  98.                 + "One\nXXX\n"
  99.                 + "Observation\nXXX\nObservation\nWWW\n"
  100.                 + "One\nWWW\n"
  101.                 + "Quit\n";
  102.         String out = test(in);
  103.         String message = "Try:\n" + in;
  104.  
  105.         bird(message, out, "XXX", "YYY", 1);
  106.         bird(message, out, "WWW", "ZZZ", 2);
  107.     }
  108.  
  109.     @Test
  110.     public void testWithTwoBirds2() {
  111.         String in = "Add\nXX\nYY\n"
  112.                 + "Add\nWW\nZZ\n"
  113.                 + "Observation\nWW\n"
  114.                 + "Observation\nXX\nObservation\nWW\n"
  115.                 + "Observation\nXX\nObservation\nWW\n"
  116.                 + "All\n"
  117.                 + "Quit\n";
  118.         String out = test(in);
  119.         String message = "Try:\n" + in;
  120.  
  121.         bird(message, out, "XX", "YY", 2);
  122.         bird(message, out, "WW", "ZZ", 3);
  123.     }
  124.  
  125.     @Test
  126.     public void testIncorrectCommands() {
  127.         test("Beer\nQuit\n");
  128.         test("Cake\nQuit\n");
  129.         test("Oservation\nQuit\n");
  130.     }
  131. }
Add Comment
Please, Sign In to add comment