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:
parent
f18c993d71
commit
920167f5b7
|
@ -17,9 +17,10 @@
|
|||
|
||||
# Files generated by integration tests
|
||||
*.txt
|
||||
backup-pom.xml
|
||||
/bin/
|
||||
/temp
|
||||
|
||||
#IntelliJ specific
|
||||
.idea
|
||||
.idea/
|
||||
*.iml
|
|
@ -401,5 +401,4 @@
|
|||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.string;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Created by smatt on 26/05/2017.
|
||||
*/
|
||||
public class StringToCharStream {
|
||||
|
||||
public StringToCharStream() {
|
||||
|
||||
//let's use the Stream API to manipulate a string
|
||||
//this will count the occurrence of each character in the test string
|
||||
|
||||
System.out.println("Counting Occurrence of Letter");
|
||||
String testString = "Noww";
|
||||
|
||||
//we don't want to use foreach, so . . .
|
||||
|
||||
Map<Character, Integer> map = new HashMap<>();
|
||||
|
||||
testString.codePoints()
|
||||
.mapToObj(c -> (char) c)
|
||||
.filter(c -> Character.isLetter(c))
|
||||
.forEach(c -> {
|
||||
if(map.containsKey(c)) {
|
||||
map.put(c, map.get(c) + 1);
|
||||
} else {
|
||||
map.put(c, 1);
|
||||
}
|
||||
});
|
||||
|
||||
//printing out the result here
|
||||
System.out.println(map.toString());
|
||||
}
|
||||
|
||||
|
||||
public static void main(String [] args) {
|
||||
new StringToCharStream();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.string;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Created by smatt on 09/06/2017.
|
||||
*/
|
||||
public class StringToCharStreamUnitTest {
|
||||
|
||||
String testString = "Tests";
|
||||
|
||||
@Test
|
||||
public void givenTestString_whenChars_thenReturnIntStream() {
|
||||
assertTrue(testString.chars() instanceof IntStream);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestString_whenCodePoints_thenReturnIntStream() {
|
||||
assertTrue(testString.codePoints() instanceof IntStream);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntStream_whenMapToObj_thenReturnCharacterStream() {
|
||||
Stream<Character> characterStream = testString.chars().mapToObj(c -> (char) c);
|
||||
Stream<Character> characterStream1 = testString.codePoints().mapToObj(c -> (char) c);
|
||||
assertNotNull("IntStream returned by chars() did not map to Stream<Character>", characterStream);
|
||||
assertNotNull("IntStream returned by codePoints() did not map to Stream<Character>", characterStream1);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
.idea/
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue