├── .gitignore ├── LICENSE ├── README.md ├── pom.xml └── src ├── main └── java │ └── com │ └── kubrynski │ └── data │ ├── config │ └── DataConfig.java │ ├── model │ ├── AbstractEntity.java │ ├── Company.java │ ├── CompanyDTO.java │ ├── Project.java │ └── User.java │ └── repository │ ├── CompanyRepository.java │ ├── CompanyRepositoryCustom.java │ ├── CompanyRepositoryImpl.java │ ├── ProjectRepository.java │ ├── UserRepository.java │ └── generic │ ├── GenericRepository.java │ ├── GenericRepositoryFactory.java │ ├── GenericRepositoryFactoryBean.java │ └── GenericRepositoryImpl.java └── test └── java └── com └── kubrynski └── data └── repository ├── AdvancedTest.java ├── CustomRepositoryFactoryTest.java ├── IntermediateTest.java └── StartupTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Package Files # 4 | *.jar 5 | *.war 6 | *.ear 7 | 8 | .idea/* 9 | *.iml -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Jakub Kubryński 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | spring-data-examples 2 | ==================== 3 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.kubrynski 8 | spring-data-example 9 | 1.0.0-SNAPSHOT 10 | 11 | 12 | UTF-8 13 | 14 | 15 | 16 | 17 | org.springframework.data 18 | spring-data-jpa 19 | 1.4.1.RELEASE 20 | 21 | 22 | cglib 23 | cglib-nodep 24 | 2.1_3 25 | 26 | 27 | 28 | org.hibernate.javax.persistence 29 | hibernate-jpa-2.0-api 30 | 1.0.1.Final 31 | 32 | 33 | org.hibernate 34 | hibernate-entitymanager 35 | 4.2.6.Final 36 | 37 | 38 | 39 | com.google.guava 40 | guava 41 | 15.0 42 | 43 | 44 | 45 | com.h2database 46 | h2 47 | 1.3.173 48 | runtime 49 | 50 | 51 | 52 | org.testng 53 | testng 54 | 6.8.7 55 | test 56 | 57 | 58 | org.springframework 59 | spring-test 60 | 3.2.4.RELEASE 61 | test 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /src/main/java/com/kubrynski/data/config/DataConfig.java: -------------------------------------------------------------------------------- 1 | package com.kubrynski.data.config; 2 | 3 | import com.kubrynski.data.repository.generic.GenericRepositoryFactoryBean; 4 | import org.hibernate.ejb.HibernatePersistence; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 8 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean; 9 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; 10 | import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean; 11 | import org.springframework.orm.jpa.JpaTransactionManager; 12 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 13 | import org.springframework.transaction.PlatformTransactionManager; 14 | import org.springframework.transaction.annotation.EnableTransactionManagement; 15 | 16 | import javax.sql.DataSource; 17 | 18 | /** 19 | * @author jkubrynski@gmail.com 20 | * @since 2013-03-30 21 | */ 22 | @Configuration 23 | @EnableJpaRepositories(basePackages = "com.kubrynski.data.repository", 24 | repositoryFactoryBeanClass = GenericRepositoryFactoryBean.class) 25 | @EnableTransactionManagement 26 | public class DataConfig { 27 | 28 | @Bean 29 | public DataSource dataSource() { 30 | EmbeddedDatabaseFactoryBean databaseFactoryBean = new EmbeddedDatabaseFactoryBean(); 31 | databaseFactoryBean.setDatabaseType(EmbeddedDatabaseType.H2); 32 | databaseFactoryBean.afterPropertiesSet(); 33 | return databaseFactoryBean.getObject(); 34 | } 35 | 36 | @Bean 37 | public AbstractEntityManagerFactoryBean entityManagerFactory() { 38 | LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean(); 39 | entityManagerFactory.setDataSource(dataSource()); 40 | entityManagerFactory.setPackagesToScan("com.kubrynski.data.model"); 41 | entityManagerFactory.setPersistenceProvider(new HibernatePersistence()); 42 | entityManagerFactory.getJpaPropertyMap().put("hibernate.hbm2ddl.auto", "create-drop"); 43 | entityManagerFactory.getJpaPropertyMap().put("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); 44 | entityManagerFactory.getJpaPropertyMap().put("hibernate.show_sql", "true"); 45 | entityManagerFactory.afterPropertiesSet(); 46 | 47 | return entityManagerFactory; 48 | } 49 | 50 | @Bean 51 | public PlatformTransactionManager transactionManager() { 52 | return new JpaTransactionManager(entityManagerFactory().getObject()); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/kubrynski/data/model/AbstractEntity.java: -------------------------------------------------------------------------------- 1 | package com.kubrynski.data.model; 2 | 3 | import javax.persistence.GeneratedValue; 4 | import javax.persistence.GenerationType; 5 | import javax.persistence.Id; 6 | import javax.persistence.MappedSuperclass; 7 | import java.io.Serializable; 8 | import java.util.UUID; 9 | 10 | /** 11 | * @author jkubrynski@gmail.com 12 | * @since 2013-03-30 13 | */ 14 | @MappedSuperclass 15 | public class AbstractEntity implements Serializable { 16 | 17 | private static final long serialVersionUID = 1L; 18 | 19 | @Id 20 | @GeneratedValue(strategy = GenerationType.AUTO) 21 | private Long id; 22 | 23 | private String uuid = UUID.randomUUID().toString(); 24 | 25 | public Long getId() { 26 | return id; 27 | } 28 | 29 | public String getUuid() { 30 | return uuid; 31 | } 32 | 33 | @Override 34 | public int hashCode() { 35 | return uuid.hashCode(); 36 | } 37 | 38 | @Override 39 | public boolean equals(Object obj) { 40 | if (this == obj) { 41 | return true; 42 | } 43 | if (!(obj instanceof AbstractEntity)) { 44 | return false; 45 | } 46 | AbstractEntity other = (AbstractEntity) obj; 47 | return getUuid().equals(other.getUuid()); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/kubrynski/data/model/Company.java: -------------------------------------------------------------------------------- 1 | package com.kubrynski.data.model; 2 | 3 | import javax.persistence.CascadeType; 4 | import javax.persistence.ElementCollection; 5 | import javax.persistence.Entity; 6 | import javax.persistence.NamedQuery; 7 | import javax.persistence.OneToMany; 8 | import java.util.HashSet; 9 | import java.util.Set; 10 | 11 | /** 12 | * @author jkubrynski@gmail.com 13 | * @since 2013-10-22 14 | */ 15 | @Entity 16 | @NamedQuery(name = "Company.legacyNamedQuery", query = "select c from Company c where c.users.size > ?1") 17 | public class Company extends AbstractEntity { 18 | 19 | private String name; 20 | 21 | @OneToMany(cascade = CascadeType.ALL) 22 | private Set users = new HashSet(); 23 | 24 | @OneToMany(cascade = CascadeType.ALL) 25 | private Set projects = new HashSet(); 26 | 27 | @ElementCollection 28 | private Set technolgies = new HashSet(); 29 | 30 | public Company() { 31 | } 32 | 33 | public Company(String name) { 34 | this.name = name; 35 | } 36 | 37 | public String getName() { 38 | return name; 39 | } 40 | 41 | public void setName(String name) { 42 | this.name = name; 43 | } 44 | 45 | public Set getUsers() { 46 | return users; 47 | } 48 | 49 | public void setUsers(Set users) { 50 | this.users = users; 51 | } 52 | 53 | public void addUser(User user) { 54 | user.setCompany(this); 55 | users.add(user); 56 | } 57 | 58 | public Set getProjects() { 59 | return projects; 60 | } 61 | 62 | public void setProjects(Set projects) { 63 | this.projects = projects; 64 | } 65 | 66 | public Set getTechnolgies() { 67 | return technolgies; 68 | } 69 | 70 | public void setTechnolgies(Set technolgies) { 71 | this.technolgies = technolgies; 72 | } 73 | 74 | public void addProject(Project project) { 75 | projects.add(project); 76 | project.setCompany(this); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/com/kubrynski/data/model/CompanyDTO.java: -------------------------------------------------------------------------------- 1 | package com.kubrynski.data.model; 2 | 3 | /** 4 | * @author jkubrynski@gmail.com 5 | * @since 2013-10-22 6 | */ 7 | public class CompanyDTO { 8 | 9 | public String name; 10 | 11 | public CompanyDTO(String name) { 12 | this.name = name; 13 | } 14 | 15 | public String getName() { 16 | return name; 17 | } 18 | 19 | public void setName(String name) { 20 | this.name = name; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/kubrynski/data/model/Project.java: -------------------------------------------------------------------------------- 1 | package com.kubrynski.data.model; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.ManyToOne; 5 | 6 | /** 7 | * @author jkubrynski@gmail.com 8 | * @since 2013-10-22 9 | */ 10 | @Entity 11 | public class Project extends AbstractEntity { 12 | 13 | private String name; 14 | 15 | @ManyToOne 16 | private Company company; 17 | 18 | public Project() { 19 | } 20 | 21 | public Project(String name) { 22 | this.name = name; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | public void setCompany(Company company) { 34 | this.company = company; 35 | } 36 | 37 | public Company getCompany() { 38 | return company; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/kubrynski/data/model/User.java: -------------------------------------------------------------------------------- 1 | package com.kubrynski.data.model; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.ManyToOne; 5 | 6 | /** 7 | * @author jkubrynski@gmail.com 8 | * @since 2013-10-11 9 | */ 10 | @Entity 11 | public class User extends AbstractEntity { 12 | 13 | private String login; 14 | private String email; 15 | 16 | @ManyToOne 17 | private Company company; 18 | 19 | public User() { 20 | } 21 | 22 | public User(String login) { 23 | this.login = login; 24 | } 25 | 26 | public String getLogin() { 27 | return login; 28 | } 29 | 30 | public void setLogin(String login) { 31 | this.login = login; 32 | } 33 | 34 | public String getEmail() { 35 | return email; 36 | } 37 | 38 | public void setEmail(String email) { 39 | this.email = email; 40 | } 41 | 42 | public Company getCompany() { 43 | return company; 44 | } 45 | 46 | public void setCompany(Company company) { 47 | this.company = company; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/kubrynski/data/repository/CompanyRepository.java: -------------------------------------------------------------------------------- 1 | package com.kubrynski.data.repository; 2 | 3 | import com.kubrynski.data.model.Company; 4 | import com.kubrynski.data.model.CompanyDTO; 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.data.jpa.repository.Query; 7 | 8 | import java.util.List; 9 | import java.util.Set; 10 | 11 | /** 12 | * @author jkubrynski@gmail.com 13 | * @since 2013-04-11 14 | */ 15 | public interface CompanyRepository extends JpaRepository, CompanyRepositoryCustom { 16 | 17 | @Query("select new com.kubrynski.data.model.CompanyDTO(c.name) from Company c") 18 | List packAllIntoDTO(); 19 | 20 | List legacyNamedQuery(Integer usersCount); 21 | 22 | List findByTechnolgies(String technology); 23 | 24 | List findDistinctByTechnolgiesIn(Set technology); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/kubrynski/data/repository/CompanyRepositoryCustom.java: -------------------------------------------------------------------------------- 1 | package com.kubrynski.data.repository; 2 | 3 | import com.kubrynski.data.model.Company; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author jkubrynski@gmail.com 9 | * @since 2013-10-22 10 | */ 11 | public interface CompanyRepositoryCustom { 12 | 13 | List findCompaniesBiggerThan(final int size); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/kubrynski/data/repository/CompanyRepositoryImpl.java: -------------------------------------------------------------------------------- 1 | package com.kubrynski.data.repository; 2 | 3 | import com.google.common.base.Predicate; 4 | import com.google.common.collect.Collections2; 5 | import com.google.common.collect.Lists; 6 | import com.kubrynski.data.model.Company; 7 | import org.springframework.transaction.annotation.Transactional; 8 | 9 | import javax.persistence.EntityManager; 10 | import javax.persistence.PersistenceContext; 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | /** 15 | * @author jkubrynski@gmail.com 16 | * @since 2013-10-22 17 | */ 18 | public class CompanyRepositoryImpl implements CompanyRepositoryCustom { 19 | 20 | @PersistenceContext 21 | private EntityManager em; 22 | 23 | @Override 24 | @Transactional 25 | public List findCompaniesBiggerThan(final int size) { 26 | List companies = em.createQuery("select c from Company c").getResultList(); 27 | 28 | ArrayList result = Lists.newArrayList(Collections2.filter(companies, new Predicate() { 29 | @Override 30 | public boolean apply(Company company) { 31 | return company.getUsers().size() > size; 32 | } 33 | })); 34 | 35 | return result; 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/kubrynski/data/repository/ProjectRepository.java: -------------------------------------------------------------------------------- 1 | package com.kubrynski.data.repository; 2 | 3 | import com.kubrynski.data.model.Project; 4 | import com.kubrynski.data.repository.generic.GenericRepository; 5 | 6 | /** 7 | * @author jkubrynski@gmail.com 8 | * @since 2013-10-22 9 | */ 10 | public interface ProjectRepository extends GenericRepository { 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/kubrynski/data/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.kubrynski.data.repository; 2 | 3 | import com.kubrynski.data.model.User; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * @author jkubrynski@gmail.com 10 | * @since 2013-04-11 11 | */ 12 | public interface UserRepository extends JpaRepository { 13 | 14 | User findUserByLogin(String login); 15 | 16 | List findByEmailLikeIgnoreCase(String email); 17 | 18 | List findByCompany_Name(String companyName); 19 | 20 | List findByCompanyNameOrderByLoginAsc(String companyName); 21 | 22 | List findByCompany_Projects_Name(String projectName); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/kubrynski/data/repository/generic/GenericRepository.java: -------------------------------------------------------------------------------- 1 | package com.kubrynski.data.repository.generic; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.data.repository.NoRepositoryBean; 5 | 6 | /** 7 | * @author jkubrynski@gmail.com 8 | * @since 2013-10-22 9 | */ 10 | @NoRepositoryBean 11 | public interface GenericRepository extends JpaRepository { 12 | 13 | T findByUuid(String uuid); 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/kubrynski/data/repository/generic/GenericRepositoryFactory.java: -------------------------------------------------------------------------------- 1 | package com.kubrynski.data.repository.generic; 2 | 3 | import org.springframework.data.jpa.repository.support.JpaRepositoryFactory; 4 | import org.springframework.data.repository.core.RepositoryMetadata; 5 | 6 | import javax.persistence.EntityManager; 7 | import java.io.Serializable; 8 | 9 | /** 10 | * @author jkubrynski@gmail.com 11 | * @since 2013-10-22 12 | */ 13 | public class GenericRepositoryFactory extends JpaRepositoryFactory { 14 | 15 | /** 16 | * Creates a new {@link org.springframework.data.jpa.repository.support.JpaRepositoryFactory}. 17 | * 18 | * @param entityManager must not be {@literal null} 19 | */ 20 | public GenericRepositoryFactory(EntityManager entityManager) { 21 | super(entityManager); 22 | } 23 | 24 | @Override 25 | protected GenericRepository getTargetRepository(RepositoryMetadata metadata, EntityManager entityManager) { 26 | return new GenericRepositoryImpl(getEntityInformation((Class) metadata.getDomainType()), entityManager); 27 | } 28 | 29 | @Override 30 | protected Class getRepositoryBaseClass(RepositoryMetadata metadata) { 31 | return GenericRepositoryImpl.class; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/kubrynski/data/repository/generic/GenericRepositoryFactoryBean.java: -------------------------------------------------------------------------------- 1 | package com.kubrynski.data.repository.generic; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean; 5 | import org.springframework.data.repository.core.support.RepositoryFactorySupport; 6 | 7 | import javax.persistence.EntityManager; 8 | import java.io.Serializable; 9 | 10 | /** 11 | * @author jkubrynski@gmail.com 12 | * @since 2013-10-22 13 | */ 14 | public class GenericRepositoryFactoryBean, S, ID extends Serializable> 15 | extends JpaRepositoryFactoryBean { 16 | 17 | /** 18 | * Returns a {@link org.springframework.data.repository.core.support.RepositoryFactorySupport}. 19 | * 20 | * @param entityManager 21 | * @return 22 | */ 23 | protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) { 24 | return new GenericRepositoryFactory(entityManager); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/kubrynski/data/repository/generic/GenericRepositoryImpl.java: -------------------------------------------------------------------------------- 1 | package com.kubrynski.data.repository.generic; 2 | 3 | import org.springframework.data.jpa.repository.support.JpaEntityInformation; 4 | import org.springframework.data.jpa.repository.support.SimpleJpaRepository; 5 | 6 | import javax.persistence.EntityManager; 7 | import javax.persistence.Query; 8 | import java.io.Serializable; 9 | 10 | /** 11 | * @author jkubrynski@gmail.com 12 | * @since 2013-10-22 13 | */ 14 | public class GenericRepositoryImpl extends SimpleJpaRepository implements GenericRepository { 15 | 16 | private JpaEntityInformation entityInformation; 17 | private final EntityManager entityManager; 18 | 19 | public GenericRepositoryImpl(JpaEntityInformation entityInformation, EntityManager entityManager) { 20 | super(entityInformation, entityManager); 21 | this.entityInformation = entityInformation; 22 | this.entityManager = entityManager; 23 | } 24 | 25 | @Override 26 | @SuppressWarnings("unchecked") 27 | public T findByUuid(String uuid) { 28 | 29 | Query query = entityManager.createQuery("from " + entityInformation.getEntityName() + " e where e.uuid = ?1") 30 | .setParameter(1, uuid); 31 | 32 | return (T) query.getSingleResult(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/com/kubrynski/data/repository/AdvancedTest.java: -------------------------------------------------------------------------------- 1 | package com.kubrynski.data.repository; 2 | 3 | import com.kubrynski.data.config.DataConfig; 4 | import com.kubrynski.data.model.Company; 5 | import com.kubrynski.data.model.CompanyDTO; 6 | import com.kubrynski.data.model.User; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.test.context.ContextConfiguration; 9 | import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; 10 | import org.testng.annotations.BeforeMethod; 11 | import org.testng.annotations.Test; 12 | 13 | import java.util.HashSet; 14 | import java.util.List; 15 | 16 | import static org.testng.Assert.assertEquals; 17 | import static org.testng.Assert.assertNotNull; 18 | 19 | /** 20 | * @author jkubrynski@gmail.com 21 | * @since 2013-10-22 22 | */ 23 | @Test 24 | @ContextConfiguration(classes = DataConfig.class) 25 | public class AdvancedTest extends AbstractTestNGSpringContextTests { 26 | 27 | @Autowired 28 | private CompanyRepository companyRepository; 29 | 30 | @BeforeMethod 31 | public void setUp() throws Exception { 32 | companyRepository.deleteAll(); 33 | 34 | Company wjug = new Company("WJUG"); 35 | wjug.addUser(new User("Adam")); 36 | wjug.addUser(new User("Leszek")); 37 | wjug.addUser(new User("Zbigniew")); 38 | wjug.getTechnolgies().add("Java"); 39 | wjug.getTechnolgies().add("Scala"); 40 | wjug.getTechnolgies().add("JRuby"); 41 | companyRepository.save(wjug); 42 | Company bjug = new Company("BJUG"); 43 | bjug.addUser(new User("Kazimierz")); 44 | bjug.addUser(new User("Zenon")); 45 | bjug.getTechnolgies().add("Groovy"); 46 | companyRepository.save(bjug); 47 | } 48 | 49 | public void shouldReturnCompaniesInDTOs() { 50 | List companyDTOs = companyRepository.packAllIntoDTO(); 51 | assertNotNull(companyDTOs); 52 | assertEquals(companyDTOs.size(), 2); 53 | } 54 | 55 | public void shouldReturnCompaniesByNamedQuery() { 56 | List companies = companyRepository.legacyNamedQuery(2); 57 | assertNotNull(companies); 58 | assertEquals(companies.size(), 1); 59 | assertEquals(companies.get(0).getName(), "WJUG"); 60 | } 61 | 62 | public void shouldReturnCompaniesByCustomMethod() { 63 | List companies = companyRepository.findCompaniesBiggerThan(2); 64 | assertNotNull(companies); 65 | assertEquals(companies.size(), 1); 66 | assertEquals(companies.get(0).getName(), "WJUG"); 67 | } 68 | 69 | public void shouldReturnCompaniesWorkingInTechnology() { 70 | List companies = companyRepository.findByTechnolgies("Java"); 71 | assertNotNull(companies); 72 | assertEquals(companies.size(), 1); 73 | assertEquals(companies.get(0).getName(), "WJUG"); 74 | } 75 | 76 | public void shouldReturnCompaniesWorkingInTechnologies() { 77 | HashSet technologies = new HashSet(); 78 | technologies.add("Java"); 79 | technologies.add("Scala"); 80 | List companies = companyRepository.findDistinctByTechnolgiesIn(technologies); 81 | assertNotNull(companies); 82 | assertEquals(companies.size(), 1); 83 | assertEquals(companies.get(0).getName(), "WJUG"); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/test/java/com/kubrynski/data/repository/CustomRepositoryFactoryTest.java: -------------------------------------------------------------------------------- 1 | package com.kubrynski.data.repository; 2 | 3 | import com.kubrynski.data.config.DataConfig; 4 | import com.kubrynski.data.model.Project; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.test.context.ContextConfiguration; 7 | import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; 8 | import org.testng.annotations.BeforeMethod; 9 | import org.testng.annotations.Test; 10 | 11 | import java.util.List; 12 | 13 | import static org.testng.Assert.assertEquals; 14 | import static org.testng.Assert.assertNotNull; 15 | 16 | /** 17 | * @author jkubrynski@gmail.com 18 | * @since 2013-10-22 19 | */ 20 | @Test 21 | @ContextConfiguration(classes = DataConfig.class) 22 | public class CustomRepositoryFactoryTest extends AbstractTestNGSpringContextTests { 23 | 24 | @Autowired 25 | private ProjectRepository projectRepository; 26 | 27 | @BeforeMethod 28 | public void setUp() throws Exception { 29 | projectRepository.deleteAll(); 30 | projectRepository.save(new Project("big")); 31 | projectRepository.save(new Project("small")); 32 | } 33 | 34 | public void shouldUseGenericRepo() { 35 | List all = projectRepository.findAll(); 36 | 37 | String uuid = all.get(0).getUuid(); 38 | Project byUuid = projectRepository.findByUuid(uuid); 39 | 40 | assertNotNull(byUuid); 41 | assertEquals(byUuid, all.get(0)); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/com/kubrynski/data/repository/IntermediateTest.java: -------------------------------------------------------------------------------- 1 | package com.kubrynski.data.repository; 2 | 3 | import com.kubrynski.data.config.DataConfig; 4 | import com.kubrynski.data.model.Company; 5 | import com.kubrynski.data.model.Project; 6 | import com.kubrynski.data.model.User; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.data.domain.Page; 9 | import org.springframework.data.domain.PageRequest; 10 | import org.springframework.test.context.ContextConfiguration; 11 | import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; 12 | import org.testng.annotations.BeforeClass; 13 | import org.testng.annotations.Test; 14 | 15 | import java.util.List; 16 | 17 | import static org.testng.Assert.assertEquals; 18 | import static org.testng.Assert.assertNotNull; 19 | 20 | /** 21 | * @author jkubrynski@gmail.com 22 | * @since 2013-10-22 23 | */ 24 | @Test 25 | @ContextConfiguration(classes = DataConfig.class) 26 | public class IntermediateTest extends AbstractTestNGSpringContextTests { 27 | 28 | private static final String COMPANY_NAME = "WJUG"; 29 | private static final String FIRST_USER_LOGIN = "Adam"; 30 | 31 | @Autowired 32 | private CompanyRepository companyRepository; 33 | 34 | @Autowired 35 | private UserRepository userRepository; 36 | 37 | @BeforeClass 38 | public void setUp() throws Exception { 39 | companyRepository.deleteAll(); 40 | 41 | Company company = new Company(); 42 | company.setName(COMPANY_NAME); 43 | 44 | company.addProject(new Project("big")); 45 | company.addProject(new Project("small")); 46 | 47 | company.addUser(new User("Zenon")); 48 | company.addUser(new User(FIRST_USER_LOGIN)); 49 | companyRepository.save(company); 50 | 51 | Company otherCompany = new Company("BJUG"); 52 | otherCompany.addUser(new User()); 53 | companyRepository.save(otherCompany); 54 | 55 | companyRepository.save(new Company("PJUG")); 56 | } 57 | 58 | public void shouldReturnUsersByCompanyName() { 59 | List users = userRepository.findByCompany_Name(COMPANY_NAME); 60 | 61 | assertNotNull(users); 62 | assertEquals(users.size(), 2); 63 | } 64 | 65 | public void shouldReturnUsersByCompanyNameOrderedByLogin() { 66 | List users = userRepository.findByCompanyNameOrderByLoginAsc(COMPANY_NAME); 67 | 68 | assertNotNull(users); 69 | assertEquals(users.size(), 2); 70 | assertEquals(users.get(0).getLogin(), FIRST_USER_LOGIN); 71 | } 72 | 73 | public void shouldReturnPaginatedCompanies() { 74 | Page firstPage = companyRepository.findAll(new PageRequest(0, 2)); 75 | assertNotNull(firstPage); 76 | assertEquals(firstPage.getSize(), 2); 77 | assertEquals(firstPage.getNumberOfElements(), 2); 78 | assertEquals(firstPage.getNumber(), 0); 79 | assertEquals(firstPage.getTotalElements(), 3); 80 | assertEquals(firstPage.getTotalPages(), 2); 81 | 82 | Page secondPage = companyRepository.findAll(new PageRequest(1, 2)); 83 | assertEquals(secondPage.getSize(), 2); 84 | assertEquals(secondPage.getNumberOfElements(), 1); 85 | assertEquals(secondPage.getNumber(), 1); 86 | 87 | } 88 | 89 | public void shouldReturnUsersByProjectName() { 90 | List big = userRepository.findByCompany_Projects_Name("big"); 91 | 92 | assertNotNull(big); 93 | assertEquals(big.size(), 2); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/test/java/com/kubrynski/data/repository/StartupTest.java: -------------------------------------------------------------------------------- 1 | package com.kubrynski.data.repository; 2 | 3 | import com.kubrynski.data.config.DataConfig; 4 | import com.kubrynski.data.model.User; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean; 7 | import org.springframework.test.context.ContextConfiguration; 8 | import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; 9 | import org.testng.annotations.BeforeClass; 10 | import org.testng.annotations.Test; 11 | 12 | import javax.persistence.EntityManager; 13 | import java.util.List; 14 | 15 | import static org.testng.Assert.assertEquals; 16 | import static org.testng.Assert.assertNotNull; 17 | 18 | /** 19 | * @author jkubrynski@gmail.com 20 | * @since 2013-04-09 21 | */ 22 | @Test 23 | @ContextConfiguration(classes = DataConfig.class) 24 | public class StartupTest extends AbstractTestNGSpringContextTests { 25 | 26 | private static final String LOGIN = "my_login"; 27 | private static final String EMAIL = "name@domain.com"; 28 | private static final String OTHER_LOGIN = "other_login"; 29 | private static final String OTHER_EMAIL = "other@Domain.org"; 30 | 31 | @Autowired 32 | private AbstractEntityManagerFactoryBean entityManagerFactoryBean; 33 | 34 | @Autowired 35 | private UserRepository userRepository; 36 | 37 | @BeforeClass 38 | public void setUp() { 39 | EntityManager entityManager = entityManagerFactoryBean.getObject().createEntityManager(); 40 | 41 | User user1 = new User(); 42 | user1.setLogin(LOGIN); 43 | user1.setEmail(EMAIL); 44 | 45 | User user2 = new User(); 46 | user2.setLogin(OTHER_LOGIN); 47 | user2.setEmail(OTHER_EMAIL); 48 | 49 | entityManager.getTransaction().begin(); 50 | entityManager.persist(user1); 51 | entityManager.persist(user2); 52 | entityManager.getTransaction().commit(); 53 | } 54 | 55 | public void shouldReturnUserByLogin() { 56 | User user = userRepository.findUserByLogin(LOGIN); 57 | 58 | assertNotNull(user); 59 | assertEquals(user.getEmail(), EMAIL); 60 | } 61 | 62 | public void shouldReturnUsersByDomainFragment() { 63 | List users = userRepository.findByEmailLikeIgnoreCase("%@domain%"); 64 | assertNotNull(users); 65 | assertEquals(users.size(), 2); 66 | } 67 | 68 | } 69 | --------------------------------------------------------------------------------