Merge branch 'master' into sla-pr/1381-vert.x-dhrubayotti

This commit is contained in:
slavisa-baeldung 2017-03-20 15:41:06 +00:00
commit c071c59ada
34 changed files with 779 additions and 45 deletions

View File

@ -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"

View File

@ -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() {

Binary file not shown.

View File

@ -250,6 +250,7 @@
<goal>single</goal>
</goals>
<configuration>
<archiveBaseDirectory>${project.basedir}</archiveBaseDirectory>
<archive>
<manifest>
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>

View File

@ -145,6 +145,7 @@
<goal>single</goal>
</goals>
<configuration>
<archiveBaseDirectory>${project.basedir}</archiveBaseDirectory>
<archive>
<manifest>
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>

1
guava21/README.md Normal file
View File

@ -0,0 +1 @@
## Relevant articles:

41
guava21/pom.xml Normal file
View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.guava</groupId>
<artifactId>tutorial</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
</project>

View File

@ -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<Integer> 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<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
}
}

View File

@ -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);
}
}

View File

@ -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<Integer> interners = Interners.<Integer>newBuilder()
.concurrencyLevel(2)
.strong()
.<Integer>build();
}
}

View File

@ -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<Integer> numbers = Arrays.asList(1);
Optional<Integer> number = numbers.stream()
.map(e -> e * 2)
.collect(MoreCollectors.toOptional());
}
}

View File

@ -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<Integer> 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<Integer> streamFromCollection = Streams.stream(numbers);
//Using Iterator
Stream<Integer> streamFromIterator = Streams.stream(numbers.iterator());
//Using Iterable
Stream<Integer> streamFromIterable = Streams.stream((Iterable<Integer>) numbers);
//Using Optional
Stream<Integer> 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<Integer> concatenatedStreams = Streams.concat(streamFromCollection,streamFromIterable,streamFromIterator);
List<Integer> integers = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
//This will return 10
Optional<Integer> lastItem = Streams.findLast(integers.stream());
Streams.zip(
Stream.of("candy", "chocolate", "bar"),
Stream.of("$1", "$2","$3"),
(arg1, arg2) -> arg1 + ":" + arg2);
}
}

View File

@ -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<Integer> numbers = Arrays.asList(1,2,3,4,4,6,7,8,9,10);
boolean isInAscendingOrder = Comparators.isInOrder(numbers, new AscendingOrderComparator<Number>());
Assert.assertTrue(isInAscendingOrder);
}
@Test
public void isInStrictOrderTest(){
List<Integer> numbers = Arrays.asList(1,2,3,4,3,6,7,8,9,10);
boolean isInAscendingOrder = Comparators.isInOrder(numbers, new AscendingOrderComparator<Number>());
Assert.assertFalse(isInAscendingOrder);
}
private class AscendingOrderComparator<I extends Number> implements Comparator<Integer>{
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
@Override
public Comparator<Integer> reversed() {
return null;
}
@Override
public Comparator<Integer> thenComparing(Comparator<? super Integer> other) {
return null;
}
@Override
public <U> Comparator<Integer> thenComparing(Function<? super Integer, ? extends U> keyExtractor, Comparator<? super U> keyComparator) {
return null;
}
@Override
public <U extends Comparable<? super U>> Comparator<Integer> thenComparing(Function<? super Integer, ? extends U> keyExtractor) {
return null;
}
@Override
public Comparator<Integer> thenComparingInt(ToIntFunction<? super Integer> keyExtractor) {
return null;
}
@Override
public Comparator<Integer> thenComparingLong(ToLongFunction<? super Integer> keyExtractor) {
return null;
}
@Override
public Comparator<Integer> thenComparingDouble(ToDoubleFunction<? super Integer> keyExtractor) {
return null;
}
}
}

View File

@ -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<Integer> 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<Integer> numbersIterable = (Iterable<Integer>) numbers;
Stream streamFromIterable = Streams.stream(numbersIterable);
Assert.assertNotNull(streamFromIterable);
StreamUtility.assertStreamEquals(streamFromIterable, numbers.stream());
}
@Test
public void createStreamsWithIterator(){
Iterator<Integer> 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<Integer> 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<String> 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<String> mappedStream = Streams.zip(stringSream,intStream, (str,index) -> str +":"+ index);
//Assert.assertNotNull(mappedStream);
Assert.assertEquals(mappedStream.findFirst().get(), "a:1");
}
}

View File

@ -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<Integer> interners = Interners.<Integer>newBuilder()
.concurrencyLevel(2)
.strong()
.<Integer>build();
Assert.assertNotNull(interners);
}
}

View File

@ -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<Integer> numbers = Arrays.asList(1);
Optional<Integer> number = numbers.stream()
.map( e -> e*2)
.collect(MoreCollectors.toOptional());
Assert.assertEquals(number.get(),new Integer(2));
}
@Test
public void onlyElementTest(){
List<Integer> numbers = Arrays.asList(1);
Integer number = numbers.stream()
.map( e -> e*2)
.collect(MoreCollectors.onlyElement());
Assert.assertEquals(number,new Integer(2));
}
}

View File

@ -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 <T> boolean assertStreamEquals(Stream<T> stream1, Stream<T> stream2){
Iterator<T> iterator1 = stream1.iterator();
Iterator<T> 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;
}
}

View File

@ -22,8 +22,17 @@
<artifactId>javaslang</artifactId>
<version>2.1.0-alpha</version>
</dependency>
<dependency>
<groupId>io.javaslang</groupId>
<artifactId>javaslang-test</artifactId>
<version>${javaslang.test.version}</version>
</dependency>
</dependencies>
<properties>
<javaslang.test.version>2.0.5</javaslang.test.version>
</properties>
<build>
<plugins>
<plugin>

View File

@ -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<String> 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<Integer> multiplesOf2 = Arbitrary.integer()
.filter(i -> i > 0)
.filter(i -> i % 2 == 0 && i % 5 != 0);
//when
CheckedFunction1<Integer, Boolean> 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<Integer> multiplesOf5 = Arbitrary.integer()
.filter(i -> i > 0)
.filter(i -> i % 5 == 0 && i % 2 == 0);
//when
CheckedFunction1<Integer, Boolean> 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();
}
}

View File

@ -30,6 +30,7 @@
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archiveBaseDirectory>${project.basedir}</archiveBaseDirectory>
<!--archive> <manifest> <mainClass>fully.qualified.MainClass</mainClass> </manifest> </archive -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>

16
pom.xml
View File

@ -11,7 +11,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<gib.referenceBranch>refs/heads/master</gib.referenceBranch>
<gib.enabled>false</gib.enabled>
<!-- <gib.enabled>false</gib.enabled>-->
</properties>
<modules>
@ -210,6 +210,18 @@
<module>vertx</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<executable>maven</executable>
</configuration>
</plugin>
</plugins>
<extensions>
<!--<extension>
<groupId>com.vackosar.gitflowincrementalbuilder</groupId>
@ -218,4 +230,4 @@
</extension>-->
</extensions>
</build>
</project>
</project>

View File

@ -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)

View File

@ -1,3 +0,0 @@
### Relevant articles
- [Concurrent Test Execution in Spring 5](http://www.baeldung.com/spring-5-concurrent-tests)

View File

@ -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)

53
spring-custom-aop/pom.xml Normal file
View File

@ -0,0 +1,53 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>spring-custom-aop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring-custom-aop</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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 {
}

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -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/)

View File

@ -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<User> users = new ArrayList<User>() {
{
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<User> 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<Object> saveUser(@Valid User user, BindingResult result, Model model) {
if (result.hasErrors()) {
List<String> errors = new ArrayList<>();
result.getAllErrors().forEach(item->{
errors.add(item.getDefaultMessage());
});
final List<String> 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<User> getUsers() {
return users;

View File

@ -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
```

View File

@ -279,6 +279,7 @@
is org.apache.maven.plugins ...which is assumed by default. -->
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archiveBaseDirectory>${project.basedir}</archiveBaseDirectory>
<descriptorRefs>
<descriptorRef>
jar-with-dependencies