[BAEL-11403] - Moved articles out of core-java (part 4)

This commit is contained in:
amit2103
2018-12-30 16:30:36 +05:30
parent 9ffbe2bfcb
commit eb4928b972
28 changed files with 19 additions and 99 deletions
@@ -1,116 +0,0 @@
package com.baeldung.decimalformat;
import static org.assertj.core.api.Assertions.assertThat;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
import org.junit.Test;
public class DecimalFormatExamplesUnitTest {
double d = 1234567.89;
@Test
public void givenSimpleDecimal_WhenFormatting_ThenCorrectOutput() {
assertThat(new DecimalFormat("#.##", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
.isEqualTo("1234567.89");
assertThat(new DecimalFormat("0.00", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
.isEqualTo("1234567.89");
assertThat(new DecimalFormat("#########.###", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
.isEqualTo("1234567.89");
assertThat(new DecimalFormat("000000000.000", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
.isEqualTo("001234567.890");
}
@Test
public void givenSmallerDecimalPattern_WhenFormatting_ThenRounding() {
assertThat(new DecimalFormat("#.#", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)).isEqualTo("1234567.9");
assertThat(new DecimalFormat("#", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)).isEqualTo("1234568");
}
@Test
public void givenGroupingSeparator_WhenFormatting_ThenGroupedOutput() {
assertThat(new DecimalFormat("#,###.#", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
.isEqualTo("1,234,567.9");
assertThat(new DecimalFormat("#,###", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
.isEqualTo("1,234,568");
}
@Test
public void givenMixedPattern_WhenFormatting_ThenCorrectOutput() {
assertThat(new DecimalFormat("The # number", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
.isEqualTo("The 1234568 number");
assertThat(new DecimalFormat("The '#' # number", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
.isEqualTo("The # 1234568 number");
}
@Test
public void givenLocales_WhenFormatting_ThenCorrectOutput() {
assertThat(new DecimalFormat("#,###.##", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
.isEqualTo("1,234,567.89");
assertThat(new DecimalFormat("#,###.##", new DecimalFormatSymbols(Locale.ITALIAN)).format(d))
.isEqualTo("1.234.567,89");
assertThat(new DecimalFormat("#,###.##", DecimalFormatSymbols.getInstance(new Locale("it", "IT"))).format(d))
.isEqualTo("1.234.567,89");
}
@Test
public void givenE_WhenFormatting_ThenScientificNotation() {
assertThat(new DecimalFormat("00.#######E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
.isEqualTo("12.3456789E5");
assertThat(new DecimalFormat("000.000000E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
.isEqualTo("123.456789E4");
assertThat(new DecimalFormat("##0.######E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
.isEqualTo("1.23456789E6");
assertThat(new DecimalFormat("###.000000E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
.isEqualTo("1.23456789E6");
}
@Test
public void givenString_WhenParsing_ThenCorrectOutput() throws ParseException {
assertThat(new DecimalFormat("", new DecimalFormatSymbols(Locale.ENGLISH)).parse("1234567.89"))
.isEqualTo(1234567.89);
assertThat(new DecimalFormat("", new DecimalFormatSymbols(Locale.ITALIAN)).parse("1.234.567,89"))
.isEqualTo(1234567.89);
}
@Test
public void givenStringAndBigDecimalFlag_WhenParsing_ThenCorrectOutput() throws ParseException {
NumberFormat nf = new DecimalFormat("", new DecimalFormatSymbols(Locale.ENGLISH));
((DecimalFormat) nf).setParseBigDecimal(true);
assertThat(nf.parse("1234567.89")).isEqualTo(BigDecimal.valueOf(1234567.89));
}
}
@@ -1,175 +0,0 @@
package com.baeldung.java.math;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class MathUnitTest {
@Test
public void whenAbsInteger_thenReturnAbsoluteValue() {
assertEquals(5,Math.abs(-5));
}
@Test
public void whenMaxBetweenTwoValue_thenReturnMaximum() {
assertEquals(10, Math.max(5,10));
}
@Test
public void whenMinBetweenTwoValue_thenReturnMinimum() {
assertEquals(5, Math.min(5,10));
}
@Test
public void whenSignumWithNegativeNumber_thenReturnMinusOne() {
assertEquals(-1, Math.signum(-5), 0);
}
@Test
public void whenCopySignWithNegativeSign_thenReturnNegativeArgument() {
assertEquals(-5, Math.copySign(5,-1), 0);
}
@Test
public void whenPow_thenReturnPoweredValue() {
assertEquals(25, Math.pow(5,2),0);
}
@Test
public void whenSqrt_thenReturnSquareRoot() {
assertEquals(5, Math.sqrt(25),0);
}
@Test
public void whenCbrt_thenReturnCubeRoot() {
assertEquals(5, Math.cbrt(125),0);
}
@Test
public void whenExp_thenReturnEulerNumberRaised() {
assertEquals(2.718, Math.exp(1),0.1);
}
@Test
public void whenExpm1_thenReturnEulerNumberMinusOne() {
assertEquals(1.718, Math.expm1(1),0.1);
}
@Test
public void whenGetExponent_thenReturnUnbiasedExponent() {
assertEquals(8, Math.getExponent(333.3),0);
assertEquals(7, Math.getExponent(222.2f),0);
}
@Test
public void whenLog_thenReturnValue() {
assertEquals(1, Math.log(Math.E),0);
}
@Test
public void whenLog10_thenReturnValue() {
assertEquals(1, Math.log10(10),0);
}
@Test
public void whenLog1p_thenReturnValue() {
assertEquals(1.31, Math.log1p(Math.E),0.1);
}
@Test
public void whenSin_thenReturnValue() {
assertEquals(1, Math.sin(Math.PI/2),0);
}
@Test
public void whenCos_thenReturnValue() {
assertEquals(1, Math.cos(0),0);
}
@Test
public void whenTan_thenReturnValue() {
assertEquals(1, Math.tan(Math.PI/4),0.1);
}
@Test
public void whenAsin_thenReturnValue() {
assertEquals(Math.PI/2, Math.asin(1),0);
}
@Test
public void whenAcos_thenReturnValue() {
assertEquals(Math.PI/2, Math.acos(0),0);
}
@Test
public void whenAtan_thenReturnValue() {
assertEquals(Math.PI/4, Math.atan(1),0);
}
@Test
public void whenAtan2_thenReturnValue() {
assertEquals(Math.PI/4, Math.atan2(1,1),0);
}
@Test
public void whenToDegrees_thenReturnValue() {
assertEquals(180, Math.toDegrees(Math.PI),0);
}
@Test
public void whenToRadians_thenReturnValue() {
assertEquals(Math.PI, Math.toRadians(180),0);
}
@Test
public void whenCeil_thenReturnValue() {
assertEquals(4, Math.ceil(Math.PI),0);
}
@Test
public void whenFloor_thenReturnValue() {
assertEquals(3, Math.floor(Math.PI),0);
}
@Test
public void whenGetExponent_thenReturnValue() {
assertEquals(8, Math.getExponent(333.3),0);
}
@Test
public void whenIEEERemainder_thenReturnValue() {
assertEquals(1.0, Math.IEEEremainder(5,2),0);
}
@Test
public void whenNextAfter_thenReturnValue() {
assertEquals(1.9499999284744263, Math.nextAfter(1.95f,1),0.0000001);
}
@Test
public void whenNextUp_thenReturnValue() {
assertEquals(1.9500002, Math.nextUp(1.95f),0.0000001);
}
@Test
public void whenRint_thenReturnValue() {
assertEquals(2.0, Math.rint(1.95f),0.0);
}
@Test
public void whenRound_thenReturnValue() {
assertEquals(2.0, Math.round(1.95f),0.0);
}
@Test
public void whenScalb_thenReturnValue() {
assertEquals(48, Math.scalb(3, 4),0.0);
}
@Test
public void whenHypot_thenReturnValue() {
assertEquals(5, Math.hypot(4, 3),0.0);
}
}
@@ -1,19 +0,0 @@
package com.baeldung.nth.root.calculator;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.junit.MockitoJUnitRunner;
public class NthRootCalculatorUnitTest {
private NthRootCalculator nthRootCalculator = new NthRootCalculator();
@Test
public void whenBaseIs125AndNIs3_thenNthRootIs5() {
Double result = nthRootCalculator.calculate(125.0, 3.0);
assertEquals(result, (Double) 5.0d);
}
}
@@ -1,34 +0,0 @@
package com.baeldung.nth.root.main;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import static org.junit.Assert.assertEquals;
public class MainUnitTest {
@InjectMocks
private Main main;
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
@Before
public void setUpStreams() {
System.setOut(new PrintStream(outContent));
}
@After
public void restoreStreams() {
System.setOut(originalOut);
}
@Test
public void givenThatTheBaseIs125_andTheExpIs3_whenMainIsCalled_thenTheCorrectResultIsPrinted() {
main.main(new String[]{"125.0", "3.0"});
assertEquals("The 3.0 root of 125.0 equals to 5.0.\n", outContent.toString().replaceAll("\r", ""));
}
}
@@ -1,100 +0,0 @@
package com.baeldung.removingdecimals;
import org.junit.Test;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.concurrent.TimeUnit;
/**
* This benchmark compares some of the approaches to formatting a floating-point
* value into a {@link String} while removing the decimal part.
*
* To run, simply run the {@link RemovingDecimalsManualTest#runBenchmarks()} test
* at the end of this class.
*
* The benchmark takes about 15 minutes to run. Since it is using {@link Mode#Throughput},
* higher numbers mean better performance.
*/
@BenchmarkMode(Mode.Throughput)
@Warmup(iterations = 5)
@Measurement(iterations = 20)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)
public class RemovingDecimalsManualTest {
@Param(value = {"345.56", "345345345.56", "345345345345345345.56"}) double doubleValue;
NumberFormat nf;
DecimalFormat df;
@Setup
public void readyFormatters() {
nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(0);
df = new DecimalFormat("#,###");
}
@Benchmark
public String whenCastToInt_thenValueIsTruncated() {
return String.valueOf((int) doubleValue);
}
@Benchmark
public String whenUsingStringFormat_thenValueIsRounded() {
return String.format("%.0f", doubleValue);
}
@Benchmark
public String whenUsingNumberFormat_thenValueIsRounded() {
nf.setRoundingMode(RoundingMode.HALF_UP);
return nf.format(doubleValue);
}
@Benchmark
public String whenUsingNumberFormatWithFloor_thenValueIsTruncated() {
nf.setRoundingMode(RoundingMode.FLOOR);
return nf.format(doubleValue);
}
@Benchmark
public String whenUsingDecimalFormat_thenValueIsRounded() {
df.setRoundingMode(RoundingMode.HALF_UP);
return df.format(doubleValue);
}
@Benchmark
public String whenUsingDecimalFormatWithFloor_thenValueIsTruncated() {
df.setRoundingMode(RoundingMode.FLOOR);
return df.format(doubleValue);
}
@Benchmark
public String whenUsingBigDecimalDoubleValue_thenValueIsTruncated() {
BigDecimal big = new BigDecimal(doubleValue);
big = big.setScale(0, RoundingMode.FLOOR);
return big.toString();
}
@Benchmark
public String whenUsingBigDecimalDoubleValueWithHalfUp_thenValueIsRounded() {
BigDecimal big = new BigDecimal(doubleValue);
big = big.setScale(0, RoundingMode.HALF_UP);
return big.toString();
}
@Test
public void runBenchmarks() throws Exception {
Options options = new OptionsBuilder()
.include(this.getClass().getSimpleName()).threads(1)
.forks(1).shouldFailOnError(true).shouldDoGC(true)
.jvmArgs("-server").build();
new Runner(options).run();
}
}
@@ -1,95 +0,0 @@
package com.baeldung.removingdecimals;
import org.junit.Test;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
/**
* Tests that demonstrate some different approaches for formatting a
* floating-point value into a {@link String} while removing the decimal part.
*/
public class RemovingDecimalsUnitTest {
private final double doubleValue = 345.56;
@Test
public void whenCastToInt_thenValueIsTruncated() {
String truncated = String.valueOf((int) doubleValue);
assertEquals("345", truncated);
}
@Test
public void givenALargeDouble_whenCastToInt_thenValueIsNotTruncated() {
double outOfIntRange = 6_000_000_000.56;
String truncationAttempt = String.valueOf((int) outOfIntRange);
assertNotEquals("6000000000", truncationAttempt);
}
@Test
public void whenUsingStringFormat_thenValueIsRounded() {
String rounded = String.format("%.0f", doubleValue);
assertEquals("346", rounded);
}
@Test
public void givenALargeDouble_whenUsingStringFormat_thenValueIsStillRounded() {
double outOfIntRange = 6_000_000_000.56;
String rounded = String.format("%.0f", outOfIntRange);
assertEquals("6000000001", rounded);
}
@Test
public void whenUsingNumberFormat_thenValueIsRounded() {
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(0);
nf.setRoundingMode(RoundingMode.HALF_UP);
String rounded = nf.format(doubleValue);
assertEquals("346", rounded);
}
@Test
public void whenUsingNumberFormatWithFloor_thenValueIsTruncated() {
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(0);
nf.setRoundingMode(RoundingMode.FLOOR);
String truncated = nf.format(doubleValue);
assertEquals("345", truncated);
}
@Test
public void whenUsingDecimalFormat_thenValueIsRounded() {
DecimalFormat df = new DecimalFormat("#,###");
df.setRoundingMode(RoundingMode.HALF_UP);
String rounded = df.format(doubleValue);
assertEquals("346", rounded);
}
@Test
public void whenUsingDecimalFormatWithFloor_thenValueIsTruncated() {
DecimalFormat df = new DecimalFormat("#,###");
df.setRoundingMode(RoundingMode.FLOOR);
String truncated = df.format(doubleValue);
assertEquals("345", truncated);
}
@Test
public void whenUsingBigDecimalDoubleValue_thenValueIsTruncated() {
BigDecimal big = new BigDecimal(doubleValue);
big = big.setScale(0, RoundingMode.FLOOR);
String truncated = big.toString();
assertEquals("345", truncated);
}
@Test
public void whenUsingBigDecimalDoubleValueWithHalfUp_thenValueIsRounded() {
BigDecimal big = new BigDecimal(doubleValue);
big = big.setScale(0, RoundingMode.HALF_UP);
String truncated = big.toString();
assertEquals("346", truncated);
}
}
@@ -1,60 +0,0 @@
package com.baeldung.simpledateformat;
import org.junit.Test;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.logging.Logger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class SimpleDateFormatUnitTest {
private static final Logger logger = Logger.getLogger(SimpleDateFormatUnitTest.class.getName());
@Test
public void givenSpecificDate_whenFormatted_thenCheckFormatCorrect() throws Exception {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
assertEquals("24-05-1977", formatter.format(new Date(233345223232L)));
}
@Test
public void givenSpecificDate_whenFormattedUsingDateFormat_thenCheckFormatCorrect() throws Exception {
DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
assertEquals("5/24/77", formatter.format(new Date(233345223232L)));
}
@Test
public void givenStringDate_whenParsed_thenCheckDateCorrect() throws Exception{
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
formatter.setTimeZone(TimeZone.getTimeZone("Europe/London"));
Date myDate = new Date(233276400000L);
Date parsedDate = formatter.parse("24-05-1977");
assertEquals(myDate.getTime(), parsedDate.getTime());
}
@Test
public void givenFranceLocale_whenFormatted_thenCheckFormatCorrect() throws Exception{
SimpleDateFormat franceDateFormatter = new SimpleDateFormat("EEEEE dd-MMMMMMM-yyyy", Locale.FRANCE);
Date myWednesday = new Date(1539341312904L);
assertTrue(franceDateFormatter.format(myWednesday).startsWith("vendredi"));
}
@Test
public void given2TimeZones_whenFormatted_thenCheckTimeDifference() throws Exception {
Date now = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE dd-MMM-yy HH:mm:ssZ");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Europe/London"));
logger.info(simpleDateFormat.format(now));
//change the date format
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
logger.info(simpleDateFormat.format(now));
}
}
@@ -1,110 +0,0 @@
/**
*
*/
package com.baeldung.string;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
/**
* @author swpraman
*
*/
public class AppendCharAtPositionXUnitTest {
private AppendCharAtPositionX appendCharAtPosition = new AppendCharAtPositionX();
private String word = "Titanc";
private char letter = 'i';
@Test
public void whenUsingCharacterArrayAndCharacterAddedAtBeginning_shouldAddCharacter() {
assertEquals("iTitanc", appendCharAtPosition.addCharUsingCharArray(word, letter, 0));
}
@Test
public void whenUsingSubstringAndCharacterAddedAtBeginning_shouldAddCharacter() {
assertEquals("iTitanc", appendCharAtPosition.addCharUsingSubstring(word, letter, 0));
}
@Test
public void whenUsingStringBuilderAndCharacterAddedAtBeginning_shouldAddCharacter() {
assertEquals("iTitanc", appendCharAtPosition.addCharUsingStringBuilder(word, letter, 0));
}
@Test
public void whenUsingCharacterArrayAndCharacterAddedAtMiddle_shouldAddCharacter() {
assertEquals("Titianc", appendCharAtPosition.addCharUsingCharArray(word, letter, 3));
}
@Test
public void whenUsingSubstringAndCharacterAddedAtMiddle_shouldAddCharacter() {
assertEquals("Titianc", appendCharAtPosition.addCharUsingSubstring(word, letter, 3));
}
@Test
public void whenUsingStringBuilderAndCharacterAddedAtMiddle_shouldAddCharacter() {
assertEquals("Titianc", appendCharAtPosition.addCharUsingStringBuilder(word, letter, 3));
}
@Test
public void whenUsingCharacterArrayAndCharacterAddedAtEnd_shouldAddCharacter() {
assertEquals("Titanci", appendCharAtPosition.addCharUsingCharArray(word, letter, word.length()));
}
@Test
public void whenUsingSubstringAndCharacterAddedAtEnd_shouldAddCharacter() {
assertEquals("Titanci", appendCharAtPosition.addCharUsingSubstring(word, letter, word.length()));
}
@Test
public void whenUsingStringBuilderAndCharacterAddedAtEnd_shouldAddCharacter() {
assertEquals("Titanci", appendCharAtPosition.addCharUsingStringBuilder(word, letter, word.length()));
}
@Test(expected=IllegalArgumentException.class)
public void whenUsingCharacterArrayAndCharacterAddedAtNegativePosition_shouldThrowException() {
appendCharAtPosition.addCharUsingStringBuilder(word, letter, -1);
}
@Test(expected=IllegalArgumentException.class)
public void whenUsingSubstringAndCharacterAddedAtNegativePosition_shouldThrowException() {
appendCharAtPosition.addCharUsingStringBuilder(word, letter, -1);
}
@Test(expected=IllegalArgumentException.class)
public void whenUsingStringBuilderAndCharacterAddedAtNegativePosition_shouldThrowException() {
appendCharAtPosition.addCharUsingStringBuilder(word, letter, -1);
}
@Test(expected=IllegalArgumentException.class)
public void whenUsingCharacterArrayAndCharacterAddedAtInvalidPosition_shouldThrowException() {
appendCharAtPosition.addCharUsingStringBuilder(word, letter, word.length() + 2);
}
@Test(expected=IllegalArgumentException.class)
public void whenUsingSubstringAndCharacterAddedAtInvalidPosition_shouldThrowException() {
appendCharAtPosition.addCharUsingStringBuilder(word, letter, word.length() + 2);
}
@Test(expected=IllegalArgumentException.class)
public void whenUsingStringBuilderAndCharacterAddedAtInvalidPosition_shouldThrowException() {
appendCharAtPosition.addCharUsingStringBuilder(word, letter, word.length() + 2);
}
@Test(expected=IllegalArgumentException.class)
public void whenUsingCharacterArrayAndCharacterAddedAtPositionXAndStringIsNull_shouldThrowException() {
appendCharAtPosition.addCharUsingStringBuilder(null, letter, 3);
}
@Test(expected=IllegalArgumentException.class)
public void whenUsingSubstringAndCharacterAddedAtPositionXAndStringIsNull_shouldThrowException() {
appendCharAtPosition.addCharUsingStringBuilder(null, letter, 3);
}
@Test(expected=IllegalArgumentException.class)
public void whenUsingStringBuilderAndCharacterAddedAtPositionXAndStringIsNull_shouldThrowException() {
appendCharAtPosition.addCharUsingStringBuilder(null, letter, 3);
}
}
@@ -1,83 +0,0 @@
package com.baeldung.string;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class StringReplaceAndRemoveUnitTest {
@Test
public void givenTestStrings_whenReplace_thenProcessedString() {
String master = "Hello World Baeldung!";
String target = "Baeldung";
String replacement = "Java";
String processed = master.replace(target, replacement);
assertTrue(processed.contains(replacement));
assertFalse(processed.contains(target));
}
@Test
public void givenTestStrings_whenReplaceAll_thenProcessedString() {
String master2 = "Welcome to Baeldung, Hello World Baeldung";
String regexTarget= "(Baeldung)$";
String replacement = "Java";
String processed2 = master2.replaceAll(regexTarget, replacement);
assertTrue(processed2.endsWith("Java"));
}
@Test
public void givenTestStrings_whenStringBuilderMethods_thenProcessedString() {
String master = "Hello World Baeldung!";
String target = "Baeldung";
String replacement = "Java";
int startIndex = master.indexOf(target);
int stopIndex = startIndex + target.length();
StringBuilder builder = new StringBuilder(master);
builder.delete(startIndex, stopIndex);
assertFalse(builder.toString().contains(target));
builder.replace(startIndex, stopIndex, replacement);
assertTrue(builder.toString().contains(replacement));
}
@Test
public void givenTestStrings_whenStringUtilsMethods_thenProcessedStrings() {
String master = "Hello World Baeldung!";
String target = "Baeldung";
String replacement = "Java";
String processed = StringUtils.replace(master, target, replacement);
assertTrue(processed.contains(replacement));
String master2 = "Hello World Baeldung!";
String target2 = "baeldung";
String processed2 = StringUtils.replaceIgnoreCase(master2, target2, replacement);
assertFalse(processed2.contains(target));
}
}
@@ -1,22 +0,0 @@
package com.baeldung.zoneddatetime;
import static org.junit.Assert.assertTrue;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import org.junit.Test;
public class OffsetDateTimeExampleUnitTest {
OffsetDateTimeExample offsetDateTimeExample = new OffsetDateTimeExample();
@Test
public void givenZoneOffset_whenGetCurrentTime_thenResultHasZone() {
String offset = "+02:00";
OffsetDateTime time = offsetDateTimeExample.getCurrentTimeByZoneOffset(offset);
assertTrue(time.getOffset()
.equals(ZoneOffset.of(offset)));
}
}
@@ -1,22 +0,0 @@
package com.baeldung.zoneddatetime;
import static org.junit.Assert.assertTrue;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import org.junit.Test;
public class OffsetTimeExampleUnitTest {
OffsetTimeExample offsetTimeExample = new OffsetTimeExample();
@Test
public void givenZoneOffset_whenGetCurrentTime_thenResultHasZone() {
String offset = "+02:00";
OffsetTime time = offsetTimeExample.getCurrentTimeByZoneOffset(offset);
assertTrue(time.getOffset()
.equals(ZoneOffset.of(offset)));
}
}
@@ -1,33 +0,0 @@
package com.baeldung.zoneddatetime;
import static org.junit.Assert.assertTrue;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import org.junit.Test;
public class ZoneDateTimeExampleUnitTest {
ZoneDateTimeExample zoneDateTimeExample = new ZoneDateTimeExample();
@Test
public void givenZone_whenGetCurrentTime_thenResultHasZone() {
String zone = "Europe/Berlin";
ZonedDateTime time = zoneDateTimeExample.getCurrentTimeByZoneId(zone);
assertTrue(time.getZone()
.equals(ZoneId.of(zone)));
}
@Test
public void givenZones_whenConvertDateByZone_thenGetConstantDiff() {
String sourceZone = "Europe/Berlin";
String destZone = "Asia/Tokyo";
ZonedDateTime sourceDate = zoneDateTimeExample.getCurrentTimeByZoneId(sourceZone);
ZonedDateTime destDate = zoneDateTimeExample.convertZonedDateTime(sourceDate, destZone);
assertTrue(sourceDate.toInstant()
.compareTo(destDate.toInstant()) == 0);
}
}