commit
a8f3ef312b
|
@ -1 +0,0 @@
|
|||
/bin/
|
|
@ -1,93 +1,89 @@
|
|||
package findItems;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class FindItemsBasedOnValues {
|
||||
|
||||
List<Employee> EmplList = new ArrayList<Employee>();
|
||||
List<Department> deptList = new ArrayList<Department>();
|
||||
|
||||
public static void main(String[] args) throws ParseException {
|
||||
FindItemsBasedOnValues findItems = new FindItemsBasedOnValues();
|
||||
findItems.givenDepartmentList_thenEmployeeListIsFilteredCorrectly();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDepartmentList_thenEmployeeListIsFilteredCorrectly() {
|
||||
Integer expectedId = 1002;
|
||||
|
||||
populate(EmplList, deptList);
|
||||
|
||||
List<Employee> filteredList = EmplList.stream()
|
||||
.filter(empl -> deptList.stream()
|
||||
.anyMatch(dept -> dept.getDepartment()
|
||||
.equals("sales") && empl.getEmployeeId()
|
||||
.equals(dept.getEmployeeId())))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(expectedId, filteredList.get(0)
|
||||
.getEmployeeId());
|
||||
}
|
||||
|
||||
private void populate(List<Employee> EmplList, List<Department> deptList) {
|
||||
Employee employee1 = new Employee(1001, "empl1");
|
||||
Employee employee2 = new Employee(1002, "empl2");
|
||||
Employee employee3 = new Employee(1003, "empl3");
|
||||
|
||||
Collections.addAll(EmplList, employee1, employee2, employee3);
|
||||
|
||||
Department department1 = new Department(1002, "sales");
|
||||
Department department2 = new Department(1003, "marketing");
|
||||
Department department3 = new Department(1004, "sales");
|
||||
|
||||
Collections.addAll(deptList, department1, department2, department3);
|
||||
}
|
||||
}
|
||||
|
||||
class Employee {
|
||||
Integer employeeId;
|
||||
String employeeName;
|
||||
|
||||
public Employee(Integer employeeId, String employeeName) {
|
||||
super();
|
||||
this.employeeId = employeeId;
|
||||
this.employeeName = employeeName;
|
||||
}
|
||||
|
||||
public Integer getEmployeeId() {
|
||||
return employeeId;
|
||||
}
|
||||
|
||||
public String getEmployeeName() {
|
||||
return employeeName;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Department {
|
||||
Integer employeeId;
|
||||
String department;
|
||||
|
||||
public Department(Integer employeeId, String department) {
|
||||
super();
|
||||
this.employeeId = employeeId;
|
||||
this.department = department;
|
||||
}
|
||||
|
||||
public Integer getEmployeeId() {
|
||||
return employeeId;
|
||||
}
|
||||
|
||||
public String getDepartment() {
|
||||
return department;
|
||||
}
|
||||
|
||||
package com.baeldung.findItems;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class FindItemsBasedOnOtherStreamUnitTest {
|
||||
|
||||
private List<Employee> employeeList = new ArrayList<Employee>();
|
||||
|
||||
private List<Department> departmentList = new ArrayList<Department>();
|
||||
|
||||
@Test
|
||||
public void givenDepartmentList_thenEmployeeListIsFilteredCorrectly() {
|
||||
Integer expectedId = 1002;
|
||||
|
||||
populate(employeeList, departmentList);
|
||||
|
||||
List<Employee> filteredList = employeeList.stream()
|
||||
.filter(empl -> departmentList.stream()
|
||||
.anyMatch(dept -> dept.getDepartment()
|
||||
.equals("sales") && empl.getEmployeeId()
|
||||
.equals(dept.getEmployeeId())))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(expectedId, filteredList.get(0)
|
||||
.getEmployeeId());
|
||||
}
|
||||
|
||||
private void populate(List<Employee> EmplList, List<Department> deptList) {
|
||||
Employee employee1 = new Employee(1001, "empl1");
|
||||
Employee employee2 = new Employee(1002, "empl2");
|
||||
Employee employee3 = new Employee(1003, "empl3");
|
||||
|
||||
Collections.addAll(EmplList, employee1, employee2, employee3);
|
||||
|
||||
Department department1 = new Department(1002, "sales");
|
||||
Department department2 = new Department(1003, "marketing");
|
||||
Department department3 = new Department(1004, "sales");
|
||||
|
||||
Collections.addAll(deptList, department1, department2, department3);
|
||||
}
|
||||
}
|
||||
|
||||
class Employee {
|
||||
private Integer employeeId;
|
||||
private String employeeName;
|
||||
|
||||
Employee(Integer employeeId, String employeeName) {
|
||||
super();
|
||||
this.employeeId = employeeId;
|
||||
this.employeeName = employeeName;
|
||||
}
|
||||
|
||||
Integer getEmployeeId() {
|
||||
return employeeId;
|
||||
}
|
||||
|
||||
public String getEmployeeName() {
|
||||
return employeeName;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Department {
|
||||
private Integer employeeId;
|
||||
private String department;
|
||||
|
||||
Department(Integer employeeId, String department) {
|
||||
super();
|
||||
this.employeeId = employeeId;
|
||||
this.department = department;
|
||||
}
|
||||
|
||||
Integer getEmployeeId() {
|
||||
return employeeId;
|
||||
}
|
||||
|
||||
String getDepartment() {
|
||||
return department;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.modulo;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Java6Assertions.*;
|
||||
|
||||
public class ModuloUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenIntegerDivision_thenLosesRemainder(){
|
||||
assertThat(11 / 4).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDoubleDivision_thenKeepsRemainder(){
|
||||
assertThat(11 / 4.0).isEqualTo(2.75);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenModulo_thenReturnsRemainder(){
|
||||
assertThat(11 % 4).isEqualTo(3);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void whenDivisionByZero_thenArithmeticException(){
|
||||
double result = 1 / 0;
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void whenModuloByZero_thenArithmeticException(){
|
||||
double result = 1 % 0;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDivisorIsOddAndModulusIs2_thenResultIs1(){
|
||||
assertThat(3 % 2).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDivisorIsEvenAndModulusIs2_thenResultIs0(){
|
||||
assertThat(4 % 2).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenItemsIsAddedToCircularQueue_thenNoArrayIndexOutOfBounds(){
|
||||
int QUEUE_CAPACITY= 10;
|
||||
int[] circularQueue = new int[QUEUE_CAPACITY];
|
||||
int itemsInserted = 0;
|
||||
for (int value = 0; value < 1000; value++) {
|
||||
int writeIndex = ++itemsInserted % QUEUE_CAPACITY;
|
||||
circularQueue[writeIndex] = value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue