Stream API: last Stream element example refactor (#1943)
* Refactor * Delete ApplicationContextTestBeanInjectionTypes.java * Delete BeanInjectionJavaConfigIntegrationTest.java * Delete BeanInjectionXMLConfigIntegrationTest.java
This commit is contained in:
parent
b840f15629
commit
a5d5460a9f
@ -5,17 +5,17 @@ import java.util.stream.Stream;
|
|||||||
|
|
||||||
public class StreamApi {
|
public class StreamApi {
|
||||||
|
|
||||||
public String getLastElementUsingReduce(List<String> valueList) {
|
public static String getLastElementUsingReduce(List<String> valueList) {
|
||||||
Stream<String> stream = valueList.stream();
|
Stream<String> stream = valueList.stream();
|
||||||
return stream.reduce((first, second) -> second).orElse(null);
|
return stream.reduce((first, second) -> second).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer getInfiniteStreamLastElementUsingReduce() {
|
public static Integer getInfiniteStreamLastElementUsingReduce() {
|
||||||
Stream<Integer> stream = Stream.iterate(0, i -> i + 1);
|
Stream<Integer> stream = Stream.iterate(0, i -> i + 1);
|
||||||
return stream.limit(20).reduce((first, second) -> second).orElse(null);
|
return stream.limit(20).reduce((first, second) -> second).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLastElementUsingSkip(List<String> valueList) {
|
public static String getLastElementUsingSkip(List<String> valueList) {
|
||||||
long count = valueList.stream().count();
|
long count = valueList.stream().count();
|
||||||
Stream<String> stream = valueList.stream();
|
Stream<String> stream = valueList.stream();
|
||||||
return stream.skip(count - 1).findFirst().orElse(null);
|
return stream.skip(count - 1).findFirst().orElse(null);
|
||||||
|
@ -1,41 +1,40 @@
|
|||||||
package com.baeldung.stream;
|
package com.baeldung.stream;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.Test;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
public class StreamApiTest {
|
public class StreamApiTest {
|
||||||
private StreamApi streamApi = new StreamApi();
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenList_whenGetLastElementUsingReduce_thenReturnLastElement() {
|
public void givenList_whenGetLastElementUsingReduce_thenReturnLastElement() {
|
||||||
List<String> valueList = new ArrayList<String>();
|
List<String> valueList = new ArrayList<>();
|
||||||
valueList.add("Joe");
|
valueList.add("Joe");
|
||||||
valueList.add("John");
|
valueList.add("John");
|
||||||
valueList.add("Sean");
|
valueList.add("Sean");
|
||||||
|
|
||||||
String last = streamApi.getLastElementUsingReduce(valueList);
|
String last = StreamApi.getLastElementUsingReduce(valueList);
|
||||||
|
|
||||||
assertEquals("Sean", last);
|
assertEquals("Sean", last);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenInfiniteStream_whenGetInfiniteStreamLastElementUsingReduce_thenReturnLastElement() {
|
public void givenInfiniteStream_whenGetInfiniteStreamLastElementUsingReduce_thenReturnLastElement() {
|
||||||
Integer last = streamApi.getInfiniteStreamLastElementUsingReduce();
|
int last = StreamApi.getInfiniteStreamLastElementUsingReduce();
|
||||||
assertEquals(new Integer(19), last);
|
assertEquals(19, last);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenListAndCount_whenGetLastElementUsingSkip_thenReturnLastElement() {
|
public void givenListAndCount_whenGetLastElementUsingSkip_thenReturnLastElement() {
|
||||||
List<String> valueList = new ArrayList<String>();
|
List<String> valueList = new ArrayList<>();
|
||||||
valueList.add("Joe");
|
valueList.add("Joe");
|
||||||
valueList.add("John");
|
valueList.add("John");
|
||||||
valueList.add("Sean");
|
valueList.add("Sean");
|
||||||
|
|
||||||
String last = streamApi.getLastElementUsingSkip(valueList);
|
String last = StreamApi.getLastElementUsingSkip(valueList);
|
||||||
|
|
||||||
assertEquals("Sean", last);
|
assertEquals("Sean", last);
|
||||||
}
|
}
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
package com.baeldung.beaninjection;
|
|
||||||
|
|
||||||
public class AnotherSampleDAOBean implements IAnotherSampleDAO {
|
|
||||||
|
|
||||||
private String propertyY;
|
|
||||||
|
|
||||||
public AnotherSampleDAOBean(String propertyY) {
|
|
||||||
this.propertyY = propertyY;
|
|
||||||
}
|
|
||||||
|
|
||||||
// standard setters and getters
|
|
||||||
|
|
||||||
public String getPropertyY() {
|
|
||||||
return propertyY;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,39 +0,0 @@
|
|||||||
package com.baeldung.beaninjection;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class ExampleDAOBean implements IExampleDAO {
|
|
||||||
|
|
||||||
private String propertyX;
|
|
||||||
|
|
||||||
public ExampleDAOBean(String propertyX) {
|
|
||||||
this.propertyX = propertyX;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPropertyX() {
|
|
||||||
return propertyX;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPropertyX(String propertyX) {
|
|
||||||
this.propertyX = propertyX;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<SampleDomainObject> getDomainList() {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public SampleDomainObject createNewDomain(SampleDomainObject inputDomain) {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public SampleDomainObject getSomeDomain() {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
return new SampleDomainObject();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,47 +0,0 @@
|
|||||||
package com.baeldung.beaninjection;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class ExampleServiceBean implements IExampleService {
|
|
||||||
|
|
||||||
private IExampleDAO exampleDAO;
|
|
||||||
private IAnotherSampleDAO anotherSampleDAO;
|
|
||||||
|
|
||||||
public ExampleServiceBean(IExampleDAO exampleDAO) {
|
|
||||||
this.exampleDAO = exampleDAO;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAnotherSampleDAO(IAnotherSampleDAO anotherSampleDAO) {
|
|
||||||
this.anotherSampleDAO = anotherSampleDAO;
|
|
||||||
}
|
|
||||||
|
|
||||||
// standard setters and getters
|
|
||||||
|
|
||||||
public IAnotherSampleDAO getAnotherSampleDAO() {
|
|
||||||
return anotherSampleDAO;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setExampleDAO(ExampleDAOBean exampleDAO) {
|
|
||||||
this.exampleDAO = exampleDAO;
|
|
||||||
}
|
|
||||||
|
|
||||||
public IExampleDAO getExampleDAO() {
|
|
||||||
return exampleDAO;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String propertyX;
|
|
||||||
|
|
||||||
public String getPropertyX() {
|
|
||||||
return propertyX;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPropertyX(String propertyX) {
|
|
||||||
this.propertyX = propertyX;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<SampleDomainObject> serviceMethodX() {
|
|
||||||
/*get domain list from DAO .. business logic on domain objects..return*/
|
|
||||||
return exampleDAO.getDomainList();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
package com.baeldung.beaninjection;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class FileReader {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private Location location;
|
|
||||||
|
|
||||||
public Location getLocation() {
|
|
||||||
return location;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLocation(Location location) {
|
|
||||||
this.location = location;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
package com.baeldung.beaninjection;
|
|
||||||
|
|
||||||
public class FtpReader {
|
|
||||||
private String ftpHost = null;
|
|
||||||
private Integer ftpPort = null;
|
|
||||||
|
|
||||||
// Constructor with arguments
|
|
||||||
public FtpReader(String host, Integer port) {
|
|
||||||
this.ftpHost = host;
|
|
||||||
this.ftpPort = port;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getFtpHost() {
|
|
||||||
return ftpHost;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFtpHost(String ftpHost) {
|
|
||||||
this.ftpHost = ftpHost;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer getFtpPort() {
|
|
||||||
return ftpPort;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFtpPort(Integer ftpPort) {
|
|
||||||
this.ftpPort = ftpPort;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
package com.baeldung.beaninjection;
|
|
||||||
|
|
||||||
public interface IAnotherSampleDAO {
|
|
||||||
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
package com.baeldung.beaninjection;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public interface IExampleDAO {
|
|
||||||
|
|
||||||
List<SampleDomainObject> getDomainList();
|
|
||||||
|
|
||||||
SampleDomainObject createNewDomain(SampleDomainObject domainObject);
|
|
||||||
|
|
||||||
SampleDomainObject getSomeDomain();
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
package com.baeldung.beaninjection;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public interface IExampleService {
|
|
||||||
|
|
||||||
List<SampleDomainObject> serviceMethodX();
|
|
||||||
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
package com.baeldung.beaninjection;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class Location {
|
|
||||||
private String filePath;
|
|
||||||
|
|
||||||
public String getFilePath() {
|
|
||||||
return filePath;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFilePath(String filePath) {
|
|
||||||
this.filePath = filePath;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
package com.baeldung.beaninjection;
|
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@ComponentScan(basePackages = { "com.baeldung.beaninjection" })
|
|
||||||
public class ReaderApplicationConfig {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public FtpReader exampleDAO() {
|
|
||||||
return new FtpReader("localhost", 21);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,33 +0,0 @@
|
|||||||
package com.baeldung.beaninjection;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class ReaderManager {
|
|
||||||
private FileReader fileReader;
|
|
||||||
private FtpReader ftpReader;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public ReaderManager(FtpReader ftpReader) {
|
|
||||||
this.ftpReader = ftpReader;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public void setFileReader(FileReader fileReader) {
|
|
||||||
this.fileReader = fileReader;
|
|
||||||
}
|
|
||||||
|
|
||||||
public FileReader getFileReader() {
|
|
||||||
return fileReader;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFtpReader(FtpReader ftpReader) {
|
|
||||||
this.ftpReader = ftpReader;
|
|
||||||
}
|
|
||||||
|
|
||||||
public FtpReader getFtpReader() {
|
|
||||||
return ftpReader;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
package com.baeldung.beaninjection;
|
|
||||||
|
|
||||||
public class ReaderService {
|
|
||||||
private FtpReader ftpReader;
|
|
||||||
|
|
||||||
public void setFtpReader(FtpReader reader) {
|
|
||||||
this.ftpReader = reader;
|
|
||||||
}
|
|
||||||
|
|
||||||
public FtpReader getFtpReader() {
|
|
||||||
return ftpReader;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,31 +0,0 @@
|
|||||||
package com.baeldung.beaninjection;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
public class SampleDomainObject implements Serializable {
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private static final long serialVersionUID = 449859763481296747L;
|
|
||||||
|
|
||||||
private String domainPropX;
|
|
||||||
private String domainPropY;
|
|
||||||
|
|
||||||
public String getDomainPropX() {
|
|
||||||
return domainPropX;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDomainPropX(String domainPropX) {
|
|
||||||
this.domainPropX = domainPropX;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDomainPropY() {
|
|
||||||
return domainPropY;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDomainPropY(String domainPropY) {
|
|
||||||
this.domainPropY = domainPropY;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,36 +0,0 @@
|
|||||||
package com.baeldung.configuration;
|
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
import com.baeldung.beaninjection.AnotherSampleDAOBean;
|
|
||||||
import com.baeldung.beaninjection.ExampleDAOBean;
|
|
||||||
import com.baeldung.beaninjection.ExampleServiceBean;
|
|
||||||
import com.baeldung.beaninjection.IAnotherSampleDAO;
|
|
||||||
import com.baeldung.beaninjection.IExampleDAO;
|
|
||||||
import com.baeldung.beaninjection.IExampleService;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@ComponentScan(basePackages = { "com.baeldung.beaninjection" })
|
|
||||||
public class ApplicationContextTestBeanInjectionTypes {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IExampleDAO exampleDAO() {
|
|
||||||
return new ExampleDAOBean("Mandatory DAO Property X");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IExampleService exampleServiceBean() {
|
|
||||||
ExampleServiceBean serviceBean = new ExampleServiceBean(exampleDAO());
|
|
||||||
serviceBean.setAnotherSampleDAO(anotherSampleDAO());
|
|
||||||
serviceBean.setPropertyX("Some Service Property X");
|
|
||||||
return serviceBean;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IAnotherSampleDAO anotherSampleDAO() {
|
|
||||||
return new AnotherSampleDAOBean("Mandatory DAO Property Y");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,24 +0,0 @@
|
|||||||
package com.baeldung.beaninjection;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
|
|
||||||
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(classes = ReaderApplicationConfig.class)
|
|
||||||
public class FileReaderTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ApplicationContext context;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testAutowiredAnnotation_WhenField_ThenInjected() {
|
|
||||||
FileReader service = context.getBean(FileReader.class);
|
|
||||||
assertNotNull(service.getLocation());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
package com.baeldung.beaninjection;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
|
|
||||||
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(locations = "classpath:injectiontypes.xml")
|
|
||||||
public class FtpReaderTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ApplicationContext context;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testXMLBeanConfig_WhenConstructorArguments_ThenInjected() {
|
|
||||||
FtpReader service = context.getBean(FtpReader.class);
|
|
||||||
assertNotNull(service.getFtpHost());
|
|
||||||
assertNotNull(service.getFtpPort());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
package com.baeldung.beaninjection;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
|
|
||||||
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(classes = ReaderApplicationConfig.class)
|
|
||||||
public class ReaderManagerTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ApplicationContext context;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testAutowiredAnnotation_WhenConstructorAndSetter_ThenInjected() {
|
|
||||||
ReaderManager service = context.getBean(ReaderManager.class);
|
|
||||||
assertNotNull(service.getFtpReader());
|
|
||||||
assertNotNull(service.getFileReader());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,24 +0,0 @@
|
|||||||
package com.baeldung.beaninjection;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
|
|
||||||
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(locations = "classpath:injectiontypes.xml")
|
|
||||||
public class ReaderServiceTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ApplicationContext context;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testXMLBeanConfig_WhenSetter_ThenInjected() {
|
|
||||||
ReaderService service = context.getBean(ReaderService.class);
|
|
||||||
assertNotNull(service.getFtpReader());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,50 +0,0 @@
|
|||||||
package com.baeldung.test.beaninjection;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.beans.BeansException;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.context.ApplicationContextAware;
|
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|
||||||
|
|
||||||
import com.baeldung.beaninjection.AnotherSampleDAOBean;
|
|
||||||
import com.baeldung.beaninjection.ExampleDAOBean;
|
|
||||||
import com.baeldung.beaninjection.ExampleServiceBean;
|
|
||||||
import com.baeldung.configuration.ApplicationContextTestBeanInjectionTypes;
|
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
|
||||||
@ContextConfiguration(classes = ApplicationContextTestBeanInjectionTypes.class)
|
|
||||||
public class BeanInjectionJavaConfigIntegrationTest implements ApplicationContextAware {
|
|
||||||
|
|
||||||
private ApplicationContext beanInjectedContext;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDAOInjectionByJava() {
|
|
||||||
ExampleServiceBean serviceBean = beanInjectedContext.getBean(ExampleServiceBean.class);
|
|
||||||
assertNotNull("Failed: Constructor Injection,Bean Reference Injection,Java Config, ExampleServiceBean", serviceBean.getExampleDAO());
|
|
||||||
assertNotNull("Failed: Constructor Injection,Bean Reference Injection,Java Config, ExampleServiceBean", serviceBean.getAnotherSampleDAO());
|
|
||||||
assertTrue("Failed: Constructor Injection,String Property , Java Config", serviceBean.getPropertyX()
|
|
||||||
.equals("Some Service Property X"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testPropertyInjectioninDAOByJava() {
|
|
||||||
ExampleDAOBean daoBean = beanInjectedContext.getBean(ExampleDAOBean.class);
|
|
||||||
assertTrue("Failed: Constructor Injection,String Property , Java Config", daoBean.getPropertyX()
|
|
||||||
.equals("Mandatory DAO Property X"));
|
|
||||||
|
|
||||||
AnotherSampleDAOBean anotherDAOBean = beanInjectedContext.getBean(AnotherSampleDAOBean.class);
|
|
||||||
assertTrue("Failed: Constructor Injection,String Property , XML Config", anotherDAOBean.getPropertyY()
|
|
||||||
.equals("Mandatory DAO Property Y"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
this.beanInjectedContext = applicationContext;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
package com.baeldung.test.beaninjection;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.beans.BeansException;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.context.ApplicationContextAware;
|
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|
||||||
|
|
||||||
import com.baeldung.beaninjection.AnotherSampleDAOBean;
|
|
||||||
import com.baeldung.beaninjection.ExampleDAOBean;
|
|
||||||
import com.baeldung.beaninjection.ExampleServiceBean;
|
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
|
||||||
@ContextConfiguration(locations = "classpath:beaninjectiontypes-context.xml")
|
|
||||||
public class BeanInjectionXMLConfigIntegrationTest implements ApplicationContextAware {
|
|
||||||
|
|
||||||
private ApplicationContext beanInjectedContext;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDAOInjectionByXML() {
|
|
||||||
ExampleServiceBean serviceBean = beanInjectedContext.getBean(ExampleServiceBean.class);
|
|
||||||
assertNotNull("Failed: Constructor Injection,Bean Reference Injection,XML Config, ExampleServiceBean", serviceBean.getExampleDAO());
|
|
||||||
assertNotNull("Failed: Constructor Injection,Bean Reference Injection,XML Config, ExampleServiceBean", serviceBean.getAnotherSampleDAO());
|
|
||||||
assertTrue("Failed: Constructor Injection,String Property , XML Config", serviceBean.getPropertyX()
|
|
||||||
.equals("Some Service Property X"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testPropertyInjectioninDAOByXML() {
|
|
||||||
ExampleDAOBean daoBean = beanInjectedContext.getBean(ExampleDAOBean.class);
|
|
||||||
assertTrue("Failed: Constructor Injection,String Property , XML Config", daoBean.getPropertyX()
|
|
||||||
.equals("Mandatory DAO Property X"));
|
|
||||||
|
|
||||||
AnotherSampleDAOBean anotherDAOBean = beanInjectedContext.getBean(AnotherSampleDAOBean.class);
|
|
||||||
assertTrue("Failed: Constructor Injection,String Property , XML Config", anotherDAOBean.getPropertyY()
|
|
||||||
.equals("Mandatory DAO Property Y"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
this.beanInjectedContext = applicationContext;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user