BAEL-865 - updated example code (#2029)
* added project files for evaluation article by smatt382@gmail.com * added project files for evaluation article by smatt382@gmail.com * added class com.baeldung.string.StringToCharStream by smatt382@gmail.com * updated the example codes. Use assert statements * updated example codes * fixed conflict in pom.xml * remove redundant files' * added unit test
This commit is contained in:
committed by
Zeger Hendrikse
parent
f18c993d71
commit
920167f5b7
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.beaninjection;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class BeanInjectionApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BeanInjectionApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.beaninjection.config;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Created by smatt on 12/05/2017.
|
||||
*/
|
||||
@Configuration
|
||||
public class StorageProperties {
|
||||
/**
|
||||
* Folder location for storing files
|
||||
*/
|
||||
|
||||
Logger logger = Logger.getLogger(StorageProperties.class);
|
||||
|
||||
private String location = "upload-dir";
|
||||
|
||||
public StorageProperties() {}
|
||||
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public void setLocation(String location) {
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.beaninjection.service;
|
||||
|
||||
import com.baeldung.beaninjection.config.StorageProperties;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* Created by smatt on 12/05/2017.
|
||||
*/
|
||||
@Service
|
||||
public class FileSystemStorageService {
|
||||
|
||||
StorageProperties storageProperties;
|
||||
|
||||
@Autowired
|
||||
public FileSystemStorageService(StorageProperties storageProperties) {
|
||||
this.storageProperties = storageProperties;
|
||||
}
|
||||
|
||||
//this is for test purpose
|
||||
public StorageProperties getStorageProperties() {
|
||||
return storageProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "FileSystemStorageService: Storage Location = " + storageProperties.getLocation();
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.beaninjection.service;
|
||||
|
||||
import com.baeldung.beaninjection.config.StorageProperties;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* Created by smatt on 12/05/2017.
|
||||
*/
|
||||
@Service
|
||||
public class NetworkStorageService {
|
||||
|
||||
StorageProperties storageProperties;
|
||||
|
||||
public NetworkStorageService() {}
|
||||
|
||||
@Autowired
|
||||
public void setStorageProperties(StorageProperties storageProperties) {
|
||||
this.storageProperties = storageProperties;
|
||||
}
|
||||
|
||||
public StorageProperties getStorageProperties() {
|
||||
return storageProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "NetworkStorageService: Storage Location = " + storageProperties.getLocation();
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.beaninjection;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class BeanInjectionApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.beaninjection;
|
||||
|
||||
import com.baeldung.beaninjection.service.FileSystemStorageService;
|
||||
import com.baeldung.beaninjection.service.NetworkStorageService;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* Created by smatt on 13/05/2017.
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class BeanInjectionUnitTests {
|
||||
|
||||
@Autowired
|
||||
NetworkStorageService networkStorageService;
|
||||
|
||||
@Autowired
|
||||
FileSystemStorageService fileSystemStorageService;
|
||||
|
||||
Logger logger = Logger.getLogger(BeanInjectionApplicationTests.class);
|
||||
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAutowiredOnClassConstructor_whenInstantiatingAndCallingGetter_thenDependencyShouldResolve() {
|
||||
Assert.assertNotNull("FileSystemStorageService not autowired in test class", fileSystemStorageService);
|
||||
Assert.assertNotNull("StorageProperties not autowired in FileSystemStorageService", fileSystemStorageService.getStorageProperties());
|
||||
logger.info(fileSystemStorageService.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAutowiredOnClassSetter_whenInstantiatingAndCallingGetter_thenDependencyShouldResolve() {
|
||||
Assert.assertNotNull("NetworkStorageService not autowired in test class", networkStorageService);
|
||||
Assert.assertNotNull("StorageProperties not autowired in NetworkStorageService", networkStorageService.getStorageProperties());
|
||||
logger.info(networkStorageService.toString());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user