forked from iluwatar/30-seconds-of-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryTest.java
More file actions
294 lines (277 loc) · 9.91 KB
/
LibraryTest.java
File metadata and controls
294 lines (277 loc) · 9.91 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
* MIT License
*
* Copyright (c) 2017-2018 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import org.junit.Test;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import static org.junit.Assert.*;
/*
* Java Snippets tests
*
*/
public class LibraryTest {
/**
* Tests for {@link Library#arrayConcat(Object[], Object[])}
*/
@Test
public void testArrayConcat() {
Integer[] integers = Library.arrayConcat(new Integer[5], new Integer[5]);
assertEquals(integers.length, 10);
String[] strings = Library.arrayConcat(new String[0], new String[0]);
assertEquals(strings.length, 0);
try {
Double[] doubles = Library.arrayConcat(null, null);
fail();
} catch (NullPointerException e) {
// expected behaviour, everything is fine
}
}
/**
* Tests for {@link Library#nArrayConcat(Object[], Object[][])}
*/
@Test
public void testNArrayConcat() {
Integer[] single = Library.nArrayConcat(new Integer[1]);
assertEquals(single.length, 1);
String[] multiple = Library.nArrayConcat(new String[5], new String[12], new String[3], new String[8]);
assertEquals(multiple.length, 28);
try {
Double[] doubles = Library.nArrayConcat(null, null, null, null);
fail();
} catch (NullPointerException e) {
// expected behaviour, everything is fine
}
}
/**
* Tests for {@link Library#fibonacci(int)}
*/
@Test
public void testFibonacci() {
assertEquals(0, Library.fibonacci(0));
assertEquals(1, Library.fibonacci(1));
assertEquals(1, Library.fibonacci(2));
assertEquals(2, Library.fibonacci(3));
assertEquals(3, Library.fibonacci(4));
assertEquals(5, Library.fibonacci(5));
assertEquals(8, Library.fibonacci(6));
assertEquals(13, Library.fibonacci(7));
assertEquals(21, Library.fibonacci(8));
assertEquals(34, Library.fibonacci(9));
assertEquals(55, Library.fibonacci(10));
}
/**
* Tests for {@link Library#factorial(int)}
*/
@Test
public void testFactorial() {
assertEquals(1, Library.factorial(0));
assertEquals(1, Library.factorial(1));
assertEquals(2, Library.factorial(2));
assertEquals(6, Library.factorial(3));
assertEquals(24, Library.factorial(4));
assertEquals(120, Library.factorial(5));
assertEquals(720, Library.factorial(6));
assertEquals(5040, Library.factorial(7));
assertEquals(40320, Library.factorial(8));
assertEquals(362880, Library.factorial(9));
assertEquals(3628800, Library.factorial(10));
}
/**
* Tests for {@link Library#reverseString(String)}
*/
@Test
public void testReverseString() {
assertEquals("oof", Library.reverseString("foo"));
assertEquals("ÖÄÅ321FED cba", Library.reverseString("abc DEF123ÅÄÖ"));
}
/**
* Tests for {@link Library#readLines(String)}
* @throws IOException
*/
@Test
public void testReadLines() throws IOException {
List<String> somelines = Library.readLines("src/test/resources/somelines.txt");
assertEquals(3, somelines.size());
assertEquals("foo", somelines.get(0));
assertEquals("bar", somelines.get(1));
assertEquals("baz", somelines.get(2));
try {
Library.readLines("some/nonexistent/filename.txt");
fail();
} catch (IOException e) {
// catched the expected exception
}
}
/**
* Tests for {@link Library#captureScreen(String)}
*/
@Test
public void testCaptureScreen() throws IOException, AWTException {
final String filename = "src/test/resources/screenshot.png";
try {
Library.captureScreen(filename);
File f = new File(filename);
assertTrue(f.exists() && !f.isDirectory());
} catch (HeadlessException e) {
// the test runs on computer without a screen, it is ok to fail
} finally {
Files.deleteIfExists(new File(filename).toPath());
}
}
/**
* Tests for {@link Library#stringToDate(String, String)}
*/
@Test
public void testStringToDate() throws ParseException {
Calendar calendar = Calendar.getInstance();
calendar.setTime(Library.stringToDate("2017-08-18", "yyyy-MM-dd"));
assertEquals(2017, calendar.get(Calendar.YEAR));
assertEquals(8, calendar.get(Calendar.MONTH) + 1);
assertEquals(18, calendar.get(Calendar.DAY_OF_MONTH));
}
/**
* Tests for {@link Library#listDirectories(String)}
*/
@Test
public void testListDirectories() {
File[] files = Library.listDirectories("src/test/resources");
Arrays.stream(files).allMatch(f -> f.isDirectory());
assertTrue(Arrays.stream(files).anyMatch(new File("src/test/resources/dir1")::equals));
assertTrue(Arrays.stream(files).anyMatch(new File("src/test/resources/dir2")::equals));
}
/**
* Tests for {@link Library#isPalindrome(String)}
*/
@Test
public void testIsPalindrome() {
assertTrue(Library.isPalindrome("saippuakauppias"));
assertTrue(Library.isPalindrome("111 Saippua - Kauppias 321"));
assertFalse(Library.isPalindrome("Type O Negative"));
assertFalse(Library.isPalindrome("Foo12121Bar"));
}
/**
* Tests for {@link Library#listFilesInDirectory(File)}
*/
@Test
public void testListFilesInDirectory() {
File[] files = Library.listFilesInDirectory(new File("src/test/resources"));
assertEquals(2, files.length);
assertEquals("src/test/resources/somelines.txt", files[0].toString());
assertEquals("src/test/resources/someotherlines.txt", files[1].toString());
}
/**
* Tests for {@link Library#listAllFiles(String)}
*/
@Test
public void testListAllFiles() {
List<File> files = Library.listAllFiles("src/test/resources");
assertEquals(4, files.size());
}
/**
* Tests for {@link Library#performLottery(int, int)}
*/
@Test
public void testPerformLottery() {
Integer[] numbers0 = Library.performLottery(0, 0);
assertArrayEquals(new Integer[]{}, numbers0);
Integer[] numbers1 = Library.performLottery(1, 1);
assertArrayEquals(new Integer[]{1}, numbers1);
Integer[] numbers2 = Library.performLottery(2, 2);
assertEquals(2, numbers2.length);
assertTrue(numbers2[0] == 1 || numbers2[0] == 2);
assertTrue(numbers2[1] == 1 || numbers2[1] == 2);
}
/**
* Tests for {@link Library#zipFile(String, String)}
*/
@Test
public void testZipFile() throws IOException {
final String src = "src/test/resources/somelines.txt";
final String dst = "src/test/resources/somelines.zip";
try {
Library.zipFile(src, dst);
assertTrue(Files.exists(Paths.get(dst)));
} finally {
Files.deleteIfExists(new File(dst).toPath());
}
}
/**
* Tests for {@link Library#zipFiles(String[], String)}
*/
@Test
public void testZipFiles() throws IOException {
final String[] srcFilenames = {"src/test/resources/somelines.txt", "src/test/resources/someotherlines.txt"};
final String dst = "src/test/resources/multiple.zip";
try {
Library.zipFiles(srcFilenames, dst);
assertTrue(Files.exists(Paths.get(dst)));
} finally {
Files.deleteIfExists(new File(dst).toPath());
}
}
/**
* Tests for {@link Library#quickSort(int[], int, int)}
*/
@Test
public void testQuickSort() {
int[] arr = new int[] {7, 13, 3, 1, 8, 5};
Library.quickSort(arr, 0, arr.length - 1);
assertEquals(arr.length, 6);
assertEquals(arr[0], 1);
assertEquals(arr[1], 3);
assertEquals(arr[2], 5);
assertEquals(arr[3], 7);
assertEquals(arr[4], 8);
assertEquals(arr[5], 13);
}
/**
* Tests for {@link Library#httpGet(URL)}
*/
@Test
public void testHttpGet() throws IOException {
int responseCode = Library.httpGet(new URL("http://www.google.com"));
assertEquals(200, responseCode);
}
/**
* Tests for {@link Library#isPrime(int)}
*/
@Test
public void testIsPrime() {
assertTrue(Library.isPrime(2));
assertTrue(Library.isPrime(3));
assertTrue(Library.isPrime(17));
assertTrue(Library.isPrime(97));
assertFalse(Library.isPrime(4));
assertFalse(Library.isPrime(100));
}
}