From 05d7d5413144bae3eb577ac3ecc3dae03fed88db Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Mon, 20 Aug 2018 21:24:50 +0200 Subject: [PATCH 01/14] BAEL-2056 --- .../hibernate/joincolumn/Address.Java | 19 ++++++++++++++ .../baeldung/hibernate/joincolumn/Email.java | 22 ++++++++++++++++ .../hibernate/joincolumn/Employee.java | 21 ++++++++++++++++ .../baeldung/hibernate/joincolumn/Office.java | 25 +++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Address.Java create mode 100644 hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Email.java create mode 100644 hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Employee.java create mode 100644 hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Office.java diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Address.Java b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Address.Java new file mode 100644 index 0000000000..c8c0ae6eb8 --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Address.Java @@ -0,0 +1,19 @@ +package com.baeldung.hibernate.joincolumn; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +class Address { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @Column(name = "ZIP") + private String zipCode; + +} \ No newline at end of file diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Email.java b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Email.java new file mode 100644 index 0000000000..6519c2011f --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Email.java @@ -0,0 +1,22 @@ +package com.baeldung.hibernate.joincolumn; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; + +@Entity +class Email { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "employee_id") + private Employee employee; + +} \ No newline at end of file diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Employee.java b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Employee.java new file mode 100644 index 0000000000..5d63c3be11 --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Employee.java @@ -0,0 +1,21 @@ +package com.baeldung.hibernate.joincolumn; + +import java.util.List; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +@Entity +class Employee { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @OneToMany(fetch = FetchType.LAZY, mappedBy = "employee") + private List emails; + +} \ No newline at end of file diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Office.java b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Office.java new file mode 100644 index 0000000000..5ca62af99c --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Office.java @@ -0,0 +1,25 @@ +package com.baeldung.hibernate.joincolumn; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinColumns; +import javax.persistence.ManyToOne; + +@Entity +class Office { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumns({ + @JoinColumn(name="ADDR_ID", referencedColumnName="ID"), + @JoinColumn(name="ADDR_ZIP", referencedColumnName="ZIP") + }) + private Address address; +} \ No newline at end of file From 3d09c6056adc525d9c2146ed02406f099ec43669 Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Mon, 20 Aug 2018 21:46:55 +0200 Subject: [PATCH 02/14] BAEL-2097 fix --- .../main/java/com/baeldung/hibernate/joincolumn/Address.Java | 2 +- .../src/main/java/com/baeldung/hibernate/joincolumn/Email.java | 2 +- .../main/java/com/baeldung/hibernate/joincolumn/Employee.java | 2 +- .../src/main/java/com/baeldung/hibernate/joincolumn/Office.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Address.Java b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Address.Java index c8c0ae6eb8..531a5dea9e 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Address.Java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Address.Java @@ -7,7 +7,7 @@ import javax.persistence.GenerationType; import javax.persistence.Id; @Entity -class Address { +public class Address { @Id @GeneratedValue(strategy = GenerationType.AUTO) diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Email.java b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Email.java index 6519c2011f..0c5c1e9f7e 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Email.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Email.java @@ -9,7 +9,7 @@ import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; @Entity -class Email { +public class Email { @Id @GeneratedValue(strategy = GenerationType.AUTO) diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Employee.java b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Employee.java index 5d63c3be11..03becac9f6 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Employee.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Employee.java @@ -9,7 +9,7 @@ import javax.persistence.Id; import javax.persistence.OneToMany; @Entity -class Employee { +public class Employee { @Id @GeneratedValue(strategy = GenerationType.AUTO) diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Office.java b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Office.java index 5ca62af99c..1eb79cb3e6 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Office.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Office.java @@ -10,7 +10,7 @@ import javax.persistence.JoinColumns; import javax.persistence.ManyToOne; @Entity -class Office { +public class Office { @Id @GeneratedValue(strategy = GenerationType.AUTO) From b27d20a46a6b3b8329f89ff4ac8235f1e059dc75 Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Mon, 20 Aug 2018 21:50:24 +0200 Subject: [PATCH 03/14] Delete Address.Java Remove file with wrong extension --- .../hibernate/joincolumn/Address.Java | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Address.Java diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Address.Java b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Address.Java deleted file mode 100644 index 531a5dea9e..0000000000 --- a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Address.Java +++ /dev/null @@ -1,19 +0,0 @@ -package com.baeldung.hibernate.joincolumn; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; - -@Entity -public class Address { - - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private Long id; - - @Column(name = "ZIP") - private String zipCode; - -} \ No newline at end of file From 82ad3bde94635966d804575bc0d984c7f4fd39d1 Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Mon, 20 Aug 2018 21:51:20 +0200 Subject: [PATCH 04/14] BAEL-2097 add Address entity --- .../hibernate/joincolumn/Address.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Address.java diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Address.java b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Address.java new file mode 100644 index 0000000000..531a5dea9e --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Address.java @@ -0,0 +1,19 @@ +package com.baeldung.hibernate.joincolumn; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Address { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @Column(name = "ZIP") + private String zipCode; + +} \ No newline at end of file From cc4b50a0d8aa37b74ada0a7dfdc03d00dc36a1f3 Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Sat, 25 Aug 2018 14:26:23 +0200 Subject: [PATCH 05/14] BAEL-2097 add integration test --- .../hibernate/joincolumn/Address.java | 15 +++++ .../baeldung/hibernate/joincolumn/Email.java | 25 ++++++++ .../hibernate/joincolumn/Employee.java | 15 +++++ .../baeldung/hibernate/joincolumn/Office.java | 16 ++++++ .../joincolumn/JoinColumnIntegrationTest | 57 +++++++++++++++++++ 5 files changed, 128 insertions(+) create mode 100644 hibernate5/src/test/java/com/baeldung/hibernate/joincolumn/JoinColumnIntegrationTest diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Address.java b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Address.java index 531a5dea9e..8b0a51858d 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Address.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Address.java @@ -16,4 +16,19 @@ public class Address { @Column(name = "ZIP") private String zipCode; + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getZipCode() { + return zipCode; + } + + public void setZipCode(String zipCode) { + this.zipCode = zipCode; + } } \ No newline at end of file diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Email.java b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Email.java index 0c5c1e9f7e..a91fb3b4c9 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Email.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Email.java @@ -15,8 +15,33 @@ public class Email { @GeneratedValue(strategy = GenerationType.AUTO) private Long id; + private String address; + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "employee_id") private Employee employee; + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public Employee getEmployee() { + return employee; + } + + public void setEmployee(Employee employee) { + this.employee = employee; + } } \ No newline at end of file diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Employee.java b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Employee.java index 03becac9f6..3fbdb3820e 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Employee.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Employee.java @@ -18,4 +18,19 @@ public class Employee { @OneToMany(fetch = FetchType.LAZY, mappedBy = "employee") private List emails; + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public List getEmails() { + return emails; + } + + public void setEmails(List emails) { + this.emails = emails; + } } \ No newline at end of file diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Office.java b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Office.java index 1eb79cb3e6..e5b9dc06bc 100644 --- a/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Office.java +++ b/hibernate5/src/main/java/com/baeldung/hibernate/joincolumn/Office.java @@ -22,4 +22,20 @@ public class Office { @JoinColumn(name="ADDR_ZIP", referencedColumnName="ZIP") }) private Address address; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } } \ No newline at end of file diff --git a/hibernate5/src/test/java/com/baeldung/hibernate/joincolumn/JoinColumnIntegrationTest b/hibernate5/src/test/java/com/baeldung/hibernate/joincolumn/JoinColumnIntegrationTest new file mode 100644 index 0000000000..8246a2b01e --- /dev/null +++ b/hibernate5/src/test/java/com/baeldung/hibernate/joincolumn/JoinColumnIntegrationTest @@ -0,0 +1,57 @@ +package com.baeldung.hibernate.joincolumn; + +import com.baeldung.hibernate.HibernateUtil; +import java.io.IOException; +import org.hibernate.Session; +import org.hibernate.Transaction; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +public class JoinColumnIntegrationTest { + + private Session session; + private Transaction transaction; + + @Before + public void setUp() throws IOException { + session = HibernateUtil.getSessionFactory("hibernate-spatial.properties") + .openSession(); + transaction = session.beginTransaction(); + } + + @After + public void tearDown() { + transaction.rollback(); + session.close(); + } + + @Test + public void givenOfficeEntity_setAddress_shouldPersist() { + Office office = new Office(); + + Address address = new Address(); + address.setZipCode("11-111"); + office.setAddress(address); + + session.save(office); + session.flush(); + session.clear(); + } + + @Test + public void givenEmployeeEntity_setEmails_shouldPersist() { + Employee employee = new Employee(); + + Email email = new Email(); + email.setAddress("example@email.com"); + email.setEmployee(employee); + + session.save(employee); + session.flush(); + session.clear(); + } + +} \ No newline at end of file From 86b279ee2090c2c16ce473fe585309460931e486 Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Mon, 27 Aug 2018 08:43:12 +0200 Subject: [PATCH 06/14] add .java extension --- .../{JoinColumnIntegrationTest => JoinColumnIntegrationTest.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename hibernate5/src/test/java/com/baeldung/hibernate/joincolumn/{JoinColumnIntegrationTest => JoinColumnIntegrationTest.java} (100%) diff --git a/hibernate5/src/test/java/com/baeldung/hibernate/joincolumn/JoinColumnIntegrationTest b/hibernate5/src/test/java/com/baeldung/hibernate/joincolumn/JoinColumnIntegrationTest.java similarity index 100% rename from hibernate5/src/test/java/com/baeldung/hibernate/joincolumn/JoinColumnIntegrationTest rename to hibernate5/src/test/java/com/baeldung/hibernate/joincolumn/JoinColumnIntegrationTest.java From 3ff20c353e475f04fa681c94c4ede26a701c9d5d Mon Sep 17 00:00:00 2001 From: Dhawal Kapil Date: Mon, 27 Aug 2018 14:07:09 +0530 Subject: [PATCH 07/14] BAEL-8497 Update "File to InputStream" article - Added new tests for data input stream and sequence input stream in JavaXToInputStreamUnitTest - Added new test resource anothersample.txt --- .../java/io/JavaXToInputStreamUnitTest.java | 24 +++++++++++++++++++ .../src/test/resources/anothersample.txt | 1 + 2 files changed, 25 insertions(+) create mode 100644 core-java-io/src/test/resources/anothersample.txt diff --git a/core-java-io/src/test/java/org/baeldung/java/io/JavaXToInputStreamUnitTest.java b/core-java-io/src/test/java/org/baeldung/java/io/JavaXToInputStreamUnitTest.java index 08a4c673cd..6604d75ed1 100644 --- a/core-java-io/src/test/java/org/baeldung/java/io/JavaXToInputStreamUnitTest.java +++ b/core-java-io/src/test/java/org/baeldung/java/io/JavaXToInputStreamUnitTest.java @@ -1,10 +1,12 @@ package org.baeldung.java.io; import java.io.ByteArrayInputStream; +import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.io.SequenceInputStream; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; @@ -74,6 +76,28 @@ public class JavaXToInputStreamUnitTest { IOUtils.closeQuietly(targetStream); } + @Test + public final void givenUsingPlainJava_whenConvertingFileToDataInputStream_thenCorrect() throws IOException { + final File initialFile = new File("src/test/resources/sample.txt"); + final InputStream targetStream = new DataInputStream(new FileInputStream(initialFile)); + + IOUtils.closeQuietly(targetStream); + } + + @Test + public final void givenUsingPlainJava_whenConvertingFileToSequenceInputStream_thenCorrect() throws IOException { + final File initialFile = new File("src/test/resources/sample.txt"); + final File anotherFile = new File("src/test/resources/anothersample.txt"); + final InputStream targetStream = new FileInputStream(initialFile); + final InputStream anotherTargetStream = new FileInputStream(anotherFile); + + InputStream sequenceTargetStream = new SequenceInputStream(targetStream, anotherTargetStream); + + IOUtils.closeQuietly(targetStream); + IOUtils.closeQuietly(anotherTargetStream); + IOUtils.closeQuietly(sequenceTargetStream); + } + @Test public final void givenUsingGuava_whenConvertingFileToInputStream_thenCorrect() throws IOException { final File initialFile = new File("src/test/resources/sample.txt"); diff --git a/core-java-io/src/test/resources/anothersample.txt b/core-java-io/src/test/resources/anothersample.txt new file mode 100644 index 0000000000..f06f18ce02 --- /dev/null +++ b/core-java-io/src/test/resources/anothersample.txt @@ -0,0 +1 @@ +...Continues \ No newline at end of file From c209eecd5e0fdbcdecdd92f589478c5c787f40b6 Mon Sep 17 00:00:00 2001 From: josephine-barboza Date: Tue, 28 Aug 2018 01:49:59 +0530 Subject: [PATCH 08/14] BAEL-1666 Working with custom attributes in Thymeleaf (#5089) * BAEL-1666 Working with custom attributes in Thymeleaf * Delete listCourses.html --- .../CourseRegistrationController.java | 30 +++++++++++ .../com/baeldung/thymeleaf/model/Course.java | 52 +++++++++++++++++++ .../WEB-INF/views/courseRegistration.html | 42 +++++++++++++++ .../src/main/webapp/WEB-INF/views/home.html | 3 ++ 4 files changed, 127 insertions(+) create mode 100644 spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/CourseRegistrationController.java create mode 100644 spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/model/Course.java create mode 100644 spring-thymeleaf/src/main/webapp/WEB-INF/views/courseRegistration.html diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/CourseRegistrationController.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/CourseRegistrationController.java new file mode 100644 index 0000000000..2d2b5906e1 --- /dev/null +++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/CourseRegistrationController.java @@ -0,0 +1,30 @@ +package com.baeldung.thymeleaf.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import com.baeldung.thymeleaf.model.Course; + +/** + * Handles requests for the student model. + * + */ +@Controller +public class CourseRegistrationController { + + @RequestMapping(value = "/registerCourse", method = RequestMethod.POST) + public String register(@ModelAttribute Course course, Model model) { + model.addAttribute("successMessage", "You have successfully registered for course: " + course.getName() + "."); + return "courseRegistration.html"; + } + + @RequestMapping(value = "/registerCourse", method = RequestMethod.GET) + public String register(Model model) { + model.addAttribute("course", new Course()); + return "courseRegistration.html"; + } + +} diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/model/Course.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/model/Course.java new file mode 100644 index 0000000000..df2e9cd097 --- /dev/null +++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/model/Course.java @@ -0,0 +1,52 @@ +package com.baeldung.thymeleaf.model; + +import java.util.Date; + +public class Course { + + private String name; + private String description; + private Date startDate; + private Date endDate; + private String teacher; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Date getStartDate() { + return startDate; + } + + public void setStartDate(Date startDate) { + this.startDate = startDate; + } + + public Date getEndDate() { + return endDate; + } + + public void setEndDate(Date endDate) { + this.endDate = endDate; + } + + public String getTeacher() { + return teacher; + } + + public void setTeacher(String teacher) { + this.teacher = teacher; + } +} diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/courseRegistration.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/courseRegistration.html new file mode 100644 index 0000000000..cfce92d055 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/courseRegistration.html @@ -0,0 +1,42 @@ + + + +Register for course + + + + +

Register Here

+
+ + + + + + + + + +
+
+ + diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/home.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/home.html index b458f7270c..25ff6a19bb 100644 --- a/spring-thymeleaf/src/main/webapp/WEB-INF/views/home.html +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/home.html @@ -24,6 +24,9 @@ + + + From 8ac53f70c4ba2ef139e1acfcbcbf647950a2df25 Mon Sep 17 00:00:00 2001 From: Kacper Date: Mon, 27 Aug 2018 22:21:33 +0200 Subject: [PATCH 09/14] Optional or else throw (#5088) * Throw and throws in Java * BAEL-2122 | throw exception in optional * BAEL-2122 | optional orelsethrow * fix test --- .../PersonRepositoryUnitTest.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/throwsexception/PersonRepositoryUnitTest.java b/core-java/src/test/java/com/baeldung/throwsexception/PersonRepositoryUnitTest.java index 0d1859fb1a..3749ce10d0 100644 --- a/core-java/src/test/java/com/baeldung/throwsexception/PersonRepositoryUnitTest.java +++ b/core-java/src/test/java/com/baeldung/throwsexception/PersonRepositoryUnitTest.java @@ -13,21 +13,31 @@ public class PersonRepositoryUnitTest { PersonRepository personRepository = new PersonRepository(); @Test - public void whenIdIsNull_thenExceptionIsThrown() throws Exception { - assertThrows(Exception.class, + public void whenIdIsNull_thenExceptionIsThrown() { + assertThrows(IllegalArgumentException.class, () -> Optional .ofNullable(personRepository.findNameById(null)) - .orElseThrow(Exception::new)); + .orElseThrow(IllegalArgumentException::new)); } @Test - public void whenIdIsNonNull_thenNoExceptionIsThrown() throws Exception { + public void whenIdIsNonNull_thenNoExceptionIsThrown() { assertAll( () -> Optional .ofNullable(personRepository.findNameById("id")) - .orElseThrow(Exception::new)); + .orElseThrow(RuntimeException::new)); + } + + @Test + public void whenIdNonNull_thenReturnsNameUpperCase() { + String name = Optional + .ofNullable(personRepository.findNameById("id")) + .map(String::toUpperCase) + .orElseThrow(RuntimeException::new); + + assertEquals("NAME", name); } @Test From 64ba6515822e448058626d46daf978c16bf45a34 Mon Sep 17 00:00:00 2001 From: Alessio Stalla Date: Mon, 27 Aug 2018 22:32:15 +0200 Subject: [PATCH 10/14] #BAEL-693 tests and code examples. (#5091) --- core-kotlin/pom.xml | 176 +++++---- .../baeldung/kotlin/exposed/ExposedTest.kt | 333 ++++++++++++++++++ 2 files changed, 431 insertions(+), 78 deletions(-) create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/kotlin/exposed/ExposedTest.kt diff --git a/core-kotlin/pom.xml b/core-kotlin/pom.xml index 1c5818b6aa..6fdc7c7c1a 100644 --- a/core-kotlin/pom.xml +++ b/core-kotlin/pom.xml @@ -1,85 +1,105 @@ - 4.0.0 - core-kotlin - jar + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + core-kotlin + jar - - com.baeldung - parent-kotlin - 1.0.0-SNAPSHOT - ../parent-kotlin - + + com.baeldung + parent-kotlin + 1.0.0-SNAPSHOT + ../parent-kotlin + - - - org.jetbrains.spek - spek-api - 1.1.5 - test - - - org.jetbrains.spek - spek-subject-extension - 1.1.5 - test - - - org.jetbrains.spek - spek-junit-platform-engine - 1.1.5 - test - - - org.apache.commons - commons-math3 - ${commons-math3.version} - - - org.junit.platform - junit-platform-runner - ${junit.platform.version} - test - - - khttp - khttp - ${khttp.version} - - - com.nhaarman - mockito-kotlin - ${mockito-kotlin.version} - test - - - com.github.salomonbrys.kodein - kodein - ${kodein.version} - - - org.assertj - assertj-core - ${assertj.version} - test - - - com.beust - klaxon - ${klaxon.version} - - + + + exposed + exposed + https://dl.bintray.com/kotlin/exposed + + - - 1.5.0 - 4.1.0 - 3.0.4 - 0.1.0 - 3.6.1 - 1.1.1 - 5.2.0 - 3.10.0 - + + + org.jetbrains.spek + spek-api + 1.1.5 + test + + + org.jetbrains.spek + spek-subject-extension + 1.1.5 + test + + + org.jetbrains.spek + spek-junit-platform-engine + 1.1.5 + test + + + org.apache.commons + commons-math3 + ${commons-math3.version} + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + khttp + khttp + ${khttp.version} + + + com.nhaarman + mockito-kotlin + ${mockito-kotlin.version} + test + + + com.github.salomonbrys.kodein + kodein + ${kodein.version} + + + org.assertj + assertj-core + ${assertj.version} + test + + + com.beust + klaxon + ${klaxon.version} + + + org.jetbrains.exposed + exposed + ${exposed.version} + + + com.h2database + h2 + ${h2database.version} + + + + + 1.5.0 + 4.1.0 + 3.0.4 + 0.1.0 + 3.6.1 + 1.1.1 + 5.2.0 + 3.10.0 + 1.4.197 + 0.10.4 + \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/exposed/ExposedTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/exposed/ExposedTest.kt new file mode 100644 index 0000000000..29fa18ef7a --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/exposed/ExposedTest.kt @@ -0,0 +1,333 @@ +package com.baeldung.kotlin.exposed + +import org.jetbrains.exposed.dao.* +import org.jetbrains.exposed.sql.* +import org.jetbrains.exposed.sql.transactions.TransactionManager +import org.jetbrains.exposed.sql.transactions.transaction +import org.junit.Test +import java.sql.DriverManager +import kotlin.test.* + +class ExposedTest { + + @Test + fun whenH2Database_thenConnectionSuccessful() { + val database = Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver") + transaction { + assertEquals(1.4.toBigDecimal(), database.version) + assertEquals("h2", database.vendor) + } + } + + @Test + fun whenH2DatabaseWithCredentials_thenConnectionSuccessful() { + Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver", user = "myself", password = "secret") + } + + @Test + fun whenH2DatabaseWithManualConnection_thenConnectionSuccessful() { + var connected = false + Database.connect({ connected = true; DriverManager.getConnection("jdbc:h2:mem:test;MODE=MySQL") }) + assertEquals(false, connected) + transaction { + addLogger(StdOutSqlLogger) + assertEquals(false, connected) + SchemaUtils.create(Cities) + assertEquals(true, connected) + } + } + + @Test + fun whenManualCommit_thenOk() { + Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver") + transaction { + assertTrue(this is Transaction) + commit() + commit() + commit() + } + } + + @Test + fun whenInsert_thenGeneratedKeys() { + Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver") + transaction { + SchemaUtils.create(StarWarsFilms) + val id = StarWarsFilms.insertAndGetId { + it[name] = "The Last Jedi" + it[sequelId] = 8 + it[director] = "Rian Johnson" + } + assertEquals(1, id.value) + val insert = StarWarsFilms.insert { + it[name] = "The Force Awakens" + it[sequelId] = 7 + it[director] = "J.J. Abrams" + } + assertEquals(2, insert[StarWarsFilms.id]?.value) + val selectAll = StarWarsFilms.selectAll() + selectAll.forEach { + assertTrue { it[StarWarsFilms.sequelId] >= 7 } + } + StarWarsFilms.slice(StarWarsFilms.name, StarWarsFilms.director).selectAll() + .forEach { + assertTrue { it[StarWarsFilms.name].startsWith("The") } + } + val select = StarWarsFilms.select { (StarWarsFilms.director like "J.J.%") and (StarWarsFilms.sequelId eq 7) } + assertEquals(1, select.count()) + StarWarsFilms.update ({ StarWarsFilms.sequelId eq 8 }) { + it[name] = "Episode VIII – The Last Jedi" + with(SqlExpressionBuilder) { + it.update(StarWarsFilms.sequelId, StarWarsFilms.sequelId + 1) + } + } + } + } + + @Test + fun whenForeignKey_thenAutoJoin() { + Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver") + transaction { + addLogger(StdOutSqlLogger) + SchemaUtils.create(StarWarsFilms, Players) + StarWarsFilms.insert { + it[name] = "The Last Jedi" + it[sequelId] = 8 + it[director] = "Rian Johnson" + } + StarWarsFilms.insert { + it[name] = "The Force Awakens" + it[sequelId] = 7 + it[director] = "J.J. Abrams" + } + Players.insert { + it[name] = "Mark Hamill" + it[sequelId] = 7 + } + Players.insert { + it[name] = "Mark Hamill" + it[sequelId] = 8 + } + val simpleInnerJoin = (StarWarsFilms innerJoin Players).selectAll() + assertEquals(2, simpleInnerJoin.count()) + simpleInnerJoin.forEach { + assertNotNull(it[StarWarsFilms.name]) + assertEquals(it[StarWarsFilms.sequelId], it[Players.sequelId]) + assertEquals("Mark Hamill", it[Players.name]) + } + val innerJoinWithCondition = (StarWarsFilms innerJoin Players) + .select { StarWarsFilms.sequelId eq Players.sequelId } + assertEquals(2, innerJoinWithCondition.count()) + innerJoinWithCondition.forEach { + assertNotNull(it[StarWarsFilms.name]) + assertEquals(it[StarWarsFilms.sequelId], it[Players.sequelId]) + assertEquals("Mark Hamill", it[Players.name]) + } + val complexInnerJoin = Join(StarWarsFilms, Players, joinType = JoinType.INNER, onColumn = StarWarsFilms.sequelId, otherColumn = Players.sequelId, additionalConstraint = { + StarWarsFilms.sequelId eq 8 + }).selectAll() + assertEquals(1, complexInnerJoin.count()) + complexInnerJoin.forEach { + assertNotNull(it[StarWarsFilms.name]) + assertEquals(it[StarWarsFilms.sequelId], it[Players.sequelId]) + assertEquals("Mark Hamill", it[Players.name]) + } + + } + } + + @Test + fun whenJoinWithAlias_thenFun() { + Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver") + transaction { + addLogger(StdOutSqlLogger) + SchemaUtils.create(StarWarsFilms, Players) + StarWarsFilms.insert { + it[name] = "The Last Jedi" + it[sequelId] = 8 + it[director] = "Rian Johnson" + } + StarWarsFilms.insert { + it[name] = "The Force Awakens" + it[sequelId] = 7 + it[director] = "J.J. Abrams" + } + val sequel = StarWarsFilms.alias("sequel") + Join(StarWarsFilms, sequel, + additionalConstraint = { sequel[StarWarsFilms.sequelId] eq StarWarsFilms.sequelId + 1 }) + .selectAll().forEach { + assertEquals(it[sequel[StarWarsFilms.sequelId]], it[StarWarsFilms.sequelId] + 1) + } + } + } + + @Test + fun whenEntity_thenDAO() { + val database = Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver") + val connection = database.connector.invoke() //Keep a connection open so the DB is not destroyed after the first transaction + val inserted = transaction { + addLogger(StdOutSqlLogger) + SchemaUtils.create(StarWarsFilms, Players) + val theLastJedi = StarWarsFilm.new { + name = "The Last Jedi" + sequelId = 8 + director = "Rian Johnson" + } + assertFalse(TransactionManager.current().entityCache.inserts.isEmpty()) + assertEquals(1, theLastJedi.id.value) //Reading this causes a flush + assertTrue(TransactionManager.current().entityCache.inserts.isEmpty()) + theLastJedi + } + transaction { + val theLastJedi = StarWarsFilm.findById(1) + assertNotNull(theLastJedi) + assertEquals(inserted.id, theLastJedi?.id) + } + connection.close() + } + + @Test + fun whenManyToOne_thenNavigation() { + val database = Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver") + val connection = database.connector.invoke() + transaction { + addLogger(StdOutSqlLogger) + SchemaUtils.create(StarWarsFilms, Players, Users, UserRatings) + val theLastJedi = StarWarsFilm.new { + name = "The Last Jedi" + sequelId = 8 + director = "Rian Johnson" + } + val someUser = User.new { + name = "Some User" + } + val rating = UserRating.new { + value = 9 + user = someUser + film = theLastJedi + } + assertEquals(theLastJedi, rating.film) + assertEquals(someUser, rating.user) + assertEquals(rating, theLastJedi.ratings.first()) + } + transaction { + val theLastJedi = StarWarsFilm.find { StarWarsFilms.sequelId eq 8 }.first() + val ratings = UserRating.find { UserRatings.film eq theLastJedi.id } + assertEquals(1, ratings.count()) + val rating = ratings.first() + assertEquals("Some User", rating.user.name) + assertEquals(rating, theLastJedi.ratings.first()) + UserRating.new { + value = 8 + user = rating.user + film = theLastJedi + } + assertEquals(2, theLastJedi.ratings.count()) + } + connection.close() + } + + @Test + fun whenManyToMany_thenAssociation() { + val database = Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver") + val connection = database.connector.invoke() + val film = transaction { + SchemaUtils.create(StarWarsFilms) + StarWarsFilm.new { + name = "The Last Jedi" + sequelId = 8 + director = "Rian Johnson" + } + } + + val actor = transaction { + SchemaUtils.create(Actors) + Actor.new { + firstname = "Daisy" + lastname = "Ridley" + } + } + + transaction { + SchemaUtils.create(StarWarsFilmActors) + film.actors = SizedCollection(listOf(actor)) + } + connection.close() + } + +} + +object Cities: IntIdTable() { + val name = varchar("name", 50) +} + +object StarWarsFilms_Simple : Table() { + val id = integer("id").autoIncrement().primaryKey() + val sequelId = integer("sequel_id").uniqueIndex() + val name = varchar("name", 50) + val director = varchar("director", 50) +} + +object StarWarsFilms : IntIdTable() { + val sequelId = integer("sequel_id").uniqueIndex() + val name = varchar("name", 50) + val director = varchar("director", 50) +} + +object Players : Table() { + //val sequelId = integer("sequel_id").uniqueIndex().references(StarWarsFilms.sequelId) + val sequelId = reference("sequel_id", StarWarsFilms.sequelId).uniqueIndex() + //val filmId = reference("film_id", StarWarsFilms).nullable() + val name = varchar("name", 50) +} + +class StarWarsFilm(id: EntityID) : Entity(id) { + companion object : EntityClass(StarWarsFilms) + + var sequelId by StarWarsFilms.sequelId + var name by StarWarsFilms.name + var director by StarWarsFilms.director + var actors by Actor via StarWarsFilmActors + val ratings by UserRating referrersOn UserRatings.film +} + +object Users: IntIdTable() { + val name = varchar("name", 50) +} + +object UserRatings: IntIdTable() { + val value = long("value") + val film = reference("film", StarWarsFilms) + val user = reference("user", Users) +} + +class User(id: EntityID): IntEntity(id) { + companion object : IntEntityClass(Users) + + var name by Users.name +} + +class UserRating(id: EntityID): IntEntity(id) { + companion object : IntEntityClass(UserRatings) + + var value by UserRatings.value + var film by StarWarsFilm referencedOn UserRatings.film + var user by User referencedOn UserRatings.user +} + +object Actors: IntIdTable() { + val firstname = varchar("firstname", 50) + val lastname = varchar("lastname", 50) +} + +class Actor(id: EntityID): IntEntity(id) { + companion object : IntEntityClass(Actors) + + var firstname by Actors.firstname + var lastname by Actors.lastname +} + +object StarWarsFilmActors : Table() { + val starWarsFilm = reference("starWarsFilm", StarWarsFilms).primaryKey(0) + val actor = reference("actor", Actors).primaryKey(1) +} \ No newline at end of file From 1256735a42c43768fec7dc4f6508c379d5491c70 Mon Sep 17 00:00:00 2001 From: Sam Millington Date: Tue, 28 Aug 2018 00:28:44 +0100 Subject: [PATCH 11/14] BAEL-2033: Added stream-ordering files (#5080) * Added stream-ordering files * Removed old files --- .../streamordering/BenchmarkUnitTest.java | 6 ++-- .../StreamsOrderingUnitTest.java | 7 ++-- testing-modules/streams-ordering/pom.xml | 34 ------------------- 3 files changed, 7 insertions(+), 40 deletions(-) rename testing-modules/streams-ordering/src/test/java/benchmarking/TestBenchmark.java => core-java-8/src/test/java/com/baeldung/streamordering/BenchmarkUnitTest.java (97%) rename testing-modules/streams-ordering/src/test/java/StreamsOrderingTest.java => core-java-8/src/test/java/com/baeldung/streamordering/StreamsOrderingUnitTest.java (96%) delete mode 100644 testing-modules/streams-ordering/pom.xml diff --git a/testing-modules/streams-ordering/src/test/java/benchmarking/TestBenchmark.java b/core-java-8/src/test/java/com/baeldung/streamordering/BenchmarkUnitTest.java similarity index 97% rename from testing-modules/streams-ordering/src/test/java/benchmarking/TestBenchmark.java rename to core-java-8/src/test/java/com/baeldung/streamordering/BenchmarkUnitTest.java index a3e99e6465..9ae0a16514 100644 --- a/testing-modules/streams-ordering/src/test/java/benchmarking/TestBenchmark.java +++ b/core-java-8/src/test/java/com/baeldung/streamordering/BenchmarkUnitTest.java @@ -1,4 +1,4 @@ -package benchmarking; +package com.baeldung.streamordering; import org.junit.Test; import org.openjdk.jmh.annotations.*; @@ -15,7 +15,7 @@ import java.util.concurrent.TimeUnit; import java.util.stream.IntStream; -public class TestBenchmark +public class BenchmarkUnitTest { @Test @@ -94,4 +94,4 @@ public class TestBenchmark for (int i = 0; i < 1000; i++) bh.consume (list.get (i)); } -} \ No newline at end of file +} diff --git a/testing-modules/streams-ordering/src/test/java/StreamsOrderingTest.java b/core-java-8/src/test/java/com/baeldung/streamordering/StreamsOrderingUnitTest.java similarity index 96% rename from testing-modules/streams-ordering/src/test/java/StreamsOrderingTest.java rename to core-java-8/src/test/java/com/baeldung/streamordering/StreamsOrderingUnitTest.java index b657911780..43a233d353 100644 --- a/testing-modules/streams-ordering/src/test/java/StreamsOrderingTest.java +++ b/core-java-8/src/test/java/com/baeldung/streamordering/StreamsOrderingUnitTest.java @@ -1,3 +1,5 @@ +package com.baeldung.streamordering; + import org.junit.Before; import org.junit.Test; @@ -10,10 +12,9 @@ import java.util.stream.IntStream; import static org.junit.Assert.assertEquals; +public class StreamsOrderingUnitTest { -public class StreamsOrderingTest { - - Logger logger = Logger.getLogger( StreamsOrderingTest.class.getName()); + Logger logger = Logger.getLogger( StreamsOrderingUnitTest.class.getName()); @Before public void setUp() throws Exception { diff --git a/testing-modules/streams-ordering/pom.xml b/testing-modules/streams-ordering/pom.xml deleted file mode 100644 index 99d08e19be..0000000000 --- a/testing-modules/streams-ordering/pom.xml +++ /dev/null @@ -1,34 +0,0 @@ - - - 4.0.0 - - com.baeldung - StreamsOrdering - 1.0-SNAPSHOT - - - - junit - junit - 4.12 - test - - - - - org.openjdk.jmh - jmh-core - 1.21 - - - - org.openjdk.jmh - jmh-generator-annprocess - 1.21 - provided - - - - \ No newline at end of file From 991335da4227e4eaca4409ac4e77e0277d837f68 Mon Sep 17 00:00:00 2001 From: the-java-guy <39062630+the-java-guy@users.noreply.github.com> Date: Tue, 28 Aug 2018 18:53:01 +0530 Subject: [PATCH 12/14] BAEL-1782 (#5067) * Bean Object, server side and client side example for event streaming example * BAEL-1628 Access a File from the Classpath in a Spring Application * inputstream retrieval added * Removed files related to evaluation article * + Aligning code to the article. Removed Utility methods and classes * Precommit fix * PMD fixes * Code Review changes Refactored : whenResourceUtils_thenReadSuccessful * BAEL-1934 * +indentation correction in pom.xml * synced with master * Precommit : rebase * BAEL-1782 --- .../event/listener/ContextEventListener.java | 24 +++++++++++++++++++ .../baeldung/event/listener/EventConfig.java | 10 ++++++++ .../baeldung/event/listener/SpringRunner.java | 12 ++++++++++ 3 files changed, 46 insertions(+) create mode 100644 spring-core/src/main/java/com/baeldung/event/listener/ContextEventListener.java create mode 100644 spring-core/src/main/java/com/baeldung/event/listener/EventConfig.java create mode 100644 spring-core/src/main/java/com/baeldung/event/listener/SpringRunner.java diff --git a/spring-core/src/main/java/com/baeldung/event/listener/ContextEventListener.java b/spring-core/src/main/java/com/baeldung/event/listener/ContextEventListener.java new file mode 100644 index 0000000000..a2603bb95c --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/event/listener/ContextEventListener.java @@ -0,0 +1,24 @@ +package com.baeldung.event.listener; + +import org.springframework.context.event.ContextStartedEvent; +import org.springframework.context.event.ContextStoppedEvent; +import org.springframework.context.event.EventListener; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +@Component +public class ContextEventListener { + + @Order(2) + @EventListener + public void handleContextRefreshEvent(ContextStartedEvent ctxStartEvt) { + System.out.println("Context Start Event received."); + } + + @Order(1) + @EventListener(classes = { ContextStartedEvent.class, ContextStoppedEvent.class }) + public void handleMultipleEvents() { + System.out.println("Multi-event listener invoked"); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/event/listener/EventConfig.java b/spring-core/src/main/java/com/baeldung/event/listener/EventConfig.java new file mode 100644 index 0000000000..f2a3af7640 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/event/listener/EventConfig.java @@ -0,0 +1,10 @@ +package com.baeldung.event.listener; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan(basePackages = "com.baeldung.event.listener") +public class EventConfig { + +} diff --git a/spring-core/src/main/java/com/baeldung/event/listener/SpringRunner.java b/spring-core/src/main/java/com/baeldung/event/listener/SpringRunner.java new file mode 100644 index 0000000000..bbe4693900 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/event/listener/SpringRunner.java @@ -0,0 +1,12 @@ +package com.baeldung.event.listener; + +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public class SpringRunner { + + public static void main(String[] args) { + ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(EventConfig.class); + ctx.start(); + } +} From 8a65afeb5d39a95e0b31f90cb4530a36c305f7d3 Mon Sep 17 00:00:00 2001 From: Sam Millington Date: Tue, 28 Aug 2018 16:57:52 +0100 Subject: [PATCH 13/14] BAEL:2033 - Stream-ordering files moved to the Java-Streams module (#5097) * Added stream-ordering files * Removed old files * Rename core-java-8/src/test/java/com/baeldung/streamordering/BenchmarkUnitTest.java to java-streams/src/test/java/com/baeldung/streamordering/BenchmarkUnitTest.java * Rename core-java-8/src/test/java/com/baeldung/streamordering/StreamsOrderingUnitTest.java to java-streams/src/test/com/baeldung/streamordering/StreamsOrderingUnitTest.java * Rename java-streams/src/test/com/baeldung/streamordering/StreamsOrderingUnitTest.java to java-streams/src/test/java/com/baeldung/streamordering/StreamsOrderingUnitTest.java * Updated Pom with JMH dependencies --- java-streams/pom.xml | 13 ++ .../streamordering/BenchmarkUnitTest.java | 97 ++++++++++++ .../StreamsOrderingUnitTest.java | 149 ++++++++++++++++++ 3 files changed, 259 insertions(+) create mode 100644 java-streams/src/test/java/com/baeldung/streamordering/BenchmarkUnitTest.java create mode 100644 java-streams/src/test/java/com/baeldung/streamordering/StreamsOrderingUnitTest.java diff --git a/java-streams/pom.xml b/java-streams/pom.xml index 351c33ecc0..75914f0247 100644 --- a/java-streams/pom.xml +++ b/java-streams/pom.xml @@ -15,6 +15,18 @@ + + + org.openjdk.jmh + jmh-core + ${jmh.version} + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh.version} + provided + org.apache.commons commons-lang3 @@ -107,6 +119,7 @@ + 1.21 3.5 1.16.12 0.9.0 diff --git a/java-streams/src/test/java/com/baeldung/streamordering/BenchmarkUnitTest.java b/java-streams/src/test/java/com/baeldung/streamordering/BenchmarkUnitTest.java new file mode 100644 index 0000000000..9ae0a16514 --- /dev/null +++ b/java-streams/src/test/java/com/baeldung/streamordering/BenchmarkUnitTest.java @@ -0,0 +1,97 @@ +package com.baeldung.streamordering; + +import org.junit.Test; +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.infra.Blackhole; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; +import org.openjdk.jmh.runner.options.TimeValue; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.concurrent.TimeUnit; +import java.util.stream.IntStream; + + +public class BenchmarkUnitTest +{ + + @Test + public void + launchBenchmark() throws Exception { + + Options opt = new OptionsBuilder() + // Specify which benchmarks to run. + // You can be more specific if you'd like to run only one benchmark per test. + .include(this.getClass().getName() + ".*") + // Set the following options as needed + .mode (Mode.AverageTime) + .timeUnit(TimeUnit.MICROSECONDS) + .warmupTime(TimeValue.seconds(1)) + .warmupIterations(2) + .measurementTime(TimeValue.seconds(1)) + .measurementIterations(2) + .threads(2) + .forks(1) + .shouldFailOnError(true) + .shouldDoGC(true) + //.jvmArgs("-XX:+UnlockDiagnosticVMOptions", "-XX:+PrintInlining") + //.addProfiler(WinPerfAsmProfiler.class) + .build(); + + new Runner(opt).run(); + + + } + + @Benchmark + public void givenOrderedStreamInput_whenStreamFiltered_showOpsPerMS(){ + IntStream.range(1, 100_000_000).parallel().filter(i -> i % 10 == 0).toArray(); + } + + @Benchmark + public void givenUnorderedStreamInput_whenStreamFiltered_showOpsPerMS(){ + IntStream.range(1,100_000_000).unordered().parallel().filter(i -> i % 10 == 0).toArray(); + } + + @Benchmark + public void givenUnorderedStreamInput_whenStreamDistinct_showOpsPerMS(){ + IntStream.range(1, 1_000_000).unordered().parallel().distinct().toArray(); + } + + @Benchmark + public void givenOrderedStreamInput_whenStreamDistinct_showOpsPerMS() { + //section 5.1. + IntStream.range(1, 1_000_000).parallel().distinct().toArray(); + } + + + // The JMH samples are the best documentation for how to use it + // http://hg.openjdk.java.net/code-tools/jmh/file/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/ + @State(Scope.Thread) + public static class BenchmarkState + { + List list; + + @Setup(Level.Trial) public void + initialize() { + + Random rand = new Random(); + + list = new ArrayList<>(); + for (int i = 0; i < 1000; i++) + list.add (rand.nextInt()); + } + } + + @Benchmark public void + benchmark1 (BenchmarkState state, Blackhole bh) { + + List list = state.list; + + for (int i = 0; i < 1000; i++) + bh.consume (list.get (i)); + } +} diff --git a/java-streams/src/test/java/com/baeldung/streamordering/StreamsOrderingUnitTest.java b/java-streams/src/test/java/com/baeldung/streamordering/StreamsOrderingUnitTest.java new file mode 100644 index 0000000000..43a233d353 --- /dev/null +++ b/java-streams/src/test/java/com/baeldung/streamordering/StreamsOrderingUnitTest.java @@ -0,0 +1,149 @@ +package com.baeldung.streamordering; + +import org.junit.Before; +import org.junit.Test; + +import java.util.*; +import java.util.function.Function; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static org.junit.Assert.assertEquals; + +public class StreamsOrderingUnitTest { + + Logger logger = Logger.getLogger( StreamsOrderingUnitTest.class.getName()); + + @Before + public void setUp() throws Exception { + logger.setLevel(Level.ALL); + } + + @Test + public void givenTwoCollections_whenStreamed_thenCheckOutputDifferent(){ + + List list = Arrays.asList("B", "A", "C", "D", "F"); + Set set = new TreeSet<>(Arrays.asList("B", "A", "C", "D", "F")); + + Object[] listOutput = list.stream().toArray(); + Object[] setOutput = set.stream().toArray(); + + assertEquals("[B, A, C, D, F]", Arrays.toString(listOutput)); + assertEquals("[A, B, C, D, F]", Arrays.toString(setOutput)); + + } + + @Test + public void givenTwoCollections_whenStreamedInParallel_thenCheckOutputDifferent(){ + + List list = Arrays.asList("B", "A", "C", "D", "F"); + Set set = new TreeSet<>(Arrays.asList("B", "A", "C", "D", "F")); + + Object[] listOutput = list.stream().parallel().toArray(); + Object[] setOutput = set.stream().parallel().toArray(); + + assertEquals("[B, A, C, D, F]", Arrays.toString(listOutput)); + assertEquals("[A, B, C, D, F]", Arrays.toString(setOutput)); + + } + + + + @Test + public void givenOrderedInput_whenUnorderedAndOrderedCompared_thenCheckUnorderedOutputChanges(){ + Set set = new TreeSet<>( + Arrays.asList(-9, -5, -4, -2, 1, 2, 4, 5, 7, 9, 12, 13, 16, 29, 23, 34, 57, 68, 90, 102, 230)); + + Object[] orderedArray = set.stream() + .parallel() + .limit(5) + .toArray(); + Object[] unorderedArray = set.stream() + .unordered() + .parallel() + .limit(5) + .toArray(); + + logger.info(Arrays.toString(orderedArray)); + logger.info(Arrays.toString(unorderedArray)); + } + + + @Test + public void givenUnsortedStreamInput_whenStreamSorted_thenCheckOrderChanged(){ + + List list = Arrays.asList(-3,10,-4,1,3); + + Object[] listOutput = list.stream().toArray(); + Object[] listOutputSorted = list.stream().sorted().toArray(); + + assertEquals("[-3, 10, -4, 1, 3]", Arrays.toString(listOutput)); + assertEquals("[-4, -3, 1, 3, 10]", Arrays.toString(listOutputSorted)); + + } + + @Test + public void givenUnsortedStreamInput_whenStreamDistinct_thenShowTimeTaken(){ + long start, end; + start = System.currentTimeMillis(); + IntStream.range(1,1_000_000).unordered().parallel().distinct().toArray(); + end = System.currentTimeMillis(); + System.out.println(String.format("Time taken when unordered: %d ms", (end - start))); + } + + + @Test + public void givenSameCollection_whenStreamTerminated_thenCheckEachVsEachOrdered(){ + + List list = Arrays.asList("B", "A", "C", "D", "F"); + + list.stream().parallel().forEach(e -> logger.log(Level.INFO, e)); + list.stream().parallel().forEachOrdered(e -> logger.log(Level.INFO, e)); + + } + + @Test + public void givenSameCollection_whenStreamCollected_thenCheckOutput(){ + + List list = Arrays.asList("B", "A", "C", "D", "F"); + + List collectionList = list.stream().parallel().collect(Collectors.toList()); + Set collectionSet = list.stream().parallel().collect(Collectors.toCollection(TreeSet::new)); + + assertEquals("[B, A, C, D, F]", collectionList.toString()); + assertEquals("[A, B, C, D, F]", collectionSet.toString()); + + } + + + @Test + public void givenListIterationOrder_whenStreamCollectedToMap_thenCeckOrderChanged() { + List list = Arrays.asList("A", "BB", "CCC"); + + Map hashMap = list.stream().collect(Collectors.toMap(Function.identity(), String::length)); + + Object[] keySet = hashMap.keySet().toArray(); + + assertEquals("[BB, A, CCC]", Arrays.toString(keySet)); + + } + + @Test + public void givenListIteration_whenStreamCollectedtoHashMap_thenCheckOrderMaintained() { + List list = Arrays.asList("A", "BB", "CCC", "CCC"); + + Map linkedHashMap = list.stream().collect(Collectors.toMap( + Function.identity(), + String::length, + (u, v) -> u, + LinkedHashMap::new + )); + + Object[] keySet = linkedHashMap.keySet().toArray(); + + assertEquals("[A, BB, CCC]", Arrays.toString(keySet)); + } + +} From 1c455d1c1cbd6b19e67efd06992a1a246a4334a0 Mon Sep 17 00:00:00 2001 From: Eugen Date: Wed, 29 Aug 2018 00:44:58 +0300 Subject: [PATCH 14/14] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d018783465..d20968b455 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ The "REST with Spring" Classes ============================== -After 5 months of work, here's the Master Class of REST With Spring:
+Here's the Master Class of REST With Spring (price changes permanently next Friday):
**[>> THE REST WITH SPRING MASTER CLASS](http://www.baeldung.com/rest-with-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=rws#master-class)** And here's the Master Class of Learn Spring Security: