Added showcase for JDK 8's Optional support on repository methods.
Abbreviated JPA sample module folder names. Renamed the Java 8 auditing sample module to Java 8 module as it not only contains samples for auditing anymore.
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.auditing;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import example.springdata.jpa.auditing.AuditableUser;
|
||||
import example.springdata.jpa.auditing.AuditableUserRepository;
|
||||
import example.springdata.jpa.auditing.AuditingConfiguration;
|
||||
import example.springdata.jpa.auditing.AuditorAwareImpl;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@Transactional
|
||||
@ContextConfiguration(classes = AuditingConfiguration.class)
|
||||
public class AuditableUserSample {
|
||||
|
||||
@Autowired AuditableUserRepository repository;
|
||||
@Autowired AuditorAwareImpl auditorAware;
|
||||
@Autowired AuditingEntityListener listener;
|
||||
|
||||
@Test
|
||||
public void auditEntityCreation() throws Exception {
|
||||
|
||||
assertThat(ReflectionTestUtils.getField(listener, "handler"), is(notNullValue()));
|
||||
|
||||
AuditableUser user = new AuditableUser();
|
||||
user.setUsername("username");
|
||||
|
||||
auditorAware.setAuditor(user);
|
||||
|
||||
user = repository.save(user);
|
||||
user = repository.save(user);
|
||||
|
||||
assertEquals(user, user.getCreatedBy());
|
||||
assertEquals(user, user.getLastModifiedBy());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright 2013-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.basics;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
|
||||
|
||||
import example.springdata.jpa.simple.SimpleUserRepository;
|
||||
import example.springdata.jpa.simple.User;
|
||||
|
||||
/**
|
||||
* Test case showing how to use the basic {@link GenericDaoFactory}
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class BasicFactorySetup {
|
||||
|
||||
private static final EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa.sample.plain");
|
||||
|
||||
private SimpleUserRepository userRepository;
|
||||
private EntityManager em;
|
||||
|
||||
private User user;
|
||||
|
||||
/**
|
||||
* Creates a {@link SimpleUserRepository} instance.
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
em = factory.createEntityManager();
|
||||
|
||||
userRepository = new JpaRepositoryFactory(em).getRepository(SimpleUserRepository.class);
|
||||
|
||||
em.getTransaction().begin();
|
||||
|
||||
user = new User();
|
||||
user.setUsername("username");
|
||||
user.setFirstname("firstname");
|
||||
user.setLastname("lastname");
|
||||
|
||||
user = userRepository.save(user);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Rollback transaction.
|
||||
*/
|
||||
@After
|
||||
public void tearDown() {
|
||||
|
||||
em.getTransaction().rollback();
|
||||
}
|
||||
|
||||
/**
|
||||
* Showing invocation of finder method.
|
||||
*/
|
||||
@Test
|
||||
public void executingFinders() {
|
||||
|
||||
assertEquals(user, userRepository.findByTheUsersName("username"));
|
||||
assertEquals(user, userRepository.findByLastname("lastname").get(0));
|
||||
assertEquals(user, userRepository.findByFirstname("firstname").get(0));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright 2013-2014 the original author or authors.
|
||||
*
|
||||
* Licenseimport static org.junit.Assert.*;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.jpa.example.domain.User;
|
||||
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
ess or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.basics;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import example.springdata.jpa.simple.User;
|
||||
|
||||
/**
|
||||
* This unit tests shows plain usage of {@link SimpleJpaRepository}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class BasicSample {
|
||||
|
||||
private CrudRepository<User, Long> userRepository;
|
||||
private EntityManager em;
|
||||
|
||||
/**
|
||||
* Sets up a {@link SimpleJpaRepository} instance.
|
||||
*/
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa.sample.plain");
|
||||
em = factory.createEntityManager();
|
||||
|
||||
userRepository = new SimpleJpaRepository<User, Long>(User.class, em);
|
||||
|
||||
em.getTransaction().begin();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
em.getTransaction().rollback();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests saving users. Don't mimic transactionality shown here. It seriously lacks resource cleanup in case of an
|
||||
* exception. Simplification serves descriptivness.
|
||||
*/
|
||||
@Test
|
||||
public void savingUsers() {
|
||||
|
||||
User user = new User();
|
||||
user.setUsername("username");
|
||||
|
||||
user = userRepository.save(user);
|
||||
|
||||
assertEquals(user, userRepository.findOne(user.getId()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright 2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/**
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
package example.springdata.jpa.basics;
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2013-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.caching;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.Cache.ValueWrapper;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import example.springdata.jpa.caching.CachingConfiguration;
|
||||
import example.springdata.jpa.caching.CachingUserRepository;
|
||||
import example.springdata.jpa.caching.User;
|
||||
|
||||
/**
|
||||
* Integration test to show how to use {@link Cacheable} with a Spring Data repository.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@Transactional
|
||||
@ContextConfiguration(classes = CachingConfiguration.class)
|
||||
public abstract class CachingRepositoryTests {
|
||||
|
||||
@Autowired CachingUserRepository repository;
|
||||
@Autowired CacheManager cacheManager;
|
||||
|
||||
@Test
|
||||
public void cachesValuesReturnedForQueryMethod() {
|
||||
|
||||
User dave = new User();
|
||||
dave.setUsername("dmatthews");
|
||||
|
||||
dave = repository.save(dave);
|
||||
|
||||
User result = repository.findByUsername("dmatthews");
|
||||
assertThat(result, is(dave));
|
||||
|
||||
// Verify entity cached
|
||||
Cache cache = cacheManager.getCache("byUsername");
|
||||
ValueWrapper wrapper = cache.get("dmatthews");
|
||||
assertThat(wrapper.get(), is((Object) dave));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright 2013-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.custom;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* Intergration test showing the basic usage of {@link UserRepository}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@Transactional
|
||||
@ContextConfiguration(classes = CustomRepositoryConfig.class)
|
||||
// @ActiveProfiles("jdbc") // Uncomment @ActiveProfiles to enable the JDBC Implementation of the custom repository
|
||||
public class UserRepositoryCustomizationTests {
|
||||
|
||||
@Autowired UserRepository repository;
|
||||
|
||||
/**
|
||||
* Tests inserting a user and asserts it can be loaded again.
|
||||
*/
|
||||
@Test
|
||||
public void testInsert() {
|
||||
|
||||
User user = new User();
|
||||
user.setUsername("username");
|
||||
|
||||
user = repository.save(user);
|
||||
|
||||
assertEquals(user, repository.findOne(user.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void saveAndFindByLastNameAndFindByUserName() {
|
||||
|
||||
User user = new User();
|
||||
user.setUsername("foobar");
|
||||
user.setLastname("lastname");
|
||||
|
||||
user = repository.save(user);
|
||||
|
||||
List<User> users = repository.findByLastname("lastname");
|
||||
|
||||
assertNotNull(users);
|
||||
assertTrue(users.contains(user));
|
||||
|
||||
User reference = repository.findByTheUsersName("foobar");
|
||||
assertEquals(user, reference);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test invocation of custom method.
|
||||
*/
|
||||
@Test
|
||||
public void testCustomMethod() {
|
||||
|
||||
User user = new User();
|
||||
user.setUsername("username");
|
||||
|
||||
user = repository.save(user);
|
||||
|
||||
List<User> users = repository.myCustomBatchOperation();
|
||||
|
||||
assertNotNull(users);
|
||||
assertTrue(users.contains(user));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright 2013-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.simple;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* Intergration test showing the basic usage of {@link SimpleUserRepository}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@Transactional
|
||||
@ContextConfiguration(classes = SimpleConfiguration.class)
|
||||
public class SimpleUserRepositoryTests {
|
||||
|
||||
@Autowired SimpleUserRepository repository;
|
||||
User user;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
user = new User();
|
||||
user.setUsername("foobar");
|
||||
user.setFirstname("firstname");
|
||||
user.setLastname("lastname");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSavedUserById() {
|
||||
|
||||
user = repository.save(user);
|
||||
|
||||
assertThat(repository.findOne(user.getId()), is(user));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSavedUserByLastname() throws Exception {
|
||||
|
||||
user = repository.save(user);
|
||||
|
||||
List<User> users = repository.findByLastname("lastname");
|
||||
|
||||
assertThat(users, is(notNullValue()));
|
||||
assertThat(users.contains(user), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByFirstnameOrLastname() throws Exception {
|
||||
|
||||
user = repository.save(user);
|
||||
|
||||
List<User> users = repository.findByFirstnameOrLastname("lastname");
|
||||
|
||||
assertThat(users.contains(user), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useGuavaOptionalInsteadOfNulls() {
|
||||
|
||||
assertThat(repository.findByUsername("foobar").isPresent(), is(false));
|
||||
|
||||
repository.save(user);
|
||||
|
||||
assertThat(repository.findByUsername("foobar").isPresent(), is(true));
|
||||
}
|
||||
}
|
||||
16
jpa/example/src/test/resources/META-INF/persistence.xml
Normal file
16
jpa/example/src/test/resources/META-INF/persistence.xml
Normal file
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
|
||||
|
||||
<persistence-unit name="jpa.sample.plain">
|
||||
<class>example.springdata.jpa.simple.User</class>
|
||||
<properties>
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
|
||||
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:spring" />
|
||||
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
|
||||
<property name="hibernate.connection.username" value="sa" />
|
||||
<property name="hibernate.connection.password" value="" />
|
||||
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
|
||||
</persistence>
|
||||
16
jpa/example/src/test/resources/logback.xml
Normal file
16
jpa/example/src/test/resources/logback.xml
Normal file
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
|
||||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d %5p %40.40c:%4L - %m%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="error" />
|
||||
|
||||
<root level="error">
|
||||
<appender-ref ref="console" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user