This is a Java Code, you are only creating unit test for Validate and ConvertToBinary methods.
Please create at least 2 test for each method and check to see if they are successful. I have
inserted some example unit test as well.
CONVERT SERVICE CLASS:
public class ConvertService {
private Boolean isValid;
private long intValue;
private String hexValue;
private String binaryValue;
public ConvertService(String binaryValue) {
this.binaryValue = binaryValue;
Validate(binaryValue);
if (this.isValid) {
ConvertToInt(binaryValue);
ConvertToHex(binaryValue);
}
}
public ConvertService(long intValue) {
this.intValue = intValue;
Validate(intValue);
if (this.isValid) {
ConvertToBinary(intValue);
ConvertToHex(this.binaryValue);
}
}
public boolean IsValid() {
return isValid;
}
public String BinaryValue() {
return binaryValue;
}
public long IntegerValue() {
return intValue;
}
public String HexadecimalValue() {
return hexValue;
}
/*
* Used when the ConvertService(String binaryValue) constructor is used.
* Validate that the input value is not blank, and less than 32 characters long.
* Validate that the input value is only 1s and 0s.
*/
private void Validate (String value) {
Boolean isValid = true;
if (!is1sAnd0sOnly(value)) {
isValid = false;
}
if (!isProperLength(value)) {
isValid = false;
}
this.isValid = isValid;
}
/*
* Used when the ConvertService(long intvalue) constructor is used.
* Validate that the input value positive and less than 4294967295L
* Note: 4294967295L = FFFFFFFF
*/
//we have to test this
private void Validate (long value) {
Boolean isValid = true;
if (value < 0 || value > 4294967295L)
isValid = false;
this.isValid = isValid;
}
private boolean is1sAnd0sOnly (String str) {
if (str.matches("^[01]+$")) {
return true;
}
return false;
}
private boolean isNullEmptyBlank(String str){
if (str == null || str.equals("") || str.trim().equals(""))
return true;
return false;
}
private boolean isProperLength (String str) {
if (isNullEmptyBlank(str) ||
str.length() <= 32 ) {
return true;
}
return false;
}
private void ConvertToInt(String value) {
long iValue = 0L;
for (int i=0; i<value.length(); i++) {
long multiplier = (long)Math.pow(2, value.length() - 1 - i);
if (value.charAt(i) == '1') {
iValue += multiplier;
}
}
this.intValue = iValue;
}
private void ConvertToHex(String value) {
String hexValue = "";
//Fill the left size of the value with 0's so the length is a multiple of four.
int leftover = value.length() % 4;
if (leftover != 0) {
for (int i=0; i< (4-leftover); i++) {
value = "0" + value;
}
}
for (int i=0; i < value.length() / 4; i++) {
//a nibble is 4 bits
String nibble = value.substring(i*4, (i*4)+4);
if (nibble.equals("0000")) {
hexValue = hexValue + "0";
} else if (nibble.equals("0001")) {
hexValue = hexValue + "1";
} else if (nibble.equals("0010")) {
hexValue = hexValue + "2";
} else if (nibble.equals("0011")) {
hexValue = hexValue + "3";
} else if (nibble.equals("0100")) {
hexValue = hexValue + "4";
} else if (nibble.equals("0101")) {
hexVa.
This is a Java Code- you are only creating unit test for Validate and.pdf
1. This is a Java Code, you are only creating unit test for Validate and ConvertToBinary methods.
Please create at least 2 test for each method and check to see if they are successful. I have
inserted some example unit test as well.
CONVERT SERVICE CLASS:
public class ConvertService {
private Boolean isValid;
private long intValue;
private String hexValue;
private String binaryValue;
public ConvertService(String binaryValue) {
this.binaryValue = binaryValue;
Validate(binaryValue);
if (this.isValid) {
ConvertToInt(binaryValue);
ConvertToHex(binaryValue);
}
}
public ConvertService(long intValue) {
this.intValue = intValue;
Validate(intValue);
if (this.isValid) {
ConvertToBinary(intValue);
ConvertToHex(this.binaryValue);
}
2. }
public boolean IsValid() {
return isValid;
}
public String BinaryValue() {
return binaryValue;
}
public long IntegerValue() {
return intValue;
}
public String HexadecimalValue() {
return hexValue;
}
/*
* Used when the ConvertService(String binaryValue) constructor is used.
* Validate that the input value is not blank, and less than 32 characters long.
* Validate that the input value is only 1s and 0s.
*/
private void Validate (String value) {
Boolean isValid = true;
if (!is1sAnd0sOnly(value)) {
isValid = false;
3. }
if (!isProperLength(value)) {
isValid = false;
}
this.isValid = isValid;
}
/*
* Used when the ConvertService(long intvalue) constructor is used.
* Validate that the input value positive and less than 4294967295L
* Note: 4294967295L = FFFFFFFF
*/
//we have to test this
private void Validate (long value) {
Boolean isValid = true;
if (value < 0 || value > 4294967295L)
isValid = false;
this.isValid = isValid;
}
private boolean is1sAnd0sOnly (String str) {
if (str.matches("^[01]+$")) {
return true;
}
return false;
7. this.hexValue = hexValue;
}
// we have to include this
void ConvertToBinary(long value) {
String bValue = "";
for (int i=32; i>0; i--) {
long multiplier = (long)Math.pow(2, i);
if (value > multiplier) {
bValue = bValue + "1";
value = value - multiplier;
} else {
bValue = bValue + "0";
}
}
this.binaryValue = bValue;
}
}
CONVERT TO SERVICE TEST Class:
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class ConvertServiceTest {
@Test
public void ConvertService_SuccessTest() {
8. String val = "1010";
ConvertService bs = new ConvertService(val);
assertEquals(true, bs.IsValid());
assertEquals(val, bs.BinaryValue());
assertEquals(10, bs.IntegerValue());
assertEquals("A", bs.HexadecimalValue());
}
@Test
public void ConvertService_Success_Short_0() {
String val = "0";
ConvertService bs = new ConvertService(val);
assertEquals(true, bs.IsValid());
assertEquals(val, bs.BinaryValue());
assertEquals(0, bs.IntegerValue());
assertEquals("0", bs.HexadecimalValue());
}
@Test
public void ConvertService_Success_32Bits_AllHexValues1() {
//Test all the values from 0 to 7
String val = "00000001001000110100010101100111";
ConvertService bs = new ConvertService(val);
assertEquals(val, bs.BinaryValue());
assertEquals(19088743, bs.IntegerValue());
9. assertEquals("01234567", bs.HexadecimalValue());
}
@Test
public void ConvertService_Success_32Bits_AllHexValues2() {
//Test all the values from 8 to 15
String val = "10001001101010111100110111101111";
ConvertService bs = new ConvertService(val);
assertEquals(val, bs.BinaryValue());
assertEquals(2309737967L, bs.IntegerValue());
assertEquals("89ABCDEF", bs.HexadecimalValue());
}
}
App Class:
public class App {
public static void main(String[] args) throws Exception {
System.out.println("Hello, World!");
ConvertService binaryService = new ConvertService("10000000");
System.out.println(binaryService.HexadecimalValue());
ConvertService binaryTest = new ConvertService("10000000");
System.out.println(binaryTest.IntegerValue());
}
}
Purpose: Use your unit testing and debugging skills to figure out if this code is working. Steps:
binary string and the binary string is used to convert the hexadecimal value. 1. Look at the new
10. constructor and follow the logic. Does it make sense? Verify the new Validate method is correct.
Verify the order of the logic is correct. First it will call ConvertToBinary, then it will call
ConvertToHex, 1s this correct? 3. Create some unit tests for this new constructor to evaluate if
the code is working or not. for validate long and convert to binary ConvertToBinary method.
Lab6-Florin-Ryan). If you work with someone else to complete this Lab, provide a note when
submitted. Only 1 person needs to submit. How will this be graded? lines in validate(long) and
ConvertToBinary(long). You do not need to provide test cases that cover the other methods.