Skip to content

Commit d011f2b

Browse files
Merge remote-tracking branch 'origin/master'
2 parents 24e560a + 94fd47d commit d011f2b

21 files changed

Lines changed: 327 additions & 142 deletions

File tree

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,13 @@ public void givenFilePath_whenUsingFilesLines_thenFileData() throws IOException,
107107
}
108108

109109
private String readFromInputStream(InputStream inputStream) throws IOException {
110-
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
111-
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
112110
StringBuilder resultStringBuilder = new StringBuilder();
113-
String line;
114-
while ((line = bufferedReader.readLine()) != null) {
115-
resultStringBuilder.append(line);
116-
resultStringBuilder.append("\n");
111+
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {
112+
String line;
113+
while ((line = bufferedReader.readLine()) != null) {
114+
resultStringBuilder.append(line).append("\n");
115+
}
117116
}
118-
bufferedReader.close();
119-
inputStreamReader.close();
120-
inputStream.close();
121117
return resultStringBuilder.toString();
122118
}
123119
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello world
Lines changed: 157 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,172 @@
1-
import java.util.ArrayList;
2-
import java.util.HashMap;
3-
import java.util.List;
4-
import java.util.Map;
51

6-
import com.fasterxml.jackson.databind.JsonNode;
7-
import org.junit.*;
2+
import static org.junit.Assert.assertEquals;
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
85

9-
import play.mvc.*;
10-
import play.test.*;
11-
import play.data.DynamicForm;
12-
import play.data.validation.ValidationError;
13-
import play.data.validation.Constraints.RequiredValidator;
14-
import play.i18n.Lang;
15-
import play.libs.F;
16-
import play.libs.F.*;
17-
import play.twirl.api.Content;
6+
import java.io.BufferedReader;
7+
import java.io.DataOutputStream;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.InputStreamReader;
11+
import java.net.HttpURLConnection;
12+
import java.net.MalformedURLException;
13+
import java.net.URL;
14+
import java.util.Arrays;
1815

16+
import org.json.JSONArray;
17+
import org.json.JSONObject;
18+
import org.junit.Before;
19+
import org.junit.Test;
20+
import model.Student;
21+
import play.test.*;
1922
import static play.test.Helpers.*;
20-
import static org.junit.Assert.*;
2123

24+
public class ApplicationTest{
25+
private static final String BASE_URL = "http://localhost:9000";
2226

23-
/**
24-
*
25-
* Simple (JUnit) tests that can call all parts of a play app.
26-
* If you are interested in mocking a whole application, see the wiki for more details.
27-
*
28-
*/
29-
public class ApplicationTest {
27+
@Test
28+
public void testInServer() throws Exception {
29+
TestServer server = testServer(3333);
30+
running(server, () -> {
31+
try {
32+
WSClient ws = play.libs.ws.WS.newClient(3333);
33+
CompletionStage<WSResponse> completionStage = ws.url("/").get();
34+
WSResponse response = completionStage.toCompletableFuture().get();
35+
ws.close();
36+
assertEquals(OK, response.getStatus());
37+
} catch (Exception e) {
38+
logger.error(e.getMessage(), e);
39+
}
40+
});
41+
}
42+
@Test
43+
public void whenCreatesRecord_thenCorrect() {
44+
Student student = new Student("jody", "west", 50);
45+
JSONObject obj = new JSONObject(makeRequest(BASE_URL, "POST", new JSONObject(student)));
46+
assertTrue(obj.getBoolean("isSuccessfull"));
47+
JSONObject body = obj.getJSONObject("body");
48+
assertEquals(student.getAge(), body.getInt("age"));
49+
assertEquals(student.getFirstName(), body.getString("firstName"));
50+
assertEquals(student.getLastName(), body.getString("lastName"));
51+
}
3052

3153
@Test
32-
public void simpleCheck() {
33-
int a = 1 + 1;
34-
assertEquals(2, a);
54+
public void whenDeletesCreatedRecord_thenCorrect() {
55+
Student student = new Student("Usain", "Bolt", 25);
56+
JSONObject ob1 = new JSONObject(makeRequest(BASE_URL, "POST", new JSONObject(student))).getJSONObject("body");
57+
int id = ob1.getInt("id");
58+
JSONObject obj1 = new JSONObject(makeRequest(BASE_URL + "/" + id, "POST", new JSONObject()));
59+
assertTrue(obj1.getBoolean("isSuccessfull"));
60+
makeRequest(BASE_URL + "/" + id, "DELETE", null);
61+
JSONObject obj2 = new JSONObject(makeRequest(BASE_URL + "/" + id, "POST", new JSONObject()));
62+
assertFalse(obj2.getBoolean("isSuccessfull"));
3563
}
3664

3765
@Test
38-
public void renderTemplate() {
39-
Content html = views.html.index.render("Your new application is ready.");
40-
assertEquals("text/html", html.contentType());
41-
assertTrue(html.body().contains("Your new application is ready."));
66+
public void whenUpdatesCreatedRecord_thenCorrect() {
67+
Student student = new Student("john", "doe", 50);
68+
JSONObject body1 = new JSONObject(makeRequest(BASE_URL, "POST", new JSONObject(student))).getJSONObject("body");
69+
assertEquals(student.getAge(), body1.getInt("age"));
70+
int newAge = 60;
71+
body1.put("age", newAge);
72+
JSONObject body2 = new JSONObject(makeRequest(BASE_URL, "PUT", body1)).getJSONObject("body");
73+
assertFalse(student.getAge() == body2.getInt("age"));
74+
assertTrue(newAge == body2.getInt("age"));
75+
}
76+
77+
@Test
78+
public void whenGetsAllRecords_thenCorrect() {
79+
Student student1 = new Student("jane", "daisy", 50);
80+
Student student2 = new Student("john", "daniel", 60);
81+
Student student3 = new Student("don", "mason", 55);
82+
Student student4 = new Student("scarlet", "ohara", 90);
83+
84+
makeRequest(BASE_URL, "POST", new JSONObject(student1));
85+
makeRequest(BASE_URL, "POST", new JSONObject(student2));
86+
makeRequest(BASE_URL, "POST", new JSONObject(student3));
87+
makeRequest(BASE_URL, "POST", new JSONObject(student4));
88+
89+
JSONObject objects = new JSONObject(makeRequest(BASE_URL, "GET", null));
90+
assertTrue(objects.getBoolean("isSuccessfull"));
91+
JSONArray array = objects.getJSONArray("body");
92+
assertTrue(array.length() >= 4);
93+
}
94+
95+
public static String makeRequest(String myUrl, String httpMethod, JSONObject parameters) {
96+
97+
URL url = null;
98+
try {
99+
url = new URL(myUrl);
100+
} catch (MalformedURLException e) {
101+
e.printStackTrace();
102+
}
103+
HttpURLConnection conn = null;
104+
try {
105+
106+
conn = (HttpURLConnection) url.openConnection();
107+
} catch (IOException e) {
108+
e.printStackTrace();
109+
}
110+
conn.setDoInput(true);
111+
112+
conn.setReadTimeout(10000);
113+
114+
conn.setRequestProperty("Content-Type", "application/json");
115+
DataOutputStream dos = null;
116+
int respCode = 0;
117+
String inputString = null;
118+
try {
119+
conn.setRequestMethod(httpMethod);
120+
121+
if (Arrays.asList("POST", "PUT").contains(httpMethod)) {
122+
String params = parameters.toString();
123+
124+
conn.setDoOutput(true);
125+
126+
dos = new DataOutputStream(conn.getOutputStream());
127+
dos.writeBytes(params);
128+
dos.flush();
129+
dos.close();
130+
}
131+
respCode = conn.getResponseCode();
132+
if (respCode != 200 && respCode != 201) {
133+
String error = inputStreamToString(conn.getErrorStream());
134+
return error;
135+
}
136+
inputString = inputStreamToString(conn.getInputStream());
137+
138+
} catch (IOException e) {
139+
140+
e.printStackTrace();
141+
}
142+
return inputString;
42143
}
43144

145+
public static String inputStreamToString(InputStream is) {
146+
BufferedReader br = null;
147+
StringBuilder sb = new StringBuilder();
148+
149+
String line;
150+
try {
151+
152+
br = new BufferedReader(new InputStreamReader(is));
153+
while ((line = br.readLine()) != null) {
154+
sb.append(line);
155+
}
44156

157+
} catch (IOException e) {
158+
e.printStackTrace();
159+
} finally {
160+
if (br != null) {
161+
try {
162+
br.close();
163+
} catch (IOException e) {
164+
e.printStackTrace();
165+
}
166+
}
167+
}
168+
169+
return sb.toString();
170+
171+
}
45172
}

‎play-framework/student-api/test/IntegrationTest.java‎

Lines changed: 0 additions & 25 deletions
This file was deleted.

‎spring-jms/pom.xml‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
<artifactId>activemq-all</artifactId>
2727
<version>${activemq.version}</version>
2828
</dependency>
29+
<dependency>
30+
<groupId>junit</groupId>
31+
<artifactId>junit</artifactId>
32+
<version>4.12</version>
33+
<scope>test</scope>
34+
</dependency>
2935
</dependencies>
3036

3137
<build>
@@ -36,8 +42,8 @@
3642
<artifactId>maven-compiler-plugin</artifactId>
3743
<version>3.2</version>
3844
<configuration>
39-
<source>1.7</source>
40-
<target>1.7</target>
45+
<source>1.8</source>
46+
<target>1.8</target>
4147
</configuration>
4248
</plugin>
4349
<plugin>
@@ -52,6 +58,16 @@
5258
</plugin>
5359
</plugins>
5460
</pluginManagement>
55-
<finalName>spring-jms</finalName>
61+
<plugins>
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-compiler-plugin</artifactId>
65+
<configuration>
66+
<source>1.8</source>
67+
<target>1.8</target>
68+
</configuration>
69+
</plugin>
70+
</plugins>
71+
<finalName>spring-jms</finalName>
5672
</build>
5773
</project>

‎spring-jms/src/main/java/com/baeldung/spring/jms/Employee.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ public Integer getAge() {
1818
}
1919

2020
public String toString() {
21-
return "Person: name(" + name + "), age(" + age + ")";
21+
return "Employee: name(" + name + "), age(" + age + ")";
2222
}
2323
}

‎spring-jms/src/main/java/com/baeldung/spring/jms/SampleJMSExample.java‎

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
package com.baeldung.spring.jms;
22

33
import org.springframework.jms.core.JmsTemplate;
4-
import org.springframework.jms.core.MessageCreator;
54

6-
import javax.jms.*;
5+
import javax.jms.Queue;
6+
import java.util.HashMap;
7+
import java.util.Map;
78

89
public class SampleJmsMessageSender {
910

1011
private JmsTemplate jmsTemplate;
1112
private Queue queue;
1213

13-
public void createJmsTemplate(ConnectionFactory cf) {
14-
this.jmsTemplate = new JmsTemplate(cf);
14+
public void setJmsTemplate(JmsTemplate jmsTemplate) {
15+
this.jmsTemplate = jmsTemplate;
1516
}
1617

1718
public void setQueue(Queue queue) {
1819
this.queue = queue;
1920
}
2021

2122
public void simpleSend() {
22-
this.jmsTemplate.send(this.queue, new MessageCreator() {
23-
public Message createMessage(Session session) throws JMSException {
24-
return session.createTextMessage("hello queue world");
25-
}
26-
});
23+
jmsTemplate.send(queue, s -> s.createTextMessage("hello queue world"));
2724
}
28-
}
25+
26+
public void sendMessage(final Employee employee) {
27+
System.out.println("Jms Message Sender : " + employee);
28+
Map<String, Object> map = new HashMap<>();
29+
map.put("name", employee.getName());
30+
map.put("age", employee.getAge());
31+
this.jmsTemplate.convertAndSend(map);
32+
}
33+
}

0 commit comments

Comments
 (0)