JAVA-628: Moved 4 articles from spring-core-2
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
-57
@@ -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"));
|
||||
}
|
||||
}
|
||||
-51
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
package com.baeldung.customscope;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
public class TenantScopeIntegrationTest {
|
||||
|
||||
@Test
|
||||
public final void whenRegisterScopeAndBeans_thenContextContainsFooAndBar() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
try {
|
||||
ctx.register(TenantScopeConfig.class);
|
||||
ctx.register(TenantBeansConfig.class);
|
||||
ctx.refresh();
|
||||
|
||||
TenantBean foo = (TenantBean) ctx.getBean("foo", TenantBean.class);
|
||||
foo.sayHello();
|
||||
TenantBean bar = (TenantBean) ctx.getBean("bar", TenantBean.class);
|
||||
bar.sayHello();
|
||||
Map<String, TenantBean> foos = ctx.getBeansOfType(TenantBean.class);
|
||||
|
||||
assertThat(foo, not(equalTo(bar)));
|
||||
assertThat(foos.size(), equalTo(2));
|
||||
assertTrue(foos.containsValue(foo));
|
||||
assertTrue(foos.containsValue(bar));
|
||||
|
||||
BeanDefinition fooDefinition = ctx.getBeanDefinition("foo");
|
||||
BeanDefinition barDefinition = ctx.getBeanDefinition("bar");
|
||||
|
||||
assertThat(fooDefinition.getScope(), equalTo("tenant"));
|
||||
assertThat(barDefinition.getScope(), equalTo("tenant"));
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenComponentScan_thenContextContainsFooAndBar() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
try {
|
||||
ctx.scan("com.baeldung.customscope");
|
||||
ctx.refresh();
|
||||
|
||||
TenantBean foo = (TenantBean) ctx.getBean("foo", TenantBean.class);
|
||||
foo.sayHello();
|
||||
TenantBean bar = (TenantBean) ctx.getBean("bar", TenantBean.class);
|
||||
bar.sayHello();
|
||||
Map<String, TenantBean> foos = ctx.getBeansOfType(TenantBean.class);
|
||||
|
||||
assertThat(foo, not(equalTo(bar)));
|
||||
assertThat(foos.size(), equalTo(2));
|
||||
assertTrue(foos.containsValue(foo));
|
||||
assertTrue(foos.containsValue(bar));
|
||||
|
||||
BeanDefinition fooDefinition = ctx.getBeanDefinition("foo");
|
||||
BeanDefinition barDefinition = ctx.getBeanDefinition("bar");
|
||||
|
||||
assertThat(fooDefinition.getScope(), equalTo("tenant"));
|
||||
assertThat(barDefinition.getScope(), equalTo("tenant"));
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.startup;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { SpringStartupConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class SpringStartupIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext ctx;
|
||||
|
||||
@Test(expected = BeanCreationException.class)
|
||||
public void whenInstantiating_shouldThrowBCE() throws Exception {
|
||||
ctx.getBean(InvalidInitExampleBean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPostConstruct_shouldLogEnv() throws Exception {
|
||||
ctx.getBean(PostConstructExampleBean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConstructorInjection_shouldLogEnv() throws Exception {
|
||||
ctx.getBean(LogicInConstructorExampleBean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInitializingBean_shouldLogEnv() throws Exception {
|
||||
ctx.getBean(InitializingBeanExampleBean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenApplicationListener_shouldRunOnce() throws Exception {
|
||||
Assertions.assertThat(StartupApplicationListenerExample.counter).isEqualTo(1);
|
||||
}
|
||||
}
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
package com.baeldung.startup;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:startupConfig.xml")
|
||||
public class SpringStartupXMLConfigIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext ctx;
|
||||
|
||||
@Test
|
||||
public void whenPostConstruct_shouldLogEnv() throws Exception {
|
||||
ctx.getBean(InitMethodExampleBean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAllStrategies_shouldLogOrder() throws Exception {
|
||||
ctx.getBean(AllStrategiesExampleBean.class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user