package at.fhj.itm.beans;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import at.fhj.itm.beans.InputValidator;
import at.fhj.itm.beans.Registration;
import at.fhj.itm.business.ServiceAssembler;
import at.fhj.itm.business.ServiceException;
import at.fhj.itm.business.ServiceTrip;
import at.fhj.itm.business.ServiceUser;
import at.fhj.itm.model.Location;
import at.fhj.itm.model.User;
import at.fhj.itm.util.EmailUtil;
import at.fhj.itm.util.GoogleUtil;
import at.fhj.itm.util.JsfUtil;
import at.fhj.itm.util.RandomUtil;
/**
*
* @author Alfort Robert
* @version v1.0
* @lastModification 08-11-2010
* @purpose JUnit Test for Registration.
*
*/
public class RegistrationTest {
private ServiceUser mockServiceUser;
private JsfUtil mockJsfUtil;
private User mockUser;
private Location mockLocation;
@Before
public void setUp() {
this.mockLocation = new Location(8605, "Kapfenberg");
this.mockUser = new User("Hannes", "Lauser", "hlauser", "halauser", "lauser@aon.at", "+436641234567", this.mockLocation, null, "");
mockServiceUser = EasyMock.createMock(ServiceUser.class);
mockJsfUtil = EasyMock.createMock(JsfUtil.class);
ServiceAssembler serviceAssembler = new ServiceAssembler() {
@Override
public ServiceUser createServiceUser() {
return mockServiceUser;
}
@Override
public ServiceTrip createServiceTrip() {
throw new UnsupportedOperationException("createServiceTrip()");
}
@Override
public JsfUtil createJsfUtil() {
return mockJsfUtil;
}
@Override
public GoogleUtil createGoogleUtil() {
throw new UnsupportedOperationException();
}
@Override
public RandomUtil createRandomUtil()
{
throw new UnsupportedOperationException("createRandomUtil()");
}
@Override
public EmailUtil createEmailUtil()
{
throw new UnsupportedOperationException("createEmailUtil()");
}
};
ServiceAssembler.setServiceAssembler(serviceAssembler);
}
@After
public void tearDown() {
this.mockUser = null;
this.mockLocation = null;
this.mockServiceUser = null;
this.mockJsfUtil = null;
}
@Test
public void testPhone() {
Registration registration = new Registration();
registration.setPhone("+436641234567");
Assert.assertEquals(registration.getPhone(), "+436641234567");
}
@Test
public void testEmailValidator() {
InputValidator iv = new InputValidator();
assertTrue(iv.isEmailAddressValid("robert.alfort.itm08@fh-joanneum.at"));
}
@Test
public void testBadNames() {
Registration registration = new Registration();
String[] badNames = { "admin" };
registration.setBadNames(badNames);
Assert.assertSame(badNames, registration.getBadNames());
}
@Test
public void testFirstName() {
Registration registration = new Registration();
String fName = "Mathias";
registration.setfName(fName);
Assert.assertEquals(fName, registration.getfName());
}
@Test
public void testLastName() {
Registration registration = new Registration();
String lName = "Meier";
registration.setlName(lName);
Assert.assertEquals(lName, registration.getlName());
}
@Test
public void testEmail() {
Registration registration = new Registration();
String eMail = "test.testen@fh-joanneum.at";
registration.seteMail(eMail);
Assert.assertEquals(eMail, registration.geteMail());
}
@Test
public void testUser1Name() {
Registration registration = new Registration();
String userName = "meier2312";
registration.setUserName(userName);
Assert.assertEquals(userName, registration.getUserName());
}
@Test
public void testPassword() {
Registration registration = new Registration();
String password = "xxxxxx";
registration.setPassword(password);
Assert.assertEquals(password, registration.getPassword());
}
@Test
public void testPasswordConfirm() {
Registration registration = new Registration();
String password2 = "xxxxxx";
registration.setPassword2(password2);
Assert.assertEquals(password2, registration.getPassword2());
}
@Test
public void testCity() {
Registration registration = new Registration();
String city = "Graz";
registration.setCity(city);
Assert.assertEquals(city, registration.getCity());
}
@Test
public void testPhone1() {
Registration registration = new Registration();
String phone = "+436645689745";
registration.setPhone(phone);
Assert.assertEquals(phone, registration.getPhone());
}
@Test
public void testZIP() {
Registration registration = new Registration();
int zip = 8600;
registration.setZip(zip);
Assert.assertEquals(zip, registration.getZip());
}
@Test
public void testVerification() {
Registration registration = new Registration();
String eMailVerification = "test.testen@fh-joanneum.at";
registration.seteMailVerification(eMailVerification);
Assert.assertEquals(eMailVerification,
registration.geteMailVerification());
}
@Test
public void testUnsetting() {
Registration registration = new Registration();
registration.unsetAll();
// TODO
}
@Test
public void testServiceUser() {
Registration registration = new Registration();
assertEquals(mockServiceUser, registration.getServiceUser());
}
@Test
public void testJSFUtil() {
Registration registration = new Registration();
assertEquals(mockJsfUtil, registration.getJsfUtil());
}
/**
* Tests whether the registration was successful after clicking the button.
*/
@Test
public void testRegButtonClickedSuccess() {
EasyMock.expect(mockServiceUser.doRegistration(mockUser.getFirstName(), mockUser.getLastName(),
mockUser.getUsername(), mockUser.getPassword(), mockUser.getEmail(),
mockUser.getPhone(), (int) mockLocation.getZip(), mockLocation.getCity()))
.andReturn(true);
EasyMock.replay(mockServiceUser);
Registration registration = new Registration();
registration.setfName(mockUser.getFirstName());
registration.setlName(mockUser.getLastName());
registration.seteMail(mockUser.getEmail());
registration.setPassword(mockUser.getPassword());
registration.setPhone(mockUser.getPhone());
registration.setUserName(mockUser.getUsername());
registration.setZip((int) mockLocation.getZip());
registration.setCity(mockLocation.getCity());
String result = registration.regButtonClicked();
Assert.assertEquals("regSuccessfull", result);
EasyMock.verify(mockServiceUser);
}
/**
* Tests whether a ServiceException due to an database error was thrown
* after clicking the registration button.
*/
@Test
public void testRegButtonClickedServiceException() {
EasyMock.expect(mockServiceUser.doRegistration(mockUser.getFirstName(), mockUser.getLastName(),
mockUser.getUsername(), mockUser.getPassword(), mockUser.getEmail(),
mockUser.getPhone(), (int) mockLocation.getZip(), mockLocation.getCity()))
.andThrow(new ServiceException("DatabaseError"));
EasyMock.replay(mockServiceUser);
Registration registration = new Registration();
registration.setfName(mockUser.getFirstName());
registration.setlName(mockUser.getLastName());
registration.seteMail(mockUser.getEmail());
registration.setPassword(mockUser.getPassword());
registration.setPhone(mockUser.getPhone());
registration.setUserName(mockUser.getUsername());
registration.setZip((int) mockLocation.getZip());
registration.setCity(mockLocation.getCity());
String result = registration.regButtonClicked();
Assert.assertEquals("errorDatabase", result);
EasyMock.verify(mockServiceUser);
}
}