Question: Create test cases to test the following Java Code: public class WebForm { public final int SIZE = 10; private String [] userInfo; private String
Create test cases to test the following Java Code:
public class WebForm
{
public final int SIZE = 10;
private String [] userInfo;
private String [] fields = { "User_id", "Password_1", "Password_2",
"Email", "Name", "Street address", "City",
"State", "Zip", "Telephone" };
/** constructor
* @param 10-element array containing user input
* instantiates array and sets all elements to empty String
*/
public WebForm( String [] startUserInfo )
{
setUserInfo( startUserInfo );
}
/** accessor
* @return copy of userInfo array
*/
public String [] getUserInfo( )
{
String [] temp = new String [userInfo.length];
for ( int i = 0; i < userInfo.length; i++ )
temp[i] = userInfo[i];
return temp;
}
/** mutator for userInfo
* @param newUserInfo array to replace userInfo
*/
public void setUserInfo( String [] newUserInfo )
{
userInfo = new String [newUserInfo.length];
for ( int i = 0; i < userInfo.length; i++ )
userInfo[i] = newUserInfo[i];
}
/** toString
* @return elements of userInfo separated by a space
*/
public String toString( )
{
String returnString = "User Info: ";
for ( int i = 0; i < userInfo.length; i++ )
returnString += fields[i] + ": " + userInfo[i] + " ";
return returnString;
}
/** missingInfo
* verifies that all fields have been given a value
*/
public boolean missingInfo( )
{
for ( int i = 0; i < userInfo.length; i++ )
{
if ( userInfo[i].equals( "" ) )
return false;
}
return true;
}
/** userIdLength
* @return number of characters in the userId
*/
public int userIdLength( )
{
// get index of UserId
int userId = UserInfoIndexes.USER_ID.ordinal( );
return userInfo[userId].length( );
}
/** validPassword
* @return true if password_1 and password_2 are equal; false, otherwise.
*/
public boolean validPassword( )
{
int password_1 = UserInfoIndexes.PASSWORD_1.ordinal( );
int password_2 = UserInfoIndexes.PASSWORD_2.ordinal( );
return userInfo[password_1].equals( userInfo[password_2] );
}
/** validEmail
* @return true if the email address contains one @ and one period following the @;
* false, otherwise
*/
public boolean validEmail( )
{
int emailIndex = UserInfoIndexes.EMAIL.ordinal( );
String email = userInfo[emailIndex];
int countAts = 0;
int atIndex = -1;
for ( int i = 0; i < email.length( ); i++ )
{
if ( email.charAt(i) == '@' )
{
countAts++;
atIndex = i;
}
}
if ( countAts != 1 )
return false;
else
{
int countDots = 0;
for ( int i = atIndex + 1; i < email.length( ); i++ )
{
if ( email.charAt(i) == '.' )
countDots++;
}
if ( countDots == 0 )
return false;
else
return true;
}
}
/** validState
* @return true if state has 2 characters
*/
public boolean validState( )
{
int state = UserInfoIndexes.STATE.ordinal( );
return ( userInfo[state].length( ) == 2 );
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
