diff --git a/core-java/src/test/java/com/baeldung/list/flattennestedlist/FlattenNestedListUnitTest.java b/core-java/src/test/java/com/baeldung/list/flattennestedlist/FlattenNestedListUnitTest.java index 50abb4bc92..3c8f519082 100644 --- a/core-java/src/test/java/com/baeldung/list/flattennestedlist/FlattenNestedListUnitTest.java +++ b/core-java/src/test/java/com/baeldung/list/flattennestedlist/FlattenNestedListUnitTest.java @@ -14,7 +14,7 @@ import org.hamcrest.collection.IsIterableContainingInOrder; import org.junit.Test; public class FlattenNestedListUnitTest { - List> lol = asList(asList("one:one"), asList("two:one", "two:two", "two:three"), asList("three:one", "three:two", "three:three", "three:four")); + private List> 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 List flattenListOfListsImperatively(List> list) { + private List flattenListOfListsImperatively(List> list) { List ls = new ArrayList<>(); list.forEach(ls::addAll); return ls; } - public List flattenListOfListsStream(List> list) { + private List flattenListOfListsStream(List> list) { return list.stream().flatMap(Collection::stream).collect(Collectors.toList()); } } diff --git a/core-java/src/test/java/com/baeldung/list/listoflist/ListOfListsUnitTest.java b/core-java/src/test/java/com/baeldung/list/listoflist/ListOfListsUnitTest.java index 75f8b8359f..7a23afa12f 100644 --- a/core-java/src/test/java/com/baeldung/list/listoflist/ListOfListsUnitTest.java +++ b/core-java/src/test/java/com/baeldung/list/listoflist/ListOfListsUnitTest.java @@ -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> listOfLists = new ArrayList>(); - private ArrayList penList = new ArrayList<>(); - private ArrayList pencilList = new ArrayList<>(); - private ArrayList rubberList = new ArrayList<>(); + private List> listOfLists = new ArrayList<>(); + private List penList = new ArrayList<>(); + private List pencilList = new ArrayList<>(); + private List 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) 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 @@ -60,17 +61,17 @@ public class ListOfListsUnitTest { ArrayList rubbers = new ArrayList<>(); rubbers.add(new Rubber("Rubber 1")); rubbers.add(new Rubber("Rubber 2")); - + List> list = new ArrayList>(); list.add(pens); list.add(pencils); 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()); } } diff --git a/core-java/src/test/java/com/baeldung/mappedbytebuffer/MappedByteBufferUnitTest.java b/core-java/src/test/java/com/baeldung/mappedbytebuffer/MappedByteBufferUnitTest.java index 22457d196e..3c2d9904d4 100644 --- a/core-java/src/test/java/com/baeldung/mappedbytebuffer/MappedByteBufferUnitTest.java +++ b/core-java/src/test/java/com/baeldung/mappedbytebuffer/MappedByteBufferUnitTest.java @@ -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()); } diff --git a/core-java/src/test/java/com/baeldung/maths/BigDecimalImplTest.java b/core-java/src/test/java/com/baeldung/maths/BigDecimalImplTest.java index 973b987224..788fbd7047 100644 --- a/core-java/src/test/java/com/baeldung/maths/BigDecimalImplTest.java +++ b/core-java/src/test/java/com/baeldung/maths/BigDecimalImplTest.java @@ -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 { diff --git a/core-java/src/test/java/com/baeldung/maths/BigIntegerImplTest.java b/core-java/src/test/java/com/baeldung/maths/BigIntegerImplTest.java index 040c595065..aa8eaa9909 100644 --- a/core-java/src/test/java/com/baeldung/maths/BigIntegerImplTest.java +++ b/core-java/src/test/java/com/baeldung/maths/BigIntegerImplTest.java @@ -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 diff --git a/core-java/src/test/java/com/baeldung/maths/FloatingPointArithmeticTest.java b/core-java/src/test/java/com/baeldung/maths/FloatingPointArithmeticTest.java index 2066f13c6d..84dd41b1d7 100644 --- a/core-java/src/test/java/com/baeldung/maths/FloatingPointArithmeticTest.java +++ b/core-java/src/test/java/com/baeldung/maths/FloatingPointArithmeticTest.java @@ -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 @@ -13,33 +13,33 @@ public class FloatingPointArithmeticTest { double b = 4.88; double c = 21.45; double result = 39.55; - + double abc = a + b + c; double acb = a + c + b; - + Assert.assertEquals(result, abc, 0); Assert.assertNotEquals(result, acb, 0); - + double ab = 18.1; double ac = 34.67; - + double ab_c = ab + c; double ac_b = ac + b; - + Assert.assertEquals(result, ab_c, 0); Assert.assertNotEquals(result, ac_b, 0); - + BigDecimal d = new BigDecimal(String.valueOf(a)); BigDecimal e = new BigDecimal(String.valueOf(b)); BigDecimal f = new BigDecimal(String.valueOf(c)); BigDecimal sum = new BigDecimal("39.55"); - + BigDecimal def = d.add(e).add(f); BigDecimal dfe = d.add(f).add(e); - + Assert.assertEquals(0, def.compareTo(sum)); Assert.assertEquals(0, dfe.compareTo(sum)); - + Assert.assertNotEquals(0, sum.compareTo(new BigDecimal(String.valueOf(acb)))); } } diff --git a/core-java/src/test/java/com/baeldung/maths/RoundTest.java b/core-java/src/test/java/com/baeldung/maths/RoundTest.java index 95db146414..ead21265c0 100644 --- a/core-java/src/test/java/com/baeldung/maths/RoundTest.java +++ b/core-java/src/test/java/com/baeldung/maths/RoundTest.java @@ -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; @@ -12,60 +15,60 @@ public class RoundTest { private double expected = 2.03d; @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); - + public void givenDecimalNumber_whenRoundToNDecimalPlaces_thenGetExpectedResult() { + 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 ! - - value = 260.775d; + + 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); } } diff --git a/core-java/src/test/java/com/baeldung/money/JavaMoneyUnitTest.java b/core-java/src/test/java/com/baeldung/money/JavaMoneyUnitTest.java index 69906543a9..8948d1ebf7 100644 --- a/core-java/src/test/java/com/baeldung/money/JavaMoneyUnitTest.java +++ b/core-java/src/test/java/com/baeldung/money/JavaMoneyUnitTest.java @@ -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 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); diff --git a/core-java/src/test/java/com/baeldung/regexp/EscapingCharsTest.java b/core-java/src/test/java/com/baeldung/regexp/EscapingCharsTest.java index f8dbde4c4f..47c9cfc621 100644 --- a/core-java/src/test/java/com/baeldung/regexp/EscapingCharsTest.java +++ b/core-java/src/test/java/com/baeldung/regexp/EscapingCharsTest.java @@ -1,71 +1,73 @@ 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 public void givenRegexWithDot_whenMatchingStr_thenMatches() { String strInput = "foof"; String strRegex = "foo."; - + assertEquals(true, strInput.matches(strRegex)); } - + @Test public void givenRegexWithDotEsc_whenMatchingStr_thenNotMatching() { String strInput = "foof"; String strRegex = "foo\\."; - + assertEquals(false, strInput.matches(strRegex)); } - + @Test public void givenRegexWithPipeEscaped_whenSplitStr_thenSplits() { String strInput = "foo|bar|hello|world"; String strRegex = "\\Q|\\E"; - + assertEquals(4, strInput.split(strRegex).length); } - + @Test public void givenRegexWithPipeEscQuoteMeth_whenSplitStr_thenSplits() { 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); - + assertThat(output, not(equalTo(m.replaceAll(strReplacement)))); } - + @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)); } } diff --git a/core-java/src/test/java/com/baeldung/scripting/NashornUnitTest.java b/core-java/src/test/java/com/baeldung/scripting/NashornUnitTest.java index fe1a7d4bd4..7f165cec86 100644 --- a/core-java/src/test/java/com/baeldung/scripting/NashornUnitTest.java +++ b/core-java/src/test/java/com/baeldung/scripting/NashornUnitTest.java @@ -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; diff --git a/core-java/src/test/java/com/baeldung/serialization/PersonUnitTest.java b/core-java/src/test/java/com/baeldung/serialization/PersonUnitTest.java index 748898ff3e..dbcdf0ec9d 100644 --- a/core-java/src/test/java/com/baeldung/serialization/PersonUnitTest.java +++ b/core-java/src/test/java/com/baeldung/serialization/PersonUnitTest.java @@ -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())); + } } diff --git a/core-java/src/test/java/com/baeldung/socket/EchoIntegrationTest.java b/core-java/src/test/java/com/baeldung/socket/EchoIntegrationTest.java index 7ac8e0a97a..2bf6d142bb 100644 --- a/core-java/src/test/java/com/baeldung/socket/EchoIntegrationTest.java +++ b/core-java/src/test/java/com/baeldung/socket/EchoIntegrationTest.java @@ -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; diff --git a/core-java/src/test/java/com/baeldung/stackoverflowerror/CyclicDependancyManualTest.java b/core-java/src/test/java/com/baeldung/stackoverflowerror/CyclicDependancyManualTest.java index 9b86770453..1f1de9149a 100644 --- a/core-java/src/test/java/com/baeldung/stackoverflowerror/CyclicDependancyManualTest.java +++ b/core-java/src/test/java/com/baeldung/stackoverflowerror/CyclicDependancyManualTest.java @@ -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(); } } diff --git a/core-java/src/test/java/com/baeldung/stackoverflowerror/InfiniteRecursionWithTerminationConditionManualTest.java b/core-java/src/test/java/com/baeldung/stackoverflowerror/InfiniteRecursionWithTerminationConditionManualTest.java index 996d0e9017..3530fa1c3e 100644 --- a/core-java/src/test/java/com/baeldung/stackoverflowerror/InfiniteRecursionWithTerminationConditionManualTest.java +++ b/core-java/src/test/java/com/baeldung/stackoverflowerror/InfiniteRecursionWithTerminationConditionManualTest.java @@ -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); } } diff --git a/core-java/src/test/java/com/baeldung/stackoverflowerror/RecursionWithCorrectTerminationConditionManualTest.java b/core-java/src/test/java/com/baeldung/stackoverflowerror/RecursionWithCorrectTerminationConditionManualTest.java index a4269aef56..858cec2ffa 100644 --- a/core-java/src/test/java/com/baeldung/stackoverflowerror/RecursionWithCorrectTerminationConditionManualTest.java +++ b/core-java/src/test/java/com/baeldung/stackoverflowerror/RecursionWithCorrectTerminationConditionManualTest.java @@ -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() { diff --git a/core-java/src/test/java/com/baeldung/stackoverflowerror/UnintendedInfiniteRecursionManualTest.java b/core-java/src/test/java/com/baeldung/stackoverflowerror/UnintendedInfiniteRecursionManualTest.java index 65126efff2..1e1c245d4d 100644 --- a/core-java/src/test/java/com/baeldung/stackoverflowerror/UnintendedInfiniteRecursionManualTest.java +++ b/core-java/src/test/java/com/baeldung/stackoverflowerror/UnintendedInfiniteRecursionManualTest.java @@ -6,25 +6,25 @@ 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); } - + @Test(expected = StackOverflowError.class) public void givenPositiveIntGtOne_whenCalcFact_thenThrowsException() { - int numToCalcFactorial= 2; + int numToCalcFactorial = 2; UnintendedInfiniteRecursion uir = new UnintendedInfiniteRecursion(); - + uir.calculateFactorial(numToCalcFactorial); } - + @Test(expected = StackOverflowError.class) public void givenNegativeInt_whenCalcFact_thenThrowsException() { - int numToCalcFactorial= -1; + int numToCalcFactorial = -1; UnintendedInfiniteRecursion uir = new UnintendedInfiniteRecursion(); - + uir.calculateFactorial(numToCalcFactorial); } } diff --git a/core-java/src/test/java/com/baeldung/strategy/StrategyDesignPatternUnitTest.java b/core-java/src/test/java/com/baeldung/strategy/StrategyDesignPatternUnitTest.java index 7ca1d000be..2238012e20 100644 --- a/core-java/src/test/java/com/baeldung/strategy/StrategyDesignPatternUnitTest.java +++ b/core-java/src/test/java/com/baeldung/strategy/StrategyDesignPatternUnitTest.java @@ -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)); } }; diff --git a/core-java/src/test/java/com/baeldung/stream/InfiniteStreamUnitTest.java b/core-java/src/test/java/com/baeldung/stream/InfiniteStreamUnitTest.java index fa482b8dfe..84c8992557 100644 --- a/core-java/src/test/java/com/baeldung/stream/InfiniteStreamUnitTest.java +++ b/core-java/src/test/java/com/baeldung/stream/InfiniteStreamUnitTest.java @@ -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 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 randomInts = infiniteStreamOfRandomUUID - .skip(10) - .limit(10) - .collect(Collectors.toList()); + .skip(10) + .limit(10) + .collect(Collectors.toList()); //then assertEquals(randomInts.size(), 10); diff --git a/core-java/src/test/java/com/baeldung/stream/StreamAddUnitTest.java b/core-java/src/test/java/com/baeldung/stream/StreamAddUnitTest.java index 8176d6a60c..d0745b8bc9 100644 --- a/core-java/src/test/java/com/baeldung/stream/StreamAddUnitTest.java +++ b/core-java/src/test/java/com/baeldung/stream/StreamAddUnitTest.java @@ -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 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 Stream insertInStream(Stream stream, T elem, int index) { + private Stream insertInStream(Stream stream, T elem, int index) { List result = stream.collect(Collectors.toList()); result.add(index, elem); return result.stream(); diff --git a/core-java/src/test/java/com/baeldung/stream/StreamApiTest.java b/core-java/src/test/java/com/baeldung/stream/StreamApiTest.java index 68b5fc83a5..f4d0ac6b08 100644 --- a/core-java/src/test/java/com/baeldung/stream/StreamApiTest.java +++ b/core-java/src/test/java/com/baeldung/stream/StreamApiTest.java @@ -20,13 +20,13 @@ public class StreamApiTest { assertEquals("Sean", last); } - + @Test public void givenInfiniteStream_whenGetInfiniteStreamLastElementUsingReduce_thenReturnLastElement() { int last = StreamApi.getInfiniteStreamLastElementUsingReduce(); assertEquals(19, last); } - + @Test public void givenListAndCount_whenGetLastElementUsingSkip_thenReturnLastElement() { List valueList = new ArrayList<>(); diff --git a/core-java/src/test/java/com/baeldung/string/JoinerSplitterUnitTest.java b/core-java/src/test/java/com/baeldung/string/JoinerSplitterUnitTest.java index d58c1026d0..8901bcf9db 100644 --- a/core-java/src/test/java/com/baeldung/string/JoinerSplitterUnitTest.java +++ b/core-java/src/test/java/com/baeldung/string/JoinerSplitterUnitTest.java @@ -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 result = JoinerSplitter.join(programming_languages); - assertEquals(result, expectation); - } - - @Test - public void givenArray_transformedToStream_convertToPrefixPostfixString() { + 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); - } - - @Test - public void givenString_transformedToStream_convertToList() { + String result = JoinerSplitter.join(programming_languages); + assertEquals(result, expectation); + } - String programming_languages = "java,python,nodejs,ruby"; - - List expectation = new ArrayList(); - expectation.add("java"); - expectation.add("python"); - expectation.add("nodejs"); - expectation.add("ruby"); - - List result = JoinerSplitter.split(programming_languages); - - assertEquals(result, expectation); - } - - @Test - public void givenString_transformedToStream_convertToListOfChar() { + @Test + public void givenArray_transformedToStream_convertToPrefixPostfixString() { + + String[] programming_languages = {"java", "python", + "nodejs", "ruby"}; + 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"; + + List expectation = new ArrayList(); + expectation.add("java"); + expectation.add("python"); + expectation.add("nodejs"); + expectation.add("ruby"); + + List result = JoinerSplitter.split(programming_languages); + + assertEquals(result, expectation); + } + + @Test + public void givenString_transformedToStream_convertToListOfChar() { + + String programming_languages = "java,python,nodejs,ruby"; + + List expectation = new ArrayList(); + char[] charArray = programming_languages.toCharArray(); + for (char c : charArray) { + expectation.add(c); + } + + List result = JoinerSplitter.splitToListOfChar(programming_languages); + assertEquals(result, expectation); + + } - String programming_languages = "java,python,nodejs,ruby"; - - List expectation = new ArrayList(); - char[] charArray = programming_languages.toCharArray(); - for (char c : charArray) { - expectation.add(c); - } - - List result = JoinerSplitter.splitToListOfChar(programming_languages); - assertEquals(result, expectation); - - } - } diff --git a/core-java/src/test/java/com/baeldung/string/SplitUnitTest.java b/core-java/src/test/java/com/baeldung/string/SplitUnitTest.java index 5d8703db1a..83fd253b97 100644 --- a/core-java/src/test/java/com/baeldung/string/SplitUnitTest.java +++ b/core-java/src/test/java/com/baeldung/string/SplitUnitTest.java @@ -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; diff --git a/core-java/src/test/java/com/baeldung/string/StringHelperUnitTest.java b/core-java/src/test/java/com/baeldung/string/StringHelperUnitTest.java index 427f02ba16..917ed1c937 100644 --- a/core-java/src/test/java/com/baeldung/string/StringHelperUnitTest.java +++ b/core-java/src/test/java/com/baeldung/string/StringHelperUnitTest.java @@ -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 { @@ -67,7 +68,7 @@ public class StringHelperUnitTest { assertEquals("abc", StringHelper.removeLastCharOptional(WHITE_SPACE_AT_THE_END_STRING)); assertEquals("abc", StringHelper.removeLastCharRegexOptional(WHITE_SPACE_AT_THE_END_STRING)); } - + @Test public void givenStringWithNewLineAtTheEnd_whenSubstring_thenGetStringWithoutNewLine() { 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)); assertNotEquals("abc", StringHelper.removeLastCharRegexOptional(NEW_LINE_AT_THE_END_STRING)); } - + @Test public void givenMultiLineString_whenSubstring_thenGetStringWithoutNewLine() { assertEquals("abc\nde", StringHelper.removeLastChar(MULTIPLE_LINES_STRING));