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/main/resources/ExcelCellFormatterTest.xlsx b/apache-poi/src/main/resources/ExcelCellFormatterTest.xlsx new file mode 100644 index 0000000000..54e8734d58 Binary files /dev/null and b/apache-poi/src/main/resources/ExcelCellFormatterTest.xlsx differ 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..d9f96ee93c --- /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 givenStringCell_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 givenBooleanCell_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 givenNumericCell_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 givenFormualCell_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 givenFormualCell_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 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 +} 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")); + } +} 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 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")); + } +} 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); + } +} 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..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 @@ -130,30 +130,33 @@ 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 DAO call + loginDao.login(userForm); result = 1; + + // no expectation for login method so that real implementation is used + + // mock setCurrentUser call partialLoginService.setCurrentUser("foo"); }}; String login = loginController.login(userForm); Assert.assertEquals("OK", login); - // verify mocked call - new FullVerifications(partialLoginService) { - }; - new FullVerifications(loginDao) { - }; + // verify mocked call + new Verifications() {{ + partialLoginService.setCurrentUser("foo"); + }}; + } }