Bladeren bron

build Test for Load File

Nick Satinover 6 jaren geleden
bovenliggende
commit
3b75194a3e
3 gewijzigde bestanden met toevoegingen van 108 en 15 verwijderingen
  1. 58
    12
      src/main/java/HamletParser.java
  2. 0
    0
      src/main/resources/output.txt
  3. 50
    3
      src/test/java/HamletParserTest.java

+ 58
- 12
src/main/java/HamletParser.java Bestand weergeven

@@ -1,15 +1,15 @@
1 1
 import java.io.File;
2 2
 import java.io.IOException;
3
-import java.util.ArrayList;
4 3
 import java.util.Scanner;
5
-
4
+import java.lang.*;
5
+import java.util.*;
6 6
 /**
7 7
  * Created by thook on 10/7/15.
8 8
  */
9 9
 public class HamletParser {
10 10
 
11 11
     private String hamletData;
12
-    String[] wordArr;
12
+    private Formatter formatter;
13 13
 
14 14
     public HamletParser(){
15 15
         this.hamletData = loadFile();
@@ -19,24 +19,33 @@ public class HamletParser {
19 19
         ClassLoader classLoader = getClass().getClassLoader();
20 20
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
21 21
         StringBuilder result = new StringBuilder("");
22
+        StringBuilder lineResult;
23
+        String[] wordArr;
22 24
 
23 25
         try(Scanner scanner = new Scanner(file)){
24 26
             while(scanner.hasNextLine()){
25 27
                 String line = scanner.nextLine();
26
-                if (isHamletOrHoratio(line)){
27
-                    wordArr = line.split(" ");
28
+                wordArr = line.split(" ");
29
+                lineResult = new StringBuilder("");
30
+                for (String s: wordArr) {
31
+                    if (findHamlet(s)) {
32
+                        lineResult.append(changeHamletToLeon()).append(" ");
33
+                    } else if (findHoratio(s)) {
34
+                        lineResult.append(changeChangeHoratioToTariq()).append(" ");
35
+                    } else {
36
+                        lineResult.append(s).append(" ");
37
+                    }
28 38
                 }
29
-                //loop through line for next
30
-                    //if next matches Hamlet, replace with Leon
31
-                    //if next matches Horatio, replace with Tariq
32
-                result.append(line).append("\n");
39
+                result.append(lineResult.toString().trim());
33 40
             }
34 41
 
35 42
             scanner.close();
36 43
         }catch(IOException e){
37 44
             e.printStackTrace();
38 45
         }
39
-
46
+        openOutFile();
47
+        addText(result.toString());
48
+        closeOutFile();
40 49
         return result.toString();
41 50
     }
42 51
 
@@ -44,12 +53,49 @@ public class HamletParser {
44 53
         return hamletData;
45 54
     }
46 55
 
47
-    public boolean isHamletOrHoratio(String line){
48
-        if (line.toLowerCase().matches("hamlet") || line.toLowerCase().matches("horatio")){
56
+    public boolean findHamlet(String s){
57
+        if (s.toLowerCase().matches("hamlet")){
58
+            return true;
59
+        } else {
60
+            return false;
61
+        }
62
+    }
63
+
64
+    public boolean findHoratio(String s){
65
+        if (s.toLowerCase().matches("horatio")){
49 66
             return true;
50 67
         } else {
51 68
             return false;
52 69
         }
53 70
     }
54 71
 
72
+    public String changeHamletToLeon(){
73
+        return "Leon";
74
+    }
75
+
76
+    public String changeChangeHoratioToTariq(){
77
+        return "Tariq";
78
+    }
79
+
80
+    /**
81
+     * Methods to print out to a new file
82
+     */
83
+    public void openOutFile(){
84
+        try {
85
+            formatter = new Formatter("output.txt");
86
+        }
87
+        catch (Exception e){
88
+            System.out.println("Error writing to file.");
89
+        }
90
+    }
91
+
92
+    public String addText(String s){
93
+        formatter.format("%s", s);
94
+        return loadFile();
95
+    }
96
+
97
+    public void closeOutFile(){
98
+        formatter.close();
99
+    }
100
+
55 101
 }

+ 0
- 0
src/main/resources/output.txt Bestand weergeven


+ 50
- 3
src/test/java/HamletParserTest.java Bestand weergeven

@@ -16,20 +16,67 @@ public class HamletParserTest {
16 16
 
17 17
     @Test
18 18
     public void testChangeHamletToLeon() {
19
-        //set to true to fail, change to false
20
-        Assert.assertTrue(hamletText.toLowerCase().contains("horatio"));
21
-        Assert.assertTrue(hamletText.toLowerCase().contains("hamlet"));
19
+        //GIVEN
20
+
21
+        //WHEN
22
+        String expected = "Leon";
23
+        //THEN
24
+        String actual = hamletParser.changeHamletToLeon();
25
+        Assert.assertEquals(expected, actual);
22 26
     }
23 27
 
24 28
     @Test
25 29
     public void testChangeHoratioToTariq() {
30
+        //GIVEN
31
+
32
+        //WHEN
33
+        String expected = "Tariq";
34
+        //THEN
35
+        String actual = hamletParser.changeChangeHoratioToTariq();
36
+        Assert.assertEquals(expected, actual);
26 37
     }
27 38
 
28 39
     @Test
29 40
     public void testFindHoratio() {
41
+        //GIVEN
42
+        String input = "horatio";
43
+        //WHEN
44
+        boolean expected = true;
45
+        //THEN
46
+        boolean actual = hamletParser.findHoratio(input);
47
+        Assert.assertEquals(expected, actual);
30 48
     }
31 49
 
32 50
     @Test
33 51
     public void testFindHamlet() {
52
+        //GIVEN
53
+        String input = "HAMLET";
54
+        //WHEN
55
+        boolean expected = true;
56
+        //THEN
57
+        boolean actual = hamletParser.findHamlet(input);
58
+        Assert.assertEquals(expected, actual);
59
+    }
60
+
61
+    @Test
62
+    public void testFindHoratio2() {
63
+        //GIVEN
64
+        String input = "nope";
65
+        //WHEN
66
+        boolean expected = false;
67
+        //THEN
68
+        boolean actual = hamletParser.findHoratio(input);
69
+        Assert.assertEquals(expected, actual);
70
+    }
71
+
72
+    @Test
73
+    public void testFindHamlet2() {
74
+        //GIVEN
75
+        String input = "hhamlet";
76
+        //WHEN
77
+        boolean expected = false;
78
+        //THEN
79
+        boolean actual = hamletParser.findHamlet(input);
80
+        Assert.assertEquals(expected, actual);
34 81
     }
35 82
 }