fix conflicts
This commit is contained in:
commit
1b4a7953a5
|
@ -14,7 +14,5 @@
|
|||
- [Calculate Factorial in Java](https://www.baeldung.com/java-calculate-factorial)
|
||||
- [Find Substrings That Are Palindromes in Java](https://www.baeldung.com/java-palindrome-substrings)
|
||||
- [Find the Longest Substring without Repeating Characters](https://www.baeldung.com/java-longest-substring-without-repeated-characters)
|
||||
- [Java Two Pointer Technique](https://www.baeldung.com/java-two-pointer-technique)
|
||||
- [Permutations of an Array in Java](https://www.baeldung.com/java-array-permutations)
|
||||
- [Implementing Simple State Machines with Java Enums](https://www.baeldung.com/java-enum-simple-state-machine)
|
||||
- [Generate Combinations in Java](https://www.baeldung.com/java-combinations-algorithm)
|
||||
|
|
|
@ -8,9 +8,6 @@
|
|||
- [Create a Sudoku Solver in Java](http://www.baeldung.com/java-sudoku)
|
||||
- [Displaying Money Amounts in Words](http://www.baeldung.com/java-money-into-words)
|
||||
- [A Collaborative Filtering Recommendation System in Java](http://www.baeldung.com/java-collaborative-filtering-recommendations)
|
||||
- [Converting Between Roman and Arabic Numerals in Java](http://www.baeldung.com/java-convert-roman-arabic)
|
||||
- [Practical Java Examples of the Big O Notation](http://www.baeldung.com/java-algorithm-complexity)
|
||||
- [An Introduction to the Theory of Big-O Notation](http://www.baeldung.com/big-o-notation)
|
||||
- [Check If Two Rectangles Overlap In Java](https://www.baeldung.com/java-check-if-two-rectangles-overlap)
|
||||
- [Calculate the Distance Between Two Points in Java](https://www.baeldung.com/java-distance-between-two-points)
|
||||
- [Find the Intersection of Two Lines in Java](https://www.baeldung.com/java-intersection-of-two-lines)
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
/target/
|
||||
.settings/
|
||||
.classpath
|
||||
.project
|
|
@ -0,0 +1,7 @@
|
|||
## Relevant articles:
|
||||
|
||||
- [Java Two Pointer Technique](https://www.baeldung.com/java-two-pointer-technique)
|
||||
- [Implementing Simple State Machines with Java Enums](https://www.baeldung.com/java-enum-simple-state-machine)
|
||||
- [Converting Between Roman and Arabic Numerals in Java](http://www.baeldung.com/java-convert-roman-arabic)
|
||||
- [Practical Java Examples of the Big O Notation](http://www.baeldung.com/java-algorithm-complexity)
|
||||
- [An Introduction to the Theory of Big-O Notation](http://www.baeldung.com/big-o-notation)
|
|
@ -0,0 +1,39 @@
|
|||
<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>
|
||||
<artifactId>algorithms-miscellaneous-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>algorithms-miscellaneous-3</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${org.assertj.core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>${exec-maven-plugin.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -4,3 +4,8 @@
|
|||
|
||||
### Relevant Articles:
|
||||
- [Set Operations in Java](http://www.baeldung.com/set-operations-in-java)
|
||||
- [HashSet and TreeSet Comparison](http://www.baeldung.com/java-hashset-vs-treeset)
|
||||
- [A Guide to HashSet in Java](http://www.baeldung.com/java-hashset)
|
||||
- [A Guide to TreeSet in Java](http://www.baeldung.com/java-tree-set)
|
||||
- [Initializing HashSet at the Time of Construction](http://www.baeldung.com/java-initialize-hashset)
|
||||
- [Guide to EnumSet](https://www.baeldung.com/java-enumset)
|
|
@ -4,14 +4,10 @@
|
|||
|
||||
### Relevant Articles:
|
||||
- [Java – Combine Multiple Collections](http://www.baeldung.com/java-combine-multiple-collections)
|
||||
- [HashSet and TreeSet Comparison](http://www.baeldung.com/java-hashset-vs-treeset)
|
||||
- [Collect a Java Stream to an Immutable Collection](http://www.baeldung.com/java-stream-immutable-collection)
|
||||
- [Introduction to the Java ArrayDeque](http://www.baeldung.com/java-array-deque)
|
||||
- [A Guide to HashSet in Java](http://www.baeldung.com/java-hashset)
|
||||
- [A Guide to TreeSet in Java](http://www.baeldung.com/java-tree-set)
|
||||
- [Getting the Size of an Iterable in Java](http://www.baeldung.com/java-iterable-size)
|
||||
- [How to Filter a Collection in Java](http://www.baeldung.com/java-collection-filtering)
|
||||
- [Initializing HashSet at the Time of Construction](http://www.baeldung.com/java-initialize-hashset)
|
||||
- [Removing the First Element of an Array](https://www.baeldung.com/java-array-remove-first-element)
|
||||
- [Fail-Safe Iterator vs Fail-Fast Iterator](http://www.baeldung.com/java-fail-safe-vs-fail-fast-iterator)
|
||||
- [Shuffling Collections In Java](http://www.baeldung.com/java-shuffle-collection)
|
||||
|
@ -23,7 +19,6 @@
|
|||
- [Time Complexity of Java Collections](https://www.baeldung.com/java-collections-complexity)
|
||||
- [Operating on and Removing an Item from Stream](https://www.baeldung.com/java-use-remove-item-stream)
|
||||
- [An Introduction to Synchronized Java Collections](https://www.baeldung.com/java-synchronized-collections)
|
||||
- [Guide to EnumSet](https://www.baeldung.com/java-enumset)
|
||||
- [Removing Elements from Java Collections](https://www.baeldung.com/java-collection-remove-elements)
|
||||
- [Combining Different Types of Collections in Java](https://www.baeldung.com/java-combine-collections)
|
||||
- [Sorting in Java](http://www.baeldung.com/java-sorting)
|
||||
|
|
|
@ -46,12 +46,12 @@ public class RestClientLiveManualTest {
|
|||
// old httpClient will throw UnsupportedOperationException
|
||||
@Ignore
|
||||
@Test
|
||||
public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenException_1() throws GeneralSecurityException {
|
||||
public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk_1() throws GeneralSecurityException {
|
||||
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
|
||||
final CloseableHttpClient httpClient = (CloseableHttpClient) requestFactory.getHttpClient();
|
||||
|
||||
TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;
|
||||
|
||||
final TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;
|
||||
|
||||
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, ALLOW_ALL_HOSTNAME_VERIFIER);
|
||||
httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 8443, sf));
|
||||
|
||||
|
@ -61,7 +61,7 @@ public class RestClientLiveManualTest {
|
|||
|
||||
// new httpClient : 4.4 and above
|
||||
@Test
|
||||
public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenException_2() throws GeneralSecurityException {
|
||||
public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk_2() throws GeneralSecurityException {
|
||||
|
||||
final TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;
|
||||
final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
|
||||
|
|
|
@ -0,0 +1,150 @@
|
|||
<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>
|
||||
<artifactId>java-strings-2</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>java-strings-2</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>${commons-codec.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<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>
|
||||
<artifactId>emoji-java</artifactId>
|
||||
<version>${emoji-java.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter-api.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-library</artifactId>
|
||||
<version>${hamcrest-library.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Added for password generation -->
|
||||
<dependency>
|
||||
<groupId>org.passay</groupId>
|
||||
<artifactId>passay</artifactId>
|
||||
<version>${passay.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-text</artifactId>
|
||||
<version>${commons-text.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.ahocorasick</groupId>
|
||||
<artifactId>ahocorasick</artifactId>
|
||||
<version>${ahocorasick.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>java-strings-2</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<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>
|
||||
<compilerArgument>-parameters</compilerArgument>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.8.1</commons-lang3.version>
|
||||
<commons-codec.version>1.10</commons-codec.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<jmh-core.version>1.19</jmh-core.version>
|
||||
<icu4j.version>61.1</icu4j.version>
|
||||
<guava.version>27.0.1-jre</guava.version>
|
||||
<emoji-java.version>4.0.0</emoji-java.version>
|
||||
<junit-jupiter-api.version>5.3.1</junit-jupiter-api.version>
|
||||
<hamcrest-library.version>1.3</hamcrest-library.version>
|
||||
<passay.version>1.3.1</passay.version>
|
||||
<commons-text.version>1.4</commons-text.version>
|
||||
<ahocorasick.version>0.4.0</ahocorasick.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,58 @@
|
|||
package com.baeldung.string.search.performance;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Fork;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.Setup;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
|
||||
/**
|
||||
* Based on https://github.com/tedyoung/indexof-contains-benchmark
|
||||
*/
|
||||
@Fork(5)
|
||||
@State(Scope.Benchmark)
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public class SubstringSearchPerformanceComparison {
|
||||
|
||||
private String message;
|
||||
|
||||
private Pattern pattern;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
org.openjdk.jmh.Main.main(args);
|
||||
}
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum";
|
||||
pattern = Pattern.compile("(?<!\\S)" + "eiusmod" + "(?!\\S)");
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public int indexOf() {
|
||||
return message.indexOf("eiusmod");
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public boolean contains() {
|
||||
return message.contains("eiusmod");
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public boolean containsStringUtilsIgnoreCase() {
|
||||
return StringUtils.containsIgnoreCase(message, "eiusmod");
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public boolean searchWithPattern() {
|
||||
return pattern.matcher(message).find();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -0,0 +1,70 @@
|
|||
package com.baeldung.string.search;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* BAEL-2832: Different ways to check if a Substring could be found in a String.
|
||||
*/
|
||||
public class SubstringSearch {
|
||||
|
||||
@Test
|
||||
public void searchSubstringWithIndexOf() {
|
||||
Assert.assertEquals(9, "Bohemian Rhapsodyan".indexOf("Rhap"));
|
||||
|
||||
// indexOf will return -1, because it's case sensitive
|
||||
Assert.assertEquals(-1, "Bohemian Rhapsodyan".indexOf("rhap"));
|
||||
|
||||
// indexOf will return 9, because it's all lowercase
|
||||
Assert.assertEquals(9, "Bohemian Rhapsodyan".toLowerCase()
|
||||
.indexOf("rhap"));
|
||||
|
||||
// it will return 6, because it's the first occurrence. Sorry Queen for being blasphemic
|
||||
Assert.assertEquals(6, "Bohemian Rhapsodyan".indexOf("an"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void searchSubstringWithContains() {
|
||||
Assert.assertTrue("Hey Ho, let's go".contains("Hey"));
|
||||
|
||||
// contains will return false, because it's case sensitive
|
||||
Assert.assertFalse("Hey Ho, let's go".contains("hey"));
|
||||
|
||||
// contains will return true, because it's all lowercase
|
||||
Assert.assertTrue("Hey Ho, let's go".toLowerCase().contains("hey"));
|
||||
|
||||
// contains will return false, because 'jey' can't be found
|
||||
Assert.assertFalse("Hey Ho, let's go".contains("jey"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void searchSubstringWithStringUtils() {
|
||||
Assert.assertTrue(StringUtils.containsIgnoreCase("Runaway train", "train"));
|
||||
|
||||
// it will also be true, because ignores case ;)
|
||||
Assert.assertTrue(StringUtils.containsIgnoreCase("Runaway train", "Train"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void searchUsingPattern() {
|
||||
|
||||
// We create the Pattern first
|
||||
Pattern pattern = Pattern.compile("(?<!\\S)" + "road" + "(?!\\S)");
|
||||
|
||||
// We need to create the Matcher after
|
||||
Matcher matcher = pattern.matcher("Hit the road Jack");
|
||||
|
||||
// find will return true when the first match is found
|
||||
Assert.assertTrue(matcher.find());
|
||||
|
||||
// We will create a different matcher with a different text
|
||||
matcher = pattern.matcher("and don't you come back no more");
|
||||
|
||||
// find will return false, because 'road' can't be find as a substring
|
||||
Assert.assertFalse(matcher.find());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
|
@ -31,6 +31,22 @@
|
|||
<artifactId>h2</artifactId>
|
||||
<version>${h2database.version}</version>
|
||||
</dependency>
|
||||
<!-- validation -->
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>${hibernate-validator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.el</groupId>
|
||||
<artifactId>javax.el-api</artifactId>
|
||||
<version>${javax.el-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish</groupId>
|
||||
<artifactId>javax.el</artifactId>
|
||||
<version>${org.glassfish.javax.el.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -47,6 +63,9 @@
|
|||
<hibernate.version>5.3.7.Final</hibernate.version>
|
||||
<h2database.version>1.4.196</h2database.version>
|
||||
<assertj-core.version>3.8.0</assertj-core.version>
|
||||
<hibernate-validator.version>5.3.3.Final</hibernate-validator.version>
|
||||
<javax.el-api.version>2.2.5</javax.el-api.version>
|
||||
<org.glassfish.javax.el.version>3.0.1-b08</org.glassfish.javax.el.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -9,7 +9,7 @@ public enum Strategy {
|
|||
//See that the classes belongs to different packages
|
||||
MAP_KEY_COLUMN_BASED(Collections.singletonList(com.baeldung.hibernate.persistmaps.mapkeycolumn.Order.class)),
|
||||
MAP_KEY_BASED(Arrays.asList(com.baeldung.hibernate.persistmaps.mapkey.Item.class,
|
||||
com.baeldung.hibernate.persistmaps.mapkey.Order.class)),
|
||||
com.baeldung.hibernate.persistmaps.mapkey.Order.class,com.baeldung.hibernate.persistmaps.mapkey.User.class)),
|
||||
MAP_KEY_JOIN_COLUMN_BASED(Arrays.asList(com.baeldung.hibernate.persistmaps.mapkeyjoincolumn.Seller.class,
|
||||
com.baeldung.hibernate.persistmaps.mapkeyjoincolumn.Item.class,
|
||||
com.baeldung.hibernate.persistmaps.mapkeyjoincolumn.Order.class)),
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
package com.baeldung.hibernate.persistmaps.mapkey;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
@Entity
|
||||
public class User {
|
||||
@Id
|
||||
@Column(length = 3)
|
||||
private String firstName;
|
||||
|
||||
@Size(min = 3, max = 15)
|
||||
private String middleName;
|
||||
|
||||
@Length(min = 3, max = 15)
|
||||
private String lastName;
|
||||
|
||||
@Column(length = 5)
|
||||
@Size(min = 3, max = 5)
|
||||
private String city;
|
||||
|
||||
public User(String firstName, String middleName, String lastName, String city) {
|
||||
super();
|
||||
this.firstName = firstName;
|
||||
this.middleName = middleName;
|
||||
this.lastName = lastName;
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getMiddleName() {
|
||||
return middleName;
|
||||
}
|
||||
|
||||
public void setMiddleName(String middletName) {
|
||||
this.middleName = middletName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
package com.baeldung.hibernate.validation;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import java.util.Set;
|
||||
import javax.persistence.PersistenceException;
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.ValidatorFactory;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import com.baeldung.hibernate.HibernateUtil;
|
||||
import com.baeldung.hibernate.Strategy;
|
||||
import com.baeldung.hibernate.persistmaps.mapkey.User;
|
||||
|
||||
public class UserValidationUnitTest {
|
||||
|
||||
private static Validator validator;
|
||||
private static SessionFactory sessionFactory;
|
||||
private Session session;
|
||||
private Set<ConstraintViolation<User>> constraintViolations;
|
||||
|
||||
@BeforeClass
|
||||
public static void before() {
|
||||
ValidatorFactory config = Validation.buildDefaultValidatorFactory();
|
||||
validator = config.getValidator();
|
||||
sessionFactory = HibernateUtil.getSessionFactory(Strategy.MAP_KEY_BASED);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidationWithSizeAndInvalidMiddleName_thenConstraintViolation() {
|
||||
User user = new User("john", "m", "butler", "japan");
|
||||
constraintViolations = validator.validateProperty(user, "middleName");
|
||||
assertEquals(constraintViolations.size(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidationWithSizeAndValidMiddleName_thenNoConstraintViolation() {
|
||||
User user = new User("john", "mathis", "butler", "japan");
|
||||
constraintViolations = validator.validateProperty(user, "middleName");
|
||||
assertEquals(constraintViolations.size(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidationWithLengthAndInvalidLastName_thenConstraintViolation() {
|
||||
User user = new User("john", "m", "b", "japan");
|
||||
constraintViolations = validator.validateProperty(user, "lastName");
|
||||
assertEquals(constraintViolations.size(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidationWithLengthAndValidLastName_thenNoConstraintViolation() {
|
||||
User user = new User("john", "mathis", "butler", "japan");
|
||||
constraintViolations = validator.validateProperty(user, "lastName");
|
||||
assertEquals(constraintViolations.size(), 0);
|
||||
}
|
||||
|
||||
@Test(expected = PersistenceException.class)
|
||||
public void whenSavingFirstNameWithInvalidFirstName_thenPersistenceException() {
|
||||
User user = new User("john", "mathis", "butler", "japan");
|
||||
session.save(user);
|
||||
session.getTransaction()
|
||||
.commit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidationWithSizeAndColumnWithValidCity_thenNoConstraintViolation() {
|
||||
User user = new User("john", "mathis", "butler", "japan");
|
||||
constraintViolations = validator.validateProperty(user, "city");
|
||||
assertEquals(constraintViolations.size(), 0);
|
||||
}
|
||||
}
|
4
pom.xml
4
pom.xml
|
@ -339,6 +339,7 @@
|
|||
<module>algorithms-genetic</module>
|
||||
<module>algorithms-miscellaneous-1</module>
|
||||
<module>algorithms-miscellaneous-2</module>
|
||||
<module>algorithms-miscellaneous-3</module>
|
||||
<module>algorithms-sorting</module>
|
||||
<module>animal-sniffer-mvn-plugin</module>
|
||||
<module>annotations</module>
|
||||
|
@ -462,6 +463,7 @@
|
|||
<module>java-streams</module>
|
||||
<module>java-streams-2</module>
|
||||
<module>java-strings</module>
|
||||
<module>java-strings-2</module>
|
||||
<module>java-vavr-stream</module>
|
||||
<module>java-websocket</module>
|
||||
<module>javafx</module>
|
||||
|
@ -1018,6 +1020,7 @@
|
|||
<module>algorithms-genetic</module>
|
||||
<module>algorithms-miscellaneous-1</module>
|
||||
<module>algorithms-miscellaneous-2</module>
|
||||
<module>algorithms-miscellaneous-3</module>
|
||||
<module>algorithms-sorting</module>
|
||||
<module>animal-sniffer-mvn-plugin</module>
|
||||
<module>annotations</module>
|
||||
|
@ -1134,6 +1137,7 @@
|
|||
<module>java-streams</module>
|
||||
<module>java-streams-2</module>
|
||||
<module>java-strings</module>
|
||||
<module>java-strings-2</module>
|
||||
<module>java-vavr-stream</module>
|
||||
<module>java-websocket</module>
|
||||
<module>javafx</module>
|
||||
|
|
Loading…
Reference in New Issue