Merge branch 'eugenp:master' into JAVA-20167_1
This commit is contained in:
@@ -10,7 +10,7 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
|
||||
- [Converting Between Roman and Arabic Numerals in Java](https://www.baeldung.com/java-convert-roman-arabic)
|
||||
- [Practical Java Examples of the Big O Notation](https://www.baeldung.com/java-algorithm-complexity)
|
||||
- [Checking If a List Is Sorted in Java](https://www.baeldung.com/java-check-if-list-sorted)
|
||||
- [Checking if a Java Graph has a Cycle](https://www.baeldung.com/java-graph-has-a-cycle)
|
||||
- [Checking if a Java Graph Has a Cycle](https://www.baeldung.com/java-graph-has-a-cycle)
|
||||
- [A Guide to the Folding Technique in Java](https://www.baeldung.com/folding-hashing-technique)
|
||||
- [Creating a Triangle with for Loops in Java](https://www.baeldung.com/java-print-triangle)
|
||||
- [The K-Means Clustering Algorithm in Java](https://www.baeldung.com/java-k-means-clustering-algorithm)
|
||||
|
||||
@@ -5,10 +5,10 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
|
||||
### Relevant articles:
|
||||
|
||||
- [Multi-Swarm Optimization Algorithm in Java](https://www.baeldung.com/java-multi-swarm-algorithm)
|
||||
- [Check If a String Contains All The Letters of The Alphabet with Java](https://www.baeldung.com/java-string-contains-all-letters)
|
||||
- [Check if a String Contains All the Letters of the Alphabet With Java](https://www.baeldung.com/java-string-contains-all-letters)
|
||||
- [Find the Middle Element of a Linked List in Java](https://www.baeldung.com/java-linked-list-middle-element)
|
||||
- [Find Substrings That Are Palindromes in Java](https://www.baeldung.com/java-palindrome-substrings)
|
||||
- [Find the Longest Substring without Repeating Characters](https://www.baeldung.com/java-longest-substring-without-repeated-characters)
|
||||
- [Find the Longest Substring Without Repeating Characters](https://www.baeldung.com/java-longest-substring-without-repeated-characters)
|
||||
- [Permutations of an Array in Java](https://www.baeldung.com/java-array-permutations)
|
||||
- [Find the Smallest Missing Integer in an Array](https://www.baeldung.com/java-smallest-missing-integer-in-array)
|
||||
- [Permutations of a String in Java](https://www.baeldung.com/java-string-permutations)
|
||||
|
||||
@@ -9,7 +9,7 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
|
||||
- [Reversing a Binary Tree in Java](https://www.baeldung.com/java-reversing-a-binary-tree)
|
||||
- [Find If Two Numbers Are Relatively Prime in Java](https://www.baeldung.com/java-two-relatively-prime-numbers)
|
||||
- [Knapsack Problem Implementation in Java](https://www.baeldung.com/java-knapsack)
|
||||
- [How to Determine if a Binary Tree is Balanced in Java](https://www.baeldung.com/java-balanced-binary-tree)
|
||||
- [How to Determine if a Binary Tree Is Balanced in Java](https://www.baeldung.com/java-balanced-binary-tree)
|
||||
- [Overview of Combinatorial Problems in Java](https://www.baeldung.com/java-combinatorial-algorithms)
|
||||
- [Prim’s Algorithm with a Java Implementation](https://www.baeldung.com/java-prim-algorithm)
|
||||
- [Maximum Subarray Problem in Java](https://www.baeldung.com/java-maximum-subarray)
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Server-Sent Events (SSE) In JAX-RS](https://www.baeldung.com/java-ee-jax-rs-sse)
|
||||
- [Server-Sent Events (SSE) in JAX-RS](https://www.baeldung.com/java-ee-jax-rs-sse)
|
||||
|
||||
@@ -13,5 +13,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
- [How To Get Cookies From the Apache HttpClient Response](https://www.baeldung.com/java-apache-httpclient-cookies)
|
||||
- [Enabling Logging for Apache HttpClient](https://www.baeldung.com/apache-httpclient-enable-logging)
|
||||
- [Apache HttpClient vs. CloseableHttpClient](https://www.baeldung.com/apache-httpclient-vs-closeablehttpclient)
|
||||
- [Expand Shortened URLs with Apache HttpClient](https://www.baeldung.com/apache-httpclient-expand-url)
|
||||
- More articles: [[<-- prev]](../apache-httpclient)
|
||||
|
||||
@@ -8,7 +8,7 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
|
||||
### Relevant Articles:
|
||||
- [Apache HttpClient – Cancel Request](https://www.baeldung.com/httpclient-cancel-request)
|
||||
- [Apache HttpClient 4 Cookbook](https://www.baeldung.com/httpclient4)
|
||||
- [Apache HttpClient Cookbook](https://www.baeldung.com/apache-httpclient-cookbook)
|
||||
- [Apache HttpClient – Follow Redirects for POST](https://www.baeldung.com/httpclient-redirect-on-http-post)
|
||||
- [Multipart Upload with Apache HttpClient](https://www.baeldung.com/httpclient-multipart-upload)
|
||||
- [Apache HttpAsyncClient Tutorial](https://www.baeldung.com/httpasyncclient-tutorial)
|
||||
|
||||
@@ -21,7 +21,7 @@ public class GetRequestMockServer {
|
||||
public static String serviceOneUrl;
|
||||
public static String serviceTwoUrl;
|
||||
|
||||
private static int serverPort;
|
||||
public static int serverPort;
|
||||
|
||||
public static final String SERVER_ADDRESS = "127.0.0.1";
|
||||
public static final String PATH_ONE = "/test1";
|
||||
@@ -29,9 +29,8 @@ public class GetRequestMockServer {
|
||||
public static final String METHOD = "GET";
|
||||
|
||||
@BeforeAll
|
||||
static void startServer() throws IOException, URISyntaxException {
|
||||
//serverPort = getFreePort();
|
||||
serverPort = 8080;
|
||||
static void startServer() throws IOException {
|
||||
serverPort = getFreePort();
|
||||
System.out.println("Free port "+serverPort);
|
||||
serviceOneUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_ONE;
|
||||
serviceTwoUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_TWO;
|
||||
|
||||
+10
-24
@@ -9,6 +9,7 @@ import java.util.concurrent.Future;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
|
||||
import org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
|
||||
@@ -55,12 +56,12 @@ class HttpAsyncClientLiveTest extends GetRequestMockServer {
|
||||
|
||||
@Test
|
||||
void whenUseHttpAsyncClient_thenCorrect() throws InterruptedException, ExecutionException, IOException {
|
||||
final HttpHost target = new HttpHost(HOST);
|
||||
final HttpHost target = new HttpHost(HOST_WITH_COOKIE);
|
||||
final SimpleHttpRequest request = SimpleRequestBuilder.get()
|
||||
.setHttpHost(target)
|
||||
.build();
|
||||
|
||||
final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
|
||||
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().build();
|
||||
client.start();
|
||||
|
||||
|
||||
@@ -102,30 +103,15 @@ class HttpAsyncClientLiveTest extends GetRequestMockServer {
|
||||
|
||||
@Test
|
||||
void whenUseProxyWithHttpClient_thenCorrect() throws Exception {
|
||||
final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
|
||||
final HttpHost proxy = new HttpHost("127.0.0.1", GetRequestMockServer.serverPort);
|
||||
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
|
||||
final CloseableHttpAsyncClient client = HttpAsyncClients.custom()
|
||||
.setRoutePlanner(routePlanner)
|
||||
.build();
|
||||
client.start();
|
||||
final HttpHost proxy = new HttpHost("127.0.0.1", 8080);
|
||||
final RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
|
||||
|
||||
final SimpleHttpRequest request = new SimpleHttpRequest("GET" ,HOST_WITH_PROXY);
|
||||
request.setConfig(config);
|
||||
final Future<SimpleHttpResponse> future = client.execute(request, new FutureCallback<>(){
|
||||
@Override
|
||||
public void completed(SimpleHttpResponse response) {
|
||||
|
||||
System.out.println("responseData");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(Exception ex) {
|
||||
System.out.println("Error executing HTTP request: " + ex.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelled() {
|
||||
System.out.println("HTTP request execution cancelled");
|
||||
}
|
||||
});
|
||||
|
||||
final Future<SimpleHttpResponse> future = client.execute(request, null);
|
||||
final HttpResponse response = future.get();
|
||||
assertThat(response.getCode(), equalTo(200));
|
||||
client.close();
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.baeldung.httpclient;
|
||||
|
||||
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
|
||||
import static org.mockserver.matchers.Times.exactly;
|
||||
import static org.mockserver.model.HttpRequest.request;
|
||||
import static org.mockserver.model.HttpResponse.response;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.mockserver.client.MockServerClient;
|
||||
import org.mockserver.integration.ClientAndServer;
|
||||
|
||||
public class GetRequestMockServer {
|
||||
|
||||
public static ClientAndServer mockServer;
|
||||
public static String serviceOneUrl;
|
||||
public static String serviceTwoUrl;
|
||||
|
||||
public static int serverPort;
|
||||
|
||||
public static final String SERVER_ADDRESS = "127.0.0.1";
|
||||
public static final String PATH_ONE = "/test1";
|
||||
public static final String PATH_TWO = "/test2";
|
||||
public static final String METHOD = "GET";
|
||||
|
||||
@BeforeAll
|
||||
static void startServer() throws IOException, URISyntaxException {
|
||||
serverPort = getFreePort();
|
||||
serviceOneUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_ONE;
|
||||
serviceTwoUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_TWO;
|
||||
mockServer = startClientAndServer(serverPort);
|
||||
mockGetRequest();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void stopServer() {
|
||||
mockServer.stop();
|
||||
}
|
||||
|
||||
private static void mockGetRequest() {
|
||||
new MockServerClient(SERVER_ADDRESS, serverPort)
|
||||
.when(
|
||||
request()
|
||||
.withPath(PATH_ONE)
|
||||
.withMethod(METHOD),
|
||||
exactly(5)
|
||||
)
|
||||
.respond(
|
||||
response()
|
||||
.withStatusCode(HttpStatus.SC_OK)
|
||||
.withBody("{\"status\":\"ok\"}")
|
||||
);
|
||||
new MockServerClient(SERVER_ADDRESS, serverPort)
|
||||
.when(
|
||||
request()
|
||||
.withPath(PATH_TWO)
|
||||
.withMethod(METHOD),
|
||||
exactly(1)
|
||||
)
|
||||
.respond(
|
||||
response()
|
||||
.withStatusCode(HttpStatus.SC_OK)
|
||||
.withBody("{\"status\":\"ok\"}")
|
||||
);
|
||||
}
|
||||
|
||||
private static int getFreePort () throws IOException {
|
||||
try (ServerSocket serverSocket = new ServerSocket(0)) {
|
||||
return serverSocket.getLocalPort();
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-2
@@ -31,7 +31,8 @@ import org.apache.http.protocol.HttpContext;
|
||||
import org.apache.http.ssl.SSLContexts;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class HttpAsyncClientV4LiveTest {
|
||||
|
||||
class HttpAsyncClientV4LiveTest extends GetRequestMockServer {
|
||||
|
||||
private static final String HOST = "http://www.google.com";
|
||||
private static final String HOST_WITH_SSL = "https://mms.nw.ru/";
|
||||
@@ -87,7 +88,7 @@ class HttpAsyncClientV4LiveTest {
|
||||
void whenUseProxyWithHttpClient_thenCorrect() throws Exception {
|
||||
final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
|
||||
client.start();
|
||||
final HttpHost proxy = new HttpHost("127.0.0.1", 8080);
|
||||
final HttpHost proxy = new HttpHost("127.0.0.1", GetRequestMockServer.serverPort);
|
||||
final RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
|
||||
final HttpGet request = new HttpGet(HOST_WITH_PROXY);
|
||||
request.setConfig(config);
|
||||
|
||||
@@ -9,3 +9,5 @@ You can build the project from the command line using: *mvn clean install*, or i
|
||||
- [Guide to Check if Apache Kafka Server Is Running](https://www.baeldung.com/apache-kafka-check-server-is-running)
|
||||
- [Add Custom Headers to a Kafka Message](https://www.baeldung.com/java-kafka-custom-headers)
|
||||
- [Get Last N Messages in Apache Kafka Topic](https://www.baeldung.com/java-apache-kafka-get-last-n-messages)
|
||||
- [Is a Key Required as Part of Sending Messages to Kafka?](https://www.baeldung.com/java-kafka-message-key)
|
||||
- [Read Data From the Beginning Using Kafka Consumer API](https://www.baeldung.com/java-kafka-consumer-api-read)
|
||||
|
||||
@@ -12,4 +12,5 @@ This module contains articles about Apache POI.
|
||||
- [Finding the Last Row in an Excel Spreadsheet From Java](https://www.baeldung.com/java-excel-find-last-row)
|
||||
- [Setting Formulas in Excel with Apache POI](https://www.baeldung.com/java-apache-poi-set-formulas)
|
||||
- [Set the Date Format Using Apache POI](https://www.baeldung.com/java-apache-poi-date-format)
|
||||
- [Replacing Variables in a Document Template with Java](https://www.baeldung.com/java-replace-pattern-word-document-doc-docx)
|
||||
- More articles: [[<-- prev]](../apache-poi)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>apache-poi-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
@@ -19,10 +19,15 @@
|
||||
<artifactId>poi-ooxml</artifactId>
|
||||
<version>${poi.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-scratchpad</artifactId>
|
||||
<version>${poi.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<poi.version>5.2.0</poi.version>
|
||||
<poi.version>5.2.3</poi.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.poi.replacevariables;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.apache.poi.hwpf.HWPFDocument;
|
||||
import org.apache.poi.hwpf.usermodel.Range;
|
||||
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
||||
|
||||
public class DocTextReplacer {
|
||||
|
||||
public void replaceText() throws IOException {
|
||||
String filePath = getClass().getClassLoader()
|
||||
.getResource("baeldung.doc")
|
||||
.getPath();
|
||||
try (InputStream inputStream = new FileInputStream(filePath); POIFSFileSystem fileSystem = new POIFSFileSystem(inputStream)) {
|
||||
HWPFDocument doc = new HWPFDocument(fileSystem);
|
||||
doc = replaceText(doc, "Baeldung", "Hello");
|
||||
saveFile(filePath, doc);
|
||||
doc.close();
|
||||
}
|
||||
}
|
||||
|
||||
private HWPFDocument replaceText(HWPFDocument doc, String originalText, String updatedText) {
|
||||
Range range = doc.getRange();
|
||||
range.replaceText(originalText, updatedText);
|
||||
return doc;
|
||||
}
|
||||
|
||||
private void saveFile(String filePath, HWPFDocument doc) throws IOException {
|
||||
try (FileOutputStream out = new FileOutputStream(filePath)) {
|
||||
doc.write(out);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.poi.replacevariables;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFRun;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFTable;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
|
||||
|
||||
public class DocxNaiveTextReplacer {
|
||||
|
||||
public void replaceText() throws IOException {
|
||||
String filePath = getClass().getClassLoader()
|
||||
.getResource("baeldung-copy.docx")
|
||||
.getPath();
|
||||
try (InputStream inputStream = new FileInputStream(filePath)) {
|
||||
XWPFDocument doc = new XWPFDocument(inputStream);
|
||||
doc = replaceText(doc, "Baeldung", "Hello");
|
||||
saveFile(filePath, doc);
|
||||
doc.close();
|
||||
}
|
||||
}
|
||||
|
||||
private XWPFDocument replaceText(XWPFDocument doc, String originalText, String updatedText) {
|
||||
replaceTextInParagraphs(doc.getParagraphs(), originalText, updatedText);
|
||||
for (XWPFTable tbl : doc.getTables()) {
|
||||
for (XWPFTableRow row : tbl.getRows()) {
|
||||
for (XWPFTableCell cell : row.getTableCells()) {
|
||||
replaceTextInParagraphs(cell.getParagraphs(), originalText, updatedText);
|
||||
}
|
||||
}
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
|
||||
private void replaceTextInParagraphs(List<XWPFParagraph> paragraphs, String originalText, String updatedText) {
|
||||
paragraphs.forEach(paragraph -> replaceTextInParagraph(paragraph, originalText, updatedText));
|
||||
}
|
||||
|
||||
private void replaceTextInParagraph(XWPFParagraph paragraph, String originalText, String updatedText) {
|
||||
List<XWPFRun> runs = paragraph.getRuns();
|
||||
for (XWPFRun run : runs) {
|
||||
String text = run.getText(0);
|
||||
if (text != null && text.contains(originalText)) {
|
||||
String updatedRunText = text.replace(originalText, updatedText);
|
||||
run.setText(updatedRunText, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void saveFile(String filePath, XWPFDocument doc) throws IOException {
|
||||
try (FileOutputStream out = new FileOutputStream(filePath)) {
|
||||
doc.write(out);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.baeldung.poi.replacevariables;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFRun;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFTable;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
|
||||
|
||||
public class DocxTextReplacer {
|
||||
|
||||
public void replaceText() throws IOException {
|
||||
String filePath = getClass().getClassLoader()
|
||||
.getResource("baeldung.docx")
|
||||
.getPath();
|
||||
try (InputStream inputStream = new FileInputStream(filePath)) {
|
||||
XWPFDocument doc = new XWPFDocument(inputStream);
|
||||
doc = replaceText(doc, "Baeldung", "Hello");
|
||||
saveFile(filePath, doc);
|
||||
doc.close();
|
||||
}
|
||||
}
|
||||
|
||||
private XWPFDocument replaceText(XWPFDocument doc, String originalText, String updatedText) {
|
||||
replaceTextInParagraphs(doc.getParagraphs(), originalText, updatedText);
|
||||
for (XWPFTable tbl : doc.getTables()) {
|
||||
for (XWPFTableRow row : tbl.getRows()) {
|
||||
for (XWPFTableCell cell : row.getTableCells()) {
|
||||
replaceTextInParagraphs(cell.getParagraphs(), originalText, updatedText);
|
||||
}
|
||||
}
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
|
||||
private void replaceTextInParagraphs(List<XWPFParagraph> paragraphs, String originalText, String updatedText) {
|
||||
paragraphs.forEach(paragraph -> replaceTextInParagraph(paragraph, originalText, updatedText));
|
||||
}
|
||||
|
||||
private void replaceTextInParagraph(XWPFParagraph paragraph, String originalText, String updatedText) {
|
||||
String paragraphText = paragraph.getParagraphText();
|
||||
if (paragraphText.contains(originalText)) {
|
||||
String updatedParagraphText = paragraphText.replace(originalText, updatedText);
|
||||
while (paragraph.getRuns().size() > 0) {
|
||||
paragraph.removeRun(0);
|
||||
}
|
||||
XWPFRun newRun = paragraph.createRun();
|
||||
newRun.setText(updatedParagraphText);
|
||||
}
|
||||
}
|
||||
|
||||
private void saveFile(String filePath, XWPFDocument doc) throws IOException {
|
||||
try (FileOutputStream out = new FileOutputStream(filePath)) {
|
||||
doc.write(out);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.poi.replacevariables;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.poi.hwpf.HWPFDocument;
|
||||
import org.apache.poi.hwpf.extractor.WordExtractor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class DocTextReplacerUnitTest {
|
||||
|
||||
@Test
|
||||
void whenReplaceText_ThenTextReplaced() throws IOException {
|
||||
new DocTextReplacer().replaceText();
|
||||
|
||||
String filePath = getClass().getClassLoader()
|
||||
.getResource("baeldung.doc")
|
||||
.getPath();
|
||||
try (FileInputStream fis = new FileInputStream(filePath); HWPFDocument document = new HWPFDocument(fis); WordExtractor extractor = new WordExtractor(document)) {
|
||||
long occurrencesOfHello = Arrays.stream(extractor.getText()
|
||||
.split("\\s+"))
|
||||
.filter(s -> s.contains("Hello"))
|
||||
.count();
|
||||
assertEquals(5, occurrencesOfHello);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.poi.replacevariables;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class DocxNaiveTextReplacerUnitTest {
|
||||
|
||||
@Test
|
||||
void whenReplaceText_ThenTextReplaced() throws IOException {
|
||||
new DocxNaiveTextReplacer().replaceText();
|
||||
|
||||
String filePath = getClass().getClassLoader()
|
||||
.getResource("baeldung-copy.docx")
|
||||
.getPath();
|
||||
try (FileInputStream fis = new FileInputStream(filePath); XWPFDocument document = new XWPFDocument(fis); XWPFWordExtractor extractor = new XWPFWordExtractor(document)) {
|
||||
long occurrencesOfHello = Arrays.stream(extractor.getText()
|
||||
.split("\\s+"))
|
||||
.filter(s -> s.contains("Hello"))
|
||||
.count();
|
||||
assertTrue(occurrencesOfHello < 5);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.poi.replacevariables;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class DocxTestReplacerUnitTest {
|
||||
|
||||
@Test
|
||||
void whenReplaceText_ThenTextReplaced() throws IOException {
|
||||
new DocxTextReplacer().replaceText();
|
||||
|
||||
String filePath = getClass().getClassLoader()
|
||||
.getResource("baeldung.docx")
|
||||
.getPath();
|
||||
try (FileInputStream fis = new FileInputStream(filePath); XWPFDocument document = new XWPFDocument(fis); XWPFWordExtractor extractor = new XWPFWordExtractor(document)) {
|
||||
long occurrencesOfHello = Arrays.stream(extractor.getText()
|
||||
.split("\\s+"))
|
||||
.filter(s -> s.contains("Hello"))
|
||||
.count();
|
||||
assertEquals(5, occurrencesOfHello);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -21,4 +21,4 @@ Two scripts are included to easily start middleware using Docker matching the pr
|
||||
- [Snapshotting Aggregates in Axon](https://www.baeldung.com/axon-snapshotting-aggregates)
|
||||
- [Dispatching Queries in Axon Framework](https://www.baeldung.com/axon-query-dispatching)
|
||||
- [Persisting the Query Model](https://www.baeldung.com/axon-persisting-query-model)
|
||||
- [Using and Testing Axon Applications via REST](https://www.baeldung.com/using-and-testing-axon-applications-via-rest)
|
||||
- [Using and Testing Axon Applications via REST](https://www.baeldung.com/axon-using-and-testing-rest)
|
||||
|
||||
+22
-38
@@ -19,24 +19,13 @@
|
||||
<dependency>
|
||||
<groupId>org.checkerframework</groupId>
|
||||
<artifactId>checker-qual</artifactId>
|
||||
<version>${checker-qual.version}</version>
|
||||
<version>${checker.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.checkerframework</groupId>
|
||||
<artifactId>checker</artifactId>
|
||||
<version>${checker.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.checkerframework</groupId>
|
||||
<artifactId>jdk8</artifactId>
|
||||
<version>${jdk8.version}</version>
|
||||
</dependency>
|
||||
<!-- The Type Annotations compiler. Uncomment if using annotations in comments. -->
|
||||
<dependency>
|
||||
<groupId>org.checkerframework</groupId>
|
||||
<artifactId>compiler</artifactId>
|
||||
<version>${checkerframework.compiler.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -45,32 +34,34 @@
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
<!-- Uncomment the following line to use the type annotations compiler. -->
|
||||
<!-- <fork>true</fork> -->
|
||||
<compilerArguments>
|
||||
<Xmaxerrs>10000</Xmaxerrs>
|
||||
<Xmaxwarns>10000</Xmaxwarns>
|
||||
</compilerArguments>
|
||||
<fork>true</fork>
|
||||
<compilerArgument>-Xlint:all</compilerArgument>
|
||||
<showWarnings>true</showWarnings>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.checkerframework</groupId>
|
||||
<artifactId>checker</artifactId>
|
||||
<version>${checker.version}</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
<annotationProcessors>
|
||||
<!-- Add all the checkers you want to enable here -->
|
||||
<annotationProcessor>org.checkerframework.checker.nullness.NullnessChecker</annotationProcessor>
|
||||
<annotationProcessor>org.checkerframework.checker.interning.InterningChecker</annotationProcessor>
|
||||
<annotationProcessor>org.checkerframework.checker.fenum.FenumChecker</annotationProcessor>
|
||||
<annotationProcessor>org.checkerframework.checker.formatter.FormatterChecker</annotationProcessor>
|
||||
<annotationProcessor>org.checkerframework.checker.regex.RegexChecker</annotationProcessor>
|
||||
</annotationProcessors>
|
||||
<compilerArgs>
|
||||
<arg>-AprintErrorStack</arg>
|
||||
|
||||
<!-- location of the annotated JDK, which comes from a Maven dependency -->
|
||||
<arg>-Xbootclasspath/p:${annotatedJdk}</arg>
|
||||
<!-- -->
|
||||
<!-- Uncomment the following line to use the type annotations compiler. -->
|
||||
<!-- <arg>-J-Xbootclasspath/p:${typeAnnotationsJavac}</arg> -->
|
||||
<!-- Uncomment the following line to turn type-checking warnings into errors. -->
|
||||
<compilerArgs combine.children="append">
|
||||
<arg>-Awarns</arg>
|
||||
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg>
|
||||
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
|
||||
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
|
||||
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
|
||||
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED</arg>
|
||||
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED</arg>
|
||||
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
|
||||
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>
|
||||
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
|
||||
</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
@@ -78,14 +69,7 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- These properties will be set by the Maven Dependency plugin -->
|
||||
<annotatedJdk>${org.checkerframework:jdk8:jar}</annotatedJdk>
|
||||
<!-- Uncomment to use the Type Annotations compiler. -->
|
||||
<!-- <typeAnnotationsJavac>${org.checkerframework:compiler:jar}</typeAnnotationsJavac> -->
|
||||
<checker-qual.version>2.3.1</checker-qual.version>
|
||||
<checker.version>2.3.1</checker.version>
|
||||
<jdk8.version>2.3.1</jdk8.version>
|
||||
<checkerframework.compiler.version>2.3.1</checkerframework.compiler.version>
|
||||
<checker.version>3.35.0</checker.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.daysinmonth;
|
||||
|
||||
import java.time.YearMonth;
|
||||
import java.util.Calendar;
|
||||
|
||||
public class DaysInMonthUtils {
|
||||
|
||||
public int getDaysInMonthWithYearOfMonth(int month, int year) {
|
||||
YearMonth yearMonth = YearMonth.of(year, month);
|
||||
return yearMonth.lengthOfMonth();
|
||||
}
|
||||
|
||||
public int getDaysInMonthWithCalendar(int month, int year) {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(Calendar.DAY_OF_MONTH, 1);
|
||||
calendar.set(Calendar.YEAR, year);
|
||||
calendar.set(Calendar.MONTH, month - 1);
|
||||
return calendar.getActualMaximum(Calendar.DATE);
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.daysinmonth;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class DaysInMonthUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
void whenGetDaysInMonthWithYearOfMonth_thenCorrectResult() {
|
||||
assertEquals(31, new DaysInMonthUtils().getDaysInMonthWithYearOfMonth(3, 2024));
|
||||
assertEquals(30, new DaysInMonthUtils().getDaysInMonthWithYearOfMonth(11, 1999));
|
||||
assertEquals(28, new DaysInMonthUtils().getDaysInMonthWithYearOfMonth(2, 2025));
|
||||
assertEquals(29, new DaysInMonthUtils().getDaysInMonthWithYearOfMonth(2, 2004));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenGetDaysInMonthWithCalendar_thenCorrectResult() {
|
||||
assertEquals(31, new DaysInMonthUtils().getDaysInMonthWithCalendar(3, 2024));
|
||||
assertEquals(30, new DaysInMonthUtils().getDaysInMonthWithCalendar(11, 1999));
|
||||
assertEquals(28, new DaysInMonthUtils().getDaysInMonthWithCalendar(2, 2025));
|
||||
assertEquals(29, new DaysInMonthUtils().getDaysInMonthWithCalendar(2, 2004));
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.spliteratorAPI;
|
||||
package com.baeldung.spliterator;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.spliteratorAPI;
|
||||
package com.baeldung.spliterator;
|
||||
|
||||
public class Author {
|
||||
private String name;
|
||||
+1
-8
@@ -1,7 +1,5 @@
|
||||
package com.baeldung.spliteratorAPI;
|
||||
package com.baeldung.spliterator;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Executor {
|
||||
@@ -11,9 +9,4 @@ public class Executor {
|
||||
RelatedAuthorCounter::accumulate, RelatedAuthorCounter::combine);
|
||||
return wordCounter.getCounter();
|
||||
}
|
||||
|
||||
public static List<Article> generateElements() {
|
||||
return Stream.generate(() -> new Article("Java")).limit(35000).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.spliteratorAPI;
|
||||
package com.baeldung.spliterator;
|
||||
|
||||
public class RelatedAuthorCounter {
|
||||
private final int counter;
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user