-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextAnalyzerTest_Success.java
More file actions
68 lines (59 loc) · 2.44 KB
/
TextAnalyzerTest_Success.java
File metadata and controls
68 lines (59 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package application;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
public class TextAnalyzerTest_Success {
private TextAnalyzerUI textAnalyzer;
@BeforeEach
public void setup() {
textAnalyzer = new TextAnalyzerUI();
}
@Test
public void testEmptyFile() throws IOException {
File file = createFile("");
Map<String, Integer> wordFrequencies = textAnalyzer.countWordOccurrences(file);
Assertions.assertTrue(wordFrequencies.isEmpty());
file.delete();
}
@Test
public void testSingleWord() throws IOException {
File file = createFile("hello");
Map<String, Integer> wordFrequencies = textAnalyzer.countWordOccurrences(file);
Assertions.assertEquals(1, wordFrequencies.size());
Assertions.assertEquals(1, wordFrequencies.get("hello"));
file.delete();
}
@Test
public void testMultipleWords() throws IOException {
File file = createFile("hello world hello");
Map<String, Integer> wordFrequencies = textAnalyzer.countWordOccurrences(file);
Assertions.assertEquals(2, wordFrequencies.size());
Assertions.assertEquals(2, wordFrequencies.get("hello"));
Assertions.assertEquals(1, wordFrequencies.get("world"));
file.delete();
}
@Test
public void testPunctuationAndWhitespace() throws IOException {
File file = createFile("Hello world! How are you? Hello world!");
Map<String, Integer> wordFrequencies = textAnalyzer.countWordOccurrences(file);
Assertions.assertEquals(5, wordFrequencies.size());
Assertions.assertEquals(2, wordFrequencies.get("Hello"));
Assertions.assertEquals(2, wordFrequencies.get("world!"));
Assertions.assertEquals(1, wordFrequencies.get("How"));
Assertions.assertEquals(1, wordFrequencies.get("are"));
Assertions.assertEquals(1, wordFrequencies.get("you?"));
file.delete();
}
private File createFile(String content) throws IOException {
File file = File.createTempFile("text_", ".txt");
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(content);
}
return file;
}
}