Java 11498 (#12382)
* added parent lombok-modules * added lombok submodule to parent lombok-modules * added lombok-2 submodule to parent lombok-modules * added lombok-custom submodule to parent lombok-modules * delete lombok submodules that were moved across Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.lombok.builder;
|
||||
|
||||
import lombok.Builder;
|
||||
|
||||
class ClientBuilder {
|
||||
|
||||
@Builder(builderMethodName = "builder")
|
||||
public static ImmutableClient newClient(int id, String name) {
|
||||
return new ImmutableClient(id, name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.lombok.builder;
|
||||
|
||||
import lombok.Value;
|
||||
|
||||
@Value
|
||||
final class ImmutableClient {
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.lombok.builder;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
|
||||
@Builder(builderMethodName = "internalBuilder")
|
||||
@Getter
|
||||
public class RequiredFieldAnnotation {
|
||||
|
||||
@NonNull
|
||||
String name;
|
||||
String description;
|
||||
|
||||
public static RequiredFieldAnnotationBuilder builder(String name) {
|
||||
return internalBuilder().name(name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.lombok.builder;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@Builder(toBuilder = true)
|
||||
public class Widget {
|
||||
|
||||
private final String name;
|
||||
private final int id;
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.lombok.builder.customsetter;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@Builder
|
||||
@Data
|
||||
public class Message {
|
||||
private String sender;
|
||||
private String recipient;
|
||||
private String text;
|
||||
private File file;
|
||||
|
||||
public static class MessageBuilder {
|
||||
private String text;
|
||||
private File file;
|
||||
|
||||
public MessageBuilder text(String text) {
|
||||
this.text = text;
|
||||
verifyTextOrFile();
|
||||
return this;
|
||||
}
|
||||
|
||||
public MessageBuilder file(File file) {
|
||||
this.file = file;
|
||||
verifyTextOrFile();
|
||||
return this;
|
||||
}
|
||||
|
||||
private void verifyTextOrFile() {
|
||||
if (text != null && file != null) {
|
||||
throw new IllegalStateException("Cannot send 'text' and 'file'.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.lombok.builder.defaultvalue;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder(toBuilder = true)
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Pojo {
|
||||
private String name = "foo";
|
||||
private boolean original = true;
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.lombok.builder.inheritance.buildermethodname;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class Child extends Parent {
|
||||
|
||||
private final String childName;
|
||||
private final int childAge;
|
||||
|
||||
@Builder(builderMethodName = "childBuilder")
|
||||
public Child(String parentName, int parentAge, String childName, int childAge) {
|
||||
super(parentName, parentAge);
|
||||
this.childName = childName;
|
||||
this.childAge = childAge;
|
||||
}
|
||||
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.lombok.builder.inheritance.buildermethodname;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@Builder
|
||||
public class Parent {
|
||||
private final String parentName;
|
||||
private final int parentAge;
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.lombok.builder.inheritance.buildermethodname;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class Student extends Child {
|
||||
|
||||
private final String schoolName;
|
||||
|
||||
@Builder(builderMethodName = "studentBuilder")
|
||||
public Student(String parentName, int parentAge, String childName, int childAge, String schoolName) {
|
||||
super(parentName, parentAge, childName, childAge);
|
||||
this.schoolName = schoolName;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.lombok.builder.inheritance.superbuilder;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@Getter
|
||||
@SuperBuilder(toBuilder = true)
|
||||
public class Child extends Parent {
|
||||
private final String childName;
|
||||
private final int childAge;
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.lombok.builder.inheritance.superbuilder;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@Getter
|
||||
@SuperBuilder(toBuilder = true)
|
||||
public class Parent {
|
||||
private final String parentName;
|
||||
private final int parentAge;
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.lombok.builder.inheritance.superbuilder;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@Getter
|
||||
@SuperBuilder(toBuilder = true)
|
||||
public class Student extends Child {
|
||||
private final String schoolName;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.lombok.builder.singular;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Singular;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@Getter
|
||||
@Builder
|
||||
public class Person {
|
||||
|
||||
private final String givenName;
|
||||
private final String additionalName;
|
||||
private final String familyName;
|
||||
|
||||
private final List<String> tags;
|
||||
@Singular private final List<String> interests;
|
||||
@Singular private final Set<String> skills;
|
||||
@Singular private final Map<String, LocalDate> awards;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.lombok.builder.singular;
|
||||
|
||||
import java.util.List;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Singular;
|
||||
|
||||
@Getter
|
||||
@Builder
|
||||
public class Sea {
|
||||
|
||||
@Singular private final List<String> grasses;
|
||||
@Singular("oneFish") private final List<String> fish;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.lombok.configexamples;
|
||||
|
||||
import lombok.*;
|
||||
import lombok.extern.java.Log;
|
||||
|
||||
import java.util.logging.Level;
|
||||
|
||||
import static java.lang.Math.abs;
|
||||
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
@Log
|
||||
public class Account {
|
||||
|
||||
@NonNull
|
||||
private Double balance = 0.;
|
||||
@NonNull
|
||||
private String accountHolder = "";
|
||||
|
||||
public Account addBalance(double amount) {
|
||||
if (amount < 0) {
|
||||
throw new IllegalArgumentException("Can not add negative amount");
|
||||
}
|
||||
|
||||
this.balance += amount;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Account withdraw(double amount) {
|
||||
if (this.balance - abs(amount) < 0) {
|
||||
domainLog.log(Level.INFO, String.format("Transaction denied for account holder: %s", this.accountHolder));
|
||||
throw new IllegalArgumentException(String.format("Not enough balance, you have %.2f", this.balance));
|
||||
}
|
||||
|
||||
this.balance -= abs(amount);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.lombok.configexamples;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
@Accessors(prefix = {"op"})
|
||||
public class TransactionLog {
|
||||
double amount;
|
||||
String description;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
clear lombok.experimental.flagUsage
|
||||
|
||||
lombok.anyconstructor.addconstructorproperties=true
|
||||
lombok.addNullAnnotations = jetbrains
|
||||
lombok.accessors.chain = true
|
||||
lombok.log.fieldName = domainLog
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.lombok.exclusions;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Data
|
||||
public class Employee {
|
||||
|
||||
@Setter(AccessLevel.NONE)
|
||||
private String name;
|
||||
|
||||
private String workplace;
|
||||
|
||||
@Getter(AccessLevel.NONE)
|
||||
private int workLength;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.lombok.exclusions;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
public class User {
|
||||
|
||||
@Setter(AccessLevel.NONE)
|
||||
private long id;
|
||||
|
||||
private String login;
|
||||
|
||||
@Getter(AccessLevel.NONE)
|
||||
private int age;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.lombok.getter;
|
||||
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Related Article Sections:
|
||||
* 4. Using @Getter on a Boolean Field
|
||||
*
|
||||
*/
|
||||
public class GetterBoolean {
|
||||
|
||||
@Getter
|
||||
private Boolean running = true;
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.lombok.getter;
|
||||
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Related Article Sections:
|
||||
* 3. Using @Getter on a boolean Field
|
||||
*
|
||||
*/
|
||||
public class GetterBooleanPrimitive {
|
||||
|
||||
@Getter
|
||||
private boolean running;
|
||||
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.lombok.getter;
|
||||
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Related Article Sections:
|
||||
* 3.2. Two boolean Fields With the Same Accessor Name
|
||||
*
|
||||
*/
|
||||
public class GetterBooleanPrimitiveSameAccessor {
|
||||
|
||||
@Getter
|
||||
boolean running = true;
|
||||
|
||||
@Getter
|
||||
boolean isRunning = false;
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.lombok.getter;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Related Article Sections:
|
||||
* 3.1. A boolean Field Having the Same Name With Its Accessor
|
||||
*
|
||||
*/
|
||||
public class GetterBooleanSameAccessor {
|
||||
@Getter
|
||||
private boolean isRunning = true;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.lombok.getter;
|
||||
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Related Article Sections:
|
||||
* 4. Using @Getter on a Boolean Field
|
||||
*
|
||||
*/
|
||||
public class GetterBooleanType {
|
||||
|
||||
@Getter
|
||||
private Boolean running = true;
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.lombok.intro;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Builder
|
||||
@Slf4j
|
||||
@Getter
|
||||
public class ApiClientConfiguration {
|
||||
|
||||
private String host;
|
||||
private int port;
|
||||
private boolean useHttps;
|
||||
|
||||
private long connectTimeout;
|
||||
private long readTimeout;
|
||||
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.lombok.intro;
|
||||
|
||||
import lombok.Builder;
|
||||
|
||||
class ClientBuilder {
|
||||
|
||||
@Builder(builderMethodName = "builder")
|
||||
public static ImmutableClient newClient(int id, String name) {
|
||||
return new ImmutableClient(id, name);
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.lombok.intro;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ContactInformationSupport implements HasContactInformation {
|
||||
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String phoneNr;
|
||||
|
||||
@Override
|
||||
public String getFullName() {
|
||||
return getFirstName() + " " + getLastName();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.lombok.intro;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
public class GetterLazy {
|
||||
|
||||
private static final String DELIMETER = ",";
|
||||
|
||||
@Getter(lazy = true)
|
||||
private final Map<String, Long> transactions = getTransactions();
|
||||
|
||||
private Map<String, Long> getTransactions() {
|
||||
|
||||
final Map<String, Long> cache = new HashMap<>();
|
||||
List<String> txnRows = readTxnListFromFile();
|
||||
|
||||
txnRows.forEach(s -> {
|
||||
String[] txnIdValueTuple = s.split(DELIMETER);
|
||||
cache.put(txnIdValueTuple[0], Long.parseLong(txnIdValueTuple[1]));
|
||||
});
|
||||
|
||||
return cache;
|
||||
}
|
||||
|
||||
private List<String> readTxnListFromFile() {
|
||||
|
||||
// read large file
|
||||
return Stream.of("file content here").collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.lombok.intro;
|
||||
|
||||
public interface HasContactInformation {
|
||||
|
||||
String getFirstName();
|
||||
void setFirstName(String firstName);
|
||||
|
||||
String getFullName();
|
||||
|
||||
String getLastName();
|
||||
void setLastName(String lastName);
|
||||
|
||||
String getPhoneNr();
|
||||
void setPhoneNr(String phoneNr);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.lombok.intro;
|
||||
|
||||
import lombok.Value;
|
||||
|
||||
@Value
|
||||
final class ImmutableClient {
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.lombok.intro;
|
||||
|
||||
import java.net.URL;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Accessors(fluent = true) @Getter
|
||||
@EqualsAndHashCode(of = {"authToken"})
|
||||
public class LoginResult {
|
||||
|
||||
private final @NonNull Instant loginTs;
|
||||
|
||||
private final @NonNull String authToken;
|
||||
private final @NonNull Duration tokenValidity;
|
||||
|
||||
private final @NonNull URL tokenRefreshUrl;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.lombok.intro;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Delegate;
|
||||
|
||||
@Entity
|
||||
@Getter @Setter @NoArgsConstructor // <--- THIS is it
|
||||
@ToString(exclude = {"events"})
|
||||
public class User implements Serializable, HasContactInformation {
|
||||
|
||||
private @Id @Setter(AccessLevel.PROTECTED) Long id; // will be set when persisting
|
||||
|
||||
private String nickname;
|
||||
|
||||
// Whichever other User-specific attributes
|
||||
|
||||
@Delegate(types = {HasContactInformation.class})
|
||||
private final ContactInformationSupport contactInformation = new ContactInformationSupport();
|
||||
|
||||
// User itelf will implement all contact information by delegation
|
||||
|
||||
@OneToMany(mappedBy = "user")
|
||||
private List<UserEvent> events;
|
||||
|
||||
public User(String nickname, String firstName, String lastName, String phoneNr) {
|
||||
this.nickname = nickname;
|
||||
contactInformation.setFirstName(firstName);
|
||||
contactInformation.setLastName(lastName);
|
||||
contactInformation.setPhoneNr(phoneNr);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.lombok.intro;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Entity
|
||||
@NoArgsConstructor @Getter @Setter
|
||||
public class UserEvent implements Serializable {
|
||||
|
||||
// This class is just for sample purposes.
|
||||
|
||||
private @Id @Setter(AccessLevel.PROTECTED) Long id;
|
||||
|
||||
@ManyToOne
|
||||
private User user;
|
||||
|
||||
public UserEvent(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.lombok.intro;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.Synchronized;
|
||||
|
||||
public class Utility {
|
||||
|
||||
@SneakyThrows
|
||||
public String resourceAsString() throws IOException {
|
||||
try (InputStream is = this.getClass().getResourceAsStream("sure_in_my_jar.txt")) {
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
||||
return br.lines().collect(Collectors.joining("\n"));
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
public void putValueInCache(String key, String value) {
|
||||
System.out.println("Thread safe here with key : [" + key + "] and value[" + value + "]");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1 @@
|
||||
Hello
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.lombok.builder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class BuilderUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenBuilder_WidgetIsBuilt() {
|
||||
Widget testWidget = Widget.builder()
|
||||
.name("foo")
|
||||
.id(1)
|
||||
.build();
|
||||
assertThat(testWidget.getName()).isEqualTo("foo");
|
||||
assertThat(testWidget.getId()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenToBuilder_whenToBuilder_BuilderIsCreated() {
|
||||
|
||||
Widget testWidget = Widget.builder()
|
||||
.name("foo")
|
||||
.id(1)
|
||||
.build();
|
||||
Widget.WidgetBuilder widgetBuilder = testWidget.toBuilder();
|
||||
|
||||
Widget newWidget = widgetBuilder.id(2)
|
||||
.build();
|
||||
assertThat(newWidget.getName()).isEqualTo("foo");
|
||||
assertThat(newWidget.getId()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBuilderMethod_ClientIsBuilt() {
|
||||
ImmutableClient testImmutableClient = ClientBuilder.builder()
|
||||
.name("foo")
|
||||
.id(1)
|
||||
.build();
|
||||
assertThat(testImmutableClient.getName()).isEqualTo("foo");
|
||||
assertThat(testImmutableClient.getId()).isEqualTo(1);
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.lombok.builder;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class RequiredFieldAnnotationUnitTest {
|
||||
RequiredFieldAnnotation requiredFieldTest;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
requiredFieldTest = RequiredFieldAnnotation.builder("NameField").description("Field Description").build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBuilderWithRequiredParameter_thenParameterIsPresent() {
|
||||
assertEquals("NameField", requiredFieldTest.getName());
|
||||
}
|
||||
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.lombok.builder.customsetter;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class BuilderWithCustomSetterUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenBuilderWithCustomSetter_TestTextOnly() {
|
||||
Message message = Message.builder()
|
||||
.sender("user@somedomain.com")
|
||||
.recipient("someuser@otherdomain.com")
|
||||
.text("How are you today?")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBuilderWithCustomSetter_TestFileOnly() {
|
||||
Message message = Message.builder()
|
||||
.sender("user@somedomain.com")
|
||||
.recipient("someuser@otherdomain.com")
|
||||
.file(new File("/path/to/file"))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void givenBuilderWithCustomSetter_TestTextAndFile() {
|
||||
Message message = Message.builder()
|
||||
.sender("user@somedomain.com")
|
||||
.recipient("someuser@otherdomain.com")
|
||||
.text("How are you today?")
|
||||
.file(new File("/path/to/file"))
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.lombok.builder.defaultvalue;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class BuilderWithDefaultValueUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenBuilderWithDefaultValue_ThanDefaultValueIsPresent() {
|
||||
Pojo build = new Pojo().toBuilder()
|
||||
.build();
|
||||
Assert.assertEquals("foo", build.getName());
|
||||
Assert.assertTrue(build.isOriginal());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBuilderWithDefaultValue_NoArgsWorksAlso() {
|
||||
Pojo build = new Pojo().toBuilder()
|
||||
.build();
|
||||
Pojo pojo = new Pojo();
|
||||
Assert.assertEquals(build.getName(), pojo.getName());
|
||||
Assert.assertTrue(build.isOriginal() == pojo.isOriginal());
|
||||
}
|
||||
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.lombok.builder.inheritance.buildermethodname;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class BuilderInheritanceUsingMethodNameUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenBuilderAtMethodLevel_ChildInheritingParentIsBuilt() {
|
||||
Child child = Child.childBuilder()
|
||||
.parentName("Andrea")
|
||||
.parentAge(38)
|
||||
.childName("Emma")
|
||||
.childAge(6)
|
||||
.build();
|
||||
|
||||
assertThat(child.getChildName()).isEqualTo("Emma");
|
||||
assertThat(child.getChildAge()).isEqualTo(6);
|
||||
assertThat(child.getParentName()).isEqualTo("Andrea");
|
||||
assertThat(child.getParentAge()).isEqualTo(38);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSuperBuilderOnAllThreeLevels_StudentInheritingChildAndParentIsBuilt() {
|
||||
Student student = Student.studentBuilder()
|
||||
.parentName("Andrea")
|
||||
.parentAge(38)
|
||||
.childName("Emma")
|
||||
.childAge(6)
|
||||
.schoolName("Baeldung High School")
|
||||
.build();
|
||||
|
||||
assertThat(student.getChildName()).isEqualTo("Emma");
|
||||
assertThat(student.getChildAge()).isEqualTo(6);
|
||||
assertThat(student.getParentName()).isEqualTo("Andrea");
|
||||
assertThat(student.getParentAge()).isEqualTo(38);
|
||||
assertThat(student.getSchoolName()).isEqualTo("Baeldung High School");
|
||||
}
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
package com.baeldung.lombok.builder.inheritance.superbuilder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class BuilderInheritanceUsingSuperBuilderUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSuperBuilderOnParentAndOnChild_ChildInheritingParentIsBuilt() {
|
||||
Child child = Child.builder()
|
||||
.parentName("Andrea")
|
||||
.parentAge(38)
|
||||
.childName("Emma")
|
||||
.childAge(6)
|
||||
.build();
|
||||
|
||||
assertThat(child.getChildName()).isEqualTo("Emma");
|
||||
assertThat(child.getChildAge()).isEqualTo(6);
|
||||
assertThat(child.getParentName()).isEqualTo("Andrea");
|
||||
assertThat(child.getParentAge()).isEqualTo(38);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSuperBuilderOnParent_StandardBuilderIsBuilt() {
|
||||
Parent parent = Parent.builder()
|
||||
.parentName("Andrea")
|
||||
.parentAge(38)
|
||||
.build();
|
||||
|
||||
assertThat(parent.getParentName()).isEqualTo("Andrea");
|
||||
assertThat(parent.getParentAge()).isEqualTo(38);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenToBuilderIsSetToTrueOnParentAndChild_DeepCopyViaBuilderIsPossible() {
|
||||
Child child1 = Child.builder()
|
||||
.parentName("Andrea")
|
||||
.parentAge(38)
|
||||
.childName("Emma")
|
||||
.childAge(6)
|
||||
.build();
|
||||
|
||||
Child child2 = child1.toBuilder()
|
||||
.childName("Anna")
|
||||
.build();
|
||||
|
||||
assertThat(child2.getChildName()).isEqualTo("Anna");
|
||||
assertThat(child2.getChildAge()).isEqualTo(6);
|
||||
assertThat(child2.getParentName()).isEqualTo("Andrea");
|
||||
assertThat(child2.getParentAge()).isEqualTo(38);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSuperBuilderOnAllThreeLevels_StudentInheritingChildAndParentIsBuilt() {
|
||||
Student student = Student.builder()
|
||||
.parentName("Andrea")
|
||||
.parentAge(38)
|
||||
.childName("Emma")
|
||||
.childAge(6)
|
||||
.schoolName("Baeldung High School")
|
||||
.build();
|
||||
|
||||
assertThat(student.getChildName()).isEqualTo("Emma");
|
||||
assertThat(student.getChildAge()).isEqualTo(6);
|
||||
assertThat(student.getParentName()).isEqualTo("Andrea");
|
||||
assertThat(student.getParentAge()).isEqualTo(38);
|
||||
assertThat(student.getSchoolName()).isEqualTo("Baeldung High School");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenToBuilderIsSetToTrueOnParentChildAndStudent_DeepCopyViaBuilderIsPossible() {
|
||||
Student student1 = Student.builder()
|
||||
.parentName("Andrea")
|
||||
.parentAge(38)
|
||||
.childName("Emma")
|
||||
.childAge(6)
|
||||
.schoolName("School 1")
|
||||
.build();
|
||||
|
||||
Student student2 = student1.toBuilder()
|
||||
.childName("Anna")
|
||||
.schoolName("School 2")
|
||||
.build();
|
||||
|
||||
assertThat(student2.getChildName()).isEqualTo("Anna");
|
||||
assertThat(student2.getChildAge()).isEqualTo(6);
|
||||
assertThat(student2.getParentName()).isEqualTo("Andrea");
|
||||
assertThat(student2.getParentAge()).isEqualTo(38);
|
||||
assertThat(student2.getSchoolName()).isEqualTo("School 2");
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+192
@@ -0,0 +1,192 @@
|
||||
package com.baeldung.lombok.builder.singular;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
public class BuilderWithSingularSupportForCollectionsUnitTest {
|
||||
|
||||
@Test
|
||||
public void canAddMultipleElementsAsNewCollection() throws Exception {
|
||||
Person person = Person.builder()
|
||||
.givenName("Aaron")
|
||||
.additionalName("A")
|
||||
.familyName("Aardvark")
|
||||
.tags(Arrays.asList("fictional", "incidental"))
|
||||
.build();
|
||||
|
||||
assertThat(person.getTags(), containsInAnyOrder("fictional", "incidental"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canUpdateCollectionAfterBuildIfMutableCollectionPassedToBuilder() throws Exception {
|
||||
|
||||
List<String> tags = new ArrayList();
|
||||
tags.add("fictional");
|
||||
tags.add("incidental");
|
||||
Person person = Person.builder()
|
||||
.givenName("Aaron")
|
||||
.additionalName("A")
|
||||
.familyName("Aardvark")
|
||||
.tags(tags)
|
||||
.build();
|
||||
person.getTags()
|
||||
.clear();
|
||||
person.getTags()
|
||||
.add("non-fictional");
|
||||
person.getTags()
|
||||
.add("important");
|
||||
|
||||
assertThat(person.getTags(), containsInAnyOrder("non-fictional", "important"));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void cannotUpdateCollectionAfterBuildIfImmutableCollectionPassedToBuilder() throws Exception {
|
||||
List<String> tags = Arrays.asList("fictional", "incidental");
|
||||
Person person = Person.builder()
|
||||
.givenName("Aaron")
|
||||
.additionalName("A")
|
||||
.familyName("Aardvark")
|
||||
.tags(tags)
|
||||
.build();
|
||||
person.getTags()
|
||||
.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canAssignToSingularAnnotatedCollectionOneByOne() throws Exception {
|
||||
|
||||
Person person = Person.builder()
|
||||
.givenName("Aaron")
|
||||
.additionalName("A")
|
||||
.familyName("Aardvark")
|
||||
.interest("history")
|
||||
.interest("sport")
|
||||
.build();
|
||||
|
||||
assertThat(person.getInterests(), containsInAnyOrder("sport", "history"));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void singularAnnotatedBuilderCreatesImmutableCollection() throws Exception {
|
||||
|
||||
Person person = Person.builder()
|
||||
.givenName("Aaron")
|
||||
.additionalName("A")
|
||||
.familyName("Aardvark")
|
||||
.interest("history")
|
||||
.interest("sport")
|
||||
.build();
|
||||
person.getInterests()
|
||||
.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unpopulatedListsCreatedAsNullIfNotSingularButEmptyArrayIfSingular() throws Exception {
|
||||
|
||||
Person person = Person.builder()
|
||||
.givenName("Aaron")
|
||||
.additionalName("A")
|
||||
.familyName("Aardvark")
|
||||
.build();
|
||||
assertThat(person.getInterests(), hasSize(0));
|
||||
assertThat(person.getSkills(), hasSize(0));
|
||||
assertThat(person.getAwards()
|
||||
.keySet(), hasSize(0));
|
||||
assertThat(person.getTags(), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singularSupportsSetsToo() throws Exception {
|
||||
|
||||
Person person = Person.builder()
|
||||
.givenName("Aaron")
|
||||
.additionalName("A")
|
||||
.familyName("Aardvark")
|
||||
.skill("singing")
|
||||
.skill("dancing")
|
||||
.build();
|
||||
assertThat(person.getSkills(), contains("singing", "dancing"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singularSetsAreLenientWithDuplicates() throws Exception {
|
||||
|
||||
Person person = Person.builder()
|
||||
.givenName("Aaron")
|
||||
.additionalName("A")
|
||||
.familyName("Aardvark")
|
||||
.interest("singing")
|
||||
.interest("singing")
|
||||
.skill("singing")
|
||||
.skill("singing")
|
||||
.build();
|
||||
assertThat(person.getInterests(), contains("singing", "singing"));
|
||||
assertThat(person.getSkills(), contains("singing"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singularSupportsMapsToo() throws Exception {
|
||||
|
||||
Person person = Person.builder()
|
||||
.givenName("Aaron")
|
||||
.additionalName("A")
|
||||
.familyName("Aardvark")
|
||||
.award("Singer of the Year", LocalDate.now()
|
||||
.minusYears(5))
|
||||
.award("Best Dancer", LocalDate.now()
|
||||
.minusYears(2))
|
||||
.build();
|
||||
assertThat(person.getAwards()
|
||||
.keySet(), contains("Singer of the Year", "Best Dancer"));
|
||||
assertThat(person.getAwards()
|
||||
.get("Best Dancer"),
|
||||
is(LocalDate.now()
|
||||
.minusYears(2)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singularIsLenientWithMapKeys() throws Exception {
|
||||
|
||||
Person person = Person.builder()
|
||||
.givenName("Aaron")
|
||||
.additionalName("A")
|
||||
.familyName("Aardvark")
|
||||
.award("Best Dancer", LocalDate.now()
|
||||
.minusYears(5))
|
||||
.award("Best Dancer", LocalDate.now()
|
||||
.minusYears(4))
|
||||
.award("Best Dancer", LocalDate.now()
|
||||
.minusYears(3))
|
||||
.award("Best Dancer", LocalDate.now()
|
||||
.minusYears(2))
|
||||
.award("Best Dancer", LocalDate.now()
|
||||
.minusYears(1))
|
||||
.build();
|
||||
assertThat(person.getAwards()
|
||||
.keySet(), hasSize(1));
|
||||
assertThat(person.getAwards()
|
||||
.get("Best Dancer"),
|
||||
is(LocalDate.now()
|
||||
.minusYears(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wordsWithNonStandardPlurals() throws Exception {
|
||||
Sea sea = Sea.builder()
|
||||
.grass("Dulse")
|
||||
.grass("Kelp")
|
||||
.oneFish("Cod")
|
||||
.oneFish("Mackerel")
|
||||
.build();
|
||||
assertThat(sea.getGrasses(), contains("Dulse", "Kelp"));
|
||||
assertThat(sea.getFish(), contains("Cod", "Mackerel"));
|
||||
}
|
||||
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.lombok.configexamples;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class AccountUnitTest {
|
||||
|
||||
@Test
|
||||
void should_initialize_account() {
|
||||
Account myAccount = new Account()
|
||||
.setBalance(2000.00)
|
||||
.setAccountHolder("John Snow");
|
||||
|
||||
assertEquals(2000.00, myAccount.getBalance());
|
||||
assertEquals("John Snow", myAccount.getAccountHolder());
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_throw_error_when_balance_becomes_negative() {
|
||||
Account myAccount = new Account()
|
||||
.setBalance(20.00)
|
||||
.setAccountHolder("John Snow");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> myAccount.withdraw(100.00));
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_throw_no_error_when_balance_becomes_zero() {
|
||||
Account myAccount = new Account()
|
||||
.setBalance(20.00)
|
||||
.setAccountHolder("John Snow");
|
||||
|
||||
myAccount.withdraw(20.00);
|
||||
|
||||
assertEquals(0.00, myAccount.getBalance());
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_update_balance_properly() {
|
||||
Account myAccount = new Account()
|
||||
.setBalance(20.00)
|
||||
.setAccountHolder("John Snow");
|
||||
|
||||
myAccount.withdraw(5.00).withdraw(10.00);
|
||||
|
||||
assertEquals(5.00, myAccount.getBalance());
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.lombok.getter;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class GetterBooleanUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenBasicBooleanField_thenMethodNamePrefixedWithIsFollowedByFieldName() {
|
||||
GetterBooleanPrimitive lombokExamples = new GetterBooleanPrimitive();
|
||||
assertFalse(lombokExamples.isRunning());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBooleanFieldPrefixedWithIs_thenMethodNameIsSameAsFieldName() {
|
||||
GetterBooleanSameAccessor lombokExamples = new GetterBooleanSameAccessor();
|
||||
assertTrue(lombokExamples.isRunning());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTwoBooleanFieldsCauseNamingConflict_thenLombokMapsToFirstDeclaredField() {
|
||||
GetterBooleanPrimitiveSameAccessor lombokExamples = new GetterBooleanPrimitiveSameAccessor();
|
||||
assertTrue(lombokExamples.isRunning() == lombokExamples.running);
|
||||
assertFalse(lombokExamples.isRunning() == lombokExamples.isRunning);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFieldOfBooleanType_thenLombokPrefixesMethodWithGetInsteadOfIs() {
|
||||
GetterBooleanType lombokExamples = new GetterBooleanType();
|
||||
assertTrue(lombokExamples.getRunning());
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.lombok.intro;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
import com.baeldung.lombok.intro.ApiClientConfiguration.ApiClientConfigurationBuilder;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ApiClientConfigurationIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenAnnotatedConfiguration_thenCanBeBuiltViaBuilder() {
|
||||
ApiClientConfiguration config =
|
||||
new ApiClientConfigurationBuilder()
|
||||
.host("api.server.com")
|
||||
.port(443)
|
||||
.useHttps(true)
|
||||
.connectTimeout(15_000L)
|
||||
.readTimeout(5_000L)
|
||||
.username("myusername")
|
||||
.password("secret")
|
||||
.build();
|
||||
|
||||
Assert.assertEquals(config.getHost(), "api.server.com");
|
||||
Assert.assertEquals(config.getPort(), 443);
|
||||
Assert.assertEquals(config.isUseHttps(), true);
|
||||
Assert.assertEquals(config.getConnectTimeout(), 15_000L);
|
||||
Assert.assertEquals(config.getReadTimeout(), 5_000L);
|
||||
Assert.assertEquals(config.getUsername(), "myusername");
|
||||
Assert.assertEquals(config.getPassword(), "secret");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnnotatedConfiguration_thenHasLoggerInstance() throws NoSuchFieldException {
|
||||
Field loggerInstance = ApiClientConfiguration.class.getDeclaredField("log");
|
||||
int modifiers = loggerInstance.getModifiers();
|
||||
Assert.assertTrue(Modifier.isPrivate(modifiers));
|
||||
Assert.assertTrue(Modifier.isStatic(modifiers));
|
||||
Assert.assertTrue(Modifier.isFinal(modifiers));
|
||||
}
|
||||
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.lombok.intro;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class BuilderMethodUnitTest
|
||||
{
|
||||
|
||||
@Test
|
||||
public void givenBuilderMethod_ClientIsBuilt() {
|
||||
ImmutableClient testImmutableClient = ClientBuilder.builder().name("foo").id(1).build();
|
||||
assertThat(testImmutableClient.getName())
|
||||
.isEqualTo("foo");
|
||||
assertThat(testImmutableClient.getId())
|
||||
.isEqualTo(1);
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.lombok.intro;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class LoginResultLiveTest {
|
||||
|
||||
@Test
|
||||
public void givenAnnotatedLoginResult_thenHasConstructorForAllFinalFields()
|
||||
throws MalformedURLException {
|
||||
/* LoginResult loginResult = */ new LoginResult(
|
||||
Instant.now(),
|
||||
"apitoken",
|
||||
Duration.ofHours(1),
|
||||
new URL("https://api.product.com/token-refresh"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnnotatedLoginResult_thenHasFluentGetters()
|
||||
throws MalformedURLException {
|
||||
Instant loginTs = Instant.now();
|
||||
LoginResult loginResult = new LoginResult(
|
||||
loginTs,
|
||||
"apitoken",
|
||||
Duration.ofHours(1),
|
||||
new URL("https://api.product.com/token-refresh"));
|
||||
|
||||
Assert.assertEquals(loginResult.loginTs(), loginTs);
|
||||
Assert.assertEquals(loginResult.authToken(), "apitoken");
|
||||
Assert.assertEquals(loginResult.tokenValidity(), Duration.ofHours(1));
|
||||
Assert.assertEquals(loginResult.tokenRefreshUrl(), new URL("https://api.product.com/token-refresh"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnnotatedLoginResult_whenSameApiToken_thenEqualInstances()
|
||||
throws MalformedURLException {
|
||||
String theSameApiToken = "testapitoken";
|
||||
|
||||
LoginResult loginResult1 = new LoginResult(
|
||||
Instant.now(),
|
||||
theSameApiToken,
|
||||
Duration.ofHours(1),
|
||||
new URL("https://api.product.com/token-refresh"));
|
||||
|
||||
LoginResult loginResult2 = new LoginResult(
|
||||
Instant.now(),
|
||||
theSameApiToken,
|
||||
Duration.ofHours(2),
|
||||
new URL("https://api.product.com/token-refresh-alt"));
|
||||
|
||||
Assert.assertEquals(loginResult1, loginResult2);
|
||||
}
|
||||
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.lombok.intro;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UserIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenAnnotatedUser_thenHasEmptyConstructor() {
|
||||
/* User user = */ new User();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnnotatedUser_thenHasGettersAndSetters() {
|
||||
User user = new User("testnickname", "Test", "JUnit", "123456");
|
||||
|
||||
Assert.assertEquals("testnickname", user.getNickname());
|
||||
Assert.assertEquals("Test", user.getFirstName());
|
||||
Assert.assertEquals("JUnit", user.getLastName());
|
||||
Assert.assertEquals("123456", user.getPhoneNr());
|
||||
|
||||
user.setNickname("testnickname2");
|
||||
user.setFirstName("Test2");
|
||||
user.setLastName("JUnit2");
|
||||
user.setPhoneNr("654321");
|
||||
|
||||
Assert.assertEquals("testnickname2", user.getNickname());
|
||||
Assert.assertEquals("Test2", user.getFirstName());
|
||||
Assert.assertEquals("JUnit2", user.getLastName());
|
||||
Assert.assertEquals("654321", user.getPhoneNr());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnnotatedUser_thenHasProtectedSetId() throws NoSuchMethodException {
|
||||
Method setIdMethod = User.class.getDeclaredMethod("setId", Long.class);
|
||||
int modifiers = setIdMethod.getModifiers();
|
||||
Assert.assertTrue(Modifier.isProtected(modifiers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnnotatedUser_thenImplementsHasContactInformation() {
|
||||
User user = new User("testnickname3", "Test3", "JUnit3", "987654");
|
||||
Assert.assertTrue(user instanceof HasContactInformation);
|
||||
|
||||
Assert.assertEquals("Test3", user.getFirstName());
|
||||
Assert.assertEquals("JUnit3", user.getLastName());
|
||||
Assert.assertEquals("987654", user.getPhoneNr());
|
||||
Assert.assertEquals("Test3 JUnit3", user.getFullName());
|
||||
|
||||
user.setFirstName("Test4");
|
||||
user.setLastName("JUnit4");
|
||||
user.setPhoneNr("456789");
|
||||
|
||||
Assert.assertEquals("Test4", user.getFirstName());
|
||||
Assert.assertEquals("JUnit4", user.getLastName());
|
||||
Assert.assertEquals("456789", user.getPhoneNr());
|
||||
Assert.assertEquals("Test4 JUnit4", user.getFullName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnnotatedUser_whenHasEvents_thenToStringDumpsNoEvents() {
|
||||
User user = new User("testnickname", "Test", "JUnit", "123456");
|
||||
List<UserEvent> events = Arrays.asList(new UserEvent(user), new UserEvent(user));
|
||||
user.setEvents(events);
|
||||
Assert.assertFalse(user.toString().contains("events"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user