Merge branch 'master' of https://github.com/vikasrajput6035/tutorials into BAEL-3832

This commit is contained in:
Vikas Ramsingh Rajput
2020-03-20 12:00:30 +03:00
157 changed files with 861 additions and 256 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)
@@ -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)
+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)
+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 {
+1
View File
@@ -4,3 +4,4 @@ This module contains articles about JSON.
### Relevant Articles:
- [Introduction to Jsoniter](https://www.baeldung.com/java-jsoniter)
- [Introduction to Moshi Json](https://www.baeldung.com/java-json-moshi)
+4
View File
@@ -12,3 +12,7 @@ Remember, for advanced libraries like [Jackson](/jackson) and [JUnit](/testing-m
- [Guide to the Cactoos Library](https://www.baeldung.com/java-cactoos)
- [Parsing Command-Line Parameters with Airline](https://www.baeldung.com/java-airline)
- [Introduction to cache2k](https://www.baeldung.com/java-cache2k)
- [Introduction to the jcabi-aspects AOP Annotations Library](https://www.baeldung.com/java-jcabi-aspects)
- [Introduction to Takes](https://www.baeldung.com/java-takes)
- [Using NullAway to Avoid NullPointerExceptions](https://www.baeldung.com/java-nullaway)
+56
View File
@@ -94,6 +94,23 @@
<artifactId>velocity-engine-core</artifactId>
<version>${velocity-engine-core.version}</version>
</dependency>
<dependency>
<groupId>com.uber.nullaway</groupId>
<artifactId>nullaway</artifactId>
<version>0.3.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-javac-errorprone</artifactId>
<version>2.8</version>
</dependency>
<!-- override plexus-compiler-javac-errorprone's dependency on
Error Prone with the latest version -->
<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_core</artifactId>
<version>2.1.3</version>
</dependency>
</dependencies>
<repositories>
@@ -130,6 +147,45 @@
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<configuration>
<compilerId>javac-with-errorprone</compilerId>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
<source>1.8</source>
<target>1.8</target>
<showWarnings>true</showWarnings>
<annotationProcessorPaths>
<path>
<groupId>com.uber.nullaway</groupId>
<artifactId>nullaway</artifactId>
<version>0.3.0</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<!-- NullAway will warn by default, uncomment the next line to make the build fail -->
<!-- <arg>-Xep:NullAway:ERROR</arg> -->
<arg>-XepExcludedPaths:(.*)/test/.*|(.*)/jcabi/.*</arg>
<arg>-XepOpt:NullAway:AnnotatedPackages=com.baeldung.nullaway</arg>
</compilerArgs>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-javac-errorprone</artifactId>
<version>2.8</version>
</dependency>
<!-- override plexus-compiler-javac-errorprone's dependency on
Error Prone with the latest version -->
<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_core</artifactId>
<version>2.3.4</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<resources>
<resource>
@@ -1,7 +1,5 @@
package com.baeldung.nullaway;
import com.baeldung.distinct.Person;
public class NullAwayExample {
/*
@@ -0,0 +1,65 @@
package com.baeldung.nullaway;
public class Person {
int age;
String name;
String email;
public Person(int age, String name, String email) {
super();
this.age = age;
this.name = name;
this.email = email;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Person [age=");
builder.append(age);
builder.append(", name=");
builder.append(name);
builder.append(", email=");
builder.append(email);
builder.append("]");
return builder.toString();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((email == null) ? 0 : email.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
return true;
}
}
+1
View File
@@ -9,3 +9,4 @@ This module contains articles about IO data processing libraries.
- [Introduction To OpenCSV](https://www.baeldung.com/opencsv)
- [Interact with Google Sheets from Java](https://www.baeldung.com/google-sheets-java-client)
- [Introduction To Docx4J](https://www.baeldung.com/docx4j)
- [Breaking YAML Strings Over Multiple Lines](https://www.baeldung.com/yaml-multi-line)

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