Introduction to Apache Commons Lang 3 (#1301)
This commit is contained in:
		
							parent
							
								
									c83c449fa5
								
							
						
					
					
						commit
						31b7ced8c4
					
				
							
								
								
									
										36
									
								
								apache-commons/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								apache-commons/pom.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,36 @@ | ||||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
|     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> | ||||
|     <groupId>com.baeldung</groupId> | ||||
|     <artifactId>apache-commons</artifactId> | ||||
|     <version>0.0.1-SNAPSHOT</version> | ||||
|     <properties> | ||||
|         <junit.version>4.12</junit.version> | ||||
|         <compiler.version>3.6.0</compiler.version> | ||||
|     </properties> | ||||
|     <build> | ||||
|         <plugins> | ||||
|             <plugin> | ||||
|                 <artifactId>maven-compiler-plugin</artifactId> | ||||
|                 <version>${compiler.version}</version> | ||||
|                 <configuration> | ||||
|                     <source>1.8</source> | ||||
|                     <target>1.8</target> | ||||
|                 </configuration> | ||||
|             </plugin> | ||||
|         </plugins> | ||||
|     </build> | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
|             <groupId>org.apache.commons</groupId> | ||||
|             <artifactId>commons-lang3</artifactId> | ||||
|             <version>3.5</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>junit</groupId> | ||||
|             <artifactId>junit</artifactId> | ||||
|             <version>${junit.version}</version> | ||||
|             <scope>test</scope> | ||||
|         </dependency> | ||||
|     </dependencies> | ||||
| </project> | ||||
| @ -0,0 +1,137 @@ | ||||
| package com.baeldung.commons.lang3; | ||||
| 
 | ||||
| import org.apache.commons.lang3.ArrayUtils; | ||||
| import org.junit.Test; | ||||
| import static org.junit.Assert.assertEquals; | ||||
| import static org.junit.Assert.assertArrayEquals; | ||||
| 
 | ||||
| public class ArrayUtilsTest { | ||||
|     @Test | ||||
|     public void givenArray_whenAddingElementAtSpecifiedPosition_thenCorrect() { | ||||
|         int[] oldArray = { 2, 3, 4, 5 }; | ||||
|         int[] newArray = ArrayUtils.add(oldArray, 0, 1); | ||||
|         int[] expectedArray = { 1, 2, 3, 4, 5 }; | ||||
|         assertArrayEquals(expectedArray, newArray); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenArray_whenAddingElementAtTheEnd_thenCorrect() { | ||||
|         int[] oldArray = { 2, 3, 4, 5 }; | ||||
|         int[] newArray = ArrayUtils.add(oldArray, 1); | ||||
|         int[] expectedArray = { 2, 3, 4, 5, 1 }; | ||||
|         assertArrayEquals(expectedArray, newArray); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenArray_whenAddingAllElementsAtTheEnd_thenCorrect() { | ||||
|         int[] oldArray = { 0, 1, 2 }; | ||||
|         int[] newArray = ArrayUtils.addAll(oldArray, 3, 4, 5); | ||||
|         int[] expectedArray = { 0, 1, 2, 3, 4, 5 }; | ||||
|         assertArrayEquals(expectedArray, newArray); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenArray_whenRemovingElementAtSpecifiedPosition_thenCorrect() { | ||||
|         int[] oldArray = { 1, 2, 3, 4, 5 }; | ||||
|         int[] newArray = ArrayUtils.remove(oldArray, 1); | ||||
|         int[] expectedArray = { 1, 3, 4, 5 }; | ||||
|         assertArrayEquals(expectedArray, newArray); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenArray_whenRemovingAllElementsAtSpecifiedPositions_thenCorrect() { | ||||
|         int[] oldArray = { 1, 2, 3, 4, 5 }; | ||||
|         int[] newArray = ArrayUtils.removeAll(oldArray, 1, 3); | ||||
|         int[] expectedArray = { 1, 3, 5 }; | ||||
|         assertArrayEquals(expectedArray, newArray); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenArray_whenRemovingAnElement_thenCorrect() { | ||||
|         int[] oldArray = { 1, 2, 3, 3, 4 }; | ||||
|         int[] newArray = ArrayUtils.removeElement(oldArray, 3); | ||||
|         int[] expectedArray = { 1, 2, 3, 4 }; | ||||
|         assertArrayEquals(expectedArray, newArray); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenArray_whenRemovingElements_thenCorrect() { | ||||
|         int[] oldArray = { 1, 2, 3, 3, 4 }; | ||||
|         int[] newArray = ArrayUtils.removeElements(oldArray, 2, 3, 5); | ||||
|         int[] expectedArray = { 1, 3, 4 }; | ||||
|         assertArrayEquals(expectedArray, newArray); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenArray_whenRemovingAllElementOccurences_thenCorrect() { | ||||
|         int[] oldArray = { 1, 2, 2, 2, 3 }; | ||||
|         int[] newArray = ArrayUtils.removeAllOccurences(oldArray, 2); | ||||
|         int[] expectedArray = { 1, 3 }; | ||||
|         assertArrayEquals(expectedArray, newArray); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenArray_whenCheckingExistingElement_thenCorrect() { | ||||
|         int[] array = { 1, 3, 5, 7, 9 }; | ||||
|         boolean evenContained = ArrayUtils.contains(array, 2); | ||||
|         boolean oddContained = ArrayUtils.contains(array, 7); | ||||
|         assertEquals(false, evenContained); | ||||
|         assertEquals(true, oddContained); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenArray_whenReversingElementsWithinARange_thenCorrect() { | ||||
|         int[] originalArray = { 1, 2, 3, 4, 5 }; | ||||
|         ArrayUtils.reverse(originalArray, 1, 4); | ||||
|         int[] expectedArray = { 1, 4, 3, 2, 5 }; | ||||
|         assertArrayEquals(expectedArray, originalArray); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenArray_whenReversingAllElements_thenCorrect() { | ||||
|         int[] originalArray = { 1, 2, 3, 4, 5 }; | ||||
|         ArrayUtils.reverse(originalArray); | ||||
|         int[] expectedArray = { 5, 4, 3, 2, 1 }; | ||||
|         assertArrayEquals(expectedArray, originalArray); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenArray_whenShiftingElementsWithinARange_thenCorrect() { | ||||
|         int[] originalArray = { 1, 2, 3, 4, 5 }; | ||||
|         ArrayUtils.shift(originalArray, 1, 4, 1); | ||||
|         int[] expectedArray = { 1, 4, 2, 3, 5 }; | ||||
|         assertArrayEquals(expectedArray, originalArray); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenArray_whenShiftingAllElements_thenCorrect() { | ||||
|         int[] originalArray = { 1, 2, 3, 4, 5 }; | ||||
|         ArrayUtils.shift(originalArray, 1); | ||||
|         int[] expectedArray = { 5, 1, 2, 3, 4 }; | ||||
|         assertArrayEquals(expectedArray, originalArray); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenArray_whenExtractingElements_thenCorrect() { | ||||
|         int[] oldArray = { 1, 2, 3, 4, 5 }; | ||||
|         int[] newArray = ArrayUtils.subarray(oldArray, 2, 7); | ||||
|         int[] expectedArray = { 3, 4, 5 }; | ||||
|         assertArrayEquals(expectedArray, newArray); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenArray_whenSwapingElementsWithinARange_thenCorrect() { | ||||
|         int[] originalArray = { 1, 2, 3, 4, 5 }; | ||||
|         ArrayUtils.swap(originalArray, 0, 3, 2); | ||||
|         int[] expectedArray = { 4, 5, 3, 1, 2 }; | ||||
|         assertArrayEquals(expectedArray, originalArray); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenArray_whenSwapingElementsAtSpecifiedPositions_thenCorrect() { | ||||
|         int[] originalArray = { 1, 2, 3, 4, 5 }; | ||||
|         ArrayUtils.swap(originalArray, 0, 3); | ||||
|         int[] expectedArray = { 4, 2, 3, 1, 5 }; | ||||
|         assertArrayEquals(expectedArray, originalArray); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,99 @@ | ||||
| package com.baeldung.commons.lang3; | ||||
| 
 | ||||
| import org.apache.commons.lang3.StringUtils; | ||||
| import org.junit.Test; | ||||
| import static org.junit.Assert.assertEquals; | ||||
| import static org.junit.Assert.assertTrue; | ||||
| import static org.junit.Assert.assertFalse; | ||||
| 
 | ||||
| public class StringUtilsTest { | ||||
|     @Test | ||||
|     public void givenString_whenCheckingContainsAny_thenCorrect() { | ||||
|         String string = "baeldung.com"; | ||||
|         boolean contained1 = StringUtils.containsAny(string, 'a', 'b', 'c'); | ||||
|         boolean contained2 = StringUtils.containsAny(string, 'x', 'y', 'z'); | ||||
|         boolean contained3 = StringUtils.containsAny(string, "abc"); | ||||
|         boolean contained4 = StringUtils.containsAny(string, "xyz"); | ||||
|         assertTrue(contained1); | ||||
|         assertFalse(contained2); | ||||
|         assertTrue(contained3); | ||||
|         assertFalse(contained4); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenString_whenCheckingContainsIgnoreCase_thenCorrect() { | ||||
|         String string = "baeldung.com"; | ||||
|         boolean contained = StringUtils.containsIgnoreCase(string, "BAELDUNG"); | ||||
|         assertTrue(contained); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenString_whenCountingMatches_thenCorrect() { | ||||
|         String string = "welcome to www.baeldung.com"; | ||||
|         int charNum = StringUtils.countMatches(string, 'w'); | ||||
|         int stringNum = StringUtils.countMatches(string, "com"); | ||||
|         assertEquals(4, charNum); | ||||
|         assertEquals(2, stringNum); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenString_whenAppendingAndPrependingIfMissing_thenCorrect() { | ||||
|         String string = "baeldung.com"; | ||||
|         String stringWithSuffix = StringUtils.appendIfMissing(string, ".com"); | ||||
|         String stringWithPrefix = StringUtils.prependIfMissing(string, "www."); | ||||
|         assertEquals("baeldung.com", stringWithSuffix); | ||||
|         assertEquals("www.baeldung.com", stringWithPrefix); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenString_whenSwappingCase_thenCorrect() { | ||||
|         String originalString = "baeldung.COM"; | ||||
|         String swappedString = StringUtils.swapCase(originalString); | ||||
|         assertEquals("BAELDUNG.com", swappedString); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenString_whenCapitalizing_thenCorrect() { | ||||
|         String originalString = "baeldung"; | ||||
|         String capitalizedString = StringUtils.capitalize(originalString); | ||||
|         assertEquals("Baeldung", capitalizedString); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenString_whenUncapitalizing_thenCorrect() { | ||||
|         String originalString = "Baeldung"; | ||||
|         String uncapitalizedString = StringUtils.uncapitalize(originalString); | ||||
|         assertEquals("baeldung", uncapitalizedString); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenString_whenReversingCharacters_thenCorrect() { | ||||
|         String originalString = "baeldung"; | ||||
|         String reversedString = StringUtils.reverse(originalString); | ||||
|         assertEquals("gnudleab", reversedString); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenString_whenReversingWithDelimiter_thenCorrect() { | ||||
|         String originalString = "www.baeldung.com"; | ||||
|         String reversedString = StringUtils.reverseDelimited(originalString, '.'); | ||||
|         assertEquals("com.baeldung.www", reversedString); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenString_whenRotatingTwoPositions_thenCorrect() { | ||||
|         String originalString = "baeldung"; | ||||
|         String rotatedString = StringUtils.rotate(originalString, 4); | ||||
|         assertEquals("dungbael", rotatedString); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenTwoStrings_whenComparing_thenCorrect() { | ||||
|         String tutorials = "Baeldung Tutorials"; | ||||
|         String courses = "Baeldung Courses"; | ||||
|         String diff1 = StringUtils.difference(tutorials, courses); | ||||
|         String diff2 = StringUtils.difference(courses, tutorials); | ||||
|         assertEquals("Courses", diff1); | ||||
|         assertEquals("Tutorials", diff2); | ||||
|     } | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user