reformat the files using intellij-baeldung-style.xml

This commit is contained in:
hajk1
2024-04-10 12:37:21 +03:30
parent 4bdfd9699f
commit a1e28048a6
@@ -8,6 +8,7 @@ import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.Arguments;
@@ -15,55 +16,49 @@ import org.junit.jupiter.params.provider.MethodSource;
class IteratorVsForeachUnitTest { class IteratorVsForeachUnitTest {
private static Stream<Arguments> listProvider() { private static Stream<Arguments> listProvider() {
return Stream.of( return Stream.of(Arguments.of(List.of("String1", "String2", "unwanted"), List.of("String1", "String2")));
Arguments.of( }
List.of("String1", "String2", "unwanted"),
List.of("String1", "String2"))
);
}
@Test @Test
public void givenEmptyCollection_whenUsingForEach_thenNoElementsAreIterated() { public void givenEmptyCollection_whenUsingForEach_thenNoElementsAreIterated() {
List<String> names = Collections.emptyList(); List<String> names = Collections.emptyList();
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
names.forEach(stringBuilder::append); names.forEach(stringBuilder::append);
assertEquals("", stringBuilder.toString()); assertEquals("", stringBuilder.toString());
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("listProvider") @MethodSource("listProvider")
public void givenCollectionWithElements_whenRemovingElementDuringForEachIteration_thenElementIsRemoved( public void givenCollectionWithElements_whenRemovingElementDuringForEachIteration_thenElementIsRemoved(List<String> input, List<String> expected) {
List<String> input, List<String> expected) { List<String> mutableList = new ArrayList<>(input);
List<String> mutableList = new ArrayList<>(input); // Separate collection for items to be removed
// Separate collection for items to be removed List<String> toRemove = new ArrayList<>();
List<String> toRemove = new ArrayList<>();
// Using forEach to identify items to remove // Using forEach to identify items to remove
input.forEach(item -> { input.forEach(item -> {
if (item.equals("unwanted")) { if (item.equals("unwanted")) {
toRemove.add(item); toRemove.add(item);
} }
}); });
// Removing the identified items from the original list // Removing the identified items from the original list
mutableList.removeAll(toRemove); mutableList.removeAll(toRemove);
assertIterableEquals(expected, mutableList); assertIterableEquals(expected, mutableList);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("listProvider") @MethodSource("listProvider")
public void givenCollectionWithElements_whenRemovingElementDuringIteratorIteration_thenElementIsRemoved( public void givenCollectionWithElements_whenRemovingElementDuringIteratorIteration_thenElementIsRemoved(List<String> input, List<String> expected) {
List<String> input, List<String> expected) { List<String> mutableList = new ArrayList<>(input);
List<String> mutableList = new ArrayList<>(input); Iterator<String> it = mutableList.iterator();
Iterator<String> it = mutableList.iterator(); while (it.hasNext()) {
while (it.hasNext()) { String item = it.next();
String item = it.next(); if (item.equals("unwanted")) {
if (item.equals("unwanted")) { it.remove(); // Safely remove item
it.remove(); // Safely remove item }
} }
assertIterableEquals(expected, mutableList);
} }
assertIterableEquals(expected, mutableList);
}
} }