Skip to content

Done #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<groupId>com.zipcodewilmington</groupId>
<artifactId>regex</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
Expand Down
31 changes: 24 additions & 7 deletions src/main/java/HamletParser.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,56 @@
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Created by thook on 10/7/15.
*/
public class HamletParser {

private String hamletData;

public HamletParser(){
private String[] patterns;
private String[] replacements;
private String fileName;


public HamletParser(String fileName, String[] patterns, String[] replacements) {
// if(replacements.length < patterns.length){
// throw new Exception("there must be at least as many replacements as patterns");
// }
this.patterns = patterns;
this.replacements = replacements;
this.fileName = fileName;
this.hamletData = loadFile();
}

private String loadFile(){
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("hamlet.txt").getFile());
File file = new File(classLoader.getResource(this.fileName).getFile());
StringBuilder result = new StringBuilder("");

try(Scanner scanner = new Scanner(file)){
while(scanner.hasNextLine()){
String line = scanner.nextLine();
result.append(line).append("\n");
}

scanner.close();
}catch(IOException e){
e.printStackTrace();
}

return result.toString();
}

public String getHamletData(){
return hamletData;
String outPut = this.hamletData;
for(int i = 0; i < patterns.length; i++){
Pattern p = Pattern.compile(this.patterns[i]);
Matcher matcher = p.matcher(outPut);
outPut = matcher.replaceAll(this.replacements[i]);
}
return outPut;
}

}


2 changes: 2 additions & 0 deletions src/main/resources/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Change Hamlet to Leon. Change Hamlet to Leon.
Change Horatio to Tariq. Change Horatio to Tariq.
60 changes: 51 additions & 9 deletions src/test/java/HamletParserTest.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,73 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.junit.Assert.*;

public class HamletParserTest {
private String hamletText;
private HamletParser hamletParser;

@Before
public void setUp() {
this.hamletParser = new HamletParser();
this.hamletText = hamletParser.getHamletData();
}

@Test
public void testChangeHamletToLeon() {
String inputLine = "Change Hamlet to Leon. Change Hamlet to Leon.";
String[] searchStr = {"Hamlet"};
String[] repStr = {"Leon"};
String fileName = "test.txt";
HamletParser test = new HamletParser(fileName, searchStr, repStr);
String actual = test.getHamletData();
String expect = "Change Leon to Leon. Change Leon to Leon.\n" +
"Change Horatio to Tariq. Change Horatio to Tariq.\n";
Assert.assertEquals(expect, actual);
}

@Test
public void testChangeHoratioToTariq() {
String inputLine = "Change Horatio to Tariq. Change Horatio to Tariq.";
String[] searchStr = {"Horatio"};
String[] repStr = {"Tariq"};
String fileName = "test.txt";
HamletParser test = new HamletParser(fileName, searchStr, repStr);
String actual = test.getHamletData();
String expect = "Change Hamlet to Leon. Change Hamlet to Leon.\n" +
"Change Tariq to Tariq. Change Tariq to Tariq.\n";
Assert.assertEquals(expect, actual);
}

@Test
public void testChangeHoratioToTariqAndHamletToLeon() {
String inputLine = "Change Horatio to Tariq. Change Horatio to Tariq.";
String[] searchStr = {"Hamlet", "Horatio"};
String[] repStr = {"Leon", "Tariq"};
String fileName = "test.txt";
HamletParser test = new HamletParser(fileName, searchStr, repStr);
String actual = test.getHamletData();
String expect = "Change Leon to Leon. Change Leon to Leon.\n" +
"Change Tariq to Tariq. Change Tariq to Tariq.\n";
Assert.assertEquals(expect, actual);
}


@Test
public void testFindHoratio() {
String[] searchStr = {"Hamlet", "Horatio"};
String[] repStr = {"Leon", "Tariq"};
String fileName = "hamlet.txt";
HamletParser test = new HamletParser(fileName, searchStr, repStr);
Boolean actual = test.getHamletData().contains("Horatio");
Assert.assertEquals(false, actual);
}

@Test
public void testFindHamlet() {
String[] searchStr = {"Hamlet", "Horatio"};
String[] repStr = {"Leon", "Tariq"};
String fileName = "hamlet.txt";
HamletParser test = new HamletParser(fileName, searchStr, repStr);
Boolean actual = test.getHamletData().contains("Hamlet");
Assert.assertEquals(false, actual);
}
}
}


Loading