Merge remote-tracking branch 'eugenp/master'

This commit is contained in:
DOHA 2018-09-17 23:35:19 +03:00
commit c3f2c6dee9
60 changed files with 929 additions and 113 deletions

View File

@ -0,0 +1,17 @@
package com.baeldung.zoneddatetime;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class OffsetDateTimeExample {
public OffsetDateTime getCurrentTimeByZoneOffset(String region) {
LocalDateTime now = LocalDateTime.now();
ZoneId zone = ZoneId.of(region);
ZoneOffset zoneOffSet= zone.getRules().getOffset(now);
OffsetDateTime date = OffsetDateTime.now(zoneOffSet);
return date;
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.zoneddatetime;
import java.time.LocalDateTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class OffsetTimeExample {
public OffsetTime getCurrentTimeByZoneOffset(String region) {
LocalDateTime now = LocalDateTime.now();
ZoneId zone = ZoneId.of(region);
ZoneOffset zoneOffSet = zone.getRules()
.getOffset(now);
OffsetTime time = OffsetTime.now(zoneOffSet);
return time;
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.zoneddatetime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ZoneDateTimeExample {
public ZonedDateTime getCurrentTimeByZoneId(String region) {
ZoneId zone = ZoneId.of(region);
ZonedDateTime date = ZonedDateTime.now(zone);
return date;
}
public ZonedDateTime convertZonedDateTime(ZonedDateTime sourceDate, String destZone) {
ZoneId destZoneId = ZoneId.of(destZone);
ZonedDateTime destDate = sourceDate.withZoneSameInstant(destZoneId);
return destDate;
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.zoneddatetime;
import static org.junit.Assert.assertTrue;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import org.junit.Test;
public class OffsetDateTimeExampleUnitTest {
OffsetDateTimeExample offsetDateTimeExample = new OffsetDateTimeExample();
@Test
public void givenZoneOffset_whenGetCurrentTime_thenResultHasZone() {
String zone = "Europe/Berlin";
OffsetDateTime time = offsetDateTimeExample.getCurrentTimeByZoneOffset(zone);
assertTrue(time.getOffset()
.equals(ZoneId.of(zone)
.getRules()
.getOffset(LocalDateTime.now())));
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.zoneddatetime;
import static org.junit.Assert.assertTrue;
import java.time.LocalDateTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import org.junit.Test;
public class OffsetTimeExampleUnitTest {
OffsetTimeExample offsetTimeExample = new OffsetTimeExample();
@Test
public void givenZoneOffset_whenGetCurrentTime_thenResultHasZone() {
String zone = "Europe/Berlin";
OffsetTime time = offsetTimeExample.getCurrentTimeByZoneOffset(zone);
assertTrue(time.getOffset()
.equals(ZoneId.of(zone)
.getRules()
.getOffset(LocalDateTime.now())));
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.zoneddatetime;
import static org.junit.Assert.assertTrue;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import org.junit.Test;
public class ZoneDateTimeExampleUnitTest {
ZoneDateTimeExample zoneDateTimeExample = new ZoneDateTimeExample();
@Test
public void givenZone_whenGetCurrentTime_thenResultHasZone() {
String zone = "Europe/Berlin";
ZonedDateTime time = zoneDateTimeExample.getCurrentTimeByZoneId(zone);
assertTrue(time.getZone()
.equals(ZoneId.of(zone)));
}
@Test
public void givenZones_whenConvertDateByZone_thenGetConstantDiff() {
String sourceZone = "Europe/Berlin";
String destZone = "Asia/Tokyo";
ZonedDateTime sourceDate = zoneDateTimeExample.getCurrentTimeByZoneId(sourceZone);
ZonedDateTime destDate = zoneDateTimeExample.convertZonedDateTime(sourceDate, destZone);
assertTrue(sourceDate.toInstant().compareTo(destDate.toInstant()) == 0);
}
}

View File

@ -0,0 +1,48 @@
package com.baeldung.kotlin
import org.junit.Test
import kotlin.test.assertEquals
class StringConcatenationTest {
@Test
fun givenTwoStrings_concatenateWithTemplates_thenEquals() {
val a = "Hello"
val b = "Baeldung"
val c = "$a $b"
assertEquals("Hello Baeldung", c)
}
@Test
fun givenTwoStrings_concatenateWithPlusOperator_thenEquals() {
val a = "Hello"
val b = "Baeldung"
val c = a + " " + b
assertEquals("Hello Baeldung", c)
}
@Test
fun givenTwoStrings_concatenateWithStringBuilder_thenEquals() {
val a = "Hello"
val b = "Baeldung"
val builder = StringBuilder()
builder.append(a).append(" ").append(b)
val c = builder.toString()
assertEquals("Hello Baeldung", c)
}
@Test
fun givenTwoStrings_concatenateWithPlusMethod_thenEquals() {
val a = "Hello"
val b = "Baeldung"
val c = a.plus(" ").plus(b)
assertEquals("Hello Baeldung", c)
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.kotlin.gson
import com.google.gson.Gson
import org.junit.Assert
import org.junit.Test
class GsonUnitTest {
var gson = Gson()
@Test
fun givenObject_thenGetJSONString() {
var jsonString = gson.toJson(TestModel(1,"Test"))
Assert.assertEquals(jsonString, "{\"id\":1,\"description\":\"Test\"}")
}
@Test
fun givenJSONString_thenGetObject() {
var jsonString = "{\"id\":1,\"description\":\"Test\"}";
var testModel = gson.fromJson(jsonString, TestModel::class.java)
Assert.assertEquals(testModel.id, 1)
Assert.assertEquals(testModel.description, "Test")
}
data class TestModel(
val id: Int,
val description: String
)
}

View File

@ -1,5 +1,11 @@
package com.baeldung.hibernate;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
import com.baeldung.hibernate.entities.DeptEmployee;
import com.baeldung.hibernate.optimisticlocking.OptimisticLockingCourse;
import com.baeldung.hibernate.optimisticlocking.OptimisticLockingStudent;
import com.baeldung.hibernate.pessimisticlocking.Individual;
@ -16,10 +22,30 @@ import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
import com.baeldung.hibernate.pojo.Course;
import com.baeldung.hibernate.pojo.Employee;
import com.baeldung.hibernate.pojo.EntityDescription;
import com.baeldung.hibernate.pojo.OrderEntry;
import com.baeldung.hibernate.pojo.OrderEntryIdClass;
import com.baeldung.hibernate.pojo.OrderEntryPK;
import com.baeldung.hibernate.pojo.Person;
import com.baeldung.hibernate.pojo.Phone;
import com.baeldung.hibernate.pojo.PointEntity;
import com.baeldung.hibernate.pojo.PolygonEntity;
import com.baeldung.hibernate.pojo.Product;
import com.baeldung.hibernate.pojo.Student;
import com.baeldung.hibernate.pojo.TemporalValues;
import com.baeldung.hibernate.pojo.User;
import com.baeldung.hibernate.pojo.UserProfile;
import com.baeldung.hibernate.pojo.inheritance.Animal;
import com.baeldung.hibernate.pojo.inheritance.Bag;
import com.baeldung.hibernate.pojo.inheritance.Book;
import com.baeldung.hibernate.pojo.inheritance.Car;
import com.baeldung.hibernate.pojo.inheritance.MyEmployee;
import com.baeldung.hibernate.pojo.inheritance.MyProduct;
import com.baeldung.hibernate.pojo.inheritance.Pen;
import com.baeldung.hibernate.pojo.inheritance.Pet;
import com.baeldung.hibernate.pojo.inheritance.Vehicle;
public class HibernateUtil {
private static SessionFactory sessionFactory;
@ -72,6 +98,8 @@ public class HibernateUtil {
metadataSources.addAnnotatedClass(PessimisticLockingCourse.class);
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Customer.class);
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Address.class);
metadataSources.addAnnotatedClass(DeptEmployee.class);
metadataSources.addAnnotatedClass(com.baeldung.hibernate.entities.Department.class);
metadataSources.addAnnotatedClass(OptimisticLockingCourse.class);
metadataSources.addAnnotatedClass(OptimisticLockingStudent.class);

View File

@ -0,0 +1,45 @@
package com.baeldung.hibernate.entities;
import java.util.List;
import javax.persistence.*;
@Entity
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
private String name;
@OneToMany(mappedBy="department")
private List<DeptEmployee> employees;
public Department(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<DeptEmployee> getEmployees() {
return employees;
}
public void setEmployees(List<DeptEmployee> employees) {
this.employees = employees;
}
}

View File

@ -0,0 +1,65 @@
package com.baeldung.hibernate.entities;
import javax.persistence.*;
@Entity
public class DeptEmployee {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
private String employeeNumber;
private String designation;
private String name;
@ManyToOne
private Department department;
public DeptEmployee(String name, String employeeNumber, Department department) {
this.name = name;
this.employeeNumber = employeeNumber;
this.department = department;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getEmployeeNumber() {
return employeeNumber;
}
public void setEmployeeNumber(String employeeNumber) {
this.employeeNumber = employeeNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.hibernate.pojo;
public class Result {
private String employeeName;
private String departmentName;
public Result(String employeeName, String departmentName) {
this.employeeName = employeeName;
this.departmentName = departmentName;
}
public Result() {
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
}

View File

@ -0,0 +1,77 @@
package com.baeldung.hibernate;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.IOException;
import java.util.List;
import com.baeldung.hibernate.entities.DeptEmployee;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.hibernate.transform.Transformers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.baeldung.hibernate.entities.Department;
import com.baeldung.hibernate.pojo.Result;
public class CustomClassIntegrationTest {
private Session session;
private Transaction transaction;
@BeforeEach
public void setUp() throws IOException {
session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
session.createNativeQuery("delete from manager").executeUpdate();
session.createNativeQuery("delete from department").executeUpdate();
Department department = new Department("Sales");
DeptEmployee employee = new DeptEmployee("John Smith", "001", department);
session.persist(department);
session.persist(employee);
transaction.commit();
transaction = session.beginTransaction();
}
@Test
public void whenAllManagersAreSelected_ThenObjectGraphIsReturned() {
Query<DeptEmployee> query = session.createQuery("from com.baeldung.hibernate.entities.DeptEmployee");
List<DeptEmployee> deptEmployees = query.list();
DeptEmployee deptEmployee = deptEmployees.get(0);
assertEquals("John Smith", deptEmployee.getName());
assertEquals("Sales", deptEmployee.getDepartment().getName());
}
@Test
public void whenIndividualPropertiesAreSelected_ThenObjectArrayIsReturned() {
Query query = session.createQuery("select m.name, m.department.name from com.baeldung.hibernate.entities.DeptEmployee m");
List managers = query.list();
Object[] manager = (Object[]) managers.get(0);
assertEquals("John Smith", manager[0]);
assertEquals("Sales", manager[1]);
}
@Test
public void whenResultConstructorInSelect_ThenListOfResultIsReturned() {
Query<Result> query = session.createQuery("select new com.baeldung.hibernate.pojo.Result(m.name, m.department.name) "
+ "from DeptEmployee m");
List<Result> results = query.list();
Result result = results.get(0);
assertEquals("John Smith", result.getEmployeeName());
assertEquals("Sales", result.getDepartmentName());
}
@Test
public void whenResultTransformerOnQuery_ThenListOfResultIsReturned() {
Query query = session.createQuery("select m.name as employeeName, m.department.name as departmentName "
+ "from com.baeldung.hibernate.entities.DeptEmployee m");
query.setResultTransformer(Transformers.aliasToBean(Result.class));
List<Result> results = query.list();
Result result = results.get(0);
assertEquals("John Smith", result.getEmployeeName());
assertEquals("Sales", result.getDepartmentName());
}
}

View File

@ -6,5 +6,4 @@
- [Writing Specifications with Kotlin and Spek](http://www.baeldung.com/kotlin-spek)
- [Processing JSON with Kotlin and Klaxson](http://www.baeldung.com/kotlin-json-klaxson)
- [Kotlin with Ktor](http://www.baeldung.com/kotlin-ktor)
- [Idiomatic Logging in Kotlin](http://www.baeldung.com/kotlin-logging)
- [Guide to the Kotlin Exposed Framework](https://www.baeldung.com/kotlin-exposed-persistence)
- [Guide to the Kotlin Exposed Framework](https://www.baeldung.com/kotlin-exposed-persistence)

View File

@ -147,7 +147,6 @@
<artifactId>jmapper-core</artifactId>
<version>${jmapper.version}</version>
</dependency>
<!-- crunch project -->
<dependency>
<groupId>org.apache.crunch</groupId>
@ -185,7 +184,72 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-connector-kafka-0.11_2.11</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-java_2.11</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-core</artifactId>
<version>${flink.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-java</artifactId>
<version>${flink.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-test-utils_2.11</artifactId>
<version>${flink.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility-proxy</artifactId>
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -336,6 +400,10 @@
<ignite.version>2.4.0</ignite.version>
<gson.version>2.8.2</gson.version>
<cache.version>1.1.0</cache.version>
<flink.version>1.5.0</flink.version>
<jackson.version>2.8.5</jackson.version>
<awaitility.version>3.0.0</awaitility.version>
<assertj.version>3.6.2</assertj.version>
<hazelcast.version>3.8.4</hazelcast.version>
<maven-antrun-plugin.version>1.8</maven-antrun-plugin.version>
<build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version>

View File

@ -18,7 +18,6 @@ public class InputMessage {
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}

View File

@ -1,8 +1,6 @@
package com.baeldung.flink.schema;
import com.baeldung.flink.model.InputMessage;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.apache.flink.api.common.serialization.DeserializationSchema;

View File

@ -154,45 +154,7 @@
<artifactId>commons-dbutils</artifactId>
<version>${commons.dbutils.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-connector-kafka-0.11_2.11</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-java_2.11</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-core</artifactId>
<version>${flink.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-java</artifactId>
<version>${flink.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-test-utils_2.11</artifactId>
<version>${flink.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
@ -239,11 +201,7 @@
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- JDO -->
<dependency>
<groupId>org.datanucleus</groupId>
@ -900,7 +858,6 @@
<httpclient.version>4.5.3</httpclient.version>
<commons.io.version>2.5</commons.io.version>
<flink.version>1.5.0</flink.version>
<jackson.version>2.8.5</jackson.version>
<neuroph.version>2.92</neuroph.version>
<serenity.version>1.9.26</serenity.version>

View File

@ -18,16 +18,19 @@ import java.util.List;
public class BeanExamples {
public static List<CsvBean> beanBuilderExample(Path path, Class clazz) {
ColumnPositionMappingStrategy ms = new ColumnPositionMappingStrategy();
return beanBuilderExample(path, clazz, ms);
}
public static List<CsvBean> beanBuilderExample(Path path, Class clazz, MappingStrategy ms) {
CsvTransfer csvTransfer = new CsvTransfer();
try {
ColumnPositionMappingStrategy ms = new ColumnPositionMappingStrategy();
ms.setType(clazz);
Reader reader = Files.newBufferedReader(path);
CsvToBean cb = new CsvToBeanBuilder(reader)
.withType(clazz)
.withMappingStrategy(ms)
.build();
CsvToBean cb = new CsvToBeanBuilder(reader).withType(clazz)
.withMappingStrategy(ms)
.build();
csvTransfer.setCsvList(cb.parse());
reader.close();
@ -40,11 +43,10 @@ public class BeanExamples {
public static String writeCsvFromBean(Path path) {
try {
Writer writer = new FileWriter(path.toString());
Writer writer = new FileWriter(path.toString());
StatefulBeanToCsv sbc = new StatefulBeanToCsvBuilder(writer)
.withSeparator(CSVWriter.DEFAULT_SEPARATOR)
.build();
StatefulBeanToCsv sbc = new StatefulBeanToCsvBuilder(writer).withSeparator(CSVWriter.DEFAULT_SEPARATOR)
.build();
List<CsvBean> list = new ArrayList<>();
list.add(new WriteExampleBean("Test1", "sfdsf", "fdfd"));

View File

@ -42,4 +42,13 @@ public class BidiMapUnitTest {
map.put("key1", "value1");
assertEquals(map.getKey("value1"), "key1");
}
@Test
public void givenKeyValue_whenAddValue_thenReplaceFirstKey() {
BidiMap<String, String> map = new DualHashBidiMap<>();
map.put("key1", "value1");
map.put("key2", "value1");
assertEquals(map.size(), 1);
assertFalse(map.containsKey("key1"));
}
}

3
optaplanner/README.md Normal file
View File

@ -0,0 +1,3 @@
### Relevant articles
- [Guide to OptaPlanner](https://www.baeldung.com/opta-planner)

View File

@ -78,9 +78,9 @@ public class PersistenceJPAConfig {
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.cache.use_second_level_cache", env.getProperty("hibernate.cache.use_second_level_cache"));
hibernateProperties.setProperty("hibernate.cache.use_query_cache", env.getProperty("hibernate.cache.use_query_cache"));
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", "false");
return hibernateProperties;
}

View File

@ -5,7 +5,7 @@ import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatche
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { PersistenceJNDIConfig.class };
return new Class[] { PersistenceJPAConfig.class };
}
@Override

View File

@ -1,6 +1,6 @@
package org.baeldung.persistence.criteria.repository;
package org.baeldung.persistence.dao;
import org.baeldung.persistence.criteria.model.Book;
import org.baeldung.persistence.model.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

View File

@ -1,8 +1,8 @@
package org.baeldung.persistence.criteria.repository;
package org.baeldung.persistence.dao;
import java.util.List;
import org.baeldung.persistence.criteria.model.Book;
import org.baeldung.persistence.model.Book;
public interface BookRepositoryCustom {

View File

@ -1,4 +1,4 @@
package org.baeldung.persistence.criteria.dao;
package org.baeldung.persistence.dao;
import java.util.ArrayList;
import java.util.List;
@ -10,8 +10,7 @@ import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.baeldung.persistence.criteria.model.Book;
import org.baeldung.persistence.criteria.repository.BookRepositoryCustom;
import org.baeldung.persistence.model.Book;
import org.springframework.stereotype.Repository;
@Repository

View File

@ -1,12 +1,12 @@
package org.baeldung.persistence.criteria.repository;
package org.baeldung.persistence.dao;
import static org.baeldung.persistence.criteria.repository.BookSpecifications.hasAuthor;
import static org.baeldung.persistence.criteria.repository.BookSpecifications.titleContains;
import static org.baeldung.persistence.dao.BookSpecifications.hasAuthor;
import static org.baeldung.persistence.dao.BookSpecifications.titleContains;
import static org.springframework.data.jpa.domain.Specifications.where;
import java.util.List;
import org.baeldung.persistence.criteria.model.Book;
import org.baeldung.persistence.model.Book;
import org.springframework.stereotype.Service;
@Service

View File

@ -1,6 +1,6 @@
package org.baeldung.persistence.criteria.repository;
package org.baeldung.persistence.dao;
import org.baeldung.persistence.criteria.model.Book;
import org.baeldung.persistence.model.Book;
import org.springframework.data.jpa.domain.Specification;
public class BookSpecifications {

View File

@ -1,4 +1,4 @@
package org.baeldung.persistence.criteria.model;
package org.baeldung.persistence.model;
import javax.persistence.Entity;
import javax.persistence.Id;

View File

@ -21,8 +21,6 @@
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
</props>
</property>
</bean>

151
pom.xml
View File

@ -405,7 +405,7 @@
<module>json</module>
<module>jsoup</module>
<module>testing-modules/junit-5</module>
<module>jws</module>
<!-- <module>jws</module> --><!-- POM jar version repo download failure -->
<module>libraries</module>
<module>libraries-data</module>
<module>libraries-server</module>
@ -604,6 +604,80 @@
<module>jnosql</module>
<module>spring-boot-angular-ecommerce</module>
<module>jta</module>
<!--<module>java-dates</module> --> <!-- Commented because we have still not upgraded to java 9 -->
<module>java-websocket</module>
<!-- <module>activejdbc</module> --><!-- PMD voilation -->
<!-- <module>animal-sniffer-mvn-plugin</module> --><!-- PMD voilation -->
<!-- <module>apache-avro</module> --><!-- Malformed POM -->
<module>apache-bval</module>
<module>apache-shiro</module>
<module>apache-spark</module>
<!-- <module>asciidoctor</module> --><!-- Malformed POM -->
<module>checker-plugin</module>
<!-- <module>core-java-10</module> --><!-- Not upgraded to java 10 -->
<!-- <module>core-java-11</module> --><!-- Not upgraded to java 11 -->
<module>core-java-sun</module>
<module>custom-pmd</module>
<module>dagger</module>
<module>data-structures</module>
<module>dubbo</module>
<!-- <module>flyway</module> --><!-- Malformed POM -->
<!-- <module>grpc</module> --><!-- protobuf-maven-plugin filecopy failure -->
<!-- <module>java-difference-date</module> --><!-- PMD voilation -->
<!-- <module>JGit</module> --><!-- Unit test failure -->
<module>jni</module>
<module>jooby</module>
<!-- <module>micronaut</module> --><!-- Not upgraded to java 9 -->
<!-- <module>microprofile</module> --><!-- Takes too much time : Downloads 93 MBs zip and .. -->
<!-- <module>muleesb</module> --><!-- load main class org.mule.munit.remote.RemoteRunner -->
<module>ratpack</module>
<!-- <module>rest-with-spark-java</module> --><!-- PMD voilation -->
<module>spring-boot-autoconfiguration</module>
<module>spring-boot-custom-starter</module>
<!-- <module>spring-boot-jasypt</module> --><!-- PMD voilation -->
<!-- <module>spring-custom-aop</module> --><!-- Malformed POM -->
<module>spring-data-rest-querydsl</module>
<!-- <module>spring-groovy</module> --><!-- PMD voilation -->
<module>spring-mobile</module>
<!-- <module>spring-mustache</module> --><!-- PMD voilation -->
<module>spring-mvc-simple</module>
<!-- <module>spring-mybatis</module> --><!-- Compilation failure -->
<module>spring-rest-hal-browser</module>
<module>spring-rest-shell</module>
<module>spring-rest-template</module>
<module>spring-roo</module>
<module>spring-security-stormpath</module>
<module>sse-jaxrs</module>
<!-- <module>static-analysis</module> --><!-- PMD voilation -->
<module>stripe</module>
<!-- <module>structurizr</module> --><!-- Artiifact not found -->
<!-- <module>Twitter4J</module> --><!-- Test failure -->
<module>wicket</module>
<module>xstream</module>
<module>cas/cas-secured-app</module>
<!-- <module>cas/cas-server</module> --><!-- Takes too much time -->
<!-- <module>graphql/graphql-java</module> --><!-- Wrong parent -->
<!-- <module>guest/deep-jsf</module> --><!-- guest post on different site -->
<!-- <module>guest/junit5-example</module> --><!-- guest post on different site - Compilation failure -->
<!-- <module>guest/log4j2-example</module> --><!-- PMD voilation -->
<!-- <module>guest/logback-example</module> --><!-- PMD voilation -->
<!-- <module>guest/memory-leaks</module> --><!-- guest post on different site -->
<!-- <module>guest/remote-debugging</module> --><!-- guest post on different site -->
<!-- <module>guest/spring-boot-app</module> --><!-- guest post on different site -->
<!-- <module>guest/spring-mvc</module> --><!-- Malformed POM -->
<!-- <module>guest/spring-security</module> --><!-- Malformed POM -->
<!-- <module>guest/thread-pools</module> --><!-- guest post on different site -->
<!-- <module>guest/tomcat-app</module> --><!-- guest post on different site -->
<module>jenkins/hello-world</module>
<!-- <module>rule-engines/easy-rules</module> --><!-- Wrong Parent -->
<!-- <module>rule-engines/openl-tablets</module> --><!-- Wrong Parent -->
<!-- <module>rule-engines/rulebook</module> --><!-- Wrong Parent -->
<module>spring-boot-custom-starter/greeter</module>
<module>spring-boot-h2/spring-boot-h2-database</module>
<module>spring-boot-h2/spring-boot-h2-remote-app</module>
<!-- <module>guest\webservices\rest-client</module> --><!-- guest post on different site -->
<!-- <module>guest\webservices\rest-server</module> --><!-- PMD voilation -->
<!-- <module>guest\webservices\spring-rest-service</module> --><!-- guest post on different site -->
</modules>
</profile>
@ -847,7 +921,80 @@
<!-- group 4 - OK, 12 min, 3,961 KB log, 12 failed tests -->
<!-- <module>libraries</module> <module>jmeter</module> -->
<!--<module>java-dates</module> --> <!-- Commented because we have still not upgraded to java 9 -->
<module>java-websocket</module>
<!-- <module>activejdbc</module> --><!-- PMD voilation -->
<!-- <module>animal-sniffer-mvn-plugin</module> --><!-- PMD voilation -->
<!-- <module>apache-avro</module> --><!-- Malformed POM -->
<module>apache-bval</module>
<module>apache-shiro</module>
<module>apache-spark</module>
<!-- <module>asciidoctor</module> --><!-- Malformed POM -->
<module>checker-plugin</module>
<!-- <module>core-java-10</module> --><!-- Not upgraded to java 10 -->
<!-- <module>core-java-11</module> --><!-- Not upgraded to java 11 -->
<module>core-java-sun</module>
<module>custom-pmd</module>
<module>dagger</module>
<module>data-structures</module>
<module>dubbo</module>
<!-- <module>flyway</module> --><!-- Malformed POM -->
<!-- <module>grpc</module> --><!-- protobuf-maven-plugin filecopy failure -->
<!-- <module>java-difference-date</module> --><!-- PMD voilation -->
<!-- <module>JGit</module> --><!-- Unit test failure -->
<module>jni</module>
<module>jooby</module>
<!-- <module>micronaut</module> --><!-- Not upgraded to java 9 -->
<!-- <module>microprofile</module> --><!-- Takes too much time : Downloads 93 MBs zip and .. -->
<!-- <module>muleesb</module> --><!-- load main class org.mule.munit.remote.RemoteRunner -->
<module>ratpack</module>
<!-- <module>rest-with-spark-java</module> --><!-- PMD voilation -->
<module>spring-boot-autoconfiguration</module>
<module>spring-boot-custom-starter</module>
<!-- <module>spring-boot-jasypt</module> --><!-- PMD voilation -->
<!-- <module>spring-custom-aop</module> --><!-- Malformed POM -->
<module>spring-data-rest-querydsl</module>
<!-- <module>spring-groovy</module> --><!-- PMD voilation -->
<module>spring-mobile</module>
<!-- <module>spring-mustache</module> --><!-- PMD voilation -->
<module>spring-mvc-simple</module>
<!-- <module>spring-mybatis</module> --><!-- Compilation failure -->
<module>spring-rest-hal-browser</module>
<module>spring-rest-shell</module>
<module>spring-rest-template</module>
<module>spring-roo</module>
<module>spring-security-stormpath</module>
<module>sse-jaxrs</module>
<!-- <module>static-analysis</module> --><!-- PMD voilation -->
<module>stripe</module>
<!-- <module>structurizr</module> --><!-- Artiifact not found -->
<!-- <module>Twitter4J</module> --><!-- Test failure -->
<module>wicket</module>
<module>xstream</module>
<module>cas/cas-secured-app</module>
<!-- <module>cas/cas-server</module> --><!-- Takes too much time -->
<!-- <module>graphql/graphql-java</module> --><!-- Wrong parent -->
<!-- <module>guest/deep-jsf</module> --><!-- guest post on different site -->
<!-- <module>guest/junit5-example</module> --><!-- guest post on different site - Compilation failure -->
<!-- <module>guest/log4j2-example</module> --><!-- PMD voilation -->
<!-- <module>guest/logback-example</module> --><!-- PMD voilation -->
<!-- <module>guest/memory-leaks</module> --><!-- guest post on different site -->
<!-- <module>guest/remote-debugging</module> --><!-- guest post on different site -->
<!-- <module>guest/spring-boot-app</module> --><!-- guest post on different site -->
<!-- <module>guest/spring-mvc</module> --><!-- Malformed POM -->
<!-- <module>guest/spring-security</module> --><!-- Malformed POM -->
<!-- <module>guest/thread-pools</module> --><!-- guest post on different site -->
<!-- <module>guest/tomcat-app</module> --><!-- guest post on different site -->
<module>jenkins/hello-world</module>
<!-- <module>rule-engines/easy-rules</module> --><!-- Wrong Parent -->
<!-- <module>rule-engines/openl-tablets</module> --><!-- Wrong Parent -->
<!-- <module>rule-engines/rulebook</module> --><!-- Wrong Parent -->
<module>spring-boot-custom-starter/greeter</module>
<module>spring-boot-h2/spring-boot-h2-database</module>
<module>spring-boot-h2/spring-boot-h2-remote-app</module>
<!-- <module>guest\webservices\rest-client</module> --><!-- guest post on different site -->
<!-- <module>guest\webservices\rest-server</module> --><!-- PMD voilation -->
<!-- <module>guest\webservices\spring-rest-service</module> --><!-- guest post on different site -->
</modules>
</profile>

View File

@ -37,14 +37,14 @@ public class ReactorIntegrationTest {
Flux.just(1, 2, 3, 4)
.log()
.map(i -> i * 2)
.zipWith(Flux.range(0, Integer.MAX_VALUE).log(), (two, one) -> String.format("First Flux: %d, Second Flux: %d", one, two))
.zipWith(Flux.range(0, Integer.MAX_VALUE).log(), (one, two) -> String.format("First Flux: %d, Second Flux: %d", one, two))
.subscribe(elements::add);
assertThat(elements).containsExactly(
"First Flux: 0, Second Flux: 2",
"First Flux: 1, Second Flux: 4",
"First Flux: 2, Second Flux: 6",
"First Flux: 3, Second Flux: 8");
"First Flux: 2, Second Flux: 0",
"First Flux: 4, Second Flux: 1",
"First Flux: 6, Second Flux: 2",
"First Flux: 8, Second Flux: 3");
}
@Test

View File

@ -1,5 +1,6 @@
<?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"
<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>
@ -134,7 +135,26 @@
</execution>
</executions>
</plugin>
<!-- this surefire configuration allows concurrent execution; do not remove -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<parallel>methods</parallel>
<useUnlimitedThreads>true</useUnlimitedThreads>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
<exclude>**/*LongRunningUnitTest.java</exclude>
<exclude>**/*ManualTest.java</exclude>
<exclude>**/JdbcTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<properties>
@ -142,6 +162,8 @@
<asciidoctor-plugin.version>1.5.6</asciidoctor-plugin.version>
<commons-collections4.version>4.1</commons-collections4.version>
<snippetsDirectory>${project.build.directory}/generated-snippets</snippetsDirectory>
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
</properties>
</project>

View File

@ -45,6 +45,15 @@
<artifactId>spring-shell</artifactId>
<version>${org.springframework.shell.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
</dependency>
<!-- aspectj -->
<dependency>
<groupId>org.springframework</groupId>

View File

@ -13,21 +13,36 @@ import org.springframework.web.bind.annotation.RequestMapping;
public class ScopesController {
public static final Logger LOG = LoggerFactory.getLogger(ScopesController.class);
@Resource(name = "requestMessage")
HelloMessageGenerator requestMessage;
@Resource(name = "requestScopedBean")
HelloMessageGenerator requestScopedBean;
@Resource(name = "sessionMessage")
HelloMessageGenerator sessionMessage;
@Resource(name = "sessionScopedBean")
HelloMessageGenerator sessionScopedBean;
@RequestMapping("/scopes")
public String getScopes(final Model model) {
LOG.info("Request Message:" + requestMessage.getMessage());
LOG.info("Session Message" + sessionMessage.getMessage());
requestMessage.setMessage("Good morning!");
sessionMessage.setMessage("Good afternoon!");
model.addAttribute("requestMessage", requestMessage.getMessage());
model.addAttribute("sessionMessage", sessionMessage.getMessage());
@Resource(name = "applicationScopedBean")
HelloMessageGenerator applicationScopedBean;
@RequestMapping("/scopes/request")
public String getRequestScopeMessage(final Model model) {
model.addAttribute("previousMessage", requestScopedBean.getMessage());
requestScopedBean.setMessage("Request Scope Message!");
model.addAttribute("currentMessage", requestScopedBean.getMessage());
return "scopesExample";
}
@RequestMapping("/scopes/session")
public String getSessionScopeMessage(final Model model) {
model.addAttribute("previousMessage", sessionScopedBean.getMessage());
sessionScopedBean.setMessage("Session Scope Message!");
model.addAttribute("currentMessage", sessionScopedBean.getMessage());
return "scopesExample";
}
@RequestMapping("/scopes/application")
public String getApplicationScopeMessage(final Model model) {
model.addAttribute("previousMessage", applicationScopedBean.getMessage());
applicationScopedBean.setMessage("Application Scope Message!");
model.addAttribute("currentMessage", applicationScopedBean.getMessage());
return "scopesExample";
}
}

View File

@ -27,19 +27,25 @@ public class ScopesConfig {
@Bean
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public HelloMessageGenerator requestMessage() {
public HelloMessageGenerator requestScopedBean() {
return new HelloMessageGenerator();
}
@Bean
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public HelloMessageGenerator sessionMessage() {
public HelloMessageGenerator sessionScopedBean() {
return new HelloMessageGenerator();
}
@Bean
@Scope(value = WebApplicationContext.SCOPE_APPLICATION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public HelloMessageGenerator globalSessionMessage() {
public HelloMessageGenerator applicationScopedBean() {
return new HelloMessageGenerator();
}
@Bean
@Scope(scopeName = "websocket", proxyMode = ScopedProxyMode.TARGET_CLASS)
public HelloMessageGenerator websocketScopedBean() {
return new HelloMessageGenerator();
}

View File

@ -2,9 +2,9 @@
<head></head>
<body>
<h1>Bean Scopes Examples</h1>
<br>
Request Message: ${requestMessage }<br>
Session Message: ${sessionMessage }
<h1>Bean Scopes Examples</h1>
<br> Previous Message: ${previousMessage }
<br> Current Message: ${currentMessage }
<br>
</body>
</html>

View File

@ -46,6 +46,10 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.baeldung.springbootmvc.SpringBootMvcApplication</mainClass>
<layout>JAR</layout>
</configuration>
</plugin>
</plugins>
</build>
@ -56,6 +60,7 @@
<java.version>1.8</java.version>
<!-- ROME for RSS -->
<rome.version>1.10.0</rome.version>
<start-class>com.baeldung.springbootmvc.SpringBootMvcApplication</start-class>
</properties>
</project>

View File

@ -6,8 +6,8 @@ import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.baeldung.dependency.exception")
public class CustomConfiguration {
public class SpringDependenciesExampleApplication {
public static void main(String[] args) {
SpringApplication.run(CustomConfiguration.class, args);
SpringApplication.run(SpringDependenciesExampleApplication.class, args);
}
}

View File

@ -0,0 +1,25 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>spring-context-testing</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring.boot.starter.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>${spring.boot.starter.version}</version>
</dependency>
</dependencies>
<properties>
<spring.boot.starter.version>2.0.4.RELEASE</spring.boot.starter.version>
</properties>
</project>

View File

@ -0,0 +1,15 @@
package com.baeldung.testpropertysource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ClassUsingProperty {
@Value("${baeldung.testpropertysource.one}")
private String propertyOne;
public String retrievePropertyOne() {
return propertyOne;
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.testpropertysource;
import static org.assertj.core.api.Assertions.assertThat;
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.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = ClassUsingProperty.class)
@TestPropertySource
public class DefaultTestPropertySourceIntegrationTest {
@Autowired
ClassUsingProperty classUsingProperty;
@Test
public void givenDefaultTestPropertySource_whenVariableOneRetrieved_thenValueInDefaultFileReturned() {
String output = classUsingProperty.retrievePropertyOne();
assertThat(output).isEqualTo("default-value");
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.testpropertysource;
import static org.assertj.core.api.Assertions.assertThat;
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.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = ClassUsingProperty.class)
@TestPropertySource(locations = "/other-location.properties")
public class LocationTestPropertySourceIntegrationTest {
@Autowired
ClassUsingProperty classUsingProperty;
@Test
public void givenDefaultTestPropertySource_whenVariableOneRetrieved_thenValueInDefaultFileReturned() {
String output = classUsingProperty.retrievePropertyOne();
assertThat(output).isEqualTo("other-location-value");
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.testpropertysource;
import static org.assertj.core.api.Assertions.assertThat;
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.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = ClassUsingProperty.class)
@TestPropertySource(locations = "/other-location.properties", properties = "baeldung.testpropertysource.one=other-properties-value")
public class PropertiesTestPropertySourceIntegrationTest {
@Autowired
ClassUsingProperty classUsingProperty;
@Test
public void givenDefaultTestPropertySource_whenVariableOneRetrieved_thenValueInDefaultFileReturned() {
String output = classUsingProperty.retrievePropertyOne();
assertThat(output).isEqualTo("other-properties-value");
}
}

View File

@ -0,0 +1 @@
baeldung.testpropertysource.one=other-location-value