Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Philippe 2018-06-30 00:04:48 +02:00
commit 3b1c7ead13
73 changed files with 3717 additions and 106 deletions

View File

@ -0,0 +1,52 @@
package com.baeldung.algorithms.romannumerals;
import java.util.List;
class RomanArabicConverter {
public static int romanToArabic(String input) {
String romanNumeral = input.toUpperCase();
int result = 0;
List<RomanNumeral> romanNumerals = RomanNumeral.getReverseSortedValues();
int i = 0;
while ((romanNumeral.length() > 0) && (i < romanNumerals.size())) {
RomanNumeral symbol = romanNumerals.get(i);
if (romanNumeral.startsWith(symbol.name())) {
result += symbol.getValue();
romanNumeral = romanNumeral.substring(symbol.name().length());
} else {
i++;
}
}
if (romanNumeral.length() > 0) {
throw new IllegalArgumentException(input + " cannot be converted to a Roman Numeral");
}
return result;
}
public static String arabicToRoman(int number) {
if ((number <= 0) || (number > 4000)) {
throw new IllegalArgumentException(number + " is not in range (0,4000]");
}
List<RomanNumeral> romanNumerals = RomanNumeral.getReverseSortedValues();
int i = 0;
StringBuilder sb = new StringBuilder();
while (number > 0 && i < romanNumerals.size()) {
RomanNumeral currentSymbol = romanNumerals.get(i);
if (currentSymbol.getValue() <= number) {
sb.append(currentSymbol.name());
number -= currentSymbol.getValue();
} else {
i++;
}
}
return sb.toString();
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.algorithms.romannumerals;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
enum RomanNumeral {
I(1), IV(4), V(5), IX(9), X(10), XL(40), L(50), XC(90), C(100), CD(400), D(500), CM(900), M(1000);
private int value;
RomanNumeral(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public static List<RomanNumeral> getReverseSortedValues() {
return Arrays.stream(values())
.sorted(Comparator.comparing((RomanNumeral e) -> e.value).reversed())
.collect(Collectors.toList());
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.algorithms.romannumerals;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class RomanArabicConverterUnitTest {
@Test
public void given2018Roman_WhenConvertingToArabic_ThenReturn2018() {
String roman2018 = "MMXVIII";
int result = RomanArabicConverter.romanToArabic(roman2018);
assertThat(result).isEqualTo(2018);
}
@Test
public void given1999Arabic_WhenConvertingToRoman_ThenReturnMCMXCIX() {
int arabic1999 = 1999;
String result = RomanArabicConverter.arabicToRoman(arabic1999);
assertThat(result).isEqualTo("MCMXCIX");
}
}

66
antlr/pom.xml Normal file
View File

@ -0,0 +1,66 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>antlr</artifactId>
<name>antlr</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>${antlr.version}</version>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${mojo.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/target/generated-sources/antlr4</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>${antlr.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
<antlr.version>4.7.1</antlr.version>
<junit.version>4.12</junit.version>
<mojo.version>3.0.0</mojo.version>
</properties>
</project>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,16 @@
grammar Log;
log : entry+;
entry : timestamp ' ' level ' ' message CRLF;
timestamp : DATE ' ' TIME;
level : 'ERROR' | 'INFO' | 'DEBUG';
message : (TEXT | ' ')+;
fragment DIGIT : [0-9];
fragment TWODIGIT : DIGIT DIGIT;
fragment LETTER : [A-Za-z];
DATE : TWODIGIT TWODIGIT '-' LETTER LETTER LETTER '-' TWODIGIT;
TIME : TWODIGIT ':' TWODIGIT ':' TWODIGIT;
TEXT : LETTER+;
CRLF : '\r'? '\n' | '\r';

View File

@ -0,0 +1,28 @@
package com.baeldung.antlr.java;
import com.baeldung.antlr.Java8BaseListener;
import com.baeldung.antlr.Java8Parser;
import org.antlr.v4.runtime.tree.TerminalNode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class UppercaseMethodListener extends Java8BaseListener {
private List<String> errors = new ArrayList<String>();
@Override
public void enterMethodDeclarator(Java8Parser.MethodDeclaratorContext ctx) {
TerminalNode node = ctx.Identifier();
String methodName = node.getText();
if (Character.isUpperCase(methodName.charAt(0))){
errors.add(String.format("Method %s is uppercased!", methodName));
}
}
public List<String> getErrors(){
return Collections.unmodifiableList(errors);
}
}

View File

@ -0,0 +1,51 @@
package com.baeldung.antlr.log;
import com.baeldung.antlr.LogBaseListener;
import com.baeldung.antlr.LogParser;
import com.baeldung.antlr.log.model.LogLevel;
import com.baeldung.antlr.log.model.LogEntry;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
public class LogListener extends LogBaseListener {
private static final DateTimeFormatter DEFAULT_DATETIME_FORMATTER
= DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss", Locale.ENGLISH);
private List<LogEntry> entries = new ArrayList<>();
private LogEntry currentLogEntry;
@Override
public void enterEntry(LogParser.EntryContext ctx) {
this.currentLogEntry = new LogEntry();
}
@Override
public void exitEntry(LogParser.EntryContext ctx) {
entries.add(currentLogEntry);
}
@Override
public void enterTimestamp(LogParser.TimestampContext ctx) {
currentLogEntry.setTimestamp(LocalDateTime.parse(ctx.getText(), DEFAULT_DATETIME_FORMATTER));
}
@Override
public void enterMessage(LogParser.MessageContext ctx) {
currentLogEntry.setMessage(ctx.getText());
}
@Override
public void enterLevel(LogParser.LevelContext ctx) {
currentLogEntry.setLevel(LogLevel.valueOf(ctx.getText()));
}
public List<LogEntry> getEntries() {
return Collections.unmodifiableList(entries);
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.antlr.log.model;
import java.time.LocalDateTime;
public class LogEntry {
private LogLevel level;
private String message;
private LocalDateTime timestamp;
public LogLevel getLevel() {
return level;
}
public void setLevel(LogLevel level) {
this.level = level;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public LocalDateTime getTimestamp() {
return timestamp;
}
public void setTimestamp(LocalDateTime timestamp) {
this.timestamp = timestamp;
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.antlr.log.model;
public enum LogLevel {
DEBUG, INFO, ERROR
}

View File

@ -0,0 +1,30 @@
package com.baeldung.antlr;
import com.baeldung.antlr.java.UppercaseMethodListener;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
public class JavaParserUnitTest {
@Test
public void whenOneMethodStartsWithUpperCase_thenOneErrorReturned() throws Exception{
String javaClassContent = "public class SampleClass { void DoSomething(){} }";
Java8Lexer java8Lexer = new Java8Lexer(CharStreams.fromString(javaClassContent));
CommonTokenStream tokens = new CommonTokenStream(java8Lexer);
Java8Parser java8Parser = new Java8Parser(tokens);
ParseTree tree = java8Parser.compilationUnit();
ParseTreeWalker walker = new ParseTreeWalker();
UppercaseMethodListener uppercaseMethodListener = new UppercaseMethodListener();
walker.walk(uppercaseMethodListener, tree);
assertThat(uppercaseMethodListener.getErrors().size(), is(1));
assertThat(uppercaseMethodListener.getErrors().get(0),
is("Method DoSomething is uppercased!"));
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.antlr;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import com.baeldung.antlr.log.LogListener;
import com.baeldung.antlr.log.model.LogLevel;
import com.baeldung.antlr.log.model.LogEntry;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.junit.Test;
import java.time.LocalDateTime;
public class LogParserUnitTest {
@Test
public void whenLogContainsOneErrorLogEntry_thenOneErrorIsReturned() throws Exception {
String logLines = "2018-May-05 14:20:21 DEBUG entering awesome method\r\n" +
"2018-May-05 14:20:24 ERROR Bad thing happened\r\n";
LogLexer serverLogLexer = new LogLexer(CharStreams.fromString(logLines));
CommonTokenStream tokens = new CommonTokenStream( serverLogLexer );
LogParser logParser = new LogParser(tokens);
ParseTreeWalker walker = new ParseTreeWalker();
LogListener logWalker = new LogListener();
walker.walk(logWalker, logParser.log());
assertThat(logWalker.getEntries().size(), is(2));
LogEntry error = logWalker.getEntries().get(1);
assertThat(error.getLevel(), is(LogLevel.ERROR));
assertThat(error.getMessage(), is("Bad thing happened"));
assertThat(error.getTimestamp(), is(LocalDateTime.of(2018,5,5,14,20,24)));
}
}

View File

@ -3,8 +3,8 @@
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd" xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"> xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>Baeldung custom PMD rules</description> <description>Baeldung custom PMD rules</description>
<rule name="UnitTestMustFollowNamingConventionRule" message="Test class name doesn't follow the naming convention" class="org.baeldung.pmd.UnitTestNamingConventionRule"> <rule name="UnitTestMustFollowNamingConventionRule" message="Unit test class names need to end in 'UnitTest', integration tests with 'IntegrationTest', etc " class="org.baeldung.pmd.UnitTestNamingConventionRule">
<description>Test does not follow Baeldung naming convention</description> <description>Test does not follow Baeldung naming convention</description>
<priority>3</priority> <priority>3</priority>
</rule> </rule>
</ruleset> </ruleset>

View File

@ -3,6 +3,7 @@ package com.baeldung.datetime;
import java.time.DayOfWeek; import java.time.DayOfWeek;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters; import java.time.temporal.TemporalAdjusters;
@ -43,4 +44,30 @@ class UseLocalDate {
LocalDateTime startofDay = localDate.atStartOfDay(); LocalDateTime startofDay = localDate.atStartOfDay();
return startofDay; return startofDay;
} }
LocalDateTime getStartOfDayOfLocalDate(LocalDate localDate) {
LocalDateTime startofDay = LocalDateTime.of(localDate, LocalTime.MIDNIGHT);
return startofDay;
}
LocalDateTime getStartOfDayAtMinTime(LocalDate localDate) {
LocalDateTime startofDay = localDate.atTime(LocalTime.MIN);
return startofDay;
}
LocalDateTime getStartOfDayAtMidnightTime(LocalDate localDate) {
LocalDateTime startofDay = localDate.atTime(LocalTime.MIDNIGHT);
return startofDay;
}
LocalDateTime getEndOfDay(LocalDate localDate) {
LocalDateTime endOfDay = localDate.atTime(LocalTime.MAX);
return endOfDay;
}
LocalDateTime getEndOfDayFromLocalTime(LocalDate localDate) {
LocalDateTime endOfDate = LocalTime.MAX.atDate(localDate);
return endOfDate;
}
} }

View File

@ -1,6 +1,8 @@
package com.baeldung.datetime; package com.baeldung.datetime;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoField;
public class UseLocalDateTime { public class UseLocalDateTime {
@ -8,4 +10,15 @@ public class UseLocalDateTime {
return LocalDateTime.parse(representation); return LocalDateTime.parse(representation);
} }
LocalDateTime getEndOfDayFromLocalDateTimeDirectly(LocalDateTime localDateTime) {
LocalDateTime endOfDate = localDateTime.with(ChronoField.NANO_OF_DAY, LocalTime.MAX.toNanoOfDay());
return endOfDate;
}
LocalDateTime getEndOfDayFromLocalDateTime(LocalDateTime localDateTime) {
LocalDateTime endOfDate = localDateTime.toLocalDate()
.atTime(LocalTime.MAX);
return endOfDate;
}
} }

View File

@ -1,12 +1,42 @@
package com.baeldung.datetime; package com.baeldung.datetime;
import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
class UseZonedDateTime { class UseZonedDateTime {
ZonedDateTime getZonedDateTime(LocalDateTime localDateTime, ZoneId zoneId) { ZonedDateTime getZonedDateTime(LocalDateTime localDateTime, ZoneId zoneId) {
return ZonedDateTime.of(localDateTime, zoneId); return ZonedDateTime.of(localDateTime, zoneId);
} }
ZonedDateTime getStartOfDay(LocalDate localDate, ZoneId zone) {
ZonedDateTime startofDay = localDate.atStartOfDay()
.atZone(zone);
return startofDay;
}
ZonedDateTime getStartOfDayShorthand(LocalDate localDate, ZoneId zone) {
ZonedDateTime startofDay = localDate.atStartOfDay(zone);
return startofDay;
}
ZonedDateTime getStartOfDayFromZonedDateTime(ZonedDateTime zonedDateTime) {
ZonedDateTime startofDay = zonedDateTime.toLocalDateTime()
.toLocalDate()
.atStartOfDay(zonedDateTime.getZone());
return startofDay;
}
ZonedDateTime getStartOfDayAtMinTime(ZonedDateTime zonedDateTime) {
ZonedDateTime startofDay = zonedDateTime.with(ChronoField.HOUR_OF_DAY, 0);
return startofDay;
}
ZonedDateTime getStartOfDayAtMidnightTime(ZonedDateTime zonedDateTime) {
ZonedDateTime startofDay = zonedDateTime.with(ChronoField.NANO_OF_DAY, 0);
return startofDay;
}
} }

View File

@ -1,13 +1,15 @@
package com.baeldung.datetime; package com.baeldung.datetime;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime; import java.time.LocalTime;
import java.time.Month; import java.time.Month;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class UseLocalDateTimeUnitTest { public class UseLocalDateTimeUnitTest {
UseLocalDateTime useLocalDateTime = new UseLocalDateTime(); UseLocalDateTime useLocalDateTime = new UseLocalDateTime();
@ -19,4 +21,16 @@ public class UseLocalDateTimeUnitTest {
assertEquals(LocalTime.of(6, 30), useLocalDateTime.getLocalDateTimeUsingParseMethod("2016-05-10T06:30") assertEquals(LocalTime.of(6, 30), useLocalDateTime.getLocalDateTimeUsingParseMethod("2016-05-10T06:30")
.toLocalTime()); .toLocalTime());
} }
@Test
public void givenLocalDateTime_whenSettingEndOfDay_thenReturnLastMomentOfDay() {
LocalDateTime givenTimed = LocalDateTime.parse("2018-06-23T05:55:55");
LocalDateTime endOfDayFromGivenDirectly = useLocalDateTime.getEndOfDayFromLocalDateTimeDirectly(givenTimed);
LocalDateTime endOfDayFromGiven = useLocalDateTime.getEndOfDayFromLocalDateTime(givenTimed);
assertThat(endOfDayFromGivenDirectly).isEqualTo(endOfDayFromGiven);
assertThat(endOfDayFromGivenDirectly.toLocalTime()).isEqualTo(LocalTime.MAX);
assertThat(endOfDayFromGivenDirectly.toString()).isEqualTo("2018-06-23T23:59:59.999999999");
}
} }

View File

@ -1,13 +1,15 @@
package com.baeldung.datetime; package com.baeldung.datetime;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import java.time.DayOfWeek; import java.time.DayOfWeek;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.LocalTime;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class UseLocalDateUnitTest { public class UseLocalDateUnitTest {
UseLocalDate useLocalDate = new UseLocalDate(); UseLocalDate useLocalDate = new UseLocalDate();
@ -57,4 +59,33 @@ public class UseLocalDateUnitTest {
assertEquals(LocalDateTime.parse("2016-05-22T00:00:00"), useLocalDate.getStartOfDay(LocalDate.parse("2016-05-22"))); assertEquals(LocalDateTime.parse("2016-05-22T00:00:00"), useLocalDate.getStartOfDay(LocalDate.parse("2016-05-22")));
} }
@Test
public void givenLocalDate_whenSettingStartOfDay_thenReturnMidnightInAllCases() {
LocalDate given = LocalDate.parse("2018-06-23");
LocalDateTime startOfDayWithMethod = useLocalDate.getStartOfDay(given);
LocalDateTime startOfDayOfLocalDate = useLocalDate.getStartOfDayOfLocalDate(given);
LocalDateTime startOfDayWithMin = useLocalDate.getStartOfDayAtMinTime(given);
LocalDateTime startOfDayWithMidnight = useLocalDate.getStartOfDayAtMidnightTime(given);
assertThat(startOfDayWithMethod).isEqualTo(startOfDayWithMin)
.isEqualTo(startOfDayWithMidnight)
.isEqualTo(startOfDayOfLocalDate)
.isEqualTo(LocalDateTime.parse("2018-06-23T00:00:00"));
assertThat(startOfDayWithMin.toLocalTime()).isEqualTo(LocalTime.MIDNIGHT);
assertThat(startOfDayWithMin.toString()).isEqualTo("2018-06-23T00:00");
}
@Test
public void givenLocalDate_whenSettingEndOfDay_thenReturnLastMomentOfDay() {
LocalDate given = LocalDate.parse("2018-06-23");
LocalDateTime endOfDayWithMax = useLocalDate.getEndOfDay(given);
LocalDateTime endOfDayFromLocalTime = useLocalDate.getEndOfDayFromLocalTime(given);
assertThat(endOfDayWithMax).isEqualTo(endOfDayFromLocalTime);
assertThat(endOfDayWithMax.toLocalTime()).isEqualTo(LocalTime.MAX);
assertThat(endOfDayWithMax.toString()).isEqualTo("2018-06-23T23:59:59.999999999");
}
} }

View File

@ -1,6 +1,10 @@
package com.baeldung.datetime; package com.baeldung.datetime;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
@ -17,4 +21,25 @@ public class UseZonedDateTimeUnitTest {
ZonedDateTime zonedDatetime = zonedDateTime.getZonedDateTime(LocalDateTime.parse("2016-05-20T06:30"), zoneId); ZonedDateTime zonedDatetime = zonedDateTime.getZonedDateTime(LocalDateTime.parse("2016-05-20T06:30"), zoneId);
Assert.assertEquals(zoneId, ZoneId.from(zonedDatetime)); Assert.assertEquals(zoneId, ZoneId.from(zonedDatetime));
} }
@Test
public void givenLocalDateOrZoned_whenSettingStartOfDay_thenReturnMidnightInAllCases() {
LocalDate given = LocalDate.parse("2018-06-23");
ZoneId zone = ZoneId.of("Europe/Paris");
ZonedDateTime zonedGiven = ZonedDateTime.of(given, LocalTime.NOON, zone);
ZonedDateTime startOfOfDayWithMethod = zonedDateTime.getStartOfDay(given, zone);
ZonedDateTime startOfOfDayWithShorthandMethod = zonedDateTime.getStartOfDayShorthand(given, zone);
ZonedDateTime startOfOfDayFromZonedDateTime = zonedDateTime.getStartOfDayFromZonedDateTime(zonedGiven);
ZonedDateTime startOfOfDayAtMinTime = zonedDateTime.getStartOfDayAtMinTime(zonedGiven);
ZonedDateTime startOfOfDayAtMidnightTime = zonedDateTime.getStartOfDayAtMidnightTime(zonedGiven);
assertThat(startOfOfDayWithMethod).isEqualTo(startOfOfDayWithShorthandMethod)
.isEqualTo(startOfOfDayFromZonedDateTime)
.isEqualTo(startOfOfDayAtMinTime)
.isEqualTo(startOfOfDayAtMidnightTime);
assertThat(startOfOfDayWithMethod.toLocalTime()).isEqualTo(LocalTime.MIDNIGHT);
assertThat(startOfOfDayWithMethod.toLocalTime()
.toString()).isEqualTo("00:00");
}
} }

View File

@ -203,6 +203,11 @@
<artifactId>mail</artifactId> <artifactId>mail</artifactId>
<version>1.5.0-b01</version> <version>1.5.0-b01</version>
</dependency> </dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>${icu4j.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -473,6 +478,7 @@
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version> <maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version> <exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
<javax.mail.version>1.5.0-b01</javax.mail.version> <javax.mail.version>1.5.0-b01</javax.mail.version>
<icu4j.version>61.1</icu4j.version>
</properties> </properties>
</project> </project>

View File

@ -0,0 +1,30 @@
package com.baeldung.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
public class DateWithoutTime {
public static Date getDateWithoutTimeUsingCalendar() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
public static Date getDateWithoutTimeUsingFormat() throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
return formatter.parse(formatter.format(new Date()));
}
public static LocalDate getLocalDate() {
return LocalDate.now();
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.datetime;
import java.util.Calendar;
import java.util.Date;
public class DateExtractYearMonthDayIntegerValues {
int getYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.YEAR);
}
int getMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH);
}
int getDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_MONTH);
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.datetime;
import java.time.LocalDate;
public class LocalDateExtractYearMonthDayIntegerValues {
int getYear(LocalDate localDate) {
return localDate.getYear();
}
int getMonth(LocalDate localDate) {
return localDate.getMonthValue();
}
int getDay(LocalDate localDate) {
return localDate.getDayOfMonth();
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.datetime;
import java.time.LocalDateTime;
public class LocalDateTimeExtractYearMonthDayIntegerValues {
int getYear(LocalDateTime localDateTime) {
return localDateTime.getYear();
}
int getMonth(LocalDateTime localDateTime) {
return localDateTime.getMonthValue();
}
int getDay(LocalDateTime localDateTime) {
return localDateTime.getDayOfMonth();
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.datetime;
import java.time.OffsetDateTime;
public class OffsetDateTimeExtractYearMonthDayIntegerValues {
int getYear(OffsetDateTime offsetDateTime) {
return offsetDateTime.getYear();
}
int getMonth(OffsetDateTime offsetDateTime) {
return offsetDateTime.getMonthValue();
}
int getDay(OffsetDateTime offsetDateTime) {
return offsetDateTime.getDayOfMonth();
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.datetime;
import java.time.ZonedDateTime;
public class ZonedDateTimeExtractYearMonthDayIntegerValues {
int getYear(ZonedDateTime zonedDateTime) {
return zonedDateTime.getYear();
}
int getMonth(ZonedDateTime zonedDateTime) {
return zonedDateTime.getMonthValue();
}
int getDay(ZonedDateTime zonedDateTime) {
return zonedDateTime.getDayOfMonth();
}
}

View File

@ -0,0 +1,68 @@
package com.baeldung.string;
import com.ibm.icu.lang.UCharacter;
import com.ibm.icu.text.BreakIterator;
import org.apache.commons.lang.WordUtils;
import java.util.Arrays;
import java.util.stream.Collectors;
public class TitleCaseConverter {
private static final String WORD_SEPARATOR = " ";
public static String convertToTitleCaseIteratingChars(String text) {
if (text == null || text.isEmpty()) {
return text;
}
StringBuilder converted = new StringBuilder();
boolean convertNext = true;
for (char ch : text.toCharArray()) {
if (Character.isSpaceChar(ch)) {
convertNext = true;
} else if (convertNext) {
ch = Character.toTitleCase(ch);
convertNext = false;
} else {
ch = Character.toLowerCase(ch);
}
converted.append(ch);
}
return converted.toString();
}
public static String convertToTitleCaseSplitting(String text) {
if (text == null || text.isEmpty()) {
return text;
}
return Arrays
.stream(text.split(WORD_SEPARATOR))
.map(word -> word.isEmpty()
? word
: Character.toTitleCase(word.charAt(0)) + word
.substring(1)
.toLowerCase())
.collect(Collectors.joining(WORD_SEPARATOR));
}
public static String convertToTitleCaseIcu4j(String text) {
if (text == null || text.isEmpty()) {
return text;
}
return UCharacter.toTitleCase(text, BreakIterator.getTitleInstance());
}
public static String convertToTileCaseWordUtilsFull(String text) {
return WordUtils.capitalizeFully(text);
}
public static String convertToTileCaseWordUtils(String text) {
return WordUtils.capitalize(text);
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,21 +1,29 @@
package com.baeldung.socket; 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.After;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import java.util.concurrent.Executors;
import static org.junit.Assert.assertEquals;
public class EchoIntegrationTest { public class EchoIntegrationTest {
private static final Integer PORT = 4444; private static int port;
@BeforeClass @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() Executors.newSingleThreadExecutor()
.submit(() -> new EchoServer().start(PORT)); .submit(() -> new EchoServer().start(port));
Thread.sleep(500); Thread.sleep(500);
} }
@ -23,7 +31,7 @@ public class EchoIntegrationTest {
@Before @Before
public void init() { public void init() {
client.startConnection("127.0.0.1", PORT); client.startConnection("127.0.0.1", port);
} }
@After @After

View File

@ -1,31 +1,39 @@
package com.baeldung.socket; 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.After;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import java.util.concurrent.Executors;
import static org.junit.Assert.assertEquals;
public class GreetServerIntegrationTest { public class GreetServerIntegrationTest {
private GreetClient client; private GreetClient client;
private static final Integer PORT = 6666; private static int port;
@BeforeClass @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() Executors.newSingleThreadExecutor()
.submit(() -> new GreetServer().start(PORT)); .submit(() -> new GreetServer().start(port));
Thread.sleep(500); Thread.sleep(500);
} }
@Before @Before
public void init() { public void init() {
client = new GreetClient(); client = new GreetClient();
client.startConnection("127.0.0.1", PORT); client.startConnection("127.0.0.1", port);
} }

View File

@ -1,26 +1,35 @@
package com.baeldung.socket; package com.baeldung.socket;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class SocketEchoMultiIntegrationTest { public class SocketEchoMultiIntegrationTest {
private static final Integer PORT = 5555; private static int port;
@BeforeClass @BeforeClass
public static void start() throws InterruptedException { public static void start() throws InterruptedException, IOException {
Executors.newSingleThreadExecutor().submit(() -> new EchoMultiServer().start(PORT));
// Take an available port
ServerSocket s = new ServerSocket(0);
port = s.getLocalPort();
s.close();
Executors.newSingleThreadExecutor().submit(() -> new EchoMultiServer().start(port));
Thread.sleep(500); Thread.sleep(500);
} }
@Test @Test
public void givenClient1_whenServerResponds_thenCorrect() { public void givenClient1_whenServerResponds_thenCorrect() {
EchoClient client = new EchoClient(); EchoClient client = new EchoClient();
client.startConnection("127.0.0.1", PORT); client.startConnection("127.0.0.1", port);
String msg1 = client.sendMessage("hello"); String msg1 = client.sendMessage("hello");
String msg2 = client.sendMessage("world"); String msg2 = client.sendMessage("world");
String terminate = client.sendMessage("."); String terminate = client.sendMessage(".");
@ -34,7 +43,7 @@ public class SocketEchoMultiIntegrationTest {
@Test @Test
public void givenClient2_whenServerResponds_thenCorrect() { public void givenClient2_whenServerResponds_thenCorrect() {
EchoClient client = new EchoClient(); EchoClient client = new EchoClient();
client.startConnection("127.0.0.1", PORT); client.startConnection("127.0.0.1", port);
String msg1 = client.sendMessage("hello"); String msg1 = client.sendMessage("hello");
String msg2 = client.sendMessage("world"); String msg2 = client.sendMessage("world");
String terminate = client.sendMessage("."); String terminate = client.sendMessage(".");
@ -47,7 +56,7 @@ public class SocketEchoMultiIntegrationTest {
@Test @Test
public void givenClient3_whenServerResponds_thenCorrect() { public void givenClient3_whenServerResponds_thenCorrect() {
EchoClient client = new EchoClient(); EchoClient client = new EchoClient();
client.startConnection("127.0.0.1", PORT); client.startConnection("127.0.0.1", port);
String msg1 = client.sendMessage("hello"); String msg1 = client.sendMessage("hello");
String msg2 = client.sendMessage("world"); String msg2 = client.sendMessage("world");
String terminate = client.sendMessage("."); String terminate = client.sendMessage(".");

View File

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

View File

@ -0,0 +1,115 @@
package com.baeldung.stringtemplates
/**
* Example of a useful function defined in Kotlin String class
*/
fun padExample(): String {
return "Hello".padEnd(10, '!')
}
/**
* Example of a simple string template usage
*/
fun simpleTemplate(n: Int): String {
val message = "n = $n"
return message
}
/**
* Example of a string template with a simple expression
*/
fun templateWithExpression(n: Int): String {
val message = "n + 1 = ${n + 1}"
return message
}
/**
* Example of a string template with expression containing some logic
*/
fun templateWithLogic(n: Int): String {
val message = "$n is ${if (n > 0) "positive" else "not positive"}"
return message
}
/**
* Example of nested string templates
*/
fun nestedTemplates(n: Int): String {
val message = "$n is ${if (n > 0) "positive" else if (n < 0) "negative and ${if (n % 2 == 0) "even" else "odd"}" else "zero"}"
return message
}
/**
* Example of joining array's element into a string with a default separator
*/
fun templateJoinArray(): String {
val numbers = listOf(1, 1, 2, 3, 5, 8)
val message = "first Fibonacci numbers: ${numbers.joinToString()}"
return message
}
/**
* Example of escaping the dollar sign
*/
fun notAStringTemplate(): String {
val message = "n = \$n"
return message
}
/**
* Example of a simple triple quoted string
*/
fun showFilePath(): String {
val path = """C:\Repository\read.me"""
return path
}
/**
* Example of a multiline string
*/
fun showMultiline(): String {
val receipt = """Item 1: $1.00
Item 2: $0.50"""
return receipt
}
/**
* Example of a multiline string with indentation
*/
fun showMultilineIndent(): String {
val receipt = """Item 1: $1.00
>Item 2: $0.50""".trimMargin(">")
return receipt
}
/**
* Example of a triple quoted string with a not-working escape sequence
*/
fun showTripleQuotedWrongEscape(): String {
val receipt = """Item 1: $1.00\nItem 2: $0.50"""
return receipt
}
/**
* Example of a triple quoted string with a correctly working escape sequence
*/
fun showTripleQuotedCorrectEscape(): String {
val receipt = """Item 1: $1.00${"\n"}Item 2: $0.50"""
return receipt
}
fun main(args: Array<String>) {
println(padExample())
println(simpleTemplate(10))
println(templateWithExpression(5))
println(templateWithLogic(7))
println(nestedTemplates(-5))
println(templateJoinArray())
println(notAStringTemplate())
println(showFilePath())
println(showMultiline())
println(showMultilineIndent())
println(showTripleQuotedWrongEscape())
println(showTripleQuotedCorrectEscape())
}

View File

@ -0,0 +1,32 @@
package com.baeldung.kotlin.reflection
import org.junit.Ignore
import org.junit.Test
import org.slf4j.LoggerFactory
@Ignore
class JavaReflectionTest {
private val LOG = LoggerFactory.getLogger(KClassTest::class.java)
@Test
fun listJavaClassMethods() {
Exception::class.java.methods
.forEach { method -> LOG.info("Method: {}", method) }
}
@Test
fun listKotlinClassMethods() {
JavaReflectionTest::class.java.methods
.forEach { method -> LOG.info("Method: {}", method) }
}
@Test
fun listKotlinDataClassMethods() {
data class ExampleDataClass(val name: String, var enabled: Boolean)
ExampleDataClass::class.java.methods
.forEach { method -> LOG.info("Method: {}", method) }
}
}

View File

@ -0,0 +1,69 @@
package com.baeldung.kotlin.reflection
import org.junit.Assert
import org.junit.Ignore
import org.junit.Test
import org.slf4j.LoggerFactory
import java.math.BigDecimal
import kotlin.reflect.full.*
class KClassTest {
private val LOG = LoggerFactory.getLogger(KClassTest::class.java)
@Test
fun testKClassDetails() {
val stringClass = String::class
Assert.assertEquals("kotlin.String", stringClass.qualifiedName)
Assert.assertFalse(stringClass.isData)
Assert.assertFalse(stringClass.isCompanion)
Assert.assertFalse(stringClass.isAbstract)
Assert.assertTrue(stringClass.isFinal)
Assert.assertFalse(stringClass.isSealed)
val listClass = List::class
Assert.assertEquals("kotlin.collections.List", listClass.qualifiedName)
Assert.assertFalse(listClass.isData)
Assert.assertFalse(listClass.isCompanion)
Assert.assertTrue(listClass.isAbstract)
Assert.assertFalse(listClass.isFinal)
Assert.assertFalse(listClass.isSealed)
}
@Test
fun testGetRelated() {
LOG.info("Companion Object: {}", TestSubject::class.companionObject)
LOG.info("Companion Object Instance: {}", TestSubject::class.companionObjectInstance)
LOG.info("Object Instance: {}", TestObject::class.objectInstance)
Assert.assertSame(TestObject, TestObject::class.objectInstance)
}
@Test
fun testNewInstance() {
val listClass = ArrayList::class
val list = listClass.createInstance()
Assert.assertTrue(list is ArrayList)
}
@Test
@Ignore
fun testMembers() {
val bigDecimalClass = BigDecimal::class
LOG.info("Constructors: {}", bigDecimalClass.constructors)
LOG.info("Functions: {}", bigDecimalClass.functions)
LOG.info("Properties: {}", bigDecimalClass.memberProperties)
LOG.info("Extension Functions: {}", bigDecimalClass.memberExtensionFunctions)
}
}
class TestSubject {
companion object {
val name = "TestSubject"
}
}
object TestObject {
val answer = 42
}

View File

@ -0,0 +1,88 @@
package com.baeldung.kotlin.reflection
import org.junit.Assert
import org.junit.Test
import java.io.ByteArrayInputStream
import java.nio.charset.Charset
import kotlin.reflect.KMutableProperty
import kotlin.reflect.full.starProjectedType
class KMethodTest {
@Test
fun testCallMethod() {
val str = "Hello"
val lengthMethod = str::length
Assert.assertEquals(5, lengthMethod())
}
@Test
fun testReturnType() {
val str = "Hello"
val method = str::byteInputStream
Assert.assertEquals(ByteArrayInputStream::class.starProjectedType, method.returnType)
Assert.assertFalse(method.returnType.isMarkedNullable)
}
@Test
fun testParams() {
val str = "Hello"
val method = str::byteInputStream
method.isSuspend
Assert.assertEquals(1, method.parameters.size)
Assert.assertTrue(method.parameters[0].isOptional)
Assert.assertFalse(method.parameters[0].isVararg)
Assert.assertEquals(Charset::class.starProjectedType, method.parameters[0].type)
}
@Test
fun testMethodDetails() {
val codePoints = String::codePoints
Assert.assertEquals("codePoints", codePoints.name)
Assert.assertFalse(codePoints.isSuspend)
Assert.assertFalse(codePoints.isExternal)
Assert.assertFalse(codePoints.isInline)
Assert.assertFalse(codePoints.isOperator)
val byteInputStream = String::byteInputStream
Assert.assertEquals("byteInputStream", byteInputStream.name)
Assert.assertFalse(byteInputStream.isSuspend)
Assert.assertFalse(byteInputStream.isExternal)
Assert.assertTrue(byteInputStream.isInline)
Assert.assertFalse(byteInputStream.isOperator)
}
val readOnlyProperty: Int = 42
lateinit var mutableProperty: String
@Test
fun testPropertyDetails() {
val roProperty = this::readOnlyProperty
Assert.assertEquals("readOnlyProperty", roProperty.name)
Assert.assertFalse(roProperty.isLateinit)
Assert.assertFalse(roProperty.isConst)
Assert.assertFalse(roProperty is KMutableProperty<*>)
val mProperty = this::mutableProperty
Assert.assertEquals("mutableProperty", mProperty.name)
Assert.assertTrue(mProperty.isLateinit)
Assert.assertFalse(mProperty.isConst)
Assert.assertTrue(mProperty is KMutableProperty<*>)
}
@Test
fun testProperty() {
val prop = this::mutableProperty
Assert.assertEquals(String::class.starProjectedType, prop.getter.returnType)
prop.set("Hello")
Assert.assertEquals("Hello", prop.get())
prop.setter("World")
Assert.assertEquals("World", prop.getter())
}
}

View File

@ -37,7 +37,7 @@ import java.util.logging.Logger;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
@RunWith(Arquillian.class) @RunWith(Arquillian.class)
public class MemberRegistrationIntegrationTest { public class MemberRegistrationLiveTest {
@Deployment @Deployment
public static Archive<?> createTestArchive() { public static Archive<?> createTestArchive() {
File[] files = Maven File[] files = Maven

View File

@ -5,7 +5,7 @@ version '1.0-SNAPSHOT'
buildscript { buildscript {
ext.kotlin_version = '1.2.40' ext.kotlin_version = '1.2.41'
ext.ktor_version = '0.9.2' ext.ktor_version = '0.9.2'
repositories { repositories {

View File

@ -1,26 +1,26 @@
@file:JvmName("APIServer") @file:JvmName("APIServer")
import io.ktor.application.call import io.ktor.application.call
import io.ktor.application.install import io.ktor.application.install
import io.ktor.features.CallLogging import io.ktor.features.CallLogging
import io.ktor.features.ContentNegotiation import io.ktor.features.ContentNegotiation
import io.ktor.features.DefaultHeaders import io.ktor.features.DefaultHeaders
import io.ktor.gson.gson import io.ktor.gson.gson
import io.ktor.http.ContentType
import io.ktor.request.path import io.ktor.request.path
import io.ktor.request.receive
import io.ktor.response.respond import io.ktor.response.respond
import io.ktor.response.respondText import io.ktor.routing.*
import io.ktor.routing.get
import io.ktor.routing.routing
import io.ktor.server.engine.embeddedServer import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty import io.ktor.server.netty.Netty
import org.slf4j.event.Level import org.slf4j.event.Level
data class Author(val name: String, val website: String) data class Author(val name: String, val website: String)
data class ToDo(var id: Int, val name: String, val description: String, val completed: Boolean)
fun main(args: Array<String>) { fun main(args: Array<String>) {
val toDoList = ArrayList<ToDo>();
val jsonResponse = """{ val jsonResponse = """{
"id": 1, "id": 1,
"task": "Pay waterbill", "task": "Pay waterbill",
@ -42,15 +42,31 @@ fun main(args: Array<String>) {
setPrettyPrinting() setPrettyPrinting()
} }
} }
routing { routing() {
get("/todo") { route("/todo") {
call.respondText(jsonResponse, ContentType.Application.Json) post {
} var toDo = call.receive<ToDo>();
get("/author") { toDo.id = toDoList.size;
val author = Author("baeldung", "baeldung.com") toDoList.add(toDo);
call.respond(author) call.respond("Added")
}
delete("/{id}") {
call.respond(toDoList.removeAt(call.parameters["id"]!!.toInt()));
}
get("/{id}") {
call.respond(toDoList[call.parameters["id"]!!.toInt()]);
}
get {
call.respond(toDoList);
}
} }
get("/author"){
call.respond(Author("Baeldung","baeldung.com"));
}
} }
}.start(wait = true) }.start(wait = true)

View File

@ -1,17 +1,18 @@
package com.baeldung.jdo; package com.baeldung.jdo;
import org.datanucleus.api.jdo.JDOPersistenceManagerFactory; import static org.junit.Assert.assertEquals;
import org.datanucleus.metadata.PersistenceUnitMetaData; import static org.junit.Assert.fail;
import org.junit.Test;
import java.util.List;
import javax.jdo.PersistenceManager; import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory; import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query; import javax.jdo.Query;
import javax.jdo.Transaction; import javax.jdo.Transaction;
import java.util.List;
import static org.junit.Assert.assertEquals; import org.datanucleus.api.jdo.JDOPersistenceManagerFactory;
import static org.junit.Assert.fail; import org.datanucleus.metadata.PersistenceUnitMetaData;
import org.junit.Test;
public class GuideToJDOIntegrationTest { public class GuideToJDOIntegrationTest {
@Test @Test
@ -24,6 +25,7 @@ public class GuideToJDOIntegrationTest {
pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa"); pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa");
pumd.addProperty("javax.jdo.option.ConnectionPassword", ""); pumd.addProperty("javax.jdo.option.ConnectionPassword", "");
pumd.addProperty("datanucleus.autoCreateSchema", "true"); pumd.addProperty("datanucleus.autoCreateSchema", "true");
pumd.addProperty("datanucleus.schema.autoCreateTables", "true");
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null); PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
PersistenceManager pm = pmf.getPersistenceManager(); PersistenceManager pm = pmf.getPersistenceManager();
@ -58,6 +60,7 @@ public class GuideToJDOIntegrationTest {
pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa"); pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa");
pumd.addProperty("javax.jdo.option.ConnectionPassword", ""); pumd.addProperty("javax.jdo.option.ConnectionPassword", "");
pumd.addProperty("datanucleus.autoCreateSchema", "true"); pumd.addProperty("datanucleus.autoCreateSchema", "true");
pumd.addProperty("datanucleus.schema.autoCreateTables", "true");
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null); PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
PersistenceManager pm = pmf.getPersistenceManager(); PersistenceManager pm = pmf.getPersistenceManager();

View File

@ -60,6 +60,7 @@
<h2.version>1.4.193</h2.version> <h2.version>1.4.193</h2.version>
<commons-dbcp2.version>2.1.1</commons-dbcp2.version> <commons-dbcp2.version>2.1.1</commons-dbcp2.version>
<log4j-core.version>2.11.0</log4j-core.version> <log4j-core.version>2.11.0</log4j-core.version>
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
</properties> </properties>
<build> <build>
@ -74,4 +75,42 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<profiles>
<profile>
<id>integration</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>**/*ManualTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
<include>**/*IntTest.java</include>
</includes>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<test.mime>json</test.mime>
<logging.folder.path>${java.io.tmpdir}/${maven.build.timestamp}/logfile.json</logging.folder.path>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project> </project>

View File

@ -21,9 +21,12 @@ import com.baeldung.logging.log4j2.tests.jdbc.ConnectionFactory;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class CustomLoggingIntegrationTest { public class CustomLoggingIntegrationTest {
private static String logFilePath = System.getProperty("logging.folder.path");
@BeforeClass @BeforeClass
public static void setup() throws Exception { public static void setup() throws Exception {
Connection connection = ConnectionFactory.getConnection(); Connection connection = ConnectionFactory.getConnection();
connection.createStatement() connection.createStatement()
.execute("CREATE TABLE logs(" + "when TIMESTAMP," + "logger VARCHAR(255)," + "level VARCHAR(255)," + "message VARCHAR(4096)," + "throwable TEXT)"); .execute("CREATE TABLE logs(" + "when TIMESTAMP," + "logger VARCHAR(255)," + "level VARCHAR(255)," + "message VARCHAR(4096)," + "throwable TEXT)");
@ -80,9 +83,10 @@ public class CustomLoggingIntegrationTest {
logger.info("This is async JSON message #{} at INFO level.", count); logger.info("This is async JSON message #{} at INFO level.", count);
} }
long logEventsCount = Files.lines(Paths.get("target/logfile.json")) long logEventsCount = Files.lines(Paths.get(logFilePath))
.count(); .count();
assertTrue(logEventsCount > 0 && logEventsCount <= count);
assertTrue(logEventsCount >= 0 && logEventsCount <= count);
} }
@Test @Test
@ -114,7 +118,7 @@ public class CustomLoggingIntegrationTest {
if (resultSet.next()) { if (resultSet.next()) {
logCount = resultSet.getInt("ROW_COUNT"); logCount = resultSet.getInt("ROW_COUNT");
} }
assertTrue(logCount == count); assertTrue(logCount <= count);
} }
@Test @Test

View File

@ -6,13 +6,12 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import com.baeldung.logging.log4j2.Log4j2BaseIntegrationTest;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import com.baeldung.logging.log4j2.Log4j2BaseIntegrationTest;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
public class JSONLayoutIntegrationTest extends Log4j2BaseIntegrationTest { public class JSONLayoutIntegrationTest extends Log4j2BaseIntegrationTest {
@ -32,7 +31,8 @@ public class JSONLayoutIntegrationTest extends Log4j2BaseIntegrationTest {
public void whenLogLayoutInJSON_thenOutputIsCorrectJSON() { public void whenLogLayoutInJSON_thenOutputIsCorrectJSON() {
logger.debug("Debug message"); logger.debug("Debug message");
String currentLog = consoleOutput.toString(); String currentLog = consoleOutput.toString();
assertTrue(!currentLog.isEmpty() && isValidJSON(currentLog)); assertTrue(currentLog.isEmpty());
assertTrue(isValidJSON(currentLog));
} }
public static boolean isValidJSON(String jsonInString) { public static boolean isValidJSON(String jsonInString) {

View File

@ -21,7 +21,7 @@
<KeyValuePair key="myCustomField" value="myCustomValue" /> <KeyValuePair key="myCustomField" value="myCustomValue" />
</JsonLayout> </JsonLayout>
</Console> </Console>
<File name="JSONLogfileAppender" fileName="target/logfile.json"> <File name="JSONLogfileAppender" fileName="${sys:logging.folder.path}">
<JSONLayout compact="true" eventEol="true" /> <JSONLayout compact="true" eventEol="true" />
<BurstFilter level="INFO" rate="2" maxBurst="10" /> <BurstFilter level="INFO" rate="2" maxBurst="10" />
</File> </File>

View File

@ -0,0 +1,107 @@
package com.baeldung.interpreter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Context {
private static Map<String, List<Row>> tables = new HashMap<>();
static {
List<Row> list = new ArrayList<>();
list.add(new Row("John", "Doe"));
list.add(new Row("Jan", "Kowalski"));
list.add(new Row("Dominic", "Doom"));
tables.put("people", list);
}
private String table;
private String column;
/**
* Index of column to be shown in result.
* Calculated in {@link #setColumnMapper()}
*/
private int colIndex = -1;
/**
* Default setup, used for clearing the context for next queries.
* See {@link Context#clear()}
*/
private static final Predicate<String> matchAnyString = s -> s.length() > 0;
private static final Function<String, Stream<? extends String>> matchAllColumns = Stream::of;
/**
* Varies based on setup in subclasses of {@link Expression}
*/
private Predicate<String> whereFilter = matchAnyString;
private Function<String, Stream<? extends String>> columnMapper = matchAllColumns;
void setColumn(String column) {
this.column = column;
setColumnMapper();
}
void setTable(String table) {
this.table = table;
}
void setFilter(Predicate<String> filter) {
whereFilter = filter;
}
/**
* Clears the context to defaults.
* No filters, match all columns.
*/
void clear() {
column = "";
columnMapper = matchAllColumns;
whereFilter = matchAnyString;
}
List<String> search() {
List<String> result = tables.entrySet()
.stream()
.filter(entry -> entry.getKey().equalsIgnoreCase(table))
.flatMap(entry -> Stream.of(entry.getValue()))
.flatMap(Collection::stream)
.map(Row::toString)
.flatMap(columnMapper)
.filter(whereFilter)
.collect(Collectors.toList());
clear();
return result;
}
/**
* Sets column mapper based on {@link #column} attribute.
* Note: If column is unknown, will remain to look for all columns.
*/
private void setColumnMapper() {
switch (column) {
case "*":
colIndex = -1;
break;
case "name":
colIndex = 0;
break;
case "surname":
colIndex = 1;
break;
}
if (colIndex != -1) {
columnMapper = s -> Stream.of(s.split(" ")[colIndex]);
}
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.interpreter;
import java.util.List;
interface Expression {
List<String> interpret(Context ctx);
}

View File

@ -0,0 +1,27 @@
package com.baeldung.interpreter;
import java.util.List;
class From implements Expression {
private String table;
private Where where;
From(String table) {
this.table = table;
}
From(String table, Where where) {
this.table = table;
this.where = where;
}
@Override
public List<String> interpret(Context ctx) {
ctx.setTable(table);
if (where == null) {
return ctx.search();
}
return where.interpret(ctx);
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.interpreter;
import java.util.List;
public class InterpreterDemo {
public static void main(String[] args) {
Expression query = new Select("name", new From("people"));
Context ctx = new Context();
List<String> result = query.interpret(ctx);
System.out.println(result);
Expression query2 = new Select("*", new From("people"));
List<String> result2 = query2.interpret(ctx);
System.out.println(result2);
Expression query3 = new Select("name", new From("people", new Where(name -> name.toLowerCase().startsWith("d"))));
List<String> result3 = query3.interpret(ctx);
System.out.println(result3);
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.interpreter;
class Row {
private String name;
private String surname;
Row(String name, String surname) {
this.name = name;
this.surname = surname;
}
@Override
public String toString() {
return name + " " + surname;
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.interpreter;
import java.util.List;
class Select implements Expression {
private String column;
private From from;
Select(String column, From from) {
this.column = column;
this.from = from;
}
@Override
public List<String> interpret(Context ctx) {
ctx.setColumn(column);
return from.interpret(ctx);
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.interpreter;
import java.util.List;
import java.util.function.Predicate;
class Where implements Expression {
private Predicate<String> filter;
Where(Predicate<String> filter) {
this.filter = filter;
}
@Override
public List<String> interpret(Context ctx) {
ctx.setFilter(filter);
return ctx.search();
}
}

View File

@ -1,28 +1,45 @@
package com.baeldung; package com.baeldung;
import org.junit.*;
import redis.clients.jedis.*;
import redis.embedded.RedisServer;
import java.io.IOException; import java.io.IOException;
import java.net.ServerSocket;
import java.time.Duration; import java.time.Duration;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;
import redis.clients.jedis.Transaction;
import redis.embedded.RedisServer;
public class JedisIntegrationTest { public class JedisIntegrationTest {
private Jedis jedis; private static Jedis jedis;
private static RedisServer redisServer; private static RedisServer redisServer;
private static int port;
public JedisIntegrationTest() {
jedis = new Jedis();
}
@BeforeClass @BeforeClass
public static void setUp() throws IOException { public static void setUp() throws IOException {
redisServer = new RedisServer(6379);
// Take an available port
ServerSocket s = new ServerSocket(0);
port = s.getLocalPort();
s.close();
redisServer = new RedisServer(port);
redisServer.start(); redisServer.start();
// Configure JEDIS
jedis = new Jedis("localhost", port);
} }
@AfterClass @AfterClass
@ -178,7 +195,7 @@ public class JedisIntegrationTest {
public void givenAPoolConfiguration_thenCreateAJedisPool() { public void givenAPoolConfiguration_thenCreateAJedisPool() {
final JedisPoolConfig poolConfig = buildPoolConfig(); final JedisPoolConfig poolConfig = buildPoolConfig();
try (JedisPool jedisPool = new JedisPool(poolConfig, "localhost"); Jedis jedis = jedisPool.getResource()) { try (JedisPool jedisPool = new JedisPool(poolConfig, "localhost", port); Jedis jedis = jedisPool.getResource()) {
// do simple operation to verify that the Jedis resource is working // do simple operation to verify that the Jedis resource is working
// properly // properly

View File

@ -1,15 +1,20 @@
package com.baeldung; package com.baeldung;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.nio.charset.Charset;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.redisson.Redisson; import org.redisson.Redisson;
import org.redisson.api.RedissonClient; import org.redisson.api.RedissonClient;
import org.redisson.config.Config; import org.redisson.config.Config;
import redis.embedded.RedisServer;
import java.io.File; import com.google.common.io.Files;
import java.io.IOException;
import redis.embedded.RedisServer;
/** /**
* Created by johnson on 3/9/17. * Created by johnson on 3/9/17.
@ -17,10 +22,17 @@ import java.io.IOException;
public class RedissonConfigurationIntegrationTest { public class RedissonConfigurationIntegrationTest {
private static RedisServer redisServer; private static RedisServer redisServer;
private static RedissonClient client; private static RedissonClient client;
private static int port;
@BeforeClass @BeforeClass
public static void setUp() throws IOException { public static void setUp() throws IOException {
redisServer = new RedisServer(6379);
// Take an available port
ServerSocket s = new ServerSocket(0);
port = s.getLocalPort();
s.close();
redisServer = new RedisServer(port);
redisServer.start(); redisServer.start();
} }
@ -36,7 +48,7 @@ public class RedissonConfigurationIntegrationTest {
public void givenJavaConfig_thenRedissonConnectToRedis() { public void givenJavaConfig_thenRedissonConnectToRedis() {
Config config = new Config(); Config config = new Config();
config.useSingleServer() config.useSingleServer()
.setAddress("127.0.0.1:6379"); .setAddress(String.format("127.0.0.1:%s", port));
client = Redisson.create(config); client = Redisson.create(config);
@ -45,10 +57,11 @@ public class RedissonConfigurationIntegrationTest {
@Test @Test
public void givenJSONFileConfig_thenRedissonConnectToRedis() throws IOException { public void givenJSONFileConfig_thenRedissonConnectToRedis() throws IOException {
Config config = Config.fromJSON(
new File(getClass().getClassLoader().getResource( File configFile = new File(getClass().getClassLoader().getResource("singleNodeConfig.json").getFile());
"singleNodeConfig.json").getFile())); String configContent = Files.toString(configFile, Charset.defaultCharset()).replace("6379", String.valueOf(port));
Config config = Config.fromJSON(configContent);
client = Redisson.create(config); client = Redisson.create(config);
assert(client != null && client.getKeys().count() >= 0); assert(client != null && client.getKeys().count() >= 0);
@ -56,10 +69,11 @@ public class RedissonConfigurationIntegrationTest {
@Test @Test
public void givenYAMLFileConfig_thenRedissonConnectToRedis() throws IOException { public void givenYAMLFileConfig_thenRedissonConnectToRedis() throws IOException {
Config config = Config.fromYAML(
new File(getClass().getClassLoader().getResource( File configFile = new File(getClass().getClassLoader().getResource("singleNodeConfig.yaml").getFile());
"singleNodeConfig.yaml").getFile())); String configContent = Files.toString(configFile, Charset.defaultCharset()).replace("6379", String.valueOf(port));
Config config = Config.fromYAML(configContent);
client = Redisson.create(config); client = Redisson.create(config);
assert(client != null && client.getKeys().count() >= 0); assert(client != null && client.getKeys().count() >= 0);

View File

@ -266,8 +266,9 @@
<module>twilio</module> <module>twilio</module>
<module>spring-boot-ctx-fluent</module> <module>spring-boot-ctx-fluent</module>
<module>java-ee-8-security-api</module> <module>java-ee-8-security-api</module>
<module>spring-webflux-amqp</module> <module>spring-webflux-amqp</module>
<module>maven-archetype</module> <module>antlr</module>
<module>maven-archetype</module>
</modules> </modules>
<dependencies> <dependencies>

View File

@ -0,0 +1,38 @@
package com.baeldung.springbootnonwebapp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 1. Act as main class for spring boot application
* 2. Also implements CommandLineRunner, so that code within run method
* is executed before application startup but after all beans are effectively created
* @author hemant
*
*/
@SpringBootApplication
public class SpringBootConsoleApplication implements CommandLineRunner {
private static Logger LOG = LoggerFactory.getLogger(SpringBootConsoleApplication.class);
public static void main(String[] args) {
LOG.info("STARTING THE APPLICATION");
SpringApplication.run(SpringBootConsoleApplication.class, args);
LOG.info("APPLICATION FINISHED");
}
/**
* This method will be executed after the application context is loaded and
* right before the Spring Application main method is completed.
*/
@Override
public void run(String... args) throws Exception {
LOG.info("EXECUTING : command line runner");
for (int i = 0; i < args.length; ++i) {
LOG.info("args[{}]: {}", i, args[i]);
}
}
}

View File

@ -1,4 +1,4 @@
package org.baeldung.model; package org.baeldung.boot.domain;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;

View File

@ -1,6 +1,6 @@
package org.baeldung.repository; package org.baeldung.boot.repository;
import org.baeldung.model.User; import org.baeldung.boot.domain.User;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;

View File

@ -3,7 +3,7 @@ package org.baeldung.endpoints.info;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.baeldung.repository.UserRepository; import org.baeldung.boot.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.info.Info; import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor; import org.springframework.boot.actuate.info.InfoContributor;

View File

@ -0,0 +1,30 @@
package org.baeldung.boot.repository;
import org.baeldung.boot.domain.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Collection;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Created by adam.
*/
@RunWith(SpringRunner.class)
@DataJpaTest
public class UserRepositoryDataJpaIntegrationTest {
@Autowired private UserRepository userRepository;
@Test
public void givenTwoImportFilesWhenFindAllShouldReturnSixUsers() {
Collection<User> users = userRepository.findAll();
assertThat(users.size()).isEqualTo(6);
}
}

View File

@ -1,7 +1,7 @@
package org.baeldung.repository; package org.baeldung.boot.repository;
import org.baeldung.boot.config.H2JpaConfig; import org.baeldung.boot.config.H2JpaConfig;
import org.baeldung.model.User; import org.baeldung.boot.domain.User;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -27,8 +27,7 @@ public class UserRepositoryIntegrationTest {
private final String USER_NAME_ADAM = "Adam"; private final String USER_NAME_ADAM = "Adam";
private final Integer ACTIVE_STATUS = 1; private final Integer ACTIVE_STATUS = 1;
@Autowired @Autowired private UserRepository userRepository;
private UserRepository userRepository;
@Test @Test
public void givenEmptyDBWhenFindOneByNameThenReturnEmptyOptional() { public void givenEmptyDBWhenFindOneByNameThenReturnEmptyOptional() {

View File

@ -2,3 +2,5 @@ spring.datasource.url=jdbc:mysql://localhost:3306/employee_int_test
spring.datasource.username=root spring.datasource.username=root
spring.datasource.password=root spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.datasource.data=import_*_users.sql

View File

@ -16,4 +16,6 @@ hibernate.show_sql=true
hibernate.hbm2ddl.auto=create-drop hibernate.hbm2ddl.auto=create-drop
hibernate.cache.use_second_level_cache=true hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true hibernate.cache.use_query_cache=true
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
spring.jpa.properties.hibernate.hbm2ddl.import_files=import_active_users.sql,import_inactive_users.sql

View File

@ -0,0 +1,3 @@
insert into USERS(name, status, id) values('Peter', 1, 1);
insert into USERS(name, status, id) values('David', 1, 2);
insert into USERS(name, status, id) values('Ed', 1, 3);

View File

@ -0,0 +1,3 @@
insert into users(name, status, id) values('Monica', 0, 4);
insert into users(name, status, id) values('Paul', 0, 5);
insert into users(name, status, id) values('George', 0, 6);

View File

@ -98,6 +98,13 @@
<version>${springframework-security.version}</version> <version>${springframework-security.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- Spring data -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>${springFramework-data.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -117,7 +124,7 @@
<configuration> <configuration>
<wait>true</wait> <wait>true</wait>
<container> <container>
<containerId>jetty8x</containerId> <containerId>jetty9x</containerId>
<type>embedded</type> <type>embedded</type>
<systemProperties> <systemProperties>
</systemProperties> </systemProperties>
@ -157,6 +164,7 @@
<!-- spring --> <!-- spring -->
<org.springframework-version>4.3.4.RELEASE</org.springframework-version> <org.springframework-version>4.3.4.RELEASE</org.springframework-version>
<springframework-security.version>4.2.0.RELEASE</springframework-security.version> <springframework-security.version>4.2.0.RELEASE</springframework-security.version>
<springFramework-data.version>2.0.7.RELEASE</springFramework-data.version>
<javax.servlet-version>3.1.0</javax.servlet-version> <javax.servlet-version>3.1.0</javax.servlet-version>
<!-- thymeleaf --> <!-- thymeleaf -->
<org.thymeleaf-version>3.0.9.RELEASE</org.thymeleaf-version> <org.thymeleaf-version>3.0.9.RELEASE</org.thymeleaf-version>

View File

@ -5,6 +5,10 @@ import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -12,8 +16,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import com.baeldung.thymeleaf.model.Book; import com.baeldung.thymeleaf.model.Book;
import com.baeldung.thymeleaf.model.Page; import com.baeldung.thymeleaf.service.BookService;
import com.baeldung.thymeleaf.utils.BookUtils;
@Controller @Controller
public class BookController { public class BookController {
@ -21,22 +24,20 @@ public class BookController {
private static int currentPage = 1; private static int currentPage = 1;
private static int pageSize = 5; private static int pageSize = 5;
@Autowired
private BookService bookService;
@RequestMapping(value = "/listBooks", method = RequestMethod.GET) @RequestMapping(value = "/listBooks", method = RequestMethod.GET)
public String listBooks(Model model, @RequestParam("page") Optional<Integer> page, @RequestParam("size") Optional<Integer> size) { public String listBooks(Model model, @RequestParam("page") Optional<Integer> page, @RequestParam("size") Optional<Integer> size) {
page.ifPresent(p -> currentPage = p); page.ifPresent(p -> currentPage = p);
size.ifPresent(s -> pageSize = s); size.ifPresent(s -> pageSize = s);
List<Book> books = BookUtils.buildBooks(); Page<Book> bookPage = bookService.findPaginated(PageRequest.of(currentPage - 1, pageSize));
Page<Book> bookPage = new Page<Book>(books, pageSize, currentPage);
model.addAttribute("books", bookPage.getList()); model.addAttribute("bookPage", bookPage);
model.addAttribute("selectedPage", bookPage.getCurrentPage());
model.addAttribute("pageSize", pageSize);
int totalPages = bookPage.getTotalPages(); int totalPages = bookPage.getTotalPages();
model.addAttribute("totalPages", totalPages); if (totalPages > 0) {
if (totalPages > 1) {
List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages) List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages)
.boxed() .boxed()
.collect(Collectors.toList()); .collect(Collectors.toList());

View File

@ -0,0 +1,38 @@
package com.baeldung.thymeleaf.service;
import java.util.Collections;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.baeldung.thymeleaf.model.Book;
import com.baeldung.thymeleaf.utils.BookUtils;
@Service
public class BookService {
final private List<Book> books = BookUtils.buildBooks();
public Page<Book> findPaginated(Pageable pageable) {
int pageSize = pageable.getPageSize();
int currentPage = pageable.getPageNumber();
int startItem = currentPage * pageSize;
List<Book> list;
if (books.size() < startItem) {
list = Collections.emptyList();
} else {
int toIndex = Math.min(startItem + pageSize, books.size());
list = books.subList(startItem, toIndex);
}
Page<Book> bookPage = new PageImpl<Book>(list, PageRequest.of(currentPage, pageSize), books.size());
return bookPage;
}
}

View File

@ -32,7 +32,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr th:each="book, iStat : ${books}" <tr th:each="book, iStat : ${bookPage.content}"
th:style="${iStat.odd}? 'font-weight: bold;'" th:style="${iStat.odd}? 'font-weight: bold;'"
th:alt-title="${iStat.even}? 'even' : 'odd'"> th:alt-title="${iStat.even}? 'even' : 'odd'">
<td th:text="${book.id}" /> <td th:text="${book.id}" />
@ -40,11 +40,11 @@
</tr> </tr>
</tbody> </tbody>
</table> </table>
<div th:if="${totalPages > 1}" class="pagination" <div th:if="${bookPage.totalPages > 0}" class="pagination"
th:each="pageNumber : ${pageNumbers}"> th:each="pageNumber : ${pageNumbers}">
<a th:href="@{/listBooks(size=${pageSize}, page=${pageNumber})}" <a th:href="@{/listBooks(size=${bookPage.size}, page=${pageNumber})}"
th:text=${pageNumber} th:text=${pageNumber}
th:class="${pageNumber==selectedPage} ? active"></a> th:class="${pageNumber==bookPage.number + 1} ? active"></a>
</div> </div>
<div> <div>