Merge pull request #308 from neonds/spring-annotation-config
Annotation Config was added.
This commit is contained in:
		
							
								
								
									
										131
									
								
								repository/src/main/java/com/iluwatar/repository/AppConfig.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										131
									
								
								repository/src/main/java/com/iluwatar/repository/AppConfig.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,131 @@ | |||||||
|  | package com.iluwatar.repository; | ||||||
|  |  | ||||||
|  | import java.sql.SQLException; | ||||||
|  | import java.util.List; | ||||||
|  | import java.util.Properties; | ||||||
|  |  | ||||||
|  | import javax.sql.DataSource; | ||||||
|  |  | ||||||
|  | import org.apache.commons.dbcp.BasicDataSource; | ||||||
|  | import org.hibernate.jpa.HibernatePersistenceProvider; | ||||||
|  | import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||||||
|  | import org.springframework.context.annotation.Bean; | ||||||
|  | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||||||
|  | import org.springframework.orm.jpa.JpaTransactionManager; | ||||||
|  | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Annotations based configuration for Spring | ||||||
|  |  * | ||||||
|  |  */ | ||||||
|  | @EnableJpaRepositories | ||||||
|  | public class AppConfig { | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * Creation of H2 db | ||||||
|  | 	 *  | ||||||
|  | 	 * @return A new Instance of DataSource | ||||||
|  | 	 */ | ||||||
|  | 	@Bean(destroyMethod = "close") | ||||||
|  | 	public DataSource dataSource() { | ||||||
|  | 		BasicDataSource basicDataSource = new BasicDataSource(); | ||||||
|  | 		basicDataSource.setDriverClassName("org.h2.Driver"); | ||||||
|  | 		basicDataSource.setUrl("jdbc:h2:~/databases/person"); | ||||||
|  | 		basicDataSource.setUsername("sa"); | ||||||
|  | 		basicDataSource.setPassword("sa"); | ||||||
|  | 		return (DataSource) basicDataSource; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * Factory to create a especific instance of Entity Manager | ||||||
|  | 	 * @return | ||||||
|  | 	 */ | ||||||
|  | 	@Bean | ||||||
|  | 	public LocalContainerEntityManagerFactoryBean entityManagerFactory() { | ||||||
|  | 		LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean(); | ||||||
|  | 		entityManager.setDataSource(dataSource()); | ||||||
|  | 		entityManager.setPackagesToScan("com.iluwatar"); | ||||||
|  | 		entityManager.setPersistenceProvider(new HibernatePersistenceProvider()); | ||||||
|  | 		entityManager.setJpaProperties(jpaProperties()); | ||||||
|  |  | ||||||
|  | 		return entityManager; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * Properties for Jpa | ||||||
|  | 	 * @return | ||||||
|  | 	 */ | ||||||
|  | 	private Properties jpaProperties() { | ||||||
|  | 		Properties properties = new Properties(); | ||||||
|  | 		properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); | ||||||
|  | 		properties.setProperty("hibernate.hbm2ddl.auto", "create-drop"); | ||||||
|  | 		return properties; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  |     @Bean | ||||||
|  |     public JpaTransactionManager transactionManager() throws SQLException { | ||||||
|  |         JpaTransactionManager transactionManager = new JpaTransactionManager(); | ||||||
|  |         transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); | ||||||
|  |         return transactionManager; | ||||||
|  |     } | ||||||
|  | 	 | ||||||
|  | 	  /** | ||||||
|  | 	   * Program entry point | ||||||
|  | 	   *  | ||||||
|  | 	   * @param args command line args | ||||||
|  | 	   */ | ||||||
|  | 	  public static void main(String[] args) { | ||||||
|  |  | ||||||
|  | 		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); | ||||||
|  | 	    PersonRepository repository = context.getBean(PersonRepository.class); | ||||||
|  |  | ||||||
|  | 	    Person peter = new Person("Peter", "Sagan", 17); | ||||||
|  | 	    Person nasta = new Person("Nasta", "Kuzminova", 25); | ||||||
|  | 	    Person john = new Person("John", "lawrence", 35); | ||||||
|  | 	    Person terry = new Person("Terry", "Law", 36); | ||||||
|  |  | ||||||
|  | 	    // Add new Person records | ||||||
|  | 	    repository.save(peter); | ||||||
|  | 	    repository.save(nasta); | ||||||
|  | 	    repository.save(john); | ||||||
|  | 	    repository.save(terry); | ||||||
|  |  | ||||||
|  | 	    // Count Person records | ||||||
|  | 	    System.out.println("Count Person records: " + repository.count()); | ||||||
|  |  | ||||||
|  | 	    // Print all records | ||||||
|  | 	    List<Person> persons = (List<Person>) repository.findAll(); | ||||||
|  | 	    for (Person person : persons) { | ||||||
|  | 	      System.out.println(person); | ||||||
|  | 	    } | ||||||
|  |  | ||||||
|  | 	    // Update Person | ||||||
|  | 	    nasta.setName("Barbora"); | ||||||
|  | 	    nasta.setSurname("Spotakova"); | ||||||
|  | 	    repository.save(nasta); | ||||||
|  |  | ||||||
|  | 	    System.out.println("Find by id 2: " + repository.findOne(2L)); | ||||||
|  |  | ||||||
|  | 	    // Remove record from Person | ||||||
|  | 	    repository.delete(2L); | ||||||
|  |  | ||||||
|  | 	    // count records | ||||||
|  | 	    System.out.println("Count Person records: " + repository.count()); | ||||||
|  |  | ||||||
|  | 	    // find by name | ||||||
|  | 	    Person p = repository.findOne(new PersonSpecifications.NameEqualSpec("John")); | ||||||
|  | 	    System.out.println("Find by John is " + p); | ||||||
|  |  | ||||||
|  | 	    // find by age | ||||||
|  | 	    persons = repository.findAll(new PersonSpecifications.AgeBetweenSpec(20, 40)); | ||||||
|  |  | ||||||
|  | 	    System.out.println("Find Person with age between 20,40: "); | ||||||
|  | 	    for (Person person : persons) { | ||||||
|  | 	      System.out.println(person); | ||||||
|  | 	    } | ||||||
|  |  | ||||||
|  | 	    context.close(); | ||||||
|  |  | ||||||
|  | 	  } | ||||||
|  |  | ||||||
|  | } | ||||||
| @@ -0,0 +1,110 @@ | |||||||
|  | package com.iluwatar.repository; | ||||||
|  |  | ||||||
|  | import static org.junit.Assert.assertEquals; | ||||||
|  | import static org.junit.Assert.assertNull; | ||||||
|  | import static org.junit.Assert.assertTrue; | ||||||
|  |  | ||||||
|  | import java.util.Arrays; | ||||||
|  | import java.util.List; | ||||||
|  |  | ||||||
|  | import javax.annotation.Resource; | ||||||
|  |  | ||||||
|  | import org.junit.After; | ||||||
|  | import org.junit.Before; | ||||||
|  | import org.junit.Test; | ||||||
|  | import org.junit.runner.RunWith; | ||||||
|  | import org.springframework.test.context.ContextConfiguration; | ||||||
|  | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||||||
|  | import org.springframework.test.context.support.AnnotationConfigContextLoader; | ||||||
|  |  | ||||||
|  | import com.google.common.collect.Lists; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Test case to test the functions of {@link PersonRepository}, beside the CRUD functions, the query | ||||||
|  |  * by {@link org.springframework.data.jpa.domain.Specification} are also test. | ||||||
|  |  */ | ||||||
|  | @RunWith(SpringJUnit4ClassRunner.class) | ||||||
|  | @ContextConfiguration(classes = { AppConfig.class}, loader = AnnotationConfigContextLoader.class) | ||||||
|  | public class AnnotationBasedRepositoryTest { | ||||||
|  |  | ||||||
|  |   @Resource | ||||||
|  |   private PersonRepository repository; | ||||||
|  |  | ||||||
|  |   Person peter = new Person("Peter", "Sagan", 17); | ||||||
|  |   Person nasta = new Person("Nasta", "Kuzminova", 25); | ||||||
|  |   Person john = new Person("John", "lawrence", 35); | ||||||
|  |   Person terry = new Person("Terry", "Law", 36); | ||||||
|  |  | ||||||
|  |   List<Person> persons = Arrays.asList(peter, nasta, john, terry); | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * Prepare data for test | ||||||
|  |    */ | ||||||
|  |   @Before | ||||||
|  |   public void setup() { | ||||||
|  |  | ||||||
|  |     repository.save(persons); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Test | ||||||
|  |   public void testFindAll() { | ||||||
|  |  | ||||||
|  |     List<Person> actuals = Lists.newArrayList(repository.findAll()); | ||||||
|  |     assertTrue(actuals.containsAll(persons) && persons.containsAll(actuals)); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Test | ||||||
|  |   public void testSave() { | ||||||
|  |  | ||||||
|  |     Person terry = repository.findByName("Terry"); | ||||||
|  |     terry.setSurname("Lee"); | ||||||
|  |     terry.setAge(47); | ||||||
|  |     repository.save(terry); | ||||||
|  |  | ||||||
|  |     terry = repository.findByName("Terry"); | ||||||
|  |     assertEquals(terry.getSurname(), "Lee"); | ||||||
|  |     assertEquals(47, terry.getAge()); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Test | ||||||
|  |   public void testDelete() { | ||||||
|  |  | ||||||
|  |     Person terry = repository.findByName("Terry"); | ||||||
|  |     repository.delete(terry); | ||||||
|  |  | ||||||
|  |     assertEquals(3, repository.count()); | ||||||
|  |     assertNull(repository.findByName("Terry")); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Test | ||||||
|  |   public void testCount() { | ||||||
|  |  | ||||||
|  |     assertEquals(4, repository.count()); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Test | ||||||
|  |   public void testFindAllByAgeBetweenSpec() { | ||||||
|  |  | ||||||
|  |     List<Person> persons = repository.findAll(new PersonSpecifications.AgeBetweenSpec(20, 40)); | ||||||
|  |  | ||||||
|  |     assertEquals(3, persons.size()); | ||||||
|  |     assertTrue(persons.stream().allMatch((item) -> { | ||||||
|  |       return item.getAge() > 20 && item.getAge() < 40; | ||||||
|  |     })); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Test | ||||||
|  |   public void testFindOneByNameEqualSpec() { | ||||||
|  |  | ||||||
|  |     Person actual = repository.findOne(new PersonSpecifications.NameEqualSpec("Terry")); | ||||||
|  |     assertEquals(terry, actual); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @After | ||||||
|  |   public void cleanup() { | ||||||
|  |  | ||||||
|  |     repository.deleteAll(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  | } | ||||||
|  |  | ||||||
| @@ -0,0 +1,54 @@ | |||||||
|  | package com.iluwatar.repository; | ||||||
|  |  | ||||||
|  | import static org.junit.Assert.*; | ||||||
|  |  | ||||||
|  | import java.sql.ResultSet; | ||||||
|  | import java.sql.SQLException; | ||||||
|  |  | ||||||
|  | import javax.sql.DataSource; | ||||||
|  |  | ||||||
|  | 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.support.AnnotationConfigContextLoader; | ||||||
|  | import org.springframework.transaction.annotation.Transactional; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * This case is Just for test the Annotation Based configuration | ||||||
|  |  *  | ||||||
|  |  */ | ||||||
|  | @RunWith(SpringJUnit4ClassRunner.class) | ||||||
|  | @ContextConfiguration(classes = { AppConfig.class}, loader = AnnotationConfigContextLoader.class) | ||||||
|  | public class AppConfigTest { | ||||||
|  |  | ||||||
|  | 	@Autowired | ||||||
|  | 	DataSource dataSource; | ||||||
|  | 	 | ||||||
|  | 	/** | ||||||
|  | 	 * Test for bean instance | ||||||
|  | 	 */ | ||||||
|  | 	@Test | ||||||
|  | 	public void testDataSource() { | ||||||
|  | 		assertNotNull(dataSource); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	/** | ||||||
|  | 	 * Test for correct query execution  | ||||||
|  | 	 * @throws SQLException  | ||||||
|  | 	 */ | ||||||
|  | 	@Test | ||||||
|  | 	@Transactional | ||||||
|  | 	public void testQuery() throws SQLException{ | ||||||
|  | 		ResultSet resultSet =  dataSource.getConnection().createStatement().executeQuery("SELECT 1"); | ||||||
|  | 		String result = null; | ||||||
|  | 		String expected = "1"; | ||||||
|  | 		while (resultSet.next()) { | ||||||
|  | 			result = resultSet.getString(1); | ||||||
|  | 			 | ||||||
|  | 		} | ||||||
|  | 		assertTrue(result.equals(expected)); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user