* 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; 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());
} }
} }

View File

@ -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

View File

@ -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());
} }

View File

@ -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 {

View File

@ -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

View File

@ -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

View File

@ -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;
@ -13,59 +16,59 @@ public class RoundTest {
@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);
} }
} }

View File

@ -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,7 +40,6 @@ 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
@ -134,6 +137,7 @@ public class JavaMoneyUnitTest {
} }
@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()

View File

@ -1,12 +1,14 @@
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
@ -38,7 +40,7 @@ public class EscapingCharsTest {
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
@ -66,6 +68,6 @@ public class EscapingCharsTest {
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));
} }
} }

View File

@ -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;

View File

@ -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,7 +8,7 @@ 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 {

View File

@ -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;

View File

@ -1,6 +1,5 @@
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 {

View File

@ -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() {

View File

@ -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() {

View File

@ -6,7 +6,7 @@ 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);
@ -14,7 +14,7 @@ public class UnintendedInfiniteRecursionManualTest {
@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);
@ -22,7 +22,7 @@ public class UnintendedInfiniteRecursionManualTest {
@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);

View File

@ -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));
} }
}; };

View File

@ -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;

View File

@ -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 {
@ -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();

View File

@ -1,13 +1,11 @@
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 {

View File

@ -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;

View File

@ -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 {