* Fix problems with money

* Refactor
This commit is contained in:
Grzegorz Piwowarek 2017-07-05 17:20:04 +02:00 committed by GitHub
parent 026c9717ce
commit 178af4e049
23 changed files with 295 additions and 281 deletions

View File

@ -14,7 +14,7 @@ import org.hamcrest.collection.IsIterableContainingInOrder;
import org.junit.Test;
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
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"));
}
public <T> List<T> flattenListOfListsImperatively(List<List<T>> list) {
private <T> List<T> flattenListOfListsImperatively(List<List<T>> list) {
List<T> ls = new ArrayList<>();
list.forEach(ls::addAll);
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());
}
}

View File

@ -1,18 +1,19 @@
package com.baeldung.list.listoflist;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
public class ListOfListsUnitTest {
private List<ArrayList<? extends Stationery>> listOfLists = new ArrayList<ArrayList<? extends Stationery>>();
private ArrayList<Pen> penList = new ArrayList<>();
private ArrayList<Pencil> pencilList = new ArrayList<>();
private ArrayList<Rubber> rubberList = new ArrayList<>();
private List<List<? extends Stationery>> listOfLists = new ArrayList<>();
private List<Pen> penList = new ArrayList<>();
private List<Pencil> pencilList = new ArrayList<>();
private List<Rubber> rubberList = new ArrayList<>();
@SuppressWarnings("unchecked")
@Before
@ -29,11 +30,11 @@ public class ListOfListsUnitTest {
@Test
public void givenListOfLists_thenCheckNames() {
assertEquals("Pen 1", ((Pen) listOfLists.get(0)
.get(0)).getName());
.get(0)).getName());
assertEquals("Pencil 1", ((Pencil) listOfLists.get(1)
.get(0)).getName());
.get(0)).getName());
assertEquals("Rubber 1", ((Rubber) listOfLists.get(2)
.get(0)).getName());
.get(0)).getName());
}
@SuppressWarnings("unchecked")
@ -43,10 +44,10 @@ public class ListOfListsUnitTest {
((ArrayList<Pencil>) listOfLists.get(1)).remove(0);
listOfLists.remove(1);
assertEquals("Rubber 1", ((Rubber) listOfLists.get(1)
.get(0)).getName());
.get(0)).getName());
listOfLists.remove(0);
assertEquals("Rubber 1", ((Rubber) listOfLists.get(0)
.get(0)).getName());
.get(0)).getName());
}
@Test
@ -67,10 +68,10 @@ public class ListOfListsUnitTest {
list.add(rubbers);
assertEquals("Pen 1", ((Pen) list.get(0)
.get(0)).getName());
.get(0)).getName());
assertEquals("Pencil 1", ((Pencil) list.get(1)
.get(0)).getName());
.get(0)).getName());
assertEquals("Rubber 1", ((Rubber) list.get(2)
.get(0)).getName());
.get(0)).getName());
}
}

View File

@ -47,7 +47,7 @@ public class MappedByteBufferUnitTest {
//when
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());
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();
return Paths.get(classLoader.getResource(fileName).getPath());
}

View File

@ -1,9 +1,10 @@
package com.baeldung.maths;
import org.junit.Assert;
import org.junit.Test;
import java.math.BigDecimal;
import java.math.RoundingMode;
import org.junit.Assert;
import org.junit.Test;
public class BigDecimalImplTest {

View File

@ -1,11 +1,10 @@
package com.baeldung.maths;
import java.math.BigInteger;
import org.junit.Assert;
import org.junit.Test;
import java.math.BigInteger;
public class BigIntegerImplTest {
@Test

View File

@ -1,10 +1,10 @@
package com.baeldung.maths;
import java.math.BigDecimal;
import org.junit.Assert;
import org.junit.Test;
import java.math.BigDecimal;
public class FloatingPointArithmeticTest {
@Test

View File

@ -5,6 +5,9 @@ import org.decimal4j.util.DoubleRounder;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
public class RoundTest {
private double value = 2.03456d;
private int places = 2;
@ -13,59 +16,59 @@ public class RoundTest {
@Test
public void givenDecimalNumber_whenRoundToNDecimalPlaces_thenGetExpectedResult() {
Assert.assertEquals(expected, Round.round(value, places), delta);
Assert.assertEquals(expected, Round.roundNotPrecise(value, places), delta);
Assert.assertEquals(expected, Round.roundAvoid(value, places), delta);
Assert.assertEquals(expected, Precision.round(value, places), delta);
Assert.assertEquals(expected, DoubleRounder.round(value, places), delta);
assertEquals(expected, Round.round(value, places), delta);
assertEquals(expected, Round.roundNotPrecise(value, places), delta);
assertEquals(expected, Round.roundAvoid(value, places), delta);
assertEquals(expected, Precision.round(value, places), delta);
assertEquals(expected, DoubleRounder.round(value, places), delta);
places = 3;
expected = 2.035d;
Assert.assertEquals(expected, Round.round(value, places), delta);
Assert.assertEquals(expected, Round.roundNotPrecise(value, places), delta);
Assert.assertEquals(expected, Round.roundAvoid(value, places), delta);
Assert.assertEquals(expected, Precision.round(value, places), delta);
Assert.assertEquals(expected, DoubleRounder.round(value, places), delta);
assertEquals(expected, Round.round(value, places), delta);
assertEquals(expected, Round.roundNotPrecise(value, places), delta);
assertEquals(expected, Round.roundAvoid(value, places), delta);
assertEquals(expected, Precision.round(value, places), delta);
assertEquals(expected, DoubleRounder.round(value, places), delta);
value = 1000.0d;
places = 17;
expected = 1000.0d;
Assert.assertEquals(expected, Round.round(value, places), delta);
Assert.assertEquals(expected, Round.roundNotPrecise(value, places), delta);
Assert.assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 92.23372036854776 !
Assert.assertEquals(expected, Precision.round(value, places), delta);
Assert.assertEquals(expected, DoubleRounder.round(value, places), delta);
assertEquals(expected, Round.round(value, places), delta);
assertEquals(expected, Round.roundNotPrecise(value, places), delta);
assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 92.23372036854776 !
assertEquals(expected, Precision.round(value, places), delta);
assertEquals(expected, DoubleRounder.round(value, places), delta);
value = 256.025d;
places = 2;
expected = 256.03d;
Assert.assertEquals(expected, Round.round(value, places), delta);
Assert.assertNotEquals(expected, Round.roundNotPrecise(value, places), delta); // Returns: 256.02 !
Assert.assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 256.02 !
Assert.assertEquals(expected, Precision.round(value, places), delta);
Assert.assertNotEquals(expected, DoubleRounder.round(value, places), delta); // Returns: 256.02 !
assertEquals(expected, Round.round(value, places), delta);
assertNotEquals(expected, Round.roundNotPrecise(value, places), delta); // Returns: 256.02 !
assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 256.02 !
assertEquals(expected, Precision.round(value, places), delta);
assertNotEquals(expected, DoubleRounder.round(value, places), delta); // Returns: 256.02 !
value = 260.775d;
places = 2;
expected = 260.78d;
Assert.assertEquals(expected, Round.round(value, places), delta);
Assert.assertNotEquals(expected, Round.roundNotPrecise(value, places), delta); // Returns: 260.77 !
Assert.assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 260.77 !
Assert.assertEquals(expected, Precision.round(value, places), delta);
Assert.assertNotEquals(expected, DoubleRounder.round(value, places), delta); // Returns: 260.77 !
assertEquals(expected, Round.round(value, places), delta);
assertNotEquals(expected, Round.roundNotPrecise(value, places), delta); // Returns: 260.77 !
assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 260.77 !
assertEquals(expected, Precision.round(value, places), delta);
assertNotEquals(expected, DoubleRounder.round(value, places), delta); // Returns: 260.77 !
value = 90080070060.1d;
places = 9;
expected = 90080070060.1d;
Assert.assertEquals(expected, Round.round(value, places), delta);
Assert.assertEquals(expected, Round.roundNotPrecise(value, places), delta);
Assert.assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 9.223372036854776E9 !
Assert.assertEquals(expected, Precision.round(value, places), delta);
Assert.assertEquals(expected, DoubleRounder.round(value, places), delta);
assertEquals(expected, Round.round(value, places), delta);
assertEquals(expected, Round.roundNotPrecise(value, places), delta);
assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 9.223372036854776E9 !
assertEquals(expected, Precision.round(value, places), delta);
assertEquals(expected, DoubleRounder.round(value, places), delta);
}
}

View File

@ -3,13 +3,13 @@ package com.baeldung.money;
import org.javamoney.moneta.FastMoney;
import org.javamoney.moneta.Money;
import org.javamoney.moneta.format.CurrencyStyle;
import org.junit.Ignore;
import org.junit.Test;
import javax.money.CurrencyUnit;
import javax.money.Monetary;
import javax.money.MonetaryAmount;
import javax.money.UnknownCurrencyException;
import javax.money.convert.ConversionQueryBuilder;
import javax.money.convert.CurrencyConversion;
import javax.money.convert.MonetaryConversions;
import javax.money.format.AmountFormatQueryBuilder;
@ -19,7 +19,11 @@ import java.util.Arrays;
import java.util.List;
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 {
@ -36,17 +40,16 @@ public class JavaMoneyUnitTest {
@Test(expected = UnknownCurrencyException.class)
public void givenCurrencyCode_whenNoExist_thanThrowsError() {
Monetary.getCurrency("AAA");
fail(); // if no exception
}
@Test
public void givenAmounts_whenStringified_thanEquals() {
CurrencyUnit usd = Monetary.getCurrency("USD");
MonetaryAmount fstAmtUSD = Monetary
.getDefaultAmountFactory()
.setCurrency(usd)
.setNumber(200)
.create();
.getDefaultAmountFactory()
.setCurrency(usd)
.setNumber(200)
.create();
Money moneyof = Money.of(12, usd);
FastMoney fastmoneyof = FastMoney.of(2, usd);
@ -59,10 +62,10 @@ public class JavaMoneyUnitTest {
@Test
public void givenCurrencies_whenCompared_thanNotequal() {
MonetaryAmount oneDolar = Monetary
.getDefaultAmountFactory()
.setCurrency("USD")
.setNumber(1)
.create();
.getDefaultAmountFactory()
.setCurrency("USD")
.setNumber(1)
.create();
Money oneEuro = Money.of(1, "EUR");
assertFalse(oneEuro.equals(FastMoney.of(1, "EUR")));
@ -72,10 +75,10 @@ public class JavaMoneyUnitTest {
@Test(expected = ArithmeticException.class)
public void givenAmount_whenDivided_thanThrowsException() {
MonetaryAmount oneDolar = Monetary
.getDefaultAmountFactory()
.setCurrency("USD")
.setNumber(1)
.create();
.getDefaultAmountFactory()
.setCurrency("USD")
.setNumber(1)
.create();
oneDolar.divide(3);
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"));
Money sumAmtCHF = (Money) monetaryAmounts
.stream()
.reduce(Money.of(0, "CHF"), MonetaryAmount::add);
.stream()
.reduce(Money.of(0, "CHF"), MonetaryAmount::add);
assertEquals("CHF 111.35", sumAmtCHF.toString());
}
@ -97,18 +100,18 @@ public class JavaMoneyUnitTest {
Money moneyof = Money.of(12, usd);
MonetaryAmount fstAmtUSD = Monetary
.getDefaultAmountFactory()
.setCurrency(usd)
.setNumber(200.50)
.create();
.getDefaultAmountFactory()
.setCurrency(usd)
.setNumber(200.50)
.create();
MonetaryAmount oneDolar = Monetary
.getDefaultAmountFactory()
.setCurrency("USD")
.setNumber(1)
.create();
.getDefaultAmountFactory()
.setCurrency("USD")
.setNumber(1)
.create();
Money subtractedAmount = Money
.of(1, "USD")
.subtract(fstAmtUSD);
.of(1, "USD")
.subtract(fstAmtUSD);
MonetaryAmount multiplyAmount = oneDolar.multiply(0.25);
MonetaryAmount divideAmount = oneDolar.divide(0.25);
@ -124,22 +127,23 @@ public class JavaMoneyUnitTest {
@Test
public void givenAmount_whenRounded_thanEquals() {
MonetaryAmount fstAmtEUR = Monetary
.getDefaultAmountFactory()
.setCurrency("EUR")
.setNumber(1.30473908)
.create();
.getDefaultAmountFactory()
.setCurrency("EUR")
.setNumber(1.30473908)
.create();
MonetaryAmount roundEUR = fstAmtEUR.with(Monetary.getDefaultRounding());
assertEquals("EUR 1.30473908", fstAmtEUR.toString());
assertEquals("EUR 1.3", roundEUR.toString());
}
@Test
@Ignore("Currency providers are not always available")
public void givenAmount_whenConversion_thenNotNull() {
MonetaryAmount oneDollar = Monetary
.getDefaultAmountFactory()
.setCurrency("USD")
.setNumber(1)
.create();
.getDefaultAmountFactory()
.setCurrency("USD")
.setNumber(1)
.create();
CurrencyConversion conversionEUR = MonetaryConversions.getConversion("EUR");
@ -152,10 +156,10 @@ public class JavaMoneyUnitTest {
@Test
public void givenLocale_whenFormatted_thanEquals() {
MonetaryAmount oneDollar = Monetary
.getDefaultAmountFactory()
.setCurrency("USD")
.setNumber(1)
.create();
.getDefaultAmountFactory()
.setCurrency("USD")
.setNumber(1)
.create();
MonetaryAmountFormat formatUSD = MonetaryFormats.getAmountFormat(Locale.US);
String usFormatted = formatUSD.format(oneDollar);
@ -167,16 +171,16 @@ public class JavaMoneyUnitTest {
@Test
public void givenAmount_whenCustomFormat_thanEquals() {
MonetaryAmount oneDollar = Monetary
.getDefaultAmountFactory()
.setCurrency("USD")
.setNumber(1)
.create();
.getDefaultAmountFactory()
.setCurrency("USD")
.setNumber(1)
.create();
MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder
.of(Locale.US)
.set(CurrencyStyle.NAME)
.set("pattern", "00000.00 ¤")
.build());
.of(Locale.US)
.set(CurrencyStyle.NAME)
.set("pattern", "00000.00 ¤")
.build());
String customFormatted = customFormat.format(oneDollar);
assertNotNull(customFormat);

View File

@ -1,12 +1,14 @@
package com.baeldung.regexp;
import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.*;
import org.junit.Test;
import java.util.regex.Matcher;
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 {
@Test
@ -38,17 +40,17 @@ public class EscapingCharsTest {
String strInput = "foo|bar|hello|world";
String strRegex = "|";
assertEquals(4,strInput.split(Pattern.quote(strRegex)).length);
assertEquals(4, strInput.split(Pattern.quote(strRegex)).length);
}
@Test
public void givenRegexWithDollar_whenReplacing_thenNotReplace() {
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 strReplacement = "£";
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);
Matcher m = p.matcher(strInput);
@ -58,14 +60,14 @@ public class EscapingCharsTest {
@Test
public void givenRegexWithDollarEsc_whenReplacing_thenReplace() {
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 strReplacement = "£";
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);
Matcher m = p.matcher(strInput);
assertEquals(output,m.replaceAll(strReplacement));
assertEquals(output, m.replaceAll(strReplacement));
}
}

View File

@ -4,7 +4,11 @@ import org.junit.Assert;
import org.junit.Before;
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.util.List;
import java.util.Map;

View File

@ -1,6 +1,6 @@
package com.baeldung.serialization;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@ -8,57 +8,57 @@ import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class PersonUnitTest {
@Test
public void whenSerializingAndDeserializing_ThenObjectIsTheSame() throws IOException, ClassNotFoundException {
Person p = new Person();
p.setAge(20);
p.setName("Joe");
@Test
public void whenSerializingAndDeserializing_ThenObjectIsTheSame() throws IOException, ClassNotFoundException {
Person p = new Person();
p.setAge(20);
p.setName("Joe");
FileOutputStream fileOutputStream = new FileOutputStream("yofile.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(p);
objectOutputStream.flush();
objectOutputStream.close();
FileOutputStream fileOutputStream = new FileOutputStream("yofile.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(p);
objectOutputStream.flush();
objectOutputStream.close();
FileInputStream fileInputStream = new FileInputStream("yofile.txt");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Person p2 = (Person) objectInputStream.readObject();
objectInputStream.close();
FileInputStream fileInputStream = new FileInputStream("yofile.txt");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Person p2 = (Person) objectInputStream.readObject();
objectInputStream.close();
assertTrue(p2.getAge() == p.getAge());
assertTrue(p2.getName().equals(p.getName()));
}
assertTrue(p2.getAge() == p.getAge());
assertTrue(p2.getName().equals(p.getName()));
}
@Test
public void whenCustomSerializingAndDeserializing_ThenObjectIsTheSame() throws IOException, ClassNotFoundException {
Person p = new Person();
p.setAge(20);
p.setName("Joe");
@Test
public void whenCustomSerializingAndDeserializing_ThenObjectIsTheSame() throws IOException, ClassNotFoundException {
Person p = new Person();
p.setAge(20);
p.setName("Joe");
Address a = new Address();
a.setHouseNumber(1);
Address a = new Address();
a.setHouseNumber(1);
Employee e = new Employee();
e.setPerson(p);
e.setAddress(a);
Employee e = new Employee();
e.setPerson(p);
e.setAddress(a);
FileOutputStream fileOutputStream = new FileOutputStream("yofile2.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(e);
objectOutputStream.flush();
objectOutputStream.close();
FileOutputStream fileOutputStream = new FileOutputStream("yofile2.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(e);
objectOutputStream.flush();
objectOutputStream.close();
FileInputStream fileInputStream = new FileInputStream("yofile2.txt");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Employee e2 = (Employee) objectInputStream.readObject();
objectInputStream.close();
FileInputStream fileInputStream = new FileInputStream("yofile2.txt");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Employee e2 = (Employee) objectInputStream.readObject();
objectInputStream.close();
assertTrue(e2.getPerson().getAge() == e.getPerson().getAge());
assertTrue(e2.getAddress().getHouseNumber() == (e.getAddress().getHouseNumber()));
}
assertTrue(e2.getPerson().getAge() == e.getPerson().getAge());
assertTrue(e2.getAddress().getHouseNumber() == (e.getAddress().getHouseNumber()));
}
}

View File

@ -1,14 +1,14 @@
package com.baeldung.socket;
import static org.junit.Assert.assertEquals;
import java.util.concurrent.Executors;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.concurrent.Executors;
import static org.junit.Assert.assertEquals;
public class EchoIntegrationTest {
private static final Integer PORT = 4444;

View File

@ -1,11 +1,10 @@
package com.baeldung.stackoverflowerror;
import static org.junit.Assert.fail;
import org.junit.Test;
public class CyclicDependancyManualTest {
@Test(expected = StackOverflowError.class)
public void whenInstanciatingClassOne_thenThrowsException() {
ClassOne obj = new ClassOne();
ClassOne obj = new ClassOne();
}
}

View File

@ -1,9 +1,9 @@
package com.baeldung.stackoverflowerror;
import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
public class InfiniteRecursionWithTerminationConditionManualTest {
@Test
public void givenPositiveIntNoOne_whenCalcFact_thenCorrectlyCalc() {
@ -23,9 +23,9 @@ public class InfiniteRecursionWithTerminationConditionManualTest {
@Test(expected = StackOverflowError.class)
public void givenNegativeInt_whenCalcFact_thenThrowsException() {
int numToCalcFactorial = -1;
InfiniteRecursionWithTerminationCondition irtc = new InfiniteRecursionWithTerminationCondition();
int numToCalcFactorial = -1;
InfiniteRecursionWithTerminationCondition irtc = new InfiniteRecursionWithTerminationCondition();
irtc.calculateFactorial(numToCalcFactorial);
irtc.calculateFactorial(numToCalcFactorial);
}
}

View File

@ -1,9 +1,9 @@
package com.baeldung.stackoverflowerror;
import static junit.framework.TestCase.assertEquals;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
public class RecursionWithCorrectTerminationConditionManualTest {
@Test
public void givenNegativeInt_whenCalcFact_thenCorrectlyCalc() {

View File

@ -6,7 +6,7 @@ import org.junit.Test;
public class UnintendedInfiniteRecursionManualTest {
@Test(expected = StackOverflowError.class)
public void givenPositiveIntNoOne_whenCalFact_thenThrowsException() {
int numToCalcFactorial= 1;
int numToCalcFactorial = 1;
UnintendedInfiniteRecursion uir = new UnintendedInfiniteRecursion();
uir.calculateFactorial(numToCalcFactorial);
@ -14,7 +14,7 @@ public class UnintendedInfiniteRecursionManualTest {
@Test(expected = StackOverflowError.class)
public void givenPositiveIntGtOne_whenCalcFact_thenThrowsException() {
int numToCalcFactorial= 2;
int numToCalcFactorial = 2;
UnintendedInfiniteRecursion uir = new UnintendedInfiniteRecursion();
uir.calculateFactorial(numToCalcFactorial);
@ -22,7 +22,7 @@ public class UnintendedInfiniteRecursionManualTest {
@Test(expected = StackOverflowError.class)
public void givenNegativeInt_whenCalcFact_thenThrowsException() {
int numToCalcFactorial= -1;
int numToCalcFactorial = -1;
UnintendedInfiniteRecursion uir = new UnintendedInfiniteRecursion();
uir.calculateFactorial(numToCalcFactorial);

View File

@ -7,7 +7,9 @@ import java.util.Arrays;
import java.util.List;
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;
public class StrategyDesignPatternUnitTest {
@ -26,7 +28,7 @@ public class StrategyDesignPatternUnitTest {
public void shouldDivideByTwo_WhenApplyingStaffDiscounterWithAnonyousTypes() {
Discounter staffDiscounter = new Discounter() {
@Override
public BigDecimal apply( BigDecimal amount) {
public BigDecimal apply(BigDecimal amount) {
return amount.multiply(BigDecimal.valueOf(0.5));
}
};

View File

@ -5,7 +5,6 @@ import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.function.Supplier;
import java.util.stream.Collectors;
@ -22,8 +21,8 @@ public class InfiniteStreamUnitTest {
//when
List<Integer> collect = infiniteStream
.limit(10)
.collect(Collectors.toList());
.limit(10)
.collect(Collectors.toList());
//then
assertEquals(collect, Arrays.asList(0, 2, 4, 6, 8, 10, 12, 14, 16, 18));
@ -37,9 +36,9 @@ public class InfiniteStreamUnitTest {
//when
List<UUID> randomInts = infiniteStreamOfRandomUUID
.skip(10)
.limit(10)
.collect(Collectors.toList());
.skip(10)
.limit(10)
.collect(Collectors.toList());
//then
assertEquals(randomInts.size(), 10);

View File

@ -1,10 +1,12 @@
package com.baeldung.stream;
import org.junit.Test;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class StreamAddUnitTest {
@ -25,7 +27,7 @@ public class StreamAddUnitTest {
Stream<Integer> newStream = Stream.concat(Stream.of(99), anStream);
assertEquals(newStream.findFirst()
.get(), (Integer) 99);
.get(), (Integer) 99);
}
@Test
@ -38,7 +40,7 @@ public class StreamAddUnitTest {
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());
result.add(index, elem);
return result.stream();

View File

@ -1,68 +1,66 @@
package com.baeldung.string;
import static org.junit.Assert.*;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.baeldung.string.JoinerSplitter;
import static org.junit.Assert.assertEquals;
public class JoinerSplitterUnitTest {
@Test
public void provided_array_convert_to_stream_and_convert_to_string() {
@Test
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);
}
String result = JoinerSplitter.join(programming_languages);
assertEquals(result, expectation);
}
@Test
public void givenArray_transformedToStream_convertToPrefixPostfixString() {
@Test
public void givenArray_transformedToStream_convertToPrefixPostfixString() {
String[] programming_languages = {"java", "python",
"nodejs", "ruby"};
String expectation = "[java,python,nodejs,ruby]";
String[] programming_languages = {"java", "python",
"nodejs", "ruby"};
String expectation = "[java,python,nodejs,ruby]";
String result = JoinerSplitter.joinWithPrefixPostFix(programming_languages);
assertEquals(result, expectation);
}
String result = JoinerSplitter.joinWithPrefixPostFix(programming_languages);
assertEquals(result, expectation);
}
@Test
public void givenString_transformedToStream_convertToList() {
@Test
public void givenString_transformedToStream_convertToList() {
String programming_languages = "java,python,nodejs,ruby";
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> expectation = new ArrayList<String>();
expectation.add("java");
expectation.add("python");
expectation.add("nodejs");
expectation.add("ruby");
List<String> result = JoinerSplitter.split(programming_languages);
List<String> result = JoinerSplitter.split(programming_languages);
assertEquals(result, expectation);
}
assertEquals(result, expectation);
}
@Test
public void givenString_transformedToStream_convertToListOfChar() {
@Test
public void givenString_transformedToStream_convertToListOfChar() {
String programming_languages = "java,python,nodejs,ruby";
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> 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);
List<Character> result = JoinerSplitter.splitToListOfChar(programming_languages);
assertEquals(result, expectation);
}
}
}

View File

@ -2,7 +2,6 @@ package com.baeldung.string;
import com.google.common.base.Splitter;
import org.apache.commons.lang.StringUtils;
import org.assertj.core.util.Lists;
import org.junit.Test;
import java.util.List;

View File

@ -1,9 +1,10 @@
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.assertNotEquals;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
public class StringHelperUnitTest {