reformat the files using intellij-baeldung-style.xml

This commit is contained in:
hajk1 2024-04-07 18:25:13 +03:30
parent fc31ffd062
commit 2a754ad337
2 changed files with 90 additions and 95 deletions

View File

@ -1,54 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>core-java-collections-6</artifactId> <artifactId>core-java-collections-6</artifactId>
<name>core-java-collections-6</name> <name>core-java-collections-6</name>
<packaging>jar</packaging> <packaging>jar</packaging>
<parent> <parent>
<artifactId>core-java-modules</artifactId> <artifactId>core-java-modules</artifactId>
<groupId>com.baeldung.core-java-modules</groupId> <groupId>com.baeldung.core-java-modules</groupId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
</parent> </parent>
<dependencies> <dependencies>
<dependency> <dependency>
<artifactId>junit-platform-runner</artifactId> <artifactId>junit-platform-runner</artifactId>
<groupId>org.junit.platform</groupId> <groupId>org.junit.platform</groupId>
<scope>test</scope> <scope>test</scope>
<version>${junit-platform.version}</version> <version>${junit-platform.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<artifactId>junit-jupiter</artifactId> <artifactId>junit-jupiter</artifactId>
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>
<scope>test</scope> <scope>test</scope>
<version>${junit.version}</version> <version>${junit.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<artifactId>junit-vintage-engine</artifactId> <artifactId>junit-vintage-engine</artifactId>
<groupId>org.junit.vintage</groupId> <groupId>org.junit.vintage</groupId>
<scope>test</scope> <scope>test</scope>
<version>${junit.version}</version> <version>${junit.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<configuration> <configuration>
<source>9</source> <source>9</source>
<target>9</target> <target>9</target>
</configuration> </configuration>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<properties> <properties>
<junit.version>5.10.2</junit.version> <junit.version>5.10.2</junit.version>
</properties> </properties>
</project> </project>

View File

@ -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
); public void givenEmptyCollection_whenUsingForEach_thenNoElementsAreIterated() {
} List<String> names = Collections.emptyList();
StringBuilder stringBuilder = new StringBuilder();
@Test names.forEach(stringBuilder::append);
public void givenEmptyCollection_whenUsingForEach_thenNoElementsAreIterated() { assertEquals("", stringBuilder.toString());
List<String> names = Collections.emptyList(); }
StringBuilder stringBuilder = new StringBuilder();
names.forEach(stringBuilder::append); @ParameterizedTest
assertEquals("", stringBuilder.toString()); @MethodSource("listProvider")
} public void givenCollectionWithElements_whenRemovingElementDuringForEachIteration_thenElementIsRemoved(List<String> input, List<String> expected) {
List<String> mutableList = new ArrayList<>(input);
@ParameterizedTest // Separate collection for items to be removed
@MethodSource("listProvider") List<String> toRemove = new ArrayList<>();
public void givenCollectionWithElements_whenRemovingElementDuringForEachIteration_thenElementIsRemoved(
List<String> input, List<String> expected) { // Using forEach to identify items to remove
List<String> mutableList = new ArrayList<>(input); input.forEach(item -> {
// Separate collection for items to be removed if (item.equals("unwanted")) {
List<String> toRemove = new ArrayList<>(); toRemove.add(item);
}
// Using forEach to identify items to remove });
input.forEach(item -> {
if (item.equals("unwanted")) { // Removing the identified items from the original list
toRemove.add(item); mutableList.removeAll(toRemove);
} assertIterableEquals(expected, mutableList);
}); }
// Removing the identified items from the original list @ParameterizedTest
mutableList.removeAll(toRemove); @MethodSource("listProvider")
assertIterableEquals(expected, mutableList); public void givenCollectionWithElements_whenRemovingElementDuringIteratorIteration_thenElementIsRemoved(List<String> input, List<String> expected) {
} List<String> mutableList = new ArrayList<>(input);
Iterator<String> it = mutableList.iterator();
@ParameterizedTest while (it.hasNext()) {
@MethodSource("listProvider") String item = it.next();
public void givenCollectionWithElements_whenRemovingElementDuringIteratorIteration_thenElementIsRemoved( if (item.equals("unwanted")) {
List<String> input, List<String> expected) { it.remove(); // Safely remove item
List<String> mutableList = new ArrayList<>(input); }
Iterator<String> it = mutableList.iterator(); }
while (it.hasNext()) { assertIterableEquals(expected, mutableList);
String item = it.next();
if (item.equals("unwanted")) {
it.remove(); // Safely remove item
}
} }
assertIterableEquals(expected, mutableList);
}
} }