Merge pull request #8125 from eugenp/revert-8119-BAEL-3275-2

Revert "BAEL-3275: Using blocking queue for pub-sub"
This commit is contained in:
Eric Martin
2019-10-31 20:43:47 -05:00
committed by GitHub
parent db85c8f275
commit 3225470df5
20543 changed files with 1642750 additions and 0 deletions
@@ -0,0 +1,25 @@
package com.baeldung.beanfactory;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class BeanFactoryWithClassPathResourceIntegrationTest {
@Test
public void createBeanFactoryAndCheckEmployeeBean() {
Resource res = new ClassPathResource("beanfactory-example.xml");
BeanFactory factory = new XmlBeanFactory(res);
Employee emp = (Employee) factory.getBean("employee");
assertTrue(factory.isSingleton("employee"));
assertTrue(factory.getBean("employee") instanceof Employee);
assertTrue(factory.isTypeMatch("employee", Employee.class));
assertTrue(factory.getAliases("employee").length > 0);
}
}
@@ -0,0 +1,2 @@
### Relevant Articles:
- [Guide to the Spring BeanFactory](http://www.baeldung.com/spring-beanfactory)
@@ -0,0 +1,39 @@
package com.baeldung.factorybean;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import javax.annotation.Resource;
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(locations = { "classpath:factorybean-abstract-spring-ctx.xml" })
public class AbstractFactoryBeanIntegrationTest {
@Resource(name = "singleTool")
private Tool tool1;
@Resource(name = "singleTool")
private Tool tool2;
@Resource(name = "nonSingleTool")
private Tool tool3;
@Resource(name = "nonSingleTool")
private Tool tool4;
@Test
public void testSingleToolFactory() {
assertThat(tool1.getId(), equalTo(1));
assertTrue(tool1 == tool2);
}
@Test
public void testNonSingleToolFactory() {
assertThat(tool3.getId(), equalTo(2));
assertThat(tool4.getId(), equalTo(2));
assertTrue(tool3 != tool4);
}
}
@@ -0,0 +1,29 @@
package com.baeldung.factorybean;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = FactoryBeanAppConfig.class)
public class FactoryBeanJavaConfigIntegrationTest {
@Autowired
private Tool tool;
@Resource(name = "&tool")
private ToolFactory toolFactory;
@Test
public void testConstructWorkerByJava() {
assertThat(tool.getId(), equalTo(2));
assertThat(toolFactory.getFactoryId(), equalTo(7070));
}
}
@@ -0,0 +1,28 @@
package com.baeldung.factorybean;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:factorybean-spring-ctx.xml" })
public class FactoryBeanXmlConfigIntegrationTest {
@Autowired
private Tool tool;
@Resource(name = "&tool")
private ToolFactory toolFactory;
@Test
public void testConstructWorkerByXml() {
assertThat(tool.getId(), equalTo(1));
assertThat(toolFactory.getFactoryId(), equalTo(9090));
}
}
@@ -0,0 +1,33 @@
package com.baeldung.getbean;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class GetBeanByNameAndTypeUnitTest {
private ApplicationContext context;
@BeforeAll
void setup() {
context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
}
@Test
void whenSpecifiedMatchingNameAndType_thenShouldReturnRelatedBean() {
Lion lion = context.getBean("lion", Lion.class);
assertEquals("Hardcoded lion name", lion.getName());
}
@Test
void whenSpecifiedNotMatchingNameAndType_thenShouldThrowException() {
assertThrows(BeanNotOfRequiredTypeException.class, () -> context.getBean("lion", Tiger.class));
}
}
@@ -0,0 +1,40 @@
package com.baeldung.getbean;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class GetBeanByNameUnitTest {
private ApplicationContext context;
@BeforeAll
void setup() {
context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
}
@Test
void whenGivenExistingBeanName_shouldReturnThatBean() {
Object lion = context.getBean("lion");
assertEquals(lion.getClass(), Lion.class);
}
@Test
void whenGivenNonExistingBeanName_shouldThrowException() {
assertThrows(NoSuchBeanDefinitionException.class, () -> context.getBean("non-existing"));
}
@Test
void whenCastingToWrongType_thenShouldThrowException() {
assertThrows(ClassCastException.class, () -> {
Tiger tiger = (Tiger) context.getBean("lion");
});
}
}
@@ -0,0 +1,44 @@
package com.baeldung.getbean;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.UnsatisfiedDependencyException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class GetBeanByNameWithConstructorParametersUnitTest {
private ApplicationContext context;
@BeforeAll
void setup() {
context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
}
@Test
void whenGivenCorrectName_thenShouldReturnBeanWithSpecifiedName() {
Tiger tiger = (Tiger) context.getBean("tiger", "Siberian");
assertEquals("Siberian", tiger.getName());
}
@Test
void whenGivenCorrectNameOrAlias_shouldReturnBeanWithSpecifiedName() {
Tiger tiger = (Tiger) context.getBean("tiger", "Siberian");
Tiger secondTiger = (Tiger) context.getBean("tiger", "Striped");
assertEquals("Siberian", tiger.getName());
assertEquals("Striped", secondTiger.getName());
}
@Test
void whenNoArgumentSpecifiedForPrototypeBean_thenShouldThrowException() {
assertThrows(UnsatisfiedDependencyException.class, () -> {
Tiger tiger = (Tiger) context.getBean("tiger");
});
}
}
@@ -0,0 +1,33 @@
package com.baeldung.getbean;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class GetBeanByTypeUnitTest {
private ApplicationContext context;
@BeforeAll
void setup() {
context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
}
@Test
void whenGivenExistingUniqueType_thenShouldReturnRelatedBean() {
Lion lion = context.getBean(Lion.class);
assertNotNull(lion);
}
@Test
void whenGivenAmbiguousType_thenShouldThrowException() {
assertThrows(NoUniqueBeanDefinitionException.class, () -> context.getBean(AnnotationConfig.Animal.class));
}
}
@@ -0,0 +1,26 @@
package com.baeldung.getbean;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.jupiter.api.Assertions.assertEquals;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class GetBeanByTypeWithConstructorParametersUnitTest {
private ApplicationContext context;
@BeforeAll
void setup() {
context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
}
@Test
void whenGivenExistingTypeAndValidParameters_thenShouldReturnRelatedBean() {
Tiger tiger = context.getBean(Tiger.class, "Shere Khan");
assertEquals("Shere Khan", tiger.getName());
}
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="employee" class="com.baeldung.beanfactory.Employee">
<constructor-arg name="name" value="Hello! My name is Java"/>
<constructor-arg name="age" value="18"/>
</bean>
<alias name="employee" alias="empalias" />
</beans>