This commit is contained in:
kkaravitis
2020-03-19 22:35:30 +02:00
196 changed files with 820 additions and 498 deletions
+1
View File
@@ -10,3 +10,4 @@ This module contains articles about searching algorithms.
- [String Search Algorithms for Large Texts](https://www.baeldung.com/java-full-text-search-algorithms)
- [Monte Carlo Tree Search for Tic-Tac-Toe Game](https://www.baeldung.com/java-monte-carlo-tree-search)
- [Range Search Algorithm in Java](https://www.baeldung.com/java-range-search)
- [Fast Pattern Matching of Strings Using Suffix Tree](https://www.baeldung.com/java-pattern-matching-suffix-tree)
+3
View File
@@ -0,0 +1,3 @@
### Relevant Articles:
- [Introduction to Apache Beam](https://www.baeldung.com/apache-beam)
+2
View File
@@ -5,3 +5,5 @@ This module contains articles about Java 14.
### Relevant articles
- [Guide to the @Serial Annotation in Java 14](https://www.baeldung.com/java-14-serial-annotation)
- [Java Text Blocks](https://www.baeldung.com/java-text-blocks)
- [Pattern Matching for instanceof in Java 14](https://www.baeldung.com/java-pattern-matching-instanceof)
@@ -9,8 +9,3 @@ This module contains articles about the improvements to core Java features intro
- [Java 9 Stream API Improvements](https://www.baeldung.com/java-9-stream-api)
- [Java 9 java.util.Objects Additions](https://www.baeldung.com/java-9-objects-new)
- [Java 9 CompletableFuture API Improvements](https://www.baeldung.com/java-9-completablefuture)
#### Relevant articles not in this module:
- [Java 9 Process API Improvements](https://www.baeldung.com/java-9-process-api) (see the [core-java-os](/core-java-os) module)
@@ -0,0 +1,3 @@
### Relevant Articles:
- [Arrays.deepEquals](https://www.baeldung.com/java-arrays-deepequals)
@@ -7,3 +7,5 @@ This module contains articles about core java exceptions
- [Is It a Bad Practice to Catch Throwable?](https://www.baeldung.com/java-catch-throwable-bad-practice)
- [Wrapping vs Rethrowing Exceptions in Java](https://www.baeldung.com/java-wrapping-vs-rethrowing-exceptions)
- [java.net.UnknownHostException: Invalid Hostname for Server](https://www.baeldung.com/java-unknownhostexception)
- [How to Handle Java SocketException](https://www.baeldung.com/java-socketexception)
- [Java Suppressed Exceptions](https://www.baeldung.com/java-suppressed-exceptions)
+8
View File
@@ -47,6 +47,14 @@
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.tomakehurst/wiremock -->
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.26.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@@ -0,0 +1,52 @@
package com.baeldung.blockingnonblocking;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import java.io.*;
import java.net.Socket;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.junit.Assert.assertTrue;
public class BlockingClientUnitTest {
private static final String REQUESTED_RESOURCE = "/test.json";
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
@Before
public void setup() {
stubFor(get(urlEqualTo(REQUESTED_RESOURCE)).willReturn(aResponse()
.withStatus(200)
.withBody("{ \"response\" : \"It worked!\" }\r\n\r\n")));
}
@Test
public void givenJavaIOSocket_whenReadingAndWritingWithStreams_thenSuccess() throws IOException {
// given an IO socket and somewhere to store our result
Socket socket = new Socket("localhost", wireMockRule.port());
StringBuilder ourStore = new StringBuilder();
// when we write and read (using try-with-resources so our resources are auto-closed)
try (InputStream serverInput = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(serverInput));
OutputStream clientOutput = socket.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(clientOutput))) {
writer.print("GET " + REQUESTED_RESOURCE + " HTTP/1.0\r\n\r\n");
writer.flush(); // important - without this the request is never sent, and the test will hang on readLine()
for (String line; (line = reader.readLine()) != null; ) {
ourStore.append(line);
ourStore.append(System.lineSeparator());
}
}
// then we read and saved our data
assertTrue(ourStore
.toString()
.contains("It worked!"));
}
}
@@ -0,0 +1,94 @@
package com.baeldung.blockingnonblocking;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.junit.Assert.assertTrue;
public class NonBlockingClientUnitTest {
private String REQUESTED_RESOURCE = "/test.json";
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
@Before
public void setup() {
stubFor(get(urlEqualTo(REQUESTED_RESOURCE)).willReturn(aResponse()
.withStatus(200)
.withBody("{ \"response\" : \"It worked!\" }")));
}
@Test
public void givenJavaNIOSocketChannel_whenReadingAndWritingWithBuffers_thenSuccess() throws IOException {
// given a NIO SocketChannel and a charset
InetSocketAddress address = new InetSocketAddress("localhost", wireMockRule.port());
SocketChannel socketChannel = SocketChannel.open(address);
Charset charset = StandardCharsets.UTF_8;
// when we write and read using buffers
socketChannel.write(charset.encode(CharBuffer.wrap("GET " + REQUESTED_RESOURCE + " HTTP/1.0\r\n\r\n")));
ByteBuffer byteBuffer = ByteBuffer.allocate(8192); // or allocateDirect if we need direct memory access
CharBuffer charBuffer = CharBuffer.allocate(8192);
CharsetDecoder charsetDecoder = charset.newDecoder();
StringBuilder ourStore = new StringBuilder();
while (socketChannel.read(byteBuffer) != -1 || byteBuffer.position() > 0) {
byteBuffer.flip();
storeBufferContents(byteBuffer, charBuffer, charsetDecoder, ourStore);
byteBuffer.compact();
}
socketChannel.close();
// then we read and saved our data
assertTrue(ourStore
.toString()
.contains("It worked!"));
}
@Test
public void givenJavaNIOSocketChannel_whenReadingAndWritingWithSmallBuffers_thenSuccess() throws IOException {
// given a NIO SocketChannel and a charset
InetSocketAddress address = new InetSocketAddress("localhost", wireMockRule.port());
SocketChannel socketChannel = SocketChannel.open(address);
Charset charset = StandardCharsets.UTF_8;
// when we write and read using buffers that are too small for our message
socketChannel.write(charset.encode(CharBuffer.wrap("GET " + REQUESTED_RESOURCE + " HTTP/1.0\r\n\r\n")));
ByteBuffer byteBuffer = ByteBuffer.allocate(8); // or allocateDirect if we need direct memory access
CharBuffer charBuffer = CharBuffer.allocate(8);
CharsetDecoder charsetDecoder = charset.newDecoder();
StringBuilder ourStore = new StringBuilder();
while (socketChannel.read(byteBuffer) != -1 || byteBuffer.position() > 0) {
byteBuffer.flip();
storeBufferContents(byteBuffer, charBuffer, charsetDecoder, ourStore);
byteBuffer.compact();
}
socketChannel.close();
// then we read and saved our data
assertTrue(ourStore
.toString()
.contains("It worked!"));
}
void storeBufferContents(ByteBuffer byteBuffer, CharBuffer charBuffer, CharsetDecoder charsetDecoder, StringBuilder ourStore) {
charsetDecoder.decode(byteBuffer, charBuffer, true);
charBuffer.flip();
ourStore.append(charBuffer);
charBuffer.clear();
}
}
@@ -9,4 +9,5 @@ This module contains articles about core features in the Java language
- [Java Default Parameters Using Method Overloading](https://www.baeldung.com/java-default-parameters-method-overloading)
- [How to Return Multiple Values From a Java Method](https://www.baeldung.com/java-method-return-multiple-values)
- [Guide to the Java finally Keyword](https://www.baeldung.com/java-finally-keyword)
- [The Java Headless Mode](https://www.baeldung.com/java-headless-mode)
- [[<-- Prev]](/core-java-modules/core-java-lang)
+2 -1
View File
@@ -8,4 +8,5 @@ This module contains articles about core Java non-blocking input and output (IO)
- [Create a Symbolic Link with Java](https://www.baeldung.com/java-symlink)
- [Introduction to the Java NIO Selector](https://www.baeldung.com/java-nio-selector)
- [Using Java MappedByteBuffer](https://www.baeldung.com/java-mapped-byte-buffer)
- [[<-- Prev]](/core-java-modules/core-java-nio)
- [How to Lock a File in Java](https://www.baeldung.com/java-lock-files)
- [[<-- Prev]](/core-java-modules/core-java-nio)
@@ -10,3 +10,4 @@ This module contains articles about performance of Java applications
- [Basic Introduction to JMX](http://www.baeldung.com/java-management-extensions)
- [Monitoring Java Applications with Flight Recorder](https://www.baeldung.com/java-flight-recorder-monitoring)
- [Branch Prediction in Java](https://www.baeldung.com/java-branch-prediction)
- [Capturing a Java Thread Dump](https://www.baeldung.com/java-thread-dump)
@@ -8,3 +8,4 @@
- [Guide to Escaping Characters in Java RegExps](http://www.baeldung.com/java-regexp-escape-char)
- [Pre-compile Regex Patterns Into Pattern Objects](https://www.baeldung.com/java-regex-pre-compile)
- [Difference Between Java Matcher find() and matches()](https://www.baeldung.com/java-matcher-find-vs-matches)
- [How to Use Regular Expressions to Replace Tokens in Strings](https://www.baeldung.com/java-regex-token-replacement)
@@ -0,0 +1,3 @@
### Relevant Articles:
- [Guide To The Java Authentication And Authorization Service (JAAS)](https://www.baeldung.com/java-authentication-authorization-service)
@@ -10,4 +10,5 @@ This module contains articles about string operations.
- [Java String equalsIgnoreCase()](https://www.baeldung.com/java-string-equalsignorecase)
- [Case-Insensitive String Matching in Java](https://www.baeldung.com/java-case-insensitive-string-matching)
- [L-Trim and R-Trim in Java](https://www.baeldung.com/l-trim-and-r-trim-in-java)
- [L-Trim and R-Trim Alternatives in Java](https://www.baeldung.com/java-trim-alternatives)
- More articles: [[<-- prev]](../core-java-string-operations)
-4
View File
@@ -1,7 +1,3 @@
## Custom PMD Rules
This module contains articles about PMD
### Relevant Articles:
- [Introduction To PMD](https://www.baeldung.com/pmd)
+1
View File
@@ -9,3 +9,4 @@ This module contains articles about data structures in Java
- [Circular Linked List Java Implementation](https://www.baeldung.com/java-circular-linked-list)
- [How to Print a Binary Tree Diagram](https://www.baeldung.com/java-print-binary-tree-diagram)
- [Introduction to Big Queue](https://www.baeldung.com/java-big-queue)
- [Guide to AVL Trees in Java](https://www.baeldung.com/java-avl-trees)
+3 -1
View File
@@ -1 +1,3 @@
## Relevant Articles
### Relevant Articles:
- [DDD Bounded Contexts and Java Modules](https://www.baeldung.com/java-modules-ddd-bounded-contexts)
-1
View File
@@ -6,5 +6,4 @@ This module contains articles about Domain-driven Design (DDD)
- [Persisting DDD Aggregates](https://www.baeldung.com/spring-persisting-ddd-aggregates)
- [Double Dispatch in DDD](https://www.baeldung.com/ddd-double-dispatch)
- [DDD Aggregates and @DomainEvents](https://www.baeldung.com/spring-data-ddd)
- [Organizing Layers Using Hexagonal Architecture, DDD, and Spring](https://www.baeldung.com/hexagonal-architecture-ddd-spring)
+3
View File
@@ -0,0 +1,3 @@
### Relevant Articles:
- [What’s New in Gradle 6.0](https://www.baeldung.com/gradle-6-features)
+3
View File
@@ -0,0 +1,3 @@
### Relevant Articles:
- [Converting Gradle Build File to Maven POM](https://www.baeldung.com/gradle-build-to-maven-pom)
+1 -1
View File
@@ -13,4 +13,4 @@ This module contains articles about Gson
- [Convert String to JsonObject with Gson](https://www.baeldung.com/gson-string-to-jsonobject)
- [Mapping Multiple JSON Fields to a Single Java Field](https://www.baeldung.com/json-multiple-fields-single-java-field)
- [Serializing and Deserializing a List with Gson](https://www.baeldung.com/gson-list)
- [Compare Two JSON Objects with Gson](https://www.baeldung.com/gson-compare-json-objects)
+2
View File
@@ -4,3 +4,5 @@ This module contains articles about image processing.
### Relevant Articles:
- [Working with Images in Java](https://www.baeldung.com/java-images)
- [Intro to OpenCV with Java](https://www.baeldung.com/java-opencv)
- [Optical Character Recognition with Tesseract](https://www.baeldung.com/java-ocr-tesseract)
+1
View File
@@ -2,3 +2,4 @@
- [Generating Random Numbers](https://www.baeldung.com/java-generating-random-numbers)
- [Convert Double to Long in Java](https://www.baeldung.com/java-convert-double-long)
- [Check for null Before Calling Parse in Double.parseDouble](https://www.baeldung.com/java-check-null-parse-double)
@@ -12,7 +12,9 @@ import javax.batch.runtime.StepExecution;
import com.baeldung.batch.understanding.BatchTestHelper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Disabled;
@Disabled("Should be fixed in BAEL-3812")
class CustomCheckPointUnitTest {
@Test
public void givenChunk_whenCustomCheckPoint_thenCommitCountIsThree() throws Exception {
@@ -13,7 +13,9 @@ import javax.batch.runtime.JobExecution;
import javax.batch.runtime.StepExecution;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Disabled;
@Disabled("Should be fixed in BAEL-3812")
class JobSequenceUnitTest {
@Test
public void givenTwoSteps_thenBatch_CompleteWithSuccess() throws Exception {
@@ -14,7 +14,9 @@ import javax.batch.runtime.Metric;
import javax.batch.runtime.StepExecution;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Disabled;
@Disabled("Should be fixed in BAEL-3812")
class SimpleChunkUnitTest {
@Test
public void givenChunk_thenBatch_CompletesWithSucess() throws Exception {
+6
View File
@@ -7,6 +7,12 @@
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Bookstore</name>
<parent>
<artifactId>jhipster-5</artifactId>
<groupId>com.baeldung.jhipster</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<repositories>
<!-- jhipster-needle-maven-repository -->
@@ -37,7 +37,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
*
* @see WebConfigurer
*/
public class WebConfigurerTest {
public class WebConfigurerUnitTest {
private WebConfigurer webConfigurer;
@@ -116,7 +116,7 @@ public class WebConfigurerTest {
props.getCors().setMaxAge(1800L);
props.getCors().setAllowCredentials(true);
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new WebConfigurerTestController())
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new WebConfigurerUnitTestController())
.addFilters(webConfigurer.corsFilter())
.build();
@@ -146,7 +146,7 @@ public class WebConfigurerTest {
props.getCors().setMaxAge(1800L);
props.getCors().setAllowCredentials(true);
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new WebConfigurerTestController())
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new WebConfigurerUnitTestController())
.addFilters(webConfigurer.corsFilter())
.build();
@@ -161,7 +161,7 @@ public class WebConfigurerTest {
public void testCorsFilterDeactivated() throws Exception {
props.getCors().setAllowedOrigins(null);
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new WebConfigurerTestController())
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new WebConfigurerUnitTestController())
.addFilters(webConfigurer.corsFilter())
.build();
@@ -176,7 +176,7 @@ public class WebConfigurerTest {
public void testCorsFilterDeactivated2() throws Exception {
props.getCors().setAllowedOrigins(new ArrayList<>());
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new WebConfigurerTestController())
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new WebConfigurerUnitTestController())
.addFilters(webConfigurer.corsFilter())
.build();
@@ -4,7 +4,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WebConfigurerTestController {
public class WebConfigurerUnitTestController {
@GetMapping("/api/test-cors")
public void testCorsOnApiPath() {

Some files were not shown because too many files have changed in this diff Show More