Merge branch 'eugenp:master' into master
This commit is contained in:
@@ -12,3 +12,4 @@ This module contains articles about Apache POI
|
||||
- [Read Excel Cell Value Rather Than Formula With Apache POI](https://www.baeldung.com/apache-poi-read-cell-value-formula)
|
||||
- [Setting Formulas in Excel with Apache POI](https://www.baeldung.com/java-apache-poi-set-formulas)
|
||||
- [Insert a Row in Excel Using Apache POI](https://www.baeldung.com/apache-poi-insert-excel-row)
|
||||
- [Multiline Text in Excel Cell Using Apache POI](https://www.baeldung.com/apache-poi-write-multiline-text)
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.poi.excel.cellstyle;
|
||||
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.apache.poi.ss.usermodel.FillPatternType;
|
||||
import org.apache.poi.ss.usermodel.IndexedColors;
|
||||
|
||||
public class CellStyleHandler {
|
||||
|
||||
public void changeCellBackgroundColor(Cell cell) {
|
||||
CellStyle cellStyle = cell.getCellStyle();
|
||||
if(cellStyle == null) {
|
||||
cellStyle = cell.getSheet().getWorkbook().createCellStyle();
|
||||
}
|
||||
cellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
|
||||
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||||
cell.setCellStyle(cellStyle);
|
||||
}
|
||||
|
||||
public void changeCellBackgroundColorWithPattern(Cell cell) {
|
||||
CellStyle cellStyle = cell.getCellStyle();
|
||||
if(cellStyle == null) {
|
||||
cellStyle = cell.getSheet().getWorkbook().createCellStyle();
|
||||
}
|
||||
cellStyle.setFillBackgroundColor(IndexedColors.BLACK.index);
|
||||
cellStyle.setFillPattern(FillPatternType.BIG_SPOTS);
|
||||
cellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
|
||||
cell.setCellStyle(cellStyle);
|
||||
}
|
||||
}
|
||||
Binary file not shown.
+56
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.poi.excel.cellstyle;
|
||||
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class CellStyleHandlerUnitTest {
|
||||
private static final String FILE_NAME = "cellstyle/CellStyleHandlerTest.xlsx";
|
||||
private static final int SHEET_INDEX = 0;
|
||||
private static final int ROW_INDEX = 0;
|
||||
private static final int CELL_INDEX = 0;
|
||||
|
||||
private String fileLocation;
|
||||
private CellStyleHandler cellStyleHandler;
|
||||
|
||||
@Before
|
||||
public void setup() throws URISyntaxException {
|
||||
fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME).toURI()).toString();
|
||||
cellStyleHandler = new CellStyleHandler();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWorkbookCell_whenChangeCellBackgroundColor() throws IOException {
|
||||
Workbook workbook = new XSSFWorkbook(fileLocation);
|
||||
Sheet sheet = workbook.getSheetAt(SHEET_INDEX);
|
||||
Row row = sheet.getRow(ROW_INDEX);
|
||||
Cell cell = row.getCell(CELL_INDEX);
|
||||
|
||||
cellStyleHandler.changeCellBackgroundColor(cell);
|
||||
|
||||
assertEquals(IndexedColors.LIGHT_BLUE.index, cell.getCellStyle().getFillForegroundColor());
|
||||
workbook.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWorkbookCell_whenChangeCellBackgroundColorWithPattern() throws IOException {
|
||||
Workbook workbook = new XSSFWorkbook(fileLocation);
|
||||
Sheet sheet = workbook.getSheetAt(SHEET_INDEX);
|
||||
Row row = sheet.getRow(ROW_INDEX);
|
||||
Cell cell = row.getCell(CELL_INDEX + 1);
|
||||
|
||||
cellStyleHandler.changeCellBackgroundColorWithPattern(cell);
|
||||
|
||||
assertEquals(IndexedColors.LIGHT_BLUE.index, cell.getCellStyle().getFillForegroundColor());
|
||||
workbook.close();
|
||||
}
|
||||
}
|
||||
@@ -32,24 +32,6 @@
|
||||
<artifactId>mockserver-junit-jupiter</artifactId>
|
||||
<version>${mockserver.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-params</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
|
||||
@@ -22,18 +22,6 @@
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -27,18 +27,6 @@
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -28,18 +28,6 @@
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.12.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -23,18 +23,6 @@
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -15,11 +15,6 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.thread-weaver</groupId>
|
||||
<artifactId>threadweaver</artifactId>
|
||||
@@ -31,6 +26,12 @@
|
||||
<artifactId>tempus-fugit</artifactId>
|
||||
<version>${tempus-fugit.version}</version>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.multithreadedtc</groupId>
|
||||
|
||||
@@ -2,3 +2,4 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Java Naming and Directory Interface Overview](https://www.baeldung.com/jndi)
|
||||
- [LDAP Authentication Using Pure Java](https://www.baeldung.com/java-ldap-auth)
|
||||
|
||||
@@ -15,23 +15,6 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
@@ -58,6 +41,18 @@
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.directory.server</groupId>
|
||||
<artifactId>apacheds-test-framework</artifactId>
|
||||
<version>${apacheds.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>3.21.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -76,6 +71,7 @@
|
||||
<properties>
|
||||
<spring.version>5.0.9.RELEASE</spring.version>
|
||||
<h2.version>1.4.199</h2.version>
|
||||
<apacheds.version>2.0.0.AM26</apacheds.version>
|
||||
<source.version>1.8</source.version>
|
||||
<target.version>1.8</target.version>
|
||||
</properties>
|
||||
|
||||
+165
@@ -0,0 +1,165 @@
|
||||
package com.baeldung.jndi.ldap.auth;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
import javax.naming.AuthenticationException;
|
||||
import javax.naming.Context;
|
||||
import javax.naming.NamingEnumeration;
|
||||
import javax.naming.directory.Attributes;
|
||||
import javax.naming.directory.DirContext;
|
||||
import javax.naming.directory.InitialDirContext;
|
||||
import javax.naming.directory.SearchControls;
|
||||
import javax.naming.directory.SearchResult;
|
||||
|
||||
import org.apache.directory.server.annotations.CreateLdapServer;
|
||||
import org.apache.directory.server.annotations.CreateTransport;
|
||||
import org.apache.directory.server.core.annotations.ApplyLdifFiles;
|
||||
import org.apache.directory.server.core.annotations.CreateDS;
|
||||
import org.apache.directory.server.core.annotations.CreatePartition;
|
||||
import org.apache.directory.server.core.integ.AbstractLdapTestUnit;
|
||||
import org.apache.directory.server.core.integ.FrameworkRunner;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(FrameworkRunner.class)
|
||||
@CreateLdapServer(transports = { @CreateTransport(protocol = "LDAP", address = "localhost", port = 10390)})
|
||||
@CreateDS(
|
||||
allowAnonAccess = false, partitions = {@CreatePartition(name = "TestPartition", suffix = "dc=baeldung,dc=com")})
|
||||
@ApplyLdifFiles({"users.ldif"})
|
||||
// class marked as manual test, as it has to run independently from the other unit tests in the module
|
||||
public class JndiLdapAuthManualTest extends AbstractLdapTestUnit {
|
||||
|
||||
private static void authenticateUser(Hashtable<String, String> environment) throws Exception {
|
||||
DirContext context = new InitialDirContext(environment);
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPreloadedLDAPUserJoe_whenAuthUserWithCorrectPW_thenAuthSucceeds() throws Exception {
|
||||
|
||||
Hashtable<String, String> environment = new Hashtable<String, String>();
|
||||
environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
|
||||
environment.put(Context.PROVIDER_URL, "ldap://localhost:10390");
|
||||
environment.put(Context.SECURITY_AUTHENTICATION, "simple");
|
||||
|
||||
environment.put(Context.SECURITY_PRINCIPAL, "cn=Joe Simms,ou=Users,dc=baeldung,dc=com");
|
||||
environment.put(Context.SECURITY_CREDENTIALS, "12345");
|
||||
|
||||
assertThatCode(() -> authenticateUser(environment)).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPreloadedLDAPUserJoe_whenAuthUserWithWrongPW_thenAuthFails() throws Exception {
|
||||
|
||||
Hashtable<String, String> environment = new Hashtable<String, String>();
|
||||
environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
|
||||
environment.put(Context.PROVIDER_URL, "ldap://localhost:10390");
|
||||
environment.put(Context.SECURITY_AUTHENTICATION, "simple");
|
||||
|
||||
environment.put(Context.SECURITY_PRINCIPAL, "cn=Joe Simms,ou=Users,dc=baeldung,dc=com");
|
||||
environment.put(Context.SECURITY_CREDENTIALS, "wronguserpw");
|
||||
|
||||
assertThatExceptionOfType(AuthenticationException.class).isThrownBy(() -> authenticateUser(environment));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPreloadedLDAPUserJoe_whenSearchAndAuthUserWithCorrectPW_thenAuthSucceeds() throws Exception {
|
||||
|
||||
// first authenticate against LDAP as admin to search up DN of user : Joe Simms
|
||||
|
||||
Hashtable<String, String> environment = new Hashtable<String, String>();
|
||||
environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
|
||||
environment.put(Context.PROVIDER_URL, "ldap://localhost:10390");
|
||||
environment.put(Context.SECURITY_AUTHENTICATION, "simple");
|
||||
environment.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
|
||||
environment.put(Context.SECURITY_CREDENTIALS, "secret");
|
||||
|
||||
DirContext adminContext = new InitialDirContext(environment);
|
||||
|
||||
// define the search filter to find the person with CN : Joe Simms
|
||||
String filter = "(&(objectClass=person)(cn=Joe Simms))";
|
||||
|
||||
// declare the attributes we want returned for the object being searched
|
||||
String[] attrIDs = { "cn" };
|
||||
|
||||
// define the search controls
|
||||
SearchControls searchControls = new SearchControls();
|
||||
searchControls.setReturningAttributes(attrIDs);
|
||||
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
|
||||
|
||||
// search for User with filter cn=Joe Simms
|
||||
NamingEnumeration<SearchResult> searchResults = adminContext.search("dc=baeldung,dc=com", filter, searchControls);
|
||||
if (searchResults.hasMore()) {
|
||||
|
||||
SearchResult result = (SearchResult) searchResults.next();
|
||||
Attributes attrs = result.getAttributes();
|
||||
|
||||
String distinguishedName = result.getNameInNamespace();
|
||||
assertThat(distinguishedName).isEqualTo("cn=Joe Simms,ou=Users,dc=baeldung,dc=com");
|
||||
|
||||
String commonName = attrs.get("cn").toString();
|
||||
assertThat(commonName).isEqualTo("cn: Joe Simms");
|
||||
|
||||
// authenticate new context with DN for user Joe Simms, using correct password
|
||||
|
||||
environment.put(Context.SECURITY_PRINCIPAL, distinguishedName);
|
||||
environment.put(Context.SECURITY_CREDENTIALS, "12345");
|
||||
|
||||
assertThatCode(() -> authenticateUser(environment)).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
adminContext.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPreloadedLDAPUserJoe_whenSearchAndAuthUserWithWrongPW_thenAuthFails() throws Exception {
|
||||
|
||||
// first authenticate against LDAP as admin to search up DN of user : Joe Simms
|
||||
|
||||
Hashtable<String, String> environment = new Hashtable<String, String>();
|
||||
environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
|
||||
environment.put(Context.PROVIDER_URL, "ldap://localhost:10390");
|
||||
environment.put(Context.SECURITY_AUTHENTICATION, "simple");
|
||||
environment.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
|
||||
environment.put(Context.SECURITY_CREDENTIALS, "secret");
|
||||
DirContext adminContext = new InitialDirContext(environment);
|
||||
|
||||
// define the search filter to find the person with CN : Joe Simms
|
||||
String filter = "(&(objectClass=person)(cn=Joe Simms))";
|
||||
|
||||
// declare the attributes we want returned for the object being searched
|
||||
String[] attrIDs = { "cn" };
|
||||
|
||||
// define the search controls
|
||||
SearchControls searchControls = new SearchControls();
|
||||
searchControls.setReturningAttributes(attrIDs);
|
||||
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
|
||||
|
||||
// search for User with filter cn=Joe Simms
|
||||
NamingEnumeration<SearchResult> searchResults = adminContext.search("dc=baeldung,dc=com", filter, searchControls);
|
||||
if (searchResults.hasMore()) {
|
||||
|
||||
SearchResult result = (SearchResult) searchResults.next();
|
||||
Attributes attrs = result.getAttributes();
|
||||
|
||||
String distinguishedName = result.getNameInNamespace();
|
||||
assertThat(distinguishedName).isEqualTo("cn=Joe Simms,ou=Users,dc=baeldung,dc=com");
|
||||
|
||||
String commonName = attrs.get("cn").toString();
|
||||
assertThat(commonName).isEqualTo("cn: Joe Simms");
|
||||
|
||||
// authenticate new context with DN for user Joe Simms, using wrong password
|
||||
|
||||
environment.put(Context.SECURITY_PRINCIPAL, distinguishedName);
|
||||
environment.put(Context.SECURITY_CREDENTIALS, "wronguserpassword");
|
||||
|
||||
assertThatExceptionOfType(AuthenticationException.class).isThrownBy(() -> authenticateUser(environment));
|
||||
}
|
||||
|
||||
adminContext.close();
|
||||
}
|
||||
}
|
||||
@@ -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="WARN">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,20 @@
|
||||
version: 1
|
||||
dn: dc=baeldung,dc=com
|
||||
objectClass: domain
|
||||
objectClass: top
|
||||
dc: baeldung
|
||||
|
||||
dn: ou=Users,dc=baeldung,dc=com
|
||||
objectClass: organizationalUnit
|
||||
objectClass: top
|
||||
ou: Users
|
||||
|
||||
dn: cn=Joe Simms,ou=Users,dc=baeldung,dc=com
|
||||
objectClass: inetOrgPerson
|
||||
objectClass: organizationalPerson
|
||||
objectClass: person
|
||||
objectClass: top
|
||||
cn: Joe Simms
|
||||
sn: Simms
|
||||
uid: user1
|
||||
userPassword: 12345
|
||||
@@ -15,11 +15,6 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
|
||||
@@ -16,11 +16,6 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.constructorchaining;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Customer extends Person {
|
||||
private final String loyaltyCardId;
|
||||
|
||||
public Customer(String firstName, String lastName, int age, String loyaltyCardId) {
|
||||
this(firstName, null, lastName, age, loyaltyCardId);
|
||||
}
|
||||
|
||||
public Customer(String firstName, String middleName, String lastName, int age, String loyaltyCardId) {
|
||||
super(firstName, middleName, lastName, age);
|
||||
this.loyaltyCardId = loyaltyCardId;
|
||||
}
|
||||
|
||||
public String getLoyaltyCardId() {
|
||||
return loyaltyCardId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
if (!super.equals(o)) return false;
|
||||
Customer customer = (Customer) o;
|
||||
return Objects.equals(loyaltyCardId, customer.loyaltyCardId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), loyaltyCardId);
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.constructorchaining;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Person {
|
||||
private final String firstName;
|
||||
private final String middleName;
|
||||
private final String lastName;
|
||||
private final int age;
|
||||
|
||||
public Person(String firstName, String lastName, int age) {
|
||||
this(firstName, null, lastName, age);
|
||||
}
|
||||
|
||||
|
||||
public Person(String firstName, String middleName, String lastName, int age) {
|
||||
this.firstName = firstName;
|
||||
this.middleName = middleName;
|
||||
this.lastName = lastName;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public String getMiddleName() {
|
||||
return middleName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Person person = (Person) o;
|
||||
return age == person.age && Objects.equals(firstName, person.firstName) && Objects.equals(middleName, person.middleName) && Objects.equals(lastName, person.lastName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(firstName, middleName, lastName, age);
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.constructorchaining;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class CustomerUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenNameLastNameAndAge_whenUsingDedicatedConstructor_shouldInitializeFieldsAndNullifyMiddleName() {
|
||||
Customer mark = new Customer("Mark", "Johnson", 23, "abcd1234");
|
||||
|
||||
assertEquals(23, mark.getAge());
|
||||
assertEquals("Mark", mark.getFirstName());
|
||||
assertEquals("Johnson", mark.getLastName());
|
||||
assertEquals("abcd1234", mark.getLoyaltyCardId());
|
||||
assertNull(mark.getMiddleName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAllFieldsRequired_whenUsingDedicatedConstructor_shouldInitializeAllFields() {
|
||||
Customer mark = new Customer("Mark", "Andrew", "Johnson", 23, "abcd1234");
|
||||
|
||||
assertEquals(23, mark.getAge());
|
||||
assertEquals("Mark", mark.getFirstName());
|
||||
assertEquals("Andrew", mark.getMiddleName());
|
||||
assertEquals("Johnson", mark.getLastName());
|
||||
assertEquals("abcd1234", mark.getLoyaltyCardId());
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.constructorchaining;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class PersonUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenNameLastNameAndAge_whenUsingDedicatedConstructor_shouldInitializeFieldsAndNullifyMiddleName() {
|
||||
Person mark = new Person("Mark", "Johnson", 23);
|
||||
|
||||
assertEquals(23, mark.getAge());
|
||||
assertEquals("Mark", mark.getFirstName());
|
||||
assertEquals("Johnson", mark.getLastName());
|
||||
assertNull(mark.getMiddleName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAllFieldsRequired_whenUsingDedicatedConstructor_shouldInitializeAllFields() {
|
||||
Person mark = new Person("Mark", "Andrew", "Johnson", 23);
|
||||
|
||||
assertEquals(23, mark.getAge());
|
||||
assertEquals("Mark", mark.getFirstName());
|
||||
assertEquals("Andrew", mark.getMiddleName());
|
||||
assertEquals("Johnson", mark.getLastName());
|
||||
}
|
||||
}
|
||||
@@ -35,11 +35,6 @@
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>javax.mail</artifactId>
|
||||
|
||||
@@ -16,12 +16,6 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
|
||||
@@ -30,11 +30,6 @@
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
|
||||
@@ -26,11 +26,6 @@
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-validator</groupId>
|
||||
<artifactId>commons-validator</artifactId>
|
||||
|
||||
@@ -20,11 +20,6 @@
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest</artifactId>
|
||||
|
||||
@@ -7,3 +7,5 @@
|
||||
- [Validate String as Filename in Java](https://www.baeldung.com/java-validate-filename)
|
||||
- [Count Spaces in a Java String](https://www.baeldung.com/java-string-count-spaces)
|
||||
- [Remove Accents and Diacritics From a String in Java](https://www.baeldung.com/java-remove-accents-from-text)
|
||||
- [Remove Beginning and Ending Double Quotes from a String](https://www.baeldung.com/java-remove-start-end-double-quote)
|
||||
- [Splitting a Java String by Multiple Delimiters](https://www.baeldung.com/java-string-split-multiple-delimiters)
|
||||
|
||||
@@ -52,6 +52,11 @@
|
||||
<artifactId>semver4j</artifactId>
|
||||
<version>${semver4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -80,6 +85,7 @@
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<spring-core.version>5.3.9</spring-core.version>
|
||||
<apache-commons-lang3.version>3.12.0</apache-commons-lang3.version>
|
||||
<guava.version>31.0.1-jre</guava.version>
|
||||
<maven-artifact.version>3.6.3</maven-artifact.version>
|
||||
<gradle-core.version>6.1.1</gradle-core.version>
|
||||
<jackson-core.version>2.11.1</jackson-core.version>
|
||||
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
package com.baeldung.multipledelimiterssplit;
|
||||
|
||||
import com.google.common.base.CharMatcher;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.Iterators;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
public class MultipleDelimitersSplitUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenString_whenSplittingByMultipleDelimitersWithRegEx_thenStringSplit() {
|
||||
String example = "Mary;Thomas:Jane-Kate";
|
||||
String[] names = example.split(";|:|-");
|
||||
String[] expectedNames = new String[]{"Mary", "Thomas", "Jane", "Kate"};
|
||||
Assertions.assertEquals(4, names.length);
|
||||
Assertions.assertArrayEquals(expectedNames, names);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenSplittingByWithCharMatcherAndOnMethod_thenStringSplit() {
|
||||
String example = "Mary;Thomas:Jane-Kate";
|
||||
String[] expectedArray = new String[]{"Mary", "Thomas", "Jane", "Kate"};
|
||||
Iterable<String> expected = Arrays.asList(expectedArray);
|
||||
Iterable<String> names = Splitter.on(CharMatcher.anyOf(";:-")).split(example);
|
||||
Assertions.assertEquals(4, Iterators.size(names.iterator()));
|
||||
Assertions.assertIterableEquals(expected, names);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenSplittingByWithRegexAndOnPatternMethod_thenStringSplit() {
|
||||
String example = "Mary;Thomas:Jane-Kate";
|
||||
String[] expectedArray = new String[]{"Mary", "Thomas", "Jane", "Kate"};
|
||||
Iterable<String> expected = Arrays.asList(expectedArray);
|
||||
Iterable<String> names = Splitter.on(Pattern.compile(";|:|-")).split(example);
|
||||
Assertions.assertEquals(4, Iterators.size(names.iterator()));
|
||||
Assertions.assertIterableEquals(expected, names);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenSplittingByMultipleDelimitersWithGuava_thenStringSplit() {
|
||||
String example = "Mary;Thomas:Jane-Kate";
|
||||
String[] expectedArray = new String[]{"Mary", "Thomas", "Jane", "Kate"};
|
||||
Iterable<String> expected = Arrays.asList(expectedArray);
|
||||
Iterable<String> names = Splitter.onPattern(";|:|-").split(example);
|
||||
Assertions.assertEquals(4, Iterators.size(names.iterator()));
|
||||
Assertions.assertIterableEquals(expected, names);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenSplittingByMultipleDelimitersWithApache_thenStringSplit() {
|
||||
String example = "Mary;Thomas:Jane-Kate";
|
||||
String[] expectedNames = new String[]{"Mary", "Thomas", "Jane", "Kate"};
|
||||
String[] names = StringUtils.split(example, ";:-");
|
||||
Assertions.assertEquals(4, names.length);
|
||||
Assertions.assertArrayEquals(expectedNames, names);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -75,8 +75,8 @@
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<argLine>
|
||||
-javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar
|
||||
@@ -97,8 +97,6 @@
|
||||
<asspectj.version>1.8.9</asspectj.version>
|
||||
<powermock.version>2.0.7</powermock.version>
|
||||
<jmockit.version>1.44</jmockit.version>
|
||||
<!-- plugins -->
|
||||
<maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user