Merge remote-tracking branch 'origin/main' into ethereum
# Conflicts: # .idea/jarRepositories.xml
This commit is contained in:
Generated
+4
-3
@@ -7,7 +7,6 @@
|
||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
<module name="jackson-annotations" />
|
||||
<module name="assertion-libraries" />
|
||||
<module name="core-java-serialization" />
|
||||
<module name="core-java-annotations" />
|
||||
@@ -23,14 +22,14 @@
|
||||
<module name="core-java-datetime-conversion" />
|
||||
<module name="core-java-lambdas" />
|
||||
<module name="junit-5-basics" />
|
||||
<module name="activejdbc" />
|
||||
<module name="spring-data-jpa-repo" />
|
||||
<module name="apache-velocity" />
|
||||
<module name="jackson-conversions-2" />
|
||||
<module name="activejdbc" />
|
||||
<module name="codebank" />
|
||||
<module name="core-java-collections-list" />
|
||||
<module name="image-compressing" />
|
||||
<module name="spring-data-jpa-repo-2" />
|
||||
<module name="image-compressing" />
|
||||
<module name="core-java-streams" />
|
||||
<module name="core-java-io-2" />
|
||||
<module name="core-java-io" />
|
||||
@@ -57,7 +56,9 @@
|
||||
<module name="core-java-11-2" target="11" />
|
||||
<module name="core-java-8-2" target="11" />
|
||||
<module name="core-java-collections-list-3" target="11" />
|
||||
<module name="jackson-annotations" target="17" />
|
||||
<module name="spring-data-jpa-repo-2" target="17" />
|
||||
<module name="xml" target="17" />
|
||||
</bytecodeTargetLevel>
|
||||
</component>
|
||||
<component name="JavacSettings">
|
||||
|
||||
Generated
+17
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||
<data-source source="LOCAL" name="postgres@nas1120" uuid="33f98331-7e30-4939-9d10-a9d8ae4f65f2">
|
||||
<driver-ref>postgresql</driver-ref>
|
||||
<synchronize>true</synchronize>
|
||||
<jdbc-driver>org.postgresql.Driver</jdbc-driver>
|
||||
<jdbc-url>jdbc:postgresql://nas1120:5433/postgres</jdbc-url>
|
||||
<jdbc-additional-properties>
|
||||
<property name="com.intellij.clouds.kubernetes.db.host.port" />
|
||||
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
|
||||
<property name="com.intellij.clouds.kubernetes.db.container.port" />
|
||||
</jdbc-additional-properties>
|
||||
<working-dir>$ProjectFileDir$</working-dir>
|
||||
</data-source>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+2
@@ -112,5 +112,7 @@
|
||||
<file url="file://$PROJECT_DIR$/toolkits/codebank/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/toolkits/discourse/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/toolkits/discourse/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/xml/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/xml/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 iSharkFly
|
||||
Copyright (c) 2024 iSharkFly
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
@@ -15,12 +15,12 @@ Java 代码使用的是 JDK 17。
|
||||
|
||||
请使用下面的联系方式和我们联系:
|
||||
|
||||
| 联系方式名称 | 联系方式 |
|
||||
|--------|------------------------------------------------------|
|
||||
| 电子邮件 | [[email protected]](mailto:[email protected]) |
|
||||
| QQ 或微信 | 103899765 |
|
||||
| QQ 交流群 | 15186112 |
|
||||
| 社区论坛 | https://www.isharkfly.com/c/computer-program/java/15 |
|
||||
| 联系方式名称 | 联系方式 |
|
||||
|--------|-----------------------------------------------|
|
||||
| 电子邮件 | [[email protected]](mailto:[email protected]) |
|
||||
| QQ 或微信 | 103899765 |
|
||||
| QQ 交流群 | 15186112 |
|
||||
| 社区论坛 | https://www.ossez.com/c/open-source/java/15 |
|
||||
|
||||
# 公众平台
|
||||
|
||||
|
||||
+1
-1
@@ -46,7 +46,7 @@ public class JavaPeriodUnitTest {
|
||||
|
||||
Period period = Period.between(startDate, endDate);
|
||||
|
||||
logger.info(String.format("Years:%d months:%d days:%d", period.getYears(), period.getMonths(), period.getDays()));
|
||||
logger.info("Years:{} months:{} days:{}", period.getYears(), period.getMonths(), period.getDays());
|
||||
|
||||
assertFalse(period.isNegative());
|
||||
assertEquals(56, period.plusDays(50)
|
||||
|
||||
+1
-11
@@ -1,23 +1,19 @@
|
||||
package com.ossez.jackson.optionalwithjackson;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
|
||||
|
||||
import static io.restassured.path.json.JsonPath.from;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class OptionalTypeUnitTest {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper().registerModule(new Jdk8Module());
|
||||
|
||||
@Test
|
||||
public void givenPresentOptional_whenSerializing_thenValueInJson() throws JsonProcessingException {
|
||||
|
||||
String subTitle = "The Parish Boy's Progress";
|
||||
@@ -30,12 +26,6 @@ public class OptionalTypeUnitTest {
|
||||
assertThat(from(result).getString("subTitle")).isEqualTo(subTitle);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFieldWithValue_whenDeserializing_thenThrowExceptio() throws JsonProcessingException {
|
||||
String bookJson = "{ \"title\": \"Oliver Twist\", \"subTitle\": \"foo\" }";
|
||||
Book result = mapper.readValue(bookJson, Book.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyOptional_whenSerializing_thenNullValue() throws JsonProcessingException {
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
<modules>
|
||||
<module>jackson</module>
|
||||
<module>jackson-annotations</module>
|
||||
<!-- <module>jackson-annotations</module>-->
|
||||
<module>jackson-conversions</module>
|
||||
<module>jackson-conversions-2</module>
|
||||
<module>jackson-custom-conversions</module>
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.assertj;
|
||||
|
||||
public class Dog {
|
||||
private String name;
|
||||
private Float weight;
|
||||
|
||||
public Dog(String name, Float weight) {
|
||||
this.name = name;
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Float getWeight() {
|
||||
return weight;
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.assertj;
|
||||
|
||||
public class Member {
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public Member(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.assertj;
|
||||
|
||||
public class Person {
|
||||
private String name;
|
||||
private Integer age;
|
||||
|
||||
public Person(String name, Integer age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
}
|
||||
-32
@@ -1,32 +0,0 @@
|
||||
package com.baeldung.assertj.custom;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Person {
|
||||
private String fullName;
|
||||
private int age;
|
||||
private List<String> nicknames;
|
||||
|
||||
public Person(String fullName, int age) {
|
||||
this.fullName = fullName;
|
||||
this.age = age;
|
||||
this.nicknames = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addNickname(String nickname) {
|
||||
nicknames.add(nickname);
|
||||
}
|
||||
|
||||
public String getFullName() {
|
||||
return fullName;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public List<String> getNicknames() {
|
||||
return nicknames;
|
||||
}
|
||||
}
|
||||
-22
@@ -1,22 +0,0 @@
|
||||
package com.baeldung.assertj.exceptions;
|
||||
|
||||
public class CityNotFoundException extends RuntimeException {
|
||||
|
||||
private String city;
|
||||
private String message;
|
||||
|
||||
CityNotFoundException(String city, String message) {
|
||||
this.city = city;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.assertj.exceptions;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public final class CityUtils {
|
||||
|
||||
private static final List<String> CITIES = Arrays.asList("Tamassint", "London", "Madrid", "New york");
|
||||
|
||||
public static String search(String searchedCity) {
|
||||
return CITIES.stream()
|
||||
.filter(searchedCity::equals)
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new CityNotFoundException(searchedCity, "The specified city is not found"));
|
||||
}
|
||||
|
||||
}
|
||||
-25
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.assertj.extracting;
|
||||
|
||||
class Address {
|
||||
private String street;
|
||||
private String city;
|
||||
private ZipCode zipCode;
|
||||
|
||||
Address(String street, String city, ZipCode zipCode) {
|
||||
this.street = street;
|
||||
this.city = city;
|
||||
this.zipCode = zipCode;
|
||||
}
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public ZipCode getZipCode() {
|
||||
return zipCode;
|
||||
}
|
||||
}
|
||||
-25
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.assertj.extracting;
|
||||
|
||||
class Person {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private Address address;
|
||||
|
||||
Person(String firstName, String lastName, Address address) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
}
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.assertj.extracting;
|
||||
|
||||
class ZipCode {
|
||||
private long zipcode;
|
||||
|
||||
ZipCode(long zipcode) {
|
||||
this.zipcode = zipcode;
|
||||
}
|
||||
|
||||
public long getZipcode() {
|
||||
return zipcode;
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package com.baeldung.jspec;
|
||||
|
||||
public abstract class Animal {
|
||||
|
||||
protected String name;
|
||||
|
||||
public Animal(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Animal other = (Animal) obj;
|
||||
if (name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
} else if (!name.equals(other.name))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package com.baeldung.jspec;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class Cage {
|
||||
|
||||
private Set<Animal> animals = new HashSet<>();
|
||||
|
||||
public void put(Animal animal) {
|
||||
animals.add(animal);
|
||||
}
|
||||
|
||||
public void put(Animal... animals) {
|
||||
this.animals.addAll(Arrays.asList(animals));
|
||||
}
|
||||
|
||||
public Animal release(Animal animal) {
|
||||
return animals.remove(animal) ? animal : null;
|
||||
}
|
||||
|
||||
public void open() {
|
||||
animals.clear();
|
||||
}
|
||||
|
||||
public boolean hasAnimals() {
|
||||
return animals.size() > 0;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return animals.isEmpty();
|
||||
}
|
||||
|
||||
public Set<Animal> getAnimals() {
|
||||
return this.animals;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return animals.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Cage [animals=" + animals + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.baeldung.jspec;
|
||||
|
||||
public class Cat extends Animal {
|
||||
|
||||
public Cat(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Cat [name=" + name + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.baeldung.jspec;
|
||||
|
||||
public class Dog extends Animal {
|
||||
|
||||
public Dog(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Dog [name=" + name + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package com.baeldung.truth;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class User implements Comparable<User> {
|
||||
private String name = "John Doe";
|
||||
private List<String> emails = Arrays.asList("[email protected]", "[email protected]");
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<String> getEmails() {
|
||||
return emails;
|
||||
}
|
||||
|
||||
public void setEmails(List<String> emails) {
|
||||
this.emails = emails;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 5;
|
||||
hash = 37 * hash + Objects.hashCode(this.name);
|
||||
return hash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null || getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final User other = (User) obj;
|
||||
return Objects.equals(this.name, other.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(User o) {
|
||||
return this.getName()
|
||||
.compareToIgnoreCase(o.getName());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package com.baeldung.truth;
|
||||
|
||||
import com.google.common.truth.ComparableSubject;
|
||||
import com.google.common.truth.FailureStrategy;
|
||||
import com.google.common.truth.IterableSubject;
|
||||
import com.google.common.truth.SubjectFactory;
|
||||
import com.google.common.truth.Truth;
|
||||
|
||||
public class UserSubject extends ComparableSubject<UserSubject, User> {
|
||||
|
||||
private UserSubject(FailureStrategy failureStrategy, User target) {
|
||||
super(failureStrategy, target);
|
||||
}
|
||||
|
||||
private static final SubjectFactory<UserSubject, User> USER_SUBJECT_FACTORY = new SubjectFactory<UserSubject, User>() {
|
||||
@Override
|
||||
public UserSubject getSubject(FailureStrategy failureStrategy, User target) {
|
||||
return new UserSubject(failureStrategy, target);
|
||||
}
|
||||
};
|
||||
|
||||
public static UserSubject assertThat(User user) {
|
||||
return Truth.assertAbout(USER_SUBJECT_FACTORY)
|
||||
.that(user);
|
||||
}
|
||||
|
||||
// Our API begins here
|
||||
public void hasName(String name) {
|
||||
if (!actual().getName()
|
||||
.equals(name)) {
|
||||
fail("has name", name);
|
||||
}
|
||||
}
|
||||
|
||||
public void hasNameIgnoringCase(String name) {
|
||||
if (!actual().getName()
|
||||
.equalsIgnoreCase(name)) {
|
||||
fail("has name ignoring case", name);
|
||||
}
|
||||
}
|
||||
|
||||
public IterableSubject emails() {
|
||||
return Truth.assertThat(actual().getEmails());
|
||||
}
|
||||
|
||||
}
|
||||
-72
@@ -1,72 +0,0 @@
|
||||
package com.baeldung.assertj;
|
||||
|
||||
import static org.assertj.core.api.Assertions.allOf;
|
||||
import static org.assertj.core.api.Assertions.anyOf;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.not;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.assertj.core.api.Condition;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AssertJConditionUnitTest {
|
||||
private Condition<Member> senior = new Condition<>(m -> m.getAge() >= 60, "senior");
|
||||
private Condition<Member> nameJohn = new Condition<>(m -> m.getName().equalsIgnoreCase("John"), "name John");
|
||||
|
||||
@Test
|
||||
public void whenUsingMemberAgeCondition_thenCorrect() {
|
||||
Member member = new Member("John", 65);
|
||||
assertThat(member).is(senior);
|
||||
|
||||
try {
|
||||
assertThat(member).isNot(senior);
|
||||
fail();
|
||||
} catch (AssertionError e) {
|
||||
assertThat(e).hasMessageContaining("not to be senior");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingMemberNameCondition_thenCorrect() {
|
||||
Member member = new Member("Jane", 60);
|
||||
assertThat(member).doesNotHave(nameJohn);
|
||||
|
||||
try {
|
||||
assertThat(member).has(nameJohn);
|
||||
fail();
|
||||
} catch (AssertionError e) {
|
||||
assertThat(e).hasMessageContaining("name John");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCollectionConditionsAreSatisfied_thenCorrect() {
|
||||
List<Member> members = new ArrayList<>();
|
||||
members.add(new Member("Alice", 50));
|
||||
members.add(new Member("Bob", 60));
|
||||
|
||||
assertThat(members).haveExactly(1, senior);
|
||||
assertThat(members).doNotHave(nameJohn);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCombiningAllOfConditions_thenCorrect() {
|
||||
Member john = new Member("John", 60);
|
||||
Member jane = new Member("Jane", 50);
|
||||
|
||||
assertThat(john).is(allOf(senior, nameJohn));
|
||||
assertThat(jane).is(allOf(not(nameJohn), not(senior)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCombiningAnyOfConditions_thenCorrect() {
|
||||
Member john = new Member("John", 50);
|
||||
Member jane = new Member("Jane", 60);
|
||||
|
||||
assertThat(john).is(anyOf(senior, nameJohn));
|
||||
assertThat(jane).is(anyOf(nameJohn, senior));
|
||||
}
|
||||
}
|
||||
-117
@@ -1,117 +0,0 @@
|
||||
package com.baeldung.assertj;
|
||||
|
||||
import org.assertj.core.util.Maps;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class AssertJCoreUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenComparingReferences_thenNotEqual() throws Exception {
|
||||
Dog fido = new Dog("Fido", 5.15f);
|
||||
Dog fidosClone = new Dog("Fido", 5.15f);
|
||||
|
||||
assertThat(fido).isNotEqualTo(fidosClone);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingFields_thenEqual() throws Exception {
|
||||
Dog fido = new Dog("Fido", 5.15f);
|
||||
Dog fidosClone = new Dog("Fido", 5.15f);
|
||||
|
||||
assertThat(fido).isEqualToComparingFieldByFieldRecursively(fidosClone);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingForElement_thenContains() throws Exception {
|
||||
List<String> list = Arrays.asList("1", "2", "3");
|
||||
|
||||
assertThat(list).contains("1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingForElement_thenMultipleAssertions() throws Exception {
|
||||
List<String> list = Arrays.asList("1", "2", "3");
|
||||
|
||||
assertThat(list).isNotEmpty();
|
||||
assertThat(list).startsWith("1");
|
||||
assertThat(list).doesNotContainNull();
|
||||
|
||||
assertThat(list).isNotEmpty().contains("1").startsWith("1").doesNotContainNull().containsSequence("2", "3");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingRunnable_thenIsInterface() throws Exception {
|
||||
assertThat(Runnable.class).isInterface();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingCharacter_thenIsUnicode() throws Exception {
|
||||
char someCharacter = 'c';
|
||||
|
||||
assertThat(someCharacter).isNotEqualTo('a').inUnicode().isGreaterThanOrEqualTo('b').isLowerCase();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAssigningNSEExToException_thenIsAssignable() throws Exception {
|
||||
assertThat(Exception.class).isAssignableFrom(NoSuchElementException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingWithOffset_thenEquals() throws Exception {
|
||||
assertThat(5.1).isEqualTo(5, withPrecision(1d));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingString_then() throws Exception {
|
||||
assertThat("".isEmpty()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingFile_then() throws Exception {
|
||||
final File someFile = File.createTempFile("aaa", "bbb");
|
||||
someFile.deleteOnExit();
|
||||
|
||||
assertThat(someFile).exists().isFile().canRead().canWrite();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckingIS_then() throws Exception {
|
||||
InputStream given = new ByteArrayInputStream("foo".getBytes());
|
||||
InputStream expected = new ByteArrayInputStream("foo".getBytes());
|
||||
|
||||
assertThat(given).hasSameContentAs(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGivenMap_then() throws Exception {
|
||||
Map<Integer, String> map = Maps.newHashMap(2, "a");
|
||||
|
||||
assertThat(map).isNotEmpty().containsKey(2).doesNotContainKeys(10).contains(entry(2, "a"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGivenException_then() throws Exception {
|
||||
Exception ex = new Exception("abc");
|
||||
|
||||
assertThat(ex).hasNoCause().hasMessageEndingWith("c");
|
||||
}
|
||||
|
||||
@Ignore // IN ORDER TO TEST, REMOVE THIS LINE
|
||||
@Test
|
||||
public void whenRunningAssertion_thenDescribed() throws Exception {
|
||||
Person person = new Person("Alex", 34);
|
||||
|
||||
assertThat(person.getAge()).as("%s's age should be equal to 100").isEqualTo(100);
|
||||
}
|
||||
}
|
||||
-96
@@ -1,96 +0,0 @@
|
||||
package com.baeldung.assertj;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.ArrayListMultimap;
|
||||
import com.google.common.collect.HashBasedTable;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.common.collect.Multimaps;
|
||||
import com.google.common.collect.Range;
|
||||
import com.google.common.collect.Table;
|
||||
import com.google.common.collect.TreeRangeMap;
|
||||
import com.google.common.io.Files;
|
||||
import org.assertj.guava.data.MapEntry;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
|
||||
import static org.assertj.guava.api.Assertions.assertThat;
|
||||
import static org.assertj.guava.api.Assertions.entry;
|
||||
|
||||
public class AssertJGuavaUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTwoEmptyFiles_whenComparingContent_thenEqual() throws Exception {
|
||||
final File temp1 = File.createTempFile("bael", "dung1");
|
||||
final File temp2 = File.createTempFile("bael", "dung2");
|
||||
|
||||
assertThat(Files.asByteSource(temp1)).hasSize(0).hasSameContentAs(Files.asByteSource(temp2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultimap_whenVerifying_thenCorrect() throws Exception {
|
||||
final Multimap<Integer, String> mmap = ArrayListMultimap.create();
|
||||
mmap.put(1, "one");
|
||||
mmap.put(1, "1");
|
||||
|
||||
assertThat(mmap).hasSize(2).containsKeys(1).contains(entry(1, "one")).contains(entry(1, "1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultimaps_whenVerifyingContent_thenCorrect() throws Exception {
|
||||
final Multimap<Integer, String> mmap1 = ArrayListMultimap.create();
|
||||
mmap1.put(1, "one");
|
||||
mmap1.put(1, "1");
|
||||
mmap1.put(2, "two");
|
||||
mmap1.put(2, "2");
|
||||
|
||||
final Multimap<Integer, String> mmap1_clone = Multimaps.newSetMultimap(new HashMap<>(), HashSet::new);
|
||||
mmap1_clone.put(1, "one");
|
||||
mmap1_clone.put(1, "1");
|
||||
mmap1_clone.put(2, "two");
|
||||
mmap1_clone.put(2, "2");
|
||||
|
||||
final Multimap<Integer, String> mmap2 = Multimaps.newSetMultimap(new HashMap<>(), HashSet::new);
|
||||
mmap2.put(1, "one");
|
||||
mmap2.put(1, "1");
|
||||
|
||||
assertThat(mmap1).containsAllEntriesOf(mmap2).containsAllEntriesOf(mmap1_clone).hasSameEntriesAs(mmap1_clone);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOptional_whenVerifyingContent_thenShouldBeEqual() throws Exception {
|
||||
final Optional<String> something = Optional.of("something");
|
||||
|
||||
assertThat(something).isPresent().extractingValue().isEqualTo("something");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRange_whenVerifying_thenShouldBeCorrect() throws Exception {
|
||||
final Range<String> range = Range.openClosed("a", "g");
|
||||
|
||||
assertThat(range).hasOpenedLowerBound().isNotEmpty().hasClosedUpperBound().contains("b");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRangeMap_whenVerifying_thenShouldBeCorrect() throws Exception {
|
||||
final TreeRangeMap<Integer, String> map = TreeRangeMap.create();
|
||||
|
||||
map.put(Range.closed(0, 60), "F");
|
||||
map.put(Range.closed(61, 70), "D");
|
||||
|
||||
assertThat(map).isNotEmpty().containsKeys(0).contains(MapEntry.entry(34, "F"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTable_whenVerifying_thenShouldBeCorrect() throws Exception {
|
||||
final Table<Integer, String, String> table = HashBasedTable.create(2, 2);
|
||||
|
||||
table.put(1, "A", "PRESENT");
|
||||
table.put(1, "B", "ABSENT");
|
||||
|
||||
assertThat(table).hasRowCount(1).containsValues("ABSENT").containsCell(1, "B", "ABSENT");
|
||||
}
|
||||
|
||||
}
|
||||
-108
@@ -1,108 +0,0 @@
|
||||
package com.baeldung.assertj;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static java.time.LocalDate.ofYearDay;
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class AssertJJava8UnitTest {
|
||||
|
||||
@Test
|
||||
public void givenOptional_shouldAssert() throws Exception {
|
||||
final Optional<String> givenOptional = Optional.of("something");
|
||||
|
||||
assertThat(givenOptional).isPresent().hasValue("something");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPredicate_shouldAssert() throws Exception {
|
||||
final Predicate<String> predicate = s -> s.length() > 4;
|
||||
|
||||
assertThat(predicate).accepts("aaaaa", "bbbbb").rejects("a", "b").acceptsAll(asList("aaaaa", "bbbbb")).rejectsAll(asList("a", "b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalDate_shouldAssert() throws Exception {
|
||||
final LocalDate givenLocalDate = LocalDate.of(2016, 7, 8);
|
||||
final LocalDate todayDate = LocalDate.now();
|
||||
|
||||
assertThat(givenLocalDate).isBefore(LocalDate.of(2020, 7, 8)).isAfterOrEqualTo(LocalDate.of(1989, 7, 8));
|
||||
|
||||
assertThat(todayDate).isAfter(LocalDate.of(1989, 7, 8)).isToday();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalDateTime_shouldAssert() throws Exception {
|
||||
final LocalDateTime givenLocalDate = LocalDateTime.of(2016, 7, 8, 12, 0);
|
||||
|
||||
assertThat(givenLocalDate).isBefore(LocalDateTime.of(2020, 7, 8, 11, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalTime_shouldAssert() throws Exception {
|
||||
final LocalTime givenLocalTime = LocalTime.of(12, 15);
|
||||
|
||||
assertThat(givenLocalTime).isAfter(LocalTime.of(1, 0)).hasSameHourAs(LocalTime.of(12, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_shouldAssertFlatExtracting() throws Exception {
|
||||
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
|
||||
|
||||
assertThat(givenList).flatExtracting(LocalDate::getYear).contains(2015);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_shouldAssertFlatExtractingLeapYear() throws Exception {
|
||||
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
|
||||
|
||||
assertThat(givenList).flatExtracting(LocalDate::isLeapYear).contains(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_shouldAssertFlatExtractingClass() throws Exception {
|
||||
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
|
||||
|
||||
assertThat(givenList).flatExtracting(Object::getClass).contains(LocalDate.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_shouldAssertMultipleFlatExtracting() throws Exception {
|
||||
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
|
||||
|
||||
assertThat(givenList).flatExtracting(LocalDate::getYear, LocalDate::getDayOfMonth).contains(2015, 6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_shouldSatisfy() throws Exception {
|
||||
final String givenString = "someString";
|
||||
|
||||
assertThat(givenString).satisfies(s -> {
|
||||
assertThat(s).isNotEmpty();
|
||||
assertThat(s).hasSize(10);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_shouldMatch() throws Exception {
|
||||
final String emptyString = "";
|
||||
|
||||
assertThat(emptyString).matches(String::isEmpty);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_shouldHasOnlyOneElementSatisfying() throws Exception {
|
||||
final List<String> givenList = Arrays.asList("");
|
||||
|
||||
assertThat(givenList).hasOnlyOneElementSatisfying(s -> assertThat(s).isEmpty());
|
||||
}
|
||||
}
|
||||
-44
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.assertj.custom;
|
||||
|
||||
import static com.baeldung.assertj.custom.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
public class AssertJCustomAssertionsUnitTest {
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void whenPersonNameMatches_thenCorrect() {
|
||||
Person person = new Person("John Doe", 20);
|
||||
assertThat(person).hasFullName("John Doe");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPersonAgeLessThanEighteen_thenNotAdult() {
|
||||
Person person = new Person("Jane Roe", 16);
|
||||
|
||||
try {
|
||||
assertThat(person).isAdult();
|
||||
fail();
|
||||
} catch (AssertionError e) {
|
||||
org.assertj.core.api.Assertions.assertThat(e).hasMessage("Expected person to be adult");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPersonDoesNotHaveAMatchingNickname_thenIncorrect() {
|
||||
Person person = new Person("John Doe", 20);
|
||||
person.addNickname("Nick");
|
||||
|
||||
try {
|
||||
assertThat(person).hasNickname("John");
|
||||
fail();
|
||||
} catch (AssertionError e) {
|
||||
org.assertj.core.api.Assertions.assertThat(e).hasMessage("Expected person to have nickname John");
|
||||
}
|
||||
}
|
||||
}
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.assertj.custom;
|
||||
|
||||
public class Assertions {
|
||||
public static PersonAssert assertThat(Person actual) {
|
||||
return new PersonAssert(actual);
|
||||
}
|
||||
|
||||
// static factory methods of other assertion classes
|
||||
}
|
||||
-38
@@ -1,38 +0,0 @@
|
||||
package com.baeldung.assertj.custom;
|
||||
|
||||
import org.assertj.core.api.AbstractAssert;
|
||||
|
||||
public class PersonAssert extends AbstractAssert<PersonAssert, Person> {
|
||||
|
||||
public PersonAssert(Person actual) {
|
||||
super(actual, PersonAssert.class);
|
||||
}
|
||||
|
||||
public static PersonAssert assertThat(Person actual) {
|
||||
return new PersonAssert(actual);
|
||||
}
|
||||
|
||||
public PersonAssert hasFullName(String fullName) {
|
||||
isNotNull();
|
||||
if (!actual.getFullName().equals(fullName)) {
|
||||
failWithMessage("Expected person to have full name %s but was %s", fullName, actual.getFullName());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public PersonAssert isAdult() {
|
||||
isNotNull();
|
||||
if (actual.getAge() < 18) {
|
||||
failWithMessage("Expected person to be adult");
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public PersonAssert hasNickname(String nickName) {
|
||||
isNotNull();
|
||||
if (!actual.getNicknames().contains(nickName)) {
|
||||
failWithMessage("Expected person to have nickname %s", nickName);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user