reformat the files using intellij-baeldung-style.xml
This commit is contained in:
		
							parent
							
								
									fc31ffd062
								
							
						
					
					
						commit
						2a754ad337
					
				| @ -1,54 +1,54 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
|   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"> | ||||
|   <modelVersion>4.0.0</modelVersion> | ||||
|   <artifactId>core-java-collections-6</artifactId> | ||||
|   <name>core-java-collections-6</name> | ||||
|   <packaging>jar</packaging> | ||||
|          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"> | ||||
|     <modelVersion>4.0.0</modelVersion> | ||||
|     <artifactId>core-java-collections-6</artifactId> | ||||
|     <name>core-java-collections-6</name> | ||||
|     <packaging>jar</packaging> | ||||
| 
 | ||||
|   <parent> | ||||
|     <artifactId>core-java-modules</artifactId> | ||||
|     <groupId>com.baeldung.core-java-modules</groupId> | ||||
|     <version>0.0.1-SNAPSHOT</version> | ||||
|   </parent> | ||||
|     <parent> | ||||
|         <artifactId>core-java-modules</artifactId> | ||||
|         <groupId>com.baeldung.core-java-modules</groupId> | ||||
|         <version>0.0.1-SNAPSHOT</version> | ||||
|     </parent> | ||||
| 
 | ||||
|   <dependencies> | ||||
|     <dependency> | ||||
|       <artifactId>junit-platform-runner</artifactId> | ||||
|       <groupId>org.junit.platform</groupId> | ||||
|       <scope>test</scope> | ||||
|       <version>${junit-platform.version}</version> | ||||
|     </dependency> | ||||
|     <dependency> | ||||
|       <artifactId>junit-jupiter</artifactId> | ||||
|       <groupId>org.junit.jupiter</groupId> | ||||
|       <scope>test</scope> | ||||
|       <version>${junit.version}</version> | ||||
|     </dependency> | ||||
|     <dependency> | ||||
|       <artifactId>junit-vintage-engine</artifactId> | ||||
|       <groupId>org.junit.vintage</groupId> | ||||
|       <scope>test</scope> | ||||
|       <version>${junit.version}</version> | ||||
|     </dependency> | ||||
|   </dependencies> | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
|             <artifactId>junit-platform-runner</artifactId> | ||||
|             <groupId>org.junit.platform</groupId> | ||||
|             <scope>test</scope> | ||||
|             <version>${junit-platform.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <artifactId>junit-jupiter</artifactId> | ||||
|             <groupId>org.junit.jupiter</groupId> | ||||
|             <scope>test</scope> | ||||
|             <version>${junit.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <artifactId>junit-vintage-engine</artifactId> | ||||
|             <groupId>org.junit.vintage</groupId> | ||||
|             <scope>test</scope> | ||||
|             <version>${junit.version}</version> | ||||
|         </dependency> | ||||
|     </dependencies> | ||||
| 
 | ||||
|   <build> | ||||
|     <plugins> | ||||
|       <plugin> | ||||
|         <artifactId>maven-compiler-plugin</artifactId> | ||||
|         <configuration> | ||||
|           <source>9</source> | ||||
|           <target>9</target> | ||||
|         </configuration> | ||||
|         <groupId>org.apache.maven.plugins</groupId> | ||||
|       </plugin> | ||||
|     </plugins> | ||||
|   </build> | ||||
|     <build> | ||||
|         <plugins> | ||||
|             <plugin> | ||||
|                 <artifactId>maven-compiler-plugin</artifactId> | ||||
|                 <configuration> | ||||
|                     <source>9</source> | ||||
|                     <target>9</target> | ||||
|                 </configuration> | ||||
|                 <groupId>org.apache.maven.plugins</groupId> | ||||
|             </plugin> | ||||
|         </plugins> | ||||
|     </build> | ||||
| 
 | ||||
|   <properties> | ||||
|     <junit.version>5.10.2</junit.version> | ||||
|   </properties> | ||||
|     <properties> | ||||
|         <junit.version>5.10.2</junit.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
|  | ||||
| @ -8,6 +8,7 @@ import java.util.Collections; | ||||
| import java.util.Iterator; | ||||
| import java.util.List; | ||||
| import java.util.stream.Stream; | ||||
| 
 | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.junit.jupiter.params.ParameterizedTest; | ||||
| import org.junit.jupiter.params.provider.Arguments; | ||||
| @ -15,55 +16,49 @@ import org.junit.jupiter.params.provider.MethodSource; | ||||
| 
 | ||||
| class IteratorVsForeachUnitTest { | ||||
| 
 | ||||
|   private static Stream<Arguments> listProvider() { | ||||
|     return Stream.of( | ||||
|         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(); | ||||
|     names.forEach(stringBuilder::append); | ||||
|     assertEquals("", stringBuilder.toString()); | ||||
|   } | ||||
| 
 | ||||
|   @ParameterizedTest | ||||
|   @MethodSource("listProvider") | ||||
|   public void givenCollectionWithElements_whenRemovingElementDuringForEachIteration_thenElementIsRemoved( | ||||
|       List<String> input, List<String> expected) { | ||||
|     List<String> mutableList = new ArrayList<>(input); | ||||
|     // Separate collection for items to be removed | ||||
|     List<String> toRemove = new ArrayList<>(); | ||||
| 
 | ||||
|     // Using forEach to identify items to remove | ||||
|     input.forEach(item -> { | ||||
|       if (item.equals("unwanted")) { | ||||
|         toRemove.add(item); | ||||
|       } | ||||
|     }); | ||||
| 
 | ||||
|     // Removing the identified items from the original list | ||||
|     mutableList.removeAll(toRemove); | ||||
|     assertIterableEquals(expected, mutableList); | ||||
|   } | ||||
| 
 | ||||
|   @ParameterizedTest | ||||
|   @MethodSource("listProvider") | ||||
|   public void givenCollectionWithElements_whenRemovingElementDuringIteratorIteration_thenElementIsRemoved( | ||||
|       List<String> input, List<String> expected) { | ||||
|     List<String> mutableList = new ArrayList<>(input); | ||||
|     Iterator<String> it = mutableList.iterator(); | ||||
|     while (it.hasNext()) { | ||||
|       String item = it.next(); | ||||
|       if (item.equals("unwanted")) { | ||||
|         it.remove(); // Safely remove item | ||||
|       } | ||||
|     private static Stream<Arguments> listProvider() { | ||||
|         return Stream.of(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(); | ||||
|         names.forEach(stringBuilder::append); | ||||
|         assertEquals("", stringBuilder.toString()); | ||||
|     } | ||||
| 
 | ||||
|     @ParameterizedTest | ||||
|     @MethodSource("listProvider") | ||||
|     public void givenCollectionWithElements_whenRemovingElementDuringForEachIteration_thenElementIsRemoved(List<String> input, List<String> expected) { | ||||
|         List<String> mutableList = new ArrayList<>(input); | ||||
|         // Separate collection for items to be removed | ||||
|         List<String> toRemove = new ArrayList<>(); | ||||
| 
 | ||||
|         // Using forEach to identify items to remove | ||||
|         input.forEach(item -> { | ||||
|             if (item.equals("unwanted")) { | ||||
|                 toRemove.add(item); | ||||
|             } | ||||
|         }); | ||||
| 
 | ||||
|         // Removing the identified items from the original list | ||||
|         mutableList.removeAll(toRemove); | ||||
|         assertIterableEquals(expected, mutableList); | ||||
|     } | ||||
| 
 | ||||
|     @ParameterizedTest | ||||
|     @MethodSource("listProvider") | ||||
|     public void givenCollectionWithElements_whenRemovingElementDuringIteratorIteration_thenElementIsRemoved(List<String> input, List<String> expected) { | ||||
|         List<String> mutableList = new ArrayList<>(input); | ||||
|         Iterator<String> it = mutableList.iterator(); | ||||
|         while (it.hasNext()) { | ||||
|             String item = it.next(); | ||||
|             if (item.equals("unwanted")) { | ||||
|                 it.remove(); // Safely remove item | ||||
|             } | ||||
|         } | ||||
|         assertIterableEquals(expected, mutableList); | ||||
|     } | ||||
|     assertIterableEquals(expected, mutableList); | ||||
|   } | ||||
| 
 | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user