Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
7f13afc009
|
@ -2,4 +2,6 @@
|
|||
|
||||
## Relevant articles:
|
||||
|
||||
- [String Matching in Groovy](http://www.baeldung.com/)
|
||||
- [String Matching in Groovy](http://www.baeldung.com/)
|
||||
- [Groovy def Keyword]
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
<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>core-groovy</artifactId>
|
||||
<artifactId>core-groovy-2</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>core-groovy-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
@ -14,12 +14,33 @@
|
|||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy</artifactId>
|
||||
<version>${groovy.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-all</artifactId>
|
||||
<version>${groovy-all.version}</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-dateutil</artifactId>
|
||||
<version>${groovy.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-sql</artifactId>
|
||||
<version>${groovy-sql.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hsqldb</groupId>
|
||||
<artifactId>hsqldb</artifactId>
|
||||
|
@ -43,12 +64,39 @@
|
|||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>addSources</goal>
|
||||
<goal>addTestSources</goal>
|
||||
<goal>compile</goal>
|
||||
<goal>compileTests</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-failsafe-plugin</artifactId>
|
||||
<version>${maven-failsafe-plugin.version}</version>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-surefire-provider</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>junit5</id>
|
||||
<goals>
|
||||
<goal>integration-test</goal>
|
||||
<goal>verify</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<includes>
|
||||
<include>**/*Test5.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.20.1</version>
|
||||
|
@ -71,10 +119,13 @@
|
|||
</repositories>
|
||||
|
||||
<properties>
|
||||
<junit.platform.version>1.0.0</junit.platform.version>
|
||||
<groovy.version>2.5.6</groovy.version>
|
||||
<groovy-all.version>2.5.6</groovy-all.version>
|
||||
<groovy-sql.version>2.5.6</groovy-sql.version>
|
||||
<hsqldb.version>2.4.0</hsqldb.version>
|
||||
<spock-core.version>1.3-groovy-2.5</spock-core.version>
|
||||
<gmavenplus-plugin.version>1.6.3</gmavenplus-plugin.version>
|
||||
<spock-core.version>1.1-groovy-2.4</spock-core.version>
|
||||
<gmavenplus-plugin.version>1.6</gmavenplus-plugin.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
package com.baeldung.defkeyword
|
||||
|
||||
import org.codehaus.groovy.runtime.NullObject
|
||||
import org.codehaus.groovy.runtime.typehandling.GroovyCastException
|
||||
|
||||
import groovy.transform.TypeChecked
|
||||
import groovy.transform.TypeCheckingMode
|
||||
|
||||
@TypeChecked
|
||||
class DefUnitTest extends GroovyTestCase {
|
||||
|
||||
def id
|
||||
def firstName = "Samwell"
|
||||
def listOfCountries = ['USA', 'UK', 'FRANCE', 'INDIA']
|
||||
|
||||
@TypeChecked(TypeCheckingMode.SKIP)
|
||||
def multiply(x, y) {
|
||||
return x*y
|
||||
}
|
||||
|
||||
@TypeChecked(TypeCheckingMode.SKIP)
|
||||
void testDefVariableDeclaration() {
|
||||
|
||||
def list
|
||||
assert list.getClass() == org.codehaus.groovy.runtime.NullObject
|
||||
assert list.is(null)
|
||||
|
||||
list = [1,2,4]
|
||||
assert list instanceof ArrayList
|
||||
}
|
||||
|
||||
@TypeChecked(TypeCheckingMode.SKIP)
|
||||
void testTypeVariables() {
|
||||
int rate = 200
|
||||
try {
|
||||
rate = [12] //GroovyCastException
|
||||
rate = "nill" //GroovyCastException
|
||||
} catch(GroovyCastException) {
|
||||
println "Cannot assign anything other than integer"
|
||||
}
|
||||
}
|
||||
|
||||
@TypeChecked(TypeCheckingMode.SKIP)
|
||||
void testDefVariableMultipleAssignment() {
|
||||
def rate
|
||||
assert rate == null
|
||||
assert rate.getClass() == org.codehaus.groovy.runtime.NullObject
|
||||
|
||||
rate = 12
|
||||
assert rate instanceof Integer
|
||||
|
||||
rate = "Not Available"
|
||||
assert rate instanceof String
|
||||
|
||||
rate = [1, 4]
|
||||
assert rate instanceof List
|
||||
|
||||
assert divide(12, 3) instanceof BigDecimal
|
||||
assert divide(1, 0) instanceof String
|
||||
|
||||
}
|
||||
|
||||
def divide(int x, int y) {
|
||||
if(y==0) {
|
||||
return "Should not divide by 0"
|
||||
} else {
|
||||
return x/y
|
||||
}
|
||||
}
|
||||
|
||||
def greetMsg() {
|
||||
println "Hello! I am Groovy"
|
||||
}
|
||||
|
||||
void testDefVsType() {
|
||||
def int count
|
||||
assert count instanceof Integer
|
||||
}
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
package com.baeldung;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class UnitTest {
|
||||
/**
|
||||
* Stub test
|
||||
*/
|
||||
@Test
|
||||
public void givenPreconditions_whenCondition_shouldResult() {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
|
@ -5,7 +5,7 @@ version '1.0-SNAPSHOT'
|
|||
|
||||
|
||||
buildscript {
|
||||
ext.kotlin_version = '1.2.41'
|
||||
ext.kotlin_version = '1.3.30'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
@ -26,8 +26,6 @@ sourceCompatibility = 1.8
|
|||
compileKotlin { kotlinOptions.jvmTarget = "1.8" }
|
||||
compileTestKotlin { kotlinOptions.jvmTarget = "1.8" }
|
||||
|
||||
kotlin { experimental { coroutines "enable" } }
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
jcenter()
|
||||
|
@ -39,10 +37,22 @@ sourceSets {
|
|||
srcDirs 'com/baeldung/ktor'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
testLogging {
|
||||
events "passed", "skipped", "failed"
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile "ch.qos.logback:logback-classic:1.2.1"
|
||||
testCompile group: 'junit', name: 'junit', version: '4.12'
|
||||
}
|
||||
implementation "ch.qos.logback:logback-classic:1.2.1"
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:${kotlin_version}"
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter:5.4.2'
|
||||
testImplementation 'junit:junit:4.12'
|
||||
testImplementation 'org.assertj:assertj-core:3.12.2'
|
||||
testImplementation 'org.mockito:mockito-core:2.27.0'
|
||||
testImplementation "org.jetbrains.kotlin:kotlin-test:${kotlin_version}"
|
||||
testImplementation "org.jetbrains.kotlin:kotlin-test-junit5:${kotlin_version}"
|
||||
}
|
||||
|
|
Binary file not shown.
|
@ -1,5 +1,5 @@
|
|||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip
|
||||
|
|
|
@ -1,5 +1,21 @@
|
|||
#!/usr/bin/env sh
|
||||
|
||||
#
|
||||
# Copyright 2015 the original author or authors.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
##############################################################################
|
||||
##
|
||||
## Gradle start up script for UN*X
|
||||
|
@ -28,7 +44,7 @@ APP_NAME="Gradle"
|
|||
APP_BASE_NAME=`basename "$0"`
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS=""
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD="maximum"
|
||||
|
|
|
@ -1,3 +1,19 @@
|
|||
@rem
|
||||
@rem Copyright 2015 the original author or authors.
|
||||
@rem
|
||||
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@rem you may not use this file except in compliance with the License.
|
||||
@rem You may obtain a copy of the License at
|
||||
@rem
|
||||
@rem http://www.apache.org/licenses/LICENSE-2.0
|
||||
@rem
|
||||
@rem Unless required by applicable law or agreed to in writing, software
|
||||
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
@rem See the License for the specific language governing permissions and
|
||||
@rem limitations under the License.
|
||||
@rem
|
||||
|
||||
@if "%DEBUG%" == "" @echo off
|
||||
@rem ##########################################################################
|
||||
@rem
|
||||
|
@ -14,7 +30,7 @@ set APP_BASE_NAME=%~n0
|
|||
set APP_HOME=%DIRNAME%
|
||||
|
||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
set DEFAULT_JVM_OPTS=
|
||||
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
||||
|
||||
@rem Find java.exe
|
||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||
|
|
|
@ -20,9 +20,21 @@
|
|||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit.jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.bytebuddy</groupId>
|
||||
<artifactId>byte-buddy</artifactId>
|
||||
<version>${byte-buddy.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -37,6 +49,12 @@
|
|||
<version>${kotlin.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-test-junit5</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -69,10 +87,11 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<kotlin.version>1.2.71</kotlin.version>
|
||||
<junit.platform.version>1.1.1</junit.platform.version>
|
||||
<junit.vintage.version>5.2.0</junit.vintage.version>
|
||||
<kotlin.version>1.3.30</kotlin.version>
|
||||
<junit.jupiter.version>5.4.2</junit.jupiter.version>
|
||||
<mockito.version>2.27.0</mockito.version>
|
||||
<byte-buddy.version>1.9.12</byte-buddy.version>
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
package com.baeldung.console
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.mockito.Mockito.`when`
|
||||
import org.mockito.Mockito.mock
|
||||
import java.io.BufferedReader
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.Console
|
||||
import java.io.InputStreamReader
|
||||
import java.io.PrintStream
|
||||
import java.util.*
|
||||
|
||||
|
||||
class ConsoleIOUnitTest {
|
||||
|
||||
@Test
|
||||
fun givenText_whenPrint_thenPrintText() {
|
||||
val expectedTest = "Hello from Kotlin"
|
||||
val out = ByteArrayOutputStream()
|
||||
System.setOut(PrintStream(out))
|
||||
|
||||
print(expectedTest)
|
||||
out.flush()
|
||||
val printedText = String(out.toByteArray())
|
||||
|
||||
assertThat(printedText).isEqualTo(expectedTest)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenInput_whenRead_thenReadText() {
|
||||
val expectedTest = "Hello from Kotlin"
|
||||
val input = ByteArrayInputStream(expectedTest.toByteArray())
|
||||
System.setIn(input)
|
||||
|
||||
val readText = readLine()
|
||||
|
||||
assertThat(readText).isEqualTo(expectedTest)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenInput_whenReadWithScanner_thenReadText() {
|
||||
val expectedTest = "Hello from Kotlin"
|
||||
val scanner = Scanner(ByteArrayInputStream(expectedTest.toByteArray()))
|
||||
|
||||
val readText = scanner.nextLine()
|
||||
|
||||
assertThat(readText).isEqualTo(expectedTest)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenInput_whenReadWithBufferedReader_thenReadText() {
|
||||
val expectedTest = "Hello from Kotlin"
|
||||
val reader = BufferedReader(InputStreamReader(ByteArrayInputStream(expectedTest.toByteArray())))
|
||||
|
||||
val readText = reader.readLine()
|
||||
|
||||
assertThat(readText).isEqualTo(expectedTest)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenInput_whenReadWithConsole_thenReadText() {
|
||||
val expectedTest = "Hello from Kotlin"
|
||||
val console = mock(Console::class.java)
|
||||
`when`(console.readLine()).thenReturn(expectedTest)
|
||||
|
||||
val readText = console.readLine()
|
||||
|
||||
assertThat(readText).isEqualTo(expectedTest)
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
fun resetIO() {
|
||||
System.setOut(System.out)
|
||||
System.setIn(System.`in`)
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
mock-maker-inline
|
|
@ -8,7 +8,8 @@ import kotlin.test.assertEquals
|
|||
|
||||
class InputStreamToStringTest {
|
||||
private val fileName = "src/test/resources/inputstream2string.txt"
|
||||
private val fileFullContent = "Computer programming can be a hassle\r\n" +
|
||||
private val endOfLine = System.lineSeparator()
|
||||
private val fileFullContent = "Computer programming can be a hassle$endOfLine" +
|
||||
"It's like trying to take a defended castle"
|
||||
|
||||
@Test
|
||||
|
@ -46,7 +47,7 @@ class InputStreamToStringTest {
|
|||
} finally {
|
||||
reader.close()
|
||||
}
|
||||
assertEquals(fileFullContent.replace("\r\n", ""), content.toString())
|
||||
assertEquals(fileFullContent.replace(endOfLine, ""), content.toString())
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
|
@ -0,0 +1,10 @@
|
|||
=========
|
||||
## HttpClient 4.x Cookbooks and Examples
|
||||
|
||||
###The Course
|
||||
The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [HttpClient 4 – Get the Status Code](http://www.baeldung.com/httpclient-status-code)
|
|
@ -0,0 +1,133 @@
|
|||
<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>httpclient-simple</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>httpclient-simple</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- utils -->
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<!-- http client -->
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>${httpclient.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>fluent-hc</artifactId>
|
||||
<version>${httpclient.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpmime</artifactId>
|
||||
<version>${httpclient.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>${commons-codec.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpasyncclient</artifactId>
|
||||
<version>${httpasyncclient.version}</version> <!-- 4.0.2 --> <!-- 4.1-beta1 -->
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.tomakehurst</groupId>
|
||||
<artifactId>wiremock</artifactId>
|
||||
<version>${wiremock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>httpclient</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>live</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>**/*LiveTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<!-- util -->
|
||||
<guava.version>19.0</guava.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<commons-codec.version>1.10</commons-codec.version>
|
||||
<httpasyncclient.version>4.1.4</httpasyncclient.version>
|
||||
<!-- testing -->
|
||||
<wiremock.version>2.5.1</wiremock.version>
|
||||
<httpclient.version>4.5.8</httpclient.version> <!-- 4.3.6 --> <!-- 4.4-beta1 -->
|
||||
<!-- maven plugins -->
|
||||
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,19 @@
|
|||
<?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>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -0,0 +1,26 @@
|
|||
package org.baeldung.httpclient;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public final class ResponseUtil {
|
||||
private ResponseUtil() {
|
||||
}
|
||||
|
||||
public static void closeResponse(CloseableHttpResponse response) throws IOException {
|
||||
if (response == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final HttpEntity entity = response.getEntity();
|
||||
if (entity != null) {
|
||||
entity.getContent().close();
|
||||
}
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
|
@ -8,7 +8,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||
### Relevant Articles:
|
||||
|
||||
- [HttpClient 4 – Send Custom Cookie](http://www.baeldung.com/httpclient-4-cookies)
|
||||
- [HttpClient 4 – Get the Status Code](http://www.baeldung.com/httpclient-status-code)
|
||||
- [HttpClient 4 – Cancel Request](http://www.baeldung.com/httpclient-cancel-request)
|
||||
- [HttpClient 4 Cookbook](http://www.baeldung.com/httpclient4)
|
||||
- [Unshorten URLs with HttpClient](http://www.baeldung.com/unshorten-url-httpclient)
|
||||
|
|
|
@ -122,11 +122,10 @@
|
|||
<guava.version>19.0</guava.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<commons-codec.version>1.10</commons-codec.version>
|
||||
<httpasyncclient.version>4.1.2</httpasyncclient.version>
|
||||
<httpasyncclient.version>4.1.4</httpasyncclient.version>
|
||||
<!-- testing -->
|
||||
<wiremock.version>2.5.1</wiremock.version>
|
||||
<httpcore.version>4.4.5</httpcore.version>
|
||||
<httpclient.version>4.5.3</httpclient.version> <!-- 4.3.6 --> <!-- 4.4-beta1 -->
|
||||
<httpclient.version>4.5.8</httpclient.version> <!-- 4.3.6 --> <!-- 4.4-beta1 -->
|
||||
<!-- maven plugins -->
|
||||
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
||||
</properties>
|
||||
|
|
|
@ -4,7 +4,6 @@ import static org.hamcrest.Matchers.equalTo;
|
|||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
|
@ -18,8 +17,7 @@ import org.apache.http.client.CredentialsProvider;
|
|||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.conn.ssl.SSLContexts;
|
||||
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
||||
import org.apache.http.conn.ssl.TrustStrategy;
|
||||
import org.apache.http.impl.client.BasicCookieStore;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
|
@ -31,6 +29,7 @@ import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
|
|||
import org.apache.http.nio.reactor.ConnectingIOReactor;
|
||||
import org.apache.http.protocol.BasicHttpContext;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.apache.http.ssl.SSLContexts;
|
||||
import org.junit.Test;
|
||||
|
||||
public class HttpAsyncClientLiveTest {
|
||||
|
@ -104,7 +103,7 @@ public class HttpAsyncClientLiveTest {
|
|||
final TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true;
|
||||
final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
|
||||
|
||||
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setSSLHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER).setSSLContext(sslContext).build();
|
||||
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).setSSLContext(sslContext).build();
|
||||
|
||||
client.start();
|
||||
final HttpGet request = new HttpGet(HOST_WITH_SSL);
|
||||
|
|
|
@ -1,32 +1,31 @@
|
|||
package org.baeldung.httpclient;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.conn.ClientConnectionManager;
|
||||
import org.apache.http.conn.scheme.Scheme;
|
||||
import org.apache.http.conn.scheme.SchemeRegistry;
|
||||
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.conn.ssl.SSLSocketFactory;
|
||||
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
|
||||
import org.apache.http.conn.ssl.TrustStrategy;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.conn.PoolingClientConnectionManager;
|
||||
import org.apache.http.ssl.SSLContextBuilder;
|
||||
import org.apache.http.ssl.SSLContexts;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLHandshakeException;
|
||||
import java.io.IOException;
|
||||
import java.security.GeneralSecurityException;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.GeneralSecurityException;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLHandshakeException;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.config.Registry;
|
||||
import org.apache.http.config.RegistryBuilder;
|
||||
import org.apache.http.conn.socket.ConnectionSocketFactory;
|
||||
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
|
||||
import org.apache.http.conn.ssl.TrustStrategy;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.ssl.SSLContextBuilder;
|
||||
import org.apache.http.ssl.SSLContexts;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* This test requires a localhost server over HTTPS <br>
|
||||
* It should only be manually run, not part of the automated build
|
||||
|
@ -50,16 +49,22 @@ public class HttpsClientSslLiveTest {
|
|||
.getStatusCode(), equalTo(200));
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public final void givenHttpClientPre4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
|
||||
final TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true;
|
||||
final SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
||||
final SchemeRegistry registry = new SchemeRegistry();
|
||||
registry.register(new Scheme("https", 443, sf));
|
||||
final ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);
|
||||
|
||||
final SSLContext sslContext = SSLContexts.custom()
|
||||
.loadTrustMaterial(null, acceptingTrustStrategy)
|
||||
.build();
|
||||
|
||||
final CloseableHttpClient httpClient = new DefaultHttpClient(ccm);
|
||||
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
|
||||
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create().register("https", sslsf).build();
|
||||
PoolingHttpClientConnectionManager clientConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
|
||||
|
||||
final CloseableHttpClient httpClient = HttpClients.custom()
|
||||
.setSSLSocketFactory(sslsf)
|
||||
.setConnectionManager(clientConnectionManager)
|
||||
.build();
|
||||
|
||||
final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
|
||||
final HttpResponse response = httpClient.execute(getMethod);
|
||||
|
@ -76,10 +81,9 @@ public class HttpsClientSslLiveTest {
|
|||
.loadTrustMaterial(null, acceptingTrustStrategy)
|
||||
.build();
|
||||
|
||||
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
||||
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
|
||||
|
||||
final CloseableHttpClient httpClient = HttpClients.custom()
|
||||
.setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
|
||||
.setSSLSocketFactory(sslsf)
|
||||
.build();
|
||||
|
||||
|
|
|
@ -327,7 +327,8 @@ public class HttpClientConnectionManagementLiveTest {
|
|||
// 8.1
|
||||
public final void whenHttpClientChecksStaleConns_thenNoExceptions() {
|
||||
poolingConnManager = new PoolingHttpClientConnectionManager();
|
||||
client = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().setStaleConnectionCheckEnabled(true).build()).setConnectionManager(poolingConnManager).build();
|
||||
poolingConnManager.setValidateAfterInactivity(1000);
|
||||
client = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().build()).setConnectionManager(poolingConnManager).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -22,6 +22,20 @@
|
|||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- YAML -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-yaml</artifactId>
|
||||
<version>2.9.8</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Allow use of LocalDate -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
<version>2.9.8</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- test scoped -->
|
||||
|
||||
|
|
|
@ -0,0 +1,68 @@
|
|||
package com.baeldung.jackson.entities;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Order {
|
||||
private String orderNo;
|
||||
private LocalDate date;
|
||||
private String customerName;
|
||||
private List<OrderLine> orderLines;
|
||||
|
||||
public Order() {
|
||||
|
||||
}
|
||||
|
||||
public Order(String orderNo, LocalDate date, String customerName, List<OrderLine> orderLines) {
|
||||
super();
|
||||
this.orderNo = orderNo;
|
||||
this.date = date;
|
||||
this.customerName = customerName;
|
||||
this.orderLines = orderLines;
|
||||
}
|
||||
|
||||
public String getOrderNo() {
|
||||
return orderNo;
|
||||
}
|
||||
|
||||
public void setOrderNo(String orderNo) {
|
||||
this.orderNo = orderNo;
|
||||
}
|
||||
|
||||
public LocalDate getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(LocalDate date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public String getCustomerName() {
|
||||
return customerName;
|
||||
}
|
||||
|
||||
public void setCustomerName(String customerName) {
|
||||
this.customerName = customerName;
|
||||
}
|
||||
|
||||
public List<OrderLine> getOrderLines() {
|
||||
if (orderLines == null) {
|
||||
orderLines = new ArrayList<>();
|
||||
}
|
||||
return orderLines;
|
||||
}
|
||||
|
||||
public void setOrderLines(List<OrderLine> orderLines) {
|
||||
if (orderLines == null) {
|
||||
orderLines = new ArrayList<>();
|
||||
}
|
||||
this.orderLines = orderLines;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Order [orderNo=" + orderNo + ", date=" + date + ", customerName=" + customerName + ", orderLines=" + orderLines + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.baeldung.jackson.entities;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class OrderLine {
|
||||
private String item;
|
||||
private int quantity;
|
||||
private BigDecimal unitPrice;
|
||||
|
||||
public OrderLine() {
|
||||
|
||||
}
|
||||
|
||||
public OrderLine(String item, int quantity, BigDecimal unitPrice) {
|
||||
super();
|
||||
this.item = item;
|
||||
this.quantity = quantity;
|
||||
this.unitPrice = unitPrice;
|
||||
}
|
||||
|
||||
public String getItem() {
|
||||
return item;
|
||||
}
|
||||
|
||||
public void setItem(String item) {
|
||||
this.item = item;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public void setQuantity(int quantity) {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public BigDecimal getUnitPrice() {
|
||||
return unitPrice;
|
||||
}
|
||||
|
||||
public void setUnitPrice(BigDecimal unitPrice) {
|
||||
this.unitPrice = unitPrice;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "OrderLine [item=" + item + ", quantity=" + quantity + ", unitPrice=" + unitPrice + "]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
orderNo: A001
|
||||
date: 2019-04-17
|
||||
customerName: Customer, Joe
|
||||
orderLines:
|
||||
- item: No. 9 Sprockets
|
||||
quantity: 12
|
||||
unitPrice: 1.23
|
||||
- item: Widget (10mm)
|
||||
quantity: 4
|
||||
unitPrice: 3.45
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
package com.baeldung.jackson.yaml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jackson.entities.Order;
|
||||
import com.baeldung.jackson.entities.OrderLine;
|
||||
import com.fasterxml.jackson.core.JsonGenerationException;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;
|
||||
|
||||
public class YamlUnitTest {
|
||||
private ObjectMapper mapper;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mapper = new ObjectMapper(new YAMLFactory().disable(Feature.WRITE_DOC_START_MARKER));
|
||||
mapper.findAndRegisterModules();
|
||||
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenYamlInput_ObjectCreated() throws JsonParseException, JsonMappingException, IOException {
|
||||
Order order = mapper.readValue(new File("src/main/resources/orderInput.yaml"), Order.class);
|
||||
assertEquals("A001", order.getOrderNo());
|
||||
assertEquals(LocalDate.parse("2019-04-17", DateTimeFormatter.ISO_DATE), order.getDate());
|
||||
assertEquals("Customer, Joe", order.getCustomerName());
|
||||
assertEquals(2, order.getOrderLines()
|
||||
.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenYamlObject_FileWritten() throws JsonGenerationException, JsonMappingException, IOException {
|
||||
List<OrderLine> lines = new ArrayList<>();
|
||||
lines.add(new OrderLine("Copper Wire (200ft)", 1, new BigDecimal(50.67).setScale(2, RoundingMode.HALF_UP)));
|
||||
lines.add(new OrderLine("Washers (1/4\")", 24, new BigDecimal(.15).setScale(2, RoundingMode.HALF_UP)));
|
||||
Order order = new Order(
|
||||
"B-9910",
|
||||
LocalDate.parse("2019-04-18", DateTimeFormatter.ISO_DATE),
|
||||
"Customer, Jane",
|
||||
lines);
|
||||
mapper.writeValue(new File("src/main/resources/orderOutput.yaml"), order);
|
||||
|
||||
File outputYaml = new File("src/main/resources/orderOutput.yaml");
|
||||
assertTrue(outputYaml.exists());
|
||||
}
|
||||
}
|
|
@ -202,10 +202,10 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<kotlin.version>1.3.10</kotlin.version>
|
||||
<kotlin.version>1.3.30</kotlin.version>
|
||||
<kotlinx.version>1.0.0</kotlinx.version>
|
||||
<ktor.io.version>0.9.5</ktor.io.version>
|
||||
<assertj.version>3.11.0</assertj.version>
|
||||
<junit.platform.version>1.2.0</junit.platform.version>
|
||||
<assertj.version>3.12.0</assertj.version>
|
||||
<junit.platform.version>1.3.2</junit.platform.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>hibernate-mapping</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2database.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>hibernate-mapping</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/test/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<hibernate.version>5.3.7.Final</hibernate.version>
|
||||
<h2database.version>1.4.196</h2database.version>
|
||||
<assertj-core.version>3.8.0</assertj-core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,64 @@
|
|||
package com.baeldung.hibernate;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.Metadata;
|
||||
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;
|
||||
|
||||
public class HibernateUtil {
|
||||
private static SessionFactory sessionFactory;
|
||||
|
||||
private HibernateUtil() {
|
||||
}
|
||||
|
||||
public static SessionFactory getSessionFactory(Strategy strategy) {
|
||||
if (sessionFactory == null) {
|
||||
sessionFactory = buildSessionFactory(strategy);
|
||||
}
|
||||
return sessionFactory;
|
||||
}
|
||||
|
||||
private static SessionFactory buildSessionFactory(Strategy strategy) {
|
||||
try {
|
||||
ServiceRegistry serviceRegistry = configureServiceRegistry();
|
||||
|
||||
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
|
||||
|
||||
for (Class<?> entityClass : strategy.getEntityClasses()) {
|
||||
metadataSources.addAnnotatedClass(entityClass);
|
||||
}
|
||||
|
||||
Metadata metadata = metadataSources.getMetadataBuilder()
|
||||
.build();
|
||||
|
||||
return metadata.getSessionFactoryBuilder()
|
||||
.build();
|
||||
} catch (IOException ex) {
|
||||
throw new ExceptionInInitializerError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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("hibernate.properties");
|
||||
try (FileInputStream inputStream = new FileInputStream(propertiesURL.getFile())) {
|
||||
properties.load(inputStream);
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.hibernate;
|
||||
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
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)),
|
||||
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)),
|
||||
MAP_KEY_ENUMERATED_BASED(Arrays.asList(com.baeldung.hibernate.persistmaps.mapkeyenumerated.Order.class,
|
||||
com.baeldung.hibernate.persistmaps.mapkey.Item.class)),
|
||||
MAP_KEY_TEMPORAL_BASED(Arrays.asList(com.baeldung.hibernate.persistmaps.mapkeytemporal.Order.class,
|
||||
com.baeldung.hibernate.persistmaps.mapkey.Item.class));
|
||||
|
||||
|
||||
private List<Class<?>> entityClasses;
|
||||
|
||||
Strategy(List<Class<?>> entityClasses) {
|
||||
this.entityClasses = entityClasses;
|
||||
}
|
||||
|
||||
public List<Class<?>> getEntityClasses() {
|
||||
return entityClasses;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package com.baeldung.hibernate.persistmaps;
|
||||
|
||||
public enum ItemType {
|
||||
JEANS,
|
||||
TSHIRTS
|
||||
}
|
|
@ -0,0 +1,95 @@
|
|||
package com.baeldung.hibernate.persistmaps.mapkey;
|
||||
|
||||
import com.baeldung.hibernate.persistmaps.ItemType;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Temporal;
|
||||
import javax.persistence.TemporalType;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(name = "item")
|
||||
public class Item {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@Column(name = "id")
|
||||
private int id;
|
||||
|
||||
@Column(name = "name")
|
||||
private String itemName;
|
||||
|
||||
@Column(name = "price")
|
||||
private double itemPrice;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(name = "item_type")
|
||||
private ItemType itemType;
|
||||
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
@Column(name = "created_on")
|
||||
private Date createdOn;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getItemName() {
|
||||
return itemName;
|
||||
}
|
||||
|
||||
public void setItemName(String itemName) {
|
||||
this.itemName = itemName;
|
||||
}
|
||||
|
||||
public double getItemPrice() {
|
||||
return itemPrice;
|
||||
}
|
||||
|
||||
public void setItemPrice(double itemPrice) {
|
||||
this.itemPrice = itemPrice;
|
||||
}
|
||||
|
||||
public ItemType getItemType() {
|
||||
return itemType;
|
||||
}
|
||||
|
||||
public void setItemType(ItemType itemType) {
|
||||
this.itemType = itemType;
|
||||
}
|
||||
|
||||
public Date getCreatedOn() {
|
||||
return createdOn;
|
||||
}
|
||||
|
||||
public void setCreatedOn(Date createdOn) {
|
||||
this.createdOn = createdOn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Item item = (Item) o;
|
||||
return id == item.id &&
|
||||
Double.compare(item.itemPrice, itemPrice) == 0 &&
|
||||
Objects.equals(itemName, item.itemName) &&
|
||||
itemType == item.itemType &&
|
||||
Objects.equals(createdOn, item.createdOn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, itemName, itemPrice, itemType, createdOn);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.hibernate.persistmaps.mapkey;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.MapKey;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import java.util.Map;
|
||||
|
||||
@Entity
|
||||
@Table(name = "orders")
|
||||
public class Order {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@Column(name = "id")
|
||||
private int id;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
@JoinTable(name = "order_item_mapping", joinColumns = {@JoinColumn(name = "order_id", referencedColumnName = "id")},
|
||||
inverseJoinColumns = {@JoinColumn(name = "item_id", referencedColumnName = "id")})
|
||||
@MapKey(name = "itemName")
|
||||
private Map<String, Item> itemMap;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Map<String, Item> getItemMap() {
|
||||
return itemMap;
|
||||
}
|
||||
|
||||
public void setItemMap(Map<String, Item> itemMap) {
|
||||
this.itemMap = itemMap;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.baeldung.hibernate.persistmaps.mapkeycolumn;
|
||||
|
||||
import javax.persistence.CollectionTable;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.ElementCollection;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.MapKeyColumn;
|
||||
import javax.persistence.Table;
|
||||
import java.util.Map;
|
||||
|
||||
@Entity
|
||||
@Table(name = "orders")
|
||||
public class Order {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@Column(name = "id")
|
||||
private int id;
|
||||
|
||||
@ElementCollection
|
||||
@CollectionTable(name = "order_item_mapping", joinColumns = {@JoinColumn(name = "order_id", referencedColumnName = "id")})
|
||||
@MapKeyColumn(name = "item_name")
|
||||
@Column(name = "price")
|
||||
private Map<String, Double> itemPriceMap;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Map<String, Double> getItemPriceMap() {
|
||||
return itemPriceMap;
|
||||
}
|
||||
|
||||
public void setItemPriceMap(Map<String, Double> itemPriceMap) {
|
||||
this.itemPriceMap = itemPriceMap;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung.hibernate.persistmaps.mapkeyenumerated;
|
||||
|
||||
import com.baeldung.hibernate.persistmaps.ItemType;
|
||||
import com.baeldung.hibernate.persistmaps.mapkey.Item;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.MapKeyEnumerated;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import java.util.Map;
|
||||
|
||||
@Entity
|
||||
@Table(name = "orders")
|
||||
public class Order {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@Column(name = "id")
|
||||
private int id;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
@JoinTable(name = "order_item_mapping", joinColumns = {@JoinColumn(name = "order_id", referencedColumnName = "id")},
|
||||
inverseJoinColumns = {@JoinColumn(name = "item_id", referencedColumnName = "id")})
|
||||
@MapKeyEnumerated(EnumType.STRING)
|
||||
private Map<ItemType, Item> itemMap;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Map<ItemType, Item> getItemMap() {
|
||||
return itemMap;
|
||||
}
|
||||
|
||||
public void setItemMap(Map<ItemType, Item> itemMap) {
|
||||
this.itemMap = itemMap;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,112 @@
|
|||
package com.baeldung.hibernate.persistmaps.mapkeyjoincolumn;
|
||||
|
||||
import com.baeldung.hibernate.persistmaps.ItemType;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Temporal;
|
||||
import javax.persistence.TemporalType;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(name = "item")
|
||||
public class Item {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@Column(name = "id")
|
||||
private int id;
|
||||
|
||||
@Column(name = "name")
|
||||
private String itemName;
|
||||
|
||||
@Column(name = "price")
|
||||
private double itemPrice;
|
||||
|
||||
@Column(name = "item_type")
|
||||
@Enumerated(EnumType.STRING)
|
||||
private ItemType itemType;
|
||||
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
@Column(name = "created_on")
|
||||
private Date createdOn;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.ALL)
|
||||
@JoinColumn(name = "seller_id")
|
||||
private Seller seller;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getItemName() {
|
||||
return itemName;
|
||||
}
|
||||
|
||||
public void setItemName(String itemName) {
|
||||
this.itemName = itemName;
|
||||
}
|
||||
|
||||
public double getItemPrice() {
|
||||
return itemPrice;
|
||||
}
|
||||
|
||||
public void setItemPrice(double itemPrice) {
|
||||
this.itemPrice = itemPrice;
|
||||
}
|
||||
|
||||
public ItemType getItemType() {
|
||||
return itemType;
|
||||
}
|
||||
|
||||
public void setItemType(ItemType itemType) {
|
||||
this.itemType = itemType;
|
||||
}
|
||||
|
||||
public Date getCreatedOn() {
|
||||
return createdOn;
|
||||
}
|
||||
|
||||
public void setCreatedOn(Date createdOn) {
|
||||
this.createdOn = createdOn;
|
||||
}
|
||||
|
||||
public Seller getSeller() {
|
||||
return seller;
|
||||
}
|
||||
|
||||
public void setSeller(Seller seller) {
|
||||
this.seller = seller;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Item item = (Item) o;
|
||||
return id == item.id &&
|
||||
Double.compare(item.itemPrice, itemPrice) == 0 &&
|
||||
Objects.equals(itemName, item.itemName) &&
|
||||
itemType == item.itemType &&
|
||||
Objects.equals(createdOn, item.createdOn) &&
|
||||
Objects.equals(seller, item.seller);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
return Objects.hash(id, itemName, itemPrice, itemType, createdOn, seller);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.hibernate.persistmaps.mapkeyjoincolumn;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.MapKeyJoinColumn;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import java.util.Map;
|
||||
|
||||
@Entity
|
||||
@Table(name = "orders")
|
||||
public class Order {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@Column(name = "id")
|
||||
private int id;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
@JoinTable(name = "order_item_mapping", joinColumns = {@JoinColumn(name = "order_id", referencedColumnName = "id")},
|
||||
inverseJoinColumns = {@JoinColumn(name = "item_id", referencedColumnName = "id")})
|
||||
@MapKeyJoinColumn(name = "seller_id")
|
||||
private Map<Seller, Item> sellerItemMap;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Map<Seller, Item> getSellerItemMap() {
|
||||
return sellerItemMap;
|
||||
}
|
||||
|
||||
public void setSellerItemMap(Map<Seller, Item> sellerItemMap) {
|
||||
this.sellerItemMap = sellerItemMap;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.hibernate.persistmaps.mapkeyjoincolumn;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(name = "seller")
|
||||
public class Seller {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@Column(name = "id")
|
||||
private int id;
|
||||
|
||||
@Column(name = "name")
|
||||
private String sellerName;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getSellerName() {
|
||||
return sellerName;
|
||||
}
|
||||
|
||||
public void setSellerName(String sellerName) {
|
||||
this.sellerName = sellerName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Seller seller = (Seller) o;
|
||||
return Objects.equals(sellerName, seller.sellerName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
return Objects.hash(sellerName);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung.hibernate.persistmaps.mapkeytemporal;
|
||||
|
||||
import com.baeldung.hibernate.persistmaps.mapkey.Item;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.MapKeyTemporal;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.TemporalType;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
@Entity
|
||||
@Table(name = "orders")
|
||||
public class Order {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@Column(name = "id")
|
||||
private int id;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
@JoinTable(name = "order_item_mapping", joinColumns = {@JoinColumn(name = "order_id", referencedColumnName = "id")},
|
||||
inverseJoinColumns = {@JoinColumn(name = "item_id", referencedColumnName = "id")})
|
||||
@MapKeyTemporal(TemporalType.TIMESTAMP)
|
||||
private Map<Date, Item> itemMap;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Map<Date, Item> getItemMap() {
|
||||
return itemMap;
|
||||
}
|
||||
|
||||
public void setItemMap(Map<Date, Item> itemMap) {
|
||||
this.itemMap = itemMap;
|
||||
}
|
||||
}
|
|
@ -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,79 @@
|
|||
package com.baeldung.hibernate.persistmaps;
|
||||
|
||||
import com.baeldung.hibernate.HibernateUtil;
|
||||
import com.baeldung.hibernate.Strategy;
|
||||
import com.baeldung.hibernate.persistmaps.mapkeycolumn.Order;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class MapKeyColumnIntegrationTest {
|
||||
private static SessionFactory sessionFactory;
|
||||
|
||||
private Session session;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTests() {
|
||||
sessionFactory = HibernateUtil.getSessionFactory(Strategy.MAP_KEY_COLUMN_BASED);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenData_whenInsertUsingMapKeyColumn_thenPersistMap() {
|
||||
Map<String, Double> itemPriceMap = new HashMap<>();
|
||||
itemPriceMap.put("Wrangler Jeans", 150.0);
|
||||
itemPriceMap.put("Lee Jeans", 180.0);
|
||||
|
||||
|
||||
Order order = new Order();
|
||||
order.setItemPriceMap(itemPriceMap);
|
||||
|
||||
session.persist(order);
|
||||
session.getTransaction().commit();
|
||||
|
||||
assertInsertedData();
|
||||
}
|
||||
|
||||
private void assertInsertedData() {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Order> orderList = session.createQuery("FROM Order").list();
|
||||
|
||||
assertNotNull(orderList);
|
||||
assertEquals(1, orderList.size());
|
||||
|
||||
Order order = orderList.get(0);
|
||||
|
||||
Map<String, Double> itemPriceMap = order.getItemPriceMap();
|
||||
assertNotNull(itemPriceMap);
|
||||
assertEquals(itemPriceMap.size(), 2);
|
||||
assertEquals((Double) 150.0, itemPriceMap.get("Wrangler Jeans"));
|
||||
assertEquals((Double) 180.0, itemPriceMap.get("Lee Jeans"));
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
session.close();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterTests() {
|
||||
sessionFactory.close();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,95 @@
|
|||
package com.baeldung.hibernate.persistmaps;
|
||||
|
||||
import com.baeldung.hibernate.HibernateUtil;
|
||||
import com.baeldung.hibernate.Strategy;
|
||||
import com.baeldung.hibernate.persistmaps.mapkey.Item;
|
||||
import com.baeldung.hibernate.persistmaps.mapkeyenumerated.Order;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class MapKeyEnumeratedIntegrationTest {
|
||||
private static SessionFactory sessionFactory;
|
||||
|
||||
private Session session;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTests() {
|
||||
sessionFactory = HibernateUtil.getSessionFactory(Strategy.MAP_KEY_ENUMERATED_BASED);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenData_whenInsertUsingMapKeyEnumerated_thenPersistMap() {
|
||||
Item item1 = new Item();
|
||||
item1.setItemName("Wrangler Jeans");
|
||||
item1.setItemPrice(150.0);
|
||||
item1.setItemType(ItemType.JEANS);
|
||||
item1.setCreatedOn(Date.from(Instant.ofEpochSecond(1554926573)));
|
||||
|
||||
|
||||
Item item2 = new Item();
|
||||
item2.setItemName("Armani Tshirts");
|
||||
item2.setItemPrice(180.0);
|
||||
item2.setItemType(ItemType.TSHIRTS);
|
||||
item2.setCreatedOn(Date.from(Instant.ofEpochSecond(1554890573)));
|
||||
|
||||
Map<ItemType, Item> itemMap = new HashMap<>();
|
||||
itemMap.put(item1.getItemType(), item1);
|
||||
itemMap.put(item2.getItemType(), item2);
|
||||
|
||||
Order order = new Order();
|
||||
order.setItemMap(itemMap);
|
||||
|
||||
session.persist(order);
|
||||
session.getTransaction().commit();
|
||||
|
||||
assertInsertedData(item1, item2);
|
||||
}
|
||||
|
||||
private void assertInsertedData(Item expectedItem1, Item expectedItem2) {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Order> orderList = session.createQuery("FROM Order").list();
|
||||
|
||||
assertNotNull(orderList);
|
||||
assertEquals(1, orderList.size());
|
||||
|
||||
Order order = orderList.get(0);
|
||||
|
||||
Map<ItemType, Item> itemMap = order.getItemMap();
|
||||
assertNotNull(itemMap);
|
||||
assertEquals(2, itemMap.size());
|
||||
assertEquals(expectedItem1, itemMap.get(ItemType.JEANS));
|
||||
assertEquals(expectedItem2, itemMap.get(ItemType.TSHIRTS));
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
session.close();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterTests() {
|
||||
sessionFactory.close();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
package com.baeldung.hibernate.persistmaps;
|
||||
|
||||
import com.baeldung.hibernate.HibernateUtil;
|
||||
import com.baeldung.hibernate.Strategy;
|
||||
import com.baeldung.hibernate.persistmaps.mapkey.Item;
|
||||
import com.baeldung.hibernate.persistmaps.mapkey.Order;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class MapKeyIntegrationTest {
|
||||
private static SessionFactory sessionFactory;
|
||||
|
||||
private Session session;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTests() {
|
||||
sessionFactory = HibernateUtil.getSessionFactory(Strategy.MAP_KEY_BASED);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenData_whenInsertUsingMapKey_thenPersistMap() {
|
||||
Item item1 = new Item();
|
||||
item1.setItemName("Wrangler Jeans");
|
||||
item1.setItemPrice(150.0);
|
||||
item1.setItemType(ItemType.JEANS);
|
||||
item1.setCreatedOn(Date.from(Instant.ofEpochSecond(1554926573)));
|
||||
|
||||
|
||||
Item item2 = new Item();
|
||||
item2.setItemName("Armani Tshirts");
|
||||
item2.setItemPrice(180.0);
|
||||
item2.setItemType(ItemType.TSHIRTS);
|
||||
item2.setCreatedOn(Date.from(Instant.ofEpochSecond(1554890573)));
|
||||
|
||||
Map<String, Item> itemMap = new HashMap<>();
|
||||
itemMap.put(item1.getItemName(), item1);
|
||||
itemMap.put(item2.getItemName(), item2);
|
||||
|
||||
Order order = new Order();
|
||||
order.setItemMap(itemMap);
|
||||
|
||||
session.persist(order);
|
||||
session.getTransaction().commit();
|
||||
|
||||
assertInsertedData(item1, item2);
|
||||
}
|
||||
|
||||
private void assertInsertedData(Item expectedItem1, Item expectedItem2) {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Order> orderList = session.createQuery("FROM Order").list();
|
||||
|
||||
assertNotNull(orderList);
|
||||
assertEquals(1, orderList.size());
|
||||
|
||||
Order order = orderList.get(0);
|
||||
|
||||
Map<String, Item> itemMap = order.getItemMap();
|
||||
assertNotNull(itemMap);
|
||||
assertEquals(2, itemMap.size());
|
||||
assertEquals(expectedItem1, itemMap.get("Wrangler Jeans"));
|
||||
assertEquals(expectedItem2, itemMap.get("Armani Tshirts"));
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
session.close();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterTests() {
|
||||
sessionFactory.close();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,105 @@
|
|||
package com.baeldung.hibernate.persistmaps;
|
||||
|
||||
import com.baeldung.hibernate.HibernateUtil;
|
||||
import com.baeldung.hibernate.Strategy;
|
||||
import com.baeldung.hibernate.persistmaps.mapkeyjoincolumn.Item;
|
||||
import com.baeldung.hibernate.persistmaps.mapkeyjoincolumn.Order;
|
||||
import com.baeldung.hibernate.persistmaps.mapkeyjoincolumn.Seller;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class MapKeyJoinColumnIntegrationTest {
|
||||
private static SessionFactory sessionFactory;
|
||||
|
||||
private Session session;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTests() {
|
||||
sessionFactory = HibernateUtil.getSessionFactory(Strategy.MAP_KEY_JOIN_COLUMN_BASED);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenData_whenInsertUsingMapKeyJoinColumn_thenPersistMap() {
|
||||
Seller seller1 = new Seller();
|
||||
seller1.setSellerName("Walmart");
|
||||
|
||||
Item item1 = new Item();
|
||||
item1.setItemName("Wrangler Jeans");
|
||||
item1.setItemPrice(150.0);
|
||||
item1.setItemType(ItemType.JEANS);
|
||||
item1.setCreatedOn(Date.from(Instant.ofEpochSecond(1554926573)));
|
||||
item1.setSeller(seller1);
|
||||
|
||||
|
||||
Seller seller2 = new Seller();
|
||||
seller2.setSellerName("Amazon");
|
||||
|
||||
Item item2 = new Item();
|
||||
item2.setItemName("Armani Tshirts");
|
||||
item2.setItemPrice(180.0);
|
||||
item2.setItemType(ItemType.TSHIRTS);
|
||||
item2.setCreatedOn(Date.from(Instant.ofEpochSecond(1554890573)));
|
||||
item2.setSeller(seller2);
|
||||
|
||||
Map<Seller, Item> itemSellerMap = new HashMap<>();
|
||||
itemSellerMap.put(seller1, item1);
|
||||
itemSellerMap.put(seller2, item2);
|
||||
|
||||
Order order = new Order();
|
||||
order.setSellerItemMap(itemSellerMap);
|
||||
|
||||
|
||||
session.persist(order);
|
||||
session.getTransaction().commit();
|
||||
|
||||
assertInsertedData(seller1, item1, seller2, item2);
|
||||
}
|
||||
|
||||
private void assertInsertedData(Seller seller1, Item expectedItem1, Seller seller2, Item expectedItem2) {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Order> orderList = session.createQuery("FROM Order").list();
|
||||
|
||||
assertNotNull(orderList);
|
||||
assertEquals(1, orderList.size());
|
||||
|
||||
Order order = orderList.get(0);
|
||||
|
||||
Map<Seller, Item> sellerItemMap = order.getSellerItemMap();
|
||||
assertNotNull(sellerItemMap);
|
||||
assertEquals(2, sellerItemMap.size());
|
||||
assertEquals(expectedItem1, sellerItemMap.get(seller1));
|
||||
assertEquals(expectedItem2, sellerItemMap.get(seller2));
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
session.close();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterTests() {
|
||||
sessionFactory.close();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,96 @@
|
|||
package com.baeldung.hibernate.persistmaps;
|
||||
|
||||
import com.baeldung.hibernate.HibernateUtil;
|
||||
import com.baeldung.hibernate.Strategy;
|
||||
import com.baeldung.hibernate.persistmaps.mapkey.Item;
|
||||
import com.baeldung.hibernate.persistmaps.mapkeytemporal.Order;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class MapKeyTemporalIntegrationTest {
|
||||
private static SessionFactory sessionFactory;
|
||||
|
||||
private Session session;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTests() {
|
||||
sessionFactory = HibernateUtil.getSessionFactory(Strategy.MAP_KEY_TEMPORAL_BASED);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenData_whenInsertUsingMapKeyEnumerated_thenPersistMap() {
|
||||
Date item1CreatedOn = Date.from(Instant.ofEpochSecond(1554926573));
|
||||
Item item1 = new Item();
|
||||
item1.setItemName("Wrangler Jeans");
|
||||
item1.setItemPrice(150.0);
|
||||
item1.setItemType(ItemType.JEANS);
|
||||
item1.setCreatedOn(item1CreatedOn);
|
||||
|
||||
|
||||
Date item2CreatedOn = Date.from(Instant.ofEpochSecond(1554890573));
|
||||
Item item2 = new Item();
|
||||
item2.setItemName("Armani Tshirts");
|
||||
item2.setItemPrice(180.0);
|
||||
item2.setItemType(ItemType.TSHIRTS);
|
||||
item2.setCreatedOn(item2CreatedOn);
|
||||
|
||||
Map<Date, Item> itemMap = new HashMap<>();
|
||||
itemMap.put(item1CreatedOn, item1);
|
||||
itemMap.put(item2CreatedOn, item2);
|
||||
|
||||
Order order = new Order();
|
||||
order.setItemMap(itemMap);
|
||||
|
||||
session.persist(order);
|
||||
session.getTransaction().commit();
|
||||
|
||||
assertInsertedData(item1CreatedOn, item1, item2CreatedOn, item2);
|
||||
}
|
||||
|
||||
private void assertInsertedData(Date item1CreatedOn, Item expectedItem1, Date item2CreatedOn, Item expectedItem2) {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Order> orderList = session.createQuery("FROM Order").list();
|
||||
|
||||
assertNotNull(orderList);
|
||||
assertEquals(1, orderList.size());
|
||||
|
||||
Order order = orderList.get(0);
|
||||
|
||||
Map<Date, Item> itemMap = order.getItemMap();
|
||||
assertNotNull(itemMap);
|
||||
assertEquals(2, itemMap.size());
|
||||
assertEquals(expectedItem1, itemMap.get(item1CreatedOn));
|
||||
assertEquals(expectedItem2, itemMap.get(item2CreatedOn));
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
session.close();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterTests() {
|
||||
sessionFactory.close();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
hibernate.connection.driver_class=org.h2.Driver
|
||||
hibernate.connection.url=jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1
|
||||
hibernate.connection.username=sa
|
||||
hibernate.connection.autocommit=true
|
||||
jdbc.password=
|
||||
|
||||
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||
hibernate.show_sql=true
|
||||
hibernate.hbm2ddl.auto=create-drop
|
||||
|
|
@ -22,6 +22,7 @@
|
|||
<module>hbase</module>
|
||||
<module>hibernate5</module>
|
||||
<module>hibernate-ogm</module>
|
||||
<module>hibernate-mapping</module>
|
||||
<module>influxdb</module>
|
||||
<module>java-cassandra</module>
|
||||
<module>java-cockroachdb</module>
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
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>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-data-jpa-2</artifactId>
|
||||
<name>spring-data-jpa</name>
|
||||
|
||||
<name>spring-data-jpa</name>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
@ -19,12 +20,22 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-oxm</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,35 @@
|
|||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.data.repository.init.Jackson2RepositoryPopulatorFactoryBean;
|
||||
import org.springframework.data.repository.init.UnmarshallerRepositoryPopulatorFactoryBean;
|
||||
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
|
||||
|
||||
import com.baeldung.entity.Fruit;
|
||||
|
||||
@Configuration
|
||||
public class JpaPopulators {
|
||||
|
||||
@Bean
|
||||
public Jackson2RepositoryPopulatorFactoryBean getRespositoryPopulator() throws Exception {
|
||||
Jackson2RepositoryPopulatorFactoryBean factory = new Jackson2RepositoryPopulatorFactoryBean();
|
||||
factory.setResources(new Resource[] { new ClassPathResource("fruit-data.json") });
|
||||
return factory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public UnmarshallerRepositoryPopulatorFactoryBean repositoryPopulator() {
|
||||
|
||||
Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller();
|
||||
unmarshaller.setClassesToBeBound(Fruit.class);
|
||||
|
||||
UnmarshallerRepositoryPopulatorFactoryBean factory = new UnmarshallerRepositoryPopulatorFactoryBean();
|
||||
factory.setUnmarshaller(unmarshaller);
|
||||
factory.setResources(new Resource[] { new ClassPathResource("apple-fruit-data.xml"), new ClassPathResource("guava-fruit-data.xml") });
|
||||
return factory;
|
||||
}
|
||||
|
||||
}
|
|
@ -2,7 +2,9 @@ package com.baeldung.entity;
|
|||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
@XmlRootElement
|
||||
@Entity
|
||||
public class Fruit {
|
||||
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<fruit>
|
||||
<id>1</id>
|
||||
<name>apple</name>
|
||||
<color>red</color>
|
||||
</fruit>
|
|
@ -0,0 +1,14 @@
|
|||
[
|
||||
{
|
||||
"_class": "com.baeldung.entity.Fruit",
|
||||
"name": "apple",
|
||||
"color": "red",
|
||||
"id": 1
|
||||
},
|
||||
{
|
||||
"_class": "com.baeldung.entity.Fruit",
|
||||
"name": "guava",
|
||||
"color": "green",
|
||||
"id": 2
|
||||
}
|
||||
]
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<fruit>
|
||||
<id>2</id>
|
||||
<name>guava</name>
|
||||
<color>green</color>
|
||||
</fruit>
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.repository;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.entity.Fruit;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class FruitPopulatorTest {
|
||||
|
||||
@Autowired
|
||||
private FruitRepository fruitRepository;
|
||||
|
||||
@Test
|
||||
public void givenFruitJsonPopulatorThenShouldInsertRecordOnStart() {
|
||||
|
||||
List<Fruit> fruits = fruitRepository.findAll();
|
||||
assertEquals("record count is not matching", 2, fruits.size());
|
||||
|
||||
fruits.forEach(fruit -> {
|
||||
if (1 == fruit.getId()) {
|
||||
assertEquals("apple", fruit.getName());
|
||||
assertEquals("red", fruit.getColor());
|
||||
} else if (2 == fruit.getId()) {
|
||||
assertEquals("guava", fruit.getName());
|
||||
assertEquals("green", fruit.getColor());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -9,6 +9,8 @@ interface PassengerRepository extends JpaRepository<Passenger, Long>, CustomPass
|
|||
|
||||
Passenger findFirstByOrderBySeatNumberAsc();
|
||||
|
||||
Passenger findTopByOrderBySeatNumberAsc();
|
||||
|
||||
List<Passenger> findByOrderBySeatNumberAsc();
|
||||
|
||||
List<Passenger> findByLastNameOrderBySeatNumberAsc(String lastName);
|
||||
|
|
4
pom.xml
4
pom.xml
|
@ -376,6 +376,7 @@
|
|||
<module>cdi</module>
|
||||
<module>checker-plugin</module>
|
||||
<module>core-groovy</module>
|
||||
<module>core-groovy-2</module>
|
||||
<!-- <module>core-java-10</module> --> <!-- We haven't upgraded to java 10. Fixing in BAEL-10841 -->
|
||||
<!-- <module>core-java-11</module> --> <!-- We haven't upgraded to java 11. Fixing in BAEL-10841 -->
|
||||
<!-- <module>core-java-12</module> --> <!-- We haven't upgraded to java 12. Fixing in BAEL-10841 -->
|
||||
|
@ -436,6 +437,7 @@
|
|||
<module>hazelcast</module>
|
||||
<module>helidon</module>
|
||||
<module>httpclient</module>
|
||||
<module>httpclient-simple</module>
|
||||
<module>hystrix</module>
|
||||
|
||||
<module>image-processing</module>
|
||||
|
@ -967,6 +969,7 @@
|
|||
<module>libraries</module> <!-- very long running -->
|
||||
|
||||
<module>persistence-modules/hibernate5</module>
|
||||
<module>persistence-modules/hibernate-mapping</module>
|
||||
<module>persistence-modules/java-jpa</module>
|
||||
<module>persistence-modules/java-mongodb</module>
|
||||
<module>persistence-modules/jnosql</module>
|
||||
|
@ -1102,6 +1105,7 @@
|
|||
<module>hazelcast</module>
|
||||
<module>helidon</module>
|
||||
<module>httpclient</module>
|
||||
<module>httpclient-simple</module>
|
||||
<module>hystrix</module>
|
||||
|
||||
<module>image-processing</module>
|
||||
|
|
|
@ -75,7 +75,7 @@
|
|||
|
||||
<properties>
|
||||
<spring-boot.version>2.1.1.RELEASE</spring-boot.version>
|
||||
<start-class>com.baeldung.springbootmvc.SpringBootMvcApplication</start-class>
|
||||
<start-class>com.baeldung.birt.engine.ReportEngineApplication</start-class>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
|
|
|
@ -7,12 +7,10 @@ Module for the articles that are part of the Spring REST E-book:
|
|||
5. [Entity To DTO Conversion for a Spring REST API](https://www.baeldung.com/entity-to-and-from-dto-for-a-java-spring-application)
|
||||
6. [Error Handling for REST with Spring](http://www.baeldung.com/exception-handling-for-rest-with-spring)
|
||||
7. [REST API Discoverability and HATEOAS](http://www.baeldung.com/restful-web-service-discoverability)
|
||||
8.
|
||||
9. [REST Pagination in Spring](http://www.baeldung.com/rest-api-pagination-in-spring)
|
||||
10. [Test a REST API with Java](http://www.baeldung.com/integration-testing-a-rest-api)
|
||||
|
||||
- [HATEOAS for a Spring REST Service](http://www.baeldung.com/rest-api-discoverability-with-spring)
|
||||
- [Versioning a REST API](http://www.baeldung.com/rest-versioning)
|
||||
- [ETags for REST with Spring](http://www.baeldung.com/etags-for-rest-with-spring)
|
||||
- [Testing REST with multiple MIME types](http://www.baeldung.com/testing-rest-api-with-multiple-media-types)
|
||||
- [Testing Web APIs with Postman Collections](https://www.baeldung.com/postman-testing-collections)
|
||||
8. [REST Pagination in Spring](http://www.baeldung.com/rest-api-pagination-in-spring)
|
||||
9. [Test a REST API with Java](http://www.baeldung.com/integration-testing-a-rest-api)
|
||||
10. [HATEOAS for a Spring REST Service](http://www.baeldung.com/rest-api-discoverability-with-spring)
|
||||
11. [Versioning a REST API](http://www.baeldung.com/rest-versioning)
|
||||
12. [ETags for REST with Spring](http://www.baeldung.com/etags-for-rest-with-spring)
|
||||
13. [Testing REST with multiple MIME types](http://www.baeldung.com/testing-rest-api-with-multiple-media-types)
|
||||
14. [Testing Web APIs with Postman Collections](https://www.baeldung.com/postman-testing-collections)
|
||||
|
|
Loading…
Reference in New Issue