Skip to content

Commit 507b89d

Browse files
committed
Apply project coding conventions to Repository example
1 parent 02d6754 commit 507b89d

File tree

7 files changed

+141
-135
lines changed

7 files changed

+141
-135
lines changed

repository/src/main/java/com/iluwatar/repository/App.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ public class App {
2424
/**
2525
* Program entry point
2626
*
27-
* @param args command line args
27+
* @param args
28+
* command line args
2829
*/
2930
public static void main(String[] args) {
3031

31-
ClassPathXmlApplicationContext context =
32-
new ClassPathXmlApplicationContext("applicationContext.xml");
32+
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
33+
"applicationContext.xml");
3334
PersonRepository repository = context.getBean(PersonRepository.class);
3435

3536
Person peter = new Person("Peter", "Sagan", 17);

repository/src/main/java/com/iluwatar/repository/AppConfig.java

Lines changed: 102 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -15,117 +15,122 @@
1515
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
1616

1717
/**
18-
* Annotations based configuration for Spring
18+
* This is the same example as in {@link App} but with annotations based
19+
* configuration for Spring.
1920
*
2021
*/
2122
@EnableJpaRepositories
2223
public class AppConfig {
2324

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;
25+
/**
26+
* Creation of H2 db
27+
*
28+
* @return A new Instance of DataSource
29+
*/
30+
@Bean(destroyMethod = "close")
31+
public DataSource dataSource() {
32+
BasicDataSource basicDataSource = new BasicDataSource();
33+
basicDataSource.setDriverClassName("org.h2.Driver");
34+
basicDataSource.setUrl("jdbc:h2:~/databases/person");
35+
basicDataSource.setUsername("sa");
36+
basicDataSource.setPassword("sa");
37+
return (DataSource) basicDataSource;
38+
}
39+
40+
/**
41+
* Factory to create a especific instance of Entity Manager
42+
*
43+
* @return
44+
*/
45+
@Bean
46+
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
47+
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
48+
entityManager.setDataSource(dataSource());
49+
entityManager.setPackagesToScan("com.iluwatar");
50+
entityManager.setPersistenceProvider(new HibernatePersistenceProvider());
51+
entityManager.setJpaProperties(jpaProperties());
52+
53+
return entityManager;
54+
}
55+
56+
/**
57+
* Properties for Jpa
58+
*
59+
* @return
60+
*/
61+
private Properties jpaProperties() {
62+
Properties properties = new Properties();
63+
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
64+
properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
65+
return properties;
66+
}
67+
68+
@Bean
69+
public JpaTransactionManager transactionManager() throws SQLException {
70+
JpaTransactionManager transactionManager = new JpaTransactionManager();
71+
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
72+
return transactionManager;
73+
}
74+
75+
/**
76+
* Program entry point
77+
*
78+
* @param args
79+
* command line args
80+
*/
81+
public static void main(String[] args) {
82+
83+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
84+
AppConfig.class);
85+
PersonRepository repository = context.getBean(PersonRepository.class);
86+
87+
Person peter = new Person("Peter", "Sagan", 17);
88+
Person nasta = new Person("Nasta", "Kuzminova", 25);
89+
Person john = new Person("John", "lawrence", 35);
90+
Person terry = new Person("Terry", "Law", 36);
91+
92+
// Add new Person records
93+
repository.save(peter);
94+
repository.save(nasta);
95+
repository.save(john);
96+
repository.save(terry);
97+
98+
// Count Person records
99+
System.out.println("Count Person records: " + repository.count());
100+
101+
// Print all records
102+
List<Person> persons = (List<Person>) repository.findAll();
103+
for (Person person : persons) {
104+
System.out.println(person);
70105
}
71-
72-
/**
73-
* Program entry point
74-
*
75-
* @param args command line args
76-
*/
77-
public static void main(String[] args) {
78106

79-
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
80-
PersonRepository repository = context.getBean(PersonRepository.class);
107+
// Update Person
108+
nasta.setName("Barbora");
109+
nasta.setSurname("Spotakova");
110+
repository.save(nasta);
81111

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);
112+
System.out.println("Find by id 2: " + repository.findOne(2L));
86113

87-
// Add new Person records
88-
repository.save(peter);
89-
repository.save(nasta);
90-
repository.save(john);
91-
repository.save(terry);
114+
// Remove record from Person
115+
repository.delete(2L);
92116

93-
// Count Person records
94-
System.out.println("Count Person records: " + repository.count());
117+
// count records
118+
System.out.println("Count Person records: " + repository.count());
95119

96-
// Print all records
97-
List<Person> persons = (List<Person>) repository.findAll();
98-
for (Person person : persons) {
99-
System.out.println(person);
100-
}
120+
// find by name
121+
Person p = repository.findOne(new PersonSpecifications.NameEqualSpec("John"));
122+
System.out.println("Find by John is " + p);
101123

102-
// Update Person
103-
nasta.setName("Barbora");
104-
nasta.setSurname("Spotakova");
105-
repository.save(nasta);
124+
// find by age
125+
persons = repository.findAll(new PersonSpecifications.AgeBetweenSpec(20, 40));
106126

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-
}
127+
System.out.println("Find Person with age between 20,40: ");
128+
for (Person person : persons) {
129+
System.out.println(person);
130+
}
126131

127-
context.close();
132+
context.close();
128133

129-
}
134+
}
130135

131136
}

repository/src/main/java/com/iluwatar/repository/Person.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public class Person {
2020

2121
private int age;
2222

23-
public Person() {}
23+
public Person() {
24+
}
2425

2526
public Person(String name, String surname, int age) {
2627
this.name = name;
@@ -52,7 +53,6 @@ public void setSurname(String surname) {
5253
this.surname = surname;
5354
}
5455

55-
5656
public int getAge() {
5757
return age;
5858
}

repository/src/main/java/com/iluwatar/repository/PersonSpecifications.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, Criteria
3131
}
3232

3333
}
34+
3435
public static class NameEqualSpec implements Specification<Person> {
3536

3637
public String name;
@@ -47,4 +48,3 @@ public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, Criteria
4748
}
4849

4950
}
50-

repository/src/test/java/com/iluwatar/repository/AnnotationBasedRepositoryTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
/**
2323
* Test case to test the functions of {@link PersonRepository}, beside the CRUD functions, the query
2424
* by {@link org.springframework.data.jpa.domain.Specification} are also test.
25+
*
2526
*/
2627
@RunWith(SpringJUnit4ClassRunner.class)
27-
@ContextConfiguration(classes = { AppConfig.class}, loader = AnnotationConfigContextLoader.class)
28+
@ContextConfiguration(classes = { AppConfig.class }, loader = AnnotationConfigContextLoader.class)
2829
public class AnnotationBasedRepositoryTest {
2930

3031
@Resource
@@ -107,4 +108,3 @@ public void cleanup() {
107108
}
108109

109110
}
110-

repository/src/test/java/com/iluwatar/repository/AppConfigTest.java

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,36 @@
2020
*
2121
*/
2222
@RunWith(SpringJUnit4ClassRunner.class)
23-
@ContextConfiguration(classes = { AppConfig.class}, loader = AnnotationConfigContextLoader.class)
23+
@ContextConfiguration(classes = { AppConfig.class }, loader = AnnotationConfigContextLoader.class)
2424
public class AppConfigTest {
2525

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-
}
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+
*
40+
* @throws SQLException
41+
*/
42+
@Test
43+
@Transactional
44+
public void testQuery() throws SQLException {
45+
ResultSet resultSet = dataSource.getConnection().createStatement().executeQuery("SELECT 1");
46+
String result = null;
47+
String expected = "1";
48+
while (resultSet.next()) {
49+
result = resultSet.getString(1);
50+
51+
}
52+
assertTrue(result.equals(expected));
53+
}
5354

5455
}

repository/src/test/java/com/iluwatar/repository/RepositoryTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* by {@link org.springframework.data.jpa.domain.Specification} are also test.
2424
*/
2525
@RunWith(SpringJUnit4ClassRunner.class)
26-
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
26+
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
2727
public class RepositoryTest {
2828

2929
@Resource
@@ -106,4 +106,3 @@ public void cleanup() {
106106
}
107107

108108
}
109-

0 commit comments

Comments
 (0)