diff --git a/.travis.yml b/.travis.yml index 6063fbf3e5..120d365569 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,9 @@ language: java -install: travis_wait 40 mvn -q clean install -Dgib.enabled=true +install: travis_wait 60 mvn -q clean install + +before_script: + - echo "MAVEN_OPTS='-Xmx2048M -Xss128M -XX:MaxPermSize=2048M -XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC -XX:+TieredCompilation -XX:TieredStopAtLevel=1 -XX:-UseGCOverheadLimit'" > ~/.mavenrc jdk: - oraclejdk8 @@ -14,10 +17,4 @@ cache: directories: - .autoconf - $HOME/.m2 - - sudo: required - - env: - global: - JAVA_OPTS="-Xmx2048M -Xss128M -XX:MaxPermSize=2048M -XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC" - MAVEN_OPTS="-Xmx2048M -Xss128M -XX:MaxPermSize=2048M -XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC" + diff --git a/algorithms/src/main/java/com/baeldung/automata/RtState.java b/algorithms/src/main/java/com/baeldung/automata/RtState.java index ba785eeff0..b4a5df7961 100644 --- a/algorithms/src/main/java/com/baeldung/automata/RtState.java +++ b/algorithms/src/main/java/com/baeldung/automata/RtState.java @@ -21,12 +21,12 @@ public final class RtState implements State { } public State transit(final CharSequence c) { - for(final Transition t : this.transitions) { - if(t.isPossible(c)) { - return t.state(); - } - } - throw new IllegalArgumentException("Input not accepted: " + c); + return transitions + .stream() + .filter(t -> t.isPossible(c)) + .map(Transition::state) + .findAny() + .orElseThrow(() -> new IllegalArgumentException("Input not accepted: " + c)); } public boolean isFinal() { diff --git a/apache-poi/temp.xlsx b/apache-poi/temp.xlsx index 50307a28c2..cbea3a410d 100644 Binary files a/apache-poi/temp.xlsx and b/apache-poi/temp.xlsx differ diff --git a/core-java/pom.xml b/core-java/pom.xml index 225eb3fc29..abf22d05e6 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -250,6 +250,7 @@ single + ${project.basedir} org.baeldung.executable.ExecutableMavenJar diff --git a/disruptor/pom.xml b/disruptor/pom.xml index 7f2c78c9b0..2523cc2125 100644 --- a/disruptor/pom.xml +++ b/disruptor/pom.xml @@ -145,6 +145,7 @@ single + ${project.basedir} org.baeldung.executable.ExecutableMavenJar diff --git a/guava21/README.md b/guava21/README.md new file mode 100644 index 0000000000..ff12555376 --- /dev/null +++ b/guava21/README.md @@ -0,0 +1 @@ +## Relevant articles: diff --git a/guava21/pom.xml b/guava21/pom.xml new file mode 100644 index 0000000000..f393c65aec --- /dev/null +++ b/guava21/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + com.baeldung.guava + tutorial + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + + com.google.guava + guava + 21.0 + + + + + junit + junit + 4.11 + + + + + + \ No newline at end of file diff --git a/guava21/src/main/java/com/baeldung/guava/tutorial/ComparatorsExamples.java b/guava21/src/main/java/com/baeldung/guava/tutorial/ComparatorsExamples.java new file mode 100644 index 0000000000..6eb5c7f5ba --- /dev/null +++ b/guava21/src/main/java/com/baeldung/guava/tutorial/ComparatorsExamples.java @@ -0,0 +1,27 @@ +package com.baeldung.guava.tutorial; + +import com.google.common.collect.Comparators; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; + +public class ComparatorsExamples { + + public static void main(String[] args){ + + List integers = Arrays.asList(1,2,3,4,4,6,7,8,9,10); + //This will return true + boolean isInAscendingOrder = Comparators.isInOrder(integers, new AscedingOrderComparator()); + + System.out.println(isInAscendingOrder); + + } + + private static class AscedingOrderComparator implements Comparator { + @Override + public int compare(Integer o1, Integer o2) { + return o1.compareTo(o2); + } + } +} diff --git a/guava21/src/main/java/com/baeldung/guava/tutorial/ConcatStreams.java b/guava21/src/main/java/com/baeldung/guava/tutorial/ConcatStreams.java new file mode 100644 index 0000000000..ab95b85751 --- /dev/null +++ b/guava21/src/main/java/com/baeldung/guava/tutorial/ConcatStreams.java @@ -0,0 +1,11 @@ +package com.baeldung.guava.tutorial; + +import com.google.common.collect.Streams; + +import java.util.stream.Stream; + +public class ConcatStreams { + public static Stream concatStreams(Stream stream1, Stream stream2, Stream stream3){ + return Streams.concat(stream1,stream2,stream3); + } +} diff --git a/guava21/src/main/java/com/baeldung/guava/tutorial/InternerBuilderExample.java b/guava21/src/main/java/com/baeldung/guava/tutorial/InternerBuilderExample.java new file mode 100644 index 0000000000..6b935ba2a8 --- /dev/null +++ b/guava21/src/main/java/com/baeldung/guava/tutorial/InternerBuilderExample.java @@ -0,0 +1,19 @@ +package com.baeldung.guava.tutorial; + +import com.google.common.collect.Interner; +import com.google.common.collect.Interners; + +import static com.google.common.collect.Interners.newBuilder; + +public class InternerBuilderExample { + + public static void main(String[] args){ + Interner interners = Interners.newBuilder() + .concurrencyLevel(2) + .strong() + .build(); + + + } + +} diff --git a/guava21/src/main/java/com/baeldung/guava/tutorial/MoreCollectorsExample.java b/guava21/src/main/java/com/baeldung/guava/tutorial/MoreCollectorsExample.java new file mode 100644 index 0000000000..6cf4b6b0ac --- /dev/null +++ b/guava21/src/main/java/com/baeldung/guava/tutorial/MoreCollectorsExample.java @@ -0,0 +1,17 @@ +package com.baeldung.guava.tutorial; + +import com.google.common.collect.MoreCollectors; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +public class MoreCollectorsExample { + + public static void main(String[] args) { + List numbers = Arrays.asList(1); + Optional number = numbers.stream() + .map(e -> e * 2) + .collect(MoreCollectors.toOptional()); + } +} diff --git a/guava21/src/main/java/com/baeldung/guava/tutorial/StreamsUtility.java b/guava21/src/main/java/com/baeldung/guava/tutorial/StreamsUtility.java new file mode 100644 index 0000000000..4ec3b44ef4 --- /dev/null +++ b/guava21/src/main/java/com/baeldung/guava/tutorial/StreamsUtility.java @@ -0,0 +1,42 @@ +package com.baeldung.guava.tutorial; + +import com.google.common.collect.Streams; + +import java.util.*; +import java.util.stream.DoubleStream; +import java.util.stream.IntStream; +import java.util.stream.LongStream; +import java.util.stream.Stream; + +public class StreamsUtility { + + public static void main(String[] args){ + + List numbers = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,11,12,13,14,15,16,17,18,19,20); + //Using Collection + Stream streamFromCollection = Streams.stream(numbers); + //Using Iterator + Stream streamFromIterator = Streams.stream(numbers.iterator()); + //Using Iterable + Stream streamFromIterable = Streams.stream((Iterable) numbers); + //Using Optional + Stream streamFromOptional = Streams.stream(Optional.of(1)); + //Using OptionalLong to LongStream + LongStream streamFromOptionalLong = Streams.stream(OptionalLong.of(1)); + //Using OptionalInt to IntStream + IntStream streamFromOptionalInt = Streams.stream(OptionalInt.of(1)); + //Using OptionalDouble to DoubleStream + DoubleStream streamFromOptionalDouble = Streams.stream(OptionalDouble.of(1.0)); + + Stream concatenatedStreams = Streams.concat(streamFromCollection,streamFromIterable,streamFromIterator); + + List integers = Arrays.asList(1,2,3,4,5,6,7,8,9,10); + //This will return 10 + Optional lastItem = Streams.findLast(integers.stream()); + + Streams.zip( + Stream.of("candy", "chocolate", "bar"), + Stream.of("$1", "$2","$3"), + (arg1, arg2) -> arg1 + ":" + arg2); + } +} diff --git a/guava21/src/test/java/ComparatorsUnitTests.java b/guava21/src/test/java/ComparatorsUnitTests.java new file mode 100644 index 0000000000..f196c41a1b --- /dev/null +++ b/guava21/src/test/java/ComparatorsUnitTests.java @@ -0,0 +1,76 @@ +import com.google.common.collect.Comparators; +import org.junit.Assert; +import org.junit.Test; + +import java.util.*; +import java.util.function.Function; +import java.util.function.ToDoubleFunction; +import java.util.function.ToIntFunction; +import java.util.function.ToLongFunction; + +public class ComparatorsUnitTests { + + @Test + public void isInOrderTest(){ + + List numbers = Arrays.asList(1,2,3,4,4,6,7,8,9,10); + + boolean isInAscendingOrder = Comparators.isInOrder(numbers, new AscendingOrderComparator()); + + Assert.assertTrue(isInAscendingOrder); + } + + @Test + public void isInStrictOrderTest(){ + + List numbers = Arrays.asList(1,2,3,4,3,6,7,8,9,10); + + boolean isInAscendingOrder = Comparators.isInOrder(numbers, new AscendingOrderComparator()); + + Assert.assertFalse(isInAscendingOrder); + } + + + private class AscendingOrderComparator implements Comparator{ + + @Override + public int compare(Integer o1, Integer o2) { + return o1.compareTo(o2); + } + + @Override + public Comparator reversed() { + return null; + } + + @Override + public Comparator thenComparing(Comparator other) { + return null; + } + + @Override + public Comparator thenComparing(Function keyExtractor, Comparator keyComparator) { + return null; + } + + @Override + public > Comparator thenComparing(Function keyExtractor) { + return null; + } + + @Override + public Comparator thenComparingInt(ToIntFunction keyExtractor) { + return null; + } + + @Override + public Comparator thenComparingLong(ToLongFunction keyExtractor) { + return null; + } + + @Override + public Comparator thenComparingDouble(ToDoubleFunction keyExtractor) { + return null; + } + } +} diff --git a/guava21/src/test/java/GauavaStreamsTests.java b/guava21/src/test/java/GauavaStreamsTests.java new file mode 100644 index 0000000000..09e3e29b47 --- /dev/null +++ b/guava21/src/test/java/GauavaStreamsTests.java @@ -0,0 +1,155 @@ +import com.google.common.collect.Streams; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.*; +import java.util.stream.DoubleStream; +import java.util.stream.IntStream; +import java.util.stream.LongStream; +import java.util.stream.Stream; + +public class GauavaStreamsTests { + + List numbers; + + @Before + public void setUp(){ + numbers = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,11,12,13,14,15,16,17,18,19,20); + } + + + @Test + public void createStreamsWithCollection(){ + //Deprecated API to create stream from collection + Stream streamFromCollection = Streams.stream(numbers); + + //Assert.assertNotNull(streamFromCollection); + StreamUtility.assertStreamEquals(streamFromCollection, numbers.stream()); + } + + @Test + public void createStreamsWithIterable(){ + Iterable numbersIterable = (Iterable) numbers; + + Stream streamFromIterable = Streams.stream(numbersIterable); + + Assert.assertNotNull(streamFromIterable); + StreamUtility.assertStreamEquals(streamFromIterable, numbers.stream()); + } + + @Test + public void createStreamsWithIterator(){ + Iterator numbersIterator = numbers.iterator(); + + Stream streamFromIterator = Streams.stream(numbersIterator); + + Assert.assertNotNull(streamFromIterator); + StreamUtility.assertStreamEquals(streamFromIterator, numbers.stream()); + } + + @Test + public void createStreamsWithOptional(){ + + Stream streamFromOptional = Streams.stream(Optional.of(1)); + + Assert.assertNotNull(streamFromOptional); + Assert.assertEquals(streamFromOptional.count(), 1); + } + + @Test + public void createStreamsWithOptionalLong(){ + + LongStream streamFromOptionalLong = Streams.stream(OptionalLong.of(1)); + + Assert.assertNotNull(streamFromOptionalLong); + Assert.assertEquals(streamFromOptionalLong.count(), 1); + } + + @Test + public void createStreamsWithOptionalInt(){ + + IntStream streamFromOptionalInt = Streams.stream(OptionalInt.of(1)); + + //Assert.assertNotNull(streamFromOptionalInt); + Assert.assertEquals(streamFromOptionalInt.count(), 1); + } + + @Test + public void createStreamsWithOptionalDouble(){ + + DoubleStream streamFromOptionalDouble = Streams.stream(OptionalDouble.of(1.0)); + + //Assert.assertNotNull(streamFromOptionalDouble); + Assert.assertEquals(streamFromOptionalDouble.count(), 1); + + } + + @Test + public void concatStreamsOfSameType(){ + Stream oddNumbers = Arrays.asList(1,3,5,7,9,11,13,15,17,19).stream(); + Stream evenNumbers = Arrays.asList(2,4,6,8,10,12,14,16,18,20).stream(); + + Stream combinedStreams = Streams.concat(oddNumbers,evenNumbers); + + //Assert.assertNotNull(combinedStreams); + StreamUtility.assertStreamEquals(combinedStreams, Stream.concat(oddNumbers, evenNumbers)); + } + + @Test + public void concatStreamsOfTypeLongStream(){ + LongStream firstTwenty = LongStream.range(1,20); + LongStream nextTwenty = LongStream.range(21,40); + + LongStream combinedStreams = Streams.concat(firstTwenty,nextTwenty); + + Assert.assertNotNull(combinedStreams); + StreamUtility.assertStreamEquals(combinedStreams, LongStream.concat(firstTwenty, nextTwenty)); + } + + @Test + public void concatStreamsOfTypeIntStream(){ + IntStream firstTwenty = IntStream.range(1,20); + IntStream nextTwenty = IntStream.range(21,40); + + IntStream combinedStreams = Streams.concat(firstTwenty,nextTwenty); + + Assert.assertNotNull(combinedStreams); + StreamUtility.assertStreamEquals(combinedStreams, IntStream.concat(firstTwenty, nextTwenty)); + } + + + @Test + public void findLastOfStream(){ + Optional lastElement = Streams.findLast(numbers.stream()); + + Assert.assertNotNull(lastElement.get()); + Assert.assertEquals(lastElement.get(), numbers.get(20)); + } + + @Test + public void mapWithIndexTest(){ + Stream stringSream = Stream.of("a","b","c"); + + Stream mappedStream = Streams.mapWithIndex(stringSream,(str,index) -> str +":"+ index); + + //Assert.assertNotNull(mappedStream); + Assert.assertEquals(mappedStream.findFirst().get(), "a:0"); + + } + + @Test + public void streamsZipTest(){ + Stream stringSream = Stream.of("a","b","c"); + Stream intStream = Stream.of(1,2,3); + Stream mappedStream = Streams.zip(stringSream,intStream, (str,index) -> str +":"+ index); + + //Assert.assertNotNull(mappedStream); + Assert.assertEquals(mappedStream.findFirst().get(), "a:1"); + + } + + + + +} diff --git a/guava21/src/test/java/InternBuilderUnitTests.java b/guava21/src/test/java/InternBuilderUnitTests.java new file mode 100644 index 0000000000..513b44f249 --- /dev/null +++ b/guava21/src/test/java/InternBuilderUnitTests.java @@ -0,0 +1,19 @@ +import com.google.common.collect.Interner; +import com.google.common.collect.Interners; +import org.junit.Assert; +import org.junit.Test; + +public class InternBuilderUnitTests { + + @Test + public void interBuilderTest(){ + + Interner interners = Interners.newBuilder() + .concurrencyLevel(2) + .strong() + .build(); + + Assert.assertNotNull(interners); + } + +} diff --git a/guava21/src/test/java/MoreCollectorsUnitTests.java b/guava21/src/test/java/MoreCollectorsUnitTests.java new file mode 100644 index 0000000000..956331e25f --- /dev/null +++ b/guava21/src/test/java/MoreCollectorsUnitTests.java @@ -0,0 +1,36 @@ +import com.google.common.collect.MoreCollectors; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +public class MoreCollectorsUnitTests { + + @Test + public void toOptionalTest(){ + + List numbers = Arrays.asList(1); + + Optional number = numbers.stream() + .map( e -> e*2) + .collect(MoreCollectors.toOptional()); + + Assert.assertEquals(number.get(),new Integer(2)); + } + + + @Test + public void onlyElementTest(){ + List numbers = Arrays.asList(1); + + Integer number = numbers.stream() + .map( e -> e*2) + .collect(MoreCollectors.onlyElement()); + + Assert.assertEquals(number,new Integer(2)); + } + + +} diff --git a/guava21/src/test/java/StreamUtility.java b/guava21/src/test/java/StreamUtility.java new file mode 100644 index 0000000000..91a03be48d --- /dev/null +++ b/guava21/src/test/java/StreamUtility.java @@ -0,0 +1,66 @@ +import org.junit.Assert; + +import java.util.Iterator; +import java.util.stream.DoubleStream; +import java.util.stream.IntStream; +import java.util.stream.LongStream; +import java.util.stream.Stream; + +public class StreamUtility { + + public static boolean assertStreamEquals(Stream stream1, Stream stream2){ + + Iterator iterator1 = stream1.iterator(); + Iterator iterator2 = stream2.iterator(); + + while (iterator1.hasNext()){ + Assert.assertEquals(iterator1.next(), iterator2.next()); + } + + Assert.assertFalse(iterator2.hasNext()); + + return true; + } + + public static boolean assertStreamEquals(LongStream stream1, LongStream stream2) { + + Iterator iterator1 = stream1.iterator(); + Iterator iterator2 = stream2.iterator(); + + while (iterator1.hasNext()){ + Assert.assertEquals(iterator1.next(), iterator2.next()); + } + + Assert.assertFalse(iterator2.hasNext()); + + return true; + } + + public static boolean assertStreamEquals(DoubleStream stream1, DoubleStream stream2) { + + Iterator iterator1 = stream1.iterator(); + Iterator iterator2 = stream2.iterator(); + + while (iterator1.hasNext()){ + Assert.assertEquals(iterator1.next(), iterator2.next()); + } + + Assert.assertFalse(iterator2.hasNext()); + + return true; + } + + public static boolean assertStreamEquals(IntStream stream1, IntStream stream2) { + + Iterator iterator1 = stream1.iterator(); + Iterator iterator2 = stream2.iterator(); + + while (iterator1.hasNext()){ + Assert.assertEquals(iterator1.next(), iterator2.next()); + } + + Assert.assertFalse(iterator2.hasNext()); + + return true; + } +} diff --git a/javaslang/pom.xml b/javaslang/pom.xml index e111132aec..7bb23c0daf 100644 --- a/javaslang/pom.xml +++ b/javaslang/pom.xml @@ -22,8 +22,17 @@ javaslang 2.1.0-alpha + + io.javaslang + javaslang-test + ${javaslang.test.version} + + + 2.0.5 + + diff --git a/javaslang/src/test/java/com/baeldung/javaslang/PropertyBasedTest.java b/javaslang/src/test/java/com/baeldung/javaslang/PropertyBasedTest.java new file mode 100644 index 0000000000..3acac34550 --- /dev/null +++ b/javaslang/src/test/java/com/baeldung/javaslang/PropertyBasedTest.java @@ -0,0 +1,69 @@ +package com.baeldung.javaslang; + + +import javaslang.CheckedFunction1; +import javaslang.collection.Stream; +import javaslang.test.Arbitrary; +import javaslang.test.CheckResult; +import javaslang.test.Property; +import org.junit.Test; + +public class PropertyBasedTest { + + public Stream stringsSupplier() { + return Stream.from(0).map(i -> { + boolean divByTwo = i % 2 == 0; + boolean divByFive = i % 5 == 0; + + if(divByFive && divByTwo){ + return "DividedByTwoAndFiveWithoutRemainder"; + }else if(divByFive){ + return "DividedByFiveWithoutRemainder"; + }else if(divByTwo){ + return "DividedByTwoWithoutRemainder"; + } + return ""; + }); + } + + @Test + public void givenArbitrarySeq_whenCheckThatEverySecondElementIsEqualToString_thenTestPass() { + //given + Arbitrary multiplesOf2 = Arbitrary.integer() + .filter(i -> i > 0) + .filter(i -> i % 2 == 0 && i % 5 != 0); + + //when + CheckedFunction1 mustEquals = + i -> stringsSupplier().get(i).equals("DividedByTwoWithoutRemainder"); + + + //then + CheckResult result = Property + .def("Every second element must equal to DividedByTwoWithoutRemainder") + .forAll(multiplesOf2) + .suchThat(mustEquals) + .check(10_000, 100); + + result.assertIsSatisfied(); + } + + @Test + public void givenArbitrarySeq_whenCheckThatEveryFifthElementIsEqualToString_thenTestPass() { + //given + Arbitrary multiplesOf5 = Arbitrary.integer() + .filter(i -> i > 0) + .filter(i -> i % 5 == 0 && i % 2 == 0); + + //when + CheckedFunction1 mustEquals = i -> + stringsSupplier().get(i).endsWith("DividedByTwoAndFiveWithoutRemainder"); + + //then + Property.def("Every fifth element must equal to DividedByTwoAndFiveWithoutRemainder") + .forAll(multiplesOf5) + .suchThat(mustEquals) + .check(10_000, 1_000) + .assertIsSatisfied(); + } +} diff --git a/jpa-storedprocedure/pom.xml b/jpa-storedprocedure/pom.xml index 797303dc29..1672afd217 100644 --- a/jpa-storedprocedure/pom.xml +++ b/jpa-storedprocedure/pom.xml @@ -30,6 +30,7 @@ maven-assembly-plugin + ${project.basedir} jar-with-dependencies diff --git a/pom.xml b/pom.xml index 9bbb6b5360..c22a33ff69 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ UTF-8 refs/heads/master - false + @@ -210,6 +210,18 @@ vertx + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + maven + + + + + - \ No newline at end of file + diff --git a/spring-5/README.md b/spring-5/README.md index 0914388b49..47a5314009 100644 --- a/spring-5/README.md +++ b/spring-5/README.md @@ -1,6 +1,8 @@ ## Spring REST Example Project -###The Course +### The Course The "REST With Spring" Classes: http://bit.ly/restwithspring -### Relevant Articles: +### Relevant Articles + +- [Concurrent Test Execution in Spring 5](http://www.baeldung.com/spring-5-concurrent-tests) diff --git a/spring-5/src/test/java/com/baeldung/README.md b/spring-5/src/test/java/com/baeldung/README.md deleted file mode 100644 index f7b358ec3e..0000000000 --- a/spring-5/src/test/java/com/baeldung/README.md +++ /dev/null @@ -1,3 +0,0 @@ -### Relevant articles - -- [Concurrent Test Execution in Spring 5](http://www.baeldung.com/spring-5-concurrent-tests) diff --git a/spring-boot/README.MD b/spring-boot/README.MD index d70e83525b..8aa5957bad 100644 --- a/spring-boot/README.MD +++ b/spring-boot/README.MD @@ -14,4 +14,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [How to Register a Servlet in a Java Web Application](http://www.baeldung.com/register-servlet) - [Guide to Spring WebUtils and ServletRequestUtils](http://www.baeldung.com/spring-webutils-servletrequestutils) - [Using Custom Banners in Spring Boot](http://www.baeldung.com/spring-boot-custom-banners) +- [Guide to Internationalization in Spring Boot](http://www.baeldung.com/spring-boot-internationalization) diff --git a/spring-custom-aop/pom.xml b/spring-custom-aop/pom.xml new file mode 100644 index 0000000000..1c3b5bdccd --- /dev/null +++ b/spring-custom-aop/pom.xml @@ -0,0 +1,53 @@ + + 4.0.0 + com.baeldung + spring-custom-aop + 0.0.1-SNAPSHOT + war + spring-custom-aop + + + org.springframework.boot + spring-boot-starter-parent + 1.5.2.RELEASE + + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-aop + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + diff --git a/spring-custom-aop/src/main/java/org/baeldung/Application.java b/spring-custom-aop/src/main/java/org/baeldung/Application.java new file mode 100644 index 0000000000..e5c764ef7e --- /dev/null +++ b/spring-custom-aop/src/main/java/org/baeldung/Application.java @@ -0,0 +1,13 @@ +package org.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/spring-custom-aop/src/main/java/org/baeldung/ExampleAspect.java b/spring-custom-aop/src/main/java/org/baeldung/ExampleAspect.java new file mode 100644 index 0000000000..7c3b5fb599 --- /dev/null +++ b/spring-custom-aop/src/main/java/org/baeldung/ExampleAspect.java @@ -0,0 +1,25 @@ +package org.baeldung; + +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.springframework.stereotype.Component; + +@Aspect +@Component +public class ExampleAspect { + + @Around("@annotation(LogExecutionTime)") + public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { + final long start = System.currentTimeMillis(); + + final Object proceed = joinPoint.proceed(); + + final long executionTime = System.currentTimeMillis() - start; + + System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms"); + + return proceed; + } + +} diff --git a/spring-custom-aop/src/main/java/org/baeldung/LogExecutionTime.java b/spring-custom-aop/src/main/java/org/baeldung/LogExecutionTime.java new file mode 100644 index 0000000000..c10f97e78f --- /dev/null +++ b/spring-custom-aop/src/main/java/org/baeldung/LogExecutionTime.java @@ -0,0 +1,11 @@ +package org.baeldung; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface LogExecutionTime { +} diff --git a/spring-custom-aop/src/main/java/org/baeldung/Service.java b/spring-custom-aop/src/main/java/org/baeldung/Service.java new file mode 100644 index 0000000000..e4bee38438 --- /dev/null +++ b/spring-custom-aop/src/main/java/org/baeldung/Service.java @@ -0,0 +1,12 @@ +package org.baeldung; + +import org.springframework.stereotype.Component; + +@Component +public class Service { + + @LogExecutionTime + public void serve() throws InterruptedException { + Thread.sleep(2000); + } +} diff --git a/spring-custom-aop/src/test/java/org/baeldung/CustomAnnotationTest.java b/spring-custom-aop/src/test/java/org/baeldung/CustomAnnotationTest.java new file mode 100644 index 0000000000..d4712cc063 --- /dev/null +++ b/spring-custom-aop/src/test/java/org/baeldung/CustomAnnotationTest.java @@ -0,0 +1,21 @@ +package org.baeldung; + +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.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest +public class CustomAnnotationTest { + + @Autowired + private Service service; + + @Test + public void shouldApplyCustomAnnotation() throws InterruptedException { + service.serve(); + } + +} diff --git a/spring-ldap/README.md b/spring-ldap/README.md index 8dffadb685..f77572d982 100644 --- a/spring-ldap/README.md +++ b/spring-ldap/README.md @@ -1,8 +1,9 @@ +## Spring LDAP Example Project + ### Relevant articles - [Spring LDAP Overview](http://www.baeldung.com/spring-ldap) +- [Spring LDAP Example Project](http://www.baeldung.com/spring-ldap-overview/) -## Spring LDAP Example Project -- (http://www.baeldung.com/spring-ldap-overview/) diff --git a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/UserController.java b/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/UserController.java index 880b224dd9..f5553cf2b7 100644 --- a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/UserController.java +++ b/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/UserController.java @@ -1,48 +1,44 @@ package com.baeldung.springmvcforms.controller; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import javax.validation.Valid; +import com.baeldung.springmvcforms.domain.User; +import org.springframework.context.support.DefaultMessageSourceResolvable; import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; -import org.springframework.validation.ObjectError; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.ResponseBody; -import com.baeldung.springmvcforms.domain.User; +import javax.validation.Valid; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; @Controller public class UserController { - List users = new ArrayList() { - { - add(new User("ana@yahoo.com", "pass", "Ana", 20)); - add(new User("bob@yahoo.com", "pass", "Bob", 30)); - add(new User("john@yahoo.com", "pass", "John", 40)); - add(new User("mary@yahoo.com", "pass", "Mary", 30)); - } - }; + private List users = Arrays.asList( + new User("ana@yahoo.com", "pass", "Ana", 20), + new User("bob@yahoo.com", "pass", "Bob", 30), + new User("john@yahoo.com", "pass", "John", 40), + new User("mary@yahoo.com", "pass", "Mary", 30)); @GetMapping("/userPage") public String getUserProfilePage() { return "user"; } - @PostMapping(value = "/user", produces = MediaType.APPLICATION_JSON_VALUE) + @PostMapping("/user") @ResponseBody public ResponseEntity saveUser(@Valid User user, BindingResult result, Model model) { if (result.hasErrors()) { - List errors = new ArrayList<>(); - result.getAllErrors().forEach(item->{ - errors.add(item.getDefaultMessage()); - }); + final List errors = result.getAllErrors().stream() + .map(DefaultMessageSourceResolvable::getDefaultMessage) + .collect(Collectors.toList()); + return new ResponseEntity<>(errors, HttpStatus.OK); } else { if (users.stream().anyMatch(it -> user.getEmail().equals(it.getEmail()))) { @@ -54,7 +50,7 @@ public class UserController { } } - @GetMapping(value = "/users", produces = MediaType.APPLICATION_JSON_VALUE) + @GetMapping("/users") @ResponseBody public List getUsers() { return users; diff --git a/spring-security-mvc-login/README.md b/spring-security-mvc-login/README.md index 35305112b4..f7eb314869 100644 --- a/spring-security-mvc-login/README.md +++ b/spring-security-mvc-login/README.md @@ -11,6 +11,7 @@ The "Learn Spring Security" Classes: http://github.learnspringsecurity.com - [Spring Security Expressions – hasRole Example](http://www.baeldung.com/spring-security-expressions-basic) - [Spring HTTP/HTTPS Channel Security](http://www.baeldung.com/spring-channel-security-https) - [Spring Security - Customize the 403 Forbidden/Access Denied Page](http://www.baeldung.com/spring-security-custom-access-denied-page) +- [Spring Security – Redirect to the Previous URL After Login](http://www.baeldung.com/spring-security-redirect-login) ### Build the Project ``` diff --git a/xml/pom.xml b/xml/pom.xml index 38bf6bf42e..cc3da078f8 100644 --- a/xml/pom.xml +++ b/xml/pom.xml @@ -279,6 +279,7 @@ is org.apache.maven.plugins ...which is assumed by default. --> maven-assembly-plugin + ${project.basedir} jar-with-dependencies