Skip to content

Commit 64d1381

Browse files
committed
Merge branch 'master' of https://github.com/eugenp/tutorials
2 parents 131b695 + c4638f1 commit 64d1381

245 files changed

Lines changed: 4335 additions & 4714 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Baeldung.java‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
package com.baeldung.cxf.jaxrs.implementation;
22

3+
import javax.ws.rs.*;
4+
import javax.ws.rs.core.Response;
35
import java.util.ArrayList;
46
import java.util.HashMap;
57
import java.util.List;
68
import java.util.Map;
79

8-
import javax.ws.rs.GET;
9-
import javax.ws.rs.PUT;
10-
import javax.ws.rs.Path;
11-
import javax.ws.rs.PathParam;
12-
import javax.ws.rs.Produces;
13-
import javax.ws.rs.core.Response;
14-
1510
@Path("baeldung")
1611
@Produces("text/xml")
1712
public class Baeldung {
@@ -51,14 +46,13 @@ public Course getCourse(@PathParam("courseOrder") int courseOrder) {
5146
@Path("courses/{courseOrder}")
5247
public Response putCourse(@PathParam("courseOrder") int courseOrder, Course course) {
5348
Course existingCourse = courses.get(courseOrder);
54-
Response response;
49+
5550
if (existingCourse == null || existingCourse.getId() != course.getId() || !(existingCourse.getName().equals(course.getName()))) {
5651
courses.put(courseOrder, course);
57-
response = Response.ok().build();
58-
} else {
59-
response = Response.notModified().build();
52+
return Response.ok().build();
6053
}
61-
return response;
54+
55+
return Response.notModified().build();
6256
}
6357

6458
@Path("courses/{courseOrder}/students")

‎apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Course.java‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
package com.baeldung.cxf.jaxrs.implementation;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
5-
6-
import javax.ws.rs.DELETE;
7-
import javax.ws.rs.GET;
8-
import javax.ws.rs.POST;
9-
import javax.ws.rs.Path;
10-
import javax.ws.rs.PathParam;
3+
import javax.ws.rs.*;
114
import javax.ws.rs.core.Response;
12-
135
import javax.xml.bind.annotation.XmlRootElement;
6+
import java.util.ArrayList;
7+
import java.util.List;
148

159
@XmlRootElement(name = "Course")
1610
public class Course {
@@ -51,7 +45,7 @@ public Student getStudent(@PathParam("studentOrder")int studentOrder) {
5145
@POST
5246
public Response postStudent(Student student) {
5347
if (students == null) {
54-
students = new ArrayList<Student>();
48+
students = new ArrayList<>();
5549
}
5650
students.add(student);
5751
return Response.ok(student).build();

‎apache-cxf/cxf-jaxrs-implementation/src/test/java/com/baeldung/cxf/jaxrs/implementation/ServiceTest.java‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
package com.baeldung.cxf.jaxrs.implementation;
22

3-
import static org.junit.Assert.assertEquals;
4-
3+
import org.apache.http.HttpResponse;
4+
import org.apache.http.client.methods.HttpDelete;
5+
import org.apache.http.client.methods.HttpPost;
6+
import org.apache.http.client.methods.HttpPut;
7+
import org.apache.http.entity.InputStreamEntity;
8+
import org.apache.http.impl.client.CloseableHttpClient;
9+
import org.apache.http.impl.client.HttpClients;
510
import org.junit.AfterClass;
611
import org.junit.BeforeClass;
712
import org.junit.Test;
813

14+
import javax.xml.bind.JAXB;
915
import java.io.IOException;
1016
import java.io.InputStream;
1117
import java.io.InputStreamReader;
1218
import java.net.URL;
1319

14-
import javax.xml.bind.JAXB;
15-
16-
import org.apache.http.HttpResponse;
17-
import org.apache.http.client.methods.HttpDelete;
18-
import org.apache.http.client.methods.HttpPost;
19-
import org.apache.http.client.methods.HttpPut;
20-
import org.apache.http.entity.InputStreamEntity;
21-
import org.apache.http.impl.client.CloseableHttpClient;
22-
import org.apache.http.impl.client.HttpClients;
20+
import static org.junit.Assert.assertEquals;
2321

2422
public class ServiceTest {
2523
private static final String BASE_URL = "http://localhost:8080/baeldung/courses/";
@@ -80,14 +78,12 @@ public void whenDeleteStudent_thenCorrect() throws IOException {
8078
private Course getCourse(int courseOrder) throws IOException {
8179
URL url = new URL(BASE_URL + courseOrder);
8280
InputStream input = url.openStream();
83-
Course course = JAXB.unmarshal(new InputStreamReader(input), Course.class);
84-
return course;
81+
return JAXB.unmarshal(new InputStreamReader(input), Course.class);
8582
}
8683

8784
private Student getStudent(int courseOrder, int studentOrder) throws IOException {
8885
URL url = new URL(BASE_URL + courseOrder + "/students/" + studentOrder);
8986
InputStream input = url.openStream();
90-
Student student = JAXB.unmarshal(new InputStreamReader(input), Student.class);
91-
return student;
87+
return JAXB.unmarshal(new InputStreamReader(input), Student.class);
9288
}
9389
}

‎assertj/pom.xml‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
54
<modelVersion>4.0.0</modelVersion>
65

76
<groupId>com.baeldung</groupId>
87
<artifactId>assertj</artifactId>
98
<version>1.0.0-SNAPSHOT</version>
109

1110
<dependencies>
11+
1212
<dependency>
1313
<groupId>com.google.guava</groupId>
1414
<artifactId>guava</artifactId>
15-
<version>19.0</version>
15+
<version>${guava.version}</version>
1616
</dependency>
17+
<dependency>
18+
<groupId>org.assertj</groupId>
19+
<artifactId>assertj-guava</artifactId>
20+
<version>3.0.0</version>
21+
</dependency>
22+
1723
<dependency>
1824
<groupId>junit</groupId>
1925
<artifactId>junit</artifactId>
@@ -26,11 +32,7 @@
2632
<version>3.5.1</version>
2733
<scope>test</scope>
2834
</dependency>
29-
<dependency>
30-
<groupId>org.assertj</groupId>
31-
<artifactId>assertj-guava</artifactId>
32-
<version>3.0.0</version>
33-
</dependency>
35+
3436
</dependencies>
3537

3638
<build>
@@ -46,4 +48,9 @@
4648
</plugin>
4749
</plugins>
4850
</build>
51+
52+
<properties>
53+
<guava.version>19.0</guava.version>
54+
</properties>
55+
4956
</project>

‎core-java-8/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<dependency>
1616
<groupId>commons-io</groupId>
1717
<artifactId>commons-io</artifactId>
18-
<version>2.4</version>
18+
<version>2.5</version>
1919
</dependency>
2020

2121
<dependency>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.encoderdecoder;
2+
3+
import org.hamcrest.CoreMatchers;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
import java.io.UnsupportedEncodingException;
8+
import java.net.URLDecoder;
9+
import java.net.URLEncoder;
10+
import java.nio.charset.StandardCharsets;
11+
12+
public class EncoderDecoder {
13+
14+
@Test
15+
public void givenPlainURL_whenUsingUTF8EncodingScheme_thenEncodeURL() throws UnsupportedEncodingException {
16+
String plainURL = "http://www.baeldung.com" ;
17+
String encodedURL = URLEncoder.encode(plainURL, StandardCharsets.UTF_8.toString());
18+
19+
Assert.assertThat("http%3A%2F%2Fwww.baeldung.com", CoreMatchers.is(encodedURL));
20+
}
21+
22+
@Test
23+
public void givenEncodedURL_whenUsingUTF8EncodingScheme_thenDecodeURL() throws UnsupportedEncodingException {
24+
String encodedURL = "http%3A%2F%2Fwww.baeldung.com" ;
25+
String decodedURL = URLDecoder.decode(encodedURL, StandardCharsets.UTF_8.toString());
26+
27+
Assert.assertThat("http://www.baeldung.com", CoreMatchers.is(decodedURL));
28+
}
29+
30+
@Test
31+
public void givenEncodedURL_whenUsingWrongEncodingScheme_thenDecodeInvalidURL() throws UnsupportedEncodingException {
32+
String encodedURL = "http%3A%2F%2Fwww.baeldung.com";
33+
34+
String decodedURL = URLDecoder.decode(encodedURL, StandardCharsets.UTF_16.toString());
35+
36+
Assert.assertFalse("http://www.baeldung.com".equals(decodedURL));
37+
}
38+
}

‎core-java-8/src/test/java/com/baeldung/file/FileOperationsTest.java‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,20 @@ public void givenFilePath_whenUsingFilesLines_thenFileData() throws IOException,
102102
StringBuilder data = new StringBuilder();
103103
Stream<String> lines = Files.lines(path);
104104
lines.forEach(line -> data.append(line).append("\n"));
105+
lines.close();
105106

106107
Assert.assertEquals(expectedData, data.toString().trim());
107108
}
108109

109110
private String readFromInputStream(InputStream inputStream) throws IOException {
110-
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
111-
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
112111
StringBuilder resultStringBuilder = new StringBuilder();
113-
String line;
114-
while ((line = bufferedReader.readLine()) != null) {
115-
resultStringBuilder.append(line);
116-
resultStringBuilder.append("\n");
112+
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {
113+
String line;
114+
while ((line = bufferedReader.readLine()) != null) {
115+
resultStringBuilder.append(line).append("\n");
116+
}
117117
}
118-
bufferedReader.close();
119-
inputStreamReader.close();
120-
inputStream.close();
118+
121119
return resultStringBuilder.toString();
122120
}
123121
}
Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,41 @@
11
package com.baeldung.util;
22

3-
import static org.junit.Assert.assertEquals;
3+
import org.junit.Test;
44

5-
import java.time.Instant;
6-
import java.time.LocalDate;
7-
import java.time.LocalTime;
5+
import java.time.*;
86
import java.time.temporal.ChronoField;
9-
import java.util.Calendar;
10-
import java.util.GregorianCalendar;
117

12-
import org.junit.Test;
8+
import static org.junit.Assert.assertEquals;
139

1410
public class CurrentDateTimeTest {
1511

12+
private static final Clock clock = Clock.fixed(Instant.parse("2016-10-09T15:10:30.00Z"), ZoneId.of("UTC"));
13+
1614
@Test
1715
public void shouldReturnCurrentDate() {
1816

19-
final LocalDate now = LocalDate.now();
20-
final Calendar calendar = GregorianCalendar.getInstance();
17+
final LocalDate now = LocalDate.now(clock);
2118

22-
assertEquals("10-10-2010".length(), now.toString().length());
23-
assertEquals(calendar.get(Calendar.DATE), now.get(ChronoField.DAY_OF_MONTH));
24-
assertEquals(calendar.get(Calendar.MONTH), now.get(ChronoField.MONTH_OF_YEAR) - 1);
25-
assertEquals(calendar.get(Calendar.YEAR), now.get(ChronoField.YEAR));
19+
assertEquals(9, now.get(ChronoField.DAY_OF_MONTH));
20+
assertEquals(10, now.get(ChronoField.MONTH_OF_YEAR));
21+
assertEquals(2016, now.get(ChronoField.YEAR));
2622
}
2723

2824
@Test
2925
public void shouldReturnCurrentTime() {
3026

31-
final LocalTime now = LocalTime.now();
32-
final Calendar calendar = GregorianCalendar.getInstance();
27+
final LocalTime now = LocalTime.now(clock);
3328

34-
assertEquals(calendar.get(Calendar.HOUR_OF_DAY), now.get(ChronoField.HOUR_OF_DAY));
35-
assertEquals(calendar.get(Calendar.MINUTE), now.get(ChronoField.MINUTE_OF_HOUR));
36-
assertEquals(calendar.get(Calendar.SECOND), now.get(ChronoField.SECOND_OF_MINUTE));
29+
assertEquals(15, now.get(ChronoField.HOUR_OF_DAY));
30+
assertEquals(10, now.get(ChronoField.MINUTE_OF_HOUR));
31+
assertEquals(30, now.get(ChronoField.SECOND_OF_MINUTE));
3732
}
3833

3934
@Test
4035
public void shouldReturnCurrentTimestamp() {
4136

42-
final Instant now = Instant.now();
43-
final Calendar calendar = GregorianCalendar.getInstance();
37+
final Instant now = Instant.now(clock);
4438

45-
assertEquals(calendar.getTimeInMillis() / 1000, now.getEpochSecond());
39+
assertEquals(clock.instant().getEpochSecond(), now.getEpochSecond());
4640
}
4741
}

‎core-java/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@
174174
<mysql-connector-java.version>5.1.38</mysql-connector-java.version>
175175

176176
<!-- marshalling -->
177-
<jackson.version>2.7.2</jackson.version>
177+
<jackson.version>2.7.8</jackson.version>
178178

179179
<!-- logging -->
180180
<org.slf4j.version>1.7.13</org.slf4j.version>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.java.nio.selector;
2+
3+
import java.io.IOException;
4+
import java.net.InetSocketAddress;
5+
import java.nio.ByteBuffer;
6+
import java.nio.channels.SocketChannel;
7+
8+
public class EchoClient {
9+
private static SocketChannel client;
10+
private static ByteBuffer buffer;
11+
private static EchoClient instance;
12+
13+
public static EchoClient start() {
14+
if (instance == null)
15+
instance = new EchoClient();
16+
17+
return instance;
18+
}
19+
20+
private EchoClient() {
21+
try {
22+
client = SocketChannel.open(new InetSocketAddress("localhost", 5454));
23+
buffer = ByteBuffer.allocate(256);
24+
} catch (IOException e) {
25+
e.printStackTrace();
26+
}
27+
}
28+
29+
public String sendMessage(String msg) {
30+
buffer = ByteBuffer.wrap(msg.getBytes());
31+
String response = null;
32+
try {
33+
client.write(buffer);
34+
buffer.clear();
35+
client.read(buffer);
36+
response = new String(buffer.array()).trim();
37+
System.out.println("response=" + response);
38+
buffer.clear();
39+
} catch (IOException e) {
40+
e.printStackTrace();
41+
}
42+
return response;
43+
44+
}
45+
46+
}

0 commit comments

Comments
 (0)