group persistence modules (#2890)
* move security content from spring-security-rest-full * swagger update * move query language to new module * rename spring-security-rest-full to spring-rest-full * group persistence modules
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
dbeb5f8ba4
commit
26c50909be
+81
@@ -0,0 +1,81 @@
|
||||
package org.baeldung.dao;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.baeldung.entity.Person;
|
||||
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.test.context.transaction.TransactionConfiguration;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
@ContextConfiguration("/test-context.xml")
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@Transactional
|
||||
@TransactionConfiguration(defaultRollback = true)
|
||||
public class PersonDaoIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private PersonDao personDao;
|
||||
|
||||
//
|
||||
|
||||
@Test
|
||||
public void testCreation() {
|
||||
personDao.save(new Person("Erich", "Gamma"));
|
||||
final Person person = new Person("Kent", "Beck");
|
||||
personDao.save(person);
|
||||
personDao.save(new Person("Ralph", "Johnson"));
|
||||
|
||||
final Person personFromDb = personDao.findPersonsByFirstnameQueryDSL("Kent").get(0);
|
||||
Assert.assertEquals(person.getId(), personFromDb.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleFilter() {
|
||||
personDao.save(new Person("Erich", "Gamma"));
|
||||
final Person person = personDao.save(new Person("Ralph", "Beck"));
|
||||
final Person person2 = personDao.save(new Person("Ralph", "Johnson"));
|
||||
|
||||
final Person personFromDb = personDao.findPersonsByFirstnameAndSurnameQueryDSL("Ralph", "Johnson").get(0);
|
||||
Assert.assertNotSame(person.getId(), personFromDb.getId());
|
||||
Assert.assertEquals(person2.getId(), personFromDb.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOrdering() {
|
||||
final Person person = personDao.save(new Person("Kent", "Gamma"));
|
||||
personDao.save(new Person("Ralph", "Johnson"));
|
||||
final Person person2 = personDao.save(new Person("Kent", "Zivago"));
|
||||
|
||||
final Person personFromDb = personDao.findPersonsByFirstnameInDescendingOrderQueryDSL("Kent").get(0);
|
||||
Assert.assertNotSame(person.getId(), personFromDb.getId());
|
||||
Assert.assertEquals(person2.getId(), personFromDb.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxAge() {
|
||||
personDao.save(new Person("Kent", "Gamma", 20));
|
||||
personDao.save(new Person("Ralph", "Johnson", 35));
|
||||
personDao.save(new Person("Kent", "Zivago", 30));
|
||||
|
||||
final int maxAge = personDao.findMaxAge();
|
||||
Assert.assertTrue(maxAge == 35);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxAgeByName() {
|
||||
personDao.save(new Person("Kent", "Gamma", 20));
|
||||
personDao.save(new Person("Ralph", "Johnson", 35));
|
||||
personDao.save(new Person("Kent", "Zivago", 30));
|
||||
|
||||
final Map<String, Integer> maxAge = personDao.findMaxAgeByName();
|
||||
Assert.assertTrue(maxAge.size() == 2);
|
||||
Assert.assertSame(35, maxAge.get("Ralph"));
|
||||
Assert.assertSame(30, maxAge.get("Kent"));
|
||||
}
|
||||
}
|
||||
+215
@@ -0,0 +1,215 @@
|
||||
/*
|
||||
* (c) Центр ИТ, 2016. Все права защищены.
|
||||
*/
|
||||
package org.baeldung.querydsl.intro;
|
||||
|
||||
import com.querydsl.core.Tuple;
|
||||
import com.querydsl.core.types.dsl.Expressions;
|
||||
import com.querydsl.core.types.dsl.NumberPath;
|
||||
import com.querydsl.jpa.JPAExpressions;
|
||||
import com.querydsl.jpa.impl.JPAQueryFactory;
|
||||
import org.baeldung.querydsl.intro.entities.BlogPost;
|
||||
import org.baeldung.querydsl.intro.entities.QBlogPost;
|
||||
import org.baeldung.querydsl.intro.entities.QUser;
|
||||
import org.baeldung.querydsl.intro.entities.User;
|
||||
import org.junit.*;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class QueryDSLIntegrationTest {
|
||||
|
||||
private static EntityManagerFactory emf;
|
||||
|
||||
private EntityManager em;
|
||||
|
||||
private JPAQueryFactory queryFactory;
|
||||
|
||||
@BeforeClass
|
||||
public static void populateDatabase() {
|
||||
emf = Persistence.createEntityManagerFactory("org.baeldung.querydsl.intro");
|
||||
EntityManager em = emf.createEntityManager();
|
||||
|
||||
em.getTransaction().begin();
|
||||
User user1 = new User();
|
||||
user1.setLogin("David");
|
||||
em.persist(user1);
|
||||
|
||||
User user2 = new User();
|
||||
user2.setLogin("Ash");
|
||||
em.persist(user2);
|
||||
|
||||
User user3 = new User();
|
||||
user3.setLogin("Call");
|
||||
em.persist(user3);
|
||||
|
||||
User user4 = new User();
|
||||
user4.setLogin("Bishop");
|
||||
em.persist(user4);
|
||||
|
||||
BlogPost blogPost1 = new BlogPost();
|
||||
blogPost1.setTitle("Hello World!");
|
||||
blogPost1.setUser(user1);
|
||||
em.persist(blogPost1);
|
||||
|
||||
BlogPost blogPost2 = new BlogPost();
|
||||
blogPost2.setTitle("My Second Post");
|
||||
blogPost2.setUser(user1);
|
||||
em.persist(blogPost2);
|
||||
|
||||
BlogPost blogPost3 = new BlogPost();
|
||||
blogPost3.setTitle("Hello World!");
|
||||
blogPost3.setUser(user3);
|
||||
em.persist(blogPost3);
|
||||
|
||||
em.getTransaction().commit();
|
||||
|
||||
em.close();
|
||||
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
queryFactory = new JPAQueryFactory(em);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindByLogin_thenShouldReturnUser() {
|
||||
|
||||
QUser user = QUser.user;
|
||||
User aUser = queryFactory.selectFrom(user)
|
||||
.where(user.login.eq("David"))
|
||||
.fetchOne();
|
||||
|
||||
assertNotNull(aUser);
|
||||
assertEquals(aUser.getLogin(), "David");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingOrderBy_thenResultsShouldBeOrdered() {
|
||||
|
||||
QUser user = QUser.user;
|
||||
List<User> users = queryFactory.selectFrom(user)
|
||||
.orderBy(user.login.asc())
|
||||
.fetch();
|
||||
|
||||
assertEquals(users.size(), 4);
|
||||
assertEquals(users.get(0).getLogin(), "Ash");
|
||||
assertEquals(users.get(1).getLogin(), "Bishop");
|
||||
assertEquals(users.get(2).getLogin(), "Call");
|
||||
assertEquals(users.get(3).getLogin(), "David");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGroupingByTitle_thenReturnsTuples() {
|
||||
|
||||
QBlogPost blogPost = QBlogPost.blogPost;
|
||||
|
||||
NumberPath<Long> count = Expressions.numberPath(Long.class, "c");
|
||||
|
||||
List<Tuple> userTitleCounts = queryFactory.select(blogPost.title, blogPost.id.count().as(count))
|
||||
.from(blogPost)
|
||||
.groupBy(blogPost.title)
|
||||
.orderBy(count.desc())
|
||||
.fetch();
|
||||
|
||||
assertEquals("Hello World!", userTitleCounts.get(0).get(blogPost.title));
|
||||
assertEquals(new Long(2), userTitleCounts.get(0).get(count));
|
||||
|
||||
assertEquals("My Second Post", userTitleCounts.get(1).get(blogPost.title));
|
||||
assertEquals(new Long(1), userTitleCounts.get(1).get(count));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenJoiningWithCondition_thenResultCountShouldMatch() {
|
||||
|
||||
QUser user = QUser.user;
|
||||
QBlogPost blogPost = QBlogPost.blogPost;
|
||||
|
||||
List<User> users = queryFactory.selectFrom(user)
|
||||
.innerJoin(user.blogPosts, blogPost)
|
||||
.on(blogPost.title.eq("Hello World!"))
|
||||
.fetch();
|
||||
|
||||
assertEquals(2, users.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRefiningWithSubquery_thenResultCountShouldMatch() {
|
||||
|
||||
QUser user = QUser.user;
|
||||
QBlogPost blogPost = QBlogPost.blogPost;
|
||||
|
||||
List<User> users = queryFactory.selectFrom(user)
|
||||
.where(user.id.in(
|
||||
JPAExpressions.select(blogPost.user.id)
|
||||
.from(blogPost)
|
||||
.where(blogPost.title.eq("Hello World!"))))
|
||||
.fetch();
|
||||
|
||||
assertEquals(2, users.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpdating_thenTheRecordShouldChange() {
|
||||
|
||||
QUser user = QUser.user;
|
||||
|
||||
queryFactory.update(user)
|
||||
.where(user.login.eq("Ash"))
|
||||
.set(user.login, "Ash2")
|
||||
.set(user.disabled, true)
|
||||
.execute();
|
||||
|
||||
em.getTransaction().commit();
|
||||
|
||||
em.getTransaction().begin();
|
||||
|
||||
assertEquals(Boolean.TRUE,
|
||||
queryFactory.select(user.disabled)
|
||||
.from(user)
|
||||
.where(user.login.eq("Ash2"))
|
||||
.fetchOne());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeleting_thenTheRecordShouldBeAbsent() {
|
||||
|
||||
QUser user = QUser.user;
|
||||
|
||||
queryFactory.delete(user)
|
||||
.where(user.login.eq("Bishop"))
|
||||
.execute();
|
||||
|
||||
em.getTransaction().commit();
|
||||
|
||||
em.getTransaction().begin();
|
||||
|
||||
assertNull(queryFactory.selectFrom(user)
|
||||
.where(user.login.eq("Bishop"))
|
||||
.fetchOne());
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() {
|
||||
emf.close();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#In memory db
|
||||
db.username=sa
|
||||
db.password=
|
||||
db.driver=org.hsqldb.jdbcDriver
|
||||
db.url=jdbc:hsqldb:mem:app-db
|
||||
db.dialect=org.hibernate.dialect.HSQLDialect
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
|
||||
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xmlns:sec="http://www.springframework.org/schema/security"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
|
||||
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
|
||||
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
|
||||
>
|
||||
|
||||
<tx:annotation-driven />
|
||||
<context:component-scan base-package="org.baeldung" />
|
||||
|
||||
<import resource="test-db.xml" />
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,49 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:mvc="http://www.springframework.org/schema/mvc"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" default-autowire="byName">
|
||||
|
||||
<bean id="placeholderConfig"
|
||||
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
|
||||
<property name="locations">
|
||||
<list>
|
||||
<value>classpath:db.properties</value>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" name="EntityManagerFactory">
|
||||
<property name="persistenceUnitName" value="default"></property>
|
||||
<property name="dataSource" ref="dataSource"></property>
|
||||
<property name="jpaVendorAdapter">
|
||||
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
|
||||
<property name="showSql" value="true" />
|
||||
<property name="generateDdl" value="true" />
|
||||
<property name="databasePlatform" value="${db.dialect}" />
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
|
||||
destroy-method="close">
|
||||
<property name="driverClassName" value="${db.driver}" />
|
||||
<property name="url" value="${db.url}" />
|
||||
<property name="username" value="${db.username}" />
|
||||
<property name="password" value="${db.password}" />
|
||||
</bean>
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" name="TransactionManager">
|
||||
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
|
||||
</bean>
|
||||
|
||||
<tx:annotation-driven />
|
||||
|
||||
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user