Merge branch 'eugenp:master' into master
This commit is contained in:
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.arrayindex;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
class ArrayIndex {
|
||||
static int forLoop(int[] numbers, int target) {
|
||||
for (int index = 0; index < numbers.length; index++) {
|
||||
if (numbers[index] == target) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int listIndexOf(Integer[] numbers, int target) {
|
||||
List<Integer> list = Arrays.asList(numbers);
|
||||
return list.indexOf(target);
|
||||
}
|
||||
|
||||
static int intStream(int[] numbers, int target) {
|
||||
return IntStream.range(0, numbers.length)
|
||||
.filter(i -> numbers[i] == target)
|
||||
.findFirst()
|
||||
.orElse(-1);
|
||||
}
|
||||
}
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
package com.baeldung.arrayindex;
|
||||
|
||||
import static com.baeldung.arrayindex.ArrayIndex.forLoop;
|
||||
import static com.baeldung.arrayindex.ArrayIndex.intStream;
|
||||
import static com.baeldung.arrayindex.ArrayIndex.listIndexOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.google.common.primitives.Ints;
|
||||
|
||||
class ArrayIndexUnitTest {
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseForLoop_thenWillGetElementIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(2, forLoop(numbers, 30));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseForLoop_thenWillGetElementMinusOneIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(-1, forLoop(numbers, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseIndexOf_thenWillGetElementIndex() {
|
||||
Integer[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(2, listIndexOf(numbers, 30));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseIndexOf_thenWillGetElementMinusOneIndex() {
|
||||
Integer[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(-1, listIndexOf(numbers, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseIntStream_thenWillGetElementIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(2, intStream(numbers, 30));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseIntStream_thenWillGetElementMinusOneIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(-1, intStream(numbers, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseBinarySearch_thenWillGetElementIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(2, Arrays.binarySearch(numbers, 30));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseBinarySearch_thenWillGetUpperBoundMinusIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(-6, Arrays.binarySearch(numbers, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseBinarySearch_thenWillGetInArrayMinusIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(-2, Arrays.binarySearch(numbers, 15));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseBinarySearch_thenWillGetLowerBoundMinusIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(-1, Arrays.binarySearch(numbers, -15));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseApacheCommons_thenWillGetElementIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(2, ArrayUtils.indexOf(numbers, 30));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseApacheCommonsStartingFromIndex_thenWillGetNegativeIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(-1, ArrayUtils.indexOf(numbers, 30, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseApacheCommons_thenWillGetElementMinusOneIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(-1, ArrayUtils.indexOf(numbers, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseGuavaInts_thenWillGetElementIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(2, Ints.indexOf(numbers, 30));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseGuavaInts_thenWillGetElementMinusOneIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(-1, Ints.indexOf(numbers, 100));
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.charandstring;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class DifferenceBetweenCharAndStringUnitTest {
|
||||
|
||||
@Test
|
||||
void whenPlusTwoChars_thenGetSumAsInteger() {
|
||||
char h = 'H'; // the value is 72
|
||||
char i = 'i'; // the value is 105
|
||||
assertEquals(177, h + i);
|
||||
assertInstanceOf(Integer.class, h + i);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenPlusTwoStrings_thenConcatenateThem() {
|
||||
String i = "i";
|
||||
String h = "H";
|
||||
assertEquals("Hi", h + i);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenPlusCharsAndStrings_thenGetExpectedValues() {
|
||||
char c = 'C';
|
||||
assertEquals("C", "" + c);
|
||||
|
||||
char h = 'H'; // the value is 72
|
||||
char i = 'i'; // the value is 105
|
||||
assertEquals("Hi", "" + h + i);
|
||||
assertEquals("Hi", h + "" + i);
|
||||
assertEquals("177", h + i + "");
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenStringChars_thenGetCharArray() {
|
||||
char h = 'h';
|
||||
char e = 'e';
|
||||
char l = 'l';
|
||||
char o = 'o';
|
||||
|
||||
String hello = "hello";
|
||||
assertEquals(h, hello.charAt(0));
|
||||
assertEquals(e, hello.charAt(1));
|
||||
assertEquals(l, hello.charAt(2));
|
||||
assertEquals(l, hello.charAt(3));
|
||||
assertEquals(o, hello.charAt(4));
|
||||
|
||||
char[] chars = new char[] { h, e, l, l, o };
|
||||
char[] charsFromString = hello.toCharArray();
|
||||
assertArrayEquals(chars, charsFromString);
|
||||
}
|
||||
}
|
||||
@@ -6,4 +6,4 @@ This module contains articles about core Java input/output(IO) APIs.
|
||||
- [Constructing a Relative Path From Two Absolute Paths in Java](https://www.baeldung.com/java-relative-path-absolute)
|
||||
- [Java Scanner Taking a Character Input](https://www.baeldung.com/java-scanner-character-input)
|
||||
- [Get the Desktop Path in Java](https://www.baeldung.com/java-desktop-path)
|
||||
|
||||
- [Integer.parseInt(scanner.nextLine()) and scanner.nextInt() in Java](https://www.baeldung.com/java-scanner-integer)
|
||||
|
||||
+2
-2
@@ -49,7 +49,7 @@ public class JavaInputStreamToXUnitTest {
|
||||
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());
|
||||
|
||||
final StringBuilder textBuilder = new StringBuilder();
|
||||
try (Reader reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName(StandardCharsets.UTF_8.name())))) {
|
||||
try (Reader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
|
||||
int c;
|
||||
while ((c = reader.read()) != -1) {
|
||||
textBuilder.append((char) c);
|
||||
@@ -63,7 +63,7 @@ public class JavaInputStreamToXUnitTest {
|
||||
final String originalString = randomAlphabetic(DEFAULT_SIZE);
|
||||
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());
|
||||
|
||||
final String text = new BufferedReader(new InputStreamReader(inputStream, Charset.forName(StandardCharsets.UTF_8.name())))
|
||||
final String text = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))
|
||||
.lines()
|
||||
.collect(Collectors.joining("\n"));
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
## Core Java Lang OOP - Constructors - Part 2
|
||||
|
||||
This module contains article about constructors in Java
|
||||
|
||||
### Relevant Articles:
|
||||
- [Different Ways to Create an Object in Java](https://www.baeldung.com/java-different-ways-to-create-objects)
|
||||
- More articles: [[<-- Prev]](/core-java-modules/core-java-lang-oop-constructors)
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
||||
<artifactId>core-java-lang-oop-constructors-2</artifactId>
|
||||
<name>core-java-lang-oop-constructors-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
</project>
|
||||
@@ -13,4 +13,4 @@ This module contains article about constructors in Java
|
||||
- [Constructor Specification in Java](https://www.baeldung.com/java-constructor-specification)
|
||||
- [Static vs. Instance Initializer Block in Java](https://www.baeldung.com/java-static-instance-initializer-blocks)
|
||||
- [Accessing Private Constructor in Java](https://www.baeldung.com/java-private-constructor-access)
|
||||
- [Different Ways to Create an Object in Java](https://www.baeldung.com/java-different-ways-to-create-objects)
|
||||
- More articles: [[next -->]](/core-java-modules/core-java-lang-oop-constructors-2)
|
||||
@@ -11,3 +11,4 @@ This module contains articles about methods in Java
|
||||
- [The Covariant Return Type in Java](https://www.baeldung.com/java-covariant-return-type)
|
||||
- [Does a Method’s Signature Include the Return Type in Java?](https://www.baeldung.com/java-method-signature-return-type)
|
||||
- [Solving the Hide Utility Class Public Constructor Sonar Warning](https://www.baeldung.com/java-sonar-hide-implicit-constructor)
|
||||
- [Best Practices for Passing Many Arguments to a Method in Java](https://www.baeldung.com/java-best-practices-many-parameters-method)
|
||||
|
||||
@@ -3,3 +3,4 @@
|
||||
- [Validating URL in Java](https://www.baeldung.com/java-validate-url)
|
||||
- [Validating IPv4 Address in Java](https://www.baeldung.com/java-validate-ipv4-address)
|
||||
- [Download a Webpage in Java](https://www.baeldung.com/java-download-webpage)
|
||||
- [URL Query Manipulation in Java](https://www.baeldung.com/java-url-query-manipulation)
|
||||
|
||||
@@ -13,3 +13,4 @@ This module contains articles about performance of Java applications
|
||||
- [Capturing a Java Thread Dump](https://www.baeldung.com/java-thread-dump)
|
||||
- [JMX Ports](https://www.baeldung.com/jmx-ports)
|
||||
- [Calling JMX MBean Method From a Shell Script](https://www.baeldung.com/jmx-mbean-shell-access)
|
||||
- [External Debugging With JMXTerm](https://www.baeldung.com/java-jmxterm-external-debugging)
|
||||
|
||||
@@ -5,4 +5,5 @@
|
||||
- [Converting Camel Case and Title Case to Words in Java](https://www.baeldung.com/java-camel-case-title-case-to-words)
|
||||
- [How to Use Regular Expressions to Replace Tokens in Strings in Java](https://www.baeldung.com/java-regex-token-replacement)
|
||||
- [Creating a Java Array from Regular Expression Matches](https://www.baeldung.com/java-array-regex-matches)
|
||||
- [Getting the Text That Follows After the Regex Match in Java](https://www.baeldung.com/java-regex-text-after-match)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-regex)
|
||||
|
||||
@@ -10,3 +10,4 @@ This module contains articles about string-related algorithms.
|
||||
- [Check if the First Letter of a String is Uppercase](https://www.baeldung.com/java-check-first-letter-uppercase)
|
||||
- [Find the First Non Repeating Character in a String in Java](https://www.baeldung.com/java-find-the-first-non-repeating-character)
|
||||
- [Find the First Embedded Occurrence of an Integer in a Java String](https://www.baeldung.com/java-string-find-embedded-integer)
|
||||
- [Find the Most Frequent Characters in a String](https://www.baeldung.com/java-string-find-most-frequent-characters)
|
||||
|
||||
+37
-1
@@ -1,5 +1,9 @@
|
||||
package com.baeldung.reverse;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class ReverseStringExamples {
|
||||
@@ -46,11 +50,43 @@ public class ReverseStringExamples {
|
||||
}
|
||||
|
||||
return output.toString()
|
||||
.trim();
|
||||
.trim();
|
||||
}
|
||||
|
||||
public static String reverseTheOrderOfWordsUsingApacheCommons(String sentence) {
|
||||
return StringUtils.reverseDelimited(sentence, ' ');
|
||||
}
|
||||
|
||||
public static String reverseUsingIntStreamRangeMethod(String str) {
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
char[] charArray = str.toCharArray();
|
||||
return IntStream.range(0, str.length())
|
||||
.mapToObj(i -> charArray[str.length() - i - 1])
|
||||
.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
|
||||
.toString();
|
||||
}
|
||||
|
||||
public static String reverseUsingStreamOfMethod(String str) {
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Stream.of(str)
|
||||
.map(string -> new StringBuilder(string).reverse())
|
||||
.collect(Collectors.joining());
|
||||
}
|
||||
|
||||
public static String reverseUsingCharsMethod(String str) {
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return str.chars()
|
||||
.mapToObj(c -> (char) c)
|
||||
.reduce("", (a, b) -> b + a, (a2, b2) -> b2 + a2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+41
-7
@@ -1,10 +1,11 @@
|
||||
package com.baeldung.reverse;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ReverseStringExamplesUnitTest {
|
||||
|
||||
private static final String STRING_INPUT = "cat";
|
||||
@@ -19,7 +20,7 @@ public class ReverseStringExamplesUnitTest {
|
||||
String reversedEmpty = ReverseStringExamples.reverse(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(STRING_INPUT_REVERSED, reversed);
|
||||
assertEquals(null, reversedNull);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
@@ -30,7 +31,7 @@ public class ReverseStringExamplesUnitTest {
|
||||
String reversedEmpty = ReverseStringExamples.reverseUsingStringBuilder(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(STRING_INPUT_REVERSED, reversed);
|
||||
assertEquals(null, reversedNull);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
@@ -41,7 +42,7 @@ public class ReverseStringExamplesUnitTest {
|
||||
String reversedEmpty = ReverseStringExamples.reverseUsingApacheCommons(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(STRING_INPUT_REVERSED, reversed);
|
||||
assertEquals(null, reversedNull);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
@@ -52,7 +53,7 @@ public class ReverseStringExamplesUnitTest {
|
||||
String reversedEmpty = ReverseStringExamples.reverseTheOrderOfWords(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(REVERSED_WORDS_SENTENCE, reversed);
|
||||
assertEquals(null, reversedNull);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
@@ -63,7 +64,40 @@ public class ReverseStringExamplesUnitTest {
|
||||
String reversedEmpty = ReverseStringExamples.reverseTheOrderOfWordsUsingApacheCommons(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(REVERSED_WORDS_SENTENCE, reversed);
|
||||
assertEquals(null, reversedNull);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReverseStringUsingIntStreamRangeMethod_ThenCorrectStringIsReturned() {
|
||||
String reversed = ReverseStringExamples.reverseUsingIntStreamRangeMethod(STRING_INPUT);
|
||||
String reversedNull = ReverseStringExamples.reverseUsingIntStreamRangeMethod(null);
|
||||
String reversedEmpty = ReverseStringExamples.reverseUsingIntStreamRangeMethod(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(STRING_INPUT_REVERSED, reversed);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReverseStringUsingCharsMethod_ThenCorrectStringIsReturned() {
|
||||
String reversed = ReverseStringExamples.reverseUsingCharsMethod(STRING_INPUT);
|
||||
String reversedNull = ReverseStringExamples.reverseUsingCharsMethod(null);
|
||||
String reversedEmpty = ReverseStringExamples.reverseUsingCharsMethod(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(STRING_INPUT_REVERSED, reversed);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReverseStringUsingStreamOfMethod_ThenCorrectStringIsReturned() {
|
||||
String reversed = ReverseStringExamples.reverseUsingStreamOfMethod(STRING_INPUT);
|
||||
String reversedNull = ReverseStringExamples.reverseUsingStreamOfMethod(null);
|
||||
String reversedEmpty = ReverseStringExamples.reverseUsingStreamOfMethod(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(STRING_INPUT_REVERSED, reversed);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.firstchardigit;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.google.common.base.CharMatcher;
|
||||
|
||||
public class FirstCharDigit {
|
||||
|
||||
public static boolean checkUsingCharAtMethod(String str) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char c = str.charAt(0);
|
||||
return c >= '0' && c <= '9';
|
||||
}
|
||||
|
||||
public static boolean checkUsingIsDigitMethod(String str) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Character.isDigit(str.charAt(0));
|
||||
}
|
||||
|
||||
public static boolean checkUsingPatternClass(String str) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Pattern.compile("^[0-9].*")
|
||||
.matcher(str)
|
||||
.matches();
|
||||
}
|
||||
|
||||
public static boolean checkUsingMatchesMethod(String str) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return str.matches("^[0-9].*");
|
||||
}
|
||||
|
||||
public static boolean checkUsingCharMatcherInRangeMethod(String str) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return CharMatcher.inRange('0', '9')
|
||||
.matches(str.charAt(0));
|
||||
}
|
||||
|
||||
public static boolean checkUsingCharMatcherForPredicateMethod(String str) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return CharMatcher.forPredicate(Character::isDigit)
|
||||
.matches(str.charAt(0));
|
||||
}
|
||||
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.firstchardigit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class FirstCharDigitUnitTest {
|
||||
|
||||
@Test
|
||||
void givenString_whenUsingCharAtMethod_thenSuccess() {
|
||||
assertTrue(FirstCharDigit.checkUsingCharAtMethod("12 years"));
|
||||
assertFalse(FirstCharDigit.checkUsingCharAtMethod("years"));
|
||||
assertFalse(FirstCharDigit.checkUsingCharAtMethod(""));
|
||||
assertFalse(FirstCharDigit.checkUsingCharAtMethod(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenString_whenUsingIsDigitMethod_thenSuccess() {
|
||||
assertTrue(FirstCharDigit.checkUsingIsDigitMethod("10 cm"));
|
||||
assertFalse(FirstCharDigit.checkUsingIsDigitMethod("cm"));
|
||||
assertFalse(FirstCharDigit.checkUsingIsDigitMethod(""));
|
||||
assertFalse(FirstCharDigit.checkUsingIsDigitMethod(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenString_whenUsingPatternClass_thenSuccess() {
|
||||
assertTrue(FirstCharDigit.checkUsingPatternClass("1 kg"));
|
||||
assertFalse(FirstCharDigit.checkUsingPatternClass("kg"));
|
||||
assertFalse(FirstCharDigit.checkUsingPatternClass(""));
|
||||
assertFalse(FirstCharDigit.checkUsingPatternClass(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenString_whenUsingMatchesMethod_thenSuccess() {
|
||||
assertTrue(FirstCharDigit.checkUsingMatchesMethod("123"));
|
||||
assertFalse(FirstCharDigit.checkUsingMatchesMethod("ABC"));
|
||||
assertFalse(FirstCharDigit.checkUsingMatchesMethod(""));
|
||||
assertFalse(FirstCharDigit.checkUsingMatchesMethod(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenString_whenUsingCharMatcherInRangeMethod_thenSuccess() {
|
||||
assertTrue(FirstCharDigit.checkUsingCharMatcherInRangeMethod("2023"));
|
||||
assertFalse(FirstCharDigit.checkUsingCharMatcherInRangeMethod("abc"));
|
||||
assertFalse(FirstCharDigit.checkUsingCharMatcherInRangeMethod(""));
|
||||
assertFalse(FirstCharDigit.checkUsingCharMatcherInRangeMethod(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenString_whenUsingCharMatcherForPredicateMethod_thenSuccess() {
|
||||
assertTrue(FirstCharDigit.checkUsingCharMatcherForPredicateMethod("100"));
|
||||
assertFalse(FirstCharDigit.checkUsingCharMatcherForPredicateMethod("abdo"));
|
||||
assertFalse(FirstCharDigit.checkUsingCharMatcherForPredicateMethod(""));
|
||||
assertFalse(FirstCharDigit.checkUsingCharMatcherForPredicateMethod(null));
|
||||
}
|
||||
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.stringwithquotes;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class PrintQuotesAroundAStringUnitTest {
|
||||
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
||||
private final PrintStream originalOut = System.out;
|
||||
|
||||
@BeforeEach
|
||||
void replaceOut() {
|
||||
System.setOut(new PrintStream(outContent));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void restoreOut() {
|
||||
System.setOut(originalOut);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenWrappingAStringWithEscapedQuote_thenGetExpectedResult() {
|
||||
String theySay = "All Java programmers are cute!";
|
||||
String quoted = "\"" + theySay + "\"";
|
||||
|
||||
System.out.println(quoted);
|
||||
|
||||
//assertion
|
||||
String expected = "\"All Java programmers are cute!\"\n";
|
||||
assertEquals(expected, outContent.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenCallingReplaceAll_thenGetExpectedResult() {
|
||||
String theySay = "Can you write Java code?";
|
||||
String quoted = theySay.replaceAll("^|$", "\"");
|
||||
|
||||
System.out.println(quoted);
|
||||
|
||||
//assertion
|
||||
String expected = "\"Can you write Java code?\"\n";
|
||||
assertEquals(expected, outContent.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenWrappingAStringWithQuoteChar_thenGetExpectedResult() {
|
||||
String weSay = "Yes, we can write beautiful Java codes!";
|
||||
String quoted = '"' + weSay + '"';
|
||||
System.out.println(quoted);
|
||||
|
||||
//assertion
|
||||
String expected = "\"Yes, we can write beautiful Java codes!\"\n";
|
||||
assertEquals(expected, outContent.toString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user