Code and tests for the lombok introduction.
This commit is contained in:
parent
f4ee749afc
commit
7ecd7ce02d
151
lombok-intro/pom.xml
Normal file
151
lombok-intro/pom.xml
Normal file
@ -0,0 +1,151 @@
|
|||||||
|
<?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>
|
||||||
|
|
||||||
|
<name>lombok-intro</name>
|
||||||
|
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>lombok-intro</artifactId>
|
||||||
|
<version>0.1-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<!-- Check for the most recent available version:
|
||||||
|
https://projectlombok.org/changelog.html -->
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate.javax.persistence</groupId>
|
||||||
|
<artifactId>hibernate-jpa-2.1-api</artifactId>
|
||||||
|
<version>${hibernate-jpa-2.1-api.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- logging -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
<version>${org.slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>ch.qos.logback</groupId>
|
||||||
|
<artifactId>logback-core</artifactId>
|
||||||
|
<version>${logback.version}</version>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- test scoped -->
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
|
||||||
|
<finalName>lombok-intro</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
|
||||||
|
<plugins>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<source>${maven.compiler.source}</source>
|
||||||
|
<target>${maven.compiler.target}</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>${maven-surefire-plugin.version}</version>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok-maven-plugin</artifactId>
|
||||||
|
<version>${delombok-maven-plugin.version}</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>delombok</id>
|
||||||
|
<phase>generate-sources</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>delombok</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
|
||||||
|
<addOutputDirectory>false</addOutputDirectory>
|
||||||
|
<formatPreferences>
|
||||||
|
<javaLangAsFQN>skip</javaLangAsFQN>
|
||||||
|
</formatPreferences>
|
||||||
|
<verbose>false</verbose>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
<!-- This is for delomboking also your tests sources.
|
||||||
|
<execution>
|
||||||
|
<id>test-delombok</id>
|
||||||
|
<phase>generate-test-sources</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>testDelombok</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<verbose>false</verbose>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
-->
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
</plugins>
|
||||||
|
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
|
||||||
|
<!-- lombok: https://projectlombok.org/changelog.html -->
|
||||||
|
<lombok.version>1.16.8</lombok.version>
|
||||||
|
|
||||||
|
<!-- various -->
|
||||||
|
<hibernate-jpa-2.1-api.version>1.0.0.Final</hibernate-jpa-2.1-api.version>
|
||||||
|
|
||||||
|
<!-- logging -->
|
||||||
|
<org.slf4j.version>1.7.13</org.slf4j.version>
|
||||||
|
<logback.version>1.1.3</logback.version>
|
||||||
|
|
||||||
|
<!-- testing -->
|
||||||
|
<junit.version>4.12</junit.version>
|
||||||
|
|
||||||
|
<!-- maven plugins -->
|
||||||
|
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
|
||||||
|
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||||
|
|
||||||
|
<!-- delombok maven plugin -->
|
||||||
|
<delombok-maven-plugin.version>${lombok.version}.0</delombok-maven-plugin.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -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,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,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,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,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 ApiClientConfigurationTest {
|
||||||
|
|
||||||
|
@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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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 LoginResultTest {
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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 UserTest {
|
||||||
|
|
||||||
|
@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"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user