📄 flightsystemtest.java
字号:
/* * 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 []) { Set set = new HashSet (); 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()"); String errorString; 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 ());} System.out.println ("OK"); System.out.println (fs); 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 ());} System.out.println ("OK"); System.out.println (fs); 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"); String errorString; 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 ());} System.out.println (fs); 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"); String errorString; 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 ());}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -