[JAVA-2306] Moved articles from spring-persistence-simple-2
* https://www.baeldung.com/spring-jdbctemplate-testing went to spring-jdbc * https://www.baeldung.com/spring-jdbctemplate-in-list went to spring-jdbc * https://www.baeldung.com/spring-mock-jndi-datasource went to spring-persistence-simple * Deleted spring-persistence-simple-2 module as all articles have been moved
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
@@ -0,0 +1,25 @@
|
||||
=========
|
||||
|
||||
## Spring Persistence Example Project
|
||||
|
||||
|
||||
### Relevant Articles:
|
||||
- [A Guide to JPA with Spring](https://www.baeldung.com/the-persistence-layer-with-spring-and-jpa)
|
||||
- [Bootstrapping Hibernate 5 with Spring](http://www.baeldung.com/hibernate-5-spring)
|
||||
- [The DAO with Spring and Hibernate](https://www.baeldung.com/persistence-layer-with-spring-and-hibernate)
|
||||
- [Simplify the DAO with Spring and Java Generics](https://www.baeldung.com/simplifying-the-data-access-layer-with-spring-and-java-generics)
|
||||
- [Transactions with Spring and JPA](https://www.baeldung.com/transaction-configuration-with-jpa-and-spring)
|
||||
- [Introduction to Spring Data JPA](http://www.baeldung.com/the-persistence-layer-with-spring-data-jpa)
|
||||
- [Spring Data JPA @Query](http://www.baeldung.com/spring-data-jpa-query)
|
||||
- [Spring JDBC](https://www.baeldung.com/spring-jdbc-jdbctemplate)
|
||||
- [Transaction Propagation and Isolation in Spring @Transactional](https://www.baeldung.com/spring-transactional-propagation-isolation)
|
||||
|
||||
### Eclipse Config
|
||||
After importing the project into Eclipse, you may see the following error:
|
||||
"No persistence xml file found in project"
|
||||
|
||||
This can be ignored:
|
||||
- Project -> Properties -> Java Persistance -> JPA -> Error/Warnings -> Select Ignore on "No persistence xml file found in project"
|
||||
Or:
|
||||
- Eclipse -> Preferences - Validation - disable the "Build" execution of the JPA Validator
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-persistence-simple</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>spring-persistence-simple</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>persistence-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-orm</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- persistence -->
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- simple-jndi -->
|
||||
<dependency>
|
||||
<groupId>com.github.h-thurow</groupId>
|
||||
<artifactId>simple-jndi</artifactId>
|
||||
<version>${simple-jndi.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<!-- Spring -->
|
||||
<org.springframework.version>5.2.4.RELEASE</org.springframework.version>
|
||||
<!-- persistence -->
|
||||
<h2.version>1.4.200</h2.version>
|
||||
<!-- simple-jndi -->
|
||||
<simple-jndi.version>0.23.0</simple-jndi.version>
|
||||
<!-- test scoped -->
|
||||
<mockito.version>3.3.3</mockito.version>
|
||||
</properties>
|
||||
</project>
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
ds.type=javax.sql.DataSource
|
||||
ds.driver=org.h2.Driver
|
||||
ds.url=jdbc:jdbc:h2:mem:testdb
|
||||
ds.user=sa
|
||||
ds.password=password
|
||||
@@ -0,0 +1,6 @@
|
||||
java.naming.factory.initial=org.osjava.sj.SimpleContextFactory
|
||||
org.osjava.sj.jndi.shared=true
|
||||
org.osjava.sj.delimiter=.
|
||||
jndi.syntax.separator=/
|
||||
org.osjava.sj.space=java:/comp/env
|
||||
org.osjava.sj.root=src/main/resources/com/baeldung/spring/jndi/datasource/mock
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.spring.jndi.datasource.mock;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SimpleJNDIUnitTest {
|
||||
|
||||
private InitialContext initContext;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() throws Exception {
|
||||
this.initContext = new InitialContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMockJndiDataSource_thenReturnJndiDataSource() throws Exception {
|
||||
String dsString = "org.h2.Driver::::jdbc:jdbc:h2:mem:testdb::::sa";
|
||||
Context envContext = (Context) this.initContext.lookup("java:/comp/env");
|
||||
DataSource ds = (DataSource) envContext.lookup("datasource/ds");
|
||||
|
||||
assertEquals(dsString, ds.toString());
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void tearDown() throws Exception {
|
||||
if (this.initContext != null) {
|
||||
this.initContext.close();
|
||||
this.initContext = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.spring.jndi.datasource.mock;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import javax.naming.InitialContext;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.mock.jndi.SimpleNamingContextBuilder;
|
||||
|
||||
// marked as a manual test as the bindings in this test and
|
||||
// SimpleJNDIUnitTest conflict depending on the order they are run in
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class SimpleNamingContextBuilderManualTest {
|
||||
|
||||
private InitialContext initContext;
|
||||
|
||||
@BeforeEach
|
||||
public void init() throws Exception {
|
||||
SimpleNamingContextBuilder.emptyActivatedContextBuilder();
|
||||
this.initContext = new InitialContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMockJndiDataSource_thenReturnJndiDataSource() throws Exception {
|
||||
this.initContext.bind("java:comp/env/jdbc/datasource", new DriverManagerDataSource("jdbc:h2:mem:testdb"));
|
||||
DataSource ds = (DataSource) this.initContext.lookup("java:comp/env/jdbc/datasource");
|
||||
|
||||
assertNotNull(ds.getConnection());
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void tearDown() throws Exception {
|
||||
if (this.initContext != null) {
|
||||
this.initContext.close();
|
||||
this.initContext = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user