Skip to content

Commit 9a85dfe

Browse files
committed
Annotation Config was added. Now AppConfig contains a Main Method (same logic in App.java) to execute
1 parent 9d4fff6 commit 9a85dfe

File tree

3 files changed

+295
-0
lines changed

3 files changed

+295
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.iluwatar.repository;
2+
3+
import java.sql.SQLException;
4+
import java.util.List;
5+
import java.util.Properties;
6+
7+
import javax.sql.DataSource;
8+
9+
import org.apache.commons.dbcp.BasicDataSource;
10+
import org.hibernate.jpa.HibernatePersistenceProvider;
11+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
14+
import org.springframework.orm.jpa.JpaTransactionManager;
15+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
16+
17+
/**
18+
* Annotations based configuration for Spring
19+
*
20+
*/
21+
@EnableJpaRepositories
22+
public class AppConfig {
23+
24+
/**
25+
* Creation of H2 db
26+
*
27+
* @return A new Instance of DataSource
28+
*/
29+
@Bean(destroyMethod = "close")
30+
public DataSource dataSource() {
31+
BasicDataSource basicDataSource = new BasicDataSource();
32+
basicDataSource.setDriverClassName("org.h2.Driver");
33+
basicDataSource.setUrl("jdbc:h2:~/databases/person");
34+
basicDataSource.setUsername("sa");
35+
basicDataSource.setPassword("sa");
36+
return (DataSource) basicDataSource;
37+
}
38+
39+
/**
40+
* Factory to create a especific instance of Entity Manager
41+
* @return
42+
*/
43+
@Bean
44+
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
45+
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
46+
entityManager.setDataSource(dataSource());
47+
entityManager.setPackagesToScan("com.iluwatar");
48+
entityManager.setPersistenceProvider(new HibernatePersistenceProvider());
49+
entityManager.setJpaProperties(jpaProperties());
50+
51+
return entityManager;
52+
}
53+
54+
/**
55+
* Properties for Jpa
56+
* @return
57+
*/
58+
private Properties jpaProperties() {
59+
Properties properties = new Properties();
60+
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
61+
properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
62+
return properties;
63+
}
64+
65+
@Bean
66+
public JpaTransactionManager transactionManager() throws SQLException {
67+
JpaTransactionManager transactionManager = new JpaTransactionManager();
68+
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
69+
return transactionManager;
70+
}
71+
72+
/**
73+
* Program entry point
74+
*
75+
* @param args command line args
76+
*/
77+
public static void main(String[] args) {
78+
79+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
80+
PersonRepository repository = context.getBean(PersonRepository.class);
81+
82+
Person peter = new Person("Peter", "Sagan", 17);
83+
Person nasta = new Person("Nasta", "Kuzminova", 25);
84+
Person john = new Person("John", "lawrence", 35);
85+
Person terry = new Person("Terry", "Law", 36);
86+
87+
// Add new Person records
88+
repository.save(peter);
89+
repository.save(nasta);
90+
repository.save(john);
91+
repository.save(terry);
92+
93+
// Count Person records
94+
System.out.println("Count Person records: " + repository.count());
95+
96+
// Print all records
97+
List<Person> persons = (List<Person>) repository.findAll();
98+
for (Person person : persons) {
99+
System.out.println(person);
100+
}
101+
102+
// Update Person
103+
nasta.setName("Barbora");
104+
nasta.setSurname("Spotakova");
105+
repository.save(nasta);
106+
107+
System.out.println("Find by id 2: " + repository.findOne(2L));
108+
109+
// Remove record from Person
110+
repository.delete(2L);
111+
112+
// count records
113+
System.out.println("Count Person records: " + repository.count());
114+
115+
// find by name
116+
Person p = repository.findOne(new PersonSpecifications.NameEqualSpec("John"));
117+
System.out.println("Find by John is " + p);
118+
119+
// find by age
120+
persons = repository.findAll(new PersonSpecifications.AgeBetweenSpec(20, 40));
121+
122+
System.out.println("Find Person with age between 20,40: ");
123+
for (Person person : persons) {
124+
System.out.println(person);
125+
}
126+
127+
context.close();
128+
129+
}
130+
131+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.iluwatar.repository;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNull;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
import javax.annotation.Resource;
11+
12+
import org.junit.After;
13+
import org.junit.Before;
14+
import org.junit.Test;
15+
import org.junit.runner.RunWith;
16+
import org.springframework.test.context.ContextConfiguration;
17+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
18+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
19+
20+
import com.google.common.collect.Lists;
21+
22+
/**
23+
* Test case to test the functions of {@link PersonRepository}, beside the CRUD functions, the query
24+
* by {@link org.springframework.data.jpa.domain.Specification} are also test.
25+
*/
26+
@RunWith(SpringJUnit4ClassRunner.class)
27+
@ContextConfiguration(classes = { AppConfig.class}, loader = AnnotationConfigContextLoader.class)
28+
public class AnnotationBasedRepositoryTest {
29+
30+
@Resource
31+
private PersonRepository repository;
32+
33+
Person peter = new Person("Peter", "Sagan", 17);
34+
Person nasta = new Person("Nasta", "Kuzminova", 25);
35+
Person john = new Person("John", "lawrence", 35);
36+
Person terry = new Person("Terry", "Law", 36);
37+
38+
List<Person> persons = Arrays.asList(peter, nasta, john, terry);
39+
40+
/**
41+
* Prepare data for test
42+
*/
43+
@Before
44+
public void setup() {
45+
46+
repository.save(persons);
47+
}
48+
49+
@Test
50+
public void testFindAll() {
51+
52+
List<Person> actuals = Lists.newArrayList(repository.findAll());
53+
assertTrue(actuals.containsAll(persons) && persons.containsAll(actuals));
54+
}
55+
56+
@Test
57+
public void testSave() {
58+
59+
Person terry = repository.findByName("Terry");
60+
terry.setSurname("Lee");
61+
terry.setAge(47);
62+
repository.save(terry);
63+
64+
terry = repository.findByName("Terry");
65+
assertEquals(terry.getSurname(), "Lee");
66+
assertEquals(47, terry.getAge());
67+
}
68+
69+
@Test
70+
public void testDelete() {
71+
72+
Person terry = repository.findByName("Terry");
73+
repository.delete(terry);
74+
75+
assertEquals(3, repository.count());
76+
assertNull(repository.findByName("Terry"));
77+
}
78+
79+
@Test
80+
public void testCount() {
81+
82+
assertEquals(4, repository.count());
83+
}
84+
85+
@Test
86+
public void testFindAllByAgeBetweenSpec() {
87+
88+
List<Person> persons = repository.findAll(new PersonSpecifications.AgeBetweenSpec(20, 40));
89+
90+
assertEquals(3, persons.size());
91+
assertTrue(persons.stream().allMatch((item) -> {
92+
return item.getAge() > 20 && item.getAge() < 40;
93+
}));
94+
}
95+
96+
@Test
97+
public void testFindOneByNameEqualSpec() {
98+
99+
Person actual = repository.findOne(new PersonSpecifications.NameEqualSpec("Terry"));
100+
assertEquals(terry, actual);
101+
}
102+
103+
@After
104+
public void cleanup() {
105+
106+
repository.deleteAll();
107+
}
108+
109+
}
110+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.iluwatar.repository;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
8+
import javax.sql.DataSource;
9+
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.test.context.ContextConfiguration;
14+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
15+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
16+
import org.springframework.transaction.annotation.Transactional;
17+
18+
/**
19+
* This case is Just for test the Annotation Based configuration
20+
*
21+
*/
22+
@RunWith(SpringJUnit4ClassRunner.class)
23+
@ContextConfiguration(classes = { AppConfig.class}, loader = AnnotationConfigContextLoader.class)
24+
public class AppConfigTest {
25+
26+
@Autowired
27+
DataSource dataSource;
28+
29+
/**
30+
* Test for bean instance
31+
*/
32+
@Test
33+
public void testDataSource() {
34+
assertNotNull(dataSource);
35+
}
36+
37+
/**
38+
* Test for correct query execution
39+
* @throws SQLException
40+
*/
41+
@Test
42+
@Transactional
43+
public void testQuery() throws SQLException{
44+
ResultSet resultSet = dataSource.getConnection().createStatement().executeQuery("SELECT 1");
45+
String result = null;
46+
String expected = "1";
47+
while (resultSet.next()) {
48+
result = resultSet.getString(1);
49+
50+
}
51+
assertTrue(result.equals(expected));
52+
}
53+
54+
}

0 commit comments

Comments
 (0)