Kate Moore 6 年前
父节点
当前提交
dde2c857f0
共有 3 个文件被更改,包括 5434 次插入2 次删除
  1. 31
    0
      src/main/java/HamletParser.java
  2. 20
    2
      src/test/java/HamletParserTest.java
  3. 5383
    0
      target/classes/hamlet.txt

+ 31
- 0
src/main/java/HamletParser.java 查看文件

@@ -1,6 +1,8 @@
1 1
 import java.io.File;
2 2
 import java.io.IOException;
3 3
 import java.util.Scanner;
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
4 6
 
5 7
 /**
6 8
  * Created by thook on 10/7/15.
@@ -8,6 +10,8 @@ import java.util.Scanner;
8 10
 public class HamletParser {
9 11
 
10 12
     private String hamletData;
13
+    private Pattern p;
14
+    private Matcher m;
11 15
 
12 16
     public HamletParser(){
13 17
         this.hamletData = loadFile();
@@ -36,4 +40,31 @@ public class HamletParser {
36 40
         return hamletData;
37 41
     }
38 42
 
43
+
44
+    public void changeHamletToLeon(){
45
+        p = Pattern.compile("hamlet", Pattern.CASE_INSENSITIVE);
46
+        m = p.matcher(hamletData);
47
+        hamletData = m.replaceAll("Leon");
48
+    }
49
+
50
+    public void changeHoratioToTariq(){
51
+        p = Pattern.compile("horatio", Pattern.CASE_INSENSITIVE);
52
+        m = p.matcher(hamletData);
53
+        hamletData = m.replaceAll("Tariq");
54
+    }
55
+
56
+    public boolean findHamlet(){
57
+        p = Pattern.compile("hamlet", Pattern.CASE_INSENSITIVE);
58
+        m = p.matcher(hamletData);
59
+        return m.find();
60
+    }
61
+
62
+    public boolean findHoratio(){
63
+        p = Pattern.compile("horatio", Pattern.CASE_INSENSITIVE);
64
+        m = p.matcher(hamletData);
65
+        return m.find();
66
+
67
+    }
68
+
69
+
39 70
 }

+ 20
- 2
src/test/java/HamletParserTest.java 查看文件

@@ -1,31 +1,49 @@
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 {
7
-    private String hamletText;
11
+
8 12
     private HamletParser hamletParser;
9 13
 
10 14
     @Before
11 15
     public void setUp() {
12 16
         this.hamletParser = new HamletParser();
13
-        this.hamletText = hamletParser.getHamletData();
14 17
     }
15 18
 
16 19
     @Test
17 20
     public void testChangeHamletToLeon() {
21
+       hamletParser.changeHamletToLeon();
22
+       boolean expected = false;
23
+       boolean actual = hamletParser.findHamlet();
24
+       Assert.assertEquals(expected, actual);
25
+
18 26
     }
19 27
 
20 28
     @Test
21 29
     public void testChangeHoratioToTariq() {
30
+        hamletParser.changeHoratioToTariq();
31
+        boolean expected = false;
32
+        boolean actual = hamletParser.findHoratio();
33
+        Assert.assertEquals(expected, actual);
22 34
     }
23 35
 
24 36
     @Test
25 37
     public void testFindHoratio() {
38
+        boolean expected = hamletParser.getHamletData().toLowerCase().contains("horatio");
39
+        boolean actual = hamletParser.findHoratio();
40
+        Assert.assertEquals(expected, actual);
26 41
     }
27 42
 
28 43
     @Test
29 44
     public void testFindHamlet() {
45
+        boolean expected = hamletParser.getHamletData().toLowerCase().contains("hamlet");
46
+        boolean actual = hamletParser.findHamlet();
47
+        Assert.assertEquals(expected, actual);
30 48
     }
31 49
 }

+ 5383
- 0
target/classes/hamlet.txt
文件差异内容过多而无法显示
查看文件