This commit is contained in:
nnhai1991@gmail.com 2018-09-24 19:58:49 +08:00
commit ed510080a9
298 changed files with 7237 additions and 731 deletions

View File

@ -1,13 +1,9 @@
<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>
<groupId>com.mabsisa</groupId>
<artifactId>Twitter4J</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Twitter4J</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.baeldung</groupId>
@ -23,27 +19,6 @@
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<excludes>
<exclude>**/ApplicationTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<twitter4j-stream.version>4.0.6</twitter4j-stream.version>
</properties>

View File

@ -1,7 +1,6 @@
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>activejdbc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
@ -79,55 +78,11 @@
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<reportFormat>brief</reportFormat>
<trimStackTrace>true</trimStackTrace>
<useFile>false</useFile>
<includes>
<include>**/*Spec*.java</include>
<include>**/*Test*.java</include>
</includes>
<excludes>
<exclude>**/helpers/*</exclude>
<exclude>**/*$*</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>snapshots1</id>
<name>JavaLite Snapshots1</name>
<url>http://repo.javalite.io/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>snapshots2</id>
<name>JavaLite Snapshots2</name>
<url>http://repo.javalite.io/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
<properties>
<activejdbc.version>1.4.13</activejdbc.version>
<activejdbc.version>2.0</activejdbc.version>
<environments>development.test,development</environments>
<mysql.connector.version>5.1.34</mysql.connector.version>
</properties>

View File

@ -16,4 +16,8 @@ public class Employee extends Model {
set("created_by",createdBy);
}
public String getLastName() {
return getString("last_name");
}
}

View File

@ -15,4 +15,8 @@ public class Role extends Model {
set("role_name",role);
set("created_by",createdBy);
}
public String getRoleName() {
return getString("role_name");
}
}

View File

@ -7,7 +7,7 @@ import org.junit.Test;
import java.util.List;
public class ActiveJDBCAppTest extends DBSpec
public class ActiveJDBCAppManualTest extends DBSpec
{
@Test
public void ifEmployeeCreated_thenIsValid() {

View File

@ -0,0 +1 @@
/bin/

View File

@ -0,0 +1,20 @@
package com.java.src;
import java.util.Scanner;
public class RoundUpToHundred {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double input = scanner.nextDouble();
scanner.close();
RoundUpToHundred.round(input);
}
static long round(double input) {
long i = (long) Math.ceil(input);
return ((i + 99) / 100) * 100;
};
}

View File

@ -0,0 +1,14 @@
package com.java.src;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class RoundUpToHundredTest {
@Test
public void givenInput_whenRound_thenRoundUpToTheNearestHundred() {
assertEquals("Rounded up to hundred", 100, RoundUpToHundred.round(99));
assertEquals("Rounded up to three hundred ", 300, RoundUpToHundred.round(200.2));
assertEquals("Returns same rounded value", 400, RoundUpToHundred.round(400));
}
}

View File

@ -3,7 +3,6 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>apache-shiro</artifactId>
<version>1.0-SNAPSHOT</version>
@ -36,13 +35,11 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
@ -53,26 +50,9 @@
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<apache-shiro-core-version>1.4.0</apache-shiro-core-version>
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
<java.version>1.8</java.version>
<log4j-version>1.2.17</log4j-version>
<slf4j-version>1.7.25</slf4j-version>
</properties>
</project>

View File

@ -42,14 +42,6 @@
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<argLine>-javaagent:"C:\asm-1.0.jar"</argLine>
</configuration>
</plugin>
</plugins>
</build>

View File

@ -2,12 +2,10 @@
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>cas-secured-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cas-secured-app</name>
<description>Demo project for Spring Boot</description>
<description>Demo project for CAS</description>
<parent>
<artifactId>parent-boot-1</artifactId>
@ -60,10 +58,4 @@
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
</project>

View File

@ -2,11 +2,17 @@
<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/xsd/maven-4.0.0.xsd ">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>cas-server</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<parent>
<artifactId>parent-boot-1</artifactId>
<groupId>com.baeldung</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-1</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.apereo.cas</groupId>
@ -39,7 +45,6 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${springboot.version}</version>
<configuration>
<mainClass>${mainClassName}</mainClass>
<addResources>true</addResources>
@ -74,42 +79,10 @@
</overlays>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
</plugin>
</plugins>
<finalName>cas</finalName>
</build>
<repositories>
<repository>
<id>sonatype-releases</id>
<url>http://oss.sonatype.org/content/repositories/releases/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<repository>
<id>sonatype-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
<repository>
<id>shibboleth-releases</id>
<url>https://build.shibboleth.net/nexus/content/repositories/releases</url>
</repository>
</repositories>
<profiles>
<profile>
<activation>
@ -214,8 +187,7 @@
</profiles>
<properties>
<cas.version>5.3.0-SNAPSHOT</cas.version>
<springboot.version>1.5.13.RELEASE</springboot.version>
<cas.version>5.3.3</cas.version>
<!-- app.server could be -jetty, -undertow, -tomcat, or blank if you plan to provide appserver -->
<app.server>-tomcat</app.server>
@ -223,14 +195,12 @@
<isExecutable>false</isExecutable>
<manifestFileToUse>${project.build.directory}/war/work/org.apereo.cas/cas-server-webapp${app.server}/META-INF/MANIFEST.MF</manifestFileToUse>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<wrapper-maven-plugin.version>0.0.4</wrapper-maven-plugin.version>
<maven-war-plugin.version>2.6</maven-war-plugin.version>
<maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
<echo-maven-plugin.version>0.3.0</echo-maven-plugin.version>
<pgpverify-maven-plugin.version>1.1.0</pgpverify-maven-plugin.version>
</properties>
</project>
</project>

15
core-java-9/logging.sh Executable file
View File

@ -0,0 +1,15 @@
# compile logging module
# javac --module-path mods -d mods/com.baeldung.logging src/modules/com.baeldung.logging/module-info.java src/modules/com.baeldung.logging/com/baeldung/logging/*.java
# compile logging slf4j module
javac --module-path mods -d mods/com.baeldung.logging.slf4j src/modules/com.baeldung.logging.slf4j/module-info.java src/modules/com.baeldung.logging.slf4j/com/baeldung/logging/slf4j/*.java
# compile logging main app module
javac --module-path mods -d mods/com.baeldung.logging.app src/modules/com.baeldung.logging.app/module-info.java src/modules/com.baeldung.logging.app/com/baeldung/logging/app/*.java
# run logging main app
# java --module-path mods -m com.baeldung.logging.app/com.baeldung.logging.app.MainApp
# run looging main app using logback
java --module-path mods -Dlogback.configurationFile=mods/logback.xml -m com.baeldung.logging.app/com.baeldung.logging.app.MainApp

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} -- %msg%n
</pattern>
</encoder>
</appender>
<root>
<appender-ref ref="STDOUT"/>
</root>
</configuration>

View File

@ -0,0 +1,13 @@
package com.baeldung.logging.app;
import static java.lang.System.Logger.*;
public class MainApp {
private static System.Logger LOGGER = System.getLogger("MainApp");
public static void main(String[] args) {
LOGGER.log(Level.ERROR, "error test");
LOGGER.log(Level.INFO, "info test");
}
}

View File

@ -0,0 +1,2 @@
module com.baeldung.logging.app {
}

View File

@ -0,0 +1,99 @@
package com.baeldung.logging.slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ResourceBundle;
public class Slf4jLogger implements System.Logger {
private final String name;
private final Logger logger;
public Slf4jLogger(String name) {
this.name = name;
logger = LoggerFactory.getLogger(name);
}
@Override
public String getName() {
return name;
}
@Override
public boolean isLoggable(Level level) {
switch (level) {
case OFF:
return false;
case TRACE:
return logger.isTraceEnabled();
case DEBUG:
return logger.isDebugEnabled();
case INFO:
return logger.isInfoEnabled();
case WARNING:
return logger.isWarnEnabled();
case ERROR:
return logger.isErrorEnabled();
case ALL:
default:
return true;
}
}
@Override
public void log(Level level, ResourceBundle bundle, String msg, Throwable thrown) {
if (!isLoggable(level)) {
return;
}
switch (level) {
case TRACE:
logger.trace(msg, thrown);
break;
case DEBUG:
logger.debug(msg, thrown);
break;
case INFO:
logger.info(msg, thrown);
break;
case WARNING:
logger.warn(msg, thrown);
break;
case ERROR:
logger.error(msg, thrown);
break;
case ALL:
default:
logger.info(msg, thrown);
}
}
@Override
public void log(Level level, ResourceBundle bundle, String format, Object... params) {
if (!isLoggable(level)) {
return;
}
switch (level) {
case TRACE:
logger.trace(format, params);
break;
case DEBUG:
logger.debug(format, params);
break;
case INFO:
logger.info(format, params);
break;
case WARNING:
logger.warn(format, params);
break;
case ERROR:
logger.error(format, params);
break;
case ALL:
default:
logger.info(format, params);
}
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.logging.slf4j;
public class Slf4jLoggerFinder extends System.LoggerFinder {
@Override
public System.Logger getLogger(String name, Module module) {
return new Slf4jLogger(name);
}
}

View File

@ -0,0 +1,6 @@
module com.baeldung.logging.slf4j {
requires org.slf4j;
provides java.lang.System.LoggerFinder
with com.baeldung.logging.slf4j.Slf4jLoggerFinder;
exports com.baeldung.logging.slf4j;
}

View File

@ -0,0 +1,27 @@
package com.baeldung.logging;
import java.text.MessageFormat;
import java.util.ResourceBundle;
public class ConsoleLogger implements System.Logger {
@Override
public String getName() {
return "ConsoleLogger";
}
@Override
public boolean isLoggable(Level level) {
return true;
}
@Override
public void log(Level level, ResourceBundle bundle, String msg, Throwable thrown) {
System.out.printf("ConsoleLogger [%s]: %s - %s%n", level, msg, thrown);
}
@Override
public void log(Level level, ResourceBundle bundle, String format, Object... params) {
System.out.printf("ConsoleLogger [%s]: %s%n", level, MessageFormat.format(format, params));
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.logging;
public class CustomLoggerFinder extends System.LoggerFinder {
@Override
public System.Logger getLogger(String name, Module module) {
return new ConsoleLogger();
}
}

View File

@ -0,0 +1,5 @@
module com.baeldung.logging {
provides java.lang.System.LoggerFinder
with com.baeldung.logging.CustomLoggerFinder;
exports com.baeldung.logging;
}

View File

@ -0,0 +1,44 @@
/**
*
*/
package com.baeldung.java.map;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.stream.Stream;
/**
* @author swpraman
*
*/
public class MapUtil {
public static <K, V> Stream<K> keys(Map<K, V> map, V value) {
return map.entrySet()
.stream()
.filter(entry -> value.equals(entry.getValue()))
.map(Map.Entry::getKey);
}
public static <K, V> Set<K> getKeys(Map<K, V> map, V value) {
Set<K> keys = new HashSet<>();
for (Entry<K, V> entry : map.entrySet()) {
if (entry.getValue().equals(value)) {
keys.add(entry.getKey());
}
}
return keys;
}
public static <K, V> K getKey(Map<K, V> map, V value) {
for (Entry<K, V> entry : map.entrySet()) {
if (entry.getValue().equals(value)) {
return entry.getKey();
}
}
return null;
}
}

View File

@ -44,4 +44,12 @@ public class Employee {
result = 31 * result + name.hashCode();
return result;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}

View File

@ -0,0 +1,60 @@
package com.baeldung.sort;
public class Employee implements Comparable<Employee> {
private Long id;
private String name;
public Employee(Long id, String name) {
this.name = name;
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
if (!id.equals(employee.id)) return false;
return name.equals(employee.name);
}
@Override
public int hashCode() {
int result = id.hashCode();
result = 31 * result + name.hashCode();
return result;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
@Override
public int compareTo(Employee employee) {
return (int)(this.id - employee.getId());
}
}

View File

@ -0,0 +1,104 @@
package com.baeldung.sort;
import com.google.common.base.Functions;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import java.util.*;
import java.util.stream.Collectors;
public class SortHashMap {
private static Map<String, Employee> map = new HashMap<>();
public static void main(String[] args) {
initialize();
treeMapSortByKey();
arrayListSortByValue();
arrayListSortByKey();
sortStream();
sortGuava();
addDuplicates();
treeSetByKey();
treeSetByValue();
}
private static void sortGuava() {
final Ordering naturalOrdering =
Ordering.natural().onResultOf(Functions.forMap(map, null));
System.out.println(ImmutableSortedMap.copyOf(map, naturalOrdering));
}
private static void sortStream() {
map.entrySet().stream()
.sorted(Map.Entry.<String, Employee>comparingByKey().reversed())
.forEach(System.out::println);
Map<String, Employee> result = map.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
result.entrySet().forEach(System.out::println);
}
private static void treeSetByValue() {
SortedSet<Employee> values = new TreeSet<>(map.values());
System.out.println(values);
}
private static void treeSetByKey() {
SortedSet<String> keysSet = new TreeSet<>(map.keySet());
System.out.println(keysSet);
}
private static void treeMapSortByKey() {
TreeMap<String, Employee> sorted = new TreeMap<>(map);
sorted.putAll(map);
sorted.entrySet().forEach(System.out::println);
}
private static void arrayListSortByValue() {
List<Employee> employeeById = new ArrayList<>(map.values());
Collections.sort(employeeById);
System.out.println(employeeById);
}
private static void arrayListSortByKey() {
List<String> employeeByKey = new ArrayList<>(map.keySet());
Collections.sort(employeeByKey);
System.out.println(employeeByKey);
}
private static void initialize() {
Employee employee1 = new Employee(1L, "Mher");
map.put(employee1.getName(), employee1);
Employee employee2 = new Employee(22L, "Annie");
map.put(employee2.getName(), employee2);
Employee employee3 = new Employee(8L, "John");
map.put(employee3.getName(), employee3);
Employee employee4 = new Employee(2L, "George");
map.put(employee4.getName(), employee4);
}
private static void addDuplicates() {
Employee employee5 = new Employee(1L, "Mher");
map.put(employee5.getName(), employee5);
Employee employee6 = new Employee(22L, "Annie");
map.put(employee6.getName(), employee6);
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.collection;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Unit tests demonstrating differences between ArrayList#clear() and ArrayList#removeAll()
*/
class ClearVsRemoveAllUnitTest {
/*
* Tests
*/
@Test
void givenArrayListWithElements_whenClear_thenListBecomesEmpty() {
Collection<Integer> collection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
collection.clear();
assertTrue(collection.isEmpty());
}
@Test
void givenTwoArrayListsWithCommonElements_whenRemoveAll_thenFirstListMissElementsFromSecondList() {
Collection<Integer> firstCollection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
Collection<Integer> secondCollection = new ArrayList<>(Arrays.asList(3, 4, 5, 6, 7));
firstCollection.removeAll(secondCollection);
assertEquals(Arrays.asList(1, 2), firstCollection);
assertEquals(Arrays.asList(3, 4, 5, 6, 7), secondCollection);
}
}

View File

@ -0,0 +1,104 @@
/**
*
*/
package com.baeldung.java.map;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.bidimap.DualHashBidiMap;
import org.junit.Test;
import com.google.common.collect.HashBiMap;
/**
* @author swpraman
*
*/
public class MapUtilUnitTest {
@Test
public void whenUsingImperativeWayForSingleKey_shouldReturnSingleKey() {
Map<String, String> capitalCountryMap = new HashMap<>();
capitalCountryMap.put("Tokyo", "Japan");
capitalCountryMap.put("New Delhi", "India");
assertEquals("New Delhi", MapUtil.getKey(capitalCountryMap, "India"));
}
@Test
public void whenUsingImperativeWayForAllKeys_shouldReturnAllKeys() {
Map<String, String> capitalCountryMap = new HashMap<>();
capitalCountryMap.put("Tokyo", "Japan");
capitalCountryMap.put("Berlin", "Germany");
capitalCountryMap.put("Cape Town", "South Africa");
capitalCountryMap.put("Pretoria", "South Africa");
capitalCountryMap.put("Bloemfontein", "South Africa");
assertEquals(new HashSet<String>(Arrays.asList(
new String[] {"Cape Town", "Pretoria", "Bloemfontein"})),
MapUtil.getKeys(capitalCountryMap, "South Africa"));
}
@Test
public void whenUsingFunctionalWayForSingleKey_shouldReturnSingleKey() {
Map<String, String> capitalCountryMap = new HashMap<>();
capitalCountryMap.put("Tokyo", "Japan");
capitalCountryMap.put("Berlin", "Germany");
assertEquals("Berlin", MapUtil.keys(capitalCountryMap, "Germany").findFirst().get());
}
@Test
public void whenUsingFunctionalWayForAllKeys_shouldReturnAllKeys() {
Map<String, String> capitalCountryMap = new HashMap<>();
capitalCountryMap.put("Tokyo", "Japan");
capitalCountryMap.put("Berlin", "Germany");
capitalCountryMap.put("Cape Town", "South Africa");
capitalCountryMap.put("Pretoria", "South Africa");
capitalCountryMap.put("Bloemfontein", "South Africa");
assertEquals(new HashSet<String>(Arrays.asList(
new String[] {"Cape Town", "Pretoria", "Bloemfontein"})),
MapUtil.keys(capitalCountryMap, "South Africa").collect(Collectors.toSet()));
}
@Test
public void whenUsingBidiMap_shouldReturnKey() {
BidiMap<String, String> capitalCountryMap = new DualHashBidiMap<String, String>();
capitalCountryMap.put("Berlin", "Germany");
capitalCountryMap.put("Cape Town", "South Africa");
assertEquals("Berlin", capitalCountryMap.getKey("Germany"));
}
@Test
public void whenUsingBidiMapAddDuplicateValue_shouldRemoveOldEntry() {
BidiMap<String, String> capitalCountryMap = new DualHashBidiMap<String, String>();
capitalCountryMap.put("Berlin", "Germany");
capitalCountryMap.put("Cape Town", "South Africa");
capitalCountryMap.put("Pretoria", "South Africa");
assertEquals("Pretoria", capitalCountryMap.getKey("South Africa"));
}
@Test
public void whenUsingBiMap_shouldReturnKey() {
HashBiMap<String, String> capitalCountryMap = HashBiMap.create();
capitalCountryMap.put("Berlin", "Germany");
capitalCountryMap.put("Cape Town", "South Africa");
assertEquals("Berlin", capitalCountryMap.inverse().get("Germany"));
}
@Test(expected=IllegalArgumentException.class)
public void whenUsingBiMapAddDuplicateValue_shouldThrowException() {
HashBiMap<String, String> capitalCountryMap = HashBiMap.create();
capitalCountryMap.put("Berlin", "Germany");
capitalCountryMap.put("Cape Town", "South Africa");
capitalCountryMap.put("Pretoria", "South Africa");
assertEquals("Berlin", capitalCountryMap.inverse().get("Germany"));
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.concurrent.yield;
public class ThreadYield {
public static void main(String[] args) {
Runnable r = () -> {
int counter = 0;
while (counter < 2) {
System.out.println(Thread.currentThread()
.getName());
counter++;
Thread.yield();
}
};
new Thread(r).start();
new Thread(r).start();
}
}

View File

@ -1,7 +1,6 @@
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>core-java-io</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
@ -166,36 +165,6 @@
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*LiveTest.java</exclude>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
<exclude>**/*LongRunningUnitTest.java</exclude>
<exclude>**/*ManualTest.java</exclude>
</excludes>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>spring-boot</classifier>
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
@ -229,32 +198,6 @@
<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>
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
<include>**/*IntTest.java</include>
</includes>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<test.mime>json</test.mime>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
@ -310,14 +253,11 @@
<avaitility.version>1.7.0</avaitility.version>
<!-- maven plugins -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
<hsqldb.version>2.4.0</hsqldb.version>
<esapi.version>2.1.0.1</esapi.version>
<jmh-generator-annprocess.version>1.19</jmh-generator-annprocess.version>
<async-http-client.version>2.4.5</async-http-client.version>
<spring-boot-maven-plugin.version>2.0.4.RELEASE</spring-boot-maven-plugin.version>
</properties>
</project>

View File

@ -24,6 +24,13 @@ public class ZipDirectory {
return;
}
if (fileToZip.isDirectory()) {
if (fileName.endsWith("/")) {
zipOut.putNextEntry(new ZipEntry(fileName));
zipOut.closeEntry();
} else {
zipOut.putNextEntry(new ZipEntry(fileName + "/"));
zipOut.closeEntry();
}
final File[] children = fileToZip.listFiles();
for (final File childFile : children) {
zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);

View File

@ -1,6 +1,7 @@
package com.baeldung.file;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.hamcrest.CoreMatchers;
import org.hamcrest.Matchers;
import org.junit.Test;
@ -120,4 +121,14 @@ public class FileOperationsManualTest {
return resultStringBuilder.toString();
}
@Test
public void givenFileName_whenUsingIOUtils_thenFileData() throws IOException {
String expectedData = "This is a content of the file";
FileInputStream fis = new FileInputStream("src/test/resources/fileToRead.txt");
String data = IOUtils.toString(fis, "UTF-8");
assertEquals(expectedData, data.trim());
}
}

View File

@ -9,7 +9,7 @@ import java.nio.file.Paths;
import org.junit.Test;
public class SymLinkExampleUnitTest {
public class SymLinkExampleManualTest {
@Test
public void whenUsingFiles_thenCreateSymbolicLink() throws IOException {

View File

@ -1,38 +0,0 @@
package com.baeldung.decimalformat;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
public class DoubletoString {
public static void main(String[] args) {
double doubleValue = 345.56;
System.out.println(String.valueOf((int) doubleValue));
System.out.println(String.format("%.0f", doubleValue));
doubleValue = Math.floor(doubleValue);
DecimalFormat df = new DecimalFormat("#");
df.setRoundingMode(RoundingMode.FLOOR);
System.out.println(df.format(doubleValue));
Locale enlocale = new Locale("en", "US");
String pattern = "###,##";
df = (DecimalFormat) NumberFormat.getNumberInstance(enlocale);
df.applyPattern(pattern);
String format = df.format(doubleValue);
System.out.println(format);
Locale dalocale = new Locale("da", "DK");
df = (DecimalFormat) NumberFormat.getNumberInstance(dalocale);
df.applyPattern(pattern);
System.out.println(df.format(doubleValue));
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.doubles;
import java.math.BigDecimal;
public class SplitFloatingPointNumbers {
public static void main(String[] args) {
double doubleNumber = 24.04;
splitUsingFloatingTypes(doubleNumber);
splitUsingString(doubleNumber);
splitUsingBigDecimal(doubleNumber);
}
private static void splitUsingFloatingTypes(double doubleNumber) {
System.out.println("Using Floating Point Arithmetics:");
int intPart = (int) doubleNumber;
System.out.println("Double Number: "+doubleNumber);
System.out.println("Integer Part: "+ intPart);
System.out.println("Decimal Part: "+ (doubleNumber - intPart));
}
private static void splitUsingString(double doubleNumber) {
System.out.println("Using String Operations:");
String doubleAsString = String.valueOf(doubleNumber);
int indexOfDecimal = doubleAsString.indexOf(".");
System.out.println("Double Number: "+doubleNumber);
System.out.println("Integer Part: "+ doubleAsString.substring(0, indexOfDecimal));
System.out.println("Decimal Part: "+ doubleAsString.substring(indexOfDecimal));
}
private static void splitUsingBigDecimal(double doubleNumber) {
System.out.println("Using BigDecimal Operations:");
BigDecimal bigDecimal = new BigDecimal(String.valueOf(doubleNumber));
int intValue = bigDecimal.intValue();
System.out.println("Double Number: "+bigDecimal.toPlainString());
System.out.println("Integer Part: "+intValue);
System.out.println("Decimal Part: "+bigDecimal.subtract(new BigDecimal(intValue)).toPlainString());
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.heapdump;
import com.sun.management.HotSpotDiagnosticMXBean;
import javax.management.MBeanServer;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.nio.file.Paths;
public class HeapDump {
public static void dumpHeap(String filePath, boolean live) throws IOException {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
HotSpotDiagnosticMXBean mxBean = ManagementFactory.newPlatformMXBeanProxy(
server, "com.sun.management:type=HotSpotDiagnostic", HotSpotDiagnosticMXBean.class);
mxBean.dumpHeap(filePath, live);
}
public static void main(String[] args) throws IOException {
String file = Paths.get("dump.hprof").toFile().getPath();
dumpHeap(file, true);
}
}

View File

@ -0,0 +1,149 @@
package com.baeldung.passwordhashing;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Arrays;
import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
/**
* Hash passwords for storage, and test passwords against password tokens.
*
* Instances of this class can be used concurrently by multiple threads.
*
* @author erickson
* @see <a href="http://stackoverflow.com/a/2861125/3474">StackOverflow</a>
*/
public final class PBKDF2Hasher
{
/**
* Each token produced by this class uses this identifier as a prefix.
*/
public static final String ID = "$31$";
/**
* The minimum recommended cost, used by default
*/
public static final int DEFAULT_COST = 16;
private static final String ALGORITHM = "PBKDF2WithHmacSHA1";
private static final int SIZE = 128;
private static final Pattern layout = Pattern.compile("\\$31\\$(\\d\\d?)\\$(.{43})");
private final SecureRandom random;
private final int cost;
public PBKDF2Hasher()
{
this(DEFAULT_COST);
}
/**
* Create a password manager with a specified cost
*
* @param cost the exponential computational cost of hashing a password, 0 to 30
*/
public PBKDF2Hasher(int cost)
{
iterations(cost); /* Validate cost */
this.cost = cost;
this.random = new SecureRandom();
}
private static int iterations(int cost)
{
if ((cost < 0) || (cost > 30))
throw new IllegalArgumentException("cost: " + cost);
return 1 << cost;
}
/**
* Hash a password for storage.
*
* @return a secure authentication token to be stored for later authentication
*/
public String hash(char[] password)
{
byte[] salt = new byte[SIZE / 8];
random.nextBytes(salt);
byte[] dk = pbkdf2(password, salt, 1 << cost);
byte[] hash = new byte[salt.length + dk.length];
System.arraycopy(salt, 0, hash, 0, salt.length);
System.arraycopy(dk, 0, hash, salt.length, dk.length);
Base64.Encoder enc = Base64.getUrlEncoder().withoutPadding();
return ID + cost + '$' + enc.encodeToString(hash);
}
/**
* Authenticate with a password and a stored password token.
*
* @return true if the password and token match
*/
public boolean checkPassword(char[] password, String token)
{
Matcher m = layout.matcher(token);
if (!m.matches())
throw new IllegalArgumentException("Invalid token format");
int iterations = iterations(Integer.parseInt(m.group(1)));
byte[] hash = Base64.getUrlDecoder().decode(m.group(2));
byte[] salt = Arrays.copyOfRange(hash, 0, SIZE / 8);
byte[] check = pbkdf2(password, salt, iterations);
int zero = 0;
for (int idx = 0; idx < check.length; ++idx)
zero |= hash[salt.length + idx] ^ check[idx];
return zero == 0;
}
private static byte[] pbkdf2(char[] password, byte[] salt, int iterations)
{
KeySpec spec = new PBEKeySpec(password, salt, iterations, SIZE);
try {
SecretKeyFactory f = SecretKeyFactory.getInstance(ALGORITHM);
return f.generateSecret(spec).getEncoded();
}
catch (NoSuchAlgorithmException ex) {
throw new IllegalStateException("Missing algorithm: " + ALGORITHM, ex);
}
catch (InvalidKeySpecException ex) {
throw new IllegalStateException("Invalid SecretKeyFactory", ex);
}
}
/**
* Hash a password in an immutable {@code String}.
*
* <p>Passwords should be stored in a {@code char[]} so that it can be filled
* with zeros after use instead of lingering on the heap and elsewhere.
*
* @deprecated Use {@link #hash(char[])} instead
*/
@Deprecated
public String hash(String password)
{
return hash(password.toCharArray());
}
/**
* Authenticate with a password in an immutable {@code String} and a stored
* password token.
*
* @deprecated Use {@link #checkPassword(char[],String)} instead.
* @see #hash(String)
*/
@Deprecated
public boolean checkPassword(String password, String token)
{
return checkPassword(password.toCharArray(), token);
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.passwordhashing;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/** A really simple SHA_512 Encryption example.
*
*/
public class SHA512Hasher {
public String hash(String passwordToHash, byte[] salt){
String generatedPassword = null;
try {
MessageDigest md = MessageDigest.getInstance("SHA-512");
md.update(salt);
byte[] bytes = md.digest(passwordToHash.getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
for(int i=0; i< bytes.length ;i++){
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
generatedPassword = sb.toString();
}
catch (NoSuchAlgorithmException e){
e.printStackTrace();
}
return generatedPassword;
}
public boolean checkPassword(String hash, String attempt, byte[] salt){
String generatedHash = hash(attempt, salt);
return hash.equals(generatedHash);
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.passwordhashing;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.security.spec.KeySpec;
/** A really simple SimplePBKDF2 Encryption example.
*
*/
public class SimplePBKDF2Hasher {
public static String hashSimple(String password, byte[] salt) throws Exception{
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128);
SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] hash = f.generateSecret(spec).getEncoded();
return String.valueOf(hash);
}
}

View File

@ -0,0 +1,70 @@
package com.baeldung.switchstatement;
public class SwitchStatement {
public String exampleOfIF(String animal) {
String result;
if (animal.equals("DOG") || animal.equals("CAT")) {
result = "domestic animal";
} else if (animal.equals("TIGER")) {
result = "wild animal";
} else {
result = "unknown animal";
}
return result;
}
public String exampleOfSwitch(String animal) {
String result;
switch (animal) {
case "DOG":
case "CAT":
result = "domestic animal";
break;
case "TIGER":
result = "wild animal";
break;
default:
result = "unknown animal";
break;
}
return result;
}
public String forgetBreakInSwitch(String animal) {
String result;
switch (animal) {
case "DOG":
System.out.println("domestic animal");
result = "domestic animal";
default:
System.out.println("unknown animal");
result = "unknown animal";
}
return result;
}
public String constantCaseValue(String animal) {
String result = "";
final String dog = "DOG";
switch (animal) {
case dog:
result = "domestic animal";
}
return result;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.zoneddatetime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public class OffsetDateTimeExample {
public OffsetDateTime getCurrentTimeByZoneOffset(String offset) {
ZoneOffset zoneOffSet= ZoneOffset.of(offset);
OffsetDateTime date = OffsetDateTime.now(zoneOffSet);
return date;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.zoneddatetime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
public class OffsetTimeExample {
public OffsetTime getCurrentTimeByZoneOffset(String offset) {
ZoneOffset zoneOffSet = ZoneOffset.of(offset);
OffsetTime time = OffsetTime.now(zoneOffSet);
return time;
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.zoneddatetime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ZoneDateTimeExample {
public ZonedDateTime getCurrentTimeByZoneId(String region) {
ZoneId zone = ZoneId.of(region);
ZonedDateTime date = ZonedDateTime.now(zone);
return date;
}
public ZonedDateTime convertZonedDateTime(ZonedDateTime sourceDate, String destZone) {
ZoneId destZoneId = ZoneId.of(destZone);
ZonedDateTime destDate = sourceDate.withZoneSameInstant(destZoneId);
return destDate;
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.passwordhashing;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class PBKDF2HasherUnitTest {
private PBKDF2Hasher mPBKDF2Hasher;
@Before
public void setUp() throws Exception {
mPBKDF2Hasher = new PBKDF2Hasher();
}
@Test
public void givenCorrectMessageAndHash_whenAuthenticated_checkAuthenticationSucceeds() throws Exception {
String message1 = "password123";
String hash1 = mPBKDF2Hasher.hash(message1.toCharArray());
assertTrue(mPBKDF2Hasher.checkPassword(message1.toCharArray(), hash1));
}
@Test
public void givenWrongMessage_whenAuthenticated_checkAuthenticationFails() throws Exception {
String message1 = "password123";
String hash1 = mPBKDF2Hasher.hash(message1.toCharArray());
String wrongPasswordAttempt = "IamWrong";
assertFalse(mPBKDF2Hasher.checkPassword(wrongPasswordAttempt.toCharArray(), hash1));
}
}

View File

@ -0,0 +1,70 @@
package com.baeldung.passwordhashing;
import org.junit.Before;
import org.junit.Test;
import java.security.SecureRandom;
import static org.junit.Assert.*;
/**
* Created by PhysicsSam on 06-Sep-18.
*/
public class SHA512HasherUnitTest {
private SHA512Hasher hasher;
private SecureRandom secureRandom;
@Before
public void setUp() throws Exception {
hasher = new SHA512Hasher();
secureRandom = new SecureRandom();
}
@Test
public void givenSamePasswordAndSalt_whenHashed_checkResultingHashesAreEqual() throws Exception {
byte[] salt = new byte[16];
secureRandom.nextBytes(salt);
String hash1 = hasher.hash("password", salt);
String hash2 = hasher.hash("password", salt);
assertEquals(hash1, hash2);
}
@Test
public void givenSamePasswordAndDifferentSalt_whenHashed_checkResultingHashesNotEqual() throws Exception {
byte[] salt = new byte[16];
secureRandom.nextBytes(salt);
String hash1 = hasher.hash("password", salt);
//generate a second salt
byte[] secondSalt = new byte[16];
String hash2 = hasher.hash("password", secondSalt);
assertNotEquals(hash1, hash2);
}
@Test
public void givenPredefinedHash_whenCorrectAttemptGiven_checkAuthenticationSucceeds() throws Exception {
byte[] salt = new byte[16];
secureRandom.nextBytes(salt);
String originalHash = hasher.hash("password123", salt);
assertTrue(hasher.checkPassword(originalHash, "password123", salt));
}
@Test
public void givenPredefinedHash_whenIncorrectAttemptGiven_checkAuthenticationFails() throws Exception {
byte[] salt = new byte[16];
secureRandom.nextBytes(salt);
String originalHash = hasher.hash("password123", salt);
assertFalse(hasher.checkPassword(originalHash, "password124", salt));
}
}

View File

@ -0,0 +1,100 @@
package com.baeldung.removingdecimals;
import org.junit.Test;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.concurrent.TimeUnit;
/**
* This benchmark compares some of the approaches to formatting a floating-point
* value into a {@link String} while removing the decimal part.
*
* To run, simply run the {@link RemovingDecimalsManualTest#runBenchmarks()} test
* at the end of this class.
*
* The benchmark takes about 15 minutes to run. Since it is using {@link Mode#Throughput},
* higher numbers mean better performance.
*/
@BenchmarkMode(Mode.Throughput)
@Warmup(iterations = 5)
@Measurement(iterations = 20)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)
public class RemovingDecimalsManualTest {
@Param(value = {"345.56", "345345345.56", "345345345345345345.56"}) double doubleValue;
NumberFormat nf;
DecimalFormat df;
@Setup
public void readyFormatters() {
nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(0);
df = new DecimalFormat("#,###");
}
@Benchmark
public String whenCastToInt_thenValueIsTruncated() {
return String.valueOf((int) doubleValue);
}
@Benchmark
public String whenUsingStringFormat_thenValueIsRounded() {
return String.format("%.0f", doubleValue);
}
@Benchmark
public String whenUsingNumberFormat_thenValueIsRounded() {
nf.setRoundingMode(RoundingMode.HALF_UP);
return nf.format(doubleValue);
}
@Benchmark
public String whenUsingNumberFormatWithFloor_thenValueIsTruncated() {
nf.setRoundingMode(RoundingMode.FLOOR);
return nf.format(doubleValue);
}
@Benchmark
public String whenUsingDecimalFormat_thenValueIsRounded() {
df.setRoundingMode(RoundingMode.HALF_UP);
return df.format(doubleValue);
}
@Benchmark
public String whenUsingDecimalFormatWithFloor_thenValueIsTruncated() {
df.setRoundingMode(RoundingMode.FLOOR);
return df.format(doubleValue);
}
@Benchmark
public String whenUsingBigDecimalDoubleValue_thenValueIsTruncated() {
BigDecimal big = new BigDecimal(doubleValue);
big = big.setScale(0, RoundingMode.FLOOR);
return big.toString();
}
@Benchmark
public String whenUsingBigDecimalDoubleValueWithHalfUp_thenValueIsRounded() {
BigDecimal big = new BigDecimal(doubleValue);
big = big.setScale(0, RoundingMode.HALF_UP);
return big.toString();
}
@Test
public void runBenchmarks() throws Exception {
Options options = new OptionsBuilder()
.include(this.getClass().getSimpleName()).threads(1)
.forks(1).shouldFailOnError(true).shouldDoGC(true)
.jvmArgs("-server").build();
new Runner(options).run();
}
}

View File

@ -0,0 +1,95 @@
package com.baeldung.removingdecimals;
import org.junit.Test;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
/**
* Tests that demonstrate some different approaches for formatting a
* floating-point value into a {@link String} while removing the decimal part.
*/
public class RemovingDecimalsUnitTest {
private final double doubleValue = 345.56;
@Test
public void whenCastToInt_thenValueIsTruncated() {
String truncated = String.valueOf((int) doubleValue);
assertEquals("345", truncated);
}
@Test
public void givenALargeDouble_whenCastToInt_thenValueIsNotTruncated() {
double outOfIntRange = 6_000_000_000.56;
String truncationAttempt = String.valueOf((int) outOfIntRange);
assertNotEquals("6000000000", truncationAttempt);
}
@Test
public void whenUsingStringFormat_thenValueIsRounded() {
String rounded = String.format("%.0f", doubleValue);
assertEquals("346", rounded);
}
@Test
public void givenALargeDouble_whenUsingStringFormat_thenValueIsStillRounded() {
double outOfIntRange = 6_000_000_000.56;
String rounded = String.format("%.0f", outOfIntRange);
assertEquals("6000000001", rounded);
}
@Test
public void whenUsingNumberFormat_thenValueIsRounded() {
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(0);
nf.setRoundingMode(RoundingMode.HALF_UP);
String rounded = nf.format(doubleValue);
assertEquals("346", rounded);
}
@Test
public void whenUsingNumberFormatWithFloor_thenValueIsTruncated() {
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(0);
nf.setRoundingMode(RoundingMode.FLOOR);
String truncated = nf.format(doubleValue);
assertEquals("345", truncated);
}
@Test
public void whenUsingDecimalFormat_thenValueIsRounded() {
DecimalFormat df = new DecimalFormat("#,###");
df.setRoundingMode(RoundingMode.HALF_UP);
String rounded = df.format(doubleValue);
assertEquals("346", rounded);
}
@Test
public void whenUsingDecimalFormatWithFloor_thenValueIsTruncated() {
DecimalFormat df = new DecimalFormat("#,###");
df.setRoundingMode(RoundingMode.FLOOR);
String truncated = df.format(doubleValue);
assertEquals("345", truncated);
}
@Test
public void whenUsingBigDecimalDoubleValue_thenValueIsTruncated() {
BigDecimal big = new BigDecimal(doubleValue);
big = big.setScale(0, RoundingMode.FLOOR);
String truncated = big.toString();
assertEquals("345", truncated);
}
@Test
public void whenUsingBigDecimalDoubleValueWithHalfUp_thenValueIsRounded() {
BigDecimal big = new BigDecimal(doubleValue);
big = big.setScale(0, RoundingMode.HALF_UP);
String truncated = big.toString();
assertEquals("346", truncated);
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.switchstatement;
import org.junit.Test;
import org.junit.Assert;
public class SwitchStatementUnitTest {
private SwitchStatement s = new SwitchStatement();
@Test
public void whenDog_thenDomesticAnimal() {
String animal = "DOG";
Assert.assertEquals("domestic animal", s.exampleOfSwitch(animal));
}
@Test
public void whenNoBreaks_thenGoThroughBlocks() {
String animal = "DOG";
Assert.assertEquals("unknown animal", s.forgetBreakInSwitch(animal));
}
@Test(expected=NullPointerException.class)
public void whenSwitchAgumentIsNull_thenNullPointerException() {
String animal = null;
Assert.assertEquals("domestic animal", s.exampleOfSwitch(animal));
}
@Test
public void whenCompareStrings_thenByEqual() {
String animal = new String("DOG");
Assert.assertEquals("domestic animal", s.exampleOfSwitch(animal));
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.zoneddatetime;
import static org.junit.Assert.assertTrue;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import org.junit.Test;
public class OffsetDateTimeExampleUnitTest {
OffsetDateTimeExample offsetDateTimeExample = new OffsetDateTimeExample();
@Test
public void givenZoneOffset_whenGetCurrentTime_thenResultHasZone() {
String offset = "+02:00";
OffsetDateTime time = offsetDateTimeExample.getCurrentTimeByZoneOffset(offset);
assertTrue(time.getOffset()
.equals(ZoneOffset.of(offset)));
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.zoneddatetime;
import static org.junit.Assert.assertTrue;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import org.junit.Test;
public class OffsetTimeExampleUnitTest {
OffsetTimeExample offsetTimeExample = new OffsetTimeExample();
@Test
public void givenZoneOffset_whenGetCurrentTime_thenResultHasZone() {
String offset = "+02:00";
OffsetTime time = offsetTimeExample.getCurrentTimeByZoneOffset(offset);
assertTrue(time.getOffset()
.equals(ZoneOffset.of(offset)));
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.zoneddatetime;
import static org.junit.Assert.assertTrue;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import org.junit.Test;
public class ZoneDateTimeExampleUnitTest {
ZoneDateTimeExample zoneDateTimeExample = new ZoneDateTimeExample();
@Test
public void givenZone_whenGetCurrentTime_thenResultHasZone() {
String zone = "Europe/Berlin";
ZonedDateTime time = zoneDateTimeExample.getCurrentTimeByZoneId(zone);
assertTrue(time.getZone()
.equals(ZoneId.of(zone)));
}
@Test
public void givenZones_whenConvertDateByZone_thenGetConstantDiff() {
String sourceZone = "Europe/Berlin";
String destZone = "Asia/Tokyo";
ZonedDateTime sourceDate = zoneDateTimeExample.getCurrentTimeByZoneId(sourceZone);
ZonedDateTime destDate = zoneDateTimeExample.convertZonedDateTime(sourceDate, destZone);
assertTrue(sourceDate.toInstant()
.compareTo(destDate.toInstant()) == 0);
}
}

View File

@ -55,6 +55,12 @@
<artifactId>fuel-coroutines</artifactId>
<version>${fuel.version}</version>
</dependency>
<dependency>
<groupId>nl.komponents.kovenant</groupId>
<artifactId>kovenant</artifactId>
<version>3.3.0</version>
<type>pom</type>
</dependency>
</dependencies>
<properties>

View File

@ -0,0 +1,191 @@
package com.baeldung.kotlin
import nl.komponents.kovenant.*
import nl.komponents.kovenant.Kovenant.deferred
import nl.komponents.kovenant.combine.and
import nl.komponents.kovenant.combine.combine
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import java.io.IOException
import java.util.*
import java.util.concurrent.TimeUnit
class KovenantTest {
@Before
fun setupTestMode() {
Kovenant.testMode { error ->
println("An unexpected error occurred")
Assert.fail(error.message)
}
}
@Test
fun testSuccessfulDeferred() {
val def = deferred<Long, Exception>()
Assert.assertFalse(def.promise.isDone())
def.resolve(1L)
Assert.assertTrue(def.promise.isDone())
Assert.assertTrue(def.promise.isSuccess())
Assert.assertFalse(def.promise.isFailure())
}
@Test
fun testFailedDeferred() {
val def = deferred<Long, Exception>()
Assert.assertFalse(def.promise.isDone())
def.reject(RuntimeException())
Assert.assertTrue(def.promise.isDone())
Assert.assertFalse(def.promise.isSuccess())
Assert.assertTrue(def.promise.isFailure())
}
@Test
fun testResolveDeferredTwice() {
val def = deferred<Long, Exception>()
def.resolve(1L)
try {
def.resolve(1L)
} catch (e: AssertionError) {
// Expected.
// This is slightly unusual. The AssertionError comes from Assert.fail() from setupTestMode()
}
}
@Test
fun testSuccessfulTask() {
val promise = task { 1L }
Assert.assertTrue(promise.isDone())
Assert.assertTrue(promise.isSuccess())
Assert.assertFalse(promise.isFailure())
}
@Test
fun testFailedTask() {
val promise = task { throw RuntimeException() }
Assert.assertTrue(promise.isDone())
Assert.assertFalse(promise.isSuccess())
Assert.assertTrue(promise.isFailure())
}
@Test
fun testCallbacks() {
val promise = task { 1L }
promise.success {
println("This was a success")
Assert.assertEquals(1L, it)
}
promise.fail {
println(it)
Assert.fail("This shouldn't happen")
}
promise.always {
println("This always happens")
}
}
@Test
fun testGetValues() {
val promise = task { 1L }
Assert.assertEquals(1L, promise.get())
}
@Test
fun testAllSucceed() {
val numbers = all(
task { 1L },
task { 2L },
task { 3L }
)
Assert.assertEquals(listOf(1L, 2L, 3L), numbers.get())
}
@Test
fun testOneFails() {
val runtimeException = RuntimeException()
val numbers = all(
task { 1L },
task { 2L },
task { throw runtimeException }
)
Assert.assertEquals(runtimeException, numbers.getError())
}
@Test
fun testAnySucceeds() {
val promise = any(
task {
TimeUnit.SECONDS.sleep(3)
1L
},
task {
TimeUnit.SECONDS.sleep(2)
2L
},
task {
TimeUnit.SECONDS.sleep(1)
3L
}
)
Assert.assertTrue(promise.isDone())
Assert.assertTrue(promise.isSuccess())
Assert.assertFalse(promise.isFailure())
}
@Test
fun testAllFails() {
val runtimeException = RuntimeException()
val ioException = IOException()
val illegalStateException = IllegalStateException()
val promise = any(
task {
TimeUnit.SECONDS.sleep(3)
throw runtimeException
},
task {
TimeUnit.SECONDS.sleep(2)
throw ioException
},
task {
TimeUnit.SECONDS.sleep(1)
throw illegalStateException
}
)
Assert.assertTrue(promise.isDone())
Assert.assertFalse(promise.isSuccess())
Assert.assertTrue(promise.isFailure())
}
@Test
fun testSimpleCombine() {
val promise = task { 1L } and task { "Hello" }
val result = promise.get()
Assert.assertEquals(1L, result.first)
Assert.assertEquals("Hello", result.second)
}
@Test
fun testLongerCombine() {
val promise = combine(
task { 1L },
task { "Hello" },
task { Currency.getInstance("USD") }
)
val result = promise.get()
Assert.assertEquals(1L, result.first)
Assert.assertEquals("Hello", result.second)
Assert.assertEquals(Currency.getInstance("USD"), result.third)
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.kotlin
import nl.komponents.kovenant.Promise
import nl.komponents.kovenant.any
import nl.komponents.kovenant.task
import org.junit.Assert
import org.junit.Ignore
import org.junit.Test
@Ignore
// Note that this can not run in the same test run if KovenantTest has already been executed
class KovenantTimeoutTest {
@Test
fun testTimeout() {
val promise = timedTask(1000) { "Hello" }
val result = promise.get()
Assert.assertEquals("Hello", result)
}
@Test
fun testTimeoutExpired() {
val promise = timedTask(1000) {
Thread.sleep(3000)
"Hello"
}
val result = promise.get()
Assert.assertNull(result)
}
fun <T> timedTask(millis: Long, body: () -> T) : Promise<T?, List<Exception>> {
val timeoutTask = task {
Thread.sleep(millis)
null
}
val activeTask = task(body = body)
return any(activeTask, timeoutTask)
}
}

View File

@ -0,0 +1,48 @@
package com.baeldung.kotlin
import org.junit.Test
import kotlin.test.assertEquals
class StringConcatenationTest {
@Test
fun givenTwoStrings_concatenateWithTemplates_thenEquals() {
val a = "Hello"
val b = "Baeldung"
val c = "$a $b"
assertEquals("Hello Baeldung", c)
}
@Test
fun givenTwoStrings_concatenateWithPlusOperator_thenEquals() {
val a = "Hello"
val b = "Baeldung"
val c = a + " " + b
assertEquals("Hello Baeldung", c)
}
@Test
fun givenTwoStrings_concatenateWithStringBuilder_thenEquals() {
val a = "Hello"
val b = "Baeldung"
val builder = StringBuilder()
builder.append(a).append(" ").append(b)
val c = builder.toString()
assertEquals("Hello Baeldung", c)
}
@Test
fun givenTwoStrings_concatenateWithPlusMethod_thenEquals() {
val a = "Hello"
val b = "Baeldung"
val c = a.plus(" ").plus(b)
assertEquals("Hello Baeldung", c)
}
}

View File

@ -1,5 +1,11 @@
package com.baeldung.hibernate;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
import com.baeldung.hibernate.entities.DeptEmployee;
import com.baeldung.hibernate.optimisticlocking.OptimisticLockingCourse;
import com.baeldung.hibernate.optimisticlocking.OptimisticLockingStudent;
import com.baeldung.hibernate.pessimisticlocking.Individual;
@ -16,10 +22,30 @@ import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
import com.baeldung.hibernate.pojo.Course;
import com.baeldung.hibernate.pojo.Employee;
import com.baeldung.hibernate.pojo.EntityDescription;
import com.baeldung.hibernate.pojo.OrderEntry;
import com.baeldung.hibernate.pojo.OrderEntryIdClass;
import com.baeldung.hibernate.pojo.OrderEntryPK;
import com.baeldung.hibernate.pojo.Person;
import com.baeldung.hibernate.pojo.Phone;
import com.baeldung.hibernate.pojo.PointEntity;
import com.baeldung.hibernate.pojo.PolygonEntity;
import com.baeldung.hibernate.pojo.Product;
import com.baeldung.hibernate.pojo.Student;
import com.baeldung.hibernate.pojo.TemporalValues;
import com.baeldung.hibernate.pojo.User;
import com.baeldung.hibernate.pojo.UserProfile;
import com.baeldung.hibernate.pojo.inheritance.Animal;
import com.baeldung.hibernate.pojo.inheritance.Bag;
import com.baeldung.hibernate.pojo.inheritance.Book;
import com.baeldung.hibernate.pojo.inheritance.Car;
import com.baeldung.hibernate.pojo.inheritance.MyEmployee;
import com.baeldung.hibernate.pojo.inheritance.MyProduct;
import com.baeldung.hibernate.pojo.inheritance.Pen;
import com.baeldung.hibernate.pojo.inheritance.Pet;
import com.baeldung.hibernate.pojo.inheritance.Vehicle;
public class HibernateUtil {
private static SessionFactory sessionFactory;
@ -72,6 +98,8 @@ public class HibernateUtil {
metadataSources.addAnnotatedClass(PessimisticLockingCourse.class);
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Customer.class);
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Address.class);
metadataSources.addAnnotatedClass(DeptEmployee.class);
metadataSources.addAnnotatedClass(com.baeldung.hibernate.entities.Department.class);
metadataSources.addAnnotatedClass(OptimisticLockingCourse.class);
metadataSources.addAnnotatedClass(OptimisticLockingStudent.class);

View File

@ -0,0 +1,45 @@
package com.baeldung.hibernate.entities;
import java.util.List;
import javax.persistence.*;
@Entity
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
private String name;
@OneToMany(mappedBy="department")
private List<DeptEmployee> employees;
public Department(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<DeptEmployee> getEmployees() {
return employees;
}
public void setEmployees(List<DeptEmployee> employees) {
this.employees = employees;
}
}

View File

@ -0,0 +1,65 @@
package com.baeldung.hibernate.entities;
import javax.persistence.*;
@Entity
public class DeptEmployee {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
private String employeeNumber;
private String designation;
private String name;
@ManyToOne
private Department department;
public DeptEmployee(String name, String employeeNumber, Department department) {
this.name = name;
this.employeeNumber = employeeNumber;
this.department = department;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getEmployeeNumber() {
return employeeNumber;
}
public void setEmployeeNumber(String employeeNumber) {
this.employeeNumber = employeeNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.hibernate.pojo;
public class Result {
private String employeeName;
private String departmentName;
public Result(String employeeName, String departmentName) {
this.employeeName = employeeName;
this.departmentName = departmentName;
}
public Result() {
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
}

View File

@ -0,0 +1,56 @@
package com.baeldung.hibernate.proxy;
import org.hibernate.annotations.BatchSize;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@BatchSize(size = 5)
public class BatchEmployee implements Serializable {
@Id
@GeneratedValue (strategy = GenerationType.SEQUENCE)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
private Boss boss;
@Column(name = "name")
private String name;
@Column(name = "surname")
private String surname;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Boss getBoss() {
return boss;
}
public void setBoss(Boss boss) {
this.boss = boss;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}

View File

@ -0,0 +1,49 @@
package com.baeldung.hibernate.proxy;
import javax.persistence.*;
import java.io.Serializable;
@Entity
public class Boss implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "surname")
private String surname;
public Boss() { }
public Boss(String name, String surname) {
this.name = name;
this.surname = surname;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.hibernate.proxy;
import javax.persistence.*;
import java.io.Serializable;
@Entity
public class Employee implements Serializable {
@Id
@GeneratedValue (strategy = GenerationType.SEQUENCE)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
private Boss boss;
@Column(name = "name")
private String name;
@Column(name = "surname")
private String surname;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Boss getBoss() {
return boss;
}
public void setBoss(Boss boss) {
this.boss = boss;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}

View File

@ -0,0 +1,57 @@
package com.baeldung.hibernate.proxy;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.SessionFactoryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static String PROPERTY_FILE_NAME;
public static SessionFactory getSessionFactory(String propertyFileName) throws IOException {
PROPERTY_FILE_NAME = propertyFileName;
if (sessionFactory == null) {
ServiceRegistry serviceRegistry = configureServiceRegistry();
sessionFactory = getSessionFactoryBuilder(serviceRegistry).build();
}
return sessionFactory;
}
private static SessionFactoryBuilder getSessionFactoryBuilder(ServiceRegistry serviceRegistry) {
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
metadataSources.addPackage("com.baeldung.hibernate.proxy");
metadataSources.addAnnotatedClass(Boss.class);
metadataSources.addAnnotatedClass(Employee.class);
Metadata metadata = metadataSources.buildMetadata();
return metadata.getSessionFactoryBuilder();
}
private static ServiceRegistry configureServiceRegistry() throws IOException {
Properties properties = getProperties();
return new StandardServiceRegistryBuilder().applySettings(properties)
.build();
}
private static Properties getProperties() throws IOException {
Properties properties = new Properties();
URL propertiesURL = Thread.currentThread()
.getContextClassLoader()
.getResource(StringUtils.defaultString(PROPERTY_FILE_NAME, "hibernate.properties"));
try (FileInputStream inputStream = new FileInputStream(propertiesURL.getFile())) {
properties.load(inputStream);
}
return properties;
}
}

View File

@ -0,0 +1,77 @@
package com.baeldung.hibernate;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.IOException;
import java.util.List;
import com.baeldung.hibernate.entities.DeptEmployee;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.hibernate.transform.Transformers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.baeldung.hibernate.entities.Department;
import com.baeldung.hibernate.pojo.Result;
public class CustomClassIntegrationTest {
private Session session;
private Transaction transaction;
@BeforeEach
public void setUp() throws IOException {
session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
session.createNativeQuery("delete from manager").executeUpdate();
session.createNativeQuery("delete from department").executeUpdate();
Department department = new Department("Sales");
DeptEmployee employee = new DeptEmployee("John Smith", "001", department);
session.persist(department);
session.persist(employee);
transaction.commit();
transaction = session.beginTransaction();
}
@Test
public void whenAllManagersAreSelected_ThenObjectGraphIsReturned() {
Query<DeptEmployee> query = session.createQuery("from com.baeldung.hibernate.entities.DeptEmployee");
List<DeptEmployee> deptEmployees = query.list();
DeptEmployee deptEmployee = deptEmployees.get(0);
assertEquals("John Smith", deptEmployee.getName());
assertEquals("Sales", deptEmployee.getDepartment().getName());
}
@Test
public void whenIndividualPropertiesAreSelected_ThenObjectArrayIsReturned() {
Query query = session.createQuery("select m.name, m.department.name from com.baeldung.hibernate.entities.DeptEmployee m");
List managers = query.list();
Object[] manager = (Object[]) managers.get(0);
assertEquals("John Smith", manager[0]);
assertEquals("Sales", manager[1]);
}
@Test
public void whenResultConstructorInSelect_ThenListOfResultIsReturned() {
Query<Result> query = session.createQuery("select new com.baeldung.hibernate.pojo.Result(m.name, m.department.name) "
+ "from DeptEmployee m");
List<Result> results = query.list();
Result result = results.get(0);
assertEquals("John Smith", result.getEmployeeName());
assertEquals("Sales", result.getDepartmentName());
}
@Test
public void whenResultTransformerOnQuery_ThenListOfResultIsReturned() {
Query query = session.createQuery("select m.name as employeeName, m.department.name as departmentName "
+ "from com.baeldung.hibernate.entities.DeptEmployee m");
query.setResultTransformer(Transformers.aliasToBean(Result.class));
List<Result> results = query.list();
Result result = results.get(0);
assertEquals("John Smith", result.getEmployeeName());
assertEquals("Sales", result.getDepartmentName());
}
}

View File

@ -0,0 +1,75 @@
package com.baeldung.hibernate.proxy;
import org.hibernate.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import java.io.IOException;
import java.util.List;
import static org.junit.Assert.fail;
public class HibernateProxyUnitTest {
private Session session;
@Before
public void init(){
try {
session = HibernateUtil.getSessionFactory("hibernate.properties")
.openSession();
} catch (HibernateException | IOException e) {
fail("Failed to initiate Hibernate Session [Exception:" + e.toString() + "]");
}
Boss boss = new Boss("Eduard", "Freud");
session.save(boss);
}
@After
public void close(){
if(session != null) {
session.close();
}
}
@Test(expected = NullPointerException.class)
public void givenAnInexistentEmployeeId_whenUseGetMethod_thenReturnNull() {
Employee employee = session.get(Employee.class, new Long(14));
assertNull(employee);
employee.getId();
}
@Test
public void givenAnInexistentEmployeeId_whenUseLoadMethod_thenReturnAProxy() {
Employee employee = session.load(Employee.class, new Long(14));
assertNotNull(employee);
}
@Test
public void givenABatchEmployeeList_whenSaveOne_thenSaveTheWholeBatch() {
Transaction transaction = session.beginTransaction();
for (long i = 1; i <= 5; i++) {
Employee employee = new Employee();
employee.setName("Employee " + i);
session.save(employee);
}
//After this line is possible to see all the insertions in the logs
session.flush();
session.clear();
transaction.commit();
transaction = session.beginTransaction();
List<Employee> employeeList = session.createQuery("from Employee")
.setCacheMode(CacheMode.IGNORE).getResultList();
assertEquals(employeeList.size(), 5);
transaction.commit();
}
}

View File

@ -105,7 +105,7 @@
<commons-lang3.version>3.5</commons-lang3.version>
<lombok.version>1.16.12</lombok.version>
<vavr.version>0.9.0</vavr.version>
<protonpack.version>1.13</protonpack.version>
<protonpack.version>1.15</protonpack.version>
<streamex.version>0.6.5</streamex.version>
<joda.version>2.10</joda.version>
<!-- testing -->

View File

@ -0,0 +1,209 @@
package com.baeldung.protonpack;
import com.codepoetics.protonpack.Indexed;
import com.codepoetics.protonpack.StreamUtils;
import com.codepoetics.protonpack.collectors.CollectorUtils;
import com.codepoetics.protonpack.collectors.NonUniqueValueException;
import com.codepoetics.protonpack.selectors.Selector;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.util.Arrays.asList;
import static java.util.Arrays.stream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@SuppressWarnings("unchecked")
public class ProtonpackUnitTest {
@Test
public void whenTakeWhile_thenTakenWhile() {
Stream<Integer> streamOfInt = Stream.iterate(1, i -> i + 1);
List<Integer> result = StreamUtils.takeWhile(streamOfInt, i -> i < 5).collect(Collectors.toList());
assertThat(result).contains(1, 2, 3, 4);
}
@Test
public void whenTakeUntil_thenTakenUntil() {
Stream<Integer> streamOfInt = Stream.iterate(1, i -> i + 1);
List<Integer> result = StreamUtils.takeUntil(streamOfInt, i -> i > 50).collect(Collectors.toList());
assertThat(result).contains(10, 20, 30, 40);
}
@Test
public void givenMultipleStream_whenZipped_thenZipped() {
String[] clubs = { "Juventus", "Barcelona", "Liverpool", "PSG" };
String[] players = { "Ronaldo", "Messi", "Salah" };
Set<String> zippedFrom2Sources = StreamUtils.zip(stream(clubs), stream(players), (club, player) -> club + " " + player)
.collect(Collectors.toSet());
assertThat(zippedFrom2Sources).contains("Juventus Ronaldo", "Barcelona Messi", "Liverpool Salah");
String[] leagues = { "Serie A", "La Liga", "Premier League" };
Set<String> zippedFrom3Sources = StreamUtils.zip(stream(clubs), stream(players), stream(leagues),
(club, player, league) -> club + " " + player + " " + league).collect(Collectors.toSet());
assertThat(zippedFrom3Sources).contains("Juventus Ronaldo Serie A", "Barcelona Messi La Liga",
"Liverpool Salah Premier League");
}
@Test
public void whenZippedWithIndex_thenZippedWithIndex() {
Stream<String> streamOfClubs = Stream.of("Juventus", "Barcelona", "Liverpool");
Set<Indexed<String>> zipsWithIndex = StreamUtils.zipWithIndex(streamOfClubs).collect(Collectors.toSet());
assertThat(zipsWithIndex).contains(Indexed.index(0, "Juventus"), Indexed.index(1, "Barcelona"),
Indexed.index(2, "Liverpool"));
}
@Test
public void givenMultipleStream_whenMerged_thenMerged() {
Stream<String> streamOfClubs = Stream.of("Juventus", "Barcelona", "Liverpool", "PSG");
Stream<String> streamOfPlayers = Stream.of("Ronaldo", "Messi", "Salah");
Stream<String> streamOfLeagues = Stream.of("Serie A", "La Liga", "Premier League");
Set<String> merged = StreamUtils.merge(() -> "", (valOne, valTwo) -> valOne + " " + valTwo, streamOfClubs,
streamOfPlayers, streamOfLeagues).collect(Collectors.toSet());
assertThat(merged).contains(" Juventus Ronaldo Serie A", " Barcelona Messi La Liga", " Liverpool Salah Premier League",
" PSG");
}
@Test
public void givenMultipleStream_whenMergedToList_thenMergedToList() {
Stream<String> streamOfClubs = Stream.of("Juventus", "Barcelona", "PSG");
Stream<String> streamOfPlayers = Stream.of("Ronaldo", "Messi");
List<List<String>> mergedListOfList = StreamUtils.mergeToList(streamOfClubs, streamOfPlayers)
.collect(Collectors.toList());
assertThat(mergedListOfList.get(0)).isInstanceOf(List.class);
assertThat(mergedListOfList.get(0)).containsExactly("Juventus", "Ronaldo");
assertThat(mergedListOfList.get(1)).containsExactly("Barcelona", "Messi");
assertThat(mergedListOfList.get(2)).containsExactly("PSG");
}
@Test
public void givenMultipleStream_whenInterleaved_thenInterleaved() {
Stream<String> streamOfClubs = Stream.of("Juventus", "Barcelona", "Liverpool");
Stream<String> streamOfPlayers = Stream.of("Ronaldo", "Messi");
Stream<String> streamOfLeagues = Stream.of("Serie A", "La Liga");
AtomicInteger counter = new AtomicInteger(0);
Selector roundRobinSelector = (o) -> {
Object[] vals = (Object[]) o;
while (counter.get() >= vals.length || vals[counter.get()] == null) {
if (counter.incrementAndGet() >= vals.length)
counter.set(0);
}
return counter.getAndIncrement();
};
Stream<String> interleavedStream = StreamUtils.interleave(roundRobinSelector, streamOfClubs, streamOfPlayers,
streamOfLeagues);
List<String> interleavedList = interleavedStream.collect(Collectors.toList());
assertThat(interleavedList).containsExactly("Juventus", "Ronaldo", "Serie A", "Barcelona", "Messi", "La Liga",
"Liverpool");
}
@Test
public void whenSkippedUntil_thenSkippedUntil() {
Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
List<Integer> skippedUntilGreaterThan5 = StreamUtils.skipUntil(stream(numbers), i -> i > 5).collect(Collectors.toList());
assertThat(skippedUntilGreaterThan5).containsExactly(6, 7, 8, 9, 10);
List<Integer> skippedUntilLessThanEquals5 = StreamUtils.skipUntil(stream(numbers), i -> i <= 5)
.collect(Collectors.toList());
assertThat(skippedUntilLessThanEquals5).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
}
@Test
public void whenSkippedWhile_thenSkippedWhile() {
Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
List<Integer> skippedWhileLessThanEquals5 = StreamUtils.skipWhile(stream(numbers), i -> i <= 5)
.collect(Collectors.toList());
assertThat(skippedWhileLessThanEquals5).containsExactly(6, 7, 8, 9, 10);
List<Integer> skippedWhileGreaterThan5 = StreamUtils.skipWhile(stream(numbers), i -> i > 5).collect(Collectors.toList());
assertThat(skippedWhileGreaterThan5).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
}
@Test
public void givenFibonacciGenerator_whenUnfolded_thenUnfolded() {
AtomicInteger lastValue = new AtomicInteger(0);
Function<Integer, Optional<Integer>> fibonacciGenerator = (i) -> (i < 10) ?
Optional.of(i + lastValue.getAndSet(i)) :
Optional.empty();
List<Integer> fib = StreamUtils.unfold(1, fibonacciGenerator).collect(Collectors.toList());
assertThat(fib).containsExactly(1, 1, 2, 3, 5, 8, 13);
}
@Test
public void whenWindowed_thenWindowed() {
Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7 };
List<List<Integer>> windowedWithSkip1 = StreamUtils.windowed(stream(numbers), 3, 1).collect(Collectors.toList());
assertThat(windowedWithSkip1).containsExactly(asList(1, 2, 3), asList(2, 3, 4), asList(3, 4, 5), asList(4, 5, 6),
asList(5, 6, 7));
List<List<Integer>> windowedWithSkip2 = StreamUtils.windowed(stream(numbers), 3, 2).collect(Collectors.toList());
assertThat(windowedWithSkip2).containsExactly(asList(1, 2, 3), asList(3, 4, 5), asList(5, 6, 7));
}
@Test
public void whenAggregated_thenAggregated() {
Integer[] numbers = { 1, 2, 2, 3, 4, 4, 4, 5 };
List<List<Integer>> aggregated = StreamUtils.aggregate(stream(numbers), (int1, int2) -> int1.compareTo(int2) == 0)
.collect(Collectors.toList());
assertThat(aggregated).containsExactly(asList(1), asList(2, 2), asList(3), asList(4, 4, 4), asList(5));
List<List<Integer>> aggregatedFixSize = StreamUtils.aggregate(stream(numbers), 5).collect(Collectors.toList());
assertThat(aggregatedFixSize).containsExactly(asList(1, 2, 2, 3, 4), asList(4, 4, 5));
}
@Test
public void whenGroupedRun_thenGroupedRun() {
Integer[] numbers = { 1, 1, 2, 3, 4, 4, 5 };
List<List<Integer>> grouped = StreamUtils.groupRuns(stream(numbers)).collect(Collectors.toList());
assertThat(grouped).containsExactly(asList(1, 1), asList(2), asList(3), asList(4, 4), asList(5));
Integer[] numbers2 = { 1, 2, 3, 1 };
List<List<Integer>> grouped2 = StreamUtils.groupRuns(stream(numbers2)).collect(Collectors.toList());
assertThat(grouped2).containsExactly(asList(1), asList(2), asList(3), asList(1));
}
@Test
public void whenAggregatedOnListCondition_thenAggregatedOnListCondition() {
Integer[] numbers = { 1, 1, 2, 3, 4, 4, 5 };
Stream<List<Integer>> aggregated = StreamUtils.aggregateOnListCondition(stream(numbers),
(currentList, nextInt) -> currentList.stream().mapToInt(Integer::intValue).sum() + nextInt <= 5);
assertThat(aggregated).containsExactly(asList(1, 1, 2), asList(3), asList(4), asList(4), asList(5));
}
@Test
public void givenProjectionFunction_whenMaxedBy_thenMaxedBy() {
Stream<String> clubs = Stream.of("Juventus", "Barcelona", "PSG");
Optional<String> longestName = clubs.collect(CollectorUtils.maxBy(String::length));
assertThat(longestName.get()).isEqualTo("Barcelona");
}
@Test
public void givenStreamOfMultipleElem_whenUniqueCollector_thenValueReturned() {
Stream<Integer> singleElement = Stream.of(1);
Optional<Integer> unique = singleElement.collect(CollectorUtils.unique());
assertThat(unique.get()).isEqualTo(1);
}
@Test
public void givenStreamOfMultipleElem_whenUniqueCollector_thenExceptionThrown() {
Stream<Integer> multipleElement = Stream.of(1, 2, 3);
assertThatExceptionOfType(NonUniqueValueException.class).isThrownBy(() -> {
multipleElement.collect(CollectorUtils.unique());
});
}
}

View File

@ -15,7 +15,7 @@ import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
public class BenchmarkUnitTest
public class BenchmarkManualTest
{
public void

View File

@ -47,11 +47,21 @@
<artifactId>jmh-core</artifactId>
<version>${jmh-core.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-core.version}</version>
</dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>${icu4j.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>com.vdurmont</groupId>
@ -92,6 +102,7 @@
<assertj.version>3.6.1</assertj.version>
<jmh-core.version>1.19</jmh-core.version>
<icu4j.version>61.1</icu4j.version>
<guava.version>26.0-jre</guava.version>
</properties>
</project>

View File

@ -2,6 +2,7 @@ package com.baeldung.string;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -32,5 +33,12 @@ public class JoinerSplitter {
.mapToObj(item -> (char) item)
.collect(Collectors.toList());
}
public static Map<String, String> arrayToMap(String[] arrayOfString) {
return Arrays.asList(arrayOfString)
.stream()
.map(str -> str.split(":"))
.collect(Collectors.<String[], String, String>toMap(str -> str[0], str -> str[1]));
}
}

View File

@ -0,0 +1,163 @@
package com.baeldung.string;
import org.apache.commons.lang3.StringUtils;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.List;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.SingleShotTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Measurement(batchSize = 10000, iterations = 10)
@Warmup(batchSize = 10000, iterations = 10)
public class StringPerformance extends StringPerformanceHints {
@Benchmark
public String benchmarkStringDynamicConcat() {
return dynamicConcat();
}
@Benchmark
public StringBuilder benchmarkStringBuilder() {
StringBuilder stringBuilder = new StringBuilder(result);
stringBuilder.append(baeldung);
return stringBuilder;
}
@Benchmark
public StringBuffer benchmarkStringBuffer() {
StringBuffer stringBuffer = new StringBuffer(result);
stringBuffer.append(baeldung);
return stringBuffer;
}
@Benchmark
public String benchmarkStringConstructor() {
return stringConstructor();
}
@Benchmark
public String benchmarkStringLiteral() {
return stringLiteral();
}
@Benchmark
public String benchmarkStringFormat_s() {
return stringFormat_s();
}
@Benchmark
public String benchmarkStringConcat() {
return stringConcat();
}
@Benchmark
public String benchmarkStringIntern() {
return stringIntern();
}
@Benchmark
public String benchmarkStringReplace() {
return longString.replace("average", " average !!!");
}
@Benchmark
public String benchmarkStringUtilsReplace() {
return StringUtils.replace(longString, "average", " average !!!");
}
@Benchmark
public List<String> benchmarkGuavaSplitter() {
return guavaSplitter();
}
@Benchmark
public String [] benchmarkStringSplit() {
return stringSplit();
}
@Benchmark
public String [] benchmarkStringSplitPattern() {
return stringSplitPattern();
}
@Benchmark
public List benchmarkStringTokenizer() {
return stringTokenizer();
}
@Benchmark
public List benchmarkStringIndexOf() {
return stringIndexOf();
}
@Benchmark
public String benchmarkIntegerToString() {
return stringIntegerToString();
}
@Benchmark
public String benchmarkStringValueOf() {
return stringValueOf();
}
@Benchmark
public String benchmarkStringConvertPlus() {
return stringConvertPlus();
}
@Benchmark
public String benchmarkStringFormat_d() {
return stringFormat_d();
}
@Benchmark
public boolean benchmarkStringEquals() {
return stringEquals();
}
@Benchmark
public boolean benchmarkStringEqualsIgnoreCase() {
return stringEqualsIgnoreCase();
}
@Benchmark
public boolean benchmarkStringMatches() {
return stringIsMatch();
}
@Benchmark
public boolean benchmarkPrecompiledMatches() {
return precompiledMatches();
}
@Benchmark
public int benchmarkStringCompareTo() {
return stringCompareTo();
}
@Benchmark
public boolean benchmarkStringIsEmpty() {
return stringIsEmpty();
}
@Benchmark
public boolean benchmarkStringLengthZero() {
return stringLengthZero();
}
public static void main(String[] args) throws Exception {
Options options = new OptionsBuilder()
.include(StringPerformance.class.getSimpleName()).threads(1)
.forks(1).shouldFailOnError(true)
.shouldDoGC(true)
.jvmArgs("-server").build();
new Runner(options).run();
}
}

View File

@ -0,0 +1,133 @@
package com.baeldung.string;
import com.google.common.base.Splitter;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
@State(Scope.Thread)
public class StringPerformanceHints {
protected String baeldung = "baeldung";
protected String longString = "Hello baeldung, I am a bit longer than other Strings";
protected String formatString = "hello %s, nice to meet you";
protected String formatDigit = "%d";
protected String emptyString = " ";
protected String result = "";
protected int sampleNumber = 100;
protected Pattern spacePattern = Pattern.compile(emptyString);
protected Pattern longPattern = Pattern.compile(longString);
protected List<String> stringSplit = new ArrayList<>();
protected List<String> stringTokenizer = new ArrayList<>();
protected String dynamicConcat() {
result += baeldung;
return result;
}
protected String stringConstructor() {
return new String(baeldung);
}
protected String stringLiteral() {
result = baeldung;
return result;
}
protected String stringFormat_s() {
return String.format(formatString, baeldung);
}
protected String stringFormat_d() {
return String.format(formatDigit, sampleNumber);
}
protected String stringConcat() {
result = result.concat(baeldung);
return result;
}
protected List stringTokenizer() {
StringTokenizer st = new StringTokenizer(longString);
while (st.hasMoreTokens()) {
stringTokenizer.add(st.nextToken());
}
return stringTokenizer;
}
protected List stringIndexOf() {
int pos = 0, end;
while ((end = longString.indexOf(' ', pos)) >= 0) {
stringSplit.add(longString.substring(pos, end));
pos = end + 1;
}
return stringSplit;
}
protected String stringIntegerToString() {
return Integer.toString(sampleNumber);
}
protected String stringValueOf() {
return String.valueOf(sampleNumber);
}
protected String stringConvertPlus() {
return sampleNumber + "";
}
protected boolean stringEquals() {
return longString.equals(baeldung);
}
protected boolean stringEqualsIgnoreCase() {
return longString.equalsIgnoreCase(baeldung);
}
protected boolean stringIsMatch() {
return longString.matches(baeldung);
}
protected boolean precompiledMatches() {
return longPattern.matcher(baeldung).matches();
}
protected int stringCompareTo() {
return longString.compareTo(baeldung);
}
protected boolean stringIsEmpty() {
return longString.isEmpty();
}
protected boolean stringLengthZero() {
return longString.length() == 0;
}
protected String [] stringSplitPattern() {
return spacePattern.split(longString, 0);
}
protected String [] stringSplit() {
return longString.split(emptyString);
}
protected List<String> guavaSplitter() {
return Splitter.on(" ").trimResults()
.omitEmptyStrings()
.splitToList(longString);
}
protected String stringIntern() {
return baeldung.intern();
}
}

View File

@ -3,7 +3,9 @@ package com.baeldung.string;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
@ -62,5 +64,20 @@ public class JoinerSplitterUnitTest {
assertEquals(result, expectation);
}
@Test
public void givenStringArray_transformedToStream_convertToMap() {
String[] programming_languages = new String[] {"language:java","os:linux","editor:emacs"};
Map<String,String> expectation=new HashMap<>();
expectation.put("language", "java");
expectation.put("os", "linux");
expectation.put("editor", "emacs");
Map<String, String> result = JoinerSplitter.arrayToMap(programming_languages);
assertEquals(result, expectation);
}
}

View File

@ -2,10 +2,11 @@ package com.baeldung.string;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import com.google.common.base.Splitter;
@ -54,4 +55,18 @@ public class SplitUnitTest {
assertThat(resultList)
.containsExactly("car", "jeep", "scooter");
}
@Test
public void givenStringContainsSpaces_whenSplitAndTrim_thenReturnsArray_using_Regex() {
assertThat(" car , jeep, scooter ".trim()
.split("\\s*,\\s*")).containsExactly("car", "jeep", "scooter");
}
@Test
public void givenStringContainsSpaces_whenSplitAndTrim_thenReturnsArray_using_java_8() {
assertThat(Arrays.stream(" car , jeep, scooter ".split(","))
.map(String::trim)
.toArray(String[]::new)).containsExactly("car", "jeep", "scooter");
}
}

View File

@ -0,0 +1,51 @@
package com.baeldung.string;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.text.IsEmptyString.isEmptyOrNullString;
import static org.hamcrest.text.IsEmptyString.isEmptyString;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import com.google.common.base.Strings;
public class StringEmptyUnitTest {
private String text = "baeldung";
@Test
public void givenAString_whenCheckedForEmptyUsingJunit_shouldAssertSuccessfully() {
assertTrue(!text.isEmpty());
assertFalse(text.isEmpty());
assertNotEquals("", text);
assertNotSame("", text);
}
@Test
public void givenAString_whenCheckedForEmptyUsingHamcrest_shouldAssertSuccessfully() {
assertThat(text, not(isEmptyString()));
assertThat(text, not(isEmptyOrNullString()));
}
@Test
public void givenAString_whenCheckedForEmptyUsingCommonsLang_shouldAssertSuccessfully() {
assertTrue(StringUtils.isNotBlank(text));
}
@Test
public void givenAString_whenCheckedForEmptyUsingAssertJ_shouldAssertSuccessfully() {
Assertions.assertThat(text).isNotEmpty();
}
@Test
public void givenAString_whenCheckedForEmptyUsingGuava_shouldAssertSuccessfully() {
assertFalse(Strings.isNullOrEmpty(text));
}
}

View File

@ -39,6 +39,11 @@
<artifactId>jersey-mvc-freemarker</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>

View File

@ -1,12 +1,14 @@
package com.baeldung.jersey.server.config;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.server.mvc.freemarker.FreemarkerMvcFeature;
public class ViewApplicationConfig extends ResourceConfig {
public ViewApplicationConfig() {
packages("com.baeldung.jersey.server");
property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
property(FreemarkerMvcFeature.TEMPLATE_BASE_PATH, "templates/freemarker");
register(FreemarkerMvcFeature.class);;
}

View File

@ -0,0 +1,35 @@
package com.baeldung.jersey.server.constraints;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.regex.Pattern;
import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = { SerialNumber.Validator.class })
public @interface SerialNumber {
String message()
default "Fruit serial number is not valid";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
public class Validator implements ConstraintValidator<SerialNumber, String> {
@Override
public void initialize(final SerialNumber serial) {
}
@Override
public boolean isValid(final String serial, final ConstraintValidatorContext constraintValidatorContext) {
final String serialNumRegex = "^\\d{3}-\\d{3}-\\d{4}$";
return Pattern.matches(serialNumRegex, serial);
}
}
}

View File

@ -1,20 +1,57 @@
package com.baeldung.jersey.server.model;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Fruit {
private final String name;
private final String colour;
@Min(value = 10, message = "Fruit weight must be 10 or greater")
private Integer weight;
@Size(min = 5, max = 200)
private String name;
@Size(min = 5, max = 200)
private String colour;
private String serial;
public Fruit() {
}
public Fruit(String name, String colour) {
this.name = name;
this.colour = colour;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setColour(String colour) {
this.colour = colour;
}
public String getColour() {
return colour;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
public String getSerial() {
return serial;
}
public void setSerial(String serial) {
this.serial = serial;
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.jersey.server.providers;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
public class FruitExceptionMapper implements ExceptionMapper<ConstraintViolationException> {
@Override
public Response toResponse(final ConstraintViolationException exception) {
return Response.status(Response.Status.BAD_REQUEST)
.entity(prepareMessage(exception))
.type("text/plain")
.build();
}
private String prepareMessage(ConstraintViolationException exception) {
final StringBuilder message = new StringBuilder();
for (ConstraintViolation<?> cv : exception.getConstraintViolations()) {
message.append(cv.getPropertyPath() + " " + cv.getMessage() + "\n");
}
return message.toString();
}
}

View File

@ -5,7 +5,13 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
@ -15,7 +21,9 @@ import org.glassfish.jersey.server.mvc.ErrorTemplate;
import org.glassfish.jersey.server.mvc.Template;
import org.glassfish.jersey.server.mvc.Viewable;
import com.baeldung.jersey.server.constraints.SerialNumber;
import com.baeldung.jersey.server.model.Fruit;
import com.baeldung.jersey.service.SimpleStorageService;
@Path("/fruit")
public class FruitResource {
@ -52,4 +60,49 @@ public class FruitResource {
return name;
}
@POST
@Path("/create")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void createFruit(
@NotNull(message = "Fruit name must not be null") @FormParam("name") String name,
@NotNull(message = "Fruit colour must not be null") @FormParam("colour") String colour) {
Fruit fruit = new Fruit(name, colour);
SimpleStorageService.storeFruit(fruit);
}
@PUT
@Path("/update")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void updateFruit(@SerialNumber @FormParam("serial") String serial) {
Fruit fruit = new Fruit();
fruit.setSerial(serial);
SimpleStorageService.storeFruit(fruit);
}
@POST
@Path("/create")
@Consumes(MediaType.APPLICATION_JSON)
public void createFruit(@Valid Fruit fruit) {
SimpleStorageService.storeFruit(fruit);
}
@GET
@Valid
@Produces(MediaType.APPLICATION_JSON)
@Path("/search/{name}")
public Fruit findFruitByName(@PathParam("name") String name) {
return SimpleStorageService.findByName(name);
}
@GET
@Produces(MediaType.TEXT_HTML)
@Path("/exception")
@Valid
public Fruit exception() {
Fruit fruit = new Fruit();
fruit.setName("a");
fruit.setColour("b");
return fruit;
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.jersey.service;
import java.util.HashMap;
import java.util.Map;
import com.baeldung.jersey.server.model.Fruit;
public class SimpleStorageService {
private static final Map<String, Fruit> fruits = new HashMap<String, Fruit>();
public static void storeFruit(final Fruit fruit) {
fruits.put(fruit.getName(), fruit);
}
public static Fruit findByName(final String name) {
return fruits.entrySet()
.stream()
.filter(map -> name.equals(map.getKey()))
.map(map -> map.getValue())
.findFirst()
.get();
}
}

View File

@ -2,41 +2,116 @@ package com.baeldung.jersey.server.rest;
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Assert;
import org.glassfish.jersey.test.TestProperties;
import org.junit.Test;
import com.baeldung.jersey.server.config.ViewApplicationConfig;
import com.baeldung.jersey.server.model.Fruit;
import com.baeldung.jersey.server.providers.FruitExceptionMapper;
public class FruitResourceIntegrationTest extends JerseyTest {
@Override
protected Application configure() {
return new ViewApplicationConfig();
enable(TestProperties.LOG_TRAFFIC);
enable(TestProperties.DUMP_ENTITY);
ViewApplicationConfig config = new ViewApplicationConfig();
config.register(FruitExceptionMapper.class);
return config;
}
@Test
public void testAllFruit() {
public void givenGetAllFruit_whenCorrectRequest_thenAllTemplateInvoked() {
final String response = target("/fruit/all").request()
.get(String.class);
Assert.assertThat(response, allOf(containsString("banana"), containsString("apple"), containsString("kiwi")));
assertThat(response, allOf(containsString("banana"), containsString("apple"), containsString("kiwi")));
}
@Test
public void testIndex() {
public void givenGetFruit_whenCorrectRequest_thenIndexTemplateInvoked() {
final String response = target("/fruit").request()
.get(String.class);
Assert.assertThat(response, containsString("Welcome Fruit Index Page!"));
assertThat(response, containsString("Welcome Fruit Index Page!"));
}
@Test
public void testErrorTemplate() {
public void givenGetFruitByName_whenFruitUnknown_thenErrorTemplateInvoked() {
final String response = target("/fruit/orange").request()
.get(String.class);
Assert.assertThat(response, containsString("Error - Fruit not found: orange!"));
assertThat(response, containsString("Error - Fruit not found: orange!"));
}
@Test
public void givenCreateFruit_whenFormContainsNullParam_thenResponseCodeIsBadRequest() {
Form form = new Form();
form.param("name", "apple");
form.param("colour", null);
Response response = target("fruit/create").request(MediaType.APPLICATION_FORM_URLENCODED)
.post(Entity.form(form));
assertEquals("Http Response should be 400 ", 400, response.getStatus());
assertThat(response.readEntity(String.class), containsString("Fruit colour must not be null"));
}
@Test
public void givenUpdateFruit_whenFormContainsBadSerialParam_thenResponseCodeIsBadRequest() {
Form form = new Form();
form.param("serial", "2345-2345");
Response response = target("fruit/update").request(MediaType.APPLICATION_FORM_URLENCODED)
.put(Entity.form(form));
assertEquals("Http Response should be 400 ", 400, response.getStatus());
assertThat(response.readEntity(String.class), containsString("Fruit serial number is not valid"));
}
@Test
public void givenCreateFruit_whenFruitIsInvalid_thenResponseCodeIsBadRequest() {
Fruit fruit = new Fruit("Blueberry", "purple");
fruit.setWeight(1);
Response response = target("fruit/create").request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(fruit, MediaType.APPLICATION_JSON_TYPE));
assertEquals("Http Response should be 400 ", 400, response.getStatus());
assertThat(response.readEntity(String.class), containsString("Fruit weight must be 10 or greater"));
}
@Test
public void givenFruitExists_whenSearching_thenResponseContainsFruit() {
Fruit fruit = new Fruit();
fruit.setName("strawberry");
fruit.setWeight(20);
Response response = target("fruit/create").request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(fruit, MediaType.APPLICATION_JSON_TYPE));
assertEquals("Http Response should be 204 ", 204, response.getStatus());
final String json = target("fruit/search/strawberry").request()
.get(String.class);
assertThat(json, containsString("{\"name\":\"strawberry\",\"weight\":20}"));
}
@Test
public void givenFruit_whenFruitIsInvalid_thenReponseContainsCustomExceptions() {
final Response response = target("fruit/exception").request()
.get();
assertEquals("Http Response should be 400 ", 400, response.getStatus());
String responseString = response.readEntity(String.class);
assertThat(responseString, containsString("exception.<return value>.colour size must be between 5 and 200"));
assertThat(responseString, containsString("exception.<return value>.name size must be between 5 and 200"));
}
}

View File

@ -6,5 +6,4 @@
- [Writing Specifications with Kotlin and Spek](http://www.baeldung.com/kotlin-spek)
- [Processing JSON with Kotlin and Klaxson](http://www.baeldung.com/kotlin-json-klaxson)
- [Kotlin with Ktor](http://www.baeldung.com/kotlin-ktor)
- [Idiomatic Logging in Kotlin](http://www.baeldung.com/kotlin-logging)
- [Guide to the Kotlin Exposed Framework](https://www.baeldung.com/kotlin-exposed-persistence)
- [Guide to the Kotlin Exposed Framework](https://www.baeldung.com/kotlin-exposed-persistence)

View File

@ -147,7 +147,6 @@
<artifactId>jmapper-core</artifactId>
<version>${jmapper.version}</version>
</dependency>
<!-- crunch project -->
<dependency>
<groupId>org.apache.crunch</groupId>
@ -185,7 +184,72 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-connector-kafka-0.11_2.11</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-java_2.11</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-core</artifactId>
<version>${flink.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-java</artifactId>
<version>${flink.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-test-utils_2.11</artifactId>
<version>${flink.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility-proxy</artifactId>
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -336,6 +400,10 @@
<ignite.version>2.4.0</ignite.version>
<gson.version>2.8.2</gson.version>
<cache.version>1.1.0</cache.version>
<flink.version>1.5.0</flink.version>
<jackson.version>2.8.5</jackson.version>
<awaitility.version>3.0.0</awaitility.version>
<assertj.version>3.6.2</assertj.version>
<hazelcast.version>3.8.4</hazelcast.version>
<maven-antrun-plugin.version>1.8</maven-antrun-plugin.version>
<build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version>

View File

@ -18,7 +18,6 @@ public class InputMessage {
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}

View File

@ -1,8 +1,6 @@
package com.baeldung.flink.schema;
import com.baeldung.flink.model.InputMessage;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.apache.flink.api.common.serialization.DeserializationSchema;

Some files were not shown because too many files have changed in this diff Show More