public void testStoreEmployee() { try { //week 3 //IEmployeeSvc ics = factory.getEmployeeSvc(); //week 4 IEmployeeSvc ics = (IEmployeeSvc)factory.getService("IEmployeeSvc"); // First let's store the Employee Assert.True(ics.storeEmployee(e)); // Then let's read it back in e = ics.getEmployee(e.id); Assert.True(e.validate()); // Update Employee e.lastName = "Smith"; Assert.True(ics.storeEmployee(e)); // Finally, let's cleanup the file that was created Assert.True(ics.deleteEmployee(e.id)); } catch(Exception e) { Console.WriteLine("Exception in testStoreEmployee: " + e.Message + "\n" + e.StackTrace); Assert.Fail(e.Message + "\n" + e.StackTrace); } }
public void testInvalidEmployee() { try { Employee e = new Employee(); Assert.False(e.validate()); } catch(Exception e) { Console.WriteLine("Exception in testInvalidEmployee: " + e.Message + "\n" + e.StackTrace); Assert.Fail(e.Message + "\n" + e.StackTrace); } }
public void testValidateEmployee() { try { Employee e = new Employee(); e.id = 1; e.firstName = "Jim"; e.lastName = "Bloom"; Assert.True(e.validate()); } catch(Exception e) { Console.WriteLine("Exception in testValidateEmployee: " + e.Message + "\n" + e.StackTrace); Assert.Fail(e.Message + "\n" + e.StackTrace); } }
/// <summary> /// This method stores a employee. /// </summary> /// <param name="e">The employee object to store</param> /// <returns>Success/Failure</returns> public Boolean storeEmployee(Employee e) { dLog.Info("Entering method storeEmployee | ID: " + e.id); Stream output = null; Boolean result = false; try { //ensure we were passed a valid object before attempting to write if (e.validate()) { output = File.Open("Employee_" + e.id + ".txt", FileMode.Create); BinaryFormatter bFormatter = new BinaryFormatter(); bFormatter.Serialize(output, e); result = true; } } catch (IOException e1) { dLog.Error("IOException in storeEmployee: " + e1.Message); result = false; } catch (Exception e2) { dLog.Error("Exception in storeEmployee: " + e2.Message); result = false; } finally { //ensure that output is close regardless of the errors in try/catch if (output != null) { output.Close(); } } return result; }