Merge remote-tracking branch 'origin/master'
|
@ -27,4 +27,3 @@ target/
|
|||
|
||||
spring-openid/src/main/resources/application.properties
|
||||
.recommenders/
|
||||
|
||||
|
|
|
@ -9,10 +9,16 @@
|
|||
<version>1.0.0-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>19.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
|
@ -20,6 +26,11 @@
|
|||
<version>3.4.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-guava</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -0,0 +1,95 @@
|
|||
package com.baeldung.assertj.introduction;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.HashBasedTable;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.common.collect.Multimaps;
|
||||
import com.google.common.collect.Range;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.google.common.collect.TreeRangeMap;
|
||||
import com.google.common.io.Files;
|
||||
import org.assertj.guava.data.MapEntry;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
|
||||
import static org.assertj.guava.api.Assertions.assertThat;
|
||||
import static org.assertj.guava.api.Assertions.entry;
|
||||
|
||||
public class AssertJGuavaTest {
|
||||
|
||||
@Test
|
||||
public void givenTwoEmptyFiles_whenComparingContent_thenEqual() throws Exception {
|
||||
final File temp = File.createTempFile("bael", "dung");
|
||||
final File temp2 = File.createTempFile("bael", "dung2");
|
||||
|
||||
assertThat(Files.asByteSource(temp))
|
||||
.hasSize(0)
|
||||
.hasSameContentAs(Files.asByteSource(temp2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultimap_whenVerifying_thenCorrect() throws Exception {
|
||||
final Multimap<Integer, String> mmap = Multimaps.newMultimap(new HashMap<>(), Sets::newHashSet);
|
||||
mmap.put(1, "one");
|
||||
mmap.put(1, "1");
|
||||
|
||||
assertThat(mmap)
|
||||
.hasSize(2)
|
||||
.containsKeys(1)
|
||||
.contains(entry(1, "one"))
|
||||
.contains(entry(1, "1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOptional_whenVerifyingContent_thenShouldBeEqual() throws Exception {
|
||||
final Optional<String> something = Optional.of("something");
|
||||
|
||||
assertThat(something)
|
||||
.isPresent()
|
||||
.extractingValue()
|
||||
.isEqualTo("something");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRange_whenVerifying_thenShouldBeCorrect() throws Exception {
|
||||
final Range<String> range = Range.openClosed("a", "g");
|
||||
|
||||
assertThat(range)
|
||||
.hasOpenedLowerBound()
|
||||
.isNotEmpty()
|
||||
.hasClosedUpperBound()
|
||||
.contains("b");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRangeMap_whenVerifying_thenShouldBeCorrect() throws Exception {
|
||||
final TreeRangeMap<Integer, String> map = TreeRangeMap.create();
|
||||
|
||||
map.put(Range.closed(0, 60), "F");
|
||||
map.put(Range.closed(61, 70), "D");
|
||||
|
||||
assertThat(map)
|
||||
.isNotEmpty()
|
||||
.containsKeys(0)
|
||||
.contains(MapEntry.entry(34, "F"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTable_whenVerifying_thenShouldBeCorrect() throws Exception {
|
||||
final HashBasedTable<Integer, String, String> table = HashBasedTable.create(2, 2);
|
||||
|
||||
table.put(1, "A", "PRESENT");
|
||||
table.put(1, "B", "ABSENT");
|
||||
|
||||
assertThat(table)
|
||||
.hasRowCount(1)
|
||||
.containsValues("ABSENT")
|
||||
.containsCell(1, "B", "ABSENT");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
apply plugin: 'java'
|
||||
apply plugin: 'eclipse'
|
||||
|
||||
allprojects {
|
||||
apply plugin: 'java'
|
||||
sourceCompatibility = 1.6
|
||||
targetCompatibility = 1.6
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
main {
|
||||
resources.srcDirs = ["src/main/java","src/main/resources"]
|
||||
}
|
||||
test {
|
||||
resources.srcDirs = ["src/main/java", "src/main/resources", "src/test/resources"]
|
||||
}
|
||||
}
|
||||
|
||||
configurations {
|
||||
compile
|
||||
}
|
||||
|
||||
test {
|
||||
testLogging {
|
||||
events 'started', 'passed'
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testCompile('junit:junit:4.11')
|
||||
testCompile('org.mockito:mockito-all:1.10.19')
|
||||
testCompile group: 'org.springframework', name: 'spring-test', version: '4.2.6.RELEASE'
|
||||
testCompile group: 'org.springframework', name: 'spring-core', version: '4.2.6.RELEASE'
|
||||
testCompile group: 'org.springframework', name: 'spring-beans', version: '4.2.6.RELEASE'
|
||||
testCompile group: 'org.springframework', name: 'spring-context', version: '4.2.6.RELEASE'
|
||||
testCompile group: 'javax.inject', name: 'javax.inject', version: '1'
|
||||
|
||||
testRuntime('junit:junit:4.11')
|
||||
}
|
Before Width: | Height: | Size: 48 KiB |
Before Width: | Height: | Size: 23 KiB |
|
@ -1 +0,0 @@
|
|||
<mxfile userAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36" version="5.5.1.6" editor="www.draw.io" type="device"><diagram>7Vjvb+I4EP1rkO4+7KkhhW0/8qt3J3V1Velq7z5VbmKIb50M55hS9q/fGXtMEgLXrprthwqEEH6ZOJ5573nAvXiSP/1uxCr7BKnUvf5Z+tSLp71+/6If4ycBWw8MBpceWBqVeiiqgLn6Jhk8Y3StUlk2Ai2AtmrVBBMoCpnYBiaMgU0zbAG6+dSVWIYnVsA8EbqNflGpzUJawwr/Q6plFp4cDTm/B5F8XRpYF/y8Xj9euJe/nIswFydaZiKFTQ2KZ1hWA4Az07f8aSI1lTaUzd93deTqbt1GFry2Z24I67DbkLtMsRQ8BGMzWEIh9KxCxy4/STOc4SizucavEX7Fh5rt34y7wT80+G3ghumImKFZV7K4y1Th0SuFK/K3/Cut3bIaxNoCQtUKrgFW/By/Zlro0bQZKmFtEo7CsROSMEvJUR93JUclS8glLhlDjNTCqsfm7II1tdzF8a2YlqC7QsAKVGHL2sw3BGAAuyO6HPgZ2Rz9yC0jcIRf/IxhVFtaBTkeD3PKeT4KveYMrpTU6QgLulFGplOZQ4v1cqNyLQqit0boAgobtEBjodUSaZsmWGdpEHiUxir0zYgvWOJonGRKp9diC2uqc2nRFGE0zsCobzitCM/Ay8Yy6WiwesSc7mRtGFlizE2gmO700LUoCaCYBLQWq1I97BacI9mqGIO1kHNQyJR0NwENmAYWINg0iIsSk4GwI/JqC4dvCO5mgmNmZFNtJfGAQ7LaNnJ5cVxpNXX8L/m88dbI/0AbIAlgKtF3qSwSXNYIwZF5UNYIs61d2JcF1sCRVkmitAa+ylC4ApxkFrVaMhSkouWCZjgslHIlElUsr13M9LxCbrkqBG0yZeUccVrTBvsNbQs430K73SRTKa7dbUtWWOG5J6LZh5jHYIxvLPiEtqIB5jXBMVIYxvimcGMnUGB+guwaTyUKayNJXC/ThGO5rYngcu4dz4kgxL1GBOctEbSI1Wrf657Y0O6c+3+Y1Ry5cC2CabxzW/YHN32D6rhNNUH7FGrxIPUNlMoqoPmNj92j9g3YG7zQwh04mJtDgzxMYd/D958+z+/ux7P70ee7v778eTub3k/AGPw1pNnfj4BTngz9syQxZPrfwtA8RU0TL9m8Tz29w54+PG829egje73GfxQ00ejqjL2Gf/8Dcb+ru93Re31uDZrs5PZXuN157Efa90G2u3A7C+vUvjtj70D7PsheB+2bDwJa7duC9+gvv54c+yacH+jPP82xEa+uxvqt/G+N/7bdOcAzDLued7SOVCo6DKG+uNeyX0I1e5h47aALRhfNLrj7q1ur8sEid9AEo/bBRjjleSfVHfJJ0O43xiWPa9UNley8uu2Tg3dW3eiCBRSqG/Nfre6ri8PqDNcf4VXn5PHsOw==</diagram></mxfile>
|
Before Width: | Height: | Size: 38 KiB |
Before Width: | Height: | Size: 19 KiB |
|
@ -1 +0,0 @@
|
|||
<mxfile userAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36" version="5.5.1.6" editor="www.draw.io" type="device"><diagram>7Vhdb9s4EPw1fuwhtqK0eYw/ctdDeijqHO7uyWAkWmJLcX0UFcf99d2llpZkKUiKOHkobBiGOVx+zcxybY2iWfHwuxWb/BOkUo8mZ+nDKJqPJpMPkwg/CdjVQBxf1kBmVVpD4wZYqu+SwTNGK5XKshPoALRTmy6YgDEycR1MWAvbbtgadHfVjcjCig2wTITuo/+o1OXhWBcN/odUWR5WHl/w+e5E8i2zUBlebzSJ1v5VdxcizMUHLXORwrYFRQuk1QLgzPSteJhJTdQG2upx14/07vdtpeG9PTEg7MPtwtllilRwE6zLIQMj9KJBp/58kmY4w1buCo1fx/gVF7W7fxn3jf+o8Vvsm+kVKUOzbqS5zZWp0WuFO6qHfJXO7dgNonKAULODG4ANr1PvmTb66LEZKqGyCUdh2xtJ2Exy1Ps95ehkCYXELWOIlVo4dd+dXbCnsn1cwyt+YWqHaeal74WueNJrJXX60XxF985lAT0Vyq0qtDBEd4vgNRgXtKG20CpDGucJnltaBO6ldQp9fMUdjjibJrnS6Y3YQUXnLh2aNLSmOVj1HacVYQ3sto5FQMO3I5Y0krWyssSYz4FyGllDN6IkgGIS0FpsSnW333CB5CszBeeg4KBwUvLBDDTgMZCAkDZBbDqY5OvlMbn7QoYB7HK+jUJz26R2FDOWt9P6gsGXSM83YUv6d9TrlR/PJaZCKk2CO7tC+MreKWeF3bU6Dp2BNHjdGleUzsI3Gbgz4F2zbtHJUHCLlmuaYdgr5UYkymQ3PmZ+3iBfmBiCtrlycok47WmLJYAyFedba5/guUpx7/6mcMKJWn7SegPKOE9mPMU30juj2yHGc82wjSqGNr4p3LoZGDyfwHE4TKK3tpL89Txb+Lzr24J9EK7zp3wQ4l5ig/OeDXrCanWY7rWwoQL5C+CnVS1QC39rs4y3/hZ956fvSB31pSboUEIt7qT+DKVyCmh+W8ceSPsG6sXM4FPqfXi5ePGAeHiEszVd4k2qrj79vbxdTRerj3/9uZjdLuarZZUksizXldac4feAk55S+rVMEW7st0hpnqLliudc36fCfsTCHr/vVvaIk70l/76It+W/PEJh978de4XdX491qi+dxRw7JfsLkt2n2M/U70G1j5HsbKxT/T6aegP1e1C9I9Rv/nPeq98OOEdP+foWig8U51fL1zHvrqX5F/l/pbCUPUNhX/Ae5ZGooscTVBQP6vVzpOYMJl2PUALPw88bJvmS/3C2SB7k+AgVcNx/rBEeu/wi5EYHvy8u+hUnEHl0cvsPDn4xcuMD58avRi42m0eqvq/12Dpa/AA=</diagram></mxfile>
|
Before Width: | Height: | Size: 23 KiB |
|
@ -1 +0,0 @@
|
|||
<mxfile userAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36" version="5.5.1.6" editor="www.draw.io" type="device"><diagram>7VhZbxs3EP41AtqHFDqydvOoy20KJwgiF2meDHp3pGXC5ahcyrLy6zPkDrWnYQVe5yGQIAjit8Njvm8OiYPJPHv4y4ht+g4TUIPxMHkYTBaD8fjP8YQ+HXAogCh6UwAbI5MCGpXASn4DBoeM7mQCec3QIiort3UwRq0htjVMGIP7utkaVX3XrdiEHUtgFQvVRj/JxKbBrYsS/xvkJg07jy7YvzsRf90Y3GnebzCerP2reJyJsBY7mqciwX0FmiyJVoNIK7tv2cMclKM20FbMu3rk6fHcBjSf7YkJ4Rz2EHyHhKjgIRqb4ga1UMsSnXn/wK0wpFFqM0VfR/SVNjWH/xj3g89u8Efkh8nUKeNW3YK+SaUu0CtJJyqmfAFrDxwNYmeRoPIE14hb3qc4szvoo24zlOPOxGxFYx9IwmyArS6PlFMkA2ZARyYTA0pYeV9fXXBMbY52Ja/0hantppm3vhdqx4teSVDJ7PBeZPBWf6EYXkCGLS3yvcyU0I70Cs1r1DYo5MZCyQ2RuYjJezAE3IOxkqJ5yg+sY24Wp1Il1+KAO+d9bilUw2iWopHfaFkR9qDHxrIUFPZVi5WbyYoZyMnmQyDezSyga5E7wNnEqJTY5vLueOCMJJB6htZixkbBUxcNc1RIbhABIXmC5M4x4CLzmOhtOUNVKiZwSQqRvy/ze3LJOqWV3H7Dds+Rn6thRf5XNNSkfbIASoYEdEzHmhI4NXfSGmEOlQfNqCAKvGZlROTW4FcIvGn0EbOuUMlQiBQFa7dCd5zkWxFLvbn2NovXJfKRWXHQPpUWVoS7M+2pCbhcpfXWyqd4KhM6u68VVlhRSO903qLU1lMZzehN5M5dfYjIrzmNScEwprczN3aOmvwTNI+mAcXVHlxsnRYSXtF2SHAQhIL+VBAEu+cEwetWELSEVbKZ6oWwoQf55P9hVTPSwtdtlvHG19FXfvma1JO21A5qSqjEHagPmEsr0a1vCtuGtD9BvYgZfEo9TvzniBd1iEcuNHP49t2/q5vb2fL27ft/lvOb5eJ2jsZQdVec3itriN1zRr9YTFycWNb7yGheohIUn8FONdoUzCl1/Nzde+zuUWjUob+P2rX9+LO71uC5Oz8nEvzvyFaD94XynPc95b2X80c6eafafeQ995NzJ+9NvY5O3qleD52c/6i3OrnFIkd/+/2csT9F845O/WIZO+LTVVT/CP/vJHWzExT2Pe9RHh1V7rLC9cVGyz5Fas5hp2sPXXDU7IIRp0yF5UB8jeQe/uWO2rcc4RbmF2E3umywe9FRt16K3fYlwi/G7njYYHfY0dP7YZeG5R2rf1a5x54svwM=</diagram></mxfile>
|
Before Width: | Height: | Size: 36 KiB |
|
@ -1 +0,0 @@
|
|||
<mxfile userAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36" version="5.5.1.6" editor="www.draw.io" type="device"><diagram>7Vptb+I4EP41le4+bEUIUPZjeenenrqn3tLT3X6qTGIS7zox5zil7K/fcTLTvHbhFOAkBEKAx87EM88zL7G4cqfRywfN1uEn5XN51e/5L1fu7KrfH/dd+LSCbS4Y9VAQaOHnIqcQLMR3jsIeSlPh86Sy0CgljVhXhZ6KY+6ZioxprTbVZSslq3dds4DuWAgWHpNN6d/CNyGZNSrkv3ERhHRnZ/Q+n1ky71ugVRrj/a767ip75dMRI11oaBIyX21KIncObtVKgWb7K3qZcmldS27Lr7t7Y/Z135rHuLcdF9A+zJZs5z64AodKm1AFKmZyXkgnmX3caujBKDSRhJ8O/ISb6u0/KM8GX+zgepgN/VuLjNW65vFjKOJceidgR/klX7kxW2QDS40CUbGDe6XWeJ+mlWh4olLtoR0wznjDdMBx1U0ushaWLkPPfOAq4rBlWKC5ZEY8V8nAkFPB67rCr/ADXdvuZtzLM5MpKr0TXPp/pkyKleD6Y/wVaDzjkWrAkWxEJFls/V7y9ErFhkCyY9ATgD9nHniEaxA8c20EEPoWJ4x13sQLhfTv2Val1iOJAbbSaBIqLb6DWkb3gGltEA1gfnnFwl6JoGmewJoHAsNemYvuWWIFdo2npGTrRCxfNxwBLCKeKGNUhIvIUkuIqZIKzAAHUPz8BHVrK8fU8waiNIt0x7REw00R4+4NysJKfKOwCwcwA5Y48M5mSL5iqQToISZ8Hnuws1sQ3+qlMJrpbWmiP5IWJ188w8/A/gQFIxZZYONlkn2xiPs1XdUVP1EMBpR0N4gILs5oUpAwMVp94wRVrDKSrkrooYjIKfnKaminZrJmnoiD+2zNbFBIPiMOVrQJheELkNs9baD02AwB+lYySyyh8MGiLEMZZljONkuttRKxybAbTuANaE5tVhqCXVMYA2loDG+7XJupisE+BtfBZRyovOGWzi0spODezUKkHZWRXbQbDLqzDlWUWNcAVop6dsmBpcqX5Zv/jGoEWGTVAmF8zLL3u0x9BWq3CbUV1SGUbMnlg0qEEcrq1/naGrQnQI+Q2oXeuDt4DhLlDKvzsFmdHYTi9OUZN1MJEjCjB6VZltLp06e/Fo9Pk/nTxz9+n08f57OnRep5PElWqZTbX37NU/ezArWX5Lln+HUt4WPs6XZFo4vltwtNMBpLNGmtppcG7ngN3IgenKiDQ1hL8DujFvjfH6CBQ6bVGrisLuWRvzAaQu4S+/vGPsVTl8apFW1a1wVtLN+Xxulg6A3Rg7vQO0DjhKcxjYJuVB6jVKsvEXtkzAnfU0Ssg23cobvl6xuoMqWO2bnuuWMSPHAtYKe2ZGNf/dpIL6WyNfaYXbRDh6OlNhq5f/oumjZT7o9iZUKuL23Siduk4bjaJrmDZhz2e20nXSTsRITmceelU+r2lLQ/A95ulVoBP0jibR5tXnqljvi1NEut+B3ilKntjPDSLf0fUdvSLh0vapvPs5/5v6mAorYHxFnpe9OR1le2ubHlsVa598Eaw9gCe4BiOKgXw2Zotbakh6iEzafIM/XxsObj8Qmd3Hzao1b5TLzr0vEVpYnmoSc58tDOJR3n69xhzbkt6eFozm0+MZ0bc2u516FgPYV3m48h50ZdSqlE3eHRvAvD4l9J2Vzpn1/u/Ac=</diagram></mxfile>
|
Before Width: | Height: | Size: 45 KiB |
Before Width: | Height: | Size: 17 KiB |
|
@ -1 +0,0 @@
|
|||
<mxfile userAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36" version="5.5.1.6" editor="www.draw.io" type="device"><diagram>7Vhtb+I4EP41SHcftgICtP1YoN1bqXtalZ7u7lPlJiZx1/HkHANlf/3OOGOSEPpyonfaOxUhFD8ejz3zzItDL5rljx+tKLLPkEjdG/aTx1407w2HZ8MIfwnYVsB4fF4BqVVJBQ1qYKG+SQb7jK5UIsuWoAPQThVtMAZjZOxamLAWNm2xJej2roVIw441sIiF7qK/q8RlwaxJjf8iVZqFnQcTtu9exF9TCyvD+/WG0dJ/qulcBF1saJmJBDYNKLpEt1oA1ExP+eNManJtcFu17uqJ2d25rTR8tucX4BQtWAu9YtN7w4nGpdMCH1J6uFJSJzeyhJWN5SfzgP5WYOYyhyCK6nfSbJbbBleWG5VrYXA0zVyuERzg4xKMW7AQjYVWqcHnGM8tLQJraZ1CQi54wkGBaJwpnVyLLazIutKht8NomoFV31CtCHvgtHUcW6PzlsSCViLcR9TKEmW+BJfRygq6FiUBJBOD1qIo1f3uwLmwqTJTcA5yFgqWXimtZ6ABzUAHBP6Da5rcMF1kq+TU8RBz9VFCLp3dokiY5bDhtBpxmm3qGI1GLJI14/OUBQXnRbrTXMcGPnB4HA4VVtEIlQ+U4XIpVtqhzQheIPAg1uJEwYlH9oMBzfRU1YFQOgtfZXCXAR8oy4YHGQoBouWSNBwOj7IQsTLptZeZj2rkhn1B0CZTTi4QpzNtsHwhBqhvqX0iZipJJKqcWnDCiYpxorcAZZz333iKX3TzrH8y7o3RrhmOkbgwxi+JWzcDg/YJXIfLJIbTRlJIHYiEkIcvR0KgngvOS9RHXLKOYX50oEjsEavVfoZXxIbq6XP+b7OaIxe0Q6Dxllief/DqW1RHXaoJ2qdQi3upv0CpqIIhZivZPWr/BfbOxq9j7+x48ninFnloQr/QaFko6xfGkKvQK3eff1vc3l19+nV+N69T+6efq+ReA6p9T+p/KizOX1nPx4wdExennbh4tnL/GG0cL2A/SBvvkHqgsz/Zxk/77T4+CNfBBvGDyaF6wCF1DPHcON6reTNtX6DTJ8uTaRteC15ib8SN9KhbGJ+uwZZM8N2Fh2hoBikYoS9rFD2HLySSNJDfGrSigXb7B+N+8CcNsOLRMLmgVynSWkhzmykqoIhSYvCSB+nclnNTrBxQ1d2d4Bo8wbRPdWY66PMZg3b5ftS6cWL+p/J5IqzU2L3Wbe1Hubn7YnQj/1opLCmv6H++8DzZZSgiyVlUnPbq5msaIacNdb03KEXjvVeKwaQbzIdiOVSso5zcfaXoRvd/2rtR6NTh3te9tQdHvrlzu7f2/5lzJ6fszRC6I1bx9t7FYf2fjJ9r/O8VXX4H</diagram></mxfile>
|
Before Width: | Height: | Size: 29 KiB |
Before Width: | Height: | Size: 19 KiB |
|
@ -1 +0,0 @@
|
|||
<mxfile userAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36" version="5.5.1.6" editor="www.draw.io" type="device"><diagram>3Vhbb+I6EP41SN0HECGQbh8boNuutkerltXZ84QMcRL3OHFqHAr99R0749y4qCpdqV0eQvx5PJ755mJDxx0nm2+SZPGtCCjvDPrBpuNOOoPB14ELTw1sC2A0uiiASLKggJwKuGfPFME+ojkL6KohqITgimVNcCnSlC5VAyNSiqemWCh4c9eMRHbHCrhfEr6L/ssCFVu3vAq/piyK7c6Oh/4tyPL/SIo8xf06Azc0n2I6IVYXOrqKSSCeapA7BVqlEKBZvyWbMeWaWktbse7qwGxpt6Qp2nZ8ATq0UlvrOg2ACRwKqWIRiZTwaYX6xj2qFfRhFKuEw6sDr7Cn3P5G3Az+04PeyAyDSx0YrTWj6SxmaYFeMTCoWPJAldpiMpBcCYAqC34IkeE+hc3a0INeI7QSuVyiFKakIjKiKDUqGYdEpiKhYDKISMqJYuumdoIpFZVyFa3wgsweCAtGe014jlrv6GPOJOR4m39FN2BdnVaQYs9kYQQ0TZlgqTLWjPzOaAII4SwCOidL8J9KANZUKgbpfIkTCQsCEztOFpT7ZZKOBRcgD/vaNLXsag0Uy7lZbGhJlcN13k0+7TKKivo9DAK2he6wGL6acNT8UzNQqe2CeE2r01IgwnAFEW/HqzTvVSFEw2sR7Aw8riOVNeLnPea6dv0E0kyn+CXM9rMNPA210AE03lU6mfXcsDanQ9/FWOo5DGepE94i/DY7LyxwS6FKgjtapPtN+gBhYiKd0ERYWfCvFC/Xa90NBKTAnSYWg1QfMrDpH3B8yKw3EcJpCFIlI23dXX2Y0JDkXEHDAM1a8oGsSY+JnkHe2QMLaIwkECw/Xaz0l5VZZSTd6yhnKe3GeDpoM52ezpXWVjVn5re/7mdzfzq/+ef7dDybTuZjISWEkEPOl14V+zXtPNGGsy/F5FpAlh/faR+tO8CJ5hwh+6j/BQYlPqk4PavnxlvdbHXmdlM1NVz23iKBfQFSITfnXGhONj8UqcJDzYEOZsZXJGEQXXdyTfmaaq3Nln+wB++ccQd7rWPbM3ZFF4dP1X3G7SNmQ2PWnSN4yoFnDtaP2i0PtI2d9vhhm+En7N+vNflTl5xna8xeRIa7Ned4e2ru4h1K7nyn5Owd+MS7JVzEMXv0ghb3+wMErXXJ0mhmbuxwyXsXdodavsbuBTaZGrm25dW5tdgp3OKPu7+XW8/emW3men+MXBhWP2+Li3j1F4I7fQE=</diagram></mxfile>
|
|
@ -1,6 +1,6 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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>
|
||||
|
||||
|
@ -9,8 +9,10 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xs
|
|||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<name>@Resource vs @Inject vs @Autowired</name>
|
||||
<description>Accompanying the demonstration of the use of the annotations related to injection mechanisms, namely @Resource, @Inject, and @Autowired</description>
|
||||
<name>Resource vs Inject vs Autowired</name>
|
||||
<description>Accompanying the demonstration of the use of the annotations related to injection mechanisms, namely
|
||||
Resource, Inject, and Autowired
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
@ -56,8 +58,8 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xs
|
|||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
|
@ -65,7 +67,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xs
|
|||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<includes>
|
||||
<include>**/*Demo.java</include>
|
||||
<include>**/*Test.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.configuration;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = {"com.baeldung.dependency"})
|
||||
public class ApplicationContextTestAutowiredName {
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.configuration;
|
||||
|
||||
import com.baeldung.dependency.AnotherArbitraryDependency;
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ApplicationContextTestAutowiredQualifier {
|
||||
|
||||
@Bean
|
||||
public ArbitraryDependency autowiredFieldDependency() {
|
||||
ArbitraryDependency autowiredFieldDependency = new ArbitraryDependency();
|
||||
|
||||
return autowiredFieldDependency;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ArbitraryDependency anotherAutowiredFieldDependency() {
|
||||
ArbitraryDependency anotherAutowiredFieldDependency = new AnotherArbitraryDependency();
|
||||
|
||||
return anotherAutowiredFieldDependency;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.configuration;
|
||||
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ApplicationContextTestAutowiredType {
|
||||
|
||||
@Bean
|
||||
public ArbitraryDependency autowiredFieldDependency() {
|
||||
ArbitraryDependency autowiredFieldDependency = new ArbitraryDependency();
|
||||
return autowiredFieldDependency;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.configuration;
|
||||
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
import com.baeldung.dependency.YetAnotherArbitraryDependency;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ApplicationContextTestInjectName {
|
||||
|
||||
@Bean
|
||||
public ArbitraryDependency yetAnotherFieldInjectDependency() {
|
||||
ArbitraryDependency yetAnotherFieldInjectDependency = new YetAnotherArbitraryDependency();
|
||||
return yetAnotherFieldInjectDependency;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.configuration;
|
||||
|
||||
import com.baeldung.dependency.AnotherArbitraryDependency;
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ApplicationContextTestInjectQualifier {
|
||||
|
||||
@Bean
|
||||
public ArbitraryDependency defaultFile() {
|
||||
ArbitraryDependency defaultFile = new ArbitraryDependency();
|
||||
return defaultFile;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ArbitraryDependency namedFile() {
|
||||
ArbitraryDependency namedFile = new AnotherArbitraryDependency();
|
||||
return namedFile;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.configuration;
|
||||
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ApplicationContextTestInjectType {
|
||||
|
||||
@Bean
|
||||
public ArbitraryDependency injectDependency() {
|
||||
ArbitraryDependency injectDependency = new ArbitraryDependency();
|
||||
return injectDependency;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@Configuration
|
||||
public class ApplicationContextTestResourceNameType {
|
||||
|
||||
@Bean(name = "namedFile")
|
||||
public File namedFile() {
|
||||
File namedFile = new File("namedFile.txt");
|
||||
return namedFile;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@Configuration
|
||||
public class ApplicationContextTestResourceQualifier {
|
||||
|
||||
@Bean(name = "defaultFile")
|
||||
public File defaultFile() {
|
||||
File defaultFile = new File("defaultFile.txt");
|
||||
return defaultFile;
|
||||
}
|
||||
|
||||
@Bean(name = "namedFile")
|
||||
public File namedFile() {
|
||||
File namedFile = new File("namedFile.txt");
|
||||
return namedFile;
|
||||
}
|
||||
}
|
|
@ -1,5 +1,8 @@
|
|||
package com.baeldung.dependency;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class AnotherArbitraryDependency extends ArbitraryDependency {
|
||||
|
||||
private final String label = "Another Arbitrary Dependency";
|
|
@ -1,5 +1,8 @@
|
|||
package com.baeldung.dependency;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value = "autowiredFieldDependency")
|
||||
public class ArbitraryDependency {
|
||||
|
||||
private final String label = "Arbitrary Dependency";
|
|
@ -1,5 +1,8 @@
|
|||
package com.baeldung.dependency;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class YetAnotherArbitraryDependency extends ArbitraryDependency {
|
||||
|
||||
private final String label = "Yet Another Arbitrary Dependency";
|
|
@ -1,26 +1,28 @@
|
|||
package com.baeldung.autowired;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestAutowiredName;
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Autowired-Name.xml"})
|
||||
public class FieldAutowiredNameDemo {
|
||||
@ContextConfiguration(
|
||||
loader = AnnotationConfigContextLoader.class,
|
||||
classes = ApplicationContextTestAutowiredName.class)
|
||||
public class FieldAutowiredNameTest {
|
||||
|
||||
@Autowired
|
||||
private ArbitraryDependency autowiredFieldDependency;
|
||||
|
||||
@Test
|
||||
public void autowiredFieldDependency_MUST_BE_AUTOWIRED_Correctly() {
|
||||
public void givenAutowiredAnnotation_WhenOnField_ThenDependencyValid() {
|
||||
assertNotNull(autowiredFieldDependency);
|
||||
assertEquals("Arbitrary Dependency", autowiredFieldDependency.toString());
|
||||
}
|
|
@ -1,26 +1,28 @@
|
|||
package com.baeldung.autowired;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestAutowiredType;
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Autowired-Type.xml"})
|
||||
public class FieldAutowiredDemo {
|
||||
@ContextConfiguration(
|
||||
loader = AnnotationConfigContextLoader.class,
|
||||
classes = ApplicationContextTestAutowiredType.class)
|
||||
public class FieldAutowiredTest {
|
||||
|
||||
@Autowired
|
||||
private ArbitraryDependency fieldDependency;
|
||||
|
||||
@Test
|
||||
public void fieldDependency_MUST_BE_AUTOWIRED_Correctly() {
|
||||
public void givenAutowired_WhenSetOnField_ThenDependencyResolved() {
|
||||
assertNotNull(fieldDependency);
|
||||
assertEquals("Arbitrary Dependency", fieldDependency.toString());
|
||||
}
|
|
@ -1,21 +1,23 @@
|
|||
package com.baeldung.autowired;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestAutowiredQualifier;
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Autowired-Qualifier.xml"})
|
||||
public class FieldQualifierAutowiredDemo {
|
||||
@ContextConfiguration(
|
||||
loader = AnnotationConfigContextLoader.class,
|
||||
classes = ApplicationContextTestAutowiredQualifier.class)
|
||||
public class FieldQualifierAutowiredTest {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("autowiredFieldDependency")
|
||||
|
@ -26,13 +28,13 @@ public class FieldQualifierAutowiredDemo {
|
|||
private ArbitraryDependency fieldDependency2;
|
||||
|
||||
@Test
|
||||
public void fieldDependency1_MUST_BE_AUTOWIRED_Correctly() {
|
||||
public void givenAutowiredQualifier_WhenOnField_ThenDep1Valid() {
|
||||
assertNotNull(fieldDependency1);
|
||||
assertEquals("Arbitrary Dependency", fieldDependency1.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fieldDependency2_MUST_BE_AUTOWIRED_Correctly() {
|
||||
public void givenAutowiredQualifier_WhenOnField_ThenDep2Valid() {
|
||||
assertNotNull(fieldDependency2);
|
||||
assertEquals("Another Arbitrary Dependency", fieldDependency2.toString());
|
||||
}
|
|
@ -1,29 +1,31 @@
|
|||
package com.baeldung.inject;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestInjectName;
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Inject-Name.xml"})
|
||||
public class FieldByNameInjectDemo {
|
||||
@ContextConfiguration(
|
||||
loader = AnnotationConfigContextLoader.class,
|
||||
classes = ApplicationContextTestInjectName.class)
|
||||
public class FieldByNameInjectTest {
|
||||
|
||||
@Inject
|
||||
@Named("yetAnotherFieldInjectDependency")
|
||||
private ArbitraryDependency yetAnotherFieldInjectDependency;
|
||||
|
||||
@Test
|
||||
public void yetAnotherFieldInjectDependency_MUST_BE_INJECTED_Correctly() {
|
||||
public void givenInjectQualifier_WhenSetOnField_ThenDependencyValid() {
|
||||
assertNotNull(yetAnotherFieldInjectDependency);
|
||||
assertEquals("Yet Another Arbitrary Dependency", yetAnotherFieldInjectDependency.toString());
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
package com.baeldung.inject;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Inject-Type.xml"})
|
||||
public class FieldInjectDemo {
|
||||
|
||||
@Inject
|
||||
private ArbitraryDependency inject1Dependency;
|
||||
|
||||
@Test
|
||||
public void fieldDependency_MUST_BE_INJECTED_Successfully() {
|
||||
assertNotNull(inject1Dependency);
|
||||
assertEquals("Arbitrary Dependency", inject1Dependency.toString());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.inject;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestInjectType;
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(
|
||||
loader = AnnotationConfigContextLoader.class,
|
||||
classes = ApplicationContextTestInjectType.class)
|
||||
public class FieldInjectTest {
|
||||
|
||||
@Inject
|
||||
private ArbitraryDependency fieldInjectDependency;
|
||||
|
||||
@Test
|
||||
public void givenInjectAnnotation_WhenOnField_ThenValidDependency() {
|
||||
assertNotNull(fieldInjectDependency);
|
||||
assertEquals("Arbitrary Dependency", fieldInjectDependency.toString());
|
||||
}
|
||||
}
|
|
@ -1,22 +1,23 @@
|
|||
package com.baeldung.inject;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestInjectQualifier;
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Inject-Qualifier.xml"})
|
||||
public class FieldQualifierInjectDemo {
|
||||
@ContextConfiguration(loader = AnnotationConfigContextLoader.class,
|
||||
classes = ApplicationContextTestInjectQualifier.class)
|
||||
public class FieldQualifierInjectTest {
|
||||
|
||||
@Inject
|
||||
@Qualifier("defaultFile")
|
||||
|
@ -27,13 +28,13 @@ public class FieldQualifierInjectDemo {
|
|||
private ArbitraryDependency namedDependency;
|
||||
|
||||
@Test
|
||||
public void defaultDependency_MUST_BE_INJECTED_Successfully() {
|
||||
public void givenInjectQualifier_WhenOnField_ThenDefaultFileValid() {
|
||||
assertNotNull(defaultDependency);
|
||||
assertEquals("Arbitrary Dependency", defaultDependency.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void namedDependency_MUST_BE_INJECTED_Correctly() {
|
||||
public void givenInjectQualifier_WhenOnField_ThenNamedFileValid() {
|
||||
assertNotNull(defaultDependency);
|
||||
assertEquals("Another Arbitrary Dependency", namedDependency.toString());
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
package com.baeldung.resource;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Resource-NameType.xml"})
|
||||
public class FieldResourceInjectionDemo {
|
||||
|
||||
@Resource(name="namedFile")
|
||||
private File defaultFile;
|
||||
|
||||
@Test
|
||||
public void plainResourceAnnotation_MUST_FIND_DefaultFile() {
|
||||
assertNotNull(defaultFile);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.resource;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestResourceNameType;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(
|
||||
loader = AnnotationConfigContextLoader.class,
|
||||
classes = ApplicationContextTestResourceNameType.class)
|
||||
public class FieldResourceInjectionTest {
|
||||
|
||||
@Resource(name = "namedFile")
|
||||
private File defaultFile;
|
||||
|
||||
@Test
|
||||
public void givenResourceAnnotation_WhenOnField_ThenDependencyValid() {
|
||||
assertNotNull(defaultFile);
|
||||
assertEquals("namedFile.txt", defaultFile.getName());
|
||||
}
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
package com.baeldung.resource;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Resource-Qualifier.xml"})
|
||||
public class MethodByQualifierResourceDemo {
|
||||
|
||||
private File arbDependency;
|
||||
private File anotherArbDependency;
|
||||
|
||||
@Test
|
||||
public void dependencies_MUST_BE_INJECTED_Correctly() {
|
||||
assertNotNull(arbDependency);
|
||||
assertEquals("namedFile.txt", arbDependency.getName());
|
||||
assertNotNull(anotherArbDependency);
|
||||
assertEquals("defaultFile.txt", anotherArbDependency.getName());
|
||||
}
|
||||
|
||||
@Resource
|
||||
@Qualifier("namedFile")
|
||||
public void setArbDependency(File arbDependency) {
|
||||
this.arbDependency = arbDependency;
|
||||
}
|
||||
|
||||
@Resource
|
||||
@Qualifier("defaultFile")
|
||||
public void setAnotherArbDependency(File anotherArbDependency) {
|
||||
this.anotherArbDependency = anotherArbDependency;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package com.baeldung.resource;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestResourceQualifier;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(
|
||||
loader = AnnotationConfigContextLoader.class,
|
||||
classes = ApplicationContextTestResourceQualifier.class)
|
||||
public class MethodByQualifierResourceTest {
|
||||
|
||||
private File arbDependency;
|
||||
private File anotherArbDependency;
|
||||
|
||||
@Test
|
||||
public void givenResourceQualifier_WhenSetter_ThenValidDependencies() {
|
||||
assertNotNull(arbDependency);
|
||||
assertEquals("namedFile.txt", arbDependency.getName());
|
||||
assertNotNull(anotherArbDependency);
|
||||
assertEquals("defaultFile.txt", anotherArbDependency.getName());
|
||||
}
|
||||
|
||||
@Resource
|
||||
@Qualifier("namedFile")
|
||||
public void setArbDependency(File arbDependency) {
|
||||
this.arbDependency = arbDependency;
|
||||
}
|
||||
|
||||
@Resource
|
||||
@Qualifier("defaultFile")
|
||||
public void setAnotherArbDependency(File anotherArbDependency) {
|
||||
this.anotherArbDependency = anotherArbDependency;
|
||||
}
|
||||
}
|
|
@ -1,19 +1,23 @@
|
|||
package com.baeldung.resource;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestResourceNameType;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Resource-NameType.xml"})
|
||||
public class SetterResourceInjectionDemo {
|
||||
@ContextConfiguration(
|
||||
loader = AnnotationConfigContextLoader.class,
|
||||
classes = ApplicationContextTestResourceNameType.class)
|
||||
public class MethodByTypeResourceTest {
|
||||
|
||||
private File defaultFile;
|
||||
|
||||
|
@ -23,7 +27,8 @@ public class SetterResourceInjectionDemo {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void setter_MUST_INJECT_Resource() {
|
||||
public void givenResourceAnnotation_WhenSetter_ThenValidDependency() {
|
||||
assertNotNull(defaultFile);
|
||||
assertEquals("namedFile.txt", defaultFile.getName());
|
||||
}
|
||||
}
|
|
@ -1,29 +1,34 @@
|
|||
package com.baeldung.resource;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestResourceNameType;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Resource-NameType.xml"})
|
||||
public class MethodResourceInjectionDemo {
|
||||
@ContextConfiguration(
|
||||
loader = AnnotationConfigContextLoader.class,
|
||||
classes = ApplicationContextTestResourceNameType.class)
|
||||
public class MethodResourceInjectionTest {
|
||||
|
||||
private File defaultFile;
|
||||
|
||||
@Resource(name="namedFile")
|
||||
@Resource(name = "namedFile")
|
||||
protected void setDefaultFile(File defaultFile) {
|
||||
this.defaultFile = defaultFile;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultFile_MUST_BE_INJECTED_Correctly() {
|
||||
public void givenResourceAnnotation_WhenSetter_ThenDependencyValid() {
|
||||
assertNotNull(defaultFile);
|
||||
assertEquals("namedFile.txt", defaultFile.getName());
|
||||
}
|
||||
}
|
|
@ -1,27 +1,29 @@
|
|||
package com.baeldung.resource;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestResourceNameType;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Resource-NameType.xml"})
|
||||
@ContextConfiguration(loader = AnnotationConfigContextLoader.class,
|
||||
classes = ApplicationContextTestResourceNameType.class)
|
||||
public class NamedResourceTest {
|
||||
|
||||
@Resource(name="namedFile")
|
||||
@Resource(name = "namedFile")
|
||||
private File testFile;
|
||||
|
||||
@Test
|
||||
public void namedResource_MUST_FIND_SPECIFIED_File() {
|
||||
public void givenResourceAnnotation_WhenOnField_THEN_DEPENDENCY_Found() {
|
||||
assertNotNull(testFile);
|
||||
assertTrue(testFile.getName().equals("namedFile.txt"));
|
||||
assertEquals("namedFile.txt", testFile.getName());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
package com.baeldung.resource;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Resource-Qualifier.xml"})
|
||||
public class QualifierResourceInjectionDemo {
|
||||
|
||||
@Resource
|
||||
private File defaultFile;
|
||||
|
||||
@Resource
|
||||
@Qualifier("namedFile")
|
||||
private File namedFile;
|
||||
|
||||
@Test
|
||||
public void defaultFile_MUST_BE_Valid() {
|
||||
assertNotNull(defaultFile);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void namedFile_MUST_BE_Valid() {
|
||||
assertNotNull(namedFile);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.resource;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestResourceQualifier;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(
|
||||
loader = AnnotationConfigContextLoader.class,
|
||||
classes = ApplicationContextTestResourceQualifier.class)
|
||||
public class QualifierResourceInjectionTest {
|
||||
|
||||
@Resource
|
||||
@Qualifier("defaultFile")
|
||||
private File dependency1;
|
||||
|
||||
@Resource
|
||||
@Qualifier("namedFile")
|
||||
private File dependency2;
|
||||
|
||||
@Test
|
||||
public void givenResourceAnnotation_WhenField_ThenDependency1Valid() {
|
||||
assertNotNull(dependency1);
|
||||
assertEquals("defaultFile.txt", dependency1.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResourceQualifier_WhenField_ThenDependency2Valid() {
|
||||
assertNotNull(dependency2);
|
||||
assertEquals("namedFile.txt", dependency2.getName());
|
||||
}
|
||||
}
|
|
@ -1,20 +1,22 @@
|
|||
package com.baeldung.resource;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestResourceNameType;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Resource-NameType.xml"})
|
||||
public class MethodByTypeResourceDemo {
|
||||
@ContextConfiguration(loader = AnnotationConfigContextLoader.class,
|
||||
classes = ApplicationContextTestResourceNameType.class)
|
||||
public class SetterResourceInjectionTest {
|
||||
|
||||
private File defaultFile;
|
||||
|
||||
|
@ -24,7 +26,8 @@ public class MethodByTypeResourceDemo {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void defaultFile_MUST_BE_INJECTED_Correctly() {
|
||||
public void givenResourceAnnotation_WhenOnSetter_THEN_MUST_INJECT_Dependency() {
|
||||
assertNotNull(defaultFile);
|
||||
assertEquals("namedFile.txt", defaultFile.getName());
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
|
||||
|
||||
<bean id="autowiredFieldDependency" class="com.baeldung.dependency.ArbitraryDependency"/>
|
||||
|
||||
<bean id="anotherAutowiredFieldDependency" class="com.baeldung.dependency.AnotherArbitraryDependency"/>
|
||||
</beans>
|
|
@ -1,12 +0,0 @@
|
|||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
|
||||
|
||||
<bean id="autowiredFieldDependency" class="com.baeldung.dependency.ArbitraryDependency"/>
|
||||
|
||||
<bean id="anotherAutowiredFieldDependency" class="com.baeldung.dependency.AnotherArbitraryDependency"/>
|
||||
</beans>
|
|
@ -1,10 +0,0 @@
|
|||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
|
||||
|
||||
<bean id="autowiredFieldDependency" class="com.baeldung.dependency.ArbitraryDependency"/>
|
||||
</beans>
|
|
@ -1,11 +0,0 @@
|
|||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
|
||||
|
||||
<bean id="yetAnotherFieldInjectDependency" class="com.baeldung.dependency.YetAnotherArbitraryDependency"/>
|
||||
|
||||
</beans>
|
|
@ -1,12 +0,0 @@
|
|||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
|
||||
|
||||
<bean id="defaultFile" class="com.baeldung.dependency.ArbitraryDependency"/>
|
||||
|
||||
<bean id="namedFile" class="com.baeldung.dependency.AnotherArbitraryDependency"/>
|
||||
</beans>
|
|
@ -1,10 +0,0 @@
|
|||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
|
||||
|
||||
<bean id="injectDependency" class="com.baeldung.dependency.ArbitraryDependency"/>
|
||||
</beans>
|
|
@ -1,12 +0,0 @@
|
|||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
|
||||
|
||||
<bean id="namedFile" class="java.io.File">
|
||||
<constructor-arg value="namedFile.txt"/>
|
||||
</bean>
|
||||
</beans>
|
|
@ -1,16 +0,0 @@
|
|||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
|
||||
|
||||
<bean id="defaultFile" class="java.io.File">
|
||||
<constructor-arg value="defaultFile.txt"/>
|
||||
</bean>
|
||||
|
||||
<bean id="namedFile" class="java.io.File">
|
||||
<constructor-arg value="namedFile.txt"/>
|
||||
</bean>
|
||||
</beans>
|
|
@ -0,0 +1,33 @@
|
|||
package org.baeldung.hamcrest;
|
||||
|
||||
public class Animal {
|
||||
String name;
|
||||
boolean wild;
|
||||
String sound;
|
||||
|
||||
public Animal(String name, boolean wild, String sound) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.wild = wild;
|
||||
this.sound = sound;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public boolean isWild() {
|
||||
return wild;
|
||||
}
|
||||
public void setWild(boolean wild) {
|
||||
this.wild = wild;
|
||||
}
|
||||
public String getSound() {
|
||||
return sound;
|
||||
}
|
||||
public void setSound(String sound) {
|
||||
this.sound = sound;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package org.baeldung.hamcrest;
|
||||
|
||||
public class Cat extends Animal {
|
||||
|
||||
public Cat() {
|
||||
super("cat", false, "meow");
|
||||
}
|
||||
|
||||
public String makeSound() {
|
||||
return getSound();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,331 @@
|
|||
package org.baeldung.hamcrest;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.baeldung.hamcrest.IsPositiveInteger.isAPositiveInteger;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.hamcrest.beans.HasProperty.hasProperty;
|
||||
import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
|
||||
import static org.hamcrest.beans.SamePropertyValuesAs.samePropertyValuesAs;
|
||||
import static org.hamcrest.collection.IsArrayContaining.hasItemInArray;
|
||||
import static org.hamcrest.collection.IsArrayContainingInAnyOrder.arrayContainingInAnyOrder;
|
||||
import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining;
|
||||
import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize;
|
||||
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
|
||||
import static org.hamcrest.collection.IsEmptyCollection.empty;
|
||||
import static org.hamcrest.collection.IsIn.isIn;
|
||||
import static org.hamcrest.collection.IsIn.isOneOf;
|
||||
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
|
||||
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
|
||||
import static org.hamcrest.collection.IsMapContaining.hasEntry;
|
||||
import static org.hamcrest.collection.IsMapContaining.hasKey;
|
||||
import static org.hamcrest.collection.IsMapContaining.hasValue;
|
||||
import static org.hamcrest.core.AllOf.allOf;
|
||||
import static org.hamcrest.core.AnyOf.anyOf;
|
||||
import static org.hamcrest.core.Every.everyItem;
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.hamcrest.core.IsInstanceOf.instanceOf;
|
||||
import static org.hamcrest.core.IsNot.not;
|
||||
import static org.hamcrest.core.IsNull.notNullValue;
|
||||
import static org.hamcrest.core.IsSame.sameInstance;
|
||||
import static org.hamcrest.core.StringContains.containsString;
|
||||
import static org.hamcrest.core.StringEndsWith.endsWith;
|
||||
import static org.hamcrest.core.StringStartsWith.startsWith;
|
||||
import static org.hamcrest.object.HasToString.hasToString;
|
||||
import static org.hamcrest.object.IsCompatibleType.typeCompatibleWith;
|
||||
import static org.hamcrest.text.IsEmptyString.isEmptyOrNullString;
|
||||
import static org.hamcrest.text.IsEmptyString.isEmptyString;
|
||||
import static org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase;
|
||||
import static org.hamcrest.text.IsEqualIgnoringWhiteSpace.equalToIgnoringWhiteSpace;
|
||||
import static org.hamcrest.text.StringContainsInOrder.stringContainsInOrder;
|
||||
|
||||
public class HamcrestMatcherTest {
|
||||
@Test
|
||||
public void given2Strings_whenEqual_thenCorrect() {
|
||||
String a = "foo";
|
||||
String b = "FOO";
|
||||
assertThat(a, equalToIgnoringCase(b));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBean_whenHasValue_thenCorrect() {
|
||||
Person person = new Person("Baeldung", "New York");
|
||||
assertThat(person, hasProperty("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBean_whenHasCorrectValue_thenCorrect() {
|
||||
Person person = new Person("Baeldung", "New York");
|
||||
assertThat(person, hasProperty("address", equalTo("New York")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given2Beans_whenHavingSameValues_thenCorrect() {
|
||||
Person person1 = new Person("Baeldung", "New York");
|
||||
Person person2 = new Person("Baeldung", "New York");
|
||||
assertThat(person1, samePropertyValuesAs(person2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenChecksSize_thenCorrect() {
|
||||
List<String> hamcrestMatchers = Arrays.asList("collections", "beans",
|
||||
"text", "number");
|
||||
assertThat(hamcrestMatchers, hasSize(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArray_whenChecksSize_thenCorrect() {
|
||||
String[] hamcrestMatchers = { "collections", "beans", "text", "number" };
|
||||
assertThat(hamcrestMatchers, arrayWithSize(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListAndValues_whenChecksListForGivenValues_thenCorrect() {
|
||||
List<String> hamcrestMatchers = Arrays.asList("collections", "beans",
|
||||
"text", "number");
|
||||
assertThat(hamcrestMatchers,
|
||||
containsInAnyOrder("beans", "text", "collections", "number"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListAndValues_whenChecksListForGivenValuesWithOrder_thenCorrect() {
|
||||
List<String> hamcrestMatchers = Arrays.asList("collections", "beans",
|
||||
"text", "number");
|
||||
assertThat(hamcrestMatchers,
|
||||
contains("collections", "beans", "text", "number"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayAndValue_whenValueFoundInArray_thenCorrect() {
|
||||
String[] hamcrestMatchers = { "collections", "beans", "text", "number" };
|
||||
assertThat(hamcrestMatchers, hasItemInArray("text"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValueAndArray_whenValueIsOneOfArrayElements_thenCorrect() {
|
||||
String[] hamcrestMatchers = { "collections", "beans", "text", "number" };
|
||||
assertThat("text", isOneOf(hamcrestMatchers));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayAndValues_whenValuesFoundInArray_thenCorrect() {
|
||||
String[] hamcrestMatchers = { "collections", "beans", "text", "number" };
|
||||
assertThat(
|
||||
hamcrestMatchers,
|
||||
arrayContainingInAnyOrder("beans", "collections", "number",
|
||||
"text"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayAndValues_whenValuesFoundInArrayInOrder_thenCorrect() {
|
||||
String[] hamcrestMatchers = { "collections", "beans", "text", "number" };
|
||||
assertThat(hamcrestMatchers,
|
||||
arrayContaining("collections", "beans", "text", "number"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCollection_whenEmpty_thenCorrect() {
|
||||
List<String> emptyList = new ArrayList<>();
|
||||
assertThat(emptyList, empty());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValueAndArray_whenValueFoundInArray_thenCorrect() {
|
||||
String[] array = new String[] { "collections", "beans", "text",
|
||||
"number" };
|
||||
assertThat("beans", isIn(array));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMapAndKey_whenKeyFoundInMap_thenCorrect() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("blogname", "baeldung");
|
||||
assertThat(map, hasKey("blogname"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMapAndEntry_whenEntryFoundInMap_thenCorrect() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("blogname", "baeldung");
|
||||
assertThat(map, hasEntry("blogname", "baeldung"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMapAndValue_whenValueFoundInMap_thenCorrect() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("blogname", "baeldung");
|
||||
assertThat(map, hasValue("baeldung"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenEmpty_thenCorrect() {
|
||||
String str = "";
|
||||
assertThat(str, isEmptyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenEmptyOrNull_thenCorrect() {
|
||||
String str = null;
|
||||
assertThat(str, isEmptyOrNullString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given2Strings_whenEqualRegardlessWhiteSpace_thenCorrect() {
|
||||
String str1 = "text";
|
||||
String str2 = " text ";
|
||||
assertThat(str1, equalToIgnoringWhiteSpace(str2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenContainsGivenSubstring_thenCorrect() {
|
||||
String str = "calligraphy";
|
||||
assertThat(str, stringContainsInOrder(Arrays.asList("call", "graph")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBean_whenToStringReturnsRequiredString_thenCorrect() {
|
||||
Person person = new Person("Barrack", "Washington");
|
||||
String str = person.toString();
|
||||
assertThat(person, hasToString(str));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given2Classes_whenOneInheritsFromOther_thenCorrect() {
|
||||
assertThat(Cat.class, typeCompatibleWith(Animal.class));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void given2Strings_whenIsEqualRegardlessWhiteSpace_thenCorrect() {
|
||||
String str1 = "text";
|
||||
String str2 = " text ";
|
||||
assertThat(str1, is(equalToIgnoringWhiteSpace(str2)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given2Strings_whenIsNotEqualRegardlessWhiteSpace_thenCorrect() {
|
||||
String str1 = "text";
|
||||
String str2 = " texts ";
|
||||
assertThat(str1, not(equalToIgnoringWhiteSpace(str2)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given2Strings_whenNotEqual_thenCorrect() {
|
||||
String str1 = "text";
|
||||
String str2 = "texts";
|
||||
assertThat(str1, not(str2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given2Strings_whenIsEqual_thenCorrect() {
|
||||
String str1 = "text";
|
||||
String str2 = "text";
|
||||
assertThat(str1, is(str2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAStrings_whenContainsAnotherGivenString_thenCorrect() {
|
||||
String str1 = "calligraphy";
|
||||
String str2 = "call";
|
||||
assertThat(str1, containsString(str2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAString_whenEndsWithAnotherGivenString_thenCorrect() {
|
||||
String str1 = "calligraphy";
|
||||
String str2 = "phy";
|
||||
assertThat(str1, endsWith(str2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAString_whenStartsWithAnotherGivenString_thenCorrect() {
|
||||
String str1 = "calligraphy";
|
||||
String str2 = "call";
|
||||
assertThat(str1, startsWith(str2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given2Objects_whenSameInstance_thenCorrect() {
|
||||
Cat cat = new Cat();
|
||||
assertThat(cat, sameInstance(cat));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnObject_whenInstanceOfGivenClass_thenCorrect() {
|
||||
Cat cat = new Cat();
|
||||
assertThat(cat, instanceOf(Cat.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_whenEachElementGreaterThan0_thenCorrect() {
|
||||
List<Integer> list = Arrays.asList(1, 2, 3);
|
||||
int baseCase = 0;
|
||||
assertThat(list, everyItem(greaterThan(baseCase)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenNotNull_thenCorrect() {
|
||||
String str = "notnull";
|
||||
assertThat(str, notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenMeetsAnyOfGivenConditions_thenCorrect() {
|
||||
String str = "calligraphy";
|
||||
String start = "call";
|
||||
String end = "foo";
|
||||
assertThat(str, anyOf(startsWith(start), containsString(end)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenMeetsAllOfGivenConditions_thenCorrect() {
|
||||
String str = "calligraphy";
|
||||
String start = "call";
|
||||
String end = "phy";
|
||||
assertThat(str, allOf(startsWith(start), endsWith(end)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInteger_whenAPositiveValue_thenCorrect() {
|
||||
int num = 1;
|
||||
assertThat(num, isAPositiveInteger());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnInteger_whenGreaterThan0_thenCorrect() {
|
||||
int num = 1;
|
||||
assertThat(num, greaterThan(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnInteger_whenGreaterThanOrEqTo5_thenCorrect() {
|
||||
int num = 5;
|
||||
assertThat(num, greaterThanOrEqualTo(5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnInteger_whenLessThan0_thenCorrect() {
|
||||
int num = -1;
|
||||
assertThat(num, lessThan(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnInteger_whenLessThanOrEqTo5_thenCorrect() {
|
||||
assertThat(-1, lessThanOrEqualTo(5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenADouble_whenCloseTo_thenCorrect() {
|
||||
assertThat(1.2, closeTo(1, 0.5));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package org.baeldung.hamcrest;
|
||||
|
||||
import org.hamcrest.Description;
|
||||
import org.hamcrest.Factory;
|
||||
import org.hamcrest.Matcher;
|
||||
import org.hamcrest.TypeSafeMatcher;
|
||||
|
||||
public class IsPositiveInteger extends TypeSafeMatcher<Integer> {
|
||||
|
||||
public void describeTo(Description description) {
|
||||
description.appendText("a positive integer");
|
||||
}
|
||||
|
||||
@Factory
|
||||
public static Matcher<Integer> isAPositiveInteger() {
|
||||
return new IsPositiveInteger();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean matchesSafely(Integer integer) {
|
||||
return integer > 0;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package org.baeldung.hamcrest;
|
||||
|
||||
public class Person {
|
||||
String name;
|
||||
String address;
|
||||
|
||||
public Person(String personName, String personAddress) {
|
||||
name = personName;
|
||||
address = personAddress;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String str="[address:"+address+",name:"+name+"]";
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,118 @@
|
|||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.baeldung.el.controllers;
|
||||
|
||||
import java.util.Random;
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.faces.application.Application;
|
||||
import javax.faces.application.FacesMessage;
|
||||
import javax.faces.bean.ManagedBean;
|
||||
import javax.faces.bean.ViewScoped;
|
||||
import javax.faces.component.html.HtmlInputText;
|
||||
import javax.faces.context.FacesContext;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Tayo
|
||||
*/
|
||||
@ManagedBean(name = "ELBean")
|
||||
@ViewScoped
|
||||
public class ELSampleBean {
|
||||
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String pageDescription = "This page demos JSF EL Basics";
|
||||
private int pageCounter;
|
||||
private Random randomIntGen = new Random();
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
pageCounter = randomIntGen.nextInt();
|
||||
}
|
||||
|
||||
public void save(){
|
||||
|
||||
}
|
||||
|
||||
public void saveFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
|
||||
public void saveByELEvaluation(){
|
||||
firstName = (String)evaluateEL("#{firstName.value}", String.class);
|
||||
FacesContext ctx = FacesContext.getCurrentInstance();
|
||||
FacesMessage theMessage = new FacesMessage("Name component Evaluated: "+firstName);
|
||||
theMessage.setSeverity(FacesMessage.SEVERITY_INFO);
|
||||
ctx.addMessage(null,theMessage);
|
||||
|
||||
}
|
||||
|
||||
private Object evaluateEL(String elExpression, Class<?> clazz) {
|
||||
Object toReturn = null;
|
||||
FacesContext ctx = FacesContext.getCurrentInstance();
|
||||
Application app = ctx.getApplication();
|
||||
toReturn = app.evaluateExpressionGet(ctx, elExpression, clazz);
|
||||
|
||||
return toReturn;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the firstName
|
||||
*/
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param firstName the firstName to set
|
||||
*/
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the lastName
|
||||
*/
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param lastName the lastName to set
|
||||
*/
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the pageDescription
|
||||
*/
|
||||
public String getPageDescription() {
|
||||
return pageDescription;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param pageDescription the pageDescription to set
|
||||
*/
|
||||
public void setPageDescription(String pageDescription) {
|
||||
this.pageDescription = pageDescription;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the pageCounter
|
||||
*/
|
||||
public int getPageCounter() {
|
||||
return pageCounter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param pageCounter the pageCounter to set
|
||||
*/
|
||||
public void setPageCounter(int pageCounter) {
|
||||
this.pageCounter = pageCounter;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
<?xml version='1.0' encoding='UTF-8' ?>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||
xmlns:h="http://java.sun.com/jsf/html"
|
||||
xmlns:f="http://java.sun.com/jsf/core">
|
||||
<h:head>
|
||||
<title>Baeldung | The EL Intro</title>
|
||||
|
||||
</h:head>
|
||||
|
||||
<h:body>
|
||||
<h:form id="elForm">
|
||||
|
||||
|
||||
<h:messages />
|
||||
<h:panelGrid columns="2">
|
||||
<h:outputText value="First Name"/>
|
||||
<h:inputText id="firstName" binding="#{firstName}" required="true" value="#{ELBean.firstName}"/>
|
||||
<h:outputText value="Last Name"/>
|
||||
<h:inputText id="lastName" required="true" value="#{ELBean.lastName}"/>
|
||||
<h:outputText value="Save by value binding"/>
|
||||
<h:commandButton value="Save" action="#{ELBean.save}">
|
||||
|
||||
</h:commandButton>
|
||||
<h:outputText value="Evaluate backing bean EL"/>
|
||||
<h:commandButton value="Save" action="#{ELBean.saveByELEvaluation}">
|
||||
|
||||
</h:commandButton>
|
||||
<h:outputText value="Save by passing value to method"/>
|
||||
<h:commandButton value="Save" action="#{ELBean.saveFirstName(firstName.value.toString().concat('(passed)'))}"/>
|
||||
<h:outputText value="JavaScript (click after saving First Name)"/>
|
||||
<h:button value="Alert" onclick="alert('Hello #{ELBean.firstName}')"/>
|
||||
</h:panelGrid>
|
||||
|
||||
</h:form>
|
||||
</h:body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
=========
|
||||
|
||||
## JMockit related tutorials
|
||||
|
||||
|
||||
### Relevant Articles:
|
||||
- [JMockit 101](http://www.baeldung.com/jmockit-101)
|
|
@ -0,0 +1,68 @@
|
|||
<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>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>mocks</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>jmockit</artifactId>
|
||||
<name>jmockit</name>
|
||||
|
||||
<properties>
|
||||
<junit.version>4.12</junit.version>
|
||||
<jmockit.version>1.24</jmockit.version>
|
||||
|
||||
<!-- maven plugins -->
|
||||
<maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
|
||||
<maven-surefire-plugin.version>2.18.1</maven-surefire-plugin.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jmockit</groupId>
|
||||
<artifactId>jmockit</artifactId>
|
||||
<version>${jmockit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>jmockit</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>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,10 @@
|
|||
package org.baeldung.mocks.jmockit;
|
||||
|
||||
public class Collaborator {
|
||||
public boolean collaborate(String string){
|
||||
return false;
|
||||
}
|
||||
public void receive(boolean bool){
|
||||
//NOOP
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package org.baeldung.mocks.jmockit;
|
||||
|
||||
public class Model {
|
||||
public String getInfo(){
|
||||
return "info";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package org.baeldung.mocks.jmockit;
|
||||
|
||||
public class Performer {
|
||||
private Collaborator collaborator;
|
||||
|
||||
public void perform(Model model){
|
||||
boolean value = collaborator.collaborate(model.getInfo());
|
||||
collaborator.receive(value);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package org.baeldung.mocks.jmockit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import mockit.*;
|
||||
import mockit.integration.junit4.JMockit;
|
||||
|
||||
@RunWith(JMockit.class)
|
||||
public class PerformerTest {
|
||||
|
||||
@Injectable
|
||||
private Collaborator collaborator;
|
||||
|
||||
@Tested
|
||||
private Performer performer;
|
||||
|
||||
@Test
|
||||
public void testThePerformMethod(@Mocked Model model) {
|
||||
new Expectations() {{
|
||||
model.getInfo();result = "bar";
|
||||
collaborator.collaborate("bar"); result = true;
|
||||
}};
|
||||
performer.perform(model);
|
||||
new Verifications() {{
|
||||
collaborator.receive(true);
|
||||
}};
|
||||
}
|
||||
|
||||
}
|
|
@ -1,11 +1,16 @@
|
|||
<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>org.baeldung</groupId>
|
||||
<artifactId>mock-comparisons</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
|
||||
<name>mockito</name>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>mocks</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>mock-comparisons</artifactId>
|
||||
<name>mock-comparisons</name>
|
||||
|
||||
<properties>
|
||||
<junit.version>4.12</junit.version>
|
|
@ -0,0 +1,20 @@
|
|||
<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>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>mocks</artifactId>
|
||||
<name>mocks</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>mock-comparisons</module>
|
||||
<module>jmockit</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
8
pom.xml
|
@ -7,8 +7,12 @@
|
|||
<name>parent-modules</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<modules>
|
||||
<module>apache-fop</module>
|
||||
<module>assertj</module>
|
||||
|
||||
<module>core-java</module>
|
||||
|
@ -25,7 +29,7 @@
|
|||
<module>jooq-spring</module>
|
||||
<module>json-path</module>
|
||||
<module>mockito</module>
|
||||
<module>mock-comparisons</module>
|
||||
<module>mocks</module>
|
||||
<module>jee7schedule</module>
|
||||
<!-- <module>jpa-storedprocedure</module> -->
|
||||
<module>querydsl</module>
|
||||
|
|
|
@ -14,30 +14,25 @@
|
|||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.6</java.version>
|
||||
<junit.version>4.10</junit.version>
|
||||
<spring.version>3.1.0.RELEASE</spring.version>
|
||||
<hibernate.version>4.3.11.Final</hibernate.version>
|
||||
<querydsl.version>2.5.0</querydsl.version>
|
||||
<slf4j.version>1.5.10</slf4j.version>
|
||||
<java.version>1.8</java.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
<spring.version>4.3.1.RELEASE</spring.version>
|
||||
<hibernate.version>5.2.1.Final</hibernate.version>
|
||||
<querydsl.version>4.1.3</querydsl.version>
|
||||
<slf4j.version>1.7.21</slf4j.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- QueryDSL -->
|
||||
<dependency>
|
||||
<groupId>com.mysema.querydsl</groupId>
|
||||
<artifactId>querydsl-core</artifactId>
|
||||
<version>${querydsl.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.mysema.querydsl</groupId>
|
||||
<groupId>com.querydsl</groupId>
|
||||
<artifactId>querydsl-jpa</artifactId>
|
||||
<version>${querydsl.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.mysema.querydsl</groupId>
|
||||
<groupId>com.querydsl</groupId>
|
||||
<artifactId>querydsl-apt</artifactId>
|
||||
<version>${querydsl.version}</version>
|
||||
<scope>provided</scope>
|
||||
|
@ -53,7 +48,7 @@
|
|||
|
||||
<dependency>
|
||||
<groupId>org.hibernate.javax.persistence</groupId>
|
||||
<artifactId>hibernate-jpa-2.0-api</artifactId>
|
||||
<artifactId>hibernate-jpa-2.1-api</artifactId>
|
||||
<version>1.0.0.Final</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
@ -69,7 +64,7 @@
|
|||
<dependency>
|
||||
<groupId>commons-pool</groupId>
|
||||
<artifactId>commons-pool</artifactId>
|
||||
<version>1.5.5</version>
|
||||
<version>1.6</version>
|
||||
<type>jar</type>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
@ -77,8 +72,8 @@
|
|||
<!-- HSQLDB Dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.hsqldb</groupId>
|
||||
<artifactId>hsqldb-j5</artifactId>
|
||||
<version>2.2.4</version>
|
||||
<artifactId>hsqldb</artifactId>
|
||||
<version>2.3.4</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Dependencies -->
|
||||
|
@ -118,9 +113,9 @@
|
|||
|
||||
<!-- Logging -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>${slf4j.version}</version>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.1.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
|
@ -128,17 +123,6 @@
|
|||
<version>${slf4j.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<version>${slf4j.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>1.2.16</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Test Dependencies -->
|
||||
<dependency>
|
||||
|
@ -157,7 +141,7 @@
|
|||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.3.2</version>
|
||||
<version>3.5.1</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
|
@ -168,16 +152,16 @@
|
|||
<!-- QueryDSL plugin -->
|
||||
<plugin>
|
||||
<groupId>com.mysema.maven</groupId>
|
||||
<artifactId>maven-apt-plugin</artifactId>
|
||||
<version>1.0.3</version>
|
||||
<artifactId>apt-maven-plugin</artifactId>
|
||||
<version>1.1.3</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>process</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>target/metamodel</outputDirectory>
|
||||
<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
|
||||
<outputDirectory>target/generated-sources/java</outputDirectory>
|
||||
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
|
|
|
@ -10,8 +10,8 @@ import org.baeldung.entity.Person;
|
|||
import org.baeldung.entity.QPerson;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.mysema.query.group.GroupBy;
|
||||
import com.mysema.query.jpa.impl.JPAQuery;
|
||||
import com.querydsl.core.group.GroupBy;
|
||||
import com.querydsl.jpa.impl.JPAQuery;
|
||||
|
||||
@Repository
|
||||
public class PersonDaoImpl implements PersonDao {
|
||||
|
@ -27,39 +27,39 @@ public class PersonDaoImpl implements PersonDao {
|
|||
|
||||
@Override
|
||||
public List<Person> findPersonsByFirstnameQueryDSL(final String firstname) {
|
||||
final JPAQuery query = new JPAQuery(em);
|
||||
final JPAQuery<Person> query = new JPAQuery<>(em);
|
||||
final QPerson person = QPerson.person;
|
||||
|
||||
return query.from(person).where(person.firstname.eq(firstname)).list(person);
|
||||
return query.from(person).where(person.firstname.eq(firstname)).fetch();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Person> findPersonsByFirstnameAndSurnameQueryDSL(final String firstname, final String surname) {
|
||||
final JPAQuery query = new JPAQuery(em);
|
||||
final JPAQuery<Person> query = new JPAQuery<>(em);
|
||||
final QPerson person = QPerson.person;
|
||||
|
||||
return query.from(person).where(person.firstname.eq(firstname).and(person.surname.eq(surname))).list(person);
|
||||
return query.from(person).where(person.firstname.eq(firstname).and(person.surname.eq(surname))).fetch();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Person> findPersonsByFirstnameInDescendingOrderQueryDSL(final String firstname) {
|
||||
final JPAQuery query = new JPAQuery(em);
|
||||
final JPAQuery<Person> query = new JPAQuery<>(em);
|
||||
final QPerson person = QPerson.person;
|
||||
|
||||
return query.from(person).where(person.firstname.eq(firstname)).orderBy(person.surname.desc()).list(person);
|
||||
return query.from(person).where(person.firstname.eq(firstname)).orderBy(person.surname.desc()).fetch();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int findMaxAge() {
|
||||
final JPAQuery query = new JPAQuery(em);
|
||||
final JPAQuery<Person> query = new JPAQuery<>(em);
|
||||
final QPerson person = QPerson.person;
|
||||
|
||||
return query.from(person).list(person.age.max()).get(0);
|
||||
return query.from(person).select(person.age.max()).fetchFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Integer> findMaxAgeByName() {
|
||||
final JPAQuery query = new JPAQuery(em);
|
||||
final JPAQuery<Person> query = new JPAQuery<>(em);
|
||||
final QPerson person = QPerson.person;
|
||||
|
||||
return query.from(person).transform(GroupBy.groupBy(person.firstname).as(GroupBy.max(person.age)));
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* (c) Центр ИТ, 2016. Все права защищены.
|
||||
*/
|
||||
package org.baeldung.querydsl.intro.entities;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class BlogPost {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private String body;
|
||||
|
||||
@ManyToOne
|
||||
private User user;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String code) {
|
||||
this.title = code;
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
return body;
|
||||
}
|
||||
|
||||
public void setBody(String body) {
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* (c) Центр ИТ, 2016. Все права защищены.
|
||||
*/
|
||||
package org.baeldung.querydsl.intro.entities;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String login;
|
||||
|
||||
private Boolean disabled;
|
||||
|
||||
@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "user")
|
||||
private Set<BlogPost> blogPosts = new HashSet<>(0);
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
|
||||
public void setLogin(String name) {
|
||||
this.login = name;
|
||||
}
|
||||
|
||||
public Set<BlogPost> getBlogPosts() {
|
||||
return blogPosts;
|
||||
}
|
||||
|
||||
public void setBlogPosts(Set<BlogPost> blogPosts) {
|
||||
this.blogPosts = blogPosts;
|
||||
}
|
||||
|
||||
public Boolean getDisabled() {
|
||||
return disabled;
|
||||
}
|
||||
|
||||
public void setDisabled(Boolean disabled) {
|
||||
this.disabled = disabled;
|
||||
}
|
||||
}
|
|
@ -16,4 +16,17 @@
|
|||
</properties>
|
||||
</persistence-unit>
|
||||
|
||||
<!-- PersistenceUnit for Intro to QueryDSL -->
|
||||
<persistence-unit name="org.baeldung.querydsl.intro">
|
||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||
<properties>
|
||||
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
|
||||
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:test"/>
|
||||
<property name="hibernate.connection.username" value="sa"/>
|
||||
<property name="hibernate.connection.password" value=""/>
|
||||
<property name="hibernate.hbm2ddl.auto" value="update"/>
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
|
||||
</persistence>
|
|
@ -1,12 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
|
||||
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
|
||||
<configuration>
|
||||
|
||||
<!-- Appenders -->
|
||||
<appender name="console" class="org.apache.log4j.ConsoleAppender">
|
||||
<param name="Target" value="System.out" />
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p: %c - %m%n" />
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<layout class="ch.qos.logback.classic.PatternLayout">
|
||||
<Pattern>%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p: %c - %m%n</Pattern>
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
|
@ -33,10 +30,8 @@
|
|||
<level value="warn" />
|
||||
</logger>
|
||||
|
||||
<!-- Root Logger -->
|
||||
<root>
|
||||
<priority value="warn" />
|
||||
<appender-ref ref="console" />
|
||||
<root level="warn">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
|
||||
</log4j:configuration>
|
||||
</configuration>
|
|
@ -0,0 +1,215 @@
|
|||
/*
|
||||
* (c) Центр ИТ, 2016. Все права защищены.
|
||||
*/
|
||||
package org.baeldung.querydsl.intro;
|
||||
|
||||
import java.util.List;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
import org.baeldung.querydsl.intro.entities.BlogPost;
|
||||
import org.baeldung.querydsl.intro.entities.QBlogPost;
|
||||
import org.baeldung.querydsl.intro.entities.QUser;
|
||||
import org.baeldung.querydsl.intro.entities.User;
|
||||
import com.querydsl.core.Tuple;
|
||||
import com.querydsl.core.types.dsl.Expressions;
|
||||
import com.querydsl.core.types.dsl.NumberPath;
|
||||
import com.querydsl.jpa.JPAExpressions;
|
||||
import com.querydsl.jpa.impl.JPAQueryFactory;
|
||||
import org.junit.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class QueryDSLTest {
|
||||
|
||||
private static EntityManagerFactory emf;
|
||||
|
||||
private EntityManager em;
|
||||
|
||||
private JPAQueryFactory queryFactory;
|
||||
|
||||
@BeforeClass
|
||||
public static void populateDatabase() {
|
||||
emf = Persistence.createEntityManagerFactory("org.baeldung.querydsl.intro");
|
||||
EntityManager em = emf.createEntityManager();
|
||||
|
||||
em.getTransaction().begin();
|
||||
User user1 = new User();
|
||||
user1.setLogin("David");
|
||||
em.persist(user1);
|
||||
|
||||
User user2 = new User();
|
||||
user2.setLogin("Ash");
|
||||
em.persist(user2);
|
||||
|
||||
User user3 = new User();
|
||||
user3.setLogin("Call");
|
||||
em.persist(user3);
|
||||
|
||||
User user4 = new User();
|
||||
user4.setLogin("Bishop");
|
||||
em.persist(user4);
|
||||
|
||||
BlogPost blogPost1 = new BlogPost();
|
||||
blogPost1.setTitle("Hello World!");
|
||||
blogPost1.setUser(user1);
|
||||
em.persist(blogPost1);
|
||||
|
||||
BlogPost blogPost2 = new BlogPost();
|
||||
blogPost2.setTitle("My Second Post");
|
||||
blogPost2.setUser(user1);
|
||||
em.persist(blogPost2);
|
||||
|
||||
BlogPost blogPost3 = new BlogPost();
|
||||
blogPost3.setTitle("Hello World!");
|
||||
blogPost3.setUser(user3);
|
||||
em.persist(blogPost3);
|
||||
|
||||
em.getTransaction().commit();
|
||||
|
||||
em.close();
|
||||
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
queryFactory = new JPAQueryFactory(em);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindByLogin_thenShouldReturnUser() {
|
||||
|
||||
QUser user = QUser.user;
|
||||
User aUser = queryFactory.selectFrom(user)
|
||||
.where(user.login.eq("David"))
|
||||
.fetchOne();
|
||||
|
||||
assertNotNull(aUser);
|
||||
assertEquals(aUser.getLogin(), "David");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingOrderBy_thenResultsShouldBeOrdered() {
|
||||
|
||||
QUser user = QUser.user;
|
||||
List<User> users = queryFactory.selectFrom(user)
|
||||
.orderBy(user.login.asc())
|
||||
.fetch();
|
||||
|
||||
assertEquals(users.size(), 4);
|
||||
assertEquals(users.get(0).getLogin(), "Ash");
|
||||
assertEquals(users.get(1).getLogin(), "Bishop");
|
||||
assertEquals(users.get(2).getLogin(), "Call");
|
||||
assertEquals(users.get(3).getLogin(), "David");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGroupingByTitle_thenReturnsTuples() {
|
||||
|
||||
QBlogPost blogPost = QBlogPost.blogPost;
|
||||
|
||||
NumberPath<Long> count = Expressions.numberPath(Long.class, "c");
|
||||
|
||||
List<Tuple> userTitleCounts = queryFactory.select(blogPost.title, blogPost.id.count().as(count))
|
||||
.from(blogPost)
|
||||
.groupBy(blogPost.title)
|
||||
.orderBy(count.desc())
|
||||
.fetch();
|
||||
|
||||
assertEquals("Hello World!", userTitleCounts.get(0).get(blogPost.title));
|
||||
assertEquals(new Long(2), userTitleCounts.get(0).get(count));
|
||||
|
||||
assertEquals("My Second Post", userTitleCounts.get(1).get(blogPost.title));
|
||||
assertEquals(new Long(1), userTitleCounts.get(1).get(count));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenJoiningWithCondition_thenResultCountShouldMatch() {
|
||||
|
||||
QUser user = QUser.user;
|
||||
QBlogPost blogPost = QBlogPost.blogPost;
|
||||
|
||||
List<User> users = queryFactory.selectFrom(user)
|
||||
.innerJoin(user.blogPosts, blogPost)
|
||||
.on(blogPost.title.eq("Hello World!"))
|
||||
.fetch();
|
||||
|
||||
assertEquals(2, users.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRefiningWithSubquery_thenResultCountShouldMatch() {
|
||||
|
||||
QUser user = QUser.user;
|
||||
QBlogPost blogPost = QBlogPost.blogPost;
|
||||
|
||||
List<User> users = queryFactory.selectFrom(user)
|
||||
.where(user.id.in(
|
||||
JPAExpressions.select(blogPost.user.id)
|
||||
.from(blogPost)
|
||||
.where(blogPost.title.eq("Hello World!"))))
|
||||
.fetch();
|
||||
|
||||
assertEquals(2, users.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpdating_thenTheRecordShouldChange() {
|
||||
|
||||
QUser user = QUser.user;
|
||||
|
||||
queryFactory.update(user)
|
||||
.where(user.login.eq("Ash"))
|
||||
.set(user.login, "Ash2")
|
||||
.set(user.disabled, true)
|
||||
.execute();
|
||||
|
||||
em.getTransaction().commit();
|
||||
|
||||
em.getTransaction().begin();
|
||||
|
||||
assertEquals(Boolean.TRUE,
|
||||
queryFactory.select(user.disabled)
|
||||
.from(user)
|
||||
.where(user.login.eq("Ash2"))
|
||||
.fetchOne());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeleting_thenTheRecordShouldBeAbsent() {
|
||||
|
||||
QUser user = QUser.user;
|
||||
|
||||
queryFactory.delete(user)
|
||||
.where(user.login.eq("Bishop"))
|
||||
.execute();
|
||||
|
||||
em.getTransaction().commit();
|
||||
|
||||
em.getTransaction().begin();
|
||||
|
||||
assertNull(queryFactory.selectFrom(user)
|
||||
.where(user.login.eq("Bishop"))
|
||||
.fetchOne());
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() {
|
||||
emf.close();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
## Spring Data Neo4j
|
||||
|
||||
### Relevant Articles:
|
||||
- [Introduction to Spring Data Neo4j](http://www.baeldung.com/spring-data-neo4j-tutorial)
|
||||
|
||||
### Build the Project with Tests Running
|
||||
```
|
||||
mvn clean install
|
||||
```
|
||||
|
||||
### Run Tests Directly
|
||||
```
|
||||
mvn test
|
||||
```
|
||||
|
|
@ -0,0 +1,93 @@
|
|||
<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>spring-data-neo4j</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<neo4j.version>3.0.1</neo4j.version>
|
||||
<spring-data-neo4j.version>4.1.1.RELEASE</spring-data-neo4j.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-neo4j</artifactId>
|
||||
<version>${spring-data-neo4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.voodoodyne.jackson.jsog</groupId>
|
||||
<artifactId>jackson-jsog</artifactId>
|
||||
<version>1.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-neo4j</artifactId>
|
||||
<version>${spring-data-neo4j.version}</version>
|
||||
<type>test-jar</type>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.neo4j</groupId>
|
||||
<artifactId>neo4j-kernel</artifactId>
|
||||
<version>${neo4j.version}</version>
|
||||
<type>test-jar</type>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.neo4j.app</groupId>
|
||||
<artifactId>neo4j-server</artifactId>
|
||||
<version>${neo4j.version}</version>
|
||||
<type>test-jar</type>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.neo4j</groupId>
|
||||
<artifactId>neo4j-ogm-test</artifactId>
|
||||
<version>2.0.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.neo4j.test</groupId>
|
||||
<artifactId>neo4j-harness</artifactId>
|
||||
<version>${neo4j.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>4.2.3.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.spring.data.neo4j.config;
|
||||
|
||||
import org.neo4j.ogm.session.SessionFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.neo4j.config.Neo4jConfiguration;
|
||||
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
|
||||
@ComponentScan(basePackages = {"com.baeldung.spring.data.neo4j.services"})
|
||||
@Configuration
|
||||
@EnableNeo4jRepositories(basePackages = "com.baeldung.spring.data.neo4j.repostory")
|
||||
public class MovieDatabaseNeo4jConfiguration extends Neo4jConfiguration {
|
||||
|
||||
public static final String URL = System.getenv("NEO4J_URL") != null ? System.getenv("NEO4J_URL") : "http://neo4j:movies@localhost:7474";
|
||||
|
||||
@Bean
|
||||
public org.neo4j.ogm.config.Configuration getConfiguration() {
|
||||
org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration();
|
||||
config
|
||||
.driverConfiguration()
|
||||
.setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver")
|
||||
.setURI(URL);
|
||||
return config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SessionFactory getSessionFactory() {
|
||||
return new SessionFactory(getConfiguration(), "com.baeldung.spring.data.neo4j.domain");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.spring.data.neo4j.config;
|
||||
|
||||
import org.neo4j.ogm.session.SessionFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.data.neo4j.config.Neo4jConfiguration;
|
||||
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
|
||||
import org.springframework.data.neo4j.server.Neo4jServer;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
|
||||
@EnableTransactionManagement
|
||||
@ComponentScan(basePackages = {"com.baeldung.spring.data.neo4j.services"})
|
||||
@Configuration
|
||||
@EnableNeo4jRepositories(basePackages = "com.baeldung.spring.data.neo4j.repostory")
|
||||
@Profile({"embedded", "test"})
|
||||
public class MovieDatabaseNeo4jTestConfiguration extends Neo4jConfiguration {
|
||||
|
||||
@Bean
|
||||
public org.neo4j.ogm.config.Configuration getConfiguration() {
|
||||
org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration();
|
||||
config
|
||||
.driverConfiguration()
|
||||
.setDriverClassName("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver");
|
||||
return config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SessionFactory getSessionFactory() {
|
||||
return new SessionFactory(getConfiguration(), "com.baeldung.spring.data.neo4j.domain");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package com.baeldung.spring.data.neo4j.domain;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
|
||||
import com.voodoodyne.jackson.jsog.JSOGGenerator;
|
||||
import org.neo4j.ogm.annotation.GraphId;
|
||||
import org.neo4j.ogm.annotation.NodeEntity;
|
||||
import org.neo4j.ogm.annotation.Relationship;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@JsonIdentityInfo(generator=JSOGGenerator.class)
|
||||
|
||||
@NodeEntity
|
||||
public class Movie {
|
||||
@GraphId
|
||||
Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private int released;
|
||||
private String tagline;
|
||||
|
||||
@Relationship(type="ACTED_IN", direction = Relationship.INCOMING) private List<Role> roles;
|
||||
|
||||
public Movie() { }
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public int getReleased() {
|
||||
return released;
|
||||
}
|
||||
|
||||
public String getTagline() {
|
||||
return tagline;
|
||||
}
|
||||
|
||||
public Collection<Role> getRoles() {
|
||||
return roles;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public void setReleased(int released) {
|
||||
this.released = released;
|
||||
}
|
||||
|
||||
public void setTagline(String tagline) {
|
||||
this.tagline = tagline;
|
||||
}
|
||||
|
||||
public void setRoles(List<Role> roles) {
|
||||
this.roles = roles;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.spring.data.neo4j.domain;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
|
||||
import com.voodoodyne.jackson.jsog.JSOGGenerator;
|
||||
import org.neo4j.ogm.annotation.GraphId;
|
||||
import org.neo4j.ogm.annotation.NodeEntity;
|
||||
import org.neo4j.ogm.annotation.Relationship;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@JsonIdentityInfo(generator=JSOGGenerator.class)
|
||||
@NodeEntity
|
||||
public class Person {
|
||||
@GraphId
|
||||
Long id;
|
||||
|
||||
private String name;
|
||||
private int born;
|
||||
|
||||
@Relationship(type = "ACTED_IN")
|
||||
private List<Movie> movies;
|
||||
|
||||
public Person() { }
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getBorn() {
|
||||
return born;
|
||||
}
|
||||
|
||||
public List<Movie> getMovies() {
|
||||
return movies;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setBorn(int born) {
|
||||
this.born = born;
|
||||
}
|
||||
|
||||
public void setMovies(List<Movie> movies) {
|
||||
this.movies = movies;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.spring.data.neo4j.domain;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
|
||||
import com.voodoodyne.jackson.jsog.JSOGGenerator;
|
||||
import org.neo4j.ogm.annotation.EndNode;
|
||||
import org.neo4j.ogm.annotation.GraphId;
|
||||
import org.neo4j.ogm.annotation.RelationshipEntity;
|
||||
import org.neo4j.ogm.annotation.StartNode;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@JsonIdentityInfo(generator=JSOGGenerator.class)
|
||||
@RelationshipEntity(type = "ACTED_IN")
|
||||
public class Role {
|
||||
@GraphId
|
||||
Long id;
|
||||
private Collection<String> roles;
|
||||
@StartNode
|
||||
private Person person;
|
||||
@EndNode
|
||||
private Movie movie;
|
||||
|
||||
public Role() {
|
||||
}
|
||||
|
||||
public Collection<String> getRoles() {
|
||||
return roles;
|
||||
}
|
||||
|
||||
public Person getPerson() {
|
||||
return person;
|
||||
}
|
||||
|
||||
public Movie getMovie() {
|
||||
return movie;
|
||||
}
|
||||
|
||||
public void setRoles(Collection<String> roles) {
|
||||
this.roles = roles;
|
||||
}
|
||||
|
||||
public void setPerson(Person person) {
|
||||
this.person = person;
|
||||
}
|
||||
|
||||
public void setMovie(Movie movie) {
|
||||
this.movie = movie;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.spring.data.neo4j.repostory;
|
||||
|
||||
import com.baeldung.spring.data.neo4j.domain.Movie;
|
||||
import org.springframework.data.neo4j.annotation.Query;
|
||||
import org.springframework.data.neo4j.repository.GraphRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Repository
|
||||
public interface MovieRepository extends GraphRepository<Movie> {
|
||||
Movie findByTitle(@Param("title") String title);
|
||||
|
||||
@Query("MATCH (m:Movie) WHERE m.title =~ ('(?i).*'+{title}+'.*') RETURN m")
|
||||
Collection<Movie> findByTitleContaining(@Param("title") String title);
|
||||
|
||||
@Query("MATCH (m:Movie)<-[:ACTED_IN]-(a:Person) RETURN m.title as movie, collect(a.name) as cast LIMIT {limit}")
|
||||
List<Map<String,Object>> graph(@Param("limit") int limit);
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.spring.data.neo4j.repostory;
|
||||
|
||||
import com.baeldung.spring.data.neo4j.domain.Person;
|
||||
import org.springframework.data.neo4j.repository.GraphRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
|
||||
@Repository
|
||||
public interface PersonRepository extends GraphRepository<Person> {
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.spring.data.neo4j.services;
|
||||
|
||||
import com.baeldung.spring.data.neo4j.repostory.MovieRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class MovieService {
|
||||
|
||||
@Autowired
|
||||
MovieRepository movieRepository;
|
||||
|
||||
private Map<String, Object> toD3Format(Iterator<Map<String, Object>> result) {
|
||||
List<Map<String,Object>> nodes = new ArrayList<Map<String,Object>>();
|
||||
List<Map<String,Object>> rels= new ArrayList<Map<String,Object>>();
|
||||
int i=0;
|
||||
while (result.hasNext()) {
|
||||
Map<String, Object> row = result.next();
|
||||
nodes.add(map("title",row.get("movie"),"label","movie"));
|
||||
int target=i;
|
||||
i++;
|
||||
for (Object name : (Collection) row.get("cast")) {
|
||||
Map<String, Object> actor = map("title", name,"label","actor");
|
||||
int source = nodes.indexOf(actor);
|
||||
if (source == -1) {
|
||||
nodes.add(actor);
|
||||
source = i++;
|
||||
}
|
||||
rels.add(map("source",source,"target",target));
|
||||
}
|
||||
}
|
||||
return map("nodes", nodes, "links", rels);
|
||||
}
|
||||
|
||||
private Map<String, Object> map(String key1, Object value1, String key2, Object value2) {
|
||||
Map<String, Object> result = new HashMap<String,Object>(2);
|
||||
result.put(key1,value1);
|
||||
result.put(key2,value2);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Map<String, Object> graph(int limit) {
|
||||
Iterator<Map<String, Object>> result = movieRepository.graph(limit).iterator();
|
||||
return toD3Format(result);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
<configuration>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>web - %date [%thread] %-5level %logger{36} - %message%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>
|
After Width: | Height: | Size: 855 B |