diff --git a/core-java/.gitignore b/core-java/.gitignore index 2a03a0f72e..3de4cc647e 100644 --- a/core-java/.gitignore +++ b/core-java/.gitignore @@ -17,9 +17,10 @@ # Files generated by integration tests *.txt +backup-pom.xml /bin/ /temp #IntelliJ specific -.idea +.idea/ *.iml \ No newline at end of file diff --git a/core-java/pom.xml b/core-java/pom.xml index 1f4804e059..2267dba1e6 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -1,405 +1,404 @@ - 4.0.0 - com.baeldung - core-java - 0.1.0-SNAPSHOT - jar + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + com.baeldung + core-java + 0.1.0-SNAPSHOT + jar - core-java + core-java - + - - - net.sourceforge.collections - collections-generic - ${collections-generic.version} - - - com.google.guava - guava - ${guava.version} - + + + net.sourceforge.collections + collections-generic + ${collections-generic.version} + + + com.google.guava + guava + ${guava.version} + - - org.apache.commons - commons-collections4 - ${commons-collections4.version} - + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + - - commons-io - commons-io - ${commons-io.version} - + + commons-io + commons-io + ${commons-io.version} + - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + - - org.apache.commons - commons-math3 - ${commons-math3.version} - + + org.apache.commons + commons-math3 + ${commons-math3.version} + - - org.decimal4j - decimal4j - ${decimal4j.version} - + + org.decimal4j + decimal4j + ${decimal4j.version} + - - org.bouncycastle - bcprov-jdk15on - ${bouncycastle.version} - + + org.bouncycastle + bcprov-jdk15on + ${bouncycastle.version} + - - org.unix4j - unix4j-command - ${unix4j.version} - + + org.unix4j + unix4j-command + ${unix4j.version} + - - com.googlecode.grep4j - grep4j - ${grep4j.version} - - + + com.googlecode.grep4j + grep4j + ${grep4j.version} + + - + - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + - + - - org.slf4j - slf4j-api - ${org.slf4j.version} - - - ch.qos.logback - logback-classic - ${logback.version} - - - - org.slf4j - jcl-over-slf4j - ${org.slf4j.version} - - - - org.slf4j - log4j-over-slf4j - ${org.slf4j.version} - - - org.projectlombok - lombok - ${lombok.version} - provided - + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + + + org.projectlombok + lombok + ${lombok.version} + provided + - + - - org.hamcrest - hamcrest-all - 1.3 - test - + + org.hamcrest + hamcrest-all + 1.3 + test + - - junit - junit - ${junit.version} - test - + + junit + junit + ${junit.version} + test + - - org.hamcrest - hamcrest-core - ${org.hamcrest.version} - test - - - org.hamcrest - hamcrest-library - ${org.hamcrest.version} - test - + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + - - org.assertj - assertj-core - ${assertj.version} - test - + + org.assertj + assertj-core + ${assertj.version} + test + - - org.mockito - mockito-core - ${mockito.version} - test - - - com.jayway.awaitility - awaitility - ${avaitility.version} - test - + + org.mockito + mockito-core + ${mockito.version} + test + + + com.jayway.awaitility + awaitility + ${avaitility.version} + test + - - commons-codec - commons-codec - ${commons-codec.version} - + + commons-codec + commons-codec + ${commons-codec.version} + - - org.javamoney - moneta - 1.1 - + + org.javamoney + moneta + 1.1 + - - org.owasp.esapi - esapi - 2.1.0.1 - + + org.owasp.esapi + esapi + 2.1.0.1 + - - com.sun.messaging.mq - fscontext - ${fscontext.version} - - + + com.sun.messaging.mq + fscontext + ${fscontext.version} + + - - core-java - - - src/main/resources - true - - + + core-java + + + src/main/resources + true + + - + - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.8 - 1.8 - - + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + - - org.apache.maven.plugins - maven-surefire-plugin - - - **/*IntegrationTest.java - **/*LongRunningUnitTest.java - **/*ManualTest.java - - true - - + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*IntegrationTest.java + **/*LongRunningUnitTest.java + **/*ManualTest.java + + true + + - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + prepare-package + + copy-dependencies + + + ${project.build.directory}/libs + + + + - - org.apache.maven.plugins - maven-jar-plugin - - - - true - libs/ - org.baeldung.executable.ExecutableMavenJar - - - - + + org.apache.maven.plugins + maven-jar-plugin + + + + true + libs/ + org.baeldung.executable.ExecutableMavenJar + + + + - - org.apache.maven.plugins - maven-assembly-plugin - - - package - - single - - - ${project.basedir} - - - org.baeldung.executable.ExecutableMavenJar - - - - jar-with-dependencies - - - - - + + org.apache.maven.plugins + maven-assembly-plugin + + + package + + single + + + ${project.basedir} + + + org.baeldung.executable.ExecutableMavenJar + + + + jar-with-dependencies + + + + + - - org.apache.maven.plugins - maven-shade-plugin - - - - shade - - - true - - - org.baeldung.executable.ExecutableMavenJar - - - - - - + + org.apache.maven.plugins + maven-shade-plugin + + + + shade + + + true + + + org.baeldung.executable.ExecutableMavenJar + + + + + + - - com.jolira - onejar-maven-plugin - - - - org.baeldung.executable.ExecutableMavenJar - true - ${project.build.finalName}-onejar.${project.packaging} - - - one-jar - - - - + + com.jolira + onejar-maven-plugin + + + + org.baeldung.executable.ExecutableMavenJar + true + ${project.build.finalName}-onejar.${project.packaging} + + + one-jar + + + + - - org.springframework.boot - spring-boot-maven-plugin - - - - repackage - - - spring-boot - org.baeldung.executable.ExecutableMavenJar - - - - + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + spring-boot + org.baeldung.executable.ExecutableMavenJar + + + + - + - + - - - integration - - - - org.apache.maven.plugins - maven-surefire-plugin - - - integration-test - - test - - - - **/*ManualTest.java - - - **/*IntegrationTest.java - - - - - - - json - - - - - - - + + + integration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*ManualTest.java + + + **/*IntegrationTest.java + + + + + + + json + + + + + + + - - - 2.8.5 + + + 2.8.5 - - 1.7.21 - 1.1.7 + + 1.7.21 + 1.1.7 - - 21.0 - 3.5 - 1.55 - 1.10 - 3.6.1 - 1.0.3 - 2.5 - 4.1 - 4.01 - 0.4 - 1.8.7 - 1.16.12 - 4.6-b01 + + 21.0 + 3.5 + 1.55 + 1.10 + 3.6.1 + 1.0.3 + 2.5 + 4.1 + 4.01 + 0.4 + 1.8.7 + 1.16.12 + 4.6-b01 - - 1.3 - 4.12 - 1.10.19 - 3.6.1 - 1.7.0 + + 1.3 + 4.12 + 1.10.19 + 3.6.1 + 1.7.0 - - 3.6.0 - 2.19.1 - - + + 3.6.0 + 2.19.1 + \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/string/StringToCharStream.java b/core-java/src/main/java/com/baeldung/string/StringToCharStream.java new file mode 100644 index 0000000000..4dc0455192 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/string/StringToCharStream.java @@ -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 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(); + } + + +} diff --git a/core-java/src/test/java/com/baeldung/string/StringToCharStreamUnitTest.java b/core-java/src/test/java/com/baeldung/string/StringToCharStreamUnitTest.java new file mode 100644 index 0000000000..0c265d0b1f --- /dev/null +++ b/core-java/src/test/java/com/baeldung/string/StringToCharStreamUnitTest.java @@ -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 characterStream = testString.chars().mapToObj(c -> (char) c); + Stream characterStream1 = testString.codePoints().mapToObj(c -> (char) c); + assertNotNull("IntStream returned by chars() did not map to Stream", characterStream); + assertNotNull("IntStream returned by codePoints() did not map to Stream", characterStream1); + } + +} diff --git a/spring-aop/.gitignore b/spring-aop/.gitignore new file mode 100644 index 0000000000..62c893550a --- /dev/null +++ b/spring-aop/.gitignore @@ -0,0 +1 @@ +.idea/ \ No newline at end of file diff --git a/spring-aop/src/main/java/com/baeldung/beaninjection/BeanInjectionApplication.java b/spring-aop/src/main/java/com/baeldung/beaninjection/BeanInjectionApplication.java new file mode 100644 index 0000000000..c8e228c6f2 --- /dev/null +++ b/spring-aop/src/main/java/com/baeldung/beaninjection/BeanInjectionApplication.java @@ -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); + } +} diff --git a/spring-aop/src/main/java/com/baeldung/beaninjection/config/StorageProperties.java b/spring-aop/src/main/java/com/baeldung/beaninjection/config/StorageProperties.java new file mode 100644 index 0000000000..18f7d36378 --- /dev/null +++ b/spring-aop/src/main/java/com/baeldung/beaninjection/config/StorageProperties.java @@ -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; + } + + +} diff --git a/spring-aop/src/main/java/com/baeldung/beaninjection/service/FileSystemStorageService.java b/spring-aop/src/main/java/com/baeldung/beaninjection/service/FileSystemStorageService.java new file mode 100644 index 0000000000..d2530d5a2d --- /dev/null +++ b/spring-aop/src/main/java/com/baeldung/beaninjection/service/FileSystemStorageService.java @@ -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(); + } +} diff --git a/spring-aop/src/main/java/com/baeldung/beaninjection/service/NetworkStorageService.java b/spring-aop/src/main/java/com/baeldung/beaninjection/service/NetworkStorageService.java new file mode 100644 index 0000000000..0720ba153e --- /dev/null +++ b/spring-aop/src/main/java/com/baeldung/beaninjection/service/NetworkStorageService.java @@ -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(); + } + +} diff --git a/spring-aop/src/test/java/com/baeldung/beaninjection/BeanInjectionApplicationTests.java b/spring-aop/src/test/java/com/baeldung/beaninjection/BeanInjectionApplicationTests.java new file mode 100644 index 0000000000..d508975481 --- /dev/null +++ b/spring-aop/src/test/java/com/baeldung/beaninjection/BeanInjectionApplicationTests.java @@ -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() { + } + + +} diff --git a/spring-aop/src/test/java/com/baeldung/beaninjection/BeanInjectionUnitTests.java b/spring-aop/src/test/java/com/baeldung/beaninjection/BeanInjectionUnitTests.java new file mode 100644 index 0000000000..fe1d224bc4 --- /dev/null +++ b/spring-aop/src/test/java/com/baeldung/beaninjection/BeanInjectionUnitTests.java @@ -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()); + } + +}