Connor Dunnigan hace 6 años
padre
commit
e8897527fd
Se han modificado 3 ficheros con 5418 adiciones y 5 borrados
  1. 25
    5
      src/main/java/HamletParser.java
  2. 10
    0
      src/test/java/HamletParserTest.java
  3. 5383
    0
      target/classes/hamlet.txt

+ 25
- 5
src/main/java/HamletParser.java Ver fichero

1
 import java.io.File;
1
 import java.io.File;
2
 import java.io.IOException;
2
 import java.io.IOException;
3
 import java.util.Scanner;
3
 import java.util.Scanner;
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
4
 
6
 
5
 /**
7
 /**
6
  * Created by thook on 10/7/15.
8
  * Created by thook on 10/7/15.
16
     private String loadFile(){
18
     private String loadFile(){
17
         ClassLoader classLoader = getClass().getClassLoader();
19
         ClassLoader classLoader = getClass().getClassLoader();
18
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
20
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19
-        StringBuilder result = new StringBuilder("");
21
+        StringBuilder result = new StringBuilder();
20
 
22
 
21
         try(Scanner scanner = new Scanner(file)){
23
         try(Scanner scanner = new Scanner(file)){
22
             while(scanner.hasNextLine()){
24
             while(scanner.hasNextLine()){
23
                 String line = scanner.nextLine();
25
                 String line = scanner.nextLine();
24
                 result.append(line).append("\n");
26
                 result.append(line).append("\n");
25
             }
27
             }
26
-
27
-            scanner.close();
28
-        }catch(IOException e){
28
+        } catch(IOException e){
29
             e.printStackTrace();
29
             e.printStackTrace();
30
         }
30
         }
31
-
32
         return result.toString();
31
         return result.toString();
33
     }
32
     }
34
 
33
 
36
         return hamletData;
35
         return hamletData;
37
     }
36
     }
38
 
37
 
38
+    public void changeHamletToLeon() {
39
+        Pattern hamPattern = Pattern.compile("Hamlet", Pattern.CASE_INSENSITIVE);
40
+        Matcher hamMatcher = hamPattern.matcher(hamletData);
41
+        hamletData = hamMatcher.replaceAll("Leon");
42
+    }
43
+    public void changeHoratioToTariq() {
44
+        Pattern horPattern = Pattern.compile("Horatio", Pattern.CASE_INSENSITIVE);
45
+        Matcher horMatcher = horPattern.matcher(hamletData);
46
+        hamletData = horMatcher.replaceAll("Tariq");
47
+    }
48
+
49
+    public boolean findHoratio() {
50
+        Pattern horPattern = Pattern.compile("Horatio", Pattern.CASE_INSENSITIVE);
51
+        Matcher horMatcher = horPattern.matcher(hamletData);
52
+        return horMatcher.find();
53
+    }
54
+    public boolean findHamlet() {
55
+        Pattern hamPattern = Pattern.compile("Hamlet", Pattern.CASE_INSENSITIVE);
56
+        Matcher hamMatcher = hamPattern.matcher(hamletData);
57
+        return hamMatcher.find();
58
+    }
39
 }
59
 }

+ 10
- 0
src/test/java/HamletParserTest.java Ver fichero

1
 import org.junit.Before;
1
 import org.junit.Before;
2
 import org.junit.Test;
2
 import org.junit.Test;
3
+import org.junit.Assert;
3
 
4
 
4
 import static org.junit.Assert.*;
5
 import static org.junit.Assert.*;
5
 
6
 
15
 
16
 
16
     @Test
17
     @Test
17
     public void testChangeHamletToLeon() {
18
     public void testChangeHamletToLeon() {
19
+        hamletParser.changeHamletToLeon();
20
+        Assert.assertFalse(hamletParser.findHamlet());
18
     }
21
     }
19
 
22
 
20
     @Test
23
     @Test
21
     public void testChangeHoratioToTariq() {
24
     public void testChangeHoratioToTariq() {
25
+        hamletParser.changeHoratioToTariq();
26
+        Assert.assertFalse(hamletParser.findHoratio());
27
+
22
     }
28
     }
23
 
29
 
24
     @Test
30
     @Test
25
     public void testFindHoratio() {
31
     public void testFindHoratio() {
32
+        boolean exp = hamletParser.findHoratio();
33
+        Assert.assertTrue(exp);
26
     }
34
     }
27
 
35
 
28
     @Test
36
     @Test
29
     public void testFindHamlet() {
37
     public void testFindHamlet() {
38
+        boolean exp = hamletParser.findHamlet();
39
+        Assert.assertTrue(exp);
30
     }
40
     }
31
 }
41
 }

+ 5383
- 0
target/classes/hamlet.txt
La diferencia del archivo ha sido suprimido porque es demasiado grande
Ver fichero