Program to process text in a file

Program to process text in a file

Capitalize Utility 

Project Goals

  • Develop a Java application for replacing strings within a file.
  • Get experience with an agile, test-driven process.

Details

For this project you must develop, using Java, a simple command-line utility called capitalize, which is the same utility for which you developed test frames in the category-partition assignment. For Deliverable 1, you have developed a first implementation of capitalize that makes your initial set of test cases pass.

For this second deliverable, you have to modify your implementation to account for a slight update in the specification for capitalize requested by your customer. The updated specification is below, with the changed parts marked in red:

Concise (Updated) Specification of the capitalize Utility

  • NAME:
    capitalize – capitalizes words in a file.
  • SYNOPSIS
    capitalize OPT <filename>where OPT can be zero or more of
  • -l
  • -e
  • -s [<string of delimiters>]
  • -x
  • -c <boolean>
  • COMMAND-LINE ARGUMENTS AND OPTIONSfilename: the file on which the capitalize operation has to be performed.

 

-l: if specified, the capitalize utility will capitalize all the first characters in each new line, including the first line.

-e: if specified, the capitalize utility will capitalize all the letters in each exclamatory sentence (i.e., in each sentence ending with a “!”).

-s [<string of delimiters>]: if specified, the capitalize utility will capitalize the first character of each sentence. Sentences end by default with one of the characters ‘.’, ‘!’, or ‘?’. The optional string of delimiters can be used to specify an alternative set of sentence delimiters that replace the default ones for the effect of -s.

-x: if specified, the capitalize utility will lowercase all existing capital letters that do not meet the specified capitalization rules, or the default capitalization rules if no other flags are specified.

-c <boolean>: converts to or from camelcase.  If true, converts to camel case, removing white space between words, and capitalizing letters following the prior whitespace.  If false, does the reverse.

If none of -l, -e, or -s are specified, capitalize capitalizes all whitespace delimited words in the file. Otherwise, the specified flags alter the default behavior, as illustrated in the examples below.

  • EXAMPLES OF USAGEExample 1:
    capitalize file1.txt
    would capitalize the first character in every whitespace delimited word.
    Example 2:
    capitalize -l file1.txt
    would capitalize only the first character in every line.

Example 3:
capitalize -e -s file1.txt
would capitalize the first character in every sentence and every letter in every exclamatory sentence, where a sentence is defined as ending in one of the characters ‘.’, ‘!’, or ‘?’.
Example 4:
capitalize -s , -x file1.txt
would capitalize only the first character in each sentence, where a sentence is defined as ending in a comma (such as the fields in a csv file).

  • NOTES
    • If the character to be capitalized is not a letter, it is left unchanged.
    • While the last command-line parameter provided is always treated as the filename, the OPT flags can be provided in any order.

You must develop the capitalize utility using a test-driven development approach. Specifically, you will perform three activities within this project:

  • For Deliverable 1, you extended the set of test cases that you created for Assignment 6; that is, you used your test specifications to prepare 15 additional JUnit tests (i.e., in addition to the 15 you developed for Assignment 6). Then, you implemented the actual capitalize utility and made sure that all of the test cases that you developed for Assignment 6 and Deliverable 1, plus the initial test cases provided by us, passed.
  • Deliverable 2 will continue the test-driven development approach, extending and refactoring capitalize to meet the updated specifications and pass the additional provided tests.
  • Deliverable 3 will be revealed in due time.

 Deliverables:

DELIVERABLE 1 (this deliverable)

  • Provided (in Assignment 6):
    • Skeleton of the main class for capitalize
    • Initial set of JUnit test cases (yours + ours)
    • Your A6 submission
  • expected:
    • 15 additional JUnit test cases
    • Implementation of capitalize that makes all test cases pass

DELIVERABLE 2

  • provided:
    • Additional set of JUnit test cases (to be run in addition to yours)
    • Updated capitalize specification
  • expected:
    • Implementation of capitalize that makes all test cases pass

DELIVERABLE 3

  • provided: TBD
  • expected: TBD 

Deliverable 2: Instructions

  1. Use files given.
  2. Unpack and create an IndividualProject directory. After unpacking, you should see the following files:
  • IndividualProject/…/capitalize/MainTest.java
  • IndividualProject/…/capitalize/MainTestSuite.java
  1. Class MainTest is the originally provided test class with both updated and additional test cases for the capitalize utility. It should replace your original MainTest.java file.  Imagine that these are test cases developed by coworkers who were also working on the project and developed tests for capitalize based on (1) updated customer requirements and (2) some of the discussion about the semantics of capitalize that took place somewhere. As was the case for the test cases we provided for Deliverable 1, all these tests must pass, unmodified, on your implementation of capitalize.

Please note that these tests clarify capitalize’s behavior and also make some assumptions on the way capitalize works. You should use the test cases to refactor your code.

In most cases, we expect that these assumptions will not violate the assumptions you made when developing your test cases for Deliverable 1, but they might in some cases. If they do, please adapt your test cases (and possibly your code) accordingly, which may also give you an additional opportunity to get some experience with refactoring.

To summarize: all the test cases in the new MainTest class should pass (unmodified) and all the test cases in the MyMainTest class should pass (possibly after modifying them) on your implementation of capitalize.

  1. Class MainTestSuite is a simple test suite that invokes all the test cases in test classes MainTest and MyMainTest. You can use this class to run all the test cases for capitalize at once and check that they all pass. 

Solution 

package edu.gatech.seclass.capitalize;

import java.io.File;

import java.io.IOException;

import java.io.PrintWriter;

import java.nio.charset.Charset;

import java.nio.charset.StandardCharsets;

import java.nio.file.Files;

import java.nio.file.Paths;

public class Main {

/*

DO NOT ALTER THIS CLASS or implement it.

*/

public static void main(String[] args) {

// Empty Skeleton Method

Charset charset = StandardCharsets.UTF_8;

boolean newLine = false;

boolean excalamtory = false;

boolean newDelimiters = false;

boolean lowerCase = false;

boolean camelCase = false;

boolean formatCamelCase = false;

String delimiters = “.!?”;

String filename = null;

if (args == null) {

usage();

return;

}

if (args.length < 1) {

usage();

return;

}

for (int i = 0; i<args.length-1; i++) {

String opt = args[i];

switch(opt) {

case “-l”:

newLine = true;

break;

case “-e”:

excalamtory = true;

break;

case “-c”:

camelCase = true;

if (i + 1 < args.length – 1) {

formatCamelCase = Boolean.parseBoolean(args[i+1]);

i++;

}

else {

usage();

return;

}

break;

case “-s”:

newDelimiters = true;

if (i + 1 < args.length – 1) {

if (args[i+1].charAt(0) != ‘-‘) {

delimiters = args[i+1];

i++;

}

}

break;

case “-x”:

lowerCase = true;

break;

 

default:

usage();

return;

}

}

filename = args[args.length-1];

 

if (!newLine && !newDelimiters  && !excalamtory) {

delimiters = ” \n”;

}

if (newLine) {

delimiters += “\n”;

}

if (excalamtory) {

delimiters += “!”;

}

String content = null;

try {

content = new String(Files.readAllBytes(Paths.get(filename)), charset);

StringBuffer buf = new StringBuffer();

StringBuffer sentence = new StringBuffer();

char[] chars = content.toCharArray();

for (int i = 0; i<chars.length; i++) {

char currentSymbol = chars[i];

boolean isDelimiter = false;

for (char c : delimiters.toCharArray()) {

if (currentSymbol == c) {

isDelimiter = true;

break;

}

}

if (isDelimiter) {

String s = sentence.toString();

sentence = new StringBuffer();

if (excalamtory && (currentSymbol == ‘!’)) {

s = s.toUpperCase();

}

for (int j = 0; j<s.length(); j++) {

if (!Character.isISOControl(s.charAt(j)) && !(s.charAt(j) == ‘ ‘)) {

if (j < s.length()-1) {

String temp = s.substring(j);

if (camelCase) {

if (formatCamelCase) {

s = s.substring(0, j) + toCamelCase(temp);

}

else {

s = s.substring(0, j) + fromCamelCase(temp);

}

}

else {

s = s.substring(0, j+1).toUpperCase() + s.substring(j+1);

}

}

else {

s = s.substring(0, j+1).toUpperCase();

}

break;

}

}

buf.append(s);

buf.append(currentSymbol);

}

else {

if (lowerCase) {

currentSymbol = Character.toLowerCase(currentSymbol);

}

sentence.append(currentSymbol);

}

}

String s = sentence.toString();

for (int j = 0; j<s.length(); j++) {

if (!Character.isISOControl(s.charAt(j)) && !(s.charAt(j) == ‘ ‘)) {

if (j < s.length()-1) {

String temp = s.substring(j);

if (camelCase) {

if (formatCamelCase) {

s = s.substring(0, j) + toCamelCase(temp);

}

else {

s = s.substring(0, j) + fromCamelCase(temp);

}

}

else {

s = s.substring(0, j+1).toUpperCase() + s.substring(j+1);

}

}

else {

s = s.substring(0, j+1).toUpperCase();

}

break;

}

}

buf.append(s);

PrintWriter writer = new PrintWriter(new File(filename));

writer.print(buf.toString());

writer.close();

} catch (IOException e) {

System.err.println(“File Not Found”);

}

}

private static void usage() {

System.err.println(“Usage: Capitalize  [-l] [-e] [-s] [string] [-x] [-c] [boolean] <filename>”);

}

private static String toCamelCase(String s) {

StringBuffer buf = new StringBuffer();

boolean isNextCapital = true;

for (int i = 0; i<s.length(); i++) {

if (Character.isISOControl(s.charAt(i)) || (s.charAt(i) == ‘ ‘)) {

isNextCapital = true;

}

else {

if (isNextCapital) {

buf.append(Character.toUpperCase(s.charAt(i)));

}

else {

buf.append(s.charAt(i));

}

isNextCapital = false;

}

}

return buf.toString();

}

private static String fromCamelCase(String s) {

StringBuffer buf = new StringBuffer();

for (int i = 0; i<s.length(); i++) {

if (i == 0) {

buf.append(Character.toUpperCase(s.charAt(i)));

}

else {

if (Character.isUpperCase(s.charAt(i))) {

buf.append(” “);

buf.append(Character.toLowerCase(s.charAt(i)));

}

else {

buf.append(s.charAt(i));

}

}

}

return buf.toString();

}

} 

MainTest.java

 package edu.gatech.seclass.capitalize;

import org.junit.After;

import org.junit.Before;

import org.junit.Rule;

import org.junit.Test;

import org.junit.rules.TemporaryFolder;

import java.io.*;

import java.nio.charset.Charset;

import java.nio.charset.StandardCharsets;

import java.nio.file.Files;

import java.nio.file.Paths;

import static org.junit.Assert.*;

/*

DO NOT ALTER THIS CLASS.  Use it as an example for MyMainTest.java

*/

public class MainTest {

private ByteArrayOutputStream outStream;

private ByteArrayOutputStream errStream;

private PrintStream outOrig;

private PrintStream errOrig;

private Charset charset = StandardCharsets.UTF_8;

@Rule

public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Before

public void setUp() throws Exception {

outStream = new ByteArrayOutputStream();

PrintStream out = new PrintStream(outStream);

errStream = new ByteArrayOutputStream();

PrintStream err = new PrintStream(errStream);

outOrig = System.out;

errOrig = System.err;

System.setOut(out);

System.setErr(err);

}

@After

public void tearDown() throws Exception {

System.setOut(outOrig);

System.setErr(errOrig);

}

// Some utilities

private File createTmpFile() throws IOException {

File tmpfile = temporaryFolder.newFile();

tmpfile.deleteOnExit();

return tmpfile;

}

private File createInputFile1() throws Exception {

File file1 =  createTmpFile();

FileWriter fileWriter = new FileWriter(file1);

fileWriter.write(“Howdy Billy,\n” +

“This is a test file for the capitalize utility.\n” +

“let’s make sure it has at least a few lines,\n” +

“so that we can create some \n”

+ “interesting test cases…And let’s say \”howdy\” to Bill again!”);

fileWriter.close();

return file1;

}

private File createInputFile2() throws Exception {

File file1 =  createTmpFile();

FileWriter fileWriter = new FileWriter(file1);

fileWriter.write(“Bill is,\n” +

“in my opinion,\n” +

“an easier name to spell than William.\n” +

“Bill is shorter,\n” +

“and Bill is\n” +

“first alphabetically.”);

fileWriter.close();

return file1;

}

private File createInputFile3() throws Exception {

File file1 =  createTmpFile();

FileWriter fileWriter = new FileWriter(file1);

fileWriter.write(“Howdy Bill, have you learned your abc and 123?\r\n” +

“I know My Abc’s.\r” +

“It is important to know your abc’s and 123’s,\n” +

“so repeat with me: abc! 123! Abc and 123!”);

fileWriter.close();

return file1;

}

private File createInputFile4() throws Exception {

File file1 =  createTmpFile();

FileWriter fileWriter = new FileWriter(file1);

fileWriter.write(“AppleBananaCarrot.\r” +

“DogElephantHorse.\r” +

“Icecream. Jello”);

fileWriter.close();

return file1;

}

private File createInputFile5() throws Exception {

File file = createTmpFile();

FileWriter fileWriter = new FileWriter(file);

fileWriter.write(“”);

fileWriter.close();

return file;

}

private File createInputFile6() throws Exception {

File file = createTmpFile();

FileWriter fileWriter = new FileWriter(file);

fileWriter.write(“this is a very simple string” + System.lineSeparator() +

“with a few lines” + System.lineSeparator() +

“and not much else”);

fileWriter.close();

return file;

}

private File createInputFile7() throws Exception {

File file = createTmpFile();

FileWriter fileWriter = new FileWriter(file);

fileWriter.write(“– — — –“);

fileWriter.close();

return file;

}

private File createInputFile8() throws Exception {

File file1 =  createTmpFile();

FileWriter fileWriter = new FileWriter(file1);

fileWriter.write(“Mary had a csv,csv,csv\n” +

“Mary had a csv,commas, in a csv.\n” +

“csv,csv,csv”);

fileWriter.close();

return file1;

}

private File createInputFile9() throws Exception {

File file1 =  createTmpFile();

FileWriter fileWriter = new FileWriter(file1);

fileWriter.write(“oneword”);

fileWriter.close();

return file1;

}

private File createInputFile10() throws Exception {

File file1 =  createTmpFile();

FileWriter fileWriter = new FileWriter(file1);

fileWriter.write(“lol.rofl.roflmho”);

fileWriter.close();

return file1;

}

private String getFileContent(String filename) {

String content = null;

try {

content = new String(Files.readAllBytes(Paths.get(filename)), charset);

} catch (IOException e) {

e.printStackTrace();

}

return content;

}

// test cases

// Purpose: To provide an example of a test case format

// Frame #: Instructor example 1 from assignment directions

@Test

public void mainTest1() throws Exception {

File inputFile1 = createInputFile1();

String args[] = {inputFile1.getPath()};

Main.main(args);

String expected1 = “Howdy Billy,\n” +

“This Is A Test File For The Capitalize Utility.\n” +

“Let’s Make Sure It Has At Least A Few Lines,\n” +

“So That We Can Create Some \n”

+ “Interesting Test Cases…And Let’s Say \”howdy\” To Bill Again!”;

String actual1 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected1, actual1);

}

// Purpose: To provide an example of a test case format

// Frame #: Instructor example 2 from assignment directions

@Test

public void mainTest2() throws Exception {

File inputFile1 = createInputFile1();

String args[] = {“-l”, inputFile1.getPath()};

Main.main(args);

String expected1 = “Howdy Billy,\n” +

“This is a test file for the capitalize utility.\n” +

“Let’s make sure it has at least a few lines,\n” +

“So that we can create some \n”

+ “Interesting test cases…And let’s say \”howdy\” to Bill again!”;

String actual1 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected1, actual1);

}

// Purpose: To provide an example of a test case format

// Frame #: Instructor example 3 from assignment directions

@Test

public void mainTest3() throws Exception {

File inputFile1 = createInputFile1();

String args[] = {“-e”, “-s”, inputFile1.getPath()};

Main.main(args);

String expected2 = “Howdy Billy,\n” +

“This is a test file for the capitalize utility.\n” +

“Let’s make sure it has at least a few lines,\n” +

“so that we can create some \n”

+ “interesting test cases…AND LET’S SAY \”HOWDY\” TO BILL AGAIN!”;

String actual2 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected2, actual2);

}

// Purpose: To provide an example of a test case format

// Frame #: Instructor example 4 from assignment directions

@Test

public void mainTest4() throws Exception {

File inputFile1 = createInputFile1();

String args[] = {“-s”, “,”, “-x”, inputFile1.getPath()};

Main.main(args);

String expected3 = “Howdy billy,\n” +

“This is a test file for the capitalize utility.\n” +

“let’s make sure it has at least a few lines,\n” +

“So that we can create some \n”

+ “interesting test cases…and let’s say \”howdy\” to bill again!”;

String actual3 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected3, actual3);

}

// Purpose: To provide an example of a test case format

// Frame #: Instructor error example

@Test

public void mainTest5() {

String args[] = null; //invalid argument

Main.main(args);

assertEquals(“Usage: Capitalize  [-l] [-e] [-s] [string] [-x] [-c] [boolean] <filename>”, errStream.toString().trim());

}

// Additional Test for Deliverable 2

@Test

public void mainTest6() {

String args[] = {“nofile.txt”};

Main.main(args);

assertEquals(“File Not Found”, errStream.toString().trim());

}

// Additional Test for Deliverable 2

@Test

public void mainTest7() throws Exception {

File inputFile1 = createInputFile1();

String args[] = {“-s”, “,”, “-x”, “-c”, inputFile1.getPath()};

Main.main(args);

assertEquals(“Usage: Capitalize  [-l] [-e] [-s] [string] [-x] [-c] [boolean] <filename>”, errStream.toString().trim());

}

// Additional Test for Deliverable 2

@Test

public void mainTest8() throws Exception {

File inputFile = createInputFile2();

String args[] = {“-s”, “,.?!”, “-x”, inputFile.getPath()};

Main.main(args);

String expected = “Bill is,\n” +

“In my opinion,\n” +

“An easier name to spell than william.\n” +

“Bill is shorter,\n” +

“And bill is\n” +

“first alphabetically.”;

String actual = getFileContent(inputFile.getPath());

assertEquals(“The files differ!”, expected, actual);

}

// Additional Test for Deliverable 2

@Test

public void mainTest9() throws Exception {

File inputFile = createInputFile2();

String args[] = {“-s”, “,.?!”, “-e”, “-x”, “-c”, “true”, inputFile.getPath()};

Main.main(args);

String expected = “BillIs,\n” +

“InMyOpinion,\n” +

“AnEasierNameToSpellThanWilliam.\n” +

“BillIsShorter,\n” +

“AndBillIsFirstAlphabetically.”;

String actual = getFileContent(inputFile.getPath());

assertEquals(“The files differ!”, expected, actual);

}

// Additional Test for Deliverable 2

@Test

public void mainTest10() throws Exception {

File inputFile = createInputFile4();

String args[] = {“-s”, “-c”, “false”, inputFile.getPath()};

Main.main(args);

String expected = “Apple banana carrot.\r” +

“Dog elephant horse.\r” +

“Icecream. Jello”;

String actual = getFileContent(inputFile.getPath());

assertEquals(“The files differ!”, expected, actual);

}

// Additional Test for Deliverable 2

@Test

public void mainTest11() throws Exception {

File inputFile = createInputFile4();

String args[] = {“-s”, inputFile.getPath()};

Main.main(args);

String expected = “AppleBananaCarrot.\r” +

“DogElephantHorse.\r” +

“Icecream. Jello”;

String actual = getFileContent(inputFile.getPath());

assertEquals(“The files differ!”, expected, actual);

}

// Additional Test for Deliverable 2

@Test

public void mainTest12() throws Exception {

File inputFile = createInputFile3();

String args[] = {“-e”, inputFile.getPath()};

Main.main(args);

String expected = “Howdy Bill, have you learned your abc and 123?\r\n” +

“I know My Abc’s.\r” +

“IT IS IMPORTANT TO KNOW YOUR ABC’S AND 123’S,\n” +

“SO REPEAT WITH ME: ABC! 123! ABC AND 123!”;

String actual = getFileContent(inputFile.getPath());

assertEquals(“The files differ!”, expected, actual);

}

// Additional Test for Deliverable 2

@Test

public void mainTest13() throws Exception {

File inputFile = createInputFile3();

String args[] = {“-e”, “-c”, “true”, inputFile.getPath()};

Main.main(args);

String expected = “HowdyBill,HaveYouLearnedYourAbcAnd123?\r\n” +

“IKnowMyAbc’s.\r” +

“ITISIMPORTANTTOKNOWYOURABC’SAND123’S,SOREPEATWITHME:ABC! 123! ABCAND123!”;

String actual = getFileContent(inputFile.getPath());

assertEquals(“The files differ!”, expected, actual);

}

// Additional Test for Deliverable 2

@Test

public void mainTest14() throws Exception {

File inputFile = createInputFile5();

String args[] = {“-s”, “-x”, inputFile.getPath()};

Main.main(args);

String expected = “”;

String actual = getFileContent(inputFile.getPath());

assertEquals(“The files differ!”, expected, actual);

}

// Additional Test for Deliverable 2

@Test

public void mainTest15() throws Exception {

File inputFile = createInputFile10();

String args[] = {“-s”, inputFile.getPath()};

Main.main(args);

String expected = “Lol.Rofl.Roflmho”;

String actual = getFileContent(inputFile.getPath());

assertEquals(“The files differ!”, expected, actual);

}

// Additional Test for Deliverable 2

@Test

public void mainTest16() throws Exception {

File inputFile = createInputFile6();

String args[] = {“-l”, inputFile.getPath()};

Main.main(args);

String expected = “This is a very simple string” + System.lineSeparator() +

“With a few lines” + System.lineSeparator()+

“And not much else”;

String actual = getFileContent(inputFile.getPath());

assertEquals(“The files differ!”, expected, actual);

}

// Additional Test for Deliverable 2

@Test

public void mainTest17() throws Exception {

File inputFile = createInputFile6();

String args[] = {“-s”, “i”, inputFile.getPath()};

Main.main(args);

String expected = “ThiS iS a very siMple striNg” + System.lineSeparator() +

“wiTh a few liNes” + System.lineSeparator() +

“and not much else”;

String actual = getFileContent(inputFile.getPath());

assertEquals(“The files differ!”, expected, actual);

}

// Additional Test for Deliverable 2

@Test

public void mainTest18() throws Exception {

File inputFile = createInputFile7();

String args[] = {“-l”, “-x”, inputFile.getPath()};

Main.main(args);

String expected = “– — — –“;

String actual = getFileContent(inputFile.getPath());

assertEquals(“The files differ!”, expected, actual);

}

// Additional Test for Deliverable 2

@Test

public void mainTest19() throws Exception {

File inputFile = createInputFile7();

String args[] = {“-s”, “-“, inputFile.getPath()};

Main.main(args);

String expected = “– — — –“;

String actual = getFileContent(inputFile.getPath());

assertEquals(“The files differ!”, expected, actual);

}

// Additional Test for Deliverable 2

@Test

public void mainTest20() throws Exception {

File inputFile = createInputFile8();

String args[] = {“-s”, “,.”, inputFile.getPath()};

Main.main(args);

String expected = “Mary had a csv,Csv,Csv\n” +

“Mary had a csv,Commas, In a csv.\n” +

“Csv,Csv,Csv”;

String actual = getFileContent(inputFile.getPath());

assertEquals(“The files differ!”, expected, actual);

}

// Additional Test for Deliverable 2

@Test

public void mainTest21() throws Exception {

File inputFile = createInputFile8();

String args[] = {“-s”, “c”, “-x”, inputFile.getPath()};

Main.main(args);

String expected = “Mary had a cSv,cSv,cSv\n” +

“mary had a cSv,cOmmas, in a cSv.\n” +

“cSv,cSv,cSv”;

String actual = getFileContent(inputFile.getPath());

assertEquals(“The files differ!”, expected, actual);

}

// Additional Test for Deliverable 2

@Test

public void mainTest22() throws Exception {

File inputFile = createInputFile9();

String args[] = {“-x”, inputFile.getPath()};

Main.main(args);

String expected = “Oneword”;

String actual = getFileContent(inputFile.getPath());

assertEquals(“The files differ!”, expected, actual);

}

// Additional Test for Deliverable 2

@Test

public void mainTest23() throws Exception {

File inputFile = createInputFile9();

String args[] = {“-c”, “false”, inputFile.getPath()};

Main.main(args);

String expected = “Oneword”;

String actual = getFileContent(inputFile.getPath());

assertEquals(“The files differ!”, expected, actual);

}

// Additional Test for Deliverable 2

@Test

public void mainTest24() throws Exception {

File inputFile = createInputFile10();

String args[] = {“-x”, “-x”, “-x”, inputFile.getPath()};

Main.main(args);

String expected = “Lol.rofl.roflmho”;

String actual = getFileContent(inputFile.getPath());

assertEquals(“The files differ!”, expected, actual);

}

// Additional Test for Deliverable 2

@Test

public void mainTest25() throws Exception {

File inputFile = createInputFile10();

String args[] = {“-s”, “l”, “-c”, “true”, inputFile.getPath()};

Main.main(args);

String expected = “lOl.rofl.roflMho”;

String actual = getFileContent(inputFile.getPath());

assertEquals(“The files differ!”, expected, actual);

}

// Additional Test for Deliverable 2

@Test

public void mainTest26() throws Exception {

File inputFile = createInputFile10();

String expected = getFileContent(inputFile.getPath());

String args[] = {“-l”, “-k”, “true”, inputFile.getPath()};

Main.main(args);

String actual = getFileContent(inputFile.getPath());

assertEquals(“The files differ!”, expected, actual);

assertEquals(“Usage: Capitalize  [-l] [-e] [-s] [string] [-x] [-c] [boolean] <filename>”, errStream.toString().trim());

}

} 

MainTestSuite.java

 package edu.gatech.seclass.capitalize;

import org.junit.runner.RunWith;

import org.junit.runners.Suite;

@RunWith(Suite.class)

@Suite.SuiteClasses({MainTest.class, MyMainTest.class})

public class MainTestSuite {}

MyMainTest.java

 package edu.gatech.seclass.capitalize;

import static org.junit.Assert.*;

import org.junit.After;

import org.junit.Before;

import org.junit.Rule;

import org.junit.Test;

import org.junit.rules.TemporaryFolder;

import java.io.*;

import java.nio.charset.Charset;

import java.nio.charset.StandardCharsets;

import java.nio.file.Files;

import java.nio.file.Paths;

/*

Place all  of your tests in this class, optionally using MainTest.java as an example.

*/

public class MyMainTest {

private ByteArrayOutputStream outStream;

private ByteArrayOutputStream errStream;

private PrintStream outOrig;

private PrintStream errOrig;

private Charset charset = StandardCharsets.UTF_8;

@Rule

public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Before

public void setUp() throws Exception {

outStream = new ByteArrayOutputStream();

PrintStream out = new PrintStream(outStream);

errStream = new ByteArrayOutputStream();

PrintStream err = new PrintStream(errStream);

outOrig = System.out;

errOrig = System.err;

System.setOut(out);

System.setErr(err);

}

@After

public void tearDown() throws Exception {

System.setOut(outOrig);

System.setErr(errOrig);

}

// Some utilities

private File createTmpFile() throws IOException {

File tmpfile = temporaryFolder.newFile();

tmpfile.deleteOnExit();

return tmpfile;

}

private File createInputFile1() throws Exception {

File file1 =  createTmpFile();

FileWriter fileWriter = new FileWriter(file1);

fileWriter.write(“hi brian,\n” +

“this is a test file for the capitalize utility.\n” +

“let’s make SURE it has at least a few lines,\n” +

“so that we can create some \n”

+ “interesting test CASES…And let’s say \”Hi\” to me again!”);

fileWriter.close();

return file1;

}

private File createInputFile2() throws Exception {

File file1 =  createTmpFile();

FileWriter fileWriter = new FileWriter(file1);

fileWriter.write(“yo BRIAN”);

fileWriter.close();

return file1;

}

private File createInputFile3() throws Exception {

File file1 =  createTmpFile();

FileWriter fileWriter = new FileWriter(file1);

fileWriter.write(“The Empire State Building is an American cultural icon,” +

“having been featured in dozens of TV shows and movies since King Kong was released in 1933.\n” +

“A symbol of New York City,\n” +

“it has been named as one of the Seven \n” +

“Wonders of the Modern World by the American Society of Civil Engineers. That’s all!”);

fileWriter.close();

return file1;

}

private File createInputFile4() throws Exception {

File file1 =  createTmpFile();

FileWriter fileWriter = new FileWriter(file1);

fileWriter.write(“The Empire State Building is an American cultural icon,” +

“having been featured in dozens of TV shows and movies since King Kong was released in 1933.” +

“A symbol of New York City,” +

“it has been named as one of the Seven ” +

“Wonders of the Modern World by the American Society of Civil Engineers. That’s all!”);

fileWriter.close();

return file1;

}

private String getFileContent(String filename) {

String content = null;

try {

content = new String(Files.readAllBytes(Paths.get(filename)), charset);

} catch (IOException e) {

e.printStackTrace();

}

return content;

}

// GT User: bbooker3

// test cases

// Purpose: To provide an example of no OPT specified

// Frame #: Incorporating Test Frame #34

@Test

public void MymainTest1() throws Exception {

File inputFile1 = createInputFile1();

String args[] = {inputFile1.getPath()};

Main.main(args);

String expected1 = “Hi Brian,\n” +

“This Is A Test File For The Capitalize Utility.\n” +

“Let’s Make SURE It Has At Least A Few Lines,\n” +

“So That We Can Create Some \n”

+ “Interesting Test CASES…And Let’s Say \”Hi\” To Me Again!”;

String actual1 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected1, actual1);

}

// Purpose: To provide an example of -l given

// Frame #: Incorporating Test Frame #21

@Test

public void MymainTest2() throws Exception {

File inputFile1 = createInputFile1();

String args[] = {“-l”, inputFile1.getPath()};

Main.main(args);

String expected1 = “Hi brian,\n” +

“This is a test file for the capitalize utility.\n” +

“Let’s make SURE it has at least a few lines,\n” +

“So that we can create some \n”

+ “Interesting test CASES…And let’s say \”Hi\” to me again!”;

String actual1 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected1, actual1);

}

// Purpose: To provide an example of -e, -s given; presence of a delimiter and exclamatory sentence

// Frame #: Incorporating Test Frame #26

@Test

public void MymainTest3() throws Exception {

File inputFile1 = createInputFile1();

String args[] = {“-e”, “-s”, inputFile1.getPath()};

Main.main(args);

String expected2 = “Hi brian,\n” +

“this is a test file for the capitalize utility.\n” +

“Let’s make SURE it has at least a few lines,\n” +

“so that we can create some \n”

+ “interesting test CASES…AND LET’S SAY \”HI\” TO ME AGAIN!”;

String actual2 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected2, actual2);

}

// Purpose: To provide an example of -s, -x given; presence of a delimiter and test all other char are lowercase

// Frame #: Incorporating Test Frame #80

@Test

public void MymainTest4() throws Exception {

File inputFile1 = createInputFile1();

String args[] = {“-s”, “,”, “-x”, inputFile1.getPath()};

Main.main(args);

String expected3 = “Hi brian,\n” +

“This is a test file for the capitalize utility.\n” +

“let’s make sure it has at least a few lines,\n” +

“So that we can create some \n”

+ “interesting test cases…and let’s say \”hi\” to me again!”;

String actual3 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected3, actual3);

}

// Purpose: Invalid argument

// Frame #: Incorporating Test Frame #5

@Test

public void MymainTest5() {

String args[] = new String[]{“-s”, “!?”, “,”, “input.txt”}; //invalid argument

Main.main(args);

assertEquals(“Usage: Capitalize  [-l] [-e] [-s] [string] [-x] [-c] [boolean] <filename>”, errStream.toString().trim());

}

// Purpose: Verify simple sentence performs correctly

// Frame #: Incorporating Test Frame #21

@Test

public void MymainTest6() throws Exception {

File inputFile1 = createInputFile1();

String args[] = {“-s”, inputFile1.getPath()};

Main.main(args);

String expected1 = “Hi brian,\n” +

“this is a test file for the capitalize utility.\n” +

“Let’s make SURE it has at least a few lines,\n” +

“so that we can create some \n”

+ “interesting test CASES…And let’s say \”Hi\” to me again!”;

String actual1 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected1, actual1);

}

// Purpose: Verify short sentence accepted

// Frame #: Incorporating Test Frame #49

@Test

public void MymainTest7() throws Exception {

File inputFile1 = createInputFile2();

String args[] = {“-s”, inputFile1.getPath()};

Main.main(args);

String expected1 = “Yo BRIAN”;

String actual1 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected1, actual1);

}

// Purpose: Verifying simple exclamatory sentence is correct

// Frame #: Incorporating Test Frame #61

@Test

public void MymainTest8() throws Exception {

File inputFile1 = createInputFile2();

String args[] = {“-e”, inputFile1.getPath()};

Main.main(args);

String expected2 = “Yo BRIAN”;

String actual2 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected2, actual2);

}

// Purpose: Verifying sentence and all others lowercase holds to be true with comma

// Frame #: Incorporating Test Frame #42

@Test

public void MymainTest9() throws Exception {

File inputFile1 = createInputFile2();

String args[] = {“-s”, “,”, “-x”, inputFile1.getPath()};

Main.main(args);

String expected3 = “Yo brian”;

String actual3 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected3, actual3);

}

// Purpose: Error occurs after removing -l, should still remain invalid

// Frame #: Incorporating Test Frame #14

@Test

public void MymainTest10() {

String args[] = new String[0];//invalid argument

Main.main(args);

assertEquals(“Usage: Capitalize  [-l] [-e] [-s] [string] [-x] [-c] [boolean] <filename>”, errStream.toString().trim());

}

// Purpose: No OPT

// Frame #: Incorporating Test Frame #28

@Test

public void MymainTest11() throws Exception {

File inputFile1 = createInputFile2();

String args[] = {inputFile1.getPath()};

Main.main(args);

String expected1 = “Yo BRIAN”;

String actual1 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected1, actual1);

}

// Purpose: Line and sentence s OPT produce correct capitalize syntax

// Frame #: Incorporating Test Frame #74

@Test

public void MymainTest12() throws Exception {

File inputFile1 = createInputFile2();

String args[] = {“-l”, “-s”, inputFile1.getPath()};

Main.main(args);

String expected1 = “Yo BRIAN”;

String actual1 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected1, actual1);

}

// Purpose: Correct capitalize syntax with exclamatory and sentence OPT

// Frame #: Incorporating Test Frame #84

@Test

public void MymainTest13() throws Exception {

File inputFile1 = createInputFile2();

String args[] = {“-e”, “-x”, “-s”, inputFile1.getPath()};

Main.main(args);

String expected2 = “Yo brian”;

String actual2 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected2, actual2);

}

// Purpose: Correct capitalize syntax with sentence and lowercase OPT

// Frame #: Incorporating Test Frame #51

@Test

public void MymainTest14() throws Exception {

File inputFile1 = createInputFile2();

String args[] = {“-s”, “-x”, inputFile1.getPath()};

Main.main(args);

String expected3 = “Yo brian”;

String actual3 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected3, actual3);

}

// Purpose: Error occurs after removing -l, and e, should still remain invalid

// Frame #: Incorporating Test Frame #14

@Test

public void MymainTest15() {

String args[] = new String[]{“l”, “input.txt”}; //invalid argument

Main.main(args);

assertEquals(“Usage: Capitalize  [-l] [-e] [-s] [string] [-x] [-c] [boolean] <filename>”, errStream.toString().trim());

}

// Purpose: To provide an example of -s given with delimiter of length 2; multiline input file.

// Frame #: Incorporating Test Frame #??

@Test

public void MyMainTest16() throws Exception {

File inputFile1 = createInputFile3();

String args[] = {“-s”, “o.”, inputFile1.getPath()};

Main.main(args);

String expected1 = “The Empire State Building is an American cultural icoN,” +

“having been featured in doZens oF TV shoWs and moVies since King KoNg was released in 1933.\n” +

“A symboL oF New YoRk City,\n” +

“it has been named as oNe oF the Seven \n” +

“WoNders oF the MoDern WoRld by the American SoCiety oF Civil Engineers. That’s all!”;

String actual1 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected1, actual1);

}

// Purpose: To provide an example of -s,-x given with delimiter of length 2; multiline input file.

// Frame #: Incorporating Test Frame #??

@Test

public void MyMainTest17() throws Exception {

File inputFile1 = createInputFile3();

String args[] = {“-s”, “o.”, “-x”, inputFile1.getPath()};

Main.main(args);

String expected2 = “The empire state building is an american cultural icoN,” +

“having been featured in doZens oF tv shoWs and moVies since king koNg was released in 1933.\n” +

“A symboL oF new yoRk city,\n” +

“it has been named as oNe oF the seven \n” +

“woNders oF the moDern woRld by the american soCiety oF civil engineers. That’s all!”;

String actual2 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected2, actual2);

}

// Purpose: To provide an example of -s,-l given with delimiter of length 2; multiline input file.

// Frame #: Incorporating Test Frame #??

@Test

public void MyMainTest18() throws Exception {

File inputFile1 = createInputFile3();

String args[] = {“-s”, “o.”, “-l”, inputFile1.getPath()};

Main.main(args);

String expected3 = “The Empire State Building is an American cultural icoN,” +

“having been featured in doZens oF TV shoWs and moVies since King KoNg was released in 1933.\n” +

“A symboL oF New YoRk City,\n” +

“It has been named as oNe oF the Seven \n” +

“WoNders oF the MoDern WoRld by the American SoCiety oF Civil Engineers. That’s all!”;

String actual3 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected3, actual3);

}

// Purpose: To provide an example of -s,-e given with delimiter of length 2; multiline input file.

// Frame #: Incorporating Test Frame #??

@Test

public void MyMainTest19() throws Exception {

File inputFile1 = createInputFile3();

String args[] = {“-s”, “o.”, “-e”, inputFile1.getPath()};

Main.main(args);

String expected4 = “The Empire State Building is an American cultural icoN,” +

“having been featured in doZens oF TV shoWs and moVies since King KoNg was released in 1933.\n” +

“A symboL oF New YoRk City,\n” +

“it has been named as oNe oF the Seven \n” +

“WoNders oF the MoDern WoRld by the American SoCiety oF Civil Engineers. THAT’S ALL!”;

String actual4 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected4, actual4);

}

// Purpose: To provide an example of -s,-l,-x given with delimiter of length 2; multiline input file.

// Frame #: Incorporating Test Frame #??

@Test

public void MyMainTest20() throws Exception{

File inputFile1 = createInputFile3();

String args[] = {“-s”, “o.”, “-l”, “-x”, inputFile1.getPath()};

Main.main(args);

String expected5 = “The empire state building is an american cultural icoN,” +

“having been featured in doZens oF tv shoWs and moVies since king koNg was released in 1933.\n” +

“A symboL oF new yoRk city,\n” +

“It has been named as oNe oF the seven \n” +

“WoNders oF the moDern woRld by the american soCiety oF civil engineers. That’s all!”;

String actual5 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected5, actual5);

}

// Purpose: To provide an example of -s,-e,-x given with delimiter of length 2; multiline input file.

// Frame #: Incorporating Test Frame #??

@Test

public void MyMainTest21() throws Exception {

File inputFile1 = createInputFile3();

String args[] = {“-s”, “o.”, “-e”, “-x”, inputFile1.getPath()};

Main.main(args);

String expected6 = “The empire state building is an american cultural icoN,” +

“having been featured in doZens oF tv shoWs and moVies since king koNg was released in 1933.\n” +

“A symboL oF new yoRk city,\n” +

“it has been named as oNe oF the seven \n” +

“woNders oF the moDern woRld by the american soCiety oF civil engineers. THAT’S ALL!”;

String actual6 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected6, actual6);

}

// Purpose: To provide an example of -s,-l,-e given with delimiter of length 2; multiline input file.

// Frame #: Incorporating Test Frame #??

@Test

public void MyMainTest22() throws Exception {

File inputFile1 = createInputFile3();

String args[] = {“-s”, “o.”, “-e”, “-l”, inputFile1.getPath()};

Main.main(args);

String expected7 = “The Empire State Building is an American cultural icoN,” +

“having been featured in doZens oF TV shoWs and moVies since King KoNg was released in 1933.\n” +

“A symboL oF New YoRk City,\n” +

“It has been named as oNe oF the Seven \n” +

“WoNders oF the MoDern WoRld by the American SoCiety oF Civil Engineers. THAT’S ALL!”;

String actual7 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected7, actual7);

}

// Purpose: To provide an example of -s,-l,-x,-e given with delimiter of length 2; multiline input file.

// Frame #: Incorporating Test Frame #??

@Test

public void MyMainTest23() throws Exception {

File inputFile1 = createInputFile3();

String args[] = {“-s”, “o.”, “-e”, “-x”, “-l”, inputFile1.getPath()};

Main.main(args);

String expected8 = “The empire state building is an american cultural icoN,” +

“having been featured in doZens oF tv shoWs and moVies since king koNg was released in 1933.\n” +

“A symboL oF new yoRk city,\n” +

“It has been named as oNe oF the seven \n” +

“WoNders oF the moDern woRld by the american soCiety oF civil engineers. THAT’S ALL!”;

String actual8 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected8, actual8);

}

// Purpose: To provide an example of -s,-x given with delimiter of length 2; single-line input file.

// Frame #: Incorporating Test Frame #??

@Test

public void MyMainTest24() throws Exception {

File inputFile1 = createInputFile4();

String args[] = {“-s”, “i.”, “-x”, inputFile1.getPath()};

Main.main(args);

String expected9 = “The empiRe state buiLdiNg iS an ameriCan cultural iCon,” +

“haviNg been featured iN dozens of tv shows and moviEs siNce kiNg kong was released iN 1933.” +

“A symbol of new york ciTy,” +

“iT has been named as one of the seven ” +

“wonders of the modern world by the ameriCan sociEty of ciViL engiNeers. That’s all!”;

String actual9 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected9, actual9);

}

// Purpose: To provide an example of -s,-l given with delimiter of length 2; single-line input file.

// Frame #: Incorporating Test Frame #??

@Test

public void MyMainTest25() throws Exception{

File inputFile1 = createInputFile4();

String args[] = {“-s”, “i.”, “-l”, inputFile1.getPath()};

Main.main(args);

String expected10 = “The EmpiRe State BuiLdiNg iS an AmeriCan cultural iCon,” +

“haviNg been featured iN dozens of TV shows and moviEs siNce KiNg Kong was released iN 1933.” +

“A symbol of New York CiTy,” +

“iT has been named as one of the Seven ” +

“Wonders of the Modern World by the AmeriCan SociEty of CiViL EngiNeers. That’s all!”;

String actual10 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected10, actual10);

}

// Purpose: To provide an example of -s,-e given with delimiter of length 2; single-line input file.

// Frame #: Incorporating Test Frame #??

@Test

public void MyMainTest26() throws Exception {

File inputFile1 = createInputFile4();

String args[] = {“-s”, “i.”, “-e”, inputFile1.getPath()};

Main.main(args);

String expected11 = “The EmpiRe State BuiLdiNg iS an AmeriCan cultural iCon,” +

“haviNg been featured iN dozens of TV shows and moviEs siNce KiNg Kong was released iN 1933.” +

“A symbol of New York CiTy,” +

“iT has been named as one of the Seven ” +

“Wonders of the Modern World by the AmeriCan SociEty of CiViL EngiNeers. THAT’S ALL!”;

String actual11 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected11, actual11);

}

// Purpose: To provide an example of -s,-x,-l given with delimiter of length 2; single-line input file.

// Frame #: Incorporating Test Frame #??

@Test

public void MyMainTest27() throws Exception {

File inputFile1 = createInputFile4();

String args[] = {“-s”, “i.”, “-x”, “-l”, inputFile1.getPath()};

Main.main(args);

String expected12 = “The empiRe state buiLdiNg iS an ameriCan cultural iCon,” +

“haviNg been featured iN dozens of tv shows and moviEs siNce kiNg kong was released iN 1933.” +

“A symbol of new york ciTy,” +

“iT has been named as one of the seven ” +

“wonders of the modern world by the ameriCan sociEty of ciViL engiNeers. That’s all!”;

String actual12 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected12, actual12);

}

// Purpose: To provide an example of -s,-x,-e given with delimiter of length 2; single-line input file.

// Frame #: Incorporating Test Frame #??

@Test

public void MyMainTest28() throws Exception {

File inputFile1 = createInputFile4();

String args[] = {“-s”, “i.”, “-x”, “-e”, inputFile1.getPath()};

Main.main(args);

String expected13 = “The empiRe state buiLdiNg iS an ameriCan cultural iCon,” +

“haviNg been featured iN dozens of tv shows and moviEs siNce kiNg kong was released iN 1933.” +

“A symbol of new york ciTy,” +

“iT has been named as one of the seven ” +

“wonders of the modern world by the ameriCan sociEty of ciViL engiNeers. THAT’S ALL!”;

String actual13 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected13, actual13);

}

// Purpose: To provide an example of -s,-e,-l given with delimiter of length 2; single-line input file.

// Frame #: Incorporating Test Frame #??

@Test

public void MyMainTest29() throws Exception {

File inputFile1 = createInputFile4();

String args[] = {“-s”, “i.”, “-l”, “-e”, inputFile1.getPath()};

Main.main(args);

String expected14 = “The EmpiRe State BuiLdiNg iS an AmeriCan cultural iCon,” +

“haviNg been featured iN dozens of TV shows and moviEs siNce KiNg Kong was released iN 1933.” +

“A symbol of New York CiTy,” +

“iT has been named as one of the Seven ” +

“Wonders of the Modern World by the AmeriCan SociEty of CiViL EngiNeers. THAT’S ALL!”;

String actual14 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected14, actual14);

}

// Purpose: To provide an example of -s,-x,-l,-e given with delimiter of length 2; single-line input file.

// Frame #: Incorporating Test Frame #??

@Test

public void MyMainTest30() throws Exception{

File inputFile1 = createInputFile4();

String args[] = {“-s”, “i.”, “-l”, “-e”, “-x”, inputFile1.getPath()};

Main.main(args);

String expected15 = “The empiRe state buiLdiNg iS an ameriCan cultural iCon,” +

“haviNg been featured iN dozens of tv shows and moviEs siNce kiNg kong was released iN 1933.” +

“A symbol of new york ciTy,” +

“iT has been named as one of the seven ” +

“wonders of the modern world by the ameriCan sociEty of ciViL engiNeers. THAT’S ALL!”;

String actual15 = getFileContent(inputFile1.getPath());

assertEquals(“The files differ!”, expected15, actual15);

}

}