From 4e97fb0f9dc5c3573ae30a5fce7f3c9c7bc42f2a Mon Sep 17 00:00:00 2001 From: Gang Date: Wed, 1 Jan 2020 21:34:48 -0700 Subject: [PATCH 01/10] BAEL-3656 Read Numeric Strings in Excel Cells as a String with Apache POI --- apache-poi/pom.xml | 2 +- .../poi/excel/ExcelCellFormatter.java | 20 +++ .../ExcelCellFormatterIntegrationTest.java | 128 ++++++++++++++++++ 3 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 apache-poi/src/main/java/com/baeldung/poi/excel/ExcelCellFormatter.java create mode 100644 apache-poi/src/test/java/com/baeldung/poi/excel/ExcelCellFormatterIntegrationTest.java diff --git a/apache-poi/pom.xml b/apache-poi/pom.xml index a114946c47..333339ed33 100644 --- a/apache-poi/pom.xml +++ b/apache-poi/pom.xml @@ -32,7 +32,7 @@ - 3.15 + 4.1.1 1.0.6 diff --git a/apache-poi/src/main/java/com/baeldung/poi/excel/ExcelCellFormatter.java b/apache-poi/src/main/java/com/baeldung/poi/excel/ExcelCellFormatter.java new file mode 100644 index 0000000000..4a8854620c --- /dev/null +++ b/apache-poi/src/main/java/com/baeldung/poi/excel/ExcelCellFormatter.java @@ -0,0 +1,20 @@ +package com.baeldung.poi.excel; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.DataFormatter; +import org.apache.poi.ss.usermodel.FormulaEvaluator; +import org.apache.poi.ss.usermodel.Workbook; + +public class ExcelCellFormatter { + + public String getCellStringValue(Cell cell) { + DataFormatter formatter = new DataFormatter(); + return formatter.formatCellValue(cell); + } + + public String getCellStringValueWithFormula(Cell cell, Workbook workbook) { + DataFormatter formatter = new DataFormatter(); + FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator(); + return formatter.formatCellValue(cell, evaluator); + } +} diff --git a/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelCellFormatterIntegrationTest.java b/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelCellFormatterIntegrationTest.java new file mode 100644 index 0000000000..889ee1b527 --- /dev/null +++ b/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelCellFormatterIntegrationTest.java @@ -0,0 +1,128 @@ +package com.baeldung.poi.excel; + +import static org.junit.Assert.assertEquals; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.DataFormat; +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.After; +import org.junit.Before; +import org.junit.Test; + +public class ExcelCellFormatterIntegrationTest { + private static int STRING_CELL_INDEX = 0; + private static int BOOLEAN_CELL_INDEX = 1; + private static int RAW_NUMERIC_CELL_INDEX = 2; + private static int FORMATTED_NUMERIC_CELL_INDEX = 3; + private static int FORMULA_CELL_INDEX = 4; + + private String fileLocation; + + @Before + public void generateExcelFile() throws IOException { + + Workbook workbook = new XSSFWorkbook(); + + Sheet sheet = workbook.createSheet("Test"); + Row row = sheet.createRow(0); + Cell cell = row.createCell(STRING_CELL_INDEX); + cell.setCellValue("String Test"); // STRING cell + + cell = row.createCell(BOOLEAN_CELL_INDEX); + cell.setCellValue(true); // BOOLEAN cell + + cell = row.createCell(RAW_NUMERIC_CELL_INDEX); + cell.setCellValue(1234.5678); // NUMERIC cell + + cell = row.createCell(FORMATTED_NUMERIC_CELL_INDEX); + cell.setCellValue(1234.5678); + CellStyle curStyle = workbook.createCellStyle(); + DataFormat df = workbook.createDataFormat(); + curStyle.setDataFormat(df.getFormat("$#,##0.00")); + cell.setCellStyle(curStyle); // NUMERIC cell with format rule + + cell = row.createCell(FORMULA_CELL_INDEX); + cell.setCellFormula("SUM(C1:D1)"); // FORMULA cell + + File tempFile = File.createTempFile("ExcelCellFormatterIntegrationTest", ".xlsx"); + + fileLocation = tempFile.getAbsolutePath(); + + FileOutputStream outputStream = new FileOutputStream(fileLocation); + workbook.write(outputStream); + workbook.close(); + outputStream.close(); + } + + @Test + public void gvieStringCell_whenGetCellStringValue_thenReturnStringValue() throws IOException { + Workbook workbook = new XSSFWorkbook(fileLocation); + Sheet sheet = workbook.getSheetAt(0); + Row row = sheet.getRow(0); + + ExcelCellFormatter formatter = new ExcelCellFormatter(); + assertEquals("String Test", formatter.getCellStringValue(row.getCell(STRING_CELL_INDEX))); + workbook.close(); + } + + @Test + public void gvieBooleanCell_whenGetCellStringValue_thenReturnBooleanStringValue() throws IOException { + Workbook workbook = new XSSFWorkbook(fileLocation); + Sheet sheet = workbook.getSheetAt(0); + Row row = sheet.getRow(0); + + ExcelCellFormatter formatter = new ExcelCellFormatter(); + assertEquals("TRUE", formatter.getCellStringValue(row.getCell(BOOLEAN_CELL_INDEX))); + workbook.close(); + } + + @Test + public void gvieNumericCell_whenGetCellStringValue_thenReturnNumericStringValue() throws IOException { + Workbook workbook = new XSSFWorkbook(fileLocation); + Sheet sheet = workbook.getSheetAt(0); + Row row = sheet.getRow(0); + + ExcelCellFormatter formatter = new ExcelCellFormatter(); + assertEquals("1234.5678", formatter.getCellStringValue(row.getCell(RAW_NUMERIC_CELL_INDEX))); + assertEquals("$1,234.57", formatter.getCellStringValue(row.getCell(FORMATTED_NUMERIC_CELL_INDEX))); + workbook.close(); + } + + @Test + public void gvieFormualCell_whenGetCellStringValue_thenReturnOriginalFormulaString() throws IOException { + Workbook workbook = new XSSFWorkbook(fileLocation); + Sheet sheet = workbook.getSheetAt(0); + Row row = sheet.getRow(0); + + ExcelCellFormatter formatter = new ExcelCellFormatter(); + assertEquals("SUM(C1:D1)", formatter.getCellStringValue(row.getCell(FORMULA_CELL_INDEX))); + workbook.close(); + } + + @Test + public void gvieFormualCell_whenGetCellStringValueForFormula_thenReturnOriginalFormulatring() throws IOException { + Workbook workbook = new XSSFWorkbook(fileLocation); + Sheet sheet = workbook.getSheetAt(0); + Row row = sheet.getRow(0); + + ExcelCellFormatter formatter = new ExcelCellFormatter(); + assertEquals("2469.1356", formatter.getCellStringValueWithFormula(row.getCell(FORMULA_CELL_INDEX), workbook)); + workbook.close(); + } + + @After + public void cleanup() { + File testFile = new File(fileLocation); + if (testFile.exists()) { + testFile.deleteOnExit(); + } + } +} \ No newline at end of file From af6804ae25fc35ec53e1c20b245e21094836b94f Mon Sep 17 00:00:00 2001 From: Gang Date: Sun, 5 Jan 2020 00:19:49 -0700 Subject: [PATCH 02/10] BAEL-3656 Change the integration test into unit test. --- .../resources/ExcelCellFormatterTest.xlsx | Bin 0 -> 3375 bytes .../ExcelCellFormatterIntegrationTest.java | 128 ------------------ .../poi/excel/ExcelCellFormatterUnitTest.java | 87 ++++++++++++ 3 files changed, 87 insertions(+), 128 deletions(-) create mode 100644 apache-poi/src/main/resources/ExcelCellFormatterTest.xlsx delete mode 100644 apache-poi/src/test/java/com/baeldung/poi/excel/ExcelCellFormatterIntegrationTest.java create mode 100644 apache-poi/src/test/java/com/baeldung/poi/excel/ExcelCellFormatterUnitTest.java diff --git a/apache-poi/src/main/resources/ExcelCellFormatterTest.xlsx b/apache-poi/src/main/resources/ExcelCellFormatterTest.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..54e8734d58f7e14ba90c1bd65417142c460db369 GIT binary patch literal 3375 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 literal 0 HcmV?d00001 diff --git a/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelCellFormatterIntegrationTest.java b/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelCellFormatterIntegrationTest.java deleted file mode 100644 index 889ee1b527..0000000000 --- a/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelCellFormatterIntegrationTest.java +++ /dev/null @@ -1,128 +0,0 @@ -package com.baeldung.poi.excel; - -import static org.junit.Assert.assertEquals; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; - -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.CellStyle; -import org.apache.poi.ss.usermodel.DataFormat; -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.After; -import org.junit.Before; -import org.junit.Test; - -public class ExcelCellFormatterIntegrationTest { - private static int STRING_CELL_INDEX = 0; - private static int BOOLEAN_CELL_INDEX = 1; - private static int RAW_NUMERIC_CELL_INDEX = 2; - private static int FORMATTED_NUMERIC_CELL_INDEX = 3; - private static int FORMULA_CELL_INDEX = 4; - - private String fileLocation; - - @Before - public void generateExcelFile() throws IOException { - - Workbook workbook = new XSSFWorkbook(); - - Sheet sheet = workbook.createSheet("Test"); - Row row = sheet.createRow(0); - Cell cell = row.createCell(STRING_CELL_INDEX); - cell.setCellValue("String Test"); // STRING cell - - cell = row.createCell(BOOLEAN_CELL_INDEX); - cell.setCellValue(true); // BOOLEAN cell - - cell = row.createCell(RAW_NUMERIC_CELL_INDEX); - cell.setCellValue(1234.5678); // NUMERIC cell - - cell = row.createCell(FORMATTED_NUMERIC_CELL_INDEX); - cell.setCellValue(1234.5678); - CellStyle curStyle = workbook.createCellStyle(); - DataFormat df = workbook.createDataFormat(); - curStyle.setDataFormat(df.getFormat("$#,##0.00")); - cell.setCellStyle(curStyle); // NUMERIC cell with format rule - - cell = row.createCell(FORMULA_CELL_INDEX); - cell.setCellFormula("SUM(C1:D1)"); // FORMULA cell - - File tempFile = File.createTempFile("ExcelCellFormatterIntegrationTest", ".xlsx"); - - fileLocation = tempFile.getAbsolutePath(); - - FileOutputStream outputStream = new FileOutputStream(fileLocation); - workbook.write(outputStream); - workbook.close(); - outputStream.close(); - } - - @Test - public void gvieStringCell_whenGetCellStringValue_thenReturnStringValue() throws IOException { - Workbook workbook = new XSSFWorkbook(fileLocation); - Sheet sheet = workbook.getSheetAt(0); - Row row = sheet.getRow(0); - - ExcelCellFormatter formatter = new ExcelCellFormatter(); - assertEquals("String Test", formatter.getCellStringValue(row.getCell(STRING_CELL_INDEX))); - workbook.close(); - } - - @Test - public void gvieBooleanCell_whenGetCellStringValue_thenReturnBooleanStringValue() throws IOException { - Workbook workbook = new XSSFWorkbook(fileLocation); - Sheet sheet = workbook.getSheetAt(0); - Row row = sheet.getRow(0); - - ExcelCellFormatter formatter = new ExcelCellFormatter(); - assertEquals("TRUE", formatter.getCellStringValue(row.getCell(BOOLEAN_CELL_INDEX))); - workbook.close(); - } - - @Test - public void gvieNumericCell_whenGetCellStringValue_thenReturnNumericStringValue() throws IOException { - Workbook workbook = new XSSFWorkbook(fileLocation); - Sheet sheet = workbook.getSheetAt(0); - Row row = sheet.getRow(0); - - ExcelCellFormatter formatter = new ExcelCellFormatter(); - assertEquals("1234.5678", formatter.getCellStringValue(row.getCell(RAW_NUMERIC_CELL_INDEX))); - assertEquals("$1,234.57", formatter.getCellStringValue(row.getCell(FORMATTED_NUMERIC_CELL_INDEX))); - workbook.close(); - } - - @Test - public void gvieFormualCell_whenGetCellStringValue_thenReturnOriginalFormulaString() throws IOException { - Workbook workbook = new XSSFWorkbook(fileLocation); - Sheet sheet = workbook.getSheetAt(0); - Row row = sheet.getRow(0); - - ExcelCellFormatter formatter = new ExcelCellFormatter(); - assertEquals("SUM(C1:D1)", formatter.getCellStringValue(row.getCell(FORMULA_CELL_INDEX))); - workbook.close(); - } - - @Test - public void gvieFormualCell_whenGetCellStringValueForFormula_thenReturnOriginalFormulatring() throws IOException { - Workbook workbook = new XSSFWorkbook(fileLocation); - Sheet sheet = workbook.getSheetAt(0); - Row row = sheet.getRow(0); - - ExcelCellFormatter formatter = new ExcelCellFormatter(); - assertEquals("2469.1356", formatter.getCellStringValueWithFormula(row.getCell(FORMULA_CELL_INDEX), workbook)); - workbook.close(); - } - - @After - public void cleanup() { - File testFile = new File(fileLocation); - if (testFile.exists()) { - testFile.deleteOnExit(); - } - } -} \ No newline at end of file diff --git a/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelCellFormatterUnitTest.java b/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelCellFormatterUnitTest.java new file mode 100644 index 0000000000..adedf951d6 --- /dev/null +++ b/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelCellFormatterUnitTest.java @@ -0,0 +1,87 @@ +package com.baeldung.poi.excel; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Paths; + +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 ExcelCellFormatterUnitTest { + private static final String FILE_NAME = "ExcelCellFormatterTest.xlsx"; + private static final int STRING_CELL_INDEX = 0; + private static final int BOOLEAN_CELL_INDEX = 1; + private static final int RAW_NUMERIC_CELL_INDEX = 2; + private static final int FORMATTED_NUMERIC_CELL_INDEX = 3; + private static final int FORMULA_CELL_INDEX = 4; + + private String fileLocation; + + @Before + public void setup() throws IOException, URISyntaxException { + fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME).toURI()).toString(); + } + + @Test + public void gvieStringCell_whenGetCellStringValue_thenReturnStringValue() throws IOException { + Workbook workbook = new XSSFWorkbook(fileLocation); + Sheet sheet = workbook.getSheetAt(0); + Row row = sheet.getRow(0); + + ExcelCellFormatter formatter = new ExcelCellFormatter(); + assertEquals("String Test", formatter.getCellStringValue(row.getCell(STRING_CELL_INDEX))); + workbook.close(); + } + + @Test + public void gvieBooleanCell_whenGetCellStringValue_thenReturnBooleanStringValue() throws IOException { + Workbook workbook = new XSSFWorkbook(fileLocation); + Sheet sheet = workbook.getSheetAt(0); + Row row = sheet.getRow(0); + + ExcelCellFormatter formatter = new ExcelCellFormatter(); + assertEquals("TRUE", formatter.getCellStringValue(row.getCell(BOOLEAN_CELL_INDEX))); + workbook.close(); + } + + @Test + public void gvieNumericCell_whenGetCellStringValue_thenReturnNumericStringValue() throws IOException { + Workbook workbook = new XSSFWorkbook(fileLocation); + Sheet sheet = workbook.getSheetAt(0); + Row row = sheet.getRow(0); + + ExcelCellFormatter formatter = new ExcelCellFormatter(); + assertEquals("1.234", formatter.getCellStringValue(row.getCell(RAW_NUMERIC_CELL_INDEX))); + assertEquals("1.23", formatter.getCellStringValue(row.getCell(FORMATTED_NUMERIC_CELL_INDEX))); + workbook.close(); + } + + @Test + public void gvieFormualCell_whenGetCellStringValue_thenReturnOriginalFormulaString() throws IOException { + Workbook workbook = new XSSFWorkbook(fileLocation); + Sheet sheet = workbook.getSheetAt(0); + Row row = sheet.getRow(0); + + ExcelCellFormatter formatter = new ExcelCellFormatter(); + assertEquals("SUM(1+2)", formatter.getCellStringValue(row.getCell(FORMULA_CELL_INDEX))); + workbook.close(); + } + + @Test + public void gvieFormualCell_whenGetCellStringValueForFormula_thenReturnOriginalFormulatring() throws IOException { + Workbook workbook = new XSSFWorkbook(fileLocation); + Sheet sheet = workbook.getSheetAt(0); + Row row = sheet.getRow(0); + + ExcelCellFormatter formatter = new ExcelCellFormatter(); + assertEquals("3", formatter.getCellStringValueWithFormula(row.getCell(FORMULA_CELL_INDEX), workbook)); + workbook.close(); + } + +} \ No newline at end of file From 00f74c251e8827d12d2a94087d1e4bc260e3bede Mon Sep 17 00:00:00 2001 From: "m.raheem" Date: Tue, 7 Jan 2020 21:28:46 +0200 Subject: [PATCH 03/10] modifying partialMocking() --- .../LoginControllerIntegrationTest.java | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/testing-modules/mocks/src/test/java/com/baeldung/jmockit/LoginControllerIntegrationTest.java b/testing-modules/mocks/src/test/java/com/baeldung/jmockit/LoginControllerIntegrationTest.java index 4dbe94991f..cb490926f8 100644 --- a/testing-modules/mocks/src/test/java/com/baeldung/jmockit/LoginControllerIntegrationTest.java +++ b/testing-modules/mocks/src/test/java/com/baeldung/jmockit/LoginControllerIntegrationTest.java @@ -130,30 +130,36 @@ public class LoginControllerIntegrationTest { }; } + @Test public void partialMocking() { - // use partial mock - final LoginService partialLoginService = new LoginService(); + + LoginService partialLoginService = new LoginService(); partialLoginService.setLoginDao(loginDao); loginController.loginService = partialLoginService; - final UserForm userForm = new UserForm(); + UserForm userForm = new UserForm(); userForm.username = "foo"; - // let service's login use implementation so let's mock DAO call - new Expectations() {{ - loginDao.login(userForm); - result = 1; - // no expectation for loginService.login + + new Expectations(partialLoginService) {{ + //let's mock loginDao#login() call + loginDao.login(userForm); result = 1; + + //no expectation for partialLoginService#login() so that real implementation is used + + //mocking partialLoginService#setCurrentUser() partialLoginService.setCurrentUser("foo"); }}; String login = loginController.login(userForm); Assert.assertEquals("OK", login); - // verify mocked call - new FullVerifications(partialLoginService) { - }; - new FullVerifications(loginDao) { + // verify that mocked partialLoginService#setCurrentUser("foo") is called + new Verifications() { + { + partialLoginService.setCurrentUser("foo"); + } }; + } } From 1b89f003e6cb55337d96088ae7949ec734125c76 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Wed, 8 Jan 2020 21:01:45 +0200 Subject: [PATCH 04/10] Update WebserviceUnitTest.groovy --- .../groovy/com/baeldung/webservice/WebserviceUnitTest.groovy | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core-groovy-2/src/test/groovy/com/baeldung/webservice/WebserviceUnitTest.groovy b/core-groovy-2/src/test/groovy/com/baeldung/webservice/WebserviceUnitTest.groovy index 302959d0d9..50433ea890 100644 --- a/core-groovy-2/src/test/groovy/com/baeldung/webservice/WebserviceUnitTest.groovy +++ b/core-groovy-2/src/test/groovy/com/baeldung/webservice/WebserviceUnitTest.groovy @@ -75,6 +75,7 @@ class WebserviceUnitTest extends GroovyTestCase { assert stories.size() == 5 } + /* see BAEL-3753 void test_whenConsumingSoap_thenReceiveResponse() { def url = "http://www.dataaccess.com/webservicesserver/numberconversion.wso" def soapClient = new SOAPClient(url) @@ -89,6 +90,7 @@ class WebserviceUnitTest extends GroovyTestCase { def words = response.NumberToWordsResponse assert words == "one thousand two hundred and thirty four " } + */ void test_whenConsumingRestGet_thenReceiveResponse() { def path = "/get" @@ -149,4 +151,4 @@ class WebserviceUnitTest extends GroovyTestCase { assert e?.response?.statusCode != 200 } } -} \ No newline at end of file +} From 85e8b654e19e6dca196d86fde5741427d2cb9b00 Mon Sep 17 00:00:00 2001 From: "m.raheem" Date: Wed, 8 Jan 2020 21:07:33 +0200 Subject: [PATCH 05/10] first review round changes --- .../jmockit/LoginControllerIntegrationTest.java | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/testing-modules/mocks/src/test/java/com/baeldung/jmockit/LoginControllerIntegrationTest.java b/testing-modules/mocks/src/test/java/com/baeldung/jmockit/LoginControllerIntegrationTest.java index cb490926f8..df3eeccca2 100644 --- a/testing-modules/mocks/src/test/java/com/baeldung/jmockit/LoginControllerIntegrationTest.java +++ b/testing-modules/mocks/src/test/java/com/baeldung/jmockit/LoginControllerIntegrationTest.java @@ -133,7 +133,6 @@ public class LoginControllerIntegrationTest { @Test public void partialMocking() { - LoginService partialLoginService = new LoginService(); partialLoginService.setLoginDao(loginDao); loginController.loginService = partialLoginService; @@ -142,24 +141,22 @@ public class LoginControllerIntegrationTest { userForm.username = "foo"; new Expectations(partialLoginService) {{ - //let's mock loginDao#login() call + // let's mock DAO call loginDao.login(userForm); result = 1; - //no expectation for partialLoginService#login() so that real implementation is used + // no expectation for login method so that real implementation is used - //mocking partialLoginService#setCurrentUser() + // mock setCurrentUser call partialLoginService.setCurrentUser("foo"); }}; String login = loginController.login(userForm); Assert.assertEquals("OK", login); - // verify that mocked partialLoginService#setCurrentUser("foo") is called - new Verifications() { - { - partialLoginService.setCurrentUser("foo"); - } - }; + // verify mocked call + new Verifications() {{ + partialLoginService.setCurrentUser("foo"); + }}; } } From fd35d741b7629eb73f60254e50feb2c8199cf618 Mon Sep 17 00:00:00 2001 From: Gang Date: Wed, 8 Jan 2020 20:07:32 -0700 Subject: [PATCH 06/10] BAEL-3656 Fix typos in the unit test names. --- .../baeldung/poi/excel/ExcelCellFormatterUnitTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelCellFormatterUnitTest.java b/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelCellFormatterUnitTest.java index adedf951d6..d9f96ee93c 100644 --- a/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelCellFormatterUnitTest.java +++ b/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelCellFormatterUnitTest.java @@ -29,7 +29,7 @@ public class ExcelCellFormatterUnitTest { } @Test - public void gvieStringCell_whenGetCellStringValue_thenReturnStringValue() throws IOException { + public void givenStringCell_whenGetCellStringValue_thenReturnStringValue() throws IOException { Workbook workbook = new XSSFWorkbook(fileLocation); Sheet sheet = workbook.getSheetAt(0); Row row = sheet.getRow(0); @@ -40,7 +40,7 @@ public class ExcelCellFormatterUnitTest { } @Test - public void gvieBooleanCell_whenGetCellStringValue_thenReturnBooleanStringValue() throws IOException { + public void givenBooleanCell_whenGetCellStringValue_thenReturnBooleanStringValue() throws IOException { Workbook workbook = new XSSFWorkbook(fileLocation); Sheet sheet = workbook.getSheetAt(0); Row row = sheet.getRow(0); @@ -51,7 +51,7 @@ public class ExcelCellFormatterUnitTest { } @Test - public void gvieNumericCell_whenGetCellStringValue_thenReturnNumericStringValue() throws IOException { + public void givenNumericCell_whenGetCellStringValue_thenReturnNumericStringValue() throws IOException { Workbook workbook = new XSSFWorkbook(fileLocation); Sheet sheet = workbook.getSheetAt(0); Row row = sheet.getRow(0); @@ -63,7 +63,7 @@ public class ExcelCellFormatterUnitTest { } @Test - public void gvieFormualCell_whenGetCellStringValue_thenReturnOriginalFormulaString() throws IOException { + public void givenFormualCell_whenGetCellStringValue_thenReturnOriginalFormulaString() throws IOException { Workbook workbook = new XSSFWorkbook(fileLocation); Sheet sheet = workbook.getSheetAt(0); Row row = sheet.getRow(0); @@ -74,7 +74,7 @@ public class ExcelCellFormatterUnitTest { } @Test - public void gvieFormualCell_whenGetCellStringValueForFormula_thenReturnOriginalFormulatring() throws IOException { + public void givenFormualCell_whenGetCellStringValueForFormula_thenReturnOriginalFormulatring() throws IOException { Workbook workbook = new XSSFWorkbook(fileLocation); Sheet sheet = workbook.getSheetAt(0); Row row = sheet.getRow(0); From 4899959adbcf52db3269465471f706bc6b3af876 Mon Sep 17 00:00:00 2001 From: Vijay Palaniappan S Date: Thu, 9 Jan 2020 23:28:12 +0530 Subject: [PATCH 07/10] Added Create Date class & Unit Tests for all the cases (#8499) --- .../com/baeldung/datebasics/CreateDate.java | 45 +++++++++++++++ .../datebasics/CreateDateUnitTest.java | 56 +++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datebasics/CreateDate.java create mode 100644 core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datebasics/CreateDateUnitTest.java diff --git a/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datebasics/CreateDate.java b/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datebasics/CreateDate.java new file mode 100644 index 0000000000..327827e03a --- /dev/null +++ b/core-java-modules/core-java-datetime-java8/src/main/java/com/baeldung/datebasics/CreateDate.java @@ -0,0 +1,45 @@ +package com.baeldung.datebasics; + +import java.time.Clock; +import java.time.LocalDate; +import java.time.Month; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; + +public class CreateDate { + public LocalDate getTodaysDate() { + return LocalDate.now(); + } + + public LocalDate getTodaysDateFromClock() { + return LocalDate.now(Clock.systemDefaultZone()); + } + + public LocalDate getTodaysDateFromZone(String zone) { + return LocalDate.now(ZoneId.of(zone)); + } + + public LocalDate getCustomDateOne(int year, int month, int dayOfMonth) { + return LocalDate.of(year, month, dayOfMonth); + } + + public LocalDate getCustomDateTwo(int year, Month month, int dayOfMonth) { + return LocalDate.of(year, month, dayOfMonth); + } + + public LocalDate getDateFromEpochDay(long epochDay) { + return LocalDate.ofEpochDay(epochDay); + } + + public LocalDate getDateFromYearAndDayOfYear(int year, int dayOfYear) { + return LocalDate.ofYearDay(year, dayOfYear); + } + + public LocalDate getDateFromString(String date) { + return LocalDate.parse(date); + } + + public LocalDate getDateFromStringAndFormatter(String date, String pattern) { + return LocalDate.parse(date, DateTimeFormatter.ofPattern(pattern)); + } +} diff --git a/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datebasics/CreateDateUnitTest.java b/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datebasics/CreateDateUnitTest.java new file mode 100644 index 0000000000..54f3285ea0 --- /dev/null +++ b/core-java-modules/core-java-datetime-java8/src/test/java/com/baeldung/datebasics/CreateDateUnitTest.java @@ -0,0 +1,56 @@ +package com.baeldung.datebasics; + +import static org.junit.Assert.assertEquals; + +import java.time.Month; + +import org.junit.Test; + +public class CreateDateUnitTest { + private CreateDate date = new CreateDate(); + + @Test + public void whenUsingNowMethod_thenLocalDate() { + assertEquals("2020-01-08", date.getTodaysDate()); + } + + @Test + public void whenUsingClock_thenLocalDate() { + assertEquals("2020-01-08", date.getTodaysDateFromClock()); + } + + @Test + public void givenValues_whenUsingZone_thenLocalDate() { + assertEquals("2020-01-08", date.getTodaysDateFromZone("Asia/Kolkata")); + } + + @Test + public void givenValues_whenUsingOfMethod_thenLocalDate() { + assertEquals("2020-01-08", date.getCustomDateOne(2020, 1, 8)); + } + + @Test + public void givenValuesWithMonthEnum_whenUsingOfMethod_thenLocalDate() { + assertEquals("2020-01-08", date.getCustomDateTwo(2020, Month.JANUARY, 8)); + } + + @Test + public void givenValues_whenUsingEpochDay_thenLocalDate() { + assertEquals("2020-01-08", date.getDateFromEpochDay(18269)); + } + + @Test + public void givenValues_whenUsingYearDay_thenLocalDate() { + assertEquals("2020-01-08", date.getDateFromYearAndDayOfYear(2020, 8)); + } + + @Test + public void givenValues_whenUsingParse_thenLocalDate() { + assertEquals("2020-01-08", date.getDateFromString("2020-01-08")); + } + + @Test + public void givenValuesWithFormatter_whenUsingParse_thenLocalDate() { + assertEquals("2020-01-08", date.getDateFromStringAndFormatter("8-Jan-2020", "d-MMM-yyyy")); + } +} From 4805a295cbbe8149c46748cae84133a9839f503e Mon Sep 17 00:00:00 2001 From: kwoyke Date: Thu, 9 Jan 2020 19:04:26 +0100 Subject: [PATCH 08/10] BAEL-3723: Add missing code - SecurityWebApplicationInitializer (#8492) --- .../security/SecurityWebApplicationInitializer.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 spring-security-modules/spring-security-rest/src/main/java/org/baeldung/security/SecurityWebApplicationInitializer.java diff --git a/spring-security-modules/spring-security-rest/src/main/java/org/baeldung/security/SecurityWebApplicationInitializer.java b/spring-security-modules/spring-security-rest/src/main/java/org/baeldung/security/SecurityWebApplicationInitializer.java new file mode 100644 index 0000000000..33978962bb --- /dev/null +++ b/spring-security-modules/spring-security-rest/src/main/java/org/baeldung/security/SecurityWebApplicationInitializer.java @@ -0,0 +1,10 @@ +package org.baeldung.security; + +import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; + +public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer { + + public SecurityWebApplicationInitializer() { + super(SecurityJavaConfig.class); + } +} From 4b24c5c296ac5f18235d73d18618f647ab077417 Mon Sep 17 00:00:00 2001 From: kwoyke Date: Thu, 9 Jan 2020 19:07:20 +0100 Subject: [PATCH 09/10] BAEL-3741: Add usage examples of @TestPropertySource (#8493) --- .../FilePropertyInjectionUnitTest.java | 24 +++++++++++++++++++ .../PropertyInjectionUnitTest.java | 23 ++++++++++++++++++ ...gBootPropertyInjectionIntegrationTest.java | 23 ++++++++++++++++++ .../src/test/resources/foo.properties | 1 + 4 files changed, 71 insertions(+) create mode 100644 spring-boot-properties/src/test/java/com/baeldung/properties/testproperty/FilePropertyInjectionUnitTest.java create mode 100644 spring-boot-properties/src/test/java/com/baeldung/properties/testproperty/PropertyInjectionUnitTest.java create mode 100644 spring-boot-properties/src/test/java/com/baeldung/properties/testproperty/SpringBootPropertyInjectionIntegrationTest.java create mode 100644 spring-boot-properties/src/test/resources/foo.properties diff --git a/spring-boot-properties/src/test/java/com/baeldung/properties/testproperty/FilePropertyInjectionUnitTest.java b/spring-boot-properties/src/test/java/com/baeldung/properties/testproperty/FilePropertyInjectionUnitTest.java new file mode 100644 index 0000000000..874f632401 --- /dev/null +++ b/spring-boot-properties/src/test/java/com/baeldung/properties/testproperty/FilePropertyInjectionUnitTest.java @@ -0,0 +1,24 @@ +package com.baeldung.properties.testproperty; + +import org.assertj.core.api.Assertions; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@TestPropertySource("/foo.properties") +public class FilePropertyInjectionUnitTest { + + @Value("${foo}") + private String foo; + + @Test + public void whenFilePropertyProvided_thenProperlyInjected() { + assertThat(foo).isEqualTo("bar"); + } +} diff --git a/spring-boot-properties/src/test/java/com/baeldung/properties/testproperty/PropertyInjectionUnitTest.java b/spring-boot-properties/src/test/java/com/baeldung/properties/testproperty/PropertyInjectionUnitTest.java new file mode 100644 index 0000000000..9492e18322 --- /dev/null +++ b/spring-boot-properties/src/test/java/com/baeldung/properties/testproperty/PropertyInjectionUnitTest.java @@ -0,0 +1,23 @@ +package com.baeldung.properties.testproperty; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@TestPropertySource(properties = {"foo=bar"}) +public class PropertyInjectionUnitTest { + + @Value("${foo}") + private String foo; + + @Test + public void whenPropertyProvided_thenProperlyInjected() { + assertThat(foo).isEqualTo("bar"); + } +} diff --git a/spring-boot-properties/src/test/java/com/baeldung/properties/testproperty/SpringBootPropertyInjectionIntegrationTest.java b/spring-boot-properties/src/test/java/com/baeldung/properties/testproperty/SpringBootPropertyInjectionIntegrationTest.java new file mode 100644 index 0000000000..fdca7e814d --- /dev/null +++ b/spring-boot-properties/src/test/java/com/baeldung/properties/testproperty/SpringBootPropertyInjectionIntegrationTest.java @@ -0,0 +1,23 @@ +package com.baeldung.properties.testproperty; + +import com.baeldung.properties.reloading.SpringBootPropertiesTestApplication; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest(properties = {"foo=bar"}, classes = SpringBootPropertiesTestApplication.class) +public class SpringBootPropertyInjectionIntegrationTest { + + @Value("${foo}") + private String foo; + + @Test + public void whenSpringBootPropertyProvided_thenProperlyInjected() { + assertThat(foo).isEqualTo("bar"); + } +} diff --git a/spring-boot-properties/src/test/resources/foo.properties b/spring-boot-properties/src/test/resources/foo.properties new file mode 100644 index 0000000000..c9f0304f65 --- /dev/null +++ b/spring-boot-properties/src/test/resources/foo.properties @@ -0,0 +1 @@ +foo=bar \ No newline at end of file From 7366c57fe66599fbc374b79d34a9f0dddab8d11a Mon Sep 17 00:00:00 2001 From: Priyesh Mashelkar Date: Thu, 9 Jan 2020 18:14:56 +0000 Subject: [PATCH 10/10] BAEL-3499 Added files for article (#8498) --- spring-boot/pom.xml | 16 +++++++++++-- .../baeldung/buildproperties/Application.java | 18 ++++++++++++++ .../buildproperties/BuildInfoService.java | 21 ++++++++++++++++ .../src/main/resources/build.properties | 2 ++ spring-boot/src/main/resources/build.yml | 2 ++ .../BuildInfoServiceIntegrationTest.java | 24 +++++++++++++++++++ 6 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 spring-boot/src/main/java/com/baeldung/buildproperties/Application.java create mode 100644 spring-boot/src/main/java/com/baeldung/buildproperties/BuildInfoService.java create mode 100644 spring-boot/src/main/resources/build.properties create mode 100644 spring-boot/src/main/resources/build.yml create mode 100644 spring-boot/src/test/java/com/baeldung/buildproperties/BuildInfoServiceIntegrationTest.java diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index 2152a0a00d..b8ebd27e13 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -7,16 +7,17 @@ spring-boot war This is simple boot application for Spring boot actuator test + 0.0.1-SNAPSHOT - com.baeldung parent-boot-2 + com.baeldung 0.0.1-SNAPSHOT ../parent-boot-2 - + org.junit.jupiter @@ -198,6 +199,16 @@ + + org.apache.maven.plugins + maven-resources-plugin + + + @ + + false + + @@ -251,6 +262,7 @@ 5.2.4 18.0 2.2.4 + @ diff --git a/spring-boot/src/main/java/com/baeldung/buildproperties/Application.java b/spring-boot/src/main/java/com/baeldung/buildproperties/Application.java new file mode 100644 index 0000000000..405cec3eac --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/buildproperties/Application.java @@ -0,0 +1,18 @@ +package com.baeldung.buildproperties; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.PropertySource; + +@SpringBootApplication +@ComponentScan(basePackages = "com.baeldung.buildproperties") +@PropertySource("classpath:build.properties") +//@PropertySource("classpath:build.yml") +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/spring-boot/src/main/java/com/baeldung/buildproperties/BuildInfoService.java b/spring-boot/src/main/java/com/baeldung/buildproperties/BuildInfoService.java new file mode 100644 index 0000000000..2a0d27188e --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/buildproperties/BuildInfoService.java @@ -0,0 +1,21 @@ +package com.baeldung.buildproperties; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +@Service +public class BuildInfoService { + @Value("${application-description}") + private String applicationDescription; + + @Value("${application-version}") + private String applicationVersion; + + public String getApplicationDescription() { + return applicationDescription; + } + + public String getApplicationVersion() { + return applicationVersion; + } +} diff --git a/spring-boot/src/main/resources/build.properties b/spring-boot/src/main/resources/build.properties new file mode 100644 index 0000000000..1612b8086d --- /dev/null +++ b/spring-boot/src/main/resources/build.properties @@ -0,0 +1,2 @@ +application-description=@project.description@ +application-version=@project.version@ \ No newline at end of file diff --git a/spring-boot/src/main/resources/build.yml b/spring-boot/src/main/resources/build.yml new file mode 100644 index 0000000000..528d2e3440 --- /dev/null +++ b/spring-boot/src/main/resources/build.yml @@ -0,0 +1,2 @@ +application-description: ^project.description^ +application-version: ^project.version^ \ No newline at end of file diff --git a/spring-boot/src/test/java/com/baeldung/buildproperties/BuildInfoServiceIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/buildproperties/BuildInfoServiceIntegrationTest.java new file mode 100644 index 0000000000..cb056fe56d --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/buildproperties/BuildInfoServiceIntegrationTest.java @@ -0,0 +1,24 @@ +package com.baeldung.buildproperties; + +import static org.junit.Assert.assertThat; + +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class) +class BuildInfoServiceIntegrationTest { + + @Autowired + private BuildInfoService service; + + @Test + void whenGetApplicationDescription_thenSuccess() { + assertThat(service.getApplicationDescription(), Matchers.is("This is simple boot application for Spring boot actuator test")); + assertThat(service.getApplicationVersion(), Matchers.is("0.0.1-SNAPSHOT")); + } +}