From 635cd110b3e2ac968efb5e023b08a7a870b7946d Mon Sep 17 00:00:00 2001 From: Karan Khanna Date: Tue, 8 May 2018 18:48:04 +0200 Subject: [PATCH 01/10] code samples for spring data key value BAEL-1467 --- spring-data-keyvalue/pom.xml | 39 ++++++++ .../spring/data/keyvalue/Configurations.java | 28 ++++++ .../SpringDataKeyValueApplication.java | 15 ++++ .../repositories/EmployeeRepository.java | 10 +++ .../keyvalue/services/EmployeeService.java | 19 ++++ .../EmployeeServicesWithKeyValueTemplate.java | 57 ++++++++++++ .../impl/EmployeeServicesWithRepository.java | 48 ++++++++++ .../spring/data/keyvalue/vo/Employee.java | 68 ++++++++++++++ ...loyeeServicesWithKeyValueTemplateTest.java | 88 +++++++++++++++++++ .../EmployeeServicesWithRepositoryTest.java | 73 +++++++++++++++ 10 files changed, 445 insertions(+) create mode 100644 spring-data-keyvalue/pom.xml create mode 100644 spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/Configurations.java create mode 100644 spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/SpringDataKeyValueApplication.java create mode 100644 spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/repositories/EmployeeRepository.java create mode 100644 spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/EmployeeService.java create mode 100644 spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/impl/EmployeeServicesWithKeyValueTemplate.java create mode 100644 spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/impl/EmployeeServicesWithRepository.java create mode 100644 spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/vo/Employee.java create mode 100644 spring-data-keyvalue/src/test/java/com/baeldung/spring/data/keyvalue/services/test/EmployeeServicesWithKeyValueTemplateTest.java create mode 100644 spring-data-keyvalue/src/test/java/com/baeldung/spring/data/keyvalue/services/test/EmployeeServicesWithRepositoryTest.java diff --git a/spring-data-keyvalue/pom.xml b/spring-data-keyvalue/pom.xml new file mode 100644 index 0000000000..1b77667e0e --- /dev/null +++ b/spring-data-keyvalue/pom.xml @@ -0,0 +1,39 @@ + + 4.0.0 + com.baeldung + spring-data-keyvalue + 1.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.0.1.RELEASE + + + + + 2.0.3.RELEASE + + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.data + spring-data-keyvalue + ${spring-data-keyvalue.version} + + + + org.springframework.boot + spring-boot-starter-test + 1.5.12.RELEASE + + + + \ No newline at end of file diff --git a/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/Configurations.java b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/Configurations.java new file mode 100644 index 0000000000..c762c00333 --- /dev/null +++ b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/Configurations.java @@ -0,0 +1,28 @@ +package com.baeldung.spring.data.keyvalue; + +import java.util.concurrent.ConcurrentHashMap; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.keyvalue.core.KeyValueAdapter; +import org.springframework.data.keyvalue.core.KeyValueOperations; +import org.springframework.data.keyvalue.core.KeyValueTemplate; +import org.springframework.data.map.MapKeyValueAdapter; + +@Configuration +public class Configurations { + + //To be used only if @EnableMapRepositories is not used. + //Else @EnableMapRepositories gives us a template as well. + @Bean("keyValueTemplate") + public KeyValueOperations keyValueTemplate() { + return new KeyValueTemplate(keyValueAdapter()); + + } + + @Bean + public KeyValueAdapter keyValueAdapter() { + return new MapKeyValueAdapter(ConcurrentHashMap.class); + } + +} diff --git a/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/SpringDataKeyValueApplication.java b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/SpringDataKeyValueApplication.java new file mode 100644 index 0000000000..6a5b5264df --- /dev/null +++ b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/SpringDataKeyValueApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.spring.data.keyvalue; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.map.repository.config.EnableMapRepositories; + +@SpringBootApplication +@EnableMapRepositories +public class SpringDataKeyValueApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringDataKeyValueApplication.class, args); + } + +} diff --git a/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/repositories/EmployeeRepository.java b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/repositories/EmployeeRepository.java new file mode 100644 index 0000000000..5b7545d5b6 --- /dev/null +++ b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/repositories/EmployeeRepository.java @@ -0,0 +1,10 @@ +package com.baeldung.spring.data.keyvalue.repositories; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import com.baeldung.spring.data.keyvalue.vo.Employee; + +@Repository("employeeRepository") +public interface EmployeeRepository extends CrudRepository { +} \ No newline at end of file diff --git a/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/EmployeeService.java b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/EmployeeService.java new file mode 100644 index 0000000000..dd89609be7 --- /dev/null +++ b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/EmployeeService.java @@ -0,0 +1,19 @@ +package com.baeldung.spring.data.keyvalue.services; + +import com.baeldung.spring.data.keyvalue.vo.Employee; + +public interface EmployeeService { + + void save(Employee employee); + + Employee get(Integer id); + + Iterable fetchAll(); + + void update(Employee employee); + + void delete(Integer id); + + Iterable getSortedListOfEmployeesBySalary(); + +} diff --git a/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/impl/EmployeeServicesWithKeyValueTemplate.java b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/impl/EmployeeServicesWithKeyValueTemplate.java new file mode 100644 index 0000000000..26f1756add --- /dev/null +++ b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/impl/EmployeeServicesWithKeyValueTemplate.java @@ -0,0 +1,57 @@ +package com.baeldung.spring.data.keyvalue.services.impl; + +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.annotation.DependsOn; +import org.springframework.data.domain.Sort; +import org.springframework.data.keyvalue.core.KeyValueTemplate; +import org.springframework.data.keyvalue.core.query.KeyValueQuery; +import org.springframework.stereotype.Service; + +import com.baeldung.spring.data.keyvalue.services.EmployeeService; +import com.baeldung.spring.data.keyvalue.vo.Employee; + +@Service("employeeServicesWithKeyValueTemplate") +@DependsOn("keyValueTemplate") +public class EmployeeServicesWithKeyValueTemplate implements EmployeeService { + + @Autowired + @Qualifier("keyValueTemplate") + KeyValueTemplate keyValueTemplate; + + @Override + public void save(Employee employee) { + keyValueTemplate.insert(employee); + } + + @Override + public Employee get(Integer id) { + Optional employee = keyValueTemplate.findById(id, Employee.class); + return employee.isPresent() ? employee.get() : null; + } + + @Override + public Iterable fetchAll() { + return keyValueTemplate.findAll(Employee.class); + } + + @Override + public void update(Employee employee) { + keyValueTemplate.update(employee); + } + + @Override + public void delete(Integer id) { + keyValueTemplate.delete(id, Employee.class); + } + + @Override + public Iterable getSortedListOfEmployeesBySalary() { + KeyValueQuery query = new KeyValueQuery(); + query.setSort(new Sort(Sort.Direction.DESC, "salary")); + return keyValueTemplate.find(query, Employee.class); + } + +} diff --git a/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/impl/EmployeeServicesWithRepository.java b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/impl/EmployeeServicesWithRepository.java new file mode 100644 index 0000000000..73f3493a6b --- /dev/null +++ b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/impl/EmployeeServicesWithRepository.java @@ -0,0 +1,48 @@ +package com.baeldung.spring.data.keyvalue.services.impl; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.spring.data.keyvalue.repositories.EmployeeRepository; +import com.baeldung.spring.data.keyvalue.services.EmployeeService; +import com.baeldung.spring.data.keyvalue.vo.Employee; + +@Service("employeeServicesWithRepository") +public class EmployeeServicesWithRepository implements EmployeeService { + + @Autowired + EmployeeRepository employeeRepository; + + + @Override + public void save(Employee employee) { + employeeRepository.save(employee); + } + + @Override + public Iterable fetchAll() { + return employeeRepository.findAll(); + + } + + @Override + public Employee get(Integer id) { + return employeeRepository.findById(id).get(); + } + + @Override + public void update(Employee employee) { + employeeRepository.save(employee); + + } + + @Override + public void delete(Integer id) { + employeeRepository.deleteById(id); + } + + public Iterable getSortedListOfEmployeesBySalary() { + throw new RuntimeException("Method not supported by CRUDRepository"); + } + +} diff --git a/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/vo/Employee.java b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/vo/Employee.java new file mode 100644 index 0000000000..208e6e1735 --- /dev/null +++ b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/vo/Employee.java @@ -0,0 +1,68 @@ +package com.baeldung.spring.data.keyvalue.vo; + +import java.io.Serializable; + +import org.springframework.data.annotation.Id; +import org.springframework.data.keyvalue.annotation.KeySpace; + +@KeySpace("employees") +public class Employee implements Serializable { + + @Id + private Integer id; + + private String name; + + private String department; + + private String salary; + + public Employee(Integer id, String name, String department, String salary) { + this.id = id; + this.name = name; + this.department = department; + this.salary = salary; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDepartment() { + return department; + } + + public void setDepartment(String department) { + this.department = department; + } + + public String getSalary() { + return salary; + } + + public void setSalary(String salary) { + this.salary = salary; + } + + @Override + public String toString() { + return "Employee{" + + "id=" + id + + ", name='" + name + '\'' + + ", department='" + department + '\'' + + ", salary='" + salary + '\'' + + '}'; + } +} diff --git a/spring-data-keyvalue/src/test/java/com/baeldung/spring/data/keyvalue/services/test/EmployeeServicesWithKeyValueTemplateTest.java b/spring-data-keyvalue/src/test/java/com/baeldung/spring/data/keyvalue/services/test/EmployeeServicesWithKeyValueTemplateTest.java new file mode 100644 index 0000000000..78e985fe4b --- /dev/null +++ b/spring-data-keyvalue/src/test/java/com/baeldung/spring/data/keyvalue/services/test/EmployeeServicesWithKeyValueTemplateTest.java @@ -0,0 +1,88 @@ +package com.baeldung.spring.data.keyvalue.services.test; + +import static org.junit.Assert.assertEquals; + +import java.util.List; + +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.keyvalue.core.KeyValueTemplate; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.spring.data.keyvalue.SpringDataKeyValueApplication; +import com.baeldung.spring.data.keyvalue.services.EmployeeService; +import com.baeldung.spring.data.keyvalue.vo.Employee; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest(classes = SpringDataKeyValueApplication.class) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class EmployeeServicesWithKeyValueTemplateTest { + + @Autowired + @Qualifier("employeeServicesWithKeyValueTemplate") + EmployeeService employeeService; + + @Autowired + @Qualifier("keyValueTemplate") + KeyValueTemplate keyValueTemplate; + + static Employee employee1; + + static Employee employee2; + + @BeforeClass + public static void setUp() { + employee1 = new Employee(1, "Karan", "IT", "5000"); + employee2 = new Employee(2, "Jack", "HR", "2000"); + } + + @Test + public void test1_whenEmployeeSaved_thenEmployeeIsAddedToMap() { + employeeService.save(employee1); + assertEquals(keyValueTemplate.findById(1, Employee.class).get(), employee1); + } + + @Test + public void test2_whenEmployeeGet_thenEmployeeIsReturnedFromMap() { + Employee employeeFetched = employeeService.get(1); + assertEquals(employeeFetched, employee1); + } + + @Test + public void test3_whenEmployeesFetched_thenEmployeesAreReturnedFromMap() { + List employees = (List)employeeService.fetchAll(); + assertEquals(employees.size(), 1); + assertEquals(employees.get(0), employee1); + } + + @Test + public void test4_whenEmployeeUpdated_thenEmployeeIsUpdatedToMap() { + employee1.setName("Pawan"); + employeeService.update(employee1); + assertEquals(keyValueTemplate.findById(1, Employee.class).get().getName(),"Pawan"); + } + + @Test + public void test5_whenSortedEmployeesFetched_thenEmployeesAreReturnedFromMapInOrder() { + employeeService.save(employee2); + List employees = (List)employeeService.getSortedListOfEmployeesBySalary(); + assertEquals(employees.size(), 2); + assertEquals(employees.get(0), employee1); + assertEquals(employees.get(1), employee2); + } + + @Test + public void test6_whenEmployeeDeleted_thenEmployeeIsRemovedMap() { + employeeService.delete(1); + assertEquals(keyValueTemplate.findById(1, Employee.class).isPresent(), false); + } + + + +} diff --git a/spring-data-keyvalue/src/test/java/com/baeldung/spring/data/keyvalue/services/test/EmployeeServicesWithRepositoryTest.java b/spring-data-keyvalue/src/test/java/com/baeldung/spring/data/keyvalue/services/test/EmployeeServicesWithRepositoryTest.java new file mode 100644 index 0000000000..5b32728893 --- /dev/null +++ b/spring-data-keyvalue/src/test/java/com/baeldung/spring/data/keyvalue/services/test/EmployeeServicesWithRepositoryTest.java @@ -0,0 +1,73 @@ +package com.baeldung.spring.data.keyvalue.services.test; + +import static org.junit.Assert.assertEquals; + +import java.util.List; + +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.spring.data.keyvalue.SpringDataKeyValueApplication; +import com.baeldung.spring.data.keyvalue.repositories.EmployeeRepository; +import com.baeldung.spring.data.keyvalue.services.EmployeeService; +import com.baeldung.spring.data.keyvalue.vo.Employee; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest(classes = SpringDataKeyValueApplication.class) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class EmployeeServicesWithRepositoryTest { + + @Autowired + @Qualifier("employeeServicesWithRepository") + EmployeeService employeeService; + + @Autowired + EmployeeRepository employeeRepository; + + static Employee employee1; + + @BeforeClass + public static void setUp() { + employee1 = new Employee(1, "Karan", "IT", "5000"); + } + + @Test + public void test1_whenEmployeeSaved_thenEmployeeIsAddedToMap() { + employeeService.save(employee1); + assertEquals(employeeRepository.findById(1).get(), employee1); + } + + @Test + public void test2_whenEmployeeGet_thenEmployeeIsReturnedFromMap() { + Employee employeeFetched = employeeService.get(1); + assertEquals(employeeFetched, employee1); + } + + @Test + public void test3_whenEmployeesFetched_thenEmployeesAreReturnedFromMap() { + List employees = (List)employeeService.fetchAll(); + assertEquals(employees.size(), 1); + assertEquals(employees.get(0), employee1); + } + + @Test + public void test4_whenEmployeeUpdated_thenEmployeeIsUpdatedToMap() { + employee1.setName("Pawan"); + employeeService.update(employee1); + assertEquals(employeeRepository.findById(1).get().getName(),"Pawan"); + } + + @Test + public void test5_whenEmployeeDeleted_thenEmployeeIsRemovedMap() { + employeeService.delete(1); + assertEquals(employeeRepository.findById(1).isPresent(), false); + } + +} From c0c319b13f5fef8c360b6090293e1c2e96e5bfb6 Mon Sep 17 00:00:00 2001 From: Patryk Kucharz Date: Mon, 30 Apr 2018 16:33:50 +0200 Subject: [PATCH 02/10] BAEL-1713: Pessimistic Locking in JPA Tests for the article. --- .../com/baeldung/hibernate/HibernateUtil.java | 38 ++--- .../hibernate/pessimisticlocking/Address.java | 34 ++++ .../pessimisticlocking/Customer.java | 58 +++++++ .../pessimisticlocking/Individual.java | 49 ++++++ .../PessimisticLockingCourse.java | 47 ++++++ .../PessimisticLockingEmployee.java | 27 ++++ .../PessimisticLockingStudent.java | 46 ++++++ ...asicPessimisticLockingIntegrationTest.java | 151 ++++++++++++++++++ .../PessimisticLockScopesIntegrationTest.java | 100 ++++++++++++ .../hibernate-pessimistic-locking.properties | 8 + .../pessimisticlocking/Address.java | 34 ++++ .../springdata/pessimisticlocking/Course.java | 50 ++++++ .../pessimisticlocking/Customer.java | 61 +++++++ .../pessimisticlocking/Employee.java | 27 ++++ .../pessimisticlocking/Individual.java | 49 ++++++ .../pessimisticlocking/Student.java | 48 ++++++ .../src/main/resources/application.properties | 2 +- .../PessimisticLockScopesIntegrationTest.java | 103 ++++++++++++ 18 files changed, 906 insertions(+), 26 deletions(-) create mode 100644 hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/Address.java create mode 100644 hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/Customer.java create mode 100644 hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/Individual.java create mode 100644 hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/PessimisticLockingCourse.java create mode 100644 hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/PessimisticLockingEmployee.java create mode 100644 hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/PessimisticLockingStudent.java create mode 100644 hibernate5/src/test/java/com/baeldung/hibernate/pessimisticlocking/BasicPessimisticLockingIntegrationTest.java create mode 100644 hibernate5/src/test/java/com/baeldung/hibernate/pessimisticlocking/PessimisticLockScopesIntegrationTest.java create mode 100644 hibernate5/src/test/resources/hibernate-pessimistic-locking.properties create mode 100644 persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Address.java create mode 100644 persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Course.java create mode 100644 persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Customer.java create mode 100644 persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Employee.java create mode 100644 persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Individual.java create mode 100644 persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Student.java create mode 100644 persistence-modules/spring-data-eclipselink/src/test/java/com/baeldung/eclipselink/springdata/pessimisticlocking/PessimisticLockScopesIntegrationTest.java diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java b/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java index 130edec8cc..e8fdabebbc 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java @@ -1,30 +1,12 @@ package com.baeldung.hibernate; -import com.baeldung.hibernate.pojo.Employee; -import com.baeldung.hibernate.pojo.EntityDescription; -import com.baeldung.hibernate.pojo.OrderEntry; -import com.baeldung.hibernate.pojo.OrderEntryIdClass; -import com.baeldung.hibernate.pojo.OrderEntryPK; -import com.baeldung.hibernate.pojo.PointEntity; -import com.baeldung.hibernate.pojo.PolygonEntity; -import com.baeldung.hibernate.pojo.Product; -import com.baeldung.hibernate.pojo.Phone; -import com.baeldung.hibernate.pojo.TemporalValues; -import com.baeldung.hibernate.pojo.Course; -import com.baeldung.hibernate.pojo.Student; -import com.baeldung.hibernate.pojo.User; -import com.baeldung.hibernate.pojo.UserProfile; -import com.baeldung.hibernate.pojo.inheritance.Animal; -import com.baeldung.hibernate.pojo.inheritance.Bag; -import com.baeldung.hibernate.pojo.inheritance.Book; -import com.baeldung.hibernate.pojo.inheritance.Car; -import com.baeldung.hibernate.pojo.inheritance.MyEmployee; -import com.baeldung.hibernate.pojo.inheritance.MyProduct; -import com.baeldung.hibernate.pojo.inheritance.Pen; -import com.baeldung.hibernate.pojo.inheritance.Person; -import com.baeldung.hibernate.pojo.inheritance.Pet; -import com.baeldung.hibernate.pojo.inheritance.Vehicle; - +import com.baeldung.hibernate.pessimisticlocking.Individual; +import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingCourse; +import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingEmployee; +import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingStudent; +import com.baeldung.hibernate.pojo.*; +import com.baeldung.hibernate.pojo.Person; +import com.baeldung.hibernate.pojo.inheritance.*; import org.apache.commons.lang3.StringUtils; import org.hibernate.SessionFactory; import org.hibernate.boot.Metadata; @@ -82,6 +64,12 @@ public class HibernateUtil { metadataSources.addAnnotatedClass(PointEntity.class); metadataSources.addAnnotatedClass(PolygonEntity.class); metadataSources.addAnnotatedClass(com.baeldung.hibernate.pojo.Person.class); + metadataSources.addAnnotatedClass(Individual.class); + metadataSources.addAnnotatedClass(PessimisticLockingEmployee.class); + metadataSources.addAnnotatedClass(PessimisticLockingStudent.class); + metadataSources.addAnnotatedClass(PessimisticLockingCourse.class); + metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Customer.class); + metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Address.class); Metadata metadata = metadataSources.buildMetadata(); return metadata.getSessionFactoryBuilder() diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/Address.java b/hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/Address.java new file mode 100644 index 0000000000..c889cb6127 --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/Address.java @@ -0,0 +1,34 @@ +package com.baeldung.hibernate.pessimisticlocking; + +import javax.persistence.Embeddable; + +@Embeddable +public class Address { + + private String country; + private String city; + + public Address(String country, String city) { + this.country = country; + this.city = city; + } + + public Address() { + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } +} diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/Customer.java b/hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/Customer.java new file mode 100644 index 0000000000..cb73cbc958 --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/Customer.java @@ -0,0 +1,58 @@ +package com.baeldung.hibernate.pessimisticlocking; + +import javax.persistence.*; +import java.util.List; + +@Entity +public class Customer { + + @Id + private Long customerId; + private String name; + private String lastName; + @ElementCollection + @CollectionTable(name = "customer_address") + private List
addressList; + + public Customer() { + } + + public Customer(Long customerId, String name, String lastName, List
addressList) { + this.customerId = customerId; + this.name = name; + this.lastName = lastName; + this.addressList = addressList; + } + + public Long getCustomerId() { + return customerId; + } + + public void setCustomerId(Long customerId) { + this.customerId = customerId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public List
getAddressList() { + return addressList; + } + + public void setAddressList(List
addressList) { + this.addressList = addressList; + } +} diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/Individual.java b/hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/Individual.java new file mode 100644 index 0000000000..e491c09eb5 --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/Individual.java @@ -0,0 +1,49 @@ +package com.baeldung.hibernate.pessimisticlocking; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; + +@Entity +@Inheritance(strategy = InheritanceType.JOINED) +public class Individual { + + @Id + private Long id; + private String name; + private String lastName; + + public Individual(Long id, String name, String lastName) { + this.id = id; + this.name = name; + this.lastName = lastName; + } + + public Individual() { + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } +} diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/PessimisticLockingCourse.java b/hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/PessimisticLockingCourse.java new file mode 100644 index 0000000000..aea7d5fc87 --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/PessimisticLockingCourse.java @@ -0,0 +1,47 @@ +package com.baeldung.hibernate.pessimisticlocking; + +import javax.persistence.*; + +@Entity +public class PessimisticLockingCourse { + + @Id + private Long courseId; + private String name; + @ManyToOne + @JoinTable(name = "student_course") + private PessimisticLockingStudent student; + + public PessimisticLockingCourse(Long courseId, String name, PessimisticLockingStudent student) { + this.courseId = courseId; + this.name = name; + this.student = student; + } + + public PessimisticLockingCourse() { + } + + public Long getCourseId() { + return courseId; + } + + public void setCourseId(Long courseId) { + this.courseId = courseId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public PessimisticLockingStudent getStudent() { + return student; + } + + public void setStudent(PessimisticLockingStudent students) { + this.student = students; + } +} diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/PessimisticLockingEmployee.java b/hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/PessimisticLockingEmployee.java new file mode 100644 index 0000000000..a1328cbdad --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/PessimisticLockingEmployee.java @@ -0,0 +1,27 @@ +package com.baeldung.hibernate.pessimisticlocking; + +import javax.persistence.Entity; +import java.math.BigDecimal; + +@Entity +public class PessimisticLockingEmployee extends Individual { + + private BigDecimal salary; + + public PessimisticLockingEmployee(Long id, String name, String lastName, BigDecimal salary) { + super(id, name, lastName); + this.salary = salary; + } + + public PessimisticLockingEmployee() { + super(); + } + + public BigDecimal getSalary() { + return salary; + } + + public void setSalary(BigDecimal average) { + this.salary = average; + } +} diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/PessimisticLockingStudent.java b/hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/PessimisticLockingStudent.java new file mode 100644 index 0000000000..e6c5f476b4 --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/pessimisticlocking/PessimisticLockingStudent.java @@ -0,0 +1,46 @@ +package com.baeldung.hibernate.pessimisticlocking; + +import javax.persistence.*; +import java.util.List; + +@Entity +public class PessimisticLockingStudent { + + @Id + private Long id; + private String name; + @OneToMany(mappedBy = "student") + private List courses; + + public PessimisticLockingStudent(Long id, String name) { + this.id = id; + this.name = name; + } + + public PessimisticLockingStudent() { + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getCourses() { + return courses; + } + + public void setCourses(List courses) { + this.courses = courses; + } +} diff --git a/hibernate5/src/test/java/com/baeldung/hibernate/pessimisticlocking/BasicPessimisticLockingIntegrationTest.java b/hibernate5/src/test/java/com/baeldung/hibernate/pessimisticlocking/BasicPessimisticLockingIntegrationTest.java new file mode 100644 index 0000000000..f416c11d1f --- /dev/null +++ b/hibernate5/src/test/java/com/baeldung/hibernate/pessimisticlocking/BasicPessimisticLockingIntegrationTest.java @@ -0,0 +1,151 @@ +package com.baeldung.hibernate.pessimisticlocking; + +import com.baeldung.hibernate.HibernateUtil; +import com.vividsolutions.jts.util.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import javax.persistence.*; +import java.io.IOException; +import java.util.Arrays; + +public class BasicPessimisticLockingIntegrationTest { + + @BeforeClass + public static void setUp() throws IOException { + EntityManager entityManager = getEntityManagerWithOpenTransaction(); + PessimisticLockingStudent student = new PessimisticLockingStudent(1L, "JOHN"); + PessimisticLockingCourse course = new PessimisticLockingCourse(1L, "MATH", student); + student.setCourses(Arrays.asList(course)); + entityManager.persist(course); + entityManager.persist(student); + entityManager.getTransaction() + .commit(); + entityManager.close(); + } + + @Test + public void givenFoundRecordWithPessimisticRead_whenFindingNewOne_PessimisticLockExceptionThrown() { + try { + EntityManager entityManager = getEntityManagerWithOpenTransaction(); + entityManager.find(PessimisticLockingStudent.class, 1L, LockModeType.PESSIMISTIC_READ); + + EntityManager entityManager2 = getEntityManagerWithOpenTransaction(); + entityManager2.find(PessimisticLockingStudent.class, 1L, LockModeType.PESSIMISTIC_READ); + + entityManager.close(); + entityManager2.close(); + } catch (Exception e) { + Assert.isTrue(e instanceof PessimisticLockException); + } + } + + @Test + public void givenRecordWithPessimisticReadQuery_whenQueryingNewOne_PessimisticLockExceptionThrown() throws IOException { + try { + EntityManager entityManager = getEntityManagerWithOpenTransaction(); + Query query = entityManager.createQuery("from Student where studentId = :studentId"); + query.setParameter("studentId", 1L); + query.setLockMode(LockModeType.PESSIMISTIC_WRITE); + query.getResultList(); + + EntityManager entityManager2 = getEntityManagerWithOpenTransaction(); + Query query2 = entityManager2.createQuery("from Student where studentId = :studentId"); + query2.setParameter("studentId", 1L); + query2.setLockMode(LockModeType.PESSIMISTIC_READ); + query2.getResultList(); + + entityManager.close(); + entityManager2.close(); + } catch (Exception e) { + Assert.isTrue(e instanceof PessimisticLockException); + } + } + + @Test + public void givenRecordWithPessimisticReadLock_whenFindingNewOne_PessimisticLockExceptionThrown() { + try { + EntityManager entityManager = getEntityManagerWithOpenTransaction(); + PessimisticLockingStudent resultStudent = entityManager.find(PessimisticLockingStudent.class, 1L); + entityManager.lock(resultStudent, LockModeType.PESSIMISTIC_READ); + + EntityManager entityManager2 = getEntityManagerWithOpenTransaction(); + entityManager2.find(PessimisticLockingStudent.class, 1L, LockModeType.PESSIMISTIC_FORCE_INCREMENT); + + entityManager.close(); + entityManager2.close(); + } catch (Exception e) { + Assert.isTrue(e instanceof PessimisticLockException); + } + } + + @Test + public void givenRecordAndRefreshWithPessimisticRead_whenFindingWithPessimisticWrite_PessimisticLockExceptionThrown() { + try { + EntityManager entityManager = getEntityManagerWithOpenTransaction(); + PessimisticLockingStudent resultStudent = entityManager.find(PessimisticLockingStudent.class, 1L); + entityManager.refresh(resultStudent, LockModeType.PESSIMISTIC_FORCE_INCREMENT); + + EntityManager entityManager2 = getEntityManagerWithOpenTransaction(); + entityManager2.find(PessimisticLockingStudent.class, 1L, LockModeType.PESSIMISTIC_WRITE); + + entityManager.close(); + entityManager2.close(); + } catch (Exception e) { + Assert.isTrue(e instanceof PessimisticLockException); + } + } + + @Test + public void givenRecordWithPessimisticRead_whenUpdatingRecord_PessimisticLockExceptionThrown() { + try { + EntityManager entityManager = getEntityManagerWithOpenTransaction(); + PessimisticLockingStudent resultStudent = entityManager.find(PessimisticLockingStudent.class, 1L); + entityManager.refresh(resultStudent, LockModeType.PESSIMISTIC_READ); + + EntityManager entityManager2 = getEntityManagerWithOpenTransaction(); + PessimisticLockingStudent resultStudent2 = entityManager2.find(PessimisticLockingStudent.class, 1L); + resultStudent2.setName("Change"); + entityManager2.persist(resultStudent2); + entityManager2.getTransaction() + .commit(); + + entityManager.close(); + entityManager2.close(); + } catch (Exception e) { + Assert.isTrue(e instanceof PessimisticLockException); + } + } + + @Test + public void givenRecordWithPessimisticWrite_whenUpdatingRecord_PessimisticLockExceptionThrown() { + try { + EntityManager entityManager = getEntityManagerWithOpenTransaction(); + PessimisticLockingStudent resultStudent = entityManager.find(PessimisticLockingStudent.class, 1L); + entityManager.refresh(resultStudent, LockModeType.PESSIMISTIC_WRITE); + + EntityManager entityManager2 = getEntityManagerWithOpenTransaction(); + PessimisticLockingStudent resultStudent2 = entityManager2.find(PessimisticLockingStudent.class, 1L); + resultStudent2.setName("Change"); + entityManager2.persist(resultStudent2); + entityManager2.getTransaction() + .commit(); + + entityManager.close(); + entityManager2.close(); + } catch (Exception e) { + Assert.isTrue(e instanceof PessimisticLockException); + } + } + + protected static EntityManager getEntityManagerWithOpenTransaction() throws IOException { + String propertyFileName = "hibernate-pessimistic-locking.properties"; + EntityManager entityManager = HibernateUtil.getSessionFactory(propertyFileName) + .openSession(); + entityManager.getTransaction() + .begin(); + + return entityManager; + } + +} diff --git a/hibernate5/src/test/java/com/baeldung/hibernate/pessimisticlocking/PessimisticLockScopesIntegrationTest.java b/hibernate5/src/test/java/com/baeldung/hibernate/pessimisticlocking/PessimisticLockScopesIntegrationTest.java new file mode 100644 index 0000000000..c774c6eaa4 --- /dev/null +++ b/hibernate5/src/test/java/com/baeldung/hibernate/pessimisticlocking/PessimisticLockScopesIntegrationTest.java @@ -0,0 +1,100 @@ +package com.baeldung.hibernate.pessimisticlocking; + +import com.baeldung.hibernate.HibernateUtil; +import org.junit.Test; + +import javax.persistence.EntityManager; +import javax.persistence.LockModeType; +import javax.persistence.PessimisticLockScope; +import java.io.IOException; +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class PessimisticLockScopesIntegrationTest { + + @Test + public void givenEclipseEntityWithJoinInheritance_whenNormalLock_thenShouldChildAndParentEntity() throws IOException { + EntityManager em = getEntityManagerWithOpenTransaction(); + PessimisticLockingEmployee employee = new PessimisticLockingEmployee(1L, "A", "B", new BigDecimal(4.5)); + em.persist(employee); + em.getTransaction() + .commit(); + em.close(); + + // NORMAL SCOPE + em = getEntityManagerWithOpenTransaction(); + PessimisticLockingEmployee foundEmployee = em.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE); + em.getTransaction() + .rollback(); + + // EXTENDED SCOPE + Map map = new HashMap<>(); + map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED); + + em = getEntityManagerWithOpenTransaction(); + foundEmployee = em.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map); + } + + @Test + public void givenEclipseEntityWithElementCollection_whenNormalLock_thenShouldLockOnlyOwningEntity() throws IOException { + EntityManager em = getEntityManagerWithOpenTransaction(); + Address address = new Address("Poland", "Warsaw"); + Customer customer = new Customer(1L, "A", "B", Arrays.asList(address)); + em.persist(customer); + em.getTransaction() + .commit(); + em.close(); + + // NORMAL SCOPE + em = getEntityManagerWithOpenTransaction(); + Customer foundCustomer = em.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE); + em.getTransaction() + .rollback(); + + // EXTENDED SCOPE + Map map = new HashMap<>(); + map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED); + + em = getEntityManagerWithOpenTransaction(); + foundCustomer = em.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map); + } + + @Test + public void givenEclipseEntityWithOneToMany_whenNormalLock_thenShouldLockOnlyOwningEntity() throws IOException { + EntityManager em = getEntityManagerWithOpenTransaction(); + PessimisticLockingStudent student = new PessimisticLockingStudent(1L, "JOE"); + PessimisticLockingCourse course = new PessimisticLockingCourse(1L, "COURSE", student); + student.setCourses(Arrays.asList(course)); + em.persist(course); + em.persist(student); + em.getTransaction() + .commit(); + em.close(); + + // NORMAL SCOPE + em = getEntityManagerWithOpenTransaction(); + PessimisticLockingCourse foundCourse = em.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE); + em.getTransaction() + .rollback(); + + // EXTENDED SCOPE + Map map = new HashMap<>(); + map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED); + + em = getEntityManagerWithOpenTransaction(); + foundCourse = em.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE, map); + } + + protected EntityManager getEntityManagerWithOpenTransaction() throws IOException { + String propertyFileName = "hibernate-pessimistic-locking.properties"; + EntityManager entityManager = HibernateUtil.getSessionFactory(propertyFileName) + .openSession(); + entityManager.getTransaction() + .begin(); + + return entityManager; + } + +} diff --git a/hibernate5/src/test/resources/hibernate-pessimistic-locking.properties b/hibernate5/src/test/resources/hibernate-pessimistic-locking.properties new file mode 100644 index 0000000000..c76bd3358b --- /dev/null +++ b/hibernate5/src/test/resources/hibernate-pessimistic-locking.properties @@ -0,0 +1,8 @@ +hibernate.connection.driver_class=org.h2.Driver +hibernate.connection.url=jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1;LOCK_TIMEOUT=100;MVCC=FALSE +hibernate.connection.username=sa +hibernate.connection.autocommit=true +hibernate.dialect=org.hibernate.dialect.H2Dialect + +hibernate.show_sql=true +hibernate.hbm2ddl.auto=create-drop \ No newline at end of file diff --git a/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Address.java b/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Address.java new file mode 100644 index 0000000000..b62889208c --- /dev/null +++ b/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Address.java @@ -0,0 +1,34 @@ +package com.baeldung.eclipselink.springdata.pessimisticlocking; + +import javax.persistence.Embeddable; + +@Embeddable +public class Address { + + private String country; + private String city; + + public Address(String country, String city) { + this.country = country; + this.city = city; + } + + public Address() { + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } +} diff --git a/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Course.java b/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Course.java new file mode 100644 index 0000000000..8d90659f3e --- /dev/null +++ b/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Course.java @@ -0,0 +1,50 @@ +package com.baeldung.eclipselink.springdata.pessimisticlocking; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinTable; +import javax.persistence.ManyToOne; + +@Entity +public class Course { + + @Id + private Long courseId; + private String name; + @ManyToOne + @JoinTable(name = "student_course") + private Student student; + + public Course(Long courseId, String name, Student student) { + this.courseId = courseId; + this.name = name; + this.student = student; + } + + public Course() { + } + + public Long getCourseId() { + return courseId; + } + + public void setCourseId(Long courseId) { + this.courseId = courseId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Student getStudent() { + return student; + } + + public void setStudent(Student students) { + this.student = students; + } +} diff --git a/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Customer.java b/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Customer.java new file mode 100644 index 0000000000..f06a676de8 --- /dev/null +++ b/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Customer.java @@ -0,0 +1,61 @@ +package com.baeldung.eclipselink.springdata.pessimisticlocking; + +import javax.persistence.CollectionTable; +import javax.persistence.ElementCollection; +import javax.persistence.Entity; +import javax.persistence.Id; +import java.util.List; + +@Entity +public class Customer { + + @Id + private Long customerId; + private String name; + private String lastName; + @ElementCollection + @CollectionTable(name = "customer_address") + private List
addressList; + + public Customer() { + } + + public Customer(Long customerId, String name, String lastName, List
addressList) { + this.customerId = customerId; + this.name = name; + this.lastName = lastName; + this.addressList = addressList; + } + + public Long getCustomerId() { + return customerId; + } + + public void setCustomerId(Long customerId) { + this.customerId = customerId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public List
getAddressList() { + return addressList; + } + + public void setAddressList(List
addressList) { + this.addressList = addressList; + } +} diff --git a/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Employee.java b/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Employee.java new file mode 100644 index 0000000000..d09b123225 --- /dev/null +++ b/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Employee.java @@ -0,0 +1,27 @@ +package com.baeldung.eclipselink.springdata.pessimisticlocking; + +import javax.persistence.Entity; +import java.math.BigDecimal; + +@Entity +public class Employee extends Individual { + + private BigDecimal salary; + + public Employee(Long id, String name, String lastName, BigDecimal salary) { + super(id, name, lastName); + this.salary = salary; + } + + public Employee() { + super(); + } + + public BigDecimal getSalary() { + return salary; + } + + public void setSalary(BigDecimal average) { + this.salary = average; + } +} diff --git a/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Individual.java b/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Individual.java new file mode 100644 index 0000000000..7edaaace54 --- /dev/null +++ b/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Individual.java @@ -0,0 +1,49 @@ +package com.baeldung.eclipselink.springdata.pessimisticlocking; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; + +@Entity +@Inheritance(strategy = InheritanceType.JOINED) +public class Individual { + + @Id + private Long id; + private String name; + private String lastName; + + public Individual(Long id, String name, String lastName) { + this.id = id; + this.name = name; + this.lastName = lastName; + } + + public Individual() { + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } +} diff --git a/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Student.java b/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Student.java new file mode 100644 index 0000000000..f613aab0f6 --- /dev/null +++ b/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/pessimisticlocking/Student.java @@ -0,0 +1,48 @@ +package com.baeldung.eclipselink.springdata.pessimisticlocking; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import java.util.List; + +@Entity +public class Student { + + @Id + private Long id; + private String name; + @OneToMany(mappedBy = "student") + private List courses; + + public Student(Long id, String name) { + this.id = id; + this.name = name; + } + + public Student() { + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getCourses() { + return courses; + } + + public void setCourses(List courses) { + this.courses = courses; + } +} diff --git a/persistence-modules/spring-data-eclipselink/src/main/resources/application.properties b/persistence-modules/spring-data-eclipselink/src/main/resources/application.properties index 549c0b4ada..5874482e7a 100644 --- a/persistence-modules/spring-data-eclipselink/src/main/resources/application.properties +++ b/persistence-modules/spring-data-eclipselink/src/main/resources/application.properties @@ -1,2 +1,2 @@ -spring.datasource.url=jdbc:h2:mem:test +spring.datasource.url=jdbc:h2:mem:test;MVCC=FALSE;LOCK_TIMEOUT=100; spring.jpa.show-sql=true \ No newline at end of file diff --git a/persistence-modules/spring-data-eclipselink/src/test/java/com/baeldung/eclipselink/springdata/pessimisticlocking/PessimisticLockScopesIntegrationTest.java b/persistence-modules/spring-data-eclipselink/src/test/java/com/baeldung/eclipselink/springdata/pessimisticlocking/PessimisticLockScopesIntegrationTest.java new file mode 100644 index 0000000000..e84e991d5d --- /dev/null +++ b/persistence-modules/spring-data-eclipselink/src/test/java/com/baeldung/eclipselink/springdata/pessimisticlocking/PessimisticLockScopesIntegrationTest.java @@ -0,0 +1,103 @@ +package com.baeldung.eclipselink.springdata.pessimisticlocking; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.persistence.*; +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class PessimisticLockScopesIntegrationTest { + + @Autowired + EntityManagerFactory entityManagerFactory; + + @Test + public void givenEclipseEntityWithJoinInheritance_whenNormalLock_thenShouldChildAndParentEntity() { + EntityManager em = getEntityManagerWithOpenTransaction(); + Employee employee = new Employee(1L, "A", "B", new BigDecimal(4.5)); + em.persist(employee); + em.getTransaction() + .commit(); + em.close(); + + // NORMAL SCOPE + em = getEntityManagerWithOpenTransaction(); + Employee foundEmployee = em.find(Employee.class, 1L, LockModeType.PESSIMISTIC_WRITE); + em.getTransaction() + .rollback(); + + // EXTENDED SCOPE + Map map = new HashMap<>(); + map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED); + + em = getEntityManagerWithOpenTransaction(); + foundEmployee = em.find(Employee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map); + } + + @Test + public void givenEclipseEntityWithElementCollection_whenNormalLock_thenShouldLockOnlyOwningEntity() { + EntityManager em = getEntityManagerWithOpenTransaction(); + Address address = new Address("Poland", "Warsaw"); + Customer customer = new Customer(1L, "A", "B", Arrays.asList(address)); + em.persist(customer); + em.getTransaction() + .commit(); + em.close(); + + // NORMAL SCOPE + em = getEntityManagerWithOpenTransaction(); + Customer foundCustomer = em.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE); + em.getTransaction() + .rollback(); + + // EXTENDED SCOPE + Map map = new HashMap<>(); + map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED); + + em = getEntityManagerWithOpenTransaction(); + foundCustomer = em.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map); + } + + @Test + public void givenEclipseEntityWithOneToMany_whenNormalLock_thenShouldLockOnlyOwningEntity() { + EntityManager em = getEntityManagerWithOpenTransaction(); + Student student = new Student(1L, "JOE"); + Course course = new Course(1L, "COURSE", student); + student.setCourses(Arrays.asList(course)); + em.persist(course); + em.persist(student); + em.getTransaction() + .commit(); + em.close(); + + // NORMAL SCOPE + em = getEntityManagerWithOpenTransaction(); + Course foundCourse = em.find(Course.class, 1L, LockModeType.PESSIMISTIC_WRITE); + em.getTransaction() + .rollback(); + + // EXTENDED SCOPE + Map map = new HashMap<>(); + map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED); + + em = getEntityManagerWithOpenTransaction(); + foundCourse = em.find(Course.class, 1L, LockModeType.PESSIMISTIC_WRITE, map); + } + + protected EntityManager getEntityManagerWithOpenTransaction() { + EntityManager entityManager = entityManagerFactory.createEntityManager(); + entityManager.getTransaction() + .begin(); + return entityManager; + } +} From fd11e089fe49e5d52c18ef3c5575943fa5f9f9e4 Mon Sep 17 00:00:00 2001 From: Patryk Kucharz Date: Thu, 10 May 2018 20:53:58 +0200 Subject: [PATCH 03/10] BAEL-1713: Pessimistic Locking in JPA Improvments for tests. --- .../PessimisticLockScopesIntegrationTest.java | 53 +++++++++++------- .../PessimisticLockScopesIntegrationTest.java | 55 ++++++++++++------- 2 files changed, 69 insertions(+), 39 deletions(-) diff --git a/hibernate5/src/test/java/com/baeldung/hibernate/pessimisticlocking/PessimisticLockScopesIntegrationTest.java b/hibernate5/src/test/java/com/baeldung/hibernate/pessimisticlocking/PessimisticLockScopesIntegrationTest.java index c774c6eaa4..ac56ab7133 100644 --- a/hibernate5/src/test/java/com/baeldung/hibernate/pessimisticlocking/PessimisticLockScopesIntegrationTest.java +++ b/hibernate5/src/test/java/com/baeldung/hibernate/pessimisticlocking/PessimisticLockScopesIntegrationTest.java @@ -17,52 +17,62 @@ public class PessimisticLockScopesIntegrationTest { @Test public void givenEclipseEntityWithJoinInheritance_whenNormalLock_thenShouldChildAndParentEntity() throws IOException { EntityManager em = getEntityManagerWithOpenTransaction(); - PessimisticLockingEmployee employee = new PessimisticLockingEmployee(1L, "A", "B", new BigDecimal(4.5)); + PessimisticLockingEmployee employee = new PessimisticLockingEmployee(1L, "JOHN", "SMITH", new BigDecimal(4.5)); em.persist(employee); em.getTransaction() .commit(); em.close(); // NORMAL SCOPE - em = getEntityManagerWithOpenTransaction(); - PessimisticLockingEmployee foundEmployee = em.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE); - em.getTransaction() + EntityManager em2 = getEntityManagerWithOpenTransaction(); + PessimisticLockingEmployee foundEmployee = em2.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE); + em2.getTransaction() .rollback(); // EXTENDED SCOPE Map map = new HashMap<>(); map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED); - em = getEntityManagerWithOpenTransaction(); - foundEmployee = em.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map); + EntityManager em3 = getEntityManagerWithOpenTransaction(); + foundEmployee = em3.find(PessimisticLockingEmployee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map); + em3.getTransaction() + .rollback(); + + em2.close(); + em3.close(); } @Test - public void givenEclipseEntityWithElementCollection_whenNormalLock_thenShouldLockOnlyOwningEntity() throws IOException { + public void givenEntityWithElementCollection_whenLock_thenHibernateExtendedScopeLockOnlyOwningEntity() throws IOException { EntityManager em = getEntityManagerWithOpenTransaction(); Address address = new Address("Poland", "Warsaw"); - Customer customer = new Customer(1L, "A", "B", Arrays.asList(address)); + Customer customer = new Customer(1L, "JOE", "DOE", Arrays.asList(address)); em.persist(customer); em.getTransaction() .commit(); em.close(); // NORMAL SCOPE - em = getEntityManagerWithOpenTransaction(); - Customer foundCustomer = em.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE); - em.getTransaction() + EntityManager em2 = getEntityManagerWithOpenTransaction(); + Customer foundCustomer = em2.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE); + em2.getTransaction() .rollback(); // EXTENDED SCOPE Map map = new HashMap<>(); map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED); - em = getEntityManagerWithOpenTransaction(); - foundCustomer = em.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map); + EntityManager em3 = getEntityManagerWithOpenTransaction(); + foundCustomer = em3.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map); + em2.getTransaction() + .rollback(); + + em2.close(); + em3.close(); } @Test - public void givenEclipseEntityWithOneToMany_whenNormalLock_thenShouldLockOnlyOwningEntity() throws IOException { + public void givenEntityWithOneToMany_whenLock_thenHibernateExtendedScopeLockOnlyOwningEntity() throws IOException { EntityManager em = getEntityManagerWithOpenTransaction(); PessimisticLockingStudent student = new PessimisticLockingStudent(1L, "JOE"); PessimisticLockingCourse course = new PessimisticLockingCourse(1L, "COURSE", student); @@ -74,17 +84,22 @@ public class PessimisticLockScopesIntegrationTest { em.close(); // NORMAL SCOPE - em = getEntityManagerWithOpenTransaction(); - PessimisticLockingCourse foundCourse = em.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE); - em.getTransaction() + EntityManager em2 = getEntityManagerWithOpenTransaction(); + PessimisticLockingCourse foundCourse = em2.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE); + em2.getTransaction() .rollback(); // EXTENDED SCOPE Map map = new HashMap<>(); map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED); - em = getEntityManagerWithOpenTransaction(); - foundCourse = em.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE, map); + EntityManager em3 = getEntityManagerWithOpenTransaction(); + foundCourse = em3.find(PessimisticLockingCourse.class, 1L, LockModeType.PESSIMISTIC_WRITE, map); + em3.getTransaction() + .rollback(); + + em2.close(); + em3.close(); } protected EntityManager getEntityManagerWithOpenTransaction() throws IOException { diff --git a/persistence-modules/spring-data-eclipselink/src/test/java/com/baeldung/eclipselink/springdata/pessimisticlocking/PessimisticLockScopesIntegrationTest.java b/persistence-modules/spring-data-eclipselink/src/test/java/com/baeldung/eclipselink/springdata/pessimisticlocking/PessimisticLockScopesIntegrationTest.java index e84e991d5d..6ee40fac9a 100644 --- a/persistence-modules/spring-data-eclipselink/src/test/java/com/baeldung/eclipselink/springdata/pessimisticlocking/PessimisticLockScopesIntegrationTest.java +++ b/persistence-modules/spring-data-eclipselink/src/test/java/com/baeldung/eclipselink/springdata/pessimisticlocking/PessimisticLockScopesIntegrationTest.java @@ -22,54 +22,64 @@ public class PessimisticLockScopesIntegrationTest { EntityManagerFactory entityManagerFactory; @Test - public void givenEclipseEntityWithJoinInheritance_whenNormalLock_thenShouldChildAndParentEntity() { + public void givenEntityWithJoinInheritance_whenLock_thenNormalAndExtendScopesLockParentAndChildEntity() { EntityManager em = getEntityManagerWithOpenTransaction(); - Employee employee = new Employee(1L, "A", "B", new BigDecimal(4.5)); + Employee employee = new Employee(1L, "JOE", "DOE", new BigDecimal(4.5)); em.persist(employee); em.getTransaction() .commit(); em.close(); // NORMAL SCOPE - em = getEntityManagerWithOpenTransaction(); - Employee foundEmployee = em.find(Employee.class, 1L, LockModeType.PESSIMISTIC_WRITE); - em.getTransaction() + EntityManager em2 = getEntityManagerWithOpenTransaction(); + Employee foundEmployee = em2.find(Employee.class, 1L, LockModeType.PESSIMISTIC_WRITE); + em2.getTransaction() .rollback(); // EXTENDED SCOPE Map map = new HashMap<>(); map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED); - em = getEntityManagerWithOpenTransaction(); - foundEmployee = em.find(Employee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map); + EntityManager em3 = getEntityManagerWithOpenTransaction(); + foundEmployee = em3.find(Employee.class, 1L, LockModeType.PESSIMISTIC_WRITE, map); + em3.getTransaction() + .rollback(); + + em2.close(); + em3.close(); } @Test - public void givenEclipseEntityWithElementCollection_whenNormalLock_thenShouldLockOnlyOwningEntity() { + public void givenEntityWithElementCollection_whenLock_thenExtendScopeLocksAlsoCollectionTable() { EntityManager em = getEntityManagerWithOpenTransaction(); Address address = new Address("Poland", "Warsaw"); - Customer customer = new Customer(1L, "A", "B", Arrays.asList(address)); + Customer customer = new Customer(1L, "JOHN", "SMITH", Arrays.asList(address)); em.persist(customer); em.getTransaction() .commit(); em.close(); // NORMAL SCOPE - em = getEntityManagerWithOpenTransaction(); - Customer foundCustomer = em.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE); - em.getTransaction() + EntityManager em2 = getEntityManagerWithOpenTransaction(); + Customer foundCustomer = em2.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE); + em2.getTransaction() .rollback(); // EXTENDED SCOPE Map map = new HashMap<>(); map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED); - em = getEntityManagerWithOpenTransaction(); - foundCustomer = em.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map); + EntityManager em3 = getEntityManagerWithOpenTransaction(); + foundCustomer = em3.find(Customer.class, 1L, LockModeType.PESSIMISTIC_WRITE, map); + em3.getTransaction() + .rollback(); + + em2.close(); + em3.close(); } @Test - public void givenEclipseEntityWithOneToMany_whenNormalLock_thenShouldLockOnlyOwningEntity() { + public void givenEclipseEntityWithOneToMany_whenLock_thenExtendedLockAlsoJoinTable() { EntityManager em = getEntityManagerWithOpenTransaction(); Student student = new Student(1L, "JOE"); Course course = new Course(1L, "COURSE", student); @@ -81,17 +91,22 @@ public class PessimisticLockScopesIntegrationTest { em.close(); // NORMAL SCOPE - em = getEntityManagerWithOpenTransaction(); - Course foundCourse = em.find(Course.class, 1L, LockModeType.PESSIMISTIC_WRITE); - em.getTransaction() + EntityManager em2 = getEntityManagerWithOpenTransaction(); + Course foundCourse = em2.find(Course.class, 1L, LockModeType.PESSIMISTIC_WRITE); + em2.getTransaction() .rollback(); // EXTENDED SCOPE Map map = new HashMap<>(); map.put("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED); - em = getEntityManagerWithOpenTransaction(); - foundCourse = em.find(Course.class, 1L, LockModeType.PESSIMISTIC_WRITE, map); + EntityManager em3 = getEntityManagerWithOpenTransaction(); + foundCourse = em3.find(Course.class, 1L, LockModeType.PESSIMISTIC_WRITE, map); + em3.getTransaction() + .rollback(); + + em2.close(); + em3.close(); } protected EntityManager getEntityManagerWithOpenTransaction() { From 114b44da7e52d4f6a91c214114b7bd98248b0f21 Mon Sep 17 00:00:00 2001 From: Karan Khanna Date: Thu, 10 May 2018 22:56:09 +0200 Subject: [PATCH 04/10] changes to add spring-data-keyvalue to parent pom. --- pom.xml | 1 + spring-data-keyvalue/pom.xml | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 3ba6b0c8df..ea1f5e1f94 100644 --- a/pom.xml +++ b/pom.xml @@ -152,6 +152,7 @@ spring-data-couchbase-2 persistence-modules/spring-data-dynamodb spring-data-elasticsearch + spring-data-keyvalue spring-data-mongodb persistence-modules/spring-data-neo4j persistence-modules/spring-data-redis diff --git a/spring-data-keyvalue/pom.xml b/spring-data-keyvalue/pom.xml index 1b77667e0e..e90f18948f 100644 --- a/spring-data-keyvalue/pom.xml +++ b/spring-data-keyvalue/pom.xml @@ -26,13 +26,11 @@ org.springframework.data spring-data-keyvalue - ${spring-data-keyvalue.version} org.springframework.boot spring-boot-starter-test - 1.5.12.RELEASE From cfdf48ac9320f9589ad4d78914023073baeda615 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sat, 12 May 2018 00:34:53 +0300 Subject: [PATCH 05/10] Update README.md --- core-java/README.md | 7 ------- 1 file changed, 7 deletions(-) diff --git a/core-java/README.md b/core-java/README.md index 8a94c9de8e..9f9ff61a48 100644 --- a/core-java/README.md +++ b/core-java/README.md @@ -74,17 +74,14 @@ - [CharSequence vs. String in Java](http://www.baeldung.com/java-char-sequence-string) - [Period and Duration in Java](http://www.baeldung.com/java-period-duration) - [Guide to the Diamond Operator in Java](http://www.baeldung.com/java-diamond-operator) -- [Singletons in Java](http://www.baeldung.com/java-singleton) - [“Sneaky Throws” in Java](http://www.baeldung.com/java-sneaky-throws) - [OutOfMemoryError: GC Overhead Limit Exceeded](http://www.baeldung.com/java-gc-overhead-limit-exceeded) - [StringBuilder and StringBuffer in Java](http://www.baeldung.com/java-string-builder-string-buffer) - [Number of Digits in an Integer in Java](http://www.baeldung.com/java-number-of-digits-in-int) -- [Proxy, Decorator, Adapter and Bridge Patterns](http://www.baeldung.com/java-structural-design-patterns) - [Creating a Java Compiler Plugin](http://www.baeldung.com/java-build-compiler-plugin) - [A Guide to the Static Keyword in Java](http://www.baeldung.com/java-static) - [Initializing Arrays in Java](http://www.baeldung.com/java-initialize-array) - [Guide to Java String Pool](http://www.baeldung.com/java-string-pool) -- [Introduction to Creational Design Patterns](http://www.baeldung.com/creational-design-patterns) - [Quick Example - Comparator vs Comparable in Java](http://www.baeldung.com/java-comparator-comparable) - [Quick Guide to Java Stack](http://www.baeldung.com/java-stack) - [The Java continue and break Keywords](http://www.baeldung.com/java-continue-and-break) @@ -116,8 +113,6 @@ - [Comparing Strings in Java](http://www.baeldung.com/java-compare-strings) - [Guide to Inheritance in Java](http://www.baeldung.com/java-inheritance) - [Guide to Externalizable Interface in Java](http://www.baeldung.com/java-externalizable) -- [The Observer Pattern in Java](http://www.baeldung.com/java-observer-pattern) -- [Flyweight Pattern in Java](http://www.baeldung.com/java-flyweight) - [Object Type Casting in Java](http://www.baeldung.com/java-type-casting) - [A Practical Guide to DecimalFormat](http://www.baeldung.com/java-decimalformat) - [How to Detect the OS Using Java](http://www.baeldung.com/java-detect-os) @@ -136,7 +131,6 @@ - [Class Loaders in Java](http://www.baeldung.com/java-classloaders) - [Find Sum and Average in a Java Array](http://www.baeldung.com/java-array-sum-average) - [Java List UnsupportedOperationException](http://www.baeldung.com/java-list-unsupported-operation-exception) -- [Service Locator Pattern](http://www.baeldung.com/java-service-locator-pattern) - [Type Erasure in Java Explained](http://www.baeldung.com/java-type-erasure) - [BigDecimal and BigInteger in Java](http://www.baeldung.com/java-bigdecimal-biginteger) - [Display All Time Zones With GMT And UTC in Java](http://www.baeldung.com/java-time-zones) @@ -145,4 +139,3 @@ - [Sending Emails with Java](http://www.baeldung.com/java-email) - [Introduction to SSL in Java](http://www.baeldung.com/java-ssl) - [Java KeyStore API](http://www.baeldung.com/java-keystore) -- [Double-Checked Locking with Singleton](http://www.baeldung.com/java-singleton-double-checked-locking) From a61bdbcc59be3ab7686c910ebac8c95d333570a8 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sat, 12 May 2018 00:39:53 +0300 Subject: [PATCH 06/10] Update README.md --- core-java/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java/README.md b/core-java/README.md index 9f9ff61a48..3cefb5820d 100644 --- a/core-java/README.md +++ b/core-java/README.md @@ -139,3 +139,4 @@ - [Sending Emails with Java](http://www.baeldung.com/java-email) - [Introduction to SSL in Java](http://www.baeldung.com/java-ssl) - [Java KeyStore API](http://www.baeldung.com/java-keystore) +- [Using Java Assertions](http://www.baeldung.com/java-assert) From 4a81b13958e4d4b8048ef423f2793b6775076d91 Mon Sep 17 00:00:00 2001 From: Ekaterina Galkina Date: Sat, 12 May 2018 18:10:42 +0500 Subject: [PATCH 07/10] BAEL-1736 --- .../java/com/baeldung/hashtable/Word.java | 28 ++ .../baeldung/hashtable/HashtableUnitTest.java | 274 ++++++++++++++++++ 2 files changed, 302 insertions(+) create mode 100644 core-java-8/src/main/java/com/baeldung/hashtable/Word.java create mode 100644 core-java-8/src/test/java/com/baeldung/hashtable/HashtableUnitTest.java diff --git a/core-java-8/src/main/java/com/baeldung/hashtable/Word.java b/core-java-8/src/main/java/com/baeldung/hashtable/Word.java new file mode 100644 index 0000000000..eb7c5b7dca --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/hashtable/Word.java @@ -0,0 +1,28 @@ +package com.baeldung.hashtable; + +public class Word { + private String name; + + public Word(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public boolean equals(Object o) { + if (o == this) + return true; + if (!(o instanceof Word)) + return false; + + Word word = (Word) o; + return word.getName().equals(this.name) ? true : false; + + } + + public int hashCode() { + return name.hashCode(); + } +} diff --git a/core-java-8/src/test/java/com/baeldung/hashtable/HashtableUnitTest.java b/core-java-8/src/test/java/com/baeldung/hashtable/HashtableUnitTest.java new file mode 100644 index 0000000000..e8c42c94e1 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/hashtable/HashtableUnitTest.java @@ -0,0 +1,274 @@ +package com.baeldung.hashtable; + +import java.util.ConcurrentModificationException; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.Map; + +import static org.junit.Assert.*; +import static org.hamcrest.Matchers.*; +import org.junit.Test; + +public class HashtableUnitTest { + + @Test + public void whenPutAndGet_thenReturnsValue() { + Hashtable table = new Hashtable(); + + Word word = new Word("cat"); + table.put(word, "an animal"); + + String definition = table.get(word); + + assertEquals("an animal", definition); + + definition = table.remove(word); + + assertEquals("an animal", definition); + } + + @Test + public void whenThesameInstanceOfKey_thenReturnsValue() { + Hashtable table = new Hashtable(); + Word word = new Word("cat"); + table.put(word, "an animal"); + String extracted = table.get(word); + assertEquals("an animal", extracted); + } + + @Test + public void whenEqualsOverridden_thenReturnsValue() { + Hashtable table = new Hashtable(); + Word word = new Word("cat"); + table.put(word, "an animal"); + String extracted = table.get(new Word("cat")); + assertEquals("an animal", extracted); + } + + @Test(expected = NullPointerException.class) + public void whenNullKey_thenException() { + Hashtable table = new Hashtable(); + table.put(null, "an animal"); + } + + @Test(expected = ConcurrentModificationException.class) + public void whenIterate_thenFailFast() { + + Hashtable table = new Hashtable(); + table.put(new Word("cat"), "an animal"); + table.put(new Word("dog"), "another animal"); + + Iterator it = table.keySet().iterator(); + System.out.println("iterator created"); + + table.remove(new Word("dog")); + System.out.println("element removed"); + + while (it.hasNext()) { + Word key = it.next(); + System.out.println(table.get(key)); + } + } + + @Test + public void whenEnumerate_thenNotFailFast() { + + Hashtable table = new Hashtable(); + table.put(new Word("1"), "one"); + table.put(new Word("2"), "two"); + table.put(new Word("3"), "three"); + table.put(new Word("4"), "four"); + table.put(new Word("5"), "five"); + table.put(new Word("6"), "six"); + table.put(new Word("7"), "seven"); + table.put(new Word("8"), "eight"); + + Enumeration enumKey = table.keys(); + System.out.println("Enumeration created"); + table.remove(new Word("1")); + System.out.println("element removed"); + while (enumKey.hasMoreElements()) { + Word key = enumKey.nextElement(); + System.out.println(table.get(key)); + } + } + + @Test + public void whenAddElements_thenIterationOrderUnpredicable() { + + Hashtable table = new Hashtable(); + table.put(new Word("1"), "one"); + table.put(new Word("2"), "two"); + table.put(new Word("3"), "three"); + table.put(new Word("4"), "four"); + table.put(new Word("5"), "five"); + table.put(new Word("6"), "six"); + table.put(new Word("7"), "seven"); + table.put(new Word("8"), "eight"); + + Iterator> it = table.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry entry = it.next(); + System.out.println(entry.getValue()); + } + } + + @Test + public void whenGetOrDefault_thenDefaultGot() { + + Hashtable table = new Hashtable(); + table.put(new Word("cat"), "a small domesticated carnivorous mammal"); + Word key = new Word("dog"); + String definition; + + // old way + /* if (table.containsKey(key)) { + definition = table.get(key); + } else { + definition = "not found"; + }*/ + + // new way + definition = table.getOrDefault(key, "not found"); + + assertThat(definition, is("not found")); + } + + @Test + public void whenPutifAbsent_thenNotRewritten() { + + Hashtable table = new Hashtable(); + table.put(new Word("cat"), "a small domesticated carnivorous mammal"); + + String definition = "an animal"; + // old way + /* if (!table.containsKey(new Word("cat"))) { + table.put(new Word("cat"), definition); + }*/ + // new way + table.putIfAbsent(new Word("cat"), definition); + + assertThat(table.get(new Word("cat")), is("a small domesticated carnivorous mammal")); + } + + @Test + public void whenRemovePair_thenCheckKeyAndValue() { + + Hashtable table = new Hashtable(); + table.put(new Word("cat"), "a small domesticated carnivorous mammal"); + + // old way + /* if (table.get(new Word("cat")).equals("an animal")) { + table.remove(new Word("cat")); + }*/ + + // new way + boolean result = table.remove(new Word("cat"), "an animal"); + + assertThat(result, is(false)); + } + + @Test + public void whenReplacePair_thenValueChecked() { + + Hashtable table = new Hashtable(); + table.put(new Word("cat"), "a small domesticated carnivorous mammal"); + + String definition = "an animal"; + + // old way + /* if (table.containsKey(new Word("cat")) && table.get(new Word("cat")).equals("a small domesticated carnivorous mammal")) { + table.put(new Word("cat"), definition); + }*/ + // new way + table.replace(new Word("cat"), "a small domesticated carnivorous mammal", definition); + + assertThat(table.get(new Word("cat")), is("an animal")); + + } + + @Test + public void whenKeyIsAbsent_thenNotRewritten() { + + Hashtable table = new Hashtable(); + table.put(new Word("cat"), "a small domesticated carnivorous mammal"); + + // old way + /* if (!table.containsKey(cat)) { + String definition = "an animal";// calculate + table.put(new Word("cat"), definition); + } + */ + // new way + + table.computeIfAbsent(new Word("cat"), key -> "an animal"); + assertThat(table.get(new Word("cat")), is("a small domesticated carnivorous mammal")); + + } + + @Test + public void whenKeyIsPresent_thenComputeIfPresent() { + + Hashtable table = new Hashtable(); + table.put(new Word("cat"), "a small domesticated carnivorous mammal"); + + Word cat = new Word("cat"); + // old way + /* if (table.containsKey(cat)) { + String concatination = cat.getName() + " - " + table.get(cat); + table.put(cat, concatination); + }*/ + + // new way + table.computeIfPresent(cat, (key, value) -> key.getName() + " - " + value); + + assertThat(table.get(cat), is("cat - a small domesticated carnivorous mammal")); + + } + + @Test + public void whenCompute_thenForAllKeys() { + + Hashtable table = new Hashtable(); + String[] animals = { "cat", "dog", "dog", "cat", "bird", "mouse", "mouse" }; + for (String animal : animals) { + table.compute(animal, (key, value) -> (value == null ? Integer.valueOf(1) : Integer.valueOf(value) + 1)); + } + assertThat(table.values(), hasItems(2, 2, 2, 1)); + + } + + @Test + public void whenInsteadOfCompute_thenMerge() { + + Hashtable table = new Hashtable(); + String[] animals = { "cat", "dog", "dog", "cat", "bird", "mouse", "mouse" }; + for (String animal : animals) { + table.merge(animal, 1, (oldValue, value) -> (oldValue + value)); + } + assertThat(table.values(), hasItems(2, 2, 2, 1)); + } + + @Test + public void whenForeach_thenIterate() { + + Hashtable table = new Hashtable(); + table.put(new Word("cat"), "a small domesticated carnivorous mammal"); + table.put(new Word("dog"), "another animal"); + table.forEach((k, v) -> System.out.println(k.getName() + " - " + v) + + ); + } + + @Test + public void whenReplaceall_thenNoIterationNeeded() { + + Hashtable table = new Hashtable(); + table.put(new Word("cat"), "a small domesticated carnivorous mammal"); + table.put(new Word("dog"), "another animal"); + table.replaceAll((k, v) -> k.getName() + " - " + v); + + assertThat(table.values(), hasItems("cat - a small domesticated carnivorous mammal", "dog - another animal")); + } +} From 9aad5c0daf90b172e5b80496364eaf9aad73dfb7 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sat, 12 May 2018 19:06:51 +0300 Subject: [PATCH 08/10] Update README.md From 506962bc96309ad49a6ad1f38dd5bb91f73e91e7 Mon Sep 17 00:00:00 2001 From: fanatixan Date: Sat, 12 May 2018 22:43:12 +0200 Subject: [PATCH 09/10] Spring annotations article (#4232) * Spring annotations * commented VehicleFactoryApplication to fix CI build --- .../java/com/baeldung/annotations/Bike.java | 20 ++++++ .../java/com/baeldung/annotations/Biker.java | 24 +++++++ .../java/com/baeldung/annotations/Car.java | 30 +++++++++ .../com/baeldung/annotations/CarMechanic.java | 10 +++ .../com/baeldung/annotations/CarUtility.java | 7 ++ .../java/com/baeldung/annotations/Driver.java | 32 +++++++++ .../java/com/baeldung/annotations/Engine.java | 26 ++++++++ .../com/baeldung/annotations/Vehicle.java | 4 ++ .../annotations/VehicleController.java | 66 +++++++++++++++++++ .../VehicleFactoryApplication.java | 13 ++++ .../annotations/VehicleFactoryConfig.java | 30 +++++++++ .../annotations/VehicleRepository.java | 8 +++ .../annotations/VehicleRestController.java | 8 +++ .../baeldung/annotations/VehicleService.java | 13 ++++ .../src/main/resources/annotations.properties | 1 + .../src/main/resources/annotations.xml | 9 +++ 16 files changed, 301 insertions(+) create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/Bike.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/Biker.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/Car.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/CarMechanic.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/CarUtility.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/Driver.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/Engine.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/Vehicle.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleController.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryApplication.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryConfig.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRepository.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRestController.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleService.java create mode 100644 spring-mvc-java/src/main/resources/annotations.properties create mode 100644 spring-mvc-java/src/main/resources/annotations.xml diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Bike.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/Bike.java new file mode 100644 index 0000000000..2a893e7903 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/Bike.java @@ -0,0 +1,20 @@ +package com.baeldung.annotations; + +import org.springframework.beans.factory.annotation.Required; +import org.springframework.context.annotation.DependsOn; + +@DependsOn +public class Bike implements Vehicle { + + private String color; + + @Required + public void setColor(String color) { + this.color = color; + } + + public String getColor() { + return color; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Biker.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/Biker.java new file mode 100644 index 0000000000..dcbe534a90 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/Biker.java @@ -0,0 +1,24 @@ +package com.baeldung.annotations; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Component; + +@Component +public class Biker { + + @Autowired + @Qualifier("bike") + private Vehicle vehicle; + + @Autowired + public Biker(@Qualifier("bike") Vehicle vehicle) { + this.vehicle = vehicle; + } + + @Autowired + public void setVehicle(@Qualifier("bike") Vehicle vehicle) { + this.vehicle = vehicle; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Car.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/Car.java new file mode 100644 index 0000000000..cb252b60c9 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/Car.java @@ -0,0 +1,30 @@ +package com.baeldung.annotations; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.DependsOn; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Component; + +@Component +@Primary +@DependsOn("engine") +public class Car implements Vehicle { + + @Autowired + private Engine engine; + + @Autowired + public Car(Engine engine) { + this.engine = engine; + } + + @Autowired + public void setEngine(Engine engine) { + this.engine = engine; + } + + public Engine getEngine() { + return engine; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/CarMechanic.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/CarMechanic.java new file mode 100644 index 0000000000..fc933dcd61 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/CarMechanic.java @@ -0,0 +1,10 @@ +package com.baeldung.annotations; + +import org.springframework.context.annotation.Lazy; +import org.springframework.stereotype.Component; + +@Component +@Lazy +public class CarMechanic { + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/CarUtility.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/CarUtility.java new file mode 100644 index 0000000000..e66ab02b01 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/CarUtility.java @@ -0,0 +1,7 @@ +package com.baeldung.annotations; + +import org.springframework.stereotype.Component; + +@Component +public class CarUtility { +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Driver.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/Driver.java new file mode 100644 index 0000000000..5a2d86e99e --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/Driver.java @@ -0,0 +1,32 @@ +package com.baeldung.annotations; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +@Component +public class Driver { + + @Autowired + private Vehicle vehicle; + + @Autowired + public Driver(Vehicle vehicle) { + this.vehicle = vehicle; + } + + @Autowired + public void setVehicle(Vehicle vehicle) { + this.vehicle = vehicle; + } + + public Vehicle getVehicle() { + return vehicle; + } + + @Scheduled(fixedRate = 10000) + @Scheduled(cron = "0 * * * * MON-FRI") + public void checkVehicle() { + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Engine.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/Engine.java new file mode 100644 index 0000000000..f8c0154639 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/Engine.java @@ -0,0 +1,26 @@ +package com.baeldung.annotations; + +import org.springframework.beans.factory.annotation.Value; + +public class Engine { + + @Value("8") + private int cylinderCount; + + @Value("${engine.fuelType}") + private String fuelType; + + public Engine() { + this(8); + } + + public Engine(@Value("8") int cylinderCount) { + this.cylinderCount = cylinderCount; + } + + @Value("8") + public void setCylinderCount(int cylinderCount) { + this.cylinderCount = cylinderCount; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/Vehicle.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/Vehicle.java new file mode 100644 index 0000000000..7176c3cc8a --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/Vehicle.java @@ -0,0 +1,4 @@ +package com.baeldung.annotations; + +public interface Vehicle { +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleController.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleController.java new file mode 100644 index 0000000000..a979dff708 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleController.java @@ -0,0 +1,66 @@ +package com.baeldung.annotations; + +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; + +@Controller +@RequestMapping(value = "/vehicles", method = RequestMethod.GET) +public class VehicleController { + + @CrossOrigin + @ResponseBody + @RequestMapping("/hello") + public String hello() { + return "Hello World!"; + } + + @RequestMapping("/home") + public String home() { + return "home"; + } + + @PostMapping("/save") + public void saveVehicle(@RequestBody Vehicle vehicle) { + } + + @RequestMapping("/{id}") + public Vehicle getVehicle(@PathVariable("id") long id) { + return null; + } + + @RequestMapping + public Vehicle getVehicleByParam(@RequestParam("id") long id) { + return null; + } + + @RequestMapping("/buy") + public Car buyCar(@RequestParam(defaultValue = "5") int seatCount) { + return null; + } + + @ExceptionHandler(IllegalArgumentException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public void onIllegalArgumentException(IllegalArgumentException exception) { + } + + @PostMapping("/assemble") + public void assembleVehicle(@ModelAttribute("vehicle") Vehicle vehicle) { + } + + @ModelAttribute("vehicle") + public Vehicle getVehicle() { + return null; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryApplication.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryApplication.java new file mode 100644 index 0000000000..ab90d8cef2 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.annotations; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +// @SpringBootApplication +public class VehicleFactoryApplication { + +// public static void main(String[] args) { +// SpringApplication.run(VehicleFactoryApplication.class, args); +// } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryConfig.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryConfig.java new file mode 100644 index 0000000000..ad43a19b66 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleFactoryConfig.java @@ -0,0 +1,30 @@ +package com.baeldung.annotations; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; +import org.springframework.context.annotation.Lazy; +import org.springframework.context.annotation.PropertySource; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.annotation.EnableScheduling; + +@Configuration +@ComponentScan(basePackages = "com.baeldung.annotations") +@ComponentScan(basePackageClasses = VehicleFactoryConfig.class) +@ImportResource("classpath:/annotations.xml") +@PropertySource("classpath:/annotations.properties") +@Lazy +@EnableAutoConfiguration +@EnableAsync +@EnableScheduling +public class VehicleFactoryConfig { + + @Bean + @Lazy(false) + public Engine engine() { + return new Engine(); + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRepository.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRepository.java new file mode 100644 index 0000000000..4dfd2dd771 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRepository.java @@ -0,0 +1,8 @@ +package com.baeldung.annotations; + +import org.springframework.stereotype.Repository; + +@Repository +public class VehicleRepository { + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRestController.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRestController.java new file mode 100644 index 0000000000..9dd98e688f --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleRestController.java @@ -0,0 +1,8 @@ +package com.baeldung.annotations; + +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class VehicleRestController { + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleService.java b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleService.java new file mode 100644 index 0000000000..19e5a3b4a4 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/annotations/VehicleService.java @@ -0,0 +1,13 @@ +package com.baeldung.annotations; + +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Service; + +@Service +public class VehicleService { + + @Async + public void repairCar() { + } + +} diff --git a/spring-mvc-java/src/main/resources/annotations.properties b/spring-mvc-java/src/main/resources/annotations.properties new file mode 100644 index 0000000000..ac2f50b39b --- /dev/null +++ b/spring-mvc-java/src/main/resources/annotations.properties @@ -0,0 +1 @@ +engine.fuelType=petrol \ No newline at end of file diff --git a/spring-mvc-java/src/main/resources/annotations.xml b/spring-mvc-java/src/main/resources/annotations.xml new file mode 100644 index 0000000000..b477b68ef6 --- /dev/null +++ b/spring-mvc-java/src/main/resources/annotations.xml @@ -0,0 +1,9 @@ + + + + + + + + From 3c28681b6db356eebe3b2661c719fb7921c87606 Mon Sep 17 00:00:00 2001 From: Tino Mulanchira Thomas <38363530+tinomthomas@users.noreply.github.com> Date: Sun, 13 May 2018 11:31:30 +0300 Subject: [PATCH 10/10] BAEL-1756 (#4230) * code samples for spring data key value BAEL-1467 * changes to add spring-data-keyvalue to parent pom. * Update README.md * Update README.md * Update README.md * How to Use Spring RestTemplate Interceptor * Spring annotations article (#4232) * Spring annotations * commented VehicleFactoryApplication to fix CI build * BAEL-1756 Test typo fixed --- .../java/com/baeldung/transfer/LoginForm.java | 6 +++ .../org/baeldung/config/RestClientConfig.java | 30 +++++++++++++ .../RestTemplateLoggingInterceptor.java | 41 ++++++++++++++++++ .../RestTemplateIntegrationTest.java | 43 +++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 spring-rest/src/main/java/org/baeldung/config/RestClientConfig.java create mode 100644 spring-rest/src/main/java/org/baeldung/interceptors/RestTemplateLoggingInterceptor.java create mode 100644 spring-rest/src/test/java/org/baeldung/resttemplate/RestTemplateIntegrationTest.java diff --git a/spring-rest/src/main/java/com/baeldung/transfer/LoginForm.java b/spring-rest/src/main/java/com/baeldung/transfer/LoginForm.java index 19c9b0a349..caafcdb500 100644 --- a/spring-rest/src/main/java/com/baeldung/transfer/LoginForm.java +++ b/spring-rest/src/main/java/com/baeldung/transfer/LoginForm.java @@ -7,6 +7,12 @@ public class LoginForm { public LoginForm() { } + public LoginForm(String username, String password) { + super(); + this.username = username; + this.password = password; + } + public String getUsername() { return username; } diff --git a/spring-rest/src/main/java/org/baeldung/config/RestClientConfig.java b/spring-rest/src/main/java/org/baeldung/config/RestClientConfig.java new file mode 100644 index 0000000000..3619f43f8a --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/config/RestClientConfig.java @@ -0,0 +1,30 @@ +package org.baeldung.config; + +import java.util.ArrayList; +import java.util.List; + +import org.baeldung.interceptors.RestTemplateLoggingInterceptor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.client.BufferingClientHttpRequestFactory; +import org.springframework.http.client.ClientHttpRequestInterceptor; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.util.CollectionUtils; +import org.springframework.web.client.RestTemplate; + +@Configuration +public class RestClientConfig { + + @Bean + public RestTemplate restTemplate() { + RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())); + + List interceptors = restTemplate.getInterceptors(); + if (CollectionUtils.isEmpty(interceptors)) { + interceptors = new ArrayList(); + } + interceptors.add(new RestTemplateLoggingInterceptor()); + restTemplate.setInterceptors(interceptors); + return restTemplate; + } +} diff --git a/spring-rest/src/main/java/org/baeldung/interceptors/RestTemplateLoggingInterceptor.java b/spring-rest/src/main/java/org/baeldung/interceptors/RestTemplateLoggingInterceptor.java new file mode 100644 index 0000000000..56de3d04b8 --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/interceptors/RestTemplateLoggingInterceptor.java @@ -0,0 +1,41 @@ +package org.baeldung.interceptors; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.http.HttpRequest; +import org.springframework.http.client.ClientHttpRequestExecution; +import org.springframework.http.client.ClientHttpRequestInterceptor; +import org.springframework.http.client.ClientHttpResponse; +import org.springframework.util.StreamUtils; + +public class RestTemplateLoggingInterceptor implements ClientHttpRequestInterceptor { + + @Override + public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { + logRequest(body); + ClientHttpResponse response = execution.execute(request, body); + logResponse(response); + response.getHeaders().add("Foo", "bar"); + return response; + } + + private void logRequest(byte[] body) { + String payLoad = StringUtils.EMPTY; + if (body.length > 0) { + payLoad = new String(body, StandardCharsets.UTF_8); + } + System.out.println("Request Body > " + payLoad); + } + + private void logResponse(ClientHttpResponse response) throws IOException { + String payLoad = StringUtils.EMPTY; + long contentLength = response.getHeaders() + .getContentLength(); + if (contentLength != 0) { + payLoad = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8); + } + System.out.println("Response Body > " + payLoad); + } +} diff --git a/spring-rest/src/test/java/org/baeldung/resttemplate/RestTemplateIntegrationTest.java b/spring-rest/src/test/java/org/baeldung/resttemplate/RestTemplateIntegrationTest.java new file mode 100644 index 0000000000..e0c24c32b1 --- /dev/null +++ b/spring-rest/src/test/java/org/baeldung/resttemplate/RestTemplateIntegrationTest.java @@ -0,0 +1,43 @@ +package org.baeldung.resttemplate; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.baeldung.config.RestClientConfig; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.web.client.RestTemplate; + +import com.baeldung.transfer.LoginForm; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = RestClientConfig.class) +public class RestTemplateIntegrationTest { + + @Autowired + RestTemplate restTemplate; + + @Test + public void givenRestTemplate_whenRequested_thenLogAndModifyResponse() { + LoginForm loginForm = new LoginForm("userName", "password"); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + HttpEntity requestEntity = new HttpEntity(loginForm, headers); + + ResponseEntity responseEntity = restTemplate.postForEntity("http://httpbin.org/post", requestEntity, String.class); + + assertThat(responseEntity.getStatusCode(), is(equalTo(HttpStatus.OK))); + assertThat(responseEntity.getHeaders() + .get("Foo") + .get(0), is(equalTo("bar"))); + } +}