Merge branch 'master' into master

This commit is contained in:
Loredana Crusoveanu
2018-06-30 11:26:37 +03:00
committed by GitHub
91 changed files with 4713 additions and 539 deletions
@@ -0,0 +1,92 @@
package com.baeldung.date;
import org.junit.Assert;
import org.junit.Test;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.util.Calendar;
import java.util.Date;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
public class DateWithoutTimeUnitTest {
private static final long MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;
@Test
public void whenGettingDateWithoutTimeUsingCalendar_thenReturnDateWithoutTime() {
Date dateWithoutTime = DateWithoutTime.getDateWithoutTimeUsingCalendar();
// first check the time is set to 0
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateWithoutTime);
assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY));
assertEquals(0, calendar.get(Calendar.MINUTE));
assertEquals(0, calendar.get(Calendar.SECOND));
assertEquals(0, calendar.get(Calendar.MILLISECOND));
// we get the day of the date
int day = calendar.get(Calendar.DAY_OF_MONTH);
// if we add the mills of one day minus 1 we should get the same day
calendar.setTimeInMillis(dateWithoutTime.getTime() + MILLISECONDS_PER_DAY - 1);
assertEquals(day, calendar.get(Calendar.DAY_OF_MONTH));
// if we add one full day in millis we should get a different day
calendar.setTimeInMillis(dateWithoutTime.getTime() + MILLISECONDS_PER_DAY);
assertNotEquals(day, calendar.get(Calendar.DAY_OF_MONTH));
}
@Test
public void whenGettingDateWithoutTimeUsingFormat_thenReturnDateWithoutTime() throws ParseException {
Date dateWithoutTime = DateWithoutTime.getDateWithoutTimeUsingFormat();
// first check the time is set to 0
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateWithoutTime);
assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY));
assertEquals(0, calendar.get(Calendar.MINUTE));
assertEquals(0, calendar.get(Calendar.SECOND));
assertEquals(0, calendar.get(Calendar.MILLISECOND));
// we get the day of the date
int day = calendar.get(Calendar.DAY_OF_MONTH);
// if we add the mills of one day minus 1 we should get the same day
calendar.setTimeInMillis(dateWithoutTime.getTime() + MILLISECONDS_PER_DAY - 1);
assertEquals(day, calendar.get(Calendar.DAY_OF_MONTH));
// if we add one full day in millis we should get a different day
calendar.setTimeInMillis(dateWithoutTime.getTime() + MILLISECONDS_PER_DAY);
assertNotEquals(day, calendar.get(Calendar.DAY_OF_MONTH));
}
@Test
public void whenGettingLocalDate_thenReturnDateWithoutTime() {
// get the local date
LocalDate localDate = DateWithoutTime.getLocalDate();
// get the millis of our LocalDate
long millisLocalDate = localDate
.atStartOfDay()
.toInstant(OffsetDateTime
.now()
.getOffset())
.toEpochMilli();
Calendar calendar = Calendar.getInstance();
// if we add the millis of one day minus 1 we should get the same day
calendar.setTimeInMillis(millisLocalDate + MILLISECONDS_PER_DAY - 1);
assertEquals(localDate.getDayOfMonth(), calendar.get(Calendar.DAY_OF_MONTH));
// if we add one full day in millis we should get a different day
calendar.setTimeInMillis(millisLocalDate + MILLISECONDS_PER_DAY);
assertNotEquals(localDate.getDayOfMonth(), calendar.get(Calendar.DAY_OF_MONTH));
}
}
@@ -0,0 +1,45 @@
package com.baeldung.datetime;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.junit.Before;
import org.junit.Test;
public class DateExtractYearMonthDayIntegerValuesUnitTest {
DateExtractYearMonthDayIntegerValues extractYearMonthDateIntegerValues = new DateExtractYearMonthDayIntegerValues();
Date date;
@Before
public void setup() throws ParseException
{
date=new SimpleDateFormat("dd-MM-yyyy").parse("01-03-2018");
}
@Test
public void whenGetYear_thenCorrectYear()
{
int actualYear=extractYearMonthDateIntegerValues.getYear(date);
assertThat(actualYear,is(2018));
}
@Test
public void whenGetMonth_thenCorrectMonth()
{
int actualMonth=extractYearMonthDateIntegerValues.getMonth(date);
assertThat(actualMonth,is(02));
}
@Test
public void whenGetDay_thenCorrectDay()
{
int actualDayOfMonth=extractYearMonthDateIntegerValues.getDay(date);
assertThat(actualDayOfMonth,is(01));
}
}
@@ -0,0 +1,36 @@
package com.baeldung.datetime;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.time.LocalDate;
import org.junit.Test;
public class LocalDateExtractYearMonthDayIntegerValuesUnitTest {
LocalDateExtractYearMonthDayIntegerValues localDateExtractYearMonthDayIntegerValues=new LocalDateExtractYearMonthDayIntegerValues();
LocalDate localDate=LocalDate.parse("2007-12-03");
@Test
public void whenGetYear_thenCorrectYear()
{
int actualYear=localDateExtractYearMonthDayIntegerValues.getYear(localDate);
assertThat(actualYear,is(2007));
}
@Test
public void whenGetMonth_thenCorrectMonth()
{
int actualMonth=localDateExtractYearMonthDayIntegerValues.getMonth(localDate);
assertThat(actualMonth,is(12));
}
@Test
public void whenGetDay_thenCorrectDay()
{
int actualDayOfMonth=localDateExtractYearMonthDayIntegerValues.getDay(localDate);
assertThat(actualDayOfMonth,is(03));
}
}
@@ -0,0 +1,36 @@
package com.baeldung.datetime;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.time.LocalDateTime;
import org.junit.Test;
public class LocalDateTimeExtractYearMonthDayIntegerValuesUnitTest {
LocalDateTimeExtractYearMonthDayIntegerValues localDateTimeExtractYearMonthDayIntegerValues = new LocalDateTimeExtractYearMonthDayIntegerValues();
LocalDateTime localDateTime=LocalDateTime.parse("2007-12-03T10:15:30");
@Test
public void whenGetYear_thenCorrectYear()
{
int actualYear=localDateTimeExtractYearMonthDayIntegerValues.getYear(localDateTime);
assertThat(actualYear,is(2007));
}
@Test
public void whenGetMonth_thenCorrectMonth()
{
int actualMonth=localDateTimeExtractYearMonthDayIntegerValues.getMonth(localDateTime);
assertThat(actualMonth,is(12));
}
@Test
public void whenGetDay_thenCorrectDay()
{
int actualDayOfMonth=localDateTimeExtractYearMonthDayIntegerValues.getDay(localDateTime);
assertThat(actualDayOfMonth,is(03));
}
}
@@ -0,0 +1,36 @@
package com.baeldung.datetime;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.time.OffsetDateTime;
import org.junit.Test;
public class OffsetDateTimeExtractYearMonthDayIntegerValuesUnitTest {
OffsetDateTimeExtractYearMonthDayIntegerValues offsetDateTimeExtractYearMonthDayIntegerValues = new OffsetDateTimeExtractYearMonthDayIntegerValues();
OffsetDateTime offsetDateTime=OffsetDateTime.parse("2007-12-03T10:15:30+01:00");
@Test
public void whenGetYear_thenCorrectYear()
{
int actualYear=offsetDateTimeExtractYearMonthDayIntegerValues.getYear(offsetDateTime);
assertThat(actualYear,is(2007));
}
@Test
public void whenGetMonth_thenCorrectMonth()
{
int actualMonth=offsetDateTimeExtractYearMonthDayIntegerValues.getMonth(offsetDateTime);
assertThat(actualMonth,is(12));
}
@Test
public void whenGetDay_thenCorrectDay()
{
int actualDayOfMonth=offsetDateTimeExtractYearMonthDayIntegerValues.getDay(offsetDateTime);
assertThat(actualDayOfMonth,is(03));
}
}
@@ -0,0 +1,36 @@
package com.baeldung.datetime;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.time.ZonedDateTime;
import org.junit.Test;
public class ZonedDateTimeExtractYearMonthDayIntegerValuesUnitTest {
ZonedDateTimeExtractYearMonthDayIntegerValues zonedDateTimeExtractYearMonthDayIntegerValues = new ZonedDateTimeExtractYearMonthDayIntegerValues();
ZonedDateTime zonedDateTime=ZonedDateTime.parse("2007-12-03T10:15:30+01:00");
@Test
public void whenGetYear_thenCorrectYear()
{
int actualYear=zonedDateTimeExtractYearMonthDayIntegerValues.getYear(zonedDateTime);
assertThat(actualYear,is(2007));
}
@Test
public void whenGetMonth_thenCorrectMonth()
{
int actualMonth=zonedDateTimeExtractYearMonthDayIntegerValues.getMonth(zonedDateTime);
assertThat(actualMonth,is(12));
}
@Test
public void whenGetDay_thenCorrectDay()
{
int actualDayOfMonth=zonedDateTimeExtractYearMonthDayIntegerValues.getDay(zonedDateTime);
assertThat(actualDayOfMonth,is(03));
}
}
@@ -1,21 +1,29 @@
package com.baeldung.socket;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.Executors;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.concurrent.Executors;
import static org.junit.Assert.assertEquals;
public class EchoIntegrationTest {
private static final Integer PORT = 4444;
private static int port;
@BeforeClass
public static void start() throws InterruptedException {
public static void start() throws InterruptedException, IOException {
// Take an available port
ServerSocket s = new ServerSocket(0);
port = s.getLocalPort();
s.close();
Executors.newSingleThreadExecutor()
.submit(() -> new EchoServer().start(PORT));
.submit(() -> new EchoServer().start(port));
Thread.sleep(500);
}
@@ -23,7 +31,7 @@ public class EchoIntegrationTest {
@Before
public void init() {
client.startConnection("127.0.0.1", PORT);
client.startConnection("127.0.0.1", port);
}
@After
@@ -1,31 +1,39 @@
package com.baeldung.socket;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.Executors;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.concurrent.Executors;
import static org.junit.Assert.assertEquals;
public class GreetServerIntegrationTest {
private GreetClient client;
private static final Integer PORT = 6666;
private static int port;
@BeforeClass
public static void start() throws InterruptedException {
public static void start() throws InterruptedException, IOException {
// Take an available port
ServerSocket s = new ServerSocket(0);
port = s.getLocalPort();
s.close();
Executors.newSingleThreadExecutor()
.submit(() -> new GreetServer().start(PORT));
.submit(() -> new GreetServer().start(port));
Thread.sleep(500);
}
@Before
public void init() {
client = new GreetClient();
client.startConnection("127.0.0.1", PORT);
client.startConnection("127.0.0.1", port);
}
@@ -1,26 +1,35 @@
package com.baeldung.socket;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.Executors;
import static org.junit.Assert.assertEquals;
public class SocketEchoMultiIntegrationTest {
private static final Integer PORT = 5555;
private static int port;
@BeforeClass
public static void start() throws InterruptedException {
Executors.newSingleThreadExecutor().submit(() -> new EchoMultiServer().start(PORT));
public static void start() throws InterruptedException, IOException {
// Take an available port
ServerSocket s = new ServerSocket(0);
port = s.getLocalPort();
s.close();
Executors.newSingleThreadExecutor().submit(() -> new EchoMultiServer().start(port));
Thread.sleep(500);
}
@Test
public void givenClient1_whenServerResponds_thenCorrect() {
EchoClient client = new EchoClient();
client.startConnection("127.0.0.1", PORT);
client.startConnection("127.0.0.1", port);
String msg1 = client.sendMessage("hello");
String msg2 = client.sendMessage("world");
String terminate = client.sendMessage(".");
@@ -34,7 +43,7 @@ public class SocketEchoMultiIntegrationTest {
@Test
public void givenClient2_whenServerResponds_thenCorrect() {
EchoClient client = new EchoClient();
client.startConnection("127.0.0.1", PORT);
client.startConnection("127.0.0.1", port);
String msg1 = client.sendMessage("hello");
String msg2 = client.sendMessage("world");
String terminate = client.sendMessage(".");
@@ -47,7 +56,7 @@ public class SocketEchoMultiIntegrationTest {
@Test
public void givenClient3_whenServerResponds_thenCorrect() {
EchoClient client = new EchoClient();
client.startConnection("127.0.0.1", PORT);
client.startConnection("127.0.0.1", port);
String msg1 = client.sendMessage("hello");
String msg2 = client.sendMessage("world");
String terminate = client.sendMessage(".");
@@ -0,0 +1,70 @@
package com.baeldung.string;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.*;
public class TitleCaseConverterUnitTest {
private static final String TEXT = "tHis IS a tiTLe";
private static final String TEXT_EXPECTED = "This Is A Title";
private static final String TEXT_EXPECTED_NOT_FULL = "THis IS A TiTLe";
private static final String TEXT_OTHER_DELIMITERS = "tHis, IS a tiTLe";
private static final String TEXT_EXPECTED_OTHER_DELIMITERS = "This, Is A Title";
private static final String TEXT_EXPECTED_OTHER_DELIMITERS_NOT_FULL = "THis, IS A TiTLe";
@Test
public void whenConvertingToTitleCaseIterating_thenStringConverted() {
assertEquals(TEXT_EXPECTED, TitleCaseConverter.convertToTitleCaseIteratingChars(TEXT));
}
@Test
public void whenConvertingToTitleCaseSplitting_thenStringConverted() {
assertEquals(TEXT_EXPECTED, TitleCaseConverter.convertToTitleCaseSplitting(TEXT));
}
@Test
public void whenConvertingToTitleCaseUsingWordUtilsFull_thenStringConverted() {
assertEquals(TEXT_EXPECTED, TitleCaseConverter.convertToTileCaseWordUtilsFull(TEXT));
}
@Test
public void whenConvertingToTitleCaseUsingWordUtils_thenStringConvertedOnlyFirstCharacter() {
assertEquals(TEXT_EXPECTED_NOT_FULL, TitleCaseConverter.convertToTileCaseWordUtils(TEXT));
}
@Test
public void whenConvertingToTitleCaseUsingIcu4j_thenStringConverted() {
assertEquals(TEXT_EXPECTED, TitleCaseConverter.convertToTitleCaseIcu4j(TEXT));
}
@Test
public void whenConvertingToTitleCaseWithDifferentDelimiters_thenDelimitersKept() {
assertEquals(TEXT_EXPECTED_OTHER_DELIMITERS, TitleCaseConverter.convertToTitleCaseIteratingChars(TEXT_OTHER_DELIMITERS));
assertEquals(TEXT_EXPECTED_OTHER_DELIMITERS, TitleCaseConverter.convertToTitleCaseSplitting(TEXT_OTHER_DELIMITERS));
assertEquals(TEXT_EXPECTED_OTHER_DELIMITERS, TitleCaseConverter.convertToTileCaseWordUtilsFull(TEXT_OTHER_DELIMITERS));
assertEquals(TEXT_EXPECTED_OTHER_DELIMITERS_NOT_FULL, TitleCaseConverter.convertToTileCaseWordUtils(TEXT_OTHER_DELIMITERS));
assertEquals(TEXT_EXPECTED_OTHER_DELIMITERS, TitleCaseConverter.convertToTitleCaseIcu4j(TEXT_OTHER_DELIMITERS));
}
@Test
public void givenNull_whenConvertingToTileCase_thenReturnNull() {
assertEquals(null, TitleCaseConverter.convertToTitleCaseIteratingChars(null));
assertEquals(null, TitleCaseConverter.convertToTitleCaseSplitting(null));
assertEquals(null, TitleCaseConverter.convertToTileCaseWordUtilsFull(null));
assertEquals(null, TitleCaseConverter.convertToTileCaseWordUtils(null));
assertEquals(null, TitleCaseConverter.convertToTitleCaseIcu4j(null));
}
@Test
public void givenEmptyString_whenConvertingToTileCase_thenReturnEmptyString() {
assertEquals("", TitleCaseConverter.convertToTitleCaseIteratingChars(""));
assertEquals("", TitleCaseConverter.convertToTitleCaseSplitting(""));
assertEquals("", TitleCaseConverter.convertToTileCaseWordUtilsFull(""));
assertEquals("", TitleCaseConverter.convertToTileCaseWordUtils(""));
assertEquals("", TitleCaseConverter.convertToTitleCaseIcu4j(""));
}
}