Merge branch 'master' of https://github.com/vikasrajput6035/tutorials into BAEL-3504

This commit is contained in:
Vikas Ramsingh Rajput 2020-01-17 23:40:44 +03:00
commit 21faf6df88
18 changed files with 389 additions and 16 deletions

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -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
}
}
}
}

View File

@ -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));
}
}

View File

@ -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"));
}
}

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -0,0 +1 @@
foo=bar

View File

@ -7,16 +7,17 @@
<name>spring-boot</name>
<packaging>war</packaging>
<description>This is simple boot application for Spring boot actuator test</description>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<groupId>com.baeldung</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
</parent>
<dependencies>
<!-- JUnit Jupiter dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
@ -198,6 +199,16 @@
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<delimiter>@</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
</build>
@ -251,6 +262,7 @@
<graphql-java-tools.version>5.2.4</graphql-java-tools.version>
<guava.version>18.0</guava.version>
<git-commit-id-plugin.version>2.2.4</git-commit-id-plugin.version>
<resource.delimiter>@</resource.delimiter>
</properties>
</project>

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,2 @@
application-description=@project.description@
application-version=@project.version@

View File

@ -0,0 +1,2 @@
application-description: ^project.description^
application-version: ^project.version^

View File

@ -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"));
}
}

View File

@ -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);
}
}

View File

@ -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");
}};
}
}