From e3a84dae5b623ed2245d35ccaf5073e5582113fa Mon Sep 17 00:00:00 2001 From: Dmytro Korniienko Date: Sun, 9 Sep 2018 13:13:17 +0300 Subject: [PATCH 1/6] BAEL-2070 Qualifier used instead of Primary to distinguish between different repository beans --- .../baeldung/dependency/exception/app/PurchaseDeptService.java | 3 ++- .../dependency/exception/repository/DressRepository.java | 3 ++- .../dependency/exception/repository/ShoeRepository.java | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/spring-core/src/main/java/com/baeldung/dependency/exception/app/PurchaseDeptService.java b/spring-core/src/main/java/com/baeldung/dependency/exception/app/PurchaseDeptService.java index 1e6fad63aa..e0fe01acdd 100644 --- a/spring-core/src/main/java/com/baeldung/dependency/exception/app/PurchaseDeptService.java +++ b/spring-core/src/main/java/com/baeldung/dependency/exception/app/PurchaseDeptService.java @@ -1,13 +1,14 @@ package com.baeldung.dependency.exception.app; import com.baeldung.dependency.exception.repository.InventoryRepository; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @Service public class PurchaseDeptService { private InventoryRepository repository; - public PurchaseDeptService(InventoryRepository repository) { + public PurchaseDeptService(@Qualifier("dresses") InventoryRepository repository) { this.repository = repository; } } \ No newline at end of file diff --git a/spring-core/src/main/java/com/baeldung/dependency/exception/repository/DressRepository.java b/spring-core/src/main/java/com/baeldung/dependency/exception/repository/DressRepository.java index 4a6c836143..5a1371ce04 100644 --- a/spring-core/src/main/java/com/baeldung/dependency/exception/repository/DressRepository.java +++ b/spring-core/src/main/java/com/baeldung/dependency/exception/repository/DressRepository.java @@ -1,9 +1,10 @@ package com.baeldung.dependency.exception.repository; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Repository; -@Primary +@Qualifier("dresses") @Repository public class DressRepository implements InventoryRepository { } diff --git a/spring-core/src/main/java/com/baeldung/dependency/exception/repository/ShoeRepository.java b/spring-core/src/main/java/com/baeldung/dependency/exception/repository/ShoeRepository.java index 60495914cd..227d8934b6 100644 --- a/spring-core/src/main/java/com/baeldung/dependency/exception/repository/ShoeRepository.java +++ b/spring-core/src/main/java/com/baeldung/dependency/exception/repository/ShoeRepository.java @@ -1,7 +1,9 @@ package com.baeldung.dependency.exception.repository; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Repository; +@Qualifier("shoes") @Repository public class ShoeRepository implements InventoryRepository { } From 9664247f4f426bea83b625a510b76ffe9068e08c Mon Sep 17 00:00:00 2001 From: Dmytro Korniienko Date: Mon, 10 Sep 2018 21:29:38 +0300 Subject: [PATCH 2/6] BAEL-2095 CrudRepository save() method --- .../baeldung/config/CustomConfiguration.java | 29 ++++++++++++++++ .../dao/repositories/InventoryRepository.java | 7 ++++ .../baeldung/domain/MerchandiseEntity.java | 34 +++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 spring-data-jpa/src/main/java/com/baeldung/config/CustomConfiguration.java create mode 100644 spring-data-jpa/src/main/java/com/baeldung/dao/repositories/InventoryRepository.java create mode 100644 spring-data-jpa/src/main/java/com/baeldung/domain/MerchandiseEntity.java diff --git a/spring-data-jpa/src/main/java/com/baeldung/config/CustomConfiguration.java b/spring-data-jpa/src/main/java/com/baeldung/config/CustomConfiguration.java new file mode 100644 index 0000000000..5af5351d76 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/config/CustomConfiguration.java @@ -0,0 +1,29 @@ +package com.baeldung.config; + +import com.baeldung.dao.repositories.InventoryRepository; +import com.baeldung.domain.MerchandiseEntity; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; + +@SpringBootApplication +@EnableJpaRepositories(basePackageClasses = InventoryRepository.class) +@EntityScan(basePackageClasses = MerchandiseEntity.class) +public class CustomConfiguration { + public static void main(String[] args) { + ConfigurableApplicationContext context = SpringApplication.run(CustomConfiguration.class, args); + + InventoryRepository repo = context.getBean(InventoryRepository.class); + + MerchandiseEntity pants = new MerchandiseEntity("Pair of Pants"); + repo.save(pants); + + MerchandiseEntity shorts = new MerchandiseEntity("Pair of Shorts"); + repo.save(shorts); + + pants.setTitle("Branded Luxury Pants"); + repo.save(pants); + } +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/InventoryRepository.java b/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/InventoryRepository.java new file mode 100644 index 0000000000..a575f0b915 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/InventoryRepository.java @@ -0,0 +1,7 @@ +package com.baeldung.dao.repositories; + +import com.baeldung.domain.MerchandiseEntity; +import org.springframework.data.repository.CrudRepository; + +public interface InventoryRepository extends CrudRepository { +} diff --git a/spring-data-jpa/src/main/java/com/baeldung/domain/MerchandiseEntity.java b/spring-data-jpa/src/main/java/com/baeldung/domain/MerchandiseEntity.java new file mode 100644 index 0000000000..921edf1b85 --- /dev/null +++ b/spring-data-jpa/src/main/java/com/baeldung/domain/MerchandiseEntity.java @@ -0,0 +1,34 @@ +package com.baeldung.domain; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class MerchandiseEntity { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + private String title; + + public MerchandiseEntity() { + } + + public MerchandiseEntity(String title) { + this.title = title; + } + + public Long getId() { + return id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } +} From 2e60aa32a251a43e3153c5aeb9609bde16995e60 Mon Sep 17 00:00:00 2001 From: Dmytro Korniienko Date: Tue, 11 Sep 2018 00:13:41 +0300 Subject: [PATCH 3/6] (REVERT) BAEL-2095 CrudRepository save() method --- .../baeldung/config/CustomConfiguration.java | 29 ---------------- .../dao/repositories/InventoryRepository.java | 7 ---- .../baeldung/domain/MerchandiseEntity.java | 34 ------------------- 3 files changed, 70 deletions(-) delete mode 100644 spring-data-jpa/src/main/java/com/baeldung/config/CustomConfiguration.java delete mode 100644 spring-data-jpa/src/main/java/com/baeldung/dao/repositories/InventoryRepository.java delete mode 100644 spring-data-jpa/src/main/java/com/baeldung/domain/MerchandiseEntity.java diff --git a/spring-data-jpa/src/main/java/com/baeldung/config/CustomConfiguration.java b/spring-data-jpa/src/main/java/com/baeldung/config/CustomConfiguration.java deleted file mode 100644 index 5af5351d76..0000000000 --- a/spring-data-jpa/src/main/java/com/baeldung/config/CustomConfiguration.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.config; - -import com.baeldung.dao.repositories.InventoryRepository; -import com.baeldung.domain.MerchandiseEntity; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.autoconfigure.domain.EntityScan; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.data.jpa.repository.config.EnableJpaRepositories; - -@SpringBootApplication -@EnableJpaRepositories(basePackageClasses = InventoryRepository.class) -@EntityScan(basePackageClasses = MerchandiseEntity.class) -public class CustomConfiguration { - public static void main(String[] args) { - ConfigurableApplicationContext context = SpringApplication.run(CustomConfiguration.class, args); - - InventoryRepository repo = context.getBean(InventoryRepository.class); - - MerchandiseEntity pants = new MerchandiseEntity("Pair of Pants"); - repo.save(pants); - - MerchandiseEntity shorts = new MerchandiseEntity("Pair of Shorts"); - repo.save(shorts); - - pants.setTitle("Branded Luxury Pants"); - repo.save(pants); - } -} diff --git a/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/InventoryRepository.java b/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/InventoryRepository.java deleted file mode 100644 index a575f0b915..0000000000 --- a/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/InventoryRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.baeldung.dao.repositories; - -import com.baeldung.domain.MerchandiseEntity; -import org.springframework.data.repository.CrudRepository; - -public interface InventoryRepository extends CrudRepository { -} diff --git a/spring-data-jpa/src/main/java/com/baeldung/domain/MerchandiseEntity.java b/spring-data-jpa/src/main/java/com/baeldung/domain/MerchandiseEntity.java deleted file mode 100644 index 921edf1b85..0000000000 --- a/spring-data-jpa/src/main/java/com/baeldung/domain/MerchandiseEntity.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.baeldung.domain; - -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; - -@Entity -public class MerchandiseEntity { - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private Long id; - - private String title; - - public MerchandiseEntity() { - } - - public MerchandiseEntity(String title) { - this.title = title; - } - - public Long getId() { - return id; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } -} From f9065d21b777cc8e29da6138914fc5cd66f59803 Mon Sep 17 00:00:00 2001 From: Dmytro Korniienko Date: Mon, 1 Oct 2018 10:17:39 +0300 Subject: [PATCH 4/6] BAEL-2258 Guide to DateTimeFormatter. Tests added for various formatting patterns and styles --- .../DateTimeFormatterUnitTest.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/core-java-8/src/test/java/com/baeldung/internationalization/DateTimeFormatterUnitTest.java b/core-java-8/src/test/java/com/baeldung/internationalization/DateTimeFormatterUnitTest.java index 4d95bc82e1..cbb84a8783 100644 --- a/core-java-8/src/test/java/com/baeldung/internationalization/DateTimeFormatterUnitTest.java +++ b/core-java-8/src/test/java/com/baeldung/internationalization/DateTimeFormatterUnitTest.java @@ -3,11 +3,14 @@ package com.baeldung.internationalization; import org.junit.Assert; import org.junit.Test; +import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.LocalTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; +import java.time.temporal.ChronoUnit; import java.util.Locale; import java.util.TimeZone; @@ -43,4 +46,70 @@ public class DateTimeFormatterUnitTest { Assert.assertEquals("Monday, January 1, 2018 10:15:50 AM PST", formattedDateTime); Assert.assertEquals("lundi 1 janvier 2018 10 h 15 PST", frFormattedDateTime); } + + @Test + public void shoulPrintFormattedDate() { + String europeanDatePattern = "dd.MM.yyyy"; + DateTimeFormatter europeanDateFormatter = DateTimeFormatter.ofPattern(europeanDatePattern); + LocalDate summerDay = LocalDate.of(2016, 7, 31); + Assert.assertEquals("31.07.2016", europeanDateFormatter.format(summerDay)); + } + + @Test + public void shouldPrintFormattedTime24() { + String timeColonPattern = "HH:mm:ss"; + DateTimeFormatter timeColonFormatter = DateTimeFormatter.ofPattern(timeColonPattern); + LocalTime colonTime = LocalTime.of(17, 35, 50); + Assert.assertEquals("17:35:50", timeColonFormatter.format(colonTime)); + } + + @Test + public void shouldPrintFormattedTimeWithMillis() { + String timeColonPattern = "HH:mm:ss SSS"; + DateTimeFormatter timeColonFormatter = DateTimeFormatter.ofPattern(timeColonPattern); + LocalTime colonTime = LocalTime.of(17, 35, 50).plus(329, ChronoUnit.MILLIS); + Assert.assertEquals("17:35:50 329", timeColonFormatter.format(colonTime)); + } + + @Test + public void shouldPrintFormattedTimePM() { + String timeColonPattern = "hh:mm:ss a"; + DateTimeFormatter timeColonFormatter = DateTimeFormatter.ofPattern(timeColonPattern); + LocalTime colonTime = LocalTime.of(17, 35, 50); + Assert.assertEquals("05:35:50 PM", timeColonFormatter.format(colonTime)); + } + + @Test + public void shouldPrintFormattedUTCRelatedZonedDateTime() { + String newYorkDateTimePattern = "dd.MM.yyyy HH:mm z"; + DateTimeFormatter newYorkDateFormatter = DateTimeFormatter.ofPattern(newYorkDateTimePattern); + LocalDateTime summerDay = LocalDateTime.of(2016, 7, 31, 14, 15); + Assert.assertEquals("31.07.2016 14:15 UTC-04:00", newYorkDateFormatter.format(ZonedDateTime.of(summerDay, ZoneId.of("UTC-4")))); + } + + @Test + public void shouldPrintFormattedNewYorkZonedDateTime() { + String newYorkDateTimePattern = "dd.MM.yyyy HH:mm z"; + DateTimeFormatter newYorkDateFormatter = DateTimeFormatter.ofPattern(newYorkDateTimePattern); + LocalDateTime summerDay = LocalDateTime.of(2016, 7, 31, 14, 15); + Assert.assertEquals("31.07.2016 14:15 EDT", newYorkDateFormatter.format(ZonedDateTime.of(summerDay, ZoneId.of("America/New_York")))); + } + + @Test + public void shouldPrintStyledDate() { + LocalDate anotherSummerDay = LocalDate.of(2016, 8, 23); + Assert.assertEquals("Tuesday, August 23, 2016", DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(anotherSummerDay)); + Assert.assertEquals("August 23, 2016", DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).format(anotherSummerDay)); + Assert.assertEquals("Aug 23, 2016", DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).format(anotherSummerDay)); + Assert.assertEquals("8/23/16", DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(anotherSummerDay)); + } + + @Test + public void shouldPrintStyledDateTime() { + LocalDateTime anotherSummerDay = LocalDateTime.of(2016, 8, 23, 13, 12, 45); + Assert.assertEquals("Tuesday, August 23, 2016 1:12:45 PM EET", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay)); + Assert.assertEquals("August 23, 2016 1:12:45 PM EET", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay)); + Assert.assertEquals("Aug 23, 2016 1:12:45 PM", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay)); + Assert.assertEquals("8/23/16 1:12 PM", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay)); + } } From 7f6df18b64e5defb7909d6aa08748bf1cda41fc5 Mon Sep 17 00:00:00 2001 From: Dmytro Korniienko Date: Wed, 3 Oct 2018 23:55:24 +0300 Subject: [PATCH 5/6] BAEL-2258 Guide to DateTimeFormatter. Predefined formatters tests added --- .../internationalization/DateTimeFormatterUnitTest.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/core-java-8/src/test/java/com/baeldung/internationalization/DateTimeFormatterUnitTest.java b/core-java-8/src/test/java/com/baeldung/internationalization/DateTimeFormatterUnitTest.java index cbb84a8783..1260a6db92 100644 --- a/core-java-8/src/test/java/com/baeldung/internationalization/DateTimeFormatterUnitTest.java +++ b/core-java-8/src/test/java/com/baeldung/internationalization/DateTimeFormatterUnitTest.java @@ -48,7 +48,7 @@ public class DateTimeFormatterUnitTest { } @Test - public void shoulPrintFormattedDate() { + public void shouldPrintFormattedDate() { String europeanDatePattern = "dd.MM.yyyy"; DateTimeFormatter europeanDateFormatter = DateTimeFormatter.ofPattern(europeanDatePattern); LocalDate summerDay = LocalDate.of(2016, 7, 31); @@ -112,4 +112,11 @@ public class DateTimeFormatterUnitTest { Assert.assertEquals("Aug 23, 2016 1:12:45 PM", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay)); Assert.assertEquals("8/23/16 1:12 PM", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay)); } + + @Test + public void shouldPrintFormattedDateTimeWithPredefined() { + Assert.assertEquals("2018-03-09", DateTimeFormatter.ISO_LOCAL_DATE.format(LocalDate.of(2018, 3, 9))); + Assert.assertEquals("2018-03-09-03:00", DateTimeFormatter.ISO_OFFSET_DATE.format(LocalDate.of(2018, 3, 9).atStartOfDay(ZoneId.of("UTC-3")))); + Assert.assertEquals("Fri, 9 Mar 2018 00:00:00 -0300", DateTimeFormatter.RFC_1123_DATE_TIME.format(LocalDate.of(2018, 3, 9).atStartOfDay(ZoneId.of("UTC-3")))); + } } From 08e902cb26072d8959bbcc191a91d1737d235c7d Mon Sep 17 00:00:00 2001 From: Dmytro Korniienko Date: Thu, 4 Oct 2018 23:14:59 +0300 Subject: [PATCH 6/6] BAEL-2258 Guide to DateTimeFormatter. parse() method usage tests added --- .../DateTimeFormatterUnitTest.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/core-java-8/src/test/java/com/baeldung/internationalization/DateTimeFormatterUnitTest.java b/core-java-8/src/test/java/com/baeldung/internationalization/DateTimeFormatterUnitTest.java index 1260a6db92..36a5f6210b 100644 --- a/core-java-8/src/test/java/com/baeldung/internationalization/DateTimeFormatterUnitTest.java +++ b/core-java-8/src/test/java/com/baeldung/internationalization/DateTimeFormatterUnitTest.java @@ -9,6 +9,7 @@ import java.time.LocalTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.time.format.FormatStyle; import java.time.temporal.ChronoUnit; import java.util.Locale; @@ -119,4 +120,39 @@ public class DateTimeFormatterUnitTest { Assert.assertEquals("2018-03-09-03:00", DateTimeFormatter.ISO_OFFSET_DATE.format(LocalDate.of(2018, 3, 9).atStartOfDay(ZoneId.of("UTC-3")))); Assert.assertEquals("Fri, 9 Mar 2018 00:00:00 -0300", DateTimeFormatter.RFC_1123_DATE_TIME.format(LocalDate.of(2018, 3, 9).atStartOfDay(ZoneId.of("UTC-3")))); } + + @Test + public void shouldParseDateTime() { + Assert.assertEquals(LocalDate.of(2018, 3, 12), LocalDate.from(DateTimeFormatter.ISO_LOCAL_DATE.parse("2018-03-09")).plusDays(3)); + } + + @Test + public void shouldParseFormatStyleFull() { + ZonedDateTime dateTime = ZonedDateTime.from(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).parse("Tuesday, August 23, 2016 1:12:45 PM EET")); + Assert.assertEquals(ZonedDateTime.of(LocalDateTime.of(2016, 8, 23, 22, 12, 45), ZoneId.of("Europe/Bucharest")), dateTime.plusHours(9)); + } + + @Test + public void shouldParseDateWithCustomFormatter() { + DateTimeFormatter europeanDateFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); + Assert.assertFalse(LocalDate.from(europeanDateFormatter.parse("15.08.2014")).isLeapYear()); + } + + @Test + public void shouldParseTimeWithCustomFormatter() { + DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss a"); + Assert.assertTrue(LocalTime.from(timeFormatter.parse("12:25:30 AM")).isBefore(LocalTime.NOON)); + } + + @Test + public void shouldParseZonedDateTimeWithCustomFormatter() { + DateTimeFormatter zonedFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm z"); + Assert.assertEquals(7200, ZonedDateTime.from(zonedFormatter.parse("31.07.2016 14:15 GMT+02:00")).getOffset().getTotalSeconds()); + } + + @Test(expected = DateTimeParseException.class) + public void shouldExpectAnExceptionIfDateTimeStringNotMatchPattern() { + DateTimeFormatter zonedFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm z"); + ZonedDateTime.from(zonedFormatter.parse("31.07.2016 14:15")); + } }