JAVA-19549 Move A Spring Custom Annotation for a Better DAO article from spring-core-3 module to spring-data-jpa-annotations module (#13748)

This commit is contained in:
anuragkumawat
2023-04-03 21:11:26 +05:30
committed by GitHub
parent 8e4f8366d2
commit cf53d94739
12 changed files with 1 additions and 1 deletions
@@ -1,40 +0,0 @@
package com.baeldung.customannotation;
import java.io.Serializable;
public class Account implements Serializable {
private static final long serialVersionUID = 7857541629844398625L;
private Long id;
private String email;
private Person person;
public Account() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
@@ -1,18 +0,0 @@
package com.baeldung.customannotation;
import org.springframework.stereotype.Repository;
@Repository
public class BeanWithGenericDAO {
@DataAccess(entity = Person.class)
private GenericDAO<Person> personGenericDAO;
public BeanWithGenericDAO() {
}
public GenericDAO<Person> getPersonGenericDAO() {
return personGenericDAO;
}
}
@@ -1,57 +0,0 @@
package com.baeldung.customannotation;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { CustomAnnotationConfiguration.class })
public class DataAccessAnnotationIntegrationTest {
@DataAccess(entity = Person.class)
private GenericDAO<Person> personGenericDAO;
@DataAccess(entity = Account.class)
private GenericDAO<Account> accountGenericDAO;
@DataAccess(entity = Person.class)
private GenericDAO<Person> anotherPersonGenericDAO;
@Test
public void whenGenericDAOInitialized_thenNotNull() {
assertThat(personGenericDAO, is(notNullValue()));
assertThat(accountGenericDAO, is(notNullValue()));
}
@Test
public void whenGenericDAOInjected_thenItIsSingleton() {
assertThat(personGenericDAO, not(sameInstance(accountGenericDAO)));
assertThat(personGenericDAO, not(equalTo(accountGenericDAO)));
assertThat(personGenericDAO, sameInstance(anotherPersonGenericDAO));
}
@Test
public void whenFindAll_thenMessagesIsCorrect() {
personGenericDAO.findAll();
assertThat(personGenericDAO.getMessage(), is("Would create findAll query from Person"));
accountGenericDAO.findAll();
assertThat(accountGenericDAO.getMessage(), is("Would create findAll query from Account"));
}
@Test
public void whenPersist_thenMakeSureThatMessagesIsCorrect() {
personGenericDAO.persist(new Person());
assertThat(personGenericDAO.getMessage(), is("Would create persist query from Person"));
accountGenericDAO.persist(new Account());
assertThat(accountGenericDAO.getMessage(), is("Would create persist query from Account"));
}
}
@@ -1,51 +0,0 @@
package com.baeldung.customannotation;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import java.lang.reflect.Type;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { CustomAnnotationConfiguration.class })
public class DataAccessFieldCallbackIntegrationTest {
@Autowired
private ConfigurableListableBeanFactory configurableListableBeanFactory;
@Autowired
private BeanWithGenericDAO beanWithGenericDAO;
@Rule
public ExpectedException ex = ExpectedException.none();
@Test
public void whenObjectCreated_thenObjectCreationIsSuccessful() {
final DataAccessFieldCallback dataAccessFieldCallback = new DataAccessFieldCallback(configurableListableBeanFactory, beanWithGenericDAO);
assertThat(dataAccessFieldCallback, is(notNullValue()));
}
@Test
public void whenMethodGenericTypeIsValidCalled_thenReturnCorrectValue() throws NoSuchFieldException, SecurityException {
final DataAccessFieldCallback callback = new DataAccessFieldCallback(configurableListableBeanFactory, beanWithGenericDAO);
final Type fieldType = BeanWithGenericDAO.class.getDeclaredField("personGenericDAO").getGenericType();
final boolean result = callback.genericTypeIsValid(Person.class, fieldType);
assertThat(result, is(true));
}
@Test
public void whenMethodGetBeanInstanceCalled_thenReturnCorrectInstance() {
final DataAccessFieldCallback callback = new DataAccessFieldCallback(configurableListableBeanFactory, beanWithGenericDAO);
final Object result = callback.getBeanInstance("personGenericDAO", GenericDAO.class, Person.class);
assertThat((result instanceof GenericDAO), is(true));
}
}
@@ -1,31 +0,0 @@
package com.baeldung.customannotation;
import java.io.Serializable;
public class Person implements Serializable {
private static final long serialVersionUID = 9005331414216374586L;
private Long id;
private String name;
public Person() {
}
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;
}
}