Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
0f0cc562fc
|
@ -0,0 +1,59 @@
|
||||||
|
<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/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>dozer-tutorial</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
<name>Dozer</name>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.3</version>
|
||||||
|
<configuration>
|
||||||
|
<source>7</source>
|
||||||
|
<target>7</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
<version>1.7.5</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>jcl-over-slf4j</artifactId>
|
||||||
|
<version>1.7.5</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>3.2.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-beanutils</groupId>
|
||||||
|
<artifactId>commons-beanutils</artifactId>
|
||||||
|
<version>1.9.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.sf.dozer</groupId>
|
||||||
|
<artifactId>dozer</artifactId>
|
||||||
|
<version>5.5.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.3</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
|
@ -0,0 +1,33 @@
|
||||||
|
package com.baeldung.dozer;
|
||||||
|
|
||||||
|
public class Dest {
|
||||||
|
private String name;
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
public Dest() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Dest(String name, int age) {
|
||||||
|
super();
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package com.baeldung.dozer;
|
||||||
|
|
||||||
|
public class Dest2 {
|
||||||
|
private int id;
|
||||||
|
private int points;
|
||||||
|
|
||||||
|
public Dest2() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Dest2(int id, int points) {
|
||||||
|
super();
|
||||||
|
this.id = id;
|
||||||
|
this.points = points;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPoints() {
|
||||||
|
return points;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPoints(int points) {
|
||||||
|
this.points = points;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Dest2 [id=" + id + ", points=" + points + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
package com.baeldung.dozer;
|
||||||
|
|
||||||
|
import java.text.DateFormat;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import org.dozer.CustomConverter;
|
||||||
|
import org.dozer.MappingException;
|
||||||
|
|
||||||
|
public class MyCustomConvertor implements CustomConverter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object convert(Object dest, Object source, Class<?> arg2,
|
||||||
|
Class<?> arg3) {
|
||||||
|
if (source == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (source instanceof Personne3) {
|
||||||
|
Personne3 person = (Personne3) source;
|
||||||
|
Date date = new Date(person.getDtob());
|
||||||
|
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
||||||
|
String isoDate = format.format(date);
|
||||||
|
return new Person3(person.getName(), isoDate);
|
||||||
|
|
||||||
|
} else if (source instanceof Person3) {
|
||||||
|
Person3 person = (Person3) source;
|
||||||
|
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
||||||
|
Date date = null;
|
||||||
|
try {
|
||||||
|
date = format.parse(person.getDtob());
|
||||||
|
|
||||||
|
} catch (ParseException e) {
|
||||||
|
throw new MappingException("Converter MyCustomConvertor "
|
||||||
|
+ "used incorrectly:" + e.getMessage());
|
||||||
|
}
|
||||||
|
long timestamp = date.getTime();
|
||||||
|
return new Personne3(person.getName(), timestamp);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
throw new MappingException("Converter MyCustomConvertor "
|
||||||
|
+ "used incorrectly. Arguments passed in were:" + dest
|
||||||
|
+ " and " + source);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.baeldung.dozer;
|
||||||
|
|
||||||
|
public class Person {
|
||||||
|
private String name;
|
||||||
|
private String nickname;
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
public Person() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Person(String name, String nickname, int age) {
|
||||||
|
super();
|
||||||
|
this.name = name;
|
||||||
|
this.nickname = nickname;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNickname() {
|
||||||
|
return nickname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNickname(String nickname) {
|
||||||
|
this.nickname = nickname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.baeldung.dozer;
|
||||||
|
|
||||||
|
public class Person2 {
|
||||||
|
private String name;
|
||||||
|
private String nickname;
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
public Person2() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Person2(String name, String nickname, int age) {
|
||||||
|
super();
|
||||||
|
this.name = name;
|
||||||
|
this.nickname = nickname;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNickname() {
|
||||||
|
return nickname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNickname(String nickname) {
|
||||||
|
this.nickname = nickname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
package com.baeldung.dozer;
|
||||||
|
|
||||||
|
public class Person3 {
|
||||||
|
private String name;
|
||||||
|
private String dtob;
|
||||||
|
|
||||||
|
public Person3() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Person3(String name, String dtob) {
|
||||||
|
super();
|
||||||
|
this.name = name;
|
||||||
|
this.dtob = dtob;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDtob() {
|
||||||
|
return dtob;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDtob(String dtob) {
|
||||||
|
this.dtob = dtob;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Person3 [name=" + name + ", dtob=" + dtob + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.baeldung.dozer;
|
||||||
|
|
||||||
|
public class Personne {
|
||||||
|
private String nom;
|
||||||
|
private String surnom;
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
public Personne() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Personne(String nom, String surnom, int age) {
|
||||||
|
super();
|
||||||
|
this.nom = nom;
|
||||||
|
this.surnom = surnom;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNom() {
|
||||||
|
return nom;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNom(String nom) {
|
||||||
|
this.nom = nom;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSurnom() {
|
||||||
|
return surnom;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSurnom(String surnom) {
|
||||||
|
this.surnom = surnom;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
package com.baeldung.dozer;
|
||||||
|
|
||||||
|
import org.dozer.Mapping;
|
||||||
|
|
||||||
|
public class Personne2 {
|
||||||
|
private String nom;
|
||||||
|
private String surnom;
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
public Personne2() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Personne2(String nom, String surnom, int age) {
|
||||||
|
super();
|
||||||
|
this.nom = nom;
|
||||||
|
this.surnom = surnom;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Mapping("name")
|
||||||
|
public String getNom() {
|
||||||
|
return nom;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Mapping("nickname")
|
||||||
|
public String getSurnom() {
|
||||||
|
return surnom;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNom(String nom) {
|
||||||
|
this.nom = nom;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSurnom(String surnom) {
|
||||||
|
this.surnom = surnom;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package com.baeldung.dozer;
|
||||||
|
|
||||||
|
public class Personne3 {
|
||||||
|
private String name;
|
||||||
|
private long dtob;
|
||||||
|
|
||||||
|
public Personne3() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Personne3(String name, long dtob) {
|
||||||
|
super();
|
||||||
|
this.name = name;
|
||||||
|
this.dtob = dtob;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getDtob() {
|
||||||
|
return dtob;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDtob(long dtob) {
|
||||||
|
this.dtob = dtob;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Personne3 [name=" + name + ", dtob=" + dtob + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.baeldung.dozer;
|
||||||
|
|
||||||
|
public class Source {
|
||||||
|
private String name;
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
public Source() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Source(String name, int age) {
|
||||||
|
super();
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package com.baeldung.dozer;
|
||||||
|
|
||||||
|
public class Source2 {
|
||||||
|
private String id;
|
||||||
|
private double points;
|
||||||
|
|
||||||
|
public Source2() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Source2(String id, double points) {
|
||||||
|
super();
|
||||||
|
this.id = id;
|
||||||
|
this.points = points;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPoints() {
|
||||||
|
return points;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPoints(double points) {
|
||||||
|
this.points = points;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Source2 [id=" + id + ", points=" + points + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,200 @@
|
||||||
|
package com.baeldung.dozer;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.dozer.DozerBeanMapper;
|
||||||
|
import org.dozer.loader.api.BeanMappingBuilder;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class DozerTest {
|
||||||
|
|
||||||
|
private DozerBeanMapper mapper = new DozerBeanMapper();
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void before() throws Exception {
|
||||||
|
mapper = new DozerBeanMapper();
|
||||||
|
}
|
||||||
|
|
||||||
|
private BeanMappingBuilder builder = new BeanMappingBuilder() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void configure() {
|
||||||
|
mapping(Person.class, Personne.class).fields("name", "nom").fields(
|
||||||
|
"nickname", "surnom");
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
private BeanMappingBuilder builderMinusAge = new BeanMappingBuilder() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void configure() {
|
||||||
|
mapping(Person.class, Personne.class).fields("name", "nom")
|
||||||
|
.fields("nickname", "surnom").exclude("age");
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenApiMapper_whenMaps_thenCorrect() {
|
||||||
|
Personne frenchAppPerson = new Personne("Sylvester Stallone", "Rambo",
|
||||||
|
70);
|
||||||
|
mapper.addMapping(builder);
|
||||||
|
Person englishAppPerson = mapper.map(frenchAppPerson, Person.class);
|
||||||
|
assertEquals(englishAppPerson.getName(), frenchAppPerson.getNom());
|
||||||
|
assertEquals(englishAppPerson.getNickname(),
|
||||||
|
frenchAppPerson.getSurnom());
|
||||||
|
assertEquals(englishAppPerson.getAge(), frenchAppPerson.getAge());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenApiMapper_whenMapsOnlySpecifiedFields_thenCorrect() {
|
||||||
|
Person englishAppPerson = new Person("Sylvester Stallone", "Rambo", 70);
|
||||||
|
mapper.addMapping(builderMinusAge);
|
||||||
|
Personne frenchAppPerson = mapper.map(englishAppPerson, Personne.class);
|
||||||
|
assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
|
||||||
|
assertEquals(frenchAppPerson.getSurnom(),
|
||||||
|
englishAppPerson.getNickname());
|
||||||
|
assertEquals(frenchAppPerson.getAge(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenApiMapper_whenMapsBidirectionally_thenCorrect() {
|
||||||
|
Person englishAppPerson = new Person("Sylvester Stallone", "Rambo", 70);
|
||||||
|
mapper.addMapping(builder);
|
||||||
|
Personne frenchAppPerson = mapper.map(englishAppPerson, Personne.class);
|
||||||
|
assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
|
||||||
|
assertEquals(frenchAppPerson.getSurnom(),
|
||||||
|
englishAppPerson.getNickname());
|
||||||
|
assertEquals(frenchAppPerson.getAge(), englishAppPerson.getAge());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSourceObjectAndDestClass_whenMapsSameNameFieldsCorrectly_thenCorrect() {
|
||||||
|
Source source = new Source("Baeldung", 10);
|
||||||
|
Dest dest = mapper.map(source, Dest.class);
|
||||||
|
assertEquals(dest.getName(), "Baeldung");
|
||||||
|
assertEquals(dest.getAge(), 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSourceObjectAndDestObject_whenMapsSameNameFieldsCorrectly_thenCorrect() {
|
||||||
|
Source source = new Source("Baeldung", 10);
|
||||||
|
Dest dest = new Dest();
|
||||||
|
mapper.map(source, dest);
|
||||||
|
assertEquals(dest.getName(), "Baeldung");
|
||||||
|
assertEquals(dest.getAge(), 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSourceAndDestWithDifferentFieldTypes_whenMapsAndAutoConverts_thenCorrect() {
|
||||||
|
Source2 source = new Source2("320", 15.2);
|
||||||
|
Dest2 dest = mapper.map(source, Dest2.class);
|
||||||
|
assertEquals(dest.getId(), 320);
|
||||||
|
assertEquals(dest.getPoints(), 15);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSrcAndDestWithDifferentFieldNamesWithCustomMapper_whenMaps_thenCorrect() {
|
||||||
|
List<String> mappingFiles = new ArrayList<>();
|
||||||
|
mappingFiles.add("dozer_mapping.xml");
|
||||||
|
Personne frenchAppPerson = new Personne("Sylvester Stallone", "Rambo",
|
||||||
|
70);
|
||||||
|
mapper.setMappingFiles(mappingFiles);
|
||||||
|
Person englishAppPerson = mapper.map(frenchAppPerson, Person.class);
|
||||||
|
assertEquals(englishAppPerson.getName(), frenchAppPerson.getNom());
|
||||||
|
assertEquals(englishAppPerson.getNickname(),
|
||||||
|
frenchAppPerson.getSurnom());
|
||||||
|
assertEquals(englishAppPerson.getAge(), frenchAppPerson.getAge());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSrcAndDestWithDifferentFieldNamesWithCustomMapper_whenMapsBidirectionally_thenCorrect() {
|
||||||
|
List<String> mappingFiles = new ArrayList<>();
|
||||||
|
mappingFiles.add("dozer_mapping.xml");
|
||||||
|
Person englishAppPerson = new Person("Dwayne Johnson", "The Rock", 44);
|
||||||
|
mapper.setMappingFiles(mappingFiles);
|
||||||
|
Personne frenchAppPerson = mapper.map(englishAppPerson, Personne.class);
|
||||||
|
assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
|
||||||
|
assertEquals(frenchAppPerson.getSurnom(),
|
||||||
|
englishAppPerson.getNickname());
|
||||||
|
assertEquals(frenchAppPerson.getAge(), englishAppPerson.getAge());
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Test
|
||||||
|
// public void givenMappingFileOutsideClasspath_whenMaps_thenCorrect() {
|
||||||
|
// List<String> mappingFiles = new ArrayList<>();
|
||||||
|
// mappingFiles.add("file:E:\\dozer_mapping.xml");
|
||||||
|
// Person englishAppPerson = new Person("Marshall Bruce Mathers III",
|
||||||
|
// "Eminem", 43);
|
||||||
|
// mapper.setMappingFiles(mappingFiles);
|
||||||
|
// Personne frenchAppPerson = mapper.map(englishAppPerson, Personne.class);
|
||||||
|
// assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
|
||||||
|
// assertEquals(frenchAppPerson.getSurnom(),
|
||||||
|
// englishAppPerson.getNickname());
|
||||||
|
// assertEquals(frenchAppPerson.getAge(), englishAppPerson.getAge());
|
||||||
|
// }
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSrcAndDest_whenMapsOnlySpecifiedFields_thenCorrect() {
|
||||||
|
List<String> mappingFiles = new ArrayList<>();
|
||||||
|
mappingFiles.add("dozer_mapping2.xml");
|
||||||
|
Person englishAppPerson = new Person("Shawn Corey Carter", "Jay Z", 46);
|
||||||
|
mapper.setMappingFiles(mappingFiles);
|
||||||
|
Personne frenchAppPerson = mapper.map(englishAppPerson, Personne.class);
|
||||||
|
assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
|
||||||
|
assertEquals(frenchAppPerson.getSurnom(),
|
||||||
|
englishAppPerson.getNickname());
|
||||||
|
assertEquals(frenchAppPerson.getAge(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAnnotatedSrcFields_whenMapsToRightDestField_thenCorrect() {
|
||||||
|
Person2 englishAppPerson = new Person2("Jean-Claude Van Damme", "JCVD",
|
||||||
|
55);
|
||||||
|
Personne2 frenchAppPerson = mapper.map(englishAppPerson,
|
||||||
|
Personne2.class);
|
||||||
|
assertEquals(frenchAppPerson.getNom(), englishAppPerson.getName());
|
||||||
|
assertEquals(frenchAppPerson.getSurnom(),
|
||||||
|
englishAppPerson.getNickname());
|
||||||
|
assertEquals(frenchAppPerson.getAge(), englishAppPerson.getAge());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAnnotatedSrcFields_whenMapsToRightDestFieldBidirectionally_thenCorrect() {
|
||||||
|
Personne2 frenchAppPerson = new Personne2("Jason Statham",
|
||||||
|
"transporter", 49);
|
||||||
|
Person2 englishAppPerson = mapper.map(frenchAppPerson, Person2.class);
|
||||||
|
assertEquals(englishAppPerson.getName(), frenchAppPerson.getNom());
|
||||||
|
assertEquals(englishAppPerson.getNickname(),
|
||||||
|
frenchAppPerson.getSurnom());
|
||||||
|
assertEquals(englishAppPerson.getAge(), frenchAppPerson.getAge());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSrcAndDestWithDifferentFieldTypes_whenAbleToCustomConvert_thenCorrect() {
|
||||||
|
String dateTime = "2007-06-26T21:22:39Z";
|
||||||
|
long timestamp = new Long("1182882159000");
|
||||||
|
Person3 person = new Person3("Rich", dateTime);
|
||||||
|
mapper.setMappingFiles(Arrays
|
||||||
|
.asList(new String[] { "dozer_custom_convertor.xml" }));
|
||||||
|
Personne3 person0 = mapper.map(person, Personne3.class);
|
||||||
|
assertEquals(timestamp, person0.getDtob());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSrcAndDestWithDifferentFieldTypes_whenAbleToCustomConvertBidirectionally_thenCorrect() {
|
||||||
|
String dateTime = "2007-06-26T21:22:39Z";
|
||||||
|
long timestamp = new Long("1182882159000");
|
||||||
|
Personne3 person = new Personne3("Rich", timestamp);
|
||||||
|
mapper.setMappingFiles(Arrays
|
||||||
|
.asList(new String[] { "dozer_custom_convertor.xml" }));
|
||||||
|
Person3 person0 = mapper.map(person, Person3.class);
|
||||||
|
assertEquals(dateTime, person0.getDtob());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://dozer.sourceforge.net
|
||||||
|
http://dozer.sourceforge.net/schema/beanmapping.xsd">
|
||||||
|
<configuration>
|
||||||
|
<custom-converters>
|
||||||
|
<converter type="com.baeldung.dozer.MyCustomConvertor">
|
||||||
|
<class-a>com.baeldung.dozer.Personne3</class-a>
|
||||||
|
<class-b>com.baeldung.dozer.Person3</class-b>
|
||||||
|
</converter>
|
||||||
|
</custom-converters>
|
||||||
|
</configuration>
|
||||||
|
|
||||||
|
</mappings>
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://dozer.sourceforge.net
|
||||||
|
http://dozer.sourceforge.net/schema/beanmapping.xsd">
|
||||||
|
<mapping>
|
||||||
|
<class-a>com.baeldung.dozer.Personne</class-a>
|
||||||
|
<class-b>com.baeldung.dozer.Person</class-b>
|
||||||
|
<field>
|
||||||
|
<a>nom</a>
|
||||||
|
<b>name</b>
|
||||||
|
</field>
|
||||||
|
<field>
|
||||||
|
<a>surnom</a>
|
||||||
|
<b>nickname</b>
|
||||||
|
</field>
|
||||||
|
</mapping>
|
||||||
|
</mappings>
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://dozer.sourceforge.net
|
||||||
|
http://dozer.sourceforge.net/schema/beanmapping.xsd">
|
||||||
|
<mapping wildcard="false">
|
||||||
|
<class-a>com.baeldung.dozer.Personne</class-a>
|
||||||
|
<class-b>com.baeldung.dozer.Person</class-b>
|
||||||
|
<field>
|
||||||
|
<a>nom</a>
|
||||||
|
<b>name</b>
|
||||||
|
</field>
|
||||||
|
<field>
|
||||||
|
<a>surnom</a>
|
||||||
|
<b>nickname</b>
|
||||||
|
</field>
|
||||||
|
</mapping>
|
||||||
|
</mappings>
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.baeldung.hystrix;
|
||||||
|
|
||||||
|
|
||||||
|
public class RemoteServiceSimulator {
|
||||||
|
|
||||||
|
public String checkSomething(final long timeout) throws InterruptedException {
|
||||||
|
|
||||||
|
System.out.print(String.format("Waiting %sms. ", timeout));
|
||||||
|
|
||||||
|
// to simulate a real world delay in processing.
|
||||||
|
Thread.sleep(timeout);
|
||||||
|
|
||||||
|
return "Done waiting.";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
<?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</groupId>
|
||||||
|
<artifactId>immutables</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.immutables</groupId>
|
||||||
|
<artifactId>value</artifactId>
|
||||||
|
<version>2.2.10</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>3.5.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mutabilitydetector</groupId>
|
||||||
|
<artifactId>MutabilityDetector</artifactId>
|
||||||
|
<version>0.9.5</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.3</version>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.baeldung.immutable;
|
||||||
|
|
||||||
|
import org.immutables.value.Value;
|
||||||
|
|
||||||
|
@Value.Immutable
|
||||||
|
public interface Address {
|
||||||
|
String getStreetName();
|
||||||
|
Integer getNumber();
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.baeldung.immutable;
|
||||||
|
|
||||||
|
import org.immutables.value.Value;
|
||||||
|
|
||||||
|
@Value.Immutable
|
||||||
|
public abstract class Person {
|
||||||
|
abstract String getName();
|
||||||
|
abstract Integer getAge();
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.immutable.auxiliary;
|
||||||
|
|
||||||
|
|
||||||
|
import org.immutables.value.Value;
|
||||||
|
|
||||||
|
@Value.Immutable
|
||||||
|
public abstract class Person {
|
||||||
|
abstract String getName();
|
||||||
|
abstract Integer getAge();
|
||||||
|
|
||||||
|
@Value.Auxiliary
|
||||||
|
abstract String getAuxiliaryField();
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.baeldung.immutable.default_;
|
||||||
|
|
||||||
|
import org.immutables.value.Value;
|
||||||
|
|
||||||
|
@Value.Immutable(prehash = true)
|
||||||
|
public abstract class Person {
|
||||||
|
|
||||||
|
abstract String getName();
|
||||||
|
|
||||||
|
@Value.Default
|
||||||
|
Integer getAge() {
|
||||||
|
return 42;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.baeldung.immutable.parameter;
|
||||||
|
|
||||||
|
|
||||||
|
import org.immutables.value.Value;
|
||||||
|
|
||||||
|
@Value.Immutable
|
||||||
|
public abstract class Person {
|
||||||
|
|
||||||
|
@Value.Parameter
|
||||||
|
abstract String getName();
|
||||||
|
|
||||||
|
@Value.Parameter
|
||||||
|
abstract Integer getAge();
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.baeldung.immutable.prehash;
|
||||||
|
|
||||||
|
import org.immutables.value.Value;
|
||||||
|
|
||||||
|
@Value.Immutable(prehash = true)
|
||||||
|
public abstract class Person {
|
||||||
|
abstract String getName();
|
||||||
|
abstract Integer getAge();
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.baeldung.immutable;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertImmutable;
|
||||||
|
|
||||||
|
public class ImmutablePersonTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenModifying_shouldCreateNewInstance() throws Exception {
|
||||||
|
final ImmutablePerson john = ImmutablePerson.builder()
|
||||||
|
.age(42)
|
||||||
|
.name("John")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
final ImmutablePerson john43 = john.withAge(43);
|
||||||
|
|
||||||
|
assertThat(john)
|
||||||
|
.isNotSameAs(john43);
|
||||||
|
|
||||||
|
assertThat(john.getAge())
|
||||||
|
.isEqualTo(42);
|
||||||
|
|
||||||
|
assertImmutable(ImmutablePerson.class);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
package com.baeldung.immutable.auxiliary;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class ImmutablePersonAuxiliaryTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenComparing_shouldIgnore() throws Exception {
|
||||||
|
final ImmutablePerson john1 = ImmutablePerson.builder()
|
||||||
|
.name("John")
|
||||||
|
.age(42)
|
||||||
|
.auxiliaryField("Value1")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
final ImmutablePerson john2 = ImmutablePerson.builder()
|
||||||
|
.name("John")
|
||||||
|
.age(42)
|
||||||
|
.auxiliaryField("Value2")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
|
assertThat(john1.equals(john2))
|
||||||
|
.isTrue();
|
||||||
|
|
||||||
|
assertThat(john1.toString())
|
||||||
|
.isEqualTo(john2.toString());
|
||||||
|
|
||||||
|
assertThat(john1.hashCode())
|
||||||
|
.isEqualTo(john2.hashCode());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.baeldung.immutable.default_;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class ImmutablePersonDefaultTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenInstantiating_shouldUseDefaultValue() throws Exception {
|
||||||
|
|
||||||
|
final ImmutablePerson john = ImmutablePerson.builder().name("John").build();
|
||||||
|
|
||||||
|
assertThat(john.getAge()).isEqualTo(42);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
3
pom.xml
3
pom.xml
|
@ -31,6 +31,7 @@
|
||||||
<module>guava19</module>
|
<module>guava19</module>
|
||||||
<module>handling-spring-static-resources</module>
|
<module>handling-spring-static-resources</module>
|
||||||
<module>httpclient</module>
|
<module>httpclient</module>
|
||||||
|
<module>immutables</module>
|
||||||
<module>jackson</module>
|
<module>jackson</module>
|
||||||
<module>javaxval</module>
|
<module>javaxval</module>
|
||||||
<module>jjwt</module>
|
<module>jjwt</module>
|
||||||
|
@ -55,7 +56,7 @@
|
||||||
<module>spring-autowire</module>
|
<module>spring-autowire</module>
|
||||||
<module>spring-batch</module>
|
<module>spring-batch</module>
|
||||||
<module>spring-boot</module>
|
<module>spring-boot</module>
|
||||||
<module>spring-controller</module>
|
<module>spring-cucumber</module>
|
||||||
<module>spring-data-cassandra</module>
|
<module>spring-data-cassandra</module>
|
||||||
<module>spring-data-couchbase-2</module>
|
<module>spring-data-couchbase-2</module>
|
||||||
<module>spring-data-couchbase-2b</module>
|
<module>spring-data-couchbase-2b</module>
|
||||||
|
|
|
@ -1,12 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<classpath>
|
|
||||||
<classpathentry kind="src" path="src/main/webapp"/>
|
|
||||||
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
|
|
||||||
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
|
|
||||||
<attributes>
|
|
||||||
<attribute name="hide" value="true"/>
|
|
||||||
</attributes>
|
|
||||||
</classpathentry>
|
|
||||||
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
|
|
||||||
<classpathentry kind="output" path=""/>
|
|
||||||
</classpath>
|
|
|
@ -1,95 +0,0 @@
|
||||||
eclipse.preferences.version=1
|
|
||||||
org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull
|
|
||||||
org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault
|
|
||||||
org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable
|
|
||||||
org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled
|
|
||||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
|
||||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
|
||||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
|
||||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
|
||||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
|
||||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
|
||||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
|
||||||
org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
|
||||||
org.eclipse.jdt.core.compiler.problem.autoboxing=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.comparingIdentical=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.deadCode=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.deprecation=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled
|
|
||||||
org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled
|
|
||||||
org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
|
||||||
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
|
|
||||||
org.eclipse.jdt.core.compiler.problem.fieldHiding=error
|
|
||||||
org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts=disabled
|
|
||||||
org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.localVariableHiding=error
|
|
||||||
org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.missingDefaultCase=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.missingEnumCaseDespiteDefault=disabled
|
|
||||||
org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled
|
|
||||||
org.eclipse.jdt.core.compiler.problem.missingSerialVersion=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=error
|
|
||||||
org.eclipse.jdt.core.compiler.problem.nullReference=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.nullSpecViolation=error
|
|
||||||
org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.potentialNullReference=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.redundantNullAnnotation=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.redundantNullCheck=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled
|
|
||||||
org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled
|
|
||||||
org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled
|
|
||||||
org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.typeParameterHiding=error
|
|
||||||
org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=enabled
|
|
||||||
org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=enabled
|
|
||||||
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled
|
|
||||||
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled
|
|
||||||
org.eclipse.jdt.core.compiler.problem.unusedImport=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.unusedLabel=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.unusedLocal=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.unusedParameter=ignore
|
|
||||||
org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference=enabled
|
|
||||||
org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled
|
|
||||||
org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled
|
|
||||||
org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning
|
|
||||||
org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning
|
|
||||||
org.eclipse.jdt.core.compiler.source=1.8
|
|
|
@ -1,55 +0,0 @@
|
||||||
#Sat Jan 21 23:04:06 EET 2012
|
|
||||||
eclipse.preferences.version=1
|
|
||||||
editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true
|
|
||||||
sp_cleanup.add_default_serial_version_id=true
|
|
||||||
sp_cleanup.add_generated_serial_version_id=false
|
|
||||||
sp_cleanup.add_missing_annotations=true
|
|
||||||
sp_cleanup.add_missing_deprecated_annotations=true
|
|
||||||
sp_cleanup.add_missing_methods=false
|
|
||||||
sp_cleanup.add_missing_nls_tags=false
|
|
||||||
sp_cleanup.add_missing_override_annotations=true
|
|
||||||
sp_cleanup.add_missing_override_annotations_interface_methods=true
|
|
||||||
sp_cleanup.add_serial_version_id=false
|
|
||||||
sp_cleanup.always_use_blocks=true
|
|
||||||
sp_cleanup.always_use_parentheses_in_expressions=true
|
|
||||||
sp_cleanup.always_use_this_for_non_static_field_access=false
|
|
||||||
sp_cleanup.always_use_this_for_non_static_method_access=false
|
|
||||||
sp_cleanup.convert_to_enhanced_for_loop=true
|
|
||||||
sp_cleanup.correct_indentation=true
|
|
||||||
sp_cleanup.format_source_code=true
|
|
||||||
sp_cleanup.format_source_code_changes_only=true
|
|
||||||
sp_cleanup.make_local_variable_final=true
|
|
||||||
sp_cleanup.make_parameters_final=true
|
|
||||||
sp_cleanup.make_private_fields_final=false
|
|
||||||
sp_cleanup.make_type_abstract_if_missing_method=false
|
|
||||||
sp_cleanup.make_variable_declarations_final=true
|
|
||||||
sp_cleanup.never_use_blocks=false
|
|
||||||
sp_cleanup.never_use_parentheses_in_expressions=false
|
|
||||||
sp_cleanup.on_save_use_additional_actions=true
|
|
||||||
sp_cleanup.organize_imports=true
|
|
||||||
sp_cleanup.qualify_static_field_accesses_with_declaring_class=false
|
|
||||||
sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true
|
|
||||||
sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true
|
|
||||||
sp_cleanup.qualify_static_member_accesses_with_declaring_class=true
|
|
||||||
sp_cleanup.qualify_static_method_accesses_with_declaring_class=false
|
|
||||||
sp_cleanup.remove_private_constructors=true
|
|
||||||
sp_cleanup.remove_trailing_whitespaces=true
|
|
||||||
sp_cleanup.remove_trailing_whitespaces_all=true
|
|
||||||
sp_cleanup.remove_trailing_whitespaces_ignore_empty=false
|
|
||||||
sp_cleanup.remove_unnecessary_casts=true
|
|
||||||
sp_cleanup.remove_unnecessary_nls_tags=false
|
|
||||||
sp_cleanup.remove_unused_imports=true
|
|
||||||
sp_cleanup.remove_unused_local_variables=false
|
|
||||||
sp_cleanup.remove_unused_private_fields=true
|
|
||||||
sp_cleanup.remove_unused_private_members=false
|
|
||||||
sp_cleanup.remove_unused_private_methods=true
|
|
||||||
sp_cleanup.remove_unused_private_types=true
|
|
||||||
sp_cleanup.sort_members=false
|
|
||||||
sp_cleanup.sort_members_all=false
|
|
||||||
sp_cleanup.use_blocks=false
|
|
||||||
sp_cleanup.use_blocks_only_for_return_and_throw=false
|
|
||||||
sp_cleanup.use_parentheses_in_expressions=false
|
|
||||||
sp_cleanup.use_this_for_non_static_field_access=true
|
|
||||||
sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true
|
|
||||||
sp_cleanup.use_this_for_non_static_method_access=true
|
|
||||||
sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true
|
|
|
@ -1,4 +0,0 @@
|
||||||
activeProfiles=
|
|
||||||
eclipse.preferences.version=1
|
|
||||||
resolveWorkspaceProjects=true
|
|
||||||
version=1
|
|
|
@ -1,2 +0,0 @@
|
||||||
eclipse.preferences.version=1
|
|
||||||
org.eclipse.m2e.wtp.enabledProjectSpecificPrefs=false
|
|
|
@ -1,10 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
|
|
||||||
<wb-module deploy-name="spring-all">
|
|
||||||
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
|
|
||||||
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
|
|
||||||
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
|
|
||||||
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
|
|
||||||
<property name="context-root" value="spring-all"/>
|
|
||||||
<property name="java-output-path" value="/spring-all/target/classes"/>
|
|
||||||
</wb-module>
|
|
||||||
</project-modules>
|
|
|
@ -1,6 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<faceted-project>
|
|
||||||
<installed facet="wst.jsdt.web" version="1.0"/>
|
|
||||||
<installed facet="java" version="1.8"/>
|
|
||||||
<installed facet="jst.web" version="3.1"/>
|
|
||||||
</faceted-project>
|
|
|
@ -1 +0,0 @@
|
||||||
org.eclipse.wst.jsdt.launching.baseBrowserLibrary
|
|
|
@ -1 +0,0 @@
|
||||||
Window
|
|
|
@ -1,15 +0,0 @@
|
||||||
DELEGATES_PREFERENCE=delegateValidatorList
|
|
||||||
USER_BUILD_PREFERENCE=enabledBuildValidatorListorg.eclipse.jst.j2ee.internal.classpathdep.ClasspathDependencyValidator;
|
|
||||||
USER_MANUAL_PREFERENCE=enabledManualValidatorListorg.eclipse.jst.j2ee.internal.classpathdep.ClasspathDependencyValidator;
|
|
||||||
USER_PREFERENCE=overrideGlobalPreferencestruedisableAllValidationfalseversion1.2.402.v201212031633
|
|
||||||
disabled=06target
|
|
||||||
eclipse.preferences.version=1
|
|
||||||
override=true
|
|
||||||
suspend=false
|
|
||||||
vals/org.eclipse.jst.jsf.ui.JSFAppConfigValidator/global=FF01
|
|
||||||
vals/org.eclipse.jst.jsp.core.JSPBatchValidator/global=FF01
|
|
||||||
vals/org.eclipse.jst.jsp.core.JSPContentValidator/global=FF01
|
|
||||||
vals/org.eclipse.jst.jsp.core.TLDValidator/global=FF01
|
|
||||||
vals/org.eclipse.wst.dtd.core.dtdDTDValidator/global=FF01
|
|
||||||
vals/org.eclipse.wst.jsdt.web.core.JsBatchValidator/global=TF02
|
|
||||||
vf.version=3
|
|
|
@ -1,2 +0,0 @@
|
||||||
eclipse.preferences.version=1
|
|
||||||
org.eclipse.wst.ws.service.policy.projectEnabled=false
|
|
|
@ -14,6 +14,10 @@
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- Spring -->
|
<!-- Spring -->
|
||||||
|
|
||||||
|
@ -41,11 +45,6 @@
|
||||||
<artifactId>spring-aspects</artifactId>
|
<artifactId>spring-aspects</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-orm</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- persistence -->
|
<!-- persistence -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -88,6 +87,14 @@
|
||||||
<scope>runtime</scope>
|
<scope>runtime</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Akka -->
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.typesafe.akka</groupId>
|
||||||
|
<artifactId>akka-actor_2.11</artifactId>
|
||||||
|
<version>2.4.8</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- util -->
|
<!-- util -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
package org.baeldung.akka;
|
||||||
|
|
||||||
|
import akka.actor.ActorSystem;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import static org.baeldung.akka.SpringExtension.SPRING_EXTENSION_PROVIDER;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ComponentScan
|
||||||
|
public class AppConfiguration {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ActorSystem actorSystem() {
|
||||||
|
ActorSystem system = ActorSystem.create("akka-spring-demo");
|
||||||
|
SPRING_EXTENSION_PROVIDER.get(system).initialize(applicationContext);
|
||||||
|
return system;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package org.baeldung.akka;
|
||||||
|
|
||||||
|
import akka.actor.UntypedActor;
|
||||||
|
import org.springframework.context.annotation.Scope;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Scope(SCOPE_PROTOTYPE)
|
||||||
|
public class GreetingActor extends UntypedActor {
|
||||||
|
|
||||||
|
private GreetingService greetingService;
|
||||||
|
|
||||||
|
public GreetingActor(GreetingService greetingService) {
|
||||||
|
this.greetingService = greetingService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onReceive(Object message) throws Throwable {
|
||||||
|
if (message instanceof Greet) {
|
||||||
|
String name = ((Greet) message).getName();
|
||||||
|
getSender().tell(greetingService.greet(name), getSelf());
|
||||||
|
} else {
|
||||||
|
unhandled(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Greet {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Greet(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
package org.baeldung.akka;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class GreetingService {
|
||||||
|
|
||||||
|
public String greet(String name) {
|
||||||
|
return "Hello, " + name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package org.baeldung.akka;
|
||||||
|
|
||||||
|
import akka.actor.Actor;
|
||||||
|
import akka.actor.IndirectActorProducer;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
|
||||||
|
public class SpringActorProducer implements IndirectActorProducer {
|
||||||
|
|
||||||
|
private ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
private String beanActorName;
|
||||||
|
|
||||||
|
public SpringActorProducer(ApplicationContext applicationContext, String beanActorName) {
|
||||||
|
this.applicationContext = applicationContext;
|
||||||
|
this.beanActorName = beanActorName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Actor produce() {
|
||||||
|
return (Actor) applicationContext.getBean(beanActorName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<? extends Actor> actorClass() {
|
||||||
|
return (Class<? extends Actor>) applicationContext.getType(beanActorName);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
package org.baeldung.akka;
|
||||||
|
|
||||||
|
import akka.actor.AbstractExtensionId;
|
||||||
|
import akka.actor.ExtendedActorSystem;
|
||||||
|
import akka.actor.Extension;
|
||||||
|
import akka.actor.Props;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
|
||||||
|
public class SpringExtension extends AbstractExtensionId<SpringExtension.SpringExt> {
|
||||||
|
|
||||||
|
public static final SpringExtension SPRING_EXTENSION_PROVIDER = new SpringExtension();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SpringExt createExtension(ExtendedActorSystem system) {
|
||||||
|
return new SpringExt();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class SpringExt implements Extension {
|
||||||
|
|
||||||
|
private volatile ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
public void initialize(ApplicationContext applicationContext) {
|
||||||
|
this.applicationContext = applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Props props(String actorBeanName) {
|
||||||
|
return Props.create(SpringActorProducer.class, applicationContext, actorBeanName);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
package com.baeldung.controller;
|
package org.baeldung.controller.controller;
|
||||||
|
|
||||||
import com.baeldung.student.Student;
|
import org.baeldung.controller.student.Student;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
|
@ -1,6 +1,6 @@
|
||||||
package com.baeldung.controller;
|
package org.baeldung.controller.controller;
|
||||||
|
|
||||||
import com.baeldung.student.Student;
|
import org.baeldung.controller.student.Student;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
@ -2,7 +2,7 @@
|
||||||
/**
|
/**
|
||||||
* @author Prashant Dutta
|
* @author Prashant Dutta
|
||||||
*/
|
*/
|
||||||
package com.baeldung.controller;
|
package org.baeldung.controller.controller;
|
||||||
|
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.student;
|
package org.baeldung.controller.student;
|
||||||
|
|
||||||
public class Student {
|
public class Student {
|
||||||
private String name;
|
private String name;
|
|
@ -10,7 +10,7 @@
|
||||||
http://www.springframework.org/schema/context/spring-context-4.0.xsd
|
http://www.springframework.org/schema/context/spring-context-4.0.xsd
|
||||||
http://www.springframework.org/schema/mvc
|
http://www.springframework.org/schema/mvc
|
||||||
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
|
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
|
||||||
<context:component-scan base-package="com.baeldung.controller" />
|
<context:component-scan base-package="org.baeldung.controller.controller" />
|
||||||
<mvc:annotation-driven />
|
<mvc:annotation-driven />
|
||||||
|
|
||||||
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
|
@ -0,0 +1,48 @@
|
||||||
|
package org.baeldung.akka;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import akka.actor.ActorRef;
|
||||||
|
import akka.actor.ActorSystem;
|
||||||
|
import akka.util.Timeout;
|
||||||
|
import org.baeldung.akka.GreetingActor.Greet;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
|
||||||
|
import scala.concurrent.Await;
|
||||||
|
import scala.concurrent.Future;
|
||||||
|
import scala.concurrent.duration.FiniteDuration;
|
||||||
|
|
||||||
|
import static akka.pattern.Patterns.ask;
|
||||||
|
import static org.baeldung.akka.SpringExtension.SPRING_EXTENSION_PROVIDER;
|
||||||
|
|
||||||
|
@ContextConfiguration(classes = AppConfiguration.class)
|
||||||
|
public class SpringAkkaTest extends AbstractJUnit4SpringContextTests {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ActorSystem system;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCallingGreetingActor_thenActorGreetsTheCaller() throws Exception {
|
||||||
|
ActorRef greeter = system.actorOf(
|
||||||
|
SPRING_EXTENSION_PROVIDER.get(system)
|
||||||
|
.props("greetingActor"), "greeter");
|
||||||
|
|
||||||
|
FiniteDuration duration = FiniteDuration.create(1, TimeUnit.SECONDS);
|
||||||
|
Timeout timeout = Timeout.durationToTimeout(duration);
|
||||||
|
|
||||||
|
Future<Object> result = ask(greeter, new Greet("John"), timeout);
|
||||||
|
|
||||||
|
Assert.assertEquals("Hello, John", Await.result(result, duration));
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
system.shutdown();
|
||||||
|
system.awaitTermination();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.test;
|
package org.baeldung.controller;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
@ -14,7 +14,7 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
import com.baeldung.student.Student;
|
import org.baeldung.controller.student.Student;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
|
@ -1,56 +0,0 @@
|
||||||
<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>test</groupId>
|
|
||||||
<artifactId>spring-controller</artifactId>
|
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
|
||||||
<packaging>war</packaging>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-webmvc</artifactId>
|
|
||||||
<version>4.3.0.RELEASE</version>
|
|
||||||
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>javax.servlet</groupId>
|
|
||||||
<artifactId>javax.servlet-api</artifactId>
|
|
||||||
<version>3.0.1</version>
|
|
||||||
<scope>compile</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
|
||||||
<artifactId>jackson-databind</artifactId>
|
|
||||||
<version>2.6.3</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
|
||||||
<artifactId>jackson-annotations</artifactId>
|
|
||||||
<version>2.6.3</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
|
||||||
<artifactId>jackson-core</artifactId>
|
|
||||||
<version>2.6.3</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-web</artifactId>
|
|
||||||
<version>4.3.0.RELEASE</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>junit</groupId>
|
|
||||||
<artifactId>junit</artifactId>
|
|
||||||
<version>4.12</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-test</artifactId>
|
|
||||||
<version>4.2.6.RELEASE</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
</project>
|
|
|
@ -3,12 +3,12 @@
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>com.example</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>demo</artifactId>
|
<artifactId>spring-cucumber</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>spring-cucumber-demo</name>
|
<name>spring-cucumber</name>
|
||||||
<description>Demo project for Spring Boot</description>
|
<description>Demo project for Spring Boot</description>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
|
@ -130,6 +130,11 @@
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.el</groupId>
|
||||||
|
<artifactId>el-api</artifactId>
|
||||||
|
<version>2.2</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -0,0 +1,76 @@
|
||||||
|
package org.baeldung.ex.nontransientexception.cause;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||||
|
import org.springframework.orm.hibernate4.HibernateTransactionManager;
|
||||||
|
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
|
||||||
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableTransactionManagement
|
||||||
|
@PropertySource({ "classpath:persistence-mysql.properties" })
|
||||||
|
@ComponentScan({ "org.baeldung.persistence" })
|
||||||
|
public class Cause1NonTransientConfig {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Environment env;
|
||||||
|
|
||||||
|
public Cause1NonTransientConfig() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public LocalSessionFactoryBean sessionFactory() {
|
||||||
|
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
|
||||||
|
sessionFactory.setDataSource(restDataSource());
|
||||||
|
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
|
||||||
|
sessionFactory.setHibernateProperties(hibernateProperties());
|
||||||
|
|
||||||
|
return sessionFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public DataSource restDataSource() {
|
||||||
|
final BasicDataSource dataSource = new BasicDataSource();
|
||||||
|
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
|
||||||
|
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
|
||||||
|
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
|
||||||
|
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
|
||||||
|
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public HibernateTransactionManager transactionManager() {
|
||||||
|
final HibernateTransactionManager txManager = new HibernateTransactionManager();
|
||||||
|
txManager.setSessionFactory(sessionFactory().getObject());
|
||||||
|
|
||||||
|
return txManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
||||||
|
return new PersistenceExceptionTranslationPostProcessor();
|
||||||
|
}
|
||||||
|
|
||||||
|
final Properties hibernateProperties() {
|
||||||
|
final Properties hibernateProperties = new Properties();
|
||||||
|
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||||
|
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||||
|
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
|
||||||
|
return hibernateProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,76 @@
|
||||||
|
package org.baeldung.ex.nontransientexception.cause;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||||
|
import org.springframework.orm.hibernate4.HibernateTransactionManager;
|
||||||
|
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
|
||||||
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableTransactionManagement
|
||||||
|
@PropertySource({ "classpath:persistence-mysql.properties" })
|
||||||
|
@ComponentScan({ "org.baeldung.persistence" })
|
||||||
|
public class Cause4NonTransientConfig {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Environment env;
|
||||||
|
|
||||||
|
public Cause4NonTransientConfig() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public LocalSessionFactoryBean sessionFactory() {
|
||||||
|
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
|
||||||
|
sessionFactory.setDataSource(restDataSource());
|
||||||
|
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
|
||||||
|
sessionFactory.setHibernateProperties(hibernateProperties());
|
||||||
|
|
||||||
|
return sessionFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public DataSource restDataSource() {
|
||||||
|
final BasicDataSource dataSource = new BasicDataSource();
|
||||||
|
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
|
||||||
|
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
|
||||||
|
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
|
||||||
|
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
|
||||||
|
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public HibernateTransactionManager transactionManager() {
|
||||||
|
final HibernateTransactionManager txManager = new HibernateTransactionManager();
|
||||||
|
txManager.setSessionFactory(sessionFactory().getObject());
|
||||||
|
|
||||||
|
return txManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
||||||
|
return new PersistenceExceptionTranslationPostProcessor();
|
||||||
|
}
|
||||||
|
|
||||||
|
final Properties hibernateProperties() {
|
||||||
|
final Properties hibernateProperties = new Properties();
|
||||||
|
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||||
|
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||||
|
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
|
||||||
|
return hibernateProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
package org.baeldung.ex.nontransientdataaccessexception;
|
||||||
|
|
||||||
|
import org.baeldung.ex.nontransientexception.cause.Cause1NonTransientConfig;
|
||||||
|
import org.baeldung.persistence.model.Foo;
|
||||||
|
import org.baeldung.persistence.service.IFooService;
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.dao.CleanupFailureDataAccessException;
|
||||||
|
import org.springframework.dao.NonTransientDataAccessException;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(classes = { Cause1NonTransientConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
|
public class CleanupFailureExceptionTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SessionFactory sessionFactory;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private IFooService fooService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCleanupAfterSaving_thenCleanupException() {
|
||||||
|
try {
|
||||||
|
final Foo fooEntity = new Foo("foo");
|
||||||
|
fooService.create(fooEntity);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
sessionFactory.close();
|
||||||
|
} catch (final NonTransientDataAccessException exc) {
|
||||||
|
throw new CleanupFailureDataAccessException("Closing connection failed", exc.getCause());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package org.baeldung.ex.nontransientdataaccessexception;
|
||||||
|
|
||||||
|
import org.baeldung.ex.nontransientexception.cause.Cause1NonTransientConfig;
|
||||||
|
import org.baeldung.persistence.model.Foo;
|
||||||
|
import org.baeldung.persistence.service.IFooService;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(classes = { Cause1NonTransientConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
|
public class DataIntegrityExceptionTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private IFooService fooService;
|
||||||
|
|
||||||
|
@Test(expected = DataIntegrityViolationException.class)
|
||||||
|
public void whenSavingNullValue_thenDataIntegrityException() {
|
||||||
|
final Foo fooEntity = new Foo();
|
||||||
|
fooService.create(fooEntity);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package org.baeldung.ex.nontransientdataaccessexception;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.baeldung.ex.nontransientexception.cause.Cause1NonTransientConfig;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.dao.DataRetrievalFailureException;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(classes = { Cause1NonTransientConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
|
public class DataRetrievalExceptionTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DataSource restDataSource;
|
||||||
|
|
||||||
|
@Test(expected = DataRetrievalFailureException.class)
|
||||||
|
public void whenRetrievingNonExistentValue_thenDataRetrievalException() {
|
||||||
|
final JdbcTemplate jdbcTemplate = new JdbcTemplate(restDataSource);
|
||||||
|
|
||||||
|
jdbcTemplate.queryForObject("select * from foo where id=3", Integer.class);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package org.baeldung.ex.nontransientdataaccessexception;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.baeldung.ex.nontransientexception.cause.Cause4NonTransientConfig;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException;
|
||||||
|
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(classes = { Cause4NonTransientConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
|
public class DataSourceLookupExceptionTest {
|
||||||
|
|
||||||
|
@Test(expected = DataSourceLookupFailureException.class)
|
||||||
|
public void whenLookupNonExistentDataSource_thenDataSourceLookupFailureException() {
|
||||||
|
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
|
||||||
|
dsLookup.setResourceRef(true);
|
||||||
|
final DataSource dataSource = dsLookup.getDataSource("java:comp/env/jdbc/example_db");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package org.baeldung.ex.nontransientdataaccessexception;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.baeldung.ex.nontransientexception.cause.Cause1NonTransientConfig;
|
||||||
|
import org.baeldung.persistence.service.IFooService;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.dao.InvalidDataAccessResourceUsageException;
|
||||||
|
import org.springframework.jdbc.BadSqlGrammarException;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(classes = { Cause1NonTransientConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
|
public class InvalidResourceUsageExceptionTest {
|
||||||
|
@Autowired
|
||||||
|
private IFooService fooService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DataSource restDataSource;
|
||||||
|
|
||||||
|
@Test(expected = InvalidDataAccessResourceUsageException.class)
|
||||||
|
public void whenRetrievingDataUserNoSelectRights_thenInvalidResourceUsageException() {
|
||||||
|
fooService.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = BadSqlGrammarException.class)
|
||||||
|
public void whenIncorrectSql_thenBadSqlGrammarException() {
|
||||||
|
final JdbcTemplate jdbcTemplate = new JdbcTemplate(restDataSource);
|
||||||
|
|
||||||
|
jdbcTemplate.queryForObject("select * fro foo where id=3", Integer.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package org.baeldung.ex.nontransientdataaccessexception;
|
||||||
|
|
||||||
|
import org.baeldung.ex.nontransientexception.cause.Cause1NonTransientConfig;
|
||||||
|
import org.baeldung.persistence.model.Foo;
|
||||||
|
import org.baeldung.persistence.service.IFooService;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.dao.PermissionDeniedDataAccessException;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(classes = { Cause1NonTransientConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
|
public class PermissionDeniedException {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private IFooService fooService;
|
||||||
|
|
||||||
|
@Test(expected = PermissionDeniedDataAccessException.class)
|
||||||
|
public void whenRetrievingDataUserNoSelectRights_thenPermissionDeniedException() {
|
||||||
|
final Foo foo = new Foo("foo");
|
||||||
|
fooService.create(foo);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -10,21 +10,33 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Controller @RequestMapping("/") public class MainController {
|
@Controller
|
||||||
|
@RequestMapping("/")
|
||||||
|
public class MainController {
|
||||||
|
|
||||||
private final ITutorialsService tutService;
|
@Autowired
|
||||||
|
private ITutorialsService tutService;
|
||||||
|
|
||||||
@Autowired public MainController(ITutorialsService tutService) {
|
@RequestMapping(value ="/", method = RequestMethod.GET)
|
||||||
this.tutService = tutService;
|
public String welcomePage() {
|
||||||
|
return "index";
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.GET) public String listTutorialsPage(Model model) {
|
|
||||||
|
@RequestMapping(value ="/list", method = RequestMethod.GET)
|
||||||
|
public String listTutorialsPage(Model model) {
|
||||||
List<Tutorial> list = tutService.listTutorials();
|
List<Tutorial> list = tutService.listTutorials();
|
||||||
model.addAttribute("tutorials", list);
|
model.addAttribute("tutorials", list);
|
||||||
return "index";
|
return "list";
|
||||||
}
|
}
|
||||||
|
|
||||||
public ITutorialsService getTutService() {
|
public ITutorialsService getTutService() {
|
||||||
return tutService;
|
return tutService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setTutService(ITutorialsService tutService) {
|
||||||
|
this.tutService = tutService;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -13,15 +13,12 @@ import java.util.Set;
|
||||||
|
|
||||||
public class MainWebAppInitializer implements WebApplicationInitializer {
|
public class MainWebAppInitializer implements WebApplicationInitializer {
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* Register and configure all Servlet container components necessary to
|
public void onStartup(final ServletContext sc) throws ServletException {
|
||||||
* power the web application.
|
|
||||||
*/
|
|
||||||
@Override public void onStartup(final ServletContext sc) throws ServletException {
|
|
||||||
|
|
||||||
// Create the 'root' Spring application context
|
// Create the 'root' Spring application context
|
||||||
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
|
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
|
||||||
root.register(WebConfig.class, SpringConfig.class);
|
root.register(WebConfig.class);
|
||||||
|
|
||||||
// Manages the lifecycle of the root application context
|
// Manages the lifecycle of the root application context
|
||||||
sc.addListener(new ContextLoaderListener(root));
|
sc.addListener(new ContextLoaderListener(root));
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
package com.baeldung.mvc.velocity.spring.config;
|
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
import com.baeldung.mvc.velocity.service.TutorialsService;
|
|
||||||
|
|
||||||
@Configuration public class SpringConfig {
|
|
||||||
|
|
||||||
@Bean public TutorialsService tutService() {
|
|
||||||
return new TutorialsService();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -11,21 +11,23 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
|
||||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
||||||
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
|
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
|
||||||
|
|
||||||
@Configuration @EnableWebMvc @ComponentScan(basePackages = { "com.baeldung.mvc.velocity.controller" }) public class WebConfig extends WebMvcConfigurerAdapter {
|
@Configuration
|
||||||
|
@EnableWebMvc
|
||||||
|
@ComponentScan(basePackages = { "com.baeldung.mvc.velocity.controller", "com.baeldung.mvc.velocity.service"})
|
||||||
|
public class WebConfig extends WebMvcConfigurerAdapter {
|
||||||
|
|
||||||
public WebConfig() {
|
@Override
|
||||||
super();
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||||
}
|
|
||||||
|
|
||||||
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
|
||||||
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
|
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
|
@Override
|
||||||
|
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
|
||||||
configurer.enable();
|
configurer.enable();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean public ViewResolver viewResolver() {
|
@Bean
|
||||||
|
public ViewResolver viewResolver() {
|
||||||
final VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
|
final VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
|
||||||
bean.setCache(true);
|
bean.setCache(true);
|
||||||
bean.setPrefix("/WEB-INF/views/");
|
bean.setPrefix("/WEB-INF/views/");
|
||||||
|
@ -34,7 +36,8 @@ import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
|
||||||
return bean;
|
return bean;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean public VelocityConfigurer velocityConfig() {
|
@Bean
|
||||||
|
public VelocityConfigurer velocityConfig() {
|
||||||
VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
|
VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
|
||||||
velocityConfigurer.setResourceLoaderPath("/");
|
velocityConfigurer.setResourceLoaderPath("/");
|
||||||
return velocityConfigurer;
|
return velocityConfigurer;
|
||||||
|
|
|
@ -1,19 +1,4 @@
|
||||||
<h1>Index</h1>
|
<h1>Index</h1>
|
||||||
|
|
||||||
<h2>Tutorials list</h2>
|
<h2>Welcome page</h2>
|
||||||
<table border="1">
|
This is just the welcome page
|
||||||
<tr>
|
|
||||||
<th>Tutorial Id</th>
|
|
||||||
<th>Tutorial Title</th>
|
|
||||||
<th>Tutorial Description</th>
|
|
||||||
<th>Tutorial Author</th>
|
|
||||||
</tr>
|
|
||||||
#foreach($tut in $tutorials)
|
|
||||||
<tr>
|
|
||||||
<td id="tutId_$foreach.count">$tut.tutId</td>
|
|
||||||
<td id="title_$foreach.count">$tut.title</td>
|
|
||||||
<td id="desc_$foreach.count">$tut.description</td>
|
|
||||||
<td id="auth_$foreach.count">$tut.author</td>
|
|
||||||
</tr>
|
|
||||||
#end
|
|
||||||
</table>
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
<h1>Index</h1>
|
||||||
|
|
||||||
|
<h2>Tutorials list</h2>
|
||||||
|
<table border="1">
|
||||||
|
<tr>
|
||||||
|
<th>Tutorial Id</th>
|
||||||
|
<th>Tutorial Title</th>
|
||||||
|
<th>Tutorial Description</th>
|
||||||
|
<th>Tutorial Author</th>
|
||||||
|
</tr>
|
||||||
|
#foreach($tut in $tutorials)
|
||||||
|
<tr>
|
||||||
|
<td id="tutId_$foreach.count">$tut.tutId</td>
|
||||||
|
<td id="title_$foreach.count">$tut.title</td>
|
||||||
|
<td id="desc_$foreach.count">$tut.description</td>
|
||||||
|
<td id="auth_$foreach.count">$tut.author</td>
|
||||||
|
</tr>
|
||||||
|
#end
|
||||||
|
</table>
|
|
@ -1,23 +1,5 @@
|
||||||
package com.baeldung.mvc.velocity.test;
|
package com.baeldung.mvc.velocity.test;
|
||||||
|
|
||||||
import com.baeldung.mvc.velocity.domain.Tutorial;
|
|
||||||
import com.baeldung.mvc.velocity.service.ITutorialsService;
|
|
||||||
import com.baeldung.mvc.velocity.spring.config.WebConfig;
|
|
||||||
import com.baeldung.mvc.velocity.test.config.TestConfig;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.mockito.Mockito;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|
||||||
import org.springframework.test.context.web.WebAppConfiguration;
|
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
|
||||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.allOf;
|
import static org.hamcrest.Matchers.allOf;
|
||||||
import static org.hamcrest.Matchers.hasItem;
|
import static org.hamcrest.Matchers.hasItem;
|
||||||
import static org.hamcrest.Matchers.hasProperty;
|
import static org.hamcrest.Matchers.hasProperty;
|
||||||
|
@ -29,45 +11,51 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
|
import com.baeldung.mvc.velocity.spring.config.WebConfig;
|
||||||
|
import com.baeldung.mvc.velocity.test.config.TestConfig;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
// @ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
|
// @ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
|
||||||
@ContextConfiguration(classes = { TestConfig.class, WebConfig.class }) @WebAppConfiguration public class DataContentControllerTest {
|
@ContextConfiguration(classes = { TestConfig.class, WebConfig.class })
|
||||||
|
@WebAppConfiguration
|
||||||
|
public class DataContentControllerTest {
|
||||||
|
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
@Autowired private ITutorialsService tutServiceMock;
|
@Autowired
|
||||||
|
private WebApplicationContext webApplicationContext;
|
||||||
|
|
||||||
@Autowired private WebApplicationContext webApplicationContext;
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
|
||||||
@Before public void setUp() {
|
|
||||||
Mockito.reset(tutServiceMock);
|
|
||||||
|
|
||||||
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
|
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void testModel() throws Exception {
|
@Test
|
||||||
|
public void whenCallingList_ThenModelAndContentOK() throws Exception {
|
||||||
|
|
||||||
Mockito.when(tutServiceMock.listTutorials())
|
mockMvc.perform(get("/list")).andExpect(status().isOk()).andExpect(view().name("list")).andExpect(model().attribute("tutorials", hasSize(2)))
|
||||||
.thenReturn(Arrays.asList(
|
.andExpect(model().attribute("tutorials", hasItem(allOf(hasProperty("tutId", is(1)), hasProperty("author", is("GuavaAuthor")), hasProperty("title", is("Guava"))))))
|
||||||
new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"),
|
.andExpect(model().attribute("tutorials", hasItem(allOf(hasProperty("tutId", is(2)), hasProperty("author", is("AndroidAuthor")), hasProperty("title", is("Android"))))));
|
||||||
new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor")));
|
|
||||||
|
|
||||||
mockMvc.perform(get("/"))
|
mockMvc.perform(get("/list")).andExpect(xpath("//table").exists());
|
||||||
.andExpect(status().isOk()).andExpect(view().name("index"))
|
mockMvc.perform(get("/list")).andExpect(xpath("//td[@id='tutId_1']").exists());
|
||||||
.andExpect(model().attribute("tutorials", hasSize(2)))
|
}
|
||||||
.andExpect(model()
|
|
||||||
.attribute("tutorials",
|
|
||||||
hasItem(allOf(hasProperty("tutId", is(1)),
|
|
||||||
hasProperty("author", is("GuavaAuthor")),
|
|
||||||
hasProperty("title", is("Guava"))))))
|
|
||||||
.andExpect(model()
|
|
||||||
.attribute("tutorials", hasItem(allOf(hasProperty("tutId", is(2)),
|
|
||||||
hasProperty("author", is("AndroidAuthor")),
|
|
||||||
hasProperty("title", is("Android"))))));
|
|
||||||
|
|
||||||
mockMvc.perform(get("/"))
|
@Test
|
||||||
.andExpect(xpath("//table").exists());
|
public void whenCallingIndex_thenViewOK() throws Exception{
|
||||||
mockMvc.perform(get("/"))
|
mockMvc.perform(get("/")).andExpect(status().isOk()).andExpect(view().name("index")).andExpect(model().size(0));
|
||||||
.andExpect(xpath("//td[@id='tutId_1']").exists());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,65 +0,0 @@
|
||||||
package com.baeldung.mvc.velocity.test;
|
|
||||||
|
|
||||||
import com.baeldung.mvc.velocity.controller.MainController;
|
|
||||||
import com.baeldung.mvc.velocity.domain.Tutorial;
|
|
||||||
import com.baeldung.mvc.velocity.service.TutorialsService;
|
|
||||||
import com.baeldung.mvc.velocity.test.config.TestConfig;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.mockito.Mockito;
|
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|
||||||
import org.springframework.test.context.web.WebAppConfiguration;
|
|
||||||
import org.springframework.ui.ExtendedModelMap;
|
|
||||||
import org.springframework.ui.Model;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.mockito.Mockito.times;
|
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
|
||||||
@WebAppConfiguration
|
|
||||||
@ContextConfiguration(classes = { TestConfig.class })
|
|
||||||
public class NavigationControllerTest {
|
|
||||||
|
|
||||||
private MainController mainController = new MainController(Mockito.mock(TutorialsService.class));
|
|
||||||
|
|
||||||
private final Model model = new ExtendedModelMap();
|
|
||||||
|
|
||||||
@Test public void shouldGoToTutorialListView() {
|
|
||||||
Mockito.when(mainController.getTutService().listTutorials()).thenReturn(createTutorialList());
|
|
||||||
|
|
||||||
final String view = mainController.listTutorialsPage(model);
|
|
||||||
final List<Tutorial> tutorialListAttribute = (List<Tutorial>) model.asMap().get("tutorials");
|
|
||||||
|
|
||||||
assertEquals("index", view);
|
|
||||||
assertNotNull(tutorialListAttribute);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test public void testContent() throws Exception {
|
|
||||||
|
|
||||||
List<Tutorial> tutorials = Arrays.asList(
|
|
||||||
new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"),
|
|
||||||
new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor"));
|
|
||||||
|
|
||||||
Mockito.when(mainController.getTutService().listTutorials())
|
|
||||||
.thenReturn(tutorials);
|
|
||||||
|
|
||||||
String view = mainController.listTutorialsPage(model);
|
|
||||||
|
|
||||||
verify(mainController.getTutService(), times(1)).listTutorials();
|
|
||||||
verifyNoMoreInteractions(mainController.getTutService());
|
|
||||||
|
|
||||||
assertEquals("index", view);
|
|
||||||
assertEquals(tutorials, model.asMap().get("tutorials"));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<Tutorial> createTutorialList() {
|
|
||||||
return Arrays.asList(new Tutorial(1, "TestAuthor", "Test Title", "Test Description"));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,14 +1,11 @@
|
||||||
package com.baeldung.mvc.velocity.test.config;
|
package com.baeldung.mvc.velocity.test.config;
|
||||||
|
|
||||||
import org.mockito.Mockito;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.web.servlet.ViewResolver;
|
import org.springframework.web.servlet.ViewResolver;
|
||||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
||||||
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
|
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
|
||||||
|
|
||||||
import com.baeldung.mvc.velocity.service.ITutorialsService;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class TestConfig {
|
public class TestConfig {
|
||||||
|
|
||||||
|
@ -30,9 +27,6 @@ public class TestConfig {
|
||||||
return velocityConfigurer;
|
return velocityConfigurer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
|
||||||
public ITutorialsService getTutServiceMock() {
|
|
||||||
return Mockito.mock(ITutorialsService.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,86 @@
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<project
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>angular-spring-rest-sample</artifactId>
|
||||||
|
<name>angular-spring-rest-sample</name>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
<packaging>war</packaging>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>1.3.3.RELEASE</version>
|
||||||
|
</parent>
|
||||||
|
<properties>
|
||||||
|
<spring.data.version>1.12.2.RELEASE</spring.data.version>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.data</groupId>
|
||||||
|
<artifactId>spring-data-commons</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>3.3</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.guava</groupId>
|
||||||
|
<artifactId>guava</artifactId>
|
||||||
|
<version>19.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.rest-assured</groupId>
|
||||||
|
<artifactId>rest-assured</artifactId>
|
||||||
|
<version>3.0.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.rest-assured</groupId>
|
||||||
|
<artifactId>spring-mock-mvc</artifactId>
|
||||||
|
<version>3.0.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<finalName>angular-spring-rest-sample</finalName>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
|
@ -0,0 +1,39 @@
|
||||||
|
package org.baeldung.mock;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.baeldung.web.vo.Student;
|
||||||
|
|
||||||
|
public class MockStudentData {
|
||||||
|
|
||||||
|
private static List<Student> studentList = new ArrayList<>();
|
||||||
|
|
||||||
|
static {
|
||||||
|
studentList.add(new Student("1", "Bryan", "Male", 20));
|
||||||
|
studentList.add(new Student("2", "Ben", "Male", 22));
|
||||||
|
studentList.add(new Student("3", "Lisa", "Female", 24));
|
||||||
|
studentList.add(new Student("4", "Sarah", "Female", 26));
|
||||||
|
studentList.add(new Student("5", "Jay", "Male", 20));
|
||||||
|
studentList.add(new Student("6", "John", "Male", 22));
|
||||||
|
studentList.add(new Student("7", "Jordan", "Male", 24));
|
||||||
|
studentList.add(new Student("8", "Rob", "Male", 26));
|
||||||
|
studentList.add(new Student("9", "Will", "Male", 20));
|
||||||
|
studentList.add(new Student("10", "Shawn", "Male", 22));
|
||||||
|
studentList.add(new Student("11", "Taylor", "Female", 24));
|
||||||
|
studentList.add(new Student("12", "Venus", "Female", 26));
|
||||||
|
studentList.add(new Student("13", "Vince", "Male", 20));
|
||||||
|
studentList.add(new Student("14", "Carol", "Female", 22));
|
||||||
|
studentList.add(new Student("15", "Joana", "Female", 24));
|
||||||
|
studentList.add(new Student("16", "Dion", "Male", 26));
|
||||||
|
studentList.add(new Student("17", "Evans", "Male", 20));
|
||||||
|
studentList.add(new Student("18", "Bart", "Male", 22));
|
||||||
|
studentList.add(new Student("19", "Jenny", "Female", 24));
|
||||||
|
studentList.add(new Student("20", "Kristine", "Female", 26));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Student> getMockDataStudents(){
|
||||||
|
return studentList;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package org.baeldung.web.exception;
|
||||||
|
|
||||||
|
public class MyResourceNotFoundException extends RuntimeException {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = 4088649120307193208L;
|
||||||
|
|
||||||
|
public MyResourceNotFoundException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public MyResourceNotFoundException(final String message, final Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MyResourceNotFoundException(final String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MyResourceNotFoundException(final Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package org.baeldung.web.main;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.web.filter.ShallowEtagHeaderFilter;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@ComponentScan("org.baeldung")
|
||||||
|
public class Application extends WebMvcConfigurerAdapter {
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Application.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ShallowEtagHeaderFilter shallowEtagHeaderFilter() {
|
||||||
|
return new ShallowEtagHeaderFilter();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package org.baeldung.web.rest;
|
||||||
|
|
||||||
|
import org.baeldung.web.service.StudentService;
|
||||||
|
import org.baeldung.web.vo.Student;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class StudentDirectoryRestController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private StudentService service;
|
||||||
|
|
||||||
|
@RequestMapping(value = "/student/get", params = { "page", "size" }, method = RequestMethod.GET)
|
||||||
|
public Page<Student> findPaginated(@RequestParam("page") int page, @RequestParam("size") int size){
|
||||||
|
|
||||||
|
Page<Student> resultPage = service.findPaginated(page, size);
|
||||||
|
|
||||||
|
return resultPage;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package org.baeldung.web.service;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
|
||||||
|
public interface IOperations<T> {
|
||||||
|
|
||||||
|
Page<T> findPaginated(int page, int size);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package org.baeldung.web.service;
|
||||||
|
|
||||||
|
import org.baeldung.web.vo.Student;
|
||||||
|
|
||||||
|
public interface StudentService extends IOperations<Student>{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
package org.baeldung.web.service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.baeldung.mock.MockStudentData;
|
||||||
|
import org.baeldung.web.exception.MyResourceNotFoundException;
|
||||||
|
import org.baeldung.web.vo.Student;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageImpl;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class StudentServiceImpl implements StudentService {
|
||||||
|
|
||||||
|
private List<Student> mockDataStudent = MockStudentData.getMockDataStudents();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page<Student> findPaginated(int page, int size){
|
||||||
|
Page<Student> studentPage = getPage(page, size);
|
||||||
|
return studentPage;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Page<Student> getPage(int page, int size) {
|
||||||
|
page = page != 0?page - 1:page;
|
||||||
|
int from = Math.max(0, page * size);
|
||||||
|
int to = Math.min(mockDataStudent.size(), (page + 1) * size);
|
||||||
|
if(from > to){
|
||||||
|
throw new MyResourceNotFoundException("page number is higher than total pages.");
|
||||||
|
}
|
||||||
|
return new PageImpl<Student>(mockDataStudent.subList(from, to),
|
||||||
|
new PageRequest(page,size),
|
||||||
|
mockDataStudent.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,60 @@
|
||||||
|
package org.baeldung.web.vo;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class Student implements Serializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public Student() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Student(String studentId, String name, String gender, Integer age) {
|
||||||
|
super();
|
||||||
|
this.studentId = studentId;
|
||||||
|
this.name = name;
|
||||||
|
this.gender = gender;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String studentId;
|
||||||
|
private String name;
|
||||||
|
private String gender;
|
||||||
|
private Integer age;
|
||||||
|
|
||||||
|
public String getStudentId() {
|
||||||
|
return studentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStudentId(String studentId) {
|
||||||
|
this.studentId = studentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGender() {
|
||||||
|
return gender;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGender(String gender) {
|
||||||
|
this.gender = gender;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(Integer age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
server.contextPath=/StudentDirectory
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
|
||||||
|
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
|
||||||
|
version="2.5">
|
||||||
|
|
||||||
|
<welcome-file-list>
|
||||||
|
<welcome-file>index.html</welcome-file>
|
||||||
|
</welcome-file-list>
|
||||||
|
|
||||||
|
</web-app>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en" ng-app="app">
|
||||||
|
<head>
|
||||||
|
<link rel="stylesheet" href="https://cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.css">
|
||||||
|
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
|
||||||
|
<script src="https://cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.js"></script>
|
||||||
|
<script src="view/app.js"></script>
|
||||||
|
</head>
|
||||||
|
<body >
|
||||||
|
<div ng-controller="StudentCtrl as vm">
|
||||||
|
<div ui-grid="gridOptions" class="grid" ui-grid-pagination></div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -0,0 +1,54 @@
|
||||||
|
var app = angular.module('app', ['ui.grid','ui.grid.pagination']);
|
||||||
|
|
||||||
|
app.controller('StudentCtrl', ['$scope','StudentService', function ($scope,StudentService) {
|
||||||
|
var paginationOptions = {
|
||||||
|
pageNumber: 1,
|
||||||
|
pageSize: 5,
|
||||||
|
sort: null
|
||||||
|
};
|
||||||
|
|
||||||
|
StudentService.getStudents(paginationOptions.pageNumber,
|
||||||
|
paginationOptions.pageSize).success(function(data){
|
||||||
|
$scope.gridOptions.data = data.content;
|
||||||
|
$scope.gridOptions.totalItems = data.totalElements;
|
||||||
|
});
|
||||||
|
|
||||||
|
$scope.gridOptions = {
|
||||||
|
paginationPageSizes: [5, 10, 20],
|
||||||
|
paginationPageSize: paginationOptions.pageSize,
|
||||||
|
enableColumnMenus:false,
|
||||||
|
columnDefs: [
|
||||||
|
{ name: 'studentId' },
|
||||||
|
{ name: 'name' },
|
||||||
|
{ name: 'gender' },
|
||||||
|
{ name: 'age' }
|
||||||
|
],
|
||||||
|
onRegisterApi: function(gridApi) {
|
||||||
|
$scope.gridApi = gridApi;
|
||||||
|
gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
|
||||||
|
paginationOptions.pageNumber = newPage;
|
||||||
|
paginationOptions.pageSize = pageSize;
|
||||||
|
StudentService.getStudents(newPage,pageSize).success(function(data){
|
||||||
|
$scope.gridOptions.data = data.content;
|
||||||
|
$scope.gridOptions.totalItems = data.totalElements;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}]);
|
||||||
|
|
||||||
|
app.service('StudentService',['$http', function ($http) {
|
||||||
|
|
||||||
|
function getStudents(pageNumber,size) {
|
||||||
|
return $http({
|
||||||
|
method: 'GET',
|
||||||
|
url: 'student/get?page='+pageNumber+'&size='+size
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
getStudents:getStudents
|
||||||
|
};
|
||||||
|
|
||||||
|
}]);
|
|
@ -0,0 +1,49 @@
|
||||||
|
package org.baeldung.web.service;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.RandomStringUtils;
|
||||||
|
import org.baeldung.web.main.Application;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.IntegrationTest;
|
||||||
|
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
|
|
||||||
|
import io.restassured.RestAssured;
|
||||||
|
import io.restassured.response.Response;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@SpringApplicationConfiguration(classes = Application.class)
|
||||||
|
@WebAppConfiguration
|
||||||
|
@IntegrationTest("server.port:8080")
|
||||||
|
public class StudentServiceTest{
|
||||||
|
|
||||||
|
private String getURL() {
|
||||||
|
return "/StudentDirectory/student/get";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenResourcesAreRetrievedPaged_then200IsReceived(){
|
||||||
|
Response response = RestAssured.given().get(getURL()+ "?page=0&size=2").andReturn();
|
||||||
|
|
||||||
|
assertTrue(response.getStatusCode() == 200 );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenPageOfResourcesAreRetrievedOutOfBounds_then404IsReceived(){
|
||||||
|
String url = getURL()+ "?page=" + RandomStringUtils.randomNumeric(5) + "&size=2";
|
||||||
|
Response response = RestAssured.given().get(url);
|
||||||
|
|
||||||
|
assertTrue(response.getStatusCode() == 500 );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources(){
|
||||||
|
Response response = RestAssured.given().get(getURL() + "?page=1&size=2" );
|
||||||
|
assertFalse(response.getBody().jsonPath().getList("content").isEmpty() );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue