|
| 1 | +package com.baeldung.priorityqueuewithpair; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 5 | + |
| 6 | +import java.time.LocalDateTime; |
| 7 | +import java.util.AbstractMap; |
| 8 | +import java.util.Comparator; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.PriorityQueue; |
| 12 | +import java.util.stream.Stream; |
| 13 | +import org.apache.commons.math3.util.Pair; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.params.ParameterizedTest; |
| 16 | +import org.junit.jupiter.params.provider.MethodSource; |
| 17 | + |
| 18 | +class PriorityQueueWithComplexObjectUnitTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + void givenMeetings_whenUseWithPriorityQueue_thenSortByStartDateTime() { |
| 22 | + Meeting projectDiscussion = new Meeting( |
| 23 | + LocalDateTime.parse("2025-11-10T19:00:00"), |
| 24 | + LocalDateTime.parse("2025-11-10T20:00:00"), |
| 25 | + "Project Discussion" |
| 26 | + ); |
| 27 | + Meeting businessMeeting = new Meeting( |
| 28 | + LocalDateTime.parse("2025-11-15T14:00:00"), |
| 29 | + LocalDateTime.parse("2025-11-15T16:00:00"), |
| 30 | + "Business Meeting" |
| 31 | + ); |
| 32 | + PriorityQueue<Meeting> meetings = new PriorityQueue<>(); |
| 33 | + meetings.add(projectDiscussion); |
| 34 | + meetings.add(businessMeeting); |
| 35 | + |
| 36 | + assertThat(meetings.poll()).isEqualTo(projectDiscussion); |
| 37 | + assertThat(meetings.poll()).isEqualTo(businessMeeting); |
| 38 | + } |
| 39 | + |
| 40 | + @ParameterizedTest |
| 41 | + @MethodSource("pairProvider") |
| 42 | + void givenPairs_whenUsePriorityQueue_thenSortThemBySecondElement(List<Pair<String, Integer>> pairs) { |
| 43 | + PriorityQueue<Pair<String, Integer>> queue = new PriorityQueue<>(Comparator.comparingInt(Pair::getSecond)); |
| 44 | + |
| 45 | + queue.addAll(pairs); |
| 46 | + Pair<String, Integer> previousEntry = queue.poll(); |
| 47 | + while (!queue.isEmpty()) { |
| 48 | + Pair<String, Integer> currentEntry = queue.poll(); |
| 49 | + assertThat(previousEntry.getSecond()).isLessThanOrEqualTo(currentEntry.getSecond()); |
| 50 | + previousEntry = currentEntry; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @ParameterizedTest |
| 55 | + @MethodSource("mapEntryProvider") |
| 56 | + void givenMapEntries_whenUsePriorityQueue_thenSortThemBySecondElement(List<Map.Entry<String, Integer>> pairs) { |
| 57 | + PriorityQueue<Map.Entry<String, Integer>> queue = new PriorityQueue<>(Comparator.comparingInt(Map.Entry::getValue)); |
| 58 | + |
| 59 | + queue.addAll(pairs); |
| 60 | + Map.Entry<String, Integer> previousEntry = queue.poll(); |
| 61 | + while (!queue.isEmpty()) { |
| 62 | + Map.Entry<String, Integer> currentEntry = queue.poll(); |
| 63 | + assertThat(previousEntry.getValue()).isLessThanOrEqualTo(currentEntry.getValue()); |
| 64 | + previousEntry = currentEntry; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @ParameterizedTest |
| 69 | + @MethodSource("bookProvider") |
| 70 | + void givenBooks_whenUsePriorityQueue_thenSortThemBySecondElement(List<Book> books) { |
| 71 | + PriorityQueue<Book> queue = new PriorityQueue<>(Comparator.comparingInt(Book::getPublicationYear)); |
| 72 | + queue.addAll(books); |
| 73 | + Book previousBook = queue.poll(); |
| 74 | + while (!queue.isEmpty()) { |
| 75 | + Book currentBook = queue.poll(); |
| 76 | + assertThat(previousBook.getPublicationYear()) |
| 77 | + .isLessThanOrEqualTo(currentBook.getPublicationYear()); |
| 78 | + previousBook = currentBook; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @ParameterizedTest |
| 83 | + @MethodSource("bookProvider") |
| 84 | + void givenBooks_whenUsePriorityQueueWithoutComparator_thenThrowClassCastExcetption(List<Book> books) { |
| 85 | + PriorityQueue<Book> queue = new PriorityQueue<>(); |
| 86 | + assertThatExceptionOfType(ClassCastException.class).isThrownBy(() -> queue.addAll(books)); |
| 87 | + } |
| 88 | + |
| 89 | + @ParameterizedTest |
| 90 | + @MethodSource("bookProvider") |
| 91 | + void givenBooks_whenUsePriorityQueueWithoutComparatorWithoutAddingElements_thenNoExcetption(List<Book> books) { |
| 92 | + PriorityQueue<Book> queue = new PriorityQueue<>(); |
| 93 | + assertThat(queue).isNotNull(); |
| 94 | + } |
| 95 | + |
| 96 | + static Stream<List> pairProvider() { |
| 97 | + return Stream.of( |
| 98 | + List.of( |
| 99 | + new Pair<>("Dune", 1965), |
| 100 | + new Pair<>("Foundation", 1951), |
| 101 | + new Pair<>("2001: A Space Odyssey", 1968), |
| 102 | + new Pair<>("Do Androids Dream of Electric Sheep?", 1968), |
| 103 | + new Pair<>("Ender's Game", 1985), |
| 104 | + new Pair<>("The Hitchhiker's Guide to the Galaxy", 1979), |
| 105 | + new Pair<>("1984", 1949), |
| 106 | + new Pair<>("Brave New World", 1932), |
| 107 | + new Pair<>("Fahrenheit 451", 1953), |
| 108 | + new Pair<>("Neuromancer", 1984), |
| 109 | + new Pair<>("Stranger in a Strange Land", 1961), |
| 110 | + new Pair<>("The Handmaid's Tale", 1985), |
| 111 | + new Pair<>("Ubik", 1969), |
| 112 | + new Pair<>("Solaris", 1961), |
| 113 | + new Pair<>("Slaughterhouse-Five", 1969), |
| 114 | + new Pair<>("The War of the Worlds", 1898), |
| 115 | + new Pair<>("Childhood's End", 1953), |
| 116 | + new Pair<>("Snow Crash", 1992), |
| 117 | + new Pair<>("The Moon is a Harsh Mistress", 1966), |
| 118 | + new Pair<>("Hyperion", 1989), |
| 119 | + new Pair<>("The Left Hand of Darkness", 1969), |
| 120 | + new Pair<>("The Gods Themselves", 1972), |
| 121 | + new Pair<>("The Stars My Destination", 1956), |
| 122 | + new Pair<>("Rendezvous with Rama", 1973), |
| 123 | + new Pair<>("The Forever War", 1974) |
| 124 | + ) |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + static Stream<List> mapEntryProvider() { |
| 129 | + return Stream.of( |
| 130 | + List.of( |
| 131 | + new AbstractMap.SimpleEntry<>("Dune", 1965), |
| 132 | + new AbstractMap.SimpleEntry<>("Foundation", 1951), |
| 133 | + new AbstractMap.SimpleEntry<>("2001: A Space Odyssey", 1968), |
| 134 | + new AbstractMap.SimpleEntry<>("Do Androids Dream of Electric Sheep?", 1968), |
| 135 | + new AbstractMap.SimpleEntry<>("Ender's Game", 1985), |
| 136 | + new AbstractMap.SimpleEntry<>("The Hitchhiker's Guide to the Galaxy", 1979), |
| 137 | + new AbstractMap.SimpleEntry<>("1984", 1949), |
| 138 | + new AbstractMap.SimpleEntry<>("Brave New World", 1932), |
| 139 | + new AbstractMap.SimpleEntry<>("Fahrenheit 451", 1953), |
| 140 | + new AbstractMap.SimpleEntry<>("Neuromancer", 1984), |
| 141 | + new AbstractMap.SimpleEntry<>("Stranger in a Strange Land", 1961), |
| 142 | + new AbstractMap.SimpleEntry<>("The Handmaid's Tale", 1985), |
| 143 | + new AbstractMap.SimpleEntry<>("Ubik", 1969), |
| 144 | + new AbstractMap.SimpleEntry<>("Solaris", 1961), |
| 145 | + new AbstractMap.SimpleEntry<>("Slaughterhouse-Five", 1969), |
| 146 | + new AbstractMap.SimpleEntry<>("The War of the Worlds", 1898), |
| 147 | + new AbstractMap.SimpleEntry<>("Childhood's End", 1953), |
| 148 | + new AbstractMap.SimpleEntry<>("Snow Crash", 1992), |
| 149 | + new AbstractMap.SimpleEntry<>("The Moon is a Harsh Mistress", 1966), |
| 150 | + new AbstractMap.SimpleEntry<>("Hyperion", 1989), |
| 151 | + new AbstractMap.SimpleEntry<>("The Left Hand of Darkness", 1969), |
| 152 | + new AbstractMap.SimpleEntry<>("The Gods Themselves", 1972), |
| 153 | + new AbstractMap.SimpleEntry<>("The Stars My Destination", 1956), |
| 154 | + new AbstractMap.SimpleEntry<>("Rendezvous with Rama", 1973), |
| 155 | + new AbstractMap.SimpleEntry<>("The Forever War", 1974) |
| 156 | + ) |
| 157 | + ); |
| 158 | + } |
| 159 | + |
| 160 | + static Stream<List<Book>> bookProvider() { |
| 161 | + return Stream.of( |
| 162 | + List.of( |
| 163 | + new Book("Frank Herbert", "Dune", 1965), |
| 164 | + new Book("Isaac Asimov", "Foundation", 1951), |
| 165 | + new Book("Arthur C. Clarke", "2001: A Space Odyssey", 1968), |
| 166 | + new Book("Philip K. Dick", "Do Androids Dream of Electric Sheep?", 1968), |
| 167 | + new Book("Orson Scott Card", "Ender's Game", 1985), |
| 168 | + new Book("Douglas Adams", "The Hitchhiker's Guide to the Galaxy", 1979), |
| 169 | + new Book("George Orwell", "1984", 1949), |
| 170 | + new Book("Aldous Huxley", "Brave New World", 1932), |
| 171 | + new Book("Ray Bradbury", "Fahrenheit 451", 1953), |
| 172 | + new Book("William Gibson", "Neuromancer", 1984), |
| 173 | + new Book("Robert A. Heinlein", "Stranger in a Strange Land", 1961), |
| 174 | + new Book("Margaret Atwood", "The Handmaid's Tale", 1985), |
| 175 | + new Book("Philip K. Dick", "Ubik", 1969), |
| 176 | + new Book("Stanislaw Lem", "Solaris", 1961), |
| 177 | + new Book("Kurt Vonnegut", "Slaughterhouse-Five", 1969), |
| 178 | + new Book("H.G. Wells", "The War of the Worlds", 1898), |
| 179 | + new Book("Arthur C. Clarke", "Childhood's End", 1953), |
| 180 | + new Book("Neal Stephenson", "Snow Crash", 1992), |
| 181 | + new Book("Robert A. Heinlein", "The Moon is a Harsh Mistress", 1966), |
| 182 | + new Book("Dan Simmons", "Hyperion", 1989), |
| 183 | + new Book("Ursula K. Le Guin", "The Left Hand of Darkness", 1969), |
| 184 | + new Book("Isaac Asimov", "The Gods Themselves", 1972), |
| 185 | + new Book("Alfred Bester", "The Stars My Destination", 1956), |
| 186 | + new Book("Arthur C. Clarke", "Rendezvous with Rama", 1973), |
| 187 | + new Book("Joe Haldeman", "The Forever War", 1974) |
| 188 | + ) |
| 189 | + ); |
| 190 | + } |
| 191 | +} |
0 commit comments