For Part I you are to: 1) create a Control Flow Graph (CFG) to model a supplied
ID: 3887026 • Letter: F
Question
For Part I you are to:
1) create a Control Flow Graph (CFG) to model a supplied utility method named getParametersFromFile();
2) use that CFG to create a test set which achieves the most complete edge-pair coverage you can find for the getParametersFromFile() method; and
3) create a non-parameterized junit test class named CfgTests for the test set you created;
4) execute the test class; and
5) create a defect report (see below) for any failures detected.
General Instructions
Begin by examining the Javadoc in the template and correlating the classes and placeholders there with entities and deliverables mentioned in this document. Be sure you understand the intended role of each part.
While not absolutely necessary, we suggest you start with Part I, as writing the tests for getParametersFromFile() should assure you have a clear understanding of how it works, making it easier for you to incorporate it into PartIITests.
Do Not write your own file reading code for part II. You will lose points if you do not use getParametersFromFile() and only getParametersFromFile() to read your test set data.
You should submit your project as a zip. That zip must include the project file/package hierarchy and all artifacts requested in this document – each placed where directed.
Just to avoid counter-productive assumptions: there are no aspects of Lab1 required to complete this lab! This lab involves no understanding of RIPR and has no need of instrumentation in the software under test. This lab is about test set design using input-partitioning (part I), graph-based models(Part II), and Parameterized tests (Part II).
All test code must be in the test source folder.
There is no application for this lab – there are only classes containing methods that need testing. If you think you need to create a main() method to do this lab.
Warning: many programming editors automatically replace tab characters with the right number of spaces. Space delimited columns will not be recognized correctly by getParametersFromFile().
You should complete author tags in the files you write.
You should supply meaningful java doc for individual (non-parameterized) junit tests.
package edu.iastate.cs417.lab2.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Vector;
public class FileUtil {
/**
* Reads test set data from a tab-delimited text file.
*
* Ignores the first line. The first line is assumed to contain column headers to
* facilitate easy maintenance of the test set.
*
* Each line after the header is assumed to contain a complete set of values for
* one test case. Each line will be returned as an array of strings, regardless of
* whether some values are numeric or not.
*
* @param filename The file containing the data. Assumed to be relative to the working directory.
* In eclipse, this is normally the project directory (top-level)
*
* @param cols the number of columns in the data.
* @return The return object is an array of arrays of String. I.e.,
* if rval is a reference to the returned object, then rval[1] is the array
* containing the first data set.
*
* throws IllegalArgumentException if filename is not found, or is not readable.
* throws InvalidDataException if file is empty, or has only one line (i.e., the file should contain
* more than just a header.
* throws MalformedTestSet, reporting the offending line number in the text file,
* if any line does not have 'cols' data items.
*
*/
public static Object[][] getParametersFromFile(String filename, int cols) {
try {
File f = new File(filename);
BufferedReader br = new BufferedReader(new FileReader(f));
Vector<Object[]> lists = new Vector<Object[]>();
String line = null;
int index = 0;
while ((line = br.readLine()) != null) {
if (index != 0){
Object[] Object[cols];
String[] parts = new String[1];
if (cols > 1){
parts = line.split(" ");
}
else {
parts[0] = line.trim();
}
for (int col = 0; col < cols; col++){
oneTest[col] = parts[col];
}
lists.addElement(oneTest);
}
index ++;
}
br.close();
Object[][] testArray = new Object[lists.size()][cols];
for (int j = 0; j < lists.size(); j++) {
for (int k = 0; k < cols; k++) {
testArray[j][k] = lists.elementAt(j)[k];
}
}
return testArray;
} catch (Exception e){
return null;
}
}
}
Explanation / Answer
PartIITests.java
package edu.iastate.cs417.lab2.part2;
import edu.iastate.cs417.lab2.demo.Counter;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.assertEquals;
@RunWith(Parameterized.class)
public class PartIITests {
private static final String filename = "testdata.txt";
private static int testId;
private String input;
private Integer expectedResult;
private Counter counter;
@Before
public void initialize() {
counter = new Counter();
}
// Each parameter should be placed as an argument here
// Every time runner triggers, it will pass the arguments
// from parameters we defined in testData() method
public PartIITests(String input, Integer expectedResult) {
this.input = input;
this.expectedResult = expectedResult;
}
@Parameterized.Parameters
public static Collection getTestSet() {
return Arrays.asList(new Object[][] {
{ "ok", 1 },
{ "book", 2 },
{ "flat", 0 }
});
}
@Test
public void testCounter() {
int result = counter.countOs(input);
System.out.format("test %02d %-14s %2d%n", testId++, input, result);
assertEquals(expectedResult, counter.countOs(input));
}
}
CfgTests.java
package edu.iastate.cs417.lab2.part1;
import edu.iastate.cs417.lab2.util.FileUtil;
import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import static org.junit.Assert.*;
public class CfgTests {
@Test
/**
* This tests:
* "Each line after the header is assumed to contain a complete set of values for
* one test case. Each line will be returned as an array of strings, regardless of
* whether some values are numeric or not. "
*
* This also tests:
* "Ignores the first line. The first line is assumed to contain column headers to
* facilitate easy maintenance of the test set."
*/
public void testValidFileValidData() {
int cols = 3;
Object[][] params = FileUtil.getParametersFromFile("lab1_part1_validTestSet.txt", cols);
assertNotNull(params);
assertEquals(params.length, cols);
assertEquals(params[0].length, cols);
/*
Jeremy 9/18/17 402
Amy 10/11/17 515
Arnold 2/11/17 203
*/
Object[][] expectedParams = new Object[cols][cols];
expectedParams[0][0] = "Jeremy";
expectedParams[0][1] = "9/18/17";
expectedParams[0][2] = "402";
expectedParams[1][0] = "Amy";
expectedParams[1][1] = "10/11/17";
expectedParams[1][2] = "515";
expectedParams[2][0] = "Arnold";
expectedParams[2][1] = "2/11/17";
expectedParams[2][2] = "203";
for (int i = 0; i < cols; i++) {
assertArrayEquals(params[i], expectedParams[i]);
}
}
@Test
/**
* This tests:
* "throws IllegalArgumentException if filename is not found, or is not readable."
*/
public void testInvalidFileName() {
int cols = 3;
Object[][] params = FileUtil.getParametersFromFile("incorrectFileName.txt", cols);
assertNull(params);
}
@Test
/**
* This test:
* "throws MalformedTestSet, reporting the offending line number in the text file,
* if any line does not have 'cols' data items."
*/
public void testInvalidCols() {
int cols = 4;
Object[][] params = FileUtil.getParametersFromFile("lab1_part1_validTestSet.txt", cols);
assertNull(params);
}
@Test
/**
* This tests:
* "invalid deliminator characters."
* This tests:
* "throws MalformedTestSet, reporting the offending line number in the text file,"
*/
public void testInvalidData() {
int cols = 3;
Object[][] params = FileUtil.getParametersFromFile("lab1_part1_invalidTestSet.txt", cols);
assertNull(params);
}
@Test
/**
* This tests:
* "throws InvalidDataException if file is empty, or has only one line (i.e., the file should contain
* more than just a header."
*/
public void testEmptyFile() {
int cols = 3;
Object[][] params = FileUtil.getParametersFromFile("lab1_part1_emptyfile.txt", cols);
try {
assertNull(params);
} catch (AssertionError e) {
File f = new File("Part_1_Defects.txt");
try {
FileWriter writer = new FileWriter(f);
writer.append(new Date().toString()+" ");
writer.append("testEmptyFile failed ");
writer.append("Inputs: ("lab1_part1_emptyfile.txt", cols, ) ");
writer.append("Reason: FileUtil.getParametersFromFile does not check if variable index = 0 after the while loop. ");
writer.flush();
writer.close();
} catch (IOException e1) {
e1.printStackTrace();
}
// According to instructions pdf: If you find no failures, then all tests in CfgTests should complete “green”.
// I did find a failure so I invoked fail(). (a.k.a. I did not find no failures.)
fail();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.