Merge branch 'master' into BAEL-5197/new_features_in_javav17
This commit is contained in:
commit
38ad8766c1
|
@ -37,7 +37,7 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-commons</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<version>${junit-platform.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
|
@ -53,7 +53,6 @@
|
|||
<commons-codec.version>1.11</commons-codec.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<guava.version>28.1-jre</guava.version>
|
||||
<junit.platform.version>1.6.0</junit.platform.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -22,7 +22,7 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-commons</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<version>${junit-platform.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
|
@ -46,7 +46,6 @@
|
|||
<properties>
|
||||
<guava.version>28.1-jre</guava.version>
|
||||
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
||||
<junit.platform.version>1.6.0</junit.platform.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter-api.version}</version>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -47,7 +47,6 @@
|
|||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
||||
<commons-codec.version>1.11</commons-codec.version>
|
||||
<junit-jupiter-api.version>5.3.1</junit-jupiter-api.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -33,7 +33,7 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter-api.version}</version>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -48,7 +48,6 @@
|
|||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
||||
<commons-codec.version>1.11</commons-codec.version>
|
||||
<junit-jupiter-api.version>5.3.1</junit-jupiter-api.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.poi.excel.multilinetext;
|
||||
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
|
||||
public class MultilineText {
|
||||
public void formatMultilineText(Cell cell, int cellNumber) {
|
||||
cell.getRow()
|
||||
.setHeightInPoints(cell.getSheet()
|
||||
.getDefaultRowHeightInPoints() * 2);
|
||||
CellStyle cellStyle = cell.getSheet()
|
||||
.getWorkbook()
|
||||
.createCellStyle();
|
||||
cellStyle.setWrapText(true);
|
||||
cell.setCellStyle(cellStyle);
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
package com.baeldung.poi.excel.multilinetext;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.DataFormatter;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class MultilineTextUnitTest {
|
||||
private static String FILE_NAME = "com/baeldung/poi/excel/multilinetext/MultilineTextTest.xlsx";
|
||||
private static final String NEW_FILE_NAME = "MultilineTextTest_output.xlsx";
|
||||
private static final int STRING_ROW_INDEX = 1;
|
||||
private static final int STRING_CELL_INDEX = 0;
|
||||
|
||||
private String fileLocation;
|
||||
|
||||
@Before
|
||||
public void setup() throws IOException, URISyntaxException {
|
||||
fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME)
|
||||
.toURI())
|
||||
.toString();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultilineTextCell_whenFormated_thenMultilineTextVisible() throws IOException {
|
||||
Workbook workbook = new XSSFWorkbook(fileLocation);
|
||||
Sheet sheet = workbook.getSheetAt(0);
|
||||
sheet.createRow(STRING_ROW_INDEX);
|
||||
Row row = sheet.getRow(STRING_ROW_INDEX);
|
||||
Cell cell = row.createCell(STRING_CELL_INDEX);
|
||||
|
||||
cell.setCellValue("Hello \n world!");
|
||||
MultilineText multilineText = new MultilineText();
|
||||
multilineText.formatMultilineText(cell, STRING_CELL_INDEX);
|
||||
|
||||
FileOutputStream outputStream = new FileOutputStream(NEW_FILE_NAME);
|
||||
workbook.write(outputStream);
|
||||
outputStream.close();
|
||||
|
||||
File file = new File(NEW_FILE_NAME);
|
||||
FileInputStream fileInputStream = new FileInputStream(file);
|
||||
Workbook testWorkbook = new XSSFWorkbook(fileInputStream);
|
||||
assertTrue(row.getHeightInPoints() == testWorkbook.getSheetAt(0)
|
||||
.getRow(STRING_ROW_INDEX)
|
||||
.getHeightInPoints());
|
||||
DataFormatter formatter = new DataFormatter();
|
||||
assertEquals("Hello \n world!", formatter.formatCellValue(testWorkbook.getSheetAt(0)
|
||||
.getRow(STRING_ROW_INDEX)
|
||||
.getCell(STRING_CELL_INDEX)));
|
||||
testWorkbook.close();
|
||||
fileInputStream.close();
|
||||
file.delete();
|
||||
|
||||
workbook.close();
|
||||
}
|
||||
}
|
|
@ -39,10 +39,6 @@
|
|||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<!-- spring-sec -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
|
|
|
@ -72,9 +72,9 @@
|
|||
<version>${derby.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- the JTA API -->
|
||||
|
|
|
@ -62,6 +62,13 @@
|
|||
<groupId>com.googlecode.json-simple</groupId>
|
||||
<artifactId>json-simple</artifactId>
|
||||
<version>${json-simple.version}</version>
|
||||
<exclusions>
|
||||
<!-- junit4 dependency is excluded as it should to be resolved from junit-vintage-engine included in parent-modules. -->
|
||||
<exclusion>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
|
|
@ -12,23 +12,17 @@
|
|||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-lambda-java-core</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<version>${aws-lambda-java-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-lambda-java-events</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<version>${aws-lambda-java-events.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.11.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
<version>${jackson-databind.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
|
@ -43,7 +37,7 @@
|
|||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>42.2.16</version>
|
||||
<version>${postgresql.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@ -71,6 +65,10 @@
|
|||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<hibernate.version>5.4.21.Final</hibernate.version>
|
||||
<aws-lambda-java-core.version>1.2.0</aws-lambda-java-core.version>
|
||||
<aws-lambda-java-events.version>3.1.0</aws-lambda-java-events.version>
|
||||
<jackson-databind.version>2.11.2</jackson-databind.version>
|
||||
<postgresql.version>42.2.16</postgresql.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -12,70 +12,70 @@
|
|||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-lambda-java-core</artifactId>
|
||||
<version>1.2.1</version>
|
||||
<version>${aws-lambda-java-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-lambda-java-events</artifactId>
|
||||
<version>3.6.0</version>
|
||||
<version>${aws-lambda-java-events.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>uk.org.webcompere</groupId>
|
||||
<artifactId>lightweight-config</artifactId>
|
||||
<version>1.1.0</version>
|
||||
<version>${lightweight-config.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-lambda-java-log4j2</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<version>${aws-lambda-java-log4j2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-slf4j-impl</artifactId>
|
||||
<version>2.13.2</version>
|
||||
<version>${log4j-slf4j-impl.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.openfeign</groupId>
|
||||
<artifactId>feign-core</artifactId>
|
||||
<version>11.2</version>
|
||||
<version>${feign-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.openfeign</groupId>
|
||||
<artifactId>feign-slf4j</artifactId>
|
||||
<version>11.2</version>
|
||||
<version>${feign-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.openfeign</groupId>
|
||||
<artifactId>feign-gson</artifactId>
|
||||
<version>11.2</version>
|
||||
<version>${feign-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.inject</groupId>
|
||||
<artifactId>guice</artifactId>
|
||||
<version>5.0.1</version>
|
||||
<version>${guice.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.1</version>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>uk.org.webcompere</groupId>
|
||||
<artifactId>system-stubs-junit4</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<version>${system-stubs-junit4.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<version>${mockito-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>3.19.0</version>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
@ -103,6 +103,17 @@
|
|||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<aws-lambda-java-core.version>1.2.1</aws-lambda-java-core.version>
|
||||
<aws-lambda-java-events.version>3.6.0</aws-lambda-java-events.version>
|
||||
<lightweight-config.version>1.1.0</lightweight-config.version>
|
||||
<aws-lambda-java-log4j2.version>1.2.0</aws-lambda-java-log4j2.version>
|
||||
<log4j-slf4j-impl.version>2.13.2</log4j-slf4j-impl.version>
|
||||
<feign-core.version>11.2</feign-core.version>
|
||||
<guice.version>5.0.1</guice.version>
|
||||
<system-stubs-junit4.version>1.2.0</system-stubs-junit4.version>
|
||||
<mockito-core.version>3.3.0</mockito-core.version>
|
||||
<assertj-core.version>3.19.0</assertj-core.version>
|
||||
<junit-jupiter.version>5.8.1</junit-jupiter.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -70,6 +70,7 @@
|
|||
<artifactId>maven-failsafe-plugin</artifactId>
|
||||
<version>${maven-failsafe-plugin.version}</version>
|
||||
<configuration>
|
||||
<skipITs>true</skipITs>
|
||||
<includes>
|
||||
<include>**/*LiveTest.java</include>
|
||||
</includes>
|
||||
|
|
|
@ -33,7 +33,7 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<version>${junit-platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -95,7 +95,7 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-surefire-provider</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<version>${junit-platform-surefire-provider.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<executions>
|
||||
|
@ -165,7 +165,6 @@
|
|||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<junit.platform.version>1.0.0</junit.platform.version>
|
||||
<hsqldb.version>2.4.0</hsqldb.version>
|
||||
<spock-core.version>1.1-groovy-2.4</spock-core.version>
|
||||
<commons-lang3.version>3.9</commons-lang3.version>
|
||||
|
|
|
@ -34,7 +34,7 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<version>${junit-platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -94,7 +94,7 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-surefire-provider</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<version>${junit-platform-surefire-provider.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<executions>
|
||||
|
@ -162,7 +162,6 @@
|
|||
</repositories>
|
||||
|
||||
<properties>
|
||||
<junit.platform.version>1.0.0</junit.platform.version>
|
||||
<hsqldb.version>2.4.0</hsqldb.version>
|
||||
<spock-core.version>1.1-groovy-2.4</spock-core.version>
|
||||
<groovy-wslite.version>1.1.3</groovy-wslite.version>
|
||||
|
|
|
@ -39,7 +39,7 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<version>${junit-platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -80,7 +80,7 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-surefire-provider</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<version>${junit-platform-surefire-provider.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<executions>
|
||||
|
@ -119,7 +119,6 @@
|
|||
</repositories>
|
||||
|
||||
<properties>
|
||||
<junit.platform.version>1.0.0</junit.platform.version>
|
||||
<groovy.version>2.5.6</groovy.version>
|
||||
<groovy-all.version>2.5.6</groovy-all.version>
|
||||
<groovy-sql.version>2.5.6</groovy-sql.version>
|
||||
|
|
|
@ -39,7 +39,7 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<version>${junit-platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -80,7 +80,7 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-surefire-provider</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<version>${junit-platform-surefire-provider.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<executions>
|
||||
|
@ -109,7 +109,6 @@
|
|||
</repositories>
|
||||
|
||||
<properties>
|
||||
<junit.platform.version>1.0.0</junit.platform.version>
|
||||
<groovy.version>2.5.6</groovy.version>
|
||||
<groovy-all.version>2.5.6</groovy-all.version>
|
||||
<groovy-sql.version>2.5.6</groovy-sql.version>
|
||||
|
|
|
@ -39,7 +39,7 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<version>${junit-platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -80,7 +80,7 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-surefire-provider</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<version>${junit-platform-surefire-provider.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<executions>
|
||||
|
@ -109,7 +109,6 @@
|
|||
</repositories>
|
||||
|
||||
<properties>
|
||||
<junit.platform.version>1.0.0</junit.platform.version>
|
||||
<groovy.version>2.5.6</groovy.version>
|
||||
<groovy-all.version>2.5.6</groovy-all.version>
|
||||
<groovy-sql.version>2.5.6</groovy-sql.version>
|
||||
|
|
|
@ -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>
|
||||
|
@ -106,7 +88,6 @@
|
|||
<maven.compiler.source.version>11</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>11</maven.compiler.target.version>
|
||||
<guava.version>29.0-jre</guava.version>
|
||||
<junit.jupiter.version>5.7.0</junit.jupiter.version>
|
||||
<assertj.version>3.17.2</assertj.version>
|
||||
<mockserver.version>5.11.1</mockserver.version>
|
||||
<commons-lang3.version>3.12.0</commons-lang3.version>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -1 +1,3 @@
|
|||
### Relevant articles:
|
||||
|
||||
- [Pattern Matching for Switch](https://www.baeldung.com/java-switch-pattern-matching)
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.switchpatterns;
|
||||
|
||||
public class GuardedPatterns {
|
||||
|
||||
static double getDoubleValueUsingIf(Object o) {
|
||||
return switch (o) {
|
||||
case String s -> {
|
||||
if (s.length() > 0) {
|
||||
yield Double.parseDouble(s);
|
||||
} else {
|
||||
yield 0d;
|
||||
}
|
||||
}
|
||||
default -> 0d;
|
||||
};
|
||||
}
|
||||
|
||||
static double getDoubleValueUsingGuardedPatterns(Object o) {
|
||||
return switch (o) {
|
||||
case String s && s.length() > 0 -> Double.parseDouble(s);
|
||||
default -> 0d;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.switchpatterns;
|
||||
|
||||
public class HandlingNullValues {
|
||||
|
||||
static double getDoubleUsingSwitchNullCase(Object o) {
|
||||
return switch (o) {
|
||||
case String s -> Double.parseDouble(s);
|
||||
case null -> 0d;
|
||||
default -> 0d;
|
||||
};
|
||||
}
|
||||
|
||||
static double getDoubleUsingSwitchTotalType(Object o) {
|
||||
return switch (o) {
|
||||
case String s -> Double.parseDouble(s);
|
||||
case Object ob -> 0d;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.switchpatterns;
|
||||
|
||||
public class ParenthesizedPatterns {
|
||||
|
||||
static double getDoubleValueUsingIf(Object o) {
|
||||
return switch (o) {
|
||||
case String s -> {
|
||||
if (s.length() > 0) {
|
||||
if (s.contains("#") || s.contains("@")) {
|
||||
yield 0d;
|
||||
} else {
|
||||
yield Double.parseDouble(s);
|
||||
}
|
||||
} else {
|
||||
yield 0d;
|
||||
}
|
||||
}
|
||||
default -> 0d;
|
||||
};
|
||||
}
|
||||
|
||||
static double getDoubleValueUsingParenthesizedPatterns(Object o) {
|
||||
return switch (o) {
|
||||
case String s && s.length() > 0 && !(s.contains("#") || s.contains("@")) -> Double.parseDouble(s);
|
||||
default -> 0d;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.switchpatterns;
|
||||
|
||||
public class PatternMatching {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Object o = args[0];
|
||||
if (o instanceof String s) {
|
||||
System.out.printf("Object is a string %s", s);
|
||||
} else if(o instanceof Number n) {
|
||||
System.out.printf("Object is a number %n", n);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.switchpatterns;
|
||||
|
||||
public class SwitchStatement {
|
||||
|
||||
public static void main(String[] args) {
|
||||
final String b = "B";
|
||||
switch (args[0]) {
|
||||
case "A" -> System.out.println("Parameter is A");
|
||||
case b -> System.out.println("Parameter is b");
|
||||
default -> System.out.println("Parameter is unknown");
|
||||
};
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.switchpatterns;
|
||||
|
||||
public class TypePatterns {
|
||||
|
||||
static double getDoubleUsingIf(Object o) {
|
||||
double result;
|
||||
|
||||
if (o instanceof Integer) {
|
||||
result = ((Integer) o).doubleValue();
|
||||
} else if (o instanceof Float) {
|
||||
result = ((Float) o).doubleValue();
|
||||
} else if (o instanceof String) {
|
||||
result = Double.parseDouble(((String) o));
|
||||
} else {
|
||||
result = 0d;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static double getDoubleUsingSwitch(Object o) {
|
||||
return switch (o) {
|
||||
case Integer i -> i.doubleValue();
|
||||
case Float f -> f.doubleValue();
|
||||
case String s -> Double.parseDouble(s);
|
||||
default -> 0d;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.switchpatterns;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static com.baeldung.switchpatterns.GuardedPatterns.*;
|
||||
|
||||
class GuardedPatternsUnitTest {
|
||||
|
||||
@Test
|
||||
void givenIfImplementation_whenUsingEmptyString_thenDoubleIsReturned() {
|
||||
assertEquals(0d, getDoubleValueUsingIf(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIfImplementation_whenUsingNonEmptyString_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleValueUsingIf("10"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPatternsImplementation_whenUsingEmptyString_thenDoubleIsReturned() {
|
||||
assertEquals(0d, getDoubleValueUsingGuardedPatterns(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPatternsImplementation_whenUsingNonEmptyString_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleValueUsingGuardedPatterns("10"));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.switchpatterns;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static com.baeldung.switchpatterns.HandlingNullValues.*;
|
||||
|
||||
class HandlingNullValuesUnitTest {
|
||||
|
||||
@Test
|
||||
void givenNullCaseInSwitch_whenUsingStringAsArgument_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleUsingSwitchNullCase("10"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTotalTypeInSwitch_whenUsingNullArgument_thenDoubleIsReturned() {
|
||||
assertEquals(0d, getDoubleUsingSwitchNullCase(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTotalTypeInSwitch_whenUsingStringAsArgument_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleUsingSwitchTotalType("10"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNullCaseInSwitch_whenUsingNullArgument_thenDoubleIsReturned() {
|
||||
assertEquals(0d, getDoubleUsingSwitchTotalType(null));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.switchpatterns;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static com.baeldung.switchpatterns.ParenthesizedPatterns.*;
|
||||
|
||||
class ParenthesizedPatternsUnitTest {
|
||||
|
||||
@Test
|
||||
void givenIfImplementation_whenUsingEmptyString_thenDoubleIsReturned() {
|
||||
assertEquals(0d, getDoubleValueUsingIf(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIfImplementation_whenUsingNonEmptyString_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleValueUsingIf("10"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIfImplementation_whenStringContainsSpecialChar_thenDoubleIsReturned() {
|
||||
assertEquals(0d, getDoubleValueUsingIf("@10"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPatternsImplementation_whenUsingEmptyString_thenDoubleIsReturned() {
|
||||
assertEquals(0d, getDoubleValueUsingParenthesizedPatterns(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPatternsImplementation_whenUsingNonEmptyString_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleValueUsingParenthesizedPatterns("10"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPatternsImplementation_whenStringContainsSpecialChar_thenDoubleIsReturned() {
|
||||
assertEquals(0d, getDoubleValueUsingParenthesizedPatterns("@10"));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.baeldung.switchpatterns;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static com.baeldung.switchpatterns.TypePatterns.*;
|
||||
|
||||
class TypePatternsUnitTest {
|
||||
|
||||
@Test
|
||||
void givenIfImplementation_whenUsingIntegerAsArgument_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleUsingIf(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIfImplementation_whenUsingDoubleAsArgument_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleUsingIf(10.0f));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIfImplementation_whenUsingStringAsArgument_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleUsingIf("10"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIfImplementation_whenUsingCharAsArgument_thenDoubleIsReturned() {
|
||||
assertEquals(0d, getDoubleUsingIf('c'));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSwitchImplementation_whenUsingIntegerAsArgument_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleUsingSwitch(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSwitchImplementation_whenUsingDoubleAsArgument_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleUsingSwitch(10.0f));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSwitchImplementation_whenUsingStringAsArgument_thenDoubleIsReturned() {
|
||||
assertEquals(10d, getDoubleUsingSwitch("10"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSwitchImplementation_whenUsingCharAsArgument_thenDoubleIsReturned() {
|
||||
assertEquals(0d, getDoubleUsingSwitch('c'));
|
||||
}
|
||||
|
||||
}
|
|
@ -40,7 +40,7 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<version>${junit-platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
@ -70,7 +70,6 @@
|
|||
<properties>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
<junit.platform.version>1.2.0</junit.platform.version>
|
||||
<awaitility.version>1.7.0</awaitility.version>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
|
|
|
@ -29,7 +29,7 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<version>${junit-platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -158,7 +158,6 @@
|
|||
<rxjava.version>3.0.0</rxjava.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
<junit.platform.version>1.2.0</junit.platform.version>
|
||||
<awaitility.version>4.0.2</awaitility.version>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<version>${junit-platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -80,7 +80,6 @@
|
|||
<properties>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
<junit.platform.version>1.2.0</junit.platform.version>
|
||||
<awaitility.version>1.7.0</awaitility.version>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
|
|
|
@ -5,3 +5,4 @@ This module contains articles about arrays conversion in Java
|
|||
## Relevant Articles
|
||||
- [Convert a Float to a Byte Array in Java](https://www.baeldung.com/java-convert-float-to-byte-array)
|
||||
- [Converting Between Stream and Array in Java](https://www.baeldung.com/java-stream-to-array)
|
||||
- [Convert a Byte Array to a Numeric Representation in Java](https://www.baeldung.com/java-byte-array-to-number)
|
||||
|
|
|
@ -43,7 +43,7 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<version>${junit-platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
@ -52,7 +52,6 @@
|
|||
<eclipse.collections.version>7.1.0</eclipse.collections.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<assertj.version>3.11.1</assertj.version>
|
||||
<junit.platform.version>1.2.0</junit.platform.version>
|
||||
<commons-exec.version>1.3</commons-exec.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -15,12 +15,6 @@
|
|||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.thread-weaver</groupId>
|
||||
<artifactId>threadweaver</artifactId>
|
||||
|
@ -32,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>
|
||||
|
@ -96,7 +96,6 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<junit.version>4.13</junit.version>
|
||||
<threadweaver.version>0.2</threadweaver.version>
|
||||
<tempus-fugit.version>1.1</tempus-fugit.version>
|
||||
<multithreadedtc.version>1.01</multithreadedtc.version>
|
||||
|
|
|
@ -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>${jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${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,7 +71,7 @@
|
|||
<properties>
|
||||
<spring.version>5.0.9.RELEASE</spring.version>
|
||||
<h2.version>1.4.199</h2.version>
|
||||
<jupiter.version>5.5.1</jupiter.version>
|
||||
<apacheds.version>2.0.0.AM26</apacheds.version>
|
||||
<source.version>1.8</source.version>
|
||||
<target.version>1.8</target.version>
|
||||
</properties>
|
||||
|
|
|
@ -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,12 +15,6 @@
|
|||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
|
|
|
@ -16,12 +16,6 @@
|
|||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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,12 +35,6 @@
|
|||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.11</version>
|
||||
<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-engine.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
|
@ -94,7 +88,6 @@
|
|||
<guava.version>25.1-jre</guava.version>
|
||||
<unix4j.version>0.4</unix4j.version>
|
||||
<grep4j.version>1.8.7</grep4j.version>
|
||||
<junit-jupiter-engine.version>5.7.2</junit-jupiter-engine.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,5 +1,6 @@
|
|||
package com.baeldung.example.soundapi;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
|
||||
|
@ -37,6 +38,7 @@ public class AppUnitTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void Given_TargetLineDataObject_When_Run_Then_GeneratesOutputStream() {
|
||||
|
||||
soundRecorder.setFormat(af);
|
||||
|
@ -52,6 +54,7 @@ public class AppUnitTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void Given_AudioInputStream_When_NotNull_Then_SaveToWavFile() {
|
||||
soundRecorder.setFormat(af);
|
||||
soundRecorder.build(af);
|
||||
|
|
|
@ -30,13 +30,6 @@
|
|||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
<type>jar</type>
|
||||
</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>
|
||||
|
|
|
@ -10,3 +10,4 @@ This module contains articles about string APIs.
|
|||
- [CharSequence vs. String in Java](https://www.baeldung.com/java-char-sequence-string)
|
||||
- [StringBuilder vs StringBuffer in Java](https://www.baeldung.com/java-string-builder-string-buffer)
|
||||
- [Generate a Secure Random Password in Java](https://www.baeldung.com/java-generate-secure-password)
|
||||
- [Getting a Character by Index From a String in Java](https://www.baeldung.com/java-character-at-position)
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.stringapi;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class StringCharAtUnitTest {
|
||||
@Test
|
||||
public void whenCallCharAt_thenSuccess() {
|
||||
String sample = "abcdefg";
|
||||
assertEquals('d', sample.charAt(3));
|
||||
}
|
||||
|
||||
@Test()
|
||||
public void whenCharAtNonExist_thenIndexOutOfBoundsExceptionThrown() {
|
||||
String sample = "abcdefg";
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> sample.charAt(-1));
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> sample.charAt(sample.length()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallCharAt_thenReturnString() {
|
||||
String sample = "abcdefg";
|
||||
assertEquals("a", Character.toString(sample.charAt(0)));
|
||||
assertEquals("a", String.valueOf(sample.charAt(0)));
|
||||
}
|
||||
|
||||
}
|
|
@ -20,12 +20,6 @@
|
|||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest</artifactId>
|
||||
|
|
|
@ -6,3 +6,6 @@
|
|||
- [Split a String in Java and Keep the Delimiters](https://www.baeldung.com/java-split-string-keep-delimiters)
|
||||
- [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>
|
||||
|
|
|
@ -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>
|
|
@ -25,12 +25,6 @@
|
|||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
|
|
|
@ -134,9 +134,4 @@
|
|||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<properties>
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -81,7 +81,6 @@
|
|||
|
||||
<appmodules.version>1.0</appmodules.version>
|
||||
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<assertj-core.version>3.12.2</assertj-core.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -4,3 +4,4 @@
|
|||
- [Reusing Docker Layers with Spring Boot](https://www.baeldung.com/docker-layers-spring-boot)
|
||||
- [Running Spring Boot with PostgreSQL in Docker Compose](https://www.baeldung.com/spring-boot-postgresql-docker)
|
||||
- [How To Configure Java Heap Size Inside a Docker Container](https://www.baeldung.com/ops/docker-jvm-heap-size)
|
||||
- [Dockerfile Strategies for Git](https://www.baeldung.com/ops/dockerfile-git-strategies)
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
FROM openjdk:11
|
||||
COPY target/docker-sample-app-0.0.1.jar app.jar
|
||||
ENTRYPOINT ["java","-jar","/app.jar"]
|
|
@ -0,0 +1,3 @@
|
|||
### Relevant Articles:
|
||||
|
||||
- How to Get Docker-Compose to Always Use the Latest Image
|
|
@ -0,0 +1,8 @@
|
|||
version: '2.4'
|
||||
services:
|
||||
db:
|
||||
image: postgres
|
||||
my_app:
|
||||
build: .
|
||||
ports:
|
||||
- "8080:8080"
|
|
@ -0,0 +1,9 @@
|
|||
version: '2.4'
|
||||
services:
|
||||
db:
|
||||
image: postgres
|
||||
my_app:
|
||||
image: "eugen/test-app:latest"
|
||||
ports:
|
||||
- "8080:8080"
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.baeldung.docker</groupId>
|
||||
<artifactId>docker</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>docker-sample-app</artifactId>
|
||||
<name>docker-sample-app</name>
|
||||
<description>Demo project for Spring Boot and Docker</description>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.docker.app;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DockAppApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DockAppApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.docker.app.endpoint;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class MyController {
|
||||
|
||||
@GetMapping
|
||||
public String version() {
|
||||
return "1.7";
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.docker.app;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class DockAppApplicationUnitTest {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
|
@ -25,6 +25,7 @@
|
|||
<modules>
|
||||
<module>docker-internal-dto</module>
|
||||
<module>docker-spring-boot</module>
|
||||
<module>docker-sample-app</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?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">
|
||||
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>
|
||||
<groupId>com.baeldung.ethereum</groupId>
|
||||
<artifactId>ethereum</artifactId>
|
||||
|
@ -112,6 +112,13 @@
|
|||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<version>${spring.boot.version}</version>
|
||||
<exclusions>
|
||||
<!-- junit4 dependency is excluded as it should to be resolved from junit-vintage-engine included in parent-modules. -->
|
||||
<exclusion>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
|
@ -137,18 +144,6 @@
|
|||
</exclusions>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-core</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest</artifactId>
|
||||
|
|
|
@ -45,9 +45,9 @@
|
|||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
|
|
@ -38,9 +38,9 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
|
|
@ -27,18 +27,6 @@
|
|||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
|
@ -70,7 +58,6 @@
|
|||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<java-hamcrest.version>2.0.0.0</java-hamcrest.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -15,21 +15,6 @@
|
|||
<relativePath>../</relativePath>
|
||||
</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.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>guava-collections-map</finalName>
|
||||
<resources>
|
||||
|
@ -40,7 +25,4 @@
|
|||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -16,18 +16,6 @@
|
|||
|
||||
<dependencies>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
|
@ -43,7 +31,6 @@
|
|||
<properties>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -32,18 +32,6 @@
|
|||
<version>${jool.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
|
@ -76,7 +64,6 @@
|
|||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<java-hamcrest.version>2.0.0.0</java-hamcrest.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -14,21 +14,6 @@
|
|||
<relativePath>../</relativePath>
|
||||
</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.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>guava-io</finalName>
|
||||
<resources>
|
||||
|
@ -39,8 +24,4 @@
|
|||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -21,18 +21,6 @@
|
|||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
|
@ -53,7 +41,6 @@
|
|||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -34,23 +34,9 @@
|
|||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<guava.version>29.0-jre</guava.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<version>${junit-platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -160,7 +160,7 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-surefire-provider</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<version>${junit-platform-surefire-provider.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<executions>
|
||||
|
@ -191,8 +191,6 @@
|
|||
<kotlinx.version>0.22.5</kotlinx.version>
|
||||
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<junit.platform.version>1.0.0</junit.platform.version>
|
||||
<junit.vintage.version>5.2.0</junit.vintage.version>
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
|
||||
<spek.api.version>1.1.5</spek.api.version>
|
||||
|
|
|
@ -18,12 +18,12 @@
|
|||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-params</artifactId>
|
||||
<version>${junit.jupiter.version}</version>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-vintage.version}</version>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
|
@ -59,9 +59,6 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<junit.jupiter.version>5.0.0-M4</junit.jupiter.version>
|
||||
<junit-vintage.version>4.12.0-M4</junit-vintage.version>
|
||||
<log4j-core.version>2.8.2</log4j-core.version>
|
||||
<junit-platform-surefire-provider.version>1.0.0-M4</junit-platform-surefire-provider.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -1,9 +1,9 @@
|
|||
package com.baeldung.jackson.advancedannotations;
|
||||
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||
|
||||
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
|
||||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
|
||||
public class NamingBean {
|
||||
private int id;
|
||||
private String beanName;
|
||||
|
|
|
@ -10,4 +10,5 @@ This module contains articles about Jackson conversions.
|
|||
- [How to Process YAML with Jackson](https://www.baeldung.com/jackson-yaml)
|
||||
- [Jackson Streaming API](https://www.baeldung.com/jackson-streaming-api)
|
||||
- [Jackson: java.util.LinkedHashMap cannot be cast to X](https://www.baeldung.com/jackson-linkedhashmap-cannot-be-cast)
|
||||
- [Deserialize Snake Case to Camel Case With Jackson](https://www.baeldung.com/jackson-deserialize-snake-to-camel-case)
|
||||
- More articles: [[<-- prev]](../jackson-conversions)
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.jackson.snakecase;
|
||||
|
||||
public class User {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.jackson.snakecase;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class UserWithPropertyNames {
|
||||
@JsonProperty("first_name")
|
||||
private String firstName;
|
||||
@JsonProperty("last_name")
|
||||
private String lastName;
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.jackson.snakecase;
|
||||
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||
|
||||
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
|
||||
public class UserWithSnakeStrategy {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.jackson.snakecase;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
||||
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class SnakeCaseUnitTest {
|
||||
|
||||
private static final String JSON = "{\"first_name\": \"Jackie\", \"last_name\": \"Chan\"}";
|
||||
|
||||
@Test(expected = UnrecognizedPropertyException.class)
|
||||
public void whenExceptionThrown_thenExpectationSatisfied() throws Exception {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.readValue(JSON, User.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSnakeCaseJson_whenParseWithJsonPropertyAnnotation_thenGetExpectedObject() throws Exception {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
UserWithPropertyNames user = objectMapper.readValue(JSON, UserWithPropertyNames.class);
|
||||
assertEquals("Jackie", user.getFirstName());
|
||||
assertEquals("Chan", user.getLastName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSnakeCaseJson_whenParseWithJsonNamingAnnotation_thenGetExpectedObject() throws Exception {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
UserWithSnakeStrategy user = objectMapper.readValue(JSON, UserWithSnakeStrategy.class);
|
||||
assertEquals("Jackie", user.getFirstName());
|
||||
assertEquals("Chan", user.getLastName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSnakeCaseJson_whenParseWithCustomMapper_thenGetExpectedObject() throws Exception {
|
||||
ObjectMapper objectMapper = new ObjectMapper().setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
|
||||
User user = objectMapper.readValue(JSON, User.class);
|
||||
assertEquals("Jackie", user.getFirstName());
|
||||
assertEquals("Chan", user.getLastName());
|
||||
}
|
||||
|
||||
}
|
|
@ -35,22 +35,6 @@
|
|||
<artifactId>jackson-dataformat-xml</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -22,18 +22,6 @@
|
|||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
|
@ -54,7 +42,6 @@
|
|||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<assertj.version>3.11.0</assertj.version>
|
||||
</properties>
|
||||
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue