Merge branch 'master' into master
This commit is contained in:
commit
f9daccc5b2
@ -0,0 +1,40 @@
|
||||
package com.baeldung.xml
|
||||
|
||||
import groovy.xml.MarkupBuilder
|
||||
import groovy.xml.XmlUtil
|
||||
import spock.lang.Specification
|
||||
|
||||
class MarkupBuilderUnitTest extends Specification {
|
||||
|
||||
def xmlFile = getClass().getResource("articles_short_formatted.xml")
|
||||
|
||||
def "Should create XML properly"() {
|
||||
given: "Node structures"
|
||||
|
||||
when: "Using MarkupBuilderUnitTest to create com.baeldung.xml structure"
|
||||
def writer = new StringWriter()
|
||||
new MarkupBuilder(writer).articles {
|
||||
article {
|
||||
title('First steps in Java')
|
||||
author(id: '1') {
|
||||
firstname('Siena')
|
||||
lastname('Kerr')
|
||||
}
|
||||
'release-date'('2018-12-01')
|
||||
}
|
||||
article {
|
||||
title('Dockerize your SpringBoot application')
|
||||
author(id: '2') {
|
||||
firstname('Jonas')
|
||||
lastname('Lugo')
|
||||
}
|
||||
'release-date'('2018-12-01')
|
||||
}
|
||||
}
|
||||
|
||||
then: "Xml is created properly"
|
||||
XmlUtil.serialize(writer.toString()) == XmlUtil.serialize(xmlFile.text)
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,94 @@
|
||||
package com.baeldung.xml
|
||||
|
||||
|
||||
import spock.lang.Shared
|
||||
import spock.lang.Specification
|
||||
|
||||
class XmlParserUnitTest extends Specification {
|
||||
|
||||
def xmlFile = getClass().getResourceAsStream("articles.xml")
|
||||
|
||||
@Shared
|
||||
def parser = new XmlParser()
|
||||
|
||||
def "Should read XML file properly"() {
|
||||
given: "XML file"
|
||||
|
||||
when: "Using XmlParser to read file"
|
||||
def articles = parser.parse(xmlFile)
|
||||
|
||||
then: "Xml is loaded properly"
|
||||
articles.'*'.size() == 4
|
||||
articles.article[0].author.firstname.text() == "Siena"
|
||||
articles.article[2].'release-date'.text() == "2018-06-12"
|
||||
articles.article[3].title.text() == "Java 12 insights"
|
||||
articles.article.find { it.author.'@id'.text() == "3" }.author.firstname.text() == "Daniele"
|
||||
}
|
||||
|
||||
|
||||
def "Should add node to existing com.baeldung.xml using NodeBuilder"() {
|
||||
given: "XML object"
|
||||
def articles = parser.parse(xmlFile)
|
||||
|
||||
when: "Adding node to com.baeldung.xml"
|
||||
def articleNode = new NodeBuilder().article(id: '5') {
|
||||
title('Traversing XML in the nutshell')
|
||||
author {
|
||||
firstname('Martin')
|
||||
lastname('Schmidt')
|
||||
}
|
||||
'release-date'('2019-05-18')
|
||||
}
|
||||
articles.append(articleNode)
|
||||
|
||||
then: "Node is added to com.baeldung.xml properly"
|
||||
articles.'*'.size() == 5
|
||||
articles.article[4].title.text() == "Traversing XML in the nutshell"
|
||||
}
|
||||
|
||||
def "Should replace node"() {
|
||||
given: "XML object"
|
||||
def articles = parser.parse(xmlFile)
|
||||
|
||||
when: "Adding node to com.baeldung.xml"
|
||||
def articleNode = new NodeBuilder().article(id: '5') {
|
||||
title('Traversing XML in the nutshell')
|
||||
author {
|
||||
firstname('Martin')
|
||||
lastname('Schmidt')
|
||||
}
|
||||
'release-date'('2019-05-18')
|
||||
}
|
||||
articles.article[0].replaceNode(articleNode)
|
||||
|
||||
then: "Node is added to com.baeldung.xml properly"
|
||||
articles.'*'.size() == 4
|
||||
articles.article[0].title.text() == "Traversing XML in the nutshell"
|
||||
}
|
||||
|
||||
def "Should modify node"() {
|
||||
given: "XML object"
|
||||
def articles = parser.parse(xmlFile)
|
||||
|
||||
when: "Changing value of one of the nodes"
|
||||
articles.article.each { it.'release-date'[0].value = "2019-05-18" }
|
||||
|
||||
then: "XML is updated"
|
||||
articles.article.findAll { it.'release-date'.text() != "2019-05-18" }.isEmpty()
|
||||
}
|
||||
|
||||
def "Should remove article from com.baeldung.xml"() {
|
||||
given: "XML object"
|
||||
def articles = parser.parse(xmlFile)
|
||||
|
||||
when: "Removing all articles but with id==3"
|
||||
articles.article
|
||||
.findAll { it.author.'@id'.text() != "3" }
|
||||
.each { articles.remove(it) }
|
||||
|
||||
then: "There is only one article left"
|
||||
articles.children().size() == 1
|
||||
articles.article[0].author.'@id'.text() == "3"
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,102 @@
|
||||
package com.baeldung.xml
|
||||
|
||||
|
||||
import groovy.xml.XmlUtil
|
||||
import spock.lang.Shared
|
||||
import spock.lang.Specification
|
||||
|
||||
class XmlSlurperUnitTest extends Specification {
|
||||
|
||||
def xmlFile = getClass().getResourceAsStream("articles.xml")
|
||||
|
||||
@Shared
|
||||
def parser = new XmlSlurper()
|
||||
|
||||
def "Should read XML file properly"() {
|
||||
given: "XML file"
|
||||
|
||||
when: "Using XmlSlurper to read file"
|
||||
def articles = parser.parse(xmlFile)
|
||||
|
||||
then: "Xml is loaded properly"
|
||||
articles.'*'.size() == 4
|
||||
articles.article[0].author.firstname == "Siena"
|
||||
articles.article[2].'release-date' == "2018-06-12"
|
||||
articles.article[3].title == "Java 12 insights"
|
||||
articles.article.find { it.author.'@id' == "3" }.author.firstname == "Daniele"
|
||||
}
|
||||
|
||||
def "Should add node to existing com.baeldung.xml"() {
|
||||
given: "XML object"
|
||||
def articles = parser.parse(xmlFile)
|
||||
|
||||
when: "Adding node to com.baeldung.xml"
|
||||
articles.appendNode {
|
||||
article(id: '5') {
|
||||
title('Traversing XML in the nutshell')
|
||||
author {
|
||||
firstname('Martin')
|
||||
lastname('Schmidt')
|
||||
}
|
||||
'release-date'('2019-05-18')
|
||||
}
|
||||
}
|
||||
|
||||
articles = parser.parseText(XmlUtil.serialize(articles))
|
||||
|
||||
then: "Node is added to com.baeldung.xml properly"
|
||||
articles.'*'.size() == 5
|
||||
articles.article[4].title == "Traversing XML in the nutshell"
|
||||
}
|
||||
|
||||
def "Should modify node"() {
|
||||
given: "XML object"
|
||||
def articles = parser.parse(xmlFile)
|
||||
|
||||
when: "Changing value of one of the nodes"
|
||||
articles.article.each { it.'release-date' = "2019-05-18" }
|
||||
|
||||
then: "XML is updated"
|
||||
articles.article.findAll { it.'release-date' != "2019-05-18" }.isEmpty()
|
||||
}
|
||||
|
||||
def "Should replace node"() {
|
||||
given: "XML object"
|
||||
def articles = parser.parse(xmlFile)
|
||||
|
||||
when: "Replacing node"
|
||||
articles.article[0].replaceNode {
|
||||
article(id: '5') {
|
||||
title('Traversing XML in the nutshell')
|
||||
author {
|
||||
firstname('Martin')
|
||||
lastname('Schmidt')
|
||||
}
|
||||
'release-date'('2019-05-18')
|
||||
}
|
||||
}
|
||||
|
||||
articles = parser.parseText(XmlUtil.serialize(articles))
|
||||
|
||||
then: "Node is replaced properly"
|
||||
articles.'*'.size() == 4
|
||||
articles.article[0].title == "Traversing XML in the nutshell"
|
||||
}
|
||||
|
||||
def "Should remove article from com.baeldung.xml"() {
|
||||
given: "XML object"
|
||||
def articles = parser.parse(xmlFile)
|
||||
|
||||
when: "Removing all articles but with id==3"
|
||||
articles.article
|
||||
.findAll { it.author.'@id' != "3" }
|
||||
.replaceNode {}
|
||||
|
||||
articles = parser.parseText(XmlUtil.serialize(articles))
|
||||
|
||||
then: "There is only one article left"
|
||||
articles.children().size() == 1
|
||||
articles.article[0].author.'@id' == "3"
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
<articles>
|
||||
<article>
|
||||
<title>First steps in Java</title>
|
||||
<author id='1'>
|
||||
<firstname>Siena</firstname>
|
||||
<lastname>Kerr</lastname>
|
||||
</author>
|
||||
<release-date>2018-12-01</release-date>
|
||||
</article>
|
||||
<article>
|
||||
<title>Dockerize your SpringBoot application</title>
|
||||
<author id='2'>
|
||||
<firstname>Jonas</firstname>
|
||||
<lastname>Lugo</lastname>
|
||||
</author>
|
||||
<release-date>2018-12-01</release-date>
|
||||
</article>
|
||||
<article>
|
||||
<title>SpringBoot tutorial</title>
|
||||
<author id='3'>
|
||||
<firstname>Daniele</firstname>
|
||||
<lastname>Ferguson</lastname>
|
||||
</author>
|
||||
<release-date>2018-06-12</release-date>
|
||||
</article>
|
||||
<article>
|
||||
<title>Java 12 insights</title>
|
||||
<author id='1'>
|
||||
<firstname>Siena</firstname>
|
||||
<lastname>Kerr</lastname>
|
||||
</author>
|
||||
<release-date>2018-07-22</release-date>
|
||||
</article>
|
||||
</articles>
|
@ -0,0 +1,18 @@
|
||||
<articles>
|
||||
<article>
|
||||
<title>First steps in Java</title>
|
||||
<author id='1'>
|
||||
<firstname>Siena</firstname>
|
||||
<lastname>Kerr</lastname>
|
||||
</author>
|
||||
<release-date>2018-12-01</release-date>
|
||||
</article>
|
||||
<article>
|
||||
<title>Dockerize your SpringBoot application</title>
|
||||
<author id='2'>
|
||||
<firstname>Jonas</firstname>
|
||||
<lastname>Lugo</lastname>
|
||||
</author>
|
||||
<release-date>2018-12-01</release-date>
|
||||
</article>
|
||||
</articles>
|
@ -1,15 +0,0 @@
|
||||
package com.baeldung.error;
|
||||
|
||||
public class ErrorGenerator {
|
||||
public void throwException() throws Exception {
|
||||
throw new Exception("checked");
|
||||
}
|
||||
|
||||
public void throwRuntimeException() {
|
||||
throw new RuntimeException("unchecked");
|
||||
}
|
||||
|
||||
public void throwError() {
|
||||
throw new Error("unchecked");
|
||||
}
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
package com.baeldung.error;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ErrorGeneratorUnitTest {
|
||||
|
||||
private ErrorGenerator errorGenerator;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
errorGenerator = new ErrorGenerator();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckedException_thenIsCaughtByCatchException() {
|
||||
try {
|
||||
errorGenerator.throwException();
|
||||
} catch (Exception e) {
|
||||
// caught! -> test pass
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUncheckedException_thenIsCaughtByCatchException() {
|
||||
try {
|
||||
errorGenerator.throwRuntimeException();
|
||||
} catch (Exception e) {
|
||||
// caught! -> test pass
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = Error.class)
|
||||
public void whenError_thenIsNotCaughtByCatchException() {
|
||||
try {
|
||||
errorGenerator.throwError();
|
||||
} catch (Exception e) {
|
||||
Assert.fail(); // errors are not caught by catch exception
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenError_thenIsCaughtByCatchError() {
|
||||
try {
|
||||
errorGenerator.throwError();
|
||||
} catch (Error e) {
|
||||
// caught! -> test pass
|
||||
}
|
||||
}
|
||||
}
|
@ -22,6 +22,9 @@
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<icu.version>64.2</icu.version>
|
||||
<hibernate.core.version>5.4.0.Final</hibernate.core.version>
|
||||
<h2database.version>1.4.197</h2database.version>
|
||||
<jackson.databind.version>2.9.8</jackson.databind.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@ -30,7 +33,21 @@
|
||||
<artifactId>icu4j</artifactId>
|
||||
<version>${icu.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${hibernate.core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2database.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.databind.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -0,0 +1,41 @@
|
||||
package com.baeldung.optionalReturnType;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class HandleOptionalTypeExample {
|
||||
static Map<String, User> usersByName = new HashMap();
|
||||
static {
|
||||
User user1 = new User();
|
||||
user1.setUserId(1l);
|
||||
user1.setFirstName("baeldung");
|
||||
usersByName.put("baeldung", user1);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
changeUserName("baeldung", "baeldung-new");
|
||||
changeUserName("user", "user-new");
|
||||
}
|
||||
|
||||
public static void changeUserName(String oldFirstName, String newFirstName) {
|
||||
Optional<User> userOpt = findUserByName(oldFirstName);
|
||||
if (userOpt.isPresent()) {
|
||||
User user = userOpt.get();
|
||||
user.setFirstName(newFirstName);
|
||||
|
||||
System.out.println("user with name " + oldFirstName + " is changed to " + user.getFirstName());
|
||||
} else {
|
||||
// user is missing
|
||||
System.out.println("user with name " + oldFirstName + " is not found.");
|
||||
}
|
||||
}
|
||||
|
||||
public static Optional<User> findUserByName(String name) {
|
||||
// look up the user in the database, the user object below could be null
|
||||
User user = usersByName.get(name);
|
||||
Optional<User> opt = Optional.ofNullable(user);
|
||||
|
||||
return opt;
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.baeldung.optionalReturnType;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class OptionalToJsonExample {
|
||||
public static void main(String[] args) {
|
||||
UserOptional user = new UserOptional();
|
||||
user.setUserId(1l);
|
||||
user.setFirstName("Bael Dung");
|
||||
|
||||
ObjectMapper om = new ObjectMapper();
|
||||
try {
|
||||
System.out.print("user in json is:" + om.writeValueAsString(user));
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.baeldung.optionalReturnType;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
public class PersistOptionalTypeExample {
|
||||
static String persistenceUnit = "com.baeldung.optionalReturnType";
|
||||
static EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit);
|
||||
|
||||
static EntityManager entityManager = emf.createEntityManager();
|
||||
|
||||
// to run this app, uncomment the follow line in META-INF/persistence.xml
|
||||
// <class>com.baeldung.optionalReturnType.UserOptionalField</class>
|
||||
public static void main(String[] args) {
|
||||
UserOptionalField user1 = new UserOptionalField();
|
||||
user1.setUserId(1l);
|
||||
user1.setFirstName(Optional.of("Bael Dung"));
|
||||
entityManager.persist(user1);
|
||||
|
||||
UserOptional user2 = entityManager.find(UserOptional.class, 1l);
|
||||
System.out.print("User2.firstName:" + user2.getFirstName());
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.baeldung.optionalReturnType;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
public class PersistOptionalTypeExample2 {
|
||||
static String persistenceUnit = "com.baeldung.optionalReturnType";
|
||||
static EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit);
|
||||
|
||||
static EntityManager em = emf.createEntityManager();
|
||||
|
||||
public static void main(String[] args) {
|
||||
UserOptional user1 = new UserOptional();
|
||||
user1.setUserId(1l);
|
||||
user1.setFirstName("Bael Dung");
|
||||
em.persist(user1);
|
||||
|
||||
UserOptional user2 = em.find(UserOptional.class, 1l);
|
||||
System.out.print("User2.firstName:" + user2.getFirstName());
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.baeldung.optionalReturnType;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
public class PersistUserExample {
|
||||
static String persistenceUnit = "com.baeldung.optionalReturnType";
|
||||
static EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit);
|
||||
|
||||
static EntityManager em = emf.createEntityManager();
|
||||
|
||||
public static void main(String[] args) {
|
||||
User user1 = new User();
|
||||
user1.setUserId(1l);
|
||||
user1.setFirstName("Bael Dung");
|
||||
em.persist(user1);
|
||||
|
||||
User user2 = em.find(User.class, 1l);
|
||||
System.out.print("User2.firstName:" + user2.getFirstName());
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package com.baeldung.optionalReturnType;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.Optional;
|
||||
|
||||
public class SerializeOptionalTypeExample {
|
||||
public static void main(String[] args) {
|
||||
User user1 = new User();
|
||||
user1.setUserId(1l);
|
||||
user1.setFirstName("baeldung");
|
||||
|
||||
serializeObject(user1, "user1.ser");
|
||||
|
||||
UserOptionalField user2 = new UserOptionalField();
|
||||
user2.setUserId(1l);
|
||||
user2.setFirstName(Optional.of("baeldung"));
|
||||
|
||||
serializeObject(user2, "user2.ser");
|
||||
|
||||
}
|
||||
|
||||
public static void serializeObject(Object object, String fileName) {
|
||||
// Serialization
|
||||
try {
|
||||
FileOutputStream file = new FileOutputStream(fileName);
|
||||
ObjectOutputStream out = new ObjectOutputStream(file);
|
||||
|
||||
out.writeObject(object);
|
||||
|
||||
out.close();
|
||||
file.close();
|
||||
|
||||
System.out.println("Object " + object.toString() + " has been serialized to file " + fileName);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.baeldung.optionalReturnType;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class User implements Serializable {
|
||||
@Id
|
||||
private long userId;
|
||||
|
||||
private String firstName;
|
||||
|
||||
public long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(long userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.baeldung.optionalReturnType;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class UserOptional implements Serializable {
|
||||
@Id
|
||||
private long userId;
|
||||
|
||||
@Column(nullable = true)
|
||||
private String firstName;
|
||||
|
||||
public long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(long userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public Optional<String> getFirstName() {
|
||||
return Optional.ofNullable(firstName);
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
Optional.ofNullable(firstName);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.baeldung.optionalReturnType;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class UserOptionalField implements Serializable {
|
||||
@Id
|
||||
private long userId;
|
||||
|
||||
private Optional<String> firstName;
|
||||
|
||||
public long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(long userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public Optional<String> getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(Optional<String> firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
|
||||
version="2.0">
|
||||
|
||||
<persistence-unit
|
||||
name="com.baeldung.optionalReturnType"
|
||||
transaction-type="RESOURCE_LOCAL">
|
||||
<description>Persist Optional Return Type Demo</description>
|
||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||
<class>com.baeldung.optionalReturnType.User</class>
|
||||
<class>com.baeldung.optionalReturnType.UserOptional</class>
|
||||
<!--
|
||||
<class>com.baeldung.optionalReturnType.UserOptionalField</class>
|
||||
-->
|
||||
<exclude-unlisted-classes>true</exclude-unlisted-classes>
|
||||
|
||||
<properties>
|
||||
<property name="javax.persistence.jdbc.driver"
|
||||
value="org.h2.Driver" />
|
||||
<property name="javax.persistence.jdbc.url"
|
||||
value="jdbc:h2:mem:test" />
|
||||
<property name="javax.persistence.jdbc.user" value="sa" />
|
||||
<property name="javax.persistence.jdbc.password" value="" />
|
||||
<property name="hibernate.dialect"
|
||||
value="org.hibernate.dialect.H2Dialect" />
|
||||
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
|
||||
<property name="show_sql" value="true" />
|
||||
<property name="hibernate.temp.use_jdbc_metadata_defaults"
|
||||
value="false" />
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
@ -0,0 +1,26 @@
|
||||
package com.baeldung.error;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ErrorGeneratorUnitTest {
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void whenError_thenIsNotCaughtByCatchException() {
|
||||
try {
|
||||
throw new AssertionError();
|
||||
} catch (Exception e) {
|
||||
Assert.fail(); // errors are not caught by catch exception
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenError_thenIsCaughtByCatchError() {
|
||||
try {
|
||||
throw new AssertionError();
|
||||
} catch (Error e) {
|
||||
// caught! -> test pass
|
||||
}
|
||||
}
|
||||
}
|
61
docker/docker-compose.yml
Normal file
61
docker/docker-compose.yml
Normal file
@ -0,0 +1,61 @@
|
||||
version: '3'
|
||||
|
||||
services:
|
||||
|
||||
## VOLUME CONTAINER-TO-CONTAINER AND HOST-TO-CONTAINER TEST ##
|
||||
|
||||
volumes-example-service:
|
||||
image: alpine:latest
|
||||
container_name: volumes-example-service
|
||||
volumes:
|
||||
- /tmp:/my-volumes/host-volume
|
||||
- /home:/my-volumes/readonly-host-volume:ro
|
||||
- my-named-global-volume:/my-volumes/named-global-volume
|
||||
tty: true # Needed to keep the container running
|
||||
|
||||
another-volumes-example-service:
|
||||
image: alpine:latest
|
||||
container_name: another-volumes-example-service
|
||||
volumes:
|
||||
- my-named-global-volume:/another-path/the-same-named-global-volume
|
||||
tty: true # Needed to keep the container running
|
||||
|
||||
## NETWORK CONTAINER-TO-CONTAINER TEST ##
|
||||
|
||||
network-example-service:
|
||||
image: karthequian/helloworld:latest
|
||||
container_name: network-example-service
|
||||
networks:
|
||||
- my-shared-network
|
||||
|
||||
another-service-in-the-same-network:
|
||||
image: alpine:latest
|
||||
container_name: another-service-in-the-same-network
|
||||
networks:
|
||||
- my-shared-network
|
||||
|
||||
tty: true # Needed to keep the container running
|
||||
|
||||
another-service-in-its-own-network:
|
||||
image: alpine:latest
|
||||
container_name: another-service-in-its-own-network
|
||||
networks:
|
||||
- my-private-network
|
||||
tty: true # Needed to keep the container running
|
||||
|
||||
## NETWORK HOST-TO-CONTAINER TEST ##
|
||||
|
||||
network-example-service-available-to-host-on-port-1337:
|
||||
image: karthequian/helloworld:latest
|
||||
container_name: network-example-service-available-to-host-on-port-1337
|
||||
networks:
|
||||
- my-shared-network
|
||||
ports:
|
||||
- "1337:80"
|
||||
|
||||
volumes:
|
||||
my-named-global-volume:
|
||||
|
||||
networks:
|
||||
my-shared-network: {}
|
||||
my-private-network: {}
|
@ -15,8 +15,7 @@ public class ConvertToMap {
|
||||
}
|
||||
|
||||
public Map<Integer, Book> listToMapWithDupKey(List<Book> books) {
|
||||
return books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity(),
|
||||
(o1, o2) -> o1));
|
||||
return books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity(), (existing, replacement) -> existing));
|
||||
}
|
||||
|
||||
public Map<Integer, Book> listToConcurrentMap(List<Book> books) {
|
||||
|
@ -2,6 +2,7 @@ package com.baeldung.convertToMap;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
@ -34,8 +35,10 @@ public class ConvertToMapUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMapHasDuplicateKey_with_merge_function() {
|
||||
assertTrue(convertToMap.listToMapWithDupKey(bookList).size() == 2);
|
||||
public void whenMapHasDuplicateKeyThenMergeFunctionHandlesCollision() {
|
||||
Map<Integer, Book> booksByYear = convertToMap.listToMapWithDupKey(bookList);
|
||||
assertEquals(2, booksByYear.size());
|
||||
assertEquals("0395489318", booksByYear.get(1954).getIsbn());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -82,6 +82,12 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>edu.uci.ics</groupId>
|
||||
<artifactId>crawler4j</artifactId>
|
||||
<version>${crawler4j.version}</version>
|
||||
</dependency
|
||||
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
@ -89,6 +95,7 @@
|
||||
<classgraph.version>4.8.28</classgraph.version>
|
||||
<jbpm.version>6.0.0.Final</jbpm.version>
|
||||
<picocli.version>3.9.6</picocli.version>
|
||||
<crawler4j.version>4.4.0</crawler4j.version>
|
||||
<spring-boot-starter.version>2.1.4.RELEASE</spring-boot-starter.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
@ -0,0 +1,22 @@
|
||||
package com.baeldung.crawler4j;
|
||||
|
||||
public class CrawlerStatistics {
|
||||
private int processedPageCount = 0;
|
||||
private int totalLinksCount = 0;
|
||||
|
||||
public void incrementProcessedPageCount() {
|
||||
processedPageCount++;
|
||||
}
|
||||
|
||||
public void incrementTotalLinksCount(int linksCount) {
|
||||
totalLinksCount += linksCount;
|
||||
}
|
||||
|
||||
public int getProcessedPageCount() {
|
||||
return processedPageCount;
|
||||
}
|
||||
|
||||
public int getTotalLinksCount() {
|
||||
return totalLinksCount;
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.baeldung.crawler4j;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import edu.uci.ics.crawler4j.crawler.Page;
|
||||
import edu.uci.ics.crawler4j.crawler.WebCrawler;
|
||||
import edu.uci.ics.crawler4j.parser.HtmlParseData;
|
||||
import edu.uci.ics.crawler4j.url.WebURL;
|
||||
|
||||
public class HtmlCrawler extends WebCrawler {
|
||||
|
||||
private final static Pattern EXCLUSIONS = Pattern.compile(".*(\\.(css|js|xml|gif|jpg|png|mp3|mp4|zip|gz|pdf))$");
|
||||
|
||||
private CrawlerStatistics stats;
|
||||
|
||||
public HtmlCrawler(CrawlerStatistics stats) {
|
||||
this.stats = stats;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldVisit(Page referringPage, WebURL url) {
|
||||
String urlString = url.getURL().toLowerCase();
|
||||
return !EXCLUSIONS.matcher(urlString).matches()
|
||||
&& urlString.startsWith("https://www.baeldung.com/");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Page page) {
|
||||
String url = page.getWebURL().getURL();
|
||||
stats.incrementProcessedPageCount();
|
||||
|
||||
if (page.getParseData() instanceof HtmlParseData) {
|
||||
HtmlParseData htmlParseData = (HtmlParseData) page.getParseData();
|
||||
String title = htmlParseData.getTitle();
|
||||
String text = htmlParseData.getText();
|
||||
String html = htmlParseData.getHtml();
|
||||
Set<WebURL> links = htmlParseData.getOutgoingUrls();
|
||||
stats.incrementTotalLinksCount(links.size());
|
||||
|
||||
System.out.printf("Page with title '%s' %n", title);
|
||||
System.out.printf(" Text length: %d %n", text.length());
|
||||
System.out.printf(" HTML length: %d %n", html.length());
|
||||
System.out.printf(" %d outbound links %n", links.size());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.baeldung.crawler4j;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import edu.uci.ics.crawler4j.crawler.CrawlConfig;
|
||||
import edu.uci.ics.crawler4j.crawler.CrawlController;
|
||||
import edu.uci.ics.crawler4j.fetcher.PageFetcher;
|
||||
import edu.uci.ics.crawler4j.robotstxt.RobotstxtConfig;
|
||||
import edu.uci.ics.crawler4j.robotstxt.RobotstxtServer;
|
||||
|
||||
public class HtmlCrawlerController {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
File crawlStorage = new File("src/test/resources/crawler4j");
|
||||
CrawlConfig config = new CrawlConfig();
|
||||
config.setCrawlStorageFolder(crawlStorage.getAbsolutePath());
|
||||
config.setMaxDepthOfCrawling(2);
|
||||
|
||||
int numCrawlers = 12;
|
||||
|
||||
PageFetcher pageFetcher = new PageFetcher(config);
|
||||
RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
|
||||
RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
|
||||
CrawlController controller = new CrawlController(config, pageFetcher, robotstxtServer);
|
||||
|
||||
controller.addSeed("https://www.baeldung.com/");
|
||||
|
||||
CrawlerStatistics stats = new CrawlerStatistics();
|
||||
CrawlController.WebCrawlerFactory<HtmlCrawler> factory = () -> new HtmlCrawler(stats);
|
||||
|
||||
controller.start(factory, numCrawlers);
|
||||
System.out.printf("Crawled %d pages %n", stats.getProcessedPageCount());
|
||||
System.out.printf("Total Number of outbound links = %d %n", stats.getTotalLinksCount());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.baeldung.crawler4j;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import edu.uci.ics.crawler4j.crawler.Page;
|
||||
import edu.uci.ics.crawler4j.crawler.WebCrawler;
|
||||
import edu.uci.ics.crawler4j.parser.BinaryParseData;
|
||||
import edu.uci.ics.crawler4j.url.WebURL;
|
||||
|
||||
public class ImageCrawler extends WebCrawler {
|
||||
private final static Pattern EXCLUSIONS = Pattern.compile(".*(\\.(css|js|xml|gif|png|mp3|mp4|zip|gz|pdf))$");
|
||||
|
||||
private static final Pattern IMG_PATTERNS = Pattern.compile(".*(\\.(jpg|jpeg))$");
|
||||
|
||||
private File saveDir;
|
||||
|
||||
public ImageCrawler(File saveDir) {
|
||||
this.saveDir = saveDir;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldVisit(Page referringPage, WebURL url) {
|
||||
String urlString = url.getURL().toLowerCase();
|
||||
if (EXCLUSIONS.matcher(urlString).matches()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (IMG_PATTERNS.matcher(urlString).matches()
|
||||
|| urlString.startsWith("https://www.baeldung.com/")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Page page) {
|
||||
String url = page.getWebURL().getURL();
|
||||
if (IMG_PATTERNS.matcher(url).matches()
|
||||
&& page.getParseData() instanceof BinaryParseData) {
|
||||
String extension = url.substring(url.lastIndexOf("."));
|
||||
int contentLength = page.getContentData().length;
|
||||
|
||||
System.out.printf("Extension is '%s' with content length %d %n", extension, contentLength);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.baeldung.crawler4j;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import edu.uci.ics.crawler4j.crawler.CrawlConfig;
|
||||
import edu.uci.ics.crawler4j.crawler.CrawlController;
|
||||
import edu.uci.ics.crawler4j.fetcher.PageFetcher;
|
||||
import edu.uci.ics.crawler4j.robotstxt.RobotstxtConfig;
|
||||
import edu.uci.ics.crawler4j.robotstxt.RobotstxtServer;
|
||||
|
||||
public class ImageCrawlerController {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
File crawlStorage = new File("src/test/resources/crawler4j");
|
||||
CrawlConfig config = new CrawlConfig();
|
||||
config.setCrawlStorageFolder(crawlStorage.getAbsolutePath());
|
||||
config.setIncludeBinaryContentInCrawling(true);
|
||||
config.setMaxPagesToFetch(500);
|
||||
|
||||
File saveDir = new File("src/test/resources/crawler4j");
|
||||
|
||||
int numCrawlers = 12;
|
||||
|
||||
PageFetcher pageFetcher = new PageFetcher(config);
|
||||
RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
|
||||
RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
|
||||
CrawlController controller = new CrawlController(config, pageFetcher, robotstxtServer);
|
||||
|
||||
controller.addSeed("https://www.baeldung.com/");
|
||||
|
||||
CrawlController.WebCrawlerFactory<ImageCrawler> factory = () -> new ImageCrawler(saveDir);
|
||||
|
||||
controller.start(factory, numCrawlers);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package com.baeldung.crawler4j;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import edu.uci.ics.crawler4j.crawler.CrawlConfig;
|
||||
import edu.uci.ics.crawler4j.crawler.CrawlController;
|
||||
import edu.uci.ics.crawler4j.fetcher.PageFetcher;
|
||||
import edu.uci.ics.crawler4j.robotstxt.RobotstxtConfig;
|
||||
import edu.uci.ics.crawler4j.robotstxt.RobotstxtServer;
|
||||
|
||||
public class MultipleCrawlerController {
|
||||
public static void main(String[] args) throws Exception {
|
||||
File crawlStorageBase = new File("src/test/resources/crawler4j");
|
||||
CrawlConfig htmlConfig = new CrawlConfig();
|
||||
CrawlConfig imageConfig = new CrawlConfig();
|
||||
|
||||
htmlConfig.setCrawlStorageFolder(new File(crawlStorageBase, "html").getAbsolutePath());
|
||||
imageConfig.setCrawlStorageFolder(new File(crawlStorageBase, "image").getAbsolutePath());
|
||||
imageConfig.setIncludeBinaryContentInCrawling(true);
|
||||
|
||||
htmlConfig.setMaxPagesToFetch(500);
|
||||
imageConfig.setMaxPagesToFetch(1000);
|
||||
|
||||
PageFetcher pageFetcherHtml = new PageFetcher(htmlConfig);
|
||||
PageFetcher pageFetcherImage = new PageFetcher(imageConfig);
|
||||
|
||||
RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
|
||||
RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcherHtml);
|
||||
|
||||
CrawlController htmlController = new CrawlController(htmlConfig, pageFetcherHtml, robotstxtServer);
|
||||
CrawlController imageController = new CrawlController(imageConfig, pageFetcherImage, robotstxtServer);
|
||||
|
||||
htmlController.addSeed("https://www.baeldung.com/");
|
||||
imageController.addSeed("https://www.baeldung.com/");
|
||||
|
||||
CrawlerStatistics stats = new CrawlerStatistics();
|
||||
CrawlController.WebCrawlerFactory<HtmlCrawler> htmlFactory = () -> new HtmlCrawler(stats);
|
||||
|
||||
File saveDir = new File("src/test/resources/crawler4j");
|
||||
CrawlController.WebCrawlerFactory<ImageCrawler> imageFactory = () -> new ImageCrawler(saveDir);
|
||||
|
||||
imageController.startNonBlocking(imageFactory, 7);
|
||||
htmlController.startNonBlocking(htmlFactory, 10);
|
||||
|
||||
|
||||
htmlController.waitUntilFinish();
|
||||
System.out.printf("Crawled %d pages %n", stats.getProcessedPageCount());
|
||||
System.out.printf("Total Number of outbound links = %d %n", stats.getTotalLinksCount());
|
||||
|
||||
imageController.waitUntilFinish();
|
||||
System.out.printf("Image Crawler is finished.");
|
||||
|
||||
}
|
||||
}
|
@ -5,9 +5,9 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<artifactId>persistence-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>hibernate-mapping</artifactId>
|
||||
@ -31,22 +31,28 @@
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
<!-- validation -->
|
||||
<!-- validation -->
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>${hibernate-validator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.el</groupId>
|
||||
<artifactId>javax.el-api</artifactId>
|
||||
<version>${javax.el-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish</groupId>
|
||||
<artifactId>javax.el</artifactId>
|
||||
<version>${org.glassfish.javax.el.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.money</groupId>
|
||||
<artifactId>money-api</artifactId>
|
||||
<version>1.0.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.javamoney</groupId>
|
||||
<artifactId>moneta</artifactId>
|
||||
<version>1.3</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -60,11 +66,10 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<hibernate.version>5.3.7.Final</hibernate.version>
|
||||
<hibernate.version>5.3.10.Final</hibernate.version>
|
||||
<assertj-core.version>3.8.0</assertj-core.version>
|
||||
<hibernate-validator.version>5.3.3.Final</hibernate-validator.version>
|
||||
<javax.el-api.version>2.2.5</javax.el-api.version>
|
||||
<org.glassfish.javax.el.version>3.0.1-b08</org.glassfish.javax.el.version>
|
||||
<hibernate-validator.version>6.0.16.Final</hibernate-validator.version>
|
||||
<org.glassfish.javax.el.version>3.0.1-b11</org.glassfish.javax.el.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
@ -4,8 +4,11 @@ import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.validation.constraints.Size;
|
||||
import javax.money.MonetaryAmount;
|
||||
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
import org.hibernate.validator.constraints.CreditCardNumber;
|
||||
import org.hibernate.validator.constraints.Currency;
|
||||
|
||||
@Entity
|
||||
public class User {
|
||||
@ -62,5 +65,4 @@ public class User {
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,290 @@
|
||||
package com.baeldung.hibernate.validation;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Duration;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.money.CurrencyContextBuilder;
|
||||
import javax.money.Monetary;
|
||||
import javax.money.MonetaryAmount;
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.ValidatorFactory;
|
||||
|
||||
import org.hibernate.validator.constraints.CodePointLength;
|
||||
import org.hibernate.validator.constraints.CreditCardNumber;
|
||||
import org.hibernate.validator.constraints.Currency;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
import org.hibernate.validator.constraints.LuhnCheck;
|
||||
import org.hibernate.validator.constraints.Range;
|
||||
import org.hibernate.validator.constraints.SafeHtml;
|
||||
import org.hibernate.validator.constraints.ScriptAssert;
|
||||
import org.hibernate.validator.constraints.URL;
|
||||
import org.hibernate.validator.constraints.time.DurationMax;
|
||||
import org.hibernate.validator.constraints.time.DurationMin;
|
||||
import org.javamoney.moneta.CurrencyUnitBuilder;
|
||||
import org.javamoney.moneta.Money;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UserAdditionalValidationUnitTest {
|
||||
|
||||
private static Validator validator;
|
||||
private Set<ConstraintViolation<AdditionalValidations>> constraintViolations;
|
||||
|
||||
@BeforeClass
|
||||
public static void before() {
|
||||
ValidatorFactory config = Validation.buildDefaultValidatorFactory();
|
||||
validator = config.getValidator();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidationWithCCNAndNullCCN_thenNoConstraintViolation() {
|
||||
AdditionalValidations validations = new AdditionalValidations();
|
||||
constraintViolations = validator.validateProperty(validations, "creditCardNumber");
|
||||
assertTrue(constraintViolations.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidationWithCCNAndValidCCN_thenNoConstraintViolation() {
|
||||
AdditionalValidations validations = new AdditionalValidations();
|
||||
validations.setCreditCardNumber("79927398713");
|
||||
constraintViolations = validator.validateProperty(validations, "creditCardNumber");
|
||||
assertTrue(constraintViolations.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidationWithCCNAndInvalidCCN_thenConstraintViolation() {
|
||||
AdditionalValidations validations = new AdditionalValidations();
|
||||
validations.setCreditCardNumber("79927398714");
|
||||
constraintViolations = validator.validateProperty(validations, "creditCardNumber");
|
||||
assertEquals(constraintViolations.size(), 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidationWithCCNAndValidCCNWithDashes_thenConstraintViolation() {
|
||||
AdditionalValidations validations = new AdditionalValidations();
|
||||
validations.setCreditCardNumber("7992-7398-713");
|
||||
constraintViolations = validator.validateProperty(validations, "creditCardNumber");
|
||||
assertEquals(1, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidationWithLenientCCNAndValidCCNWithDashes_thenNoConstraintViolation() {
|
||||
AdditionalValidations validations = new AdditionalValidations();
|
||||
validations.setLenientCreditCardNumber("7992-7398-713");
|
||||
constraintViolations = validator.validateProperty(validations, "lenientCreditCardNumber");
|
||||
assertTrue(constraintViolations.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMonetaryAmountWithRightCurrency_thenNoConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
bean.setBalance(Money.of(new BigDecimal(100.0), Monetary.getCurrency("EUR")));
|
||||
constraintViolations = validator.validateProperty(bean, "balance");
|
||||
assertEquals(0, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMonetaryAmountWithWrongCurrency_thenConstraintViolation() {
|
||||
AdditionalValidations validations = new AdditionalValidations();
|
||||
validations.setBalance(Money.of(new BigDecimal(100.0), Monetary.getCurrency("USD")));
|
||||
constraintViolations = validator.validateProperty(validations, "balance");
|
||||
assertEquals(1, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDurationShorterThanMin_thenConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
bean.setDuration(Duration.ofDays(1).plusHours(1));
|
||||
constraintViolations = validator.validateProperty(bean, "duration");
|
||||
assertEquals(1, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDurationLongerThanMax_thenConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
bean.setDuration(Duration.ofDays(2).plusHours(3));
|
||||
constraintViolations = validator.validateProperty(bean, "duration");
|
||||
assertEquals(1, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDurationBetweenMinAndMax_thenNoConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
bean.setDuration(Duration.ofDays(2));
|
||||
constraintViolations = validator.validateProperty(bean, "duration");
|
||||
assertEquals(0, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValueBelowRangeMin_thenConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
bean.setPercent(new BigDecimal("-1.4"));
|
||||
constraintViolations = validator.validateProperty(bean, "percent");
|
||||
assertEquals(1, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValueAboveRangeMax_thenConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
bean.setPercent(new BigDecimal("100.03"));
|
||||
constraintViolations = validator.validateProperty(bean, "percent");
|
||||
assertEquals(1, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValueInRange_thenNoConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
bean.setPercent(new BigDecimal("53.23"));
|
||||
constraintViolations = validator.validateProperty(bean, "percent");
|
||||
assertEquals(0, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLengthInRange_thenNoConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
bean.setSomeString("aaa");
|
||||
constraintViolations = validator.validateProperty(bean, "someString");
|
||||
assertEquals(0, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCodePointLengthNotInRange_thenConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
bean.setSomeString("aa\uD835\uDD0A");
|
||||
constraintViolations = validator.validateProperty(bean, "someString");
|
||||
assertEquals(1, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidUrlWithWrongProtocol_thenConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
|
||||
bean.setUrl("https://www.google.com/");
|
||||
constraintViolations = validator.validateProperty(bean, "url");
|
||||
assertEquals(0, constraintViolations.size());
|
||||
|
||||
bean.setUrl("http://www.google.com/");
|
||||
constraintViolations = validator.validateProperty(bean, "url");
|
||||
assertEquals(1, constraintViolations.size());
|
||||
|
||||
bean.setUrl("https://foo:bar");
|
||||
constraintViolations = validator.validateProperty(bean, "url");
|
||||
assertEquals(1, constraintViolations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenScriptAssertFails_thenConstraintViolation() {
|
||||
AdditionalValidations bean = new AdditionalValidations();
|
||||
|
||||
constraintViolations = validator.validate(bean);
|
||||
assertEquals(0, constraintViolations.size());
|
||||
|
||||
bean.setValid(false);
|
||||
|
||||
constraintViolations = validator.validate(bean);
|
||||
assertEquals(1, constraintViolations.size());
|
||||
|
||||
constraintViolations = validator.validateProperty(bean, "valid");
|
||||
assertEquals(0, constraintViolations.size());
|
||||
}
|
||||
|
||||
@ScriptAssert(lang = "nashorn", script = "_this.valid")
|
||||
public class AdditionalValidations {
|
||||
private boolean valid = true;
|
||||
|
||||
@CreditCardNumber
|
||||
@LuhnCheck(startIndex = 0, endIndex = Integer.MAX_VALUE, checkDigitIndex = -1)
|
||||
private String creditCardNumber;
|
||||
|
||||
@CreditCardNumber(ignoreNonDigitCharacters = true)
|
||||
private String lenientCreditCardNumber;
|
||||
|
||||
@Currency("EUR")
|
||||
private MonetaryAmount balance;
|
||||
|
||||
@DurationMin(days = 1, hours = 2)
|
||||
@DurationMax(days = 2, hours = 1)
|
||||
private Duration duration;
|
||||
|
||||
@Range(min = 0, max = 100)
|
||||
private BigDecimal percent;
|
||||
|
||||
@Length(min = 1, max = 3)
|
||||
@CodePointLength(min = 1, max = 3)
|
||||
private String someString;
|
||||
|
||||
@URL(protocol = "https")
|
||||
private String url;
|
||||
|
||||
public String getCreditCardNumber() {
|
||||
return creditCardNumber;
|
||||
}
|
||||
|
||||
public void setCreditCardNumber(String creditCardNumber) {
|
||||
this.creditCardNumber = creditCardNumber;
|
||||
}
|
||||
|
||||
public String getLenientCreditCardNumber() {
|
||||
return lenientCreditCardNumber;
|
||||
}
|
||||
|
||||
public void setLenientCreditCardNumber(String lenientCreditCardNumber) {
|
||||
this.lenientCreditCardNumber = lenientCreditCardNumber;
|
||||
}
|
||||
|
||||
public MonetaryAmount getBalance() {
|
||||
return balance;
|
||||
}
|
||||
|
||||
public void setBalance(MonetaryAmount balance) {
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
public Duration getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
public void setDuration(Duration duration) {
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
public BigDecimal getPercent() {
|
||||
return percent;
|
||||
}
|
||||
|
||||
public void setPercent(BigDecimal percent) {
|
||||
this.percent = percent;
|
||||
}
|
||||
|
||||
public String getSomeString() {
|
||||
return someString;
|
||||
}
|
||||
|
||||
public void setSomeString(String someString) {
|
||||
this.someString = someString;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return valid;
|
||||
}
|
||||
|
||||
public void setValid(boolean valid) {
|
||||
this.valid = valid;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -122,14 +122,10 @@ public class DynamicMappingIntegrationTest {
|
||||
Employee employee = session.get(Employee.class, 1);
|
||||
assertThat(employee.getGrossIncome()).isEqualTo(10_000);
|
||||
|
||||
session.close();
|
||||
|
||||
session = HibernateUtil.getSessionFactory().openSession();
|
||||
transaction = session.beginTransaction();
|
||||
|
||||
session.disableFilter("incomeLevelFilter");
|
||||
employees = session.createQuery("from Employee").getResultList();
|
||||
|
||||
assertThat(employees).hasSize(0);
|
||||
assertThat(employees).hasSize(3);
|
||||
|
||||
}
|
||||
|
||||
|
@ -3,3 +3,4 @@
|
||||
- [A Guide to MongoDB with Java](http://www.baeldung.com/java-mongodb)
|
||||
- [A Simple Tagging Implementation with MongoDB](http://www.baeldung.com/mongodb-tagging)
|
||||
- [MongoDB BSON Guide](https://www.baeldung.com/mongodb-bson)
|
||||
- [Geospatial Support in MongoDB](https://www.baeldung.com/mongodb-geospatial-support)
|
||||
|
32
pom.xml
32
pom.xml
@ -123,17 +123,22 @@
|
||||
</excludes>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-surefire-provider</artifactId>
|
||||
<version>${junit-platform.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
@ -921,6 +926,7 @@
|
||||
<module>spring-vault</module>
|
||||
<module>spring-vertx</module>
|
||||
<module>spring-zuul/spring-zuul-foos-resource</module>
|
||||
<module>persistence-modules/hibernate-mapping</module>
|
||||
<module>persistence-modules/spring-data-dynamodb</module>
|
||||
<module>persistence-modules/spring-data-eclipselink</module>
|
||||
<module>persistence-modules/spring-data-solr</module>
|
||||
@ -1550,8 +1556,8 @@
|
||||
<jstl.version>1.2</jstl.version>
|
||||
<jackson.version>2.9.8</jackson.version>
|
||||
<commons-fileupload.version>1.3</commons-fileupload.version>
|
||||
<junit-platform.version>1.4.2</junit-platform.version>
|
||||
<junit-jupiter.version>5.4.2</junit-jupiter.version>
|
||||
<junit-platform.version>1.2.0</junit-platform.version>
|
||||
<junit-jupiter.version>5.2.0</junit-jupiter.version>
|
||||
<directory-maven-plugin.version>0.3.1</directory-maven-plugin.version>
|
||||
<maven-install-plugin.version>2.5.1</maven-install-plugin.version>
|
||||
<custom-pmd.version>0.0.1</custom-pmd.version>
|
||||
|
@ -0,0 +1,10 @@
|
||||
package com.baeldung.autoconfiguration.service;
|
||||
|
||||
public class CustomService implements SimpleService {
|
||||
|
||||
@Override
|
||||
public String serve() {
|
||||
return "Custom Service";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.baeldung.autoconfiguration.service;
|
||||
|
||||
public class DefaultService implements SimpleService {
|
||||
|
||||
@Override
|
||||
public String serve() {
|
||||
return "Default Service";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.baeldung.autoconfiguration.service;
|
||||
|
||||
public interface SimpleService {
|
||||
|
||||
public String serve();
|
||||
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package com.baeldung.autoconfiguration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
public class ConditionalOnBeanIntegrationTest {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
@Test
|
||||
public void whenDependentBeanIsPresent_thenConditionalBeanCreated() {
|
||||
this.contextRunner.withUserConfiguration(BasicConfiguration.class, ConditionalOnBeanConfiguration.class)
|
||||
.run((context) -> {
|
||||
assertThat(context).hasBean("created");
|
||||
assertThat(context).getBean("created")
|
||||
.isEqualTo("This is always created");
|
||||
assertThat(context).hasBean("createOnBean");
|
||||
assertThat(context).getBean("createOnBean")
|
||||
.isEqualTo("This is created when bean (name=created) is present");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDependentBeanIsPresent_thenConditionalMissingBeanIgnored() {
|
||||
this.contextRunner.withUserConfiguration(BasicConfiguration.class, ConditionalOnMissingBeanConfiguration.class)
|
||||
.run((context) -> {
|
||||
assertThat(context).hasBean("created");
|
||||
assertThat(context).getBean("created")
|
||||
.isEqualTo("This is always created");
|
||||
assertThat(context).doesNotHaveBean("createOnMissingBean");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDependentBeanIsNotPresent_thenConditionalMissingBeanCreated() {
|
||||
this.contextRunner.withUserConfiguration(ConditionalOnMissingBeanConfiguration.class)
|
||||
.run((context) -> {
|
||||
assertThat(context).hasBean("createOnMissingBean");
|
||||
assertThat(context).getBean("createOnMissingBean")
|
||||
.isEqualTo("This is created when bean (name=created) is missing");
|
||||
assertThat(context).doesNotHaveBean("created");
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class BasicConfiguration {
|
||||
@Bean
|
||||
public String created() {
|
||||
return "This is always created";
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnBean(name = "created")
|
||||
protected static class ConditionalOnBeanConfiguration {
|
||||
@Bean
|
||||
public String createOnBean() {
|
||||
return "This is created when bean (name=created) is present";
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnMissingBean(name = "created")
|
||||
protected static class ConditionalOnMissingBeanConfiguration {
|
||||
@Bean
|
||||
public String createOnMissingBean() {
|
||||
return "This is created when bean (name=created) is missing";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
package com.baeldung.autoconfiguration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
|
||||
import org.springframework.boot.test.context.FilteredClassLoader;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
public class ConditionalOnClassIntegrationTest {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
@Test
|
||||
public void whenDependentClassIsPresent_thenBeanCreated() {
|
||||
this.contextRunner.withUserConfiguration(ConditionalOnClassConfiguration.class)
|
||||
.run(context -> {
|
||||
assertThat(context).hasBean("created");
|
||||
assertThat(context.getBean("created")).isEqualTo("This is created when ConditionalOnClassIntegrationTest is present on the classpath");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDependentClassIsPresent_thenBeanMissing() {
|
||||
this.contextRunner.withUserConfiguration(ConditionalOnMissingClassConfiguration.class)
|
||||
.run(context -> {
|
||||
assertThat(context).doesNotHaveBean("missed");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDependentClassIsNotPresent_thenBeanMissing() {
|
||||
this.contextRunner.withUserConfiguration(ConditionalOnClassConfiguration.class)
|
||||
.withClassLoader(new FilteredClassLoader(ConditionalOnClassIntegrationTest.class))
|
||||
.run((context) -> {
|
||||
assertThat(context).doesNotHaveBean("created");
|
||||
assertThat(context).doesNotHaveBean(ConditionalOnClassIntegrationTest.class);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDependentClassIsNotPresent_thenBeanCreated() {
|
||||
this.contextRunner.withUserConfiguration(ConditionalOnMissingClassConfiguration.class)
|
||||
.withClassLoader(new FilteredClassLoader(ConditionalOnClassIntegrationTest.class))
|
||||
.run((context) -> {
|
||||
assertThat(context).hasBean("missed");
|
||||
assertThat(context).getBean("missed")
|
||||
.isEqualTo("This is missed when ConditionalOnClassIntegrationTest is present on the classpath");
|
||||
assertThat(context).doesNotHaveBean(ConditionalOnClassIntegrationTest.class);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass(ConditionalOnClassIntegrationTest.class)
|
||||
protected static class ConditionalOnClassConfiguration {
|
||||
@Bean
|
||||
public String created() {
|
||||
return "This is created when ConditionalOnClassIntegrationTest is present on the classpath";
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnMissingClass("com.baeldung.autoconfiguration.ConditionalOnClassIntegrationTest")
|
||||
protected static class ConditionalOnMissingClassConfiguration {
|
||||
@Bean
|
||||
public String missed() {
|
||||
return "This is missed when ConditionalOnClassIntegrationTest is present on the classpath";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package com.baeldung.autoconfiguration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
|
||||
import com.baeldung.autoconfiguration.service.CustomService;
|
||||
import com.baeldung.autoconfiguration.service.DefaultService;
|
||||
import com.baeldung.autoconfiguration.service.SimpleService;
|
||||
|
||||
public class ConditionalOnPropertyIntegrationTest {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
@Test
|
||||
public void whenGivenCustomPropertyValue_thenCustomServiceCreated() {
|
||||
this.contextRunner.withPropertyValues("com.baeldung.service=custom")
|
||||
.withUserConfiguration(SimpleServiceConfiguration.class)
|
||||
.run(context -> {
|
||||
assertThat(context).hasBean("customService");
|
||||
SimpleService simpleService = context.getBean(CustomService.class);
|
||||
assertThat(simpleService.serve()).isEqualTo("Custom Service");
|
||||
assertThat(context).doesNotHaveBean("defaultService");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGivenDefaultPropertyValue_thenDefaultServiceCreated() {
|
||||
this.contextRunner.withPropertyValues("com.baeldung.service=default")
|
||||
.withUserConfiguration(SimpleServiceConfiguration.class)
|
||||
.run(context -> {
|
||||
assertThat(context).hasBean("defaultService");
|
||||
SimpleService simpleService = context.getBean(DefaultService.class);
|
||||
assertThat(simpleService.serve()).isEqualTo("Default Service");
|
||||
assertThat(context).doesNotHaveBean("customService");
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@TestPropertySource("classpath:ConditionalOnPropertyTest.properties")
|
||||
protected static class SimpleServiceConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "com.baeldung.service", havingValue = "default")
|
||||
@ConditionalOnMissingBean
|
||||
public DefaultService defaultService() {
|
||||
return new DefaultService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "com.baeldung.service", havingValue = "custom")
|
||||
@ConditionalOnMissingBean
|
||||
public CustomService customService() {
|
||||
return new CustomService();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
com.baeldung.service=custom
|
@ -38,12 +38,12 @@
|
||||
</sequenceFlow>
|
||||
<serviceTask id="tutorialApproved"
|
||||
name="Publish the approved tutorial."
|
||||
flowable:class="com.sapient.learning.service.PublishArticleService" />
|
||||
flowable:class="com.baeldung.service.PublishArticleService" />
|
||||
<sequenceFlow sourceRef="tutorialApproved"
|
||||
targetRef="end" />
|
||||
<serviceTask id="tutorialRejected"
|
||||
name="Send out rejection email"
|
||||
flowable:class="com.sapient.learning.service.SendMailService" />
|
||||
flowable:class="com.baeldung.service.SendMailService" />
|
||||
<sequenceFlow sourceRef="tutorialRejected"
|
||||
targetRef="end" />
|
||||
<endEvent id="end" />
|
||||
|
@ -13,11 +13,11 @@ import org.flowable.task.api.Task;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@ExtendWith(FlowableSpringExtension.class)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
public class ArticleWorkflowUnitTest {
|
||||
@SpringBootTest
|
||||
public class ArticleWorkflowIntegrationTest {
|
||||
@Autowired
|
||||
private RuntimeService runtimeService;
|
||||
@Autowired
|
@ -24,7 +24,7 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-zuul</artifactId>
|
||||
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
@ -34,14 +34,16 @@
|
||||
<dependency>
|
||||
<groupId>org.webjars</groupId>
|
||||
<artifactId>jquery</artifactId>
|
||||
<version>${jquery.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.webjars</groupId>
|
||||
<artifactId>bootstrap</artifactId>
|
||||
<version>${bootstrap.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.webjars</groupId>
|
||||
<artifactId>webjars-locator</artifactId>
|
||||
<artifactId>webjars-locator-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
@ -62,8 +64,8 @@
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security.oauth</groupId>
|
||||
<artifactId>spring-security-oauth2</artifactId>
|
||||
<groupId>org.springframework.security.oauth.boot</groupId>
|
||||
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@ -89,8 +91,10 @@
|
||||
</dependencyManagement>
|
||||
|
||||
<properties>
|
||||
<js-cookie.version>2.1.0</js-cookie.version>
|
||||
<spring-cloud.version>Dalston.SR4</spring-cloud.version>
|
||||
<js-cookie.version>2.2.0</js-cookie.version>
|
||||
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
|
||||
<jquery.version>3.4.1</jquery.version>
|
||||
<bootstrap.version>4.3.1</bootstrap.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
@ -2,7 +2,8 @@
|
||||
# These are default settings, but we add them for clarity.
|
||||
server:
|
||||
port: 8080
|
||||
contextPath: /
|
||||
servlet:
|
||||
context-path: /
|
||||
|
||||
# Configure the Authorization Server and User Info Resource Server details
|
||||
security:
|
||||
@ -21,6 +22,7 @@ person:
|
||||
# Proxies the calls to http://localhost:8080/api/* to our REST service at http://localhost:8081/*
|
||||
# and automatically includes our OAuth2 token in the request headers
|
||||
zuul:
|
||||
sensitiveHeaders: Cookie,Set-Cookie
|
||||
routes:
|
||||
resource:
|
||||
path: /api/**
|
||||
|
@ -19,8 +19,8 @@
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security.oauth</groupId>
|
||||
<artifactId>spring-security-oauth2</artifactId>
|
||||
<groupId>org.springframework.security.oauth.boot</groupId>
|
||||
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
@ -30,6 +30,7 @@
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-jwt</artifactId>
|
||||
<version>${spring-jwt.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@ -55,7 +56,8 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
|
||||
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
|
||||
<spring-jwt.version>1.0.10.RELEASE</spring-jwt.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
@ -3,7 +3,7 @@ package com.baeldung.config;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.http.SessionCreationPolicy;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
|
||||
|
||||
@ -11,15 +11,18 @@ import org.springframework.security.oauth2.config.annotation.web.configuration.R
|
||||
* REST API Resource Server.
|
||||
*/
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
@EnableResourceServer
|
||||
@EnableGlobalMethodSecurity(prePostEnabled = true) // Allow method annotations like @PreAuthorize
|
||||
public class ResourceConfigurer extends ResourceServerConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
public void configure(HttpSecurity http) throws Exception {
|
||||
http.httpBasic().disable();
|
||||
http.authorizeRequests().anyRequest().authenticated();
|
||||
http.sessionManagement()
|
||||
.sessionCreationPolicy(SessionCreationPolicy.NEVER)
|
||||
.and()
|
||||
.authorizeRequests()
|
||||
.anyRequest().authenticated();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ server:
|
||||
|
||||
# Configure the public key to use for verifying the incoming JWT tokens
|
||||
security:
|
||||
sessions: NEVER
|
||||
oauth2:
|
||||
resource:
|
||||
jwt:
|
||||
|
@ -38,7 +38,7 @@
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<spring-cloud-starter-oauth2.version>1.1.2.RELEASE</spring-cloud-starter-oauth2.version>
|
||||
<spring-cloud-starter-oauth2.version>2.1.2.RELEASE</spring-cloud-starter-oauth2.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -9,6 +9,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
|
||||
@ -19,9 +20,7 @@ import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFacto
|
||||
@Configuration
|
||||
@EnableAuthorizationServer
|
||||
@Order(6)
|
||||
public class AuthServerConfigurer
|
||||
extends
|
||||
AuthorizationServerConfigurerAdapter {
|
||||
public class AuthServerConfigurer extends AuthorizationServerConfigurerAdapter {
|
||||
|
||||
@Value("${jwt.certificate.store.file}")
|
||||
private Resource keystore;
|
||||
@ -37,6 +36,9 @@ public class AuthServerConfigurer
|
||||
|
||||
@Autowired
|
||||
private UserDetailsService userDetailsService;
|
||||
|
||||
@Autowired
|
||||
private BCryptPasswordEncoder passwordEncoder;
|
||||
|
||||
@Override
|
||||
public void configure(
|
||||
@ -45,8 +47,8 @@ public class AuthServerConfigurer
|
||||
clients
|
||||
.inMemory()
|
||||
.withClient("authserver")
|
||||
.secret("passwordforauthserver")
|
||||
.redirectUris("http://localhost:8080/")
|
||||
.secret(passwordEncoder.encode("passwordforauthserver"))
|
||||
.redirectUris("http://localhost:8080/login")
|
||||
.authorizedGrantTypes("authorization_code",
|
||||
"refresh_token")
|
||||
.scopes("myscope")
|
||||
|
@ -2,10 +2,10 @@ package com.baeldung.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
|
||||
public class WebMvcConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
@ -6,8 +6,8 @@ import org.springframework.security.config.annotation.authentication.builders.Au
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;
|
||||
|
||||
@Configuration
|
||||
@ -34,7 +34,7 @@ public class WebSecurityConfigurer
|
||||
AuthenticationManagerBuilder auth) throws Exception {
|
||||
auth
|
||||
.inMemoryAuthentication()
|
||||
.withUser("user").password("user")
|
||||
.withUser("user").password(passwordEncoder().encode("user"))
|
||||
.roles("USER")
|
||||
.and()
|
||||
.withUser("admin").password("admin")
|
||||
@ -48,5 +48,9 @@ public class WebSecurityConfigurer
|
||||
return super.userDetailsServiceBean();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public BCryptPasswordEncoder passwordEncoder() {
|
||||
return new BCryptPasswordEncoder();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,8 @@
|
||||
# Make the application available at http://localhost:7070/authserver
|
||||
server:
|
||||
port: 7070
|
||||
contextPath: /authserver
|
||||
servlet:
|
||||
context-path: /authserver
|
||||
|
||||
# Our certificate settings for enabling JWT tokens
|
||||
jwt:
|
||||
@ -11,11 +12,4 @@ jwt:
|
||||
password: abirkhan04
|
||||
key:
|
||||
alias: myauthkey
|
||||
password: abirkhan04
|
||||
|
||||
|
||||
security:
|
||||
oauth2:
|
||||
resource:
|
||||
filter-order: 3
|
||||
|
||||
password: abirkhan04
|
@ -8,10 +8,10 @@
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-1</artifactId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-1</relativePath>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
|
@ -8,3 +8,4 @@ The "REST With Spring" Classes: http://github.learnspringsecurity.com
|
||||
### Relevant Articles:
|
||||
- [Spring Security Authentication Provider](http://www.baeldung.com/spring-security-authentication-provider)
|
||||
- [Retrieve User Information in Spring Security](http://www.baeldung.com/get-user-in-spring-security)
|
||||
- [Spring Security – Run-As Authentication](https://www.baeldung.com/spring-security-run-as-auth)
|
||||
|
@ -1,43 +0,0 @@
|
||||
package com.baeldung.springsessionmongodb;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.session.data.mongo.MongoOperationsSessionRepository;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import springsessionmongodb.SpringSessionMongoDBApplication;
|
||||
|
||||
import java.util.Base64;
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = SpringSessionMongoDBApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
|
||||
public class SpringSessionMongoDBIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MongoOperationsSessionRepository repository;
|
||||
|
||||
private TestRestTemplate restTemplate = new TestRestTemplate();
|
||||
|
||||
@Test
|
||||
public void givenEndpointIsCalledTwiceAndResponseIsReturned_whenMongoDBIsQueriedForCount_thenCountMustBeSame() {
|
||||
HttpEntity<String> response = restTemplate.
|
||||
exchange("http://localhost:" + 8080, HttpMethod.GET, null, String.class);
|
||||
HttpHeaders headers = response.getHeaders();
|
||||
String set_cookie = headers.getFirst(HttpHeaders.SET_COOKIE);
|
||||
|
||||
Assert.assertEquals(response.getBody(),
|
||||
repository.findById(getSessionId(set_cookie)).getAttribute("count").toString());
|
||||
}
|
||||
|
||||
private String getSessionId(String set_cookie) {
|
||||
return new String(Base64.getDecoder().decode(set_cookie.split(";")[0].split("=")[1]));
|
||||
}
|
||||
|
||||
}
|
@ -32,6 +32,7 @@
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||
<version>${spring-boot-starter-data-mongodb.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
@ -43,6 +44,7 @@
|
||||
|
||||
<properties>
|
||||
<spring-session-data-mongodb.version>2.1.3.RELEASE</spring-session-data-mongodb.version>
|
||||
<spring-boot-starter-data-mongodb.version>2.1.5.RELEASE</spring-boot-starter-data-mongodb.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
|
@ -1,11 +1,11 @@
|
||||
package com.baeldung.springsessionmongodb.controller;
|
||||
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
@RestController
|
||||
public class SpringSessionMongoDBController {
|
||||
|
||||
@ -17,7 +17,7 @@ public class SpringSessionMongoDBController {
|
||||
if (counter == null) {
|
||||
counter = 1;
|
||||
} else {
|
||||
counter += 1;
|
||||
counter++;
|
||||
}
|
||||
|
||||
session.setAttribute("count", counter);
|
||||
|
@ -1,7 +1,5 @@
|
||||
package com.baeldung.springsessionmongodb;
|
||||
|
||||
import java.util.Base64;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@ -14,6 +12,8 @@ import org.springframework.http.HttpMethod;
|
||||
import org.springframework.session.data.mongo.MongoOperationsSessionRepository;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.Base64;
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = SpringSessionMongoDBApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
|
||||
@ -26,8 +26,8 @@ public class SpringSessionMongoDBIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenEndpointIsCalledTwiceAndResponseIsReturned_whenMongoDBIsQueriedForCount_thenCountMustBeSame() {
|
||||
HttpEntity<String> response = restTemplate.
|
||||
exchange("http://localhost:" + 8080, HttpMethod.GET, null, String.class);
|
||||
HttpEntity<String> response = restTemplate
|
||||
.exchange("http://localhost:" + 8080, HttpMethod.GET, null, String.class);
|
||||
HttpHeaders headers = response.getHeaders();
|
||||
String set_cookie = headers.getFirst(HttpHeaders.SET_COOKIE);
|
||||
|
||||
|
@ -1,12 +1,11 @@
|
||||
package org.baeldung;
|
||||
|
||||
import com.baeldung.springsessionmongodb.SpringSessionMongoDBApplication;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.springsessionmongodb.SpringSessionMongoDBApplication;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = SpringSessionMongoDBApplication.class)
|
||||
public class SpringContextIntegrationTest {
|
||||
|
@ -44,6 +44,11 @@
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>LATEST</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.persistence</groupId>
|
||||
<artifactId>javax.persistence</artifactId>
|
||||
@ -66,6 +71,11 @@
|
||||
<version>${awaitility.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${servlet.api.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -84,6 +94,8 @@
|
||||
<hamcrest.version>2.0.0.0</hamcrest.version>
|
||||
<awaitility.version>3.1.6</awaitility.version>
|
||||
<junit.jupiter.version>5.4.0</junit.jupiter.version>
|
||||
<spring.version>5.1.4.RELEASE</spring.version>
|
||||
<servlet.api.version>4.0.1</servlet.api.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -0,0 +1,17 @@
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
@EnableWebMvc
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = {"com.baeldung.controller.parameterized"})
|
||||
public class WebConfig {
|
||||
|
||||
@Autowired
|
||||
private ServletContext ctx;
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.baeldung.controller.parameterized;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Controller
|
||||
public class EmployeeRoleController {
|
||||
|
||||
private static Map<String, Role> userRoleCache = new HashMap<>();
|
||||
|
||||
static {
|
||||
userRoleCache.put("John", Role.ADMIN);
|
||||
userRoleCache.put("Doe", Role.EMPLOYEE);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/role/{name}", method = RequestMethod.GET, produces = "application/text")
|
||||
@ResponseBody
|
||||
public String getEmployeeRole(@PathVariable("name") String employeeName) {
|
||||
|
||||
return userRoleCache.get(employeeName).toString();
|
||||
}
|
||||
|
||||
private enum Role {
|
||||
ADMIN, EMPLOYEE
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.baeldung.controller.parameterized;
|
||||
|
||||
import com.baeldung.config.WebConfig;
|
||||
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.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = WebConfig.class)
|
||||
public class RoleControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
private static final String CONTENT_TYPE = "application/text;charset=ISO-8859-1";
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeNameJohnWhenInvokeRoleThenReturnAdmin() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/role/John")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE))
|
||||
.andExpect(MockMvcResultMatchers.content().string("ADMIN"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeNameDoeWhenInvokeRoleThenReturnEmployee() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/role/Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE))
|
||||
.andExpect(MockMvcResultMatchers.content().string("EMPLOYEE"));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
package com.baeldung.controller.parameterized;
|
||||
|
||||
import com.baeldung.config.WebConfig;
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameter;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestContextManager;
|
||||
import org.springframework.test.context.junit4.rules.SpringClassRule;
|
||||
import org.springframework.test.context.junit4.rules.SpringMethodRule;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = WebConfig.class)
|
||||
public class RoleControllerParameterizedClassRuleIntegrationTest {
|
||||
|
||||
private static final String CONTENT_TYPE = "application/text;charset=ISO-8859-1";
|
||||
|
||||
@ClassRule
|
||||
public static final SpringClassRule scr = new SpringClassRule();
|
||||
|
||||
@Rule
|
||||
public final SpringMethodRule smr = new SpringMethodRule();
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Parameter(value = 0)
|
||||
public String name;
|
||||
|
||||
@Parameter(value = 1)
|
||||
public String role;
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> data() {
|
||||
Collection<Object[]> params = new ArrayList();
|
||||
params.add(new Object[]{"John", "ADMIN"});
|
||||
params.add(new Object[]{"Doe", "EMPLOYEE"});
|
||||
|
||||
return params;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeNameWhenInvokeRoleThenReturnRole() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/role/" + name)).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE))
|
||||
.andExpect(MockMvcResultMatchers.content().string(role));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package com.baeldung.controller.parameterized;
|
||||
|
||||
import com.baeldung.config.WebConfig;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameter;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestContextManager;
|
||||
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.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = WebConfig.class)
|
||||
public class RoleControllerParameterizedIntegrationTest {
|
||||
|
||||
private static final String CONTENT_TYPE = "application/text;charset=ISO-8859-1";
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
private TestContextManager testContextManager;
|
||||
|
||||
@Parameter(value = 0)
|
||||
public String name;
|
||||
|
||||
@Parameter(value = 1)
|
||||
public String role;
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> data() {
|
||||
Collection<Object[]> params = new ArrayList();
|
||||
params.add(new Object[]{"John", "ADMIN"});
|
||||
params.add(new Object[]{"Doe", "EMPLOYEE"});
|
||||
|
||||
return params;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.testContextManager = new TestContextManager(getClass());
|
||||
this.testContextManager.prepareTestInstance(this);
|
||||
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeNameWhenInvokeRoleThenReturnRole() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/role/" + name)).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE))
|
||||
.andExpect(MockMvcResultMatchers.content().string(role));
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user