Java 11 migrate remaining q-r (#1121)
* Moves queue-load-leveling to Java 11 * Moves reactor to Java 11 * Moves reader-writer-lock to Java 11 * Moves repository to Java 11 * Moves resource-acquisition-is-initialization to Java 11 * Moves retry to Java 11 * Moves role-object to Java 11
This commit is contained in:
committed by
Ilkka Seppälä
parent
cd2a2e7711
commit
20ea465b7f
@ -24,7 +24,6 @@
|
||||
package com.iluwatar.repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
@ -55,14 +54,13 @@ public class App {
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"applicationContext.xml");
|
||||
PersonRepository repository = context.getBean(PersonRepository.class);
|
||||
var context = new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||
var 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);
|
||||
var peter = new Person("Peter", "Sagan", 17);
|
||||
var nasta = new Person("Nasta", "Kuzminova", 25);
|
||||
var john = new Person("John", "lawrence", 35);
|
||||
var terry = new Person("Terry", "Law", 36);
|
||||
|
||||
// Add new Person records
|
||||
repository.save(peter);
|
||||
@ -74,17 +72,15 @@ public class App {
|
||||
LOGGER.info("Count Person records: {}", repository.count());
|
||||
|
||||
// Print all records
|
||||
List<Person> persons = (List<Person>) repository.findAll();
|
||||
for (Person person : persons) {
|
||||
LOGGER.info(person.toString());
|
||||
}
|
||||
var persons = (List<Person>) repository.findAll();
|
||||
persons.stream().map(Person::toString).forEach(LOGGER::info);
|
||||
|
||||
// Update Person
|
||||
nasta.setName("Barbora");
|
||||
nasta.setSurname("Spotakova");
|
||||
repository.save(nasta);
|
||||
|
||||
LOGGER.info("Find by id 2: {}", repository.findById(2L).get());
|
||||
repository.findById(2L).ifPresent(p -> LOGGER.info("Find by id 2: {}", p));
|
||||
|
||||
// Remove record from Person
|
||||
repository.deleteById(2L);
|
||||
@ -93,16 +89,15 @@ public class App {
|
||||
LOGGER.info("Count Person records: {}", repository.count());
|
||||
|
||||
// find by name
|
||||
Optional<Person> p = repository.findOne(new PersonSpecifications.NameEqualSpec("John"));
|
||||
LOGGER.info("Find by John is {}", p.get());
|
||||
repository
|
||||
.findOne(new PersonSpecifications.NameEqualSpec("John"))
|
||||
.ifPresent(p -> LOGGER.info("Find by John is {}", p));
|
||||
|
||||
// find by age
|
||||
persons = repository.findAll(new PersonSpecifications.AgeBetweenSpec(20, 40));
|
||||
|
||||
LOGGER.info("Find Person with age between 20,40: ");
|
||||
for (Person person : persons) {
|
||||
LOGGER.info(person.toString());
|
||||
}
|
||||
persons.stream().map(Person::toString).forEach(LOGGER::info);
|
||||
|
||||
repository.deleteAll();
|
||||
|
||||
|
@ -25,7 +25,6 @@ package com.iluwatar.repository;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
import javax.sql.DataSource;
|
||||
import org.apache.commons.dbcp.BasicDataSource;
|
||||
@ -55,7 +54,7 @@ public class AppConfig {
|
||||
*/
|
||||
@Bean(destroyMethod = "close")
|
||||
public DataSource dataSource() {
|
||||
BasicDataSource basicDataSource = new BasicDataSource();
|
||||
var basicDataSource = new BasicDataSource();
|
||||
basicDataSource.setDriverClassName("org.h2.Driver");
|
||||
basicDataSource.setUrl("jdbc:h2:~/databases/person");
|
||||
basicDataSource.setUsername("sa");
|
||||
@ -68,13 +67,11 @@ public class AppConfig {
|
||||
*/
|
||||
@Bean
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||
LocalContainerEntityManagerFactoryBean entityManager =
|
||||
new LocalContainerEntityManagerFactoryBean();
|
||||
var entityManager = new LocalContainerEntityManagerFactoryBean();
|
||||
entityManager.setDataSource(dataSource());
|
||||
entityManager.setPackagesToScan("com.iluwatar");
|
||||
entityManager.setPersistenceProvider(new HibernatePersistenceProvider());
|
||||
entityManager.setJpaProperties(jpaProperties());
|
||||
|
||||
return entityManager;
|
||||
}
|
||||
|
||||
@ -82,7 +79,7 @@ public class AppConfig {
|
||||
* Properties for Jpa.
|
||||
*/
|
||||
private static Properties jpaProperties() {
|
||||
Properties properties = new Properties();
|
||||
var properties = new Properties();
|
||||
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
|
||||
properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
|
||||
return properties;
|
||||
@ -93,7 +90,7 @@ public class AppConfig {
|
||||
*/
|
||||
@Bean
|
||||
public JpaTransactionManager transactionManager() throws SQLException {
|
||||
JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||
var transactionManager = new JpaTransactionManager();
|
||||
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
|
||||
return transactionManager;
|
||||
}
|
||||
@ -104,15 +101,13 @@ public class AppConfig {
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
var context = new AnnotationConfigApplicationContext(AppConfig.class);
|
||||
var repository = context.getBean(PersonRepository.class);
|
||||
|
||||
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);
|
||||
var peter = new Person("Peter", "Sagan", 17);
|
||||
var nasta = new Person("Nasta", "Kuzminova", 25);
|
||||
var john = new Person("John", "lawrence", 35);
|
||||
var terry = new Person("Terry", "Law", 36);
|
||||
|
||||
// Add new Person records
|
||||
repository.save(peter);
|
||||
@ -124,17 +119,15 @@ public class AppConfig {
|
||||
LOGGER.info("Count Person records: {}", repository.count());
|
||||
|
||||
// Print all records
|
||||
List<Person> persons = (List<Person>) repository.findAll();
|
||||
for (Person person : persons) {
|
||||
LOGGER.info(person.toString());
|
||||
}
|
||||
var persons = (List<Person>) repository.findAll();
|
||||
persons.stream().map(Person::toString).forEach(LOGGER::info);
|
||||
|
||||
// Update Person
|
||||
nasta.setName("Barbora");
|
||||
nasta.setSurname("Spotakova");
|
||||
repository.save(nasta);
|
||||
|
||||
LOGGER.info("Find by id 2: {}", repository.findById(2L).get());
|
||||
repository.findById(2L).ifPresent(p -> LOGGER.info("Find by id 2: {}", p));
|
||||
|
||||
// Remove record from Person
|
||||
repository.deleteById(2L);
|
||||
@ -143,16 +136,15 @@ public class AppConfig {
|
||||
LOGGER.info("Count Person records: {}", repository.count());
|
||||
|
||||
// find by name
|
||||
Optional<Person> p = repository.findOne(new PersonSpecifications.NameEqualSpec("John"));
|
||||
LOGGER.info("Find by John is {}", p.get());
|
||||
repository
|
||||
.findOne(new PersonSpecifications.NameEqualSpec("John"))
|
||||
.ifPresent(p -> LOGGER.info("Find by John is {}", p));
|
||||
|
||||
// find by age
|
||||
persons = repository.findAll(new PersonSpecifications.AgeBetweenSpec(20, 40));
|
||||
|
||||
LOGGER.info("Find Person with age between 20,40: ");
|
||||
for (Person person : persons) {
|
||||
LOGGER.info(person.toString());
|
||||
}
|
||||
persons.stream().map(Person::toString).forEach(LOGGER::info);
|
||||
|
||||
context.close();
|
||||
|
||||
|
@ -92,9 +92,8 @@ public class Person {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
final var prime = 31;
|
||||
var result = 1;
|
||||
result = prime * result + age;
|
||||
result = prime * result + (id == null ? 0 : id.hashCode());
|
||||
result = prime * result + (name == null ? 0 : name.hashCode());
|
||||
@ -113,7 +112,7 @@ public class Person {
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Person other = (Person) obj;
|
||||
var other = (Person) obj;
|
||||
if (age != other.age) {
|
||||
return false;
|
||||
}
|
||||
@ -132,13 +131,9 @@ public class Person {
|
||||
return false;
|
||||
}
|
||||
if (surname == null) {
|
||||
if (other.surname != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!surname.equals(other.surname)) {
|
||||
return false;
|
||||
return other.surname == null;
|
||||
}
|
||||
return true;
|
||||
return surname.equals(other.surname);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -50,9 +50,7 @@ public class PersonSpecifications {
|
||||
|
||||
@Override
|
||||
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
|
||||
|
||||
return cb.between(root.get("age"), from, to);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -72,9 +70,7 @@ public class PersonSpecifications {
|
||||
* Get predicate.
|
||||
*/
|
||||
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
|
||||
|
||||
return cb.equal(root.get("name"), this.name);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,13 @@
|
||||
|
||||
package com.iluwatar.repository;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import java.util.List;
|
||||
import javax.annotation.Resource;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -31,51 +37,41 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest(classes = { AppConfig.class })
|
||||
@SpringBootTest(classes = {AppConfig.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);
|
||||
private Person peter = new Person("Peter", "Sagan", 17);
|
||||
private Person nasta = new Person("Nasta", "Kuzminova", 25);
|
||||
private Person john = new Person("John", "lawrence", 35);
|
||||
private Person terry = new Person("Terry", "Law", 36);
|
||||
|
||||
List<Person> persons = List.of(peter, nasta, john, terry);
|
||||
private List<Person> persons = List.of(peter, nasta, john, terry);
|
||||
|
||||
/**
|
||||
* Prepare data for test
|
||||
*/
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
|
||||
repository.saveAll(persons);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAll() {
|
||||
|
||||
List<Person> actuals = Lists.newArrayList(repository.findAll());
|
||||
var actuals = Lists.newArrayList(repository.findAll());
|
||||
assertTrue(actuals.containsAll(persons) && persons.containsAll(actuals));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSave() {
|
||||
|
||||
Person terry = repository.findByName("Terry");
|
||||
var terry = repository.findByName("Terry");
|
||||
terry.setSurname("Lee");
|
||||
terry.setAge(47);
|
||||
repository.save(terry);
|
||||
@ -87,8 +83,7 @@ public class AnnotationBasedRepositoryTest {
|
||||
|
||||
@Test
|
||||
public void testDelete() {
|
||||
|
||||
Person terry = repository.findByName("Terry");
|
||||
var terry = repository.findByName("Terry");
|
||||
repository.delete(terry);
|
||||
|
||||
assertEquals(3, repository.count());
|
||||
@ -97,31 +92,26 @@ public class AnnotationBasedRepositoryTest {
|
||||
|
||||
@Test
|
||||
public void testCount() {
|
||||
|
||||
assertEquals(4, repository.count());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAllByAgeBetweenSpec() {
|
||||
|
||||
List<Person> persons = repository.findAll(new PersonSpecifications.AgeBetweenSpec(20, 40));
|
||||
var persons = repository.findAll(new PersonSpecifications.AgeBetweenSpec(20, 40));
|
||||
|
||||
assertEquals(3, persons.size());
|
||||
assertTrue(persons.stream().allMatch((item) -> {
|
||||
return item.getAge() > 20 && item.getAge() < 40;
|
||||
}));
|
||||
assertTrue(persons.stream().allMatch((item) -> item.getAge() > 20 && item.getAge() < 40));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindOneByNameEqualSpec() {
|
||||
|
||||
Optional<Person> actual = repository.findOne(new PersonSpecifications.NameEqualSpec("Terry"));
|
||||
var actual = repository.findOne(new PersonSpecifications.NameEqualSpec("Terry"));
|
||||
assertTrue(actual.isPresent());
|
||||
assertEquals(terry, actual.get());
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void cleanup() {
|
||||
|
||||
repository.deleteAll();
|
||||
}
|
||||
|
||||
|
@ -23,6 +23,12 @@
|
||||
|
||||
package com.iluwatar.repository;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import javax.sql.DataSource;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -30,19 +36,11 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
/**
|
||||
* This case is Just for test the Annotation Based configuration
|
||||
*
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest(classes = { AppConfig.class })
|
||||
@SpringBootTest(classes = {AppConfig.class})
|
||||
public class AppConfigTest {
|
||||
|
||||
@Autowired
|
||||
@ -62,12 +60,11 @@ public class AppConfigTest {
|
||||
@Test
|
||||
@Transactional
|
||||
public void testQuery() throws SQLException {
|
||||
ResultSet resultSet = dataSource.getConnection().createStatement().executeQuery("SELECT 1");
|
||||
var resultSet = dataSource.getConnection().createStatement().executeQuery("SELECT 1");
|
||||
var expected = "1";
|
||||
String result = null;
|
||||
String expected = "1";
|
||||
while (resultSet.next()) {
|
||||
result = resultSet.getString(1);
|
||||
|
||||
}
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
@ -25,15 +25,12 @@ package com.iluwatar.repository;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Tests that Repository example runs without errors.
|
||||
*/
|
||||
public class AppTest {
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
App.main(new String[]{});
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,13 @@
|
||||
|
||||
package com.iluwatar.repository;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import java.util.List;
|
||||
import javax.annotation.Resource;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -31,50 +37,41 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest(properties = { "locations=classpath:applicationContext.xml" })
|
||||
@SpringBootTest(properties = {"locations=classpath:applicationContext.xml"})
|
||||
public class RepositoryTest {
|
||||
|
||||
@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);
|
||||
private Person peter = new Person("Peter", "Sagan", 17);
|
||||
private Person nasta = new Person("Nasta", "Kuzminova", 25);
|
||||
private Person john = new Person("John", "lawrence", 35);
|
||||
private Person terry = new Person("Terry", "Law", 36);
|
||||
|
||||
List<Person> persons = List.of(peter, nasta, john, terry);
|
||||
private List<Person> persons = List.of(peter, nasta, john, terry);
|
||||
|
||||
/**
|
||||
* Prepare data for test
|
||||
*/
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
|
||||
repository.saveAll(persons);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAll() {
|
||||
|
||||
List<Person> actuals = Lists.newArrayList(repository.findAll());
|
||||
var actuals = Lists.newArrayList(repository.findAll());
|
||||
assertTrue(actuals.containsAll(persons) && persons.containsAll(actuals));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSave() {
|
||||
|
||||
Person terry = repository.findByName("Terry");
|
||||
var terry = repository.findByName("Terry");
|
||||
terry.setSurname("Lee");
|
||||
terry.setAge(47);
|
||||
repository.save(terry);
|
||||
@ -86,8 +83,7 @@ public class RepositoryTest {
|
||||
|
||||
@Test
|
||||
public void testDelete() {
|
||||
|
||||
Person terry = repository.findByName("Terry");
|
||||
var terry = repository.findByName("Terry");
|
||||
repository.delete(terry);
|
||||
|
||||
assertEquals(3, repository.count());
|
||||
@ -96,14 +92,12 @@ public class RepositoryTest {
|
||||
|
||||
@Test
|
||||
public void testCount() {
|
||||
|
||||
assertEquals(4, repository.count());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAllByAgeBetweenSpec() {
|
||||
|
||||
List<Person> persons = repository.findAll(new PersonSpecifications.AgeBetweenSpec(20, 40));
|
||||
var persons = repository.findAll(new PersonSpecifications.AgeBetweenSpec(20, 40));
|
||||
|
||||
assertEquals(3, persons.size());
|
||||
assertTrue(persons.stream().allMatch(item -> item.getAge() > 20 && item.getAge() < 40));
|
||||
@ -111,14 +105,13 @@ public class RepositoryTest {
|
||||
|
||||
@Test
|
||||
public void testFindOneByNameEqualSpec() {
|
||||
|
||||
Optional<Person> actual = repository.findOne(new PersonSpecifications.NameEqualSpec("Terry"));
|
||||
var actual = repository.findOne(new PersonSpecifications.NameEqualSpec("Terry"));
|
||||
assertTrue(actual.isPresent());
|
||||
assertEquals(terry, actual.get());
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void cleanup() {
|
||||
|
||||
repository.deleteAll();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user