parent
026c9717ce
commit
178af4e049
|
@ -14,7 +14,7 @@ import org.hamcrest.collection.IsIterableContainingInOrder;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class FlattenNestedListUnitTest {
|
public class FlattenNestedListUnitTest {
|
||||||
List<List<String>> lol = asList(asList("one:one"), asList("two:one", "two:two", "two:three"), asList("three:one", "three:two", "three:three", "three:four"));
|
private List<List<String>> lol = asList(asList("one:one"), asList("two:one", "two:two", "two:three"), asList("three:one", "three:two", "three:three", "three:four"));
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNestedList_thenFlattenImperatively() {
|
public void givenNestedList_thenFlattenImperatively() {
|
||||||
|
@ -36,13 +36,13 @@ public class FlattenNestedListUnitTest {
|
||||||
assertThat(ls, IsIterableContainingInOrder.contains("one:one", "two:one", "two:two", "two:three", "three:one", "three:two", "three:three", "three:four"));
|
assertThat(ls, IsIterableContainingInOrder.contains("one:one", "two:one", "two:two", "two:three", "three:one", "three:two", "three:three", "three:four"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> List<T> flattenListOfListsImperatively(List<List<T>> list) {
|
private <T> List<T> flattenListOfListsImperatively(List<List<T>> list) {
|
||||||
List<T> ls = new ArrayList<>();
|
List<T> ls = new ArrayList<>();
|
||||||
list.forEach(ls::addAll);
|
list.forEach(ls::addAll);
|
||||||
return ls;
|
return ls;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> List<T> flattenListOfListsStream(List<List<T>> list) {
|
private <T> List<T> flattenListOfListsStream(List<List<T>> list) {
|
||||||
return list.stream().flatMap(Collection::stream).collect(Collectors.toList());
|
return list.stream().flatMap(Collection::stream).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,19 @@
|
||||||
package com.baeldung.list.listoflist;
|
package com.baeldung.list.listoflist;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class ListOfListsUnitTest {
|
public class ListOfListsUnitTest {
|
||||||
|
|
||||||
private List<ArrayList<? extends Stationery>> listOfLists = new ArrayList<ArrayList<? extends Stationery>>();
|
private List<List<? extends Stationery>> listOfLists = new ArrayList<>();
|
||||||
private ArrayList<Pen> penList = new ArrayList<>();
|
private List<Pen> penList = new ArrayList<>();
|
||||||
private ArrayList<Pencil> pencilList = new ArrayList<>();
|
private List<Pencil> pencilList = new ArrayList<>();
|
||||||
private ArrayList<Rubber> rubberList = new ArrayList<>();
|
private List<Rubber> rubberList = new ArrayList<>();
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Before
|
@Before
|
||||||
|
@ -29,11 +30,11 @@ public class ListOfListsUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenListOfLists_thenCheckNames() {
|
public void givenListOfLists_thenCheckNames() {
|
||||||
assertEquals("Pen 1", ((Pen) listOfLists.get(0)
|
assertEquals("Pen 1", ((Pen) listOfLists.get(0)
|
||||||
.get(0)).getName());
|
.get(0)).getName());
|
||||||
assertEquals("Pencil 1", ((Pencil) listOfLists.get(1)
|
assertEquals("Pencil 1", ((Pencil) listOfLists.get(1)
|
||||||
.get(0)).getName());
|
.get(0)).getName());
|
||||||
assertEquals("Rubber 1", ((Rubber) listOfLists.get(2)
|
assertEquals("Rubber 1", ((Rubber) listOfLists.get(2)
|
||||||
.get(0)).getName());
|
.get(0)).getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
|
@ -43,10 +44,10 @@ public class ListOfListsUnitTest {
|
||||||
((ArrayList<Pencil>) listOfLists.get(1)).remove(0);
|
((ArrayList<Pencil>) listOfLists.get(1)).remove(0);
|
||||||
listOfLists.remove(1);
|
listOfLists.remove(1);
|
||||||
assertEquals("Rubber 1", ((Rubber) listOfLists.get(1)
|
assertEquals("Rubber 1", ((Rubber) listOfLists.get(1)
|
||||||
.get(0)).getName());
|
.get(0)).getName());
|
||||||
listOfLists.remove(0);
|
listOfLists.remove(0);
|
||||||
assertEquals("Rubber 1", ((Rubber) listOfLists.get(0)
|
assertEquals("Rubber 1", ((Rubber) listOfLists.get(0)
|
||||||
.get(0)).getName());
|
.get(0)).getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -60,17 +61,17 @@ public class ListOfListsUnitTest {
|
||||||
ArrayList<Rubber> rubbers = new ArrayList<>();
|
ArrayList<Rubber> rubbers = new ArrayList<>();
|
||||||
rubbers.add(new Rubber("Rubber 1"));
|
rubbers.add(new Rubber("Rubber 1"));
|
||||||
rubbers.add(new Rubber("Rubber 2"));
|
rubbers.add(new Rubber("Rubber 2"));
|
||||||
|
|
||||||
List<ArrayList<? extends Stationery>> list = new ArrayList<ArrayList<? extends Stationery>>();
|
List<ArrayList<? extends Stationery>> list = new ArrayList<ArrayList<? extends Stationery>>();
|
||||||
list.add(pens);
|
list.add(pens);
|
||||||
list.add(pencils);
|
list.add(pencils);
|
||||||
list.add(rubbers);
|
list.add(rubbers);
|
||||||
|
|
||||||
assertEquals("Pen 1", ((Pen) list.get(0)
|
assertEquals("Pen 1", ((Pen) list.get(0)
|
||||||
.get(0)).getName());
|
.get(0)).getName());
|
||||||
assertEquals("Pencil 1", ((Pencil) list.get(1)
|
assertEquals("Pencil 1", ((Pencil) list.get(1)
|
||||||
.get(0)).getName());
|
.get(0)).getName());
|
||||||
assertEquals("Rubber 1", ((Rubber) list.get(2)
|
assertEquals("Rubber 1", ((Rubber) list.get(2)
|
||||||
.get(0)).getName());
|
.get(0)).getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,7 @@ public class MappedByteBufferUnitTest {
|
||||||
|
|
||||||
//when
|
//when
|
||||||
try (FileChannel fileChannel = (FileChannel) Files.newByteChannel(pathToWrite,
|
try (FileChannel fileChannel = (FileChannel) Files.newByteChannel(pathToWrite,
|
||||||
EnumSet.of(StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING))) {
|
EnumSet.of(StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING))) {
|
||||||
MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, charBuffer.length());
|
MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, charBuffer.length());
|
||||||
|
|
||||||
if (mappedByteBuffer != null) {
|
if (mappedByteBuffer != null) {
|
||||||
|
@ -61,7 +61,7 @@ public class MappedByteBufferUnitTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Path getFileURIFromResources(String fileName) throws Exception {
|
private Path getFileURIFromResources(String fileName) throws Exception {
|
||||||
ClassLoader classLoader = getClass().getClassLoader();
|
ClassLoader classLoader = getClass().getClassLoader();
|
||||||
return Paths.get(classLoader.getResource(fileName).getPath());
|
return Paths.get(classLoader.getResource(fileName).getPath());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
package com.baeldung.maths;
|
package com.baeldung.maths;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class BigDecimalImplTest {
|
public class BigDecimalImplTest {
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
package com.baeldung.maths;
|
package com.baeldung.maths;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
public class BigIntegerImplTest {
|
public class BigIntegerImplTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
package com.baeldung.maths;
|
package com.baeldung.maths;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
public class FloatingPointArithmeticTest {
|
public class FloatingPointArithmeticTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -13,33 +13,33 @@ public class FloatingPointArithmeticTest {
|
||||||
double b = 4.88;
|
double b = 4.88;
|
||||||
double c = 21.45;
|
double c = 21.45;
|
||||||
double result = 39.55;
|
double result = 39.55;
|
||||||
|
|
||||||
double abc = a + b + c;
|
double abc = a + b + c;
|
||||||
double acb = a + c + b;
|
double acb = a + c + b;
|
||||||
|
|
||||||
Assert.assertEquals(result, abc, 0);
|
Assert.assertEquals(result, abc, 0);
|
||||||
Assert.assertNotEquals(result, acb, 0);
|
Assert.assertNotEquals(result, acb, 0);
|
||||||
|
|
||||||
double ab = 18.1;
|
double ab = 18.1;
|
||||||
double ac = 34.67;
|
double ac = 34.67;
|
||||||
|
|
||||||
double ab_c = ab + c;
|
double ab_c = ab + c;
|
||||||
double ac_b = ac + b;
|
double ac_b = ac + b;
|
||||||
|
|
||||||
Assert.assertEquals(result, ab_c, 0);
|
Assert.assertEquals(result, ab_c, 0);
|
||||||
Assert.assertNotEquals(result, ac_b, 0);
|
Assert.assertNotEquals(result, ac_b, 0);
|
||||||
|
|
||||||
BigDecimal d = new BigDecimal(String.valueOf(a));
|
BigDecimal d = new BigDecimal(String.valueOf(a));
|
||||||
BigDecimal e = new BigDecimal(String.valueOf(b));
|
BigDecimal e = new BigDecimal(String.valueOf(b));
|
||||||
BigDecimal f = new BigDecimal(String.valueOf(c));
|
BigDecimal f = new BigDecimal(String.valueOf(c));
|
||||||
BigDecimal sum = new BigDecimal("39.55");
|
BigDecimal sum = new BigDecimal("39.55");
|
||||||
|
|
||||||
BigDecimal def = d.add(e).add(f);
|
BigDecimal def = d.add(e).add(f);
|
||||||
BigDecimal dfe = d.add(f).add(e);
|
BigDecimal dfe = d.add(f).add(e);
|
||||||
|
|
||||||
Assert.assertEquals(0, def.compareTo(sum));
|
Assert.assertEquals(0, def.compareTo(sum));
|
||||||
Assert.assertEquals(0, dfe.compareTo(sum));
|
Assert.assertEquals(0, dfe.compareTo(sum));
|
||||||
|
|
||||||
Assert.assertNotEquals(0, sum.compareTo(new BigDecimal(String.valueOf(acb))));
|
Assert.assertNotEquals(0, sum.compareTo(new BigDecimal(String.valueOf(acb))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,9 @@ import org.decimal4j.util.DoubleRounder;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
|
||||||
public class RoundTest {
|
public class RoundTest {
|
||||||
private double value = 2.03456d;
|
private double value = 2.03456d;
|
||||||
private int places = 2;
|
private int places = 2;
|
||||||
|
@ -12,60 +15,60 @@ public class RoundTest {
|
||||||
private double expected = 2.03d;
|
private double expected = 2.03d;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDecimalNumber_whenRoundToNDecimalPlaces_thenGetExpectedResult() {
|
public void givenDecimalNumber_whenRoundToNDecimalPlaces_thenGetExpectedResult() {
|
||||||
Assert.assertEquals(expected, Round.round(value, places), delta);
|
assertEquals(expected, Round.round(value, places), delta);
|
||||||
Assert.assertEquals(expected, Round.roundNotPrecise(value, places), delta);
|
assertEquals(expected, Round.roundNotPrecise(value, places), delta);
|
||||||
Assert.assertEquals(expected, Round.roundAvoid(value, places), delta);
|
assertEquals(expected, Round.roundAvoid(value, places), delta);
|
||||||
Assert.assertEquals(expected, Precision.round(value, places), delta);
|
assertEquals(expected, Precision.round(value, places), delta);
|
||||||
Assert.assertEquals(expected, DoubleRounder.round(value, places), delta);
|
assertEquals(expected, DoubleRounder.round(value, places), delta);
|
||||||
|
|
||||||
places = 3;
|
places = 3;
|
||||||
expected = 2.035d;
|
expected = 2.035d;
|
||||||
|
|
||||||
Assert.assertEquals(expected, Round.round(value, places), delta);
|
assertEquals(expected, Round.round(value, places), delta);
|
||||||
Assert.assertEquals(expected, Round.roundNotPrecise(value, places), delta);
|
assertEquals(expected, Round.roundNotPrecise(value, places), delta);
|
||||||
Assert.assertEquals(expected, Round.roundAvoid(value, places), delta);
|
assertEquals(expected, Round.roundAvoid(value, places), delta);
|
||||||
Assert.assertEquals(expected, Precision.round(value, places), delta);
|
assertEquals(expected, Precision.round(value, places), delta);
|
||||||
Assert.assertEquals(expected, DoubleRounder.round(value, places), delta);
|
assertEquals(expected, DoubleRounder.round(value, places), delta);
|
||||||
|
|
||||||
value = 1000.0d;
|
value = 1000.0d;
|
||||||
places = 17;
|
places = 17;
|
||||||
expected = 1000.0d;
|
expected = 1000.0d;
|
||||||
|
|
||||||
Assert.assertEquals(expected, Round.round(value, places), delta);
|
assertEquals(expected, Round.round(value, places), delta);
|
||||||
Assert.assertEquals(expected, Round.roundNotPrecise(value, places), delta);
|
assertEquals(expected, Round.roundNotPrecise(value, places), delta);
|
||||||
Assert.assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 92.23372036854776 !
|
assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 92.23372036854776 !
|
||||||
Assert.assertEquals(expected, Precision.round(value, places), delta);
|
assertEquals(expected, Precision.round(value, places), delta);
|
||||||
Assert.assertEquals(expected, DoubleRounder.round(value, places), delta);
|
assertEquals(expected, DoubleRounder.round(value, places), delta);
|
||||||
|
|
||||||
value = 256.025d;
|
value = 256.025d;
|
||||||
places = 2;
|
places = 2;
|
||||||
expected = 256.03d;
|
expected = 256.03d;
|
||||||
|
|
||||||
Assert.assertEquals(expected, Round.round(value, places), delta);
|
assertEquals(expected, Round.round(value, places), delta);
|
||||||
Assert.assertNotEquals(expected, Round.roundNotPrecise(value, places), delta); // Returns: 256.02 !
|
assertNotEquals(expected, Round.roundNotPrecise(value, places), delta); // Returns: 256.02 !
|
||||||
Assert.assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 256.02 !
|
assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 256.02 !
|
||||||
Assert.assertEquals(expected, Precision.round(value, places), delta);
|
assertEquals(expected, Precision.round(value, places), delta);
|
||||||
Assert.assertNotEquals(expected, DoubleRounder.round(value, places), delta); // Returns: 256.02 !
|
assertNotEquals(expected, DoubleRounder.round(value, places), delta); // Returns: 256.02 !
|
||||||
|
|
||||||
value = 260.775d;
|
value = 260.775d;
|
||||||
places = 2;
|
places = 2;
|
||||||
expected = 260.78d;
|
expected = 260.78d;
|
||||||
|
|
||||||
Assert.assertEquals(expected, Round.round(value, places), delta);
|
assertEquals(expected, Round.round(value, places), delta);
|
||||||
Assert.assertNotEquals(expected, Round.roundNotPrecise(value, places), delta); // Returns: 260.77 !
|
assertNotEquals(expected, Round.roundNotPrecise(value, places), delta); // Returns: 260.77 !
|
||||||
Assert.assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 260.77 !
|
assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 260.77 !
|
||||||
Assert.assertEquals(expected, Precision.round(value, places), delta);
|
assertEquals(expected, Precision.round(value, places), delta);
|
||||||
Assert.assertNotEquals(expected, DoubleRounder.round(value, places), delta); // Returns: 260.77 !
|
assertNotEquals(expected, DoubleRounder.round(value, places), delta); // Returns: 260.77 !
|
||||||
|
|
||||||
value = 90080070060.1d;
|
value = 90080070060.1d;
|
||||||
places = 9;
|
places = 9;
|
||||||
expected = 90080070060.1d;
|
expected = 90080070060.1d;
|
||||||
|
|
||||||
Assert.assertEquals(expected, Round.round(value, places), delta);
|
assertEquals(expected, Round.round(value, places), delta);
|
||||||
Assert.assertEquals(expected, Round.roundNotPrecise(value, places), delta);
|
assertEquals(expected, Round.roundNotPrecise(value, places), delta);
|
||||||
Assert.assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 9.223372036854776E9 !
|
assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 9.223372036854776E9 !
|
||||||
Assert.assertEquals(expected, Precision.round(value, places), delta);
|
assertEquals(expected, Precision.round(value, places), delta);
|
||||||
Assert.assertEquals(expected, DoubleRounder.round(value, places), delta);
|
assertEquals(expected, DoubleRounder.round(value, places), delta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,13 +3,13 @@ package com.baeldung.money;
|
||||||
import org.javamoney.moneta.FastMoney;
|
import org.javamoney.moneta.FastMoney;
|
||||||
import org.javamoney.moneta.Money;
|
import org.javamoney.moneta.Money;
|
||||||
import org.javamoney.moneta.format.CurrencyStyle;
|
import org.javamoney.moneta.format.CurrencyStyle;
|
||||||
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import javax.money.CurrencyUnit;
|
import javax.money.CurrencyUnit;
|
||||||
import javax.money.Monetary;
|
import javax.money.Monetary;
|
||||||
import javax.money.MonetaryAmount;
|
import javax.money.MonetaryAmount;
|
||||||
import javax.money.UnknownCurrencyException;
|
import javax.money.UnknownCurrencyException;
|
||||||
import javax.money.convert.ConversionQueryBuilder;
|
|
||||||
import javax.money.convert.CurrencyConversion;
|
import javax.money.convert.CurrencyConversion;
|
||||||
import javax.money.convert.MonetaryConversions;
|
import javax.money.convert.MonetaryConversions;
|
||||||
import javax.money.format.AmountFormatQueryBuilder;
|
import javax.money.format.AmountFormatQueryBuilder;
|
||||||
|
@ -19,7 +19,11 @@ import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
public class JavaMoneyUnitTest {
|
public class JavaMoneyUnitTest {
|
||||||
|
|
||||||
|
@ -36,17 +40,16 @@ public class JavaMoneyUnitTest {
|
||||||
@Test(expected = UnknownCurrencyException.class)
|
@Test(expected = UnknownCurrencyException.class)
|
||||||
public void givenCurrencyCode_whenNoExist_thanThrowsError() {
|
public void givenCurrencyCode_whenNoExist_thanThrowsError() {
|
||||||
Monetary.getCurrency("AAA");
|
Monetary.getCurrency("AAA");
|
||||||
fail(); // if no exception
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAmounts_whenStringified_thanEquals() {
|
public void givenAmounts_whenStringified_thanEquals() {
|
||||||
CurrencyUnit usd = Monetary.getCurrency("USD");
|
CurrencyUnit usd = Monetary.getCurrency("USD");
|
||||||
MonetaryAmount fstAmtUSD = Monetary
|
MonetaryAmount fstAmtUSD = Monetary
|
||||||
.getDefaultAmountFactory()
|
.getDefaultAmountFactory()
|
||||||
.setCurrency(usd)
|
.setCurrency(usd)
|
||||||
.setNumber(200)
|
.setNumber(200)
|
||||||
.create();
|
.create();
|
||||||
Money moneyof = Money.of(12, usd);
|
Money moneyof = Money.of(12, usd);
|
||||||
FastMoney fastmoneyof = FastMoney.of(2, usd);
|
FastMoney fastmoneyof = FastMoney.of(2, usd);
|
||||||
|
|
||||||
|
@ -59,10 +62,10 @@ public class JavaMoneyUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenCurrencies_whenCompared_thanNotequal() {
|
public void givenCurrencies_whenCompared_thanNotequal() {
|
||||||
MonetaryAmount oneDolar = Monetary
|
MonetaryAmount oneDolar = Monetary
|
||||||
.getDefaultAmountFactory()
|
.getDefaultAmountFactory()
|
||||||
.setCurrency("USD")
|
.setCurrency("USD")
|
||||||
.setNumber(1)
|
.setNumber(1)
|
||||||
.create();
|
.create();
|
||||||
Money oneEuro = Money.of(1, "EUR");
|
Money oneEuro = Money.of(1, "EUR");
|
||||||
|
|
||||||
assertFalse(oneEuro.equals(FastMoney.of(1, "EUR")));
|
assertFalse(oneEuro.equals(FastMoney.of(1, "EUR")));
|
||||||
|
@ -72,10 +75,10 @@ public class JavaMoneyUnitTest {
|
||||||
@Test(expected = ArithmeticException.class)
|
@Test(expected = ArithmeticException.class)
|
||||||
public void givenAmount_whenDivided_thanThrowsException() {
|
public void givenAmount_whenDivided_thanThrowsException() {
|
||||||
MonetaryAmount oneDolar = Monetary
|
MonetaryAmount oneDolar = Monetary
|
||||||
.getDefaultAmountFactory()
|
.getDefaultAmountFactory()
|
||||||
.setCurrency("USD")
|
.setCurrency("USD")
|
||||||
.setNumber(1)
|
.setNumber(1)
|
||||||
.create();
|
.create();
|
||||||
oneDolar.divide(3);
|
oneDolar.divide(3);
|
||||||
fail(); // if no exception
|
fail(); // if no exception
|
||||||
}
|
}
|
||||||
|
@ -85,8 +88,8 @@ public class JavaMoneyUnitTest {
|
||||||
List<MonetaryAmount> monetaryAmounts = Arrays.asList(Money.of(100, "CHF"), Money.of(10.20, "CHF"), Money.of(1.15, "CHF"));
|
List<MonetaryAmount> monetaryAmounts = Arrays.asList(Money.of(100, "CHF"), Money.of(10.20, "CHF"), Money.of(1.15, "CHF"));
|
||||||
|
|
||||||
Money sumAmtCHF = (Money) monetaryAmounts
|
Money sumAmtCHF = (Money) monetaryAmounts
|
||||||
.stream()
|
.stream()
|
||||||
.reduce(Money.of(0, "CHF"), MonetaryAmount::add);
|
.reduce(Money.of(0, "CHF"), MonetaryAmount::add);
|
||||||
|
|
||||||
assertEquals("CHF 111.35", sumAmtCHF.toString());
|
assertEquals("CHF 111.35", sumAmtCHF.toString());
|
||||||
}
|
}
|
||||||
|
@ -97,18 +100,18 @@ public class JavaMoneyUnitTest {
|
||||||
|
|
||||||
Money moneyof = Money.of(12, usd);
|
Money moneyof = Money.of(12, usd);
|
||||||
MonetaryAmount fstAmtUSD = Monetary
|
MonetaryAmount fstAmtUSD = Monetary
|
||||||
.getDefaultAmountFactory()
|
.getDefaultAmountFactory()
|
||||||
.setCurrency(usd)
|
.setCurrency(usd)
|
||||||
.setNumber(200.50)
|
.setNumber(200.50)
|
||||||
.create();
|
.create();
|
||||||
MonetaryAmount oneDolar = Monetary
|
MonetaryAmount oneDolar = Monetary
|
||||||
.getDefaultAmountFactory()
|
.getDefaultAmountFactory()
|
||||||
.setCurrency("USD")
|
.setCurrency("USD")
|
||||||
.setNumber(1)
|
.setNumber(1)
|
||||||
.create();
|
.create();
|
||||||
Money subtractedAmount = Money
|
Money subtractedAmount = Money
|
||||||
.of(1, "USD")
|
.of(1, "USD")
|
||||||
.subtract(fstAmtUSD);
|
.subtract(fstAmtUSD);
|
||||||
MonetaryAmount multiplyAmount = oneDolar.multiply(0.25);
|
MonetaryAmount multiplyAmount = oneDolar.multiply(0.25);
|
||||||
MonetaryAmount divideAmount = oneDolar.divide(0.25);
|
MonetaryAmount divideAmount = oneDolar.divide(0.25);
|
||||||
|
|
||||||
|
@ -124,22 +127,23 @@ public class JavaMoneyUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenAmount_whenRounded_thanEquals() {
|
public void givenAmount_whenRounded_thanEquals() {
|
||||||
MonetaryAmount fstAmtEUR = Monetary
|
MonetaryAmount fstAmtEUR = Monetary
|
||||||
.getDefaultAmountFactory()
|
.getDefaultAmountFactory()
|
||||||
.setCurrency("EUR")
|
.setCurrency("EUR")
|
||||||
.setNumber(1.30473908)
|
.setNumber(1.30473908)
|
||||||
.create();
|
.create();
|
||||||
MonetaryAmount roundEUR = fstAmtEUR.with(Monetary.getDefaultRounding());
|
MonetaryAmount roundEUR = fstAmtEUR.with(Monetary.getDefaultRounding());
|
||||||
assertEquals("EUR 1.30473908", fstAmtEUR.toString());
|
assertEquals("EUR 1.30473908", fstAmtEUR.toString());
|
||||||
assertEquals("EUR 1.3", roundEUR.toString());
|
assertEquals("EUR 1.3", roundEUR.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Ignore("Currency providers are not always available")
|
||||||
public void givenAmount_whenConversion_thenNotNull() {
|
public void givenAmount_whenConversion_thenNotNull() {
|
||||||
MonetaryAmount oneDollar = Monetary
|
MonetaryAmount oneDollar = Monetary
|
||||||
.getDefaultAmountFactory()
|
.getDefaultAmountFactory()
|
||||||
.setCurrency("USD")
|
.setCurrency("USD")
|
||||||
.setNumber(1)
|
.setNumber(1)
|
||||||
.create();
|
.create();
|
||||||
|
|
||||||
CurrencyConversion conversionEUR = MonetaryConversions.getConversion("EUR");
|
CurrencyConversion conversionEUR = MonetaryConversions.getConversion("EUR");
|
||||||
|
|
||||||
|
@ -152,10 +156,10 @@ public class JavaMoneyUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenLocale_whenFormatted_thanEquals() {
|
public void givenLocale_whenFormatted_thanEquals() {
|
||||||
MonetaryAmount oneDollar = Monetary
|
MonetaryAmount oneDollar = Monetary
|
||||||
.getDefaultAmountFactory()
|
.getDefaultAmountFactory()
|
||||||
.setCurrency("USD")
|
.setCurrency("USD")
|
||||||
.setNumber(1)
|
.setNumber(1)
|
||||||
.create();
|
.create();
|
||||||
MonetaryAmountFormat formatUSD = MonetaryFormats.getAmountFormat(Locale.US);
|
MonetaryAmountFormat formatUSD = MonetaryFormats.getAmountFormat(Locale.US);
|
||||||
String usFormatted = formatUSD.format(oneDollar);
|
String usFormatted = formatUSD.format(oneDollar);
|
||||||
|
|
||||||
|
@ -167,16 +171,16 @@ public class JavaMoneyUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenAmount_whenCustomFormat_thanEquals() {
|
public void givenAmount_whenCustomFormat_thanEquals() {
|
||||||
MonetaryAmount oneDollar = Monetary
|
MonetaryAmount oneDollar = Monetary
|
||||||
.getDefaultAmountFactory()
|
.getDefaultAmountFactory()
|
||||||
.setCurrency("USD")
|
.setCurrency("USD")
|
||||||
.setNumber(1)
|
.setNumber(1)
|
||||||
.create();
|
.create();
|
||||||
|
|
||||||
MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder
|
MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder
|
||||||
.of(Locale.US)
|
.of(Locale.US)
|
||||||
.set(CurrencyStyle.NAME)
|
.set(CurrencyStyle.NAME)
|
||||||
.set("pattern", "00000.00 ¤")
|
.set("pattern", "00000.00 ¤")
|
||||||
.build());
|
.build());
|
||||||
String customFormatted = customFormat.format(oneDollar);
|
String customFormatted = customFormat.format(oneDollar);
|
||||||
|
|
||||||
assertNotNull(customFormat);
|
assertNotNull(customFormat);
|
||||||
|
|
|
@ -1,71 +1,73 @@
|
||||||
package com.baeldung.regexp;
|
package com.baeldung.regexp;
|
||||||
|
|
||||||
import static junit.framework.TestCase.assertEquals;
|
import org.junit.Test;
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
import static org.hamcrest.CoreMatchers.*;
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.junit.Test;
|
import static junit.framework.TestCase.assertEquals;
|
||||||
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
|
import static org.hamcrest.CoreMatchers.not;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
public class EscapingCharsTest {
|
public class EscapingCharsTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenRegexWithDot_whenMatchingStr_thenMatches() {
|
public void givenRegexWithDot_whenMatchingStr_thenMatches() {
|
||||||
String strInput = "foof";
|
String strInput = "foof";
|
||||||
String strRegex = "foo.";
|
String strRegex = "foo.";
|
||||||
|
|
||||||
assertEquals(true, strInput.matches(strRegex));
|
assertEquals(true, strInput.matches(strRegex));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenRegexWithDotEsc_whenMatchingStr_thenNotMatching() {
|
public void givenRegexWithDotEsc_whenMatchingStr_thenNotMatching() {
|
||||||
String strInput = "foof";
|
String strInput = "foof";
|
||||||
String strRegex = "foo\\.";
|
String strRegex = "foo\\.";
|
||||||
|
|
||||||
assertEquals(false, strInput.matches(strRegex));
|
assertEquals(false, strInput.matches(strRegex));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenRegexWithPipeEscaped_whenSplitStr_thenSplits() {
|
public void givenRegexWithPipeEscaped_whenSplitStr_thenSplits() {
|
||||||
String strInput = "foo|bar|hello|world";
|
String strInput = "foo|bar|hello|world";
|
||||||
String strRegex = "\\Q|\\E";
|
String strRegex = "\\Q|\\E";
|
||||||
|
|
||||||
assertEquals(4, strInput.split(strRegex).length);
|
assertEquals(4, strInput.split(strRegex).length);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenRegexWithPipeEscQuoteMeth_whenSplitStr_thenSplits() {
|
public void givenRegexWithPipeEscQuoteMeth_whenSplitStr_thenSplits() {
|
||||||
String strInput = "foo|bar|hello|world";
|
String strInput = "foo|bar|hello|world";
|
||||||
String strRegex = "|";
|
String strRegex = "|";
|
||||||
|
|
||||||
assertEquals(4,strInput.split(Pattern.quote(strRegex)).length);
|
assertEquals(4, strInput.split(Pattern.quote(strRegex)).length);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenRegexWithDollar_whenReplacing_thenNotReplace() {
|
public void givenRegexWithDollar_whenReplacing_thenNotReplace() {
|
||||||
String strInput = "I gave $50 to my brother."
|
String strInput = "I gave $50 to my brother."
|
||||||
+ "He bought candy for $35. Now he has $15 left.";
|
+ "He bought candy for $35. Now he has $15 left.";
|
||||||
String strRegex = "$";
|
String strRegex = "$";
|
||||||
String strReplacement = "£";
|
String strReplacement = "£";
|
||||||
String output = "I gave £50 to my brother."
|
String output = "I gave £50 to my brother."
|
||||||
+ "He bought candy for £35. Now he has £15 left.";
|
+ "He bought candy for £35. Now he has £15 left.";
|
||||||
Pattern p = Pattern.compile(strRegex);
|
Pattern p = Pattern.compile(strRegex);
|
||||||
Matcher m = p.matcher(strInput);
|
Matcher m = p.matcher(strInput);
|
||||||
|
|
||||||
assertThat(output, not(equalTo(m.replaceAll(strReplacement))));
|
assertThat(output, not(equalTo(m.replaceAll(strReplacement))));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenRegexWithDollarEsc_whenReplacing_thenReplace() {
|
public void givenRegexWithDollarEsc_whenReplacing_thenReplace() {
|
||||||
String strInput = "I gave $50 to my brother."
|
String strInput = "I gave $50 to my brother."
|
||||||
+ "He bought candy for $35. Now he has $15 left.";
|
+ "He bought candy for $35. Now he has $15 left.";
|
||||||
String strRegex = "\\$";
|
String strRegex = "\\$";
|
||||||
String strReplacement = "£";
|
String strReplacement = "£";
|
||||||
String output = "I gave £50 to my brother."
|
String output = "I gave £50 to my brother."
|
||||||
+ "He bought candy for £35. Now he has £15 left.";
|
+ "He bought candy for £35. Now he has £15 left.";
|
||||||
Pattern p = Pattern.compile(strRegex);
|
Pattern p = Pattern.compile(strRegex);
|
||||||
Matcher m = p.matcher(strInput);
|
Matcher m = p.matcher(strInput);
|
||||||
|
|
||||||
assertEquals(output,m.replaceAll(strReplacement));
|
assertEquals(output, m.replaceAll(strReplacement));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,11 @@ import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import javax.script.*;
|
import javax.script.Bindings;
|
||||||
|
import javax.script.Invocable;
|
||||||
|
import javax.script.ScriptEngine;
|
||||||
|
import javax.script.ScriptEngineManager;
|
||||||
|
import javax.script.ScriptException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package com.baeldung.serialization;
|
package com.baeldung.serialization;
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
|
@ -8,57 +8,57 @@ import java.io.IOException;
|
||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
|
|
||||||
import org.junit.Test;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
public class PersonUnitTest {
|
public class PersonUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSerializingAndDeserializing_ThenObjectIsTheSame() throws IOException, ClassNotFoundException {
|
public void whenSerializingAndDeserializing_ThenObjectIsTheSame() throws IOException, ClassNotFoundException {
|
||||||
Person p = new Person();
|
Person p = new Person();
|
||||||
p.setAge(20);
|
p.setAge(20);
|
||||||
p.setName("Joe");
|
p.setName("Joe");
|
||||||
|
|
||||||
FileOutputStream fileOutputStream = new FileOutputStream("yofile.txt");
|
FileOutputStream fileOutputStream = new FileOutputStream("yofile.txt");
|
||||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
|
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
|
||||||
objectOutputStream.writeObject(p);
|
objectOutputStream.writeObject(p);
|
||||||
objectOutputStream.flush();
|
objectOutputStream.flush();
|
||||||
objectOutputStream.close();
|
objectOutputStream.close();
|
||||||
|
|
||||||
FileInputStream fileInputStream = new FileInputStream("yofile.txt");
|
FileInputStream fileInputStream = new FileInputStream("yofile.txt");
|
||||||
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
|
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
|
||||||
Person p2 = (Person) objectInputStream.readObject();
|
Person p2 = (Person) objectInputStream.readObject();
|
||||||
objectInputStream.close();
|
objectInputStream.close();
|
||||||
|
|
||||||
assertTrue(p2.getAge() == p.getAge());
|
assertTrue(p2.getAge() == p.getAge());
|
||||||
assertTrue(p2.getName().equals(p.getName()));
|
assertTrue(p2.getName().equals(p.getName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCustomSerializingAndDeserializing_ThenObjectIsTheSame() throws IOException, ClassNotFoundException {
|
public void whenCustomSerializingAndDeserializing_ThenObjectIsTheSame() throws IOException, ClassNotFoundException {
|
||||||
Person p = new Person();
|
Person p = new Person();
|
||||||
p.setAge(20);
|
p.setAge(20);
|
||||||
p.setName("Joe");
|
p.setName("Joe");
|
||||||
|
|
||||||
Address a = new Address();
|
Address a = new Address();
|
||||||
a.setHouseNumber(1);
|
a.setHouseNumber(1);
|
||||||
|
|
||||||
Employee e = new Employee();
|
Employee e = new Employee();
|
||||||
e.setPerson(p);
|
e.setPerson(p);
|
||||||
e.setAddress(a);
|
e.setAddress(a);
|
||||||
|
|
||||||
FileOutputStream fileOutputStream = new FileOutputStream("yofile2.txt");
|
FileOutputStream fileOutputStream = new FileOutputStream("yofile2.txt");
|
||||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
|
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
|
||||||
objectOutputStream.writeObject(e);
|
objectOutputStream.writeObject(e);
|
||||||
objectOutputStream.flush();
|
objectOutputStream.flush();
|
||||||
objectOutputStream.close();
|
objectOutputStream.close();
|
||||||
|
|
||||||
FileInputStream fileInputStream = new FileInputStream("yofile2.txt");
|
FileInputStream fileInputStream = new FileInputStream("yofile2.txt");
|
||||||
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
|
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
|
||||||
Employee e2 = (Employee) objectInputStream.readObject();
|
Employee e2 = (Employee) objectInputStream.readObject();
|
||||||
objectInputStream.close();
|
objectInputStream.close();
|
||||||
|
|
||||||
assertTrue(e2.getPerson().getAge() == e.getPerson().getAge());
|
assertTrue(e2.getPerson().getAge() == e.getPerson().getAge());
|
||||||
assertTrue(e2.getAddress().getHouseNumber() == (e.getAddress().getHouseNumber()));
|
assertTrue(e2.getAddress().getHouseNumber() == (e.getAddress().getHouseNumber()));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
package com.baeldung.socket;
|
package com.baeldung.socket;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
public class EchoIntegrationTest {
|
public class EchoIntegrationTest {
|
||||||
private static final Integer PORT = 4444;
|
private static final Integer PORT = 4444;
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
package com.baeldung.stackoverflowerror;
|
package com.baeldung.stackoverflowerror;
|
||||||
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class CyclicDependancyManualTest {
|
public class CyclicDependancyManualTest {
|
||||||
@Test(expected = StackOverflowError.class)
|
@Test(expected = StackOverflowError.class)
|
||||||
public void whenInstanciatingClassOne_thenThrowsException() {
|
public void whenInstanciatingClassOne_thenThrowsException() {
|
||||||
ClassOne obj = new ClassOne();
|
ClassOne obj = new ClassOne();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package com.baeldung.stackoverflowerror;
|
package com.baeldung.stackoverflowerror;
|
||||||
|
|
||||||
import static junit.framework.TestCase.assertEquals;
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static junit.framework.TestCase.assertEquals;
|
||||||
|
|
||||||
public class InfiniteRecursionWithTerminationConditionManualTest {
|
public class InfiniteRecursionWithTerminationConditionManualTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenPositiveIntNoOne_whenCalcFact_thenCorrectlyCalc() {
|
public void givenPositiveIntNoOne_whenCalcFact_thenCorrectlyCalc() {
|
||||||
|
@ -23,9 +23,9 @@ public class InfiniteRecursionWithTerminationConditionManualTest {
|
||||||
|
|
||||||
@Test(expected = StackOverflowError.class)
|
@Test(expected = StackOverflowError.class)
|
||||||
public void givenNegativeInt_whenCalcFact_thenThrowsException() {
|
public void givenNegativeInt_whenCalcFact_thenThrowsException() {
|
||||||
int numToCalcFactorial = -1;
|
int numToCalcFactorial = -1;
|
||||||
InfiniteRecursionWithTerminationCondition irtc = new InfiniteRecursionWithTerminationCondition();
|
InfiniteRecursionWithTerminationCondition irtc = new InfiniteRecursionWithTerminationCondition();
|
||||||
|
|
||||||
irtc.calculateFactorial(numToCalcFactorial);
|
irtc.calculateFactorial(numToCalcFactorial);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package com.baeldung.stackoverflowerror;
|
package com.baeldung.stackoverflowerror;
|
||||||
|
|
||||||
import static junit.framework.TestCase.assertEquals;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static junit.framework.TestCase.assertEquals;
|
||||||
|
|
||||||
public class RecursionWithCorrectTerminationConditionManualTest {
|
public class RecursionWithCorrectTerminationConditionManualTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenNegativeInt_whenCalcFact_thenCorrectlyCalc() {
|
public void givenNegativeInt_whenCalcFact_thenCorrectlyCalc() {
|
||||||
|
|
|
@ -6,25 +6,25 @@ import org.junit.Test;
|
||||||
public class UnintendedInfiniteRecursionManualTest {
|
public class UnintendedInfiniteRecursionManualTest {
|
||||||
@Test(expected = StackOverflowError.class)
|
@Test(expected = StackOverflowError.class)
|
||||||
public void givenPositiveIntNoOne_whenCalFact_thenThrowsException() {
|
public void givenPositiveIntNoOne_whenCalFact_thenThrowsException() {
|
||||||
int numToCalcFactorial= 1;
|
int numToCalcFactorial = 1;
|
||||||
UnintendedInfiniteRecursion uir = new UnintendedInfiniteRecursion();
|
UnintendedInfiniteRecursion uir = new UnintendedInfiniteRecursion();
|
||||||
|
|
||||||
uir.calculateFactorial(numToCalcFactorial);
|
uir.calculateFactorial(numToCalcFactorial);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = StackOverflowError.class)
|
@Test(expected = StackOverflowError.class)
|
||||||
public void givenPositiveIntGtOne_whenCalcFact_thenThrowsException() {
|
public void givenPositiveIntGtOne_whenCalcFact_thenThrowsException() {
|
||||||
int numToCalcFactorial= 2;
|
int numToCalcFactorial = 2;
|
||||||
UnintendedInfiniteRecursion uir = new UnintendedInfiniteRecursion();
|
UnintendedInfiniteRecursion uir = new UnintendedInfiniteRecursion();
|
||||||
|
|
||||||
uir.calculateFactorial(numToCalcFactorial);
|
uir.calculateFactorial(numToCalcFactorial);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = StackOverflowError.class)
|
@Test(expected = StackOverflowError.class)
|
||||||
public void givenNegativeInt_whenCalcFact_thenThrowsException() {
|
public void givenNegativeInt_whenCalcFact_thenThrowsException() {
|
||||||
int numToCalcFactorial= -1;
|
int numToCalcFactorial = -1;
|
||||||
UnintendedInfiniteRecursion uir = new UnintendedInfiniteRecursion();
|
UnintendedInfiniteRecursion uir = new UnintendedInfiniteRecursion();
|
||||||
|
|
||||||
uir.calculateFactorial(numToCalcFactorial);
|
uir.calculateFactorial(numToCalcFactorial);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,9 @@ import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
import static com.baeldung.strategy.Discounter.*;
|
import static com.baeldung.strategy.Discounter.christmas;
|
||||||
|
import static com.baeldung.strategy.Discounter.easter;
|
||||||
|
import static com.baeldung.strategy.Discounter.newYear;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public class StrategyDesignPatternUnitTest {
|
public class StrategyDesignPatternUnitTest {
|
||||||
|
@ -26,7 +28,7 @@ public class StrategyDesignPatternUnitTest {
|
||||||
public void shouldDivideByTwo_WhenApplyingStaffDiscounterWithAnonyousTypes() {
|
public void shouldDivideByTwo_WhenApplyingStaffDiscounterWithAnonyousTypes() {
|
||||||
Discounter staffDiscounter = new Discounter() {
|
Discounter staffDiscounter = new Discounter() {
|
||||||
@Override
|
@Override
|
||||||
public BigDecimal apply( BigDecimal amount) {
|
public BigDecimal apply(BigDecimal amount) {
|
||||||
return amount.multiply(BigDecimal.valueOf(0.5));
|
return amount.multiply(BigDecimal.valueOf(0.5));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,7 +5,6 @@ import org.junit.Test;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
@ -22,8 +21,8 @@ public class InfiniteStreamUnitTest {
|
||||||
|
|
||||||
//when
|
//when
|
||||||
List<Integer> collect = infiniteStream
|
List<Integer> collect = infiniteStream
|
||||||
.limit(10)
|
.limit(10)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
//then
|
//then
|
||||||
assertEquals(collect, Arrays.asList(0, 2, 4, 6, 8, 10, 12, 14, 16, 18));
|
assertEquals(collect, Arrays.asList(0, 2, 4, 6, 8, 10, 12, 14, 16, 18));
|
||||||
|
@ -37,9 +36,9 @@ public class InfiniteStreamUnitTest {
|
||||||
|
|
||||||
//when
|
//when
|
||||||
List<UUID> randomInts = infiniteStreamOfRandomUUID
|
List<UUID> randomInts = infiniteStreamOfRandomUUID
|
||||||
.skip(10)
|
.skip(10)
|
||||||
.limit(10)
|
.limit(10)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
//then
|
//then
|
||||||
assertEquals(randomInts.size(), 10);
|
assertEquals(randomInts.size(), 10);
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package com.baeldung.stream;
|
package com.baeldung.stream;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class StreamAddUnitTest {
|
public class StreamAddUnitTest {
|
||||||
|
|
||||||
|
@ -25,7 +27,7 @@ public class StreamAddUnitTest {
|
||||||
Stream<Integer> newStream = Stream.concat(Stream.of(99), anStream);
|
Stream<Integer> newStream = Stream.concat(Stream.of(99), anStream);
|
||||||
|
|
||||||
assertEquals(newStream.findFirst()
|
assertEquals(newStream.findFirst()
|
||||||
.get(), (Integer) 99);
|
.get(), (Integer) 99);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -38,7 +40,7 @@ public class StreamAddUnitTest {
|
||||||
assertEquals(resultList.get(3), (Double) 9.9);
|
assertEquals(resultList.get(3), (Double) 9.9);
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> Stream<T> insertInStream(Stream<T> stream, T elem, int index) {
|
private <T> Stream<T> insertInStream(Stream<T> stream, T elem, int index) {
|
||||||
List<T> result = stream.collect(Collectors.toList());
|
List<T> result = stream.collect(Collectors.toList());
|
||||||
result.add(index, elem);
|
result.add(index, elem);
|
||||||
return result.stream();
|
return result.stream();
|
||||||
|
|
|
@ -20,13 +20,13 @@ public class StreamApiTest {
|
||||||
|
|
||||||
assertEquals("Sean", last);
|
assertEquals("Sean", last);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenInfiniteStream_whenGetInfiniteStreamLastElementUsingReduce_thenReturnLastElement() {
|
public void givenInfiniteStream_whenGetInfiniteStreamLastElementUsingReduce_thenReturnLastElement() {
|
||||||
int last = StreamApi.getInfiniteStreamLastElementUsingReduce();
|
int last = StreamApi.getInfiniteStreamLastElementUsingReduce();
|
||||||
assertEquals(19, last);
|
assertEquals(19, last);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenListAndCount_whenGetLastElementUsingSkip_thenReturnLastElement() {
|
public void givenListAndCount_whenGetLastElementUsingSkip_thenReturnLastElement() {
|
||||||
List<String> valueList = new ArrayList<>();
|
List<String> valueList = new ArrayList<>();
|
||||||
|
|
|
@ -1,68 +1,66 @@
|
||||||
package com.baeldung.string;
|
package com.baeldung.string;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.Test;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import com.baeldung.string.JoinerSplitter;
|
|
||||||
|
|
||||||
public class JoinerSplitterUnitTest {
|
public class JoinerSplitterUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void provided_array_convert_to_stream_and_convert_to_string() {
|
public void provided_array_convert_to_stream_and_convert_to_string() {
|
||||||
|
|
||||||
String[] programming_languages = {"java", "python", "nodejs", "ruby"};
|
String[] programming_languages = {"java", "python", "nodejs", "ruby"};
|
||||||
|
|
||||||
String expectation = "java,python,nodejs,ruby";
|
String expectation = "java,python,nodejs,ruby";
|
||||||
|
|
||||||
String result = JoinerSplitter.join(programming_languages);
|
|
||||||
assertEquals(result, expectation);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenArray_transformedToStream_convertToPrefixPostfixString() {
|
|
||||||
|
|
||||||
String[] programming_languages = {"java", "python",
|
String result = JoinerSplitter.join(programming_languages);
|
||||||
"nodejs", "ruby"};
|
assertEquals(result, expectation);
|
||||||
String expectation = "[java,python,nodejs,ruby]";
|
}
|
||||||
|
|
||||||
String result = JoinerSplitter.joinWithPrefixPostFix(programming_languages);
|
|
||||||
assertEquals(result, expectation);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenString_transformedToStream_convertToList() {
|
|
||||||
|
|
||||||
String programming_languages = "java,python,nodejs,ruby";
|
@Test
|
||||||
|
public void givenArray_transformedToStream_convertToPrefixPostfixString() {
|
||||||
List<String> expectation = new ArrayList<String>();
|
|
||||||
expectation.add("java");
|
String[] programming_languages = {"java", "python",
|
||||||
expectation.add("python");
|
"nodejs", "ruby"};
|
||||||
expectation.add("nodejs");
|
String expectation = "[java,python,nodejs,ruby]";
|
||||||
expectation.add("ruby");
|
|
||||||
|
String result = JoinerSplitter.joinWithPrefixPostFix(programming_languages);
|
||||||
List<String> result = JoinerSplitter.split(programming_languages);
|
assertEquals(result, expectation);
|
||||||
|
}
|
||||||
assertEquals(result, expectation);
|
|
||||||
}
|
@Test
|
||||||
|
public void givenString_transformedToStream_convertToList() {
|
||||||
@Test
|
|
||||||
public void givenString_transformedToStream_convertToListOfChar() {
|
String programming_languages = "java,python,nodejs,ruby";
|
||||||
|
|
||||||
|
List<String> expectation = new ArrayList<String>();
|
||||||
|
expectation.add("java");
|
||||||
|
expectation.add("python");
|
||||||
|
expectation.add("nodejs");
|
||||||
|
expectation.add("ruby");
|
||||||
|
|
||||||
|
List<String> result = JoinerSplitter.split(programming_languages);
|
||||||
|
|
||||||
|
assertEquals(result, expectation);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenString_transformedToStream_convertToListOfChar() {
|
||||||
|
|
||||||
|
String programming_languages = "java,python,nodejs,ruby";
|
||||||
|
|
||||||
|
List<Character> expectation = new ArrayList<Character>();
|
||||||
|
char[] charArray = programming_languages.toCharArray();
|
||||||
|
for (char c : charArray) {
|
||||||
|
expectation.add(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Character> result = JoinerSplitter.splitToListOfChar(programming_languages);
|
||||||
|
assertEquals(result, expectation);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
String programming_languages = "java,python,nodejs,ruby";
|
|
||||||
|
|
||||||
List<Character> expectation = new ArrayList<Character>();
|
|
||||||
char[] charArray = programming_languages.toCharArray();
|
|
||||||
for (char c : charArray) {
|
|
||||||
expectation.add(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Character> result = JoinerSplitter.splitToListOfChar(programming_languages);
|
|
||||||
assertEquals(result, expectation);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package com.baeldung.string;
|
||||||
|
|
||||||
import com.google.common.base.Splitter;
|
import com.google.common.base.Splitter;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.assertj.core.util.Lists;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
package com.baeldung.string;
|
package com.baeldung.string;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNotEquals;
|
import static org.junit.Assert.assertNotEquals;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class StringHelperUnitTest {
|
public class StringHelperUnitTest {
|
||||||
|
|
||||||
|
@ -67,7 +68,7 @@ public class StringHelperUnitTest {
|
||||||
assertEquals("abc", StringHelper.removeLastCharOptional(WHITE_SPACE_AT_THE_END_STRING));
|
assertEquals("abc", StringHelper.removeLastCharOptional(WHITE_SPACE_AT_THE_END_STRING));
|
||||||
assertEquals("abc", StringHelper.removeLastCharRegexOptional(WHITE_SPACE_AT_THE_END_STRING));
|
assertEquals("abc", StringHelper.removeLastCharRegexOptional(WHITE_SPACE_AT_THE_END_STRING));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenStringWithNewLineAtTheEnd_whenSubstring_thenGetStringWithoutNewLine() {
|
public void givenStringWithNewLineAtTheEnd_whenSubstring_thenGetStringWithoutNewLine() {
|
||||||
assertEquals("abc", StringHelper.removeLastChar(NEW_LINE_AT_THE_END_STRING));
|
assertEquals("abc", StringHelper.removeLastChar(NEW_LINE_AT_THE_END_STRING));
|
||||||
|
@ -78,7 +79,7 @@ public class StringHelperUnitTest {
|
||||||
assertEquals("abc", StringHelper.removeLastCharOptional(NEW_LINE_AT_THE_END_STRING));
|
assertEquals("abc", StringHelper.removeLastCharOptional(NEW_LINE_AT_THE_END_STRING));
|
||||||
assertNotEquals("abc", StringHelper.removeLastCharRegexOptional(NEW_LINE_AT_THE_END_STRING));
|
assertNotEquals("abc", StringHelper.removeLastCharRegexOptional(NEW_LINE_AT_THE_END_STRING));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenMultiLineString_whenSubstring_thenGetStringWithoutNewLine() {
|
public void givenMultiLineString_whenSubstring_thenGetStringWithoutNewLine() {
|
||||||
assertEquals("abc\nde", StringHelper.removeLastChar(MULTIPLE_LINES_STRING));
|
assertEquals("abc\nde", StringHelper.removeLastChar(MULTIPLE_LINES_STRING));
|
||||||
|
|
Loading…
Reference in New Issue