Question: This assignment implements a validation utility that utilizes regular expressions to validate email and phone strings. The following page has a brief description of regular
This assignment implements a validation utility that utilizes regular expressions to validate email and phone strings.
The following page has a brief description of regular expressions: Week09 - Regular Expressions and Parsing
Create the week09 package in your working area.
Download the following: TestEngine.java
Note: put into test package (replace any existing files) AbstractTestCase.java
Note: put into test package (replace any existing files) TestCaseValidatorUtility.java
TestHarness.java

Using the video, create the ValidationUtility.java
The class has three public static methods:
public static boolean isValidPhone(String phoneString)
public static boolean isValidEmail(String emailString)
public static String dumpConfiguration()
The email regular expression is:
"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?";
The phone regular expression is:
"[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]";
UML:
Turn in:
ValidationUtility.java
package test;
import java.util.ArrayList;
/**
* Test engine
* Executes registered tests and keeps track of individual
* results.
*/
public class TestEngine
{
private enum STATUS {PASSED, FAILED};
/**
* Default constructor
*/
public TestEngine()
{
m_tests = new ArrayList
}
/**
* Add a test case for the engine to run
*
* @param test An AbstractTestCase subclass instance
*/
public void addTest(AbstractTestCase test)
{
m_tests.add(test);
}
/**
* Iterates the list of tests and executes them
*/
public void runTests()
{
STATUS status = STATUS.PASSED; // overall test
harness status
STATUS testStatus = STATUS.PASSED;// individual test
status
for(AbstractTestCase test : m_tests)
{
// reset individual test status
testStatus = STATUS.PASSED;
trace(" -- starting test: " +
test.getName());
// This is using the polymorphic behavior of
OOP.
// Each test case implements the abstract
runTest method
// which is uniquely implemented by each
subclass test case
boolean result = test.run();
if( !result )
{
displayResult(" --", test.getName(),
STATUS.FAILED, test.results());
status = STATUS.FAILED; // overall test
harness status
testStatus = STATUS.FAILED; //
individual test status
}
//displayResult(" -- Completed",
test.getName(), testStatus, test.results());
}
displayResult("--", "Test Harness", status, "");
}
static private void displayResult(String pad, String what, STATUS
status, String results)
{
String msg = String.format("%s %s: **%s** - %s", pad,
what, status, results);
trace(msg);
}
static private void trace(String msg)
{
System.out.println(msg);
}
private ArrayList
}
package test;
import java.io.PrintStream;
/**
* Abstract base class for a test case
* This is the base class for test cases used in this class.
*
* Each TestCase must inherit from this class.
*
* @author scottl
*
*/
public abstract class AbstractTestCase
{
/**
* Default constructor
*/
protected AbstractTestCase(String name)
{
m_testName = name;
m_stream = System.out; // Standard out
m_results = new StringBuilder();
}
/**
* Subclasses must implement this abstract method
*/
public boolean run()
{
boolean result = runTest();
return result;
}
/**
* Subclass must implement this method to execute the test
*
* @return true if successful, otherwise false
*/
protected abstract boolean runTest();
/**
* Subclasses provide the results of their test.
*
* @return The formatted results from the subclass
*/
protected String results()
{
return m_results.toString();
}
/**
*
* @return TestCase Name
*/
public String getName()
{
return m_testName;
}
/**
* Allows user to assign a different output stream besides stdout
* @param stream
*/
public void setPrintStream(PrintStream stream)
{
m_stream = stream;
}
/**
* Helper method to record test status.
* Used by subclasses to assign results for subtests.
*
* @param msg Message to add
*/
protected void addResultMessage(String msg)
{
m_results.append(msg);
m_results.append(" ");
}
/**
* Trace the msg to a PrintStream
* Provides the method for tests to report status
* @param msg
*/
protected void trace(String msg)
{
m_stream.println(" " + msg);
}
protected String m_testName;
protected PrintStream m_stream;
private StringBuilder m_results;
}
ackage week09;
import test.AbstractTestCase;
public class TestCaseValidatorUtility extends AbstractTestCase
{
/**
* Default constructor
*/
public TestCaseValidatorUtility()
{
super("TestCaseValidatorUtility");
}
@Override
protected boolean runTest()
{
this.addResultMessage(ValidationUtility.dumpConfiguration());
boolean emailTest = runEmailTest();
boolean phoneTest = runPhoneTest();
return emailTest && phoneTest;
}
protected boolean runEmailTest()
{
trace("runEmailTest");
boolean result = true;
StringBuilder builder = new StringBuilder();
try
{
for(int index = 0; index
testEmailStrings.length; index++)
{
TestEmailData testData =
testEmailStrings[index];
boolean actual =
ValidationUtility.isValidEmail(testData.getEmail());
if(testData.getExpectedResult() !=
actual)
{
builder.append(
String.format(
"Expected email result %s, got %s for email %s",
testData.getExpectedResult(),actual,testData.getEmail()));
builder.append(" ");
result = false;
}
}
}
catch(Exception ex)
{
builder.append("Error
TestCaseValidatorUtility testing email " + ex.getMessage());
result = false;
}
finally
{
this.addResultMessage(builder.toString());
}
return result;
}
protected boolean runPhoneTest()
{
trace("runPhoneTest");
boolean result = true;
StringBuilder builder = new StringBuilder();
try
{
for(int index = 0; index
testPhoneStrings.length; index++)
{
TestPhoneData testData =
testPhoneStrings[index];
boolean actual =
ValidationUtility.isValidPhone(testData.getPhone());
if(testData.getExpectedResult() !=
actual)
{
builder.append(
String.format(
"Expected phone result %s, got %s for email %s",
testData.getExpectedResult(),actual,testData.getPhone()));
builder.append(" ");
result = false;
}
}
}
catch(Exception ex)
{
builder.append("Error
TestCaseValidatorUtility testing phone " + ex.getMessage());
result = false;
}
finally
{
this.addResultMessage(builder.toString());
}
return result;
}
private static TestEmailData[] testEmailStrings = {
new TestEmailData("Abc.@example.com",false) ,
new TestEmailData("Abc..123@example.com",
false),
new TestEmailData("jimbo@cfl.rr.com", true),
new TestEmailData("asdf23asdf@yahoo.com",
true),
new TestEmailData("qwerqwer%m@.com",
true),
new TestEmailData("asdf23asdf@yahoo.info",
true),
new TestEmailData("asdf@3asdf@yahoo.info",
false)
};
private static TestPhoneData[] testPhoneStrings = {
new TestPhoneData("1234567890",false) ,
new TestPhoneData("123-456-7890", true),
new TestPhoneData("(123)-456-7890", false),
new TestPhoneData("abc-456-7890", false),
new TestPhoneData("123456789012", false)
};
}
class TestEmailData
{
TestEmailData(String email, boolean expectedResult)
{
m_email = email;
m_expectedResult = expectedResult;
}
public String getEmail() { return m_email;}
public boolean getExpectedResult() {return m_expectedResult; }
private String m_email;
private boolean m_expectedResult;
}
class TestPhoneData
{
TestPhoneData(String phone, boolean expectedResult)
{
m_phone = phone;
m_expectedResult = expectedResult;
}
public String getPhone() { return m_phone;}
public boolean getExpectedResult() {return m_expectedResult; }
private String m_phone;
private boolean m_expectedResult;
}
package week09;
import test.TestEngine;
/**
* File: TestHarness.java
*/
class TestHarness
{
public static void main(String[] args)
{
trace("Starting test...");
trace(" -- setup test data");
TestEngine engine = new TestEngine();
engine.addTest(new TestCaseValidatorUtility());
engine.runTests();
trace("Completed test");
}
static private void trace(String msg)
{
System.out.println(msg);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
