Système de réservation

  1. /*
  2.  * FlightSystemTest.java
  3.  * JUnit based test
  4.  *
  5.  * Created on 8 novembre 2004, 11:22
  6.  */
  7.  
  8. package test;
  9. import reservation.system.*;
  10. import reservation.system.functions.Functions;
  11. import reservation.system.functions.*;
  12.  
  13. import java.util.Vector;
  14. import java.util.ListIterator;
  15. import java.util.LinkedList;
  16. import java.util.Collection;
  17. import java.util.HashSet;
  18. import java.util.Set;
  19. import junit.framework.*;
  20.  
  21. /**
  22.  *
  23.  * @author mathieu
  24.  */
  25. public class FlightSystemTest extends TestCase {
  26.     static FlightSystem fs = FlightSystem.getInstance();
  27.    
  28.     public FlightSystemTest (java.lang.String testName) {
  29.         super (testName);
  30.     }
  31.    
  32.     public static Test suite () {
  33.         TestSuite suite = new TestSuite (FlightSystemTest.class);
  34.         return suite;
  35.     }
  36.    
  37.     static public Set ArrayToSet (String str []) {
  38.         Set set = new HashSet ();
  39.         for (int i = 0; i< str.length; i++)
  40.             set.add (str[i]);
  41.         return set;
  42.     }
  43.    
  44.     static public void functionsException (String errorString, Functions function, String[] arg) {
  45.         System.out.println ("\r\n Test :" +errorString);
  46.         try {
  47.             function.execute (arg);
  48.             fail ("Exception not catch :" + errorString);
  49.         } catch (Exception e) {System.out.println ("\r\nError received :"+ e.getMessage ());}
  50.     }
  51.  
  52.     static public void createException (String errorString, String name, int rows, int rowsLength) {
  53.         int listLenght =0;
  54.         System.out.println ("\r\n Test :" +errorString);
  55.         try {
  56.             String flightList[] = fs.getFlightList ();
  57.             listLenght = flightList.length;
  58.         } catch (Exception e) {fail (e.getMessage ());}
  59.         try {
  60.             fs.create (name,(short)rows,(short) rowsLength);
  61.             fail ("Exception not catch :" + errorString);
  62.         } catch (Exception e) {System.out.println ("\r\nError received :"+ e.getMessage ());}
  63.         try {
  64.             assertEquals (fs.getFlightList ().length, listLenght);
  65.             System.out.println ("OK ( state of flightSystem unchanged :"+fs.getFlightList ().length+" flights counted )");
  66.         } catch (Exception e) {fail (e.getMessage ());}
  67.     }
  68.    
  69.     /**
  70.      * Test of create method, of class FlightSystem.
  71.      */
  72.     static public void testCreate () {
  73.         System.out.println ("test Create()");
  74.         String errorString;
  75.          Functions function = new Create ();
  76.        
  77.         errorString = "commun case : \t";
  78.         /*-----------------*/System.out.print (errorString);/*-----------------*/
  79.         String test[] =  {"a","b","c","d"};
  80.        
  81.         try {
  82.             fs.create ("a",(short)8,(short)2);
  83.             fs.create ("b",(short)8,(short)2);
  84.             fs.create ("c",(short)8,(short)2);
  85.            fs.create ("d",(short)8,(short)2);
  86.         } catch (Exception e) {fail (e.getMessage ());}
  87.         System.out.println ("OK");
  88.         System.out.println (fs);
  89.        
  90.         errorString = "normal case for overloaded create : \t";
  91.         /*-----------------*/System.out.print (errorString);/*-----------------*/
  92.         try {
  93.             Flight ftest = null;
  94.             ftest = new Flight ("e", new Pos ( (short)1,(short)1) );
  95.             fs.create (ftest);
  96.             assertEquals (fs.selectFlight ("e"), ftest);
  97.         } catch (Exception e) {fail (e.getMessage ());}
  98.        
  99.         System.out.println ("OK");
  100.         System.out.println (fs);
  101.        
  102.         createException ("Duplicate element.", "e",2,1);
  103.         createException ("Rows : Null.", "f",0,1);
  104.         createException ("RowLength : Null.", "f",1,0);
  105.         createException ("Rows : negative value.", "f",-1,1);
  106.         createException ("RowLength : negative value.", "f",1,-1);
  107.         createException ("Rows : out of range.", "f",101,1);
  108.         createException ("RowLength : out of range.", "f",10,11);
  109.         createException ("Name : null.", null,1,1);
  110.         createException ("Name : empty.", "",1,1);
  111.         createException ("Name : unexpected character .", ";#dsq",1,1);
  112.        
  113.         errorString = "Rows null";
  114.         functionsException (errorString, function, new String[]{"1", "0", "1"} );
  115.         errorString = "RowsLength null";
  116.         functionsException (errorString, function, new String[]{"1", "5", "0"} );
  117.         errorString = "Flight null";
  118.         functionsException (errorString, function, new String[]{null, "5", "5"} );
  119.         errorString = "Rows null";
  120.         functionsException (errorString, function, new String[]{"1", null, "0"} );
  121.         errorString = "RowsLength null";
  122.         functionsException (errorString, function, new String[]{"1", "5", null} );
  123.         errorString = "Too much arguments";
  124.         functionsException (errorString, function, new String[]{"1", "5", "5","java"} );
  125.         errorString = "Too less arguments";
  126.         functionsException (errorString, function, new String[]{"1", "5"} );
  127.         errorString = "arguments null";
  128.         functionsException (errorString, function,null);
  129.         errorString = "array arguments contains one null element";
  130.         functionsException (errorString, function, new String[]{null} );
  131.     }
  132.    
  133.     static public void reserveException (String errorString, String flightName, Set name) {
  134.         int listLenght =0;
  135.         System.out.println ("Test :" +errorString);
  136.         try {
  137.             Integer flightList[] = fs.getBookingList ();
  138.             listLenght = flightList.length;
  139.         } catch (Exception e) {fail (e.getMessage ());}
  140.         try {
  141.             fs.reserve (flightName,name);
  142.             fail ("Exception not catch :" + errorString);
  143.         } catch (Exception e) {System.out.println ("\r\nError received :"+ e.getMessage ());}
  144.         try {
  145.             assertEquals (fs.getBookingList ().length, listLenght);
  146.             System.out.println ("OK ( state of flightSystem unchanged :"+fs.getBookingList ().length+" persons counted )");
  147.         } catch (Exception e) {fail (e.getMessage ());}
  148.     }
  149.     /**
  150.      * Test of reserve method, of class FlightSystem.
  151.      */
  152.     static public void testReserve () {
  153.         System.out.println ("test Reserve");
  154.         String errorString;
  155.         int[] bookingNumberTest = new int[4];
  156.         Functions function = new Reserve ();
  157.         try {
  158.             bookingNumberTest[0] = fs.reserve ("a", ArrayToSet (new String[]{"John", "Adam", "Eve", "Caroline"}));
  159.             bookingNumberTest[1] = fs.reserve ("b", ArrayToSet (new String[]{"Fred", "Lars", "Lys", "Mathieu"}));
  160.             fs.reserve ("a",ArrayToSet (new String[]{"John", "Adam", "Eve", "Caroline"}));
  161.         } catch (Exception e) {fail (e.getMessage ());}
  162.         System.out.println (fs);
  163.        
  164.         reserveException ("flight has a null value",null, ArrayToSet (new String []{"Fred", "Lars", "Lys", "Mathieu"}));
  165.         reserveException ("One person has a null value","b", ArrayToSet (new String []{"Georges", null, "Bill", "Franklin"}));
  166.         reserveException ("The set of person is empty","b", ArrayToSet (new String []{null}));
  167.         reserveException ("The set of person has a null value","b", null);
  168.         reserveException ("flight doesn't exist","doesNotExist", ArrayToSet (new String []{"Georges", "Frank", "Bill", "Franklin"}));
  169.         errorString = "Person names contain illegal character";
  170.         functionsException (errorString, function, new String[]{"a", "0", "1"} );
  171.         errorString = "Flight null";
  172.         functionsException (errorString, function, new String[]{null, "Adam", "Eve"} );
  173.         errorString = "One name null";
  174.         functionsException (errorString, function, new String[]{"a", null, "Eve"} );
  175.         errorString = "No person name";
  176.         functionsException (errorString, function, new String[]{"a",null} );
  177.         errorString = "The first letter on the name is not in upper case";
  178.         functionsException (errorString, function, new String[]{"a", "adam"} );
  179.         errorString = "arguments null";
  180.         functionsException (errorString, function,null);
  181.         errorString = "array arguments contains one null element";
  182.         functionsException (errorString, function, new String[]{null} );
  183.     }
  184.    
  185.     /**
  186.      * Test of cancel method, of class FlightSystem.
  187.      */
  188.     static public void testCancel () {
  189.         System.out.println ("testCancel");
  190.         String errorString;
  191.  
  192.         errorString = "the bookingNumber doesn't exist\t\t";
  193.         try {
  194.             fs.cancel (46, ArrayToSet (new String[]{"Adam","Caroline"}));
  195.             fail ("Exception not catch :" + errorString);
  196.         } catch (Exception e) {System.out.println (errorString + " OK :" + e.getMessage ());}
  197.        
  198.         errorString = "the name doesn't exist\t\t";
  199.         try {
  200.             fs.cancel (3, ArrayToSet (new String[]{"Jens","Adam"}));
  201.             fail ("Exception not catch :" + errorString);
  202.         } catch (Exception e) {System.out.println (errorString + " OK :" + e.getMessage ());}
  203.        
  204.         errorString = "the value of booking number is null\t\t";
  205.         try {
  206.             fs.cancel (0, ArrayToSet (new String[]{"Adam","Caroline"}));
  207.             fail ("Exception not catch :" + errorString);
  208.         } catch (Exception e) {System.out.println (errorString + " OK :" + e.getMessage ());}
  209.        
  210.         errorString = "a name is null\t\t";
  211.         try {
  212.             fs.cancel (2, ArrayToSet (new String[]{null,"Lars"}));
  213.             fail ("Exception not catch :" + errorString);
  214.         } catch (Exception e) {System.out.println (errorString + " OK :" + e.getMessage ());}
  215.        
  216.         cancelException ("Person name null", 1, ArrayToSet (new String []{null}));
  217.         cancelException ("Person name null and ArraytoSet Null)", 1, null);
  218.         cancelException ("Booking Number null", 0, ArrayToSet (new String []{"Lars"}));
  219.         cancelException ("Booking Number negative", -1, ArrayToSet (new String []{"Lars"}));
  220.         cancelException ("Name doesn't correspond to the booking number", 1, ArrayToSet (new String []{"Lys"}));
  221.         cancelException ("Name with unexpected characters", 2, ArrayToSet (new String []{"Lys054"}));
  222.         cancelException ("Name with unexpected characters", 2, ArrayToSet (new String []{"lys"}));
  223.         cancelException ("Name with unexpected characters", 2, ArrayToSet (new String []{"Lys","Lars",null}));
  224.  
  225.         Functions function = new Cancel ();    
  226.         errorString = "Booking number is not a number";
  227.         functionsException (errorString, function, new String[]{"a", "Adam", "Eve"} );
  228.         errorString = "Illegal character for one person name";
  229.         functionsException (errorString, function, new String[]{"1", "5", "Eve"} );
  230.         errorString = "booking number null";
  231.         functionsException (errorString, function, new String[]{null, "Adam", "Eve"} );
  232.         errorString = "One name null";
  233.         functionsException (errorString, function, new String[]{"1", null, "Eve"} );
  234.         errorString = "arguments null";
  235.         functionsException (errorString, function,null);
  236.         errorString = "array arguments contains one null element";
  237.         functionsException (errorString, function, new String[]{null} );
  238.     }
  239.    
  240.     static public void cancelException (String errorString, int bookingNumber, Set name) {
  241.         int listLenght =0;
  242.         System.out.println ("\r\n Test :" +errorString);
  243.         try {
  244.             Integer bookingList[] = fs.getBookingList ();
  245.             listLenght = fs.getBookingList ().length;
  246.         } catch (Exception e) {fail (e.getMessage ());}
  247.         try {
  248.             boolean res = fs.cancel (bookingNumber, name);
  249.             if (res)
  250.             fail ("Exception not catch :" + errorString);
  251.         } catch (Exception e) {System.out.println ("\r\nError received :"+ e.getMessage ());}
  252.         try {
  253.             int newListLenght = fs.getBookingList ().length;
  254.             assertEquals (newListLenght, listLenght);
  255.             System.out.println ("OK ( state of flightSystem unchanged :"+fs.getFlightList ().length+" flights counted )");
  256.         } catch (Exception e) {fail (e.getMessage ());}
  257.     }
  258.    
  259.     static public void cancelException (String errorString, int bookingNumber) {
  260.         int listLenght =0;
  261.         System.out.println ("\r\n Test :" +errorString);
  262.         try {
  263.             Integer bookingList[] = fs.getBookingList ();
  264.             listLenght = bookingList.length;
  265.         } catch (Exception e) {fail (e.getMessage ());}
  266.         try {
  267.             boolean res = fs.cancel (bookingNumber);
  268.             if (res)
  269.                 fail ("Exception not catch :" + errorString);
  270.         } catch (Exception e) {System.out.println ("\r\nError received :"+ e.getMessage ());}
  271.         try {
  272.             int newListLenght = fs.getBookingList ().length;
  273.             assertEquals (fs.getBookingList ().length, listLenght);
  274.             System.out.println ("OK ( state of flightSystem unchanged :"+fs.getFlightList ().length+" flights counted )");
  275.         } catch (Exception e) {fail (e.getMessage ());}
  276.     }
  277.    
  278.     /**
  279.      * Test of cancel method, of class FlightSystem.
  280.      */
  281.     static public void test1Cancel () {
  282.         System.out.println ("\n\r testCancel");
  283.         String errorString;
  284.         Functions function = new Cancel ();
  285.         errorString = "the bookingNumber doesn't exist\t\t";
  286.  
  287.         cancelException ("Booking Number null", 0);
  288.         cancelException ("Booking Number negative", -1);
  289.         cancelException ("Booking number doesn't exist", 56);
  290.        
  291.         errorString = "Booking number is not a number";
  292.         functionsException (errorString, function, new String[]{"a"} );
  293.         errorString = "booking number null";
  294.         functionsException (errorString, function, new String[]{null} );
  295.         errorString = "Booking number equal 0";
  296.         functionsException (errorString, function, new String[]{"0"} );
  297.         errorString = "arguments null";
  298.         functionsException (errorString, function,null);
  299.         errorString = "array arguments contains one null element";
  300.         functionsException (errorString, function, new String[]{null} );
  301.     }
  302.    
  303.     /**
  304.      * Test of identify method, of class FlightSystem.
  305.      */
  306.     public void testIdentify () {
  307.         System.out.println ("\n\r testIdentify");
  308.         String errorString;
  309.         Functions function = new Identify ();
  310.         errorString = "the booking number doesn't exist\t\t";
  311.         try {
  312.             assertTrue (fs.identify (19).isEmpty ());
  313.         } catch (Exception e) {System.out.println (errorString + " OK :" + e.getMessage ());}
  314.        
  315.         errorString = "a name is null\t\t";
  316.         try {
  317.             fs.identify (0);
  318.             fail ("Exception not catch :" + errorString);
  319.         } catch (Exception e) {System.out.println (errorString + " OK :" + e.getMessage ());}
  320.         errorString = "Booking number is not a number";
  321.         functionsException (errorString, function, new String[]{"a"} );
  322.         errorString = "booking number null";
  323.         functionsException (errorString, function, new String[]{null} );
  324.         errorString = "Booking number equal 0";
  325.         functionsException (errorString, function, new String[]{"0"} );
  326.         errorString = "Too much arguments";
  327.         functionsException (errorString, function, new String[]{"a", "5", "5","java"} );
  328.         errorString = "arguments null";
  329.         functionsException (errorString, function,null);
  330.     }
  331.    
  332.     /**
  333.      * Test of list method, of class FlightSystem.
  334.      */
  335.     public void testList () {
  336.         System.out.println (" \n\r testList");
  337.         String errorString;
  338.         Functions function = new Lists ();
  339.         errorString = "the flight name doesn't exist\t\t";
  340.         try {
  341.             fs.list ("g");
  342.             fail ("Exception not catch :" + errorString);
  343.         } catch (Exception e) {System.out.println (errorString + " OK :" + e.getMessage ());}
  344.        
  345.         errorString = "the flight name is null\t\t";
  346.         try {
  347.             fs.list (null );
  348.             fail ("Exception not catch :" + errorString);
  349.         } catch (Exception e) {System.out.println (errorString + " OK :" + e.getMessage ());}
  350.  
  351.         errorString = "Flight null";
  352.         functionsException (errorString, function, new String[]{null} );
  353.         errorString = "Illegal character";
  354.         functionsException (errorString, function, new String[]{"a*/"} );
  355.         errorString = "Too much arguments";
  356.         functionsException (errorString, function, new String[]{"a", "5", "5","java"} );
  357.         errorString = "arguments null";
  358.         functionsException (errorString, function,null);
  359.     }
  360.    
  361.    
  362.     /**
  363.      * Test of selectFlight method, of class FlightSystem.
  364.      */
  365.     public void testSelectFlight () {
  366.         System.out.println (" \n\r testSelectFlight");
  367.        
  368.         try {
  369.             Flight flight = new Flight ("h",new Pos ((short)2,(short)1));
  370.             fs.create (flight);
  371.             assertEquals (flight,fs.selectFlight ("h"));
  372.         } catch (Exception e) {fail (e.getMessage ());}
  373.     }
  374.    
  375.     /**
  376.      * Test of getFlightList method, of class FlightSystem.
  377.      */
  378.     public void testGetFlightList () {
  379.         System.out.println ("\n\r testGetFlightList");
  380.         String test[] =  {"a","b","c","d"};
  381.         try {
  382.             for (int i = 0; i<4; i++)
  383.                 assertEquals (fs.getFlightList ()[i], test[i]);
  384.         } catch (Exception e) {fail (e.getMessage ());}
  385.     }
  386.    
  387.     /**
  388.      * Test of getBookingList method, of class FlightSystem.
  389.      */
  390.     public void testGetBookingList () {
  391.         System.out.println ("\n\r testGetBookingList");
  392.         System.out.println (fs);
  393.         Set bookingNumberTest = new HashSet ();
  394.         bookingNumberTest.add (new Integer (1));
  395.         bookingNumberTest.add (new Integer (2));
  396.         bookingNumberTest.add (new Integer (3));
  397.         try {
  398.             for (int i = 0; i<3; i++) {
  399.                 assertTrue (bookingNumberTest.contains (fs.getBookingList ()[i]));
  400.             }
  401.         } catch (Exception e) {fail (e.getMessage ());}
  402.     }
  403. }
  404.  
  405.  
  406.  

contact - faire un lien