Skip to content

Commit e88fe8b

Browse files
Egima profilepivovarit
authored andcommitted
standardized exception based tests (eugenp#790)
* made changes to java reflection * removed redundant method makeSound in Animal abstract class * added project for play-framework article * added project for regex * changed regex project from own model to core-java * added project for routing in play * made changes to regex project * refactored code for REST API with Play project * refactored student store indexing to zero base * added unit tests, removed bad names * added NIO Selector project under core-java module * requested changes made * added project for nio2 * standardized exception based tests
1 parent 1858abf commit e88fe8b

5 files changed

Lines changed: 637 additions & 67 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.java.networking.udp;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.io.IOException;
6+
7+
import org.junit.After;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
public class UDPTest {
12+
EchoClient client = null;
13+
14+
@Before
15+
public void setup() throws IOException {
16+
new EchoServer().start();
17+
client = new EchoClient();
18+
}
19+
20+
@Test
21+
public void whenCanSendAndReceivePacket_thenCorrect1() {
22+
String echo = client.sendEcho("hello server");
23+
assertEquals("hello server", echo);
24+
echo = client.sendEcho("server is working");
25+
assertFalse(echo.equals("hello server"));
26+
}
27+
28+
@After
29+
public void tearDown() {
30+
client.sendEcho("end");
31+
client.close();
32+
}
33+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.baeldung.java.networking.url;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.net.MalformedURLException;
6+
import java.net.URL;
7+
8+
import org.junit.Test;
9+
10+
public class UrlTest {
11+
12+
@Test
13+
public void givenUrl_whenCanIdentifyProtocol_thenCorrect() throws MalformedURLException {
14+
URL url = new URL("http://baeldung.com");
15+
assertEquals("http", url.getProtocol());
16+
}
17+
18+
@Test
19+
public void givenUrl_whenCanGetHost_thenCorrect() throws MalformedURLException {
20+
URL url = new URL("http://baeldung.com");
21+
assertEquals("baeldung.com", url.getHost());
22+
}
23+
24+
@Test
25+
public void givenUrl_whenCanGetFileName_thenCorrect2() throws MalformedURLException {
26+
URL url = new URL("http://baeldung.com/articles?topic=java&version=8");
27+
assertEquals("/articles?topic=java&version=8", url.getFile());
28+
}
29+
30+
@Test
31+
public void givenUrl_whenCanGetFileName_thenCorrect1() throws MalformedURLException {
32+
URL url = new URL("http://baeldung.com/guidelines.txt");
33+
assertEquals("/guidelines.txt", url.getFile());
34+
}
35+
36+
@Test
37+
public void givenUrl_whenCanGetPathParams_thenCorrect() throws MalformedURLException {
38+
URL url = new URL("http://baeldung.com/articles?topic=java&version=8");
39+
assertEquals("/articles", url.getPath());
40+
}
41+
42+
@Test
43+
public void givenUrl_whenCanGetQueryParams_thenCorrect() throws MalformedURLException {
44+
URL url = new URL("http://baeldung.com/articles?topic=java");
45+
assertEquals("topic=java", url.getQuery());
46+
}
47+
48+
@Test
49+
public void givenUrl_whenGetsDefaultPort_thenCorrect() throws MalformedURLException {
50+
URL url = new URL("http://baeldung.com");
51+
assertEquals(-1, url.getPort());
52+
assertEquals(80, url.getDefaultPort());
53+
}
54+
55+
@Test
56+
public void givenUrl_whenGetsPort_thenCorrect() throws MalformedURLException {
57+
URL url = new URL("http://baeldung.com:8090");
58+
assertEquals(8090, url.getPort());
59+
assertEquals(80, url.getDefaultPort());
60+
}
61+
62+
@Test
63+
public void givenBaseUrl_whenCreatesRelativeUrl_thenCorrect() throws MalformedURLException {
64+
URL baseUrl = new URL("http://baeldung.com");
65+
URL relativeUrl = new URL(baseUrl, "a-guide-to-java-sockets");
66+
assertEquals("http://baeldung.com/a-guide-to-java-sockets", relativeUrl.toString());
67+
}
68+
69+
@Test
70+
public void givenAbsoluteUrl_whenIgnoresBaseUrl_thenCorrect() throws MalformedURLException {
71+
URL baseUrl = new URL("http://baeldung.com");
72+
URL relativeUrl = new URL(baseUrl, "http://baeldung.com/a-guide-to-java-sockets");
73+
assertEquals("http://baeldung.com/a-guide-to-java-sockets", relativeUrl.toString());
74+
}
75+
76+
@Test
77+
public void givenUrlComponents_whenConstructsCompleteUrl_thenCorrect() throws MalformedURLException {
78+
String protocol = "http";
79+
String host = "baeldung.com";
80+
String file = "/guidelines.txt";
81+
URL url = new URL(protocol, host, file);
82+
assertEquals("http://baeldung.com/guidelines.txt", url.toString());
83+
}
84+
85+
@Test
86+
public void givenUrlComponents_whenConstructsCompleteUrl_thenCorrect2() throws MalformedURLException {
87+
String protocol = "http";
88+
String host = "baeldung.com";
89+
String file = "/articles?topic=java&version=8";
90+
URL url = new URL(protocol, host, file);
91+
assertEquals("http://baeldung.com/guidelines.txt", url.toString());
92+
}
93+
94+
@Test
95+
public void givenUrlComponentsWithPort_whenConstructsCompleteUrl_thenCorrect() throws MalformedURLException {
96+
String protocol = "http";
97+
String host = "baeldung.com";
98+
int port = 9000;
99+
String file = "/guidelines.txt";
100+
URL url = new URL(protocol, host, port, file);
101+
assertEquals("http://baeldung.com:9000/guidelines.txt", url.toString());
102+
}
103+
104+
}
Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
package com.baeldung.java.nio2;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.io.IOException;
7+
import java.nio.file.CopyOption;
8+
import java.nio.file.DirectoryNotEmptyException;
9+
import java.nio.file.FileAlreadyExistsException;
10+
import java.nio.file.Files;
11+
import java.nio.file.NoSuchFileException;
12+
import java.nio.file.Path;
13+
import java.nio.file.Paths;
14+
import java.nio.file.StandardCopyOption;
15+
import java.util.Date;
16+
17+
import org.junit.Test;
18+
19+
public class FileTest {
20+
private static final String HOME = System.getProperty("user.home");
21+
22+
// checking file or dir
23+
@Test
24+
public void givenExistentPath_whenConfirmsFileExists_thenCorrect() {
25+
Path p = Paths.get(HOME);
26+
assertTrue(Files.exists(p));
27+
}
28+
29+
@Test
30+
public void givenNonexistentPath_whenConfirmsFileNotExists_thenCorrect() {
31+
Path p = Paths.get(HOME + "/inexistent_file.txt");
32+
assertTrue(Files.notExists(p));
33+
}
34+
35+
@Test
36+
public void givenExistentDirPath_whenConfirmsNotRegularFile_thenCorrect() {
37+
Path p = Paths.get(HOME);
38+
assertFalse(Files.isRegularFile(p));
39+
}
40+
41+
@Test
42+
public void givenExistentDirPath_whenConfirmsReadable_thenCorrect() {
43+
Path p = Paths.get(HOME);
44+
assertTrue(Files.isReadable(p));
45+
}
46+
47+
@Test
48+
public void givenExistentDirPath_whenConfirmsWritable_thenCorrect() {
49+
Path p = Paths.get(HOME);
50+
assertTrue(Files.isWritable(p));
51+
}
52+
53+
@Test
54+
public void givenExistentDirPath_whenConfirmsExecutable_thenCorrect() {
55+
Path p = Paths.get(HOME);
56+
assertTrue(Files.isExecutable(p));
57+
}
58+
59+
@Test
60+
public void givenSameFilePaths_whenConfirmsIsSame_thenCorrect() throws IOException {
61+
Path p1 = Paths.get(HOME);
62+
Path p2 = Paths.get(HOME);
63+
assertTrue(Files.isSameFile(p1, p2));
64+
}
65+
66+
// reading, writing and creating files
67+
// creating file
68+
@Test
69+
public void givenFilePath_whenCreatesNewFile_thenCorrect() throws IOException {
70+
String fileName = "myfile_" + new Date().getTime() + ".txt";
71+
Path p = Paths.get(HOME + "/" + fileName);
72+
assertFalse(Files.exists(p));
73+
Files.createFile(p);
74+
assertTrue(Files.exists(p));
75+
76+
}
77+
78+
@Test
79+
public void givenDirPath_whenCreatesNewDir_thenCorrect() throws IOException {
80+
String dirName = "myDir_" + new Date().getTime();
81+
Path p = Paths.get(HOME + "/" + dirName);
82+
assertFalse(Files.exists(p));
83+
Files.createDirectory(p);
84+
assertTrue(Files.exists(p));
85+
assertFalse(Files.isRegularFile(p));
86+
assertTrue(Files.isDirectory(p));
87+
88+
}
89+
90+
@Test(expected = NoSuchFileException.class)
91+
public void givenDirPath_whenFailsToCreateRecursively_thenCorrect() throws IOException {
92+
String dirName = "myDir_" + new Date().getTime() + "/subdir";
93+
Path p = Paths.get(HOME + "/" + dirName);
94+
assertFalse(Files.exists(p));
95+
Files.createDirectory(p);
96+
97+
}
98+
99+
@Test
100+
public void givenDirPath_whenCreatesRecursively_thenCorrect() throws IOException {
101+
Path dir = Paths.get(HOME + "/myDir_" + new Date().getTime());
102+
Path subdir = dir.resolve("subdir");
103+
assertFalse(Files.exists(dir));
104+
assertFalse(Files.exists(subdir));
105+
Files.createDirectories(subdir);
106+
assertTrue(Files.exists(dir));
107+
assertTrue(Files.exists(subdir));
108+
}
109+
110+
@Test
111+
public void givenFilePath_whenCreatesTempFile_thenCorrect() throws IOException {
112+
String prefix = "log_";
113+
String suffix = ".txt";
114+
Path p = Paths.get(HOME + "/");
115+
p = Files.createTempFile(p, prefix, suffix);
116+
// like log_8821081429012075286.txt
117+
assertTrue(Files.exists(p));
118+
119+
}
120+
121+
@Test
122+
public void givenFilePath_whenCreatesTempFileWithDefaultsNaming_thenCorrect() throws IOException {
123+
Path p = Paths.get(HOME + "/");
124+
p = Files.createTempFile(p, null, null);
125+
// like 8600179353689423985.tmp
126+
assertTrue(Files.exists(p));
127+
}
128+
129+
@Test
130+
public void givenNoFilePath_whenCreatesTempFileInTempDir_thenCorrect() throws IOException {
131+
Path p = Files.createTempFile(null, null);
132+
// like C:\Users\new\AppData\Local\Temp\6100927974988978748.tmp
133+
assertTrue(Files.exists(p));
134+
135+
}
136+
137+
// delete file
138+
@Test
139+
public void givenPath_whenDeletes_thenCorrect() throws IOException {
140+
Path p = Paths.get(HOME + "/fileToDelete.txt");
141+
assertFalse(Files.exists(p));
142+
Files.createFile(p);
143+
assertTrue(Files.exists(p));
144+
Files.delete(p);
145+
assertFalse(Files.exists(p));
146+
147+
}
148+
149+
@Test(expected = DirectoryNotEmptyException.class)
150+
public void givenPath_whenFailsToDeleteNonEmptyDir_thenCorrect() throws IOException {
151+
Path dir = Paths.get(HOME + "/emptyDir" + new Date().getTime());
152+
Files.createDirectory(dir);
153+
assertTrue(Files.exists(dir));
154+
Path file = dir.resolve("file.txt");
155+
Files.createFile(file);
156+
Files.delete(dir);
157+
158+
assertTrue(Files.exists(dir));
159+
160+
}
161+
162+
@Test
163+
public void givenInexistentFile_whenDeleteFails_thenCorrect() throws IOException {
164+
Path p = Paths.get(HOME + "/inexistentFile.txt");
165+
assertFalse(Files.exists(p));
166+
try {
167+
Files.delete(p);
168+
} catch (IOException e) {
169+
assertTrue(e instanceof NoSuchFileException);
170+
}
171+
172+
}
173+
174+
@Test
175+
public void givenInexistentFile_whenDeleteIfExistsWorks_thenCorrect() throws IOException {
176+
Path p = Paths.get(HOME + "/inexistentFile.txt");
177+
assertFalse(Files.exists(p));
178+
Files.deleteIfExists(p);
179+
180+
}
181+
182+
// copy file
183+
@Test
184+
public void givenFilePath_whenCopiesToNewLocation_thenCorrect() throws IOException {
185+
Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime());
186+
Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime());
187+
Files.createDirectory(dir1);
188+
Files.createDirectory(dir2);
189+
Path file1 = dir1.resolve("filetocopy.txt");
190+
Path file2 = dir2.resolve("filetocopy.txt");
191+
Files.createFile(file1);
192+
assertTrue(Files.exists(file1));
193+
assertFalse(Files.exists(file2));
194+
Files.copy(file1, file2);
195+
assertTrue(Files.exists(file2));
196+
197+
}
198+
199+
@Test(expected = FileAlreadyExistsException.class)
200+
public void givenPath_whenCopyFailsDueToExistingFile_thenCorrect() throws IOException {
201+
Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime());
202+
Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime());
203+
Files.createDirectory(dir1);
204+
Files.createDirectory(dir2);
205+
Path file1 = dir1.resolve("filetocopy.txt");
206+
Path file2 = dir2.resolve("filetocopy.txt");
207+
Files.createFile(file1);
208+
Files.createFile(file2);
209+
assertTrue(Files.exists(file1));
210+
assertTrue(Files.exists(file2));
211+
Files.copy(file1, file2);
212+
Files.copy(file1, file2, StandardCopyOption.REPLACE_EXISTING);
213+
}
214+
215+
// moving files
216+
@Test
217+
public void givenFilePath_whenMovesToNewLocation_thenCorrect() throws IOException {
218+
Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime());
219+
Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime());
220+
Files.createDirectory(dir1);
221+
Files.createDirectory(dir2);
222+
Path file1 = dir1.resolve("filetocopy.txt");
223+
Path file2 = dir2.resolve("filetocopy.txt");
224+
Files.createFile(file1);
225+
assertTrue(Files.exists(file1));
226+
assertFalse(Files.exists(file2));
227+
Files.move(file1, file2);
228+
assertTrue(Files.exists(file2));
229+
assertFalse(Files.exists(file1));
230+
231+
}
232+
233+
@Test(expected = FileAlreadyExistsException.class)
234+
public void givenFilePath_whenMoveFailsDueToExistingFile_thenCorrect() throws IOException {
235+
Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime());
236+
Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime());
237+
Files.createDirectory(dir1);
238+
Files.createDirectory(dir2);
239+
Path file1 = dir1.resolve("filetocopy.txt");
240+
Path file2 = dir2.resolve("filetocopy.txt");
241+
Files.createFile(file1);
242+
Files.createFile(file2);
243+
assertTrue(Files.exists(file1));
244+
assertTrue(Files.exists(file2));
245+
Files.move(file1, file2);
246+
Files.move(file1, file2, StandardCopyOption.REPLACE_EXISTING);
247+
assertTrue(Files.exists(file2));
248+
assertFalse(Files.exists(file1));
249+
}
250+
}

0 commit comments

Comments
 (0)