simplify demos
This commit is contained in:
@@ -4,32 +4,36 @@ import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
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 AbstractFactoryBeanTest {
|
||||
|
||||
@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() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:factorybean-abstract-spring-ctx.xml");
|
||||
|
||||
Worker worker1 = (Worker) context.getBean("worker1");
|
||||
Worker worker2 = (Worker) context.getBean("worker2");
|
||||
|
||||
assertThat(worker1.getNumber(), equalTo("50001"));
|
||||
assertThat(worker2.getNumber(), equalTo("50002"));
|
||||
assertTrue(worker1.getTool() == worker2.getTool());
|
||||
assertThat(tool1.getId(), equalTo(1));
|
||||
assertTrue(tool1 == tool2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonSingleToolFactory() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:factorybean-abstract-spring-ctx.xml");
|
||||
|
||||
Worker worker3 = (Worker) context.getBean("worker3");
|
||||
Worker worker4 = (Worker) context.getBean("worker4");
|
||||
|
||||
assertThat(worker3.getNumber(), equalTo("50003"));
|
||||
assertThat(worker4.getNumber(), equalTo("50004"));
|
||||
assertTrue(worker3.getTool() != worker4.getTool());
|
||||
assertThat(tool3.getId(), equalTo(2));
|
||||
assertThat(tool4.getId(), equalTo(2));
|
||||
assertTrue(tool3 != tool4);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.factorybean;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class FactoryBeanInitializeTest {
|
||||
@Test(expected = BeanCreationException.class)
|
||||
public void testInitializationToolFactory() {
|
||||
new ClassPathXmlApplicationContext("classpath:factorybean-init-spring-ctx.xml");
|
||||
}
|
||||
|
||||
@Test(expected = BeanCreationException.class)
|
||||
public void testPostConstructToolFactory() {
|
||||
new ClassPathXmlApplicationContext("classpath:factorybean-postconstruct-spring-ctx.xml");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
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.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = FactoryBeanAppConfig.class)
|
||||
public class FactoryBeanJavaConfigTest {
|
||||
|
||||
@Resource
|
||||
private Tool tool;
|
||||
@Resource(name = "&toolFactory")
|
||||
private ToolFactory toolFactory;
|
||||
|
||||
@Test
|
||||
public void testConstructWorkerByJava() {
|
||||
assertThat(tool.getId(), equalTo(2));
|
||||
assertThat(toolFactory.getFactoryId(), equalTo(7070));
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package com.baeldung.factorybean;
|
||||
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class FactoryBeanTest {
|
||||
@Test
|
||||
public void testConstructWorkerByXml() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:factorybean-spring-ctx.xml");
|
||||
|
||||
Worker worker = (Worker) context.getBean("worker");
|
||||
assertThat(worker.getNumber(), equalTo("1001"));
|
||||
assertThat(worker.getTool().getId(), equalTo(1));
|
||||
assertThat(worker.getTool().getName(), equalTo("screwdriver"));
|
||||
assertThat(worker.getTool().getPrice(), equalTo(1.5));
|
||||
|
||||
ToolFactory toolFactory = (ToolFactory) context.getBean("&tool");
|
||||
assertThat(toolFactory.getFactoryId(), equalTo(9090));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructWorkerByJava() {
|
||||
ApplicationContext context = new AnnotationConfigApplicationContext(FactoryBeanAppConfig.class);
|
||||
|
||||
Worker worker = (Worker) context.getBean("worker");
|
||||
assertThat(worker.getNumber(), equalTo("1002"));
|
||||
assertThat(worker.getTool().getId(), equalTo(2));
|
||||
assertThat(worker.getTool().getName(), equalTo("wrench"));
|
||||
assertThat(worker.getTool().getPrice(), equalTo(3.7));
|
||||
|
||||
ToolFactory toolFactory = (ToolFactory) context.getBean("&tool");
|
||||
assertThat(toolFactory.getFactoryId(), equalTo(7070));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
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.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "classpath:factorybean-spring-ctx.xml" })
|
||||
public class FactoryBeanXmlConfigTest {
|
||||
|
||||
@Resource
|
||||
private Tool tool;
|
||||
@Resource(name = "&tool")
|
||||
private ToolFactory toolFactory;
|
||||
|
||||
@Test
|
||||
public void testConstructWorkerByXml() {
|
||||
assertThat(tool.getId(), equalTo(1));
|
||||
assertThat(toolFactory.getFactoryId(), equalTo(9090));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user