|
/*
* FlightSystemTest.java
* JUnit based test
*
* Created on 8 novembre 2004, 11:22
*/
package test;
import reservation.system.*;
import reservation.system.functions.Functions;
import reservation.system.functions.*;
import java.util.Vector;
import java.util.ListIterator;
import java.util.LinkedList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import junit.framework.*;
/**
*
* @author mathieu
*/
public class FlightSystemTest extends TestCase {
static FlightSystem fs = FlightSystem.getInstance();
public FlightSystemTest (java. lang. String testName ) {
super (testName);
}
public static Test suite () {
TestSuite suite = new TestSuite (FlightSystemTest.class);
return suite;
}
static public Set ArrayToSet (String str []) {
for (int i = 0; i< str.length; i++)
set.add (str[i]);
return set;
}
static public void functionsException (String errorString, Functions function, String[] arg ) {
System. out. println ("\r\n Test :" +errorString );
try {
function.execute (arg);
fail ("Exception not catch :" + errorString);
} catch (Exception e ) {System. out. println ("\r\nError received :"+ e. getMessage ());}
}
static public void createException (String errorString, String name, int rows, int rowsLength ) {
int listLenght =0;
System. out. println ("\r\n Test :" +errorString );
try {
String flightList [] = fs. getFlightList ();
listLenght = flightList.length;
} catch (Exception e ) {fail (e. getMessage ());}
try {
fs.create (name,(short)rows,(short) rowsLength);
fail ("Exception not catch :" + errorString);
} catch (Exception e ) {System. out. println ("\r\nError received :"+ e. getMessage ());}
try {
assertEquals (fs.getFlightList ().length, listLenght);
System. out. println ("OK ( state of flightSystem unchanged :"+fs. getFlightList (). length+" flights counted )");
} catch (Exception e ) {fail (e. getMessage ());}
}
/**
* Test of create method, of class FlightSystem.
*/
static public void testCreate () {
System. out. println ("test Create()");
Functions function = new Create ();
errorString = "commun case : \t";
/*-----------------*/System. out. print (errorString );/*-----------------*/
String test [] = {"a", "b", "c", "d"};
try {
fs.create ("a",(short)8,(short)2);
fs.create ("b",(short)8,(short)2);
fs.create ("c",(short)8,(short)2);
fs.create ("d",(short)8,(short)2);
} catch (Exception e ) {fail (e. getMessage ());}
errorString = "normal case for overloaded create : \t";
/*-----------------*/System. out. print (errorString );/*-----------------*/
try {
Flight ftest = null;
ftest = new Flight ("e", new Pos ( (short)1,(short)1) );
fs.create (ftest);
assertEquals (fs.selectFlight ("e"), ftest);
} catch (Exception e ) {fail (e. getMessage ());}
createException ("Duplicate element.", "e",2,1);
createException ("Rows : Null.", "f",0,1);
createException ("RowLength : Null.", "f",1,0);
createException ("Rows : negative value.", "f",-1,1);
createException ("RowLength : negative value.", "f",1,-1);
createException ("Rows : out of range.", "f",101,1);
createException ("RowLength : out of range.", "f",10,11);
createException ("Name : null.", null,1,1);
createException ("Name : empty.", "",1,1);
createException ("Name : unexpected character .", ";#dsq",1,1);
errorString = "Rows null";
functionsException (errorString, function, new String[]{"1", "0", "1"} );
errorString = "RowsLength null";
functionsException (errorString, function, new String[]{"1", "5", "0"} );
errorString = "Flight null";
functionsException (errorString, function, new String[]{null, "5", "5"} );
errorString = "Rows null";
functionsException (errorString, function, new String[]{"1", null, "0"} );
errorString = "RowsLength null";
functionsException (errorString, function, new String[]{"1", "5", null} );
errorString = "Too much arguments";
functionsException (errorString, function, new String[]{"1", "5", "5", "java"} );
errorString = "Too less arguments";
functionsException (errorString, function, new String[]{"1", "5"} );
errorString = "arguments null";
functionsException (errorString, function,null);
errorString = "array arguments contains one null element";
functionsException (errorString, function, new String[]{null} );
}
static public void reserveException (String errorString, String flightName, Set name ) {
int listLenght =0;
System. out. println ("Test :" +errorString );
try {
Integer flightList [] = fs. getBookingList ();
listLenght = flightList.length;
} catch (Exception e ) {fail (e. getMessage ());}
try {
fs.reserve (flightName,name);
fail ("Exception not catch :" + errorString);
} catch (Exception e ) {System. out. println ("\r\nError received :"+ e. getMessage ());}
try {
assertEquals (fs.getBookingList ().length, listLenght);
System. out. println ("OK ( state of flightSystem unchanged :"+fs. getBookingList (). length+" persons counted )");
} catch (Exception e ) {fail (e. getMessage ());}
}
/**
* Test of reserve method, of class FlightSystem.
*/
static public void testReserve () {
System. out. println ("test Reserve");
int[] bookingNumberTest = new int[4];
Functions function = new Reserve ();
try {
bookingNumberTest [0] = fs. reserve ("a", ArrayToSet (new String[]{"John", "Adam", "Eve", "Caroline"}));
bookingNumberTest [1] = fs. reserve ("b", ArrayToSet (new String[]{"Fred", "Lars", "Lys", "Mathieu"}));
fs. reserve ("a",ArrayToSet (new String[]{"John", "Adam", "Eve", "Caroline"}));
} catch (Exception e ) {fail (e. getMessage ());}
reserveException ("flight has a null value", null, ArrayToSet (new String []{"Fred", "Lars", "Lys", "Mathieu"}));
reserveException ("One person has a null value", "b", ArrayToSet (new String []{"Georges", null, "Bill", "Franklin"}));
reserveException ("The set of person is empty", "b", ArrayToSet (new String []{null}));
reserveException ("The set of person has a null value","b", null);
reserveException ("flight doesn't exist", "doesNotExist", ArrayToSet (new String []{"Georges", "Frank", "Bill", "Franklin"}));
errorString = "Person names contain illegal character";
functionsException (errorString, function, new String[]{"a", "0", "1"} );
errorString = "Flight null";
functionsException (errorString, function, new String[]{null, "Adam", "Eve"} );
errorString = "One name null";
functionsException (errorString, function, new String[]{"a", null, "Eve"} );
errorString = "No person name";
functionsException (errorString, function, new String[]{"a", null} );
errorString = "The first letter on the name is not in upper case";
functionsException (errorString, function, new String[]{"a", "adam"} );
errorString = "arguments null";
functionsException (errorString, function,null);
errorString = "array arguments contains one null element";
functionsException (errorString, function, new String[]{null} );
}
/**
* Test of cancel method, of class FlightSystem.
*/
static public void testCancel () {
System. out. println ("testCancel");
errorString = "the bookingNumber doesn't exist\t\t";
try {
fs. cancel (46, ArrayToSet (new String[]{"Adam", "Caroline"}));
fail ("Exception not catch :" + errorString);
} catch (Exception e ) {System. out. println (errorString + " OK :" + e. getMessage ());}
errorString = "the name doesn't exist\t\t";
try {
fs. cancel (3, ArrayToSet (new String[]{"Jens", "Adam"}));
fail ("Exception not catch :" + errorString);
} catch (Exception e ) {System. out. println (errorString + " OK :" + e. getMessage ());}
errorString = "the value of booking number is null\t\t";
try {
fs. cancel (0, ArrayToSet (new String[]{"Adam", "Caroline"}));
fail ("Exception not catch :" + errorString);
} catch (Exception e ) {System. out. println (errorString + " OK :" + e. getMessage ());}
errorString = "a name is null\t\t";
try {
fs. cancel (2, ArrayToSet (new String[]{null, "Lars"}));
fail ("Exception not catch :" + errorString);
} catch (Exception e ) {System. out. println (errorString + " OK :" + e. getMessage ());}
cancelException ("Person name null", 1, ArrayToSet (new String []{null}));
cancelException ("Person name null and ArraytoSet Null)", 1, null);
cancelException ("Booking Number null", 0, ArrayToSet (new String []{"Lars"}));
cancelException ("Booking Number negative", -1, ArrayToSet (new String []{"Lars"}));
cancelException ("Name doesn't correspond to the booking number", 1, ArrayToSet (new String []{"Lys"}));
cancelException ("Name with unexpected characters", 2, ArrayToSet (new String []{"Lys054"}));
cancelException ("Name with unexpected characters", 2, ArrayToSet (new String []{"lys"}));
cancelException ("Name with unexpected characters", 2, ArrayToSet (new String []{"Lys", "Lars", null}));
Functions function = new Cancel ();
errorString = "Booking number is not a number";
functionsException (errorString, function, new String[]{"a", "Adam", "Eve"} );
errorString = "Illegal character for one person name";
functionsException (errorString, function, new String[]{"1", "5", "Eve"} );
errorString = "booking number null";
functionsException (errorString, function, new String[]{null, "Adam", "Eve"} );
errorString = "One name null";
functionsException (errorString, function, new String[]{"1", null, "Eve"} );
errorString = "arguments null";
functionsException (errorString, function,null);
errorString = "array arguments contains one null element";
functionsException (errorString, function, new String[]{null} );
}
static public void cancelException (String errorString, int bookingNumber, Set name ) {
int listLenght =0;
System. out. println ("\r\n Test :" +errorString );
try {
Integer bookingList [] = fs. getBookingList ();
listLenght = fs.getBookingList ().length;
} catch (Exception e ) {fail (e. getMessage ());}
try {
boolean res = fs.cancel (bookingNumber, name);
if (res)
fail ("Exception not catch :" + errorString);
} catch (Exception e ) {System. out. println ("\r\nError received :"+ e. getMessage ());}
try {
int newListLenght = fs.getBookingList ().length;
assertEquals (newListLenght, listLenght);
System. out. println ("OK ( state of flightSystem unchanged :"+fs. getFlightList (). length+" flights counted )");
} catch (Exception e ) {fail (e. getMessage ());}
}
static public void cancelException (String errorString, int bookingNumber ) {
int listLenght =0;
System. out. println ("\r\n Test :" +errorString );
try {
Integer bookingList [] = fs. getBookingList ();
listLenght = bookingList.length;
} catch (Exception e ) {fail (e. getMessage ());}
try {
boolean res = fs.cancel (bookingNumber);
if (res)
fail ("Exception not catch :" + errorString);
} catch (Exception e ) {System. out. println ("\r\nError received :"+ e. getMessage ());}
try {
int newListLenght = fs.getBookingList ().length;
assertEquals (fs.getBookingList ().length, listLenght);
System. out. println ("OK ( state of flightSystem unchanged :"+fs. getFlightList (). length+" flights counted )");
} catch (Exception e ) {fail (e. getMessage ());}
}
/**
* Test of cancel method, of class FlightSystem.
*/
static public void test1Cancel () {
System. out. println ("\n\r testCancel");
Functions function = new Cancel ();
errorString = "the bookingNumber doesn't exist\t\t";
cancelException ("Booking Number null", 0);
cancelException ("Booking Number negative", -1);
cancelException ("Booking number doesn't exist", 56);
errorString = "Booking number is not a number";
functionsException (errorString, function, new String[]{"a"} );
errorString = "booking number null";
functionsException (errorString, function, new String[]{null} );
errorString = "Booking number equal 0";
functionsException (errorString, function, new String[]{"0"} );
errorString = "arguments null";
functionsException (errorString, function,null);
errorString = "array arguments contains one null element";
functionsException (errorString, function, new String[]{null} );
}
/**
* Test of identify method, of class FlightSystem.
*/
public void testIdentify () {
System. out. println ("\n\r testIdentify");
Functions function = new Identify ();
errorString = "the booking number doesn't exist\t\t";
try {
assertTrue (fs.identify (19).isEmpty ());
} catch (Exception e ) {System. out. println (errorString + " OK :" + e. getMessage ());}
errorString = "a name is null\t\t";
try {
fs.identify (0);
fail ("Exception not catch :" + errorString);
} catch (Exception e ) {System. out. println (errorString + " OK :" + e. getMessage ());}
errorString = "Booking number is not a number";
functionsException (errorString, function, new String[]{"a"} );
errorString = "booking number null";
functionsException (errorString, function, new String[]{null} );
errorString = "Booking number equal 0";
functionsException (errorString, function, new String[]{"0"} );
errorString = "Too much arguments";
functionsException (errorString, function, new String[]{"a", "5", "5", "java"} );
errorString = "arguments null";
functionsException (errorString, function,null);
}
/**
* Test of list method, of class FlightSystem.
*/
public void testList () {
System. out. println (" \n\r testList");
Functions function = new Lists ();
errorString = "the flight name doesn't exist\t\t";
try {
fs.list ("g");
fail ("Exception not catch :" + errorString);
} catch (Exception e ) {System. out. println (errorString + " OK :" + e. getMessage ());}
errorString = "the flight name is null\t\t";
try {
fs.list (null );
fail ("Exception not catch :" + errorString);
} catch (Exception e ) {System. out. println (errorString + " OK :" + e. getMessage ());}
errorString = "Flight null";
functionsException (errorString, function, new String[]{null} );
errorString = "Illegal character";
functionsException (errorString, function, new String[]{"a*/"} );
errorString = "Too much arguments";
functionsException (errorString, function, new String[]{"a", "5", "5", "java"} );
errorString = "arguments null";
functionsException (errorString, function,null);
}
/**
* Test of selectFlight method, of class FlightSystem.
*/
public void testSelectFlight () {
System. out. println (" \n\r testSelectFlight");
try {
Flight flight = new Flight ("h",new Pos ((short)2,(short)1));
fs.create (flight);
assertEquals (flight,fs.selectFlight ("h"));
} catch (Exception e ) {fail (e. getMessage ());}
}
/**
* Test of getFlightList method, of class FlightSystem.
*/
public void testGetFlightList () {
System. out. println ("\n\r testGetFlightList");
String test [] = {"a", "b", "c", "d"};
try {
for (int i = 0; i<4; i++)
assertEquals (fs.getFlightList ()[i], test[i]);
} catch (Exception e ) {fail (e. getMessage ());}
}
/**
* Test of getBookingList method, of class FlightSystem.
*/
public void testGetBookingList () {
System. out. println ("\n\r testGetBookingList");
bookingNumberTest. add (new Integer (1));
bookingNumberTest. add (new Integer (2));
bookingNumberTest. add (new Integer (3));
try {
for (int i = 0; i<3; i++) {
assertTrue (bookingNumberTest.contains (fs.getBookingList ()[i]));
}
} catch (Exception e ) {fail (e. getMessage ());}
}
}
|
|
|