Quellcode durchsuchen

finished commit

Joshua Chung vor 6 Jahren
Ursprung
Commit
8da1cd5172
4 geänderte Dateien mit 5422 neuen und 1 gelöschten Zeilen
  1. 12
    0
      pom.xml
  2. 11
    1
      src/main/java/HamletParser.java
  3. 16
    0
      src/test/java/HamletParserTest.java
  4. 5383
    0
      target/classes/hamlet.txt

+ 12
- 0
pom.xml Datei anzeigen

@@ -7,6 +7,18 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>regex</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>7</source>
17
+                    <target>7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
 
11 23
     <dependencies>
12 24
         <dependency>

+ 11
- 1
src/main/java/HamletParser.java Datei anzeigen

@@ -1,6 +1,7 @@
1 1
 import java.io.File;
2 2
 import java.io.IOException;
3 3
 import java.util.Scanner;
4
+import java.util.regex.Pattern;
4 5
 
5 6
 /**
6 7
  * Created by thook on 10/7/15.
@@ -16,7 +17,7 @@ public class HamletParser {
16 17
     private String loadFile(){
17 18
         ClassLoader classLoader = getClass().getClassLoader();
18 19
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19
-        StringBuilder result = new StringBuilder("");
20
+        StringBuilder result = new StringBuilder();
20 21
 
21 22
         try(Scanner scanner = new Scanner(file)){
22 23
             while(scanner.hasNextLine()){
@@ -36,4 +37,13 @@ public class HamletParser {
36 37
         return hamletData;
37 38
     }
38 39
 
40
+    public String changeHamletToLeon(String hamlet) {
41
+        hamlet = getHamletData();
42
+        return hamlet.replaceAll( "(?i:.*HAMLET*)", "Leon");
43
+    }
44
+
45
+    public String changeHoratioToTariq(String hamlet) {
46
+        hamlet = getHamletData();
47
+        return hamlet.replaceAll("(?i:.*HORATIO*)", "Tariq");
48
+    }
39 49
 }

+ 16
- 0
src/test/java/HamletParserTest.java Datei anzeigen

@@ -1,6 +1,10 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
2 3
 import org.junit.Test;
3 4
 
5
+import java.util.regex.Matcher;
6
+import java.util.regex.Pattern;
7
+
4 8
 import static org.junit.Assert.*;
5 9
 
6 10
 public class HamletParserTest {
@@ -15,17 +19,29 @@ public class HamletParserTest {
15 19
 
16 20
     @Test
17 21
     public void testChangeHamletToLeon() {
22
+        String expected = hamletParser.changeHamletToLeon(hamletText);
23
+        Assert.assertTrue(!expected.equals(hamletText));
18 24
     }
19 25
 
20 26
     @Test
21 27
     public void testChangeHoratioToTariq() {
28
+        String expected = hamletParser.changeHoratioToTariq(hamletText);
29
+        Assert.assertTrue(!expected.equals(hamletText));
22 30
     }
23 31
 
24 32
     @Test
25 33
     public void testFindHoratio() {
34
+        Pattern p = Pattern.compile("(i:.*HORATIO*)");
35
+        Matcher m = p.matcher(hamletParser.changeHoratioToTariq(hamletText));
36
+        boolean b = m.matches();
37
+        Assert.assertFalse(b);
26 38
     }
27 39
 
28 40
     @Test
29 41
     public void testFindHamlet() {
42
+        Pattern p = Pattern.compile("(i:.*HAMLET*)");
43
+        Matcher m = p.matcher(hamletParser.changeHamletToLeon(hamletText));
44
+        boolean b = m.matches();
45
+        Assert.assertFalse(b);
30 46
     }
31 47
 }

+ 5383
- 0
target/classes/hamlet.txt
Datei-Diff unterdrückt, da er zu groß ist
Datei anzeigen