|
| 1 | +package com.baeldung.findanelement; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +public class FindACustomerInGivenListTest { |
| 11 | + |
| 12 | + private static List<Customer> customers = new ArrayList<>(); |
| 13 | + |
| 14 | + static { |
| 15 | + customers.add(new Customer(1, "Jack")); |
| 16 | + customers.add(new Customer(2, "James")); |
| 17 | + customers.add(new Customer(3, "Sam")); |
| 18 | + } |
| 19 | + |
| 20 | + private static FindACustomerInGivenList findACustomerInGivenList = new FindACustomerInGivenList(); |
| 21 | + |
| 22 | + @Test |
| 23 | + public void givenAnIndex_whenFoundUsingGivenIndex_thenReturnCustomer() { |
| 24 | + Customer customer = findACustomerInGivenList.findUsingGivenIndex(0, customers); |
| 25 | + |
| 26 | + assertEquals(1, customer.getId()); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void givenAnIndex_whenNotFoundUsingGivenIndex_thenReturnNull() { |
| 31 | + Customer customer = findACustomerInGivenList.findUsingGivenIndex(5, customers); |
| 32 | + |
| 33 | + assertNull(customer); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void givenACustomer_whenFoundUsingContains_thenReturnTrue() { |
| 38 | + Customer james = new Customer(2, "James"); |
| 39 | + boolean isJamesPresent = findACustomerInGivenList.findUsingContains(james, customers); |
| 40 | + |
| 41 | + assertEquals(true, isJamesPresent); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void givenACustomer_whenNotFoundUsingContains_thenReturnFalse() { |
| 46 | + Customer john = new Customer(5, "John"); |
| 47 | + boolean isJohnPresent = findACustomerInGivenList.findUsingContains(john, customers); |
| 48 | + |
| 49 | + assertEquals(false, isJohnPresent); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void givenACustomer_whenFoundUsingIndexOf_thenReturnItsIndex() { |
| 54 | + Customer james = new Customer(2, "James"); |
| 55 | + int indexOfJames = findACustomerInGivenList.findUsingIndexOf(james, customers); |
| 56 | + |
| 57 | + assertEquals(1, indexOfJames); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void givenACustomer_whenNotFoundUsingIndexOf_thenReturnMinus1() { |
| 62 | + Customer john = new Customer(5, "John"); |
| 63 | + int indexOfJohn = findACustomerInGivenList.findUsingIndexOf(john, customers); |
| 64 | + |
| 65 | + assertEquals(-1, indexOfJohn); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void givenName_whenCustomerWithNameFoundUsingIterator_thenReturnCustomer() { |
| 70 | + Customer james = findACustomerInGivenList.findUsingIterator("James", customers); |
| 71 | + |
| 72 | + assertEquals("James", james.getName()); |
| 73 | + assertEquals(2, james.getId()); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void givenName_whenCustomerWithNameNotFoundUsingIterator_thenReturnNull() { |
| 78 | + Customer john = findACustomerInGivenList.findUsingIterator("John", customers); |
| 79 | + |
| 80 | + assertNull(john); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void givenName_whenCustomerWithNameFoundUsingEnhancedFor_thenReturnCustomer() { |
| 85 | + Customer james = findACustomerInGivenList.findUsingEnhancedForLoop("James", customers); |
| 86 | + |
| 87 | + assertEquals("James", james.getName()); |
| 88 | + assertEquals(2, james.getId()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void givenName_whenCustomerWithNameNotFoundUsingEnhancedFor_thenReturnNull() { |
| 93 | + Customer john = findACustomerInGivenList.findUsingEnhancedForLoop("John", customers); |
| 94 | + |
| 95 | + assertNull(john); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void givenName_whenCustomerWithNameFoundUsingStream_thenReturnCustomer() { |
| 100 | + Customer james = findACustomerInGivenList.findUsingStream("James", customers); |
| 101 | + |
| 102 | + assertEquals("James", james.getName()); |
| 103 | + assertEquals(2, james.getId()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void givenName_whenCustomerWithNameNotFoundUsingStream_thenReturnNull() { |
| 108 | + Customer john = findACustomerInGivenList.findUsingStream("John", customers); |
| 109 | + |
| 110 | + assertNull(john); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void givenName_whenCustomerWithNameFoundUsingParallelStream_thenReturnCustomer() { |
| 115 | + Customer james = findACustomerInGivenList.findUsingParallelStream("James", customers); |
| 116 | + |
| 117 | + assertEquals("James", james.getName()); |
| 118 | + assertEquals(2, james.getId()); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void givenName_whenCustomerWithNameNotFoundUsingParallelStream_thenReturnNull() { |
| 123 | + Customer john = findACustomerInGivenList.findUsingParallelStream("John", customers); |
| 124 | + |
| 125 | + assertNull(john); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void givenName_whenCustomerWithNameFoundUsingApacheCommon_thenReturnCustomer() { |
| 130 | + Customer james = findACustomerInGivenList.findUsingApacheCommon("James", customers); |
| 131 | + |
| 132 | + assertEquals("James", james.getName()); |
| 133 | + assertEquals(2, james.getId()); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void givenName_whenCustomerWithNameNotFoundUsingApacheCommon_thenReturnNull() { |
| 138 | + Customer john = findACustomerInGivenList.findUsingApacheCommon("John", customers); |
| 139 | + |
| 140 | + assertNull(john); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void givenName_whenCustomerWithNameFoundUsingGuava_thenReturnCustomer() { |
| 145 | + Customer james = findACustomerInGivenList.findUsingGuava("James", customers); |
| 146 | + |
| 147 | + assertEquals("James", james.getName()); |
| 148 | + assertEquals(2, james.getId()); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void givenName_whenCustomerWithNameNotFoundUsingGuava_thenReturnNull() { |
| 153 | + Customer john = findACustomerInGivenList.findUsingGuava("John", customers); |
| 154 | + |
| 155 | + assertNull(john); |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments