Skip to content

Commit 7918324

Browse files
Merge remote-tracking branch 'origin/master'
2 parents 5ee61b4 + 585a884 commit 7918324

4 files changed

Lines changed: 599 additions & 0 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+
}

0 commit comments

Comments
 (0)