Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
commit
f1f9e87468
|
@ -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)
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
### Relevant Articles:
|
||||
|
||||
- [Introduction to Apache Beam](https://www.baeldung.com/apache-beam)
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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,7 +1,3 @@
|
|||
## Custom PMD Rules
|
||||
|
||||
This module contains articles about PMD
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Introduction To PMD](https://www.baeldung.com/pmd)
|
|
@ -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)
|
||||
|
|
|
@ -1 +1,3 @@
|
|||
## Relevant Articles
|
||||
### Relevant Articles:
|
||||
|
||||
- [DDD Bounded Contexts and Java Modules](https://www.baeldung.com/java-modules-ddd-bounded-contexts)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
### Relevant Articles:
|
||||
|
||||
- [What’s New in Gradle 6.0](https://www.baeldung.com/gradle-6-features)
|
|
@ -0,0 +1,3 @@
|
|||
### Relevant Articles:
|
||||
|
||||
- [Converting Gradle Build File to Maven POM](https://www.baeldung.com/gradle-build-to-maven-pom)
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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() {
|
|
@ -33,7 +33,7 @@ import static com.baeldung.jhipster5.repository.CustomAuditEventRepository.EVENT
|
|||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = BookstoreApp.class)
|
||||
@Transactional
|
||||
public class CustomAuditEventRepositoryIntTest {
|
||||
public class CustomAuditEventRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private PersistenceAuditEventRepository persistenceAuditEventRepository;
|
|
@ -28,7 +28,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = BookstoreApp.class)
|
||||
@Transactional
|
||||
public class DomainUserDetailsServiceIntTest {
|
||||
public class DomainUserDetailsServiceIntegrationTest {
|
||||
|
||||
private static final String USER_ONE_LOGIN = "test-user-one";
|
||||
private static final String USER_ONE_EMAIL = "test-user-one@localhost";
|
|
@ -20,7 +20,7 @@ import java.util.Collections;
|
|||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class JWTFilterTest {
|
||||
public class JWTFilterUnitTest {
|
||||
|
||||
private TokenProvider tokenProvider;
|
||||
|
|
@ -22,7 +22,7 @@ import io.jsonwebtoken.security.Keys;
|
|||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TokenProviderTest {
|
||||
public class TokenProviderUnitTest {
|
||||
|
||||
private final long ONE_MINUTE = 60000;
|
||||
private Key key;
|
|
@ -31,7 +31,7 @@ import static org.mockito.Mockito.*;
|
|||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = BookstoreApp.class)
|
||||
public class MailServiceIntTest {
|
||||
public class MailServiceIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private JHipsterProperties jHipsterProperties;
|
|
@ -38,7 +38,7 @@ import static org.mockito.Mockito.when;
|
|||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = BookstoreApp.class)
|
||||
@Transactional
|
||||
public class UserServiceIntTest {
|
||||
public class UserServiceIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
|
@ -26,7 +26,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = BookstoreApp.class)
|
||||
public class UserMapperTest {
|
||||
public class UserMapperUnitTest {
|
||||
|
||||
private static final String DEFAULT_LOGIN = "johndoe";
|
||||
|
|
@ -49,7 +49,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = BookstoreApp.class)
|
||||
public class AccountResourceIntTest {
|
||||
public class AccountResourceIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
|
@ -35,7 +35,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = BookstoreApp.class)
|
||||
@Transactional
|
||||
public class AuditResourceIntTest {
|
||||
public class AuditResourceIntegrationTest {
|
||||
|
||||
private static final String SAMPLE_PRINCIPAL = "SAMPLE_PRINCIPAL";
|
||||
private static final String SAMPLE_TYPE = "SAMPLE_TYPE";
|
|
@ -43,7 +43,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = BookstoreApp.class)
|
||||
public class BookResourceIntTest {
|
||||
public class BookResourceIntegrationTest {
|
||||
|
||||
private static final String DEFAULT_TITLE = "AAAAAAAAAA";
|
||||
private static final String UPDATED_TITLE = "BBBBBBBBBB";
|
|
@ -27,7 +27,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = BookstoreApp.class)
|
||||
public class LogsResourceIntTest {
|
||||
public class LogsResourceIntegrationTest {
|
||||
|
||||
private MockMvc restLogsMockMvc;
|
||||
|
|
@ -33,7 +33,7 @@ import static org.hamcrest.Matchers.not;
|
|||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = BookstoreApp.class)
|
||||
public class UserJWTControllerIntTest {
|
||||
public class UserJWTControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private TokenProvider tokenProvider;
|
|
@ -42,7 +42,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = BookstoreApp.class)
|
||||
public class UserResourceIntTest {
|
||||
public class UserResourceIntegrationTest {
|
||||
|
||||
private static final String DEFAULT_LOGIN = "johndoe";
|
||||
private static final String UPDATED_LOGIN = "jhipster";
|
|
@ -25,7 +25,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = BookstoreApp.class)
|
||||
public class ExceptionTranslatorIntTest {
|
||||
public class ExceptionTranslatorIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ExceptionTranslatorTestController controller;
|
|
@ -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)
|
||||
|
|
|
@ -33,9 +33,26 @@
|
|||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.squareup.moshi</groupId>
|
||||
<artifactId>moshi</artifactId>
|
||||
<version>${moshi.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.moshi</groupId>
|
||||
<artifactId>moshi-adapters</artifactId>
|
||||
<version>${moshi.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.9</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<jsoniter.version>0.9.23</jsoniter.version>
|
||||
<assertj-core.version>3.11.1</assertj-core.version>
|
||||
<moshi.version>1.9.2</moshi.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -12,7 +12,11 @@ import org.junit.jupiter.api.Test
|
|||
import java.io.File
|
||||
import java.util.concurrent.CountDownLatch
|
||||
|
||||
internal class FuelHttpUnitTest {
|
||||
/**
|
||||
* These live tests make connections to the external systems: http://httpbin.org, https://jsonplaceholder.typicode.com
|
||||
* Make sure these hosts are up and your internet connection is on before running the tests.
|
||||
*/
|
||||
internal class FuelHttpLiveTest {
|
||||
|
||||
@Test
|
||||
fun whenMakingAsyncHttpGetRequest_thenResponseNotNullAndErrorNullAndStatusCode200() {
|
|
@ -12,3 +12,5 @@ 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)
|
||||
|
|
|
@ -73,16 +73,6 @@
|
|||
<version>${cache2k.version}</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.moshi</groupId>
|
||||
<artifactId>moshi</artifactId>
|
||||
<version>${moshi.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.moshi</groupId>
|
||||
<artifactId>moshi-adapters</artifactId>
|
||||
<version>${moshi.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jcabi</groupId>
|
||||
<artifactId>jcabi-aspects</artifactId>
|
||||
|
@ -201,7 +191,6 @@
|
|||
<cactoos.version>0.43</cactoos.version>
|
||||
<airline.version>2.7.2</airline.version>
|
||||
<cache2k.version>1.2.3.Final</cache2k.version>
|
||||
<moshi.version>1.9.2</moshi.version>
|
||||
<jcabi-aspects.version>0.22.6</jcabi-aspects.version>
|
||||
<aspectjrt.version>1.9.2</aspectjrt.version>
|
||||
<jcabi-maven-plugin.version>0.14.1</jcabi-maven-plugin.version>
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.arthas;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
public class FibonacciGenerator {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
System.out.println("Press a key to continue");
|
||||
System.in.read();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
long result = fibonacci(i);
|
||||
System.out.println(format("fib(%d): %d", i, result));
|
||||
}
|
||||
}
|
||||
|
||||
public static long fibonacci(int n) {
|
||||
if (n == 0 || n == 1) {
|
||||
return 1L;
|
||||
} else {
|
||||
return fibonacci(n - 1) + fibonacci(n - 2);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
|
|
|
@ -45,4 +45,5 @@ Remember, for advanced libraries like [Jackson](/jackson) and [JUnit](/testing-m
|
|||
- [Implementing a FTP-Client in Java](https://www.baeldung.com/java-ftp-client)
|
||||
- [Introduction to Functional Java](https://www.baeldung.com/java-functional-library)
|
||||
- [A Guide to the Reflections Library](https://www.baeldung.com/reflections-library)
|
||||
- [Using NullAway to Avoid NullPointerExceptions](https://www.baeldung.com/java-nullaway)
|
||||
- More articles [[next -->]](/libraries-2)
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
### Relevant Articles:
|
||||
|
||||
- [Introduction to Open Liberty](https://www.baeldung.com/java-open-liberty)
|
|
@ -31,7 +31,7 @@
|
|||
<repository>
|
||||
<id>spring-milestone</id>
|
||||
<name>Spring Milestone Repository</name>
|
||||
<url>http://repo.spring.io/milestone</url>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
|
|
|
@ -19,8 +19,8 @@ public class User {
|
|||
}
|
||||
|
||||
public static User createWithLoggedInstantiationTime(String name, String email, String country) {
|
||||
setLoggerProperties();
|
||||
LOGGER.log(Level.INFO, "Creating User instance at : {0}", LocalTime.now());
|
||||
|
||||
return new User(name, email, country);
|
||||
}
|
||||
|
||||
|
@ -53,11 +53,4 @@ public class User {
|
|||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
private static void setLoggerProperties() {
|
||||
ConsoleHandler handler = new ConsoleHandler();
|
||||
handler.setLevel(Level.INFO);
|
||||
handler.setFormatter(new SimpleFormatter());
|
||||
LOGGER.addHandler(handler);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,3 +9,4 @@ This module contains articles about MongoDB in Java.
|
|||
- [MongoDB BSON Guide](https://www.baeldung.com/mongodb-bson)
|
||||
- [Geospatial Support in MongoDB](https://www.baeldung.com/mongodb-geospatial-support)
|
||||
- [Introduction to Morphia – Java ODM for MongoDB](https://www.baeldung.com/mongodb-morphia)
|
||||
- [MongoDB Aggregations Using Java](https://www.baeldung.com/java-mongodb-aggregations)
|
||||
|
|
|
@ -29,12 +29,30 @@
|
|||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.vladmihalcea</groupId>
|
||||
<artifactId>db-util</artifactId>
|
||||
<version>${db-util.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<!-- The main class to start by executing java -jar -->
|
||||
<start-class>com.baeldung.h2db.demo.server.SpringBootApp</start-class>
|
||||
<spring-boot.version>2.0.4.RELEASE</spring-boot.version>
|
||||
<hibernate.version>5.3.11.Final</hibernate.version>
|
||||
<db-util.version>1.0.4</db-util.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.h2db.lazy_load_no_trans;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableTransactionManagement
|
||||
public class LazyLoadNoTransSpringBootApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(LazyLoadNoTransSpringBootApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
package com.baeldung.h2db.lazy_load_no_trans.config;
|
||||
|
||||
import net.ttddyy.dsproxy.listener.DataSourceQueryCountListener;
|
||||
import net.ttddyy.dsproxy.listener.logging.CommonsQueryLoggingListener;
|
||||
import net.ttddyy.dsproxy.listener.logging.DefaultQueryLogEntryCreator;
|
||||
import net.ttddyy.dsproxy.listener.logging.SLF4JLogLevel;
|
||||
import net.ttddyy.dsproxy.listener.logging.SLF4JQueryLoggingListener;
|
||||
import net.ttddyy.dsproxy.support.ProxyDataSource;
|
||||
import net.ttddyy.dsproxy.support.ProxyDataSourceBuilder;
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@Component
|
||||
public class DatasourceProxyBeanPostProcessor implements BeanPostProcessor {
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) {
|
||||
if (bean instanceof DataSource && !(bean instanceof ProxyDataSource)) {
|
||||
// Instead of directly returning a less specific datasource bean
|
||||
// (e.g.: HikariDataSource -> DataSource), return a proxy object.
|
||||
// See following links for why:
|
||||
// https://stackoverflow.com/questions/44237787/how-to-use-user-defined-database-proxy-in-datajpatest
|
||||
// https://gitter.im/spring-projects/spring-boot?at=5983602d2723db8d5e70a904
|
||||
// http://blog.arnoldgalovics.com/2017/06/26/configuring-a-datasource-proxy-in-spring-boot/
|
||||
final ProxyFactory factory = new ProxyFactory(bean);
|
||||
factory.setProxyTargetClass(true);
|
||||
factory.addAdvice(new ProxyDataSourceInterceptor((DataSource) bean));
|
||||
return factory.getProxy();
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) {
|
||||
return bean;
|
||||
}
|
||||
|
||||
private static class ProxyDataSourceInterceptor implements MethodInterceptor {
|
||||
private final DataSource dataSource;
|
||||
|
||||
public ProxyDataSourceInterceptor(final DataSource dataSource) {
|
||||
this.dataSource = ProxyDataSourceBuilder.create(dataSource)
|
||||
.name("MyDS")
|
||||
.multiline()
|
||||
.logQueryBySlf4j(SLF4JLogLevel.INFO)
|
||||
.listener(new DataSourceQueryCountListener())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(final MethodInvocation invocation) throws Throwable {
|
||||
final Method proxyMethod = ReflectionUtils.findMethod(this.dataSource.getClass(),
|
||||
invocation.getMethod().getName());
|
||||
if (proxyMethod != null) {
|
||||
return proxyMethod.invoke(this.dataSource, invocation.getArguments());
|
||||
}
|
||||
return invocation.proceed();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.h2db.lazy_load_no_trans.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.hibernate.annotations.Immutable;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Immutable
|
||||
public class Document {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private Long userId;
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.h2db.lazy_load_no_trans.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.hibernate.annotations.Fetch;
|
||||
import org.hibernate.annotations.FetchMode;
|
||||
import org.hibernate.annotations.Immutable;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Immutable
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String comment;
|
||||
|
||||
@OneToMany(mappedBy = "userId")
|
||||
@Fetch(FetchMode.SUBSELECT)
|
||||
private List<Document> docs = new ArrayList<>();
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.h2db.lazy_load_no_trans.repository;
|
||||
|
||||
import com.baeldung.h2db.lazy_load_no_trans.entity.User;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.h2db.lazy_load_no_trans.service;
|
||||
|
||||
import com.baeldung.h2db.lazy_load_no_trans.entity.User;
|
||||
import com.baeldung.h2db.lazy_load_no_trans.repository.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@Service
|
||||
public class ServiceLayer {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public long countAllDocsTransactional() {
|
||||
return countAllDocs();
|
||||
}
|
||||
|
||||
public long countAllDocsNonTransactional() {
|
||||
return countAllDocs();
|
||||
}
|
||||
|
||||
private long countAllDocs() {
|
||||
return userRepository.findAll()
|
||||
.stream()
|
||||
.map(User::getDocs)
|
||||
.mapToLong(Collection::size)
|
||||
.sum();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
spring.datasource.url=jdbc:h2:mem:mydb
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
spring.h2.console.enabled=true
|
||||
spring.h2.console.path=/h2-console
|
||||
|
||||
logging.level.org.hibernate.SQL=INFO
|
||||
logging.level.org.hibernate.type=TRACE
|
||||
spring.jpa.properties.hibernate.validator.apply_to_ddl=false
|
||||
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=false
|
||||
spring.jpa.open-in-view=false
|
|
@ -0,0 +1,13 @@
|
|||
spring.datasource.url=jdbc:h2:mem:mydb
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
spring.h2.console.enabled=true
|
||||
spring.h2.console.path=/h2-console
|
||||
|
||||
logging.level.org.hibernate.SQL=INFO
|
||||
logging.level.org.hibernate.type=TRACE
|
||||
spring.jpa.properties.hibernate.validator.apply_to_ddl=false
|
||||
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
|
||||
spring.jpa.open-in-view=false
|
|
@ -10,4 +10,17 @@ CREATE TABLE billionaires (
|
|||
INSERT INTO billionaires (first_name, last_name, career) VALUES
|
||||
('Aliko', 'Dangote', 'Billionaire Industrialist'),
|
||||
('Bill', 'Gates', 'Billionaire Tech Entrepreneur'),
|
||||
('Folrunsho', 'Alakija', 'Billionaire Oil Magnate');
|
||||
('Folrunsho', 'Alakija', 'Billionaire Oil Magnate');
|
||||
|
||||
insert into USER values (101, 'user1', 'comment1');
|
||||
insert into USER values (102, 'user2', 'comment2');
|
||||
insert into USER values (103, 'user3', 'comment3');
|
||||
insert into USER values (104, 'user4', 'comment4');
|
||||
insert into USER values (105, 'user5', 'comment5');
|
||||
|
||||
insert into DOCUMENT values (1, 'doc1', 101);
|
||||
insert into DOCUMENT values (2, 'doc2', 101);
|
||||
insert into DOCUMENT values (3, 'doc3', 101);
|
||||
insert into DOCUMENT values (4, 'doc4', 101);
|
||||
insert into DOCUMENT values (5, 'doc5', 102);
|
||||
insert into DOCUMENT values (6, 'doc6', 102);
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.lazy_load_no_trans;
|
||||
|
||||
import com.baeldung.h2db.lazy_load_no_trans.LazyLoadNoTransSpringBootApplication;
|
||||
import com.baeldung.h2db.lazy_load_no_trans.service.ServiceLayer;
|
||||
import com.vladmihalcea.sql.SQLStatementCountValidator;
|
||||
import org.hibernate.LazyInitializationException;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = LazyLoadNoTransSpringBootApplication.class)
|
||||
@ActiveProfiles("lazy-load-no-trans-off")
|
||||
public class LazyLoadNoTransPropertyOffIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ServiceLayer serviceLayer;
|
||||
|
||||
private static final long EXPECTED_DOCS_COLLECTION_SIZE = 6;
|
||||
|
||||
@Test(expected = LazyInitializationException.class)
|
||||
public void whenCallNonTransactionalMethodWithPropertyOff_thenThrowException() {
|
||||
serviceLayer.countAllDocsNonTransactional();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallTransactionalMethodWithPropertyOff_thenTestPass() {
|
||||
SQLStatementCountValidator.reset();
|
||||
|
||||
long docsCount = serviceLayer.countAllDocsTransactional();
|
||||
|
||||
assertEquals(EXPECTED_DOCS_COLLECTION_SIZE, docsCount);
|
||||
|
||||
SQLStatementCountValidator.assertSelectCount(2);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package com.baeldung.lazy_load_no_trans;
|
||||
|
||||
import com.baeldung.h2db.lazy_load_no_trans.LazyLoadNoTransSpringBootApplication;
|
||||
import com.baeldung.h2db.lazy_load_no_trans.service.ServiceLayer;
|
||||
import com.vladmihalcea.sql.SQLStatementCountValidator;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = LazyLoadNoTransSpringBootApplication.class)
|
||||
@ActiveProfiles("lazy-load-no-trans-on")
|
||||
public class LazyLoadNoTransPropertyOnIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ServiceLayer serviceLayer;
|
||||
|
||||
private static final long EXPECTED_DOCS_COLLECTION_SIZE = 6;
|
||||
private static final long EXPECTED_USERS_COUNT = 5;
|
||||
|
||||
@Test
|
||||
public void whenCallNonTransactionalMethodWithPropertyOn_thenGetNplusOne() {
|
||||
SQLStatementCountValidator.reset();
|
||||
|
||||
long docsCount = serviceLayer.countAllDocsNonTransactional();
|
||||
|
||||
assertEquals(EXPECTED_DOCS_COLLECTION_SIZE, docsCount);
|
||||
|
||||
SQLStatementCountValidator.assertSelectCount(EXPECTED_USERS_COUNT + 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallTransactionalMethodWithPropertyOn_thenTestPass() {
|
||||
SQLStatementCountValidator.reset();
|
||||
|
||||
long docsCount = serviceLayer.countAllDocsTransactional();
|
||||
|
||||
assertEquals(EXPECTED_DOCS_COLLECTION_SIZE, docsCount);
|
||||
|
||||
SQLStatementCountValidator.assertSelectCount(2);
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@
|
|||
- [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)
|
||||
- [Working with Lazy Element Collections in JPA](https://www.baeldung.com/java-jpa-lazy-collections)
|
||||
- [Calling Stored Procedures from Spring Data JPA Repositories](https://www.baeldung.com/spring-data-jpa-stored-procedures)
|
||||
|
||||
### Eclipse Config
|
||||
After importing the project into Eclipse, you may see the following error:
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
- [Introduction to Spring Data Redis](https://www.baeldung.com/spring-data-redis-tutorial)
|
||||
- [PubSub Messaging with Spring Data Redis](https://www.baeldung.com/spring-data-redis-pub-sub)
|
||||
- [An Introduction to Spring Data Redis Reactive](https://www.baeldung.com/spring-data-redis-reactive)
|
||||
- [Delete Everything in Redis](https://www.baeldung.com/redis-delete-data)
|
||||
|
||||
### Build the Project with Tests Running
|
||||
```
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
### Relevant Articles:
|
||||
|
||||
- [Asynchronous HTTP Programming with Play Framework](https://www.baeldung.com/java-play-asynchronous-http-programming)
|
|
@ -13,5 +13,4 @@ This module contains articles about RxJava.
|
|||
- [RxJava Maybe](https://www.baeldung.com/rxjava-maybe)
|
||||
- [Combining RxJava Completables](https://www.baeldung.com/rxjava-completable)
|
||||
- [RxJava Hooks](https://www.baeldung.com/rxjava-hooks)
|
||||
- [Introduction to rxjava-jdbc](https://www.baeldung.com/rxjava-jdbc)
|
||||
- More articles: [[next -->]](/rxjava-2)
|
||||
|
|
|
@ -16,3 +16,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||
- [Spring Assert Statements](https://www.baeldung.com/spring-assert)
|
||||
- [Configuring a Hikari Connection Pool with Spring Boot](https://www.baeldung.com/spring-boot-hikari)
|
||||
- [Difference between \<context:annotation-config> vs \<context:component-scan>](https://www.baeldung.com/spring-contextannotation-contextcomponentscan)
|
||||
- [Finding the Spring Version](https://www.baeldung.com/spring-find-version)
|
||||
|
|
|
@ -7,3 +7,4 @@ This module contains articles about Spring with the AMQP messaging system
|
|||
- [Messaging With Spring AMQP](https://www.baeldung.com/spring-amqp)
|
||||
- [RabbitMQ Message Dispatching with Spring AMQP](https://www.baeldung.com/rabbitmq-spring-amqp)
|
||||
- [Error Handling with Spring AMQP](https://www.baeldung.com/spring-amqp-error-handling)
|
||||
- [Exponential Backoff With Spring AMQP](https://www.baeldung.com/spring-amqp-exponential-backoff)
|
||||
|
|
|
@ -9,3 +9,4 @@ This module contains articles about Spring Batch
|
|||
- [How to Trigger and Stop a Scheduled Spring Batch Job](https://www.baeldung.com/spring-batch-start-stop-job)
|
||||
- [Configuring Skip Logic in Spring Batch](https://www.baeldung.com/spring-batch-skip-logic)
|
||||
- [Testing a Spring Batch Job](https://www.baeldung.com/spring-batch-testing-job)
|
||||
- [Configuring Retry Logic in Spring Batch](https://www.baeldung.com/spring-batch-retry-logic)
|
||||
|
|
|
@ -10,3 +10,4 @@ This module contains articles about bootstrapping Spring Boot applications.
|
|||
- [Deploy a Spring Boot Application to OpenShift](https://www.baeldung.com/spring-boot-deploy-openshift)
|
||||
- [Deploy a Spring Boot Application to AWS Beanstalk](https://www.baeldung.com/spring-boot-deploy-aws-beanstalk)
|
||||
- [Guide to @SpringBootConfiguration in Spring Boot](https://www.baeldung.com/springbootconfiguration-annotation)
|
||||
- [Implement Health Checks in OpenShift](https://www.baeldung.com/openshift-health-checks)
|
||||
|
|
|
@ -6,4 +6,5 @@ This module contains articles about Spring Web MVC in Spring Boot projects.
|
|||
|
||||
- [Functional Controllers in Spring MVC](https://www.baeldung.com/spring-mvc-functional-controllers)
|
||||
- [Specify an Array of Strings as Body Parameters in Swagger](https://www.baeldung.com/swagger-body-array-of-strings)
|
||||
- More articles: [[prev -->]](/spring-boot-mvc)
|
||||
- [Swagger @ApiParam vs @ApiModelProperty](https://www.baeldung.com/swagger-apiparam-vs-apimodelproperty)
|
||||
- More articles: [[prev -->]](/spring-boot-modules/spring-boot-mvc)
|
||||
|
|
|
@ -10,4 +10,4 @@ This module contains articles about Spring Web MVC in Spring Boot projects.
|
|||
- [A Controller, Service and DAO Example with Spring Boot and JSF](https://www.baeldung.com/jsf-spring-boot-controller-service-dao)
|
||||
- [Setting Up Swagger 2 with a Spring REST API](https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api)
|
||||
- [Using Spring ResponseEntity to Manipulate the HTTP Response](https://www.baeldung.com/spring-response-entity)
|
||||
- More articles: [[next -->]](/spring-boot-mvc-2)
|
||||
- More articles: [[next -->]](/spring-boot-modules/spring-boot-mvc-2)
|
||||
|
|
|
@ -12,3 +12,5 @@ This module contains articles about Properties in Spring Boot.
|
|||
- [Spring YAML Configuration](https://www.baeldung.com/spring-yaml)
|
||||
- [Using Spring @Value with Defaults](https://www.baeldung.com/spring-value-defaults)
|
||||
- [How to Inject a Property Value Into a Class Not Managed by Spring?](https://www.baeldung.com/inject-properties-value-non-spring-class)
|
||||
- [Add Build Properties to a Spring Boot Application](https://www.baeldung.com/spring-boot-build-properties)
|
||||
- [IntelliJ – Cannot Resolve Spring Boot Configuration Properties Error](https://www.baeldung.com/intellij-resolve-spring-boot-configuration-properties)
|
||||
|
|
|
@ -12,4 +12,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||
- [Exclude Auto-Configuration Classes in Spring Boot Tests](https://www.baeldung.com/spring-boot-exclude-auto-configuration-test)
|
||||
- [Setting the Log Level in Spring Boot when Testing](https://www.baeldung.com/spring-boot-testing-log-level)
|
||||
- [Embedded Redis Server with Spring Boot Test](https://www.baeldung.com/spring-embedded-redis)
|
||||
- [Testing Spring Boot @ConfigurationProperties](https://www.baeldung.com/spring-boot-testing-configurationproperties)
|
||||
- [Prevent ApplicationRunner or CommandLineRunner Beans From Executing During Junit Testing]()
|
||||
|
|
|
@ -5,3 +5,4 @@ This module contains articles about Spring Cloud Gateway
|
|||
### Relevant Articles:
|
||||
- [Exploring the new Spring Cloud Gateway](http://www.baeldung.com/spring-cloud-gateway)
|
||||
- [Writing Custom Spring Cloud Gateway Filters](https://www.baeldung.com/spring-cloud-custom-gateway-filters)
|
||||
- [Spring Cloud Gateway Routing Predicate Factories](https://www.baeldung.com/spring-cloud-gateway-routing-predicate-factories)
|
||||
|
|
|
@ -5,3 +5,4 @@ This module contains articles about Spring with Netflix Zuul
|
|||
### Relevant Articles:
|
||||
- [Rate Limiting in Spring Cloud Netflix Zuul](https://www.baeldung.com/spring-cloud-zuul-rate-limit)
|
||||
- [Spring REST with a Zuul Proxy](https://www.baeldung.com/spring-rest-with-zuul-proxy)
|
||||
- [Modifying the Response Body in a Zuul Filter](https://www.baeldung.com/zuul-filter-modifying-response-body)
|
||||
|
|
|
@ -8,4 +8,6 @@ This module contains articles about core Spring functionality
|
|||
- [Guide to the Spring BeanFactory](https://www.baeldung.com/spring-beanfactory)
|
||||
- [How to use the Spring FactoryBean?](https://www.baeldung.com/spring-factorybean)
|
||||
- [Spring – Injecting Collections](https://www.baeldung.com/spring-injecting-collections)
|
||||
- [Design Patterns in the Spring Framework](https://www.baeldung.com/spring-framework-design-patterns)
|
||||
- [Injecting a Value in a Static Field in Spring](https://www.baeldung.com/spring-inject-static-field)
|
||||
- More articles: [[<-- prev]](/spring-core-2)
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
package com.baeldung;
|
||||
package com.baeldung.staticvalue.injection;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@SpringBootApplication
|
||||
@PropertySource("/application.properties")
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
|
@ -1,7 +1,6 @@
|
|||
package com.baeldung.controller;
|
||||
package com.baeldung.staticvalue.injection;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
# Inject a value to a static field
|
||||
|
||||
## How to run
|
||||
```sh
|
||||
mvn clean install
|
||||
mvn spring-boot:run
|
||||
```
|
||||
|
||||
## Request
|
||||
|
||||
**GET**
|
||||
http://localhost:8080/properties
|
||||
|
||||
|
||||
## Response
|
||||
```json
|
||||
[
|
||||
"Inject a value to a static field",
|
||||
"Inject a value to a static field",
|
||||
null
|
||||
]
|
||||
```
|
|
@ -1,52 +0,0 @@
|
|||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.4.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>static.value.injection</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>static.value.injection</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -1,13 +0,0 @@
|
|||
package com.baeldung;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class ApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
### Relevant Articles:
|
||||
|
||||
- [Spring Bean vs. EJB – A Feature Comparison](https://www.baeldung.com/spring-bean-vs-ejb)
|
|
@ -0,0 +1,3 @@
|
|||
### Relevant Articles:
|
||||
|
||||
- [Cache Headers in Spring MVC](https://www.baeldung.com/spring-mvc-cache-headers)
|
|
@ -17,6 +17,7 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||
- [A Java Web Application Without a web.xml](https://www.baeldung.com/java-web-app-without-web-xml)
|
||||
- [Validating RequestParams and PathVariables in Spring](https://www.baeldung.com/spring-validate-requestparam-pathvariable)
|
||||
- [Debugging the Spring MVC 404 “No mapping found for HTTP request” Error](https://www.baeldung.com/spring-mvc-404-error)
|
||||
- [Getting Started with CRaSH](https://www.baeldung.com/jvm-crash-shell)
|
||||
|
||||
## Spring MVC with XML Configuration Example Project
|
||||
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue