@@ -11,3 +11,4 @@ This module contains articles about Apache POI
|
||||
- [Get String Value of Excel Cell with Apache POI](https://www.baeldung.com/java-apache-poi-cell-string-value)
|
||||
- [Read Excel Cell Value Rather Than Formula With Apache POI](https://www.baeldung.com/apache-poi-read-cell-value-formula)
|
||||
- [Setting Formulas in Excel with Apache POI](https://www.baeldung.com/java-apache-poi-set-formulas)
|
||||
- [Insert a Row in Excel Using Apache POI](https://www.baeldung.com/apache-poi-insert-excel-row)
|
||||
|
||||
@@ -5,3 +5,4 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [ArrayList vs. LinkedList vs. HashMap in Java](https://www.baeldung.com/java-arraylist-vs-linkedlist-vs-hashmap)
|
||||
- [Java Deque vs. Stack](https://www.baeldung.com/java-deque-vs-stack)
|
||||
|
||||
@@ -8,4 +8,5 @@ This module contains articles about Map data structures in Java.
|
||||
- [The Map.computeIfAbsent() Method](https://www.baeldung.com/java-map-computeifabsent)
|
||||
- [Collections.synchronizedMap vs. ConcurrentHashMap](https://www.baeldung.com/java-synchronizedmap-vs-concurrenthashmap)
|
||||
- [Java HashMap Load Factor](https://www.baeldung.com/java-hashmap-load-factor)
|
||||
- [Converting java.util.Properties to HashMap](https://www.baeldung.com/java-convert-properties-to-hashmap)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-maps-2)
|
||||
|
||||
+2
-2
@@ -5,12 +5,12 @@ import com.google.common.io.CharSource;
|
||||
import com.google.common.io.Files;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.io.input.ReaderInputStream;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class JavaXToInputStreamUnitTest {
|
||||
protected final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
@@ -28,7 +28,7 @@ public class JavaXToInputStreamUnitTest {
|
||||
@Test
|
||||
public final void givenUsingGuava_whenConvertingStringToInputStream_thenCorrect() throws IOException {
|
||||
final String initialString = "text";
|
||||
final InputStream targetStream = new ReaderInputStream(CharSource.wrap(initialString).openStream());
|
||||
final InputStream targetStream = CharSource.wrap(initialString).asByteSource(StandardCharsets.UTF_8).openStream();
|
||||
|
||||
IOUtils.closeQuietly(targetStream);
|
||||
}
|
||||
|
||||
@@ -14,6 +14,15 @@
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-reflection-2</finalName>
|
||||
<resources>
|
||||
@@ -40,5 +49,6 @@
|
||||
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
|
||||
<source.version>1.8</source.version>
|
||||
<target.version>1.8</target.version>
|
||||
<spring.version>5.3.4</spring.version>
|
||||
</properties>
|
||||
</project>
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.reflection.access.privatemethods;
|
||||
|
||||
public class LongArrayUtil {
|
||||
|
||||
public static int indexOf(long[] array, long target) {
|
||||
return indexOf(array, target, 0, array.length);
|
||||
}
|
||||
|
||||
private static int indexOf(long[] array, long target, int start, int end) {
|
||||
for (int i = start; i < end; i++) {
|
||||
if (array[i] == target) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.reflection.access.privatemethods;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class InvokePrivateMethodsUnitTest {
|
||||
|
||||
private final long[] someLongArray = new long[] { 1L, 2L, 1L, 4L, 2L };
|
||||
|
||||
@Test
|
||||
void whenSearchingForLongValueInSubsequenceUsingReflection_thenTheCorrectIndexOfTheValueIsReturned() throws Exception {
|
||||
Method indexOfMethod = LongArrayUtil.class.getDeclaredMethod("indexOf", long[].class, long.class, int.class, int.class);
|
||||
indexOfMethod.setAccessible(true);
|
||||
|
||||
assertEquals(2, indexOfMethod.invoke(LongArrayUtil.class, someLongArray, 1L, 1, someLongArray.length), "The index should be 2.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenSearchingForLongValueInSubsequenceUsingSpring_thenTheCorrectIndexOfTheValueIsReturned() throws Exception {
|
||||
int indexOfSearchTarget = ReflectionTestUtils.invokeMethod(LongArrayUtil.class, "indexOf", someLongArray, 1L, 1, someLongArray.length);
|
||||
assertEquals(2, indexOfSearchTarget, "The index should be 2.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,4 +15,5 @@ This module contains articles about core Java Security
|
||||
- [Security Context Basics: User, Subject and Principal](https://www.baeldung.com/security-context-basics)
|
||||
- [Java AES Encryption and Decryption](https://www.baeldung.com/java-aes-encryption-decryption)
|
||||
- [InvalidAlgorithmParameterException: Wrong IV Length](https://www.baeldung.com/java-invalidalgorithmparameter-exception)
|
||||
- [The java.security.egd JVM Option](https://www.baeldung.com/java-security-egd)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-security)
|
||||
|
||||
+23
-18
@@ -5,7 +5,7 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
public class CircularLinkedList {
|
||||
|
||||
final Logger LOGGER = LoggerFactory.getLogger(CircularLinkedList.class);
|
||||
final Logger logger = LoggerFactory.getLogger(CircularLinkedList.class);
|
||||
|
||||
private Node head = null;
|
||||
private Node tail = null;
|
||||
@@ -42,24 +42,29 @@ public class CircularLinkedList {
|
||||
}
|
||||
|
||||
public void deleteNode(int valueToDelete) {
|
||||
|
||||
Node currentNode = head;
|
||||
|
||||
if (head != null) {
|
||||
if (currentNode.value == valueToDelete) {
|
||||
head = head.nextNode;
|
||||
tail.nextNode = head;
|
||||
} else {
|
||||
do {
|
||||
Node nextNode = currentNode.nextNode;
|
||||
if (nextNode.value == valueToDelete) {
|
||||
currentNode.nextNode = nextNode.nextNode;
|
||||
break;
|
||||
}
|
||||
currentNode = currentNode.nextNode;
|
||||
} while (currentNode != head);
|
||||
}
|
||||
if (head == null) {
|
||||
return;
|
||||
}
|
||||
do {
|
||||
Node nextNode = currentNode.nextNode;
|
||||
if (nextNode.value == valueToDelete) {
|
||||
if (tail == head) {
|
||||
head = null;
|
||||
tail = null;
|
||||
} else {
|
||||
currentNode.nextNode = nextNode.nextNode;
|
||||
if (head == nextNode) {
|
||||
head = head.nextNode;
|
||||
}
|
||||
if (tail == nextNode) {
|
||||
tail = currentNode;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
currentNode = nextNode;
|
||||
} while (currentNode != head);
|
||||
}
|
||||
|
||||
public void traverseList() {
|
||||
@@ -68,7 +73,7 @@ public class CircularLinkedList {
|
||||
|
||||
if (head != null) {
|
||||
do {
|
||||
LOGGER.info(currentNode.value + " ");
|
||||
logger.info(currentNode.value + " ");
|
||||
currentNode = currentNode.nextNode;
|
||||
} while (currentNode != head);
|
||||
}
|
||||
|
||||
+29
-3
@@ -1,10 +1,10 @@
|
||||
package com.baeldung.circularlinkedlist;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CircularLinkedListUnitTest {
|
||||
|
||||
@Test
|
||||
@@ -23,7 +23,7 @@ public class CircularLinkedListUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenACircularLinkedList_WhenDeletingElements_ThenListDoesNotContainThoseElements() {
|
||||
public void givenACircularLinkedList_WhenDeletingInOrderHeadMiddleTail_ThenListDoesNotContainThoseElements() {
|
||||
CircularLinkedList cll = createCircularLinkedList();
|
||||
|
||||
assertTrue(cll.containsNode(13));
|
||||
@@ -39,6 +39,32 @@ public class CircularLinkedListUnitTest {
|
||||
assertFalse(cll.containsNode(46));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenACircularLinkedList_WhenDeletingInOrderTailMiddleHead_ThenListDoesNotContainThoseElements() {
|
||||
CircularLinkedList cll = createCircularLinkedList();
|
||||
|
||||
assertTrue(cll.containsNode(46));
|
||||
cll.deleteNode(46);
|
||||
assertFalse(cll.containsNode(46));
|
||||
|
||||
assertTrue(cll.containsNode(1));
|
||||
cll.deleteNode(1);
|
||||
assertFalse(cll.containsNode(1));
|
||||
|
||||
assertTrue(cll.containsNode(13));
|
||||
cll.deleteNode(13);
|
||||
assertFalse(cll.containsNode(13));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenACircularLinkedListWithOneNode_WhenDeletingElement_ThenListDoesNotContainTheElement() {
|
||||
CircularLinkedList cll = new CircularLinkedList();
|
||||
cll.addNode(1);
|
||||
cll.deleteNode(1);
|
||||
assertFalse(cll.containsNode(1));
|
||||
}
|
||||
|
||||
|
||||
private CircularLinkedList createCircularLinkedList() {
|
||||
CircularLinkedList cll = new CircularLinkedList();
|
||||
|
||||
|
||||
+2
-2
@@ -1,12 +1,12 @@
|
||||
package com.baeldung.jackson.bidirection;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonManagedReference;
|
||||
import com.fasterxml.jackson.annotation.JsonBackReference;
|
||||
|
||||
public class ItemWithRef {
|
||||
public int id;
|
||||
public String itemName;
|
||||
|
||||
@JsonManagedReference
|
||||
@JsonBackReference
|
||||
public UserWithRef owner;
|
||||
|
||||
public ItemWithRef() {
|
||||
|
||||
+3
-3
@@ -3,13 +3,13 @@ package com.baeldung.jackson.bidirection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonBackReference;
|
||||
import com.fasterxml.jackson.annotation.JsonManagedReference;
|
||||
|
||||
public class UserWithRef {
|
||||
public int id;
|
||||
public String name;
|
||||
|
||||
@JsonBackReference
|
||||
@JsonManagedReference
|
||||
public List<ItemWithRef> userItems;
|
||||
|
||||
public UserWithRef() {
|
||||
@@ -19,7 +19,7 @@ public class UserWithRef {
|
||||
public UserWithRef(final int id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
userItems = new ArrayList<ItemWithRef>();
|
||||
userItems = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addItem(final ItemWithRef item) {
|
||||
|
||||
+31
-6
@@ -1,7 +1,9 @@
|
||||
package com.baeldung.jackson.bidirection;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@@ -16,7 +18,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class JacksonBidirectionRelationUnitTest {
|
||||
|
||||
@Test(expected = JsonMappingException.class)
|
||||
@Test (expected = JsonMappingException.class)
|
||||
public void givenBidirectionRelation_whenSerializing_thenException() throws JsonProcessingException {
|
||||
final User user = new User(1, "John");
|
||||
final Item item = new Item(2, "book", user);
|
||||
@@ -26,16 +28,39 @@ public class JacksonBidirectionRelationUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBidirectionRelation_whenUsingJacksonReferenceAnnotation_thenCorrect() throws JsonProcessingException {
|
||||
public void givenBidirectionRelation_whenUsingJacksonReferenceAnnotationWithSerialization_thenCorrect() throws JsonProcessingException {
|
||||
final UserWithRef user = new UserWithRef(1, "John");
|
||||
final ItemWithRef item = new ItemWithRef(2, "book", user);
|
||||
user.addItem(item);
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(item);
|
||||
final String itemJson = new ObjectMapper().writeValueAsString(item);
|
||||
final String userJson = new ObjectMapper().writeValueAsString(user);
|
||||
|
||||
assertThat(result, containsString("book"));
|
||||
assertThat(result, containsString("John"));
|
||||
assertThat(result, not(containsString("userItems")));
|
||||
assertThat(itemJson, containsString("book"));
|
||||
assertThat(itemJson, not(containsString("John")));
|
||||
|
||||
assertThat(userJson, containsString("John"));
|
||||
assertThat(userJson, containsString("userItems"));
|
||||
assertThat(userJson, containsString("book"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBidirectionRelation_whenUsingJacksonReferenceAnnotationWithDeserialization_thenCorrect() throws JsonProcessingException {
|
||||
final UserWithRef user = new UserWithRef(1, "John");
|
||||
final ItemWithRef item = new ItemWithRef(2, "book", user);
|
||||
user.addItem(item);
|
||||
|
||||
final String itemJson = new ObjectMapper().writeValueAsString(item);
|
||||
final String userJson = new ObjectMapper().writeValueAsString(user);
|
||||
|
||||
final ItemWithRef itemRead = new ObjectMapper().readValue(itemJson, ItemWithRef.class);
|
||||
final UserWithRef userRead = new ObjectMapper().readValue(userJson, UserWithRef.class);
|
||||
|
||||
assertThat(itemRead.itemName, is("book"));
|
||||
assertThat(itemRead.owner, nullValue());
|
||||
|
||||
assertThat(userRead.name, is("John"));
|
||||
assertThat(userRead.userItems.get(0).itemName, is("book"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -3,3 +3,4 @@
|
||||
- [Java Map With Case-Insensitive Keys](https://www.baeldung.com/java-map-with-case-insensitive-keys)
|
||||
- [Using a Byte Array as Map Key in Java](https://www.baeldung.com/java-map-key-byte-array)
|
||||
- [Using the Map.Entry Java Class](https://www.baeldung.com/java-map-entry)
|
||||
- [Optimizing HashMap’s Performance](https://www.baeldung.com/java-hashmap-optimize-performance)
|
||||
|
||||
@@ -52,3 +52,4 @@ Enjoy it :)
|
||||
|
||||
- [Intro to Performance Testing using JMeter](https://www.baeldung.com/jmeter)
|
||||
- [Configure Jenkins to Run and Show JMeter Tests](https://www.baeldung.com/jenkins-and-jmeter)
|
||||
- [Write Extracted Data to a File Using JMeter](https://www.baeldung.com/jmeter-write-to-file)
|
||||
|
||||
@@ -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">
|
||||
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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>libraries-http-2</artifactId>
|
||||
<name>libraries-http-2</name>
|
||||
@@ -13,12 +13,17 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- Dependencies for response decoder with okhttp -->
|
||||
<!-- Dependencies for response decoder with okhttp -->
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>${okhttp.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>logging-interceptor</artifactId>
|
||||
<version>${okhttp.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
@@ -81,9 +86,9 @@
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<okhttp.version>3.14.2</okhttp.version>
|
||||
<okhttp.version>4.9.1</okhttp.version>
|
||||
<gson.version>2.8.5</gson.version>
|
||||
<mockwebserver.version>3.14.2</mockwebserver.version>
|
||||
<mockwebserver.version>4.9.1</mockwebserver.version>
|
||||
<jetty.httpclient.version>1.0.3</jetty.httpclient.version>
|
||||
<jetty.server.version>9.4.19.v20190610</jetty.server.version>
|
||||
<rxjava2.version>2.2.11</rxjava2.version>
|
||||
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.okhttp.interceptors;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class CacheControlResponeInterceptor implements Interceptor {
|
||||
|
||||
@Override
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
Response response = chain.proceed(chain.request());
|
||||
return response.newBuilder()
|
||||
.header("Cache-Control", "no-store")
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.okhttp.interceptors;
|
||||
|
||||
public class ErrorMessage {
|
||||
|
||||
private final int status;
|
||||
private final String detail;
|
||||
|
||||
public ErrorMessage(int status, String detail) {
|
||||
this.status = status;
|
||||
this.detail = detail;
|
||||
}
|
||||
|
||||
public int getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public String getDetail() {
|
||||
return detail;
|
||||
}
|
||||
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.okhttp.interceptors;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
public class ErrorResponseInterceptor implements Interceptor {
|
||||
|
||||
public static final MediaType APPLICATION_JSON = MediaType.get("application/json; charset=utf-8");
|
||||
|
||||
@Override
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
Response response = chain.proceed(chain.request());
|
||||
|
||||
if(!response.isSuccessful()) {
|
||||
Gson gson = new Gson();
|
||||
String body = gson.toJson(new ErrorMessage(response.code(), "The response from the server was not OK"));
|
||||
ResponseBody responseBody = ResponseBody.create(body, APPLICATION_JSON);
|
||||
|
||||
return response.newBuilder()
|
||||
.body(responseBody)
|
||||
.build();
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.okhttp.interceptors;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class SimpleLoggingInterceptor implements Interceptor {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleLoggingInterceptor.class);
|
||||
|
||||
@Override
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
Request request = chain.request();
|
||||
|
||||
LOGGER.info("Intercepted headers: {} from URL: {}", request.headers(), request.url());
|
||||
|
||||
return chain.proceed(request);
|
||||
}
|
||||
|
||||
}
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
package com.baeldung.okhttp.interceptors;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.logging.HttpLoggingInterceptor;
|
||||
import okhttp3.logging.HttpLoggingInterceptor.Level;
|
||||
import okhttp3.mockwebserver.MockResponse;
|
||||
import okhttp3.mockwebserver.MockWebServer;
|
||||
|
||||
public class InterceptorIntegrationTest {
|
||||
|
||||
@Rule
|
||||
public MockWebServer server = new MockWebServer();
|
||||
|
||||
@Test
|
||||
public void givenSimpleLogginInterceptor_whenRequestSent_thenHeadersLogged() throws IOException {
|
||||
server.enqueue(new MockResponse().setBody("Hello Baeldung Readers!"));
|
||||
|
||||
OkHttpClient client = new OkHttpClient.Builder()
|
||||
.addNetworkInterceptor(new SimpleLoggingInterceptor())
|
||||
.build();
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/greeting"))
|
||||
.header("User-Agent", "A Baeldung Reader")
|
||||
.build();
|
||||
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
assertEquals("Response code should be: ", 200, response.code());
|
||||
assertEquals("Body should be: ", "Hello Baeldung Readers!", response.body().string());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResponseInterceptor_whenRequestSent_thenCacheControlSetToNoStore() throws IOException {
|
||||
server.enqueue(new MockResponse().setBody("Hello Baeldung Readers!"));
|
||||
|
||||
OkHttpClient client = new OkHttpClient.Builder()
|
||||
.addInterceptor(getHttpLogger())
|
||||
.addInterceptor(new CacheControlResponeInterceptor())
|
||||
.build();
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/greeting"))
|
||||
.header("User-Agent", "A Baeldung Reader")
|
||||
.build();
|
||||
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
assertEquals("Response code should be: ", 200, response.code());
|
||||
assertEquals("Body should be: ", "Hello Baeldung Readers!", response.body().string());
|
||||
assertEquals("Response cache-control should be", "no-store", response.header("Cache-Control"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenErrorResponseInterceptor_whenResponseIs500_thenBodyIsJsonWithStatus() throws IOException {
|
||||
server.enqueue(new MockResponse().setResponseCode(500).setBody("Hello Baeldung Readers!"));
|
||||
|
||||
OkHttpClient client = new OkHttpClient.Builder()
|
||||
.addInterceptor(getHttpLogger())
|
||||
.addInterceptor(new ErrorResponseInterceptor())
|
||||
.build();
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/greeting"))
|
||||
.header("User-Agent", "A Baeldung Reader")
|
||||
.build();
|
||||
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
assertEquals("Response code should be: ", 500, response.code());
|
||||
assertEquals("Body should be: ", "{\"status\":500,\"detail\":\"The response from the server was not OK\"}",
|
||||
response.body().string());
|
||||
}
|
||||
}
|
||||
|
||||
private HttpLoggingInterceptor getHttpLogger() {
|
||||
HttpLoggingInterceptor logger = new HttpLoggingInterceptor();
|
||||
logger.setLevel(Level.HEADERS);
|
||||
return logger;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,7 +6,6 @@ This module contains articles about annotations used in Spring Data JPA
|
||||
|
||||
- [DDD Aggregates and @DomainEvents](https://www.baeldung.com/spring-data-ddd)
|
||||
- [JPA @Embedded And @Embeddable](https://www.baeldung.com/jpa-embedded-embeddable)
|
||||
- [Spring Data JPA @Modifying Annotation](https://www.baeldung.com/spring-data-jpa-modifying-annotation)
|
||||
- [Spring JPA @Embedded and @EmbeddedId](https://www.baeldung.com/spring-jpa-embedded-method-parameters)
|
||||
- [Programmatic Transaction Management in Spring](https://www.baeldung.com/spring-programmatic-transaction-management)
|
||||
- [JPA Entity Lifecycle Events](https://www.baeldung.com/jpa-entity-lifecycle-events)
|
||||
|
||||
@@ -12,6 +12,7 @@ This module contains articles about Spring Data JPA used in enterprise applicati
|
||||
- [Working with Lazy Element Collections in JPA](https://www.baeldung.com/java-jpa-lazy-collections)
|
||||
- [Custom Naming Convention with Spring Data JPA](https://www.baeldung.com/spring-data-jpa-custom-naming)
|
||||
- [Partial Data Update with Spring Data](https://www.baeldung.com/spring-data-partial-update)
|
||||
- [Spring Data JPA @Modifying Annotation](https://www.baeldung.com/spring-data-jpa-modifying-annotation)
|
||||
|
||||
### Eclipse Config
|
||||
After importing the project into Eclipse, you may see the following error:
|
||||
|
||||
+3
@@ -93,6 +93,9 @@ public interface UserRepository extends JpaRepository<User, Integer> , UserRepos
|
||||
@Query("delete User u where u.active = false")
|
||||
int deleteDeactivatedUsers();
|
||||
|
||||
@Query("delete User u where u.active = false")
|
||||
int deleteDeactivatedUsersWithNoModifyingAnnotation();
|
||||
|
||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||
@Query(value = "alter table USERS add column deleted int(1) not null default 0", nativeQuery = true)
|
||||
void addDeletedColumn();
|
||||
|
||||
+18
@@ -3,6 +3,7 @@ package com.baeldung.boot.daos;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
@@ -21,6 +22,7 @@ import java.util.function.Predicate;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class UserRepositoryCommon {
|
||||
@@ -520,6 +522,22 @@ public class UserRepositoryCommon {
|
||||
assertEquals(1, deletedUsersCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenTwoUsers_whenDeleteDeactivatedUsersWithNoModifyingAnnotation_ThenException() {
|
||||
User usr01 = new User("usr01", LocalDate.of(2018, 1, 1), "[email protected]", 1);
|
||||
usr01.setLastLoginDate(LocalDate.now());
|
||||
User usr02 = new User("usr02", LocalDate.of(2018, 6, 1), "[email protected]", 0);
|
||||
usr02.setLastLoginDate(LocalDate.of(2018, 7, 20));
|
||||
usr02.setActive(false);
|
||||
|
||||
userRepository.save(usr01);
|
||||
userRepository.save(usr02);
|
||||
|
||||
assertThatThrownBy(() -> userRepository.deleteDeactivatedUsersWithNoModifyingAnnotation())
|
||||
.isInstanceOf(InvalidDataAccessApiUsageException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenTwoUsers_whenAddDeletedColumn_ThenUsersHaveDeletedColumn() {
|
||||
|
||||
@@ -531,7 +531,7 @@
|
||||
<module>protobuffer</module>
|
||||
|
||||
<module>quarkus</module>
|
||||
<module>quarkus-extension</module>
|
||||
<!-- <module>quarkus-extension</module> --> <!-- Module broken, fixing in http://team.baeldung.com/browse/JAVA-4770 -->
|
||||
|
||||
<module>rabbitmq</module>
|
||||
<!-- <module>raml</module> --> <!-- Not a maven project -->
|
||||
@@ -992,7 +992,7 @@
|
||||
<module>protobuffer</module>
|
||||
|
||||
<module>quarkus</module>
|
||||
<module>quarkus-extension</module>
|
||||
<!-- <module>quarkus-extension</module> --> <!-- Module broken, fixing in http://team.baeldung.com/browse/JAVA-4770 -->
|
||||
|
||||
<module>rabbitmq</module>
|
||||
<!-- <module>raml</module> --> <!-- Not a maven project -->
|
||||
|
||||
@@ -5,4 +5,4 @@ This module contains articles about Spring Boot: JAX-RS vs Spring
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [REST API: JAX-RS vs Spring](https://www.baeldung.com/TBD)
|
||||
- [REST API: JAX-RS vs Spring](https://www.baeldung.com/rest-api-jax-rs-vs-spring)
|
||||
|
||||
-2
@@ -1,3 +1 @@
|
||||
server.port=8888
|
||||
spring.security.user.name=root
|
||||
spring.security.user.password=docker!
|
||||
|
||||
-2
@@ -1,3 +1 @@
|
||||
server.port=9999
|
||||
spring.security.user.name=root
|
||||
spring.security.user.password=docker!
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
<module>spring-security-legacy-oidc</module>
|
||||
<module>spring-security-oidc</module>
|
||||
<module>spring-security-okta</module>
|
||||
<module>spring-security-saml</module>
|
||||
<module>spring-security-web-react</module>
|
||||
<module>spring-security-web-rest</module>
|
||||
<module>spring-security-web-rest-basic-auth</module>
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user