diff --git a/README.md b/README.md index da46989455..f0d3d29da7 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ The "REST with Spring" Classes ============================== -After 5 months of work, here's the Master Class:
+After 5 months of work, here's the Master Class of REST With Spring:
**[>> THE REST WITH SPRING MASTER CLASS](http://www.baeldung.com/rest-with-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=rws#master-class)** @@ -19,3 +19,8 @@ Any IDE can be used to work with the projects, but if you're using Eclipse, cons - import the included **formatter** in Eclipse: `https://github.com/eugenp/tutorials/tree/master/eclipse` + + +CI - Jenkins +================================ +This tutorials project is being built **[>> HERE](https://rest-security.ci.cloudbees.com/job/tutorials/)** diff --git a/assertj/pom.xml b/assertj/pom.xml new file mode 100644 index 0000000000..ce97278a97 --- /dev/null +++ b/assertj/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + + com.baeldung + assertj + 1.0.0-SNAPSHOT + + + + junit + junit + 4.12 + + + org.assertj + assertj-core + 3.4.1 + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + + + + + \ No newline at end of file diff --git a/assertj/src/main/java/com/baeldung/assertj/introduction/domain/Dog.java b/assertj/src/main/java/com/baeldung/assertj/introduction/domain/Dog.java new file mode 100644 index 0000000000..623f71214c --- /dev/null +++ b/assertj/src/main/java/com/baeldung/assertj/introduction/domain/Dog.java @@ -0,0 +1,19 @@ +package com.baeldung.assertj.introduction.domain; + +public class Dog { + private String name; + private Float weight; + + public Dog(String name, Float weight) { + this.name = name; + this.weight = weight; + } + + public String getName() { + return name; + } + + public Float getWeight() { + return weight; + } +} diff --git a/assertj/src/main/java/com/baeldung/assertj/introduction/domain/Person.java b/assertj/src/main/java/com/baeldung/assertj/introduction/domain/Person.java new file mode 100644 index 0000000000..90ef787ebe --- /dev/null +++ b/assertj/src/main/java/com/baeldung/assertj/introduction/domain/Person.java @@ -0,0 +1,19 @@ +package com.baeldung.assertj.introduction.domain; + +public class Person { + private String name; + private Integer age; + + public Person(String name, Integer age) { + this.name = name; + this.age = age; + } + + public String getName() { + return name; + } + + public Integer getAge() { + return age; + } +} diff --git a/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJCoreTest.java b/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJCoreTest.java new file mode 100644 index 0000000000..21bc40ae9f --- /dev/null +++ b/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJCoreTest.java @@ -0,0 +1,143 @@ +package com.baeldung.assertj.introduction; + +import com.baeldung.assertj.introduction.domain.Dog; +import com.baeldung.assertj.introduction.domain.Person; +import org.assertj.core.util.Maps; +import org.junit.Ignore; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.InputStream; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; +import static org.assertj.core.api.Assertions.withPrecision; + +public class AssertJCoreTest { + + @Test + public void whenComparingReferences_thenNotEqual() throws Exception { + Dog fido = new Dog("Fido", 5.15f); + Dog fidosClone = new Dog("Fido", 5.15f); + + assertThat(fido).isNotEqualTo(fidosClone); + } + + @Test + public void whenComparingFields_thenEqual() throws Exception { + Dog fido = new Dog("Fido", 5.15f); + Dog fidosClone = new Dog("Fido", 5.15f); + + assertThat(fido).isEqualToComparingFieldByFieldRecursively(fidosClone); + } + + @Test + public void whenCheckingForElement_thenContains() throws Exception { + List list = Arrays.asList("1", "2", "3"); + + assertThat(list) + .contains("1"); + } + + @Test + public void whenCheckingForElement_thenMultipleAssertions() throws Exception { + List list = Arrays.asList("1", "2", "3"); + + assertThat(list).isNotEmpty(); + assertThat(list).startsWith("1"); + assertThat(list).doesNotContainNull(); + + assertThat(list) + .isNotEmpty() + .contains("1") + .startsWith("1") + .doesNotContainNull() + .containsSequence("2", "3"); + } + + @Test + public void whenCheckingRunnable_thenIsInterface() throws Exception { + assertThat(Runnable.class).isInterface(); + } + + @Test + public void whenCheckingCharacter_thenIsUnicode() throws Exception { + char someCharacter = 'c'; + + assertThat(someCharacter) + .isNotEqualTo('a') + .inUnicode() + .isGreaterThanOrEqualTo('b') + .isLowerCase(); + } + + @Test + public void whenAssigningNSEExToException_thenIsAssignable() throws Exception { + assertThat(Exception.class).isAssignableFrom(NoSuchElementException.class); + } + + @Test + public void whenComparingWithOffset_thenEquals() throws Exception { + assertThat(5.1).isEqualTo(5, withPrecision(1d)); + } + + @Test + public void whenCheckingString_then() throws Exception { + assertThat("".isEmpty()).isTrue(); + } + + @Test + public void whenCheckingFile_then() throws Exception { + final File someFile = File.createTempFile("aaa", "bbb"); + someFile.deleteOnExit(); + + assertThat(someFile) + .exists() + .isFile() + .canRead() + .canWrite(); + } + + @Test + public void whenCheckingIS_then() throws Exception { + InputStream given = new ByteArrayInputStream("foo".getBytes()); + InputStream expected = new ByteArrayInputStream("foo".getBytes()); + + assertThat(given).hasSameContentAs(expected); + } + + @Test + public void whenGivenMap_then() throws Exception { + Map map = Maps.newHashMap(2, "a"); + + assertThat(map) + .isNotEmpty() + .containsKey(2) + .doesNotContainKeys(10) + .contains(entry(2, "a")); + } + + @Test + public void whenGivenException_then() throws Exception { + Exception ex = new Exception("abc"); + + assertThat(ex) + .hasNoCause() + .hasMessageEndingWith("c"); + } + + @Ignore // IN ORDER TO TEST, REMOVE THIS LINE + @Test + public void whenRunningAssertion_thenDescribed() throws Exception { + Person person = new Person("Alex", 34); + + assertThat(person.getAge()) + .as("%s's age should be equal to 100") + .isEqualTo(100); + } +} diff --git a/core-java-8/pom.xml b/core-java-8/pom.xml index f99d85f564..8c9bb36f7d 100644 --- a/core-java-8/pom.xml +++ b/core-java-8/pom.xml @@ -63,6 +63,12 @@ test + + org.assertj + assertj-core + 3.4.1 + + org.mockito mockito-core diff --git a/core-java-8/src/test/java/com/baeldung/dateapi/ConversionExample.java b/core-java-8/src/test/java/com/baeldung/dateapi/ConversionExample.java new file mode 100644 index 0000000000..a543c80eaf --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/dateapi/ConversionExample.java @@ -0,0 +1,19 @@ +package com.baeldung.dateapi; + +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.TimeZone; + +public class ConversionExample { + public static void main(String[] args) { + Instant instantFromCalendar = GregorianCalendar.getInstance().toInstant(); + ZonedDateTime zonedDateTimeFromCalendar = new GregorianCalendar().toZonedDateTime(); + Date dateFromInstant = Date.from(Instant.now()); + GregorianCalendar calendarFromZonedDateTime = GregorianCalendar.from(ZonedDateTime.now()); + Instant instantFromDate = new Date().toInstant(); + ZoneId zoneIdFromTimeZone = TimeZone.getTimeZone("PST").toZoneId(); + } +} diff --git a/core-java-8/src/test/java/com/baeldung/dateapi/JavaUtilTimeTest.java b/core-java-8/src/test/java/com/baeldung/dateapi/JavaUtilTimeTest.java new file mode 100644 index 0000000000..4bce40c2d9 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/dateapi/JavaUtilTimeTest.java @@ -0,0 +1,89 @@ +package com.baeldung.dateapi; + +import org.junit.Test; + +import java.text.ParseException; +import java.time.Duration; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.Month; +import java.time.YearMonth; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; + +import static org.assertj.core.api.Assertions.assertThat; + +public class JavaUtilTimeTest { + + @Test + public void currentTime() { + final LocalDate now = LocalDate.now(); + + System.out.println(now); + // there is not much to test here + } + + @Test + public void specificTime() { + LocalDate birthDay = LocalDate.of(1990, Month.DECEMBER, 15); + + System.out.println(birthDay); + // there is not much to test here + } + + @Test + public void extractMonth() { + Month month = LocalDate.of(1990, Month.DECEMBER, 15).getMonth(); + + assertThat(month).isEqualTo(Month.DECEMBER); + } + + @Test + public void subtractTime() { + LocalDateTime fiveHoursBefore = LocalDateTime.of(1990, Month.DECEMBER, 15, 15, 0).minusHours(5); + + assertThat(fiveHoursBefore.getHour()).isEqualTo(10); + } + + @Test + public void alterField() { + LocalDateTime inJune = LocalDateTime.of(1990, Month.DECEMBER, 15, 15, 0).with(Month.JUNE); + + assertThat(inJune.getMonth()).isEqualTo(Month.JUNE); + } + + @Test + public void truncate() { + LocalTime truncated = LocalTime.of(15, 12, 34).truncatedTo(ChronoUnit.HOURS); + + assertThat(truncated).isEqualTo(LocalTime.of(15, 0, 0)); + } + + @Test + public void getTimeSpan() { + LocalDateTime now = LocalDateTime.now(); + LocalDateTime hourLater = now.plusHours(1); + Duration span = Duration.between(now, hourLater); + + assertThat(span).isEqualTo(Duration.ofHours(1)); + } + + @Test + public void formatAndParse() throws ParseException { + LocalDate someDate = LocalDate.of(2016, 12, 7); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + String formattedDate = someDate.format(formatter); + LocalDate parsedDate = LocalDate.parse(formattedDate, formatter); + + assertThat(formattedDate).isEqualTo("2016-12-07"); + assertThat(parsedDate).isEqualTo(someDate); + } + + @Test + public void daysInMonth() { + int daysInMonth = YearMonth.of(1990, 2).lengthOfMonth(); + + assertThat(daysInMonth).isEqualTo(28); + } +} diff --git a/core-java/.classpath b/core-java/.classpath index f9b079e8c9..ca829f1262 100644 --- a/core-java/.classpath +++ b/core-java/.classpath @@ -27,7 +27,7 @@ - + diff --git a/core-java/.settings/org.eclipse.jdt.core.prefs b/core-java/.settings/org.eclipse.jdt.core.prefs index 046168cf24..1882edb712 100644 --- a/core-java/.settings/org.eclipse.jdt.core.prefs +++ b/core-java/.settings/org.eclipse.jdt.core.prefs @@ -6,8 +6,13 @@ org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annota org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 -org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.autoboxing=ignore @@ -92,4 +97,4 @@ org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning org.eclipse.jdt.core.compiler.problem.unusedTypeParameter=ignore org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning -org.eclipse.jdt.core.compiler.source=1.7 +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/core-java/.settings/org.eclipse.wst.common.project.facet.core.xml b/core-java/.settings/org.eclipse.wst.common.project.facet.core.xml index bc0009a455..f4ef8aa0a5 100644 --- a/core-java/.settings/org.eclipse.wst.common.project.facet.core.xml +++ b/core-java/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -1,4 +1,4 @@ - + diff --git a/core-java/src/test/java/org/baeldung/java/collections/JavaCollectionConversionUnitTest.java b/core-java/src/test/java/org/baeldung/java/collections/JavaCollectionConversionUnitTest.java index 95b79810cd..a5f684a141 100644 --- a/core-java/src/test/java/org/baeldung/java/collections/JavaCollectionConversionUnitTest.java +++ b/core-java/src/test/java/org/baeldung/java/collections/JavaCollectionConversionUnitTest.java @@ -31,7 +31,7 @@ public class JavaCollectionConversionUnitTest { @Test public final void givenUsingCoreJava_whenListConvertedToArray_thenCorrect() { - final List sourceList = Lists. newArrayList(0, 1, 2, 3, 4, 5); + final List sourceList = Arrays.asList(0, 1, 2, 3, 4, 5); final Integer[] targetArray = sourceList.toArray(new Integer[sourceList.size()]); } diff --git a/dependency-injection/docs/autowired-name-demo-classdiagram.png b/dependency-injection/docs/autowired-name-demo-classdiagram.png deleted file mode 100644 index f367fdbf41..0000000000 Binary files a/dependency-injection/docs/autowired-name-demo-classdiagram.png and /dev/null differ diff --git a/dependency-injection/docs/autowired-type-demo-classdiagram.png b/dependency-injection/docs/autowired-type-demo-classdiagram.png deleted file mode 100644 index 5f3f341556..0000000000 Binary files a/dependency-injection/docs/autowired-type-demo-classdiagram.png and /dev/null differ diff --git a/dependency-injection/docs/autowired-type-demo-classdiagram.xml b/dependency-injection/docs/autowired-type-demo-classdiagram.xml deleted file mode 100644 index 71dc839457..0000000000 --- a/dependency-injection/docs/autowired-type-demo-classdiagram.xml +++ /dev/null @@ -1 +0,0 @@ -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== \ No newline at end of file diff --git a/dependency-injection/docs/inject-demo-classdiagram.png b/dependency-injection/docs/inject-demo-classdiagram.png deleted file mode 100644 index 9996fdc733..0000000000 Binary files a/dependency-injection/docs/inject-demo-classdiagram.png and /dev/null differ diff --git a/dependency-injection/docs/inject-field-demo-classdiagram.png b/dependency-injection/docs/inject-field-demo-classdiagram.png deleted file mode 100644 index e63b6e5f42..0000000000 Binary files a/dependency-injection/docs/inject-field-demo-classdiagram.png and /dev/null differ diff --git a/dependency-injection/docs/inject-field-demo-classdiagram.xml b/dependency-injection/docs/inject-field-demo-classdiagram.xml deleted file mode 100644 index 147c545a2d..0000000000 --- a/dependency-injection/docs/inject-field-demo-classdiagram.xml +++ /dev/null @@ -1 +0,0 @@ -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= \ No newline at end of file diff --git a/dependency-injection/docs/inject-name-demo-classdiagram.png b/dependency-injection/docs/inject-name-demo-classdiagram.png deleted file mode 100644 index 96a6a3425e..0000000000 Binary files a/dependency-injection/docs/inject-name-demo-classdiagram.png and /dev/null differ diff --git a/dependency-injection/docs/inject-name-demo-classdiagram.xml b/dependency-injection/docs/inject-name-demo-classdiagram.xml deleted file mode 100644 index 5a8bb9f8c4..0000000000 --- a/dependency-injection/docs/inject-name-demo-classdiagram.xml +++ /dev/null @@ -1 +0,0 @@ -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= \ No newline at end of file diff --git a/dependency-injection/docs/inject-qualifier-demo-classdiagram.png b/dependency-injection/docs/inject-qualifier-demo-classdiagram.png index 1ffe6453cb..7366999ec1 100644 Binary files a/dependency-injection/docs/inject-qualifier-demo-classdiagram.png and b/dependency-injection/docs/inject-qualifier-demo-classdiagram.png differ diff --git a/dependency-injection/docs/inject-qualifier-demo-classdiagram.xml b/dependency-injection/docs/inject-qualifier-demo-classdiagram.xml index ec122afe71..704801bdf2 100644 --- a/dependency-injection/docs/inject-qualifier-demo-classdiagram.xml +++ b/dependency-injection/docs/inject-qualifier-demo-classdiagram.xml @@ -1 +1 @@ -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= \ No newline at end of file +7VptbyI3EP41SO2Hi1iWl+RjAuSuUpqml6jX+xQZ1uz6zrumXhPC/fqb8Y7ZV0IaXqpGICTWw3hsz/N4Zmxo+cP4+aNm8+h3FXDZ6rSD55Y/anU6XrvvwwdKVpmk3yZBqEVASrngXvzgridJFyLgaUnRKCWNmJeFU5UkfGpKMqa1WpbVZkqWR52z0I2YC+6nTNalX0Rgokx63unn8k9chJEb2etfZN9M2PR7qNUiofFaHX9mX9nXMXO2aKFpxAK1LIj8MfhVKwWW8Sl+HnKJvnVuy/pdb/h2PW/NE5rblg5uHmbl1s4DcAU1lTaRClXC5DiXXtn1cbTQhlZkYgmPHjzCoHr1N8lt4ys2znq2GVwiMmh1zpOHSCSZ9FrAjLIu37gxK2IDWxgFonwGN0rNaZz6KmnhqVroKa0D2pY3TIectAaZCFdY6Eae+chVzGHKoKC5ZEY8lcnAiFPhWi/3KzyQa5vdTHN5YnJBRq8Fl8GfCybFTHD9W/INaDzisarBkS5FLFmCfi94eqYS40DCNtgJwZ+jKXiEaxA8cW0EEPqSvjDovKtpJGRww1ZqgR5JDbDVta4ipcUPMMvcGPC1NoQGML+ocY89CTTNU9C5c2Bgz0x0w1IUoM5UScnmqZisJxwDLCK5UsaomJTcSpEQQyUVLAMc4PbPC6jjWrmLPc2IUhwaEAOW+abuuq0YFTd0h+LVLqCTiQLoH1qdPosRiGSS4ge0Jfo+nbMEnkN8Bj7IYMRhjwQ8ma5gQZkODFdUa/mXYPxST4TRTK/yDk4/EE9O9y3jwtQ3j1u29sIkoHNhHjVyA2yWejmxU6PVd+7gT5Ql/qzACBI5wks+QwvNdId5T0US3lidUTeXfCaoUbSMhOH3IMc5LSGfYdQBezNpg1UkAliRjXqGGZYxGOk6VyIxlh69K3gDi4YY6XqwriG0ATfXhjeqazNUCayPQT/oxmF7LDlukQZmu4Dxama71LSN2d3u7sQmEwVi14CVohqxMmBdNrUx7F+jGgMWNgMRjA82I3yw5ktQ+3WoUVSFULIJl3cqFUYotK8z3Qq0R0BvQEhtQ+98d/B6DeA1xoMQEmByCVl4KTTPU9UfiU1djw+fxrePo/Hd+HY0vh1+9R7/AoVgU9BosP/Lry9GtieVWyuFkD1MtvOmyQJjqhPb0PMU9Y4U9Qa0IbZGvT2kc48i7P+rVObPwthhzroX1MSBvLP2gJp3XAtwBlaN1vKrimuKIsXi2iP09lddU9c7pByoOEacE8Au8/nUdiayeVKvHF9wKcPhnRoxeeM4nXZlnHY21fVRaot+t505KadXNoOcbGufvIp/RL9C4G6su07HhwMcH+jbfq8MsTs9F8KP128IPxcVhr4l/NDJpXyaaNsKJstL90ZDjD8lm9cmG7efdimxG9F2erugTXntVGLvDb0eeXAbensosekusFpiwxVmtkehmDzt2GNg7vA9xo71qCTad3l4NvBwX61LRKjc/HMnqNZuhcpxIhXm2EPesHruar5QBRL3j3/F6iZTrI8SZSKuT2XSscskVxfRNvT79cy5rpZL165OuBMR6pftp0ppp7i73lq7lEqNgO8l8Nbv2U+10o74NRRLjfjtoVjymm6TT9XSf7FrG8qlw+3a+nn2M/9nAbe39kpmC8Q29W10JPoKixtMj5XM/RqsaRsjsHtIht3KdZVf31qNJek+MmH9FPlOfVy5l/HPj+jk+mnPlcrvxLveBR1tXkoOh3Gus/F+ndt3t9euciI6HcO59RPTO3Nu/Qq/KcMdyLv1Y8g7827PXWu7qDs4GHehmf8nLvvtJP/joT/+CQ== \ No newline at end of file diff --git a/dependency-injection/docs/resource-demo-classdiagram.png b/dependency-injection/docs/resource-demo-classdiagram.png deleted file mode 100644 index 1f0a41a19e..0000000000 Binary files a/dependency-injection/docs/resource-demo-classdiagram.png and /dev/null differ diff --git a/dependency-injection/docs/resource-field-demo-classdiagram.png b/dependency-injection/docs/resource-field-demo-classdiagram.png deleted file mode 100644 index 0d6207d09a..0000000000 Binary files a/dependency-injection/docs/resource-field-demo-classdiagram.png and /dev/null differ diff --git a/dependency-injection/docs/resource-field-demo-classdiagram.xml b/dependency-injection/docs/resource-field-demo-classdiagram.xml deleted file mode 100644 index 44c742f2bc..0000000000 --- a/dependency-injection/docs/resource-field-demo-classdiagram.xml +++ /dev/null @@ -1 +0,0 @@ -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 \ No newline at end of file diff --git a/dependency-injection/docs/resource-method-byname-demo-classdiagram.png b/dependency-injection/docs/resource-method-byname-demo-classdiagram.png deleted file mode 100644 index c5cd0c0fcb..0000000000 Binary files a/dependency-injection/docs/resource-method-byname-demo-classdiagram.png and /dev/null differ diff --git a/dependency-injection/docs/resource-method-demo-classdiagram.png b/dependency-injection/docs/resource-method-demo-classdiagram.png deleted file mode 100644 index 6be7dbbeea..0000000000 Binary files a/dependency-injection/docs/resource-method-demo-classdiagram.png and /dev/null differ diff --git a/dependency-injection/docs/resource-method-demo-classdiagram.xml b/dependency-injection/docs/resource-method-demo-classdiagram.xml deleted file mode 100644 index 94b39279fc..0000000000 --- a/dependency-injection/docs/resource-method-demo-classdiagram.xml +++ /dev/null @@ -1 +0,0 @@ -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= \ No newline at end of file diff --git a/dependency-injection/pom.xml b/dependency-injection/pom.xml index 46f57e512e..f87d061a53 100644 --- a/dependency-injection/pom.xml +++ b/dependency-injection/pom.xml @@ -65,7 +65,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xs maven-surefire-plugin - **/*Demo.java + **/*Test.java diff --git a/dependency-injection/src/test/java/com/baeldung/autowired/FieldAutowiredNameDemo.java b/dependency-injection/src/test/java/com/baeldung/autowired/FieldAutowiredNameTest.java similarity index 55% rename from dependency-injection/src/test/java/com/baeldung/autowired/FieldAutowiredNameDemo.java rename to dependency-injection/src/test/java/com/baeldung/autowired/FieldAutowiredNameTest.java index c11ed5286a..0455ca500c 100644 --- a/dependency-injection/src/test/java/com/baeldung/autowired/FieldAutowiredNameDemo.java +++ b/dependency-injection/src/test/java/com/baeldung/autowired/FieldAutowiredNameTest.java @@ -8,20 +8,24 @@ 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.configuration.ApplicationContextTestAutowiredName; import com.baeldung.dependency.ArbitraryDependency; @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()); + assertEquals("Arbitrary Dependency", + autowiredFieldDependency.toString()); } } diff --git a/dependency-injection/src/test/java/com/baeldung/autowired/FieldAutowiredDemo.java b/dependency-injection/src/test/java/com/baeldung/autowired/FieldAutowiredTest.java similarity index 63% rename from dependency-injection/src/test/java/com/baeldung/autowired/FieldAutowiredDemo.java rename to dependency-injection/src/test/java/com/baeldung/autowired/FieldAutowiredTest.java index c71365097f..811daebaf6 100644 --- a/dependency-injection/src/test/java/com/baeldung/autowired/FieldAutowiredDemo.java +++ b/dependency-injection/src/test/java/com/baeldung/autowired/FieldAutowiredTest.java @@ -8,19 +8,22 @@ 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.configuration.ApplicationContextTestAutowiredType; import com.baeldung.dependency.ArbitraryDependency; @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()); } diff --git a/dependency-injection/src/test/java/com/baeldung/autowired/FieldQualifierAutowiredDemo.java b/dependency-injection/src/test/java/com/baeldung/autowired/FieldQualifierAutowiredTest.java similarity index 62% rename from dependency-injection/src/test/java/com/baeldung/autowired/FieldQualifierAutowiredDemo.java rename to dependency-injection/src/test/java/com/baeldung/autowired/FieldQualifierAutowiredTest.java index 5afce6ab6a..d72d38e9a8 100644 --- a/dependency-injection/src/test/java/com/baeldung/autowired/FieldQualifierAutowiredDemo.java +++ b/dependency-injection/src/test/java/com/baeldung/autowired/FieldQualifierAutowiredTest.java @@ -9,13 +9,16 @@ 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.configuration.ApplicationContextTestAutowiredQualifier; import com.baeldung.dependency.ArbitraryDependency; @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,14 +29,15 @@ 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()); + assertEquals("Another Arbitrary Dependency", + fieldDependency2.toString()); } } diff --git a/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestAutowiredName.java b/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestAutowiredName.java new file mode 100644 index 0000000000..41dfdb09e2 --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestAutowiredName.java @@ -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 { +} diff --git a/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestAutowiredQualifier.java b/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestAutowiredQualifier.java new file mode 100644 index 0000000000..138fbc46c5 --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestAutowiredQualifier.java @@ -0,0 +1,25 @@ +package com.baeldung.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.dependency.AnotherArbitraryDependency; +import com.baeldung.dependency.ArbitraryDependency; + +@Configuration +public class ApplicationContextTestAutowiredQualifier { + + @Bean + public ArbitraryDependency autowiredFieldDependency() { + ArbitraryDependency autowiredFieldDependency = new ArbitraryDependency(); + + return autowiredFieldDependency; + } + + @Bean + public ArbitraryDependency anotherAutowiredFieldDependency() { + ArbitraryDependency anotherAutowiredFieldDependency = new AnotherArbitraryDependency(); + + return anotherAutowiredFieldDependency; + } +} diff --git a/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestAutowiredType.java b/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestAutowiredType.java new file mode 100644 index 0000000000..703a8214b9 --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestAutowiredType.java @@ -0,0 +1,16 @@ +package com.baeldung.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.dependency.ArbitraryDependency; + +@Configuration +public class ApplicationContextTestAutowiredType { + + @Bean + public ArbitraryDependency autowiredFieldDependency() { + ArbitraryDependency autowiredFieldDependency = new ArbitraryDependency(); + return autowiredFieldDependency; + } +} diff --git a/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestInjectName.java b/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestInjectName.java new file mode 100644 index 0000000000..7fe8306a39 --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestInjectName.java @@ -0,0 +1,17 @@ +package com.baeldung.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.dependency.ArbitraryDependency; +import com.baeldung.dependency.YetAnotherArbitraryDependency; + +@Configuration +public class ApplicationContextTestInjectName { + + @Bean + public ArbitraryDependency yetAnotherFieldInjectDependency() { + ArbitraryDependency yetAnotherFieldInjectDependency = new YetAnotherArbitraryDependency(); + return yetAnotherFieldInjectDependency; + } +} diff --git a/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestInjectQualifier.java b/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestInjectQualifier.java new file mode 100644 index 0000000000..557ba78aca --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestInjectQualifier.java @@ -0,0 +1,23 @@ +package com.baeldung.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.dependency.AnotherArbitraryDependency; +import com.baeldung.dependency.ArbitraryDependency; + +@Configuration +public class ApplicationContextTestInjectQualifier { + + @Bean + public ArbitraryDependency defaultFile() { + ArbitraryDependency defaultFile = new ArbitraryDependency(); + return defaultFile; + } + + @Bean + public ArbitraryDependency namedFile() { + ArbitraryDependency namedFile = new AnotherArbitraryDependency(); + return namedFile; + } +} diff --git a/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestInjectType.java b/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestInjectType.java new file mode 100644 index 0000000000..f84fa8f3a2 --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestInjectType.java @@ -0,0 +1,16 @@ +package com.baeldung.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.dependency.ArbitraryDependency; + +@Configuration +public class ApplicationContextTestInjectType { + + @Bean + public ArbitraryDependency injectDependency() { + ArbitraryDependency injectDependency = new ArbitraryDependency(); + return injectDependency; + } +} diff --git a/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestResourceNameType.java b/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestResourceNameType.java new file mode 100644 index 0000000000..f53002feca --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestResourceNameType.java @@ -0,0 +1,16 @@ +package com.baeldung.configuration; + +import java.io.File; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ApplicationContextTestResourceNameType { + + @Bean(name="namedFile") + public File namedFile() { + File namedFile = new File("namedFile.txt"); + return namedFile; + } +} diff --git a/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestResourceQualifier.java b/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestResourceQualifier.java new file mode 100644 index 0000000000..18f5af4282 --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/configuration/ApplicationContextTestResourceQualifier.java @@ -0,0 +1,22 @@ +package com.baeldung.configuration; + +import java.io.File; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@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; + } +} diff --git a/dependency-injection/src/test/java/com/baeldung/dependency/AnotherArbitraryDependency.java b/dependency-injection/src/test/java/com/baeldung/dependency/AnotherArbitraryDependency.java index 27ba03f6e8..0e19523b7e 100644 --- a/dependency-injection/src/test/java/com/baeldung/dependency/AnotherArbitraryDependency.java +++ b/dependency-injection/src/test/java/com/baeldung/dependency/AnotherArbitraryDependency.java @@ -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"; diff --git a/dependency-injection/src/test/java/com/baeldung/dependency/ArbitraryDependency.java b/dependency-injection/src/test/java/com/baeldung/dependency/ArbitraryDependency.java index bab289777c..d120e8daf7 100644 --- a/dependency-injection/src/test/java/com/baeldung/dependency/ArbitraryDependency.java +++ b/dependency-injection/src/test/java/com/baeldung/dependency/ArbitraryDependency.java @@ -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"; diff --git a/dependency-injection/src/test/java/com/baeldung/dependency/YetAnotherArbitraryDependency.java b/dependency-injection/src/test/java/com/baeldung/dependency/YetAnotherArbitraryDependency.java index 1f59500ec5..a88abd0924 100644 --- a/dependency-injection/src/test/java/com/baeldung/dependency/YetAnotherArbitraryDependency.java +++ b/dependency-injection/src/test/java/com/baeldung/dependency/YetAnotherArbitraryDependency.java @@ -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"; diff --git a/dependency-injection/src/test/java/com/baeldung/inject/FieldByNameInjectDemo.java b/dependency-injection/src/test/java/com/baeldung/inject/FieldByNameInjectTest.java similarity index 56% rename from dependency-injection/src/test/java/com/baeldung/inject/FieldByNameInjectDemo.java rename to dependency-injection/src/test/java/com/baeldung/inject/FieldByNameInjectTest.java index a670ee8313..0f0429ea94 100644 --- a/dependency-injection/src/test/java/com/baeldung/inject/FieldByNameInjectDemo.java +++ b/dependency-injection/src/test/java/com/baeldung/inject/FieldByNameInjectTest.java @@ -10,21 +10,25 @@ 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.configuration.ApplicationContextTestInjectName; import com.baeldung.dependency.ArbitraryDependency; @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()); + assertEquals("Yet Another Arbitrary Dependency", + yetAnotherFieldInjectDependency.toString()); } } diff --git a/dependency-injection/src/test/java/com/baeldung/inject/FieldInjectDemo.java b/dependency-injection/src/test/java/com/baeldung/inject/FieldInjectDemo.java deleted file mode 100644 index df40e516ba..0000000000 --- a/dependency-injection/src/test/java/com/baeldung/inject/FieldInjectDemo.java +++ /dev/null @@ -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()); - } -} diff --git a/dependency-injection/src/test/java/com/baeldung/inject/FieldInjectTest.java b/dependency-injection/src/test/java/com/baeldung/inject/FieldInjectTest.java new file mode 100644 index 0000000000..7cbba63113 --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/inject/FieldInjectTest.java @@ -0,0 +1,31 @@ +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 org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.configuration.ApplicationContextTestInjectType; +import com.baeldung.dependency.ArbitraryDependency; + +@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()); + } +} diff --git a/dependency-injection/src/test/java/com/baeldung/inject/FieldQualifierInjectDemo.java b/dependency-injection/src/test/java/com/baeldung/inject/FieldQualifierInjectTest.java similarity index 54% rename from dependency-injection/src/test/java/com/baeldung/inject/FieldQualifierInjectDemo.java rename to dependency-injection/src/test/java/com/baeldung/inject/FieldQualifierInjectTest.java index 3cc9b643c7..f0fc5866a3 100644 --- a/dependency-injection/src/test/java/com/baeldung/inject/FieldQualifierInjectDemo.java +++ b/dependency-injection/src/test/java/com/baeldung/inject/FieldQualifierInjectTest.java @@ -10,13 +10,15 @@ 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.configuration.ApplicationContextTestInjectQualifier; import com.baeldung.dependency.ArbitraryDependency; @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,14 +29,16 @@ 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()); + 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()); + assertEquals("Another Arbitrary Dependency", + namedDependency.toString()); } } diff --git a/dependency-injection/src/test/java/com/baeldung/resource/FieldResourceInjectionDemo.java b/dependency-injection/src/test/java/com/baeldung/resource/FieldResourceInjectionDemo.java deleted file mode 100644 index fbb378d672..0000000000 --- a/dependency-injection/src/test/java/com/baeldung/resource/FieldResourceInjectionDemo.java +++ /dev/null @@ -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); - } -} diff --git a/dependency-injection/src/test/java/com/baeldung/resource/FieldResourceInjectionTest.java b/dependency-injection/src/test/java/com/baeldung/resource/FieldResourceInjectionTest.java new file mode 100644 index 0000000000..ed73ae8d59 --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/resource/FieldResourceInjectionTest.java @@ -0,0 +1,31 @@ +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.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.configuration.ApplicationContextTestResourceNameType; + +@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()); + } +} diff --git a/dependency-injection/src/test/java/com/baeldung/resource/MethodByQualifierResourceDemo.java b/dependency-injection/src/test/java/com/baeldung/resource/MethodByQualifierResourceTest.java similarity index 72% rename from dependency-injection/src/test/java/com/baeldung/resource/MethodByQualifierResourceDemo.java rename to dependency-injection/src/test/java/com/baeldung/resource/MethodByQualifierResourceTest.java index fcca34dc2f..e512b6fe0a 100644 --- a/dependency-injection/src/test/java/com/baeldung/resource/MethodByQualifierResourceDemo.java +++ b/dependency-injection/src/test/java/com/baeldung/resource/MethodByQualifierResourceTest.java @@ -12,17 +12,21 @@ 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.configuration.ApplicationContextTestResourceQualifier; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(locations={ - "/applicationContextTest-@Resource-Qualifier.xml"}) -public class MethodByQualifierResourceDemo { +@ContextConfiguration( + loader=AnnotationConfigContextLoader.class, + classes=ApplicationContextTestResourceQualifier.class) +public class MethodByQualifierResourceTest { private File arbDependency; private File anotherArbDependency; @Test - public void dependencies_MUST_BE_INJECTED_Correctly() { + public void givenResourceQualifier_WhenSetter_ThenValidDependencies(){ assertNotNull(arbDependency); assertEquals("namedFile.txt", arbDependency.getName()); assertNotNull(anotherArbDependency); diff --git a/dependency-injection/src/test/java/com/baeldung/resource/MethodByTypeResourceDemo.java b/dependency-injection/src/test/java/com/baeldung/resource/MethodByTypeResourceTest.java similarity index 52% rename from dependency-injection/src/test/java/com/baeldung/resource/MethodByTypeResourceDemo.java rename to dependency-injection/src/test/java/com/baeldung/resource/MethodByTypeResourceTest.java index af6a805bd9..edcf9b12ea 100644 --- a/dependency-injection/src/test/java/com/baeldung/resource/MethodByTypeResourceDemo.java +++ b/dependency-injection/src/test/java/com/baeldung/resource/MethodByTypeResourceTest.java @@ -1,5 +1,6 @@ package com.baeldung.resource; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import java.io.File; @@ -10,11 +11,15 @@ 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.configuration.ApplicationContextTestResourceNameType; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(locations={ - "/applicationContextTest-@Resource-NameType.xml"}) -public class MethodByTypeResourceDemo { +@ContextConfiguration( + loader=AnnotationConfigContextLoader.class, + classes=ApplicationContextTestResourceNameType.class) +public class MethodByTypeResourceTest { private File defaultFile; @@ -24,7 +29,8 @@ public class MethodByTypeResourceDemo { } @Test - public void defaultFile_MUST_BE_INJECTED_Correctly() { + public void givenResourceAnnotation_WhenSetter_ThenValidDependency(){ assertNotNull(defaultFile); + assertEquals("namedFile.txt", defaultFile.getName()); } } diff --git a/dependency-injection/src/test/java/com/baeldung/resource/MethodResourceInjectionDemo.java b/dependency-injection/src/test/java/com/baeldung/resource/MethodResourceInjectionTest.java similarity index 53% rename from dependency-injection/src/test/java/com/baeldung/resource/MethodResourceInjectionDemo.java rename to dependency-injection/src/test/java/com/baeldung/resource/MethodResourceInjectionTest.java index d746fd4d85..38e9db8737 100644 --- a/dependency-injection/src/test/java/com/baeldung/resource/MethodResourceInjectionDemo.java +++ b/dependency-injection/src/test/java/com/baeldung/resource/MethodResourceInjectionTest.java @@ -1,4 +1,6 @@ package com.baeldung.resource; + +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import java.io.File; @@ -9,11 +11,15 @@ 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.configuration.ApplicationContextTestResourceNameType; @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; @@ -23,7 +29,8 @@ public class MethodResourceInjectionDemo { } @Test - public void defaultFile_MUST_BE_INJECTED_Correctly() { + public void givenResourceAnnotation_WhenSetter_ThenDependencyValid(){ assertNotNull(defaultFile); + assertEquals("namedFile.txt", defaultFile.getName()); } } diff --git a/dependency-injection/src/test/java/com/baeldung/resource/NamedResourceTest.java b/dependency-injection/src/test/java/com/baeldung/resource/NamedResourceTest.java index 8b218dfe98..6dbe77da39 100644 --- a/dependency-injection/src/test/java/com/baeldung/resource/NamedResourceTest.java +++ b/dependency-injection/src/test/java/com/baeldung/resource/NamedResourceTest.java @@ -1,6 +1,6 @@ package com.baeldung.resource; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; import java.io.File; @@ -10,18 +10,21 @@ 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.configuration.ApplicationContextTestResourceNameType; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(locations={ - "/applicationContextTest-@Resource-NameType.xml"}) +@ContextConfiguration(loader=AnnotationConfigContextLoader.class, + classes=ApplicationContextTestResourceNameType.class) public class NamedResourceTest { @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()); } } diff --git a/dependency-injection/src/test/java/com/baeldung/resource/QualifierResourceInjectionDemo.java b/dependency-injection/src/test/java/com/baeldung/resource/QualifierResourceInjectionDemo.java deleted file mode 100644 index 0aaa2085d5..0000000000 --- a/dependency-injection/src/test/java/com/baeldung/resource/QualifierResourceInjectionDemo.java +++ /dev/null @@ -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); - } -} diff --git a/dependency-injection/src/test/java/com/baeldung/resource/QualifierResourceInjectionTest.java b/dependency-injection/src/test/java/com/baeldung/resource/QualifierResourceInjectionTest.java new file mode 100644 index 0000000000..9afa0cf4f9 --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/resource/QualifierResourceInjectionTest.java @@ -0,0 +1,43 @@ +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; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.configuration.ApplicationContextTestResourceQualifier; + +@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()); + } +} diff --git a/dependency-injection/src/test/java/com/baeldung/resource/SetterResourceInjectionDemo.java b/dependency-injection/src/test/java/com/baeldung/resource/SetterResourceInjectionTest.java similarity index 51% rename from dependency-injection/src/test/java/com/baeldung/resource/SetterResourceInjectionDemo.java rename to dependency-injection/src/test/java/com/baeldung/resource/SetterResourceInjectionTest.java index 25dd5bb9ff..8a249fea4e 100644 --- a/dependency-injection/src/test/java/com/baeldung/resource/SetterResourceInjectionDemo.java +++ b/dependency-injection/src/test/java/com/baeldung/resource/SetterResourceInjectionTest.java @@ -1,4 +1,6 @@ package com.baeldung.resource; + +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import java.io.File; @@ -9,11 +11,14 @@ 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.configuration.ApplicationContextTestResourceNameType; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(locations={ - "/applicationContextTest-@Resource-NameType.xml"}) -public class SetterResourceInjectionDemo { +@ContextConfiguration(loader=AnnotationConfigContextLoader.class, + classes=ApplicationContextTestResourceNameType.class) +public class SetterResourceInjectionTest { private File defaultFile; @@ -23,7 +28,8 @@ public class SetterResourceInjectionDemo { } @Test - public void setter_MUST_INJECT_Resource() { + public void givenResourceAnnotation_WhenOnSetter_THEN_MUST_INJECT_Dependency() { assertNotNull(defaultFile); + assertEquals("namedFile.txt", defaultFile.getName()); } } diff --git a/httpclient/README.md b/httpclient/README.md index e06c57da71..a848edfea6 100644 --- a/httpclient/README.md +++ b/httpclient/README.md @@ -1,7 +1,9 @@ ========= - ## HttpClient 4.x Cookbooks and Examples +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring + ### Relevant Articles: diff --git a/jackson/README.md b/jackson/README.md index 53b9c7c31d..68765de686 100644 --- a/jackson/README.md +++ b/jackson/README.md @@ -2,6 +2,9 @@ ## Jackson Cookbooks and Examples +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring + ### Relevant Articles: - [Jackson Ignore Properties on Marshalling](http://www.baeldung.com/jackson-ignore-properties-on-serialization) - [Jackson – Unmarshall to Collection/Array](http://www.baeldung.com/jackson-collection-array) diff --git a/jooq-spring/pom.xml b/jooq-spring/pom.xml index 7a3ec0ac24..e77ff0cbfd 100644 --- a/jooq-spring/pom.xml +++ b/jooq-spring/pom.xml @@ -5,22 +5,13 @@ jooq-spring 0.0.1-SNAPSHOT - - 3.7.3 - 1.4.191 - 4.2.5.RELEASE - 1.7.18 - 1.1.3 - 4.12 - - org.springframework.boot spring-boot-dependencies - 1.3.3.RELEASE + 1.3.5.RELEASE pom import @@ -46,30 +37,25 @@ org.springframework spring-context - ${org.springframework.version} org.springframework spring-jdbc - ${org.springframework.version} org.springframework.boot spring-boot-starter-jooq - 1.3.3.RELEASE org.slf4j slf4j-api - ${org.slf4j.version} runtime ch.qos.logback logback-classic - ${ch.qos.logback.version} runtime @@ -77,15 +63,13 @@ junit junit - ${junit.version} test org.springframework spring-test - ${org.springframework.version} test - + @@ -166,6 +150,29 @@ + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + + 3.7.3 + 1.4.191 + 4.2.5.RELEASE + 1.7.18 + 1.1.3 + 4.12 + + 3.5.1 + + \ No newline at end of file diff --git a/mock-comparisons/pom.xml b/mock-comparisons/pom.xml index 97e24c61cd..c5424d262d 100644 --- a/mock-comparisons/pom.xml +++ b/mock-comparisons/pom.xml @@ -12,6 +12,8 @@ 1.10.19 3.4 1.24 + + UTF-8 3.3 diff --git a/pom.xml b/pom.xml index 19c602091d..75281ce80d 100644 --- a/pom.xml +++ b/pom.xml @@ -9,6 +9,8 @@ apache-fop + assertj + core-java core-java-8 gson diff --git a/raml/README.MD b/raml/README.MD new file mode 100644 index 0000000000..2a87b46021 --- /dev/null +++ b/raml/README.MD @@ -0,0 +1,2 @@ +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring diff --git a/rest-testing/README.md b/rest-testing/README.md index db7f0c8a86..54a2e98dda 100644 --- a/rest-testing/README.md +++ b/rest-testing/README.md @@ -2,6 +2,8 @@ ## REST Testing and Examples +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant Articles: - [Test a REST API with Java](http://www.baeldung.com/2011/10/13/integration-testing-a-rest-api/) diff --git a/spring-all/README.md b/spring-all/README.md index 977b8b7357..47c947a414 100644 --- a/spring-all/README.md +++ b/spring-all/README.md @@ -4,6 +4,8 @@ This project is used to replicate Spring Exceptions only. +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant articles: - [Properties with Spring](http://www.baeldung.com/2012/02/06/properties-with-spring) - checkout the `org.baeldung.properties` package for all scenarios of properties injection and usage diff --git a/spring-boot/README.MD b/spring-boot/README.MD new file mode 100644 index 0000000000..2a87b46021 --- /dev/null +++ b/spring-boot/README.MD @@ -0,0 +1,2 @@ +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring diff --git a/spring-data-neo4j/README.md b/spring-data-neo4j/README.md new file mode 100644 index 0000000000..e62c69f8b9 --- /dev/null +++ b/spring-data-neo4j/README.md @@ -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 +``` + diff --git a/spring-data-neo4j/pom.xml b/spring-data-neo4j/pom.xml new file mode 100644 index 0000000000..a5a2e9220a --- /dev/null +++ b/spring-data-neo4j/pom.xml @@ -0,0 +1,93 @@ + + + 4.0.0 + com.baeldung + spring-data-neo4j + 1.0 + jar + + + 1.8 + UTF-8 + UTF-8 + 3.0.1 + 4.1.1.RELEASE + + + + + org.springframework.data + spring-data-neo4j + ${spring-data-neo4j.version} + + + + com.voodoodyne.jackson.jsog + jackson-jsog + 1.1 + compile + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.data + spring-data-neo4j + ${spring-data-neo4j.version} + test-jar + + + + org.neo4j + neo4j-kernel + ${neo4j.version} + test-jar + + + + org.neo4j.app + neo4j-server + ${neo4j.version} + test-jar + + + + org.neo4j + neo4j-ogm-test + 2.0.2 + test + + + + org.neo4j.test + neo4j-harness + ${neo4j.version} + test + + + junit + junit + 4.12 + + + org.springframework + spring-test + 4.2.3.RELEASE + + + + + + + + maven-compiler-plugin + + + + + diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jConfiguration.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jConfiguration.java new file mode 100644 index 0000000000..ac9a7260be --- /dev/null +++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jConfiguration.java @@ -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"); + } +} diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jTestConfiguration.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jTestConfiguration.java new file mode 100644 index 0000000000..2b6394184d --- /dev/null +++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jTestConfiguration.java @@ -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"); + } +} diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Movie.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Movie.java new file mode 100644 index 0000000000..e48dfaf276 --- /dev/null +++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Movie.java @@ -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 roles; + + public Movie() { } + + public String getTitle() { + return title; + } + + public int getReleased() { + return released; + } + + public String getTagline() { + return tagline; + } + + public Collection 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 roles) { + this.roles = roles; + } + + +} diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Person.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Person.java new file mode 100644 index 0000000000..d96dc07530 --- /dev/null +++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Person.java @@ -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 movies; + + public Person() { } + + public String getName() { + return name; + } + + public int getBorn() { + return born; + } + + public List getMovies() { + return movies; + } + + public void setName(String name) { + this.name = name; + } + + public void setBorn(int born) { + this.born = born; + } + + public void setMovies(List movies) { + this.movies = movies; + } + +} diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Role.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Role.java new file mode 100644 index 0000000000..20512a10ad --- /dev/null +++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Role.java @@ -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 roles; + @StartNode + private Person person; + @EndNode + private Movie movie; + + public Role() { + } + + public Collection getRoles() { + return roles; + } + + public Person getPerson() { + return person; + } + + public Movie getMovie() { + return movie; + } + + public void setRoles(Collection roles) { + this.roles = roles; + } + + public void setPerson(Person person) { + this.person = person; + } + + public void setMovie(Movie movie) { + this.movie = movie; + } +} diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/MovieRepository.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/MovieRepository.java new file mode 100644 index 0000000000..850d2336ba --- /dev/null +++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/MovieRepository.java @@ -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 findByTitle(@Param("title") String title); + + @Query("MATCH (m:Movie) WHERE m.title =~ ('(?i).*'+{title}+'.*') RETURN m") + Collection 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> graph(@Param("limit") int limit); +} + + diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/PersonRepository.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/PersonRepository.java new file mode 100644 index 0000000000..4c287f99a4 --- /dev/null +++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/PersonRepository.java @@ -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 { + +} diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/services/MovieService.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/services/MovieService.java new file mode 100644 index 0000000000..532cc79091 --- /dev/null +++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/services/MovieService.java @@ -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 toD3Format(Iterator> result) { + List> nodes = new ArrayList>(); + List> rels= new ArrayList>(); + int i=0; + while (result.hasNext()) { + Map row = result.next(); + nodes.add(map("title",row.get("movie"),"label","movie")); + int target=i; + i++; + for (Object name : (Collection) row.get("cast")) { + Map 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 map(String key1, Object value1, String key2, Object value2) { + Map result = new HashMap(2); + result.put(key1,value1); + result.put(key2,value2); + return result; + } + + public Map graph(int limit) { + Iterator> result = movieRepository.graph(limit).iterator(); + return toD3Format(result); + } +} diff --git a/spring-data-neo4j/src/main/resources/logback.xml b/spring-data-neo4j/src/main/resources/logback.xml new file mode 100644 index 0000000000..215eeede64 --- /dev/null +++ b/spring-data-neo4j/src/main/resources/logback.xml @@ -0,0 +1,20 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-data-neo4j/src/main/resources/test.png b/spring-data-neo4j/src/main/resources/test.png new file mode 100644 index 0000000000..c3b5e80276 Binary files /dev/null and b/spring-data-neo4j/src/main/resources/test.png differ diff --git a/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryTest.java b/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryTest.java new file mode 100644 index 0000000000..8061b3c2a7 --- /dev/null +++ b/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryTest.java @@ -0,0 +1,133 @@ +package com.baeldung.spring.data.neo4j; + +import com.baeldung.spring.data.neo4j.config.MovieDatabaseNeo4jTestConfiguration; +import com.baeldung.spring.data.neo4j.domain.Movie; +import com.baeldung.spring.data.neo4j.domain.Person; +import com.baeldung.spring.data.neo4j.domain.Role; +import com.baeldung.spring.data.neo4j.repostory.MovieRepository; +import com.baeldung.spring.data.neo4j.repostory.PersonRepository; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.*; + +import static junit.framework.TestCase.assertNull; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = MovieDatabaseNeo4jTestConfiguration.class) +@ActiveProfiles(profiles = "test") +public class MovieRepositoryTest { + + @Autowired + private MovieRepository movieRepository; + + @Autowired + private PersonRepository personRepository; + + public MovieRepositoryTest() { + } + + @Before + public void initializeDatabase() { + System.out.println("seeding embedded database"); + Movie italianJob = new Movie(); + italianJob.setTitle("The Italian Job"); + italianJob.setReleased(1999); + movieRepository.save(italianJob); + + Person mark = new Person(); + mark.setName("Mark Wahlberg"); + personRepository.save(mark); + + Role charlie = new Role(); + charlie.setMovie(italianJob); + charlie.setPerson(mark); + Collection roleNames = new HashSet(); + roleNames.add("Charlie Croker"); + charlie.setRoles(roleNames); + List roles = new ArrayList(); + roles.add(charlie); + italianJob.setRoles(roles); + movieRepository.save(italianJob); + } + + @Test + @DirtiesContext + public void testFindByTitle() { + System.out.println("findByTitle"); + String title = "The Italian Job"; + Movie result = movieRepository.findByTitle(title); + assertNotNull(result); + assertEquals(1999, result.getReleased()); + } + + @Test + @DirtiesContext + public void testCount() { + System.out.println("count"); + long movieCount = movieRepository.count(); + assertNotNull(movieCount); + assertEquals(1, movieCount); + } + + @Test + @DirtiesContext + public void testFindAll() { + System.out.println("findAll"); + Collection result = + (Collection) movieRepository.findAll(); + assertNotNull(result); + assertEquals(1, result.size()); + } + + @Test + @DirtiesContext + public void testFindByTitleContaining() { + System.out.println("findByTitleContaining"); + String title = "Italian"; + Collection result = + movieRepository.findByTitleContaining(title); + assertNotNull(result); + assertEquals(1, result.size()); + } + + @Test + @DirtiesContext + public void testGraph() { + System.out.println("graph"); + List> graph = movieRepository.graph(5); + assertEquals(1, graph.size()); + Map map = graph.get(0); + assertEquals(2, map.size()); + String[] cast = (String[]) map.get("cast"); + String movie = (String) map.get("movie"); + assertEquals("The Italian Job", movie); + assertEquals("Mark Wahlberg", cast[0]); + } + + @Test + @DirtiesContext + public void testDeleteMovie() { + System.out.println("deleteMovie"); + movieRepository.delete(movieRepository.findByTitle("The Italian Job")); + assertNull(movieRepository.findByTitle("The Italian Job")); + } + + @Test + @DirtiesContext + public void testDeleteAll() { + System.out.println("deleteAll"); + movieRepository.deleteAll(); + Collection result = + (Collection) movieRepository.findAll(); + assertEquals(0, result.size()); + } +} diff --git a/spring-data-rest/README.md b/spring-data-rest/README.md index d9be83113b..7dc439206b 100644 --- a/spring-data-rest/README.md +++ b/spring-data-rest/README.md @@ -1,3 +1,6 @@ +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring + # About this project This project contains examples from the [Introduction to Spring Data REST](http://www.baeldung.com/spring-data-rest-intro) article from Baeldung. diff --git a/spring-katharsis/README.md b/spring-katharsis/README.md index ec0141f41a..cf2a001760 100644 --- a/spring-katharsis/README.md +++ b/spring-katharsis/README.md @@ -2,5 +2,8 @@ ## Java Web Application +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring + ### Relevant Articles: - [JSON API in a Java Web Application](http://www.baeldung.com/json-api-java-spring-web-app) diff --git a/spring-mvc-java/README.md b/spring-mvc-java/README.md index e5264b0370..951d80033e 100644 --- a/spring-mvc-java/README.md +++ b/spring-mvc-java/README.md @@ -2,6 +2,8 @@ ## Spring MVC with Java Configuration Example Project +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant Articles: - [Spring Bean Annotations](http://www.baeldung.com/spring-bean-annotations) diff --git a/spring-mvc-no-xml/README.md b/spring-mvc-no-xml/README.md index 5ffe2385b5..208cb35f78 100644 --- a/spring-mvc-no-xml/README.md +++ b/spring-mvc-no-xml/README.md @@ -2,6 +2,8 @@ ## Spring MVC with NO XML Configuration Example Project +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant Articles: -- \ No newline at end of file +- diff --git a/spring-mvc-xml/README.md b/spring-mvc-xml/README.md index 2409ec8174..ce823a9682 100644 --- a/spring-mvc-xml/README.md +++ b/spring-mvc-xml/README.md @@ -1,5 +1,8 @@ ========= +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring + ## Spring MVC with XML Configuration Example Project - access a sample jsp page at: `http://localhost:8080/spring-mvc-xml/sample.html` diff --git a/spring-rest-docs/README.MD b/spring-rest-docs/README.MD new file mode 100644 index 0000000000..2a87b46021 --- /dev/null +++ b/spring-rest-docs/README.MD @@ -0,0 +1,2 @@ +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring diff --git a/spring-rest/.project b/spring-rest/.project index 894841d690..525c5e7795 100644 --- a/spring-rest/.project +++ b/spring-rest/.project @@ -15,11 +15,6 @@ - - org.eclipse.wst.validation.validationbuilder - - - org.springframework.ide.eclipse.core.springbuilder @@ -38,6 +33,5 @@ org.eclipse.jdt.core.javanature org.eclipse.m2e.core.maven2Nature org.eclipse.wst.common.project.facet.core.nature - org.eclipse.wst.jsdt.core.jsNature diff --git a/spring-rest/.settings/.jsdtscope b/spring-rest/.settings/.jsdtscope index b46b9207a8..7b3f0c8b9f 100644 --- a/spring-rest/.settings/.jsdtscope +++ b/spring-rest/.settings/.jsdtscope @@ -1,12 +1,5 @@ - - - - - - - diff --git a/spring-rest/.settings/org.eclipse.wst.common.project.facet.core.xml b/spring-rest/.settings/org.eclipse.wst.common.project.facet.core.xml index b9386231e6..8a1c189419 100644 --- a/spring-rest/.settings/org.eclipse.wst.common.project.facet.core.xml +++ b/spring-rest/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -1,8 +1,5 @@ - - - - + diff --git a/spring-rest/README.md b/spring-rest/README.md index 9d373962c4..7d993b38b8 100644 --- a/spring-rest/README.md +++ b/spring-rest/README.md @@ -2,6 +2,8 @@ ## Spring REST Example Project +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant Articles: - [Spring @RequestMapping](http://www.baeldung.com/spring-requestmapping) diff --git a/spring-rest/pom.xml b/spring-rest/pom.xml index b05a795c6d..00631d816c 100644 --- a/spring-rest/pom.xml +++ b/spring-rest/pom.xml @@ -9,7 +9,7 @@ org.springframework.boot spring-boot-starter-parent - 1.2.4.RELEASE + 1.3.5.RELEASE @@ -59,7 +59,6 @@ javax.servlet javax.servlet-api - 3.0.1 provided @@ -101,24 +100,20 @@ org.slf4j slf4j-api - ${org.slf4j.version} ch.qos.logback logback-classic - ${logback.version} org.slf4j jcl-over-slf4j - ${org.slf4j.version} org.slf4j log4j-over-slf4j - ${org.slf4j.version} @@ -126,34 +121,29 @@ junit junit - ${junit.version} test org.hamcrest hamcrest-core - ${org.hamcrest.version} test org.hamcrest hamcrest-library - ${org.hamcrest.version} test org.mockito mockito-core - ${mockito.version} test org.springframework spring-test - ${spring.version} com.google.protobuf @@ -235,11 +225,10 @@ - 4.0.4.RELEASE 4.3.11.Final - 5.1.38 + 5.1.39 diff --git a/spring-scurity-custom-permission/README.MD b/spring-scurity-custom-permission/README.MD new file mode 100644 index 0000000000..8fb14fa522 --- /dev/null +++ b/spring-scurity-custom-permission/README.MD @@ -0,0 +1,2 @@ +###The Course +The "Learn Spring Security" Classes: http://bit.ly/learnspringsecurity diff --git a/spring-security-basic-auth/README.md b/spring-security-basic-auth/README.md index 95e45ae519..f3c29e1777 100644 --- a/spring-security-basic-auth/README.md +++ b/spring-security-basic-auth/README.md @@ -2,6 +2,8 @@ ## Spring Security with Basic Authentication Example Project +###The Course +The "Learn Spring Security" Classes: http://bit.ly/learnspringsecurity ### Relevant Article: - [Spring Security - security none, filters none, access permitAll](http://www.baeldung.com/security-none-filters-none-access-permitAll) @@ -9,4 +11,4 @@ ### Notes -- the project includes both views as well as a REST layer \ No newline at end of file +- the project includes both views as well as a REST layer diff --git a/spring-security-client/README.MD b/spring-security-client/README.MD new file mode 100644 index 0000000000..2a87b46021 --- /dev/null +++ b/spring-security-client/README.MD @@ -0,0 +1,2 @@ +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring diff --git a/spring-security-custom-permission/README.MD b/spring-security-custom-permission/README.MD new file mode 100644 index 0000000000..2a87b46021 --- /dev/null +++ b/spring-security-custom-permission/README.MD @@ -0,0 +1,2 @@ +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring diff --git a/spring-security-mvc-custom/README.md b/spring-security-mvc-custom/README.md index 17f32e4a2f..cbf5fc6a97 100644 --- a/spring-security-mvc-custom/README.md +++ b/spring-security-mvc-custom/README.md @@ -2,6 +2,8 @@ ## Spring Security Login Example Project +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant Articles: - [Spring Security Remember Me](http://www.baeldung.com/spring-security-remember-me) diff --git a/spring-security-mvc-digest-auth/README.md b/spring-security-mvc-digest-auth/README.md index 3b93a84505..21835266bf 100644 --- a/spring-security-mvc-digest-auth/README.md +++ b/spring-security-mvc-digest-auth/README.md @@ -2,6 +2,8 @@ ## Spring Security with Digest Authentication Example Project +###The Course +The "Learn Spring Security" Classes: http://bit.ly/learnspringsecurity ### Relevant Article: - [Spring Security Digest Authentication](http://www.baeldung.com/spring-security-digest-authentication) diff --git a/spring-security-mvc-ldap/README.md b/spring-security-mvc-ldap/README.md index 686f611f99..1eb3b75405 100644 --- a/spring-security-mvc-ldap/README.md +++ b/spring-security-mvc-ldap/README.md @@ -1,6 +1,8 @@ ## Spring Security with LDAP Example Project +###The Course +The "Learn Spring Security" Classes: http://bit.ly/learnspringsecurity ### Relevant Article: - [Spring Security - security none, filters none, access permitAll](http://www.baeldung.com/security-none-filters-none-access-permitAll) diff --git a/spring-security-mvc-login/README.md b/spring-security-mvc-login/README.md index 256078f4b6..448c25d27b 100644 --- a/spring-security-mvc-login/README.md +++ b/spring-security-mvc-login/README.md @@ -2,11 +2,13 @@ ## Spring Security Login Example Project +###The Course +The "Learn Spring Security" Classes: http://bit.ly/learnspringsecurity ### Relevant Articles: - [Spring Security Form Login](http://www.baeldung.com/spring-security-login) - [Spring Security Logout](http://www.baeldung.com/spring-security-logout) -- [Spring Security Expressions – hasRole Example](http://www.baeldung.com/spring-security-expressions-basic) +- [Spring Security Expressions – hasRole Example](http://www.baeldung.com/spring-security-expressions-basic) ### Build the Project diff --git a/spring-security-mvc-persisted-remember-me/README.md b/spring-security-mvc-persisted-remember-me/README.md index df83fd3d77..0d5f4f5f0e 100644 --- a/spring-security-mvc-persisted-remember-me/README.md +++ b/spring-security-mvc-persisted-remember-me/README.md @@ -2,6 +2,8 @@ ## Spring Security Persisted Remember Me Example Project +###The Course +The "Learn Spring Security" Classes: http://bit.ly/learnspringsecurity ### Relevant Articles: - [Spring Security Persisted Remember Me](http://www.baeldung.com/spring-security-persistent-remember-me) diff --git a/spring-security-mvc-session/README.md b/spring-security-mvc-session/README.md index 0df728688a..28f216c130 100644 --- a/spring-security-mvc-session/README.md +++ b/spring-security-mvc-session/README.md @@ -2,9 +2,11 @@ ## Spring Security Login Example Project +###The Course +The "Learn Spring Security" Classes: http://bit.ly/learnspringsecurity ### Relevant Articles: -- [HttpSessionListener Example – Monitoring](http://www.baeldung.com/httpsessionlistener_with_metrics) +- [HttpSessionListener Example – Monitoring](http://www.baeldung.com/httpsessionlistener_with_metrics) - [Spring Security Session Management](http://www.baeldung.com/spring-security-session) diff --git a/spring-security-rest-basic-auth/README.md b/spring-security-rest-basic-auth/README.md index 723bcebbdd..9621773d91 100644 --- a/spring-security-rest-basic-auth/README.md +++ b/spring-security-rest-basic-auth/README.md @@ -2,6 +2,8 @@ ## REST API with Basic Authentication - Example Project +###The Course +The "Learn Spring Security" Classes: http://bit.ly/learnspringsecurity ### Relevant Articles: - [RestTemplate with Basic Authentication in Spring](http://www.baeldung.com/2012/04/16/how-to-use-resttemplate-with-basic-authentication-in-spring-3-1) diff --git a/spring-security-rest-custom/README.md b/spring-security-rest-custom/README.md index f19af32d41..38dc638e8d 100644 --- a/spring-security-rest-custom/README.md +++ b/spring-security-rest-custom/README.md @@ -2,6 +2,9 @@ ## Spring Security for REST Example Project +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring + ### Relevant Articles: - [Spring Security Authentication Provider](http://www.baeldung.com/spring-security-authentication-provider) - [Retrieve User Information in Spring Security](http://www.baeldung.com/get-user-in-spring-security) diff --git a/spring-security-rest-digest-auth/README.md b/spring-security-rest-digest-auth/README.md index 06e847edad..4fdc934fe5 100644 --- a/spring-security-rest-digest-auth/README.md +++ b/spring-security-rest-digest-auth/README.md @@ -2,6 +2,8 @@ ## REST API with Digest Authentication - Example Project +###The Course +The "Learn Spring Security" Classes: http://bit.ly/learnspringsecurity ### Relevant Articles: - [RestTemplate with Digest Authentication](http://www.baeldung.com/resttemplate-digest-authentication) diff --git a/spring-security-rest-full/README.md b/spring-security-rest-full/README.md index 72e8636df1..947d32e87c 100644 --- a/spring-security-rest-full/README.md +++ b/spring-security-rest-full/README.md @@ -2,8 +2,10 @@ ## REST Example Project with Spring Security -### The Course - The "REST With Spring" Classes: http://bit.ly/restwithspring +### Courses +The "REST With Spring" Classes: http://bit.ly/restwithspring + +The "Learn Spring Security" Classes: http://bit.ly/learnspringsecurity ### Relevant Articles: - [Spring Security Expressions - hasRole Example](http://www.baeldung.com/spring-security-expressions-basic) diff --git a/spring-security-rest/README.md b/spring-security-rest/README.md index 290cd491e0..87f14a9047 100644 --- a/spring-security-rest/README.md +++ b/spring-security-rest/README.md @@ -2,6 +2,10 @@ ## Spring Security for REST Example Project +### Courses +The "REST With Spring" Classes: http://bit.ly/restwithspring + +The "Learn Spring Security" Classes: http://bit.ly/learnspringsecurity ### Relevant Articles: - [Spring REST Service Security](http://www.baeldung.com/2011/10/31/securing-a-restful-web-service-with-spring-security-3-1-part-3/) diff --git a/xml/pom.xml b/xml/pom.xml index fc158901e6..9d88bd75eb 100644 --- a/xml/pom.xml +++ b/xml/pom.xml @@ -1,139 +1,117 @@ - 4.0.0 - com.baeldung - xml - 0.1-SNAPSHOT + 4.0.0 + com.baeldung + xml + 0.1-SNAPSHOT - xml + xml - - + + + + dom4j + dom4j + 1.6.1 + + + jaxen + jaxen + 1.1.6 + - - com.google.guava - guava - ${guava.version} - - - commons-io - commons-io - 2.4 - + + org.jdom + jdom2 + 2.0.6 + - - org.apache.commons - commons-collections4 - 4.0 - + + xerces + xercesImpl + 2.9.1 + - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - - - - + + + commons-io + commons-io + 2.4 + - - junit - junit - ${junit.version} - test - + + org.apache.commons + commons-collections4 + 4.0 + - - org.hamcrest - hamcrest-core - ${org.hamcrest.version} - test - - - org.hamcrest - hamcrest-library - ${org.hamcrest.version} - test - + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + - - org.mockito - mockito-core - ${mockito.version} - test - - + - - xml - - - src/main/resources - true - - + + junit + junit + ${junit.version} + test + - + - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.8 - 1.8 - - + + xml + + + src/main/resources + true + + - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - + - + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + - + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + - - - 4.3.11.Final - 5.1.38 + - - 2.7.2 + - - 1.7.13 - 1.1.3 + - - 5.1.3.Final + + 19.0 + 3.4 - - 19.0 - 3.4 + + 4.12 - - 1.3 - 4.12 - 1.10.19 + + 3.5.1 + 2.6 + 2.19.1 + 2.7 + 1.4.18 - 4.4.1 - 4.5 - - 2.9.0 - - - 3.5.1 - 2.6 - 2.19.1 - 2.7 - 1.4.18 - - + diff --git a/xml/src/main/java/com/baeldung/xml/DefaultParser.java b/xml/src/main/java/com/baeldung/xml/DefaultParser.java index 89326f45c9..1f2a7418fb 100644 --- a/xml/src/main/java/com/baeldung/xml/DefaultParser.java +++ b/xml/src/main/java/com/baeldung/xml/DefaultParser.java @@ -39,7 +39,7 @@ public class DefaultParser { XPath xPath = XPathFactory.newInstance().newXPath(); - String expression = "/Tutorials/Tutorial"; + String expression = "/tutorials/tutorial"; nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET); @@ -60,7 +60,7 @@ public class DefaultParser { XPath xPath = XPathFactory.newInstance().newXPath(); - String expression = "/Tutorials/Tutorial[@tutId=" + "'" + id + "'" + "]"; + String expression = "/tutorials/tutorial[@tutId=" + "'" + id + "'" + "]"; node = (Node) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE); @@ -83,7 +83,7 @@ public class DefaultParser { XPath xPath = XPathFactory.newInstance().newXPath(); - String expression = "//Tutorial[descendant::title[text()=" + "'" + name + "'" + "]]"; + String expression = "//tutorial[descendant::title[text()=" + "'" + name + "'" + "]]"; nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET); @@ -107,7 +107,7 @@ public class DefaultParser { XPath xPath = XPathFactory.newInstance().newXPath(); - String expression = "//Tutorial[number(translate(date, '/', '')) > " + date + "]"; + String expression = "//tutorial[number(translate(date, '/', '')) > " + date + "]"; nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET); @@ -151,7 +151,7 @@ public class DefaultParser { } }); - String expression = "/bdn:Tutorials/bdn:Tutorial"; + String expression = "/bdn:tutorials/bdn:tutorial"; nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET); diff --git a/xml/src/main/java/com/baeldung/xml/Dom4JParser.java b/xml/src/main/java/com/baeldung/xml/Dom4JParser.java new file mode 100644 index 0000000000..d9058ba236 --- /dev/null +++ b/xml/src/main/java/com/baeldung/xml/Dom4JParser.java @@ -0,0 +1,131 @@ +package com.baeldung.xml; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.DocumentHelper; +import org.dom4j.Element; +import org.dom4j.Node; +import org.dom4j.io.OutputFormat; +import org.dom4j.io.SAXReader; +import org.dom4j.io.XMLWriter; + +public class Dom4JParser { + + private File file; + + public Dom4JParser(File file) { + this.file = file; + } + + public Element getRootElement() { + try { + SAXReader reader = new SAXReader(); + Document document = reader.read(file); + return document.getRootElement(); + } catch (DocumentException e) { + e.printStackTrace(); + return null; + } + } + + public List getFirstElementList() { + try { + SAXReader reader = new SAXReader(); + Document document = reader.read(file); + return document.getRootElement().elements(); + } catch (DocumentException e) { + e.printStackTrace(); + return null; + } + } + + public Node getNodeById(String id) { + try { + SAXReader reader = new SAXReader(); + Document document = reader.read(file); + List elements = document.selectNodes("//*[@tutId='" + id + "']"); + return elements.get(0); + } catch (DocumentException e) { + e.printStackTrace(); + return null; + } + } + + public Node getElementsListByTitle(String name) { + try { + SAXReader reader = new SAXReader(); + Document document = reader.read(file); + List elements = document + .selectNodes("//tutorial[descendant::title[text()=" + "'" + name + "'" + "]]"); + return elements.get(0); + } catch (DocumentException e) { + e.printStackTrace(); + return null; + } + } + + public void generateModifiedDocument() { + try { + SAXReader reader = new SAXReader(); + Document document = reader.read(file); + List nodes = document.selectNodes("/tutorials/tutorial"); + for (Node node : nodes) { + Element element = (Element) node; + Iterator iterator = element.elementIterator("title"); + while (iterator.hasNext()) { + Element title = (Element) iterator.next(); + title.setText(title.getText() + " updated"); + } + } + XMLWriter writer = new XMLWriter(new FileWriter(new File("src/test/resources/example_updated.xml"))); + writer.write(document); + writer.close(); + } catch (DocumentException e) { + e.printStackTrace(); + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void generateNewDocument() { + try { + Document document = DocumentHelper.createDocument(); + Element root = document.addElement("XMLTutorials"); + Element tutorialElement = root.addElement("tutorial").addAttribute("tutId", "01"); + tutorialElement.addAttribute("type", "xml"); + + tutorialElement.addElement("title").addText("XML with Dom4J"); + + tutorialElement.addElement("description").addText("XML handling with Dom4J"); + + tutorialElement.addElement("date").addText("14/06/2016"); + + tutorialElement.addElement("author").addText("Dom4J tech writer"); + + OutputFormat format = OutputFormat.createPrettyPrint(); + XMLWriter writer = new XMLWriter(new FileWriter(new File("src/test/resources/example_new.xml")), format); + writer.write(document); + writer.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public File getFile() { + return file; + } + + public void setFile(File file) { + this.file = file; + } + +} diff --git a/xml/src/main/java/com/baeldung/xml/JDomParser.java b/xml/src/main/java/com/baeldung/xml/JDomParser.java new file mode 100644 index 0000000000..08676b44f8 --- /dev/null +++ b/xml/src/main/java/com/baeldung/xml/JDomParser.java @@ -0,0 +1,62 @@ +package com.baeldung.xml; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.filter.Filters; +import org.jdom2.input.SAXBuilder; +import org.jdom2.xpath.XPathExpression; +import org.jdom2.xpath.XPathFactory; + + +public class JDomParser { + + private File file; + + public JDomParser(File file) { + this.file = file; + } + + public List getAllTitles() { + try { + SAXBuilder builder = new SAXBuilder(); + Document doc = builder.build(this.getFile()); + Element tutorials = doc.getRootElement(); + List titles = tutorials.getChildren("tutorial"); + return titles; + } catch (JDOMException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return null; + } + } + + public Element getNodeById(String id) { + try { + SAXBuilder builder = new SAXBuilder(); + Document document = (Document) builder.build(file); + String filter = "//*[@tutId='" + id + "']"; + XPathFactory xFactory = XPathFactory.instance(); + XPathExpression expr = xFactory.compile(filter, Filters.element()); + List node = expr.evaluate(document); + + return node.get(0); + } catch (JDOMException | IOException e ) { + e.printStackTrace(); + return null; + } + } + + public File getFile() { + return file; + } + + public void setFile(File file) { + this.file = file; + } + +} diff --git a/xml/src/main/java/com/baeldung/xml/JaxbParser.java b/xml/src/main/java/com/baeldung/xml/JaxbParser.java new file mode 100644 index 0000000000..758ebb969c --- /dev/null +++ b/xml/src/main/java/com/baeldung/xml/JaxbParser.java @@ -0,0 +1,68 @@ +package com.baeldung.xml; + +import java.io.File; +import java.util.ArrayList; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; + +import com.baeldung.xml.binding.Tutorial; +import com.baeldung.xml.binding.Tutorials; + +public class JaxbParser { + + private File file; + + public JaxbParser(File file) { + this.file = file; + } + + public Tutorials getFullDocument() { + try { + JAXBContext jaxbContext = JAXBContext.newInstance(Tutorials.class); + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + Tutorials tutorials = (Tutorials) jaxbUnmarshaller.unmarshal(this.getFile()); + return tutorials; + } catch (JAXBException e) { + e.printStackTrace(); + return null; + } + } + + public void createNewDocument() { + Tutorials tutorials = new Tutorials(); + tutorials.setTutorial(new ArrayList()); + Tutorial tut = new Tutorial(); + tut.setTutId("01"); + tut.setType("XML"); + tut.setTitle("XML with Jaxb"); + tut.setDescription("XML Binding with Jaxb"); + tut.setDate("04/02/2015"); + tut.setAuthor("Jaxb author"); + tutorials.getTutorial().add(tut); + + try { + JAXBContext jaxbContext = JAXBContext.newInstance(Tutorials.class); + Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); + + jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + + jaxbMarshaller.marshal(tutorials, file); + + } catch (JAXBException e) { + e.printStackTrace(); + } + + } + + public File getFile() { + return file; + } + + public void setFile(File file) { + this.file = file; + } + +} diff --git a/xml/src/main/java/com/baeldung/xml/JaxenDemo.java b/xml/src/main/java/com/baeldung/xml/JaxenDemo.java new file mode 100644 index 0000000000..0c2dca0573 --- /dev/null +++ b/xml/src/main/java/com/baeldung/xml/JaxenDemo.java @@ -0,0 +1,57 @@ +package com.baeldung.xml; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.List; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.jaxen.JaxenException; +import org.jaxen.XPath; +import org.jaxen.dom.DOMXPath; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +public class JaxenDemo { + + private File file; + + public JaxenDemo(File file) { + this.file = file; + } + + public List getAllTutorial(){ + try { + FileInputStream fileIS = new FileInputStream(this.getFile()); + DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); + + DocumentBuilder builder = builderFactory.newDocumentBuilder(); + + Document xmlDocument = builder.parse(fileIS); + + String expression = "/tutorials/tutorial"; + + XPath path = new DOMXPath(expression); + List result = path.selectNodes(xmlDocument); + return result; + + } catch (SAXException | IOException | ParserConfigurationException | JaxenException e) { + e.printStackTrace(); + return null; + } + + } + + public File getFile() { + return file; + } + + public void setFile(File file) { + this.file = file; + } + + +} diff --git a/xml/src/main/java/com/baeldung/xml/StaxParser.java b/xml/src/main/java/com/baeldung/xml/StaxParser.java new file mode 100644 index 0000000000..e14d872831 --- /dev/null +++ b/xml/src/main/java/com/baeldung/xml/StaxParser.java @@ -0,0 +1,120 @@ +package com.baeldung.xml; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import javax.xml.stream.XMLEventReader; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.events.Attribute; +import javax.xml.stream.events.Characters; +import javax.xml.stream.events.EndElement; +import javax.xml.stream.events.StartElement; +import javax.xml.stream.events.XMLEvent; + +import com.baeldung.xml.binding.Tutorial; + +public class StaxParser { + + private File file; + + public StaxParser(File file) { + this.file = file; + } + + public List getAllTutorial() { + boolean bTitle = false; + boolean bDescription = false; + boolean bDate = false; + boolean bAuthor = false; + List tutorials = new ArrayList(); + try { + XMLInputFactory factory = XMLInputFactory.newInstance(); + XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(this.getFile())); + Tutorial current = null; + while (eventReader.hasNext()) { + XMLEvent event = eventReader.nextEvent(); + switch (event.getEventType()) { + case XMLStreamConstants.START_ELEMENT: + StartElement startElement = event.asStartElement(); + String qName = startElement.getName().getLocalPart(); + if (qName.equalsIgnoreCase("tutorial")) { + current = new Tutorial(); + Iterator attributes = startElement.getAttributes(); + while (attributes.hasNext()) { + Attribute currentAt = attributes.next(); + if (currentAt.getName().toString().equalsIgnoreCase("tutId")) { + current.setTutId(currentAt.getValue()); + } else if (currentAt.getName().toString().equalsIgnoreCase("type")) { + current.setType(currentAt.getValue()); + } + } + } else if (qName.equalsIgnoreCase("title")) { + bTitle = true; + } else if (qName.equalsIgnoreCase("description")) { + bDescription = true; + } else if (qName.equalsIgnoreCase("date")) { + bDate = true; + } else if (qName.equalsIgnoreCase("author")) { + bAuthor = true; + } + break; + case XMLStreamConstants.CHARACTERS: + Characters characters = event.asCharacters(); + if (bTitle) { + if (current != null) { + current.setTitle(characters.getData()); + } + bTitle = false; + } + if (bDescription) { + if (current != null) { + current.setDescription(characters.getData()); + } + bDescription = false; + } + if (bDate) { + if (current != null) { + current.setDate(characters.getData()); + } + bDate = false; + } + if (bAuthor) { + if (current != null) { + current.setAuthor(characters.getData()); + } + bAuthor = false; + } + break; + case XMLStreamConstants.END_ELEMENT: + EndElement endElement = event.asEndElement(); + if (endElement.getName().getLocalPart().equalsIgnoreCase("tutorial")) { + if(current != null){ + tutorials.add(current); + } + } + break; + } + } + + } catch (FileNotFoundException | XMLStreamException e) { + e.printStackTrace(); + } + + return tutorials; + } + + public File getFile() { + return file; + } + + public void setFile(File file) { + this.file = file; + } + +} diff --git a/xml/src/main/java/com/baeldung/xml/binding/Tutorial.java b/xml/src/main/java/com/baeldung/xml/binding/Tutorial.java new file mode 100644 index 0000000000..7201d499d0 --- /dev/null +++ b/xml/src/main/java/com/baeldung/xml/binding/Tutorial.java @@ -0,0 +1,59 @@ +package com.baeldung.xml.binding; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; + +public class Tutorial { + + private String tutId; + private String type; + private String title; + private String description; + private String date; + private String author; + + + public String getTutId() { + return tutId; + } + + @XmlAttribute + public void setTutId(String tutId) { + this.tutId = tutId; + } + public String getType() { + return type; + } + @XmlAttribute + public void setType(String type) { + this.type = type; + } + public String getTitle() { + return title; + } + @XmlElement + public void setTitle(String title) { + this.title = title; + } + public String getDescription() { + return description; + } + @XmlElement + public void setDescription(String description) { + this.description = description; + } + public String getDate() { + return date; + } + @XmlElement + public void setDate(String date) { + this.date = date; + } + public String getAuthor() { + return author; + } + @XmlElement + public void setAuthor(String author) { + this.author = author; + } +} diff --git a/xml/src/main/java/com/baeldung/xml/binding/Tutorials.java b/xml/src/main/java/com/baeldung/xml/binding/Tutorials.java new file mode 100644 index 0000000000..ab6669c0ad --- /dev/null +++ b/xml/src/main/java/com/baeldung/xml/binding/Tutorials.java @@ -0,0 +1,23 @@ +package com.baeldung.xml.binding; + +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +public class Tutorials { + + private List tutorial; + + public List getTutorial() { + return tutorial; + } + + @XmlElement + public void setTutorial(List tutorial) { + this.tutorial = tutorial; + } + + +} diff --git a/xml/src/test/java/com/baeldung/xml/DefaultParserTest.java b/xml/src/test/java/com/baeldung/xml/DefaultParserTest.java index 451917a5da..734e7a93cb 100644 --- a/xml/src/test/java/com/baeldung/xml/DefaultParserTest.java +++ b/xml/src/test/java/com/baeldung/xml/DefaultParserTest.java @@ -8,9 +8,6 @@ import org.junit.Test; import org.w3c.dom.Node; import org.w3c.dom.NodeList; -/** - * Unit test for simple App. - */ public class DefaultParserTest { final String fileName = "src/test/resources/example.xml"; @@ -29,7 +26,7 @@ public class DefaultParserTest { } @Test - public void getNodeListByTitle() { + public void getNodeListByTitleTest() { parser = new DefaultParser(new File(fileName)); NodeList list = parser.getNodeListByTitle("XML"); @@ -47,7 +44,7 @@ public class DefaultParserTest { } @Test - public void getNodeById() { + public void getNodeByIdTest() { parser = new DefaultParser(new File(fileName)); Node node = parser.getNodeById("03"); @@ -56,7 +53,7 @@ public class DefaultParserTest { } @Test - public void getNodeListByDate(){ + public void getNodeListByDateTest(){ parser = new DefaultParser(new File(fileName)); NodeList list = parser.getNodeListByTitle("04022016"); for (int i = 0; null != list && i < list.getLength(); i++) { @@ -73,7 +70,7 @@ public class DefaultParserTest { } @Test - public void getNodeListWithNamespace(){ + public void getNodeListWithNamespaceTest(){ parser = new DefaultParser(new File(fileNameSpace)); NodeList list = parser.getAllTutorials(); assertNotNull(list); diff --git a/xml/src/test/java/com/baeldung/xml/Dom4JParserTest.java b/xml/src/test/java/com/baeldung/xml/Dom4JParserTest.java new file mode 100644 index 0000000000..277eca8355 --- /dev/null +++ b/xml/src/test/java/com/baeldung/xml/Dom4JParserTest.java @@ -0,0 +1,88 @@ +package com.baeldung.xml; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.util.List; + +import org.dom4j.Element; +import org.dom4j.Node; +import org.junit.Test; + +public class Dom4JParserTest { + + final String fileName = "src/test/resources/example.xml"; + + Dom4JParser parser; + + @Test + public void getRootElementTest() { + parser = new Dom4JParser(new File(fileName)); + Element root = parser.getRootElement(); + + assertNotNull(root); + assertTrue(root.elements().size() == 4); + } + + @Test + public void getFirstElementListTest() { + parser = new Dom4JParser(new File(fileName)); + List firstList = parser.getFirstElementList(); + + assertNotNull(firstList); + assertTrue(firstList.size() == 4); + assertTrue(firstList.get(0).attributeValue("type").equals("java")); + } + + @Test + public void getElementByIdTest() { + parser = new Dom4JParser(new File(fileName)); + Node element = parser.getNodeById("03"); + + String type = element.valueOf("@type"); + assertEquals("android", type); + } + + @Test + public void getElementsListByTitleTest() { + parser = new Dom4JParser(new File(fileName)); + Node element = parser.getElementsListByTitle("XML"); + + assertEquals("java", element.valueOf("@type")); + assertEquals("02", element.valueOf("@tutId")); + assertEquals("XML", element.selectSingleNode("title").getText()); + assertEquals("title", element.selectSingleNode("title").getName()); + } + + @Test + public void generateModifiedDocumentTest() { + parser = new Dom4JParser(new File(fileName)); + parser.generateModifiedDocument(); + + File generatedFile = new File("src/test/resources/example_updated.xml"); + assertTrue(generatedFile.exists()); + + parser.setFile(generatedFile); + Node element = parser.getNodeById("02"); + + assertEquals("XML updated", element.selectSingleNode("title").getText()); + + } + + @Test + public void generateNewDocumentTest() { + parser = new Dom4JParser(new File(fileName)); + parser.generateNewDocument(); + + File newFile = new File("src/test/resources/example_new.xml"); + assertTrue(newFile.exists()); + + parser.setFile(newFile); + Node element = parser.getNodeById("01"); + + assertEquals("XML with Dom4J", element.selectSingleNode("title").getText()); + + } +} diff --git a/xml/src/test/java/com/baeldung/xml/JDomParserTest.java b/xml/src/test/java/com/baeldung/xml/JDomParserTest.java new file mode 100644 index 0000000000..981458469f --- /dev/null +++ b/xml/src/test/java/com/baeldung/xml/JDomParserTest.java @@ -0,0 +1,38 @@ +package com.baeldung.xml; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.util.List; + +import org.jdom2.Element; +import org.junit.Test; + +public class JDomParserTest { + + final String fileName = "src/test/resources/example.xml"; + + JDomParser parser; + + @Test + public void getFirstElementListTest() { + parser = new JDomParser(new File(fileName)); + List firstList = parser.getAllTitles(); + + assertNotNull(firstList); + assertTrue(firstList.size() == 4); + assertTrue(firstList.get(0).getAttributeValue("type").equals("java")); + } + + @Test + public void getElementByIdTest() { + parser = new JDomParser(new File(fileName)); + Element el = parser.getNodeById("03"); + + String type = el.getAttributeValue("type"); + assertEquals("android", type); + } + +} diff --git a/xml/src/test/java/com/baeldung/xml/JaxbParserTest.java b/xml/src/test/java/com/baeldung/xml/JaxbParserTest.java new file mode 100644 index 0000000000..6c31a7bfdd --- /dev/null +++ b/xml/src/test/java/com/baeldung/xml/JaxbParserTest.java @@ -0,0 +1,44 @@ +package com.baeldung.xml; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; + +import org.junit.Test; + +import com.baeldung.xml.binding.Tutorials; + +public class JaxbParserTest { + + + final String fileName = "src/test/resources/example.xml"; + + JaxbParser parser; + + @Test + public void getFullDocumentTest(){ + parser = new JaxbParser(new File(fileName)); + Tutorials tutorials = parser.getFullDocument(); + + assertNotNull(tutorials); + assertTrue(tutorials.getTutorial().size() == 4); + assertTrue(tutorials.getTutorial().get(0).getType().equalsIgnoreCase("java")); + } + + @Test + public void createNewDocumentTest(){ + File newFile = new File("src/test/resources/example_new.xml"); + parser = new JaxbParser(newFile); + parser.createNewDocument(); + + + assertTrue(newFile.exists()); + + Tutorials tutorials = parser.getFullDocument(); + + assertNotNull(tutorials); + assertTrue(tutorials.getTutorial().size() == 1); + assertTrue(tutorials.getTutorial().get(0).getTitle().equalsIgnoreCase("XML with Jaxb")); + } +} diff --git a/xml/src/test/java/com/baeldung/xml/JaxenDemoTest.java b/xml/src/test/java/com/baeldung/xml/JaxenDemoTest.java new file mode 100644 index 0000000000..2521fcaf8a --- /dev/null +++ b/xml/src/test/java/com/baeldung/xml/JaxenDemoTest.java @@ -0,0 +1,25 @@ +package com.baeldung.xml; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.util.List; + +import org.junit.Test; + +public class JaxenDemoTest { + + final String fileName = "src/test/resources/example.xml"; + + JaxenDemo jaxenDemo; + + @Test + public void getFirstLevelNodeListTest() { + jaxenDemo = new JaxenDemo(new File(fileName)); + List list = jaxenDemo.getAllTutorial(); + + assertNotNull(list); + assertTrue(list.size() == 4); + } +} diff --git a/xml/src/test/java/com/baeldung/xml/StaxParserTest.java b/xml/src/test/java/com/baeldung/xml/StaxParserTest.java new file mode 100644 index 0000000000..cf7ee1ee75 --- /dev/null +++ b/xml/src/test/java/com/baeldung/xml/StaxParserTest.java @@ -0,0 +1,28 @@ +package com.baeldung.xml; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.util.List; + +import org.junit.Test; + +import com.baeldung.xml.binding.Tutorial; + +public class StaxParserTest { + + final String fileName = "src/test/resources/example.xml"; + + StaxParser parser; + + @Test + public void getAllTutorialsTest(){ + parser = new StaxParser(new File(fileName)); + List tutorials = parser.getAllTutorial(); + + assertNotNull(tutorials); + assertTrue(tutorials.size() == 4); + assertTrue(tutorials.get(0).getType().equalsIgnoreCase("java")); + } +} diff --git a/xml/src/test/resources/example.xml b/xml/src/test/resources/example.xml index d546dd137b..0993b6240a 100644 --- a/xml/src/test/resources/example.xml +++ b/xml/src/test/resources/example.xml @@ -1,24 +1,24 @@ - - + + Guava Introduction to Guava 04/04/2016 GuavaAuthor - - + + XML Introduction to XPath 04/05/2016 XMLAuthor - - + + Android Introduction to Android 04/03/2016 AndroidAuthor - - + + Spring Introduction to Spring 04/02/2016 @@ -28,5 +28,5 @@
Spring MVC
Spring Batch
-
-
\ No newline at end of file +
+
\ No newline at end of file diff --git a/xml/src/test/resources/example_namespace.xml b/xml/src/test/resources/example_namespace.xml index 26131302ea..f1a880951a 100644 --- a/xml/src/test/resources/example_namespace.xml +++ b/xml/src/test/resources/example_namespace.xml @@ -1,24 +1,24 @@ - - + + Guava Introduction to Guava 04/04/2016 GuavaAuthor - - + + XML Introduction to XPath 04/05/2016 XMLAuthor - - + + Android Introduction to Android 04/03/2016 AndroidAuthor - - + + Spring Introduction to Spring 04/02/2016 @@ -28,5 +28,5 @@
Spring MVC
Spring Batch
-
-
\ No newline at end of file +
+
\ No newline at end of file diff --git a/xml/src/test/resources/example_new.xml b/xml/src/test/resources/example_new.xml new file mode 100644 index 0000000000..020760fdd3 --- /dev/null +++ b/xml/src/test/resources/example_new.xml @@ -0,0 +1,10 @@ + + + + + XML with Dom4J + XML handling with Dom4J + 14/06/2016 + Dom4J tech writer + + diff --git a/xml/src/test/resources/example_updated.xml b/xml/src/test/resources/example_updated.xml new file mode 100644 index 0000000000..962ca0c889 --- /dev/null +++ b/xml/src/test/resources/example_updated.xml @@ -0,0 +1,32 @@ + + + + Guava updated + Introduction to Guava + 04/04/2016 + GuavaAuthor + + + XML updated + Introduction to XPath + 04/05/2016 + XMLAuthor + + + Android updated + Introduction to Android + 04/03/2016 + AndroidAuthor + + + Spring updated + Introduction to Spring + 04/02/2016 + SpringAuthor + +
Spring Core
+
Spring MVC
+
Spring Batch
+
+
+
\ No newline at end of file