Skip to content

Commit 8ee89ac

Browse files
Merge remote-tracking branch 'origin/master'
2 parents 4ecead4 + eb26f83 commit 8ee89ac

31 files changed

Lines changed: 605 additions & 39 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.stringtokenizer;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.StringTokenizer;
6+
7+
public class Application {
8+
9+
public List<String> getTokens(String str) {
10+
List<String> tokens = new ArrayList<String>();
11+
// StringTokenizer tokenizer = new StringTokenizer( str );
12+
StringTokenizer tokenizer = new StringTokenizer( str , "," );
13+
// StringTokenizer tokenizer = new StringTokenizer( str , "," , true );
14+
while (tokenizer.hasMoreElements()) {
15+
tokens.add( tokenizer.nextToken() );
16+
// tokens.add( tokenizer.nextToken( "," ) );
17+
}
18+
return tokens;
19+
}
20+
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.junit4vstestng;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.junit.FixMethodOrder;
6+
import org.junit.Test;
7+
import org.junit.runners.MethodSorters;
8+
9+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
10+
public class SortedTests {
11+
12+
@Test
13+
public void a_givenString_whenChangedtoInt_thenTrue() {
14+
assertTrue(Integer.valueOf("10") instanceof Integer);
15+
}
16+
17+
@Test
18+
public void b_givenInt_whenChangedtoString_thenTrue() {
19+
assertTrue(String.valueOf(10) instanceof String);
20+
}
21+
22+
}

‎core-java/src/test/java/com/baeldung/list/flattennestedlist/FlattenNestedListTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class FlattenNestedListTest {
1717
List<List<String>> lol = asList(asList("one:one"), asList("two:one", "two:two", "two:three"), asList("three:one", "three:two", "three:three", "three:four"));
1818

1919
@Test
20-
public void givenString_flattenNestedList1() {
20+
public void givenNestedList_thenFlattenNestedListImperative() {
2121
List<String> ls = flattenListOfListsImperatively(lol);
2222

2323
assertNotNull(ls);
@@ -27,7 +27,7 @@ public void givenString_flattenNestedList1() {
2727
}
2828

2929
@Test
30-
public void givenString_flattenNestedList2() {
30+
public void givenNestedList_thenFlattenNestedListStream() {
3131
List<String> ls = flattenListOfListsStream(lol);
3232

3333
assertNotNull(ls);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.stringtokenizer;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class ApplicationTest {
12+
13+
Application application = new Application();
14+
List<String> expectedTokens = new ArrayList<String>();
15+
16+
@Before
17+
public void init() {
18+
expectedTokens.add( "Welcome" );
19+
expectedTokens.add( "to" );
20+
expectedTokens.add( "baeldung.com" );
21+
}
22+
23+
@Test
24+
public void givenString_thenGetListOfString() {
25+
String str = "Welcome,to,baeldung.com";
26+
List<String> actualTokens = application.getTokens(str);
27+
assertEquals(expectedTokens, actualTokens);
28+
}
29+
30+
}

jsoup/src/test/java/com/baeldung/jsoup/JsoupParserTest.java renamed to jsoup/src/test/java/com/baeldung/jsoup/JsoupParserIntegrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
import static org.junit.Assert.assertEquals;
1515
import static org.junit.Assert.assertTrue;
1616

17-
public class JsoupParserTest {
17+
public class JsoupParserIntegrationTest {
1818

19-
Document doc;
19+
private Document doc;
2020

2121
@Before
2222
public void setUp() throws IOException {

‎libraries/pom.xml‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<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">
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<parent>
55
<artifactId>parent-modules</artifactId>
66
<groupId>com.baeldung</groupId>
@@ -72,6 +72,26 @@
7272
<artifactId>javers-core</artifactId>
7373
<version>${javers.version}</version>
7474
</dependency>
75+
<dependency>
76+
<groupId>org.eclipse.jetty</groupId>
77+
<artifactId>jetty-server</artifactId>
78+
<version>${jetty.version}</version>
79+
</dependency>
80+
<dependency>
81+
<groupId>org.eclipse.jetty</groupId>
82+
<artifactId>jetty-servlet</artifactId>
83+
<version>${jetty.version}</version>
84+
</dependency>
85+
<dependency>
86+
<groupId>org.apache.httpcomponents</groupId>
87+
<artifactId>httpclient</artifactId>
88+
<version>${httpclient.version}</version>
89+
</dependency>
90+
<dependency>
91+
<groupId>commons-io</groupId>
92+
<artifactId>commons-io</artifactId>
93+
<version>${commons.io.version}</version>
94+
</dependency>
7595
</dependencies>
7696

7797
<properties>
@@ -84,6 +104,9 @@
84104
<assertj.version>3.6.2</assertj.version>
85105
<jsonassert.version>1.5.0</jsonassert.version>
86106
<javers.version>3.1.0</javers.version>
107+
<jetty.version>9.4.2.v20170220</jetty.version>
108+
<httpclient.version>4.5.3</httpclient.version>
109+
<commons.io.version>2.5</commons.io.version>
87110
</properties>
88111

89112
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.jetty;
2+
3+
import javax.servlet.AsyncContext;
4+
import javax.servlet.ServletException;
5+
import javax.servlet.ServletOutputStream;
6+
import javax.servlet.WriteListener;
7+
import javax.servlet.http.HttpServlet;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
import java.io.IOException;
11+
import java.nio.ByteBuffer;
12+
import java.nio.charset.StandardCharsets;
13+
14+
public class AsyncServlet extends HttpServlet {
15+
private static final String HEAVY_RESOURCE = "This is some heavy resource that will be served in an async way";
16+
17+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
18+
ByteBuffer content = ByteBuffer.wrap(HEAVY_RESOURCE.getBytes(StandardCharsets.UTF_8));
19+
20+
AsyncContext async = request.startAsync();
21+
ServletOutputStream out = response.getOutputStream();
22+
out.setWriteListener(new WriteListener() {
23+
@Override
24+
public void onWritePossible() throws IOException {
25+
while (out.isReady()) {
26+
if (!content.hasRemaining()) {
27+
response.setStatus(200);
28+
async.complete();
29+
return;
30+
}
31+
out.write(content.get());
32+
}
33+
}
34+
35+
@Override
36+
public void onError(Throwable t) {
37+
getServletContext().log("Async Error", t);
38+
async.complete();
39+
}
40+
});
41+
}
42+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.jetty;
2+
3+
import javax.servlet.ServletException;
4+
import javax.servlet.http.HttpServlet;
5+
import javax.servlet.http.HttpServletRequest;
6+
import javax.servlet.http.HttpServletResponse;
7+
import java.io.IOException;
8+
9+
public class BlockingServlet extends HttpServlet {
10+
11+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
12+
response.setContentType("application/json");
13+
response.setStatus(HttpServletResponse.SC_OK);
14+
response.getWriter().println("{ \"status\": \"ok\"}");
15+
}
16+
}
17+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.jetty;
2+
3+
import org.eclipse.jetty.server.Connector;
4+
import org.eclipse.jetty.server.Server;
5+
import org.eclipse.jetty.server.ServerConnector;
6+
import org.eclipse.jetty.servlet.ServletHandler;
7+
8+
public class JettyServer {
9+
10+
private Server server;
11+
12+
public void start() throws Exception {
13+
14+
server = new Server();
15+
ServerConnector connector = new ServerConnector(server);
16+
connector.setPort(8090);
17+
server.setConnectors(new Connector[]{connector});
18+
19+
ServletHandler servletHandler = new ServletHandler();
20+
server.setHandler(servletHandler);
21+
22+
servletHandler.addServletWithMapping(BlockingServlet.class, "/status");
23+
servletHandler.addServletWithMapping(AsyncServlet.class, "/heavy/async");
24+
25+
server.start();
26+
27+
}
28+
29+
public void stop() throws Exception {
30+
server.stop();
31+
}
32+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.jetty;
2+
3+
4+
import org.apache.commons.io.IOUtils;
5+
import org.apache.http.HttpResponse;
6+
import org.apache.http.client.HttpClient;
7+
import org.apache.http.client.methods.HttpGet;
8+
import org.apache.http.impl.client.HttpClientBuilder;
9+
import org.junit.After;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
13+
import java.nio.charset.StandardCharsets;
14+
15+
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
16+
17+
public class JettyTest {
18+
private JettyServer jettyServer;
19+
20+
@Before
21+
public void setup() throws Exception {
22+
jettyServer = new JettyServer();
23+
jettyServer.start();
24+
}
25+
26+
@After
27+
public void cleanup() throws Exception {
28+
Thread.sleep(2000);
29+
jettyServer.stop();
30+
}
31+
32+
@Test
33+
public void givenServer_whenSendRequestToBlockingServlet_thenReturnStatusOK() throws Exception {
34+
//given
35+
String url = "http://localhost:8090/status";
36+
HttpClient client = HttpClientBuilder.create().build();
37+
HttpGet request = new HttpGet(url);
38+
HttpResponse response = client.execute(request);
39+
40+
//then
41+
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
42+
43+
}
44+
45+
@Test
46+
public void givenServer_whenSendRequestToNonBlockingServlet_thenReturnStatusOK() throws Exception {
47+
//when
48+
String url = "http://localhost:8090/heavy/async";
49+
HttpClient client = HttpClientBuilder.create().build();
50+
HttpGet request = new HttpGet(url);
51+
HttpResponse response = client.execute(request);
52+
53+
//then
54+
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
55+
String responseContent = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);
56+
assertThat(responseContent).isEqualTo("This is some heavy resource that will be served in an async way");
57+
}
58+
}

0 commit comments

Comments
 (0)