Last active
September 27, 2017 18:33
-
-
Save claraj/de896bbd8c0cdda3213f3f21f2fb2ac9 to your computer and use it in GitHub Desktop.
Q5 Test Corrections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package week_5; | |
import com.google.common.base.Joiner; | |
import org.junit.Test; | |
import test_utils.PrintUtils; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.util.NoSuchElementException; | |
import static org.junit.Assert.*; | |
/** | |
* Created by Clara on 8/3/17. | |
* | |
*/ | |
public class Question_5_Add_Exception_HandlingTest { | |
@Test | |
public void printLanguageList() throws Exception { | |
// Ensure code does not crash | |
// Check that code still prints the same output | |
try { | |
Question_5_Add_Exception_Handling q5 = new Question_5_Add_Exception_Handling(); | |
PrintUtils.catchStandardOut(); // Catch the program's output | |
q5.printLanguageList(); | |
String out = PrintUtils.resetStandardOut(); // And save it, to chck the program still prints the same data | |
out = out.replace("\n", ""); | |
out = out.replace("\r", ""); | |
assertTrue("printLanguageList should still print the same 3 languages in the same order.", | |
out.contains("C#SwiftPython")); | |
} catch (NoSuchElementException e) { | |
fail("Add try-catch statements to catch the exception thrown in printLanguageList"); | |
} | |
} | |
@Test | |
public void printLanguageListTryCatch() throws Exception { | |
// Read source code and check that a try-catch statement has been added | |
// Not something that would be done in a commercial test :) | |
Joiner joiner = Joiner.on(System.getProperty("file.separator")); | |
String path = joiner.join("src", "main", "java", "week_5", "Question_5_Add_Exception_Handling.java"); | |
BufferedReader reader = new BufferedReader(new FileReader(new File(path))); | |
String line = reader.readLine(); | |
StringBuffer allLines = new StringBuffer(); | |
boolean inMethod = false; | |
while (line != null) { | |
if (line.contains("Start of printLanguageList.")) { | |
inMethod = true; | |
} | |
if (line.contains("End of printLanguageList.")) { | |
break; | |
} | |
if (inMethod) { | |
allLines.append(line); | |
} | |
line = reader.readLine(); | |
} | |
String allCode = allLines.toString(); | |
String pattern = "[\\S\\s]*catch.*\\(\\s*NoSuchElementException[\\S\\s]*"; | |
assertTrue("Add try-catch statements inside the printLanguageList method to catch the exception being thrown", | |
allCode.matches(pattern)); | |
} | |
@Test | |
public void wordCount() throws Exception { | |
// Ensure code does not crash | |
// Read source code to check for presence of try-catch block for the specific exception thrown | |
try { | |
Question_5_Add_Exception_Handling q5 = new Question_5_Add_Exception_Handling(); | |
int words = q5.wordCount("testing one two three"); | |
assertEquals("Make sure you don't change the behavior of wordCount. It should still count words.", 4, words); | |
int nowords = q5.wordCount(null); | |
assertEquals("wordCount should return 0 if sentence is null.", 0, nowords); | |
} catch (NullPointerException npe) { | |
fail("Add try-catch statements to catch the exception thrown in wordCount when sentence is null"); | |
} | |
} | |
@Test | |
public void wordCountTryCatch() throws Exception { | |
// Read source code and check that a try-catch statement has been added | |
// Not something that would be done in a commercial test :) | |
Joiner joiner = Joiner.on(System.getProperty("file.separator")); | |
String path = joiner.join("src", "main", "java", "week_5", "Question_5_Add_Exception_Handling.java"); | |
BufferedReader reader = new BufferedReader(new FileReader(new File(path))); | |
String line = reader.readLine(); | |
StringBuffer allLines = new StringBuffer(); | |
boolean inMethod = false; | |
while (line != null) { | |
if (line.contains("Start of wordCount.")) { | |
inMethod = true; | |
} | |
if (line.contains("End of wordCount.")) { | |
break; | |
} | |
if (inMethod) { | |
allLines.append(line); | |
} | |
line = reader.readLine(); | |
} | |
String allCode = allLines.toString(); | |
String pattern = "[\\S\\s]*catch.*\\(\\s*NullPointerException[\\S\\s]*"; | |
assertTrue("Add try-catch statements inside the wordCount method to catch the exception being thrown", | |
allCode.matches(pattern)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package week_5; | |
import com.google.common.base.Joiner; | |
import org.junit.Test; | |
import test_utils.PrintUtils; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.util.NoSuchElementException; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import static org.junit.Assert.*; | |
/** | |
* Created by Clara on 8/3/17. | |
* | |
*/ | |
public class Question_6_Fix_Code_No_Exception_HandlingTest { | |
@Test | |
public void printLanguageList() throws Exception { | |
try { | |
// Check that code still prints the same output | |
Question_6_Fix_Code_No_Exception_Handling q6 = new Question_6_Fix_Code_No_Exception_Handling(); | |
PrintUtils.catchStandardOut(); // Catch the program's output | |
q6.printLanguageList(); | |
String out = PrintUtils.resetStandardOut(); // And save it, to check the program still prints the same data | |
out = out.replace("\n", ""); | |
out = out.replace("\r", ""); | |
assertTrue("printLanguageList should still print the same 3 languages in the same order.", | |
out.contains("C#PythonJavaScript")); | |
} catch (NoSuchElementException e) { | |
// Ensure code does not crash | |
fail("Check if the languages LinkedList is empty before removing elements from it"); | |
} | |
} | |
@Test | |
public void wordCount() throws Exception { | |
// Ensure code does not crash | |
// Read source code to check for presence of try-catch block for the specific exception thrown | |
try { | |
Question_6_Fix_Code_No_Exception_Handling q6 = new Question_6_Fix_Code_No_Exception_Handling(); | |
int words = q6.wordCount("testing one two three"); | |
assertEquals("Make sure you don't change the behavior of wordCount. It should still count words.", 4, words); | |
int nowords = q6.wordCount(null); | |
assertEquals("wordCount should return 0 if sentence is null.", 0, nowords); | |
} catch (NullPointerException npe) { | |
fail("Check if the sentence is null before calling sentence.split()"); | |
} | |
} | |
@Test | |
public void findTryCatch() throws Exception { | |
// Read source code and check that try-catch statements have not been added | |
// Not something that would be done in a commercial test :) | |
Joiner joiner = Joiner.on(System.getProperty("file.separator")); | |
String path = joiner.join("src", "main", "java", "week_5", "Question_6_Fix_Code_No_Exception_Handling.java"); | |
BufferedReader reader = new BufferedReader(new FileReader(new File(path))); | |
Pattern nseePattern = Pattern.compile("catch.*\\(\\s*NoSuchElementException"); | |
Pattern npePattern = Pattern.compile("catch.*\\(\\s*NullPointerException"); | |
String line = reader.readLine(); | |
while (line != null) { | |
Matcher nseeMatcher = nseePattern.matcher(line); | |
Matcher npeMatcher = npePattern.matcher(line); | |
assertFalse("The text \"catch (NoSuchElementException ... ) { ... }\" should not appear in your code for this problem. " + | |
"Your code should make checks to avoid an exception being thrown", | |
nseeMatcher.find()); | |
assertFalse("The text \"catch (NullPointerException ... ) { ... }\" should not appear in your code for this problem. " + | |
"Your code should make checks to avoid an exception being thrown", | |
npeMatcher.find()); | |
line = reader.readLine(); | |
} | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package week_5; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Test; | |
import test_utils.FileUtils; | |
import java.io.*; | |
import java.lang.reflect.Method; | |
import static org.junit.Assert.*; | |
/** | |
* Created by clara on 8/3/17. | |
*/ | |
public class Question_7_Coffee_ShopTest { | |
String testPriceFilename; | |
String testSalesFilename; | |
String testOutputFile; | |
@Before | |
public void createFilenames() { | |
testPriceFilename = FileUtils.uniqueFilename("temporary_file_for_testing_test_price_data"); | |
testSalesFilename = FileUtils.uniqueFilename("temporary_file_for_testing_test_sales_data.txt"); | |
testOutputFile = FileUtils.uniqueFilename("temporary_file_for_testing_report.txt"); | |
} | |
@Test | |
public void salesReport() throws Exception { | |
Question_7_Coffee_Shop q7 = new Question_7_Coffee_Shop(); | |
String originalReportFilename = q7.output_report_file; | |
//Create some example input files | |
String priceData = "Coke;0.1;2\n" + | |
"Sprite;0.2;2.50"; | |
String salesData = "Coke;4\n" + | |
"Sprite;7"; | |
FileWriter writer = new FileWriter(testPriceFilename); | |
writer.write(priceData); | |
writer.close(); | |
writer = new FileWriter(testSalesFilename); | |
writer.write(salesData); | |
writer.close(); | |
// Replace the original filenames with these testing files | |
q7.output_report_file = testOutputFile; | |
q7.sales_data_file = testSalesFilename; | |
q7.price_data_file = testPriceFilename; | |
// Contents of expected sales report, based on the data above | |
String expectedSalesReport = "Coke: Sold 4, Expenses $0.40, Revenue $8.00, Profit $7.60\n" + | |
"Sprite: Sold 7, Expenses $1.40, Revenue $17.50, Profit $16.10\n" + | |
"All Drinks: Sold 11, Expenses $1.80, Revenue $25.50, Profit $23.70"; | |
q7.salesReport(); | |
// Read the file and compare to expectedSalesReport | |
try { | |
BufferedReader reader = new BufferedReader(new FileReader(q7.output_report_file)); | |
StringBuilder builder = new StringBuilder(); | |
String line = reader.readLine(); | |
while (line != null) { | |
builder.append(line); | |
builder.append("\n"); | |
line = reader.readLine(); | |
} | |
String data = builder.toString().trim().replace("\r", ""); // remove trailing white space and Windows '\r' characters | |
reader.close(); | |
assertEquals("Make sure you write the data in the exact format requested, and verify your math is correct.", expectedSalesReport, data); | |
} catch (FileNotFoundException f) { | |
fail("Write the report to a file called " + originalReportFilename + ". Use the variable output_report_file for the file name."); | |
} | |
} | |
@Test | |
public void checkMethodDoesNotThrowException() throws Exception { | |
//Verify readCoffeeDataFiles and writeReportFile do not throw exceptions | |
// TODO verify try-with-resources is used. | |
Class q7 = Class.forName("week_5.Question_7_Coffee_Shop"); | |
Method mRead = q7.getMethod("readCoffeeDataFiles", String.class, String.class); | |
assertEquals("Add try-catch blocks to your readCoffeeDataFiles method. Handle any possible exceptions with try-catch statements within the method.", 0, mRead.getExceptionTypes().length); | |
// since the return type has changed, have to search the methods in the class, instead of being able to specify a particular method. | |
Method[] allMethods = q7.getMethods(); | |
for (Method m : allMethods) { | |
if (m.getName().equals("writeReportFile")) { | |
assertEquals("Add try-catch blocks to your writeReportFile method. " + | |
"Handle any possible exceptions with try-catch statements within the method.", 0, m.getExceptionTypes().length); | |
} | |
} | |
} | |
// Since the implementation of the code is mostly up to you, it's impossible for me to write any more | |
// detailed tests. Maybe you could write some tests for your methods? | |
@After | |
public void cleanupFiles() { | |
// Move temporary files used for the tests. | |
FileUtils.moveToTemporaryTestFolder(testOutputFile); | |
FileUtils.moveToTemporaryTestFolder(testPriceFilename); | |
FileUtils.moveToTemporaryTestFolder(testSalesFilename); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment