This commit is contained in:
Jonathan Cook
2020-04-09 10:09:45 +02:00
171 changed files with 2295 additions and 225 deletions
@@ -0,0 +1,56 @@
package com.baeldung.java14.helpfulnullpointerexceptions;
public class HelpfulNullPointerException {
public static void main(String[] args) {
Employee employee = null;
employee.getName();
}
public String getEmployeeEmailAddress(Employee employee) {
String emailAddress = employee.getPersonalDetails().getEmailAddress().toLowerCase();
return emailAddress;
}
static class Employee {
String name;
PersonalDetails personalDetails;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public PersonalDetails getPersonalDetails() {
return personalDetails;
}
public void setPersonalDetails(PersonalDetails personalDetails) {
this.personalDetails = personalDetails;
}
}
static class PersonalDetails {
String emailAddress;
String phone;
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
}
@@ -0,0 +1,37 @@
package com.baeldung.java14.helpfulnullpointerexceptions;
import org.junit.Test;
import static com.baeldung.java14.helpfulnullpointerexceptions.HelpfulNullPointerException.Employee;
import static com.baeldung.java14.helpfulnullpointerexceptions.HelpfulNullPointerException.PersonalDetails;
import static org.assertj.core.api.Assertions.assertThat;
public class HelpfulNullPointerExceptionUnitTest {
@Test (expected = NullPointerException.class)
public void givenAnEmptyPersonalDetails_whenEmailAddressIsAccessed_thenThrowNPE() {
var helpfulNPE = new HelpfulNullPointerException();
var employee = new Employee();
employee.setName("Eduard");
employee.setPersonalDetails(new PersonalDetails());
helpfulNPE.getEmployeeEmailAddress(employee);
}
@Test
public void givenCompletePersonalDetails_whenEmailAddressIsAccessed_thenSuccess() {
var helpfulNPE = new HelpfulNullPointerException();
var emailAddress = "[email protected]";
var employee = new Employee();
employee.setName("Eduard");
var personalDetails = new PersonalDetails();
personalDetails.setEmailAddress(emailAddress.toUpperCase());
personalDetails.setPhone("1234");
employee.setPersonalDetails(personalDetails);
assertThat(helpfulNPE.getEmployeeEmailAddress(employee)).isEqualTo(emailAddress);
}
}
@@ -0,0 +1,25 @@
package com.baeldung.concurrent.volatilekeyword;
public class TaskRunner {
private static int number;
private volatile static boolean ready;
private static class Reader extends Thread {
@Override
public void run() {
while (!ready) {
Thread.yield();
}
System.out.println(number);
}
}
public static void main(String[] args) {
new Reader().start();
number = 42;
ready = true;
}
}
@@ -1,18 +1,17 @@
package com.baeldung.java8;
import static org.hamcrest.Matchers.equalTo;
import com.baeldung.java8.entity.Human;
import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
import org.junit.Assert;
import org.junit.Test;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Assert;
import org.junit.Test;
import com.baeldung.java8.entity.Human;
import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
import static org.hamcrest.Matchers.equalTo;
public class Java8SortUnitTest {
@@ -113,11 +112,11 @@ public class Java8SortUnitTest {
humans.sort(Comparator.comparing(Human::getName));
Assert.assertThat(humans.get(0), equalTo(new Human("Jack", 12)));
}
@Test
public final void givenStreamNaturalOrdering_whenSortingEntitiesByName_thenCorrectlySorted() {
final List<String> letters = Lists.newArrayList("B", "A", "C");
final List<String> sortedLetters = letters.stream().sorted().collect(Collectors.toList());
Assert.assertThat(sortedLetters.get(0), equalTo("A"));
}
@@ -126,7 +125,7 @@ public class Java8SortUnitTest {
public final void givenStreamCustomOrdering_whenSortingEntitiesByName_thenCorrectlySorted() {
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 10), new Human("Jack", 12));
final Comparator<Human> nameComparator = (h1, h2) -> h1.getName().compareTo(h2.getName());
final List<Human> sortedHumans = humans.stream().sorted(nameComparator).collect(Collectors.toList());
Assert.assertThat(sortedHumans.get(0), equalTo(new Human("Jack", 12)));
}
@@ -164,4 +163,48 @@ public class Java8SortUnitTest {
Assert.assertThat(reverseSortedHumans.get(0), equalTo(new Human("Sarah", 10)));
}
@Test(expected = NullPointerException.class)
public final void givenANullElement_whenSortingEntitiesByName_thenThrowsNPE() {
final List<Human> humans = Lists.newArrayList(null, new Human("Jack", 12));
humans.sort((h1, h2) -> h1.getName().compareTo(h2.getName()));
}
@Test
public final void givenANullElement_whenSortingEntitiesByNameManually_thenMovesTheNullToLast() {
final List<Human> humans = Lists.newArrayList(null, new Human("Jack", 12), null);
humans.sort((h1, h2) -> {
if (h1 == null) return h2 == null ? 0 : 1;
else if (h2 == null) return -1;
return h1.getName().compareTo(h2.getName());
});
Assert.assertNotNull(humans.get(0));
Assert.assertNull(humans.get(1));
Assert.assertNull(humans.get(2));
}
@Test
public final void givenANullElement_whenSortingEntitiesByName_thenMovesTheNullToLast() {
final List<Human> humans = Lists.newArrayList(null, new Human("Jack", 12), null);
humans.sort(Comparator.nullsLast(Comparator.comparing(Human::getName)));
Assert.assertNotNull(humans.get(0));
Assert.assertNull(humans.get(1));
Assert.assertNull(humans.get(2));
}
@Test
public final void givenANullElement_whenSortingEntitiesByName_thenMovesTheNullToStart() {
final List<Human> humans = Lists.newArrayList(null, new Human("Jack", 12), null);
humans.sort(Comparator.nullsFirst(Comparator.comparing(Human::getName)));
Assert.assertNull(humans.get(0));
Assert.assertNull(humans.get(1));
Assert.assertNotNull(humans.get(2));
}
}
@@ -1,8 +1,12 @@
package com.baeldung.comparelong;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import org.junit.Test;
import java.util.Objects;
public class CompareLongUnitTest {
@Test
@@ -32,6 +36,33 @@ public class CompareLongUnitTest {
assertThat(l1.equals(l2)).isTrue();
}
@Test
public void givenLongValuesLessThan128_whenUsingObjectsEquals_thenSuccess() {
Long l1 = 127L;
Long l2 = 127L;
assertThat(Objects.equals(l1, l2)).isTrue();
}
@Test
public void givenLongValuesGreaterOrEqualsThan128_whenUsingObjectsEquals_thenSuccess() {
Long l1 = 128L;
Long l2 = 128L;
assertThat(Objects.equals(l1, l2)).isTrue();
}
@Test
public void givenNullReference_whenUsingObjectsEquals_thenNoException() {
Long l1 = null;
Long l2 = 128L;
assertThatCode(() -> Objects.equals(l1, l2)).doesNotThrowAnyException();
}
@Test
public void givenLongValuesGreaterOrEqualsThan128_whenUsingComparisonOperator_andLongValue_thenSuccess() {
@@ -1,3 +1,11 @@
## Core Java Security
This module contains articles about core Java Security
### Relevant Articles:
- [Guide To The Java Authentication And Authorization Service (JAAS)](https://www.baeldung.com/java-authentication-authorization-service)
- [MD5 Hashing in Java](http://www.baeldung.com/java-md5)
- [Hashing a Password in Java](https://www.baeldung.com/java-password-hashing)
- [SHA-256 and SHA3-256 Hashing in Java](https://www.baeldung.com/sha-256-hashing-java)
- More articles: [[<-- prev]](/core-java-modules/core-java-security)
@@ -16,4 +16,45 @@
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>${bouncycastle.version}</version>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
<properties>
<!-- util -->
<bouncycastle.version>1.60</bouncycastle.version>
<commons-codec.version>1.11</commons-codec.version>
<!-- testing -->
<assertj-core.version>3.10.0</assertj-core.version>
</properties>
</project>
@@ -0,0 +1,23 @@
package com.baeldung.checksums;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.Checksum;
public class ChecksumUtils {
public static long getChecksumCRC32(byte[] bytes) {
Checksum crc32 = new CRC32();
crc32.update(bytes, 0, bytes.length);
return crc32.getValue();
}
public static long getChecksumCRC32(InputStream stream, int bufferSize) throws IOException {
CheckedInputStream checkedInputStream = new CheckedInputStream(stream, new CRC32());
byte[] buffer = new byte[bufferSize];
while (checkedInputStream.read(buffer, 0, buffer.length) >= 0) {}
return checkedInputStream.getChecksum().getValue();
}
}
@@ -0,0 +1,51 @@
package com.baeldung.checksums;
import org.junit.Before;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import static org.junit.jupiter.api.Assertions.*;
class ChecksumUtilsUnitTest {
byte[] arr;
@Before
void setUp() {
arr = new byte[]{0,10,21,20,35,40,120,56,72,22};
}
@Test
void givenByteArray_whenChecksumCreated_checkCorrect() {
long checksum = ChecksumUtils.getChecksumCRC32(arr);
assertEquals(3915397664L, checksum);
}
@Test
void givenTwoDifferentStrings_whenChecksumCreated_checkCollision() {
String plumless = "plumless";
String buckeroo = "buckeroo";
long plumlessChecksum = ChecksumUtils.getChecksumCRC32(plumless.getBytes());
long buckerooChecksum = ChecksumUtils.getChecksumCRC32(buckeroo.getBytes());
assertEquals(plumlessChecksum, buckerooChecksum);
}
@Test
void givenInputString_whenChecksumCreated_checkCorrect() throws IOException {
InputStream inputStream = new ByteArrayInputStream(arr);
long checksum = ChecksumUtils.getChecksumCRC32(inputStream, 10);
assertEquals(3915397664L, checksum);
}
}
@@ -3,17 +3,16 @@
This module contains articles about core Java Security
### Relevant Articles:
- [MD5 Hashing in Java](http://www.baeldung.com/java-md5)
- [Guide to the Cipher Class](http://www.baeldung.com/java-cipher-class)
- [Introduction to SSL in Java](http://www.baeldung.com/java-ssl)
- [Java KeyStore API](http://www.baeldung.com/java-keystore)
- [Encrypting and Decrypting Files in Java](http://www.baeldung.com/java-cipher-input-output-stream)
- [Hashing a Password in Java](https://www.baeldung.com/java-password-hashing)
- [SSL Handshake Failures](https://www.baeldung.com/java-ssl-handshake-failures)
- [SHA-256 and SHA3-256 Hashing in Java](https://www.baeldung.com/sha-256-hashing-java)
- [Enabling TLS v1.2 in Java 7](https://www.baeldung.com/java-7-tls-v12)
- [The Java SecureRandom Class](https://www.baeldung.com/java-secure-random)
- [An Introduction to Java SASL](https://www.baeldung.com/java-sasl)
- [A Guide to Java GSS API](https://www.baeldung.com/java-gss)
- [Intro to the Java SecurityManager](https://www.baeldung.com/java-security-manager)
- More articles: [[next -->]](/core-java-modules/core-java-security-2)
@@ -24,24 +24,9 @@
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>${bouncycastle.version}</version>
</dependency>
</dependencies>
<properties>
<!-- util -->
<bouncycastle.version>1.60</bouncycastle.version>
<commons-codec.version>1.11</commons-codec.version>
<!-- testing -->
<assertj-core.version>3.10.0</assertj-core.version>
</properties>
@@ -0,0 +1,42 @@
package com.baeldung.streams.bigdecimals;
import static org.junit.Assert.assertEquals;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.junit.Test;
public class AddNumbersUnitTest {
@Test
public void givenIntStream_whenSum_thenResultIsCorrect() {
IntStream intNumbers = IntStream.range(0, 3);
assertEquals(3, intNumbers.sum());
}
@Test
public void givenCollectionOfDouble_whenUsingMapToDoubleToSum_thenResultIsCorrect() {
List<Double> doubleNumbers = Arrays.asList(23.48, 52.26, 13.5);
double result = doubleNumbers.stream()
.mapToDouble(Double::doubleValue)
.sum();
assertEquals(89.24, result, .1);
}
public void givenStreamOfIntegers_whenUsingReduceToSum_thenResultIsCorrect() {
Stream<Integer> intNumbers = Stream.of(0, 1, 2);
int result = intNumbers.reduce(0, Integer::sum);
assertEquals(106, result);
}
public void givenStreamOfBigDecimals_whenUsingReduceToSum_thenResultIsCorrect() {
Stream<BigDecimal> bigDecimalNumber = Stream.of(BigDecimal.ZERO, BigDecimal.ONE, BigDecimal.TEN);
BigDecimal result = bigDecimalNumber.reduce(BigDecimal.ZERO, BigDecimal::add);
assertEquals(11, result);
}
}
@@ -0,0 +1,20 @@
package com.baeldung.guava.mapmaker;
public class Profile {
private long id;
private String type;
public Profile(long id, String type) {
this.id = id;
this.type = type;
}
public long getId() {
return id;
}
public String getName() {
return type;
}
}
@@ -0,0 +1,13 @@
package com.baeldung.guava.mapmaker;
public class Session {
private long id;
public Session(long id) {
this.id = id;
}
public long getId() {
return id;
}
}
@@ -0,0 +1,20 @@
package com.baeldung.guava.mapmaker;
public class User {
private long id;
private String name;
public User(long id, String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
}

Some files were not shown because too many files have changed in this diff Show More