-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathTextAnalyzerTest.java
More file actions
206 lines (167 loc) · 7.96 KB
/
TextAnalyzerTest.java
File metadata and controls
206 lines (167 loc) · 7.96 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.epam.izh.rd.online;
import com.epam.izh.rd.online.helper.Direction;
import com.epam.izh.rd.online.helper.FileReaderService;
import com.epam.izh.rd.online.helper.IFileReaderService;
import com.epam.izh.rd.online.service.SimpleTextStatisticsAnalyzer;
import com.epam.izh.rd.online.service.StreamApiTextStatisticsAnalyzer;
import com.epam.izh.rd.online.service.TextStatisticsAnalyzer;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
public class TextAnalyzerTest {
private static final int SUM_LENGTH_OF_WORDS = 2119;
private static final int COUNT_OF_WORDS = 503;
private static final int COUNT_OF_UNIQUE_WORDS = 265;
private static List<String> wordsList;
private static TextStatisticsAnalyzer simpleTextStatisticsAnalyzer;
private static TextStatisticsAnalyzer streamApiTextStatisticsAnalyzer;
private static String text;
private static Properties properties;
@BeforeAll
static void setup() {
IFileReaderService fileReaderService = new FileReaderService();
simpleTextStatisticsAnalyzer = new SimpleTextStatisticsAnalyzer();
streamApiTextStatisticsAnalyzer = new StreamApiTextStatisticsAnalyzer();
try {
text = fileReaderService.readFromFileToString("sample-text.txt");
properties = fileReaderService.loadProperties("words-statistics.properties");
wordsList = readWordsFromProperties();
} catch (Exception e) {
throw new NullPointerException(e.getMessage());
}
}
@Test
@DisplayName("Тест метода TextStatisticsAnalyzer.countSumLengthOfWords(String text)")
void testCountSumLengthOfWords() {
assertEquals(SUM_LENGTH_OF_WORDS, simpleTextStatisticsAnalyzer.countSumLengthOfWords(text));
}
@Test
@DisplayName("Тест метода TextStatisticsAnalyzer.countNumberOfWords(String text)")
void testCountNumberOfWords() {
assertEquals(COUNT_OF_WORDS, simpleTextStatisticsAnalyzer.countNumberOfWords(text));
}
@Test
@DisplayName("Тест метода TextStatisticsAnalyzer.countNumberOfUniqueWords(String text)")
void testCountNumberOfUniqueWords() {
assertEquals(COUNT_OF_UNIQUE_WORDS, simpleTextStatisticsAnalyzer.countNumberOfUniqueWords(text));
}
@Test
@DisplayName("Тест метода TextStatisticsAnalyzer.sortWordsByLength(String text, ASC)")
void testSortWordsByLength() {
assertIsListSorted(simpleTextStatisticsAnalyzer.sortWordsByLength(text, Direction.ASC),Direction.ASC);
}
@Test
@DisplayName("Тест метода TextStatisticsAnalyzer.sortWordsByLength(String text, DESC)")
void testSortWordsByLengthDesc() {
assertIsListSorted(simpleTextStatisticsAnalyzer.sortWordsByLength(text, Direction.DESC),Direction.DESC);
}
@Test
@DisplayName("Тест метода TextStatisticsAnalyzer.getWords(String text)")
void testGetWords() throws IOException, URISyntaxException {
assertListsContainSameElements(wordsList, simpleTextStatisticsAnalyzer.getWords(text));
}
@Test
@DisplayName("Тест метода TextStatisticsAnalyzer.getUniqueWords(String text)")
void testGetUniqueWords() {
assertListsContainSameElements(wordsList.stream().distinct().collect(Collectors.toList()), simpleTextStatisticsAnalyzer.getUniqueWords(text));
}
@Test
@DisplayName("Тест метода TextStatisticsAnalyzer.countNumberOfWordsRepetitions(String text)")
void testCountNumberOfWordsRepetitions() {
assertRepetitions(simpleTextStatisticsAnalyzer.countNumberOfWordsRepetitions(text));
}
@Test
@DisplayName("Тест метода StreamApiTextStatisticsAnalyzer.countSumLengthOfWords(String text)")
void testCountSumLengthOfWordsStream() {
assertEquals(SUM_LENGTH_OF_WORDS, streamApiTextStatisticsAnalyzer.countSumLengthOfWords(text));
}
@Test
@DisplayName("Тест метода StreamApiTextStatisticsAnalyzer.countNumberOfWords(String text)")
void testCountNumberOfWordsStream() {
assertEquals(COUNT_OF_WORDS, streamApiTextStatisticsAnalyzer.countNumberOfWords(text));
}
@Test
@DisplayName("Тест метода StreamApiTextStatisticsAnalyzer.countNumberOfUniqueWords(String text)")
void testCountNumberOfUniqueWordsStream() {
assertEquals(COUNT_OF_UNIQUE_WORDS, streamApiTextStatisticsAnalyzer.countNumberOfUniqueWords(text));
}
@Test
@DisplayName("Тест метода StreamApiTextStatisticsAnalyzer.sortWordsByLength(String text, ASC)")
void testSortWordsByLengthStream() {
assertIsListSorted(streamApiTextStatisticsAnalyzer.sortWordsByLength(text, Direction.ASC),Direction.ASC);
}
@Test
@DisplayName("Тест метода StreamApiTextStatisticsAnalyzer.sortWordsByLength(String text, DESC)")
void testSortWordsByLengthDescStream() {
assertIsListSorted(streamApiTextStatisticsAnalyzer.sortWordsByLength(text, Direction.DESC),Direction.DESC);
}
@Test
@DisplayName("Тест метода StreamApiTextStatisticsAnalyzer.getWords(String text)")
void testGetWordsStream() throws IOException, URISyntaxException {
assertListsContainSameElements(wordsList, streamApiTextStatisticsAnalyzer.getWords(text));
}
@Test
@DisplayName("Тест метода StreamApiTextStatisticsAnalyzer.getUniqueWords(String text)")
void testGetUniqueWordsStream() {
assertListsContainSameElements(wordsList.stream().distinct().collect(Collectors.toList()), streamApiTextStatisticsAnalyzer.getUniqueWords(text));
}
@Test
@DisplayName("Тест метода StreamApiTextStatisticsAnalyzer.countNumberOfWordsRepetitions(String text)")
void testCountNumberOfWordsRepetitionsStream() {
assertRepetitions(streamApiTextStatisticsAnalyzer.countNumberOfWordsRepetitions(text));
}
private static List<String> readWordsFromProperties() {
List<String> result = new ArrayList<>();
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
int wordRepetition = Integer.valueOf(entry.getValue().toString());
String word = entry.getKey().toString();
if (wordRepetition == 1) {
result.add(word);
} else {
IntStream.rangeClosed(1, wordRepetition).forEach(a -> result.add(word));
}
}
return result;
}
private void assertListsContainSameElements(Collection<?> expected, Collection<?> actual) {
if (expected.size() != actual.size() || !actual.containsAll(expected)) {
fail();
}
}
private void assertRepetitions(Map<String, Integer> result) {
if (result.isEmpty()) {
fail();
}
for (Map.Entry<String, Integer> e : result.entrySet()) {
boolean wordMatchWithProperty = Integer.valueOf(properties.getProperty(e.getKey())).equals(e.getValue());
if (!wordMatchWithProperty) {
fail();
}
}
}
private void assertIsListSorted(List<String> list, Direction direction) {
Comparator<String> stringComparator = getStringComparator(direction);
if (list.isEmpty()) {
fail();
}
for(int i = 0; i < list.size() - 1; i++) {
String cur = list.get(i);
String next = list.get(i+1);
if (stringComparator.compare(cur, next) > 0) {
fail();
}
}
}
private Comparator<String> getStringComparator(Direction direction) {
return direction.equals(Direction.ASC) ?
Comparator.comparing(String::length) :
Comparator.comparing(String::length).reversed();
}
}