From 2bff7a623e06c4705f3225b022ca7c6bb494f3e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Thiago?= Date: Thu, 21 Dec 2017 13:06:58 -0200 Subject: [PATCH 1/3] BAEL-1328 Solving conflcts and applying code formatter. (#3273) * BAEL-1328 How to Invert an Array in Java * Removing code from evaluation article. * BAEL-1328 Adjusting spaces and implement inversion with commons lang3 and guava. * BAEL-1328 Renaming unit test class; using AssertJ. * BAEL-1328 Change to use rangeClosed. * BAEL-1326 Solving conflicts and apply code formatting. --- core-java/src/main/java/com/baeldung/array/ArrayInverter.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core-java/src/main/java/com/baeldung/array/ArrayInverter.java b/core-java/src/main/java/com/baeldung/array/ArrayInverter.java index 7f7fcbb5a8..86438baf1a 100644 --- a/core-java/src/main/java/com/baeldung/array/ArrayInverter.java +++ b/core-java/src/main/java/com/baeldung/array/ArrayInverter.java @@ -25,7 +25,9 @@ public class ArrayInverter { } public Object[] invertUsingStreams(final Object[] array) { - return IntStream.range(1, array.length + 1).mapToObj(i -> array[array.length - i]).toArray(); + return IntStream.rangeClosed(1, array.length) + .mapToObj(i -> array[array.length - i]) + .toArray(); } public void invertUsingCommonsLang(Object[] array) { From d14cc423068d9799381815a012d4caec23c5d11e Mon Sep 17 00:00:00 2001 From: Shouvik Bhattacharya <33756821+shouvikbhattacharya@users.noreply.github.com> Date: Thu, 21 Dec 2017 22:14:27 +0530 Subject: [PATCH 2/3] BAEL-1438: TreeMap vs HashMap (#3265) * BAEL-1438: Final code. * BAEL-1438: Temporary commit. * BAEL-1438: New Changes. * BAEL-1438: New test case to support Java 8 features. --- .../WhenComparingTreeMapVsHashMap.java | 60 +++++++++++++++++++ .../java/com/baeldung/ExceptionUnitTest.java | 15 +++++ 2 files changed, 75 insertions(+) create mode 100644 core-java/src/test/java/com/baeldung/collection/WhenComparingTreeMapVsHashMap.java diff --git a/core-java/src/test/java/com/baeldung/collection/WhenComparingTreeMapVsHashMap.java b/core-java/src/test/java/com/baeldung/collection/WhenComparingTreeMapVsHashMap.java new file mode 100644 index 0000000000..f2dfc992c2 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/collection/WhenComparingTreeMapVsHashMap.java @@ -0,0 +1,60 @@ +package com.baeldung.collection; + +import java.util.ConcurrentModificationException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.TreeMap; + +import org.hamcrest.Matchers; +import org.junit.Assert; +import org.junit.Test; + +public class WhenComparingTreeMapVsHashMap { + + @Test + public void whenInsertObjectsTreeMap_thenNaturalOrder() { + Map treemap = new TreeMap<>(); + treemap.put(3, "TreeMap"); + treemap.put(2, "vs"); + treemap.put(1, "HashMap"); + Assert.assertThat(treemap.keySet(), Matchers.contains(1, 2, 3)); + } + + @Test(expected = NullPointerException.class) + public void whenInsertNullInTreeMap_thenException() { + Map treemap = new TreeMap<>(); + treemap.put(null, "NullPointerException"); + } + + @Test + public void whenInsertObjectsHashMap_thenRandomOrder() { + Map hashmap = new HashMap<>(); + hashmap.put(3, "TreeMap"); + hashmap.put(2, "vs"); + hashmap.put(1, "HashMap"); + Assert.assertThat(hashmap.keySet(), Matchers.containsInAnyOrder(1, 2, 3)); + } + + @Test + public void whenInsertNullInHashMap_thenInsertsNull() { + Map hashmap = new HashMap<>(); + hashmap.put(null, null); + Assert.assertNull(hashmap.get(null)); + } + + @Test + public void givenHashMapAndTreeMap_whenputDuplicates_thenOnlyUnique() { + Map treeMap = new HashMap<>(); + treeMap.put(1, "Baeldung"); + treeMap.put(1, "Baeldung"); + + Assert.assertTrue(treeMap.size() == 1); + + Map treeMap2 = new TreeMap<>(); + treeMap2.put(1, "Baeldung"); + treeMap2.put(1, "Baeldung"); + + Assert.assertTrue(treeMap2.size() == 1); + } +} \ No newline at end of file diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/ExceptionUnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/ExceptionUnitTest.java index bd57f5b3cb..440c3e40ab 100644 --- a/testing-modules/junit-5/src/test/java/com/baeldung/ExceptionUnitTest.java +++ b/testing-modules/junit-5/src/test/java/com/baeldung/ExceptionUnitTest.java @@ -3,7 +3,12 @@ package com.baeldung; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import java.util.ConcurrentModificationException; +import java.util.HashMap; +import java.util.Map; + import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; public class ExceptionUnitTest { @@ -22,4 +27,14 @@ public class ExceptionUnitTest { Integer.valueOf(str); }); } + + @Test + public void whenModifyMapDuringIteration_thenThrowExecption() { + Map hashmap = new HashMap<>(); + hashmap.put(1, "One"); + hashmap.put(2, "Two"); + + Executable executable = () -> hashmap.forEach((key, value) -> hashmap.remove(1)); + assertThrows(ConcurrentModificationException.class, executable); + } } From 4f0ae5d2e33cae3a9dcdb7e29001416200f2a913 Mon Sep 17 00:00:00 2001 From: Tim Schimandle Date: Thu, 21 Dec 2017 10:22:27 -0700 Subject: [PATCH 3/3] BAEL-1176 SPring Could Heroku (#3212) * BAEL-1176 setting up base application with in memory database * BAEL-1176 adding postgres stuff * BAEL-1176 swapping product entity for book entity to make this in line with other spring boot apps. * BAEL-1176 change products endpoint to books --- .../cloud/connectors/heroku/book/Book.java | 39 +++++++++++++++++++ .../heroku/book/BookController.java | 26 +++++++++++++ .../heroku/book/BookRepository.java | 6 +++ .../connectors/heroku/book/BookService.java | 29 ++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/Book.java create mode 100644 spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookController.java create mode 100644 spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookRepository.java create mode 100644 spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookService.java diff --git a/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/Book.java b/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/Book.java new file mode 100644 index 0000000000..6f45ef6a29 --- /dev/null +++ b/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/Book.java @@ -0,0 +1,39 @@ +package com.baeldung.spring.cloud.connectors.heroku.book; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Book { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private String title; + private String author; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } +} diff --git a/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookController.java b/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookController.java new file mode 100644 index 0000000000..eb2972f35a --- /dev/null +++ b/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookController.java @@ -0,0 +1,26 @@ +package com.baeldung.spring.cloud.connectors.heroku.book; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/books") +public class BookController { + + private final BookService bookService; + + @Autowired + public BookController(BookService bookService) { + this.bookService = bookService; + } + + @GetMapping("/{bookId}") + public Book findBook(@PathVariable Long bookId) { + return bookService.findBookById(bookId); + } + + @PostMapping + public Book createBook(@RequestBody Book book) { + return bookService.createBook(book); + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookRepository.java b/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookRepository.java new file mode 100644 index 0000000000..9bdf16148d --- /dev/null +++ b/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookRepository.java @@ -0,0 +1,6 @@ +package com.baeldung.spring.cloud.connectors.heroku.book; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface BookRepository extends JpaRepository{ +} diff --git a/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookService.java b/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookService.java new file mode 100644 index 0000000000..4978ded65f --- /dev/null +++ b/spring-cloud/spring-cloud-connectors-heroku/src/main/java/com/baeldung/spring/cloud/connectors/heroku/book/BookService.java @@ -0,0 +1,29 @@ +package com.baeldung.spring.cloud.connectors.heroku.book; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional(readOnly = true) +public class BookService { + private final BookRepository bookRepository; + + @Autowired + public BookService(BookRepository bookRepository) { + this.bookRepository = bookRepository; + } + + public Book findBookById(Long bookId) { + return bookRepository.findOne(bookId); + } + + @Transactional(propagation = Propagation.REQUIRED) + public Book createBook(Book book) { + Book newBook = new Book(); + newBook.setTitle(book.getTitle()); + newBook.setAuthor(book.getAuthor()); + return bookRepository.save(newBook); + } +}