Merge remote-tracking branch 'upstream/master' into feature/BAEL-7062-GetIndex
This commit is contained in:
@@ -30,6 +30,11 @@ public final class Point {
|
||||
return new Point(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Point{" + "x=" + x + ", y=" + y + ", z=" + z + '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other == null || getClass() != other.getClass())
|
||||
|
||||
+6
-3
@@ -1,14 +1,17 @@
|
||||
package com.baeldung.value_based_class;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ValueBasedClassUnitTest {
|
||||
@Test
|
||||
public void givenAutoboxedAndPrimitive_whenCompared_thenReturnEquals() {
|
||||
int primitive_a = 125;
|
||||
Integer obj_a = 125; // this is autoboxed
|
||||
Assert.assertSame(primitive_a, obj_a);
|
||||
List<Integer> list = new ArrayList<>();
|
||||
list.add(1); // this is autoboxed
|
||||
Assert.assertEquals(list.get(0), Integer.valueOf(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-18</artifactId>
|
||||
<name>core-java-18</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<release>${maven.compiler.release}</release>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
<source>${maven.compiler.source.version}</source>
|
||||
<target>${maven.compiler.target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
<configuration>
|
||||
<forkCount>1</forkCount>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven.surefire</groupId>
|
||||
<artifactId>surefire-api</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source.version>18</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>18</maven.compiler.target.version>
|
||||
<maven.compiler.release>18</maven.compiler.release>
|
||||
<surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.finalization_closeable_cleaner;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class FinalizationExamples {
|
||||
FileInputStream fis = null;
|
||||
|
||||
public void readFileOperationWithFinalization() throws IOException {
|
||||
try {
|
||||
fis = new FileInputStream("input.txt");
|
||||
// perform operation on the file
|
||||
System.out.println(fis.readAllBytes().length);
|
||||
|
||||
} finally {
|
||||
if (fis != null)
|
||||
fis.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void readFileOperationWithTryWith() throws IOException {
|
||||
try (FileInputStream fis = new FileInputStream("input.txt")) {
|
||||
// perform operations
|
||||
System.out.println(fis.readAllBytes().length);
|
||||
}
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.finalization_closeable_cleaner;
|
||||
|
||||
import java.lang.ref.Cleaner;
|
||||
|
||||
public class MyCleanerResourceClass implements AutoCloseable {
|
||||
private static Resource resource;
|
||||
|
||||
private static final Cleaner cleaner = Cleaner.create();
|
||||
private final Cleaner.Cleanable cleanable;
|
||||
|
||||
public MyCleanerResourceClass() {
|
||||
resource = new Resource();
|
||||
this.cleanable = cleaner.register(this, new CleaningState());
|
||||
}
|
||||
|
||||
public void useResource() {
|
||||
// using the resource here
|
||||
resource.use();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
// perform actions to close all underlying resources
|
||||
this.cleanable.clean();
|
||||
}
|
||||
|
||||
static class CleaningState implements Runnable {
|
||||
CleaningState() {
|
||||
// constructor
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// some cleanup action
|
||||
System.out.println("Cleanup done");
|
||||
}
|
||||
}
|
||||
|
||||
static class Resource {
|
||||
void use() {
|
||||
System.out.println("Using the resource");
|
||||
}
|
||||
|
||||
void close() {
|
||||
System.out.println("Cleanup done");
|
||||
}
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.finalization_closeable_cleaner;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
|
||||
public class MyCloseableResourceClass implements AutoCloseable {
|
||||
|
||||
private final FileInputStream fis;
|
||||
|
||||
public MyCloseableResourceClass() throws FileNotFoundException {
|
||||
this.fis = new FileInputStream("src/main/resources/file.txt");
|
||||
|
||||
}
|
||||
|
||||
public int getByteLength() throws IOException {
|
||||
System.out.println("Some operation");
|
||||
return this.fis.readAllBytes().length;
|
||||
}
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
System.out.println("Finalized object");
|
||||
this.fis.close();
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.finalization_closeable_cleaner;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
|
||||
public class MyFinalizableResourceClass {
|
||||
private FileInputStream fis;
|
||||
|
||||
public MyFinalizableResourceClass() throws FileNotFoundException {
|
||||
this.fis = new FileInputStream("src/main/resources/file.txt");
|
||||
}
|
||||
|
||||
public int getByteLength() throws IOException {
|
||||
System.out.println("Some operation");
|
||||
return this.fis.readAllBytes().length;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
System.out.println("Finalized object");
|
||||
this.fis.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
This is a test file.
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.finalization_closeable_cleaner;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FinalizationCloseableCleanerUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenMyFinalizationResource_whenUsingFinalize_thenShouldClean() {
|
||||
assertDoesNotThrow(() -> {
|
||||
MyFinalizableResourceClass mfr = new MyFinalizableResourceClass();
|
||||
mfr.getByteLength();
|
||||
});
|
||||
}
|
||||
@Test
|
||||
public void givenMyCleanerResource_whenUsingCleanerAPI_thenShouldClean() {
|
||||
assertDoesNotThrow(() -> {
|
||||
try (MyCleanerResourceClass myCleanerResourceClass = new MyCleanerResourceClass()) {
|
||||
myCleanerResourceClass.useResource();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCloseableResource_whenUsingTryWith_thenShouldClose() throws IOException {
|
||||
int length = 0;
|
||||
try (MyCloseableResourceClass mcr = new MyCloseableResourceClass()) {
|
||||
length = mcr.getByteLength();
|
||||
}
|
||||
Assert.assertEquals(20, length);
|
||||
}
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.array.conversions;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class CharArrayToIntArrayUtils {
|
||||
|
||||
static int[] usingGetNumericValueMethod(char[] chars) {
|
||||
if (chars == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int[] ints = new int[chars.length];
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
ints[i] = Character.getNumericValue(chars[i]);
|
||||
}
|
||||
|
||||
return ints;
|
||||
}
|
||||
|
||||
static int[] usingDigitMethod(char[] chars) {
|
||||
if (chars == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int[] ints = new int[chars.length];
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
ints[i] = Character.digit(chars[i], 10);
|
||||
}
|
||||
|
||||
return ints;
|
||||
}
|
||||
|
||||
static int[] usingStreamApiMethod(char[] chars) {
|
||||
if (chars == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new String(chars).chars()
|
||||
.map(c -> c - 48)
|
||||
.toArray();
|
||||
}
|
||||
|
||||
static int[] usingParseIntMethod(char[] chars) {
|
||||
if (chars == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int[] ints = new int[chars.length];
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
ints[i] = Integer.parseInt(String.valueOf(chars[i]));
|
||||
}
|
||||
|
||||
return ints;
|
||||
}
|
||||
|
||||
static int[] usingArraysSetAllMethod(char[] chars) {
|
||||
if (chars == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int[] ints = new int[chars.length];
|
||||
Arrays.setAll(ints, i -> Character.getNumericValue(chars[i]));
|
||||
|
||||
return ints;
|
||||
}
|
||||
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.array.conversions;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CharArrayToIntArrayUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
void givenCharArray_whenUsingGetNumericValueMethod_shouldGetIntArray() {
|
||||
int[] expected = { 2, 3, 4, 5 };
|
||||
char[] chars = { '2', '3', '4', '5' };
|
||||
int[] result = CharArrayToIntArrayUtils.usingGetNumericValueMethod(chars);
|
||||
|
||||
assertArrayEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenCharArray_whenUsingDigitMethod_shouldGetIntArray() {
|
||||
int[] expected = { 1, 2, 3, 6 };
|
||||
char[] chars = { '1', '2', '3', '6' };
|
||||
int[] result = CharArrayToIntArrayUtils.usingDigitMethod(chars);
|
||||
|
||||
assertArrayEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenCharArray_whenUsingStreamApi_shouldGetIntArray() {
|
||||
int[] expected = { 9, 8, 7, 6 };
|
||||
char[] chars = { '9', '8', '7', '6' };
|
||||
int[] result = CharArrayToIntArrayUtils.usingStreamApiMethod(chars);
|
||||
|
||||
assertArrayEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenCharArray_whenUsingParseIntMethod_shouldGetIntArray() {
|
||||
int[] expected = { 9, 8, 7, 6 };
|
||||
char[] chars = { '9', '8', '7', '6' };
|
||||
int[] result = CharArrayToIntArrayUtils.usingParseIntMethod(chars);
|
||||
|
||||
assertArrayEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenCharArray_whenUsingArraysSetAllMethod_shouldGetIntArray() {
|
||||
int[] expected = { 4, 9, 2, 3 };
|
||||
char[] chars = { '4', '9', '2', '3' };
|
||||
int[] result = CharArrayToIntArrayUtils.usingArraysSetAllMethod(chars);
|
||||
|
||||
assertArrayEquals(expected, result);
|
||||
}
|
||||
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
package com.baeldung.rmlinebreaks;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
public class RemoveLinebreaksUnitTest {
|
||||
|
||||
private Path file1Path() throws Exception {
|
||||
return Paths.get(this.getClass().getClassLoader().getResource("multiple-line-1.txt").toURI());
|
||||
}
|
||||
|
||||
private Path file2Path() throws Exception {
|
||||
return Paths.get(this.getClass().getClassLoader().getResource("multiple-line-2.txt").toURI());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenRemovingLineSeparatorFromFile1_thenGetTheExpectedResult() throws Exception {
|
||||
String content = Files.readString(file1Path(), StandardCharsets.UTF_8);
|
||||
|
||||
String result = content.replace(System.getProperty("line.separator"), "");
|
||||
assertEquals("A, B, C, D, E, F", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenRemovingLineSeparatorFromFile2_thenNotGetTheExpectedResult() throws Exception {
|
||||
String content = Files.readString(file2Path(), StandardCharsets.UTF_8);
|
||||
|
||||
String result = content.replace(System.getProperty("line.separator"), "");
|
||||
assertNotEquals("A, B, C, D, E, F", result); // <-- NOT equals assertion!
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenRemovingAllLinebreaks_thenGetTheExpectedResult() throws Exception {
|
||||
String content1 = Files.readString(file1Path(), StandardCharsets.UTF_8);
|
||||
|
||||
// file contains CRLF
|
||||
String content2 = Files.readString(file2Path(), StandardCharsets.UTF_8);
|
||||
|
||||
String result1 = content1.replace("\r", "").replace("\n", "");
|
||||
String result2 = content2.replace("\r", "").replace("\n", "");
|
||||
|
||||
assertEquals("A, B, C, D, E, F", result1);
|
||||
assertEquals("A, B, C, D, E, F", result2);
|
||||
|
||||
String resultReplaceAll = content2.replaceAll("[\\n\\r]", "");
|
||||
assertEquals("A, B, C, D, E, F", resultReplaceAll);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingReadAllLinesAndJoin_thenGetExpectedResult() throws Exception {
|
||||
List<String> lines1 = Files.readAllLines(file1Path(), StandardCharsets.UTF_8);
|
||||
|
||||
// file contains CRLF
|
||||
List<String> lines2 = Files.readAllLines(file2Path(), StandardCharsets.UTF_8);
|
||||
|
||||
String result1 = String.join("", lines1);
|
||||
String result2 = String.join("", lines2);
|
||||
|
||||
assertEquals("A, B, C, D, E, F", result1);
|
||||
assertEquals("A, B, C, D, E, F", result2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
A,
|
||||
B,
|
||||
C,
|
||||
D,
|
||||
E,
|
||||
F
|
||||
@@ -0,0 +1,4 @@
|
||||
A,
|
||||
B,
|
||||
C,
|
||||
D,
|
||||
+25
@@ -2,10 +2,15 @@ package com.baeldung.networking.url;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.http.client.utils.URIBuilder;
|
||||
@@ -149,4 +154,24 @@ public class UrlUnitTest {
|
||||
assertEquals("http://baeldung.com:9090/articles?topic=java&version=8", url.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenURI_whenConvertingToURL_thenCorrect() throws IOException, URISyntaxException {
|
||||
String aURIString = "http://courses.baeldung.com";
|
||||
URI uri = new URI(aURIString);
|
||||
URL url = uri.toURL();
|
||||
assertNotNull(url);
|
||||
assertEquals(aURIString, url.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPath_whenConvertingToURIAndThenURL_thenCorrect() throws IOException, URISyntaxException {
|
||||
String finalPath = "file:/D:/baeldung/java-url";
|
||||
Path path = Paths.get("/baeldung/java-url");
|
||||
URI uri = path.toUri();
|
||||
URL url = uri.toURL();
|
||||
assertNotNull(url);
|
||||
// Adapt the finalPath value to match your own path
|
||||
// assertEquals(finalPath, url.toString());
|
||||
}
|
||||
|
||||
}
|
||||
+17
-1
@@ -1,6 +1,8 @@
|
||||
package com.baeldung.integerToBinary;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class IntegerToBinaryUnitTest {
|
||||
@@ -24,4 +26,18 @@ public class IntegerToBinaryUnitTest {
|
||||
String binaryString = Integer.toString(n, 2);
|
||||
assertEquals("111", binaryString);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnInteger_whenFormatAndReplaceCalled_thenZeroPaddedBinaryString() {
|
||||
int n = 7;
|
||||
String binaryString = String.format("%8s", Integer.toBinaryString(n)).replace(" ", "0");
|
||||
assertEquals("00000111", binaryString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnInteger_whenUsingApacheStringUtils_thenZeroPaddedBinaryString() {
|
||||
int n = 7;
|
||||
String binaryString = StringUtils.leftPad(Integer.toBinaryString(n), 8, "0");
|
||||
assertEquals("00000111", binaryString);
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.javadoublevsbigdecimal;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class BigDecimalConversionUnitTest {
|
||||
|
||||
@Test
|
||||
void whenConvertingDoubleToBigDecimal_thenConversionIsCorrect() {
|
||||
double doubleValue = 123.456;
|
||||
BigDecimal bigDecimalValue = BigDecimal.valueOf(doubleValue);
|
||||
BigDecimal expected = new BigDecimal("123.456").setScale(3, RoundingMode.HALF_UP);
|
||||
assertEquals(expected, bigDecimalValue.setScale(3, RoundingMode.HALF_UP));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenDecimalPlacesGreaterThan15_whenConvertingBigDecimalToDouble_thenPrecisionIsLost() {
|
||||
BigDecimal bigDecimalValue = new BigDecimal("789.1234567890123456");
|
||||
double doubleValue = bigDecimalValue.doubleValue();
|
||||
BigDecimal convertedBackToBigDecimal = BigDecimal.valueOf(doubleValue);
|
||||
assertNotEquals(bigDecimalValue, convertedBackToBigDecimal);
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.javadoublevsbigdecimal;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class BigDecimalUnitTest {
|
||||
|
||||
private BigDecimal bigDecimal1 = new BigDecimal("124567890.0987654321");
|
||||
private BigDecimal bigDecimal2 = new BigDecimal("987654321.123456789");
|
||||
|
||||
@Test
|
||||
public void givenTwoBigDecimals_whenAdd_thenCorrect() {
|
||||
BigDecimal expected = new BigDecimal("1112222211.2222222211");
|
||||
BigDecimal actual = bigDecimal1.add(bigDecimal2);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoBigDecimals_whenMultiply_thenCorrect() {
|
||||
BigDecimal expected = new BigDecimal("123030014929277547.5030955772112635269");
|
||||
BigDecimal actual = bigDecimal1.multiply(bigDecimal2);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoBigDecimals_whenSubtract_thenCorrect() {
|
||||
BigDecimal expected = new BigDecimal("-863086431.0246913569");
|
||||
BigDecimal actual = bigDecimal1.subtract(bigDecimal2);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoBigDecimals_whenDivide_thenCorrect() {
|
||||
BigDecimal expected = new BigDecimal("0.13");
|
||||
BigDecimal actual = bigDecimal1.divide(bigDecimal2, 2, RoundingMode.HALF_UP);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.javadoublevsbigdecimal;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class JavaDoubleUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDoubleLiteral_whenAssigningToDoubleVariable_thenValueIsNotExactlyEqual() {
|
||||
double doubleValue = 0.1;
|
||||
double epsilon = 0.0000000000000001;
|
||||
assertEquals(0.1, doubleValue, epsilon);
|
||||
}
|
||||
}
|
||||
+4
-8
@@ -9,25 +9,21 @@ public class FloatDoubleConversionsTest {
|
||||
public void whenDoubleType_thenFloatTypeSuccess(){
|
||||
double interestRatesYearly = 13.333333333333334;
|
||||
float interest = (float) interestRatesYearly;
|
||||
System.out.println(interest); //13.333333
|
||||
Assert.assertTrue(Float.class.isInstance(interest));//true
|
||||
Assert.assertEquals(13.333333f, interest, 0.000004f);
|
||||
|
||||
Double monthlyRates = 2.111111111111112;
|
||||
float rates = monthlyRates.floatValue();
|
||||
System.out.println(rates); //2.1111112
|
||||
Assert.assertTrue(Float.class.isInstance(rates));//true
|
||||
Assert.assertEquals(2.1111112f, rates, 0.00000013);
|
||||
}
|
||||
@Test
|
||||
public void whenFloatType_thenDoubleTypeSuccess(){
|
||||
float gradeAverage =2.05f;
|
||||
double average = gradeAverage;
|
||||
System.out.println(average); //2.049999952316284
|
||||
Assert.assertTrue(Double.class.isInstance(average));//true
|
||||
Assert.assertEquals(2.05, average, 0.06);
|
||||
|
||||
Float monthlyRates = 2.1111112f;
|
||||
Double rates = monthlyRates.doubleValue();
|
||||
System.out.println(rates); //2.1111112
|
||||
Assert.assertTrue(Double.class.isInstance(rates));//true
|
||||
Assert.assertEquals(2.11111112, rates, 0.0000002);//true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.floattobigdecimal;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class FloatToBigDecimalUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenFloatComparedWithDifferentValues_thenCouldMatch() {
|
||||
assertNotEquals(1.1f, 1.09f);
|
||||
assertEquals(1.1f, 1.09999999f);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatedFromFloat_thenMatchesInternallyStoredValue() {
|
||||
float floatToConvert = 1.10000002384185791015625f;
|
||||
BigDecimal bdFromFloat = new BigDecimal(floatToConvert);
|
||||
assertEquals("1.10000002384185791015625", bdFromFloat.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatedFromString_thenPreservesTheOriginal() {
|
||||
BigDecimal bdFromString = new BigDecimal("1.1");
|
||||
assertEquals("1.1", bdFromString.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatedFromFloatConvertedToString_thenFloatInternalValueGetsTruncated() {
|
||||
String floatValue = Float.toString(1.10000002384185791015625f);
|
||||
BigDecimal bdFromString = new BigDecimal(floatValue);
|
||||
assertEquals("1.1", floatValue);
|
||||
assertEquals("1.1", bdFromString.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatedByValueOf_thenFloatValueGetsTruncated() {
|
||||
assertEquals("1.100000023841858", BigDecimal.valueOf(1.10000002384185791015625f).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDoubleConvertsFloatToString_thenFloatValueGetsTruncated() {
|
||||
assertEquals("1.100000023841858", Double.toString(1.10000002384185791015625f));
|
||||
}
|
||||
|
||||
}
|
||||
+7
-7
@@ -7,7 +7,7 @@ import javax.net.ssl.SSLSocket;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
|
||||
public class SecureConnection {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (args.length != 2) {
|
||||
System.out.println("Use: SecureConnection host port");
|
||||
@@ -20,20 +20,20 @@ public class SecureConnection {
|
||||
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket(host, port);
|
||||
InputStream in = sslsocket.getInputStream();
|
||||
OutputStream out = sslsocket.getOutputStream();
|
||||
|
||||
|
||||
out.write(1);
|
||||
|
||||
|
||||
while (in.available() > 0) {
|
||||
System.out.print(in.read());
|
||||
}
|
||||
|
||||
|
||||
System.out.println("Secured connection performed successfully");
|
||||
|
||||
|
||||
} catch (Exception exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the host from arguments
|
||||
* @param args the arguments
|
||||
@@ -42,7 +42,7 @@ public class SecureConnection {
|
||||
private static String getHost(String[] args) {
|
||||
return args[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the port from arguments
|
||||
* @param args the arguments
|
||||
|
||||
+3
-4
@@ -15,10 +15,8 @@ public class SimpleClient {
|
||||
SocketFactory factory = SSLSocketFactory.getDefault();
|
||||
|
||||
try (Socket connection = factory.createSocket(host, port)) {
|
||||
((SSLSocket) connection).setEnabledCipherSuites(
|
||||
new String[] { "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256"});
|
||||
((SSLSocket) connection).setEnabledProtocols(
|
||||
new String[] { "TLSv1.2"});
|
||||
((SSLSocket) connection).setEnabledCipherSuites(new String[] { "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256" });
|
||||
((SSLSocket) connection).setEnabledProtocols(new String[] { "TLSv1.2" });
|
||||
SSLParameters sslParams = new SSLParameters();
|
||||
sslParams.setEndpointIdentificationAlgorithm("HTTPS");
|
||||
((SSLSocket) connection).setSSLParameters(sslParams);
|
||||
@@ -28,6 +26,7 @@ public class SimpleClient {
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
System.setProperty("javax.net.debug", "ssl:handshake");
|
||||
System.out.println(startClient("localhost", 8443));
|
||||
}
|
||||
}
|
||||
|
||||
+4
-4
@@ -14,11 +14,10 @@ public class SimpleServer {
|
||||
ServerSocketFactory factory = SSLServerSocketFactory.getDefault();
|
||||
|
||||
try (ServerSocket listener = factory.createServerSocket(port)) {
|
||||
System.setProperty("javax.net.debug", "ssl:handshake");
|
||||
((SSLServerSocket) listener).setNeedClientAuth(true);
|
||||
((SSLServerSocket) listener).setEnabledCipherSuites(
|
||||
new String[] { "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256"});
|
||||
((SSLServerSocket) listener).setEnabledProtocols(
|
||||
new String[] { "TLSv1.2"});
|
||||
((SSLServerSocket) listener).setEnabledCipherSuites(new String[] { "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256" });
|
||||
((SSLServerSocket) listener).setEnabledProtocols(new String[] { "TLSv1.2" });
|
||||
while (true) {
|
||||
try (Socket socket = listener.accept()) {
|
||||
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
|
||||
@@ -29,6 +28,7 @@ public class SimpleServer {
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
System.setProperty("javax.net.debug", "ssl:handshake");
|
||||
startServer(8443);
|
||||
}
|
||||
}
|
||||
+41
-2
@@ -1,11 +1,11 @@
|
||||
package com.baeldung.charsequence;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CharSequenceVsStringUnitTest {
|
||||
|
||||
@Test
|
||||
@@ -44,4 +44,43 @@ public class CharSequenceVsStringUnitTest {
|
||||
|
||||
assertEquals(firstAddressOfTest, secondAddressOfTest);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCharSequenceAsString_whenConvertingUsingCasting_thenCorrect() {
|
||||
String expected = "baeldung";
|
||||
CharSequence charSequence = "baeldung";
|
||||
String explicitCastedString = (String) charSequence;
|
||||
|
||||
assertEquals(expected, charSequence);
|
||||
assertEquals(expected, explicitCastedString);
|
||||
}
|
||||
|
||||
@Test(expected = ClassCastException.class)
|
||||
public void givenCharSequenceAsStringBuiler_whenConvertingUsingCasting_thenThrowException() {
|
||||
CharSequence charSequence = new StringBuilder("baeldung");
|
||||
String castedString = (String) charSequence;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCharSequence_whenConvertingUsingToString_thenCorrect() {
|
||||
String expected = "baeldung";
|
||||
CharSequence charSequence1 = "baeldung";
|
||||
CharSequence charSequence2 = new StringBuilder("baeldung");
|
||||
|
||||
assertEquals(expected, charSequence1.toString());
|
||||
assertEquals(expected, charSequence2.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCharSequence_whenConvertingUsingValueOf_thenCorrect() {
|
||||
String expected = "baeldung";
|
||||
CharSequence charSequence1 = "baeldung";
|
||||
CharSequence charSequence2 = new StringBuilder("baeldung");
|
||||
CharSequence charSequence3 = null;
|
||||
|
||||
assertEquals(expected, String.valueOf(charSequence1));
|
||||
assertEquals(expected, String.valueOf(charSequence2));
|
||||
assertEquals("null", String.valueOf(charSequence3));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -83,6 +83,7 @@
|
||||
<module>core-java-collections-maps</module>
|
||||
<module>core-java-collections-maps-2</module>
|
||||
<module>core-java-collections-maps-3</module>
|
||||
<module>core-java-collections-maps-7</module>
|
||||
<module>core-java-compiler</module>
|
||||
<module>core-java-concurrency-2</module>
|
||||
<module>core-java-concurrency-advanced</module>
|
||||
|
||||
Reference in New Issue
Block a user