Question
software testing without making any changes to the java classes provided below, come up with some junit test cases to test the code. To get
software testing
without making any changes to the java classes provided below, come up with some junit test cases to test the code.
To get you started, here are four test cases you must implement:
Use the setString() function in MyCustomStringInterface to set the value to H3y, l3t'5 put s0me d161ts in this 5tr1n6!11!!. Then test to see if the CountNumbers() function is equal to the number of sequential digits in the original string (in this case, 9).
Use the setString() function in MyCustomStringInterface to set the value to Peter Piper picked a peck of pickled peppers.. Then test to see if reverseNCharacters() function returns the reversed string when the characters are reversed in groups of 4 and padding is disabled (in this case, etePiP r repkcipa decep fo kcip delkpep srep.).
MyCustomString
public class MyCustomString implements MyCustomStringInterface { private String string;
@Override public String getString() { return string; }
@Override public void setString(String string) { this.string = string; }
@Override public int countNumbers() { StringBuffer tmpString = new StringBuffer(); int count=0; boolean inNumber=false; //avoid null pointer exception! if(string==null || string.isEmpty()) return 0; for (int i = 0; i < string.length(); i++) { char ch = string.charAt(i); if (Character.isDigit(ch)) { if (!inNumber) { count++; inNumber = true; } } else { if (inNumber) { inNumber = false; } } } return count; }
@Override public String reverseNCharacters(int n, boolean padded) { if (string == null) { throw new NullPointerException(); } if (n <= 0) { throw new IllegalArgumentException(); } StringBuffer tmpString = new StringBuffer(); StringBuffer resultString = new StringBuffer(); StringBuffer currString = new StringBuffer(string);
int i; for (i = 0; i + n < currString.length(); i+=n) { tmpString = tmpString.append(new StringBuffer(currString.substring(i,i+n)).reverse()); } if (padded) { for (int x = n - (currString.length() - i); x > 0; x--) currString.append('X'); } tmpString = tmpString.append(new StringBuffer(currString.substring(i)).reverse());
return tmpString.toString(); }
@Override public void convertDigitsToNamesInSubstring(int startPosition, int endPosition) { if (string == null) { throw new NullPointerException(); } if ((startPosition > endPosition)) { throw new IllegalArgumentException(); }
if (endPosition > string.length() || (startPosition < 1)){ throw new MyIndexOutOfBoundsException(); } StringBuffer tmpString = new StringBuffer(); String tmpDigit = new String(); boolean look_back = false; for (int i = 0; i < string.length(); i++) { char ch = string.charAt(i); if ((i < startPosition - 1) || (i > endPosition - 1)) { if(look_back) { look_back = false; } tmpString.append(ch); continue; } else { if (Character.isDigit(ch)) { switch (ch) { case '0': tmpDigit = "Zero"; break; case '1': tmpDigit = "One"; break; case '2': tmpDigit = "Two"; break; case '3': tmpDigit = "Three"; break; case '4': tmpDigit = "Four"; break; case '5': tmpDigit = "Five"; break; case '6': tmpDigit = "Six"; break; case '7': tmpDigit = "Seven"; break; case '8': tmpDigit = "Eight"; break; case '9': tmpDigit = "Nine"; break; } if(look_back) tmpString.append(tmpDigit.toLowerCase()); else tmpString.append(tmpDigit); look_back = true; } else { if(look_back){ look_back = false;} tmpString.append(ch);
} }
MyCustomStringInterface
} string = tmpString.toString(); } }
public interface MyCustomStringInterface {
String getString();
void setString(String string);
int countNumbers();
String reverseNCharacters(int n, boolean padded);
void convertDigitsToNamesInSubstring(int startPosition, int endPosition); } MyIndexOutOfBoundsException
public class MyIndexOutOfBoundsException extends RuntimeException { private static final long serialVersionUID = 8226094121089030034L;
public MyIndexOutOfBoundsException(String message) { super(message); }
public MyIndexOutOfBoundsException() { super(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started