diff --git a/.gitignore b/.gitignore index 30f0f0cbc2..319eb5ca2d 100644 --- a/.gitignore +++ b/.gitignore @@ -7,8 +7,11 @@ # Eclipse .settings/ +*.project +*.classpath .prefs *.prefs +.metadata/ # Intellij .idea/ @@ -21,3 +24,7 @@ # Maven log/ target/ + +spring-openid/src/main/resources/application.properties +.recommenders/ + diff --git a/.project b/.project index 2d04570bfe..594b00d611 100644 --- a/.project +++ b/.project @@ -1,11 +1,17 @@ - parent - - - - - - - + parent-modules + + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + diff --git a/README.md b/README.md index 3e88f0c017..f0d3d29da7 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ The "REST with Spring" Classes ============================== -This is what I'm working on:
-**[>> THE REST WITH SPRING CLASSES](http://www.baeldung.com/rest-with-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=25off)** +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)** Spring Tutorials @@ -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/apache-fop/pom.xml b/apache-fop/pom.xml index 13fc2257cf..658d567a90 100644 --- a/apache-fop/pom.xml +++ b/apache-fop/pom.xml @@ -1,6 +1,6 @@ 4.0.0 - org.baeldung + com.baeldung apache-fop 0.1-SNAPSHOT @@ -37,7 +37,7 @@ junit - junit-dep + junit ${junit.version} test @@ -142,16 +142,12 @@ - - 4.1.5.RELEASE - 3.2.5.RELEASE - - 4.3.8.Final - 5.1.34 + 4.3.11.Final + 5.1.38 - 2.4.4 + 2.7.2 1.7.9 @@ -161,25 +157,25 @@ 5.1.3.Final - 17.0 + 19.0 3.3.2 1.3 - 4.11 + 4.12 1.10.19 4.4 4.4 - 2.4.0 + 2.9.0 - 3.2 + 3.5.1 2.6 - 2.18.1 + 2.19.1 2.7 - 1.4.12 + 1.4.18 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/.project b/core-java-8/.project index 235c555abb..a23ff42ae0 100644 --- a/core-java-8/.project +++ b/core-java-8/.project @@ -1,15 +1,10 @@ - core-java-8 + core-java8 - - org.eclipse.jdt.core.javabuilder - - - org.eclipse.m2e.core.maven2Builder @@ -17,8 +12,6 @@ - org.eclipse.jem.workbench.JavaEMFNature - org.eclipse.jdt.core.javanature org.eclipse.m2e.core.maven2Nature diff --git a/core-java-8/.settings/.jsdtscope b/core-java-8/.settings/.jsdtscope deleted file mode 100644 index 7b3f0c8b9f..0000000000 --- a/core-java-8/.settings/.jsdtscope +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/core-java-8/.settings/org.eclipse.jdt.core.prefs b/core-java-8/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index c57289fc09..0000000000 --- a/core-java-8/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,96 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignore -org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull -org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault -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.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 -org.eclipse.jdt.core.compiler.problem.comparingIdentical=warning -org.eclipse.jdt.core.compiler.problem.deadCode=warning -org.eclipse.jdt.core.compiler.problem.deprecation=warning -org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled -org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled -org.eclipse.jdt.core.compiler.problem.discouragedReference=warning -org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore -org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore -org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled -org.eclipse.jdt.core.compiler.problem.fieldHiding=error -org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning -org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning -org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning -org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts=disabled -org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning -org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=ignore -org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore -org.eclipse.jdt.core.compiler.problem.localVariableHiding=error -org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning -org.eclipse.jdt.core.compiler.problem.missingDefaultCase=ignore -org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore -org.eclipse.jdt.core.compiler.problem.missingEnumCaseDespiteDefault=disabled -org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=ignore -org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=ignore -org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled -org.eclipse.jdt.core.compiler.problem.missingSerialVersion=warning -org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignore -org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning -org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning -org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore -org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=error -org.eclipse.jdt.core.compiler.problem.nullReference=warning -org.eclipse.jdt.core.compiler.problem.nullSpecViolation=error -org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=warning -org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning -org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore -org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=ignore -org.eclipse.jdt.core.compiler.problem.potentialNullReference=ignore -org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=ignore -org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning -org.eclipse.jdt.core.compiler.problem.redundantNullAnnotation=warning -org.eclipse.jdt.core.compiler.problem.redundantNullCheck=ignore -org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=ignore -org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=ignore -org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore -org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=ignore -org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled -org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning -org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled -org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled -org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore -org.eclipse.jdt.core.compiler.problem.typeParameterHiding=error -org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=enabled -org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning -org.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning -org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore -org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning -org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore -org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=ignore -org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=ignore -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=enabled -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled -org.eclipse.jdt.core.compiler.problem.unusedImport=warning -org.eclipse.jdt.core.compiler.problem.unusedLabel=warning -org.eclipse.jdt.core.compiler.problem.unusedLocal=warning -org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=ignore -org.eclipse.jdt.core.compiler.problem.unusedParameter=ignore -org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference=enabled -org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled -org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled -org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning -org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning -org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning -org.eclipse.jdt.core.compiler.source=1.8 diff --git a/core-java-8/.settings/org.eclipse.jdt.ui.prefs b/core-java-8/.settings/org.eclipse.jdt.ui.prefs deleted file mode 100644 index d84d2a7f8c..0000000000 --- a/core-java-8/.settings/org.eclipse.jdt.ui.prefs +++ /dev/null @@ -1,57 +0,0 @@ -eclipse.preferences.version=1 -editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true -sp_cleanup.add_default_serial_version_id=true -sp_cleanup.add_generated_serial_version_id=false -sp_cleanup.add_missing_annotations=true -sp_cleanup.add_missing_deprecated_annotations=true -sp_cleanup.add_missing_methods=false -sp_cleanup.add_missing_nls_tags=false -sp_cleanup.add_missing_override_annotations=true -sp_cleanup.add_missing_override_annotations_interface_methods=true -sp_cleanup.add_serial_version_id=false -sp_cleanup.always_use_blocks=true -sp_cleanup.always_use_parentheses_in_expressions=true -sp_cleanup.always_use_this_for_non_static_field_access=false -sp_cleanup.always_use_this_for_non_static_method_access=false -sp_cleanup.convert_functional_interfaces=false -sp_cleanup.convert_to_enhanced_for_loop=true -sp_cleanup.correct_indentation=true -sp_cleanup.format_source_code=true -sp_cleanup.format_source_code_changes_only=true -sp_cleanup.make_local_variable_final=true -sp_cleanup.make_parameters_final=true -sp_cleanup.make_private_fields_final=false -sp_cleanup.make_type_abstract_if_missing_method=false -sp_cleanup.make_variable_declarations_final=true -sp_cleanup.never_use_blocks=false -sp_cleanup.never_use_parentheses_in_expressions=false -sp_cleanup.on_save_use_additional_actions=true -sp_cleanup.organize_imports=true -sp_cleanup.qualify_static_field_accesses_with_declaring_class=false -sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true -sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true -sp_cleanup.qualify_static_member_accesses_with_declaring_class=true -sp_cleanup.qualify_static_method_accesses_with_declaring_class=false -sp_cleanup.remove_private_constructors=true -sp_cleanup.remove_trailing_whitespaces=true -sp_cleanup.remove_trailing_whitespaces_all=true -sp_cleanup.remove_trailing_whitespaces_ignore_empty=false -sp_cleanup.remove_unnecessary_casts=true -sp_cleanup.remove_unnecessary_nls_tags=false -sp_cleanup.remove_unused_imports=true -sp_cleanup.remove_unused_local_variables=false -sp_cleanup.remove_unused_private_fields=true -sp_cleanup.remove_unused_private_members=false -sp_cleanup.remove_unused_private_methods=true -sp_cleanup.remove_unused_private_types=true -sp_cleanup.sort_members=false -sp_cleanup.sort_members_all=false -sp_cleanup.use_anonymous_class_creation=false -sp_cleanup.use_blocks=true -sp_cleanup.use_blocks_only_for_return_and_throw=false -sp_cleanup.use_lambda=false -sp_cleanup.use_parentheses_in_expressions=false -sp_cleanup.use_this_for_non_static_field_access=true -sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true -sp_cleanup.use_this_for_non_static_method_access=true -sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true diff --git a/core-java-8/.settings/org.eclipse.m2e.core.prefs b/core-java-8/.settings/org.eclipse.m2e.core.prefs deleted file mode 100644 index f897a7f1cb..0000000000 --- a/core-java-8/.settings/org.eclipse.m2e.core.prefs +++ /dev/null @@ -1,4 +0,0 @@ -activeProfiles= -eclipse.preferences.version=1 -resolveWorkspaceProjects=true -version=1 diff --git a/core-java-8/.springBeans b/core-java-8/.springBeans deleted file mode 100644 index a79097f40d..0000000000 --- a/core-java-8/.springBeans +++ /dev/null @@ -1,14 +0,0 @@ - - - 1 - - - - - - - src/main/webapp/WEB-INF/api-servlet.xml - - - - diff --git a/core-java-8/README.md b/core-java-8/README.md index 9bb6bb811c..b03d24c34e 100644 --- a/core-java-8/README.md +++ b/core-java-8/README.md @@ -3,4 +3,9 @@ ## Core Java 8 Cookbooks and Examples ### Relevant Articles: -// - [Java 8 – Powerful Comparison with Lambdas](http://www.baeldung.com/java-8-sort-lambda) +// - [Java 8 – Powerful Comparison with Lambdas](http://www.baeldung.com/java-8-sort-lambda) +- [Java – Directory Size](http://www.baeldung.com/java-folder-size) +- [Java – Try with Resources](http://www.baeldung.com/java-try-with-resources) +- [Lambda Expressions and Functional Interfaces: Tips and Best Practices](http://www.baeldung.com/java-8-lambda-expressions-tips) +- [The Double Colon Operator in Java 8](http://www.baeldung.com/java-8-double-colon-operator) +- [A Guide to the Java ExecutorService](http://www.baeldung.com/java-executor-service-tutorial) diff --git a/core-java-8/pom.xml b/core-java-8/pom.xml index 9db2562fb4..8c9bb36f7d 100644 --- a/core-java-8/pom.xml +++ b/core-java-8/pom.xml @@ -1,10 +1,11 @@ - + 4.0.0 - org.baeldung - spring-rest + com.baeldung + core-java8 0.1-SNAPSHOT - spring-rest + core-java8 @@ -40,6 +41,12 @@ 3.3.2 + + org.slf4j + slf4j-api + ${org.slf4j.version} + + @@ -56,6 +63,12 @@ test + + org.assertj + assertj-core + 3.4.1 + + org.mockito mockito-core @@ -98,14 +111,14 @@ - 1.7.12 + 1.7.13 1.0.13 5.1.3.Final - 18.0 + 19.0 3.4 @@ -114,9 +127,9 @@ 1.10.19 - 3.3 + 3.5.1 2.6 - 2.18.1 + 2.19.1 2.7 diff --git a/core-java-8/src/main/java/com/baeldung/Adder.java b/core-java-8/src/main/java/com/baeldung/Adder.java new file mode 100644 index 0000000000..e3e100f121 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/Adder.java @@ -0,0 +1,12 @@ +package com.baeldung; + +import java.util.function.Consumer; +import java.util.function.Function; + +public interface Adder { + + String addWithFunction(Function f); + + void addWithConsumer(Consumer f); + +} diff --git a/core-java-8/src/main/java/com/baeldung/AdderImpl.java b/core-java-8/src/main/java/com/baeldung/AdderImpl.java new file mode 100644 index 0000000000..f67cdc26b3 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/AdderImpl.java @@ -0,0 +1,17 @@ +package com.baeldung; + +import java.util.function.Consumer; +import java.util.function.Function; + +public class AdderImpl implements Adder { + + @Override + public String addWithFunction(final Function f) { + return f.apply("Something "); + } + + @Override + public void addWithConsumer(final Consumer f) { + } + +} diff --git a/core-java-8/src/main/java/com/baeldung/Bar.java b/core-java-8/src/main/java/com/baeldung/Bar.java new file mode 100644 index 0000000000..f9b6f2773e --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/Bar.java @@ -0,0 +1,12 @@ +package com.baeldung; + +@FunctionalInterface +public interface Bar { + + String method(String string); + + default String defaultMethod() { + return "String from Bar"; + } + +} diff --git a/core-java-8/src/main/java/com/baeldung/Baz.java b/core-java-8/src/main/java/com/baeldung/Baz.java new file mode 100644 index 0000000000..6d03f74198 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/Baz.java @@ -0,0 +1,11 @@ +package com.baeldung; + +@FunctionalInterface +public interface Baz { + + String method(String string); + + default String defaultMethod() { + return "String from Baz"; + } +} diff --git a/core-java-8/src/main/java/com/baeldung/Foo.java b/core-java-8/src/main/java/com/baeldung/Foo.java new file mode 100644 index 0000000000..90ebdfeed3 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/Foo.java @@ -0,0 +1,10 @@ +package com.baeldung; + +@FunctionalInterface +public interface Foo { + + String method(String string); + + default void defaultMethod() { + } +} diff --git a/core-java-8/src/main/java/com/baeldung/FooExtended.java b/core-java-8/src/main/java/com/baeldung/FooExtended.java new file mode 100644 index 0000000000..c8ed0c35dd --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/FooExtended.java @@ -0,0 +1,11 @@ +package com.baeldung; + +@FunctionalInterface +public interface FooExtended extends Baz, Bar { + + @Override + default String defaultMethod() { + return Bar.super.defaultMethod(); + } + +} diff --git a/core-java-8/src/main/java/com/baeldung/UseFoo.java b/core-java-8/src/main/java/com/baeldung/UseFoo.java new file mode 100644 index 0000000000..a91404ebaf --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/UseFoo.java @@ -0,0 +1,37 @@ +package com.baeldung; + +import java.util.function.Function; + +public class UseFoo { + + private String value = "Enclosing scope value"; + + public String add(final String string, final Foo foo) { + return foo.method(string); + } + + public String addWithStandardFI(final String string, final Function fn) { + return fn.apply(string); + } + + public String scopeExperiment() { + final Foo fooIC = new Foo() { + String value = "Inner class value"; + + @Override + public String method(final String string) { + return value; + } + }; + final String resultIC = fooIC.method(""); + + final Foo fooLambda = parameter -> { + final String value = "Lambda value"; + return this.value; + }; + final String resultLambda = fooLambda.method(""); + + return "Results: resultIC = " + resultIC + ", resultLambda = " + resultLambda; + } + +} diff --git a/core-java-8/src/main/java/com/baeldung/datetime/UseDuration.java b/core-java-8/src/main/java/com/baeldung/datetime/UseDuration.java new file mode 100644 index 0000000000..125b6fbe38 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/datetime/UseDuration.java @@ -0,0 +1,16 @@ +package com.baeldung.datetime; + +import java.time.Duration; +import java.time.LocalTime; +import java.time.Period; + +public class UseDuration { + + public LocalTime modifyDates(LocalTime localTime,Duration duration){ + return localTime.plus(duration); + } + + public Duration getDifferenceBetweenDates(LocalTime localTime1,LocalTime localTime2){ + return Duration.between(localTime1, localTime2); + } +} diff --git a/core-java-8/src/main/java/com/baeldung/datetime/UseLocalDate.java b/core-java-8/src/main/java/com/baeldung/datetime/UseLocalDate.java new file mode 100644 index 0000000000..47b1b3f67d --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/datetime/UseLocalDate.java @@ -0,0 +1,46 @@ +package com.baeldung.datetime; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalAdjusters; + +public class UseLocalDate { + + public LocalDate getLocalDateUsingFactoryOfMethod(int year, int month, int dayOfMonth){ + return LocalDate.of(year, month, dayOfMonth); + } + + public LocalDate getLocalDateUsingParseMethod(String representation){ + return LocalDate.parse(representation); + } + + public LocalDate getLocalDateFromClock(){ + LocalDate localDate = LocalDate.now(); + return localDate; + } + + public LocalDate getNextDay(LocalDate localDate){ + return localDate.plusDays(1); + } + + public LocalDate getPreviousDay(LocalDate localDate){ + return localDate.minus(1, ChronoUnit.DAYS); + } + + public DayOfWeek getDayOfWeek(LocalDate localDate){ + DayOfWeek day = localDate.getDayOfWeek(); + return day; + } + + public LocalDate getFirstDayOfMonth(){ + LocalDate firstDayOfMonth = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()); + return firstDayOfMonth; + } + + public LocalDateTime getStartOfDay(LocalDate localDate){ + LocalDateTime startofDay = localDate.atStartOfDay(); + return startofDay; + } +} diff --git a/core-java-8/src/main/java/com/baeldung/datetime/UseLocalDateTime.java b/core-java-8/src/main/java/com/baeldung/datetime/UseLocalDateTime.java new file mode 100644 index 0000000000..7aa1eaa276 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/datetime/UseLocalDateTime.java @@ -0,0 +1,11 @@ +package com.baeldung.datetime; + +import java.time.LocalDateTime; + +public class UseLocalDateTime { + + public LocalDateTime getLocalDateTimeUsingParseMethod(String representation){ + return LocalDateTime.parse(representation); + } + +} diff --git a/core-java-8/src/main/java/com/baeldung/datetime/UseLocalTime.java b/core-java-8/src/main/java/com/baeldung/datetime/UseLocalTime.java new file mode 100644 index 0000000000..e13fd10d6f --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/datetime/UseLocalTime.java @@ -0,0 +1,35 @@ +package com.baeldung.datetime; + +import java.time.LocalTime; +import java.time.temporal.ChronoUnit; + +public class UseLocalTime { + + public LocalTime getLocalTimeUsingFactoryOfMethod(int hour, int min, int seconds){ + LocalTime localTime = LocalTime.of(hour, min, seconds); + return localTime; + } + + public LocalTime getLocalTimeUsingParseMethod(String timeRepresentation){ + LocalTime localTime = LocalTime.parse(timeRepresentation); + return localTime; + } + + public LocalTime getLocalTimeFromClock(){ + LocalTime localTime = LocalTime.now(); + return localTime; + } + + public LocalTime addAnHour(LocalTime localTime){ + LocalTime newTime = localTime.plus(1,ChronoUnit.HOURS); + return newTime; + } + + public int getHourFromLocalTime(LocalTime localTime){ + return localTime.getHour(); + } + + public LocalTime getLocalTimeWithMinuteSetToValue(LocalTime localTime, int minute){ + return localTime.withMinute(minute); + } +} diff --git a/core-java-8/src/main/java/com/baeldung/datetime/UsePeriod.java b/core-java-8/src/main/java/com/baeldung/datetime/UsePeriod.java new file mode 100644 index 0000000000..326cfad650 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/datetime/UsePeriod.java @@ -0,0 +1,15 @@ +package com.baeldung.datetime; + +import java.time.LocalDate; +import java.time.Period; + +public class UsePeriod { + + public LocalDate modifyDates(LocalDate localDate,Period period){ + return localDate.plus(period); + } + + public Period getDifferenceBetweenDates(LocalDate localDate1,LocalDate localDate2){ + return Period.between(localDate1, localDate2); + } +} diff --git a/core-java-8/src/main/java/com/baeldung/datetime/UseToInstant.java b/core-java-8/src/main/java/com/baeldung/datetime/UseToInstant.java new file mode 100644 index 0000000000..1ddb096cf6 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/datetime/UseToInstant.java @@ -0,0 +1,19 @@ +package com.baeldung.datetime; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.util.Calendar; +import java.util.Date; + +public class UseToInstant { + + public LocalDateTime convertDateToLocalDate(Date date){ + LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); + return localDateTime; + } + + public LocalDateTime convertDateToLocalDate(Calendar calendar){ + LocalDateTime localDateTime = LocalDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault()); + return localDateTime; + } +} diff --git a/core-java-8/src/main/java/com/baeldung/datetime/UseZonedDateTime.java b/core-java-8/src/main/java/com/baeldung/datetime/UseZonedDateTime.java new file mode 100644 index 0000000000..0369de9835 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/datetime/UseZonedDateTime.java @@ -0,0 +1,13 @@ +package com.baeldung.datetime; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; + +public class UseZonedDateTime { + + public ZonedDateTime getZonedDateTime(LocalDateTime localDateTime,ZoneId zoneId){ + ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId); + return zonedDateTime; + } +} diff --git a/core-java-8/src/main/java/com/baeldung/doublecolon/Computer.java b/core-java-8/src/main/java/com/baeldung/doublecolon/Computer.java new file mode 100644 index 0000000000..b5d2e70abd --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/doublecolon/Computer.java @@ -0,0 +1,88 @@ +package com.baeldung.doublecolon; + +public class Computer { + + private Integer age; + private String color; + private Integer healty; + + public Computer(final int age, final String color) { + this.age = age; + this.color = color; + } + + public Computer(final Integer age, final String color, final Integer healty) { + this.age = age; + this.color = color; + this.healty = healty; + } + + public Computer() { + } + + public Integer getAge() { + return age; + } + + public void setAge(final Integer age) { + this.age = age; + } + + public String getColor() { + return color; + } + + public void setColor(final String color) { + this.color = color; + } + + public Integer getHealty() { + return healty; + } + + public void setHealty(final Integer healty) { + this.healty = healty; + } + + public void turnOnPc() { + System.out.println("Computer turned on"); + } + + public void turnOffPc() { + System.out.println("Computer turned off"); + } + + public Double calculateValue(Double initialValue) { + return initialValue / 1.50; + } + + @Override + public String toString() { + return "Computer{" + "age=" + age + ", color='" + color + '\'' + ", healty=" + healty + '}'; + } + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + final Computer computer = (Computer) o; + + if (age != null ? !age.equals(computer.age) : computer.age != null) { + return false; + } + return color != null ? color.equals(computer.color) : computer.color == null; + + } + + @Override + public int hashCode() { + int result = age != null ? age.hashCode() : 0; + result = 31 * result + (color != null ? color.hashCode() : 0); + return result; + } +} diff --git a/core-java-8/src/main/java/com/baeldung/doublecolon/ComputerUtils.java b/core-java-8/src/main/java/com/baeldung/doublecolon/ComputerUtils.java new file mode 100644 index 0000000000..d181dfcdf7 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/doublecolon/ComputerUtils.java @@ -0,0 +1,27 @@ +package com.baeldung.doublecolon; + +import com.baeldung.doublecolon.function.ComputerPredicate; + +import java.util.ArrayList; +import java.util.List; + +public class ComputerUtils { + + public static final ComputerPredicate after2010Predicate = (c) -> (c.getAge() > 2010); + public static final ComputerPredicate blackPredicate = (c) -> "black".equals(c.getColor()); + + public static List filter(final List inventory, final ComputerPredicate p) { + + final List result = new ArrayList<>(); + inventory.stream().filter(p::filter).forEach(result::add); + + return result; + } + + public static void repair(final Computer computer) { + if (computer.getHealty() < 50) { + computer.setHealty(100); + } + } + +} diff --git a/core-java-8/src/main/java/com/baeldung/doublecolon/MacbookPro.java b/core-java-8/src/main/java/com/baeldung/doublecolon/MacbookPro.java new file mode 100644 index 0000000000..3fc459c681 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/doublecolon/MacbookPro.java @@ -0,0 +1,34 @@ +package com.baeldung.doublecolon; + +import java.util.function.Function; + +public class MacbookPro extends Computer { + + public MacbookPro(int age, String color) { + super(age, color); + } + + public MacbookPro(Integer age, String color, Integer healty) { + super(age, color, healty); + } + + @Override + public void turnOnPc() { + System.out.println("MacbookPro turned on"); + } + + @Override + public void turnOffPc() { + System.out.println("MacbookPro turned off"); + } + + @Override + public Double calculateValue(Double initialValue) { + + Function function = super::calculateValue; + final Double pcValue = function.apply(initialValue); + System.out.println("First value is:" + pcValue); + return pcValue + (initialValue / 10); + + } +} diff --git a/core-java-8/src/main/java/com/baeldung/doublecolon/function/ComputerPredicate.java b/core-java-8/src/main/java/com/baeldung/doublecolon/function/ComputerPredicate.java new file mode 100644 index 0000000000..5ce491e86a --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/doublecolon/function/ComputerPredicate.java @@ -0,0 +1,10 @@ +package com.baeldung.doublecolon.function; + +import com.baeldung.doublecolon.Computer; + +@FunctionalInterface +public interface ComputerPredicate { + + boolean filter(Computer c); + +} diff --git a/core-java-8/src/main/java/com/baeldung/doublecolon/function/TriFunction.java b/core-java-8/src/main/java/com/baeldung/doublecolon/function/TriFunction.java new file mode 100644 index 0000000000..d0d6299479 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/doublecolon/function/TriFunction.java @@ -0,0 +1,15 @@ +package com.baeldung.doublecolon.function; + +import java.util.Objects; +import java.util.function.Function; + +@FunctionalInterface +public interface TriFunction { + + R apply(A a, B b, C c); + + default TriFunction andThen(final Function after) { + Objects.requireNonNull(after); + return (final A a, final B b, final C c) -> after.apply(apply(a, b, c)); + } +} \ No newline at end of file diff --git a/core-java-8/src/main/java/com/baeldung/enums/Pizza.java b/core-java-8/src/main/java/com/baeldung/enums/Pizza.java new file mode 100644 index 0000000000..5bc2d9a9eb --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/enums/Pizza.java @@ -0,0 +1,91 @@ +package com.baeldung.enums; + +import java.util.EnumMap; +import java.util.EnumSet; +import java.util.List; +import java.util.stream.Collectors; + +public class Pizza { + + private static EnumSet deliveredPizzaStatuses = + EnumSet.of(PizzaStatusEnum.DELIVERED); + + private PizzaStatusEnum status; + + public enum PizzaStatusEnum { + ORDERED(5) { + @Override + public boolean isOrdered() { + return true; + } + }, + READY(2) { + @Override + public boolean isReady() { + return true; + } + }, + DELIVERED(0) { + @Override + public boolean isDelivered() { + return true; + } + }; + + private int timeToDelivery; + + public boolean isOrdered() { + return false; + } + + public boolean isReady() { + return false; + } + + public boolean isDelivered() { + return false; + } + + public int getTimeToDelivery() { + return timeToDelivery; + } + + PizzaStatusEnum(int timeToDelivery) { + this.timeToDelivery = timeToDelivery; + } + } + + public PizzaStatusEnum getStatus() { + return status; + } + + public void setStatus(PizzaStatusEnum status) { + this.status = status; + } + + public boolean isDeliverable() { + return this.status.isReady(); + } + + public void printTimeToDeliver() { + System.out.println("Time to delivery is " + this.getStatus().getTimeToDelivery() + " days"); + } + + public static List getAllUndeliveredPizzas(List input) { + return input.stream().filter((s) -> !deliveredPizzaStatuses.contains(s.getStatus())).collect(Collectors.toList()); + } + + public static EnumMap> groupPizzaByStatus(List pzList) { + return pzList.stream().collect( + Collectors.groupingBy(Pizza::getStatus, + () -> new EnumMap<>(PizzaStatusEnum.class), Collectors.toList())); + } + + public void deliver() { + if (isDeliverable()) { + PizzaDeliverySystemConfiguration.getInstance().getDeliveryStrategy().deliver(this); + this.setStatus(PizzaStatusEnum.DELIVERED); + } + } + +} \ No newline at end of file diff --git a/core-java-8/src/main/java/com/baeldung/enums/PizzaDeliveryStrategy.java b/core-java-8/src/main/java/com/baeldung/enums/PizzaDeliveryStrategy.java new file mode 100644 index 0000000000..ed65919387 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/enums/PizzaDeliveryStrategy.java @@ -0,0 +1,18 @@ +package com.baeldung.enums; + +public enum PizzaDeliveryStrategy { + EXPRESS { + @Override + public void deliver(Pizza pz) { + System.out.println("Pizza will be delivered in express mode"); + } + }, + NORMAL { + @Override + public void deliver(Pizza pz) { + System.out.println("Pizza will be delivered in normal mode"); + } + }; + + public abstract void deliver(Pizza pz); +} diff --git a/core-java-8/src/main/java/com/baeldung/enums/PizzaDeliverySystemConfiguration.java b/core-java-8/src/main/java/com/baeldung/enums/PizzaDeliverySystemConfiguration.java new file mode 100644 index 0000000000..5ccff5e959 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/enums/PizzaDeliverySystemConfiguration.java @@ -0,0 +1,22 @@ +package com.baeldung.enums; + + +public enum PizzaDeliverySystemConfiguration { + INSTANCE; + + PizzaDeliverySystemConfiguration() { + // Do the configuration initialization which + // involves overriding defaults like delivery strategy + } + + private PizzaDeliveryStrategy deliveryStrategy = PizzaDeliveryStrategy.NORMAL; + + public static PizzaDeliverySystemConfiguration getInstance() { + return INSTANCE; + } + + public PizzaDeliveryStrategy getDeliveryStrategy() { + return deliveryStrategy; + } + +} diff --git a/core-java-8/src/main/java/com/baeldung/forkjoin/CustomRecursiveAction.java b/core-java-8/src/main/java/com/baeldung/forkjoin/CustomRecursiveAction.java new file mode 100644 index 0000000000..f1ab2d8d09 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/forkjoin/CustomRecursiveAction.java @@ -0,0 +1,49 @@ +package com.baeldung.forkjoin; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.concurrent.ForkJoinTask; +import java.util.concurrent.RecursiveAction; +import java.util.logging.Logger; + +public class CustomRecursiveAction extends RecursiveAction { + + private String workLoad = ""; + private static final int THRESHOLD = 4; + + private static Logger logger = Logger.getAnonymousLogger(); + + public CustomRecursiveAction(String workLoad) { + this.workLoad = workLoad; + } + + @Override + protected void compute() { + + if (workLoad.length() > THRESHOLD) { + ForkJoinTask.invokeAll(createSubtasks()); + } else { + processing(workLoad); + } + } + + private Collection createSubtasks() { + + List subtasks = + new ArrayList<>(); + + String partOne = workLoad.substring(0, workLoad.length() / 2); + String partTwo = workLoad.substring(workLoad.length() / 2, workLoad.length()); + + subtasks.add(new CustomRecursiveAction(partOne)); + subtasks.add(new CustomRecursiveAction(partTwo)); + + return subtasks; + } + + private void processing(String work) { + String result = work.toUpperCase(); + logger.info("This result - (" + result + ") - was processed by " + Thread.currentThread().getName()); + } +} diff --git a/core-java-8/src/main/java/com/baeldung/forkjoin/CustomRecursiveTask.java b/core-java-8/src/main/java/com/baeldung/forkjoin/CustomRecursiveTask.java new file mode 100644 index 0000000000..5d4d97b805 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/forkjoin/CustomRecursiveTask.java @@ -0,0 +1,51 @@ +package com.baeldung.forkjoin; + + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.concurrent.ForkJoinTask; +import java.util.concurrent.RecursiveTask; + +public class CustomRecursiveTask extends RecursiveTask { + + private int[] arr; + + private static final int THRESHOLD = 20; + + public CustomRecursiveTask(int[] arr) { + this.arr = arr; + } + + @Override + protected Integer compute() { + + if (arr.length > THRESHOLD) { + + return ForkJoinTask.invokeAll(createSubtasks()) + .stream() + .mapToInt(ForkJoinTask::join) + .sum(); + + } else { + return processing(arr); + } + } + + private Collection createSubtasks() { + List dividedTasks = new ArrayList<>(); + dividedTasks.add(new CustomRecursiveTask( + Arrays.copyOfRange(arr, 0, arr.length / 2))); + dividedTasks.add(new CustomRecursiveTask( + Arrays.copyOfRange(arr, arr.length / 2, arr.length))); + return dividedTasks; + } + + private Integer processing(int[] arr) { + return Arrays.stream(arr) + .filter(a -> a > 10 && a < 27) + .map(a -> a * 10) + .sum(); + } +} diff --git a/core-java-8/src/main/java/com/baeldung/forkjoin/util/PoolUtil.java b/core-java-8/src/main/java/com/baeldung/forkjoin/util/PoolUtil.java new file mode 100644 index 0000000000..521616600f --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/forkjoin/util/PoolUtil.java @@ -0,0 +1,10 @@ +package com.baeldung.forkjoin.util; + + +import java.util.concurrent.ForkJoinPool; + +public class PoolUtil { + + public static ForkJoinPool forkJoinPool = new ForkJoinPool(2); + +} diff --git a/core-java-8/src/main/java/com/baeldung/java_8_features/Address.java b/core-java-8/src/main/java/com/baeldung/java_8_features/Address.java new file mode 100644 index 0000000000..1f89503288 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/java_8_features/Address.java @@ -0,0 +1,14 @@ +package com.baeldung.java_8_features; + +public class Address { + + private String street; + + public String getStreet() { + return street; + } + + public void setStreet(String street) { + this.street = street; + } +} diff --git a/core-java-8/src/main/java/com/baeldung/java_8_features/CustomException.java b/core-java-8/src/main/java/com/baeldung/java_8_features/CustomException.java new file mode 100644 index 0000000000..ff9be6ab06 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/java_8_features/CustomException.java @@ -0,0 +1,4 @@ +package com.baeldung.java_8_features; + +public class CustomException extends RuntimeException { +} diff --git a/core-java-8/src/main/java/com/baeldung/java_8_features/Detail.java b/core-java-8/src/main/java/com/baeldung/java_8_features/Detail.java new file mode 100644 index 0000000000..811937dba7 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/java_8_features/Detail.java @@ -0,0 +1,13 @@ +package com.baeldung.java_8_features; + +import java.util.Arrays; +import java.util.List; + +public class Detail { + + private static final List PARTS = Arrays.asList("turbine", "pump"); + + public List getParts() { + return PARTS; + } +} diff --git a/core-java-8/src/main/java/com/baeldung/java_8_features/OptionalAddress.java b/core-java-8/src/main/java/com/baeldung/java_8_features/OptionalAddress.java new file mode 100644 index 0000000000..8d6c517ac5 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/java_8_features/OptionalAddress.java @@ -0,0 +1,16 @@ +package com.baeldung.java_8_features; + +import java.util.Optional; + +public class OptionalAddress { + + private String street; + + public Optional getStreet() { + return Optional.ofNullable(street); + } + + public void setStreet(String street) { + this.street = street; + } +} diff --git a/core-java-8/src/main/java/com/baeldung/java_8_features/OptionalUser.java b/core-java-8/src/main/java/com/baeldung/java_8_features/OptionalUser.java new file mode 100644 index 0000000000..ff06cd21d6 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/java_8_features/OptionalUser.java @@ -0,0 +1,16 @@ +package com.baeldung.java_8_features; + +import java.util.Optional; + +public class OptionalUser { + + private OptionalAddress address; + + public Optional getAddress() { + return Optional.of(address); + } + + public void setAddress(OptionalAddress address) { + this.address = address; + } +} diff --git a/core-java-8/src/main/java/com/baeldung/java_8_features/User.java b/core-java-8/src/main/java/com/baeldung/java_8_features/User.java new file mode 100644 index 0000000000..3708d276c8 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/java_8_features/User.java @@ -0,0 +1,40 @@ +package com.baeldung.java_8_features; + +import java.util.Optional; + +public class User { + + private String name; + + private Address address; + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } + + public User() { + } + + public User(String name) { + this.name = name; + } + + public static boolean isRealUser(User user) { + return true; + } + + public String getOrThrow() { + String value = null; + Optional valueOpt = Optional.ofNullable(value); + String result = valueOpt.orElseThrow(CustomException::new).toUpperCase(); + return result; + } + + public boolean isLegalName(String name) { + return name.length() > 3 && name.length() < 16; + } +} diff --git a/core-java-8/src/main/java/com/baeldung/java_8_features/Vehicle.java b/core-java-8/src/main/java/com/baeldung/java_8_features/Vehicle.java new file mode 100644 index 0000000000..011173bcaf --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/java_8_features/Vehicle.java @@ -0,0 +1,18 @@ +package com.baeldung.java_8_features; + +public interface Vehicle { + + void moveTo(long altitude, long longitude); + + static String producer() { + return "N&F Vehicles"; + } + + default long[] startPosition() { + return new long[]{23, 15}; + } + + default String getOverview() { + return "ATV made by " + producer(); + } +} diff --git a/core-java-8/src/main/java/com/baeldung/java_8_features/VehicleImpl.java b/core-java-8/src/main/java/com/baeldung/java_8_features/VehicleImpl.java new file mode 100644 index 0000000000..83e55f5f4d --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/java_8_features/VehicleImpl.java @@ -0,0 +1,9 @@ +package com.baeldung.java_8_features; + +public class VehicleImpl implements Vehicle { + + @Override + public void moveTo(long altitude, long longitude) { + //do nothing + } +} diff --git a/core-java-8/src/main/java/com/baeldung/streamApi/Product.java b/core-java-8/src/main/java/com/baeldung/streamApi/Product.java new file mode 100644 index 0000000000..18f3a61904 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/streamApi/Product.java @@ -0,0 +1,51 @@ +package com.baeldung.streamApi; + +import java.util.List; +import java.util.Optional; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +/** + * Created by Alex Vengr + */ +public class Product { + + private int price; + + private String name; + + private boolean utilize; + + public Product(int price, String name) { + this(price); + this.name = name; + } + + public Product(int price) { + this.price = price; + } + + public Product() { + } + + public int getPrice() { + return price; + } + + public void setPrice(int price) { + this.price = price; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + + public static Stream streamOf(List list) { + return (list == null || list.isEmpty()) ? Stream.empty() : list.stream(); + } +} diff --git a/core-java-8/src/main/java/com/baeldung/unzip/UnzipFile.java b/core-java-8/src/main/java/com/baeldung/unzip/UnzipFile.java new file mode 100644 index 0000000000..6648d5f926 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/unzip/UnzipFile.java @@ -0,0 +1,30 @@ +package com.baeldung.unzip; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +public class UnzipFile { + public static void main(final String[] args) throws IOException { + final String fileZip = "src/main/resources/compressed.zip"; + final byte[] buffer = new byte[1024]; + final ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip)); + ZipEntry zipEntry = zis.getNextEntry(); + while (zipEntry != null) { + final String fileName = zipEntry.getName(); + final File newFile = new File("src/main/resources/unzipTest/" + fileName); + final FileOutputStream fos = new FileOutputStream(newFile); + int len; + while ((len = zis.read(buffer)) > 0) { + fos.write(buffer, 0, len); + } + fos.close(); + zipEntry = zis.getNextEntry(); + } + zis.closeEntry(); + zis.close(); + } +} \ No newline at end of file diff --git a/core-java-8/src/main/java/com/baeldung/zip/ZipDirectory.java b/core-java-8/src/main/java/com/baeldung/zip/ZipDirectory.java new file mode 100644 index 0000000000..7da71a093d --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/zip/ZipDirectory.java @@ -0,0 +1,43 @@ +package com.baeldung.zip; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +public class ZipDirectory { + public static void main(final String[] args) throws IOException { + final String sourceFile = "src/main/resources/zipTest"; + final FileOutputStream fos = new FileOutputStream("src/main/resources/dirCompressed.zip"); + final ZipOutputStream zipOut = new ZipOutputStream(fos); + final File fileToZip = new File(sourceFile); + + zipFile(fileToZip, fileToZip.getName(), zipOut); + zipOut.close(); + fos.close(); + } + + private static void zipFile(final File fileToZip, final String fileName, final ZipOutputStream zipOut) throws IOException { + if (fileToZip.isHidden()) { + return; + } + if (fileToZip.isDirectory()) { + final File[] children = fileToZip.listFiles(); + for (final File childFile : children) { + zipFile(childFile, fileName + "/" + childFile.getName(), zipOut); + } + return; + } + final FileInputStream fis = new FileInputStream(fileToZip); + final ZipEntry zipEntry = new ZipEntry(fileName); + zipOut.putNextEntry(zipEntry); + final byte[] bytes = new byte[1024]; + int length; + while ((length = fis.read(bytes)) >= 0) { + zipOut.write(bytes, 0, length); + } + fis.close(); + } +} \ No newline at end of file diff --git a/core-java-8/src/main/java/com/baeldung/zip/ZipFile.java b/core-java-8/src/main/java/com/baeldung/zip/ZipFile.java new file mode 100644 index 0000000000..f9ccac9713 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/zip/ZipFile.java @@ -0,0 +1,28 @@ +package com.baeldung.zip; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +public class ZipFile { + public static void main(final String[] args) throws IOException { + final String sourceFile = "src/main/resources/zipTest/test1.txt"; + final FileOutputStream fos = new FileOutputStream("src/main/resources/compressed.zip"); + final ZipOutputStream zipOut = new ZipOutputStream(fos); + final File fileToZip = new File(sourceFile); + final FileInputStream fis = new FileInputStream(fileToZip); + final ZipEntry zipEntry = new ZipEntry(fileToZip.getName()); + zipOut.putNextEntry(zipEntry); + final byte[] bytes = new byte[1024]; + int length; + while ((length = fis.read(bytes)) >= 0) { + zipOut.write(bytes, 0, length); + } + zipOut.close(); + fis.close(); + fos.close(); + } +} \ No newline at end of file diff --git a/core-java-8/src/main/java/com/baeldung/zip/ZipMultipleFiles.java b/core-java-8/src/main/java/com/baeldung/zip/ZipMultipleFiles.java new file mode 100644 index 0000000000..fc86147e43 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/zip/ZipMultipleFiles.java @@ -0,0 +1,33 @@ +package com.baeldung.zip; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +public class ZipMultipleFiles { + public static void main(final String[] args) throws IOException { + final List srcFiles = Arrays.asList("src/main/resources/zipTest/test1.txt", "src/main/resources/zipTest/test2.txt"); + final FileOutputStream fos = new FileOutputStream("src/main/resources/multiCompressed.zip"); + final ZipOutputStream zipOut = new ZipOutputStream(fos); + for (final String srcFile : srcFiles) { + final File fileToZip = new File(srcFile); + final FileInputStream fis = new FileInputStream(fileToZip); + final ZipEntry zipEntry = new ZipEntry(fileToZip.getName()); + zipOut.putNextEntry(zipEntry); + + final byte[] bytes = new byte[1024]; + int length; + while ((length = fis.read(bytes)) >= 0) { + zipOut.write(bytes, 0, length); + } + fis.close(); + } + zipOut.close(); + fos.close(); + } +} \ No newline at end of file diff --git a/core-java-8/src/main/resources/compressed.zip b/core-java-8/src/main/resources/compressed.zip new file mode 100644 index 0000000000..03f840ae2b Binary files /dev/null and b/core-java-8/src/main/resources/compressed.zip differ diff --git a/core-java-8/src/main/resources/dirCompressed.zip b/core-java-8/src/main/resources/dirCompressed.zip new file mode 100644 index 0000000000..f42d3aa5c6 Binary files /dev/null and b/core-java-8/src/main/resources/dirCompressed.zip differ diff --git a/core-java-8/src/main/resources/logback.xml b/core-java-8/src/main/resources/logback.xml index 62d0ea5037..eefdc7a337 100644 --- a/core-java-8/src/main/resources/logback.xml +++ b/core-java-8/src/main/resources/logback.xml @@ -10,7 +10,7 @@ - + \ No newline at end of file diff --git a/core-java-8/src/main/resources/multiCompressed.zip b/core-java-8/src/main/resources/multiCompressed.zip new file mode 100644 index 0000000000..002e70ef81 Binary files /dev/null and b/core-java-8/src/main/resources/multiCompressed.zip differ diff --git a/core-java-8/src/main/resources/unzipTest/test1.txt b/core-java-8/src/main/resources/unzipTest/test1.txt new file mode 100644 index 0000000000..c57eff55eb --- /dev/null +++ b/core-java-8/src/main/resources/unzipTest/test1.txt @@ -0,0 +1 @@ +Hello World! \ No newline at end of file diff --git a/core-java-8/src/main/resources/zipTest/test1.txt b/core-java-8/src/main/resources/zipTest/test1.txt new file mode 100644 index 0000000000..c57eff55eb --- /dev/null +++ b/core-java-8/src/main/resources/zipTest/test1.txt @@ -0,0 +1 @@ +Hello World! \ No newline at end of file diff --git a/core-java-8/src/main/resources/zipTest/test2.txt b/core-java-8/src/main/resources/zipTest/test2.txt new file mode 100644 index 0000000000..f0fb0f14d1 --- /dev/null +++ b/core-java-8/src/main/resources/zipTest/test2.txt @@ -0,0 +1 @@ +My Name is John \ No newline at end of file diff --git a/core-java-8/src/main/resources/zipTest/testFolder/test3.txt b/core-java-8/src/main/resources/zipTest/testFolder/test3.txt new file mode 100644 index 0000000000..882edb168e --- /dev/null +++ b/core-java-8/src/main/resources/zipTest/testFolder/test3.txt @@ -0,0 +1 @@ +My Name is Tom \ No newline at end of file diff --git a/core-java-8/src/main/resources/zipTest/testFolder/test4.txt b/core-java-8/src/main/resources/zipTest/testFolder/test4.txt new file mode 100644 index 0000000000..a78c3fadc8 --- /dev/null +++ b/core-java-8/src/main/resources/zipTest/testFolder/test4.txt @@ -0,0 +1 @@ +My Name is Jane \ No newline at end of file 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-8/src/test/java/com/baeldung/datetime/UseLocalDateTest.java b/core-java-8/src/test/java/com/baeldung/datetime/UseLocalDateTest.java new file mode 100644 index 0000000000..8af33393be --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/datetime/UseLocalDateTest.java @@ -0,0 +1,55 @@ +package com.baeldung.datetime; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.Month; + +import org.junit.Assert; +import org.junit.Test; + +public class UseLocalDateTest { + + UseLocalDate useLocalDate = new UseLocalDate(); + + @Test + public void givenValues_whenUsingFactoryOf_thenLocalDate(){ + Assert.assertEquals("2016-05-10",useLocalDate.getLocalDateUsingFactoryOfMethod(2016,5,10).toString()); + } + + @Test + public void givenString_whenUsingParse_thenLocalDate(){ + Assert.assertEquals("2016-05-10",useLocalDate.getLocalDateUsingParseMethod("2016-05-10").toString()); + } + + @Test + public void whenUsingClock_thenLocalDate(){ + Assert.assertEquals(LocalDate.now(),useLocalDate.getLocalDateFromClock()); + } + + @Test + public void givenDate_whenUsingPlus_thenNextDay(){ + Assert.assertEquals(LocalDate.now().plusDays(1),useLocalDate.getNextDay(LocalDate.now())); + } + + @Test + public void givenDate_whenUsingMinus_thenPreviousDay(){ + Assert.assertEquals(LocalDate.now().minusDays(1),useLocalDate.getPreviousDay(LocalDate.now())); + } + + @Test + public void givenToday_whenUsingGetDayOfWeek_thenDayOfWeek(){ + Assert.assertEquals(DayOfWeek.SUNDAY,useLocalDate.getDayOfWeek(LocalDate.parse("2016-05-22"))); + } + + @Test + public void givenToday_whenUsingWithTemporalAdjuster_thenFirstDayOfMonth(){ + Assert.assertEquals(1,useLocalDate.getFirstDayOfMonth().getDayOfMonth()); + } + + @Test + public void givenLocalDate_whenUsingAtStartOfDay_thenReturnMidnight(){ + Assert.assertEquals(LocalDateTime.parse("2016-05-22T00:00:00"),useLocalDate.getStartOfDay(LocalDate.parse("2016-05-22"))); + } + +} diff --git a/core-java-8/src/test/java/com/baeldung/datetime/UseLocalDateTimeTest.java b/core-java-8/src/test/java/com/baeldung/datetime/UseLocalDateTimeTest.java new file mode 100644 index 0000000000..69a289fd02 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/datetime/UseLocalDateTimeTest.java @@ -0,0 +1,19 @@ +package com.baeldung.datetime; + +import java.time.LocalDate; +import java.time.LocalTime; +import java.time.Month; + +import org.junit.Assert; +import org.junit.Test; + +public class UseLocalDateTimeTest { + + UseLocalDateTime useLocalDateTime = new UseLocalDateTime(); + + @Test + public void givenString_whenUsingParse_thenLocalDateTime(){ + Assert.assertEquals(LocalDate.of(2016, Month.MAY, 10),useLocalDateTime.getLocalDateTimeUsingParseMethod("2016-05-10T06:30").toLocalDate()); + Assert.assertEquals(LocalTime.of(6,30),useLocalDateTime.getLocalDateTimeUsingParseMethod("2016-05-10T06:30").toLocalTime()); + } +} diff --git a/core-java-8/src/test/java/com/baeldung/datetime/UseLocalTimeTest.java b/core-java-8/src/test/java/com/baeldung/datetime/UseLocalTimeTest.java new file mode 100644 index 0000000000..7776fad363 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/datetime/UseLocalTimeTest.java @@ -0,0 +1,36 @@ +package com.baeldung.datetime; + +import java.time.LocalTime; + +import org.junit.Assert; +import org.junit.Test; + +public class UseLocalTimeTest { + + UseLocalTime useLocalTime = new UseLocalTime(); + + @Test + public void givenValues_whenUsingFactoryOf_thenLocalTime(){ + Assert.assertEquals("07:07:07",useLocalTime.getLocalTimeUsingFactoryOfMethod(7,7,7).toString()); + } + + @Test + public void givenString_whenUsingParse_thenLocalTime(){ + Assert.assertEquals("06:30",useLocalTime.getLocalTimeUsingParseMethod("06:30").toString()); + } + + @Test + public void givenTime_whenAddHour_thenLocalTime(){ + Assert.assertEquals("07:30",useLocalTime.addAnHour(LocalTime.of(6,30)).toString()); + } + + @Test + public void getHourFromLocalTime(){ + Assert.assertEquals(1, useLocalTime.getHourFromLocalTime(LocalTime.of(1,1))); + } + + @Test + public void getLocalTimeWithMinuteSetToValue(){ + Assert.assertEquals(LocalTime.of(10, 20), useLocalTime.getLocalTimeWithMinuteSetToValue(LocalTime.of(10,10), 20)); + } +} diff --git a/core-java-8/src/test/java/com/baeldung/datetime/UsePeriodTest.java b/core-java-8/src/test/java/com/baeldung/datetime/UsePeriodTest.java new file mode 100644 index 0000000000..8a3228aaa5 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/datetime/UsePeriodTest.java @@ -0,0 +1,29 @@ +package com.baeldung.datetime; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.Period; +import java.time.ZoneId; +import java.time.ZonedDateTime; + +import org.junit.Assert; +import org.junit.Test; + +public class UsePeriodTest { + UsePeriod usingPeriod=new UsePeriod(); + + @Test + public void givenPeriodAndLocalDate_thenCalculateModifiedDate(){ + Period period = Period.ofDays(1); + LocalDate localDate = LocalDate.parse("2007-05-10"); + Assert.assertEquals(localDate.plusDays(1),usingPeriod.modifyDates(localDate, period)); + } + + @Test + public void givenDates_thenGetPeriod(){ + LocalDate localDate1 = LocalDate.parse("2007-05-10"); + LocalDate localDate2 = LocalDate.parse("2007-05-15"); + + Assert.assertEquals(Period.ofDays(5), usingPeriod.getDifferenceBetweenDates(localDate1, localDate2)); + } +} diff --git a/core-java-8/src/test/java/com/baeldung/datetime/UseZonedDateTimeTest.java b/core-java-8/src/test/java/com/baeldung/datetime/UseZonedDateTimeTest.java new file mode 100644 index 0000000000..5af01ad678 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/datetime/UseZonedDateTimeTest.java @@ -0,0 +1,20 @@ +package com.baeldung.datetime; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; + +import org.junit.Assert; +import org.junit.Test; + +public class UseZonedDateTimeTest { + + UseZonedDateTime zonedDateTime=new UseZonedDateTime(); + + @Test + public void givenZoneId_thenZonedDateTime(){ + ZoneId zoneId=ZoneId.of("Europe/Paris"); + ZonedDateTime zonedDatetime=zonedDateTime.getZonedDateTime(LocalDateTime.parse("2016-05-20T06:30"), zoneId); + Assert.assertEquals(zoneId,ZoneId.from(zonedDatetime)); + } +} diff --git a/core-java-8/src/test/java/com/baeldung/doublecolon/TestComputerUtils.java b/core-java-8/src/test/java/com/baeldung/doublecolon/TestComputerUtils.java new file mode 100644 index 0000000000..85194f5aa6 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/doublecolon/TestComputerUtils.java @@ -0,0 +1,91 @@ +package com.baeldung.doublecolon; + +import com.baeldung.doublecolon.function.TriFunction; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; +import java.util.function.BiFunction; + +import static com.baeldung.doublecolon.ComputerUtils.*; + +public class TestComputerUtils { + + @Before + public void setup() { + } + + @After + public void tearDown() { + } + + @Test + public void testConstructorReference() { + + Computer c1 = new Computer(2015, "white"); + Computer c2 = new Computer(2009, "black"); + Computer c3 = new Computer(2014, "black"); + + BiFunction c4Function = Computer::new; + Computer c4 = c4Function.apply(2013, "white"); + BiFunction c5Function = Computer::new; + Computer c5 = c5Function.apply(2010, "black"); + BiFunction c6Function = Computer::new; + Computer c6 = c6Function.apply(2008, "black"); + + List inventory = Arrays.asList(c1, c2, c3, c4, c5, c6); + + List blackComputer = filter(inventory, blackPredicate); + Assert.assertEquals("The black Computers are: ", blackComputer.size(), 4); + + List after2010Computer = filter(inventory, after2010Predicate); + Assert.assertEquals("The Computer bought after 2010 are: ", after2010Computer.size(), 3); + + List before2011Computer = filter(inventory, c -> c.getAge() < 2011); + Assert.assertEquals("The Computer bought before 2011 are: ", before2011Computer.size(), 3); + + inventory.sort(Comparator.comparing(Computer::getAge)); + + Assert.assertEquals("Oldest Computer in inventory", c6, inventory.get(0)); + + } + + @Test + public void testStaticMethodReference() { + + Computer c1 = new Computer(2015, "white", 35); + Computer c2 = new Computer(2009, "black", 65); + TriFunction c6Function = Computer::new; + Computer c3 = c6Function.apply(2008, "black", 90); + + List inventory = Arrays.asList(c1, c2, c3); + inventory.forEach(ComputerUtils::repair); + + Assert.assertEquals("Computer repaired", new Integer(100), c1.getHealty()); + } + + @Test + public void testInstanceMethodArbitraryObjectParticularType() { + + Computer c1 = new Computer(2015, "white", 35); + Computer c2 = new MacbookPro(2009, "black", 65); + List inventory = Arrays.asList(c1, c2); + inventory.forEach(Computer::turnOnPc); + + } + + @Test + public void testSuperMethodReference() { + + final TriFunction integerStringIntegerObjectTriFunction = MacbookPro::new; + final MacbookPro macbookPro = integerStringIntegerObjectTriFunction.apply(2010, "black", 100); + Double initialValue = new Double(999.99); + final Double actualValue = macbookPro.calculateValue(initialValue); + Assert.assertEquals(766.659, actualValue, 0.0); + } + +} diff --git a/core-java-8/src/test/java/com/baeldung/enums/PizzaTest.java b/core-java-8/src/test/java/com/baeldung/enums/PizzaTest.java new file mode 100644 index 0000000000..deeebaa240 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/enums/PizzaTest.java @@ -0,0 +1,80 @@ +package com.baeldung.enums; + + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.EnumMap; +import java.util.List; + +import static junit.framework.TestCase.assertTrue; + +public class PizzaTest { + + @Test + public void givenPizaOrder_whenReady_thenDeliverable() { + Pizza testPz = new Pizza(); + testPz.setStatus(Pizza.PizzaStatusEnum.READY); + assertTrue(testPz.isDeliverable()); + } + + @Test + public void givenPizaOrders_whenRetrievingUnDeliveredPzs_thenCorrectlyRetrieved() { + List pzList = new ArrayList<>(); + Pizza pz1 = new Pizza(); + pz1.setStatus(Pizza.PizzaStatusEnum.DELIVERED); + + Pizza pz2 = new Pizza(); + pz2.setStatus(Pizza.PizzaStatusEnum.ORDERED); + + Pizza pz3 = new Pizza(); + pz3.setStatus(Pizza.PizzaStatusEnum.ORDERED); + + Pizza pz4 = new Pizza(); + pz4.setStatus(Pizza.PizzaStatusEnum.READY); + + pzList.add(pz1); + pzList.add(pz2); + pzList.add(pz3); + pzList.add(pz4); + + List undeliveredPzs = Pizza.getAllUndeliveredPizzas(pzList); + assertTrue(undeliveredPzs.size() == 3); + } + + @Test + public void givenPizaOrders_whenGroupByStatusCalled_thenCorrectlyGrouped() { + + List pzList = new ArrayList<>(); + Pizza pz1 = new Pizza(); + pz1.setStatus(Pizza.PizzaStatusEnum.DELIVERED); + + Pizza pz2 = new Pizza(); + pz2.setStatus(Pizza.PizzaStatusEnum.ORDERED); + + Pizza pz3 = new Pizza(); + pz3.setStatus(Pizza.PizzaStatusEnum.ORDERED); + + Pizza pz4 = new Pizza(); + pz4.setStatus(Pizza.PizzaStatusEnum.READY); + + pzList.add(pz1); + pzList.add(pz2); + pzList.add(pz3); + pzList.add(pz4); + + EnumMap> map = Pizza.groupPizzaByStatus(pzList); + assertTrue(map.get(Pizza.PizzaStatusEnum.DELIVERED).size() == 1); + assertTrue(map.get(Pizza.PizzaStatusEnum.ORDERED).size() == 2); + assertTrue(map.get(Pizza.PizzaStatusEnum.READY).size() == 1); + } + + @Test + public void givenPizaOrder_whenDelivered_thenPizzaGetsDeliveredAndStatusChanges() { + Pizza pz = new Pizza(); + pz.setStatus(Pizza.PizzaStatusEnum.READY); + pz.deliver(); + assertTrue(pz.getStatus() == Pizza.PizzaStatusEnum.DELIVERED); + } + +} diff --git a/core-java-8/src/test/java/org/baeldung/java8/Java8CollectionCleanupUnitTest.java b/core-java-8/src/test/java/com/baeldung/java8/Java8CollectionCleanupUnitTest.java similarity index 80% rename from core-java-8/src/test/java/org/baeldung/java8/Java8CollectionCleanupUnitTest.java rename to core-java-8/src/test/java/com/baeldung/java8/Java8CollectionCleanupUnitTest.java index acca829b78..ef4b80c6e8 100644 --- a/core-java-8/src/test/java/org/baeldung/java8/Java8CollectionCleanupUnitTest.java +++ b/core-java-8/src/test/java/com/baeldung/java8/Java8CollectionCleanupUnitTest.java @@ -1,14 +1,13 @@ -package org.baeldung.java8; +package com.baeldung.java8; -import static org.hamcrest.Matchers.hasSize; -import static org.junit.Assert.assertThat; +import com.google.common.collect.Lists; +import org.junit.Test; import java.util.List; import java.util.stream.Collectors; -import org.junit.Test; - -import com.google.common.collect.Lists; +import static org.hamcrest.Matchers.hasSize; +import static org.junit.Assert.assertThat; public class Java8CollectionCleanupUnitTest { @@ -30,6 +29,14 @@ public class Java8CollectionCleanupUnitTest { assertThat(listWithoutNulls, hasSize(3)); } + @Test + public void givenListContainsNulls_whenRemovingNullsWithRemoveIf_thenCorrect() { + final List listWithoutNulls = Lists.newArrayList(null, 1, 2, null, 3, null); + listWithoutNulls.removeIf(p -> p == null); + + assertThat(listWithoutNulls, hasSize(3)); + } + @Test public void givenListContainsDuplicates_whenRemovingDuplicatesWithJava8_thenCorrect() { final List listWithDuplicates = Lists.newArrayList(1, 1, 2, 2, 3, 3); diff --git a/core-java-8/src/test/java/com/baeldung/java8/Java8DefaultStaticIntefaceMethodsTest.java b/core-java-8/src/test/java/com/baeldung/java8/Java8DefaultStaticIntefaceMethodsTest.java new file mode 100644 index 0000000000..21a5e34b9b --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/java8/Java8DefaultStaticIntefaceMethodsTest.java @@ -0,0 +1,27 @@ +package com.baeldung.java8; + +import com.baeldung.java_8_features.Vehicle; +import com.baeldung.java_8_features.VehicleImpl; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class Java8DefaultStaticIntefaceMethodsTest { + + @Test + public void callStaticInterfaceMethdosMethods_whenExpectedResults_thenCorrect() { + Vehicle vehicle = new VehicleImpl(); + String overview = vehicle.getOverview(); + long[] startPosition = vehicle.startPosition(); + + assertEquals(overview, "ATV made by N&F Vehicles"); + assertEquals(startPosition[0], 23); + assertEquals(startPosition[1], 15); + } + + @Test + public void callDefaultInterfaceMethods_whenExpectedResults_thenCorrect() { + String producer = Vehicle.producer(); + assertEquals(producer, "N&F Vehicles"); + } +} diff --git a/core-java-8/src/test/java/com/baeldung/java8/Java8ExecutorServiceTest.java b/core-java-8/src/test/java/com/baeldung/java8/Java8ExecutorServiceTest.java new file mode 100644 index 0000000000..581ccec182 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/java8/Java8ExecutorServiceTest.java @@ -0,0 +1,156 @@ +package com.baeldung.java8; + + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.*; + +import static org.junit.Assert.*; + + +public class Java8ExecutorServiceTest { + + private Runnable runnableTask; + private Callable callableTask; + private List> callableTasks; + + @Before + public void init() { + + runnableTask = () -> { + try { + TimeUnit.MILLISECONDS.sleep(300); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }; + + callableTask = () -> { + TimeUnit.MILLISECONDS.sleep(300); + return "Task's execution"; + }; + + callableTasks = new ArrayList<>(); + callableTasks.add(callableTask); + callableTasks.add(callableTask); + callableTasks.add(callableTask); + } + + @Test + public void creationSubmittingTaskShuttingDown_whenShutDown_thenCorrect() { + + ExecutorService executorService = Executors.newFixedThreadPool(10); + executorService.submit(runnableTask); + executorService.submit(callableTask); + executorService.shutdown(); + + assertTrue(executorService.isShutdown()); + } + + @Test + public void creationSubmittingTasksShuttingDownNow_whenShutDownAfterAwating_thenCorrect() { + + ExecutorService threadPoolExecutor = + new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, + new LinkedBlockingQueue<>()); + + for (int i = 0; i < 100; i++) { + threadPoolExecutor.submit(callableTask); + } + + List notExecutedTasks = smartShutdown(threadPoolExecutor); + + assertTrue(threadPoolExecutor.isShutdown()); + assertFalse(notExecutedTasks.isEmpty()); + assertTrue(notExecutedTasks.size() > 0 && notExecutedTasks.size() < 98); + } + + private List smartShutdown(ExecutorService executorService) { + + List notExecutedTasks = new ArrayList<>(); + executorService.shutdown(); + try { + if (!executorService.awaitTermination(800, TimeUnit.MILLISECONDS)) { + notExecutedTasks = executorService.shutdownNow(); + } + } catch (InterruptedException e) { + notExecutedTasks = executorService.shutdownNow(); + } + return notExecutedTasks; + } + + @Test + public void submittingTasks_whenExecutedOneAndAll_thenCorrect() { + + ExecutorService executorService = Executors.newFixedThreadPool(10); + + String result = null; + List> futures = new ArrayList<>(); + try { + result = executorService.invokeAny(callableTasks); + futures = executorService.invokeAll(callableTasks); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + + assertEquals("Task's execution", result); + assertTrue(futures.size() == 3); + } + + @Test + public void submittingTaskShuttingDown_whenGetExpectedResult_thenCorrect() { + + ExecutorService executorService = Executors.newFixedThreadPool(10); + + Future future = executorService.submit(callableTask); + String result = null; + try { + result = future.get(); + result = future.get(200, TimeUnit.MILLISECONDS); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + e.printStackTrace(); + } + + executorService.shutdown(); + + assertEquals("Task's execution", result); + } + + @Test + public void submittingTask_whenCanceled_thenCorrect() { + + ExecutorService executorService = Executors.newFixedThreadPool(10); + + Future future = executorService.submit(callableTask); + + boolean canceled = future.cancel(true); + boolean isCancelled = future.isCancelled(); + + executorService.shutdown(); + + assertTrue(canceled); + assertTrue(isCancelled); + } + + @Test + public void submittingTaskScheduling_whenExecuted_thenCorrect() { + + ScheduledExecutorService executorService = Executors + .newSingleThreadScheduledExecutor(); + + Future resultFuture = executorService.schedule(callableTask, 1, TimeUnit.SECONDS); + String result = null; + try { + result = resultFuture.get(); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + + executorService.shutdown(); + + assertEquals("Task's execution", result); + } +} diff --git a/core-java-8/src/test/java/com/baeldung/java8/Java8ForkJoinTest.java b/core-java-8/src/test/java/com/baeldung/java8/Java8ForkJoinTest.java new file mode 100644 index 0000000000..273b2d78db --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/java8/Java8ForkJoinTest.java @@ -0,0 +1,100 @@ +package com.baeldung.java8; + + +import com.baeldung.forkjoin.CustomRecursiveAction; +import com.baeldung.forkjoin.CustomRecursiveTask; +import com.baeldung.forkjoin.util.PoolUtil; +import org.junit.Before; +import org.junit.Test; + +import java.util.Random; +import java.util.concurrent.ForkJoinPool; + +import static org.junit.Assert.*; + +public class Java8ForkJoinTest { + + private int[] arr; + private CustomRecursiveTask customRecursiveTask; + + @Before + public void init() { + Random random = new Random(); + arr = new int[50]; + for (int i = 0; i < arr.length; i++) { + arr[i] = random.nextInt(35); + } + customRecursiveTask = new CustomRecursiveTask(arr); + } + + + @Test + public void callPoolUtil_whenExistsAndExpectedType_thenCorrect() { + + ForkJoinPool forkJoinPool = PoolUtil.forkJoinPool; + ForkJoinPool forkJoinPoolTwo = PoolUtil.forkJoinPool; + + assertNotNull(forkJoinPool); + assertEquals(2, forkJoinPool.getParallelism()); + assertEquals(forkJoinPool, forkJoinPoolTwo); + + } + + @Test + public void callCommonPool_whenExistsAndExpectedType_thenCorrect() { + + ForkJoinPool commonPool = ForkJoinPool.commonPool(); + ForkJoinPool commonPoolTwo = ForkJoinPool.commonPool(); + + assertNotNull(commonPool); + assertEquals(commonPool, commonPoolTwo); + + } + + @Test + public void executeRecursiveAction_whenExecuted_thenCorrect() { + + CustomRecursiveAction myRecursiveAction = new CustomRecursiveAction("ddddffffgggghhhh"); + ForkJoinPool.commonPool().invoke(myRecursiveAction); + + assertTrue(myRecursiveAction.isDone()); + + } + + @Test + public void executeRecursiveTask_whenExecuted_thenCorrect() { + + ForkJoinPool forkJoinPool = ForkJoinPool.commonPool(); + + forkJoinPool.execute(customRecursiveTask); + int result = customRecursiveTask.join(); + assertTrue(customRecursiveTask.isDone()); + + forkJoinPool.submit(customRecursiveTask); + int resultTwo = customRecursiveTask.join(); + assertTrue(customRecursiveTask.isDone()); + + } + + @Test + public void executeRecursiveTaskWithFJ_whenExecuted_thenCorrect() { + + CustomRecursiveTask customRecursiveTaskFirst = new CustomRecursiveTask(arr); + CustomRecursiveTask customRecursiveTaskSecond = new CustomRecursiveTask(arr); + CustomRecursiveTask customRecursiveTaskLast = new CustomRecursiveTask(arr); + + customRecursiveTaskFirst.fork(); + customRecursiveTaskSecond.fork(); + customRecursiveTaskLast.fork(); + int result = 0; + result += customRecursiveTaskLast.join(); + result += customRecursiveTaskSecond.join(); + result += customRecursiveTaskFirst.join(); + + assertTrue(customRecursiveTaskFirst.isDone()); + assertTrue(customRecursiveTaskSecond.isDone()); + assertTrue(customRecursiveTaskLast.isDone()); + assertTrue(result != 0); + + } +} diff --git a/core-java-8/src/test/java/com/baeldung/java8/Java8FunctionalInteracesLambdasTest.java b/core-java-8/src/test/java/com/baeldung/java8/Java8FunctionalInteracesLambdasTest.java new file mode 100644 index 0000000000..faaf3ae407 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/java8/Java8FunctionalInteracesLambdasTest.java @@ -0,0 +1,94 @@ +package com.baeldung.java8; + +import com.baeldung.Foo; +import com.baeldung.FooExtended; +import com.baeldung.UseFoo; +import org.junit.Before; +import org.junit.Test; + +import java.util.function.Function; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +public class Java8FunctionalInteracesLambdasTest { + + private UseFoo useFoo; + + @Before + public void init() { + useFoo = new UseFoo(); + } + + @Test + public void functionalInterfaceInstantiation_whenReturnDefiniteString_thenCorrect() { + final Foo foo = parameter -> parameter + "from lambda"; + final String result = useFoo.add("Message ", foo); + + assertEquals("Message from lambda", result); + } + + @Test + public void standardFIParameter_whenReturnDefiniteString_thenCorrect() { + final Function fn = parameter -> parameter + "from lambda"; + final String result = useFoo.addWithStandardFI("Message ", fn); + + assertEquals("Message from lambda", result); + } + + @Test + public void defaultMethodFromExtendedInterface_whenReturnDefiniteString_thenCorrect() { + final FooExtended fooExtended = string -> string; + final String result = fooExtended.defaultMethod(); + + assertEquals("String from Bar", result); + } + + @Test + public void lambdaAndInnerClassInstantiation_whenReturnSameString_thenCorrect() { + final Foo foo = parameter -> parameter + "from Foo"; + + final Foo fooByIC = new Foo() { + @Override + public String method(final String string) { + return string + "from Foo"; + } + }; + + assertEquals(foo.method("Something "), fooByIC.method("Something ")); + } + + @Test + public void accessVariablesFromDifferentScopes_whenReturnPredefinedString_thenCorrect() { + assertEquals("Results: resultIC = Inner class value, resultLambda = Enclosing scope value", useFoo.scopeExperiment()); + } + + @Test + public void shorteningLambdas_whenReturnEqualsResults_thenCorrect() { + final Foo foo = parameter -> buildString(parameter); + + final Foo fooHuge = parameter -> { + final String result = "Something " + parameter; + // many lines of code + return result; + }; + + assertEquals(foo.method("Something"), fooHuge.method("Something")); + } + + private String buildString(final String parameter) { + final String result = "Something " + parameter; + // many lines of code + return result; + } + + @Test + public void mutatingOfEffectivelyFinalVariable_whenNotEquals_thenCorrect() { + final int[] total = new int[1]; + final Runnable r = () -> total[0]++; + r.run(); + + assertNotEquals(0, total[0]); + } + +} diff --git a/core-java-8/src/test/java/com/baeldung/java8/Java8MethodReferenceTest.java b/core-java-8/src/test/java/com/baeldung/java8/Java8MethodReferenceTest.java new file mode 100644 index 0000000000..d9d88c5052 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/java8/Java8MethodReferenceTest.java @@ -0,0 +1,67 @@ +package com.baeldung.java8; + +import com.baeldung.java_8_features.User; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class Java8MethodReferenceTest { + + private List list; + + @Before + public void init() { + list = new ArrayList<>(); + list.add("One"); + list.add("OneAndOnly"); + list.add("Derek"); + list.add("Change"); + list.add("factory"); + list.add("justBefore"); + list.add("Italy"); + list.add("Italy"); + list.add("Thursday"); + list.add(""); + list.add(""); + } + + @Test + public void checkStaticMethodReferences_whenWork_thenCorrect() { + + List users = new ArrayList<>(); + users.add(new User()); + users.add(new User()); + boolean isReal = users.stream().anyMatch(u -> User.isRealUser(u)); + boolean isRealRef = users.stream().anyMatch(User::isRealUser); + assertTrue(isReal); + assertTrue(isRealRef); + } + + @Test + public void checkInstanceMethodReferences_whenWork_thenCorrect() { + User user = new User(); + boolean isLegalName = list.stream().anyMatch(user::isLegalName); + assertTrue(isLegalName); + } + + @Test + public void checkParticularTypeReferences_whenWork_thenCorrect() { + long count = list.stream().filter(String::isEmpty).count(); + assertEquals(count, 2); + } + + @Test + public void checkConstructorReferences_whenWork_thenCorrect() { + Stream stream = list.stream().map(User::new); + List userList = stream.collect(Collectors.toList()); + assertEquals(userList.size(), list.size()); + assertTrue(userList.get(0) instanceof User); + } +} diff --git a/core-java-8/src/test/java/com/baeldung/java8/Java8OptionalTest.java b/core-java-8/src/test/java/com/baeldung/java8/Java8OptionalTest.java new file mode 100644 index 0000000000..26de39bc0e --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/java8/Java8OptionalTest.java @@ -0,0 +1,118 @@ +package com.baeldung.java8; + +import com.baeldung.java_8_features.*; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import static org.junit.Assert.*; + +public class Java8OptionalTest { + + private List list; + + @Before + public void init() { + list = new ArrayList<>(); + list.add("One"); + list.add("OneAndOnly"); + list.add("Derek"); + list.add("Change"); + list.add("factory"); + list.add("justBefore"); + list.add("Italy"); + list.add("Italy"); + list.add("Thursday"); + list.add(""); + list.add(""); + } + + @Test + public void checkOptional_whenAsExpected_thenCorrect() { + + Optional optionalEmpty = Optional.empty(); + assertFalse(optionalEmpty.isPresent()); + + String str = "value"; + Optional optional = Optional.of(str); + assertEquals(optional.get(), "value"); + + Optional optionalNullable = Optional.ofNullable(str); + Optional optionalNull = Optional.ofNullable(null); + assertEquals(optionalNullable.get(), "value"); + assertFalse(optionalNull.isPresent()); + + List listOpt = Optional.of(list).orElse(new ArrayList<>()); + List listNull = null; + List listOptNull = Optional.ofNullable(listNull).orElse(new ArrayList<>()); + assertTrue(listOpt == list); + assertTrue(listOptNull.isEmpty()); + + Optional user = Optional.ofNullable(getUser()); + String result = user.map(User::getAddress) + .map(Address::getStreet) + .orElse("not specified"); + assertEquals(result, "1st Avenue"); + + Optional optionalUser = Optional.ofNullable(getOptionalUser()); + String resultOpt = optionalUser.flatMap(OptionalUser::getAddress) + .flatMap(OptionalAddress::getStreet) + .orElse("not specified"); + assertEquals(resultOpt, "1st Avenue"); + + Optional userNull = Optional.ofNullable(getUserNull()); + String resultNull = userNull.map(User::getAddress) + .map(Address::getStreet) + .orElse("not specified"); + assertEquals(resultNull, "not specified"); + + Optional optionalUserNull = Optional.ofNullable(getOptionalUserNull()); + String resultOptNull = optionalUserNull.flatMap(OptionalUser::getAddress) + .flatMap(OptionalAddress::getStreet) + .orElse("not specified"); + assertEquals(resultOptNull, "not specified"); + + } + + @Test(expected = CustomException.class) + public void callMethod_whenCustomException_thenCorrect() { + User user = new User(); + String result = user.getOrThrow(); + } + + private User getUser() { + User user = new User(); + Address address = new Address(); + address.setStreet("1st Avenue"); + user.setAddress(address); + return user; + } + + private OptionalUser getOptionalUser() { + OptionalUser user = new OptionalUser(); + OptionalAddress address = new OptionalAddress(); + address.setStreet("1st Avenue"); + user.setAddress(address); + return user; + } + + private OptionalUser getOptionalUserNull() { + OptionalUser user = new OptionalUser(); + OptionalAddress address = new OptionalAddress(); + address.setStreet(null); + user.setAddress(address); + return user; + } + + private User getUserNull() { + User user = new User(); + Address address = new Address(); + address.setStreet(null); + user.setAddress(address); + return user; + } + +} diff --git a/core-java-8/src/test/java/org/baeldung/java8/Java8SortUnitTest.java b/core-java-8/src/test/java/com/baeldung/java8/Java8SortUnitTest.java similarity index 98% rename from core-java-8/src/test/java/org/baeldung/java8/Java8SortUnitTest.java rename to core-java-8/src/test/java/com/baeldung/java8/Java8SortUnitTest.java index 8ed7828de0..f371c0d7da 100644 --- a/core-java-8/src/test/java/org/baeldung/java8/Java8SortUnitTest.java +++ b/core-java-8/src/test/java/com/baeldung/java8/Java8SortUnitTest.java @@ -1,17 +1,16 @@ -package org.baeldung.java8; +package com.baeldung.java8; -import static org.hamcrest.Matchers.equalTo; +import com.baeldung.java8.entity.Human; +import com.google.common.collect.Lists; +import com.google.common.primitives.Ints; +import org.junit.Assert; +import org.junit.Test; import java.util.Collections; import java.util.Comparator; import java.util.List; -import org.baeldung.java8.entity.Human; -import org.junit.Assert; -import org.junit.Test; - -import com.google.common.collect.Lists; -import com.google.common.primitives.Ints; +import static org.hamcrest.Matchers.equalTo; public class Java8SortUnitTest { diff --git a/core-java-8/src/test/java/com/baeldung/java8/Java8StreamApiTest.java b/core-java-8/src/test/java/com/baeldung/java8/Java8StreamApiTest.java new file mode 100644 index 0000000000..37326c6d26 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/java8/Java8StreamApiTest.java @@ -0,0 +1,263 @@ +package com.baeldung.java8; + +import com.baeldung.streamApi.Product; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.*; +import java.util.regex.Pattern; +import java.util.stream.*; + +import static org.junit.Assert.*; + +public class Java8StreamApiTest { + + private long counter; + + private static Logger log = LoggerFactory.getLogger(Java8StreamApiTest.class); + + private List productList; + + @Before + public void init() { + productList = Arrays.asList( + new Product(23, "potatoes"), new Product(14, "orange"), + new Product(13, "lemon"), new Product(23, "bread"), + new Product(13, "sugar")); + } + + @Test + public void checkPipeline_whenStreamOneElementShorter_thenCorrect() { + + List list = Arrays.asList("abc1", "abc2", "abc3"); + long size = list.stream().skip(1) + .map(element -> element.substring(0, 3)).count(); + assertEquals(list.size() - 1, size); + } + + @Test + public void checkOrder_whenChangeQuantityOfMethodCalls_thenCorrect() { + + List list = Arrays.asList("abc1", "abc2", "abc3"); + + counter = 0; + long sizeFirst = list.stream() + .skip(2).map(element -> { + wasCalled(); + return element.substring(0, 3); + }).count(); + assertEquals(1, counter); + + counter = 0; + long sizeSecond = list.stream().map(element -> { + wasCalled(); + return element.substring(0, 3); + }).skip(2).count(); + assertEquals(3, counter); + } + + @Test + public void createEmptyStream_whenEmpty_thenCorrect() { + + Stream streamEmpty = Stream.empty(); + assertEquals(0, streamEmpty.count()); + + List names = Collections.emptyList(); + Stream streamOf = Product.streamOf(names); + assertTrue(streamOf.count() == 0); + } + + @Test + public void createStream_whenCreated_thenCorrect() { + + Collection collection = Arrays.asList("a", "b", "c"); + Stream streamOfCollection = collection.stream(); + assertEquals(3, streamOfCollection.count()); + + Stream streamOfArray = Stream.of("a", "b", "c"); + assertEquals(3, streamOfArray.count()); + + String[] arr = new String[]{"a", "b", "c"}; + Stream streamOfArrayPart = Arrays.stream(arr, 1, 3); + assertEquals(2, streamOfArrayPart.count()); + + IntStream intStream = IntStream.range(1, 3); + LongStream longStream = LongStream.rangeClosed(1, 3); + Random random = new Random(); + DoubleStream doubleStream = random.doubles(3); + assertEquals(2, intStream.count()); + assertEquals(3, longStream.count()); + assertEquals(3, doubleStream.count()); + + IntStream streamOfChars = "abc".chars(); + IntStream str = "".chars(); + assertEquals(3, streamOfChars.count()); + + Stream streamOfString = Pattern.compile(", ").splitAsStream("a, b, c"); + assertEquals("a", streamOfString.findFirst().get()); + + Path path = getPath(); + Stream streamOfStrings = null; + try { + streamOfStrings = Files.lines(path, Charset.forName("UTF-8")); + } catch (IOException e) { + log.error("Error creating streams from paths {}", path, e.getMessage(), e); + } + assertEquals("a", streamOfStrings.findFirst().get()); + + Stream streamBuilder = Stream.builder().add("a").add("b").add("c").build(); + assertEquals(3, streamBuilder.count()); + + Stream streamGenerated = Stream.generate(() -> "element").limit(10); + assertEquals(10, streamGenerated.count()); + + Stream streamIterated = Stream.iterate(40, n -> n + 2).limit(20); + assertTrue(40 <= streamIterated.findAny().get()); + } + + @Test + public void runStreamPipeline_whenOrderIsRight_thenCorrect() { + + List list = Arrays.asList("abc1", "abc2", "abc3"); + Optional stream = list.stream() + .filter(element -> { + log.info("filter() was called"); + return element.contains("2"); + }).map(element -> { + log.info("map() was called"); + return element.toUpperCase(); + }).findFirst(); + } + + @Test + public void reduce_whenExpected_thenCorrect() { + + OptionalInt reduced = IntStream.range(1, 4).reduce((a, b) -> a + b); + assertEquals(6, reduced.getAsInt()); + + int reducedTwoParams = IntStream.range(1, 4).reduce(10, (a, b) -> a + b); + assertEquals(16, reducedTwoParams); + + int reducedThreeParams = Stream.of(1, 2, 3) + .reduce(10, (a, b) -> a + b, (a, b) -> { + log.info("combiner was called"); + return a + b; + }); + assertEquals(16, reducedThreeParams); + + int reducedThreeParamsParallel = Arrays.asList(1, 2, 3).parallelStream() + .reduce(10, (a, b) -> a + b, (a, b) -> { + log.info("combiner was called"); + return a + b; + }); + assertEquals(36, reducedThreeParamsParallel); + } + + @Test + public void collecting_whenAsExpected_thenCorrect() { + + List collectorCollection = productList.stream() + .map(Product::getName).collect(Collectors.toList()); + + assertTrue(collectorCollection instanceof List); + assertEquals(5, collectorCollection.size()); + + String listToString = productList.stream().map(Product::getName) + .collect(Collectors.joining(", ", "[", "]")); + + assertTrue(listToString.contains(",") && listToString.contains("[") && listToString.contains("]")); + + double averagePrice = productList.stream().collect(Collectors.averagingInt(Product::getPrice)); + assertTrue(17.2 == averagePrice); + + int summingPrice = productList.stream().collect(Collectors.summingInt(Product::getPrice)); + assertEquals(86, summingPrice); + + IntSummaryStatistics statistics = productList.stream() + .collect(Collectors.summarizingInt(Product::getPrice)); + assertEquals(23, statistics.getMax()); + + Map> collectorMapOfLists = productList.stream() + .collect(Collectors.groupingBy(Product::getPrice)); + assertEquals(3, collectorMapOfLists.keySet().size()); + + Map> mapPartioned = productList.stream() + .collect(Collectors.partitioningBy(element -> element.getPrice() > 15)); + assertEquals(2, mapPartioned.keySet().size()); + + } + + @Test(expected = UnsupportedOperationException.class) + public void collect_whenThrows_thenCorrect() { + Set unmodifiableSet = productList.stream() + .collect(Collectors.collectingAndThen(Collectors.toSet(), + Collections::unmodifiableSet)); + unmodifiableSet.add(new Product(4, "tea")); + } + + @Test + public void customCollector_whenResultContainsAllElementsFrSource_thenCorrect() { + Collector> toLinkedList = + Collector.of(LinkedList::new, LinkedList::add, + (first, second) -> { + first.addAll(second); + return first; + }); + + LinkedList linkedListOfPersons = productList.stream().collect(toLinkedList); + assertTrue(linkedListOfPersons.containsAll(productList)); + } + + @Test + public void parallelStream_whenWorks_thenCorrect() { + Stream streamOfCollection = productList.parallelStream(); + boolean isParallel = streamOfCollection.isParallel(); + boolean haveBigPrice = streamOfCollection.map(product -> product.getPrice() * 12) + .anyMatch(price -> price > 200); + assertTrue(isParallel && haveBigPrice); + } + + @Test + public void parallel_whenIsParallel_thenCorrect() { + IntStream intStreamParallel = + IntStream.range(1, 150).parallel().map(element -> element * 34); + boolean isParallel = intStreamParallel.isParallel(); + assertTrue(isParallel); + } + + @Test + public void parallel_whenIsSequential_thenCorrect() { + IntStream intStreamParallel = + IntStream.range(1, 150).parallel().map(element -> element * 34); + IntStream intStreamSequential = intStreamParallel.sequential(); + boolean isParallel = intStreamParallel.isParallel(); + assertFalse(isParallel); + } + + private Path getPath() { + Path path = null; + try { + path = Files.createTempFile(null, ".txt"); + } catch (IOException e) { + log.error(e.getMessage()); + } + + try (BufferedWriter writer = Files.newBufferedWriter(path)) { + writer.write("a\nb\nc"); + } catch (IOException e) { + log.error(e.getMessage()); + } + return path; + } + + private void wasCalled() { + counter++; + } +} diff --git a/core-java-8/src/test/java/com/baeldung/java8/Java8StreamsTest.java b/core-java-8/src/test/java/com/baeldung/java8/Java8StreamsTest.java new file mode 100644 index 0000000000..1f1dda49ce --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/java8/Java8StreamsTest.java @@ -0,0 +1,113 @@ +package com.baeldung.java8; + +import com.baeldung.java_8_features.Detail; +import org.junit.Before; +import org.junit.Test; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.junit.Assert.*; + +public class Java8StreamsTest { + + private List list; + + @Before + public void init() { + list = new ArrayList<>(); + list.add("One"); + list.add("OneAndOnly"); + list.add("Derek"); + list.add("Change"); + list.add("factory"); + list.add("justBefore"); + list.add("Italy"); + list.add("Italy"); + list.add("Thursday"); + list.add(""); + list.add(""); + } + + @Test + public void checkStreamCount_whenCreating_givenDifferentSources() { + String[] arr = new String[]{"a", "b", "c"}; + Stream streamArr = Arrays.stream(arr); + assertEquals(streamArr.count(), 3); + + Stream streamOf = Stream.of("a", "b", "c"); + assertEquals(streamOf.count(), 3); + + long count = list.stream().distinct().count(); + assertEquals(count, 9); + } + + + @Test + public void checkStreamCount_whenOperationFilter_thanCorrect() { + Stream streamFilter = list.stream().filter(element -> element.isEmpty()); + assertEquals(streamFilter.count(), 2); + } + + + @Test + public void checkStreamCount_whenOperationMap_thanCorrect() { + List uris = new ArrayList<>(); + uris.add("C:\\My.txt"); + Stream streamMap = uris.stream().map(uri -> Paths.get(uri)); + assertEquals(streamMap.count(), 1); + + List details = new ArrayList<>(); + details.add(new Detail()); + details.add(new Detail()); + Stream streamFlatMap = details.stream() + .flatMap(detail -> detail.getParts().stream()); + assertEquals(streamFlatMap.count(), 4); + } + + + @Test + public void checkStreamCount_whenOperationMatch_thenCorrect() { + boolean isValid = list.stream().anyMatch(element -> element.contains("h")); + boolean isValidOne = list.stream().allMatch(element -> element.contains("h")); + boolean isValidTwo = list.stream().noneMatch(element -> element.contains("h")); + assertTrue(isValid); + assertFalse(isValidOne); + assertFalse(isValidTwo); + } + + + @Test + public void checkStreamReducedValue_whenOperationReduce_thenCorrect() { + List integers = new ArrayList<>(); + integers.add(1); + integers.add(1); + integers.add(1); + Integer reduced = integers.stream().reduce(23, (a, b) -> a + b); + assertTrue(reduced == 26); + } + + @Test + public void checkStreamContains_whenOperationCollect_thenCorrect() { + List resultList = list.stream() + .map(element -> element.toUpperCase()) + .collect(Collectors.toList()); + assertEquals(resultList.size(), list.size()); + assertTrue(resultList.contains("")); + } + + + @Test + public void checkParallelStream_whenDoWork() { + list.parallelStream().forEach(element -> doWork(element)); + } + + private void doWork(String string) { + assertTrue(true); //just imitate an amount of work + } +} diff --git a/core-java-8/src/test/java/org/baeldung/java8/JavaFolderSizeTest.java b/core-java-8/src/test/java/com/baeldung/java8/JavaFolderSizeTest.java similarity index 82% rename from core-java-8/src/test/java/org/baeldung/java8/JavaFolderSizeTest.java rename to core-java-8/src/test/java/com/baeldung/java8/JavaFolderSizeTest.java index baa41511de..efd548a4b1 100644 --- a/core-java-8/src/test/java/org/baeldung/java8/JavaFolderSizeTest.java +++ b/core-java-8/src/test/java/com/baeldung/java8/JavaFolderSizeTest.java @@ -1,29 +1,34 @@ -package org.baeldung.java8; +package com.baeldung.java8; -import static org.junit.Assert.assertEquals; +import org.apache.commons.io.FileUtils; +import org.junit.Before; +import org.junit.Test; import java.io.File; import java.io.IOException; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.SimpleFileVisitor; +import java.nio.file.*; import java.nio.file.attribute.BasicFileAttributes; import java.text.DecimalFormat; import java.util.concurrent.atomic.AtomicLong; import java.util.stream.StreamSupport; -import org.apache.commons.io.FileUtils; -import org.junit.Test; +import static org.junit.Assert.assertEquals; public class JavaFolderSizeTest { + private String path; + + @Before + public void init() { + final String separator = File.separator; + path = "src" + separator + "test" + separator + "resources"; + } + @Test public void whenGetFolderSizeRecursive_thenCorrect() { final long expectedSize = 136; - final File folder = new File("src/test/resources"); + final File folder = new File(path); final long size = getFolderSize(folder); assertEquals(expectedSize, size); @@ -34,7 +39,7 @@ public class JavaFolderSizeTest { final long expectedSize = 136; final AtomicLong size = new AtomicLong(0); - final Path folder = Paths.get("src/test/resources"); + final Path folder = Paths.get(path); Files.walkFileTree(folder, new SimpleFileVisitor() { @Override @@ -51,7 +56,7 @@ public class JavaFolderSizeTest { public void whenGetFolderSizeUsingJava8_thenCorrect() throws IOException { final long expectedSize = 136; - final Path folder = Paths.get("src/test/resources"); + final Path folder = Paths.get(path); final long size = Files.walk(folder).filter(p -> p.toFile().isFile()).mapToLong(p -> p.toFile().length()).sum(); assertEquals(expectedSize, size); @@ -61,7 +66,7 @@ public class JavaFolderSizeTest { public void whenGetFolderSizeUsingApacheCommonsIO_thenCorrect() { final long expectedSize = 136; - final File folder = new File("src/test/resources"); + final File folder = new File(path); final long size = FileUtils.sizeOfDirectory(folder); assertEquals(expectedSize, size); @@ -71,7 +76,7 @@ public class JavaFolderSizeTest { public void whenGetFolderSizeUsingGuava_thenCorrect() { final long expectedSize = 136; - final File folder = new File("src/test/resources"); + final File folder = new File(path); final Iterable files = com.google.common.io.Files.fileTreeTraverser().breadthFirstTraversal(folder); final long size = StreamSupport.stream(files.spliterator(), false).filter(f -> f.isFile()).mapToLong(File::length).sum(); @@ -81,10 +86,10 @@ public class JavaFolderSizeTest { @Test public void whenGetReadableSize_thenCorrect() { - final File folder = new File("src/test/resources"); + final File folder = new File(path); final long size = getFolderSize(folder); - final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" }; + final String[] units = new String[]{"B", "KB", "MB", "GB", "TB"}; final int unitIndex = (int) (Math.log10(size) / 3); final double unitValue = 1 << (unitIndex * 10); diff --git a/core-java-8/src/test/java/com/baeldung/java8/JavaTryWithResourcesTest.java b/core-java-8/src/test/java/com/baeldung/java8/JavaTryWithResourcesTest.java new file mode 100644 index 0000000000..224c4e9d6a --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/java8/JavaTryWithResourcesTest.java @@ -0,0 +1,86 @@ +package com.baeldung.java8; + +import org.junit.Assert; +import org.junit.Test; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.Date; +import java.util.Scanner; + +public class JavaTryWithResourcesTest { + + private static final String TEST_STRING_HELLO_WORLD = "Hello World"; + private Date resource1Date, resource2Date; + + // tests + + /* Example for using Try_with_resources */ + @Test + public void whenWritingToStringWriter_thenCorrectlyWritten() { + final StringWriter sw = new StringWriter(); + try (PrintWriter pw = new PrintWriter(sw, true)) { + pw.print(TEST_STRING_HELLO_WORLD); + } + + Assert.assertEquals(sw.getBuffer().toString(), TEST_STRING_HELLO_WORLD); + } + + /* Example for using multiple resources */ + @Test + public void givenStringToScanner_whenWritingToStringWriter_thenCorrectlyWritten() { + + final StringWriter sw = new StringWriter(); + try (Scanner sc = new Scanner(TEST_STRING_HELLO_WORLD); PrintWriter pw = new PrintWriter(sw, true)) { + while (sc.hasNext()) { + pw.print(sc.nextLine()); + } + } + + Assert.assertEquals(sw.getBuffer().toString(), TEST_STRING_HELLO_WORLD); + } + + /* Example to show order in which the resources are closed */ + @Test + public void whenFirstAutoClosableResourceIsinitializedFirst_thenFirstAutoClosableResourceIsReleasedFirst() throws Exception { + try (AutoCloseableResourcesFirst af = new AutoCloseableResourcesFirst(); AutoCloseableResourcesSecond as = new AutoCloseableResourcesSecond()) { + af.doSomething(); + as.doSomething(); + } + Assert.assertTrue(resource1Date.after(resource2Date)); + } + + class AutoCloseableResourcesFirst implements AutoCloseable { + public AutoCloseableResourcesFirst() { + System.out.println("Constructor -> AutoCloseableResources_First"); + } + + public void doSomething() { + System.out.println("Something -> AutoCloseableResources_First"); + } + + @Override + public void close() throws Exception { + System.out.println("Closed AutoCloseableResources_First"); + resource1Date = new Date(); + } + } + + class AutoCloseableResourcesSecond implements AutoCloseable { + public AutoCloseableResourcesSecond() { + System.out.println("Constructor -> AutoCloseableResources_Second"); + } + + public void doSomething() { + System.out.println("Something -> AutoCloseableResources_Second"); + } + + @Override + public void close() throws Exception { + System.out.println("Closed AutoCloseableResources_Second"); + resource2Date = new Date(); + Thread.sleep(10000); + } + } + +} diff --git a/core-java-8/src/test/java/org/baeldung/java8/base64/ApacheCommonsEncodeDecodeTest.java b/core-java-8/src/test/java/com/baeldung/java8/base64/ApacheCommonsEncodeDecodeTest.java similarity index 91% rename from core-java-8/src/test/java/org/baeldung/java8/base64/ApacheCommonsEncodeDecodeTest.java rename to core-java-8/src/test/java/com/baeldung/java8/base64/ApacheCommonsEncodeDecodeTest.java index 9745655d8a..164a571817 100644 --- a/core-java-8/src/test/java/org/baeldung/java8/base64/ApacheCommonsEncodeDecodeTest.java +++ b/core-java-8/src/test/java/com/baeldung/java8/base64/ApacheCommonsEncodeDecodeTest.java @@ -1,14 +1,12 @@ -package org.baeldung.java8.base64; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; - -import java.io.UnsupportedEncodingException; +package com.baeldung.java8.base64; import org.apache.commons.codec.binary.Base64; import org.junit.Test; +import java.io.UnsupportedEncodingException; + +import static org.junit.Assert.*; + public class ApacheCommonsEncodeDecodeTest { // tests diff --git a/core-java-8/src/test/java/org/baeldung/java8/base64/Java8EncodeDecodeTest.java b/core-java-8/src/test/java/com/baeldung/java8/base64/Java8EncodeDecodeTest.java similarity index 95% rename from core-java-8/src/test/java/org/baeldung/java8/base64/Java8EncodeDecodeTest.java rename to core-java-8/src/test/java/com/baeldung/java8/base64/Java8EncodeDecodeTest.java index 7b7a0be26a..18dccf71ba 100644 --- a/core-java-8/src/test/java/org/baeldung/java8/base64/Java8EncodeDecodeTest.java +++ b/core-java-8/src/test/java/com/baeldung/java8/base64/Java8EncodeDecodeTest.java @@ -1,14 +1,12 @@ -package org.baeldung.java8.base64; +package com.baeldung.java8.base64; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; +import org.junit.Test; import java.io.UnsupportedEncodingException; import java.util.Base64; import java.util.UUID; -import org.junit.Test; +import static org.junit.Assert.*; public class Java8EncodeDecodeTest { diff --git a/core-java-8/src/test/java/org/baeldung/java8/entity/Human.java b/core-java-8/src/test/java/com/baeldung/java8/entity/Human.java similarity index 98% rename from core-java-8/src/test/java/org/baeldung/java8/entity/Human.java rename to core-java-8/src/test/java/com/baeldung/java8/entity/Human.java index ac58b301b7..cab8546129 100644 --- a/core-java-8/src/test/java/org/baeldung/java8/entity/Human.java +++ b/core-java-8/src/test/java/com/baeldung/java8/entity/Human.java @@ -1,4 +1,4 @@ -package org.baeldung.java8.entity; +package com.baeldung.java8.entity; public class Human { private String name; 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/README.md b/core-java/README.md index 772681ad57..23fe12465f 100644 --- a/core-java/README.md +++ b/core-java/README.md @@ -6,4 +6,10 @@ - [Immutable ArrayList in Java](http://www.baeldung.com/java-immutable-list) - [Java - Reading a Large File Efficiently](http://www.baeldung.com/java-read-lines-large-file) - [Java InputStream to String](http://www.baeldung.com/convert-input-stream-to-string) - +- [Converting between an Array and a List in Java](http://www.baeldung.com/convert-array-to-list-and-list-to-array) +- [Converting between an Array and a Set in Java](http://www.baeldung.com/convert-array-to-set-and-set-to-array) +- [Converting between a List and a Set in Java](http://www.baeldung.com/convert-list-to-set-and-set-to-list) +- [Convert a Map to an Array, List or Set in Java](http://www.baeldung.com/convert-map-values-to-array-list-set) +- [Java – Write to File](http://www.baeldung.com/java-write-to-file) +- [Java Scanner](http://www.baeldung.com/java-scanner) +- [Java Timer](http://www.baeldung.com/java-timer-and-timertask) diff --git a/core-java/pom.xml b/core-java/pom.xml index c6f2b32cd8..cb194a6d9f 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -1,15 +1,19 @@ 4.0.0 - org.baeldung + com.baeldung core-java - 0.1-SNAPSHOT + 0.1.0-SNAPSHOT core-java - + + net.sourceforge.collections + collections-generic + 4.01 + com.google.guava guava @@ -79,7 +83,7 @@ junit - junit-dep + junit ${junit.version} test @@ -122,8 +126,8 @@ maven-compiler-plugin ${maven-compiler-plugin.version} - 1.7 - 1.7 + 1.8 + 1.8 @@ -143,44 +147,40 @@ - - 4.1.5.RELEASE - 3.2.5.RELEASE - - 4.3.10.Final - 5.1.35 + 4.3.11.Final + 5.1.38 - 2.4.4 + 2.7.2 - 1.7.12 + 1.7.13 1.1.3 5.1.3.Final - 18.0 + 19.0 3.4 1.3 - 4.11 + 4.12 1.10.19 4.4.1 4.5 - 2.4.1 + 2.9.0 - 3.3 + 3.5.1 2.6 - 2.18.1 + 2.19.1 2.7 - 1.4.14 + 1.4.18 diff --git a/core-java/src/main/java/com/baeldung/enums/Pizza.java b/core-java/src/main/java/com/baeldung/enums/Pizza.java new file mode 100644 index 0000000000..7742781081 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/enums/Pizza.java @@ -0,0 +1,110 @@ +package com.baeldung.enums; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.collections15.Predicate; + +import java.io.IOException; +import java.util.EnumMap; +import java.util.EnumSet; +import java.util.List; +import java.util.stream.Collectors; + +public class Pizza { + + private static EnumSet undeliveredPizzaStatuses = + EnumSet.of(PizzaStatus.ORDERED, PizzaStatus.READY); + + private PizzaStatus status; + + @JsonFormat(shape = JsonFormat.Shape.OBJECT) + public enum PizzaStatus { + ORDERED(5) { + @Override + public boolean isOrdered() { + return true; + } + }, + READY(2) { + @Override + public boolean isReady() { + return true; + } + }, + DELIVERED(0) { + @Override + public boolean isDelivered() { + return true; + } + }; + + private int timeToDelivery; + + public boolean isOrdered() { + return false; + } + + public boolean isReady() { + return false; + } + + public boolean isDelivered() { + return false; + } + + @JsonProperty("timeToDelivery") + public int getTimeToDelivery() { + return timeToDelivery; + } + + PizzaStatus(int timeToDelivery) { + this.timeToDelivery = timeToDelivery; + } + } + + public PizzaStatus getStatus() { + return status; + } + + public void setStatus(PizzaStatus status) { + this.status = status; + } + + public boolean isDeliverable() { + return this.status.isReady(); + } + + public void printTimeToDeliver() { + System.out.println("Time to delivery is " + this.getStatus().getTimeToDelivery() + " days"); + } + + public static List getAllUndeliveredPizzas(List input) { + return input.stream().filter( + (s) -> undeliveredPizzaStatuses.contains(s.getStatus())) + .collect(Collectors.toList()); + } + + public static EnumMap> + groupPizzaByStatus(List pzList) { + return pzList.stream().collect( + Collectors.groupingBy(Pizza::getStatus, + () -> new EnumMap<>(PizzaStatus.class), Collectors.toList())); + } + + public void deliver() { + if (isDeliverable()) { + PizzaDeliverySystemConfiguration.getInstance().getDeliveryStrategy().deliver(this); + this.setStatus(PizzaStatus.DELIVERED); + } + } + + public static String getJsonString(Pizza pz) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(pz); + } + + private static Predicate thatAreNotDelivered() { + return entry -> undeliveredPizzaStatuses.contains(entry.getStatus()); + } +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/enums/PizzaDeliveryStrategy.java b/core-java/src/main/java/com/baeldung/enums/PizzaDeliveryStrategy.java new file mode 100644 index 0000000000..ed65919387 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/enums/PizzaDeliveryStrategy.java @@ -0,0 +1,18 @@ +package com.baeldung.enums; + +public enum PizzaDeliveryStrategy { + EXPRESS { + @Override + public void deliver(Pizza pz) { + System.out.println("Pizza will be delivered in express mode"); + } + }, + NORMAL { + @Override + public void deliver(Pizza pz) { + System.out.println("Pizza will be delivered in normal mode"); + } + }; + + public abstract void deliver(Pizza pz); +} diff --git a/core-java/src/main/java/com/baeldung/enums/PizzaDeliverySystemConfiguration.java b/core-java/src/main/java/com/baeldung/enums/PizzaDeliverySystemConfiguration.java new file mode 100644 index 0000000000..a276b3c000 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/enums/PizzaDeliverySystemConfiguration.java @@ -0,0 +1,21 @@ +package com.baeldung.enums; + +public enum PizzaDeliverySystemConfiguration { + INSTANCE; + + PizzaDeliverySystemConfiguration() { + // Do the configuration initialization which + // involves overriding defaults like delivery strategy + } + + private PizzaDeliveryStrategy deliveryStrategy = PizzaDeliveryStrategy.NORMAL; + + public static PizzaDeliverySystemConfiguration getInstance() { + return INSTANCE; + } + + public PizzaDeliveryStrategy getDeliveryStrategy() { + return deliveryStrategy; + } + +} diff --git a/core-java/src/main/webapp/WEB-INF/api-servlet.xml b/core-java/src/main/webapp/WEB-INF/api-servlet.xml deleted file mode 100644 index 4c74be8912..0000000000 --- a/core-java/src/main/webapp/WEB-INF/api-servlet.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/core-java/src/main/webapp/WEB-INF/web.xml b/core-java/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index 935beae648..0000000000 --- a/core-java/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - - Spring MVC Application - - - - contextClass - - org.springframework.web.context.support.AnnotationConfigWebApplicationContext - - - - contextConfigLocation - org.baeldung.config - - - - org.springframework.web.context.ContextLoaderListener - - - - - api - org.springframework.web.servlet.DispatcherServlet - 1 - - - api - / - - - - - - - \ No newline at end of file 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/core-java/src/test/java/org/baeldung/java/enums/PizzaTest.java b/core-java/src/test/java/org/baeldung/java/enums/PizzaTest.java new file mode 100644 index 0000000000..a6814ee600 --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/enums/PizzaTest.java @@ -0,0 +1,80 @@ +package org.baeldung.java.enums; + + +import com.baeldung.enums.Pizza; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.EnumMap; +import java.util.List; + +import static junit.framework.TestCase.assertTrue; + + +public class PizzaTest { + @Test + public void givenPizaOrder_whenReady_thenDeliverable() { + Pizza testPz = new Pizza(); + testPz.setStatus(Pizza.PizzaStatus.READY); + assertTrue(testPz.isDeliverable()); + } + + @Test + public void givenPizaOrders_whenRetrievingUnDeliveredPzs_thenCorrectlyRetrieved() { + List pzList = new ArrayList<>(); + Pizza pz1 = new Pizza(); + pz1.setStatus(Pizza.PizzaStatus.DELIVERED); + + Pizza pz2 = new Pizza(); + pz2.setStatus(Pizza.PizzaStatus.ORDERED); + + Pizza pz3 = new Pizza(); + pz3.setStatus(Pizza.PizzaStatus.ORDERED); + + Pizza pz4 = new Pizza(); + pz4.setStatus(Pizza.PizzaStatus.READY); + + pzList.add(pz1); + pzList.add(pz2); + pzList.add(pz3); + pzList.add(pz4); + + List undeliveredPzs = Pizza.getAllUndeliveredPizzas(pzList); + assertTrue(undeliveredPzs.size() == 3); + } + + @Test + public void givenPizaOrders_whenGroupByStatusCalled_thenCorrectlyGrouped() { + + List pzList = new ArrayList<>(); + Pizza pz1 = new Pizza(); + pz1.setStatus(Pizza.PizzaStatus.DELIVERED); + + Pizza pz2 = new Pizza(); + pz2.setStatus(Pizza.PizzaStatus.ORDERED); + + Pizza pz3 = new Pizza(); + pz3.setStatus(Pizza.PizzaStatus.ORDERED); + + Pizza pz4 = new Pizza(); + pz4.setStatus(Pizza.PizzaStatus.READY); + + pzList.add(pz1); + pzList.add(pz2); + pzList.add(pz3); + pzList.add(pz4); + + EnumMap> map = Pizza.groupPizzaByStatus(pzList); + assertTrue(map.get(Pizza.PizzaStatus.DELIVERED).size() == 1); + assertTrue(map.get(Pizza.PizzaStatus.ORDERED).size() == 2); + assertTrue(map.get(Pizza.PizzaStatus.READY).size() == 1); + } + + @Test + public void givenPizaOrder_whenDelivered_thenPizzaGetsDeliveredAndStatusChanges() { + Pizza pz = new Pizza(); + pz.setStatus(Pizza.PizzaStatus.READY); + pz.deliver(); + assertTrue(pz.getStatus() == Pizza.PizzaStatus.DELIVERED); + } +} diff --git a/core-java/src/test/java/org/baeldung/java/io/JavaFileIntegrationTest.java b/core-java/src/test/java/org/baeldung/java/io/JavaFileIntegrationTest.java index c139e34afb..24213ba869 100644 --- a/core-java/src/test/java/org/baeldung/java/io/JavaFileIntegrationTest.java +++ b/core-java/src/test/java/org/baeldung/java/io/JavaFileIntegrationTest.java @@ -1,5 +1,6 @@ package org.baeldung.java.io; +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; import static org.junit.Assert.assertTrue; import java.io.File; @@ -57,9 +58,8 @@ public class JavaFileIntegrationTest { @Test public final void givenUsingJDK7Nio2_whenMovingFile_thenCorrect() throws IOException { - final Path fileToMovePath = Files.createFile(Paths.get("src/test/resources/fileToMove.txt")); - final Path dirPath = Paths.get("src/test/resources/"); - final Path targetPath = Files.createDirectory(dirPath); + final Path fileToMovePath = Files.createFile(Paths.get("src/test/resources/" + randomAlphabetic(5) + ".txt")); + final Path targetPath = Paths.get("src/main/resources/"); Files.move(fileToMovePath, targetPath.resolve(fileToMovePath.getFileName())); } diff --git a/core-java/src/test/java/org/baeldung/java/io/JavaXToWriterUnitTest.java b/core-java/src/test/java/org/baeldung/java/io/JavaXToWriterUnitTest.java index 35ec15df16..eb393668bd 100644 --- a/core-java/src/test/java/org/baeldung/java/io/JavaXToWriterUnitTest.java +++ b/core-java/src/test/java/org/baeldung/java/io/JavaXToWriterUnitTest.java @@ -1,5 +1,7 @@ package org.baeldung.java.io; +import static org.junit.Assert.assertEquals; + import java.io.IOException; import java.io.StringWriter; import java.io.Writer; @@ -23,6 +25,8 @@ public class JavaXToWriterUnitTest { final Writer targetWriter = new StringWriter().append(new String(initialArray)); targetWriter.close(); + + assertEquals("With Java", targetWriter.toString()); } @Test @@ -40,6 +44,8 @@ public class JavaXToWriterUnitTest { charSink.write(buffer); stringWriter.close(); + + assertEquals("With Guava", stringWriter.toString()); } @Test @@ -48,6 +54,8 @@ public class JavaXToWriterUnitTest { final Writer targetWriter = new StringBuilderWriter(new StringBuilder(new String(initialArray))); targetWriter.close(); + + assertEquals("With Commons IO", targetWriter.toString()); } } diff --git a/dependency-injection/.gitignore b/dependency-injection/.gitignore new file mode 100644 index 0000000000..6531dfc93f --- /dev/null +++ b/dependency-injection/.gitignore @@ -0,0 +1,12 @@ +RemoteSystemsTempFiles/ +.classpath +.project +.settings/ +bin/ +.metadata/ +docs/*.autosave +docs/*.autosave +.recommenders/ +build/ +.gradle/ +.DS_Store diff --git a/dependency-injection/build.gradle b/dependency-injection/build.gradle new file mode 100644 index 0000000000..968636154d --- /dev/null +++ b/dependency-injection/build.gradle @@ -0,0 +1,43 @@ +apply plugin: 'java' +apply plugin: 'eclipse' + +allprojects { + apply plugin: 'java' + sourceCompatibility = 1.6 + targetCompatibility = 1.6 +} + +repositories { + mavenCentral() +} + +sourceSets { + main { + resources.srcDirs = ["src/main/java","src/main/resources"] + } + test { + resources.srcDirs = ["src/main/java", "src/main/resources", "src/test/resources"] + } +} + +configurations { + compile +} + +test { + testLogging { + events 'started', 'passed' + } +} + +dependencies { + testCompile('junit:junit:4.11') + testCompile('org.mockito:mockito-all:1.10.19') + testCompile group: 'org.springframework', name: 'spring-test', version: '4.2.6.RELEASE' + testCompile group: 'org.springframework', name: 'spring-core', version: '4.2.6.RELEASE' + testCompile group: 'org.springframework', name: 'spring-beans', version: '4.2.6.RELEASE' + testCompile group: 'org.springframework', name: 'spring-context', version: '4.2.6.RELEASE' + testCompile group: 'javax.inject', name: 'javax.inject', version: '1' + + testRuntime('junit:junit:4.11') +} diff --git a/dependency-injection/docs/autowired-name-demo-classdiagram.png b/dependency-injection/docs/autowired-name-demo-classdiagram.png new file mode 100644 index 0000000000..f367fdbf41 Binary files /dev/null and b/dependency-injection/docs/autowired-name-demo-classdiagram.png differ diff --git a/dependency-injection/docs/autowired-type-demo-classdiagram.png b/dependency-injection/docs/autowired-type-demo-classdiagram.png new file mode 100644 index 0000000000..5f3f341556 Binary files /dev/null and b/dependency-injection/docs/autowired-type-demo-classdiagram.png differ diff --git a/dependency-injection/docs/autowired-type-demo-classdiagram.xml b/dependency-injection/docs/autowired-type-demo-classdiagram.xml new file mode 100644 index 0000000000..71dc839457 --- /dev/null +++ b/dependency-injection/docs/autowired-type-demo-classdiagram.xml @@ -0,0 +1 @@ +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 new file mode 100644 index 0000000000..9996fdc733 Binary files /dev/null and b/dependency-injection/docs/inject-demo-classdiagram.png differ diff --git a/dependency-injection/docs/inject-field-demo-classdiagram.png b/dependency-injection/docs/inject-field-demo-classdiagram.png new file mode 100644 index 0000000000..e63b6e5f42 Binary files /dev/null and b/dependency-injection/docs/inject-field-demo-classdiagram.png differ diff --git a/dependency-injection/docs/inject-field-demo-classdiagram.xml b/dependency-injection/docs/inject-field-demo-classdiagram.xml new file mode 100644 index 0000000000..147c545a2d --- /dev/null +++ b/dependency-injection/docs/inject-field-demo-classdiagram.xml @@ -0,0 +1 @@ +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 new file mode 100644 index 0000000000..96a6a3425e Binary files /dev/null and b/dependency-injection/docs/inject-name-demo-classdiagram.png differ diff --git a/dependency-injection/docs/inject-name-demo-classdiagram.xml b/dependency-injection/docs/inject-name-demo-classdiagram.xml new file mode 100644 index 0000000000..5a8bb9f8c4 --- /dev/null +++ b/dependency-injection/docs/inject-name-demo-classdiagram.xml @@ -0,0 +1 @@ +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 new file mode 100644 index 0000000000..1ffe6453cb Binary files /dev/null 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 new file mode 100644 index 0000000000..ec122afe71 --- /dev/null +++ b/dependency-injection/docs/inject-qualifier-demo-classdiagram.xml @@ -0,0 +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 diff --git a/dependency-injection/docs/resource-demo-classdiagram.png b/dependency-injection/docs/resource-demo-classdiagram.png new file mode 100644 index 0000000000..1f0a41a19e Binary files /dev/null and b/dependency-injection/docs/resource-demo-classdiagram.png differ diff --git a/dependency-injection/docs/resource-field-demo-classdiagram.png b/dependency-injection/docs/resource-field-demo-classdiagram.png new file mode 100644 index 0000000000..0d6207d09a Binary files /dev/null and b/dependency-injection/docs/resource-field-demo-classdiagram.png differ diff --git a/dependency-injection/docs/resource-field-demo-classdiagram.xml b/dependency-injection/docs/resource-field-demo-classdiagram.xml new file mode 100644 index 0000000000..44c742f2bc --- /dev/null +++ b/dependency-injection/docs/resource-field-demo-classdiagram.xml @@ -0,0 +1 @@ +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 new file mode 100644 index 0000000000..c5cd0c0fcb Binary files /dev/null and b/dependency-injection/docs/resource-method-byname-demo-classdiagram.png differ diff --git a/dependency-injection/docs/resource-method-demo-classdiagram.png b/dependency-injection/docs/resource-method-demo-classdiagram.png new file mode 100644 index 0000000000..6be7dbbeea Binary files /dev/null and b/dependency-injection/docs/resource-method-demo-classdiagram.png differ diff --git a/dependency-injection/docs/resource-method-demo-classdiagram.xml b/dependency-injection/docs/resource-method-demo-classdiagram.xml new file mode 100644 index 0000000000..94b39279fc --- /dev/null +++ b/dependency-injection/docs/resource-method-demo-classdiagram.xml @@ -0,0 +1 @@ +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 new file mode 100644 index 0000000000..46f57e512e --- /dev/null +++ b/dependency-injection/pom.xml @@ -0,0 +1,81 @@ + + + 4.0.0 + + com.baeldung + dependency-injection + 0.0.1-SNAPSHOT + war + + @Resource vs @Inject vs @Autowired + Accompanying the demonstration of the use of the annotations related to injection mechanisms, namely @Resource, @Inject, and @Autowired + + + + junit + junit + 4.11 + test + + + org.mockito + mockito-all + 1.10.19 + + + org.springframework + spring-test + 4.2.6.RELEASE + + + org.springframework + spring-core + 4.2.6.RELEASE + + + org.springframework + spring-beans + 4.2.6.RELEASE + + + org.springframework + spring-context + 4.2.6.RELEASE + + + javax.inject + javax.inject + 1 + + + + + + + maven-compiler-plugin + + 1.6 + 1.6 + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*Demo.java + + + + + + + + + java.net + https://maven.java.net/content/repositories/releases/ + + + diff --git a/dependency-injection/src/test/java/com/baeldung/autowired/FieldAutowiredDemo.java b/dependency-injection/src/test/java/com/baeldung/autowired/FieldAutowiredDemo.java new file mode 100644 index 0000000000..c71365097f --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/autowired/FieldAutowiredDemo.java @@ -0,0 +1,27 @@ +package com.baeldung.autowired; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.dependency.ArbitraryDependency; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations={ + "/applicationContextTest-@Autowired-Type.xml"}) +public class FieldAutowiredDemo { + + @Autowired + private ArbitraryDependency fieldDependency; + + @Test + public void fieldDependency_MUST_BE_AUTOWIRED_Correctly() { + assertNotNull(fieldDependency); + assertEquals("Arbitrary Dependency", fieldDependency.toString()); + } +} diff --git a/dependency-injection/src/test/java/com/baeldung/autowired/FieldAutowiredNameDemo.java b/dependency-injection/src/test/java/com/baeldung/autowired/FieldAutowiredNameDemo.java new file mode 100644 index 0000000000..c11ed5286a --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/autowired/FieldAutowiredNameDemo.java @@ -0,0 +1,27 @@ +package com.baeldung.autowired; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.dependency.ArbitraryDependency; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations={ + "/applicationContextTest-@Autowired-Name.xml"}) +public class FieldAutowiredNameDemo { + + @Autowired + private ArbitraryDependency autowiredFieldDependency; + + @Test + public void autowiredFieldDependency_MUST_BE_AUTOWIRED_Correctly() { + assertNotNull(autowiredFieldDependency); + assertEquals("Arbitrary Dependency", autowiredFieldDependency.toString()); + } +} diff --git a/dependency-injection/src/test/java/com/baeldung/autowired/FieldQualifierAutowiredDemo.java b/dependency-injection/src/test/java/com/baeldung/autowired/FieldQualifierAutowiredDemo.java new file mode 100644 index 0000000000..5afce6ab6a --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/autowired/FieldQualifierAutowiredDemo.java @@ -0,0 +1,39 @@ +package com.baeldung.autowired; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.dependency.ArbitraryDependency; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations={ + "/applicationContextTest-@Autowired-Qualifier.xml"}) +public class FieldQualifierAutowiredDemo { + + @Autowired + @Qualifier("autowiredFieldDependency") + private ArbitraryDependency fieldDependency1; + + @Autowired + @Qualifier("anotherAutowiredFieldDependency") + private ArbitraryDependency fieldDependency2; + + @Test + public void fieldDependency1_MUST_BE_AUTOWIRED_Correctly() { + assertNotNull(fieldDependency1); + assertEquals("Arbitrary Dependency", fieldDependency1.toString()); + } + + @Test + public void fieldDependency2_MUST_BE_AUTOWIRED_Correctly() { + assertNotNull(fieldDependency2); + assertEquals("Another Arbitrary Dependency", fieldDependency2.toString()); + } +} diff --git a/dependency-injection/src/test/java/com/baeldung/dependency/AnotherArbitraryDependency.java b/dependency-injection/src/test/java/com/baeldung/dependency/AnotherArbitraryDependency.java new file mode 100644 index 0000000000..27ba03f6e8 --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/dependency/AnotherArbitraryDependency.java @@ -0,0 +1,10 @@ +package com.baeldung.dependency; + +public class AnotherArbitraryDependency extends ArbitraryDependency { + + private final String label = "Another Arbitrary Dependency"; + + public String toString() { + return label; + } +} diff --git a/dependency-injection/src/test/java/com/baeldung/dependency/ArbitraryDependency.java b/dependency-injection/src/test/java/com/baeldung/dependency/ArbitraryDependency.java new file mode 100644 index 0000000000..bab289777c --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/dependency/ArbitraryDependency.java @@ -0,0 +1,10 @@ +package com.baeldung.dependency; + +public class ArbitraryDependency { + + private final String label = "Arbitrary Dependency"; + + public String toString() { + return label; + } +} diff --git a/dependency-injection/src/test/java/com/baeldung/dependency/YetAnotherArbitraryDependency.java b/dependency-injection/src/test/java/com/baeldung/dependency/YetAnotherArbitraryDependency.java new file mode 100644 index 0000000000..1f59500ec5 --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/dependency/YetAnotherArbitraryDependency.java @@ -0,0 +1,10 @@ +package com.baeldung.dependency; + +public class YetAnotherArbitraryDependency extends ArbitraryDependency { + + private final String label = "Yet Another Arbitrary Dependency"; + + public String toString() { + return label; + } +} diff --git a/dependency-injection/src/test/java/com/baeldung/inject/FieldByNameInjectDemo.java b/dependency-injection/src/test/java/com/baeldung/inject/FieldByNameInjectDemo.java new file mode 100644 index 0000000000..a670ee8313 --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/inject/FieldByNameInjectDemo.java @@ -0,0 +1,30 @@ +package com.baeldung.inject; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import javax.inject.Inject; +import javax.inject.Named; + +import 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-Name.xml"}) +public class FieldByNameInjectDemo { + + @Inject + @Named("yetAnotherFieldInjectDependency") + private ArbitraryDependency yetAnotherFieldInjectDependency; + + @Test + public void yetAnotherFieldInjectDependency_MUST_BE_INJECTED_Correctly() { + assertNotNull(yetAnotherFieldInjectDependency); + 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 new file mode 100644 index 0000000000..df40e516ba --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/inject/FieldInjectDemo.java @@ -0,0 +1,27 @@ +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/FieldQualifierInjectDemo.java b/dependency-injection/src/test/java/com/baeldung/inject/FieldQualifierInjectDemo.java new file mode 100644 index 0000000000..3cc9b643c7 --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/inject/FieldQualifierInjectDemo.java @@ -0,0 +1,40 @@ +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.beans.factory.annotation.Qualifier; +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-Qualifier.xml"}) +public class FieldQualifierInjectDemo { + + @Inject + @Qualifier("defaultFile") + private ArbitraryDependency defaultDependency; + + @Inject + @Qualifier("namedFile") + private ArbitraryDependency namedDependency; + + @Test + public void defaultDependency_MUST_BE_INJECTED_Successfully() { + assertNotNull(defaultDependency); + assertEquals("Arbitrary Dependency", defaultDependency.toString()); + } + + @Test + public void namedDependency_MUST_BE_INJECTED_Correctly() { + assertNotNull(defaultDependency); + 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 new file mode 100644 index 0000000000..fbb378d672 --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/resource/FieldResourceInjectionDemo.java @@ -0,0 +1,25 @@ +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/MethodByQualifierResourceDemo.java b/dependency-injection/src/test/java/com/baeldung/resource/MethodByQualifierResourceDemo.java new file mode 100644 index 0000000000..fcca34dc2f --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/resource/MethodByQualifierResourceDemo.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; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations={ + "/applicationContextTest-@Resource-Qualifier.xml"}) +public class MethodByQualifierResourceDemo { + + private File arbDependency; + private File anotherArbDependency; + + @Test + public void dependencies_MUST_BE_INJECTED_Correctly() { + assertNotNull(arbDependency); + assertEquals("namedFile.txt", arbDependency.getName()); + assertNotNull(anotherArbDependency); + assertEquals("defaultFile.txt", anotherArbDependency.getName()); + } + + @Resource + @Qualifier("namedFile") + public void setArbDependency(File arbDependency) { + this.arbDependency = arbDependency; + } + + @Resource + @Qualifier("defaultFile") + public void setAnotherArbDependency(File anotherArbDependency) { + this.anotherArbDependency = anotherArbDependency; + } +} diff --git a/dependency-injection/src/test/java/com/baeldung/resource/MethodByTypeResourceDemo.java b/dependency-injection/src/test/java/com/baeldung/resource/MethodByTypeResourceDemo.java new file mode 100644 index 0000000000..af6a805bd9 --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/resource/MethodByTypeResourceDemo.java @@ -0,0 +1,30 @@ +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 MethodByTypeResourceDemo { + + private File defaultFile; + + @Resource + protected void setDefaultFile(File defaultFile) { + this.defaultFile = defaultFile; + } + + @Test + public void defaultFile_MUST_BE_INJECTED_Correctly() { + assertNotNull(defaultFile); + } +} diff --git a/dependency-injection/src/test/java/com/baeldung/resource/MethodResourceInjectionDemo.java b/dependency-injection/src/test/java/com/baeldung/resource/MethodResourceInjectionDemo.java new file mode 100644 index 0000000000..d746fd4d85 --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/resource/MethodResourceInjectionDemo.java @@ -0,0 +1,29 @@ +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 MethodResourceInjectionDemo { + + private File defaultFile; + + @Resource(name="namedFile") + protected void setDefaultFile(File defaultFile) { + this.defaultFile = defaultFile; + } + + @Test + public void defaultFile_MUST_BE_INJECTED_Correctly() { + assertNotNull(defaultFile); + } +} diff --git a/dependency-injection/src/test/java/com/baeldung/resource/NamedResourceTest.java b/dependency-injection/src/test/java/com/baeldung/resource/NamedResourceTest.java new file mode 100644 index 0000000000..8b218dfe98 --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/resource/NamedResourceTest.java @@ -0,0 +1,27 @@ +package com.baeldung.resource; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; + +import javax.annotation.Resource; + +import 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 NamedResourceTest { + + @Resource(name="namedFile") + private File testFile; + + @Test + public void namedResource_MUST_FIND_SPECIFIED_File() { + assertNotNull(testFile); + assertTrue(testFile.getName().equals("namedFile.txt")); + } +} diff --git a/dependency-injection/src/test/java/com/baeldung/resource/QualifierResourceInjectionDemo.java b/dependency-injection/src/test/java/com/baeldung/resource/QualifierResourceInjectionDemo.java new file mode 100644 index 0000000000..0aaa2085d5 --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/resource/QualifierResourceInjectionDemo.java @@ -0,0 +1,35 @@ +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/SetterResourceInjectionDemo.java b/dependency-injection/src/test/java/com/baeldung/resource/SetterResourceInjectionDemo.java new file mode 100644 index 0000000000..25dd5bb9ff --- /dev/null +++ b/dependency-injection/src/test/java/com/baeldung/resource/SetterResourceInjectionDemo.java @@ -0,0 +1,29 @@ +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 SetterResourceInjectionDemo { + + private File defaultFile; + + @Resource + protected void setDefaultFile(File defaultFile) { + this.defaultFile = defaultFile; + } + + @Test + public void setter_MUST_INJECT_Resource() { + assertNotNull(defaultFile); + } +} diff --git a/dependency-injection/src/test/resources/applicationContextTest-@Autowired-Name.xml b/dependency-injection/src/test/resources/applicationContextTest-@Autowired-Name.xml new file mode 100644 index 0000000000..d7fe3abcb3 --- /dev/null +++ b/dependency-injection/src/test/resources/applicationContextTest-@Autowired-Name.xml @@ -0,0 +1,12 @@ + + + + + + diff --git a/dependency-injection/src/test/resources/applicationContextTest-@Autowired-Qualifier.xml b/dependency-injection/src/test/resources/applicationContextTest-@Autowired-Qualifier.xml new file mode 100644 index 0000000000..d7fe3abcb3 --- /dev/null +++ b/dependency-injection/src/test/resources/applicationContextTest-@Autowired-Qualifier.xml @@ -0,0 +1,12 @@ + + + + + + diff --git a/dependency-injection/src/test/resources/applicationContextTest-@Autowired-Type.xml b/dependency-injection/src/test/resources/applicationContextTest-@Autowired-Type.xml new file mode 100644 index 0000000000..c65b85ccf4 --- /dev/null +++ b/dependency-injection/src/test/resources/applicationContextTest-@Autowired-Type.xml @@ -0,0 +1,10 @@ + + + + diff --git a/dependency-injection/src/test/resources/applicationContextTest-@Inject-Name.xml b/dependency-injection/src/test/resources/applicationContextTest-@Inject-Name.xml new file mode 100644 index 0000000000..610e8687a7 --- /dev/null +++ b/dependency-injection/src/test/resources/applicationContextTest-@Inject-Name.xml @@ -0,0 +1,11 @@ + + + + + diff --git a/dependency-injection/src/test/resources/applicationContextTest-@Inject-Qualifier.xml b/dependency-injection/src/test/resources/applicationContextTest-@Inject-Qualifier.xml new file mode 100644 index 0000000000..627ec37914 --- /dev/null +++ b/dependency-injection/src/test/resources/applicationContextTest-@Inject-Qualifier.xml @@ -0,0 +1,12 @@ + + + + + + diff --git a/dependency-injection/src/test/resources/applicationContextTest-@Inject-Type.xml b/dependency-injection/src/test/resources/applicationContextTest-@Inject-Type.xml new file mode 100644 index 0000000000..405164e603 --- /dev/null +++ b/dependency-injection/src/test/resources/applicationContextTest-@Inject-Type.xml @@ -0,0 +1,10 @@ + + + + diff --git a/dependency-injection/src/test/resources/applicationContextTest-@Resource-NameType.xml b/dependency-injection/src/test/resources/applicationContextTest-@Resource-NameType.xml new file mode 100644 index 0000000000..82cbe97157 --- /dev/null +++ b/dependency-injection/src/test/resources/applicationContextTest-@Resource-NameType.xml @@ -0,0 +1,12 @@ + + + + + + diff --git a/dependency-injection/src/test/resources/applicationContextTest-@Resource-Qualifier.xml b/dependency-injection/src/test/resources/applicationContextTest-@Resource-Qualifier.xml new file mode 100644 index 0000000000..1680a6f66a --- /dev/null +++ b/dependency-injection/src/test/resources/applicationContextTest-@Resource-Qualifier.xml @@ -0,0 +1,16 @@ + + + + + + + + + + diff --git a/gatling/pom.xml b/gatling/pom.xml new file mode 100644 index 0000000000..273797d76c --- /dev/null +++ b/gatling/pom.xml @@ -0,0 +1,110 @@ + + + 4.0.0 + org.baeldung + gatling + 1.0-SNAPSHOT + + + 1.8 + 1.8 + 2.11.7 + UTF-8 + 2.2.0 + 3.2.2 + + + + + + io.gatling + gatling-app + ${gatling.version} + + + io.gatling + gatling-recorder + ${gatling.version} + + + io.gatling.highcharts + gatling-charts-highcharts + ${gatling.version} + + + org.scala-lang + scala-library + ${scala.version} + + + + + + + io.gatling.highcharts + gatling-charts-highcharts + + + io.gatling + gatling-app + + + io.gatling + gatling-recorder + + + org.scala-lang + scala-library + + + + + src/test/scala + + + + net.alchim31.maven + scala-maven-plugin + ${scala-maven-plugin.version} + + + + + + net.alchim31.maven + scala-maven-plugin + + + + testCompile + + + + -Ybackend:GenBCode + -Ydelambdafy:method + -target:jvm-1.8 + -deprecation + -feature + -unchecked + -language:implicitConversions + -language:postfixOps + + + + + + + io.gatling + gatling-maven-plugin + ${gatling.version} + + + test + execute + + + + + + diff --git a/gatling/src/test/resources/gatling.conf b/gatling/src/test/resources/gatling.conf new file mode 100644 index 0000000000..8bfa0ed366 --- /dev/null +++ b/gatling/src/test/resources/gatling.conf @@ -0,0 +1,127 @@ +######################### +# Gatling Configuration # +######################### + +# This file contains all the settings configurable for Gatling with their default values + +gatling { + core { + #outputDirectoryBaseName = "" # The prefix for each simulation result folder (then suffixed by the report generation timestamp) + #runDescription = "" # The description for this simulation run, displayed in each report + #encoding = "utf-8" # Encoding to use throughout Gatling for file and string manipulation + #simulationClass = "" # The FQCN of the simulation to run (when used in conjunction with noReports, the simulation for which assertions will be validated) + #mute = false # When set to true, don't ask for simulation name nor run description (currently only used by Gatling SBT plugin) + #elFileBodiesCacheMaxCapacity = 200 # Cache size for request body EL templates, set to 0 to disable + #rawFileBodiesCacheMaxCapacity = 200 # Cache size for request body Raw templates, set to 0 to disable + #rawFileBodiesInMemoryMaxSize = 1000 # Below this limit, raw file bodies will be cached in memory + + extract { + regex { + #cacheMaxCapacity = 200 # Cache size for the compiled regexes, set to 0 to disable caching + } + xpath { + #cacheMaxCapacity = 200 # Cache size for the compiled XPath queries, set to 0 to disable caching + } + jsonPath { + #cacheMaxCapacity = 200 # Cache size for the compiled jsonPath queries, set to 0 to disable caching + #preferJackson = false # When set to true, prefer Jackson over Boon for JSON-related operations + } + css { + #cacheMaxCapacity = 200 # Cache size for the compiled CSS selectors queries, set to 0 to disable caching + } + } + + directory { + #data = user-files/data # Folder where user's data (e.g. files used by Feeders) is located + #bodies = user-files/bodies # Folder where bodies are located + #simulations = user-files/simulations # Folder where the bundle's simulations are located + #reportsOnly = "" # If set, name of report folder to look for in order to generate its report + #binaries = "" # If set, name of the folder where compiles classes are located: Defaults to GATLING_HOME/target. + #results = results # Name of the folder where all reports folder are located + } + } + charting { + #noReports = false # When set to true, don't generate HTML reports + #maxPlotPerSeries = 1000 # Number of points per graph in Gatling reports + #useGroupDurationMetric = false # Switch group timings from cumulated response time to group duration. + indicators { + #lowerBound = 800 # Lower bound for the requests' response time to track in the reports and the console summary + #higherBound = 1200 # Higher bound for the requests' response time to track in the reports and the console summary + #percentile1 = 50 # Value for the 1st percentile to track in the reports, the console summary and Graphite + #percentile2 = 75 # Value for the 2nd percentile to track in the reports, the console summary and Graphite + #percentile3 = 95 # Value for the 3rd percentile to track in the reports, the console summary and Graphite + #percentile4 = 99 # Value for the 4th percentile to track in the reports, the console summary and Graphite + } + } + http { + #fetchedCssCacheMaxCapacity = 200 # Cache size for CSS parsed content, set to 0 to disable + #fetchedHtmlCacheMaxCapacity = 200 # Cache size for HTML parsed content, set to 0 to disable + #perUserCacheMaxCapacity = 200 # Per virtual user cache size, set to 0 to disable + #warmUpUrl = "http://gatling.io" # The URL to use to warm-up the HTTP stack (blank means disabled) + #enableGA = true # Very light Google Analytics, please support + ssl { + keyStore { + #type = "" # Type of SSLContext's KeyManagers store + #file = "" # Location of SSLContext's KeyManagers store + #password = "" # Password for SSLContext's KeyManagers store + #algorithm = "" # Algorithm used SSLContext's KeyManagers store + } + trustStore { + #type = "" # Type of SSLContext's TrustManagers store + #file = "" # Location of SSLContext's TrustManagers store + #password = "" # Password for SSLContext's TrustManagers store + #algorithm = "" # Algorithm used by SSLContext's TrustManagers store + } + } + ahc { + #keepAlive = true # Allow pooling HTTP connections (keep-alive header automatically added) + #connectTimeout = 60000 # Timeout when establishing a connection + #pooledConnectionIdleTimeout = 60000 # Timeout when a connection stays unused in the pool + #readTimeout = 60000 # Timeout when a used connection stays idle + #maxRetry = 2 # Number of times that a request should be tried again + #requestTimeout = 60000 # Timeout of the requests + #acceptAnyCertificate = true # When set to true, doesn't validate SSL certificates + #httpClientCodecMaxInitialLineLength = 4096 # Maximum length of the initial line of the response (e.g. "HTTP/1.0 200 OK") + #httpClientCodecMaxHeaderSize = 8192 # Maximum size, in bytes, of each request's headers + #httpClientCodecMaxChunkSize = 8192 # Maximum length of the content or each chunk + #webSocketMaxFrameSize = 10240000 # Maximum frame payload size + #sslEnabledProtocols = [TLSv1.2, TLSv1.1, TLSv1] # Array of enabled protocols for HTTPS, if empty use the JDK defaults + #sslEnabledCipherSuites = [] # Array of enabled cipher suites for HTTPS, if empty use the JDK defaults + #sslSessionCacheSize = 0 # SSLSession cache size, set to 0 to use JDK's default + #sslSessionTimeout = 0 # SSLSession timeout in seconds, set to 0 to use JDK's default (24h) + #useOpenSsl = false # if OpenSSL should be used instead of JSSE (requires tcnative jar) + #useNativeTransport = false # if native transport should be used instead of Java NIO (requires netty-transport-native-epoll, currently Linux only) + #usePooledMemory = true # if Gatling should use pooled memory + #tcpNoDelay = true + #soReuseAddress = false + #soLinger = -1 + #soSndBuf = -1 + #soRcvBuf = -1 + } + dns { + #queryTimeout = 5000 # Timeout of each DNS query in millis + #maxQueriesPerResolve = 3 # Maximum allowed number of DNS queries for a given name resolution + } + } + data { + #writers = [console, file] # The list of DataWriters to which Gatling write simulation data (currently supported : console, file, graphite, jdbc) + console { + #light = false # When set to true, displays a light version without detailed request stats + } + file { + #bufferSize = 8192 # FileDataWriter's internal data buffer size, in bytes + } + leak { + #noActivityTimeout = 30 # Period, in seconds, for which Gatling may have no activity before considering a leak may be happening + } + graphite { + #light = false # only send the all* stats + #host = "localhost" # The host where the Carbon server is located + #port = 2003 # The port to which the Carbon server listens to (2003 is default for plaintext, 2004 is default for pickle) + #protocol = "tcp" # The protocol used to send data to Carbon (currently supported : "tcp", "udp") + #rootPathPrefix = "gatling" # The common prefix of all metrics sent to Graphite + #bufferSize = 8192 # GraphiteDataWriter's internal data buffer size, in bytes + #writeInterval = 1 # GraphiteDataWriter's write interval, in seconds + } + } +} diff --git a/gatling/src/test/resources/logback.xml b/gatling/src/test/resources/logback.xml new file mode 100644 index 0000000000..b9ba6255a0 --- /dev/null +++ b/gatling/src/test/resources/logback.xml @@ -0,0 +1,22 @@ + + + + + + %d{HH:mm:ss.SSS} [%-5level] %logger{15} - %msg%n%rEx + false + + + + + + + + + + + + + + + diff --git a/gatling/src/test/resources/recorder.conf b/gatling/src/test/resources/recorder.conf new file mode 100644 index 0000000000..969b9e8668 --- /dev/null +++ b/gatling/src/test/resources/recorder.conf @@ -0,0 +1,53 @@ +recorder { + core { + #mode = "Proxy" + #encoding = "utf-8" # The encoding used for reading/writing request bodies and the generated simulation + #outputFolder = "" # The folder where generated simulation will we written + #package = "" # The package's name of the generated simulation + #className = "RecordedSimulation" # The name of the generated Simulation class + #thresholdForPauseCreation = 100 # The minimum time, in milliseconds, that must pass between requests to trigger a pause creation + #saveConfig = false # When set to true, the configuration from the Recorder GUI overwrites this configuration + #headless = false # When set to true, run the Recorder in headless mode instead of the GUI + #harFilePath = "" # The path of the HAR file to convert + } + filters { + #filterStrategy = "Disabled" # The selected filter resources filter strategy (currently supported : "Disabled", "BlackList", "WhiteList") + #whitelist = [] # The list of ressources patterns that are part of the Recorder's whitelist + #blacklist = [] # The list of ressources patterns that are part of the Recorder's blacklist + } + http { + #automaticReferer = true # When set to false, write the referer + enable 'disableAutoReferer' in the generated simulation + #followRedirect = true # When set to false, write redirect requests + enable 'disableFollowRedirect' in the generated simulation + #removeCacheHeaders = true # When set to true, removes from the generated requests headers leading to request caching + #inferHtmlResources = true # When set to true, add inferred resources + set 'inferHtmlResources' with the configured blacklist/whitelist in the generated simulation + #checkResponseBodies = false # When set to true, save response bodies as files and add raw checks in the generated simulation + } + proxy { + #port = 8000 # Local port used by Gatling's Proxy for HTTP/HTTPS + https { + #mode = "SelfSignedCertificate" # The selected "HTTPS mode" (currently supported : "SelfSignedCertificate", "ProvidedKeyStore", "GatlingCertificateAuthority", "CustomCertificateAuthority") + keyStore { + #path = "" # The path of the custom key store + #password = "" # The password for this key store + #type = "JKS" # The type of the key store (currently supported: "JKS") + } + certificateAuthority { + #certificatePath = "" # The path of the custom certificate + #privateKeyPath = "" # The certificate's private key path + } + } + outgoing { + #host = "" # The outgoing proxy's hostname + #username = "" # The username to use to connect to the outgoing proxy + #password = "" # The password corresponding to the user to use to connect to the outgoing proxy + #port = 0 # The HTTP port to use to connect to the outgoing proxy + #sslPort = 0 # If set, The HTTPS port to use to connect to the outgoing proxy + } + } + netty { + #maxInitialLineLength = 10000 # Maximum length of the initial line of the response (e.g. "HTTP/1.0 200 OK") + #maxHeaderSize = 20000 # Maximum size, in bytes, of each request's headers + #maxChunkSize = 8192 # Maximum length of the content or each chunk + #maxContentLength = 100000000 # Maximum length of the aggregated content of each response + } +} diff --git a/gatling/src/test/scala/Engine.scala b/gatling/src/test/scala/Engine.scala new file mode 100644 index 0000000000..32c85fbe45 --- /dev/null +++ b/gatling/src/test/scala/Engine.scala @@ -0,0 +1,13 @@ +import io.gatling.app.Gatling +import io.gatling.core.config.GatlingPropertiesBuilder + +object Engine extends App { + + val props = new GatlingPropertiesBuilder + props.dataDirectory(IDEPathHelper.dataDirectory.toString) + props.resultsDirectory(IDEPathHelper.resultsDirectory.toString) + props.bodiesDirectory(IDEPathHelper.bodiesDirectory.toString) + props.binariesDirectory(IDEPathHelper.mavenBinariesDirectory.toString) + + Gatling.fromMap(props.build) +} diff --git a/gatling/src/test/scala/IDEPathHelper.scala b/gatling/src/test/scala/IDEPathHelper.scala new file mode 100644 index 0000000000..0abf6a42ef --- /dev/null +++ b/gatling/src/test/scala/IDEPathHelper.scala @@ -0,0 +1,22 @@ +import java.nio.file.Path + +import io.gatling.commons.util.PathHelper._ + +object IDEPathHelper { + + val gatlingConfUrl: Path = getClass.getClassLoader.getResource("gatling.conf").toURI + val projectRootDir = gatlingConfUrl.ancestor(3) + + val mavenSourcesDirectory = projectRootDir / "src" / "test" / "scala" + val mavenResourcesDirectory = projectRootDir / "src" / "test" / "resources" + val mavenTargetDirectory = projectRootDir / "target" + val mavenBinariesDirectory = mavenTargetDirectory / "test-classes" + + val dataDirectory = mavenResourcesDirectory / "data" + val bodiesDirectory = mavenResourcesDirectory / "bodies" + + val recorderOutputDirectory = mavenSourcesDirectory + val resultsDirectory = mavenTargetDirectory / "gatling" + + val recorderConfigFile = mavenResourcesDirectory / "recorder.conf" +} diff --git a/gatling/src/test/scala/Recorder.scala b/gatling/src/test/scala/Recorder.scala new file mode 100644 index 0000000000..6ad320618b --- /dev/null +++ b/gatling/src/test/scala/Recorder.scala @@ -0,0 +1,12 @@ +import io.gatling.recorder.GatlingRecorder +import io.gatling.recorder.config.RecorderPropertiesBuilder + +object Recorder extends App { + + val props = new RecorderPropertiesBuilder + props.simulationOutputFolder(IDEPathHelper.recorderOutputDirectory.toString) + props.simulationPackage("org.baeldung") + props.bodiesFolder(IDEPathHelper.bodiesDirectory.toString) + + GatlingRecorder.fromMap(props.build, Some(IDEPathHelper.recorderConfigFile)) +} diff --git a/gatling/src/test/scala/org/baeldung/RecordedSimulation.scala b/gatling/src/test/scala/org/baeldung/RecordedSimulation.scala new file mode 100644 index 0000000000..cdbef1bf3c --- /dev/null +++ b/gatling/src/test/scala/org/baeldung/RecordedSimulation.scala @@ -0,0 +1,46 @@ +package org.baeldung + +import scala.concurrent.duration._ + +import io.gatling.core.Predef._ +import io.gatling.http.Predef._ +import io.gatling.jdbc.Predef._ + +class RecordedSimulation extends Simulation { + + val httpProtocol = http + .baseURL("http://computer-database.gatling.io") + .inferHtmlResources(BlackList(""".*\.css""", """.*\.js""", """.*\.ico"""), WhiteList()) + .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") + .acceptEncodingHeader("gzip, deflate") + .acceptLanguageHeader("it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3") + .userAgentHeader("Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0") + + + + + + val scn = scenario("RecordedSimulation") + .exec(http("request_0") + .get("/")) + .pause(5) + .exec(http("request_1") + .get("/computers?f=amstrad")) + .pause(4) + .exec(http("request_2") + .get("/computers/412")) + .pause(2) + .exec(http("request_3") + .get("/")) + .pause(2) + .exec(http("request_4") + .get("/computers?p=1")) + .pause(1) + .exec(http("request_5") + .get("/computers?p=2")) + .pause(2) + .exec(http("request_6") + .get("/computers?p=3")) + + setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol) +} diff --git a/gson-jackson-performance/.classpath b/gson-jackson-performance/.classpath new file mode 100644 index 0000000000..19b2ff18be --- /dev/null +++ b/gson-jackson-performance/.classpath @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/gson-jackson-performance/.project b/gson-jackson-performance/.project new file mode 100644 index 0000000000..b3b5ccad87 --- /dev/null +++ b/gson-jackson-performance/.project @@ -0,0 +1,14 @@ + + + gson-jackson-performance + NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse. + + + + org.eclipse.jdt.core.javabuilder + + + + org.eclipse.jdt.core.javanature + + \ No newline at end of file diff --git a/gson-jackson-performance/README.md b/gson-jackson-performance/README.md new file mode 100644 index 0000000000..5b08f6cdec --- /dev/null +++ b/gson-jackson-performance/README.md @@ -0,0 +1,3 @@ +## Performance of Gson and Jackson + +Standalone java programs to measure the performance of both JSON APIs based on file size and object graph complexity. diff --git a/gson-jackson-performance/pom.xml b/gson-jackson-performance/pom.xml new file mode 100644 index 0000000000..1ea43bd7fa --- /dev/null +++ b/gson-jackson-performance/pom.xml @@ -0,0 +1,72 @@ + + 4.0.0 + + com.baeldung + gson-jackson-performance + 0.0.1-SNAPSHOT + jar + + gson-jackson-performance + http://maven.apache.org + + + UTF-8 + + + + gson-jackson-performance + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + + + + com.google.code.gson + gson + 2.5 + + + + com.fasterxml.jackson.core + jackson-databind + 2.7.1-1 + + + + junit + junit + 3.8.1 + test + + + + log4j + log4j + 1.2.17 + + + + + + + diff --git a/gson-jackson-performance/src/main/java/com/baeldung/data/complex/ComplexDataGeneration.java b/gson-jackson-performance/src/main/java/com/baeldung/data/complex/ComplexDataGeneration.java new file mode 100644 index 0000000000..b69d306edf --- /dev/null +++ b/gson-jackson-performance/src/main/java/com/baeldung/data/complex/ComplexDataGeneration.java @@ -0,0 +1,63 @@ +package com.baeldung.data.complex; + +import java.util.ArrayList; +import java.util.List; + +import com.baeldung.data.utility.Utility; +import com.baeldung.pojo.complex.AddressDetails; +import com.baeldung.pojo.complex.ContactDetails; +import com.baeldung.pojo.complex.CustomerAddressDetails; +import com.baeldung.pojo.complex.CustomerPortfolioComplex; + +/** + * + * This class is responsible for generating data for complex type object + */ + +public class ComplexDataGeneration { + + public static CustomerPortfolioComplex generateData() { + List customerAddressDetailsList = new ArrayList(); + for (int i = 0; i < 600000; i++) { + CustomerAddressDetails customerAddressDetails = new CustomerAddressDetails(); + customerAddressDetails.setAge(20); + customerAddressDetails.setFirstName(Utility.generateRandomName()); + customerAddressDetails.setLastName(Utility.generateRandomName()); + + List addressDetailsList = new ArrayList(); + customerAddressDetails.setAddressDetails(addressDetailsList); + + for (int j = 0; j < 2; j++) { + AddressDetails addressDetails = new AddressDetails(); + addressDetails.setZipcode(Utility.generateRandomZip()); + addressDetails.setAddress(Utility.generateRandomAddress()); + + List contactDetailsList = new ArrayList(); + addressDetails.setContactDetails(contactDetailsList); + + for (int k = 0; k < 2; k++) { + ContactDetails contactDetails = new ContactDetails(); + contactDetails.setLandline(Utility.generateRandomPhone()); + contactDetails.setMobile(Utility.generateRandomPhone()); + contactDetailsList.add(contactDetails); + contactDetails = null; + } + + addressDetailsList.add(addressDetails); + addressDetails = null; + contactDetailsList = null; + } + customerAddressDetailsList.add(customerAddressDetails); + customerAddressDetails = null; + + if (i % 6000 == 0) { + Runtime.getRuntime().gc(); + } + } + + CustomerPortfolioComplex customerPortfolioComplex = new CustomerPortfolioComplex(); + customerPortfolioComplex.setCustomerAddressDetailsList(customerAddressDetailsList); + customerAddressDetailsList = null; + return customerPortfolioComplex; + } +} diff --git a/gson-jackson-performance/src/main/java/com/baeldung/data/complex/ComplexDataGson.java b/gson-jackson-performance/src/main/java/com/baeldung/data/complex/ComplexDataGson.java new file mode 100644 index 0000000000..b97893e8f1 --- /dev/null +++ b/gson-jackson-performance/src/main/java/com/baeldung/data/complex/ComplexDataGson.java @@ -0,0 +1,91 @@ +package com.baeldung.data.complex; + +import java.util.Map; + +import org.apache.log4j.Logger; +import com.baeldung.data.utility.Utility; +import com.baeldung.pojo.complex.CustomerPortfolioComplex; + +import com.google.gson.Gson; + +/** + * + * This class is responsible for performing functions for Complex type + * GSON like + * Java to Json + * Json to Map + * Json to Java Object + */ + +public class ComplexDataGson { + + private static final Logger logger = Logger.getLogger(ComplexDataGson.class); + + static Gson gson = new Gson(); + + static long generateJsonAvgTime = 0L; + + static long parseJsonToMapAvgTime = 0L; + + static long parseJsonToActualObjectAvgTime = 0L; + + public static void main(String[] args) { + CustomerPortfolioComplex customerPortfolioComplex = ComplexDataGeneration.generateData(); + int j = 50; + for (int i = 0; i < j; i++) { + logger.info("-------Round " + (i + 1)); + String jsonStr = generateJson(customerPortfolioComplex); + logger.info("Size of Complex content Jackson :: " + Utility.bytesIntoMB(jsonStr.getBytes().length)); + logger.info("--------------------------------------------------------------------------"); + parseJsonToMap(jsonStr); + parseJsonToActualObject(jsonStr); + jsonStr = null; + } + + generateJsonAvgTime = generateJsonAvgTime / j; + parseJsonToMapAvgTime = parseJsonToMapAvgTime / j; + parseJsonToActualObjectAvgTime = parseJsonToActualObjectAvgTime / j; + + logger.info("--------------------------------------------------------------------------"); + logger.info("Average Time to Generate JSON : " + generateJsonAvgTime); + logger.info("Average Time to Parse JSON To Map : " + parseJsonToMapAvgTime); + logger.info("Average Time to Parse JSON To Actual Object : " + parseJsonToActualObjectAvgTime); + logger.info("--------------------------------------------------------------------------"); + } + + private static String generateJson(CustomerPortfolioComplex customerPortfolioComplex) { + Runtime.getRuntime().gc(); + long startParsTime = System.nanoTime(); + String json = gson.toJson(customerPortfolioComplex); + long endParsTime = System.nanoTime(); + long elapsedTime = endParsTime - startParsTime; + generateJsonAvgTime = generateJsonAvgTime + elapsedTime; + logger.info("Json Generation Time(ms):: " + elapsedTime); + return json; + } + + private static void parseJsonToMap(String jsonStr) { + long startParsTime = System.nanoTime(); + Map parsedMap = gson.fromJson(jsonStr , Map.class); + long endParsTime = System.nanoTime(); + long elapsedTime = endParsTime - startParsTime; + parseJsonToMapAvgTime = parseJsonToMapAvgTime + elapsedTime; + logger.info("Generating Map from json Time(ms):: " + elapsedTime); + logger.info("--------------------------------------------------------------------------"); + parsedMap = null; + Runtime.getRuntime().gc(); + } + + private static void parseJsonToActualObject(String jsonStr) { + long startParsTime = System.nanoTime(); + CustomerPortfolioComplex customerPortfolioComplex = gson.fromJson(jsonStr , CustomerPortfolioComplex.class); + long endParsTime = System.nanoTime(); + long elapsedTime = endParsTime - startParsTime; + parseJsonToActualObjectAvgTime = parseJsonToActualObjectAvgTime + elapsedTime; + logger.info("Generating Actual Object from json Time(ms):: " + elapsedTime); + logger.info("--------------------------------------------------------------------------"); + customerPortfolioComplex = null; + Runtime.getRuntime().gc(); + + } +} diff --git a/gson-jackson-performance/src/main/java/com/baeldung/data/complex/ComplexDataJackson.java b/gson-jackson-performance/src/main/java/com/baeldung/data/complex/ComplexDataJackson.java new file mode 100644 index 0000000000..07a210fb37 --- /dev/null +++ b/gson-jackson-performance/src/main/java/com/baeldung/data/complex/ComplexDataJackson.java @@ -0,0 +1,95 @@ +package com.baeldung.data.complex; + +import java.io.IOException; +import java.util.Map; + +import com.baeldung.data.utility.Utility; +import org.apache.log4j.Logger; +import com.baeldung.pojo.complex.CustomerPortfolioComplex; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * + * This class is responsible for performing functions for Complex type + * Jackson like + * Java to Json + * Json to Map + * Json to Java Object + */ + +public class ComplexDataJackson { + + private static final Logger logger = Logger.getLogger(ComplexDataJackson.class); + + static ObjectMapper mapper = new ObjectMapper(); + + static long generateJsonAvgTime = 0L; + + static long parseJsonToMapAvgTime = 0L; + + static long parseJsonToActualObjectAvgTime = 0L; + + public static void main(String[] args) throws IOException { + CustomerPortfolioComplex customerPortfolioComplex = ComplexDataGeneration.generateData(); + int j = 50; + for (int i = 0; i < j; i++) { + logger.info("-------Round " + (i + 1)); + String jsonStr = generateJson(customerPortfolioComplex); + logger.info("Size of Complex content Jackson :: " + Utility.bytesIntoMB(jsonStr.getBytes().length)); + logger.info("--------------------------------------------------------------------------"); + parseJsonToMap(jsonStr); + parseJsonToActualObject(jsonStr); + jsonStr = null; + } + + generateJsonAvgTime = generateJsonAvgTime / j; + parseJsonToMapAvgTime = parseJsonToMapAvgTime / j; + parseJsonToActualObjectAvgTime = parseJsonToActualObjectAvgTime / j; + + logger.info("--------------------------------------------------------------------------"); + logger.info("Average Time to Generate JSON : " + generateJsonAvgTime); + logger.info("Average Time to Parse JSON To Map : " + parseJsonToMapAvgTime); + logger.info("Average Time to Parse JSON To Actual Object : " + parseJsonToActualObjectAvgTime); + logger.info("--------------------------------------------------------------------------"); + } + + private static String generateJson(CustomerPortfolioComplex customerPortfolioComplex) + throws JsonProcessingException { + Runtime.getRuntime().gc(); + long startParsTime = System.nanoTime(); + String json = mapper.writeValueAsString(customerPortfolioComplex); + long endParsTime = System.nanoTime(); + long elapsedTime = endParsTime - startParsTime; + generateJsonAvgTime = generateJsonAvgTime + elapsedTime; + logger.info("Json Generation Time(ms):: " + elapsedTime); + return json; + } + + private static void parseJsonToMap(String jsonStr) throws JsonParseException , JsonMappingException , IOException { + long startParsTime = System.nanoTime(); + Map parsedMap = mapper.readValue(jsonStr , Map.class); + long endParsTime = System.nanoTime(); + long elapsedTime = endParsTime - startParsTime; + parseJsonToMapAvgTime = parseJsonToMapAvgTime + elapsedTime; + logger.info("Generating Map from json Time(ms):: " + elapsedTime); + parsedMap = null; + Runtime.getRuntime().gc(); + + } + + private static void parseJsonToActualObject(String jsonStr) throws JsonParseException , JsonMappingException , + IOException { + long startParsTime = System.nanoTime(); + CustomerPortfolioComplex customerPortfolioComplex = mapper.readValue(jsonStr , CustomerPortfolioComplex.class); + long endParsTime = System.nanoTime(); + long elapsedTime = endParsTime - startParsTime; + parseJsonToActualObjectAvgTime = parseJsonToActualObjectAvgTime + elapsedTime; + logger.info("Generating Actual Object from json Time(ms):: " + elapsedTime); + customerPortfolioComplex = null; + Runtime.getRuntime().gc(); + } +} diff --git a/gson-jackson-performance/src/main/java/com/baeldung/data/simple/SimpleDataGeneration.java b/gson-jackson-performance/src/main/java/com/baeldung/data/simple/SimpleDataGeneration.java new file mode 100644 index 0000000000..1e186adc72 --- /dev/null +++ b/gson-jackson-performance/src/main/java/com/baeldung/data/simple/SimpleDataGeneration.java @@ -0,0 +1,35 @@ +package com.baeldung.data.simple; + +import java.util.ArrayList; +import java.util.List; + +import com.baeldung.data.utility.Utility; +import com.baeldung.pojo.simple.Customer; +import com.baeldung.pojo.simple.CustomerPortfolioSimple; + +/** + * + * This class is responsible for generating data for simple type object + */ + +public class SimpleDataGeneration { + + public static CustomerPortfolioSimple generateData() { + List customerList = new ArrayList(); + for (int i = 0; i < 1; i++) { + Customer customer = new Customer(); + customer.setAge(20); + customer.setFirstName(Utility.generateRandomName()); + customer.setLastName(Utility.generateRandomName()); + + customerList.add(customer); + customer = null; + + } + + CustomerPortfolioSimple customerPortfolioSimple = new CustomerPortfolioSimple(); + customerPortfolioSimple.setCustomerLists(customerList); + customerList = null; + return customerPortfolioSimple; + } +} diff --git a/gson-jackson-performance/src/main/java/com/baeldung/data/simple/SimpleDataGson.java b/gson-jackson-performance/src/main/java/com/baeldung/data/simple/SimpleDataGson.java new file mode 100644 index 0000000000..b190313462 --- /dev/null +++ b/gson-jackson-performance/src/main/java/com/baeldung/data/simple/SimpleDataGson.java @@ -0,0 +1,91 @@ +package com.baeldung.data.simple; + +import java.util.Map; + +import org.apache.log4j.Logger; +import com.baeldung.data.utility.Utility; +import com.baeldung.pojo.simple.CustomerPortfolioSimple; + +import com.google.gson.Gson; + +/** + * + * This class is responsible for performing functions for Simple type + * GSON like + * Java to Json + * Json to Map + * Json to Java Object + */ + +public class SimpleDataGson { + + private static final Logger logger = Logger.getLogger(SimpleDataGson.class); + + static Gson gson = new Gson(); + + static long generateJsonAvgTime = 0L; + + static long parseJsonToMapAvgTime = 0L; + + static long parseJsonToActualObjectAvgTime = 0L; + + public static void main(String[] args) { + CustomerPortfolioSimple customerPortfolioSimple = SimpleDataGeneration.generateData(); + String jsonStr = null; + int j = 5; + for (int i = 0; i < j; i++) { + logger.info("-------Round " + (i + 1)); + jsonStr = generateJson(customerPortfolioSimple); + logger.info("Size of Simple content Gson :: " + Utility.bytesIntoMB(jsonStr.getBytes().length)); + logger.info("--------------------------------------------------------------------------"); + parseJsonToMap(jsonStr); + parseJsonToActualObject(jsonStr); + jsonStr = null; + } + generateJsonAvgTime = generateJsonAvgTime / j; + parseJsonToMapAvgTime = parseJsonToMapAvgTime / j; + parseJsonToActualObjectAvgTime = parseJsonToActualObjectAvgTime / j; + + logger.info("--------------------------------------------------------------------------"); + logger.info("Average Time to Generate JSON : " + generateJsonAvgTime); + logger.info("Average Time to Parse JSON To Map : " + parseJsonToMapAvgTime); + logger.info("Average Time to Parse JSON To Actual Object : " + parseJsonToActualObjectAvgTime); + logger.info("--------------------------------------------------------------------------"); + } + + private static String generateJson(CustomerPortfolioSimple customerPortfolioSimple) { + Runtime.getRuntime().gc(); + long startParsTime = System.nanoTime(); + String json = gson.toJson(customerPortfolioSimple); + long endParsTime = System.nanoTime(); + long elapsedTime = endParsTime - startParsTime; + generateJsonAvgTime = generateJsonAvgTime + elapsedTime; + logger.info("Json Generation Time(ms):: " + elapsedTime); + return json; + } + + private static void parseJsonToMap(String jsonStr) { + long startParsTime = System.nanoTime(); + Map parsedMap = gson.fromJson(jsonStr , Map.class); + long endParsTime = System.nanoTime(); + long elapsedTime = endParsTime - startParsTime; + parseJsonToMapAvgTime = parseJsonToMapAvgTime + elapsedTime; + logger.info("Generating Map from json Time(ms):: " + elapsedTime); + logger.info("--------------------------------------------------------------------------"); + parsedMap = null; + Runtime.getRuntime().gc(); + + } + + private static void parseJsonToActualObject(String jsonStr) { + long startParsTime = System.nanoTime(); + CustomerPortfolioSimple customerPortfolioSimple = gson.fromJson(jsonStr , CustomerPortfolioSimple.class); + long endParsTime = System.nanoTime(); + long elapsedTime = endParsTime - startParsTime; + parseJsonToActualObjectAvgTime = parseJsonToActualObjectAvgTime + elapsedTime; + logger.info("Generating Actual Object from json Time(ms):: " + elapsedTime); + logger.info("--------------------------------------------------------------------------"); + customerPortfolioSimple = null; + Runtime.getRuntime().gc(); + } +} diff --git a/gson-jackson-performance/src/main/java/com/baeldung/data/simple/SimpleDataJackson.java b/gson-jackson-performance/src/main/java/com/baeldung/data/simple/SimpleDataJackson.java new file mode 100644 index 0000000000..9330333604 --- /dev/null +++ b/gson-jackson-performance/src/main/java/com/baeldung/data/simple/SimpleDataJackson.java @@ -0,0 +1,95 @@ +package com.baeldung.data.simple; + +import java.io.IOException; +import java.util.Map; + +import com.baeldung.data.utility.Utility; +import org.apache.log4j.Logger; +import com.baeldung.pojo.simple.CustomerPortfolioSimple; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * + * This class is responsible for performing functions for Simple type + * Jackson like + * Java to Json + * Json to Map + * Json to Java Object + */ + +public class SimpleDataJackson { + + private static final Logger logger = Logger.getLogger(SimpleDataJackson.class); + + static ObjectMapper mapper = new ObjectMapper(); + + static long generateJsonAvgTime = 0L; + + static long parseJsonToMapAvgTime = 0L; + + static long parseJsonToActualObjectAvgTime = 0L; + + public static void main(String[] args) throws IOException { + CustomerPortfolioSimple customerPortfolioSimple = SimpleDataGeneration.generateData(); + int j = 50; + for (int i = 0; i < j; i++) { + logger.info("-------Round " + (i + 1)); + String jsonStr = generateJson(customerPortfolioSimple); + logger.info("Size of Simple content Jackson :: " + Utility.bytesIntoMB(jsonStr.getBytes().length)); + logger.info("--------------------------------------------------------------------------"); + + parseJsonToMap(jsonStr); + parseJsonToActualObject(jsonStr); + jsonStr = null; + } + + generateJsonAvgTime = generateJsonAvgTime / j; + parseJsonToMapAvgTime = parseJsonToMapAvgTime / j; + parseJsonToActualObjectAvgTime = parseJsonToActualObjectAvgTime / j; + + logger.info("--------------------------------------------------------------------------"); + logger.info("Average Time to Generate JSON : " + generateJsonAvgTime); + logger.info("Average Time to Parse JSON To Map : " + parseJsonToMapAvgTime); + logger.info("Average Time to Parse JSON To Actual Object : " + parseJsonToActualObjectAvgTime); + logger.info("--------------------------------------------------------------------------"); + } + + private static String generateJson(CustomerPortfolioSimple customerPortfolioSimple) throws JsonProcessingException { + Runtime.getRuntime().gc(); + long startParsTime = System.nanoTime(); + String json = mapper.writeValueAsString(customerPortfolioSimple); + long endParsTime = System.nanoTime(); + long elapsedTime = endParsTime - startParsTime; + generateJsonAvgTime = generateJsonAvgTime + elapsedTime; + logger.info("Json Generation Time(ms):: " + elapsedTime); + return json; + } + + private static void parseJsonToMap(String jsonStr) throws JsonParseException , JsonMappingException , IOException { + long startParsTime = System.nanoTime(); + Map parsedMap = mapper.readValue(jsonStr , Map.class); + long endParsTime = System.nanoTime(); + long elapsedTime = endParsTime - startParsTime; + parseJsonToMapAvgTime = parseJsonToMapAvgTime + elapsedTime; + logger.info("Generating Map from json Time(ms):: " + elapsedTime); + logger.info("--------------------------------------------------------------------------"); + parsedMap = null; + Runtime.getRuntime().gc(); + } + + private static void parseJsonToActualObject(String jsonStr) throws JsonParseException , JsonMappingException , + IOException { + long startParsTime = System.nanoTime(); + CustomerPortfolioSimple customerPortfolioSimple = mapper.readValue(jsonStr , CustomerPortfolioSimple.class); + long endParsTime = System.nanoTime(); + long elapsedTime = endParsTime - startParsTime; + parseJsonToActualObjectAvgTime = parseJsonToActualObjectAvgTime + elapsedTime; + logger.info("Generating Actual Object from json Time(ms):: " + elapsedTime); + customerPortfolioSimple = null; + Runtime.getRuntime().gc(); + } +} diff --git a/gson-jackson-performance/src/main/java/com/baeldung/data/utility/Utility.java b/gson-jackson-performance/src/main/java/com/baeldung/data/utility/Utility.java new file mode 100644 index 0000000000..8b0c402e47 --- /dev/null +++ b/gson-jackson-performance/src/main/java/com/baeldung/data/utility/Utility.java @@ -0,0 +1,90 @@ +package com.baeldung.data.utility; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.Random; + +public class Utility { + + public static String generateRandomName() { + char[] chars = "abcdefghijklmnopqrstuvwxyz ".toCharArray(); + StringBuilder sb = new StringBuilder(); + Random random = new Random(); + for (int i = 0; i < 8; i++) { + char c = chars[random.nextInt(chars.length)]; + sb.append(c); + } + return sb.toString(); + } + + public static String generateRandomAddress() { + char[] chars = "abcdefghijklmnopqrstuvwxyz ".toCharArray(); + StringBuilder sb = new StringBuilder(); + Random random = new Random(); + for (int i = 0; i < 30; i++) { + char c = chars[random.nextInt(chars.length)]; + sb.append(c); + } + return sb.toString(); + } + + public static String generateRandomZip() { + char[] chars = "1234567890".toCharArray(); + StringBuilder sb = new StringBuilder(); + Random random = new Random(); + for (int i = 0; i < 8; i++) { + char c = chars[random.nextInt(chars.length)]; + sb.append(c); + } + return sb.toString(); + } + + public static String generateRandomPhone() { + char[] chars = "1234567890".toCharArray(); + StringBuilder sb = new StringBuilder(); + Random random = new Random(); + for (int i = 0; i < 10; i++) { + char c = chars[random.nextInt(chars.length)]; + sb.append(c); + } + return sb.toString(); + } + + public static String readFile(String fileName , Class clazz) throws IOException { + String dir = clazz.getResource("/").getFile(); + dir = dir + "/" + fileName; + + BufferedReader br = new BufferedReader(new FileReader(dir)); + try { + StringBuilder sb = new StringBuilder(); + String line = br.readLine(); + + while (line != null) { + sb.append(line); + sb.append(System.lineSeparator()); + line = br.readLine(); + } + return sb.toString(); + } finally { + br.close(); + } + } + + public static String bytesIntoMB(long bytes) { + long kilobyte = 1024; + long megabyte = kilobyte * 1024; + + if ((bytes >= 0) && (bytes < kilobyte)) { + return bytes + " B"; + + } else if ((bytes >= kilobyte) && (bytes < megabyte)) { + return (bytes / kilobyte) + " KB"; + + } else if ((bytes >= megabyte)) { + return (bytes / megabyte) + " MB"; + } + + return null; + } +} diff --git a/gson-jackson-performance/src/main/java/com/baeldung/pojo/complex/AddressDetails.java b/gson-jackson-performance/src/main/java/com/baeldung/pojo/complex/AddressDetails.java new file mode 100644 index 0000000000..79725a97bd --- /dev/null +++ b/gson-jackson-performance/src/main/java/com/baeldung/pojo/complex/AddressDetails.java @@ -0,0 +1,37 @@ +package com.baeldung.pojo.complex; + +import java.util.List; + +public class AddressDetails { + + private String address; + + private String zipcode; + + private List contactDetails; + + public String getZipcode() { + return zipcode; + } + + public void setZipcode(String zipcode) { + this.zipcode = zipcode; + } + + public List getContactDetails() { + return contactDetails; + } + + public void setContactDetails(List contactDetails) { + this.contactDetails = contactDetails; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + +} diff --git a/gson-jackson-performance/src/main/java/com/baeldung/pojo/complex/ContactDetails.java b/gson-jackson-performance/src/main/java/com/baeldung/pojo/complex/ContactDetails.java new file mode 100644 index 0000000000..164fe3f470 --- /dev/null +++ b/gson-jackson-performance/src/main/java/com/baeldung/pojo/complex/ContactDetails.java @@ -0,0 +1,25 @@ +package com.baeldung.pojo.complex; + +public class ContactDetails { + + private String mobile; + + private String landline; + + public String getMobile() { + return mobile; + } + + public void setMobile(String mobile) { + this.mobile = mobile; + } + + public String getLandline() { + return landline; + } + + public void setLandline(String landline) { + this.landline = landline; + } + +} diff --git a/gson-jackson-performance/src/main/java/com/baeldung/pojo/complex/CustomerAddressDetails.java b/gson-jackson-performance/src/main/java/com/baeldung/pojo/complex/CustomerAddressDetails.java new file mode 100644 index 0000000000..9bc5951716 --- /dev/null +++ b/gson-jackson-performance/src/main/java/com/baeldung/pojo/complex/CustomerAddressDetails.java @@ -0,0 +1,18 @@ +package com.baeldung.pojo.complex; + +import java.util.List; + +import com.baeldung.pojo.simple.Customer; + +public class CustomerAddressDetails extends Customer { + + private List addressDetails; + + public List getAddressDetails() { + return addressDetails; + } + + public void setAddressDetails(List addressDetails) { + this.addressDetails = addressDetails; + } +} diff --git a/gson-jackson-performance/src/main/java/com/baeldung/pojo/complex/CustomerPortfolioComplex.java b/gson-jackson-performance/src/main/java/com/baeldung/pojo/complex/CustomerPortfolioComplex.java new file mode 100644 index 0000000000..13f9d240b8 --- /dev/null +++ b/gson-jackson-performance/src/main/java/com/baeldung/pojo/complex/CustomerPortfolioComplex.java @@ -0,0 +1,17 @@ +package com.baeldung.pojo.complex; + +import java.util.List; + +public class CustomerPortfolioComplex { + + private List customerAddressDetailsList; + + public List getCustomerAddressDetailsList() { + return customerAddressDetailsList; + } + + public void setCustomerAddressDetailsList(List customerAddressDetailsList) { + this.customerAddressDetailsList = customerAddressDetailsList; + } + +} diff --git a/gson-jackson-performance/src/main/java/com/baeldung/pojo/simple/Customer.java b/gson-jackson-performance/src/main/java/com/baeldung/pojo/simple/Customer.java new file mode 100644 index 0000000000..3d5668f85a --- /dev/null +++ b/gson-jackson-performance/src/main/java/com/baeldung/pojo/simple/Customer.java @@ -0,0 +1,36 @@ +package com.baeldung.pojo.simple; + + +public class Customer { + + private String firstName; + + private String lastName; + + private int age; + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + +} diff --git a/gson-jackson-performance/src/main/java/com/baeldung/pojo/simple/CustomerPortfolioSimple.java b/gson-jackson-performance/src/main/java/com/baeldung/pojo/simple/CustomerPortfolioSimple.java new file mode 100644 index 0000000000..7cb684813c --- /dev/null +++ b/gson-jackson-performance/src/main/java/com/baeldung/pojo/simple/CustomerPortfolioSimple.java @@ -0,0 +1,16 @@ +package com.baeldung.pojo.simple; + +import java.util.List; + +public class CustomerPortfolioSimple { + + private List customerLists; + + public List getCustomerLists() { + return customerLists; + } + + public void setCustomerLists(List customerLists) { + this.customerLists = customerLists; + } +} diff --git a/gson-jackson-performance/src/main/resources/log4j.properties b/gson-jackson-performance/src/main/resources/log4j.properties new file mode 100644 index 0000000000..371cbc9048 --- /dev/null +++ b/gson-jackson-performance/src/main/resources/log4j.properties @@ -0,0 +1,16 @@ +# Root logger option +log4j.rootLogger=DEBUG, file + +# Redirect log messages to console +# log4j.appender.stdout=org.apache.log4j.ConsoleAppender +# log4j.appender.stdout.Target=System.out +# log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +# log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n + +# Redirect log messages to a log file, support file rolling. +log4j.appender.file=org.apache.log4j.RollingFileAppender +log4j.appender.file.File=log4j-application.log +log4j.appender.file.MaxFileSize=5MB +log4j.appender.file.MaxBackupIndex=10 +log4j.appender.file.layout=org.apache.log4j.PatternLayout +log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n \ No newline at end of file diff --git a/gson/README.md b/gson/README.md index 559e536d81..67651b732e 100644 --- a/gson/README.md +++ b/gson/README.md @@ -2,5 +2,6 @@ ## GSON Cookbooks and Examples -### Relevant Articles: +### Relevant Articles: +- [Gson Deserialization Cookbook](http://www.baeldung.com/gson-deserialization-guide) diff --git a/gson/pom.xml b/gson/pom.xml index 500d14c40c..4f331f6fd9 100644 --- a/gson/pom.xml +++ b/gson/pom.xml @@ -1,6 +1,6 @@ 4.0.0 - org.baeldung + com.baeldung gson 0.1-SNAPSHOT @@ -53,7 +53,7 @@ junit - junit-dep + junit ${junit.version} test @@ -112,44 +112,40 @@ - - 4.1.5.RELEASE - 3.2.5.RELEASE - - 4.3.10.Final - 5.1.35 + 4.3.11.Final + 5.1.38 2.3 - 1.7.12 + 1.7.13 1.1.3 5.1.3.Final - 18.0 + 19.0 3.4 1.3 - 4.11 + 4.12 1.10.19 4.4.1 4.5 - 2.4.1 + 2.9.0 - 3.3 + 3.5.1 2.6 - 2.18.1 + 2.19.1 2.7 - 1.4.14 + 1.4.18 diff --git a/gson/src/main/webapp/WEB-INF/api-servlet.xml b/gson/src/main/webapp/WEB-INF/api-servlet.xml deleted file mode 100644 index 4c74be8912..0000000000 --- a/gson/src/main/webapp/WEB-INF/api-servlet.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/gson/src/main/webapp/WEB-INF/web.xml b/gson/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index 935beae648..0000000000 --- a/gson/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - - Spring MVC Application - - - - contextClass - - org.springframework.web.context.support.AnnotationConfigWebApplicationContext - - - - contextConfigLocation - org.baeldung.config - - - - org.springframework.web.context.ContextLoaderListener - - - - - api - org.springframework.web.servlet.DispatcherServlet - 1 - - - api - / - - - - - - - \ No newline at end of file diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/Foo.java b/gson/src/test/java/org/baeldung/gson/deserialization/Foo.java index 64720f63f9..84f8aaef13 100644 --- a/gson/src/test/java/org/baeldung/gson/deserialization/Foo.java +++ b/gson/src/test/java/org/baeldung/gson/deserialization/Foo.java @@ -9,6 +9,10 @@ public class Foo { this.stringValue = stringValue; } + public Foo(final String stringValue) { + this.stringValue = stringValue; + } + public Foo() { super(); } @@ -19,27 +23,33 @@ public class Foo { public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + intValue; - result = prime * result + ((stringValue == null) ? 0 : stringValue.hashCode()); + result = (prime * result) + intValue; + result = (prime * result) + ((stringValue == null) ? 0 : stringValue.hashCode()); return result; } @Override public boolean equals(final Object obj) { - if (this == obj) + if (this == obj) { return true; - if (obj == null) + } + if (obj == null) { return false; - if (getClass() != obj.getClass()) + } + if (getClass() != obj.getClass()) { return false; + } final Foo other = (Foo) obj; - if (intValue != other.intValue) + if (intValue != other.intValue) { return false; + } if (stringValue == null) { - if (other.stringValue != null) + if (other.stringValue != null) { return false; - } else if (!stringValue.equals(other.stringValue)) + } + } else if (!stringValue.equals(other.stringValue)) { return false; + } return true; } diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/FooInstanceCreator.java b/gson/src/test/java/org/baeldung/gson/deserialization/FooInstanceCreator.java new file mode 100644 index 0000000000..4df3986ec3 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/deserialization/FooInstanceCreator.java @@ -0,0 +1,14 @@ +package org.baeldung.gson.deserialization; + +import java.lang.reflect.Type; + +import com.google.gson.InstanceCreator; + +public class FooInstanceCreator implements InstanceCreator { + + @Override + public Foo createInstance(Type type) { + return new Foo("sample"); + } + +} diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/FooWithInner.java b/gson/src/test/java/org/baeldung/gson/deserialization/FooWithInner.java new file mode 100644 index 0000000000..705e534e77 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/deserialization/FooWithInner.java @@ -0,0 +1,24 @@ +package org.baeldung.gson.deserialization; + +public class FooWithInner { + public int intValue; + public String stringValue; + public InnerFoo innerFoo; + + public FooWithInner(int intValue, String stringValue, String name) { + super(); + this.intValue = intValue; + this.stringValue = stringValue; + this.innerFoo = new InnerFoo(name); + } + + public class InnerFoo { + public String name; + + public InnerFoo(String name) { + super(); + this.name = name; + } + + } +} diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationTest.java b/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationTest.java index 342b3096d7..072a25e749 100644 --- a/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationTest.java +++ b/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationTest.java @@ -12,6 +12,8 @@ import java.util.Collection; import org.baeldung.gson.deserialization.Foo; import org.baeldung.gson.deserialization.FooDeserializerFromJsonWithDifferentFields; +import org.baeldung.gson.deserialization.FooInstanceCreator; +import org.baeldung.gson.deserialization.FooWithInner; import org.baeldung.gson.deserialization.GenericFoo; import org.junit.Test; @@ -108,4 +110,29 @@ public class GsonDeserializationTest { assertEquals(targetObject.stringValue, "seven"); } + // new examples + + @Test + public void whenDeserializingToNestedObjects_thenCorrect() { + final String json = "{\"intValue\":1,\"stringValue\":\"one\",\"innerFoo\":{\"name\":\"inner\"}}"; + + final FooWithInner targetObject = new Gson().fromJson(json, FooWithInner.class); + + assertEquals(targetObject.intValue, 1); + assertEquals(targetObject.stringValue, "one"); + assertEquals(targetObject.innerFoo.name, "inner"); + } + + @Test + public void whenDeserializingUsingInstanceCreator_thenCorrect() { + final String json = "{\"intValue\":1}"; + + final GsonBuilder gsonBldr = new GsonBuilder(); + gsonBldr.registerTypeAdapter(Foo.class, new FooInstanceCreator()); + final Foo targetObject = gsonBldr.create().fromJson(json, Foo.class); + + assertEquals(targetObject.intValue, 1); + assertEquals(targetObject.stringValue, "sample"); + } + } diff --git a/guava/README.md b/guava/README.md index 8960b4676e..28bcfeb912 100644 --- a/guava/README.md +++ b/guava/README.md @@ -7,7 +7,11 @@ - [Guava Collections Cookbook](http://www.baeldung.com/guava-collections) - [Guava Ordering Cookbook](http://www.baeldung.com/guava-order) - [Guava Functional Cookbook](http://www.baeldung.com/guava-functions-predicates) - - [Hamcrest Collections Cookbook](http://www.baeldung.com/hamcrest-collections-arrays) - - [Partition a List in Java](http://www.baeldung.com/java-list-split) +- [Filtering and Transforming Collections in Guava](http://www.baeldung.com/guava-filter-and-transform-a-collection) +- [Guava – Join and Split Collections](http://www.baeldung.com/guava-joiner-and-splitter-tutorial) +- [Guava – Write to File, Read from File](http://www.baeldung.com/guava-write-to-file-read-from-file) +- [Guava – Lists](http://www.baeldung.com/guava-lists) +- [Guava – Sets](http://www.baeldung.com/guava-sets) +- [Guava – Maps](http://www.baeldung.com/guava-maps) diff --git a/guava/pom.xml b/guava/pom.xml index 78c5a14e41..59ca3aa1bb 100644 --- a/guava/pom.xml +++ b/guava/pom.xml @@ -1,8 +1,8 @@ 4.0.0 - org.baeldung + com.baeldung guava - 0.1-SNAPSHOT + 0.1.0-SNAPSHOT guava @@ -34,7 +34,7 @@ junit - junit-dep + junit ${junit.version} test @@ -93,41 +93,37 @@ - - 4.1.5.RELEASE - 3.2.5.RELEASE - - 4.3.10.Final - 5.1.35 + 4.3.11.Final + 5.1.38 - 1.7.12 + 1.7.13 1.1.3 5.1.3.Final - 18.0 + 19.0 3.4 1.3 - 4.11 + 4.12 1.10.19 4.4.1 4.5 - 2.4.1 + 2.9.0 - 3.3 + 3.5.1 2.6 - 2.18.1 + 2.19.1 2.7 - 1.4.14 + 1.4.18 diff --git a/guava/src/main/webapp/WEB-INF/api-servlet.xml b/guava/src/main/webapp/WEB-INF/api-servlet.xml deleted file mode 100644 index 4c74be8912..0000000000 --- a/guava/src/main/webapp/WEB-INF/api-servlet.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/guava/src/main/webapp/WEB-INF/web.xml b/guava/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index 935beae648..0000000000 --- a/guava/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - - Spring MVC Application - - - - contextClass - - org.springframework.web.context.support.AnnotationConfigWebApplicationContext - - - - contextConfigLocation - org.baeldung.config - - - - org.springframework.web.context.ContextLoaderListener - - - - - api - org.springframework.web.servlet.DispatcherServlet - 1 - - - api - / - - - - - - - \ No newline at end of file diff --git a/guava/src/test/java/org/baeldung/guava/GuavaCollectionTypesTest.java b/guava/src/test/java/org/baeldung/guava/GuavaCollectionTypesTest.java index 26d3fa90ca..9c38afbcc2 100644 --- a/guava/src/test/java/org/baeldung/guava/GuavaCollectionTypesTest.java +++ b/guava/src/test/java/org/baeldung/guava/GuavaCollectionTypesTest.java @@ -148,6 +148,15 @@ public class GuavaCollectionTypesTest { assertThat(intersection, containsInAnyOrder('b', 'c')); } + @Test + public void whenCalculatingSetSymmetricDifference_thenCorrect() { + final Set first = ImmutableSet.of('a', 'b', 'c'); + final Set second = ImmutableSet.of('b', 'c', 'd'); + + final Set intersection = Sets.symmetricDifference(first, second); + assertThat(intersection, containsInAnyOrder('a', 'd')); + } + @Test public void whenCalculatingPowerSet_thenCorrect() { final Set chars = ImmutableSet.of('a', 'b'); diff --git a/guava/src/test/java/org/baeldung/guava/GuavaMapFromSet.java b/guava/src/test/java/org/baeldung/guava/GuavaMapFromSet.java new file mode 100644 index 0000000000..602205ff9f --- /dev/null +++ b/guava/src/test/java/org/baeldung/guava/GuavaMapFromSet.java @@ -0,0 +1,101 @@ +package org.baeldung.guava; + +import java.util.AbstractMap; +import java.util.AbstractSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.util.WeakHashMap; + +import com.google.common.base.Function; + +public class GuavaMapFromSet extends AbstractMap { + + private class SingleEntry implements Entry { + private K key; + + public SingleEntry( K key) { + this.key = key; + } + + @Override + public K getKey() { + return this.key; + } + + @Override + public V getValue() { + V value = GuavaMapFromSet.this.cache.get(this.key); + if (value == null) { + value = GuavaMapFromSet.this.function.apply(this.key); + GuavaMapFromSet.this.cache.put(this.key, value); + } + return value; + } + + @Override + public V setValue( V value) { + throw new UnsupportedOperationException(); + } + } + + private class MyEntrySet extends AbstractSet> { + + public class EntryIterator implements Iterator> { + private Iterator inner; + + public EntryIterator() { + this.inner = MyEntrySet.this.keys.iterator(); + } + + @Override + public boolean hasNext() { + return this.inner.hasNext(); + } + + @Override + public Map.Entry next() { + K key = this.inner.next(); + return new SingleEntry(key); + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + } + + private Set keys; + + public MyEntrySet( Set keys) { + this.keys = keys; + } + + @Override + public Iterator> iterator() { + return new EntryIterator(); + } + + @Override + public int size() { + return this.keys.size(); + } + + } + + private WeakHashMap cache; + private Set> entries; + private Function function; + + public GuavaMapFromSet( Set keys, Function function) { + this.function = function; + this.cache = new WeakHashMap(); + this.entries = new MyEntrySet(keys); + } + + @Override + public Set> entrySet() { + return this.entries; + } + +} diff --git a/guava/src/test/java/org/baeldung/guava/GuavaMapFromSetTests.java b/guava/src/test/java/org/baeldung/guava/GuavaMapFromSetTests.java new file mode 100644 index 0000000000..9abb5d14a9 --- /dev/null +++ b/guava/src/test/java/org/baeldung/guava/GuavaMapFromSetTests.java @@ -0,0 +1,66 @@ +package org.baeldung.guava; + +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + +import org.junit.Test; + +import com.google.common.base.Function; + +public class GuavaMapFromSetTests { + + @Test + public void givenStringSet_whenMapsToElementLength_thenCorrect() { + Function function = new Function() { + + @Override + public String apply(Integer from) { + return Integer.toBinaryString(from.intValue()); + } + }; + Set set = (Set) new TreeSet(Arrays.asList( + 32, 64, 128)); + Map map = new GuavaMapFromSet(set, + function); + assertTrue(map.get(32).equals("100000") + && map.get(64).equals("1000000") + && map.get(128).equals("10000000")); + } + + @Test + public void givenIntSet_whenMapsToElementBinaryValue_thenCorrect() { + Function function = new Function() { + + @Override + public Integer apply(String from) { + return from.length(); + } + }; + Set set = (Set) new TreeSet(Arrays.asList( + "four", "three", "twelve")); + Map map = new GuavaMapFromSet(set, + function); + assertTrue(map.get("four") == 4 && map.get("three") == 5 + && map.get("twelve") == 6); + } + @Test + public void givenSet_whenNewSetElementAddedAndMappedLive_thenCorrect() { + Function function = new Function() { + + @Override + public Integer apply(String from) { + return from.length(); + } + }; + Set set = (Set) new TreeSet(Arrays.asList( + "four", "three", "twelve")); + Map map = new GuavaMapFromSet(set, + function); + set.add("one"); + assertTrue(map.get("one") == 3 && map.size()==4); + } +} diff --git a/guava/src/test/java/org/baeldung/java/CollectionJavaPartitionUnitTest.java b/guava/src/test/java/org/baeldung/java/CollectionJavaPartitionUnitTest.java new file mode 100644 index 0000000000..63583987ea --- /dev/null +++ b/guava/src/test/java/org/baeldung/java/CollectionJavaPartitionUnitTest.java @@ -0,0 +1,70 @@ +package org.baeldung.java; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +import org.junit.Test; + +import com.google.common.collect.Lists; + +public class CollectionJavaPartitionUnitTest { + + // java8 groupBy + @Test + public final void givenList_whenParitioningIntoNSublistsUsingGroupingBy_thenCorrect() { + final List intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8); + + final Map> groups = intList.stream().collect(Collectors.groupingBy(s -> (s - 1) / 3)); + final List> subSets = new ArrayList>(groups.values()); + + // When + final List lastPartition = subSets.get(2); + final List expectedLastPartition = Lists. newArrayList(7, 8); + assertThat(subSets.size(), equalTo(3)); + assertThat(lastPartition, equalTo(expectedLastPartition)); + + // intList.add(9); + // System.out.println(groups.values()); + } + + // java8 partitionBy + @Test + public final void givenList_whenParitioningIntoSublistsUsingPartitionBy_thenCorrect() { + final List intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8); + + final Map> groups = intList.stream().collect(Collectors.partitioningBy(s -> s > 6)); + final List> subSets = new ArrayList>(groups.values()); + + // When + final List lastPartition = subSets.get(1); + final List expectedLastPartition = Lists. newArrayList(7, 8); + assertThat(subSets.size(), equalTo(2)); + assertThat(lastPartition, equalTo(expectedLastPartition)); + + // intList.add(9); + // System.out.println(groups.values()); + } + + // java8 split by separator + @Test + public final void givenList_whenSplittingBySeparator_thenCorrect() { + final List intList = Lists.newArrayList(1, 2, 3, 0, 4, 5, 6, 0, 7, 8); + + final int[] indexes = Stream.of(IntStream.of(-1), IntStream.range(0, intList.size()).filter(i -> intList.get(i) == 0), IntStream.of(intList.size())).flatMapToInt(s -> s).toArray(); + final List> subSets = IntStream.range(0, indexes.length - 1).mapToObj(i -> intList.subList(indexes[i] + 1, indexes[i + 1])).collect(Collectors.toList()); + + // When + final List lastPartition = subSets.get(2); + final List expectedLastPartition = Lists. newArrayList(7, 8); + assertThat(subSets.size(), equalTo(3)); + assertThat(lastPartition, equalTo(expectedLastPartition)); + } + +} diff --git a/spring-security-oauth/.project b/guava18/.project similarity index 90% rename from spring-security-oauth/.project rename to guava18/.project index fe6e295165..11dff52392 100644 --- a/spring-security-oauth/.project +++ b/guava18/.project @@ -1,6 +1,6 @@ - spring-security-oauth + guava18 diff --git a/guava18/README.md b/guava18/README.md index 8960b4676e..9924d7c16f 100644 --- a/guava18/README.md +++ b/guava18/README.md @@ -7,7 +7,6 @@ - [Guava Collections Cookbook](http://www.baeldung.com/guava-collections) - [Guava Ordering Cookbook](http://www.baeldung.com/guava-order) - [Guava Functional Cookbook](http://www.baeldung.com/guava-functions-predicates) - - [Hamcrest Collections Cookbook](http://www.baeldung.com/hamcrest-collections-arrays) - - [Partition a List in Java](http://www.baeldung.com/java-list-split) +- [Guava 18: What’s New?](http://www.baeldung.com/whats-new-in-guava-18) diff --git a/guava18/pom.xml b/guava18/pom.xml index 21f4697a73..6002b37d74 100644 --- a/guava18/pom.xml +++ b/guava18/pom.xml @@ -5,8 +5,8 @@ 4.0.0 com.baeldung - guava - 1.0-SNAPSHOT + guava18 + 0.1.0-SNAPSHOT diff --git a/guava19/.project b/guava19/.project new file mode 100644 index 0000000000..b4c292ad2c --- /dev/null +++ b/guava19/.project @@ -0,0 +1,17 @@ + + + guava19 + + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + + diff --git a/guava19/README.md b/guava19/README.md new file mode 100644 index 0000000000..be9f2d72a4 --- /dev/null +++ b/guava19/README.md @@ -0,0 +1,7 @@ +========= + +## Guava 19 + + +### Relevant Articles: +- [Guava 19: What’s New?](http://www.baeldung.com/whats-new-in-guava-19) diff --git a/guava19/pom.xml b/guava19/pom.xml index 97c1b0ea4e..13f3b471f1 100644 --- a/guava19/pom.xml +++ b/guava19/pom.xml @@ -1,12 +1,10 @@ - + 4.0.0 com.baeldung - guava - 1.0-SNAPSHOT + guava19 + 0.1.0-SNAPSHOT @@ -25,6 +23,7 @@ 1.3 + diff --git a/guava19/src/test/java/com/baeldung/guava/GuavaMiscUtilsTest.java b/guava19/src/test/java/com/baeldung/guava/GuavaMiscUtilsTest.java index 903381bd29..c7b8441b78 100644 --- a/guava19/src/test/java/com/baeldung/guava/GuavaMiscUtilsTest.java +++ b/guava19/src/test/java/com/baeldung/guava/GuavaMiscUtilsTest.java @@ -1,16 +1,9 @@ package com.baeldung.guava; -import com.baeldung.guava.entity.User; -import com.google.common.base.CharMatcher; import com.google.common.base.Throwables; import com.google.common.collect.*; -import com.google.common.hash.HashCode; -import com.google.common.hash.HashFunction; -import com.google.common.hash.Hashing; -import com.google.common.reflect.TypeToken; import org.junit.Test; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; diff --git a/handling-spring-static-resources/.classpath b/handling-spring-static-resources/.classpath index 88536687a5..613195248b 100644 --- a/handling-spring-static-resources/.classpath +++ b/handling-spring-static-resources/.classpath @@ -6,7 +6,11 @@ - + + + + + @@ -19,9 +23,9 @@ - + - + diff --git a/handling-spring-static-resources/.project b/handling-spring-static-resources/.project index a16def8042..ae47dfac0e 100644 --- a/handling-spring-static-resources/.project +++ b/handling-spring-static-resources/.project @@ -1,6 +1,6 @@ - handling-spring-static-resources + spring-static-resources diff --git a/handling-spring-static-resources/pom.xml b/handling-spring-static-resources/pom.xml index 67591e1844..f47d8a5592 100644 --- a/handling-spring-static-resources/pom.xml +++ b/handling-spring-static-resources/pom.xml @@ -1,12 +1,14 @@ 4.0.0 - org.baeldung - handling-spring-static-resources - handling-spring-static-resources - 0.1-SNAPSHOT + + com.baeldung + spring-static-resources + 0.1.0-SNAPSHOT war + spring-static-resources + @@ -161,7 +163,7 @@ - handling-spring-static-resources + spring-static-resources src/main/resources @@ -171,53 +173,50 @@ - 1.7 + 1.8 + - 4.1.5.RELEASE - 3.2.5.RELEASE + 4.2.5.RELEASE + 4.0.4.RELEASE 1.8.1 2.3.2-b01 - - 4.1.5.RELEASE - 3.2.5.RELEASE - - 4.3.10.Final - 5.1.35 - 1.7.2.RELEASE + 4.3.11.Final + 5.1.38 + 1.9.2.RELEASE - 2.4.4 + 2.7.2 - 1.7.12 + 1.7.13 1.1.3 - 5.1.3.Final + 5.2.2.Final - 18.0 + 19.0 3.4 1.3 - 4.11 + 4.12 1.10.19 4.4.1 4.5 - 2.4.1 + 2.9.0 - 3.3 + 3.5.1 2.6 - 2.18.1 - 1.4.14 + 2.19.1 + 1.4.18 \ No newline at end of file diff --git a/handling-spring-static-resources/src/main/resources/webSecurityConfig.xml b/handling-spring-static-resources/src/main/resources/webSecurityConfig.xml index 56d7f9c113..76b5015e7b 100644 --- a/handling-spring-static-resources/src/main/resources/webSecurityConfig.xml +++ b/handling-spring-static-resources/src/main/resources/webSecurityConfig.xml @@ -2,10 +2,10 @@ @@ -21,7 +21,7 @@ - + diff --git a/handling-spring-static-resources/src/main/webapp/WEB-INF/view/login.jsp b/handling-spring-static-resources/src/main/webapp/WEB-INF/view/login.jsp index 1b09f7c634..8c9f46c027 100644 --- a/handling-spring-static-resources/src/main/webapp/WEB-INF/view/login.jsp +++ b/handling-spring-static-resources/src/main/webapp/WEB-INF/view/login.jsp @@ -41,15 +41,15 @@

-
+ - + - + diff --git a/httpclient/.settings/org.eclipse.jdt.ui.prefs b/httpclient/.settings/org.eclipse.jdt.ui.prefs deleted file mode 100644 index 471e9b0d81..0000000000 --- a/httpclient/.settings/org.eclipse.jdt.ui.prefs +++ /dev/null @@ -1,55 +0,0 @@ -#Sat Jan 21 23:04:06 EET 2012 -eclipse.preferences.version=1 -editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true -sp_cleanup.add_default_serial_version_id=true -sp_cleanup.add_generated_serial_version_id=false -sp_cleanup.add_missing_annotations=true -sp_cleanup.add_missing_deprecated_annotations=true -sp_cleanup.add_missing_methods=false -sp_cleanup.add_missing_nls_tags=false -sp_cleanup.add_missing_override_annotations=true -sp_cleanup.add_missing_override_annotations_interface_methods=true -sp_cleanup.add_serial_version_id=false -sp_cleanup.always_use_blocks=true -sp_cleanup.always_use_parentheses_in_expressions=true -sp_cleanup.always_use_this_for_non_static_field_access=false -sp_cleanup.always_use_this_for_non_static_method_access=false -sp_cleanup.convert_to_enhanced_for_loop=true -sp_cleanup.correct_indentation=true -sp_cleanup.format_source_code=true -sp_cleanup.format_source_code_changes_only=true -sp_cleanup.make_local_variable_final=true -sp_cleanup.make_parameters_final=true -sp_cleanup.make_private_fields_final=false -sp_cleanup.make_type_abstract_if_missing_method=false -sp_cleanup.make_variable_declarations_final=true -sp_cleanup.never_use_blocks=false -sp_cleanup.never_use_parentheses_in_expressions=false -sp_cleanup.on_save_use_additional_actions=true -sp_cleanup.organize_imports=true -sp_cleanup.qualify_static_field_accesses_with_declaring_class=false -sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true -sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true -sp_cleanup.qualify_static_member_accesses_with_declaring_class=true -sp_cleanup.qualify_static_method_accesses_with_declaring_class=false -sp_cleanup.remove_private_constructors=true -sp_cleanup.remove_trailing_whitespaces=true -sp_cleanup.remove_trailing_whitespaces_all=true -sp_cleanup.remove_trailing_whitespaces_ignore_empty=false -sp_cleanup.remove_unnecessary_casts=true -sp_cleanup.remove_unnecessary_nls_tags=false -sp_cleanup.remove_unused_imports=true -sp_cleanup.remove_unused_local_variables=false -sp_cleanup.remove_unused_private_fields=true -sp_cleanup.remove_unused_private_members=false -sp_cleanup.remove_unused_private_methods=true -sp_cleanup.remove_unused_private_types=true -sp_cleanup.sort_members=false -sp_cleanup.sort_members_all=false -sp_cleanup.use_blocks=false -sp_cleanup.use_blocks_only_for_return_and_throw=false -sp_cleanup.use_parentheses_in_expressions=false -sp_cleanup.use_this_for_non_static_field_access=true -sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true -sp_cleanup.use_this_for_non_static_method_access=true -sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true diff --git a/httpclient/README.md b/httpclient/README.md index 529ac754f8..a848edfea6 100644 --- a/httpclient/README.md +++ b/httpclient/README.md @@ -1,16 +1,21 @@ ========= - ## HttpClient 4.x Cookbooks and Examples +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring + ### Relevant Articles: -- [HttpClient 4 – Send Custom Cookie](http://www.baeldung.com/httpclient-4-cookies) -- [HttpClient 4 – Get the Status Code](http://www.baeldung.com/httpclient-status-code) -- [HttpClient 4 – Cancel / Abort Request](http://www.baeldung.com/httpclient-cancel-request) +- [HttpClient 4 – Send Custom Cookie](http://www.baeldung.com/httpclient-4-cookies) +- [HttpClient 4 – Get the Status Code](http://www.baeldung.com/httpclient-status-code) +- [HttpClient 4 – Cancel / Abort Request](http://www.baeldung.com/httpclient-cancel-request) - [HttpClient 4 Cookbook](http://www.baeldung.com/httpclient4) - [Unshorten URLs with HttpClient](http://www.baeldung.com/unshorten-url-httpclient) - [HttpClient with SSL](http://www.baeldung.com/httpclient-ssl) -- [HttpClient 4 – Follow Redirects for POST](http://www.baeldung.com/httpclient-redirect-on-http-post) -- [HttpClient – Set Custom Header](http://www.baeldung.com/httpclient-custom-http-header) +- [HttpClient 4 – Follow Redirects for POST](http://www.baeldung.com/httpclient-redirect-on-http-post) +- [HttpClient – Set Custom Header](http://www.baeldung.com/httpclient-custom-http-header) - [HttpClient Basic Authentication](http://www.baeldung.com/httpclient-4-basic-authentication) +- [Multipart Upload with HttpClient 4](http://www.baeldung.com/httpclient-multipart-upload) +- [HttpAsyncClient Tutorial](http://www.baeldung.com/httpasyncclient-tutorial) +- [HttpClient 4 Tutorial](http://www.baeldung.com/httpclient-guide) diff --git a/httpclient/pom.xml b/httpclient/pom.xml index a6df3842fd..66b2076852 100644 --- a/httpclient/pom.xml +++ b/httpclient/pom.xml @@ -1,6 +1,6 @@ 4.0.0 - org.baeldung + com.baeldung httpclient 0.1-SNAPSHOT @@ -89,7 +89,7 @@ junit - junit-dep + junit ${junit.version} test @@ -148,41 +148,37 @@ - - 4.1.5.RELEASE - 3.2.5.RELEASE - - 4.3.10.Final - 5.1.35 + 4.3.11.Final + 5.1.38 - 1.7.12 + 1.7.13 1.1.3 5.1.3.Final - 18.0 + 19.0 3.4 1.3 - 4.11 + 4.12 1.10.19 4.4.1 4.5 - 2.4.1 + 2.9.0 - 3.3 + 3.5.1 2.6 - 2.18.1 + 2.19.1 2.7 - 1.4.14 + 1.4.18 diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientMultipartTest.java b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientMultipartTest.java index 687a248490..371657af62 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientMultipartTest.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientMultipartTest.java @@ -79,18 +79,22 @@ public class HttpClientMultipartTest { @Test public final void givenFileandMultipleTextParts_whenUploadwithAddPart_thenNoExceptions() throws IOException { final URL url = Thread.currentThread().getContextClassLoader().getResource("uploads/" + TEXTFILENAME); + final File file = new File(url.getPath()); final FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY); final StringBody stringBody1 = new StringBody("This is message 1", ContentType.MULTIPART_FORM_DATA); final StringBody stringBody2 = new StringBody("This is message 2", ContentType.MULTIPART_FORM_DATA); + // final MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); builder.addPart("upfile", fileBody); builder.addPart("text1", stringBody1); builder.addPart("text2", stringBody2); final HttpEntity entity = builder.build(); + // post.setEntity(entity); response = client.execute(post); + final int statusCode = response.getStatusLine().getStatusCode(); final String responseString = getContent(); final String contentTypeInHeader = getContentTypeHeader(); @@ -124,7 +128,7 @@ public class HttpClientMultipartTest { } @Test - public final void givenFileandInputStreamandText_whenUploadwithAddBinaryBodyandAddTextBody_ThenNoException() throws ClientProtocolException, IOException { + public final void givenFileAndInputStreamandText_whenUploadwithAddBinaryBodyandAddTextBody_ThenNoException() throws ClientProtocolException, IOException { final URL url = Thread.currentThread().getContextClassLoader().getResource("uploads/" + ZIPFILENAME); final URL url2 = Thread.currentThread().getContextClassLoader().getResource("uploads/" + IMAGEFILENAME); final InputStream inputStream = new FileInputStream(url.getPath()); diff --git a/httpclient/src/test/java/org/baeldung/httpclient/SandboxTest.java b/httpclient/src/test/java/org/baeldung/httpclient/SandboxTest.java new file mode 100644 index 0000000000..dc1a206f0d --- /dev/null +++ b/httpclient/src/test/java/org/baeldung/httpclient/SandboxTest.java @@ -0,0 +1,188 @@ +package org.baeldung.httpclient; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + +import org.apache.http.Header; +import org.apache.http.HttpHost; +import org.apache.http.HttpResponse; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.AuthenticationException; +import org.apache.http.auth.MalformedChallengeException; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.AuthCache; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.CookieStore; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.impl.auth.DigestScheme; +import org.apache.http.impl.client.BasicAuthCache; +import org.apache.http.impl.client.BasicCookieStore; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.cookie.BasicClientCookie; +import org.apache.http.util.EntityUtils; +import org.junit.Ignore; +import org.junit.Test; + +public class SandboxTest { + + // original example + @Ignore + @Test + public final void whenInterestingDigestAuthScenario_then401UnAuthorized() throws AuthenticationException, ClientProtocolException, IOException, MalformedChallengeException { + final HttpHost targetHost = new HttpHost("httpbin.org", 80, "http"); + + // set up the credentials to run agains the server + final CredentialsProvider credsProvider = new BasicCredentialsProvider(); + credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("user", "passwd")); + + // We need a first run to get a 401 to seed the digest auth + + // Make a client using those creds + final CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); + + // And make a call to the URL we are after + final HttpGet httpget = new HttpGet("http://httpbin.org/digest-auth/auth/user/passwd"); + + // Create a context to use + final HttpClientContext context = HttpClientContext.create(); + + // Get a response from the sever (expect a 401!) + final HttpResponse authResponse = client.execute(targetHost, httpget, context); + + // Pull out the auth header that came back from the server + final Header challenge = authResponse.getHeaders("WWW-Authenticate")[0]; + + // Lets use a digest scheme to solve it + final DigestScheme digest = new DigestScheme(); + digest.processChallenge(challenge); + + // Make a header with the solution based upon user/password and what the digest got out of the initial 401 reponse + final Header solution = digest.authenticate(new UsernamePasswordCredentials("user", "passwd"), httpget, context); + + // Need an auth cache to use the new digest we made + final AuthCache authCache = new BasicAuthCache(); + authCache.put(targetHost, digest); + + // Add the authCache and thus solved digest to the context + context.setAuthCache(authCache); + + // Pimp up our http get with the solved header made by the digest + httpget.addHeader(solution); + + // use it! + System.out.println("Executing request " + httpget.getRequestLine() + " to target " + targetHost); + + for (int i = 0; i < 3; i++) { + final CloseableHttpResponse responseGood = client.execute(targetHost, httpget, context); + + try { + System.out.println("----------------------------------------"); + System.out.println(responseGood.getStatusLine()); + System.out.println(EntityUtils.toString(responseGood.getEntity())); + } finally { + responseGood.close(); + } + } + } + + @Test + public final void whenInterestingDigestAuthScenario_then200OK() throws AuthenticationException, ClientProtocolException, IOException, MalformedChallengeException { + final HttpHost targetHost = new HttpHost("httpbin.org", 80, "http"); + + // set up the credentials to run agains the server + final CredentialsProvider credsProvider = new BasicCredentialsProvider(); + credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("user", "passwd")); + + // This endpoint need fake cookie to work properly + final CookieStore cookieStore = new BasicCookieStore(); + final BasicClientCookie cookie = new BasicClientCookie("fake", "fake_value"); + cookie.setDomain("httpbin.org"); + cookie.setPath("/"); + cookieStore.addCookie(cookie); + + // Make a client using those creds + final CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(cookieStore).setDefaultCredentialsProvider(credsProvider).build(); + + // And make a call to the URL we are after + final HttpGet httpget = new HttpGet("http://httpbin.org/digest-auth/auth/user/passwd"); + + // Create a context to use + final HttpClientContext context = HttpClientContext.create(); + + // Get a response from the sever (401 implicitly) + final HttpResponse authResponse = client.execute(targetHost, httpget, context); + assertEquals(200, authResponse.getStatusLine().getStatusCode()); + + // HttpClient will use cached digest parameters for future requests + System.out.println("Executing request " + httpget.getRequestLine() + " to target " + targetHost); + + for (int i = 0; i < 3; i++) { + final CloseableHttpResponse responseGood = client.execute(targetHost, httpget, context); + + try { + System.out.println("----------------------------------------"); + System.out.println(responseGood.getStatusLine()); + assertEquals(200, responseGood.getStatusLine().getStatusCode()); + } finally { + responseGood.close(); + } + } + client.close(); + } + + // This test needs module spring-security-rest-digest-auth to be running + @Test + public final void whenWeKnowDigestParameters_thenNo401Status() throws AuthenticationException, ClientProtocolException, IOException, MalformedChallengeException { + final HttpHost targetHost = new HttpHost("localhost", 8080, "http"); + + final CredentialsProvider credsProvider = new BasicCredentialsProvider(); + credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user1", "user1Pass")); + + final CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); + + final HttpGet httpget = new HttpGet("http://localhost:8080/spring-security-rest-digest-auth/api/foos/1"); + + final HttpClientContext context = HttpClientContext.create(); + // == make it preemptive + final AuthCache authCache = new BasicAuthCache(); + final DigestScheme digestAuth = new DigestScheme(); + digestAuth.overrideParamter("realm", "Custom Realm Name"); + digestAuth.overrideParamter("nonce", "nonce value goes here"); + authCache.put(targetHost, digestAuth); + context.setAuthCache(authCache); + // == end + System.out.println("Executing The Request knowing the digest parameters ==== "); + final HttpResponse authResponse = client.execute(targetHost, httpget, context); + assertEquals(200, authResponse.getStatusLine().getStatusCode()); + client.close(); + } + + // This test needs module spring-security-rest-digest-auth to be running + @Test + public final void whenDoNotKnowParameters_thenOnlyOne401() throws AuthenticationException, ClientProtocolException, IOException, MalformedChallengeException { + final HttpClientContext context = HttpClientContext.create(); + final HttpHost targetHost = new HttpHost("localhost", 8080, "http"); + final CredentialsProvider credsProvider = new BasicCredentialsProvider(); + credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user1", "user1Pass")); + final CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); + + final HttpGet httpget = new HttpGet("http://localhost:8080/spring-security-rest-digest-auth/api/foos/1"); + System.out.println("Executing The Request NOT knowing the digest parameters ==== "); + final HttpResponse tempResponse = client.execute(targetHost, httpget, context); + assertEquals(200, tempResponse.getStatusLine().getStatusCode()); + + for (int i = 0; i < 3; i++) { + System.out.println("No more Challenges or 401"); + final CloseableHttpResponse authResponse = client.execute(targetHost, httpget, context); + assertEquals(200, authResponse.getStatusLine().getStatusCode()); + authResponse.close(); + } + client.close(); + } +} diff --git a/jackson/README.md b/jackson/README.md index 539cb34761..68765de686 100644 --- a/jackson/README.md +++ b/jackson/README.md @@ -2,11 +2,20 @@ ## 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) +- [Jackson – Unmarshall to Collection/Array](http://www.baeldung.com/jackson-collection-array) - [Jackson Unmarshalling json with Unknown Properties](http://www.baeldung.com/jackson-deserialize-json-unknown-properties) -- [Jackson – Custom Serializer](http://www.baeldung.com/jackson-custom-serialization) -- [Jackson – Custom Deserializer](http://www.baeldung.com/jackson-deserialization) - - +- [Jackson – Custom Serializer](http://www.baeldung.com/jackson-custom-serialization) +- [Jackson – Custom Deserializer](http://www.baeldung.com/jackson-deserialization) +- [Jackson Exceptions – Problems and Solutions](http://www.baeldung.com/jackson-exception) +- [Jackson Date](http://www.baeldung.com/jackson-serialize-dates) +- [Jackson – Bidirectional Relationships](http://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion) +- [Jackson JSON Tutorial](http://www.baeldung.com/jackson) +- [Jackson – Working with Maps and nulls](http://www.baeldung.com/jackson-map-null-values-or-null-key) +- [Jackson – Decide What Fields Get Serialized/Deserializaed](http://www.baeldung.com/jackson-field-serializable-deserializable-or-not) +- [A Guide to Jackson Annotations](http://www.baeldung.com/jackson-annotations) +- [Working with Tree Model Nodes in Jackson](http://www.baeldung.com/jackson-json-node-tree-model) diff --git a/jackson/pom.xml b/jackson/pom.xml index b2c2465436..c7cd172757 100644 --- a/jackson/pom.xml +++ b/jackson/pom.xml @@ -1,6 +1,7 @@ - + 4.0.0 - org.baeldung + com.baeldung jackson 0.1-SNAPSHOT @@ -21,6 +22,12 @@ commons-io 2.4 + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + 2.7.4 + org.apache.commons @@ -47,32 +54,38 @@ com.fasterxml.jackson.datatype jackson-datatype-jsr310 - 2.4.0 + ${jackson.version} com.fasterxml.jackson.datatype jackson-datatype-joda - 2.4.0 + ${jackson.version} + + + + com.fasterxml.jackson.module + jackson-module-jsonSchema + 2.7.2 joda-time joda-time - 2.6 + 2.9.2 com.google.code.gson gson - 2.3.1 + 2.6.2 junit - junit-dep + junit ${junit.version} test @@ -131,45 +144,41 @@ - - 4.1.5.RELEASE - 3.2.5.RELEASE - - 4.3.10.Final - 5.1.35 + 4.3.11.Final + 5.1.38 - 2.4.4 + 2.7.2 - 1.7.12 + 1.7.13 1.1.3 5.1.3.Final - 18.0 + 19.0 3.4 1.3 - 4.11 + 4.12 1.10.19 4.4.1 4.5 - 2.4.1 + 2.9.0 - 3.3 + 3.5.1 2.6 - 2.18.1 + 2.19.1 2.7 - 1.4.14 + 1.4.18 - \ No newline at end of file + diff --git a/jackson/src/main/resources/node_example.json b/jackson/src/main/resources/node_example.json new file mode 100644 index 0000000000..69d37efbf6 --- /dev/null +++ b/jackson/src/main/resources/node_example.json @@ -0,0 +1,10 @@ +{ + "name": + { + "first": "Tatu", + "last": "Saloranta" + }, + + "title": "Jackson founder", + "company": "FasterXML" +} \ No newline at end of file diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithCreator.java b/jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithCreator.java similarity index 89% rename from jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithCreator.java rename to jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithCreator.java index 01507ab4d6..90a45efc96 100644 --- a/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithCreator.java +++ b/jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithCreator.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.annotation; +package com.baeldung.jackson.annotation; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithCustomAnnotation.java b/jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithCustomAnnotation.java similarity index 90% rename from jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithCustomAnnotation.java rename to jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithCustomAnnotation.java index 3b468f474f..65d64059c9 100644 --- a/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithCustomAnnotation.java +++ b/jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithCustomAnnotation.java @@ -1,10 +1,10 @@ -package org.baeldung.jackson.annotation; +package com.baeldung.jackson.annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.Date; -import org.baeldung.jackson.annotation.BeanWithCustomAnnotation.CustomAnnotation; +import com.baeldung.jackson.annotation.BeanWithCustomAnnotation.CustomAnnotation; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import com.fasterxml.jackson.annotation.JsonInclude; diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithFilter.java b/jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithFilter.java similarity index 88% rename from jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithFilter.java rename to jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithFilter.java index a0627757b6..48fecf8c13 100644 --- a/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithFilter.java +++ b/jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithFilter.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.annotation; +package com.baeldung.jackson.annotation; import com.fasterxml.jackson.annotation.JsonFilter; diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithGetter.java b/jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithGetter.java similarity index 93% rename from jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithGetter.java rename to jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithGetter.java index 6585018564..39db9f06b9 100644 --- a/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithGetter.java +++ b/jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithGetter.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.annotation; +package com.baeldung.jackson.annotation; import com.fasterxml.jackson.annotation.JsonGetter; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithIgnore.java b/jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithIgnore.java similarity index 90% rename from jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithIgnore.java rename to jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithIgnore.java index 4f0866bdc0..aa4e6b495d 100644 --- a/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithIgnore.java +++ b/jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithIgnore.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.annotation; +package com.baeldung.jackson.annotation; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithInject.java b/jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithInject.java similarity index 88% rename from jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithInject.java rename to jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithInject.java index aa8da1da4f..473058fd1c 100644 --- a/jackson/src/test/java/org/baeldung/jackson/annotation/BeanWithInject.java +++ b/jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithInject.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.annotation; +package com.baeldung.jackson.annotation; import com.fasterxml.jackson.annotation.JacksonInject; diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/ExtendableBean.java b/jackson/src/test/java/com/baeldung/jackson/annotation/ExtendableBean.java similarity index 94% rename from jackson/src/test/java/org/baeldung/jackson/annotation/ExtendableBean.java rename to jackson/src/test/java/com/baeldung/jackson/annotation/ExtendableBean.java index c8e5f453ec..e03ffed613 100644 --- a/jackson/src/test/java/org/baeldung/jackson/annotation/ExtendableBean.java +++ b/jackson/src/test/java/com/baeldung/jackson/annotation/ExtendableBean.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.annotation; +package com.baeldung.jackson.annotation; import java.util.HashMap; import java.util.Map; diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/MyBean.java b/jackson/src/test/java/com/baeldung/jackson/annotation/MyBean.java similarity index 91% rename from jackson/src/test/java/org/baeldung/jackson/annotation/MyBean.java rename to jackson/src/test/java/com/baeldung/jackson/annotation/MyBean.java index 651d7c2da0..5d4ce4202c 100644 --- a/jackson/src/test/java/org/baeldung/jackson/annotation/MyBean.java +++ b/jackson/src/test/java/com/baeldung/jackson/annotation/MyBean.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.annotation; +package com.baeldung.jackson.annotation; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/PrivateBean.java b/jackson/src/test/java/com/baeldung/jackson/annotation/PrivateBean.java similarity index 90% rename from jackson/src/test/java/org/baeldung/jackson/annotation/PrivateBean.java rename to jackson/src/test/java/com/baeldung/jackson/annotation/PrivateBean.java index 472fa84a26..a49ce2e6d5 100644 --- a/jackson/src/test/java/org/baeldung/jackson/annotation/PrivateBean.java +++ b/jackson/src/test/java/com/baeldung/jackson/annotation/PrivateBean.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.annotation; +package com.baeldung.jackson.annotation; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/RawBean.java b/jackson/src/test/java/com/baeldung/jackson/annotation/RawBean.java similarity index 87% rename from jackson/src/test/java/org/baeldung/jackson/annotation/RawBean.java rename to jackson/src/test/java/com/baeldung/jackson/annotation/RawBean.java index a481fbf02e..a0482ef5c1 100644 --- a/jackson/src/test/java/org/baeldung/jackson/annotation/RawBean.java +++ b/jackson/src/test/java/com/baeldung/jackson/annotation/RawBean.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.annotation; +package com.baeldung.jackson.annotation; import com.fasterxml.jackson.annotation.JsonRawValue; diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/UnwrappedUser.java b/jackson/src/test/java/com/baeldung/jackson/annotation/UnwrappedUser.java similarity index 93% rename from jackson/src/test/java/org/baeldung/jackson/annotation/UnwrappedUser.java rename to jackson/src/test/java/com/baeldung/jackson/annotation/UnwrappedUser.java index 4c761a57e0..d2b7522221 100644 --- a/jackson/src/test/java/org/baeldung/jackson/annotation/UnwrappedUser.java +++ b/jackson/src/test/java/com/baeldung/jackson/annotation/UnwrappedUser.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.annotation; +package com.baeldung.jackson.annotation; import com.fasterxml.jackson.annotation.JsonUnwrapped; diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/UserWithIgnoreType.java b/jackson/src/test/java/com/baeldung/jackson/annotation/UserWithIgnoreType.java similarity index 93% rename from jackson/src/test/java/org/baeldung/jackson/annotation/UserWithIgnoreType.java rename to jackson/src/test/java/com/baeldung/jackson/annotation/UserWithIgnoreType.java index eaf4cb7b74..36f383cf65 100644 --- a/jackson/src/test/java/org/baeldung/jackson/annotation/UserWithIgnoreType.java +++ b/jackson/src/test/java/com/baeldung/jackson/annotation/UserWithIgnoreType.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.annotation; +package com.baeldung.jackson.annotation; import com.fasterxml.jackson.annotation.JsonIgnoreType; diff --git a/jackson/src/test/java/org/baeldung/jackson/annotation/Zoo.java b/jackson/src/test/java/com/baeldung/jackson/annotation/Zoo.java similarity index 96% rename from jackson/src/test/java/org/baeldung/jackson/annotation/Zoo.java rename to jackson/src/test/java/com/baeldung/jackson/annotation/Zoo.java index d59be09f12..9c11ec0e36 100644 --- a/jackson/src/test/java/org/baeldung/jackson/annotation/Zoo.java +++ b/jackson/src/test/java/com/baeldung/jackson/annotation/Zoo.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.annotation; +package com.baeldung.jackson.annotation; import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonTypeInfo; diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/AppendBeans.java b/jackson/src/test/java/com/baeldung/jackson/annotation/extra/AppendBeans.java new file mode 100644 index 0000000000..7b75c205c9 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/annotation/extra/AppendBeans.java @@ -0,0 +1,58 @@ +package com.baeldung.jackson.annotation.extra; + +import com.fasterxml.jackson.databind.annotation.JsonAppend; + +public class AppendBeans { + public static class BeanWithoutAppend { + private int id; + private String name; + + public BeanWithoutAppend(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + @JsonAppend(attrs = { @JsonAppend.Attr(value = "version") }) + public static class BeanWithAppend { + private int id; + private String name; + + public BeanWithAppend(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/ExtraAnnotationTest.java b/jackson/src/test/java/com/baeldung/jackson/annotation/extra/ExtraAnnotationTest.java new file mode 100644 index 0000000000..79aae1dd04 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/annotation/extra/ExtraAnnotationTest.java @@ -0,0 +1,131 @@ +package com.baeldung.jackson.annotation.extra; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.not; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import com.baeldung.jackson.annotation.extra.AppendBeans.BeanWithAppend; +import com.baeldung.jackson.annotation.extra.AppendBeans.BeanWithoutAppend; +import com.baeldung.jackson.annotation.extra.IdentityReferenceBeans.BeanWithIdentityReference; +import com.baeldung.jackson.annotation.extra.IdentityReferenceBeans.BeanWithoutIdentityReference; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping; +import com.fasterxml.jackson.databind.ObjectWriter; +import com.fasterxml.jackson.module.jsonSchema.JsonSchema; +import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper; + +public class ExtraAnnotationTest { + @Test + public void whenNotUsingJsonIdentityReferenceAnnotation_thenCorrect() throws JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(); + BeanWithoutIdentityReference bean = new BeanWithoutIdentityReference(1, "Bean Without Identity Reference Annotation"); + String jsonString = mapper.writeValueAsString(bean); + + assertThat(jsonString, containsString("Bean Without Identity Reference Annotation")); + } + + @Test + public void whenUsingJsonIdentityReferenceAnnotation_thenCorrect() throws JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(); + BeanWithIdentityReference bean = new BeanWithIdentityReference(1, "Bean With Identity Reference Annotation"); + String jsonString = mapper.writeValueAsString(bean); + + assertEquals("1", jsonString); + } + + @Test + public void whenNotUsingJsonAppendAnnotation_thenCorrect() throws JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(); + + BeanWithoutAppend bean = new BeanWithoutAppend(2, "Bean Without Append Annotation"); + ObjectWriter writer = mapper.writerFor(BeanWithoutAppend.class).withAttribute("version", "1.0"); + String jsonString = writer.writeValueAsString(bean); + + assertThat(jsonString, not(containsString("version"))); + assertThat(jsonString, not(containsString("1.0"))); + } + + @Test + public void whenUsingJsonAppendAnnotation_thenCorrect() throws JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(); + + BeanWithAppend bean = new BeanWithAppend(2, "Bean With Append Annotation"); + ObjectWriter writer = mapper.writerFor(BeanWithAppend.class).withAttribute("version", "1.0"); + String jsonString = writer.writeValueAsString(bean); + + assertThat(jsonString, containsString("version")); + assertThat(jsonString, containsString("1.0")); + } + + @Test + public void whenUsingJsonNamingAnnotation_thenCorrect() throws JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(); + NamingBean bean = new NamingBean(3, "Naming Bean"); + String jsonString = mapper.writeValueAsString(bean); + + assertThat(jsonString, containsString("bean_name")); + } + + @Test + public void whenUsingJsonPropertyDescriptionAnnotation_thenCorrect() throws JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(); + SchemaFactoryWrapper wrapper = new SchemaFactoryWrapper(); + mapper.acceptJsonFormatVisitor(PropertyDescriptionBean.class, wrapper); + JsonSchema jsonSchema = wrapper.finalSchema(); + String jsonString = mapper.writeValueAsString(jsonSchema); + System.out.println(jsonString); + assertThat(jsonString, containsString("This is a description of the name property")); + } + + @Test + public void whenUsingJsonPOJOBuilderAnnotation_thenCorrect() throws IOException { + ObjectMapper mapper = new ObjectMapper(); + String jsonString = "{\"id\":5,\"name\":\"POJO Builder Bean\"}"; + POJOBuilderBean bean = mapper.readValue(jsonString, POJOBuilderBean.class); + + assertEquals(5, bean.getIdentity()); + assertEquals("POJO Builder Bean", bean.getBeanName()); + } + + @Test + public void whenUsingJsonTypeIdAnnotation_thenCorrect() throws JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(); + mapper.enableDefaultTyping(DefaultTyping.NON_FINAL); + TypeIdBean bean = new TypeIdBean(6, "Type Id Bean"); + String jsonString = mapper.writeValueAsString(bean); + + assertThat(jsonString, containsString("Type Id Bean")); + } + + @Test + public void whenUsingJsonTypeIdResolverAnnotation_thenCorrect() throws IOException { + TypeIdResolverStructure.FirstBean bean1 = new TypeIdResolverStructure.FirstBean(1, "Bean 1"); + TypeIdResolverStructure.LastBean bean2 = new TypeIdResolverStructure.LastBean(2, "Bean 2"); + + List beans = new ArrayList<>(); + beans.add(bean1); + beans.add(bean2); + + TypeIdResolverStructure.BeanContainer serializedContainer = new TypeIdResolverStructure.BeanContainer(); + serializedContainer.setBeans(beans); + + ObjectMapper mapper = new ObjectMapper(); + String jsonString = mapper.writeValueAsString(serializedContainer); + assertThat(jsonString, containsString("bean1")); + assertThat(jsonString, containsString("bean2")); + + TypeIdResolverStructure.BeanContainer deserializedContainer = mapper.readValue(jsonString, TypeIdResolverStructure.BeanContainer.class); + List beanList = deserializedContainer.getBeans(); + assertThat(beanList.get(0), instanceOf(TypeIdResolverStructure.FirstBean.class)); + assertThat(beanList.get(1), instanceOf(TypeIdResolverStructure.LastBean.class)); + } +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/IdentityReferenceBeans.java b/jackson/src/test/java/com/baeldung/jackson/annotation/extra/IdentityReferenceBeans.java new file mode 100644 index 0000000000..495bb7de43 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/annotation/extra/IdentityReferenceBeans.java @@ -0,0 +1,63 @@ +package com.baeldung.jackson.annotation.extra; + +import com.fasterxml.jackson.annotation.JsonIdentityInfo; +import com.fasterxml.jackson.annotation.JsonIdentityReference; +import com.fasterxml.jackson.annotation.ObjectIdGenerators; + + +public class IdentityReferenceBeans { + @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") + public static class BeanWithoutIdentityReference { + private int id; + private String name; + + public BeanWithoutIdentityReference(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") + @JsonIdentityReference(alwaysAsId = true) + public static class BeanWithIdentityReference { + private int id; + private String name; + + public BeanWithIdentityReference(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/NamingBean.java b/jackson/src/test/java/com/baeldung/jackson/annotation/extra/NamingBean.java new file mode 100644 index 0000000000..efd26ab9ae --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/annotation/extra/NamingBean.java @@ -0,0 +1,31 @@ +package com.baeldung.jackson.annotation.extra; + +import com.fasterxml.jackson.databind.PropertyNamingStrategy; +import com.fasterxml.jackson.databind.annotation.JsonNaming; + +@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) +public class NamingBean { + private int id; + private String beanName; + + public NamingBean(int id, String beanName) { + this.id = id; + this.beanName = beanName; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getBeanName() { + return beanName; + } + + public void setBeanName(String beanName) { + this.beanName = beanName; + } +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/POJOBuilderBean.java b/jackson/src/test/java/com/baeldung/jackson/annotation/extra/POJOBuilderBean.java new file mode 100644 index 0000000000..e0a89c6903 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/annotation/extra/POJOBuilderBean.java @@ -0,0 +1,51 @@ +package com.baeldung.jackson.annotation.extra; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; + +@JsonDeserialize(builder = POJOBuilderBean.BeanBuilder.class) +public class POJOBuilderBean { + private int identity; + private String beanName; + + @JsonPOJOBuilder(buildMethodName = "createBean", withPrefix = "construct") + public static class BeanBuilder { + private int idValue; + private String nameValue; + + public BeanBuilder constructId(int id) { + idValue = id; + return this; + } + + public BeanBuilder constructName(String name) { + nameValue = name; + return this; + } + + public POJOBuilderBean createBean() { + return new POJOBuilderBean(idValue, nameValue); + } + } + + public POJOBuilderBean(int identity, String beanName) { + this.identity = identity; + this.beanName = beanName; + } + + public int getIdentity() { + return identity; + } + + public void setIdentity(int identity) { + this.identity = identity; + } + + public String getBeanName() { + return beanName; + } + + public void setBeanName(String beanName) { + this.beanName = beanName; + } +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/PropertyDescriptionBean.java b/jackson/src/test/java/com/baeldung/jackson/annotation/extra/PropertyDescriptionBean.java new file mode 100644 index 0000000000..1563cddb83 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/annotation/extra/PropertyDescriptionBean.java @@ -0,0 +1,25 @@ +package com.baeldung.jackson.annotation.extra; + +import com.fasterxml.jackson.annotation.JsonPropertyDescription; + +public class PropertyDescriptionBean { + private int id; + @JsonPropertyDescription("This is a description of the name property") + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/TypeIdBean.java b/jackson/src/test/java/com/baeldung/jackson/annotation/extra/TypeIdBean.java new file mode 100644 index 0000000000..32a6d5a1d5 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/annotation/extra/TypeIdBean.java @@ -0,0 +1,30 @@ +package com.baeldung.jackson.annotation.extra; + +import com.fasterxml.jackson.annotation.JsonTypeId; + +public class TypeIdBean { + private int id; + @JsonTypeId + private String name; + + public TypeIdBean(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/TypeIdResolverStructure.java b/jackson/src/test/java/com/baeldung/jackson/annotation/extra/TypeIdResolverStructure.java new file mode 100644 index 0000000000..9056023c69 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/annotation/extra/TypeIdResolverStructure.java @@ -0,0 +1,130 @@ +package com.baeldung.jackson.annotation.extra; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.annotation.JsonTypeInfo.Id; +import com.fasterxml.jackson.databind.DatabindContext; +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver; +import com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase; + +public class TypeIdResolverStructure { + public static class BeanContainer { + private List beans; + + public List getBeans() { + return beans; + } + + public void setBeans(List beans) { + this.beans = beans; + } + } + + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "@type") + @JsonTypeIdResolver(BeanIdResolver.class) + public static class AbstractBean { + private int id; + + protected AbstractBean() { + } + + protected AbstractBean(int id) { + this.id = id; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + } + + public static class FirstBean extends AbstractBean { + String firstName; + + public FirstBean() { + } + + public FirstBean(int id, String name) { + super(id); + setFirstName(name); + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String name) { + firstName = name; + } + } + + public static class LastBean extends AbstractBean { + String lastName; + + public LastBean() { + } + + public LastBean(int id, String name) { + super(id); + setLastName(name); + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String name) { + lastName = name; + } + } + + public static class BeanIdResolver extends TypeIdResolverBase { + private JavaType superType; + + @Override + public void init(JavaType baseType) { + superType = baseType; + } + + @Override + public Id getMechanism() { + return Id.NAME; + } + + @Override + public String idFromValue(Object obj) { + return idFromValueAndType(obj, obj.getClass()); + } + + @Override + public String idFromValueAndType(Object obj, Class subType) { + String typeId = null; + switch (subType.getSimpleName()) { + case "FirstBean": + typeId = "bean1"; + break; + case "LastBean": + typeId = "bean2"; + } + return typeId; + } + + @Override + public JavaType typeFromId(DatabindContext context, String id) { + Class subType = null; + switch (id) { + case "bean1": + subType = FirstBean.class; + break; + case "bean2": + subType = LastBean.class; + } + return context.constructSpecializedType(superType, subType); + } + } +} \ No newline at end of file diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/CustomListDeserializer.java b/jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListDeserializer.java similarity index 93% rename from jackson/src/test/java/org/baeldung/jackson/bidirection/CustomListDeserializer.java rename to jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListDeserializer.java index cc4e1d5589..5f1f1edf2b 100644 --- a/jackson/src/test/java/org/baeldung/jackson/bidirection/CustomListDeserializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListDeserializer.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.bidirection; +package com.baeldung.jackson.bidirection; import java.io.IOException; import java.util.ArrayList; diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/CustomListSerializer.java b/jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListSerializer.java similarity index 94% rename from jackson/src/test/java/org/baeldung/jackson/bidirection/CustomListSerializer.java rename to jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListSerializer.java index 545f7e18c1..1d8ca011ea 100644 --- a/jackson/src/test/java/org/baeldung/jackson/bidirection/CustomListSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListSerializer.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.bidirection; +package com.baeldung.jackson.bidirection; import java.io.IOException; import java.util.ArrayList; diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/Item.java b/jackson/src/test/java/com/baeldung/jackson/bidirection/Item.java similarity index 87% rename from jackson/src/test/java/org/baeldung/jackson/bidirection/Item.java rename to jackson/src/test/java/com/baeldung/jackson/bidirection/Item.java index 223721dc62..55b8632e42 100644 --- a/jackson/src/test/java/org/baeldung/jackson/bidirection/Item.java +++ b/jackson/src/test/java/com/baeldung/jackson/bidirection/Item.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.bidirection; +package com.baeldung.jackson.bidirection; public class Item { public int id; diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithIdentity.java b/jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithIdentity.java similarity index 93% rename from jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithIdentity.java rename to jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithIdentity.java index 2091ec6e40..25de4a8f7a 100644 --- a/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithIdentity.java +++ b/jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithIdentity.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.bidirection; +package com.baeldung.jackson.bidirection; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.ObjectIdGenerators; diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithIgnore.java b/jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithIgnore.java similarity index 89% rename from jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithIgnore.java rename to jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithIgnore.java index 8b6d623f18..910ccec174 100644 --- a/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithIgnore.java +++ b/jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithIgnore.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.bidirection; +package com.baeldung.jackson.bidirection; public class ItemWithIgnore { public int id; diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithRef.java b/jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithRef.java similarity index 91% rename from jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithRef.java rename to jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithRef.java index 295ec9580d..0ca8d721e8 100644 --- a/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithRef.java +++ b/jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithRef.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.bidirection; +package com.baeldung.jackson.bidirection; import com.fasterxml.jackson.annotation.JsonManagedReference; diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithSerializer.java b/jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithSerializer.java similarity index 89% rename from jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithSerializer.java rename to jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithSerializer.java index d9d9612677..a57ca89d2c 100644 --- a/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithSerializer.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.bidirection; +package com.baeldung.jackson.bidirection; public class ItemWithSerializer { public int id; diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithView.java b/jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithView.java similarity index 85% rename from jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithView.java rename to jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithView.java index 65e0d08b4e..ffa19fbad2 100644 --- a/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithView.java +++ b/jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithView.java @@ -1,6 +1,6 @@ -package org.baeldung.jackson.bidirection; +package com.baeldung.jackson.bidirection; -import org.baeldung.jackson.jsonview.Views; +import com.baeldung.jackson.jsonview.Views; import com.fasterxml.jackson.annotation.JsonView; diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/User.java b/jackson/src/test/java/com/baeldung/jackson/bidirection/User.java similarity index 90% rename from jackson/src/test/java/org/baeldung/jackson/bidirection/User.java rename to jackson/src/test/java/com/baeldung/jackson/bidirection/User.java index a92dff4052..71c9ec6a68 100644 --- a/jackson/src/test/java/org/baeldung/jackson/bidirection/User.java +++ b/jackson/src/test/java/com/baeldung/jackson/bidirection/User.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.bidirection; +package com.baeldung.jackson.bidirection; import java.util.ArrayList; import java.util.List; diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithIdentity.java b/jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithIdentity.java similarity index 94% rename from jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithIdentity.java rename to jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithIdentity.java index 52fa22c32d..db83a09389 100644 --- a/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithIdentity.java +++ b/jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithIdentity.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.bidirection; +package com.baeldung.jackson.bidirection; import java.util.ArrayList; import java.util.List; diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithIgnore.java b/jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithIgnore.java similarity index 92% rename from jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithIgnore.java rename to jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithIgnore.java index 7714487f76..857a373cc5 100644 --- a/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithIgnore.java +++ b/jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithIgnore.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.bidirection; +package com.baeldung.jackson.bidirection; import java.util.ArrayList; import java.util.List; diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithRef.java b/jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithRef.java similarity index 92% rename from jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithRef.java rename to jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithRef.java index 406b87f78c..3de03fc651 100644 --- a/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithRef.java +++ b/jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithRef.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.bidirection; +package com.baeldung.jackson.bidirection; import java.util.ArrayList; import java.util.List; diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithSerializer.java b/jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithSerializer.java similarity index 94% rename from jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithSerializer.java rename to jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithSerializer.java index 25c202a9d3..9fda969e41 100644 --- a/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithSerializer.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.bidirection; +package com.baeldung.jackson.bidirection; import java.util.ArrayList; import java.util.List; diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithView.java b/jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithView.java similarity index 87% rename from jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithView.java rename to jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithView.java index e7043292a0..d92d67b3a1 100644 --- a/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithView.java +++ b/jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithView.java @@ -1,9 +1,9 @@ -package org.baeldung.jackson.bidirection; +package com.baeldung.jackson.bidirection; import java.util.ArrayList; import java.util.List; -import org.baeldung.jackson.jsonview.Views; +import com.baeldung.jackson.jsonview.Views; import com.fasterxml.jackson.annotation.JsonView; diff --git a/jackson/src/test/java/org/baeldung/jackson/date/CustomDateDeserializer.java b/jackson/src/test/java/com/baeldung/jackson/date/CustomDateDeserializer.java similarity index 85% rename from jackson/src/test/java/org/baeldung/jackson/date/CustomDateDeserializer.java rename to jackson/src/test/java/com/baeldung/jackson/date/CustomDateDeserializer.java index bbd039f6ae..a63190c8f5 100644 --- a/jackson/src/test/java/org/baeldung/jackson/date/CustomDateDeserializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/date/CustomDateDeserializer.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.date; +package com.baeldung.jackson.date; import java.io.IOException; import java.text.ParseException; @@ -12,7 +12,7 @@ import com.fasterxml.jackson.databind.JsonDeserializer; public class CustomDateDeserializer extends JsonDeserializer { - private static SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss"); + private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss"); @Override public Date deserialize(final JsonParser jsonparser, final DeserializationContext context) throws IOException, JsonProcessingException { diff --git a/jackson/src/test/java/org/baeldung/jackson/date/CustomDateSerializer.java b/jackson/src/test/java/com/baeldung/jackson/date/CustomDateSerializer.java similarity index 82% rename from jackson/src/test/java/org/baeldung/jackson/date/CustomDateSerializer.java rename to jackson/src/test/java/com/baeldung/jackson/date/CustomDateSerializer.java index fc7cbb2db1..8d435b7b69 100644 --- a/jackson/src/test/java/org/baeldung/jackson/date/CustomDateSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/date/CustomDateSerializer.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.date; +package com.baeldung.jackson.date; import java.io.IOException; import java.text.SimpleDateFormat; @@ -11,7 +11,7 @@ import com.fasterxml.jackson.databind.SerializerProvider; public class CustomDateSerializer extends JsonSerializer { - private static SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss"); + private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss"); @Override public void serialize(final Date value, final JsonGenerator gen, final SerializerProvider arg2) throws IOException, JsonProcessingException { diff --git a/jackson/src/test/java/org/baeldung/jackson/date/CustomDateTimeSerializer.java b/jackson/src/test/java/com/baeldung/jackson/date/CustomDateTimeSerializer.java similarity index 95% rename from jackson/src/test/java/org/baeldung/jackson/date/CustomDateTimeSerializer.java rename to jackson/src/test/java/com/baeldung/jackson/date/CustomDateTimeSerializer.java index 9f2a290bc4..88c069419b 100644 --- a/jackson/src/test/java/org/baeldung/jackson/date/CustomDateTimeSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/date/CustomDateTimeSerializer.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.date; +package com.baeldung.jackson.date; import java.io.IOException; diff --git a/jackson/src/test/java/org/baeldung/jackson/date/CustomLocalDateTimeSerializer.java b/jackson/src/test/java/com/baeldung/jackson/date/CustomLocalDateTimeSerializer.java similarity index 95% rename from jackson/src/test/java/org/baeldung/jackson/date/CustomLocalDateTimeSerializer.java rename to jackson/src/test/java/com/baeldung/jackson/date/CustomLocalDateTimeSerializer.java index b1e6a8ba86..3f8f5e098e 100644 --- a/jackson/src/test/java/org/baeldung/jackson/date/CustomLocalDateTimeSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/date/CustomLocalDateTimeSerializer.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.date; +package com.baeldung.jackson.date; import java.io.IOException; import java.time.LocalDateTime; diff --git a/jackson/src/test/java/org/baeldung/jackson/date/Event.java b/jackson/src/test/java/com/baeldung/jackson/date/Event.java similarity index 91% rename from jackson/src/test/java/org/baeldung/jackson/date/Event.java rename to jackson/src/test/java/com/baeldung/jackson/date/Event.java index 783eee6af8..e20882abc1 100644 --- a/jackson/src/test/java/org/baeldung/jackson/date/Event.java +++ b/jackson/src/test/java/com/baeldung/jackson/date/Event.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.date; +package com.baeldung.jackson.date; import java.util.Date; diff --git a/jackson/src/test/java/org/baeldung/jackson/date/EventWithFormat.java b/jackson/src/test/java/com/baeldung/jackson/date/EventWithFormat.java similarity index 94% rename from jackson/src/test/java/org/baeldung/jackson/date/EventWithFormat.java rename to jackson/src/test/java/com/baeldung/jackson/date/EventWithFormat.java index 4b2053eb4b..607e694cef 100644 --- a/jackson/src/test/java/org/baeldung/jackson/date/EventWithFormat.java +++ b/jackson/src/test/java/com/baeldung/jackson/date/EventWithFormat.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.date; +package com.baeldung.jackson.date; import java.util.Date; diff --git a/jackson/src/test/java/org/baeldung/jackson/date/EventWithJodaTime.java b/jackson/src/test/java/com/baeldung/jackson/date/EventWithJodaTime.java similarity index 94% rename from jackson/src/test/java/org/baeldung/jackson/date/EventWithJodaTime.java rename to jackson/src/test/java/com/baeldung/jackson/date/EventWithJodaTime.java index 0c72030650..6abee344ef 100644 --- a/jackson/src/test/java/org/baeldung/jackson/date/EventWithJodaTime.java +++ b/jackson/src/test/java/com/baeldung/jackson/date/EventWithJodaTime.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.date; +package com.baeldung.jackson.date; import org.joda.time.DateTime; diff --git a/jackson/src/test/java/org/baeldung/jackson/date/EventWithLocalDateTime.java b/jackson/src/test/java/com/baeldung/jackson/date/EventWithLocalDateTime.java similarity index 94% rename from jackson/src/test/java/org/baeldung/jackson/date/EventWithLocalDateTime.java rename to jackson/src/test/java/com/baeldung/jackson/date/EventWithLocalDateTime.java index 6c48cf3f0e..16104222d4 100644 --- a/jackson/src/test/java/org/baeldung/jackson/date/EventWithLocalDateTime.java +++ b/jackson/src/test/java/com/baeldung/jackson/date/EventWithLocalDateTime.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.date; +package com.baeldung.jackson.date; import java.time.LocalDateTime; diff --git a/jackson/src/test/java/org/baeldung/jackson/date/EventWithSerializer.java b/jackson/src/test/java/com/baeldung/jackson/date/EventWithSerializer.java similarity index 95% rename from jackson/src/test/java/org/baeldung/jackson/date/EventWithSerializer.java rename to jackson/src/test/java/com/baeldung/jackson/date/EventWithSerializer.java index 9cdd6a0fab..c359b5c846 100644 --- a/jackson/src/test/java/org/baeldung/jackson/date/EventWithSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/date/EventWithSerializer.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.date; +package com.baeldung.jackson.date; import java.util.Date; diff --git a/jackson/src/test/java/org/baeldung/jackson/deserialization/ItemDeserializer.java b/jackson/src/test/java/com/baeldung/jackson/deserialization/ItemDeserializer.java similarity index 88% rename from jackson/src/test/java/org/baeldung/jackson/deserialization/ItemDeserializer.java rename to jackson/src/test/java/com/baeldung/jackson/deserialization/ItemDeserializer.java index baf5b945e7..3be6685103 100644 --- a/jackson/src/test/java/org/baeldung/jackson/deserialization/ItemDeserializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/deserialization/ItemDeserializer.java @@ -1,9 +1,9 @@ -package org.baeldung.jackson.deserialization; +package com.baeldung.jackson.deserialization; import java.io.IOException; -import org.baeldung.jackson.dtos.Item; -import org.baeldung.jackson.dtos.User; +import com.baeldung.jackson.dtos.User; +import com.baeldung.jackson.dtos.Item; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; diff --git a/jackson/src/test/java/org/baeldung/jackson/deserialization/ItemDeserializerOnClass.java b/jackson/src/test/java/com/baeldung/jackson/deserialization/ItemDeserializerOnClass.java similarity index 88% rename from jackson/src/test/java/org/baeldung/jackson/deserialization/ItemDeserializerOnClass.java rename to jackson/src/test/java/com/baeldung/jackson/deserialization/ItemDeserializerOnClass.java index 346f75db07..169a5c1c50 100644 --- a/jackson/src/test/java/org/baeldung/jackson/deserialization/ItemDeserializerOnClass.java +++ b/jackson/src/test/java/com/baeldung/jackson/deserialization/ItemDeserializerOnClass.java @@ -1,9 +1,9 @@ -package org.baeldung.jackson.deserialization; +package com.baeldung.jackson.deserialization; import java.io.IOException; -import org.baeldung.jackson.dtos.ItemWithSerializer; -import org.baeldung.jackson.dtos.User; +import com.baeldung.jackson.dtos.ItemWithSerializer; +import com.baeldung.jackson.dtos.User; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/Item.java b/jackson/src/test/java/com/baeldung/jackson/dtos/Item.java similarity index 93% rename from jackson/src/test/java/org/baeldung/jackson/dtos/Item.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/Item.java index 1dd840372a..6fce2bc88e 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/Item.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/Item.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.dtos; +package com.baeldung.jackson.dtos; public class Item { public int id; diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/ItemWithSerializer.java b/jackson/src/test/java/com/baeldung/jackson/dtos/ItemWithSerializer.java similarity index 81% rename from jackson/src/test/java/org/baeldung/jackson/dtos/ItemWithSerializer.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/ItemWithSerializer.java index 6dadfa4908..aea9aa770d 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/ItemWithSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/ItemWithSerializer.java @@ -1,7 +1,7 @@ -package org.baeldung.jackson.dtos; +package com.baeldung.jackson.dtos; -import org.baeldung.jackson.deserialization.ItemDeserializerOnClass; -import org.baeldung.jackson.serialization.ItemSerializerOnClass; +import com.baeldung.jackson.deserialization.ItemDeserializerOnClass; +import com.baeldung.jackson.serialization.ItemSerializerOnClass; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/MyDto.java b/jackson/src/test/java/com/baeldung/jackson/dtos/MyDto.java similarity index 96% rename from jackson/src/test/java/org/baeldung/jackson/dtos/MyDto.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/MyDto.java index 668eea3fcc..49cf07baea 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/MyDto.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/MyDto.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.dtos; +package com.baeldung.jackson.dtos; public class MyDto { diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoFieldNameChanged.java b/jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoFieldNameChanged.java similarity index 96% rename from jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoFieldNameChanged.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoFieldNameChanged.java index 9c4086a965..ce7086f1fe 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoFieldNameChanged.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoFieldNameChanged.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.dtos; +package com.baeldung.jackson.dtos; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoIncludeNonDefault.java b/jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoIncludeNonDefault.java similarity index 96% rename from jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoIncludeNonDefault.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoIncludeNonDefault.java index b99d793363..ca03a8be62 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoIncludeNonDefault.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoIncludeNonDefault.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.dtos; +package com.baeldung.jackson.dtos; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoNoAccessors.java b/jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessors.java similarity index 92% rename from jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoNoAccessors.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessors.java index 6e88f5a2db..6e9abc90ae 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoNoAccessors.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessors.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.dtos; +package com.baeldung.jackson.dtos; public class MyDtoNoAccessors { diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoNoAccessorsAndFieldVisibility.java b/jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessorsAndFieldVisibility.java similarity index 94% rename from jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoNoAccessorsAndFieldVisibility.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessorsAndFieldVisibility.java index 1723a71230..149969f769 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoNoAccessorsAndFieldVisibility.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessorsAndFieldVisibility.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.dtos; +package com.baeldung.jackson.dtos; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoWithFilter.java b/jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoWithFilter.java similarity index 96% rename from jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoWithFilter.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoWithFilter.java index 03d7edf985..91f5e148b2 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/MyDtoWithFilter.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoWithFilter.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.dtos; +package com.baeldung.jackson.dtos; import com.fasterxml.jackson.annotation.JsonFilter; diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/MyMixInForString.java b/jackson/src/test/java/com/baeldung/jackson/dtos/MyMixInForString.java similarity index 76% rename from jackson/src/test/java/org/baeldung/jackson/dtos/MyMixInForString.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/MyMixInForString.java index 3d5228139e..b386541df6 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/MyMixInForString.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/MyMixInForString.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.dtos; +package com.baeldung.jackson.dtos; import com.fasterxml.jackson.annotation.JsonIgnoreType; diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/User.java b/jackson/src/test/java/com/baeldung/jackson/dtos/User.java similarity index 90% rename from jackson/src/test/java/org/baeldung/jackson/dtos/User.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/User.java index cef29f11b4..2418e8070d 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/User.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/User.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.dtos; +package com.baeldung.jackson.dtos; public class User { public int id; diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/ignore/MyDtoIgnoreField.java b/jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreField.java similarity index 94% rename from jackson/src/test/java/org/baeldung/jackson/dtos/ignore/MyDtoIgnoreField.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreField.java index 4e3aecd0d2..f573501e85 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/ignore/MyDtoIgnoreField.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreField.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.dtos.ignore; +package com.baeldung.jackson.dtos.ignore; import com.fasterxml.jackson.annotation.JsonIgnore; diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/ignore/MyDtoIgnoreFieldByName.java b/jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreFieldByName.java similarity index 95% rename from jackson/src/test/java/org/baeldung/jackson/dtos/ignore/MyDtoIgnoreFieldByName.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreFieldByName.java index d752e0576d..e7b8ea2a8e 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/ignore/MyDtoIgnoreFieldByName.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreFieldByName.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.dtos.ignore; +package com.baeldung.jackson.dtos.ignore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/ignore/MyDtoIgnoreNull.java b/jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreNull.java similarity index 96% rename from jackson/src/test/java/org/baeldung/jackson/dtos/ignore/MyDtoIgnoreNull.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreNull.java index 0e28e43024..bc443500a1 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/ignore/MyDtoIgnoreNull.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreNull.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.dtos.ignore; +package com.baeldung.jackson.dtos.ignore; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/ignore/MyDtoIgnoreType.java b/jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreType.java similarity index 96% rename from jackson/src/test/java/org/baeldung/jackson/dtos/ignore/MyDtoIgnoreType.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreType.java index 58876aec79..3c813145f6 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/ignore/MyDtoIgnoreType.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreType.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.dtos.ignore; +package com.baeldung.jackson.dtos.ignore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/ignore/MyDtoIgnoreUnknown.java b/jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreUnknown.java similarity index 96% rename from jackson/src/test/java/org/baeldung/jackson/dtos/ignore/MyDtoIgnoreUnknown.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreUnknown.java index ca702343eb..c1174a12f5 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/ignore/MyDtoIgnoreUnknown.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreUnknown.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.dtos.ignore; +package com.baeldung.jackson.dtos.ignore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/MyDtoWithEnum.java b/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/MyDtoWithEnum.java similarity index 96% rename from jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/MyDtoWithEnum.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/MyDtoWithEnum.java index aa05539b89..258eb6febd 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/MyDtoWithEnum.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/MyDtoWithEnum.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.dtos.withEnum; +package com.baeldung.jackson.dtos.withEnum; public class MyDtoWithEnum { diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/MyDtoWithEnumCustom.java b/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/MyDtoWithEnumCustom.java similarity index 96% rename from jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/MyDtoWithEnumCustom.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/MyDtoWithEnumCustom.java index b58ea4bd15..676e22686e 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/MyDtoWithEnumCustom.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/MyDtoWithEnumCustom.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.dtos.withEnum; +package com.baeldung.jackson.dtos.withEnum; public class MyDtoWithEnumCustom { diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/TypeEnum.java b/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeEnum.java similarity index 93% rename from jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/TypeEnum.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeEnum.java index 316fdb12e7..e0c9718330 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/TypeEnum.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeEnum.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.dtos.withEnum; +package com.baeldung.jackson.dtos.withEnum; import com.fasterxml.jackson.annotation.JsonFormat; diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/TypeEnumSimple.java b/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeEnumSimple.java similarity index 92% rename from jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/TypeEnumSimple.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeEnumSimple.java index b76a5740ec..477db67069 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/TypeEnumSimple.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeEnumSimple.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.dtos.withEnum; +package com.baeldung.jackson.dtos.withEnum; public enum TypeEnumSimple { TYPE1(1, "Type A"), TYPE2(2, "Type 2"); diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/TypeEnumWithCustomSerializer.java b/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeEnumWithCustomSerializer.java similarity index 93% rename from jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/TypeEnumWithCustomSerializer.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeEnumWithCustomSerializer.java index 7e004d2d7d..e7c2859dd1 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/TypeEnumWithCustomSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeEnumWithCustomSerializer.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.dtos.withEnum; +package com.baeldung.jackson.dtos.withEnum; import com.fasterxml.jackson.databind.annotation.JsonSerialize; diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/TypeEnumWithValue.java b/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeEnumWithValue.java similarity index 93% rename from jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/TypeEnumWithValue.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeEnumWithValue.java index cf104df473..c5ddf222ff 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/TypeEnumWithValue.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeEnumWithValue.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.dtos.withEnum; +package com.baeldung.jackson.dtos.withEnum; import com.fasterxml.jackson.annotation.JsonValue; diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/TypeSerializer.java b/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeSerializer.java similarity index 94% rename from jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/TypeSerializer.java rename to jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeSerializer.java index 8aa7e5c551..c5d5d7e0a8 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/withEnum/TypeSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeSerializer.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.dtos.withEnum; +package com.baeldung.jackson.dtos.withEnum; import java.io.IOException; diff --git a/jackson/src/test/java/com/baeldung/jackson/dynamicIgnore/Address.java b/jackson/src/test/java/com/baeldung/jackson/dynamicIgnore/Address.java new file mode 100644 index 0000000000..c2d2e84d45 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/dynamicIgnore/Address.java @@ -0,0 +1,41 @@ +package com.baeldung.jackson.dynamicIgnore; + + +public class Address implements Hidable { + private String city; + private String country; + private boolean hidden; + + public Address(final String city, final String country, final boolean hidden) { + super(); + this.city = city; + this.country = country; + this.hidden = hidden; + } + + public String getCity() { + return city; + } + + public void setCity(final String city) { + this.city = city; + } + + public String getCountry() { + return country; + } + + public void setCountry(final String country) { + this.country = country; + } + + @Override + public boolean isHidden() { + return hidden; + } + + public void setHidden(final boolean hidden) { + this.hidden = hidden; + } + +} diff --git a/jackson/src/test/java/com/baeldung/jackson/dynamicIgnore/Hidable.java b/jackson/src/test/java/com/baeldung/jackson/dynamicIgnore/Hidable.java new file mode 100644 index 0000000000..edca786432 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/dynamicIgnore/Hidable.java @@ -0,0 +1,9 @@ +package com.baeldung.jackson.dynamicIgnore; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + + +@JsonIgnoreProperties("hidden") +public interface Hidable { + boolean isHidden(); +} diff --git a/jackson/src/test/java/com/baeldung/jackson/dynamicIgnore/HidableSerializer.java b/jackson/src/test/java/com/baeldung/jackson/dynamicIgnore/HidableSerializer.java new file mode 100644 index 0000000000..46396dae2a --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/dynamicIgnore/HidableSerializer.java @@ -0,0 +1,29 @@ +package com.baeldung.jackson.dynamicIgnore; + +import java.io.IOException; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; + +public class HidableSerializer extends JsonSerializer { + + private JsonSerializer defaultSerializer; + + public HidableSerializer(final JsonSerializer serializer) { + defaultSerializer = serializer; + } + + @Override + public void serialize(final Hidable value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException { + if (value.isHidden()) + return; + defaultSerializer.serialize(value, jgen, provider); + } + + @Override + public boolean isEmpty(final SerializerProvider provider, final Hidable value) { + return (value == null || value.isHidden()); + } +} diff --git a/jackson/src/test/java/com/baeldung/jackson/dynamicIgnore/Person.java b/jackson/src/test/java/com/baeldung/jackson/dynamicIgnore/Person.java new file mode 100644 index 0000000000..366f611edf --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/dynamicIgnore/Person.java @@ -0,0 +1,41 @@ +package com.baeldung.jackson.dynamicIgnore; + + +public class Person implements Hidable { + private String name; + private Address address; + private boolean hidden; + + public Person(final String name, final Address address, final boolean hidden) { + super(); + this.name = name; + this.address = address; + this.hidden = hidden; + } + + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public Address getAddress() { + return address; + } + + public void setAddress(final Address address) { + this.address = address; + } + @Override + public boolean isHidden() { + return hidden; + } + + public void setHidden(final boolean hidden) { + this.hidden = hidden; + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/exception/User.java b/jackson/src/test/java/com/baeldung/jackson/exception/User.java similarity index 81% rename from jackson/src/test/java/org/baeldung/jackson/exception/User.java rename to jackson/src/test/java/com/baeldung/jackson/exception/User.java index 764d5872ad..1d78e82bec 100644 --- a/jackson/src/test/java/org/baeldung/jackson/exception/User.java +++ b/jackson/src/test/java/com/baeldung/jackson/exception/User.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.exception; +package com.baeldung.jackson.exception; public class User { public int id; diff --git a/jackson/src/test/java/org/baeldung/jackson/exception/UserWithConflict.java b/jackson/src/test/java/com/baeldung/jackson/exception/UserWithConflict.java similarity index 91% rename from jackson/src/test/java/org/baeldung/jackson/exception/UserWithConflict.java rename to jackson/src/test/java/com/baeldung/jackson/exception/UserWithConflict.java index 79d4199e91..01ff695475 100644 --- a/jackson/src/test/java/org/baeldung/jackson/exception/UserWithConflict.java +++ b/jackson/src/test/java/com/baeldung/jackson/exception/UserWithConflict.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.exception; +package com.baeldung.jackson.exception; public class UserWithConflict { public int id; diff --git a/jackson/src/test/java/org/baeldung/jackson/exception/UserWithPrivateFields.java b/jackson/src/test/java/com/baeldung/jackson/exception/UserWithPrivateFields.java similarity index 86% rename from jackson/src/test/java/org/baeldung/jackson/exception/UserWithPrivateFields.java rename to jackson/src/test/java/com/baeldung/jackson/exception/UserWithPrivateFields.java index 707623bbb2..f627975184 100644 --- a/jackson/src/test/java/org/baeldung/jackson/exception/UserWithPrivateFields.java +++ b/jackson/src/test/java/com/baeldung/jackson/exception/UserWithPrivateFields.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.exception; +package com.baeldung.jackson.exception; public class UserWithPrivateFields { int id; diff --git a/jackson/src/test/java/org/baeldung/jackson/exception/UserWithRoot.java b/jackson/src/test/java/com/baeldung/jackson/exception/UserWithRoot.java similarity index 89% rename from jackson/src/test/java/org/baeldung/jackson/exception/UserWithRoot.java rename to jackson/src/test/java/com/baeldung/jackson/exception/UserWithRoot.java index 2cf78e0365..d879c16e6a 100644 --- a/jackson/src/test/java/org/baeldung/jackson/exception/UserWithRoot.java +++ b/jackson/src/test/java/com/baeldung/jackson/exception/UserWithRoot.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.exception; +package com.baeldung.jackson.exception; import com.fasterxml.jackson.annotation.JsonRootName; diff --git a/jackson/src/test/java/org/baeldung/jackson/exception/Zoo.java b/jackson/src/test/java/com/baeldung/jackson/exception/Zoo.java similarity index 84% rename from jackson/src/test/java/org/baeldung/jackson/exception/Zoo.java rename to jackson/src/test/java/com/baeldung/jackson/exception/Zoo.java index 79c24569f2..647b5955c0 100644 --- a/jackson/src/test/java/org/baeldung/jackson/exception/Zoo.java +++ b/jackson/src/test/java/com/baeldung/jackson/exception/Zoo.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.exception; +package com.baeldung.jackson.exception; public class Zoo { public Animal animal; diff --git a/jackson/src/test/java/org/baeldung/jackson/exception/ZooConfigured.java b/jackson/src/test/java/com/baeldung/jackson/exception/ZooConfigured.java similarity index 91% rename from jackson/src/test/java/org/baeldung/jackson/exception/ZooConfigured.java rename to jackson/src/test/java/com/baeldung/jackson/exception/ZooConfigured.java index 776b702acb..f51b1e150a 100644 --- a/jackson/src/test/java/org/baeldung/jackson/exception/ZooConfigured.java +++ b/jackson/src/test/java/com/baeldung/jackson/exception/ZooConfigured.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.exception; +package com.baeldung.jackson.exception; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; diff --git a/jackson/src/test/java/org/baeldung/jackson/field/MyDto.java b/jackson/src/test/java/com/baeldung/jackson/field/MyDto.java similarity index 96% rename from jackson/src/test/java/org/baeldung/jackson/field/MyDto.java rename to jackson/src/test/java/com/baeldung/jackson/field/MyDto.java index edfc9d2f91..f19371937d 100644 --- a/jackson/src/test/java/org/baeldung/jackson/field/MyDto.java +++ b/jackson/src/test/java/com/baeldung/jackson/field/MyDto.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.field; +package com.baeldung.jackson.field; public class MyDto { diff --git a/jackson/src/test/java/org/baeldung/jackson/field/MyDtoAccessLevel.java b/jackson/src/test/java/com/baeldung/jackson/field/MyDtoAccessLevel.java similarity index 93% rename from jackson/src/test/java/org/baeldung/jackson/field/MyDtoAccessLevel.java rename to jackson/src/test/java/com/baeldung/jackson/field/MyDtoAccessLevel.java index c164b00e4a..df16720038 100644 --- a/jackson/src/test/java/org/baeldung/jackson/field/MyDtoAccessLevel.java +++ b/jackson/src/test/java/com/baeldung/jackson/field/MyDtoAccessLevel.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.field; +package com.baeldung.jackson.field; public class MyDtoAccessLevel { diff --git a/jackson/src/test/java/org/baeldung/jackson/field/MyDtoWithGetter.java b/jackson/src/test/java/com/baeldung/jackson/field/MyDtoWithGetter.java similarity index 91% rename from jackson/src/test/java/org/baeldung/jackson/field/MyDtoWithGetter.java rename to jackson/src/test/java/com/baeldung/jackson/field/MyDtoWithGetter.java index 0f15b2fcc3..4ea20b93c1 100644 --- a/jackson/src/test/java/org/baeldung/jackson/field/MyDtoWithGetter.java +++ b/jackson/src/test/java/com/baeldung/jackson/field/MyDtoWithGetter.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.field; +package com.baeldung.jackson.field; public class MyDtoWithGetter { diff --git a/jackson/src/test/java/org/baeldung/jackson/field/MyDtoWithSetter.java b/jackson/src/test/java/com/baeldung/jackson/field/MyDtoWithSetter.java similarity index 93% rename from jackson/src/test/java/org/baeldung/jackson/field/MyDtoWithSetter.java rename to jackson/src/test/java/com/baeldung/jackson/field/MyDtoWithSetter.java index e37793492e..fd3f6790a3 100644 --- a/jackson/src/test/java/org/baeldung/jackson/field/MyDtoWithSetter.java +++ b/jackson/src/test/java/com/baeldung/jackson/field/MyDtoWithSetter.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.field; +package com.baeldung.jackson.field; public class MyDtoWithSetter { diff --git a/jackson/src/test/java/com/baeldung/jackson/inheritance/IgnoranceAnnotationStructure.java b/jackson/src/test/java/com/baeldung/jackson/inheritance/IgnoranceAnnotationStructure.java new file mode 100644 index 0000000000..520929463c --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/inheritance/IgnoranceAnnotationStructure.java @@ -0,0 +1,96 @@ +package com.baeldung.jackson.inheritance; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +public class IgnoranceAnnotationStructure { + public static abstract class Vehicle { + private String make; + private String model; + + protected Vehicle() { + } + + protected Vehicle(String make, String model) { + this.make = make; + this.model = model; + } + + public String getMake() { + return make; + } + + public void setMake(String make) { + this.make = make; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + } + + @JsonIgnoreProperties({ "model", "seatingCapacity" }) + public static abstract class Car extends Vehicle { + private int seatingCapacity; + @JsonIgnore + private double topSpeed; + + protected Car() { + } + + protected Car(String make, String model, int seatingCapacity, double topSpeed) { + super(make, model); + this.seatingCapacity = seatingCapacity; + this.topSpeed = topSpeed; + } + + public int getSeatingCapacity() { + return seatingCapacity; + } + + public void setSeatingCapacity(int seatingCapacity) { + this.seatingCapacity = seatingCapacity; + } + + public double getTopSpeed() { + return topSpeed; + } + + public void setTopSpeed(double topSpeed) { + this.topSpeed = topSpeed; + } + } + + public static class Sedan extends Car { + public Sedan() { + } + + public Sedan(String make, String model, int seatingCapacity, double topSpeed) { + super(make, model, seatingCapacity, topSpeed); + } + } + + public static class Crossover extends Car { + private double towingCapacity; + + public Crossover() { + } + + public Crossover(String make, String model, int seatingCapacity, double topSpeed, double towingCapacity) { + super(make, model, seatingCapacity, topSpeed); + this.towingCapacity = towingCapacity; + } + + public double getTowingCapacity() { + return towingCapacity; + } + + public void setTowingCapacity(double towingCapacity) { + this.towingCapacity = towingCapacity; + } + } +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/inheritance/IgnoranceMixinOrIntrospection.java b/jackson/src/test/java/com/baeldung/jackson/inheritance/IgnoranceMixinOrIntrospection.java new file mode 100644 index 0000000000..52c0bbea5e --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/inheritance/IgnoranceMixinOrIntrospection.java @@ -0,0 +1,91 @@ +package com.baeldung.jackson.inheritance; + +public class IgnoranceMixinOrIntrospection { + public static abstract class Vehicle { + private String make; + private String model; + + protected Vehicle() { + } + + protected Vehicle(String make, String model) { + this.make = make; + this.model = model; + } + + public String getMake() { + return make; + } + + public void setMake(String make) { + this.make = make; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + } + + public static abstract class Car extends Vehicle { + private int seatingCapacity; + private double topSpeed; + + protected Car() { + } + + protected Car(String make, String model, int seatingCapacity, double topSpeed) { + super(make, model); + this.seatingCapacity = seatingCapacity; + this.topSpeed = topSpeed; + } + + public int getSeatingCapacity() { + return seatingCapacity; + } + + public void setSeatingCapacity(int seatingCapacity) { + this.seatingCapacity = seatingCapacity; + } + + public double getTopSpeed() { + return topSpeed; + } + + public void setTopSpeed(double topSpeed) { + this.topSpeed = topSpeed; + } + } + + public static class Sedan extends Car { + public Sedan() { + } + + public Sedan(String make, String model, int seatingCapacity, double topSpeed) { + super(make, model, seatingCapacity, topSpeed); + } + } + + public static class Crossover extends Car { + private double towingCapacity; + + public Crossover() { + } + + public Crossover(String make, String model, int seatingCapacity, double topSpeed, double towingCapacity) { + super(make, model, seatingCapacity, topSpeed); + this.towingCapacity = towingCapacity; + } + + public double getTowingCapacity() { + return towingCapacity; + } + + public void setTowingCapacity(double towingCapacity) { + this.towingCapacity = towingCapacity; + } + } +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/inheritance/IgnoranceTest.java b/jackson/src/test/java/com/baeldung/jackson/inheritance/IgnoranceTest.java new file mode 100644 index 0000000000..8d22f471a6 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/inheritance/IgnoranceTest.java @@ -0,0 +1,92 @@ +package com.baeldung.jackson.inheritance; + +import static org.junit.Assert.assertThat; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.not; + +import org.junit.Test; + +import java.util.List; +import java.util.ArrayList; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.introspect.AnnotatedMember; +import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector; + +public class IgnoranceTest { + private static abstract class CarMixIn { + @JsonIgnore + public String make; + @JsonIgnore + public String topSpeed; + } + + private static class IgnoranceIntrospector extends JacksonAnnotationIntrospector { + private static final long serialVersionUID = 1422295680188892323L; + + public boolean hasIgnoreMarker(AnnotatedMember m) { + return m.getDeclaringClass() == IgnoranceMixinOrIntrospection.Vehicle.class && m.getName() == "model" || m.getDeclaringClass() == IgnoranceMixinOrIntrospection.Car.class || m.getName() == "towingCapacity" || super.hasIgnoreMarker(m); + } + } + + @Test + public void givenAnnotations_whenIgnoringProperties_thenCorrect() throws JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(); + + IgnoranceAnnotationStructure.Sedan sedan = new IgnoranceAnnotationStructure.Sedan("Mercedes-Benz", "S500", 5, 250.0); + IgnoranceAnnotationStructure.Crossover crossover = new IgnoranceAnnotationStructure.Crossover("BMW", "X6", 5, 250.0, 6000.0); + + List vehicles = new ArrayList<>(); + vehicles.add(sedan); + vehicles.add(crossover); + + String jsonDataString = mapper.writeValueAsString(vehicles); + + assertThat(jsonDataString, containsString("make")); + assertThat(jsonDataString, not(containsString("model"))); + assertThat(jsonDataString, not(containsString("seatingCapacity"))); + assertThat(jsonDataString, not(containsString("topSpeed"))); + assertThat(jsonDataString, containsString("towingCapacity")); + } + + @Test + public void givenMixIns_whenIgnoringProperties_thenCorrect() throws JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(); + mapper.addMixIn(IgnoranceMixinOrIntrospection.Car.class, CarMixIn.class); + + String jsonDataString = instantiateAndSerializeObjects(mapper); + + assertThat(jsonDataString, not(containsString("make"))); + assertThat(jsonDataString, containsString("model")); + assertThat(jsonDataString, containsString("seatingCapacity")); + assertThat(jsonDataString, not(containsString("topSpeed"))); + assertThat(jsonDataString, containsString("towingCapacity")); + } + + @Test + public void givenIntrospection_whenIgnoringProperties_thenCorrect() throws JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(); + mapper.setAnnotationIntrospector(new IgnoranceIntrospector()); + + String jsonDataString = instantiateAndSerializeObjects(mapper); + + assertThat(jsonDataString, containsString("make")); + assertThat(jsonDataString, not(containsString("model"))); + assertThat(jsonDataString, not(containsString("seatingCapacity"))); + assertThat(jsonDataString, not(containsString("topSpeed"))); + assertThat(jsonDataString, not(containsString("towingCapacity"))); + } + + private String instantiateAndSerializeObjects(ObjectMapper mapper) throws JsonProcessingException { + IgnoranceMixinOrIntrospection.Sedan sedan = new IgnoranceMixinOrIntrospection.Sedan("Mercedes-Benz", "S500", 5, 250.0); + IgnoranceMixinOrIntrospection.Crossover crossover = new IgnoranceMixinOrIntrospection.Crossover("BMW", "X6", 5, 250.0, 6000.0); + + List vehicles = new ArrayList<>(); + vehicles.add(sedan); + vehicles.add(crossover); + + return mapper.writeValueAsString(vehicles); + } +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeConstructorStructure.java b/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeConstructorStructure.java new file mode 100644 index 0000000000..8a8db8ae47 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeConstructorStructure.java @@ -0,0 +1,92 @@ +package com.baeldung.jackson.inheritance; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; + +public class SubTypeConstructorStructure { + public static class Fleet { + private List vehicles; + + public List getVehicles() { + return vehicles; + } + + public void setVehicles(List vehicles) { + this.vehicles = vehicles; + } + } + + public static abstract class Vehicle { + private String make; + private String model; + + protected Vehicle(String make, String model) { + this.make = make; + this.model = model; + } + + public String getMake() { + return make; + } + + public void setMake(String make) { + this.make = make; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + } + + public static class Car extends Vehicle { + private int seatingCapacity; + private double topSpeed; + + @JsonCreator + public Car(@JsonProperty("make") String make, @JsonProperty("model") String model, @JsonProperty("seating") int seatingCapacity, @JsonProperty("topSpeed") double topSpeed) { + super(make, model); + this.seatingCapacity = seatingCapacity; + this.topSpeed = topSpeed; + } + + public int getSeatingCapacity() { + return seatingCapacity; + } + + public void setSeatingCapacity(int seatingCapacity) { + this.seatingCapacity = seatingCapacity; + } + + public double getTopSpeed() { + return topSpeed; + } + + public void setTopSpeed(double topSpeed) { + this.topSpeed = topSpeed; + } + } + + public static class Truck extends Vehicle { + private double payloadCapacity; + + @JsonCreator + public Truck(@JsonProperty("make") String make, @JsonProperty("model") String model, @JsonProperty("payload") double payloadCapacity) { + super(make, model); + this.payloadCapacity = payloadCapacity; + } + + public double getPayloadCapacity() { + return payloadCapacity; + } + + public void setPayloadCapacity(double payloadCapacity) { + this.payloadCapacity = payloadCapacity; + } + } +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeConversionStructure.java b/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeConversionStructure.java new file mode 100644 index 0000000000..346fd65eef --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeConversionStructure.java @@ -0,0 +1,87 @@ +package com.baeldung.jackson.inheritance; + +import com.fasterxml.jackson.annotation.JsonIgnore; + +public class SubTypeConversionStructure { + public static abstract class Vehicle { + private String make; + private String model; + + protected Vehicle() { + } + + protected Vehicle(String make, String model) { + this.make = make; + this.model = model; + } + + public String getMake() { + return make; + } + + public void setMake(String make) { + this.make = make; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + } + + public static class Car extends Vehicle { + @JsonIgnore + private int seatingCapacity; + @JsonIgnore + private double topSpeed; + + public Car() { + } + + public Car(String make, String model, int seatingCapacity, double topSpeed) { + super(make, model); + this.seatingCapacity = seatingCapacity; + this.topSpeed = topSpeed; + } + + public int getSeatingCapacity() { + return seatingCapacity; + } + + public void setSeatingCapacity(int seatingCapacity) { + this.seatingCapacity = seatingCapacity; + } + + public double getTopSpeed() { + return topSpeed; + } + + public void setTopSpeed(double topSpeed) { + this.topSpeed = topSpeed; + } + } + + public static class Truck extends Vehicle { + @JsonIgnore + private double payloadCapacity; + + public Truck() { + } + + public Truck(String make, String model, double payloadCapacity) { + super(make, model); + this.payloadCapacity = payloadCapacity; + } + + public double getPayloadCapacity() { + return payloadCapacity; + } + + public void setPayloadCapacity(double payloadCapacity) { + this.payloadCapacity = payloadCapacity; + } + } +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeHandlingTest.java b/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeHandlingTest.java new file mode 100644 index 0000000000..2d4c8fe698 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeHandlingTest.java @@ -0,0 +1,43 @@ +package com.baeldung.jackson.inheritance; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import java.util.List; +import java.util.ArrayList; +import java.io.IOException; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class SubTypeHandlingTest { + @Test + public void givenSubTypes_whenConvertingObjects_thenDataValuesArePreserved() { + ObjectMapper mapper = new ObjectMapper(); + + SubTypeConversionStructure.Car car = new SubTypeConversionStructure.Car("Mercedes-Benz", "S500", 5, 250.0); + SubTypeConversionStructure.Truck truck = mapper.convertValue(car, SubTypeConversionStructure.Truck.class); + + assertEquals("Mercedes-Benz", truck.getMake()); + assertEquals("S500", truck.getModel()); + } + + @Test + public void givenSubType_whenNotUsingNoArgsConstructors_thenSucceed() throws IOException{ + ObjectMapper mapper = new ObjectMapper(); + mapper.enableDefaultTyping(); + + SubTypeConstructorStructure.Car car = new SubTypeConstructorStructure.Car("Mercedes-Benz", "S500", 5, 250.0); + SubTypeConstructorStructure.Truck truck = new SubTypeConstructorStructure.Truck("Isuzu", "NQR", 7500.0); + + List vehicles = new ArrayList<>(); + vehicles.add(car); + vehicles.add(truck); + + SubTypeConstructorStructure.Fleet serializedFleet = new SubTypeConstructorStructure.Fleet(); + serializedFleet.setVehicles(vehicles); + + String jsonDataString = mapper.writeValueAsString(serializedFleet); + mapper.readValue(jsonDataString, SubTypeConstructorStructure.Fleet.class); + } +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoAnnotatedStructure.java b/jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoAnnotatedStructure.java new file mode 100644 index 0000000000..cb552a7b80 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoAnnotatedStructure.java @@ -0,0 +1,102 @@ +package com.baeldung.jackson.inheritance; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonSubTypes.Type; + +public class TypeInfoAnnotatedStructure { + public static class Fleet { + private List vehicles; + + public List getVehicles() { + return vehicles; + } + + public void setVehicles(List vehicles) { + this.vehicles = vehicles; + } + } + + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type") + @JsonSubTypes({ @Type(value = Car.class, name = "car"), @Type(value = Truck.class, name = "truck") }) + public static abstract class Vehicle { + private String make; + private String model; + + protected Vehicle() { + } + + protected Vehicle(String make, String model) { + this.make = make; + this.model = model; + } + + public String getMake() { + return make; + } + + public void setMake(String make) { + this.make = make; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + } + + public static class Car extends Vehicle { + private int seatingCapacity; + private double topSpeed; + + public Car() { + } + + public Car(String make, String model, int seatingCapacity, double topSpeed) { + super(make, model); + this.seatingCapacity = seatingCapacity; + this.topSpeed = topSpeed; + } + + public int getSeatingCapacity() { + return seatingCapacity; + } + + public void setSeatingCapacity(int seatingCapacity) { + this.seatingCapacity = seatingCapacity; + } + + public double getTopSpeed() { + return topSpeed; + } + + public void setTopSpeed(double topSpeed) { + this.topSpeed = topSpeed; + } + } + + public static class Truck extends Vehicle { + private double payloadCapacity; + + public Truck() { + } + + public Truck(String make, String model, double payloadCapacity) { + super(make, model); + this.payloadCapacity = payloadCapacity; + } + + public double getPayloadCapacity() { + return payloadCapacity; + } + + public void setPayloadCapacity(double payloadCapacity) { + this.payloadCapacity = payloadCapacity; + } + } +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoInclusionTest.java b/jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoInclusionTest.java new file mode 100644 index 0000000000..aefbc172d0 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoInclusionTest.java @@ -0,0 +1,57 @@ +package com.baeldung.jackson.inheritance; + +import static org.junit.Assert.assertThat; +import static org.hamcrest.CoreMatchers.instanceOf; + +import org.junit.Test; + +import java.util.List; +import java.util.ArrayList; +import java.io.IOException; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class TypeInfoInclusionTest { + @Test + public void givenTypeInfo_whenAnnotatingGlobally_thenTypesAreCorrectlyRecovered() throws IOException { + ObjectMapper mapper = new ObjectMapper(); + mapper.enableDefaultTyping(); + + TypeInfoStructure.Car car = new TypeInfoStructure.Car("Mercedes-Benz", "S500", 5, 250.0); + TypeInfoStructure.Truck truck = new TypeInfoStructure.Truck("Isuzu", "NQR", 7500.0); + + List vehicles = new ArrayList<>(); + vehicles.add(car); + vehicles.add(truck); + + TypeInfoStructure.Fleet serializedFleet = new TypeInfoStructure.Fleet(); + serializedFleet.setVehicles(vehicles); + + String jsonDataString = mapper.writeValueAsString(serializedFleet); + TypeInfoStructure.Fleet deserializedFleet = mapper.readValue(jsonDataString, TypeInfoStructure.Fleet.class); + + assertThat(deserializedFleet.getVehicles().get(0), instanceOf(TypeInfoStructure.Car.class)); + assertThat(deserializedFleet.getVehicles().get(1), instanceOf(TypeInfoStructure.Truck.class)); + } + + @Test + public void givenTypeInfo_whenAnnotatingPerClass_thenTypesAreCorrectlyRecovered() throws IOException { + ObjectMapper mapper = new ObjectMapper(); + + TypeInfoAnnotatedStructure.Car car = new TypeInfoAnnotatedStructure.Car("Mercedes-Benz", "S500", 5, 250.0); + TypeInfoAnnotatedStructure.Truck truck = new TypeInfoAnnotatedStructure.Truck("Isuzu", "NQR", 7500.0); + + List vehicles = new ArrayList<>(); + vehicles.add(car); + vehicles.add(truck); + + TypeInfoAnnotatedStructure.Fleet serializedFleet = new TypeInfoAnnotatedStructure.Fleet(); + serializedFleet.setVehicles(vehicles); + + String jsonDataString = mapper.writeValueAsString(serializedFleet); + TypeInfoAnnotatedStructure.Fleet deserializedFleet = mapper.readValue(jsonDataString, TypeInfoAnnotatedStructure.Fleet.class); + + assertThat(deserializedFleet.getVehicles().get(0), instanceOf(TypeInfoAnnotatedStructure.Car.class)); + assertThat(deserializedFleet.getVehicles().get(1), instanceOf(TypeInfoAnnotatedStructure.Truck.class)); + } +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoStructure.java b/jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoStructure.java new file mode 100644 index 0000000000..5c5186dfcc --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoStructure.java @@ -0,0 +1,96 @@ +package com.baeldung.jackson.inheritance; + +import java.util.List; + +public class TypeInfoStructure { + public static class Fleet { + private List vehicles; + + public List getVehicles() { + return vehicles; + } + + public void setVehicles(List vehicles) { + this.vehicles = vehicles; + } + } + + public static abstract class Vehicle { + private String make; + private String model; + + protected Vehicle() { + } + + protected Vehicle(String make, String model) { + this.make = make; + this.model = model; + } + + public String getMake() { + return make; + } + + public void setMake(String make) { + this.make = make; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + } + + public static class Car extends Vehicle { + private int seatingCapacity; + private double topSpeed; + + public Car() { + } + + public Car(String make, String model, int seatingCapacity, double topSpeed) { + super(make, model); + this.seatingCapacity = seatingCapacity; + this.topSpeed = topSpeed; + } + + public int getSeatingCapacity() { + return seatingCapacity; + } + + public void setSeatingCapacity(int seatingCapacity) { + this.seatingCapacity = seatingCapacity; + } + + public double getTopSpeed() { + return topSpeed; + } + + public void setTopSpeed(double topSpeed) { + this.topSpeed = topSpeed; + } + } + + public static class Truck extends Vehicle { + private double payloadCapacity; + + public Truck() { + } + + public Truck(String make, String model, double payloadCapacity) { + super(make, model); + this.payloadCapacity = payloadCapacity; + } + + public double getPayloadCapacity() { + return payloadCapacity; + } + + public void setPayloadCapacity(double payloadCapacity) { + this.payloadCapacity = payloadCapacity; + } + } +} \ No newline at end of file diff --git a/jackson/src/test/java/org/baeldung/jackson/jsonview/Item.java b/jackson/src/test/java/com/baeldung/jackson/jsonview/Item.java similarity index 94% rename from jackson/src/test/java/org/baeldung/jackson/jsonview/Item.java rename to jackson/src/test/java/com/baeldung/jackson/jsonview/Item.java index 5705f810af..26d20d4847 100644 --- a/jackson/src/test/java/org/baeldung/jackson/jsonview/Item.java +++ b/jackson/src/test/java/com/baeldung/jackson/jsonview/Item.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.jsonview; +package com.baeldung.jackson.jsonview; import com.fasterxml.jackson.annotation.JsonView; diff --git a/jackson/src/test/java/org/baeldung/jackson/jsonview/MyBeanSerializerModifier.java b/jackson/src/test/java/com/baeldung/jackson/jsonview/MyBeanSerializerModifier.java similarity index 95% rename from jackson/src/test/java/org/baeldung/jackson/jsonview/MyBeanSerializerModifier.java rename to jackson/src/test/java/com/baeldung/jackson/jsonview/MyBeanSerializerModifier.java index 0986e5ea76..3b94c13d8b 100644 --- a/jackson/src/test/java/org/baeldung/jackson/jsonview/MyBeanSerializerModifier.java +++ b/jackson/src/test/java/com/baeldung/jackson/jsonview/MyBeanSerializerModifier.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.jsonview; +package com.baeldung.jackson.jsonview; import java.util.List; diff --git a/jackson/src/test/java/org/baeldung/jackson/jsonview/UpperCasingWriter.java b/jackson/src/test/java/com/baeldung/jackson/jsonview/UpperCasingWriter.java similarity index 94% rename from jackson/src/test/java/org/baeldung/jackson/jsonview/UpperCasingWriter.java rename to jackson/src/test/java/com/baeldung/jackson/jsonview/UpperCasingWriter.java index 948fb36cda..3a69d66a05 100644 --- a/jackson/src/test/java/org/baeldung/jackson/jsonview/UpperCasingWriter.java +++ b/jackson/src/test/java/com/baeldung/jackson/jsonview/UpperCasingWriter.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.jsonview; +package com.baeldung.jackson.jsonview; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.SerializerProvider; diff --git a/jackson/src/test/java/org/baeldung/jackson/jsonview/User.java b/jackson/src/test/java/com/baeldung/jackson/jsonview/User.java similarity index 91% rename from jackson/src/test/java/org/baeldung/jackson/jsonview/User.java rename to jackson/src/test/java/com/baeldung/jackson/jsonview/User.java index 5850dcbe84..48353fe4e4 100644 --- a/jackson/src/test/java/org/baeldung/jackson/jsonview/User.java +++ b/jackson/src/test/java/com/baeldung/jackson/jsonview/User.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.jsonview; +package com.baeldung.jackson.jsonview; import com.fasterxml.jackson.annotation.JsonView; diff --git a/jackson/src/test/java/org/baeldung/jackson/jsonview/Views.java b/jackson/src/test/java/com/baeldung/jackson/jsonview/Views.java similarity index 75% rename from jackson/src/test/java/org/baeldung/jackson/jsonview/Views.java rename to jackson/src/test/java/com/baeldung/jackson/jsonview/Views.java index 0a430ad123..65950b7f9f 100644 --- a/jackson/src/test/java/org/baeldung/jackson/jsonview/Views.java +++ b/jackson/src/test/java/com/baeldung/jackson/jsonview/Views.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.jsonview; +package com.baeldung.jackson.jsonview; public class Views { public static class Public { diff --git a/jackson/src/test/java/com/baeldung/jackson/node/ExampleStructure.java b/jackson/src/test/java/com/baeldung/jackson/node/ExampleStructure.java new file mode 100644 index 0000000000..14f9024d0b --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/node/ExampleStructure.java @@ -0,0 +1,17 @@ +package com.baeldung.jackson.node; + +import java.io.IOException; +import java.io.InputStream; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class ExampleStructure { + private static ObjectMapper mapper = new ObjectMapper(); + + static JsonNode getExampleRoot() throws IOException { + InputStream exampleInput = ExampleStructure.class.getClassLoader().getResourceAsStream("node_example.json"); + JsonNode rootNode = mapper.readTree(exampleInput); + return rootNode; + } +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/node/NodeBean.java b/jackson/src/test/java/com/baeldung/jackson/node/NodeBean.java new file mode 100644 index 0000000000..da5ffece51 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/node/NodeBean.java @@ -0,0 +1,30 @@ +package com.baeldung.jackson.node; + +public class NodeBean { + private int id; + private String name; + + public NodeBean() { + } + + public NodeBean(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/jackson/src/test/java/com/baeldung/jackson/node/NodeOperationTest.java b/jackson/src/test/java/com/baeldung/jackson/node/NodeOperationTest.java new file mode 100644 index 0000000000..2fcb828613 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/node/NodeOperationTest.java @@ -0,0 +1,103 @@ +package com.baeldung.jackson.node; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +import org.junit.Test; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; + +public class NodeOperationTest { + private static ObjectMapper mapper = new ObjectMapper(); + + @Test + public void givenAnObject_whenConvertingIntoNode_thenCorrect() { + final NodeBean fromValue = new NodeBean(2016, "baeldung.com"); + + final JsonNode node = mapper.valueToTree(fromValue); + + assertEquals(2016, node.get("id").intValue()); + assertEquals("baeldung.com", node.get("name").textValue()); + } + + @Test + public void givenANode_whenWritingOutAsAJsonString_thenCorrect() throws IOException { + final String pathToTestFile = "node_to_json_test.json"; + final char[] characterBuffer = new char[50]; + + final JsonNode node = mapper.createObjectNode(); + ((ObjectNode) node).put("id", 2016); + ((ObjectNode) node).put("name", "baeldung.com"); + + try (FileWriter outputStream = new FileWriter(pathToTestFile)) { + mapper.writeValue(outputStream, node); + } + + try (FileReader inputStreamForAssertion = new FileReader(pathToTestFile)) { + inputStreamForAssertion.read(characterBuffer); + } + final String textContentOfTestFile = new String(characterBuffer); + + assertThat(textContentOfTestFile, containsString("2016")); + assertThat(textContentOfTestFile, containsString("baeldung.com")); + + Files.delete(Paths.get(pathToTestFile)); + } + + @Test + public void givenANode_whenConvertingIntoAnObject_thenCorrect() throws JsonProcessingException { + final JsonNode node = mapper.createObjectNode(); + ((ObjectNode) node).put("id", 2016); + ((ObjectNode) node).put("name", "baeldung.com"); + + final NodeBean toValue = mapper.treeToValue(node, NodeBean.class); + + assertEquals(2016, toValue.getId()); + assertEquals("baeldung.com", toValue.getName()); + } + + @Test + public void givenANode_whenAddingIntoATree_thenCorrect() throws IOException { + final JsonNode rootNode = ExampleStructure.getExampleRoot(); + final ObjectNode addedNode = ((ObjectNode) rootNode).putObject("address"); + addedNode.put("city", "Seattle").put("state", "Washington").put("country", "United States"); + + assertFalse(rootNode.path("address").isMissingNode()); + assertEquals("Seattle", rootNode.path("address").path("city").textValue()); + assertEquals("Washington", rootNode.path("address").path("state").textValue()); + assertEquals("United States", rootNode.path("address").path("country").textValue()); + } + + @Test + public void givenANode_whenModifyingIt_thenCorrect() throws IOException { + final String newString = "{\"nick\": \"cowtowncoder\"}"; + final JsonNode newNode = mapper.readTree(newString); + + final JsonNode rootNode = ExampleStructure.getExampleRoot(); + ((ObjectNode) rootNode).set("name", newNode); + + assertFalse(rootNode.path("name").path("nick").isMissingNode()); + assertEquals("cowtowncoder", rootNode.path("name").path("nick").textValue()); + } + + @Test + public void givenANode_whenRemovingFromATree_thenCorrect() throws IOException { + final JsonNode rootNode = ExampleStructure.getExampleRoot(); + ((ObjectNode) rootNode).remove("company"); + + assertTrue(rootNode.path("company").isMissingNode()); + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/sandbox/JacksonPrettyPrintUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/sandbox/JacksonPrettyPrintUnitTest.java similarity index 97% rename from jackson/src/test/java/org/baeldung/jackson/sandbox/JacksonPrettyPrintUnitTest.java rename to jackson/src/test/java/com/baeldung/jackson/sandbox/JacksonPrettyPrintUnitTest.java index 58dcec51f6..a68af20f15 100644 --- a/jackson/src/test/java/org/baeldung/jackson/sandbox/JacksonPrettyPrintUnitTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/sandbox/JacksonPrettyPrintUnitTest.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.sandbox; +package com.baeldung.jackson.sandbox; import java.io.File; import java.io.IOException; diff --git a/jackson/src/test/java/org/baeldung/jackson/sandbox/SandboxTest.java b/jackson/src/test/java/com/baeldung/jackson/sandbox/SandboxTest.java similarity index 96% rename from jackson/src/test/java/org/baeldung/jackson/sandbox/SandboxTest.java rename to jackson/src/test/java/com/baeldung/jackson/sandbox/SandboxTest.java index 19491377e8..958a7eec98 100644 --- a/jackson/src/test/java/org/baeldung/jackson/sandbox/SandboxTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/sandbox/SandboxTest.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.sandbox; +package com.baeldung.jackson.sandbox; import java.io.IOException; diff --git a/jackson/src/test/java/org/baeldung/jackson/sandbox/TestElement.java b/jackson/src/test/java/com/baeldung/jackson/sandbox/TestElement.java similarity index 88% rename from jackson/src/test/java/org/baeldung/jackson/sandbox/TestElement.java rename to jackson/src/test/java/com/baeldung/jackson/sandbox/TestElement.java index 3b6a852b6b..82f53fcf4a 100644 --- a/jackson/src/test/java/org/baeldung/jackson/sandbox/TestElement.java +++ b/jackson/src/test/java/com/baeldung/jackson/sandbox/TestElement.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.sandbox; +package com.baeldung.jackson.sandbox; public class TestElement { diff --git a/jackson/src/test/java/org/baeldung/jackson/serialization/ItemSerializer.java b/jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializer.java similarity index 89% rename from jackson/src/test/java/org/baeldung/jackson/serialization/ItemSerializer.java rename to jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializer.java index 7a1362a416..cb93f9cb03 100644 --- a/jackson/src/test/java/org/baeldung/jackson/serialization/ItemSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializer.java @@ -1,8 +1,8 @@ -package org.baeldung.jackson.serialization; +package com.baeldung.jackson.serialization; import java.io.IOException; -import org.baeldung.jackson.dtos.Item; +import com.baeldung.jackson.dtos.Item; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; diff --git a/jackson/src/test/java/org/baeldung/jackson/serialization/ItemSerializerOnClass.java b/jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializerOnClass.java similarity index 88% rename from jackson/src/test/java/org/baeldung/jackson/serialization/ItemSerializerOnClass.java rename to jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializerOnClass.java index 44060cabb9..79b450d7f1 100644 --- a/jackson/src/test/java/org/baeldung/jackson/serialization/ItemSerializerOnClass.java +++ b/jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializerOnClass.java @@ -1,8 +1,8 @@ -package org.baeldung.jackson.serialization; +package com.baeldung.jackson.serialization; import java.io.IOException; -import org.baeldung.jackson.dtos.ItemWithSerializer; +import com.baeldung.jackson.dtos.ItemWithSerializer; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; diff --git a/jackson/src/test/java/org/baeldung/jackson/serialization/MyDtoNullKeySerializer.java b/jackson/src/test/java/com/baeldung/jackson/serialization/MyDtoNullKeySerializer.java similarity index 89% rename from jackson/src/test/java/org/baeldung/jackson/serialization/MyDtoNullKeySerializer.java rename to jackson/src/test/java/com/baeldung/jackson/serialization/MyDtoNullKeySerializer.java index 8219abaddf..e915378498 100644 --- a/jackson/src/test/java/org/baeldung/jackson/serialization/MyDtoNullKeySerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/serialization/MyDtoNullKeySerializer.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.serialization; +package com.baeldung.jackson.serialization; import java.io.IOException; diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonAnnotationTest.java b/jackson/src/test/java/com/baeldung/jackson/test/JacksonAnnotationTest.java similarity index 90% rename from jackson/src/test/java/org/baeldung/jackson/test/JacksonAnnotationTest.java rename to jackson/src/test/java/com/baeldung/jackson/test/JacksonAnnotationTest.java index c188e9bce1..74fbd021a0 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonAnnotationTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/test/JacksonAnnotationTest.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.test; +package com.baeldung.jackson.test; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; @@ -12,31 +12,32 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; -import org.baeldung.jackson.annotation.BeanWithCreator; -import org.baeldung.jackson.annotation.BeanWithCustomAnnotation; -import org.baeldung.jackson.annotation.BeanWithFilter; -import org.baeldung.jackson.annotation.BeanWithGetter; -import org.baeldung.jackson.annotation.BeanWithIgnore; -import org.baeldung.jackson.annotation.BeanWithInject; -import org.baeldung.jackson.annotation.ExtendableBean; -import org.baeldung.jackson.annotation.MyBean; -import org.baeldung.jackson.annotation.PrivateBean; -import org.baeldung.jackson.annotation.RawBean; -import org.baeldung.jackson.annotation.UnwrappedUser; -import org.baeldung.jackson.annotation.UserWithIgnoreType; -import org.baeldung.jackson.annotation.Zoo; -import org.baeldung.jackson.bidirection.ItemWithIdentity; -import org.baeldung.jackson.bidirection.ItemWithRef; -import org.baeldung.jackson.bidirection.UserWithIdentity; -import org.baeldung.jackson.bidirection.UserWithRef; -import org.baeldung.jackson.date.EventWithFormat; -import org.baeldung.jackson.date.EventWithSerializer; -import org.baeldung.jackson.dtos.MyMixInForString; -import org.baeldung.jackson.dtos.User; -import org.baeldung.jackson.dtos.withEnum.TypeEnumWithValue; -import org.baeldung.jackson.exception.UserWithRoot; -import org.baeldung.jackson.jsonview.Item; -import org.baeldung.jackson.jsonview.Views; +import com.baeldung.jackson.bidirection.ItemWithIdentity; +import com.baeldung.jackson.bidirection.ItemWithRef; +import com.baeldung.jackson.bidirection.UserWithRef; +import com.baeldung.jackson.dtos.User; +import com.baeldung.jackson.dtos.withEnum.TypeEnumWithValue; +import com.baeldung.jackson.annotation.BeanWithCreator; +import com.baeldung.jackson.annotation.BeanWithCustomAnnotation; +import com.baeldung.jackson.annotation.BeanWithFilter; +import com.baeldung.jackson.annotation.BeanWithGetter; +import com.baeldung.jackson.annotation.BeanWithIgnore; +import com.baeldung.jackson.annotation.BeanWithInject; +import com.baeldung.jackson.annotation.ExtendableBean; +import com.baeldung.jackson.annotation.MyBean; +import com.baeldung.jackson.annotation.PrivateBean; +import com.baeldung.jackson.annotation.RawBean; +import com.baeldung.jackson.annotation.UnwrappedUser; +import com.baeldung.jackson.annotation.UserWithIgnoreType; +import com.baeldung.jackson.annotation.Zoo; +import com.baeldung.jackson.bidirection.UserWithIdentity; +import com.baeldung.jackson.date.EventWithFormat; +import com.baeldung.jackson.date.EventWithSerializer; +import com.baeldung.jackson.dtos.MyMixInForString; +import com.baeldung.jackson.exception.UserWithRoot; +import com.baeldung.jackson.jsonview.Item; +import com.baeldung.jackson.jsonview.Views; +import org.junit.Ignore; import org.junit.Test; import com.fasterxml.jackson.core.JsonParseException; @@ -337,6 +338,7 @@ public class JacksonAnnotationTest { assertThat(result, not(containsString("dateCreated"))); } + @Ignore("Jackson 2.7.1-1 seems to have changed the API regarding mixins") @Test public void whenSerializingUsingMixInAnnotation_thenCorrect() throws JsonProcessingException { final User user = new User(1, "John"); @@ -345,7 +347,7 @@ public class JacksonAnnotationTest { assertThat(result, containsString("John")); final ObjectMapper mapper = new ObjectMapper(); - mapper.addMixInAnnotations(String.class, MyMixInForString.class); + mapper.addMixIn(String.class, MyMixInForString.class); result = mapper.writeValueAsString(user); assertThat(result, not(containsString("John"))); diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonBidirectionRelationTest.java b/jackson/src/test/java/com/baeldung/jackson/test/JacksonBidirectionRelationTest.java similarity index 87% rename from jackson/src/test/java/org/baeldung/jackson/test/JacksonBidirectionRelationTest.java rename to jackson/src/test/java/com/baeldung/jackson/test/JacksonBidirectionRelationTest.java index f7f687da96..971b40406a 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonBidirectionRelationTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/test/JacksonBidirectionRelationTest.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.test; +package com.baeldung.jackson.test; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; @@ -7,19 +7,19 @@ import static org.junit.Assert.assertThat; import java.io.IOException; -import org.baeldung.jackson.bidirection.Item; -import org.baeldung.jackson.bidirection.ItemWithIdentity; -import org.baeldung.jackson.bidirection.ItemWithIgnore; -import org.baeldung.jackson.bidirection.ItemWithRef; -import org.baeldung.jackson.bidirection.ItemWithSerializer; -import org.baeldung.jackson.bidirection.ItemWithView; -import org.baeldung.jackson.bidirection.User; -import org.baeldung.jackson.bidirection.UserWithIdentity; -import org.baeldung.jackson.bidirection.UserWithIgnore; -import org.baeldung.jackson.bidirection.UserWithRef; -import org.baeldung.jackson.bidirection.UserWithSerializer; -import org.baeldung.jackson.bidirection.UserWithView; -import org.baeldung.jackson.jsonview.Views; +import com.baeldung.jackson.bidirection.Item; +import com.baeldung.jackson.bidirection.ItemWithIdentity; +import com.baeldung.jackson.bidirection.ItemWithIgnore; +import com.baeldung.jackson.bidirection.ItemWithRef; +import com.baeldung.jackson.bidirection.ItemWithSerializer; +import com.baeldung.jackson.bidirection.ItemWithView; +import com.baeldung.jackson.bidirection.User; +import com.baeldung.jackson.bidirection.UserWithIdentity; +import com.baeldung.jackson.bidirection.UserWithIgnore; +import com.baeldung.jackson.bidirection.UserWithRef; +import com.baeldung.jackson.bidirection.UserWithSerializer; +import com.baeldung.jackson.bidirection.UserWithView; +import com.baeldung.jackson.jsonview.Views; import org.junit.Test; import com.fasterxml.jackson.core.JsonProcessingException; diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonCollectionDeserializationUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/test/JacksonCollectionDeserializationUnitTest.java similarity index 97% rename from jackson/src/test/java/org/baeldung/jackson/test/JacksonCollectionDeserializationUnitTest.java rename to jackson/src/test/java/com/baeldung/jackson/test/JacksonCollectionDeserializationUnitTest.java index c3d8153546..cd166386e6 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonCollectionDeserializationUnitTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/test/JacksonCollectionDeserializationUnitTest.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.test; +package com.baeldung.jackson.test; import static org.hamcrest.Matchers.instanceOf; import static org.junit.Assert.assertThat; @@ -7,7 +7,7 @@ import java.io.IOException; import java.util.LinkedHashMap; import java.util.List; -import org.baeldung.jackson.dtos.MyDto; +import com.baeldung.jackson.dtos.MyDto; import org.junit.Test; import com.fasterxml.jackson.core.JsonParseException; diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonDateTest.java b/jackson/src/test/java/com/baeldung/jackson/test/JacksonDateTest.java similarity index 95% rename from jackson/src/test/java/org/baeldung/jackson/test/JacksonDateTest.java rename to jackson/src/test/java/com/baeldung/jackson/test/JacksonDateTest.java index fc7f7730b8..50ec50b668 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonDateTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/test/JacksonDateTest.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.test; +package com.baeldung.jackson.test; import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; @@ -11,11 +11,11 @@ import java.time.LocalDateTime; import java.util.Date; import java.util.TimeZone; -import org.baeldung.jackson.date.Event; -import org.baeldung.jackson.date.EventWithFormat; -import org.baeldung.jackson.date.EventWithJodaTime; -import org.baeldung.jackson.date.EventWithLocalDateTime; -import org.baeldung.jackson.date.EventWithSerializer; +import com.baeldung.jackson.date.EventWithLocalDateTime; +import com.baeldung.jackson.date.Event; +import com.baeldung.jackson.date.EventWithFormat; +import com.baeldung.jackson.date.EventWithJodaTime; +import com.baeldung.jackson.date.EventWithSerializer; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.junit.Test; diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonDeserializationUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/test/JacksonDeserializationUnitTest.java similarity index 96% rename from jackson/src/test/java/org/baeldung/jackson/test/JacksonDeserializationUnitTest.java rename to jackson/src/test/java/com/baeldung/jackson/test/JacksonDeserializationUnitTest.java index 2ecca664a4..45d957b90b 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonDeserializationUnitTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/test/JacksonDeserializationUnitTest.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.test; +package com.baeldung.jackson.test; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.notNullValue; @@ -7,11 +7,11 @@ import static org.junit.Assert.assertThat; import java.io.IOException; -import org.baeldung.jackson.deserialization.ItemDeserializer; -import org.baeldung.jackson.dtos.Item; -import org.baeldung.jackson.dtos.ItemWithSerializer; -import org.baeldung.jackson.dtos.MyDto; -import org.baeldung.jackson.dtos.ignore.MyDtoIgnoreUnknown; +import com.baeldung.jackson.deserialization.ItemDeserializer; +import com.baeldung.jackson.dtos.Item; +import com.baeldung.jackson.dtos.ItemWithSerializer; +import com.baeldung.jackson.dtos.MyDto; +import com.baeldung.jackson.dtos.ignore.MyDtoIgnoreUnknown; import org.junit.Test; import com.fasterxml.jackson.core.JsonFactory; diff --git a/jackson/src/test/java/com/baeldung/jackson/test/JacksonDynamicIgnoreTest.java b/jackson/src/test/java/com/baeldung/jackson/test/JacksonDynamicIgnoreTest.java new file mode 100644 index 0000000000..16a55f442a --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/test/JacksonDynamicIgnoreTest.java @@ -0,0 +1,100 @@ +package com.baeldung.jackson.test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; + +import com.baeldung.jackson.dynamicIgnore.Address; +import com.baeldung.jackson.dynamicIgnore.HidableSerializer; +import com.baeldung.jackson.dynamicIgnore.Person; +import com.baeldung.jackson.dynamicIgnore.Hidable; +import org.junit.Before; +import org.junit.Test; + +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.BeanDescription; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationConfig; +import com.fasterxml.jackson.databind.module.SimpleModule; +import com.fasterxml.jackson.databind.ser.BeanSerializerModifier; + +public class JacksonDynamicIgnoreTest { + + private ObjectMapper mapper = new ObjectMapper(); + + @Before + public void setUp() { + mapper.setSerializationInclusion(Include.NON_EMPTY); + mapper.registerModule(new SimpleModule() { + @Override + public void setupModule(final SetupContext context) { + super.setupModule(context); + context.addBeanSerializerModifier(new BeanSerializerModifier() { + @Override + public JsonSerializer modifySerializer(final SerializationConfig config, final BeanDescription beanDesc, final JsonSerializer serializer) { + if (Hidable.class.isAssignableFrom(beanDesc.getBeanClass())) { + return new HidableSerializer((JsonSerializer) serializer); + } + return serializer; + } + }); + } + }); + } + + @Test + public void whenNotHidden_thenCorrect() throws JsonProcessingException { + final Address ad = new Address("ny", "usa", false); + final Person person = new Person("john", ad, false); + final String result = mapper.writeValueAsString(person); + + assertTrue(result.contains("name")); + assertTrue(result.contains("john")); + assertTrue(result.contains("address")); + assertTrue(result.contains("usa")); + + System.out.println("Not Hidden = " + result); + } + + @Test + public void whenAddressHidden_thenCorrect() throws JsonProcessingException { + final Address ad = new Address("ny", "usa", true); + final Person person = new Person("john", ad, false); + final String result = mapper.writeValueAsString(person); + + assertTrue(result.contains("name")); + assertTrue(result.contains("john")); + assertFalse(result.contains("address")); + assertFalse(result.contains("usa")); + + System.out.println("Address Hidden = " + result); + } + + @Test + public void whenAllHidden_thenCorrect() throws JsonProcessingException { + final Address ad = new Address("ny", "usa", false); + final Person person = new Person("john", ad, true); + final String result = mapper.writeValueAsString(person); + + assertTrue(result.length() == 0); + + System.out.println("All Hidden = " + result); + } + + @Test + public void whenSerializeList_thenCorrect() throws JsonProcessingException { + final Address ad1 = new Address("tokyo", "jp", true); + final Address ad2 = new Address("london", "uk", false); + final Address ad3 = new Address("ny", "usa", false); + final Person p1 = new Person("john", ad1, false); + final Person p2 = new Person("tom", ad2, true); + final Person p3 = new Person("adam", ad3, false); + + final String result = mapper.writeValueAsString(Arrays.asList(p1, p2, p3)); + + System.out.println(result); + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonExceptionsTest.java b/jackson/src/test/java/com/baeldung/jackson/test/JacksonExceptionsTest.java similarity index 86% rename from jackson/src/test/java/org/baeldung/jackson/test/JacksonExceptionsTest.java rename to jackson/src/test/java/com/baeldung/jackson/test/JacksonExceptionsTest.java index aab7f9409d..90317848ce 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonExceptionsTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/test/JacksonExceptionsTest.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.test; +package com.baeldung.jackson.test; import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; @@ -7,11 +7,7 @@ import static org.junit.Assert.assertThat; import java.io.IOException; import java.util.List; -import org.baeldung.jackson.dtos.User; -import org.baeldung.jackson.exception.UserWithPrivateFields; -import org.baeldung.jackson.exception.UserWithRoot; -import org.baeldung.jackson.exception.Zoo; -import org.baeldung.jackson.exception.ZooConfigured; +import com.baeldung.jackson.exception.*; import org.junit.Test; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; @@ -71,7 +67,7 @@ public class JacksonExceptionsTest { final String json = "{\"id\":1,\"name\":\"John\"}"; final ObjectMapper mapper = new ObjectMapper(); - mapper.reader().withType(org.baeldung.jackson.exception.User.class).readValue(json); + mapper.reader().withType(User.class).readValue(json); } @Test @@ -79,7 +75,7 @@ public class JacksonExceptionsTest { final String json = "{\"id\":1,\"name\":\"John\"}"; final ObjectMapper mapper = new ObjectMapper(); - final User user = mapper.reader().withType(User.class).readValue(json); + final com.baeldung.jackson.dtos.User user = mapper.reader().withType(com.baeldung.jackson.dtos.User.class).readValue(json); assertEquals("John", user.name); } @@ -91,7 +87,7 @@ public class JacksonExceptionsTest { final ObjectMapper mapper = new ObjectMapper(); mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); - mapper.reader().withType(User.class).readValue(json); + mapper.reader().withType(com.baeldung.jackson.dtos.User.class).readValue(json); } @Test @@ -111,7 +107,7 @@ public class JacksonExceptionsTest { final String json = "[{\"id\":1,\"name\":\"John\"},{\"id\":2,\"name\":\"Adam\"}]"; final ObjectMapper mapper = new ObjectMapper(); - mapper.reader().withType(User.class).readValue(json); + mapper.reader().withType(com.baeldung.jackson.dtos.User.class).readValue(json); } @Test @@ -119,7 +115,7 @@ public class JacksonExceptionsTest { final String json = "[{\"id\":1,\"name\":\"John\"},{\"id\":2,\"name\":\"Adam\"}]"; final ObjectMapper mapper = new ObjectMapper(); - final List users = mapper.reader().withType(new TypeReference>() { + final List users = mapper.reader().withType(new TypeReference>() { }).readValue(json); assertEquals(2, users.size()); @@ -131,7 +127,7 @@ public class JacksonExceptionsTest { final String json = "{\"id\":1,\"name\":\"John\", \"checked\":true}"; final ObjectMapper mapper = new ObjectMapper(); - mapper.reader().withType(User.class).readValue(json); + mapper.reader().withType(com.baeldung.jackson.dtos.User.class).readValue(json); } @Test @@ -141,7 +137,7 @@ public class JacksonExceptionsTest { final ObjectMapper mapper = new ObjectMapper(); mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); - final User user = mapper.reader().withType(User.class).readValue(json); + final com.baeldung.jackson.dtos.User user = mapper.reader().withType(com.baeldung.jackson.dtos.User.class).readValue(json); assertEquals("John", user.name); } @@ -151,7 +147,7 @@ public class JacksonExceptionsTest { final String json = "{'id':1,'name':'John'}"; final ObjectMapper mapper = new ObjectMapper(); - mapper.reader().withType(User.class).readValue(json); + mapper.reader().withType(com.baeldung.jackson.dtos.User.class).readValue(json); } @Test @@ -162,7 +158,7 @@ public class JacksonExceptionsTest { factory.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES); final ObjectMapper mapper = new ObjectMapper(factory); - final User user = mapper.reader().withType(User.class).readValue(json); + final com.baeldung.jackson.dtos.User user = mapper.reader().withType(com.baeldung.jackson.dtos.User.class).readValue(json); assertEquals("John", user.name); } diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonFieldUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/test/JacksonFieldUnitTest.java similarity index 95% rename from jackson/src/test/java/org/baeldung/jackson/test/JacksonFieldUnitTest.java rename to jackson/src/test/java/com/baeldung/jackson/test/JacksonFieldUnitTest.java index 53933215f5..ccc5905e88 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonFieldUnitTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/test/JacksonFieldUnitTest.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.test; +package com.baeldung.jackson.test; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -8,9 +8,9 @@ import static org.junit.Assert.assertThat; import java.io.IOException; -import org.baeldung.jackson.field.MyDtoAccessLevel; -import org.baeldung.jackson.field.MyDtoWithSetter; -import org.baeldung.jackson.field.MyDtoWithGetter; +import com.baeldung.jackson.field.MyDtoAccessLevel; +import com.baeldung.jackson.field.MyDtoWithSetter; +import com.baeldung.jackson.field.MyDtoWithGetter; import org.junit.Test; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonJsonViewTest.java b/jackson/src/test/java/com/baeldung/jackson/test/JacksonJsonViewTest.java similarity index 92% rename from jackson/src/test/java/org/baeldung/jackson/test/JacksonJsonViewTest.java rename to jackson/src/test/java/com/baeldung/jackson/test/JacksonJsonViewTest.java index 276db9261c..61fa2919ac 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonJsonViewTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/test/JacksonJsonViewTest.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.test; +package com.baeldung.jackson.test; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; @@ -7,10 +7,10 @@ import static org.junit.Assert.assertThat; import java.io.IOException; -import org.baeldung.jackson.jsonview.Item; -import org.baeldung.jackson.jsonview.MyBeanSerializerModifier; -import org.baeldung.jackson.jsonview.User; -import org.baeldung.jackson.jsonview.Views; +import com.baeldung.jackson.jsonview.Item; +import com.baeldung.jackson.jsonview.User; +import com.baeldung.jackson.jsonview.MyBeanSerializerModifier; +import com.baeldung.jackson.jsonview.Views; import org.junit.Test; import com.fasterxml.jackson.core.JsonProcessingException; diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationEnumsUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/test/JacksonSerializationEnumsUnitTest.java similarity index 87% rename from jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationEnumsUnitTest.java rename to jackson/src/test/java/com/baeldung/jackson/test/JacksonSerializationEnumsUnitTest.java index d64834078a..78c6316aa6 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationEnumsUnitTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/test/JacksonSerializationEnumsUnitTest.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.test; +package com.baeldung.jackson.test; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; @@ -6,12 +6,12 @@ import static org.junit.Assert.assertThat; import java.io.IOException; -import org.baeldung.jackson.dtos.withEnum.MyDtoWithEnum; -import org.baeldung.jackson.dtos.withEnum.MyDtoWithEnumCustom; -import org.baeldung.jackson.dtos.withEnum.TypeEnum; -import org.baeldung.jackson.dtos.withEnum.TypeEnumSimple; -import org.baeldung.jackson.dtos.withEnum.TypeEnumWithCustomSerializer; -import org.baeldung.jackson.dtos.withEnum.TypeEnumWithValue; +import com.baeldung.jackson.dtos.withEnum.MyDtoWithEnum; +import com.baeldung.jackson.dtos.withEnum.TypeEnum; +import com.baeldung.jackson.dtos.withEnum.TypeEnumSimple; +import com.baeldung.jackson.dtos.withEnum.TypeEnumWithValue; +import com.baeldung.jackson.dtos.withEnum.MyDtoWithEnumCustom; +import com.baeldung.jackson.dtos.withEnum.TypeEnumWithCustomSerializer; import org.junit.Test; import com.fasterxml.jackson.core.JsonParseException; diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationIgnoreUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/test/JacksonSerializationIgnoreUnitTest.java similarity index 94% rename from jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationIgnoreUnitTest.java rename to jackson/src/test/java/com/baeldung/jackson/test/JacksonSerializationIgnoreUnitTest.java index 64614fb854..4c01ec9333 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationIgnoreUnitTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/test/JacksonSerializationIgnoreUnitTest.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.test; +package com.baeldung.jackson.test; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; @@ -8,14 +8,15 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; -import org.baeldung.jackson.dtos.MyDto; -import org.baeldung.jackson.dtos.MyDtoIncludeNonDefault; -import org.baeldung.jackson.dtos.MyDtoWithFilter; -import org.baeldung.jackson.dtos.MyMixInForString; -import org.baeldung.jackson.dtos.ignore.MyDtoIgnoreField; -import org.baeldung.jackson.dtos.ignore.MyDtoIgnoreFieldByName; -import org.baeldung.jackson.dtos.ignore.MyDtoIgnoreNull; -import org.baeldung.jackson.serialization.MyDtoNullKeySerializer; +import com.baeldung.jackson.dtos.MyDtoIncludeNonDefault; +import com.baeldung.jackson.dtos.MyDtoWithFilter; +import com.baeldung.jackson.dtos.ignore.MyDtoIgnoreNull; +import com.baeldung.jackson.dtos.MyDto; +import com.baeldung.jackson.dtos.MyMixInForString; +import com.baeldung.jackson.dtos.ignore.MyDtoIgnoreField; +import com.baeldung.jackson.dtos.ignore.MyDtoIgnoreFieldByName; +import com.baeldung.jackson.serialization.MyDtoNullKeySerializer; +import org.junit.Ignore; import org.junit.Test; import com.fasterxml.jackson.annotation.JsonInclude.Include; @@ -84,10 +85,11 @@ public class JacksonSerializationIgnoreUnitTest { System.out.println(dtoAsString); } + @Ignore("Jackson 2.7.1-1 seems to have changed the API for this case") @Test public final void givenFieldTypeIsIgnored_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException { final ObjectMapper mapper = new ObjectMapper(); - mapper.addMixInAnnotations(String.class, MyMixInForString.class); + mapper.addMixIn(String.class, MyMixInForString.class); final MyDto dtoObject = new MyDto(); dtoObject.setBooleanValue(true); diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/test/JacksonSerializationUnitTest.java similarity index 92% rename from jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationUnitTest.java rename to jackson/src/test/java/com/baeldung/jackson/test/JacksonSerializationUnitTest.java index 4a34f168c7..563dda9eb6 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationUnitTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/test/JacksonSerializationUnitTest.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.test; +package com.baeldung.jackson.test; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; @@ -8,14 +8,14 @@ import static org.junit.Assert.assertThat; import java.io.IOException; import java.util.List; -import org.baeldung.jackson.dtos.Item; -import org.baeldung.jackson.dtos.ItemWithSerializer; -import org.baeldung.jackson.dtos.MyDto; -import org.baeldung.jackson.dtos.MyDtoFieldNameChanged; -import org.baeldung.jackson.dtos.MyDtoNoAccessors; -import org.baeldung.jackson.dtos.MyDtoNoAccessorsAndFieldVisibility; -import org.baeldung.jackson.dtos.User; -import org.baeldung.jackson.serialization.ItemSerializer; +import com.baeldung.jackson.dtos.MyDtoFieldNameChanged; +import com.baeldung.jackson.dtos.User; +import com.baeldung.jackson.dtos.Item; +import com.baeldung.jackson.dtos.ItemWithSerializer; +import com.baeldung.jackson.dtos.MyDto; +import com.baeldung.jackson.dtos.MyDtoNoAccessors; +import com.baeldung.jackson.dtos.MyDtoNoAccessorsAndFieldVisibility; +import com.baeldung.jackson.serialization.ItemSerializer; import org.junit.Test; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; diff --git a/jackson/src/test/java/org/baeldung/jackson/test/UnitTestSuite.java b/jackson/src/test/java/com/baeldung/jackson/test/UnitTestSuite.java similarity index 78% rename from jackson/src/test/java/org/baeldung/jackson/test/UnitTestSuite.java rename to jackson/src/test/java/com/baeldung/jackson/test/UnitTestSuite.java index 3bc5715e2a..d47b6e217a 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/UnitTestSuite.java +++ b/jackson/src/test/java/com/baeldung/jackson/test/UnitTestSuite.java @@ -1,7 +1,7 @@ -package org.baeldung.jackson.test; +package com.baeldung.jackson.test; -import org.baeldung.jackson.sandbox.JacksonPrettyPrintUnitTest; -import org.baeldung.jackson.sandbox.SandboxTest; +import com.baeldung.jackson.sandbox.JacksonPrettyPrintUnitTest; +import com.baeldung.jackson.sandbox.SandboxTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; diff --git a/jackson/src/test/java/org/baeldung/jackson/try1/IEntity.java b/jackson/src/test/java/com/baeldung/jackson/try1/IEntity.java similarity index 59% rename from jackson/src/test/java/org/baeldung/jackson/try1/IEntity.java rename to jackson/src/test/java/com/baeldung/jackson/try1/IEntity.java index ce4c83729a..27e0a71c9d 100644 --- a/jackson/src/test/java/org/baeldung/jackson/try1/IEntity.java +++ b/jackson/src/test/java/com/baeldung/jackson/try1/IEntity.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.try1; +package com.baeldung.jackson.try1; public interface IEntity { public int getId(); diff --git a/jackson/src/test/java/org/baeldung/jackson/try1/JacksonDeserializationUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/try1/JacksonDeserializationUnitTest.java similarity index 89% rename from jackson/src/test/java/org/baeldung/jackson/try1/JacksonDeserializationUnitTest.java rename to jackson/src/test/java/com/baeldung/jackson/try1/JacksonDeserializationUnitTest.java index 673577cf6f..9fc195a8aa 100644 --- a/jackson/src/test/java/org/baeldung/jackson/try1/JacksonDeserializationUnitTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/try1/JacksonDeserializationUnitTest.java @@ -1,11 +1,11 @@ -package org.baeldung.jackson.try1; +package com.baeldung.jackson.try1; import static org.hamcrest.Matchers.notNullValue; import static org.junit.Assert.assertThat; import java.io.IOException; -import org.baeldung.jackson.dtos.ItemWithSerializer; +import com.baeldung.jackson.dtos.ItemWithSerializer; import org.junit.Test; import com.fasterxml.jackson.core.JsonParseException; diff --git a/jackson/src/test/java/org/baeldung/jackson/try1/RestLoaderRequest.java b/jackson/src/test/java/com/baeldung/jackson/try1/RestLoaderRequest.java similarity index 96% rename from jackson/src/test/java/org/baeldung/jackson/try1/RestLoaderRequest.java rename to jackson/src/test/java/com/baeldung/jackson/try1/RestLoaderRequest.java index 171f4c1e14..7ef8864a63 100644 --- a/jackson/src/test/java/org/baeldung/jackson/try1/RestLoaderRequest.java +++ b/jackson/src/test/java/com/baeldung/jackson/try1/RestLoaderRequest.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.try1; +package com.baeldung.jackson.try1; import java.io.Serializable; diff --git a/jackson/src/test/java/org/baeldung/jackson/try1/RestLoaderRequestDeserializer.java b/jackson/src/test/java/com/baeldung/jackson/try1/RestLoaderRequestDeserializer.java similarity index 97% rename from jackson/src/test/java/org/baeldung/jackson/try1/RestLoaderRequestDeserializer.java rename to jackson/src/test/java/com/baeldung/jackson/try1/RestLoaderRequestDeserializer.java index 6110e8b0e0..849607586d 100644 --- a/jackson/src/test/java/org/baeldung/jackson/try1/RestLoaderRequestDeserializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/try1/RestLoaderRequestDeserializer.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.try1; +package com.baeldung.jackson.try1; import java.io.IOException; diff --git a/jackson/src/test/java/com/baeldung/jackson/xml/TestXMLSerializeDeserialize.java b/jackson/src/test/java/com/baeldung/jackson/xml/TestXMLSerializeDeserialize.java new file mode 100644 index 0000000000..92d0bd13d4 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/xml/TestXMLSerializeDeserialize.java @@ -0,0 +1,86 @@ +package com.baeldung.jackson.xml; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import org.junit.Test; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; + +public class TestXMLSerializeDeserialize { + + @Test + public void whenJavaSerializedToXmlStr_thenCorrect() throws JsonProcessingException { + XmlMapper xmlMapper = new XmlMapper(); + String xml = xmlMapper.writeValueAsString(new SimpleBean()); + assertNotNull(xml); + } + + @Test + public void whenJavaSerializedToXmlFile_thenCorrect() throws IOException { + XmlMapper xmlMapper = new XmlMapper(); + xmlMapper.writeValue(new File("target/simple_bean.xml"), new SimpleBean()); + File file = new File("target/simple_bean.xml"); + assertNotNull(file); + } + + @Test + public void whenJavaGotFromXmlStr_thenCorrect() throws IOException { + XmlMapper xmlMapper = new XmlMapper(); + SimpleBean value = xmlMapper.readValue( + "12", SimpleBean.class); + assertTrue(value.getX() == 1 && value.getY() == 2); + } + + @Test + public void whenJavaGotFromXmlFile_thenCorrect() throws IOException { + File file = new File("src/test/resources/simple_bean.xml"); + XmlMapper xmlMapper = new XmlMapper(); + String xml = inputStreamToString(new FileInputStream(file)); + SimpleBean value = xmlMapper.readValue(xml, SimpleBean.class); + assertTrue(value.getX() == 1 && value.getY() == 2); + } + + private static String inputStreamToString(InputStream is) throws IOException { + BufferedReader br; + StringBuilder sb = new StringBuilder(); + + String line; + br = new BufferedReader(new InputStreamReader(is)); + while ((line = br.readLine()) != null) { + sb.append(line); + } + br.close(); + return sb.toString(); + } +} + +class SimpleBean { + private int x = 1; + private int y = 2; + + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; + } + +} diff --git a/jackson/src/test/resources/simple_bean.xml b/jackson/src/test/resources/simple_bean.xml new file mode 100644 index 0000000000..7829ea35a4 --- /dev/null +++ b/jackson/src/test/resources/simple_bean.xml @@ -0,0 +1,4 @@ + + 1 + 2 + \ No newline at end of file diff --git a/javaxval/bin/pom.xml b/javaxval/bin/pom.xml index 592f3c3431..7606f0d072 100644 --- a/javaxval/bin/pom.xml +++ b/javaxval/bin/pom.xml @@ -1,7 +1,7 @@ 4.0.0 - org.baeldung + com.baeldung javaxval 0.1-SNAPSHOT diff --git a/javaxval/pom.xml b/javaxval/pom.xml index 592f3c3431..493119d92e 100644 --- a/javaxval/pom.xml +++ b/javaxval/pom.xml @@ -1,51 +1,47 @@ - - 4.0.0 - org.baeldung - javaxval - 0.1-SNAPSHOT + + 4.0.0 + com.baeldung + javaxval + 0.1-SNAPSHOT + - + + junit + junit + 4.12 + + + javax.validation + validation-api + 1.1.0.Final + - - junit - junit - 4.12 - + + org.hibernate + hibernate-validator + 5.2.1.Final + - - javax.validation - validation-api - 1.1.0.Final - + + org.hibernate + hibernate-validator-annotation-processor + 5.2.1.Final + - - org.hibernate - hibernate-validator - 5.2.1.Final - + + javax.el + javax.el-api + 2.2.4 + - - org.hibernate - hibernate-validator-annotation-processor - 5.2.1.Final - - - - javax.el - javax.el-api - 2.2.4 - - - - org.glassfish.web - javax.el - 2.2.4 - - - + + org.glassfish.web + javax.el + 2.2.4 + + \ No newline at end of file diff --git a/jee7schedule/pom.xml b/jee7schedule/pom.xml new file mode 100644 index 0000000000..627b8723e3 --- /dev/null +++ b/jee7schedule/pom.xml @@ -0,0 +1,311 @@ + + + 4.0.0 + + com.baeldung + jee7schedule + 1.0-SNAPSHOT + JavaEE 7 Arquillian Archetype Sample + war + + + 1.7 + 3.0.0 + + 4.11 + 7.0 + + 1.1.4.Final + + 8.0.0.Final + + + + ${maven.min.version} + + + + + + org.jboss.arquillian + arquillian-bom + ${version.arquillian_core} + import + pom + + + + + + + javax + javaee-api + ${version.javaee_api} + provided + + + + junit + junit + ${version.junit} + test + + + org.jboss.arquillian.junit + arquillian-junit-container + test + + + com.jayway.awaitility + awaitility + 1.6.0 + test + + + + org.jboss.shrinkwrap.resolver + shrinkwrap-resolver-impl-maven + test + jar + + + + org.jboss.shrinkwrap.resolver + shrinkwrap-resolver-impl-maven-archive + test + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + ${java.min.version} + ${java.min.version} + + + + org.apache.maven.plugins + maven-war-plugin + 2.1.1 + + false + + + + + + + + + wildfly-managed-arquillian + + true + + + standalone-full.xml + ${project.build.directory}/wildfly-${version.wildfly} + + + + io.undertow + undertow-websockets-jsr + 1.0.0.Beta25 + test + + + org.jboss.resteasy + resteasy-client + 3.0.5.Final + test + + + org.jboss.resteasy + resteasy-jaxb-provider + 3.0.5.Final + test + + + org.jboss.resteasy + resteasy-json-p-provider + 3.0.5.Final + test + + + org.wildfly + wildfly-arquillian-container-managed + ${version.wildfly} + test + + + + + + maven-dependency-plugin + 2.8 + + ${maven.test.skip} + + + + unpack + process-test-classes + + unpack + + + + + org.wildfly + wildfly-dist + ${version.wildfly} + zip + false + ${project.build.directory} + + + + + + + + maven-surefire-plugin + 2.17 + + + ${project.build.directory}/wildfly-${version.wildfly} + + + + + + + + wildfly-remote-arquillian + + + io.undertow + undertow-websockets-jsr + 1.0.0.Beta25 + test + + + org.jboss.resteasy + resteasy-client + 3.0.5.Final + test + + + org.jboss.resteasy + resteasy-jaxb-provider + 3.0.5.Final + test + + + org.jboss.resteasy + resteasy-json-p-provider + 3.0.5.Final + test + + + org.wildfly + wildfly-arquillian-container-remote + ${version.wildfly} + test + + + + + glassfish-embedded-arquillian + + + org.glassfish.main.extras + glassfish-embedded-all + 4.0 + test + + + org.glassfish + javax.json + 1.0.4 + test + + + org.glassfish.tyrus + tyrus-client + 1.3 + test + + + org.glassfish.tyrus + tyrus-container-grizzly-client + 1.3 + test + + + org.glassfish.jersey.core + jersey-client + 2.4 + test + + + org.jboss.arquillian.container + arquillian-glassfish-embedded-3.1 + 1.0.0.CR4 + test + + + + + glassfish-remote-arquillian + + + org.glassfish + javax.json + 1.0.4 + test + + + org.glassfish.tyrus + tyrus-client + 1.3 + test + + + org.glassfish.tyrus + tyrus-container-grizzly-client + 1.3 + test + + + org.glassfish.jersey.core + jersey-client + 2.4 + test + + + org.glassfish.jersey.media + jersey-media-json-jackson + 2.4 + test + + + org.glassfish.jersey.media + jersey-media-json-processing + 2.4 + test + + + org.jboss.arquillian.container + arquillian-glassfish-remote-3.1 + 1.0.0.CR4 + test + + + + + diff --git a/jee7schedule/src/main/java/com/baeldung/timer/AutomaticTimerBean.java b/jee7schedule/src/main/java/com/baeldung/timer/AutomaticTimerBean.java new file mode 100644 index 0000000000..373d962f02 --- /dev/null +++ b/jee7schedule/src/main/java/com/baeldung/timer/AutomaticTimerBean.java @@ -0,0 +1,27 @@ +package com.baeldung.timer; + +import javax.ejb.Schedule; +import javax.ejb.Singleton; +import javax.ejb.Startup; +import javax.enterprise.event.Event; +import javax.inject.Inject; +import java.util.Date; + + +@Startup +@Singleton +public class AutomaticTimerBean { + + + @Inject + Event event; + + /** + * This method will be called every 10 second and will fire an @TimerEvent + */ + @Schedule(hour = "*", minute = "*", second = "*/10", info = "Every 10 second timer") + public void printDate() { + event.fire(new TimerEvent("TimerEvent sent at :" + new Date())); + } + +} diff --git a/jee7schedule/src/main/java/com/baeldung/timer/FixedDelayTimerBean.java b/jee7schedule/src/main/java/com/baeldung/timer/FixedDelayTimerBean.java new file mode 100644 index 0000000000..a466682dea --- /dev/null +++ b/jee7schedule/src/main/java/com/baeldung/timer/FixedDelayTimerBean.java @@ -0,0 +1,20 @@ +package com.baeldung.timer; + +import javax.ejb.*; + +/** + * Created by ccristianchiovari on 5/2/16. + */ +@Singleton +public class FixedDelayTimerBean { + + @EJB + private WorkerBean workerBean; + + @Lock(LockType.READ) + @Schedule(second = "*/5", minute = "*", hour = "*", persistent = false) + public void atSchedule() throws InterruptedException { + workerBean.doTimerWork(); + } + +} \ No newline at end of file diff --git a/jee7schedule/src/main/java/com/baeldung/timer/ProgrammaticAtFixedRateTimerBean.java b/jee7schedule/src/main/java/com/baeldung/timer/ProgrammaticAtFixedRateTimerBean.java new file mode 100644 index 0000000000..c1c210c8ac --- /dev/null +++ b/jee7schedule/src/main/java/com/baeldung/timer/ProgrammaticAtFixedRateTimerBean.java @@ -0,0 +1,31 @@ +package com.baeldung.timer; + +import javax.annotation.PostConstruct; +import javax.annotation.Resource; +import javax.ejb.*; +import javax.enterprise.event.Event; +import javax.inject.Inject; + +/** + * author: Cristian Chiovari + */ +@Startup +@Singleton +public class ProgrammaticAtFixedRateTimerBean { + + @Inject + Event event; + + @Resource + TimerService timerService; + + @PostConstruct + public void initialize() { + timerService.createTimer(0,1000, "Every second timer"); + } + + @Timeout + public void programmaticTimout(Timer timer) { + event.fire(new TimerEvent(timer.getInfo().toString())); + } +} diff --git a/jee7schedule/src/main/java/com/baeldung/timer/ProgrammaticTimerBean.java b/jee7schedule/src/main/java/com/baeldung/timer/ProgrammaticTimerBean.java new file mode 100644 index 0000000000..d2dba1239f --- /dev/null +++ b/jee7schedule/src/main/java/com/baeldung/timer/ProgrammaticTimerBean.java @@ -0,0 +1,39 @@ +package com.baeldung.timer; + +import javax.annotation.PostConstruct; +import javax.annotation.Resource; +import javax.ejb.*; +import javax.enterprise.event.Event; +import javax.inject.Inject; + +/** + * author: Jacek Jackowiak + */ +@Startup +@Singleton +public class ProgrammaticTimerBean { + + @Inject + Event event; + + @Resource + TimerService timerService; + + @PostConstruct + public void initialize() { + ScheduleExpression scheduleExpression = new ScheduleExpression() + .hour("*") + .minute("*") + .second("*/5"); + + TimerConfig timerConfig = new TimerConfig(); + timerConfig.setInfo("Every 5 second timer"); + + timerService.createCalendarTimer(scheduleExpression, timerConfig); + } + + @Timeout + public void programmaticTimout(Timer timer) { + event.fire(new TimerEvent(timer.getInfo().toString())); + } +} diff --git a/jee7schedule/src/main/java/com/baeldung/timer/ProgrammaticWithInitialFixedDelayTimerBean.java b/jee7schedule/src/main/java/com/baeldung/timer/ProgrammaticWithInitialFixedDelayTimerBean.java new file mode 100644 index 0000000000..9a1ebcdc57 --- /dev/null +++ b/jee7schedule/src/main/java/com/baeldung/timer/ProgrammaticWithInitialFixedDelayTimerBean.java @@ -0,0 +1,31 @@ +package com.baeldung.timer; + +import javax.annotation.PostConstruct; +import javax.annotation.Resource; +import javax.ejb.*; +import javax.enterprise.event.Event; +import javax.inject.Inject; + +/** + * author: Cristian Chiovari + */ +@Startup +@Singleton +public class ProgrammaticWithInitialFixedDelayTimerBean { + + @Inject + Event event; + + @Resource + TimerService timerService; + + @PostConstruct + public void initialize() { + timerService.createTimer(10000l,5000l, "Delay 10 seconds then every 5 second timer"); + } + + @Timeout + public void programmaticTimout(Timer timer) { + event.fire(new TimerEvent(timer.getInfo().toString())); + } +} diff --git a/jee7schedule/src/main/java/com/baeldung/timer/ScheduleTimerBean.java b/jee7schedule/src/main/java/com/baeldung/timer/ScheduleTimerBean.java new file mode 100644 index 0000000000..09fb95c889 --- /dev/null +++ b/jee7schedule/src/main/java/com/baeldung/timer/ScheduleTimerBean.java @@ -0,0 +1,27 @@ +package com.baeldung.timer; + +import javax.ejb.Schedule; +import javax.ejb.Singleton; +import javax.ejb.Startup; +import javax.ejb.Timer; +import javax.enterprise.event.Event; +import javax.inject.Inject; + + +@Startup +@Singleton +public class ScheduleTimerBean { + + @Inject + Event event; + + @Schedule(hour = "*", minute = "*", second = "*/5", info = "Every 5 second timer") + public void automaticallyScheduled(Timer timer) { + fireEvent(timer); + } + + + private void fireEvent(Timer timer) { + event.fire(new TimerEvent(timer.getInfo().toString())); + } +} diff --git a/jee7schedule/src/main/java/com/baeldung/timer/TimerEvent.java b/jee7schedule/src/main/java/com/baeldung/timer/TimerEvent.java new file mode 100644 index 0000000000..e430cfc1b1 --- /dev/null +++ b/jee7schedule/src/main/java/com/baeldung/timer/TimerEvent.java @@ -0,0 +1,27 @@ +package com.baeldung.timer; + +public class TimerEvent { + + private String eventInfo; + private long time = System.currentTimeMillis(); + + public TimerEvent(String s) { + this.eventInfo = s; + } + + public long getTime() { + return time; + } + + public String getEventInfo() { + return eventInfo; + } + + @Override + public String toString() { + return "TimerEvent {" + + "eventInfo='" + eventInfo + '\'' + + ", time=" + time + + '}'; + } +} diff --git a/jee7schedule/src/main/java/com/baeldung/timer/TimerEventListener.java b/jee7schedule/src/main/java/com/baeldung/timer/TimerEventListener.java new file mode 100644 index 0000000000..c89677213a --- /dev/null +++ b/jee7schedule/src/main/java/com/baeldung/timer/TimerEventListener.java @@ -0,0 +1,26 @@ +package com.baeldung.timer; + +import javax.ejb.Singleton; +import javax.ejb.Startup; +import javax.enterprise.event.Observes; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +/** + * This class will listen to all TimerEvent and will collect them + */ +@Startup +@Singleton +public class TimerEventListener { + + final List events = new CopyOnWriteArrayList<>(); + + public void listen(@Observes TimerEvent event) { + System.out.println("event = " + event); + events.add(event); + } + + public List getEvents() { + return events; + } +} diff --git a/jee7schedule/src/main/java/com/baeldung/timer/WorkerBean.java b/jee7schedule/src/main/java/com/baeldung/timer/WorkerBean.java new file mode 100644 index 0000000000..c1f781e95e --- /dev/null +++ b/jee7schedule/src/main/java/com/baeldung/timer/WorkerBean.java @@ -0,0 +1,33 @@ +package com.baeldung.timer; + +import javax.ejb.Lock; +import javax.ejb.LockType; +import javax.ejb.Singleton; +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * Created by cristianchiovari on 5/2/16. + */ +@Singleton +public class WorkerBean { + + private AtomicBoolean busy = new AtomicBoolean(false); + + @Lock(LockType.READ) + public void doTimerWork() throws InterruptedException { + + System.out.println("Timer method called but not started yet !"); + + if (!busy.compareAndSet(false, true)) { + return; + } + + try { + System.out.println("Timer work started"); + Thread.sleep(12000); + System.out.println("Timer work done"); + } finally { + busy.set(false); + } + } +} \ No newline at end of file diff --git a/core-java/src/main/resources/fileToMove.txt b/jee7schedule/src/main/webapp/WEB-INF/beans.xml similarity index 100% rename from core-java/src/main/resources/fileToMove.txt rename to jee7schedule/src/main/webapp/WEB-INF/beans.xml diff --git a/jee7schedule/src/test/java/com/baeldung/timer/AutomaticTimerBeanTest.java b/jee7schedule/src/test/java/com/baeldung/timer/AutomaticTimerBeanTest.java new file mode 100644 index 0000000000..df922b28df --- /dev/null +++ b/jee7schedule/src/test/java/com/baeldung/timer/AutomaticTimerBeanTest.java @@ -0,0 +1,66 @@ +package com.baeldung.timer; + +import com.jayway.awaitility.Awaitility; +import org.hamcrest.Matchers; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.shrinkwrap.resolver.api.maven.Maven; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.inject.Inject; +import java.io.File; +import java.util.concurrent.TimeUnit; + +import static com.jayway.awaitility.Awaitility.await; +import static com.jayway.awaitility.Awaitility.to; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + + +@RunWith(Arquillian.class) +public class AutomaticTimerBeanTest { + + //the @AutomaticTimerBean has a method called every 10 seconds + //testing the difference ==> 100000 + final static long TIMEOUT = 10000l; + + //the tolerance accepted , so if between two consecutive calls there has to be at least 9 or max 11 seconds. + //because the timer service is not intended for real-time applications so it will not be exactly 10 seconds + final static long TOLERANCE = 1000l; + + @Inject + TimerEventListener timerEventListener; + + @Deployment + public static WebArchive deploy() { + File[] jars = Maven.resolver().loadPomFromFile("pom.xml") + .resolve("com.jayway.awaitility:awaitility") + .withTransitivity().asFile(); + + //only @AutomaticTimerBean is deployed not the other timers + return ShrinkWrap.create(WebArchive.class) + .addAsLibraries(jars) + .addClasses(WithinWindowMatcher.class, TimerEvent.class, TimerEventListener.class, AutomaticTimerBean.class); + } + + + + @Test + public void should_receive_two_pings() { + Awaitility.setDefaultTimeout(30, TimeUnit.SECONDS); + //the test will wait here until two events are triggered + await().untilCall(to(timerEventListener.getEvents()).size(), equalTo(2)); + + TimerEvent firstEvent = timerEventListener.getEvents().get(0); + TimerEvent secondEvent = timerEventListener.getEvents().get(1); + + long delay = secondEvent.getTime() - firstEvent.getTime(); + System.out.println("Actual timeout = " + delay); + + //ensure that the delay between the events is more or less 10 seconds (no real time precision) + assertThat(delay, Matchers.is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE))); + } +} diff --git a/jee7schedule/src/test/java/com/baeldung/timer/ProgrammaticAtFixedRateTimerBeanTest.java b/jee7schedule/src/test/java/com/baeldung/timer/ProgrammaticAtFixedRateTimerBeanTest.java new file mode 100644 index 0000000000..76c0e3e5b8 --- /dev/null +++ b/jee7schedule/src/test/java/com/baeldung/timer/ProgrammaticAtFixedRateTimerBeanTest.java @@ -0,0 +1,56 @@ +package com.baeldung.timer; + +import com.jayway.awaitility.Awaitility; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.shrinkwrap.resolver.api.maven.Maven; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.inject.Inject; +import java.io.File; +import java.util.concurrent.TimeUnit; + +import static com.jayway.awaitility.Awaitility.await; +import static com.jayway.awaitility.Awaitility.to; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + + +@RunWith(Arquillian.class) +public class ProgrammaticAtFixedRateTimerBeanTest { + + final static long TIMEOUT = 1000; + final static long TOLERANCE = 500l; + + @Inject + TimerEventListener timerEventListener; + + @Deployment + public static WebArchive deploy() { + File[] jars = Maven.resolver().loadPomFromFile("pom.xml") + .resolve("com.jayway.awaitility:awaitility") + .withTransitivity().asFile(); + + return ShrinkWrap.create(WebArchive.class) + .addAsLibraries(jars) + .addClasses(WithinWindowMatcher.class, TimerEvent.class, TimerEventListener.class, ProgrammaticAtFixedRateTimerBean.class); + } + + @Test + public void should_receive_ten_pings() { + + Awaitility.setDefaultTimeout(30, TimeUnit.SECONDS); + + await().untilCall(to(timerEventListener.getEvents()).size(), equalTo(10)); + TimerEvent firstEvent = timerEventListener.getEvents().get(0); + TimerEvent secondEvent = timerEventListener.getEvents().get(1); + + long delay = secondEvent.getTime() - firstEvent.getTime(); + System.out.println("Actual timeout = " + delay); + assertThat(delay, is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE))); + } +} \ No newline at end of file diff --git a/jee7schedule/src/test/java/com/baeldung/timer/ProgrammaticTimerBeanTest.java b/jee7schedule/src/test/java/com/baeldung/timer/ProgrammaticTimerBeanTest.java new file mode 100644 index 0000000000..f93ba61fe3 --- /dev/null +++ b/jee7schedule/src/test/java/com/baeldung/timer/ProgrammaticTimerBeanTest.java @@ -0,0 +1,53 @@ +package com.baeldung.timer; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.shrinkwrap.resolver.api.maven.Maven; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.inject.Inject; +import java.io.File; + +import static com.jayway.awaitility.Awaitility.await; +import static com.jayway.awaitility.Awaitility.to; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + + +@RunWith(Arquillian.class) +public class ProgrammaticTimerBeanTest { + + final static long TIMEOUT = 5000l; + final static long TOLERANCE = 1000l; + + @Inject + TimerEventListener timerEventListener; + + @Deployment + public static WebArchive deploy() { + File[] jars = Maven.resolver().loadPomFromFile("pom.xml") + .resolve("com.jayway.awaitility:awaitility") + .withTransitivity().asFile(); + + return ShrinkWrap.create(WebArchive.class) + .addAsLibraries(jars) + .addClasses(WithinWindowMatcher.class, TimerEvent.class, TimerEventListener.class, ProgrammaticTimerBean.class); + } + + @Test + public void should_receive_two_pings() { + + await().untilCall(to(timerEventListener.getEvents()).size(), equalTo(2)); + + TimerEvent firstEvent = timerEventListener.getEvents().get(0); + TimerEvent secondEvent = timerEventListener.getEvents().get(1); + + long delay = secondEvent.getTime() - firstEvent.getTime(); + System.out.println("Actual timeout = " + delay); + assertThat(delay, is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE))); + } +} \ No newline at end of file diff --git a/jee7schedule/src/test/java/com/baeldung/timer/ProgrammaticWithFixedDelayTimerBeanTest.java b/jee7schedule/src/test/java/com/baeldung/timer/ProgrammaticWithFixedDelayTimerBeanTest.java new file mode 100644 index 0000000000..6764aa386d --- /dev/null +++ b/jee7schedule/src/test/java/com/baeldung/timer/ProgrammaticWithFixedDelayTimerBeanTest.java @@ -0,0 +1,61 @@ +package com.baeldung.timer; + +import com.jayway.awaitility.Awaitility; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.shrinkwrap.resolver.api.maven.Maven; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.inject.Inject; +import java.io.File; +import java.util.concurrent.TimeUnit; + +import static com.jayway.awaitility.Awaitility.await; +import static com.jayway.awaitility.Awaitility.to; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + + +@RunWith(Arquillian.class) +public class ProgrammaticWithFixedDelayTimerBeanTest { + + final static long TIMEOUT = 15000l; + final static long TOLERANCE = 1000l; + + @Inject + TimerEventListener timerEventListener; + + @Deployment + public static WebArchive deploy() { + File[] jars = Maven.resolver().loadPomFromFile("pom.xml") + .resolve("com.jayway.awaitility:awaitility") + .withTransitivity().asFile(); + + return ShrinkWrap.create(WebArchive.class) + .addAsLibraries(jars) + .addClasses(WithinWindowMatcher.class, TimerEvent.class, TimerEventListener.class, ProgrammaticWithInitialFixedDelayTimerBean.class); + } + + @Test + public void should_receive_two_pings() { + + Awaitility.setDefaultTimeout(30, TimeUnit.SECONDS); + + // 10 seconds pause so we get the startTime and it will trigger first event + long startTime = System.currentTimeMillis(); + + await().untilCall(to(timerEventListener.getEvents()).size(), equalTo(2)); + TimerEvent firstEvent = timerEventListener.getEvents().get(0); + TimerEvent secondEvent = timerEventListener.getEvents().get(1); + + long delay = secondEvent.getTime() - startTime; + System.out.println("Actual timeout = " + delay); + + //apx 15 seconds = 10 delay + 2 timers (first after a pause of 10 seconds and the next others every 5 seconds) + assertThat(delay, is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE))); + } +} \ No newline at end of file diff --git a/jee7schedule/src/test/java/com/baeldung/timer/ScheduleTimerBeanTest.java b/jee7schedule/src/test/java/com/baeldung/timer/ScheduleTimerBeanTest.java new file mode 100644 index 0000000000..7a5b043101 --- /dev/null +++ b/jee7schedule/src/test/java/com/baeldung/timer/ScheduleTimerBeanTest.java @@ -0,0 +1,59 @@ +package com.baeldung.timer; + +import com.jayway.awaitility.Awaitility; +import org.hamcrest.Matchers; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.shrinkwrap.resolver.api.maven.Maven; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.inject.Inject; +import java.io.File; +import java.util.concurrent.TimeUnit; + +import static com.jayway.awaitility.Awaitility.await; +import static com.jayway.awaitility.Awaitility.to; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + + +@RunWith(Arquillian.class) +public class ScheduleTimerBeanTest { + + final static long TIMEOUT = 5000l; + final static long TOLERANCE = 1000l; + + @Inject + TimerEventListener timerEventListener; + + @Deployment + public static WebArchive deploy() { + File[] jars = Maven.resolver().loadPomFromFile("pom.xml") + .resolve("com.jayway.awaitility:awaitility") + .withTransitivity().asFile(); + + return ShrinkWrap.create(WebArchive.class) + .addAsLibraries(jars) + .addClasses(WithinWindowMatcher.class, TimerEvent.class, TimerEventListener.class, ScheduleTimerBean.class); + } + + @Test + public void should_receive_three_pings() { + + Awaitility.setDefaultTimeout(30, TimeUnit.SECONDS); + await().untilCall(to(timerEventListener.getEvents()).size(), equalTo(3)); + + TimerEvent firstEvent = timerEventListener.getEvents().get(0); + TimerEvent secondEvent = timerEventListener.getEvents().get(1); + TimerEvent thirdEvent = timerEventListener.getEvents().get(2); + + long delay = secondEvent.getTime() - firstEvent.getTime(); + assertThat(delay, Matchers.is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE))); + delay = thirdEvent.getTime() - secondEvent.getTime(); + assertThat(delay, Matchers.is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE))); + + } +} diff --git a/jee7schedule/src/test/java/com/baeldung/timer/WithinWindowMatcher.java b/jee7schedule/src/test/java/com/baeldung/timer/WithinWindowMatcher.java new file mode 100644 index 0000000000..91adca6042 --- /dev/null +++ b/jee7schedule/src/test/java/com/baeldung/timer/WithinWindowMatcher.java @@ -0,0 +1,30 @@ +package com.baeldung.timer; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; + +class WithinWindowMatcher extends BaseMatcher { + private final long timeout; + private final long tolerance; + + public WithinWindowMatcher(long timeout, long tolerance) { + this.timeout = timeout; + this.tolerance = tolerance; + } + + @Override + public boolean matches(Object item) { + final Long actual = (Long) item; + return Math.abs(actual - timeout) < tolerance; + } + + @Override + public void describeTo(Description description) { + //To change body of implemented methods use File | Settings | File Templates. + } + + public static Matcher withinWindow(final long timeout, final long tolerance) { + return new WithinWindowMatcher(timeout, tolerance); + } +} diff --git a/jooq-spring/.gitignore b/jooq-spring/.gitignore new file mode 100644 index 0000000000..aba850c8e6 --- /dev/null +++ b/jooq-spring/.gitignore @@ -0,0 +1 @@ +/src/main/java/com/baeldung/jooq/introduction/db \ No newline at end of file diff --git a/jooq-spring/pom.xml b/jooq-spring/pom.xml new file mode 100644 index 0000000000..e77ff0cbfd --- /dev/null +++ b/jooq-spring/pom.xml @@ -0,0 +1,178 @@ + + 4.0.0 + com.baeldung + jooq-spring + 0.0.1-SNAPSHOT + + + + + + org.springframework.boot + spring-boot-dependencies + 1.3.5.RELEASE + pom + import + + + + + + + + org.jooq + jooq + ${org.jooq.version} + + + + + com.h2database + h2 + ${com.h2database.version} + + + + + org.springframework + spring-context + + + org.springframework + spring-jdbc + + + org.springframework.boot + spring-boot-starter-jooq + + + + + org.slf4j + slf4j-api + runtime + + + ch.qos.logback + logback-classic + runtime + + + + + junit + junit + test + + + org.springframework + spring-test + test + + + + + + + org.codehaus.mojo + properties-maven-plugin + 1.0.0 + + + initialize + + read-project-properties + + + + src/main/resources/intro_config.properties + + + + + + + + org.codehaus.mojo + sql-maven-plugin + 1.5 + + + initialize + + execute + + + ${db.driver} + ${db.url} + ${db.username} + ${db.password} + + src/main/resources/intro_schema.sql + + + + + + + com.h2database + h2 + ${com.h2database.version} + + + + + + org.jooq + jooq-codegen-maven + ${org.jooq.version} + + + generate-sources + + generate + + + + ${db.driver} + ${db.url} + ${db.username} + ${db.password} + + + + com.baeldung.jooq.introduction.db + src/main/java + + + + + + + + + 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/jooq-spring/src/main/resources/application.properties b/jooq-spring/src/main/resources/application.properties new file mode 100644 index 0000000000..2b32da2356 --- /dev/null +++ b/jooq-spring/src/main/resources/application.properties @@ -0,0 +1,3 @@ +spring.datasource.url=jdbc:h2:~/jooq +spring.datasource.username=sa +spring.datasource.password= \ No newline at end of file diff --git a/jooq-spring/src/main/resources/intro_config.properties b/jooq-spring/src/main/resources/intro_config.properties new file mode 100644 index 0000000000..3275089585 --- /dev/null +++ b/jooq-spring/src/main/resources/intro_config.properties @@ -0,0 +1,8 @@ +#Database Configuration +db.driver=org.h2.Driver +db.url=jdbc:h2:~/jooq +db.username=sa +db.password= + +#SQL Dialect +jooq.sql.dialect=H2 \ No newline at end of file diff --git a/jooq-spring/src/main/resources/intro_schema.sql b/jooq-spring/src/main/resources/intro_schema.sql new file mode 100644 index 0000000000..0d4bd26235 --- /dev/null +++ b/jooq-spring/src/main/resources/intro_schema.sql @@ -0,0 +1,34 @@ +DROP TABLE IF EXISTS author_book, author, book; + +CREATE TABLE author ( + id INT NOT NULL PRIMARY KEY, + first_name VARCHAR(50), + last_name VARCHAR(50) NOT NULL +); + +CREATE TABLE book ( + id INT NOT NULL PRIMARY KEY, + title VARCHAR(100) NOT NULL +); + +CREATE TABLE author_book ( + author_id INT NOT NULL, + book_id INT NOT NULL, + + PRIMARY KEY (author_id, book_id), + CONSTRAINT fk_ab_author FOREIGN KEY (author_id) REFERENCES author (id) + ON UPDATE CASCADE ON DELETE CASCADE, + CONSTRAINT fk_ab_book FOREIGN KEY (book_id) REFERENCES book (id) +); + +INSERT INTO author VALUES + (1, 'Kathy', 'Sierra'), + (2, 'Bert', 'Bates'), + (3, 'Bryan', 'Basham'); + +INSERT INTO book VALUES + (1, 'Head First Java'), + (2, 'Head First Servlets and JSP'), + (3, 'OCA/OCP Java SE 7 Programmer'); + +INSERT INTO author_book VALUES (1, 1), (1, 3), (2, 1); \ No newline at end of file diff --git a/jooq-spring/src/test/java/com/baeldung/jooq/introduction/ExceptionTranslator.java b/jooq-spring/src/test/java/com/baeldung/jooq/introduction/ExceptionTranslator.java new file mode 100644 index 0000000000..8312f20c05 --- /dev/null +++ b/jooq-spring/src/test/java/com/baeldung/jooq/introduction/ExceptionTranslator.java @@ -0,0 +1,19 @@ +package com.baeldung.jooq.introduction; + +import org.jooq.ExecuteContext; +import org.jooq.SQLDialect; +import org.jooq.impl.DefaultExecuteListener; + +import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator; +import org.springframework.jdbc.support.SQLExceptionTranslator; + +public class ExceptionTranslator extends DefaultExecuteListener { + + @Override + public void exception(ExecuteContext context) { + SQLDialect dialect = context.configuration().dialect(); + SQLExceptionTranslator translator = new SQLErrorCodeSQLExceptionTranslator(dialect.name()); + + context.exception(translator.translate("Access database using jOOQ", context.sql(), context.sqlException())); + } +} \ No newline at end of file diff --git a/jooq-spring/src/test/java/com/baeldung/jooq/introduction/PersistenceContext.java b/jooq-spring/src/test/java/com/baeldung/jooq/introduction/PersistenceContext.java new file mode 100644 index 0000000000..df628f9f78 --- /dev/null +++ b/jooq-spring/src/test/java/com/baeldung/jooq/introduction/PersistenceContext.java @@ -0,0 +1,78 @@ +package com.baeldung.jooq.introduction; + +import org.h2.jdbcx.JdbcDataSource; +import org.jooq.SQLDialect; +import org.jooq.impl.DataSourceConnectionProvider; +import org.jooq.impl.DefaultConfiguration; +import org.jooq.impl.DefaultDSLContext; +import org.jooq.impl.DefaultExecuteListenerProvider; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import javax.sql.DataSource; + +@Configuration +@ComponentScan({ "com.baeldung.jooq.introduction.db.public_.tables" }) +@EnableTransactionManagement +@PropertySource("classpath:intro_config.properties") +public class PersistenceContext { + + @Autowired + private Environment environment; + + @Bean + public DataSource dataSource() { + JdbcDataSource dataSource = new JdbcDataSource(); + + dataSource.setUrl(environment.getRequiredProperty("db.url")); + dataSource.setUser(environment.getRequiredProperty("db.username")); + dataSource.setPassword(environment.getRequiredProperty("db.password")); + + return dataSource; + } + + @Bean + public TransactionAwareDataSourceProxy transactionAwareDataSource() { + return new TransactionAwareDataSourceProxy(dataSource()); + } + + @Bean + public DataSourceTransactionManager transactionManager() { + return new DataSourceTransactionManager(dataSource()); + } + + @Bean + public DataSourceConnectionProvider connectionProvider() { + return new DataSourceConnectionProvider(transactionAwareDataSource()); + } + + @Bean + public ExceptionTranslator exceptionTransformer() { + return new ExceptionTranslator(); + } + + @Bean + public DefaultDSLContext dsl() { + return new DefaultDSLContext(configuration()); + } + + @Bean + public DefaultConfiguration configuration() { + DefaultConfiguration jooqConfiguration = new DefaultConfiguration(); + jooqConfiguration.set(connectionProvider()); + jooqConfiguration.set(new DefaultExecuteListenerProvider(exceptionTransformer())); + + String sqlDialectName = environment.getRequiredProperty("jooq.sql.dialect"); + SQLDialect dialect = SQLDialect.valueOf(sqlDialectName); + jooqConfiguration.set(dialect); + + return jooqConfiguration; + } +} \ No newline at end of file diff --git a/jooq-spring/src/test/java/com/baeldung/jooq/introduction/QueryTest.java b/jooq-spring/src/test/java/com/baeldung/jooq/introduction/QueryTest.java new file mode 100644 index 0000000000..68f975dd6d --- /dev/null +++ b/jooq-spring/src/test/java/com/baeldung/jooq/introduction/QueryTest.java @@ -0,0 +1,122 @@ +package com.baeldung.jooq.introduction; + +import org.jooq.DSLContext; +import org.jooq.Record3; +import org.jooq.Result; +import org.jooq.impl.DSL; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataAccessException; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; + +import static com.baeldung.jooq.introduction.db.public_.tables.Author.AUTHOR; +import static com.baeldung.jooq.introduction.db.public_.tables.AuthorBook.AUTHOR_BOOK; +import static com.baeldung.jooq.introduction.db.public_.tables.Book.BOOK; +import static org.junit.Assert.assertEquals; + +@ContextConfiguration(classes = PersistenceContext.class) +@Transactional(transactionManager = "transactionManager") +@RunWith(SpringJUnit4ClassRunner.class) +public class QueryTest { + + @Autowired + private DSLContext dsl; + + @Test + public void givenValidData_whenInserting_thenSucceed() { + dsl.insertInto(AUTHOR) + .set(AUTHOR.ID, 4) + .set(AUTHOR.FIRST_NAME, "Herbert") + .set(AUTHOR.LAST_NAME, "Schildt") + .execute(); + + dsl.insertInto(BOOK) + .set(BOOK.ID, 4) + .set(BOOK.TITLE, "A Beginner's Guide") + .execute(); + + dsl.insertInto(AUTHOR_BOOK) + .set(AUTHOR_BOOK.AUTHOR_ID, 4) + .set(AUTHOR_BOOK.BOOK_ID, 4) + .execute(); + + final Result> result = dsl.select(AUTHOR.ID, AUTHOR.LAST_NAME, DSL.count()) + .from(AUTHOR) + .join(AUTHOR_BOOK).on(AUTHOR.ID.equal(AUTHOR_BOOK.AUTHOR_ID)) + .join(BOOK).on(AUTHOR_BOOK.BOOK_ID.equal(BOOK.ID)) + .groupBy(AUTHOR.LAST_NAME).fetch(); + + assertEquals(3, result.size()); + assertEquals("Sierra", result.getValue(0, AUTHOR.LAST_NAME)); + assertEquals(Integer.valueOf(2), result.getValue(0, DSL.count())); + assertEquals("Schildt", result.getValue(2, AUTHOR.LAST_NAME)); + assertEquals(Integer.valueOf(1), result.getValue(2, DSL.count())); + } + + @Test(expected = DataAccessException.class) + public void givenInvalidData_whenInserting_thenFail() { + dsl.insertInto(AUTHOR_BOOK).set(AUTHOR_BOOK.AUTHOR_ID, 4).set(AUTHOR_BOOK.BOOK_ID, 5).execute(); + } + + @Test + public void givenValidData_whenUpdating_thenSucceed() { + dsl.update(AUTHOR) + .set(AUTHOR.LAST_NAME, "Baeldung") + .where(AUTHOR.ID.equal(3)) + .execute(); + + dsl.update(BOOK) + .set(BOOK.TITLE, "Building your REST API with Spring") + .where(BOOK.ID.equal(3)).execute(); + + dsl.insertInto(AUTHOR_BOOK) + .set(AUTHOR_BOOK.AUTHOR_ID, 3) + .set(AUTHOR_BOOK.BOOK_ID, 3) + .execute(); + + final Result> result = dsl.select(AUTHOR.ID, AUTHOR.LAST_NAME, BOOK.TITLE) + .from(AUTHOR) + .join(AUTHOR_BOOK).on(AUTHOR.ID.equal(AUTHOR_BOOK.AUTHOR_ID)) + .join(BOOK).on(AUTHOR_BOOK.BOOK_ID.equal(BOOK.ID)) + .where(AUTHOR.ID.equal(3)) + .fetch(); + + assertEquals(1, result.size()); + assertEquals(Integer.valueOf(3), result.getValue(0, AUTHOR.ID)); + assertEquals("Baeldung", result.getValue(0, AUTHOR.LAST_NAME)); + assertEquals("Building your REST API with Spring", result.getValue(0, BOOK.TITLE)); + } + + @Test(expected = DataAccessException.class) + public void givenInvalidData_whenUpdating_thenFail() { + dsl.update(AUTHOR_BOOK) + .set(AUTHOR_BOOK.AUTHOR_ID, 4) + .set(AUTHOR_BOOK.BOOK_ID, 5) + .execute(); + } + + @Test + public void givenValidData_whenDeleting_thenSucceed() { + dsl.delete(AUTHOR) + .where(AUTHOR.ID.lt(3)) + .execute(); + + final Result> result = dsl.select(AUTHOR.ID, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME) + .from(AUTHOR) + .fetch(); + + assertEquals(1, result.size()); + assertEquals("Bryan", result.getValue(0, AUTHOR.FIRST_NAME)); + assertEquals("Basham", result.getValue(0, AUTHOR.LAST_NAME)); + } + + @Test(expected = DataAccessException.class) + public void givenInvalidData_whenDeleting_thenFail() { + dsl.delete(BOOK) + .where(BOOK.ID.equal(1)) + .execute(); + } +} \ No newline at end of file diff --git a/jooq-spring/src/test/java/com/baeldung/jooq/springboot/Application.java b/jooq-spring/src/test/java/com/baeldung/jooq/springboot/Application.java new file mode 100644 index 0000000000..ff74851d2a --- /dev/null +++ b/jooq-spring/src/test/java/com/baeldung/jooq/springboot/Application.java @@ -0,0 +1,9 @@ +package com.baeldung.jooq.springboot; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@SpringBootApplication +@EnableTransactionManagement +public class Application { +} \ No newline at end of file diff --git a/jooq-spring/src/test/java/com/baeldung/jooq/springboot/InitialConfiguration.java b/jooq-spring/src/test/java/com/baeldung/jooq/springboot/InitialConfiguration.java new file mode 100644 index 0000000000..c0d9dac797 --- /dev/null +++ b/jooq-spring/src/test/java/com/baeldung/jooq/springboot/InitialConfiguration.java @@ -0,0 +1,39 @@ +package com.baeldung.jooq.springboot; + +import com.baeldung.jooq.introduction.ExceptionTranslator; +import org.jooq.impl.DataSourceConnectionProvider; +import org.jooq.impl.DefaultConfiguration; +import org.jooq.impl.DefaultDSLContext; +import org.jooq.impl.DefaultExecuteListenerProvider; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; + +import javax.sql.DataSource; + +@Configuration +public class InitialConfiguration { + + @Autowired + private DataSource dataSource; + + @Bean + public DataSourceConnectionProvider connectionProvider() { + return new DataSourceConnectionProvider(new TransactionAwareDataSourceProxy(dataSource)); + } + + @Bean + public DefaultDSLContext dsl() { + return new DefaultDSLContext(configuration()); + } + + public DefaultConfiguration configuration() { + DefaultConfiguration jooqConfiguration = new DefaultConfiguration(); + + jooqConfiguration.set(connectionProvider()); + jooqConfiguration.set(new DefaultExecuteListenerProvider(new ExceptionTranslator())); + + return jooqConfiguration; + } +} diff --git a/jooq-spring/src/test/java/com/baeldung/jooq/springboot/SpringBootTest.java b/jooq-spring/src/test/java/com/baeldung/jooq/springboot/SpringBootTest.java new file mode 100644 index 0000000000..f9427f30fb --- /dev/null +++ b/jooq-spring/src/test/java/com/baeldung/jooq/springboot/SpringBootTest.java @@ -0,0 +1,124 @@ +package com.baeldung.jooq.springboot; + +import org.jooq.DSLContext; +import org.jooq.Record3; +import org.jooq.Result; +import org.jooq.impl.DSL; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.dao.DataAccessException; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; + +import static com.baeldung.jooq.introduction.db.public_.tables.Author.AUTHOR; +import static com.baeldung.jooq.introduction.db.public_.tables.AuthorBook.AUTHOR_BOOK; +import static com.baeldung.jooq.introduction.db.public_.tables.Book.BOOK; +import static org.junit.Assert.assertEquals; + +@SpringApplicationConfiguration(Application.class) +@Transactional("transactionManager") +@RunWith(SpringJUnit4ClassRunner.class) +public class SpringBootTest { + + @Autowired + private DSLContext dsl; + + @Test + public void givenValidData_whenInserting_thenSucceed() { + dsl.insertInto(AUTHOR) + .set(AUTHOR.ID, 4) + .set(AUTHOR.FIRST_NAME, "Herbert") + .set(AUTHOR.LAST_NAME, "Schildt") + .execute(); + + dsl.insertInto(BOOK) + .set(BOOK.ID, 4) + .set(BOOK.TITLE, "A Beginner's Guide") + .execute(); + + dsl.insertInto(AUTHOR_BOOK) + .set(AUTHOR_BOOK.AUTHOR_ID, 4) + .set(AUTHOR_BOOK.BOOK_ID, 4) + .execute(); + + final Result> result = dsl.select(AUTHOR.ID, AUTHOR.LAST_NAME, DSL.count()) + .from(AUTHOR).join(AUTHOR_BOOK).on(AUTHOR.ID.equal(AUTHOR_BOOK.AUTHOR_ID)) + .join(BOOK).on(AUTHOR_BOOK.BOOK_ID.equal(BOOK.ID)) + .groupBy(AUTHOR.LAST_NAME) + .fetch(); + + assertEquals(3, result.size()); + assertEquals("Sierra", result.getValue(0, AUTHOR.LAST_NAME)); + assertEquals(Integer.valueOf(2), result.getValue(0, DSL.count())); + assertEquals("Schildt", result.getValue(2, AUTHOR.LAST_NAME)); + assertEquals(Integer.valueOf(1), result.getValue(2, DSL.count())); + } + + @Test(expected = DataAccessException.class) + public void givenInvalidData_whenInserting_thenFail() { + dsl.insertInto(AUTHOR_BOOK) + .set(AUTHOR_BOOK.AUTHOR_ID, 4) + .set(AUTHOR_BOOK.BOOK_ID, 5) + .execute(); + } + + @Test + public void givenValidData_whenUpdating_thenSucceed() { + dsl.update(AUTHOR) + .set(AUTHOR.LAST_NAME, "Baeldung") + .where(AUTHOR.ID.equal(3)) + .execute(); + + dsl.update(BOOK) + .set(BOOK.TITLE, "Building your REST API with Spring") + .where(BOOK.ID.equal(3)) + .execute(); + + dsl.insertInto(AUTHOR_BOOK) + .set(AUTHOR_BOOK.AUTHOR_ID, 3) + .set(AUTHOR_BOOK.BOOK_ID, 3) + .execute(); + + final Result> result = dsl.select(AUTHOR.ID, AUTHOR.LAST_NAME, BOOK.TITLE) + .from(AUTHOR).join(AUTHOR_BOOK).on(AUTHOR.ID.equal(AUTHOR_BOOK.AUTHOR_ID)) + .join(BOOK).on(AUTHOR_BOOK.BOOK_ID.equal(BOOK.ID)) + .where(AUTHOR.ID.equal(3)) + .fetch(); + + assertEquals(1, result.size()); + assertEquals(Integer.valueOf(3), result.getValue(0, AUTHOR.ID)); + assertEquals("Baeldung", result.getValue(0, AUTHOR.LAST_NAME)); + assertEquals("Building your REST API with Spring", result.getValue(0, BOOK.TITLE)); + } + + @Test(expected = DataAccessException.class) + public void givenInvalidData_whenUpdating_thenFail() { + dsl.update(AUTHOR_BOOK) + .set(AUTHOR_BOOK.AUTHOR_ID, 4) + .set(AUTHOR_BOOK.BOOK_ID, 5) + .execute(); + } + + @Test + public void givenValidData_whenDeleting_thenSucceed() { + dsl.delete(AUTHOR) + .where(AUTHOR.ID.lt(3)) + .execute(); + + final Result> result = dsl.select(AUTHOR.ID, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME) + .from(AUTHOR).fetch(); + + assertEquals(1, result.size()); + assertEquals("Bryan", result.getValue(0, AUTHOR.FIRST_NAME)); + assertEquals("Basham", result.getValue(0, AUTHOR.LAST_NAME)); + } + + @Test(expected = DataAccessException.class) + public void givenInvalidData_whenDeleting_thenFail() { + dsl.delete(BOOK) + .where(BOOK.ID.equal(1)) + .execute(); + } +} \ No newline at end of file diff --git a/core-java-8/.classpath b/jpa-storedprocedure/.classpath similarity index 100% rename from core-java-8/.classpath rename to jpa-storedprocedure/.classpath index 5efa587d72..fae1a2b37d 100644 --- a/core-java-8/.classpath +++ b/jpa-storedprocedure/.classpath @@ -6,23 +6,18 @@ - - - - - - - + + - + @@ -32,5 +27,10 @@ + + + + + diff --git a/mockito/.project b/jpa-storedprocedure/.project similarity index 84% rename from mockito/.project rename to jpa-storedprocedure/.project index 1c6c0deddc..b5ac58ebd1 100644 --- a/mockito/.project +++ b/jpa-storedprocedure/.project @@ -1,22 +1,17 @@ - mockito + jpa-storedprocedure - - org.eclipse.jdt.core.javabuilder - - - org.eclipse.wst.common.project.facet.core.builder - org.eclipse.wst.validation.validationbuilder + org.eclipse.jdt.core.javabuilder @@ -25,10 +20,13 @@ + + org.eclipse.wst.validation.validationbuilder + + + - org.eclipse.jem.workbench.JavaEMFNature - org.eclipse.wst.common.modulecore.ModuleCoreNature org.eclipse.jdt.core.javanature org.eclipse.m2e.core.maven2Nature org.eclipse.wst.common.project.facet.core.nature diff --git a/jpa-storedprocedure/pom.xml b/jpa-storedprocedure/pom.xml new file mode 100644 index 0000000000..b2ebaa32e2 --- /dev/null +++ b/jpa-storedprocedure/pom.xml @@ -0,0 +1,80 @@ + + + 4.0.0 + + com.baeldung + jpa-storedprocedure + 1.0 + jar + + + 7.0 + 5.1.0.Final + 5.1.38 + + + + JpaStoredProcedure + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + maven-assembly-plugin + + + + jar-with-dependencies + + + + + + + + + + + + javax + javaee-api + ${jee.version} + provided + + + + org.hibernate + hibernate-entitymanager + ${hibernate.version} + + + + + + mysql + mysql-connector-java + 5.1.38 + + + + + + junit + junit + 4.4 + + + + commons-io + commons-io + 2.4 + + + + + \ No newline at end of file diff --git a/jpa-storedprocedure/src/main/java/com/baeldung/jpa/model/Car.java b/jpa-storedprocedure/src/main/java/com/baeldung/jpa/model/Car.java new file mode 100644 index 0000000000..676d76307e --- /dev/null +++ b/jpa-storedprocedure/src/main/java/com/baeldung/jpa/model/Car.java @@ -0,0 +1,60 @@ +package com.baeldung.jpa.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.NamedStoredProcedureQueries; +import javax.persistence.NamedStoredProcedureQuery; +import javax.persistence.ParameterMode; +import javax.persistence.StoredProcedureParameter; +import javax.persistence.Table; + +@Entity +@Table(name = "CAR") +@NamedStoredProcedureQueries({ + @NamedStoredProcedureQuery(name = "findByYearProcedure", procedureName = "FIND_CAR_BY_YEAR", resultClasses = { Car.class }, parameters = { @StoredProcedureParameter(name = "p_year", type = Integer.class, mode = ParameterMode.IN) }) }) +public class Car { + + private long id; + private String model; + private Integer year; + + public Car(final String model, final Integer year) { + this.model = model; + this.year = year; + } + + public Car() { + } + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "ID", unique = true, nullable = false, scale = 0) + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } + + @Column(name = "MODEL") + public String getModel() { + return model; + } + + public void setModel(final String model) { + this.model = model; + } + + @Column(name = "YEAR") + public Integer getYear() { + return year; + } + + public void setYear(final Integer year) { + this.year = year; + } +} diff --git a/jpa-storedprocedure/src/main/java/com/baeldung/jpa/model/QueryParameter.java b/jpa-storedprocedure/src/main/java/com/baeldung/jpa/model/QueryParameter.java new file mode 100644 index 0000000000..0a15213f62 --- /dev/null +++ b/jpa-storedprocedure/src/main/java/com/baeldung/jpa/model/QueryParameter.java @@ -0,0 +1,28 @@ +package com.baeldung.jpa.model; + +import java.util.HashMap; +import java.util.Map; + +public class QueryParameter { + + private Map parameters = null; + + private QueryParameter(final String name, final Object value) { + this.parameters = new HashMap<>(); + this.parameters.put(name, value); + } + + public static QueryParameter with(final String name, final Object value) { + return new QueryParameter(name, value); + } + + public QueryParameter and(final String name, final Object value) { + this.parameters.put(name, value); + return this; + } + + public Map parameters() { + return this.parameters; + } + +} \ No newline at end of file diff --git a/jpa-storedprocedure/src/main/resources/META-INF/persistence.xml b/jpa-storedprocedure/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000000..4c443cb7cf --- /dev/null +++ b/jpa-storedprocedure/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,20 @@ + + + + + org.hibernate.jpa.HibernatePersistenceProvider + com.baeldung.jpa.model.Car + + + + + + + + + + \ No newline at end of file diff --git a/jpa-storedprocedure/src/main/resources/config/database/FindCarByYearProcedureMySQL.sql b/jpa-storedprocedure/src/main/resources/config/database/FindCarByYearProcedureMySQL.sql new file mode 100644 index 0000000000..0ef84fb7eb --- /dev/null +++ b/jpa-storedprocedure/src/main/resources/config/database/FindCarByYearProcedureMySQL.sql @@ -0,0 +1,8 @@ +DELIMITER $$ +CREATE DEFINER=`root`@`localhost` PROCEDURE `FIND_CAR_BY_YEAR`(in p_year int) +begin +SELECT ID, MODEL, YEAR + FROM CAR + WHERE YEAR = p_year; +end$$ +DELIMITER ; diff --git a/jpa-storedprocedure/src/main/resources/config/database/create_table_mysql.sql b/jpa-storedprocedure/src/main/resources/config/database/create_table_mysql.sql new file mode 100644 index 0000000000..4a4feebdea --- /dev/null +++ b/jpa-storedprocedure/src/main/resources/config/database/create_table_mysql.sql @@ -0,0 +1,6 @@ +CREATE TABLE `car` ( + `ID` int(10) NOT NULL AUTO_INCREMENT, + `MODEL` varchar(50) NOT NULL, + `YEAR` int(4) NOT NULL, + PRIMARY KEY (`ID`) +) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; \ No newline at end of file diff --git a/jpa-storedprocedure/src/main/resources/config/database/insert_cars.sql b/jpa-storedprocedure/src/main/resources/config/database/insert_cars.sql new file mode 100644 index 0000000000..89f69ac1ee --- /dev/null +++ b/jpa-storedprocedure/src/main/resources/config/database/insert_cars.sql @@ -0,0 +1,5 @@ +INSERT INTO CAR (ID, MODEL, YEAR) VALUES ('123456', 'Camaro', '2012'); +INSERT INTO CAR (ID, MODEL, YEAR) VALUES ('12112', 'Fiat Panda', '2000'); +INSERT INTO CAR (ID, MODEL, YEAR) VALUES ('111000', 'Fiat Punto', '2007'); +INSERT INTO CAR (ID, MODEL, YEAR) VALUES ('3382', 'Citroen C3', '2009'); +commit; \ No newline at end of file diff --git a/jpa-storedprocedure/src/test/java/com/baeldung/jpa/storedprocedure/StoredProcedureTest.java b/jpa-storedprocedure/src/test/java/com/baeldung/jpa/storedprocedure/StoredProcedureTest.java new file mode 100644 index 0000000000..69351d9683 --- /dev/null +++ b/jpa-storedprocedure/src/test/java/com/baeldung/jpa/storedprocedure/StoredProcedureTest.java @@ -0,0 +1,72 @@ +package com.baeldung.jpa.storedprocedure; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.ParameterMode; +import javax.persistence.Persistence; +import javax.persistence.StoredProcedureQuery; + +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.baeldung.jpa.model.Car; + +public class StoredProcedureTest { + + private static EntityManagerFactory factory = null; + private static EntityManager entityManager = null; + + @BeforeClass + public static void init() { + factory = Persistence.createEntityManagerFactory("jpa-db"); + entityManager = factory.createEntityManager(); + } + + @Before + public void setup() { + } + + @Test + public void createCarTest() { + final EntityTransaction transaction = entityManager.getTransaction(); + try { + transaction.begin(); + final Car car = new Car("Fiat Marea", 2015); + entityManager.persist(car); + transaction.commit(); + } catch (final Exception e) { + System.out.println(e.getCause()); + if (transaction.isActive()) { + transaction.rollback(); + } + } + } + + @Test + public void findCarsByYearNamedProcedure() { + final StoredProcedureQuery findByYearProcedure = entityManager.createNamedStoredProcedureQuery("findByYearProcedure"); + final StoredProcedureQuery storedProcedure = findByYearProcedure.setParameter("p_year", 2015); + storedProcedure.getResultList().forEach(c -> Assert.assertEquals(new Integer(2015), ((Car) c).getYear())); + } + + @Test + public void findCarsByYearNoNamed() { + final StoredProcedureQuery storedProcedure = entityManager.createStoredProcedureQuery("FIND_CAR_BY_YEAR", Car.class).registerStoredProcedureParameter(1, Integer.class, ParameterMode.IN).setParameter(1, 2015); + storedProcedure.getResultList().forEach(c -> Assert.assertEquals(new Integer(2015), ((Car) c).getYear())); + } + + @AfterClass + public static void destroy() { + + if (entityManager != null) { + entityManager.close(); + } + if (factory != null) { + factory.close(); + } + } +} diff --git a/jpa-storedprocedure/src/test/resources/persistence.xml b/jpa-storedprocedure/src/test/resources/persistence.xml new file mode 100644 index 0000000000..d94221b54f --- /dev/null +++ b/jpa-storedprocedure/src/test/resources/persistence.xml @@ -0,0 +1,21 @@ + + + + + org.hibernate.jpa.HibernatePersistenceProvider + com.baeldung.jpa.model.Car + + + + + + + + + + + diff --git a/jsf/.gitignore b/jsf/.gitignore new file mode 100644 index 0000000000..d1c20ca0f1 --- /dev/null +++ b/jsf/.gitignore @@ -0,0 +1,2 @@ +/target +*.iml diff --git a/jsf/pom.xml b/jsf/pom.xml new file mode 100644 index 0000000000..c4801996de --- /dev/null +++ b/jsf/pom.xml @@ -0,0 +1,130 @@ + + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + 4.0.0 + + jsf + 0.1-SNAPSHOT + war + + + + + com.sun.faces + jsf-api + ${com.sun.faces.version} + + + com.sun.faces + jsf-impl + ${com.sun.faces.version} + + + + + + org.springframework + spring-web + ${org.springframework.version} + + + org.springframework + spring-webmvc + ${org.springframework.version} + + + org.springframework + spring-websocket + ${org.springframework.version} + + + org.springframework + spring-messaging + ${org.springframework.version} + + + org.springframework + spring-web + ${org.springframework.version} + + + javax.servlet + javax.servlet-api + provided + ${javax.servlet.version} + + + + + + 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} + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-war-plugin + ${maven-war-plugin.version} + + false + + + + + + + + 4.2.5.RELEASE + + + 2.1.7 + + + 1.7.13 + 1.1.3 + + + 3.1.0 + + + 3.5.1 + 2.6 + + + \ No newline at end of file diff --git a/jsf/src/main/java/com/baeldung/springintegration/config/MainWebAppInitializer.java b/jsf/src/main/java/com/baeldung/springintegration/config/MainWebAppInitializer.java new file mode 100644 index 0000000000..2de647915c --- /dev/null +++ b/jsf/src/main/java/com/baeldung/springintegration/config/MainWebAppInitializer.java @@ -0,0 +1,36 @@ +package com.baeldung.springintegration.config; + +import com.sun.faces.config.FacesInitializer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.WebApplicationInitializer; +import org.springframework.web.context.ContextLoaderListener; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import java.util.Set; + +public class MainWebAppInitializer extends FacesInitializer implements WebApplicationInitializer { + private static final Logger LOGGER = LoggerFactory.getLogger(MainWebAppInitializer.class); + + @Override + public void onStartup(Set> classes, ServletContext servletContext) throws ServletException { + super.onStartup(classes, servletContext); + } + + /** + * Register and configure all Servlet container components necessary to power the web application. + */ + @Override + public void onStartup(final ServletContext sc) throws ServletException { + LOGGER.info("MainWebAppInitializer.onStartup()"); + sc.setInitParameter("javax.faces.FACELETS_SKIP_COMMENTS", "true"); + + // Create the 'root' Spring application context + final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); + root.register(SpringCoreConfig.class); + sc.addListener(new ContextLoaderListener(root)); + } + +} diff --git a/jsf/src/main/java/com/baeldung/springintegration/config/SpringCoreConfig.java b/jsf/src/main/java/com/baeldung/springintegration/config/SpringCoreConfig.java new file mode 100644 index 0000000000..a39da3fe25 --- /dev/null +++ b/jsf/src/main/java/com/baeldung/springintegration/config/SpringCoreConfig.java @@ -0,0 +1,15 @@ +package com.baeldung.springintegration.config; + +import com.baeldung.springintegration.dao.UserManagementDAO; +import com.baeldung.springintegration.dao.UserManagementDAOImpl; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class SpringCoreConfig { + + @Bean + public UserManagementDAO userManagementDAO() { + return new UserManagementDAOImpl(); + } +} diff --git a/jsf/src/main/java/com/baeldung/springintegration/controllers/RegistrationBean.java b/jsf/src/main/java/com/baeldung/springintegration/controllers/RegistrationBean.java new file mode 100644 index 0000000000..cf8dab5289 --- /dev/null +++ b/jsf/src/main/java/com/baeldung/springintegration/controllers/RegistrationBean.java @@ -0,0 +1,62 @@ +package com.baeldung.springintegration.controllers; + +import com.baeldung.springintegration.dao.UserManagementDAO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.faces.bean.ManagedBean; +import javax.faces.bean.ManagedProperty; +import javax.faces.bean.ViewScoped; +import javax.faces.context.FacesContext; +import java.io.Serializable; + +@ManagedBean(name = "registration") +@ViewScoped +public class RegistrationBean implements Serializable { + private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationBean.class); + + @ManagedProperty(value = "#{userManagementDAO}") + transient private UserManagementDAO userDao; + private String userName; + private String operationMessage; + + public void createNewUser() { + try { + LOGGER.info("Creating new user"); + FacesContext context = FacesContext.getCurrentInstance(); + boolean operationStatus = userDao.createUser(userName); + context.isValidationFailed(); + if (operationStatus) { + operationMessage = "User " + userName + " created"; + } + } catch (Exception ex) { + LOGGER.error("Error registering new user "); + ex.printStackTrace(); + operationMessage = "Error " + userName + " not created"; + } + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public void setUserDao(UserManagementDAO userDao) { + this.userDao = userDao; + } + + public UserManagementDAO getUserDao() { + return this.userDao; + } + + public String getOperationMessage() { + return operationMessage; + } + + public void setOperationMessage(String operationMessage) { + this.operationMessage = operationMessage; + } +} diff --git a/jsf/src/main/java/com/baeldung/springintegration/dao/UserManagementDAO.java b/jsf/src/main/java/com/baeldung/springintegration/dao/UserManagementDAO.java new file mode 100644 index 0000000000..2acb4e636d --- /dev/null +++ b/jsf/src/main/java/com/baeldung/springintegration/dao/UserManagementDAO.java @@ -0,0 +1,7 @@ +package com.baeldung.springintegration.dao; + +public interface UserManagementDAO { + + boolean createUser(String newUserData); + +} diff --git a/jsf/src/main/java/com/baeldung/springintegration/dao/UserManagementDAOImpl.java b/jsf/src/main/java/com/baeldung/springintegration/dao/UserManagementDAOImpl.java new file mode 100644 index 0000000000..56cbdd7b88 --- /dev/null +++ b/jsf/src/main/java/com/baeldung/springintegration/dao/UserManagementDAOImpl.java @@ -0,0 +1,38 @@ +package com.baeldung.springintegration.dao; + + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Repository; + +import java.util.ArrayList; +import java.util.List; +import javax.annotation.PostConstruct; + +@Repository +public class UserManagementDAOImpl implements UserManagementDAO { + private static final Logger LOGGER = LoggerFactory.getLogger(UserManagementDAOImpl.class); + + private List users; + + @PostConstruct + public void initUserList() { + users = new ArrayList<>(); + } + + @Override + public boolean createUser(String newUserData) { + if (newUserData != null) { + users.add(newUserData); + LOGGER.info("User {} successfully created", newUserData); + return true; + } else { + return false; + } + } + + public UserManagementDAOImpl() { + } + + +} diff --git a/sandbox/src/main/resources/logback.xml b/jsf/src/main/resources/logback.xml similarity index 79% rename from sandbox/src/main/resources/logback.xml rename to jsf/src/main/resources/logback.xml index 62d0ea5037..e9ae1894a6 100644 --- a/sandbox/src/main/resources/logback.xml +++ b/jsf/src/main/resources/logback.xml @@ -7,8 +7,6 @@ - - diff --git a/jsf/src/main/resources/messages.properties b/jsf/src/main/resources/messages.properties new file mode 100644 index 0000000000..41e9622630 --- /dev/null +++ b/jsf/src/main/resources/messages.properties @@ -0,0 +1,3 @@ +message.valueRequired = This value is required +message.welcome = Baeldung | Register +label.saveButton = Save diff --git a/jsf/src/main/webapp/META-INF/context.xml b/jsf/src/main/webapp/META-INF/context.xml new file mode 100644 index 0000000000..cf746bfae4 --- /dev/null +++ b/jsf/src/main/webapp/META-INF/context.xml @@ -0,0 +1,2 @@ + + diff --git a/jsf/src/main/webapp/WEB-INF/faces-config.xml b/jsf/src/main/webapp/WEB-INF/faces-config.xml new file mode 100644 index 0000000000..e9e6404b95 --- /dev/null +++ b/jsf/src/main/webapp/WEB-INF/faces-config.xml @@ -0,0 +1,39 @@ + + + + + + + + + + messages + + + msg + + + + + constraints + + + constraints + + + org.springframework.web.jsf.el.SpringBeanFacesELResolver + + + + /* + + home + /index.xhtml + + + + + diff --git a/jsf/src/main/webapp/index.xhtml b/jsf/src/main/webapp/index.xhtml new file mode 100644 index 0000000000..b37c864f2c --- /dev/null +++ b/jsf/src/main/webapp/index.xhtml @@ -0,0 +1,28 @@ + + + + + <h:outputText value="#{msg['message.welcome']}"/> + + + + + + + + + + + + + + + diff --git a/sandbox/.classpath b/json-path/.classpath similarity index 91% rename from sandbox/.classpath rename to json-path/.classpath index 8ebf6d9c31..2244ed1e21 100644 --- a/sandbox/.classpath +++ b/json-path/.classpath @@ -6,31 +6,27 @@ - - - - - - + + + + + + + - - - - - diff --git a/mockito/src/test/resources/.gitignore b/json-path/.gitignore similarity index 100% rename from mockito/src/test/resources/.gitignore rename to json-path/.gitignore diff --git a/json-path/.project b/json-path/.project new file mode 100644 index 0000000000..caa2c41711 --- /dev/null +++ b/json-path/.project @@ -0,0 +1,23 @@ + + + json-path + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/json-path/pom.xml b/json-path/pom.xml new file mode 100644 index 0000000000..c03385cf1d --- /dev/null +++ b/json-path/pom.xml @@ -0,0 +1,65 @@ + + 4.0.0 + com.baeldung + json-path + 0.0.1-SNAPSHOT + json-path + + + + + com.jayway.jsonpath + json-path + ${json-path.version} + + + + + junit + junit + ${junit.version} + test + + + + + org.slf4j + slf4j-api + ${slf4j.version} + runtime + + + ch.qos.logback + logback-classic + ${logback.version} + runtime + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + + + + + 2.1.0 + + + 4.12 + + + 1.7.14 + 1.1.3 + + \ No newline at end of file diff --git a/json-path/src/main/resources/intro_api.json b/json-path/src/main/resources/intro_api.json new file mode 100644 index 0000000000..8a252f2971 --- /dev/null +++ b/json-path/src/main/resources/intro_api.json @@ -0,0 +1,57 @@ +{ + "tool": + { + "jsonpath": + { + "creator": + { + "name": "Jayway Inc.", + "location": + [ + "Malmo", + "Stockholm", + "Copenhagen", + "San Francisco", + "Karlskrona", + "Halmstad", + "Helsingborg" + ] + }, + + "current release": "2.1" + } + }, + + "book": + [ + { + "title": "Beginning JSON", + "author": "Ben Smith", + "price": 49.99 + }, + + { + "title": "JSON at Work", + "author": "Tom Marrs", + "price": 29.99 + }, + + { + "title": "Learn JSON in a DAY", + "author": "Acodemy", + "price": 8.99 + }, + + { + "title": "JSON: Questions and Answers", + "author": "George Duckett", + "price": 6.00 + } + ], + + "price range": + { + "cheap": 10.00, + "medium": 20.00 + } +} \ No newline at end of file diff --git a/json-path/src/main/resources/intro_service.json b/json-path/src/main/resources/intro_service.json new file mode 100644 index 0000000000..448492463a --- /dev/null +++ b/json-path/src/main/resources/intro_service.json @@ -0,0 +1,61 @@ +[ + { + "id": 1, + "title": "Casino Royale", + "director": "Martin Campbell", + "starring": + [ + "Daniel Craig", + "Eva Green" + ], + + "desc": "Twenty-first James Bond movie", + "release date": 1163466000000, + "box office": 594275385 + }, + + { + "id": 2, + "title": "Quantum of Solace", + "director": "Marc Forster", + "starring": + [ + "Daniel Craig", + "Olga Kurylenko" + ], + + "desc": "Twenty-second James Bond movie", + "release date": 1225242000000, + "box office": 591692078 + }, + + { + "id": 3, + "title": "Skyfall", + "director": "Sam Mendes", + "starring": + [ + "Daniel Craig", + "Naomie Harris" + ], + + "desc": "Twenty-third James Bond movie", + "release date": 1350954000000, + "box office": 1110526981 + }, + + { + "id": 4, + "title": "Spectre", + "director": "Sam Mendes", + "starring": + [ + "Daniel Craig", + "Lea Seydoux" + ], + + "desc": "Twenty-fourth James Bond movie", + "release date": 1445821200000, + "box office": 879376275 + } +] \ No newline at end of file diff --git a/json-path/src/main/resources/intro_user.json b/json-path/src/main/resources/intro_user.json new file mode 100644 index 0000000000..c35914c6c4 --- /dev/null +++ b/json-path/src/main/resources/intro_user.json @@ -0,0 +1,46 @@ +[ + { + "username": "oracle", + "password": + { + "current": + { + "value": "Java_SE_8", + "created": 1397754000000 + }, + + "old": + [ + { + "value": "Java_SE_7", + "created": 1312650000000 + } + ] + } + }, + + { + "username": "sun", + "password": + { + "current": + { + "value": "Java_SE_6", + "created": 1168448400000 + }, + + "old": + [ + { + "value": "J2SE_5.0", + "created": 1099069200000 + }, + + { + "value": "J2SE_1.4", + "created": 1025542800000 + } + ] + } + } +] \ No newline at end of file diff --git a/json-path/src/test/java/com/baeldung/jsonpath/introduction/OperationTest.java b/json-path/src/test/java/com/baeldung/jsonpath/introduction/OperationTest.java new file mode 100644 index 0000000000..d90fafb7cb --- /dev/null +++ b/json-path/src/test/java/com/baeldung/jsonpath/introduction/OperationTest.java @@ -0,0 +1,71 @@ +package com.baeldung.jsonpath.introduction; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.not; + +import org.junit.Test; + +import java.io.InputStream; +import java.util.List; +import java.util.Map; +import java.util.Scanner; + +import com.jayway.jsonpath.Criteria; +import com.jayway.jsonpath.DocumentContext; +import com.jayway.jsonpath.Filter; +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.Predicate; + +public class OperationTest { + InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("intro_api.json"); + String jsonDataSourceString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z").next(); + + @Test + public void givenJsonPathWithoutPredicates_whenReading_thenCorrect() { + String jsonpathCreatorNamePath = "$['tool']['jsonpath']['creator']['name']"; + String jsonpathCreatorLocationPath = "$['tool']['jsonpath']['creator']['location'][*]"; + + DocumentContext jsonContext = JsonPath.parse(jsonDataSourceString); + String jsonpathCreatorName = jsonContext.read(jsonpathCreatorNamePath); + List jsonpathCreatorLocation = jsonContext.read(jsonpathCreatorLocationPath); + + assertEquals("Jayway Inc.", jsonpathCreatorName); + assertThat(jsonpathCreatorLocation.toString(), containsString("Malmo")); + assertThat(jsonpathCreatorLocation.toString(), containsString("San Francisco")); + assertThat(jsonpathCreatorLocation.toString(), containsString("Helsingborg")); + } + + @Test + public void givenJsonPathWithFilterPredicate_whenReading_thenCorrect() { + Filter expensiveFilter = Filter.filter(Criteria.where("price").gt(20.00)); + List> expensive = JsonPath.parse(jsonDataSourceString).read("$['book'][?]", expensiveFilter); + predicateUsageAssertionHelper(expensive); + } + + @Test + public void givenJsonPathWithCustomizedPredicate_whenReading_thenCorrect() { + Predicate expensivePredicate = new Predicate() { + public boolean apply(PredicateContext context) { + String value = context.item(Map.class).get("price").toString(); + return Float.valueOf(value) > 20.00; + } + }; + List> expensive = JsonPath.parse(jsonDataSourceString).read("$['book'][?]", expensivePredicate); + predicateUsageAssertionHelper(expensive); + } + + @Test + public void givenJsonPathWithInlinePredicate_whenReading_thenCorrect() { + List> expensive = JsonPath.parse(jsonDataSourceString).read("$['book'][?(@['price'] > $['price range']['medium'])]"); + predicateUsageAssertionHelper(expensive); + } + + private void predicateUsageAssertionHelper(List predicate) { + assertThat(predicate.toString(), containsString("Beginning JSON")); + assertThat(predicate.toString(), containsString("JSON at Work")); + assertThat(predicate.toString(), not(containsString("Learn JSON in a DAY"))); + assertThat(predicate.toString(), not(containsString("JSON: Questions and Answers"))); + } +} diff --git a/json-path/src/test/java/com/baeldung/jsonpath/introduction/ServiceTest.java b/json-path/src/test/java/com/baeldung/jsonpath/introduction/ServiceTest.java new file mode 100644 index 0000000000..868acac8d0 --- /dev/null +++ b/json-path/src/test/java/com/baeldung/jsonpath/introduction/ServiceTest.java @@ -0,0 +1,91 @@ +package com.baeldung.jsonpath.introduction; + +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertEquals; +import static org.hamcrest.CoreMatchers.containsString; + +import org.junit.Test; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Scanner; + +import com.jayway.jsonpath.Configuration; +import com.jayway.jsonpath.DocumentContext; +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.Option; + +public class ServiceTest { + InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("intro_service.json"); + String jsonString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z").next(); + + @Test + public void givenId_whenRequestingRecordData_thenSucceed() { + Object dataObject = JsonPath.parse(jsonString).read("$[?(@.id == 2)]"); + String dataString = dataObject.toString(); + + assertThat(dataString, containsString("2")); + assertThat(dataString, containsString("Quantum of Solace")); + assertThat(dataString, containsString("Twenty-second James Bond movie")); + } + + @Test + public void givenStarring_whenRequestingMovieTitle_thenSucceed() { + List> dataList = JsonPath.parse(jsonString).read("$[?('Eva Green' in @['starring'])]"); + String title = (String) dataList.get(0).get("title"); + + assertEquals("Casino Royale", title); + } + + @Test + public void givenCompleteStructure_whenCalculatingTotalRevenue_thenSucceed() { + DocumentContext context = JsonPath.parse(jsonString); + int length = context.read("$.length()"); + long revenue = 0; + for (int i = 0; i < length; i++) { + revenue += context.read("$[" + i + "]['box office']", Long.class); + } + + assertEquals(594275385L + 591692078L + 1110526981L + 879376275L, revenue); + } + + @Test + public void givenStructure_whenRequestingHighestRevenueMovieTitle_thenSucceed() { + DocumentContext context = JsonPath.parse(jsonString); + List revenueList = context.read("$[*]['box office']"); + Integer[] revenueArray = revenueList.toArray(new Integer[0]); + Arrays.sort(revenueArray); + + int highestRevenue = revenueArray[revenueArray.length - 1]; + Configuration pathConfiguration = Configuration.builder().options(Option.AS_PATH_LIST).build(); + List pathList = JsonPath.using(pathConfiguration).parse(jsonString).read("$[?(@['box office'] == " + highestRevenue + ")]"); + + Map dataRecord = context.read(pathList.get(0)); + String title = dataRecord.get("title"); + + assertEquals("Skyfall", title); + } + + @Test + public void givenDirector_whenRequestingLatestMovieTitle_thenSucceed() { + DocumentContext context = JsonPath.parse(jsonString); + List> dataList = context.read("$[?(@.director == 'Sam Mendes')]"); + + List dateList = new ArrayList<>(); + for (Map item : dataList) { + Object date = item.get("release date"); + dateList.add(date); + } + Long[] dateArray = dateList.toArray(new Long[0]); + Arrays.sort(dateArray); + + long latestTime = dateArray[dateArray.length - 1]; + List> finalDataList = context.read("$[?(@['director'] == 'Sam Mendes' && @['release date'] == " + latestTime + ")]"); + String title = (String) finalDataList.get(0).get("title"); + + assertEquals("Spectre", title); + } +} \ No newline at end of file diff --git a/junit5/pom.xml b/junit5/pom.xml new file mode 100644 index 0000000000..5a2ea61668 --- /dev/null +++ b/junit5/pom.xml @@ -0,0 +1,83 @@ + + + 4.0.0 + + com.baeldung + junit5 + 1.0-SNAPSHOT + + junit5 + Intro to JUnit 5 + + + UTF-8 + 1.8 + + 5.0.0-SNAPSHOT + + + + + snapshots-repo + https://oss.sonatype.org/content/repositories/snapshots + + false + + + + always + true + + + + + + + snapshots-repo + https://oss.sonatype.org/content/repositories/snapshots + + false + + + + always + true + + + + + + + + maven-compiler-plugin + 3.1 + + ${java.version} + ${java.version} + + + + maven-surefire-plugin + 2.19 + + + org.junit + surefire-junit5 + ${junit.gen5.version} + + + + + + + + + org.junit + junit5-api + ${junit.gen5.version} + test + + + \ No newline at end of file diff --git a/junit5/src/test/java/com/baeldung/AssumptionTest.java b/junit5/src/test/java/com/baeldung/AssumptionTest.java new file mode 100644 index 0000000000..e4c2b56124 --- /dev/null +++ b/junit5/src/test/java/com/baeldung/AssumptionTest.java @@ -0,0 +1,30 @@ +package com.baeldung; + +import org.junit.gen5.api.Test; + +import static org.junit.gen5.api.Assertions.assertEquals; +import static org.junit.gen5.api.Assumptions.*; + +public class AssumptionTest { + + @Test + void trueAssumption() { + assumeTrue(5 > 1); + assertEquals(5 + 2, 7); + } + + @Test + void falseAssumption() { + assumeFalse(5 < 1); + assertEquals(5 + 2, 7); + } + + @Test + void assumptionThat() { + String someString = "Just a string"; + assumingThat( + someString.equals("Just a string"), + () -> assertEquals(2 + 2, 4) + ); + } +} diff --git a/junit5/src/test/java/com/baeldung/ExceptionTest.java b/junit5/src/test/java/com/baeldung/ExceptionTest.java new file mode 100644 index 0000000000..5c30ad5b44 --- /dev/null +++ b/junit5/src/test/java/com/baeldung/ExceptionTest.java @@ -0,0 +1,18 @@ +package com.baeldung; + +import org.junit.gen5.api.Test; + +import static org.junit.gen5.api.Assertions.assertEquals; +import static org.junit.gen5.api.Assertions.expectThrows; + +public class ExceptionTest { + + @Test + void shouldThrowException() { + Throwable exception = expectThrows(UnsupportedOperationException.class, + () -> { + throw new UnsupportedOperationException("Not supported"); + }); + assertEquals(exception.getMessage(), "Not supported"); + } +} diff --git a/junit5/src/test/java/com/baeldung/FirstTest.java b/junit5/src/test/java/com/baeldung/FirstTest.java new file mode 100644 index 0000000000..4fc6037174 --- /dev/null +++ b/junit5/src/test/java/com/baeldung/FirstTest.java @@ -0,0 +1,37 @@ +package com.baeldung; + +import org.junit.gen5.api.Disabled; +import org.junit.gen5.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.gen5.api.Assertions.*; + +class FirstTest { + + @Test + void lambdaExpressions() { + List numbers = Arrays.asList(1, 2, 3); + assertTrue(numbers + .stream() + .mapToInt(i -> i) + .sum() > 5, "Sum should be greater than 5"); + } + + @Test + void groupAssertions() { + int[] numbers = {0, 1, 2, 3, 4}; + assertAll("numbers", + () -> assertEquals(numbers[0], 1), + () -> assertEquals(numbers[3], 3), + () -> assertEquals(numbers[4], 1) + ); + } + + @Test + @Disabled + void disabledTest() { + assertTrue(false); + } +} diff --git a/junit5/src/test/java/com/baeldung/NestedTest.java b/junit5/src/test/java/com/baeldung/NestedTest.java new file mode 100644 index 0000000000..3fbe4f8644 --- /dev/null +++ b/junit5/src/test/java/com/baeldung/NestedTest.java @@ -0,0 +1,77 @@ +package com.baeldung; + +import org.junit.gen5.api.*; + +import java.util.EmptyStackException; +import java.util.Stack; + +public class NestedTest { + Stack stack; + boolean isRun = false; + + @Test + @DisplayName("is instantiated with new Stack()") + void isInstantiatedWithNew() { + new Stack(); + } + + @Nested + @DisplayName("when new") + class WhenNew { + + @BeforeEach + void init() { + stack = new Stack(); + } + + @Test + @DisplayName("is empty") + void isEmpty() { + Assertions.assertTrue(stack.isEmpty()); + } + + @Test + @DisplayName("throws EmptyStackException when popped") + void throwsExceptionWhenPopped() { + Assertions.expectThrows(EmptyStackException.class, () -> stack.pop()); + } + + @Test + @DisplayName("throws EmptyStackException when peeked") + void throwsExceptionWhenPeeked() { + Assertions.expectThrows(EmptyStackException.class, () -> stack.peek()); + } + + @Nested + @DisplayName("after pushing an element") + class AfterPushing { + + String anElement = "an element"; + + @BeforeEach + void init() { + stack.push(anElement); + } + + @Test + @DisplayName("it is no longer empty") + void isEmpty() { + Assertions.assertFalse(stack.isEmpty()); + } + + @Test + @DisplayName("returns the element when popped and is empty") + void returnElementWhenPopped() { + Assertions.assertEquals(anElement, stack.pop()); + Assertions.assertTrue(stack.isEmpty()); + } + + @Test + @DisplayName("returns the element when peeked but remains not empty") + void returnElementWhenPeeked() { + Assertions.assertEquals(anElement, stack.peek()); + Assertions.assertFalse(stack.isEmpty()); + } + } + } +} diff --git a/junit5/src/test/java/com/baeldung/TaggedTest.java b/junit5/src/test/java/com/baeldung/TaggedTest.java new file mode 100644 index 0000000000..dbc82f4022 --- /dev/null +++ b/junit5/src/test/java/com/baeldung/TaggedTest.java @@ -0,0 +1,16 @@ +package com.baeldung; + +import org.junit.gen5.api.Tag; +import org.junit.gen5.api.Test; + +import static org.junit.gen5.api.Assertions.assertEquals; + +@Tag("Test case") +public class TaggedTest { + + @Test + @Tag("Method") + void testMethod() { + assertEquals(2 + 2, 4); + } +} diff --git a/lombok/pom.xml b/lombok/pom.xml new file mode 100644 index 0000000000..b816642165 --- /dev/null +++ b/lombok/pom.xml @@ -0,0 +1,151 @@ + + + + 4.0.0 + + lombok + + com.baeldung + lombok + 0.1-SNAPSHOT + + + + + org.projectlombok + lombok + + ${lombok.version} + provided + + + + org.hibernate.javax.persistence + hibernate-jpa-2.1-api + ${hibernate-jpa-2.1-api.version} + + + + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + + ch.qos.logback + logback-core + ${logback.version} + runtime + + + + + + junit + junit + ${junit.version} + test + + + + + + + lombok + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + + org.projectlombok + lombok-maven-plugin + ${delombok-maven-plugin.version} + + + delombok + generate-sources + + delombok + + + ${project.basedir}/src/main/java + false + + skip + + false + + + + + + + + + + + + UTF-8 + + 1.8 + 1.8 + + + 1.16.8 + + + 1.0.0.Final + + + 1.7.13 + 1.1.3 + + + 4.12 + + + 3.5.1 + 2.19.1 + + + ${lombok.version}.0 + + + diff --git a/lombok/src/main/java/com/baeldung/lombok/intro/ApiClientConfiguration.java b/lombok/src/main/java/com/baeldung/lombok/intro/ApiClientConfiguration.java new file mode 100644 index 0000000000..74cc929d32 --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/intro/ApiClientConfiguration.java @@ -0,0 +1,22 @@ +package com.baeldung.lombok.intro; + +import lombok.Builder; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; + +@Builder +@Slf4j +@Getter +public class ApiClientConfiguration { + + private String host; + private int port; + private boolean useHttps; + + private long connectTimeout; + private long readTimeout; + + private String username; + private String password; + +} diff --git a/lombok/src/main/java/com/baeldung/lombok/intro/ContactInformationSupport.java b/lombok/src/main/java/com/baeldung/lombok/intro/ContactInformationSupport.java new file mode 100644 index 0000000000..ea9c3d23f5 --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/intro/ContactInformationSupport.java @@ -0,0 +1,17 @@ +package com.baeldung.lombok.intro; + +import lombok.Data; + +@Data +public class ContactInformationSupport implements HasContactInformation { + + private String firstName; + private String lastName; + private String phoneNr; + + @Override + public String getFullName() { + return getFirstName() + " " + getLastName(); + } + +} diff --git a/lombok/src/main/java/com/baeldung/lombok/intro/HasContactInformation.java b/lombok/src/main/java/com/baeldung/lombok/intro/HasContactInformation.java new file mode 100644 index 0000000000..9128df5fb0 --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/intro/HasContactInformation.java @@ -0,0 +1,16 @@ +package com.baeldung.lombok.intro; + +public interface HasContactInformation { + + String getFirstName(); + void setFirstName(String firstName); + + String getFullName(); + + String getLastName(); + void setLastName(String lastName); + + String getPhoneNr(); + void setPhoneNr(String phoneNr); + +} diff --git a/lombok/src/main/java/com/baeldung/lombok/intro/LoginResult.java b/lombok/src/main/java/com/baeldung/lombok/intro/LoginResult.java new file mode 100644 index 0000000000..6e75696612 --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/intro/LoginResult.java @@ -0,0 +1,25 @@ +package com.baeldung.lombok.intro; + +import java.net.URL; +import java.time.Duration; +import java.time.Instant; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import lombok.experimental.Accessors; + +@RequiredArgsConstructor +@Accessors(fluent = true) @Getter +@EqualsAndHashCode(of = {"authToken"}) +public class LoginResult { + + private final @NonNull Instant loginTs; + + private final @NonNull String authToken; + private final @NonNull Duration tokenValidity; + + private final @NonNull URL tokenRefreshUrl; + +} diff --git a/lombok/src/main/java/com/baeldung/lombok/intro/User.java b/lombok/src/main/java/com/baeldung/lombok/intro/User.java new file mode 100644 index 0000000000..d032d1f9e7 --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/intro/User.java @@ -0,0 +1,43 @@ +package com.baeldung.lombok.intro; + +import java.io.Serializable; +import java.util.List; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; +import lombok.experimental.Delegate; + +@Entity +@Getter @Setter @NoArgsConstructor // <--- THIS is it +@ToString(exclude = {"events"}) +public class User implements Serializable, HasContactInformation { + + private @Id @Setter(AccessLevel.PROTECTED) Long id; // will be set when persisting + + private String nickname; + + // Whichever other User-specific attributes + + @Delegate(types = {HasContactInformation.class}) + private final ContactInformationSupport contactInformation = new ContactInformationSupport(); + + // User itelf will implement all contact information by delegation + + @OneToMany(mappedBy = "user") + private List events; + + public User(String nickname, String firstName, String lastName, String phoneNr) { + this.nickname = nickname; + contactInformation.setFirstName(firstName); + contactInformation.setLastName(lastName); + contactInformation.setPhoneNr(phoneNr); + } + +} diff --git a/lombok/src/main/java/com/baeldung/lombok/intro/UserEvent.java b/lombok/src/main/java/com/baeldung/lombok/intro/UserEvent.java new file mode 100644 index 0000000000..6a13608664 --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/intro/UserEvent.java @@ -0,0 +1,29 @@ +package com.baeldung.lombok.intro; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.ManyToOne; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Entity +@NoArgsConstructor @Getter @Setter +public class UserEvent implements Serializable { + + // This class is just for sample purposes. + + private @Id @Setter(AccessLevel.PROTECTED) Long id; + + @ManyToOne + private User user; + + public UserEvent(User user) { + this.user = user; + } + +} diff --git a/lombok/src/test/java/com/baeldung/lombok/intro/ApiClientConfigurationTest.java b/lombok/src/test/java/com/baeldung/lombok/intro/ApiClientConfigurationTest.java new file mode 100644 index 0000000000..8283fc655e --- /dev/null +++ b/lombok/src/test/java/com/baeldung/lombok/intro/ApiClientConfigurationTest.java @@ -0,0 +1,43 @@ +package com.baeldung.lombok.intro; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + +import com.baeldung.lombok.intro.ApiClientConfiguration.ApiClientConfigurationBuilder; +import org.junit.Assert; +import org.junit.Test; + +public class ApiClientConfigurationTest { + + @Test + public void givenAnnotatedConfiguration_thenCanBeBuiltViaBuilder() { + ApiClientConfiguration config = + new ApiClientConfigurationBuilder() + .host("api.server.com") + .port(443) + .useHttps(true) + .connectTimeout(15_000L) + .readTimeout(5_000L) + .username("myusername") + .password("secret") + .build(); + + Assert.assertEquals(config.getHost(), "api.server.com"); + Assert.assertEquals(config.getPort(), 443); + Assert.assertEquals(config.isUseHttps(), true); + Assert.assertEquals(config.getConnectTimeout(), 15_000L); + Assert.assertEquals(config.getReadTimeout(), 5_000L); + Assert.assertEquals(config.getUsername(), "myusername"); + Assert.assertEquals(config.getPassword(), "secret"); + } + + @Test + public void givenAnnotatedConfiguration_thenHasLoggerInstance() throws NoSuchFieldException { + Field loggerInstance = ApiClientConfiguration.class.getDeclaredField("log"); + int modifiers = loggerInstance.getModifiers(); + Assert.assertTrue(Modifier.isPrivate(modifiers)); + Assert.assertTrue(Modifier.isStatic(modifiers)); + Assert.assertTrue(Modifier.isFinal(modifiers)); + } + +} diff --git a/lombok/src/test/java/com/baeldung/lombok/intro/LoginResultTest.java b/lombok/src/test/java/com/baeldung/lombok/intro/LoginResultTest.java new file mode 100644 index 0000000000..56878e4a03 --- /dev/null +++ b/lombok/src/test/java/com/baeldung/lombok/intro/LoginResultTest.java @@ -0,0 +1,59 @@ +package com.baeldung.lombok.intro; + +import java.net.MalformedURLException; +import java.net.URL; +import java.time.Duration; +import java.time.Instant; + +import org.junit.Assert; +import org.junit.Test; + +public class LoginResultTest { + + @Test + public void givenAnnotatedLoginResult_thenHasConstructorForAllFinalFields() + throws MalformedURLException { + /* LoginResult loginResult = */ new LoginResult( + Instant.now(), + "apitoken", + Duration.ofHours(1), + new URL("https://api.product.com/token-refresh")); + } + + @Test + public void givenAnnotatedLoginResult_thenHasFluentGetters() + throws MalformedURLException { + Instant loginTs = Instant.now(); + LoginResult loginResult = new LoginResult( + loginTs, + "apitoken", + Duration.ofHours(1), + new URL("https://api.product.com/token-refresh")); + + Assert.assertEquals(loginResult.loginTs(), loginTs); + Assert.assertEquals(loginResult.authToken(), "apitoken"); + Assert.assertEquals(loginResult.tokenValidity(), Duration.ofHours(1)); + Assert.assertEquals(loginResult.tokenRefreshUrl(), new URL("https://api.product.com/token-refresh")); + } + + @Test + public void givenAnnotatedLoginResult_whenSameApiToken_thenEqualInstances() + throws MalformedURLException { + String theSameApiToken = "testapitoken"; + + LoginResult loginResult1 = new LoginResult( + Instant.now(), + theSameApiToken, + Duration.ofHours(1), + new URL("https://api.product.com/token-refresh")); + + LoginResult loginResult2 = new LoginResult( + Instant.now(), + theSameApiToken, + Duration.ofHours(2), + new URL("https://api.product.com/token-refresh-alt")); + + Assert.assertEquals(loginResult1, loginResult2); + } + +} diff --git a/lombok/src/test/java/com/baeldung/lombok/intro/UserTest.java b/lombok/src/test/java/com/baeldung/lombok/intro/UserTest.java new file mode 100644 index 0000000000..b3bf21478f --- /dev/null +++ b/lombok/src/test/java/com/baeldung/lombok/intro/UserTest.java @@ -0,0 +1,73 @@ +package com.baeldung.lombok.intro; + +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.Arrays; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; + +public class UserTest { + + @Test + public void givenAnnotatedUser_thenHasEmptyConstructor() { + /* User user = */ new User(); + } + + @Test + public void givenAnnotatedUser_thenHasGettersAndSetters() { + User user = new User("testnickname", "Test", "JUnit", "123456"); + + Assert.assertEquals("testnickname", user.getNickname()); + Assert.assertEquals("Test", user.getFirstName()); + Assert.assertEquals("JUnit", user.getLastName()); + Assert.assertEquals("123456", user.getPhoneNr()); + + user.setNickname("testnickname2"); + user.setFirstName("Test2"); + user.setLastName("JUnit2"); + user.setPhoneNr("654321"); + + Assert.assertEquals("testnickname2", user.getNickname()); + Assert.assertEquals("Test2", user.getFirstName()); + Assert.assertEquals("JUnit2", user.getLastName()); + Assert.assertEquals("654321", user.getPhoneNr()); + } + + @Test + public void givenAnnotatedUser_thenHasProtectedSetId() throws NoSuchMethodException { + Method setIdMethod = User.class.getDeclaredMethod("setId", Long.class); + int modifiers = setIdMethod.getModifiers(); + Assert.assertTrue(Modifier.isProtected(modifiers)); + } + + @Test + public void givenAnnotatedUser_thenImplementsHasContactInformation() { + User user = new User("testnickname3", "Test3", "JUnit3", "987654"); + Assert.assertTrue(user instanceof HasContactInformation); + + Assert.assertEquals("Test3", user.getFirstName()); + Assert.assertEquals("JUnit3", user.getLastName()); + Assert.assertEquals("987654", user.getPhoneNr()); + Assert.assertEquals("Test3 JUnit3", user.getFullName()); + + user.setFirstName("Test4"); + user.setLastName("JUnit4"); + user.setPhoneNr("456789"); + + Assert.assertEquals("Test4", user.getFirstName()); + Assert.assertEquals("JUnit4", user.getLastName()); + Assert.assertEquals("456789", user.getPhoneNr()); + Assert.assertEquals("Test4 JUnit4", user.getFullName()); + } + + @Test + public void givenAnnotatedUser_whenHasEvents_thenToStringDumpsNoEvents() { + User user = new User("testnickname", "Test", "JUnit", "123456"); + List events = Arrays.asList(new UserEvent(user), new UserEvent(user)); + user.setEvents(events); + Assert.assertFalse(user.toString().contains("events")); + } + +} diff --git a/mock-comparisons/README.md b/mock-comparisons/README.md new file mode 100644 index 0000000000..7795487b77 --- /dev/null +++ b/mock-comparisons/README.md @@ -0,0 +1,7 @@ +========= + +## Mock comparison realated tutorials + + +### Relevant Articles: +- [Mockito vs EasyMock vs JMockit](http://www.baeldung.com/mockito-vs-easymock-vs-jmockit) diff --git a/mock-comparisons/pom.xml b/mock-comparisons/pom.xml new file mode 100644 index 0000000000..c5424d262d --- /dev/null +++ b/mock-comparisons/pom.xml @@ -0,0 +1,86 @@ + + 4.0.0 + org.baeldung + mock-comparisons + 0.1-SNAPSHOT + + mockito + + + 4.12 + 1.10.19 + 3.4 + 1.24 + + UTF-8 + + + 3.3 + 2.18.1 + + + + + junit + junit + ${junit.version} + test + + + + org.mockito + mockito-core + ${mockito.version} + test + + + + org.easymock + easymock + ${easymock.version} + test + + + + org.jmockit + jmockit + ${jmockit.version} + test + + + + + + mock-comparisons + + + src/main/resources + true + + + + + + + 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} + + + + + + + + \ No newline at end of file diff --git a/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/LoginController.java b/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/LoginController.java new file mode 100644 index 0000000000..914b0034d2 --- /dev/null +++ b/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/LoginController.java @@ -0,0 +1,29 @@ +package org.baeldung.mocks.testCase; + +public class LoginController { + + public LoginService loginService; + + public String login(UserForm userForm) { + if (null == userForm) { + return "ERROR"; + } else { + boolean logged; + + try { + logged = loginService.login(userForm); + } catch (Exception e) { + return "ERROR"; + } + + if (logged) { + loginService.setCurrentUser(userForm.getUsername()); + return "OK"; + } else { + return "KO"; + } + } + } + + // standard setters and getters +} diff --git a/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/LoginDao.java b/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/LoginDao.java new file mode 100644 index 0000000000..2cbff6c9d4 --- /dev/null +++ b/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/LoginDao.java @@ -0,0 +1,9 @@ +package org.baeldung.mocks.testCase; + +public class LoginDao { + + public int login(UserForm userForm) { + //actual call to a third party library + return 0; + } +} diff --git a/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/LoginService.java b/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/LoginService.java new file mode 100644 index 0000000000..d6a31a8047 --- /dev/null +++ b/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/LoginService.java @@ -0,0 +1,33 @@ +package org.baeldung.mocks.testCase; + +public class LoginService { + + private LoginDao loginDao; + + private String currentUser; + + public boolean login(UserForm userForm) { + assert null != userForm; + + int loginResults = loginDao.login(userForm); + + switch (loginResults) { + case 1: + return true; + default: + return false; + } + } + + public void setCurrentUser(String username) { + if (null != username) { + this.currentUser = username; + } + } + + public void setLoginDao(LoginDao loginDao) { + this.loginDao = loginDao; + } + + // standard setters and getters +} diff --git a/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/UserForm.java b/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/UserForm.java new file mode 100644 index 0000000000..14136d0f31 --- /dev/null +++ b/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/UserForm.java @@ -0,0 +1,15 @@ +package org.baeldung.mocks.testCase; + +public class UserForm { + + // public access modifiers as only for testing + + public String password; + + public String username; + + public String getUsername() { + return username; + } + +} diff --git a/mock-comparisons/src/test/java/org/baeldung/mocks/easymock/LoginControllerTest.java b/mock-comparisons/src/test/java/org/baeldung/mocks/easymock/LoginControllerTest.java new file mode 100644 index 0000000000..25d2b91ede --- /dev/null +++ b/mock-comparisons/src/test/java/org/baeldung/mocks/easymock/LoginControllerTest.java @@ -0,0 +1,143 @@ +package org.baeldung.mocks.easymock; + +import org.baeldung.mocks.testCase.LoginController; +import org.baeldung.mocks.testCase.LoginDao; +import org.baeldung.mocks.testCase.LoginService; +import org.baeldung.mocks.testCase.UserForm; +import org.easymock.*; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(EasyMockRunner.class) +public class LoginControllerTest { + + @Mock + private LoginDao loginDao; + + @Mock + private LoginService loginService; + + @TestSubject + private LoginController loginController = new LoginController(); + + @Test + public void assertThatNoMethodHasBeenCalled() { + EasyMock.replay(loginService); + loginController.login(null); + + // no method called + EasyMock.verify(loginService); + } + + @Test + public void assertTwoMethodsHaveBeenCalled() { + UserForm userForm = new UserForm(); + userForm.username = "foo"; + EasyMock.expect(loginService.login(userForm)).andReturn(true); + loginService.setCurrentUser("foo"); + EasyMock.replay(loginService); + + String login = loginController.login(userForm); + + Assert.assertEquals("OK", login); + EasyMock.verify(loginService); + } + + @Test + public void assertOnlyOneMethodHasBeenCalled() { + UserForm userForm = new UserForm(); + userForm.username = "foo"; + EasyMock.expect(loginService.login(userForm)).andReturn(false); + EasyMock.replay(loginService); + + String login = loginController.login(userForm); + + Assert.assertEquals("KO", login); + EasyMock.verify(loginService); + } + + @Test + public void mockExceptionThrowing() { + UserForm userForm = new UserForm(); + EasyMock.expect(loginService.login(userForm)).andThrow(new IllegalArgumentException()); + EasyMock.replay(loginService); + + String login = loginController.login(userForm); + + Assert.assertEquals("ERROR", login); + EasyMock.verify(loginService); + } + + @Test + public void mockAnObjectToPassAround() { + UserForm userForm = EasyMock.mock(UserForm.class); + EasyMock.expect(userForm.getUsername()).andReturn("foo"); + EasyMock.expect(loginService.login(userForm)).andReturn(true); + loginService.setCurrentUser("foo"); + EasyMock.replay(userForm); + EasyMock.replay(loginService); + + String login = loginController.login(userForm); + + Assert.assertEquals("OK", login); + EasyMock.verify(userForm); + EasyMock.verify(loginService); + } + + @Test + public void argumentMatching() { + UserForm userForm = new UserForm(); + userForm.username = "foo"; + // default matcher + EasyMock.expect(loginService.login(EasyMock.isA(UserForm.class))).andReturn(true); + // complex matcher + loginService.setCurrentUser(specificArgumentMatching("foo")); + EasyMock.replay(loginService); + + String login = loginController.login(userForm); + + Assert.assertEquals("OK", login); + EasyMock.verify(loginService); + } + + private static String specificArgumentMatching(final String expected) { + EasyMock.reportMatcher(new IArgumentMatcher() { + @Override + public boolean matches(Object argument) { + return argument instanceof String && ((String) argument).startsWith(expected); + } + + @Override + public void appendTo(StringBuffer buffer) { + //NOOP + } + }); + return null; + } + + @Test + public void partialMocking() { + UserForm userForm = new UserForm(); + userForm.username = "foo"; + // use partial mock + LoginService loginServicePartial = EasyMock.partialMockBuilder(LoginService.class). + addMockedMethod("setCurrentUser").createMock(); + loginServicePartial.setCurrentUser("foo"); + // let service's login use implementation so let's mock DAO call + EasyMock.expect(loginDao.login(userForm)).andReturn(1); + + loginServicePartial.setLoginDao(loginDao); + loginController.loginService = loginServicePartial; + + EasyMock.replay(loginDao); + EasyMock.replay(loginServicePartial); + + String login = loginController.login(userForm); + + Assert.assertEquals("OK", login); + // verify mocked call + EasyMock.verify(loginServicePartial); + EasyMock.verify(loginDao); + } +} diff --git a/mock-comparisons/src/test/java/org/baeldung/mocks/jmockit/LoginControllerTest.java b/mock-comparisons/src/test/java/org/baeldung/mocks/jmockit/LoginControllerTest.java new file mode 100644 index 0000000000..621342fed2 --- /dev/null +++ b/mock-comparisons/src/test/java/org/baeldung/mocks/jmockit/LoginControllerTest.java @@ -0,0 +1,159 @@ +package org.baeldung.mocks.jmockit; + +import mockit.*; +import mockit.integration.junit4.JMockit; +import org.baeldung.mocks.testCase.LoginController; +import org.baeldung.mocks.testCase.LoginDao; +import org.baeldung.mocks.testCase.LoginService; +import org.baeldung.mocks.testCase.UserForm; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(JMockit.class) +public class LoginControllerTest { + + @Injectable + private LoginDao loginDao; + + @Injectable + private LoginService loginService; + + @Tested + private LoginController loginController; + + @Test + public void assertThatNoMethodHasBeenCalled() { + loginController.login(null); + // no method called + new FullVerifications(loginService) { + }; + } + + @Test + public void assertTwoMethodsHaveBeenCalled() { + final UserForm userForm = new UserForm(); + userForm.username = "foo"; + new Expectations() {{ + loginService.login(userForm); + result = true; + loginService.setCurrentUser("foo"); + }}; + + String login = loginController.login(userForm); + + Assert.assertEquals("OK", login); + new FullVerifications(loginService) { + }; + } + + @Test + public void assertOnlyOneMethodHasBeenCalled() { + final UserForm userForm = new UserForm(); + userForm.username = "foo"; + new Expectations() {{ + loginService.login(userForm); + result = false; + // no expectation for setCurrentUser + }}; + + String login = loginController.login(userForm); + + Assert.assertEquals("KO", login); + new FullVerifications(loginService) { + }; + } + + @Test + public void mockExceptionThrowing() { + final UserForm userForm = new UserForm(); + new Expectations() {{ + loginService.login(userForm); + result = new IllegalArgumentException(); + // no expectation for setCurrentUser + }}; + + String login = loginController.login(userForm); + + Assert.assertEquals("ERROR", login); + new FullVerifications(loginService) { + }; + } + + @Test + public void mockAnObjectToPassAround(@Mocked final UserForm userForm) { + new Expectations() {{ + userForm.getUsername(); + result = "foo"; + loginService.login(userForm); + result = true; + loginService.setCurrentUser("foo"); + }}; + + String login = loginController.login(userForm); + + Assert.assertEquals("OK", login); + new FullVerifications(loginService) { + }; + new FullVerifications(userForm) { + }; + } + + @Test + public void argumentMatching() { + final UserForm userForm = new UserForm(); + userForm.username = "foo"; + // default matcher + new Expectations() {{ + loginService.login((UserForm) any); + result = true; + // complex matcher + loginService.setCurrentUser(withArgThat(new BaseMatcher() { + @Override + public boolean matches(Object item) { + return item instanceof String && ((String) item).startsWith("foo"); + } + + @Override + public void describeTo(Description description) { + //NOOP + } + })); + }}; + + String login = loginController.login(userForm); + + Assert.assertEquals("OK", login); + new FullVerifications(loginService) { + }; + } + + @Test + public void partialMocking() { + // use partial mock + final LoginService partialLoginService = new LoginService(); + partialLoginService.setLoginDao(loginDao); + loginController.loginService = partialLoginService; + + final UserForm userForm = new UserForm(); + userForm.username = "foo"; + // let service's login use implementation so let's mock DAO call + new Expectations() {{ + loginDao.login(userForm); + result = 1; + // no expectation for loginService.login + partialLoginService.setCurrentUser("foo"); + }}; + + String login = loginController.login(userForm); + + Assert.assertEquals("OK", login); + // verify mocked call + new FullVerifications(partialLoginService) { + }; + new FullVerifications(loginDao) { + }; + } +} diff --git a/mock-comparisons/src/test/java/org/baeldung/mocks/mockito/LoginControllerTest.java b/mock-comparisons/src/test/java/org/baeldung/mocks/mockito/LoginControllerTest.java new file mode 100644 index 0000000000..59b28a2cb4 --- /dev/null +++ b/mock-comparisons/src/test/java/org/baeldung/mocks/mockito/LoginControllerTest.java @@ -0,0 +1,125 @@ +package org.baeldung.mocks.mockito; + +import org.baeldung.mocks.testCase.LoginController; +import org.baeldung.mocks.testCase.LoginDao; +import org.baeldung.mocks.testCase.LoginService; +import org.baeldung.mocks.testCase.UserForm; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.*; + +public class LoginControllerTest { + + @Mock + private LoginDao loginDao; + + @Spy + @InjectMocks + private LoginService spiedLoginService; + + @Mock + private LoginService loginService; + + @InjectMocks + private LoginController loginController; + + @Before + public void setUp() { + loginController = new LoginController(); + MockitoAnnotations.initMocks(this); + } + + @Test + public void assertThatNoMethodHasBeenCalled() { + loginController.login(null); + // no method called + Mockito.verifyZeroInteractions(loginService); + } + + @Test + public void assertTwoMethodsHaveBeenCalled() { + UserForm userForm = new UserForm(); + userForm.username = "foo"; + Mockito.when(loginService.login(userForm)).thenReturn(true); + + String login = loginController.login(userForm); + + Assert.assertEquals("OK", login); + Mockito.verify(loginService).login(userForm); + Mockito.verify(loginService).setCurrentUser("foo"); + } + + @Test + public void assertOnlyOneMethodHasBeenCalled() { + UserForm userForm = new UserForm(); + userForm.username = "foo"; + Mockito.when(loginService.login(userForm)).thenReturn(false); + + String login = loginController.login(userForm); + + Assert.assertEquals("KO", login); + Mockito.verify(loginService).login(userForm); + Mockito.verifyNoMoreInteractions(loginService); + } + + @Test + public void mockExceptionThrowing() { + UserForm userForm = new UserForm(); + Mockito.when(loginService.login(userForm)).thenThrow(IllegalArgumentException.class); + + String login = loginController.login(userForm); + + Assert.assertEquals("ERROR", login); + Mockito.verify(loginService).login(userForm); + Mockito.verifyZeroInteractions(loginService); + } + + @Test + public void mockAnObjectToPassAround() { + UserForm userForm = Mockito.when(Mockito.mock(UserForm.class).getUsername()).thenReturn("foo").getMock(); + Mockito.when(loginService.login(userForm)).thenReturn(true); + + String login = loginController.login(userForm); + + Assert.assertEquals("OK", login); + Mockito.verify(loginService).login(userForm); + Mockito.verify(loginService).setCurrentUser("foo"); + } + + @Test + public void argumentMatching() { + UserForm userForm = new UserForm(); + userForm.username = "foo"; + // default matcher + Mockito.when(loginService.login(Mockito.any(UserForm.class))).thenReturn(true); + + String login = loginController.login(userForm); + + Assert.assertEquals("OK", login); + Mockito.verify(loginService).login(userForm); + // complex matcher + Mockito.verify(loginService).setCurrentUser(Mockito.argThat(new ArgumentMatcher() { + @Override + public boolean matches(Object argument) { + return argument instanceof String && ((String) argument).startsWith("foo"); + } + })); + } + + @Test + public void partialMocking() { + // use partial mock + loginController.loginService = spiedLoginService; + UserForm userForm = new UserForm(); + userForm.username = "foo"; + // let service's login use implementation so let's mock DAO call + Mockito.when(loginDao.login(userForm)).thenReturn(1); + + String login = loginController.login(userForm); + + Assert.assertEquals("OK", login); + // verify mocked call + Mockito.verify(spiedLoginService).setCurrentUser("foo"); + } +} diff --git a/mockito/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch b/mockito/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch deleted file mode 100644 index 627021fb96..0000000000 --- a/mockito/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/mockito/.settings/.jsdtscope b/mockito/.settings/.jsdtscope deleted file mode 100644 index 7b3f0c8b9f..0000000000 --- a/mockito/.settings/.jsdtscope +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/mockito/.settings/org.eclipse.jdt.core.prefs b/mockito/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index b126d6476b..0000000000 --- a/mockito/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,95 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignore -org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull -org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault -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.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 -org.eclipse.jdt.core.compiler.problem.comparingIdentical=warning -org.eclipse.jdt.core.compiler.problem.deadCode=warning -org.eclipse.jdt.core.compiler.problem.deprecation=warning -org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled -org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled -org.eclipse.jdt.core.compiler.problem.discouragedReference=warning -org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore -org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore -org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled -org.eclipse.jdt.core.compiler.problem.fieldHiding=error -org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning -org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning -org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning -org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts=disabled -org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning -org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=ignore -org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore -org.eclipse.jdt.core.compiler.problem.localVariableHiding=error -org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning -org.eclipse.jdt.core.compiler.problem.missingDefaultCase=ignore -org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore -org.eclipse.jdt.core.compiler.problem.missingEnumCaseDespiteDefault=disabled -org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=ignore -org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=ignore -org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled -org.eclipse.jdt.core.compiler.problem.missingSerialVersion=warning -org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignore -org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning -org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning -org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore -org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=error -org.eclipse.jdt.core.compiler.problem.nullReference=warning -org.eclipse.jdt.core.compiler.problem.nullSpecViolation=error -org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=warning -org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning -org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore -org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=ignore -org.eclipse.jdt.core.compiler.problem.potentialNullReference=ignore -org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=ignore -org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning -org.eclipse.jdt.core.compiler.problem.redundantNullAnnotation=warning -org.eclipse.jdt.core.compiler.problem.redundantNullCheck=ignore -org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=ignore -org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=ignore -org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore -org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=ignore -org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled -org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning -org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled -org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled -org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore -org.eclipse.jdt.core.compiler.problem.typeParameterHiding=error -org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=enabled -org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning -org.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning -org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore -org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning -org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore -org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=ignore -org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=ignore -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=enabled -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled -org.eclipse.jdt.core.compiler.problem.unusedImport=warning -org.eclipse.jdt.core.compiler.problem.unusedLabel=warning -org.eclipse.jdt.core.compiler.problem.unusedLocal=warning -org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=ignore -org.eclipse.jdt.core.compiler.problem.unusedParameter=ignore -org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference=enabled -org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled -org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled -org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning -org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning -org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning -org.eclipse.jdt.core.compiler.source=1.8 diff --git a/mockito/.settings/org.eclipse.jdt.ui.prefs b/mockito/.settings/org.eclipse.jdt.ui.prefs deleted file mode 100644 index 471e9b0d81..0000000000 --- a/mockito/.settings/org.eclipse.jdt.ui.prefs +++ /dev/null @@ -1,55 +0,0 @@ -#Sat Jan 21 23:04:06 EET 2012 -eclipse.preferences.version=1 -editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true -sp_cleanup.add_default_serial_version_id=true -sp_cleanup.add_generated_serial_version_id=false -sp_cleanup.add_missing_annotations=true -sp_cleanup.add_missing_deprecated_annotations=true -sp_cleanup.add_missing_methods=false -sp_cleanup.add_missing_nls_tags=false -sp_cleanup.add_missing_override_annotations=true -sp_cleanup.add_missing_override_annotations_interface_methods=true -sp_cleanup.add_serial_version_id=false -sp_cleanup.always_use_blocks=true -sp_cleanup.always_use_parentheses_in_expressions=true -sp_cleanup.always_use_this_for_non_static_field_access=false -sp_cleanup.always_use_this_for_non_static_method_access=false -sp_cleanup.convert_to_enhanced_for_loop=true -sp_cleanup.correct_indentation=true -sp_cleanup.format_source_code=true -sp_cleanup.format_source_code_changes_only=true -sp_cleanup.make_local_variable_final=true -sp_cleanup.make_parameters_final=true -sp_cleanup.make_private_fields_final=false -sp_cleanup.make_type_abstract_if_missing_method=false -sp_cleanup.make_variable_declarations_final=true -sp_cleanup.never_use_blocks=false -sp_cleanup.never_use_parentheses_in_expressions=false -sp_cleanup.on_save_use_additional_actions=true -sp_cleanup.organize_imports=true -sp_cleanup.qualify_static_field_accesses_with_declaring_class=false -sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true -sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true -sp_cleanup.qualify_static_member_accesses_with_declaring_class=true -sp_cleanup.qualify_static_method_accesses_with_declaring_class=false -sp_cleanup.remove_private_constructors=true -sp_cleanup.remove_trailing_whitespaces=true -sp_cleanup.remove_trailing_whitespaces_all=true -sp_cleanup.remove_trailing_whitespaces_ignore_empty=false -sp_cleanup.remove_unnecessary_casts=true -sp_cleanup.remove_unnecessary_nls_tags=false -sp_cleanup.remove_unused_imports=true -sp_cleanup.remove_unused_local_variables=false -sp_cleanup.remove_unused_private_fields=true -sp_cleanup.remove_unused_private_members=false -sp_cleanup.remove_unused_private_methods=true -sp_cleanup.remove_unused_private_types=true -sp_cleanup.sort_members=false -sp_cleanup.sort_members_all=false -sp_cleanup.use_blocks=false -sp_cleanup.use_blocks_only_for_return_and_throw=false -sp_cleanup.use_parentheses_in_expressions=false -sp_cleanup.use_this_for_non_static_field_access=true -sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true -sp_cleanup.use_this_for_non_static_method_access=true -sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true diff --git a/mockito/.settings/org.eclipse.m2e.core.prefs b/mockito/.settings/org.eclipse.m2e.core.prefs deleted file mode 100644 index f897a7f1cb..0000000000 --- a/mockito/.settings/org.eclipse.m2e.core.prefs +++ /dev/null @@ -1,4 +0,0 @@ -activeProfiles= -eclipse.preferences.version=1 -resolveWorkspaceProjects=true -version=1 diff --git a/mockito/.settings/org.eclipse.m2e.wtp.prefs b/mockito/.settings/org.eclipse.m2e.wtp.prefs deleted file mode 100644 index ef86089622..0000000000 --- a/mockito/.settings/org.eclipse.m2e.wtp.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.m2e.wtp.enabledProjectSpecificPrefs=false diff --git a/mockito/.settings/org.eclipse.wst.common.component b/mockito/.settings/org.eclipse.wst.common.component deleted file mode 100644 index e98377cb0f..0000000000 --- a/mockito/.settings/org.eclipse.wst.common.component +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/mockito/.settings/org.eclipse.wst.common.project.facet.core.xml b/mockito/.settings/org.eclipse.wst.common.project.facet.core.xml deleted file mode 100644 index f4ef8aa0a5..0000000000 --- a/mockito/.settings/org.eclipse.wst.common.project.facet.core.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - diff --git a/mockito/.settings/org.eclipse.wst.jsdt.ui.superType.container b/mockito/.settings/org.eclipse.wst.jsdt.ui.superType.container deleted file mode 100644 index 3bd5d0a480..0000000000 --- a/mockito/.settings/org.eclipse.wst.jsdt.ui.superType.container +++ /dev/null @@ -1 +0,0 @@ -org.eclipse.wst.jsdt.launching.baseBrowserLibrary \ No newline at end of file diff --git a/mockito/.settings/org.eclipse.wst.jsdt.ui.superType.name b/mockito/.settings/org.eclipse.wst.jsdt.ui.superType.name deleted file mode 100644 index 05bd71b6ec..0000000000 --- a/mockito/.settings/org.eclipse.wst.jsdt.ui.superType.name +++ /dev/null @@ -1 +0,0 @@ -Window \ No newline at end of file diff --git a/mockito/.settings/org.eclipse.wst.validation.prefs b/mockito/.settings/org.eclipse.wst.validation.prefs deleted file mode 100644 index cacf5451ae..0000000000 --- a/mockito/.settings/org.eclipse.wst.validation.prefs +++ /dev/null @@ -1,14 +0,0 @@ -DELEGATES_PREFERENCE=delegateValidatorList -USER_BUILD_PREFERENCE=enabledBuildValidatorListorg.eclipse.jst.j2ee.internal.classpathdep.ClasspathDependencyValidator; -USER_MANUAL_PREFERENCE=enabledManualValidatorListorg.eclipse.jst.j2ee.internal.classpathdep.ClasspathDependencyValidator; -USER_PREFERENCE=overrideGlobalPreferencestruedisableAllValidationfalseversion1.2.303.v201202090300 -eclipse.preferences.version=1 -override=true -suspend=false -vals/org.eclipse.jst.jsf.ui.JSFAppConfigValidator/global=FF01 -vals/org.eclipse.jst.jsp.core.JSPBatchValidator/global=FF01 -vals/org.eclipse.jst.jsp.core.JSPContentValidator/global=FF01 -vals/org.eclipse.jst.jsp.core.TLDValidator/global=FF01 -vals/org.eclipse.wst.dtd.core.dtdDTDValidator/global=FF01 -vals/org.eclipse.wst.jsdt.web.core.JsBatchValidator/global=TF02 -vf.version=3 diff --git a/mockito/.settings/org.eclipse.wst.ws.service.policy.prefs b/mockito/.settings/org.eclipse.wst.ws.service.policy.prefs deleted file mode 100644 index 9cfcabe16f..0000000000 --- a/mockito/.settings/org.eclipse.wst.ws.service.policy.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.wst.ws.service.policy.projectEnabled=false diff --git a/mockito/.springBeans b/mockito/.springBeans deleted file mode 100644 index a79097f40d..0000000000 --- a/mockito/.springBeans +++ /dev/null @@ -1,14 +0,0 @@ - - - 1 - - - - - - - src/main/webapp/WEB-INF/api-servlet.xml - - - - diff --git a/mockito/README.md b/mockito/README.md index 5ecc5722b0..5e7cd19f78 100644 --- a/mockito/README.md +++ b/mockito/README.md @@ -6,4 +6,5 @@ ### Relevant Articles: - [Mockito Verify Cookbook](http://www.baeldung.com/mockito-verify) - [Mockito When/Then Cookbook](http://www.baeldung.com/mockito-behavior) - +- [Mockito – Using Spies](http://www.baeldung.com/mockito-spy) +- [Mockito – @Mock, @Spy, @Captor and @InjectMocks](http://www.baeldung.com/mockito-annotations) diff --git a/mockito/pom.xml b/mockito/pom.xml index a97a017c5c..16c7cb0dd8 100644 --- a/mockito/pom.xml +++ b/mockito/pom.xml @@ -1,128 +1,143 @@ - - 4.0.0 - org.baeldung - mockito - 0.1-SNAPSHOT + + 4.0.0 + org.baeldung + mockito + 0.1-SNAPSHOT - mockito + mockito - + - + - - com.google.guava - guava - ${guava.version} - + + com.google.guava + guava + ${guava.version} + - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + - + - + - - junit - junit-dep - ${junit.version} - test - + + junit + junit + ${junit.version} + test + - - org.hamcrest - hamcrest-core - ${org.hamcrest.version} - test - - - org.hamcrest - hamcrest-library - ${org.hamcrest.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.mockito + mockito-core + ${mockito.version} + test + - + + org.powermock + powermock-module-junit4 + ${powermock.version} + test + + + org.powermock + powermock-api-mockito + ${powermock.version} + test + - - mockito - - - src/main/resources - true - - + - + + mockito + + + src/main/resources + true + + - - 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} - + + 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.1.5.RELEASE - 3.2.5.RELEASE + - - 4.3.10.Final - 5.1.35 + + + 4.1.5.RELEASE + 3.2.5.RELEASE - - 1.7.12 - 1.1.3 + + 4.3.10.Final + 5.1.35 - - 5.1.3.Final + + 1.7.13 + 1.1.3 - - 18.0 - 3.4 + + 5.1.3.Final - - 1.3 - 4.11 - 1.10.19 + + 19.0 + 3.4 - 4.4.1 - 4.5 + + 1.3 + 4.12 + 1.10.19 + 1.6.4 - 2.4.1 + 4.4.1 + 4.5 - - 3.3 - 2.6 - 2.18.1 - 2.7 - 1.4.14 + 2.4.1 - + + 3.3 + 2.6 + 2.18.1 + 2.7 + 1.4.14 + + \ No newline at end of file diff --git a/mockito/src/main/webapp/WEB-INF/api-servlet.xml b/mockito/src/main/webapp/WEB-INF/api-servlet.xml deleted file mode 100644 index 4c74be8912..0000000000 --- a/mockito/src/main/webapp/WEB-INF/api-servlet.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/mockito/src/main/webapp/WEB-INF/web.xml b/mockito/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index 935beae648..0000000000 --- a/mockito/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - - Spring MVC Application - - - - contextClass - - org.springframework.web.context.support.AnnotationConfigWebApplicationContext - - - - contextConfigLocation - org.baeldung.config - - - - org.springframework.web.context.ContextLoaderListener - - - - - api - org.springframework.web.servlet.DispatcherServlet - 1 - - - api - / - - - - - - - \ No newline at end of file diff --git a/mockito/src/test/java/com/baeldung/powermockito/introduction/CollaboratorForPartialMocking.java b/mockito/src/test/java/com/baeldung/powermockito/introduction/CollaboratorForPartialMocking.java new file mode 100644 index 0000000000..771444f13d --- /dev/null +++ b/mockito/src/test/java/com/baeldung/powermockito/introduction/CollaboratorForPartialMocking.java @@ -0,0 +1,20 @@ +package com.baeldung.powermockito.introduction; + +public class CollaboratorForPartialMocking { + + public static String staticMethod() { + return "Hello Baeldung!"; + } + + public final String finalMethod() { + return "Hello Baeldung!"; + } + + private String privateMethod() { + return "Hello Baeldung!"; + } + + public String privateMethodCaller() { + return privateMethod() + " Welcome to the Java world."; + } +} \ No newline at end of file diff --git a/mockito/src/test/java/com/baeldung/powermockito/introduction/CollaboratorWithFinalMethods.java b/mockito/src/test/java/com/baeldung/powermockito/introduction/CollaboratorWithFinalMethods.java new file mode 100644 index 0000000000..8287454782 --- /dev/null +++ b/mockito/src/test/java/com/baeldung/powermockito/introduction/CollaboratorWithFinalMethods.java @@ -0,0 +1,9 @@ +package com.baeldung.powermockito.introduction; + +public class CollaboratorWithFinalMethods { + + public final String helloMethod() { + return "Hello World!"; + } + +} diff --git a/mockito/src/test/java/com/baeldung/powermockito/introduction/CollaboratorWithStaticMethods.java b/mockito/src/test/java/com/baeldung/powermockito/introduction/CollaboratorWithStaticMethods.java new file mode 100644 index 0000000000..2795ae97f1 --- /dev/null +++ b/mockito/src/test/java/com/baeldung/powermockito/introduction/CollaboratorWithStaticMethods.java @@ -0,0 +1,16 @@ +package com.baeldung.powermockito.introduction; + +public class CollaboratorWithStaticMethods { + + public static String firstMethod(String name) { + return "Hello " + name + " !"; + } + + public static String secondMethod() { + return "Hello no one!"; + } + + public static String thirdMethod() { + return "Hello no one again!"; + } +} \ No newline at end of file diff --git a/mockito/src/test/java/com/baeldung/powermockito/introduction/PowerMockitoTest.java b/mockito/src/test/java/com/baeldung/powermockito/introduction/PowerMockitoTest.java new file mode 100644 index 0000000000..6f12f3e459 --- /dev/null +++ b/mockito/src/test/java/com/baeldung/powermockito/introduction/PowerMockitoTest.java @@ -0,0 +1,78 @@ +package com.baeldung.powermockito.introduction; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import static org.junit.Assert.assertEquals; +import static org.powermock.api.mockito.PowerMockito.*; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(fullyQualifiedNames = "com.baeldung.powermockito.introduction.*") +public class PowerMockitoTest { + + @Test + public void givenFinalMethods_whenUsingPowerMockito_thenCorrect() throws Exception { + CollaboratorWithFinalMethods mock = mock(CollaboratorWithFinalMethods.class); + whenNew(CollaboratorWithFinalMethods.class).withNoArguments().thenReturn(mock); + + CollaboratorWithFinalMethods collaborator = new CollaboratorWithFinalMethods(); + verifyNew(CollaboratorWithFinalMethods.class).withNoArguments(); + + when(collaborator.helloMethod()).thenReturn("Hello Baeldung!"); + String welcome = collaborator.helloMethod(); + Mockito.verify(collaborator).helloMethod(); + assertEquals("Hello Baeldung!", welcome); + } + + @Test(expected = RuntimeException.class) + public void givenStaticMethods_whenUsingPowerMockito_thenCorrect() { + mockStatic(CollaboratorWithStaticMethods.class); + + when(CollaboratorWithStaticMethods.firstMethod(Mockito.anyString())).thenReturn("Hello Baeldung!"); + when(CollaboratorWithStaticMethods.secondMethod()).thenReturn("Nothing special"); + doThrow(new RuntimeException()).when(CollaboratorWithStaticMethods.class); + CollaboratorWithStaticMethods.thirdMethod(); + + String firstWelcome = CollaboratorWithStaticMethods.firstMethod("Whoever"); + String secondWelcome = CollaboratorWithStaticMethods.firstMethod("Whatever"); + + assertEquals("Hello Baeldung!", firstWelcome); + assertEquals("Hello Baeldung!", secondWelcome); + + verifyStatic(Mockito.times(2)); + CollaboratorWithStaticMethods.firstMethod(Mockito.anyString()); + + verifyStatic(Mockito.never()); + CollaboratorWithStaticMethods.secondMethod(); + + CollaboratorWithStaticMethods.thirdMethod(); + } + + @Test + public void givenPartialMocking_whenUsingPowerMockito_thenCorrect() throws Exception { + String returnValue; + + spy(CollaboratorForPartialMocking.class); + when(CollaboratorForPartialMocking.staticMethod()).thenReturn("I am a static mock method."); + returnValue = CollaboratorForPartialMocking.staticMethod(); + verifyStatic(); + CollaboratorForPartialMocking.staticMethod(); + assertEquals("I am a static mock method.", returnValue); + + CollaboratorForPartialMocking collaborator = new CollaboratorForPartialMocking(); + CollaboratorForPartialMocking mock = spy(collaborator); + + when(mock.finalMethod()).thenReturn("I am a final mock method."); + returnValue = mock.finalMethod(); + Mockito.verify(mock).finalMethod(); + assertEquals("I am a final mock method.", returnValue); + + when(mock, "privateMethod").thenReturn("I am a private mock method."); + returnValue = mock.privateMethodCaller(); + verifyPrivate(mock).invoke("privateMethod"); + assertEquals("I am a private mock method. Welcome to the Java world.", returnValue); + } +} \ No newline at end of file diff --git a/mockito/src/test/java/org/baeldung/mockito/MockitoAnnotationTest.java b/mockito/src/test/java/org/baeldung/mockito/MockitoAnnotationTest.java index 3acc2c937b..ec68c38f70 100644 --- a/mockito/src/test/java/org/baeldung/mockito/MockitoAnnotationTest.java +++ b/mockito/src/test/java/org/baeldung/mockito/MockitoAnnotationTest.java @@ -1,20 +1,14 @@ package org.baeldung.mockito; -import static org.junit.Assert.assertEquals; +import org.junit.Before; +import org.junit.Test; +import org.mockito.*; import java.util.ArrayList; import java.util.List; import java.util.Map; -import org.junit.Before; -import org.junit.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.mockito.Spy; +import static org.junit.Assert.assertEquals; //@RunWith(MockitoJUnitRunner.class) public class MockitoAnnotationTest { diff --git a/mockito/src/test/java/org/baeldung/mockito/MockitoConfigExamplesTest.java b/mockito/src/test/java/org/baeldung/mockito/MockitoConfigExamplesTest.java index 399c511d4e..de6e7fca72 100644 --- a/mockito/src/test/java/org/baeldung/mockito/MockitoConfigExamplesTest.java +++ b/mockito/src/test/java/org/baeldung/mockito/MockitoConfigExamplesTest.java @@ -1,20 +1,17 @@ package org.baeldung.mockito; +import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.when; - -import org.junit.Test; -import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; +import static org.mockito.Mockito.*; public class MockitoConfigExamplesTest { diff --git a/mockito/src/test/java/org/baeldung/mockito/MockitoMockTest.java b/mockito/src/test/java/org/baeldung/mockito/MockitoMockTest.java new file mode 100644 index 0000000000..5a1996fa97 --- /dev/null +++ b/mockito/src/test/java/org/baeldung/mockito/MockitoMockTest.java @@ -0,0 +1,69 @@ +package org.baeldung.mockito; + +import static org.mockito.Mockito.*; +import static org.junit.Assert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.containsString; +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; + +import org.junit.Test; +import org.junit.Rule; +import org.junit.rules.ExpectedException; +import org.mockito.MockSettings; +import org.mockito.exceptions.verification.TooLittleActualInvocations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class MockitoMockTest { + + private static class CustomAnswer implements Answer { + @Override + public Boolean answer(InvocationOnMock invocation) throws Throwable { + return false; + } + } + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void whenUsingSimpleMock_thenCorrect() { + MyList listMock = mock(MyList.class); + when(listMock.add(anyString())).thenReturn(false); + boolean added = listMock.add(randomAlphabetic(6)); + + verify(listMock).add(anyString()); + assertThat(added, is(false)); + } + + @Test + public void whenUsingMockWithName_thenCorrect() { + MyList listMock = mock(MyList.class, "myMock"); + when(listMock.add(anyString())).thenReturn(false); + listMock.add(randomAlphabetic(6)); + + thrown.expect(TooLittleActualInvocations.class); + thrown.expectMessage(containsString("myMock.add")); + + verify(listMock, times(2)).add(anyString()); + } + + @Test + public void whenUsingMockWithAnswer_thenCorrect() { + MyList listMock = mock(MyList.class, new CustomAnswer()); + boolean added = listMock.add(randomAlphabetic(6)); + + verify(listMock).add(anyString()); + assertThat(added, is(false)); + } + + @Test + public void whenUsingMockWithSettings_thenCorrect() { + MockSettings customSettings = withSettings().defaultAnswer(new CustomAnswer()); + MyList listMock = mock(MyList.class, customSettings); + boolean added = listMock.add(randomAlphabetic(6)); + + verify(listMock).add(anyString()); + assertThat(added, is(false)); + } +} \ No newline at end of file diff --git a/mockito/src/test/java/org/baeldung/mockito/MockitoSpyTest.java b/mockito/src/test/java/org/baeldung/mockito/MockitoSpyTest.java index adfd3ed1ae..8682e16e5d 100644 --- a/mockito/src/test/java/org/baeldung/mockito/MockitoSpyTest.java +++ b/mockito/src/test/java/org/baeldung/mockito/MockitoSpyTest.java @@ -1,16 +1,16 @@ package org.baeldung.mockito; -import static org.junit.Assert.assertEquals; - -import java.util.ArrayList; -import java.util.List; - import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.mockito.Spy; import org.mockito.runners.MockitoJUnitRunner; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; + @RunWith(MockitoJUnitRunner.class) public class MockitoSpyTest { diff --git a/mockito/src/test/java/org/baeldung/mockito/MockitoVerifyExamplesTest.java b/mockito/src/test/java/org/baeldung/mockito/MockitoVerifyExamplesTest.java index 6875a9f819..6beae34138 100644 --- a/mockito/src/test/java/org/baeldung/mockito/MockitoVerifyExamplesTest.java +++ b/mockito/src/test/java/org/baeldung/mockito/MockitoVerifyExamplesTest.java @@ -1,26 +1,18 @@ package org.baeldung.mockito; -import static org.hamcrest.Matchers.hasItem; -import static org.junit.Assert.assertThat; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.atLeast; -import static org.mockito.Mockito.atMost; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.verifyZeroInteractions; - -import java.util.List; - +import com.google.common.collect.Lists; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.InOrder; import org.mockito.Mockito; import org.mockito.exceptions.verification.NoInteractionsWanted; -import com.google.common.collect.Lists; +import java.util.List; + +import static org.hamcrest.Matchers.hasItem; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.*; public class MockitoVerifyExamplesTest { @@ -115,10 +107,10 @@ public class MockitoVerifyExamplesTest { @Test public final void whenVerifyingAnInteractionWithArgumentCapture_thenCorrect() { final List mockedList = mock(MyList.class); - mockedList.addAll(Lists. newArrayList("someElement")); + mockedList.addAll(Lists.newArrayList("someElement")); final ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(List.class); verify(mockedList).addAll(argumentCaptor.capture()); - final List capturedArgument = argumentCaptor.> getValue(); + final List capturedArgument = argumentCaptor.>getValue(); assertThat(capturedArgument, hasItem("someElement")); } diff --git a/mockito/src/test/java/org/baeldung/mockito/MyDictionary.java b/mockito/src/test/java/org/baeldung/mockito/MyDictionary.java index 3fb2c30646..a613b28f59 100644 --- a/mockito/src/test/java/org/baeldung/mockito/MyDictionary.java +++ b/mockito/src/test/java/org/baeldung/mockito/MyDictionary.java @@ -4,6 +4,7 @@ import java.util.HashMap; import java.util.Map; public class MyDictionary { + Map wordMap; public MyDictionary() { diff --git a/pom.xml b/pom.xml index 5dad868501..75281ce80d 100644 --- a/pom.xml +++ b/pom.xml @@ -1,116 +1,79 @@ - - - 4.0.0 - org.baeldung - spring-security-login-and-registration - spring-security-login-and-registration - war - 1.0.0-BUILD-SNAPSHOT + + 4.0.0 + com.baeldung + parent-modules + 1.0.0-SNAPSHOT - - org.springframework.boot - spring-boot-starter-parent - 1.1.4.RELEASE - + parent-modules + pom - - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.security - spring-security-config - runtime - - - - - - - org.slf4j - slf4j-api - - - ch.qos.logback - logback-classic - - + + apache-fop + assertj - - org.slf4j - jcl-over-slf4j - - + core-java + core-java-8 + gson + + guava + guava18 + guava19 + handling-spring-static-resources + httpclient + jackson + javaxval + jooq-spring + json-path + mockito + mock-comparisons + jee7schedule + + querydsl + + rest-testing + resteasy - - org.slf4j - log4j-over-slf4j - + spring-all + spring-apache-camel + spring-batch + spring-boot + spring-data-cassandra + spring-data-elasticsearch + spring-data-mongodb + spring-data-redis + spring-exceptions + spring-freemarker + spring-hibernate3 + spring-hibernate4 + spring-jpa + spring-katharsis + spring-mockito + spring-mvc-java + spring-mvc-no-xml + spring-mvc-xml + spring-openid + spring-protobuf + spring-quartz + spring-rest - - - javax.inject - javax.inject - ${javax.inject.version} - + spring-security-basic-auth + spring-security-mvc-custom + spring-security-mvc-digest-auth + spring-security-mvc-ldap + spring-security-mvc-login + spring-security-mvc-persisted-remember-me + spring-security-mvc-session + spring-security-rest + spring-security-rest-basic-auth + spring-security-rest-custom + spring-security-rest-digest-auth + spring-security-rest-full + spring-thymeleaf + spring-zuul + jsf + xml - - - javax.servlet - javax.servlet-api - - - - javax.servlet.jsp - javax.servlet.jsp-api - ${javax.servlet.jsp-api.version} - - - - javax.servlet - jstl - - - - - org.springframework.security - spring-security-taglibs - - - - - junit - junit - test - - - - spring-security-login-and-registration - - - src/main/resources - true - - - - - 1.7 - 3.1.1.RELEASE - 3.2.4.RELEASE - 1.6.10 - - - 1.7.6 - 1.1.1 - - - 2.3.2-b01 - - - 1 - + lombok + diff --git a/rest-testing/.classpath b/querydsl/.classpath similarity index 91% rename from rest-testing/.classpath rename to querydsl/.classpath index 8ebf6d9c31..264bb653bb 100644 --- a/rest-testing/.classpath +++ b/querydsl/.classpath @@ -6,18 +6,24 @@ + + + + + + - + - + @@ -27,10 +33,5 @@ - - - - - diff --git a/sandbox/.project b/querydsl/.project similarity index 84% rename from sandbox/.project rename to querydsl/.project index f039cd270e..729f89c323 100644 --- a/sandbox/.project +++ b/querydsl/.project @@ -1,17 +1,17 @@ - sandbox + querydsl - org.eclipse.jdt.core.javabuilder + org.eclipse.wst.common.project.facet.core.builder - org.eclipse.wst.common.project.facet.core.builder + org.eclipse.jdt.core.javabuilder @@ -27,8 +27,6 @@ - org.eclipse.jem.workbench.JavaEMFNature - org.eclipse.wst.common.modulecore.ModuleCoreNature org.eclipse.jdt.core.javanature org.eclipse.m2e.core.maven2Nature org.eclipse.wst.common.project.facet.core.nature diff --git a/querydsl/pom.xml b/querydsl/pom.xml new file mode 100644 index 0000000000..cf50144d3f --- /dev/null +++ b/querydsl/pom.xml @@ -0,0 +1,188 @@ + + + 4.0.0 + + com.baeldung + querydsl + 0.1-SNAPSHOT + jar + + querydsl + http://maven.apache.org + + + UTF-8 + 1.6 + 4.10 + 3.1.0.RELEASE + 4.3.11.Final + 2.5.0 + 1.5.10 + + + + + + com.mysema.querydsl + querydsl-core + ${querydsl.version} + + + + com.mysema.querydsl + querydsl-jpa + ${querydsl.version} + + + + com.mysema.querydsl + querydsl-apt + ${querydsl.version} + provided + + + + + org.hibernate + hibernate-entitymanager + ${hibernate.version} + compile + + + + org.hibernate.javax.persistence + hibernate-jpa-2.0-api + 1.0.0.Final + compile + + + + commons-dbcp + commons-dbcp + 1.4 + jar + compile + + + + commons-pool + commons-pool + 1.5.5 + jar + compile + + + + + org.hsqldb + hsqldb-j5 + 2.2.4 + + + + + org.springframework + spring-context + ${spring.version} + + + + org.springframework + spring-webmvc + ${spring.version} + + + + org.springframework + spring-orm + ${spring.version} + jar + compile + + + + org.springframework + spring-aop + ${spring.version} + + + + org.springframework + spring-test + ${spring.version} + jar + test + + + + + org.slf4j + slf4j-api + ${slf4j.version} + + + org.slf4j + jcl-over-slf4j + ${slf4j.version} + runtime + + + org.slf4j + slf4j-log4j12 + ${slf4j.version} + runtime + + + log4j + log4j + 1.2.16 + + + + + junit + junit + ${junit.version} + test + + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + ${java.version} + ${java.version} + -proc:none + + + + + + com.mysema.maven + maven-apt-plugin + 1.0.3 + + + + process + + + target/metamodel + com.mysema.query.apt.jpa.JPAAnnotationProcessor + + + + + + + + \ No newline at end of file diff --git a/querydsl/src/main/java/org/baeldung/dao/PersonDao.java b/querydsl/src/main/java/org/baeldung/dao/PersonDao.java new file mode 100644 index 0000000000..7df4ebb22d --- /dev/null +++ b/querydsl/src/main/java/org/baeldung/dao/PersonDao.java @@ -0,0 +1,22 @@ +package org.baeldung.dao; + +import java.util.List; +import java.util.Map; + +import org.baeldung.entity.Person; + +public interface PersonDao { + + public Person save(Person person); + + public List findPersonsByFirstnameQueryDSL(String firstname); + + public List findPersonsByFirstnameAndSurnameQueryDSL(String firstname, String surname); + + public List findPersonsByFirstnameInDescendingOrderQueryDSL(String firstname); + + public int findMaxAge(); + + public Map findMaxAgeByName(); + +} \ No newline at end of file diff --git a/querydsl/src/main/java/org/baeldung/dao/PersonDaoImpl.java b/querydsl/src/main/java/org/baeldung/dao/PersonDaoImpl.java new file mode 100644 index 0000000000..555ec226ce --- /dev/null +++ b/querydsl/src/main/java/org/baeldung/dao/PersonDaoImpl.java @@ -0,0 +1,68 @@ +package org.baeldung.dao; + +import java.util.List; +import java.util.Map; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.baeldung.entity.Person; +import org.baeldung.entity.QPerson; +import org.springframework.stereotype.Repository; + +import com.mysema.query.group.GroupBy; +import com.mysema.query.jpa.impl.JPAQuery; + +@Repository +public class PersonDaoImpl implements PersonDao { + + @PersistenceContext + private EntityManager em; + + @Override + public Person save(final Person person) { + em.persist(person); + return person; + } + + @Override + public List findPersonsByFirstnameQueryDSL(final String firstname) { + final JPAQuery query = new JPAQuery(em); + final QPerson person = QPerson.person; + + return query.from(person).where(person.firstname.eq(firstname)).list(person); + } + + @Override + public List findPersonsByFirstnameAndSurnameQueryDSL(final String firstname, final String surname) { + final JPAQuery query = new JPAQuery(em); + final QPerson person = QPerson.person; + + return query.from(person).where(person.firstname.eq(firstname).and(person.surname.eq(surname))).list(person); + } + + @Override + public List findPersonsByFirstnameInDescendingOrderQueryDSL(final String firstname) { + final JPAQuery query = new JPAQuery(em); + final QPerson person = QPerson.person; + + return query.from(person).where(person.firstname.eq(firstname)).orderBy(person.surname.desc()).list(person); + } + + @Override + public int findMaxAge() { + final JPAQuery query = new JPAQuery(em); + final QPerson person = QPerson.person; + + return query.from(person).list(person.age.max()).get(0); + } + + @Override + public Map findMaxAgeByName() { + final JPAQuery query = new JPAQuery(em); + final QPerson person = QPerson.person; + + return query.from(person).transform(GroupBy.groupBy(person.firstname).as(GroupBy.max(person.age))); + } + +} \ No newline at end of file diff --git a/querydsl/src/main/java/org/baeldung/entity/Person.java b/querydsl/src/main/java/org/baeldung/entity/Person.java new file mode 100644 index 0000000000..6f04210d90 --- /dev/null +++ b/querydsl/src/main/java/org/baeldung/entity/Person.java @@ -0,0 +1,69 @@ +package org.baeldung.entity; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Person { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column + private String firstname; + + @Column + private String surname; + + @Column + private int age; + + Person() { + } + + public Person(final String firstname, final String surname) { + this.firstname = firstname; + this.surname = surname; + } + + public Person(final String firstname, final String surname, final int age) { + this(firstname, surname); + this.age = age; + } + + public Long getId() { + return id; + } + + public void setId(final Long id) { + this.id = id; + } + + public String getFirstname() { + return firstname; + } + + public void setFirstname(final String firstname) { + this.firstname = firstname; + } + + public String getSurname() { + return surname; + } + + public void setSurname(final String surname) { + this.surname = surname; + } + + public int getAge() { + return age; + } + + public void setAge(final int age) { + this.age = age; + } +} \ No newline at end of file diff --git a/querydsl/src/main/resources/META-INF/persistence.xml b/querydsl/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000000..111d7933c3 --- /dev/null +++ b/querydsl/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/querydsl/src/main/resources/log4j.xml b/querydsl/src/main/resources/log4j.xml new file mode 100644 index 0000000000..a7f96b38a4 --- /dev/null +++ b/querydsl/src/main/resources/log4j.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/querydsl/src/test/java/org/baeldung/dao/PersonDaoTest.java b/querydsl/src/test/java/org/baeldung/dao/PersonDaoTest.java new file mode 100644 index 0000000000..0e5996a8c8 --- /dev/null +++ b/querydsl/src/test/java/org/baeldung/dao/PersonDaoTest.java @@ -0,0 +1,81 @@ +package org.baeldung.dao; + +import java.util.Map; + +import org.baeldung.entity.Person; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.transaction.TransactionConfiguration; +import org.springframework.transaction.annotation.Transactional; + +import junit.framework.Assert; + +@ContextConfiguration("/test-context.xml") +@RunWith(SpringJUnit4ClassRunner.class) +@Transactional +@TransactionConfiguration(defaultRollback = true) +public class PersonDaoTest { + + @Autowired + private PersonDao personDao; + + // + + @Test + public void testCreation() { + personDao.save(new Person("Erich", "Gamma")); + final Person person = new Person("Kent", "Beck"); + personDao.save(person); + personDao.save(new Person("Ralph", "Johnson")); + + final Person personFromDb = personDao.findPersonsByFirstnameQueryDSL("Kent").get(0); + Assert.assertEquals(person.getId(), personFromDb.getId()); + } + + @Test + public void testMultipleFilter() { + personDao.save(new Person("Erich", "Gamma")); + final Person person = personDao.save(new Person("Ralph", "Beck")); + final Person person2 = personDao.save(new Person("Ralph", "Johnson")); + + final Person personFromDb = personDao.findPersonsByFirstnameAndSurnameQueryDSL("Ralph", "Johnson").get(0); + Assert.assertNotSame(person.getId(), personFromDb.getId()); + Assert.assertEquals(person2.getId(), personFromDb.getId()); + } + + @Test + public void testOrdering() { + final Person person = personDao.save(new Person("Kent", "Gamma")); + personDao.save(new Person("Ralph", "Johnson")); + final Person person2 = personDao.save(new Person("Kent", "Zivago")); + + final Person personFromDb = personDao.findPersonsByFirstnameInDescendingOrderQueryDSL("Kent").get(0); + Assert.assertNotSame(person.getId(), personFromDb.getId()); + Assert.assertEquals(person2.getId(), personFromDb.getId()); + } + + @Test + public void testMaxAge() { + personDao.save(new Person("Kent", "Gamma", 20)); + personDao.save(new Person("Ralph", "Johnson", 35)); + personDao.save(new Person("Kent", "Zivago", 30)); + + final int maxAge = personDao.findMaxAge(); + Assert.assertTrue(maxAge == 35); + } + + @Test + public void testMaxAgeByName() { + personDao.save(new Person("Kent", "Gamma", 20)); + personDao.save(new Person("Ralph", "Johnson", 35)); + personDao.save(new Person("Kent", "Zivago", 30)); + + final Map maxAge = personDao.findMaxAgeByName(); + Assert.assertTrue(maxAge.size() == 2); + Assert.assertSame(35, maxAge.get("Ralph")); + Assert.assertSame(30, maxAge.get("Kent")); + } +} \ No newline at end of file diff --git a/querydsl/src/test/resources/db.properties b/querydsl/src/test/resources/db.properties new file mode 100644 index 0000000000..efee3669ce --- /dev/null +++ b/querydsl/src/test/resources/db.properties @@ -0,0 +1,6 @@ +#In memory db +db.username=sa +db.password= +db.driver=org.hsqldb.jdbcDriver +db.url=jdbc:hsqldb:mem:app-db +db.dialect=org.hibernate.dialect.HSQLDialect \ No newline at end of file diff --git a/querydsl/src/test/resources/test-context.xml b/querydsl/src/test/resources/test-context.xml new file mode 100644 index 0000000000..13d823a857 --- /dev/null +++ b/querydsl/src/test/resources/test-context.xml @@ -0,0 +1,20 @@ + + + + + + + + + \ No newline at end of file diff --git a/querydsl/src/test/resources/test-db.xml b/querydsl/src/test/resources/test-db.xml new file mode 100644 index 0000000000..7f85630b6b --- /dev/null +++ b/querydsl/src/test/resources/test-db.xml @@ -0,0 +1,49 @@ + + + + + + + classpath:db.properties + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file 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/raml/annotations/README.md b/raml/annotations/README.md new file mode 100644 index 0000000000..4142b33353 --- /dev/null +++ b/raml/annotations/README.md @@ -0,0 +1,7 @@ +========= + +## Define Custom RAML + + +### Relevant Articles: +- [Define Custom RAML Properties Using Annotations](http://www.baeldung.com/raml-custom-properties-with-annotations) diff --git a/raml/annotations/api.raml b/raml/annotations/api.raml new file mode 100644 index 0000000000..e0123536eb --- /dev/null +++ b/raml/annotations/api.raml @@ -0,0 +1,126 @@ +#%RAML 1.0 +title: API for REST Services used in the RAML tutorials on Baeldung.com +documentation: + - title: Overview + content: | + This document defines the interface for the REST services + used in the popular RAML Tutorial series at Baeldung.com. + - title: Disclaimer + content: | + All names used in this definition are purely fictional. + Any similarities between the names used in this tutorial and those of real persons, whether living or dead, are merely coincidental. + - title: Copyright + content: Copyright 2016 by Baeldung.com. All rights reserved. +uses: + mySecuritySchemes: !include libraries/securitySchemes.raml + myDataTypes: !include libraries/dataTypes.raml + myTraits: !include libraries/traits.raml + myResourceTypes: !include libraries/resourceTypes.raml +version: v1 +protocols: [ HTTPS ] +baseUri: http://rest-api.baeldung.com/api/{version} +mediaType: application/json +securedBy: [ mySecuritySchemes.basicAuth ] +annotationTypes: + testCase: + allowedTargets: [ Method ] + allowMultiple: true + usage: | + Use this annotation to declare a test case + within a testSuite declaration. + You may apply this annotation multiple times + within the target testSuite. + properties: + scenario: string + setupScript?: string[] + testScript: string[] + expectedOutput?: string + cleanupScript?: string[] +/foos: + type: myResourceTypes.collection + get: + queryParameters: + name?: string + ownerName?: string + responses: + 200: + body: + example: !include examples/Foos.json + (testCase): + scenario: No Foos + setupScript: deleteAllFoosIfAny + testScript: getAllFoos + expectedOutput: "" + (testCase): + scenario: One Foo + setupScript: [ deleteAllFoosIfAny, addInputFoos ] + testScript: getAllFoos + expectedOutput: '[ { "id": 999, "name": Joe } ]' + cleanupScript: deleteInputFoos + (testCase): + scenario: Multiple Foos + setupScript: [ deleteAllFoosIfAny, addInputFoos ] + testScript: getAllFoos + expectedOutput: '[ { "id": 998, "name": "Bob" }, { "id": 999, "name": "Joe", "ownerName": "Bob" } ]' + cleanupScript: deleteInputFoos + post: + responses: + 200: + body: + example: !include examples/Foo.json + /{fooId}: + type: myResourceTypes.item + get: + responses: + 200: + body: + example: !include examples/Foo.json + put: + responses: + 200: + body: + example: !include examples/Foo.json +/foos/name/{name}: + get: + description: Get all Foos with the name {name} + responses: + 200: + body: + type: myDataTypes.Foo + 404: + body: + type: myDataTypes.Error +/foos/bar/{barId}: + get: + description: Get the Foo for the Bar with barId = {barId} + responses: + 200: + body: + example: !include examples/Foo.json +/bars: + type: myResourceTypes.collection + get: + queryParameters: + name?: string + ownerName?: string + responses: + 200: + body: + example: !include examples/Bars.json + post: + responses: + 200: + body: + example: !include examples/Bar.json + /{barId}: + type: myResourceTypes.item + get: + responses: + 200: + body: + example: !include examples/Bar.json + put: + responses: + 200: + body: + example: !include examples/Bars.json diff --git a/raml/annotations/examples/Bar.json b/raml/annotations/examples/Bar.json new file mode 100644 index 0000000000..0ee1b34edb --- /dev/null +++ b/raml/annotations/examples/Bar.json @@ -0,0 +1,6 @@ +{ + "id" : 1, + "name" : "First Bar", + "city" : "Austin", + "fooId" : 2 +} \ No newline at end of file diff --git a/raml/annotations/examples/Bars.json b/raml/annotations/examples/Bars.json new file mode 100644 index 0000000000..89ea875432 --- /dev/null +++ b/raml/annotations/examples/Bars.json @@ -0,0 +1,19 @@ +[ + { + "id" : 1, + "name" : "First Bar", + "city" : "Austin", + "fooId" : 2 + }, + { + "id" : 2, + "name" : "Second Bar", + "city" : "Dallas", + "fooId" : 1 + }, + { + "id" : 3, + "name" : "Third Bar", + "fooId" : 2 + } +] \ No newline at end of file diff --git a/raml/annotations/examples/Error.json b/raml/annotations/examples/Error.json new file mode 100644 index 0000000000..dca56da7c2 --- /dev/null +++ b/raml/annotations/examples/Error.json @@ -0,0 +1,4 @@ +{ + "message" : "Not found", + "code" : 1001 +} \ No newline at end of file diff --git a/raml/annotations/examples/Foo.json b/raml/annotations/examples/Foo.json new file mode 100644 index 0000000000..1b1b8c891e --- /dev/null +++ b/raml/annotations/examples/Foo.json @@ -0,0 +1,4 @@ +{ + "id" : 1, + "name" : "First Foo" +} \ No newline at end of file diff --git a/raml/annotations/examples/Foos.json b/raml/annotations/examples/Foos.json new file mode 100644 index 0000000000..74f64689f0 --- /dev/null +++ b/raml/annotations/examples/Foos.json @@ -0,0 +1,16 @@ +[ + { + "id" : 1, + "name" : "First Foo", + "ownerName" : "Jack Robinson" + }, + { + "id" : 2, + "name" : "Second Foo" + }, + { + "id" : 3, + "name" : "Third Foo", + "ownerName" : "Chuck Norris" + } +] \ No newline at end of file diff --git a/raml/annotations/extensions/en_US/additionalResources.raml b/raml/annotations/extensions/en_US/additionalResources.raml new file mode 100644 index 0000000000..9ab0d0a243 --- /dev/null +++ b/raml/annotations/extensions/en_US/additionalResources.raml @@ -0,0 +1,13 @@ +#%RAML 1.0 Extension +# File located at: +# /extensions/en_US/additionalResources.raml +masterRef: ../../api.raml +usage: This extension defines additional resources for version 2 of the API. +version: v2 +/foos: + /bar/{barId}: + get: + description: | + Get the foo that is related to the bar having barId = {barId} + queryParameters: + barId?: integer diff --git a/raml/annotations/libraries/dataTypes.raml b/raml/annotations/libraries/dataTypes.raml new file mode 100644 index 0000000000..8a240e62dc --- /dev/null +++ b/raml/annotations/libraries/dataTypes.raml @@ -0,0 +1,19 @@ +#%RAML 1.0 Library +# This is the file /libraries/dataTypes.raml +usage: This library defines the data types for the API +types: + Foo: + properties: + id: integer + name: string + ownerName?: string + Bar: + properties: + id: integer + name: string + city?: string + fooId: integer + Error: + properties: + code: integer + message: string diff --git a/raml/annotations/libraries/resourceTypes.raml b/raml/annotations/libraries/resourceTypes.raml new file mode 100644 index 0000000000..fb054f1a36 --- /dev/null +++ b/raml/annotations/libraries/resourceTypes.raml @@ -0,0 +1,38 @@ +#%RAML 1.0 Library +# This is the file /libraries/resourceTypes.raml +usage: This library defines the resource types for the API +uses: + myTraits: !include traits.raml +resourceTypes: + collection: + usage: Use this resourceType to represent a collection of items + description: A collection of <> + get: + description: | + Get all <>, + optionally filtered + is: [ myTraits.hasResponseCollection ] + post: + description: | + Create a new <> + is: [ myTraits.hasRequestItem ] + item: + usage: Use this resourceType to represent any single item + description: A single <> + get: + description: | + Get a <> + by <>Id + is: [ myTraits.hasResponseItem, myTraits.hasNotFound ] + put: + description: | + Update a <> + by <>Id + is: [ myTraits.hasRequestItem, myTraits.hasResponseItem, myTraits.hasNotFound ] + delete: + description: | + Delete a <> + by <>Id + is: [ myTraits.hasNotFound ] + responses: + 204: diff --git a/raml/annotations/libraries/securitySchemes.raml b/raml/annotations/libraries/securitySchemes.raml new file mode 100644 index 0000000000..621c6ac975 --- /dev/null +++ b/raml/annotations/libraries/securitySchemes.raml @@ -0,0 +1,20 @@ +#%RAML 1.0 Library +# This is the file /libraries/securitySchemes.raml +securitySchemes: + - basicAuth: + description: Each request must contain the headers necessary for + basic authentication + type: Basic Authentication + describedBy: + headers: + Authorization: + description: | + Used to send the Base64 encoded "username:password" + credentials + type: string + responses: + 401: + description: | + Unauthorized. Either the provided username and password + combination is invalid, or the user is not allowed to + access the content provided by the requested URL. diff --git a/raml/annotations/libraries/traits.raml b/raml/annotations/libraries/traits.raml new file mode 100644 index 0000000000..610747e79f --- /dev/null +++ b/raml/annotations/libraries/traits.raml @@ -0,0 +1,32 @@ +#%RAML 1.0 Library +# This is the file /libraries/traits.raml +usage: This library defines some basic traits +uses: + myDataTypes: !include dataTypes.raml +traits: + hasRequestItem: + usage: Use this trait for resources whose request body is a single item + body: + application/json: + type: <> + hasResponseItem: + usage: Use this trait for resources whose response body is a single item + responses: + 200: + body: + application/json: + type: <> + hasResponseCollection: + usage: Use this trait for resources whose response body is a collection of items + responses: + 200: + body: + application/json: + type: <>[] + hasNotFound: + usage: Use this trait for resources that could respond with a 404 status + responses: + 404: + body: + application/json: + type: myDataTypes.Error diff --git a/raml/annotations/overlays/es_ES/additionalResources.raml b/raml/annotations/overlays/es_ES/additionalResources.raml new file mode 100644 index 0000000000..0edd6a9231 --- /dev/null +++ b/raml/annotations/overlays/es_ES/additionalResources.raml @@ -0,0 +1,11 @@ +#%RAML 1.0 Overlay +# Archivo situado en: +# /overlays/es_ES/additionalResources.raml +masterRef: ../../api.raml +usage: | + Se trata de un español demasiado que describe los recursos adicionales + para la versión 1 del API. +/foos/bar/{barId}: + get: + description: | + Obtener el foo que se relaciona con el bar tomando barId = {barId} diff --git a/raml/annotations/overlays/es_ES/documentationItems.raml b/raml/annotations/overlays/es_ES/documentationItems.raml new file mode 100644 index 0000000000..49dd46fb59 --- /dev/null +++ b/raml/annotations/overlays/es_ES/documentationItems.raml @@ -0,0 +1,22 @@ +#%RAML 1.0 Overlay +# File located at (archivo situado en): +# /overlays/es_ES/documentationItems.raml +masterRef: ../../api.raml +usage: | + To provide user documentation and other descriptive text in Spanish + (Para proporcionar la documentación del usuario y otro texto descriptivo en español) +title: API para servicios REST utilizados en los tutoriales RAML en Baeldung.com +documentation: + - title: Descripción general + content: | + Este documento define la interfaz para los servicios REST + utilizados en la popular serie de RAML Tutorial en Baeldung.com + - title: Renuncia + content: | + Todos los nombres usados ​​en esta definición son pura ficción. + Cualquier similitud entre los nombres utilizados en este tutorial + y los de las personas reales, ya sea vivo o muerto, + no son más que coincidenta. + - title: Derechos de autor + content: | + Derechos de autor 2016 por Baeldung.com. Todos los derechos reservados. diff --git a/raml/introduction/0.8/api.raml b/raml/introduction/0.8/api.raml new file mode 100644 index 0000000000..29f769ae16 --- /dev/null +++ b/raml/introduction/0.8/api.raml @@ -0,0 +1,102 @@ +#%RAML 0.8 +title: Baeldung Foo REST Services API +version: v1 +protocols: [ HTTPS ] +baseUri: http://rest-api.baeldung.com/api/{version} +mediaType: application/json +securitySchemes: + - basicAuth: + description: Each request must contain the headers necessary for + basic authentication + type: Basic Authentication + describedBy: + headers: + Authorization: + description: Used to send the Base64 encoded "username:password" + credentials + type: string + responses: + 401: + description: | + Unauthorized. Either the provided username and password + combination is invalid, or the user is not allowed to access + the content provided by the requested URL. + +schemas: + - foo: !include foo.json + - foos: !include foos.json + - error: !include error.json + +/foos: + get: + description: List all Foos matching query criteria, if provided; + otherwise list all Foos + queryParameters: + name: + type: string + required: false + ownerName: + type: string + required: false + responses: + 200: + body: + application/json: + schema: foos + example: !include foos-example.json + post: + description: Create a new Foo + body: + application/json: + schema: foo + example: foo-example.json + responses: + 201: + body: + application/json: + schema: foo + example: foo-example.json + /{id}: + get: + description: Get a Foo by id + responses: + 200: + body: + application/json: + schema: foo + 404: + body: + application/json: + schema: error + put: + description: Update a Foo by id + body: + application/json: + schema: foo + example: foo-example.json + responses: + 200: + body: + application/json: + schema: foo + 404: + body: + application/json: + schema: error + delete: + description: Delete a Foo by id + responses: + 204: + 404: + body: + application/json: + schema: error + /name/{name}: + get: + description: List all Foos with a certain name + responses: + 200: + body: + application/json: + schema: foos + example: !include foos-example.json \ No newline at end of file diff --git a/raml/introduction/0.8/error-example.json b/raml/introduction/0.8/error-example.json new file mode 100644 index 0000000000..dca56da7c2 --- /dev/null +++ b/raml/introduction/0.8/error-example.json @@ -0,0 +1,4 @@ +{ + "message" : "Not found", + "code" : 1001 +} \ No newline at end of file diff --git a/raml/introduction/0.8/error.json b/raml/introduction/0.8/error.json new file mode 100644 index 0000000000..f6e25b51d2 --- /dev/null +++ b/raml/introduction/0.8/error.json @@ -0,0 +1,12 @@ +{ "$schema": "http://json-schema.org/schema", + "type": "object", + "description": "Error message", + "properties": { + "message": { "type": "string" }, + "code": { "type": integer } + }, + "required": [ + "message", + "code" + ] +} \ No newline at end of file diff --git a/raml/introduction/0.8/foo-example.json b/raml/introduction/0.8/foo-example.json new file mode 100644 index 0000000000..1b1b8c891e --- /dev/null +++ b/raml/introduction/0.8/foo-example.json @@ -0,0 +1,4 @@ +{ + "id" : 1, + "name" : "First Foo" +} \ No newline at end of file diff --git a/raml/introduction/0.8/foo.json b/raml/introduction/0.8/foo.json new file mode 100644 index 0000000000..c024a8c2d3 --- /dev/null +++ b/raml/introduction/0.8/foo.json @@ -0,0 +1,13 @@ +{ "$schema": "http://json-schema.org/schema", + "type": "object", + "description": "Foo details", + "properties": { + "id": { "type": integer }, + "name": { "type": "string" }, + "ownerName": { "type": "string" } + }, + "required": [ + "id", + "name" + ] +} \ No newline at end of file diff --git a/raml/introduction/0.8/foos-example.json b/raml/introduction/0.8/foos-example.json new file mode 100644 index 0000000000..fe9c89d36a --- /dev/null +++ b/raml/introduction/0.8/foos-example.json @@ -0,0 +1,10 @@ +[ + { + "id" : 1, + "name" : "First Foo" + }, + { + "id" : 2, + "name" : "Second Foo" + } +] \ No newline at end of file diff --git a/raml/introduction/0.8/foos.json b/raml/introduction/0.8/foos.json new file mode 100644 index 0000000000..931deab029 --- /dev/null +++ b/raml/introduction/0.8/foos.json @@ -0,0 +1,5 @@ +{ "$schema": "http://json-schema.org/schema", + "type": "array", + "items": { "$ref": "foo" } + "description": "Collection of Foos" +} \ No newline at end of file diff --git a/raml/introduction/1.0/api.raml b/raml/introduction/1.0/api.raml new file mode 100644 index 0000000000..a54d4f11db --- /dev/null +++ b/raml/introduction/1.0/api.raml @@ -0,0 +1,101 @@ +#%RAML 1.0 +title: Baeldung Foo REST Services API +version: v1 +protocols: [ HTTPS ] +baseUri: http://rest-api.baeldung.com/api/{version} +mediaType: application/json +securedBy: basicAuth +securitySchemes: + - basicAuth: + description: Each request must contain the headers necessary for + basic authentication + type: Basic Authentication + describedBy: + headers: + Authorization: + description: Used to send the Base64 encoded "username:password" + credentials + type: string + responses: + 401: + description: | + Unauthorized. Either the provided username and password + combination is invalid, or the user is not allowed to access + the content provided by the requested URL. +types: + Foo: !include types/Foo.raml + Error: !include types/Error.raml +/foos: + get: + description: List all Foos matching query criteria, if provided; + otherwise list all Foos + queryParameters: + name?: string + ownerName?: string + responses: + 200: + body: + application/json: + type: Foo[] + example: !include examples/Foos.json + post: + description: Create a new Foo + body: + application/json: + type: Foo + example: !include examples/Foo.json + responses: + 201: + body: + application/json: + type: Foo + example: !include examples/Foo.json + /{id}: + get: + description: Get a Foo by id + responses: + 200: + body: + application/json: + type: Foo + example: !include examples/Foo.json + 404: + body: + application/json: + type: Error + example: !include examples/Error.json + put: + description: Update a Foo by id + body: + application/json: + type: Foo + example: !include examples/Foo.json + responses: + 200: + body: + application/json: + type: Foo + example: !include examples/Foo.json + 404: + body: + application/json: + type: Error + example: !include examples/Error.json + delete: + description: Delete a Foo by id + responses: + 204: + 404: + body: + application/json: + type: Error + example: !include examples/Error.json + /name/{name}: + get: + description: List all Foos with a certain name + responses: + 200: + body: + application/json: + type: Foo[] + example: !include examples/Foos.json \ No newline at end of file diff --git a/raml/introduction/1.0/examples/Error.json b/raml/introduction/1.0/examples/Error.json new file mode 100644 index 0000000000..dca56da7c2 --- /dev/null +++ b/raml/introduction/1.0/examples/Error.json @@ -0,0 +1,4 @@ +{ + "message" : "Not found", + "code" : 1001 +} \ No newline at end of file diff --git a/raml/introduction/1.0/examples/Foo.json b/raml/introduction/1.0/examples/Foo.json new file mode 100644 index 0000000000..1b1b8c891e --- /dev/null +++ b/raml/introduction/1.0/examples/Foo.json @@ -0,0 +1,4 @@ +{ + "id" : 1, + "name" : "First Foo" +} \ No newline at end of file diff --git a/raml/introduction/1.0/examples/Foos.json b/raml/introduction/1.0/examples/Foos.json new file mode 100644 index 0000000000..fe9c89d36a --- /dev/null +++ b/raml/introduction/1.0/examples/Foos.json @@ -0,0 +1,10 @@ +[ + { + "id" : 1, + "name" : "First Foo" + }, + { + "id" : 2, + "name" : "Second Foo" + } +] \ No newline at end of file diff --git a/raml/introduction/1.0/types/Error.raml b/raml/introduction/1.0/types/Error.raml new file mode 100644 index 0000000000..8d54b5f181 --- /dev/null +++ b/raml/introduction/1.0/types/Error.raml @@ -0,0 +1,5 @@ +#%RAML 1.0 DataType + + properties: + code: integer + message: string diff --git a/raml/introduction/1.0/types/Foo.raml b/raml/introduction/1.0/types/Foo.raml new file mode 100644 index 0000000000..1702865e05 --- /dev/null +++ b/raml/introduction/1.0/types/Foo.raml @@ -0,0 +1,6 @@ +#%RAML 1.0 DataType + +properties: + id: integer + name: string + ownerName?: string diff --git a/raml/modularization/README.md b/raml/modularization/README.md new file mode 100644 index 0000000000..de0e047ea6 --- /dev/null +++ b/raml/modularization/README.md @@ -0,0 +1,6 @@ +========= + +## Modular RESTful API Modeling Language + +### Relevant Articles: +- [Modular RAML Using Includes, Libraries, Overlays and Extensions](http://www.baeldung.com/modular-raml-includes-overlays-libraries-extensions) diff --git a/raml/modularization/api-before-modularization.raml b/raml/modularization/api-before-modularization.raml new file mode 100644 index 0000000000..b580c33983 --- /dev/null +++ b/raml/modularization/api-before-modularization.raml @@ -0,0 +1,119 @@ +#%RAML 1.0 +title: API for REST Services used in the RAML tutorials on Baeldung.com +documentation: + - title: Overview + - content: | + This document defines the interface for the REST services + used in the popular RAML Tutorial series at Baeldung.com. + - title: Disclaimer: + - content: | + All names used in this definition are purely fictional. + Any similarities between the names used in this tutorial and those of real persons, whether living or dead, are merely coincidental. + - title: Copyright + - content: Copyright 2016 by Baeldung.com. All rights reserved. +version: v1 +protocols: [ HTTPS ] +baseUri: http://rest-api.baeldung.com/api/{version} +mediaType: application/json +securedBy: basicAuth +securitySchemes: + - basicAuth: + description: Each request must contain the headers necessary for + basic authentication + type: Basic Authentication + describedBy: + headers: + Authorization: + description: | + Used to send the Base64 encoded "username:password" + credentials + type: string + responses: + 401: + description: | + Unauthorized. Either the provided username and password + combination is invalid, or the user is not allowed to + access the content provided by the requested URL. +types: + Foo: !include types/Foo.raml + Bar: !include types/Bar.raml + Error: !include types/Error.raml +resourceTypes: + - collection: + usage: Use this resourceType to represent a collection of items + description: A collection of <> + get: + description: | + Get all <>, + optionally filtered + is: [ hasResponseCollection ] + post: + description: | + Create a new <> + is: [ hasRequestItem ] + - item: + usage: Use this resourceType to represent any single item + description: A single <> + get: + description: Get a <> by <> + is: [ hasResponseItem, hasNotFound ] + put: + description: Update a <> by <> + is: [ hasRequestItem, hasResponseItem, hasNotFound ] + delete: + description: Delete a <> by <> + is: [ hasNotFound ] + responses: + 204: +traits: + - hasRequestItem: + body: + application/json: + type: <> + - hasResponseItem: + responses: + 200: + body: + application/json: + type: <> + example: !include examples/<>.json + - hasResponseCollection: + responses: + 200: + body: + application/json: + type: <>[] + example: !include examples/<>.json + - hasNotFound: + responses: + 404: + body: + application/json: + type: Error + example: !include examples/Error.json +/foos: + type: collection + typeName: Foo + get: + queryParameters: + name?: string + ownerName?: string + /{fooId}: + type: item + typeName: Foo + /name/{name}: + get: + description: List all foos with a certain name + typeName: Foo + is: [ hasResponseCollection ] +/bars: + type: collection + typeName: Bar + /{barId}: + type: item + typeName: Bar + /fooId/{fooId}: + get: + description: Get all bars for the matching fooId + typeName: Bar + is: [ hasResponseCollection ] \ No newline at end of file diff --git a/raml/modularization/api-with-libraries.raml b/raml/modularization/api-with-libraries.raml new file mode 100644 index 0000000000..b3081e843a --- /dev/null +++ b/raml/modularization/api-with-libraries.raml @@ -0,0 +1,50 @@ +#%RAML 1.0 +title: API for REST Services used in the RAML tutorials on Baeldung.com +documentation: + - title: Overview + - content: | + This document defines the interface for the REST services + used in the popular RAML Tutorial series at Baeldung.com. + - title: Disclaimer: + - content: | + All names used in this definition are purely fictional. + Any similarities between the names used in this tutorial and those of real persons, whether living or dead, are merely coincidental. + - title: Copyright + - content: Copyright 2016 by Baeldung.com. All rights reserved. +uses: + mySecuritySchemes: !include libraries/security.raml + myDataTypes: !include libraries/dataTypes.raml + myResourceTypes: !include libraries/resourceTypes.raml + myTraits: !include libraries/traits.raml +version: v1 +protocols: [ HTTPS ] +baseUri: http://rest-api.baeldung.com/api/{version} +mediaType: application/json +securedBy: [ mySecuritySchemes.basicAuth ] +/foos: + type: myResourceTypes.collection + typeName: myDataTypes.Foo + get: + queryParameters: + name?: string + ownerName?: string + /{fooId}: + type: myResourceTypes.item + typeName: myDataTypes.Foo + /name/{name}: + get: + description: List all foos with a certain name + typeName: myDataTypes.Foo + is: [ myTraits.hasResponseCollection ] +/bars: + type: myResourceTypes.collection + typeName: myDataTypes.Bar + /{barId}: + type: myResourceTypes.item + typeName: myDataTypes.Bar + /fooId/{fooId}: + get: + description: Get all bars for the matching fooId + type: myResourceTypes.item + typeName: myDataTypes.Bar + is: [ myTraits.hasResponseCollection ] \ No newline at end of file diff --git a/raml/modularization/api-with-typed-fragments.raml b/raml/modularization/api-with-typed-fragments.raml new file mode 100644 index 0000000000..2bb4e317c1 --- /dev/null +++ b/raml/modularization/api-with-typed-fragments.raml @@ -0,0 +1,74 @@ +#%RAML 1.0 +title: API for REST Services used in the RAML tutorials on Baeldung.com +documentation: + - title: Overview + - content: | + This document defines the interface for the REST services + used in the popular RAML Tutorial series at Baeldung.com. + - title: Disclaimer: + - content: | + All names used in this definition are purely fictional. + Any similarities between the names used in this tutorial and those of real persons, whether living or dead, are merely coincidental. + - title: Copyright + - content: Copyright 2016 by Baeldung.com. All rights reserved. +version: v1 +protocols: [ HTTPS ] +baseUri: http://rest-api.baeldung.com/api/{version} +mediaType: application/json +securedBy: [ basicAuth ] +securitySchemes: + - basicAuth: + description: Each request must contain the headers necessary for + basic authentication + type: Basic Authentication + describedBy: + headers: + Authorization: + description: | + Used to send the Base64 encoded "username:password" + credentials + type: string + responses: + 401: + description: | + Unauthorized. Either the provided username and password + combination is invalid, or the user is not allowed to + access the content provided by the requested URL. +types: + Foo: !include types/Foo.raml + Bar: !include types/Bar.raml + Error: !include types/Error.raml +resourceTypes: + - collection: !include resourceTypes/collection.raml + - item: !include resourceTypes/item.raml +traits: + - hasRequestItem: !include traits/hasRequestItem.raml + - hasResponseItem: !include traits/hasResponseItem.raml + - hasResponseCollection: !include traits/hasResponseCollection.raml + - hasNotFound: !include traits/hasNotFound.raml +/foos: + type: collection + typeName: Foo + get: + queryParameters: + name?: string + ownerName?: string + /{fooId}: + type: item + typeName: Foo + /name/{name}: + get: + description: List all foos with a certain name + typeName: Foo + is: [ hasResponseCollection ] +/bars: + type: collection + typeName: Bar + /{barId}: + type: item + typeName: Bar + /fooId/{fooId}: + get: + description: Get all bars for the matching fooId + typeName: Bar + is: [ hasResponseCollection ] \ No newline at end of file diff --git a/raml/modularization/api.raml b/raml/modularization/api.raml new file mode 100644 index 0000000000..184027cd26 --- /dev/null +++ b/raml/modularization/api.raml @@ -0,0 +1,47 @@ +#%RAML 1.0 +title: Baeldung Foo REST Services API +uses: + security: !include libraries/security.raml +version: v1 +protocols: [ HTTPS ] +baseUri: http://rest-api.baeldung.com/api/{version} +mediaType: application/json +securedBy: [ security.basicAuth ] +types: + Foo: !include types/Foo.raml + Bar: !include types/Bar.raml + Error: !include types/Error.raml +resourceTypes: + - collection: !include resourceTypes/collection.raml + - item: !include resourceTypes/item.raml +traits: + - hasRequestItem: !include traits/hasRequestItem.raml + - hasResponseItem: !include traits/hasResponseItem.raml + - hasResponseCollection: !include traits/hasResponseCollection.raml + - hasNotFound: !include traits/hasNotFound.raml +/foos: + type: collection + typeName: Foo + get: + queryParameters: + name?: string + ownerName?: string + /{fooId}: + type: item + typeName: Foo + /name/{name}: + get: + description: List all foos with a certain name + typeName: Foo + is: [ hasResponseCollection ] +/bars: + type: collection + typeName: Bar + /{barId}: + type: item + typeName: Bar + /fooId/{fooId}: + get: + description: Get all bars for the matching fooId + typeName: Bar + is: [ hasResponseCollection ] \ No newline at end of file diff --git a/raml/modularization/examples/Bar.json b/raml/modularization/examples/Bar.json new file mode 100644 index 0000000000..0ee1b34edb --- /dev/null +++ b/raml/modularization/examples/Bar.json @@ -0,0 +1,6 @@ +{ + "id" : 1, + "name" : "First Bar", + "city" : "Austin", + "fooId" : 2 +} \ No newline at end of file diff --git a/raml/modularization/examples/Bars.json b/raml/modularization/examples/Bars.json new file mode 100644 index 0000000000..89ea875432 --- /dev/null +++ b/raml/modularization/examples/Bars.json @@ -0,0 +1,19 @@ +[ + { + "id" : 1, + "name" : "First Bar", + "city" : "Austin", + "fooId" : 2 + }, + { + "id" : 2, + "name" : "Second Bar", + "city" : "Dallas", + "fooId" : 1 + }, + { + "id" : 3, + "name" : "Third Bar", + "fooId" : 2 + } +] \ No newline at end of file diff --git a/raml/modularization/examples/Error.json b/raml/modularization/examples/Error.json new file mode 100644 index 0000000000..dca56da7c2 --- /dev/null +++ b/raml/modularization/examples/Error.json @@ -0,0 +1,4 @@ +{ + "message" : "Not found", + "code" : 1001 +} \ No newline at end of file diff --git a/raml/modularization/examples/Foo.json b/raml/modularization/examples/Foo.json new file mode 100644 index 0000000000..1b1b8c891e --- /dev/null +++ b/raml/modularization/examples/Foo.json @@ -0,0 +1,4 @@ +{ + "id" : 1, + "name" : "First Foo" +} \ No newline at end of file diff --git a/raml/modularization/examples/Foos.json b/raml/modularization/examples/Foos.json new file mode 100644 index 0000000000..74f64689f0 --- /dev/null +++ b/raml/modularization/examples/Foos.json @@ -0,0 +1,16 @@ +[ + { + "id" : 1, + "name" : "First Foo", + "ownerName" : "Jack Robinson" + }, + { + "id" : 2, + "name" : "Second Foo" + }, + { + "id" : 3, + "name" : "Third Foo", + "ownerName" : "Chuck Norris" + } +] \ No newline at end of file diff --git a/raml/modularization/extensions/en_US/additionalResources.raml b/raml/modularization/extensions/en_US/additionalResources.raml new file mode 100644 index 0000000000..20c6851f23 --- /dev/null +++ b/raml/modularization/extensions/en_US/additionalResources.raml @@ -0,0 +1,16 @@ +#%RAML 1.0 Extension +# File located at: +# /extensions/en_US/additionalResources.raml +masterRef: /api.raml +usage: This extension defines additional resources for version 2 of the API. +version: v2 +/foos: + /bar/{barId}: + get: + description: | + Get the foo that is related to the bar having barId = {barId} + typeName: Foo + queryParameters: + barId?: integer + typeName: Foo + is: [ hasResponseItem ] diff --git a/raml/modularization/libraries/dataTypes.raml b/raml/modularization/libraries/dataTypes.raml new file mode 100644 index 0000000000..8a240e62dc --- /dev/null +++ b/raml/modularization/libraries/dataTypes.raml @@ -0,0 +1,19 @@ +#%RAML 1.0 Library +# This is the file /libraries/dataTypes.raml +usage: This library defines the data types for the API +types: + Foo: + properties: + id: integer + name: string + ownerName?: string + Bar: + properties: + id: integer + name: string + city?: string + fooId: integer + Error: + properties: + code: integer + message: string diff --git a/raml/modularization/libraries/resourceTypes.raml b/raml/modularization/libraries/resourceTypes.raml new file mode 100644 index 0000000000..681ff710d6 --- /dev/null +++ b/raml/modularization/libraries/resourceTypes.raml @@ -0,0 +1,32 @@ +#%RAML 1.0 Library +# This is the file /libraries/resourceTypes.raml +usage: This library defines the resource types for the API +uses: + myTraits: !include traits.raml +resourceTypes: + collection: + usage: Use this resourceType to represent a collection of items + description: A collection of <> + get: + description: | + Get all <>, + optionally filtered + is: [ myTraits.hasResponseCollection ] + post: + description: | + Create a new <> + is: [ myTraits.hasRequestItem ] + item: + usage: Use this resourceType to represent any single item + description: A single <> + get: + description: Get a <> by <> + is: [ myTraits.hasResponseItem, myTraits.hasNotFound ] + put: + description: Update a <> by <> + is: [ myTraits.hasRequestItem, myTraits.hasResponseItem, myTraits.hasNotFound ] + delete: + description: Delete a <> by <> + is: [ myTraits.hasNotFound ] + responses: + 204: diff --git a/raml/modularization/libraries/securitySchemes.raml b/raml/modularization/libraries/securitySchemes.raml new file mode 100644 index 0000000000..621c6ac975 --- /dev/null +++ b/raml/modularization/libraries/securitySchemes.raml @@ -0,0 +1,20 @@ +#%RAML 1.0 Library +# This is the file /libraries/securitySchemes.raml +securitySchemes: + - basicAuth: + description: Each request must contain the headers necessary for + basic authentication + type: Basic Authentication + describedBy: + headers: + Authorization: + description: | + Used to send the Base64 encoded "username:password" + credentials + type: string + responses: + 401: + description: | + Unauthorized. Either the provided username and password + combination is invalid, or the user is not allowed to + access the content provided by the requested URL. diff --git a/raml/modularization/libraries/traits.raml b/raml/modularization/libraries/traits.raml new file mode 100644 index 0000000000..c101d94c02 --- /dev/null +++ b/raml/modularization/libraries/traits.raml @@ -0,0 +1,33 @@ +#%RAML 1.0 Library +# This is the file /libraries/traits.raml +usage: This library defines some basic traits +traits: + hasRequestItem: + usage: Use this trait for resources whose request body is a single item + body: + application/json: + type: <> + hasResponseItem: + usage: Use this trait for resources whose response body is a single item + responses: + 200: + body: + application/json: + type: <> + example: !include /examples/<>.json + hasResponseCollection: + usage: Use this trait for resources whose response body is a collection of items + responses: + 200: + body: + application/json: + type: <>[] + example: !include /examples/<>.json + hasNotFound: + usage: Use this trait for resources that could respond with a 404 status + responses: + 404: + body: + application/json: + type: Error + example: !include /examples/Error.json diff --git a/raml/modularization/overlays/es_ES/additionalResources.raml b/raml/modularization/overlays/es_ES/additionalResources.raml new file mode 100644 index 0000000000..e8748fd726 --- /dev/null +++ b/raml/modularization/overlays/es_ES/additionalResources.raml @@ -0,0 +1,13 @@ +#%RAML 1.0 Overlay +# Archivo situado en: +# /overlays/es_ES/additionalResources.raml +masterRef: /api.raml +usage: | + Se trata de un español demasiado que describe los recursos adicionales + para la versión 2 del API. +version: v2 +/foos: + /bar/{barId}: + get: + description: | + Obtener el foo que se relaciona con el bar tomando barId = {barId} diff --git a/raml/modularization/overlays/es_ES/documentationItems.raml b/raml/modularization/overlays/es_ES/documentationItems.raml new file mode 100644 index 0000000000..dc6ca3eaef --- /dev/null +++ b/raml/modularization/overlays/es_ES/documentationItems.raml @@ -0,0 +1,23 @@ +#%RAML 1.0 Overlay +# File located at (archivo situado en): +# /overlays/es_ES/documentationItems.raml +masterRef: /api.raml +usage: | + To provide user documentation and other descriptive text in Spanish + (Para proporcionar la documentación del usuario y otro texto descriptivo en español) +title: API para servicios REST utilizados en los tutoriales RAML en Baeldung.com +documentation: + - title: Descripción general + - content: | + Este documento define la interfaz para los servicios REST + utilizados en la popular serie de RAML Tutorial en Baeldung.com + - title: Renuncia + - content: | + Todos los nombres usados ​​en esta definición son pura ficción. + Cualquier similitud entre los nombres utilizados en este tutorial + y los de las personas reales, ya sea vivo o muerto, + no son más que coincidenta. + + - title: Derechos de autor + - content: | + Derechos de autor 2016 por Baeldung.com. Todos los derechos reservados. diff --git a/raml/modularization/resourceTypes/collection.raml b/raml/modularization/resourceTypes/collection.raml new file mode 100644 index 0000000000..0cab417f14 --- /dev/null +++ b/raml/modularization/resourceTypes/collection.raml @@ -0,0 +1,12 @@ +#%RAML 1.0 ResourceType +usage: Use this resourceType to represent a collection of items +description: A collection of <> +get: + description: | + Get all <>, + optionally filtered + is: [ hasResponseCollection ] +post: + description: | + Create a new <> + is: [ hasRequestItem ] diff --git a/raml/modularization/resourceTypes/item.raml b/raml/modularization/resourceTypes/item.raml new file mode 100644 index 0000000000..59f057ca98 --- /dev/null +++ b/raml/modularization/resourceTypes/item.raml @@ -0,0 +1,14 @@ +#%RAML 1.0 ResourceType +usage: Use this resourceType to represent any single item +description: A single <> +get: + description: Get a <> by <> + is: [ hasResponseItem, hasNotFound ] +put: + description: Update a <> by <> + is: [ hasRequestItem, hasResponseItem, hasNotFound ] +delete: + description: Delete a <> by <> + is: [ hasNotFound ] + responses: + 204: diff --git a/raml/modularization/traits/hasNotFound.raml b/raml/modularization/traits/hasNotFound.raml new file mode 100644 index 0000000000..8d2d940c03 --- /dev/null +++ b/raml/modularization/traits/hasNotFound.raml @@ -0,0 +1,8 @@ +#%RAML 1.0 Trait +usage: Use this trait for resources that could respond with a 404 status +responses: + 404: + body: + application/json: + type: Error + example: !include /examples/Error.json diff --git a/raml/modularization/traits/hasRequestItem.raml b/raml/modularization/traits/hasRequestItem.raml new file mode 100644 index 0000000000..06281781c0 --- /dev/null +++ b/raml/modularization/traits/hasRequestItem.raml @@ -0,0 +1,5 @@ +#%RAML 1.0 Trait +usage: Use this trait for resources whose request body is a single item +body: + application/json: + type: <> diff --git a/raml/modularization/traits/hasResponseCollection.raml b/raml/modularization/traits/hasResponseCollection.raml new file mode 100644 index 0000000000..47dc1c2d5f --- /dev/null +++ b/raml/modularization/traits/hasResponseCollection.raml @@ -0,0 +1,8 @@ +#%RAML 1.0 Trait +usage: Use this trait for resources whose response body is a collection of items +responses: + 200: + body: + application/json: + type: <>[] + example: !include /examples/<>.json diff --git a/raml/modularization/traits/hasResponseItem.raml b/raml/modularization/traits/hasResponseItem.raml new file mode 100644 index 0000000000..94d3ba0756 --- /dev/null +++ b/raml/modularization/traits/hasResponseItem.raml @@ -0,0 +1,8 @@ +#%RAML 1.0 Trait +usage: Use this trait for resources whose response body is a single item +responses: + 200: + body: + application/json: + type: <> + example: !include /examples/<>.json diff --git a/raml/modularization/types/Bar.raml b/raml/modularization/types/Bar.raml new file mode 100644 index 0000000000..92255a75fe --- /dev/null +++ b/raml/modularization/types/Bar.raml @@ -0,0 +1,7 @@ +#%RAML 1.0 DataType + +properties: + id: integer + name: string + city?: string + fooId: integer diff --git a/raml/modularization/types/Error.raml b/raml/modularization/types/Error.raml new file mode 100644 index 0000000000..8d54b5f181 --- /dev/null +++ b/raml/modularization/types/Error.raml @@ -0,0 +1,5 @@ +#%RAML 1.0 DataType + + properties: + code: integer + message: string diff --git a/raml/modularization/types/Foo.raml b/raml/modularization/types/Foo.raml new file mode 100644 index 0000000000..1702865e05 --- /dev/null +++ b/raml/modularization/types/Foo.raml @@ -0,0 +1,6 @@ +#%RAML 1.0 DataType + +properties: + id: integer + name: string + ownerName?: string diff --git a/raml/resource-types-and-traits/api-before-resource-types-and-traits.raml b/raml/resource-types-and-traits/api-before-resource-types-and-traits.raml new file mode 100644 index 0000000000..652a3d10f7 --- /dev/null +++ b/raml/resource-types-and-traits/api-before-resource-types-and-traits.raml @@ -0,0 +1,177 @@ +#%RAML 1.0 +title: Baeldung Foo REST Services API +version: v1 +protocols: [ HTTPS ] +baseUri: http://rest-api.baeldung.com/api/{version} +mediaType: application/json +securedBy: basicAuth +securitySchemes: + - basicAuth: + description: Each request must contain the headers necessary for + basic authentication + type: Basic Authentication + describedBy: + headers: + Authorization: + description: Used to send the Base64 encoded "username:password" + credentials + type: string + responses: + 401: + description: | + Unauthorized. Either the provided username and password + combination is invalid, or the user is not allowed to access + the content provided by the requested URL. +types: + Foo: !include types/Foo.raml + Bar: !include types/Bar.raml + Error: !include types/Error.raml +traits: +/foos: + get: + description: List all foos matching query criteria, if provided; + otherwise list all foos + queryParameters: + name?: string + ownerName?: string + responses: + 200: + body: + application/json: + type: Foo[] + example: !include examples/Foos.json + post: + description: Create a new foo + body: + application/json: + type: Foo + example: !include examples/Foo.json + responses: + 201: + body: + application/json: + type: Foo + example: !include examples/Foo.json + /{fooId}: + get: + description: Get a foo by fooId + responses: + 200: + body: + application/json: + type: Foo + example: !include examples/Foo.json + 404: + body: + application/json: + type: Error + example: !include examples/Error.json + put: + description: Update a foo by fooId + body: + application/json: + type: Foo + example: !include examples/Foo.json + responses: + 200: + body: + application/json: + type: Foo + example: !include examples/Foo.json + 404: + body: + application/json: + type: Error + example: !include examples/Error.json + delete: + description: Delete a foo by fooId + responses: + 204: + 404: + body: + application/json: + type: Error + example: !include examples/Error.json + /name/{name}: + get: + description: List all foos with a certain name + responses: + 200: + body: + application/json: + type: Foo[] + example: !include examples/Foos.json +/bars: + get: + description: List all bars matching query criteria, if provided; + otherwise list all bars + queryParameters: + name?: string + ownerName?: string + responses: + 200: + body: + application/json: + type: Bar[] + example: !include examples/Bars.json + post: + description: Create a new bar + body: + application/json: + type: Bar + example: !include examples/Bar.json + responses: + 201: + body: + application/json: + type: Bar + example: !include examples/Bar.json + /{barId}: + get: + description: Get a bar by barId + responses: + 200: + body: + application/json: + type: Bar + example: !include examples/Bar.json + 404: + body: + application/json: + type: Error + example: !include examples/Error.json + put: + description: Update a bar by barId + body: + application/json: + type: Bar + example: !include examples/Bar.json + responses: + 200: + body: + application/json: + type: Bar + example: !include examples/Bar.json + 404: + body: + application/json: + type: Error + example: !include examples/Error.json + delete: + description: Delete a bar by barId + responses: + 204: + 404: + body: + application/json: + type: Error + example: !include examples/Error.json + /fooId/{fooId}: + get: + description: Get all bars for the matching fooId + responses: + 200: + body: + application/json: + type: Bar[] + example: !include examples/Bars.json \ No newline at end of file diff --git a/raml/resource-types-and-traits/api.raml b/raml/resource-types-and-traits/api.raml new file mode 100644 index 0000000000..c07f0cba28 --- /dev/null +++ b/raml/resource-types-and-traits/api.raml @@ -0,0 +1,108 @@ +#%RAML 1.0 +title: Baeldung Foo REST Services API +version: v1 +protocols: [ HTTPS ] +baseUri: http://rest-api.baeldung.com/api/{version} +mediaType: application/json +securedBy: basicAuth +securitySchemes: + - basicAuth: + description: Each request must contain the headers necessary for + basic authentication + type: Basic Authentication + describedBy: + headers: + Authorization: + description: | + Used to send the Base64 encoded "username:password" + credentials + type: string + responses: + 401: + description: | + Unauthorized. Either the provided username and password + combination is invalid, or the user is not allowed to + access the content provided by the requested URL. +types: + Foo: !include types/Foo.raml + Bar: !include types/Bar.raml + Error: !include types/Error.raml +resourceTypes: + - collection: + usage: Use this resourceType to represent a collection of items + description: A collection of <> + get: + description: | + Get all <>, + optionally filtered + is: [ hasResponseCollection ] + post: + description: | + Create a new <> + is: [ hasRequestItem ] + - item: + usage: Use this resourceType to represent any single item + description: A single <> + get: + description: Get a <> by <> + is: [ hasResponseItem, hasNotFound ] + put: + description: Update a <> by <> + is: [ hasRequestItem, hasResponseItem, hasNotFound ] + delete: + description: Delete a <> by <> + is: [ hasNotFound ] + responses: + 204: +traits: + - hasRequestItem: + body: + application/json: + type: <> + - hasResponseItem: + responses: + 200: + body: + application/json: + type: <> + example: !include examples/<>.json + - hasResponseCollection: + responses: + 200: + body: + application/json: + type: <>[] + example: !include examples/<>.json + - hasNotFound: + responses: + 404: + body: + application/json: + type: Error + example: !include examples/Error.json +/foos: + type: collection + typeName: Foo + get: + queryParameters: + name?: string + ownerName?: string + /{fooId}: + type: item + typeName: Foo + /name/{name}: + get: + description: List all foos with a certain name + typeName: Foo + is: [ hasResponseCollection ] +/bars: + type: collection + typeName: Bar + /{barId}: + type: item + typeName: Bar + /fooId/{fooId}: + get: + description: Get all bars for the matching fooId + typeName: Bar + is: [ hasResponseCollection ] \ No newline at end of file diff --git a/raml/resource-types-and-traits/examples/Bar.json b/raml/resource-types-and-traits/examples/Bar.json new file mode 100644 index 0000000000..0ee1b34edb --- /dev/null +++ b/raml/resource-types-and-traits/examples/Bar.json @@ -0,0 +1,6 @@ +{ + "id" : 1, + "name" : "First Bar", + "city" : "Austin", + "fooId" : 2 +} \ No newline at end of file diff --git a/raml/resource-types-and-traits/examples/Bars.json b/raml/resource-types-and-traits/examples/Bars.json new file mode 100644 index 0000000000..89ea875432 --- /dev/null +++ b/raml/resource-types-and-traits/examples/Bars.json @@ -0,0 +1,19 @@ +[ + { + "id" : 1, + "name" : "First Bar", + "city" : "Austin", + "fooId" : 2 + }, + { + "id" : 2, + "name" : "Second Bar", + "city" : "Dallas", + "fooId" : 1 + }, + { + "id" : 3, + "name" : "Third Bar", + "fooId" : 2 + } +] \ No newline at end of file diff --git a/raml/resource-types-and-traits/examples/Error.json b/raml/resource-types-and-traits/examples/Error.json new file mode 100644 index 0000000000..dca56da7c2 --- /dev/null +++ b/raml/resource-types-and-traits/examples/Error.json @@ -0,0 +1,4 @@ +{ + "message" : "Not found", + "code" : 1001 +} \ No newline at end of file diff --git a/raml/resource-types-and-traits/examples/Foo.json b/raml/resource-types-and-traits/examples/Foo.json new file mode 100644 index 0000000000..1b1b8c891e --- /dev/null +++ b/raml/resource-types-and-traits/examples/Foo.json @@ -0,0 +1,4 @@ +{ + "id" : 1, + "name" : "First Foo" +} \ No newline at end of file diff --git a/raml/resource-types-and-traits/examples/Foos.json b/raml/resource-types-and-traits/examples/Foos.json new file mode 100644 index 0000000000..74f64689f0 --- /dev/null +++ b/raml/resource-types-and-traits/examples/Foos.json @@ -0,0 +1,16 @@ +[ + { + "id" : 1, + "name" : "First Foo", + "ownerName" : "Jack Robinson" + }, + { + "id" : 2, + "name" : "Second Foo" + }, + { + "id" : 3, + "name" : "Third Foo", + "ownerName" : "Chuck Norris" + } +] \ No newline at end of file diff --git a/raml/resource-types-and-traits/types/Bar.raml b/raml/resource-types-and-traits/types/Bar.raml new file mode 100644 index 0000000000..92255a75fe --- /dev/null +++ b/raml/resource-types-and-traits/types/Bar.raml @@ -0,0 +1,7 @@ +#%RAML 1.0 DataType + +properties: + id: integer + name: string + city?: string + fooId: integer diff --git a/raml/resource-types-and-traits/types/Error.raml b/raml/resource-types-and-traits/types/Error.raml new file mode 100644 index 0000000000..8d54b5f181 --- /dev/null +++ b/raml/resource-types-and-traits/types/Error.raml @@ -0,0 +1,5 @@ +#%RAML 1.0 DataType + + properties: + code: integer + message: string diff --git a/raml/resource-types-and-traits/types/Foo.raml b/raml/resource-types-and-traits/types/Foo.raml new file mode 100644 index 0000000000..1702865e05 --- /dev/null +++ b/raml/resource-types-and-traits/types/Foo.raml @@ -0,0 +1,6 @@ +#%RAML 1.0 DataType + +properties: + id: integer + name: string + ownerName?: string diff --git a/rest-testing/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch b/rest-testing/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch deleted file mode 100644 index 627021fb96..0000000000 --- a/rest-testing/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/rest-testing/.gitignore b/rest-testing/.gitignore index 83c05e60c8..601d2281e5 100644 --- a/rest-testing/.gitignore +++ b/rest-testing/.gitignore @@ -10,4 +10,8 @@ # Packaged files # *.jar *.war -*.ear \ No newline at end of file +*.ear + +.externalToolBuilders +.settings +.springBeans \ No newline at end of file diff --git a/rest-testing/.settings/.jsdtscope b/rest-testing/.settings/.jsdtscope deleted file mode 100644 index 7b3f0c8b9f..0000000000 --- a/rest-testing/.settings/.jsdtscope +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/rest-testing/.settings/org.eclipse.jdt.core.prefs b/rest-testing/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index b126d6476b..0000000000 --- a/rest-testing/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,95 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignore -org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull -org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault -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.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 -org.eclipse.jdt.core.compiler.problem.comparingIdentical=warning -org.eclipse.jdt.core.compiler.problem.deadCode=warning -org.eclipse.jdt.core.compiler.problem.deprecation=warning -org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled -org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled -org.eclipse.jdt.core.compiler.problem.discouragedReference=warning -org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore -org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore -org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled -org.eclipse.jdt.core.compiler.problem.fieldHiding=error -org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning -org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning -org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning -org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts=disabled -org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning -org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=ignore -org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore -org.eclipse.jdt.core.compiler.problem.localVariableHiding=error -org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning -org.eclipse.jdt.core.compiler.problem.missingDefaultCase=ignore -org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore -org.eclipse.jdt.core.compiler.problem.missingEnumCaseDespiteDefault=disabled -org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=ignore -org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=ignore -org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled -org.eclipse.jdt.core.compiler.problem.missingSerialVersion=warning -org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignore -org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning -org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning -org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore -org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=error -org.eclipse.jdt.core.compiler.problem.nullReference=warning -org.eclipse.jdt.core.compiler.problem.nullSpecViolation=error -org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=warning -org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning -org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore -org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=ignore -org.eclipse.jdt.core.compiler.problem.potentialNullReference=ignore -org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=ignore -org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning -org.eclipse.jdt.core.compiler.problem.redundantNullAnnotation=warning -org.eclipse.jdt.core.compiler.problem.redundantNullCheck=ignore -org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=ignore -org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=ignore -org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore -org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=ignore -org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled -org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning -org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled -org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled -org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore -org.eclipse.jdt.core.compiler.problem.typeParameterHiding=error -org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=enabled -org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning -org.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning -org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore -org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning -org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore -org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=ignore -org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=ignore -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=enabled -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled -org.eclipse.jdt.core.compiler.problem.unusedImport=warning -org.eclipse.jdt.core.compiler.problem.unusedLabel=warning -org.eclipse.jdt.core.compiler.problem.unusedLocal=warning -org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=ignore -org.eclipse.jdt.core.compiler.problem.unusedParameter=ignore -org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference=enabled -org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled -org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled -org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning -org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning -org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning -org.eclipse.jdt.core.compiler.source=1.8 diff --git a/rest-testing/.settings/org.eclipse.jdt.ui.prefs b/rest-testing/.settings/org.eclipse.jdt.ui.prefs deleted file mode 100644 index 471e9b0d81..0000000000 --- a/rest-testing/.settings/org.eclipse.jdt.ui.prefs +++ /dev/null @@ -1,55 +0,0 @@ -#Sat Jan 21 23:04:06 EET 2012 -eclipse.preferences.version=1 -editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true -sp_cleanup.add_default_serial_version_id=true -sp_cleanup.add_generated_serial_version_id=false -sp_cleanup.add_missing_annotations=true -sp_cleanup.add_missing_deprecated_annotations=true -sp_cleanup.add_missing_methods=false -sp_cleanup.add_missing_nls_tags=false -sp_cleanup.add_missing_override_annotations=true -sp_cleanup.add_missing_override_annotations_interface_methods=true -sp_cleanup.add_serial_version_id=false -sp_cleanup.always_use_blocks=true -sp_cleanup.always_use_parentheses_in_expressions=true -sp_cleanup.always_use_this_for_non_static_field_access=false -sp_cleanup.always_use_this_for_non_static_method_access=false -sp_cleanup.convert_to_enhanced_for_loop=true -sp_cleanup.correct_indentation=true -sp_cleanup.format_source_code=true -sp_cleanup.format_source_code_changes_only=true -sp_cleanup.make_local_variable_final=true -sp_cleanup.make_parameters_final=true -sp_cleanup.make_private_fields_final=false -sp_cleanup.make_type_abstract_if_missing_method=false -sp_cleanup.make_variable_declarations_final=true -sp_cleanup.never_use_blocks=false -sp_cleanup.never_use_parentheses_in_expressions=false -sp_cleanup.on_save_use_additional_actions=true -sp_cleanup.organize_imports=true -sp_cleanup.qualify_static_field_accesses_with_declaring_class=false -sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true -sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true -sp_cleanup.qualify_static_member_accesses_with_declaring_class=true -sp_cleanup.qualify_static_method_accesses_with_declaring_class=false -sp_cleanup.remove_private_constructors=true -sp_cleanup.remove_trailing_whitespaces=true -sp_cleanup.remove_trailing_whitespaces_all=true -sp_cleanup.remove_trailing_whitespaces_ignore_empty=false -sp_cleanup.remove_unnecessary_casts=true -sp_cleanup.remove_unnecessary_nls_tags=false -sp_cleanup.remove_unused_imports=true -sp_cleanup.remove_unused_local_variables=false -sp_cleanup.remove_unused_private_fields=true -sp_cleanup.remove_unused_private_members=false -sp_cleanup.remove_unused_private_methods=true -sp_cleanup.remove_unused_private_types=true -sp_cleanup.sort_members=false -sp_cleanup.sort_members_all=false -sp_cleanup.use_blocks=false -sp_cleanup.use_blocks_only_for_return_and_throw=false -sp_cleanup.use_parentheses_in_expressions=false -sp_cleanup.use_this_for_non_static_field_access=true -sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true -sp_cleanup.use_this_for_non_static_method_access=true -sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true diff --git a/rest-testing/.settings/org.eclipse.m2e.core.prefs b/rest-testing/.settings/org.eclipse.m2e.core.prefs deleted file mode 100644 index f897a7f1cb..0000000000 --- a/rest-testing/.settings/org.eclipse.m2e.core.prefs +++ /dev/null @@ -1,4 +0,0 @@ -activeProfiles= -eclipse.preferences.version=1 -resolveWorkspaceProjects=true -version=1 diff --git a/rest-testing/.settings/org.eclipse.m2e.wtp.prefs b/rest-testing/.settings/org.eclipse.m2e.wtp.prefs deleted file mode 100644 index ef86089622..0000000000 --- a/rest-testing/.settings/org.eclipse.m2e.wtp.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.m2e.wtp.enabledProjectSpecificPrefs=false diff --git a/rest-testing/.settings/org.eclipse.wst.common.component b/rest-testing/.settings/org.eclipse.wst.common.component deleted file mode 100644 index e98377cb0f..0000000000 --- a/rest-testing/.settings/org.eclipse.wst.common.component +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/rest-testing/.settings/org.eclipse.wst.common.project.facet.core.xml b/rest-testing/.settings/org.eclipse.wst.common.project.facet.core.xml deleted file mode 100644 index f4ef8aa0a5..0000000000 --- a/rest-testing/.settings/org.eclipse.wst.common.project.facet.core.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - diff --git a/rest-testing/.settings/org.eclipse.wst.jsdt.ui.superType.container b/rest-testing/.settings/org.eclipse.wst.jsdt.ui.superType.container deleted file mode 100644 index 3bd5d0a480..0000000000 --- a/rest-testing/.settings/org.eclipse.wst.jsdt.ui.superType.container +++ /dev/null @@ -1 +0,0 @@ -org.eclipse.wst.jsdt.launching.baseBrowserLibrary \ No newline at end of file diff --git a/rest-testing/.settings/org.eclipse.wst.jsdt.ui.superType.name b/rest-testing/.settings/org.eclipse.wst.jsdt.ui.superType.name deleted file mode 100644 index 05bd71b6ec..0000000000 --- a/rest-testing/.settings/org.eclipse.wst.jsdt.ui.superType.name +++ /dev/null @@ -1 +0,0 @@ -Window \ No newline at end of file diff --git a/rest-testing/.settings/org.eclipse.wst.validation.prefs b/rest-testing/.settings/org.eclipse.wst.validation.prefs deleted file mode 100644 index cacf5451ae..0000000000 --- a/rest-testing/.settings/org.eclipse.wst.validation.prefs +++ /dev/null @@ -1,14 +0,0 @@ -DELEGATES_PREFERENCE=delegateValidatorList -USER_BUILD_PREFERENCE=enabledBuildValidatorListorg.eclipse.jst.j2ee.internal.classpathdep.ClasspathDependencyValidator; -USER_MANUAL_PREFERENCE=enabledManualValidatorListorg.eclipse.jst.j2ee.internal.classpathdep.ClasspathDependencyValidator; -USER_PREFERENCE=overrideGlobalPreferencestruedisableAllValidationfalseversion1.2.303.v201202090300 -eclipse.preferences.version=1 -override=true -suspend=false -vals/org.eclipse.jst.jsf.ui.JSFAppConfigValidator/global=FF01 -vals/org.eclipse.jst.jsp.core.JSPBatchValidator/global=FF01 -vals/org.eclipse.jst.jsp.core.JSPContentValidator/global=FF01 -vals/org.eclipse.jst.jsp.core.TLDValidator/global=FF01 -vals/org.eclipse.wst.dtd.core.dtdDTDValidator/global=FF01 -vals/org.eclipse.wst.jsdt.web.core.JsBatchValidator/global=TF02 -vf.version=3 diff --git a/rest-testing/.settings/org.eclipse.wst.ws.service.policy.prefs b/rest-testing/.settings/org.eclipse.wst.ws.service.policy.prefs deleted file mode 100644 index 9cfcabe16f..0000000000 --- a/rest-testing/.settings/org.eclipse.wst.ws.service.policy.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.wst.ws.service.policy.projectEnabled=false diff --git a/rest-testing/.springBeans b/rest-testing/.springBeans deleted file mode 100644 index a79097f40d..0000000000 --- a/rest-testing/.springBeans +++ /dev/null @@ -1,14 +0,0 @@ - - - 1 - - - - - - - src/main/webapp/WEB-INF/api-servlet.xml - - - - 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/rest-testing/pom.xml b/rest-testing/pom.xml index 488a8a3cdb..652f2ab601 100644 --- a/rest-testing/pom.xml +++ b/rest-testing/pom.xml @@ -1,176 +1,191 @@ - - 4.0.0 - org.baeldung - rest-testing - 0.1-SNAPSHOT + + 4.0.0 + com.baeldung + rest-testing + 0.1-SNAPSHOT - rest-testing + rest-testing - + - + - - com.google.guava - guava - ${guava.version} - + + com.google.guava + guava + ${guava.version} + - - commons-io - commons-io - 2.4 - + + commons-io + commons-io + 2.4 + - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + - + - - org.apache.httpcomponents - httpclient - ${httpclient.version} - - - org.apache.httpcomponents - httpcore - ${httpcore.version} - + + org.apache.httpcomponents + httpclient + ${httpclient.version} + + + org.apache.httpcomponents + httpcore + ${httpcore.version} + - + - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + - + - - 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} - + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + runtime + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + - + - - junit - junit-dep - ${junit.version} - test - + + junit + junit + ${junit.version} + test + - - org.hamcrest - hamcrest-core - ${org.hamcrest.version} - test - - - org.hamcrest - hamcrest-library - ${org.hamcrest.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.mockito + mockito-core + ${mockito.version} + test + + + com.github.tomakehurst + wiremock + 1.58 + test + - + + info.cukes + cucumber-java + 1.2.4 + test + + + info.cukes + cucumber-junit + 1.2.4 + + - - rest-testing - - - src/main/resources - true - - + + rest-testing + + + src/main/resources + true + + - + - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.8 - 1.8 - - + + 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} - + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + - + - + - - - 4.1.5.RELEASE + + + 2.7.2 - - 2.4.4 + + 1.7.13 + 1.1.3 - - 1.7.12 - 1.1.3 + + 5.1.3.Final - - 5.1.3.Final + + 19.0 + 3.4 - - 18.0 - 3.4 + + 1.3 + 4.12 + 1.10.19 - - 1.3 - 4.11 - 1.10.19 + 4.4.1 + 4.5 - 4.4.1 - 4.5 + 2.9.0 - 2.4.1 + + 3.5.1 + 2.6 + 2.19.1 + 2.7 + 1.4.18 - - 3.3 - 2.6 - 2.18.1 - 2.7 - 1.4.14 - - + \ No newline at end of file diff --git a/rest-testing/src/main/resources/Feature/cucumber.feature b/rest-testing/src/main/resources/Feature/cucumber.feature new file mode 100644 index 0000000000..99dd8249fe --- /dev/null +++ b/rest-testing/src/main/resources/Feature/cucumber.feature @@ -0,0 +1,10 @@ +Feature: Testing a REST API + Users should be able to submit GET and POST requests to a web service, represented by WireMock + + Scenario: Data Upload to a web service + When users upload data on a project + Then the server should handle it and return a success status + + Scenario: Data retrieval from a web service + When users want to get information on the Cucumber project + Then the requested data is returned \ No newline at end of file diff --git a/rest-testing/src/main/resources/cucumber.json b/rest-testing/src/main/resources/cucumber.json new file mode 100644 index 0000000000..38ebe066ac --- /dev/null +++ b/rest-testing/src/main/resources/cucumber.json @@ -0,0 +1,14 @@ +{ + "testing-framework": "cucumber", + "supported-language": + [ + "Ruby", + "Java", + "Javascript", + "PHP", + "Python", + "C++" + ], + + "website": "cucumber.io" +} \ No newline at end of file diff --git a/rest-testing/src/main/resources/wiremock_intro.json b/rest-testing/src/main/resources/wiremock_intro.json new file mode 100644 index 0000000000..ece2d35907 --- /dev/null +++ b/rest-testing/src/main/resources/wiremock_intro.json @@ -0,0 +1,5 @@ +{ + "testing-library": "WireMock", + "creator": "Tom Akehurst", + "website": "wiremock.org" +} \ No newline at end of file diff --git a/rest-testing/src/main/webapp/WEB-INF/api-servlet.xml b/rest-testing/src/main/webapp/WEB-INF/api-servlet.xml deleted file mode 100644 index 4c74be8912..0000000000 --- a/rest-testing/src/main/webapp/WEB-INF/api-servlet.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/rest-testing/src/main/webapp/WEB-INF/web.xml b/rest-testing/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index 935beae648..0000000000 --- a/rest-testing/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - - Spring MVC Application - - - - contextClass - - org.springframework.web.context.support.AnnotationConfigWebApplicationContext - - - - contextConfigLocation - org.baeldung.config - - - - org.springframework.web.context.ContextLoaderListener - - - - - api - org.springframework.web.servlet.DispatcherServlet - 1 - - - api - / - - - - - - - \ No newline at end of file diff --git a/rest-testing/src/test/java/com/baeldung/rest/cucumber/CucumberTest.java b/rest-testing/src/test/java/com/baeldung/rest/cucumber/CucumberTest.java new file mode 100644 index 0000000000..041de592e9 --- /dev/null +++ b/rest-testing/src/test/java/com/baeldung/rest/cucumber/CucumberTest.java @@ -0,0 +1,10 @@ +package com.baeldung.rest.cucumber; + +import org.junit.runner.RunWith; +import cucumber.api.CucumberOptions; +import cucumber.api.junit.Cucumber; + +@RunWith(Cucumber.class) +@CucumberOptions(features = "classpath:Feature") +public class CucumberTest { +} \ No newline at end of file diff --git a/rest-testing/src/test/java/com/baeldung/rest/cucumber/StepDefinition.java b/rest-testing/src/test/java/com/baeldung/rest/cucumber/StepDefinition.java new file mode 100644 index 0000000000..b461da8403 --- /dev/null +++ b/rest-testing/src/test/java/com/baeldung/rest/cucumber/StepDefinition.java @@ -0,0 +1,101 @@ +package com.baeldung.rest.cucumber; + +import com.github.tomakehurst.wiremock.WireMockServer; +import cucumber.api.java.en.Then; +import cucumber.api.java.en.When; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Scanner; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.configureFor; +import static com.github.tomakehurst.wiremock.client.WireMock.containing; +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.post; +import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static com.github.tomakehurst.wiremock.client.WireMock.verify; +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +public class StepDefinition { + + private static final String CREATE_PATH = "/create"; + private static final String APPLICATION_JSON = "application/json"; + + private final InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("cucumber.json"); + private final String jsonString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z").next(); + + private final WireMockServer wireMockServer = new WireMockServer(); + private final CloseableHttpClient httpClient = HttpClients.createDefault(); + + @When("^users upload data on a project$") + public void usersUploadDataOnAProject() throws IOException { + wireMockServer.start(); + + configureFor("localhost", 8080); + stubFor(post(urlEqualTo(CREATE_PATH)) + .withHeader("content-type", equalTo(APPLICATION_JSON)) + .withRequestBody(containing("testing-framework")) + .willReturn(aResponse().withStatus(200))); + + HttpPost request = new HttpPost("http://localhost:8080/create"); + StringEntity entity = new StringEntity(jsonString); + request.addHeader("content-type", APPLICATION_JSON); + request.setEntity(entity); + HttpResponse response = httpClient.execute(request); + + assertEquals(200, response.getStatusLine().getStatusCode()); + verify(postRequestedFor(urlEqualTo(CREATE_PATH)) + .withHeader("content-type", equalTo(APPLICATION_JSON))); + + wireMockServer.stop(); + } + + @When("^users want to get information on the (.+) project$") + public void usersGetInformationOnAProject(String projectName) throws IOException { + wireMockServer.start(); + + configureFor("localhost", 8080); + stubFor(get(urlEqualTo("/projects/cucumber")).withHeader("accept", equalTo(APPLICATION_JSON)) + .willReturn(aResponse().withBody(jsonString))); + + HttpGet request = new HttpGet("http://localhost:8080/projects/" + projectName.toLowerCase()); + request.addHeader("accept", APPLICATION_JSON); + HttpResponse httpResponse = httpClient.execute(request); + String responseString = convertResponseToString(httpResponse); + + assertThat(responseString, containsString("\"testing-framework\": \"cucumber\"")); + assertThat(responseString, containsString("\"website\": \"cucumber.io\"")); + verify(getRequestedFor(urlEqualTo("/projects/cucumber")).withHeader("accept", equalTo(APPLICATION_JSON))); + + wireMockServer.stop(); + } + + @Then("^the server should handle it and return a success status$") + public void theServerShouldReturnASuccessStatus() { + } + + @Then("^the requested data is returned$") + public void theRequestedDataIsReturned() { + } + + private String convertResponseToString(HttpResponse response) throws IOException { + InputStream responseStream = response.getEntity().getContent(); + Scanner scanner = new Scanner(responseStream, "UTF-8"); + String responseString = scanner.useDelimiter("\\Z").next(); + scanner.close(); + return responseString; + } +} \ No newline at end of file diff --git a/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/JUnitManaged.java b/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/JUnitManaged.java new file mode 100644 index 0000000000..664c3fac78 --- /dev/null +++ b/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/JUnitManaged.java @@ -0,0 +1,144 @@ +package com.baeldung.rest.wiremock.introduction; + +import com.github.tomakehurst.wiremock.junit.WireMockRule; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.junit.Rule; +import org.junit.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Scanner; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.containing; +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.matching; +import static com.github.tomakehurst.wiremock.client.WireMock.post; +import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching; +import static com.github.tomakehurst.wiremock.client.WireMock.verify; +import static org.junit.Assert.assertEquals; + +public class JUnitManaged { + + private static final String BAELDUNG_WIREMOCK_PATH = "/baeldung/wiremock"; + private static final String APPLICATION_JSON = "application/json"; + + @Rule + public WireMockRule wireMockRule = new WireMockRule(); + + @Test + public void givenJUnitManagedServer_whenMatchingURL_thenCorrect() throws IOException { + stubFor(get(urlPathMatching("/baeldung/.*")) + .willReturn(aResponse() + .withStatus(200) + .withHeader("Content-Type", APPLICATION_JSON) + .withBody("\"testing-library\": \"WireMock\""))); + + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpGet request = new HttpGet("http://localhost:8080/baeldung/wiremock"); + HttpResponse httpResponse = httpClient.execute(request); + String stringResponse = convertHttpResponseToString(httpResponse); + + verify(getRequestedFor(urlEqualTo(BAELDUNG_WIREMOCK_PATH))); + assertEquals(200, httpResponse.getStatusLine().getStatusCode()); + assertEquals(APPLICATION_JSON, httpResponse.getFirstHeader("Content-Type").getValue()); + assertEquals("\"testing-library\": \"WireMock\"", stringResponse); + } + + @Test + public void givenJUnitManagedServer_whenMatchingHeaders_thenCorrect() throws IOException { + stubFor(get(urlPathEqualTo(BAELDUNG_WIREMOCK_PATH)) + .withHeader("Accept", matching("text/.*")) + .willReturn(aResponse() + .withStatus(503) + .withHeader("Content-Type", "text/html") + .withBody("!!! Service Unavailable !!!"))); + + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpGet request = new HttpGet("http://localhost:8080/baeldung/wiremock"); + request.addHeader("Accept", "text/html"); + HttpResponse httpResponse = httpClient.execute(request); + String stringResponse = convertHttpResponseToString(httpResponse); + + verify(getRequestedFor(urlEqualTo(BAELDUNG_WIREMOCK_PATH))); + assertEquals(503, httpResponse.getStatusLine().getStatusCode()); + assertEquals("text/html", httpResponse.getFirstHeader("Content-Type").getValue()); + assertEquals("!!! Service Unavailable !!!", stringResponse); + } + + @Test + public void givenJUnitManagedServer_whenMatchingBody_thenCorrect() throws IOException { + stubFor(post(urlEqualTo(BAELDUNG_WIREMOCK_PATH)) + .withHeader("Content-Type", equalTo(APPLICATION_JSON)) + .withRequestBody(containing("\"testing-library\": \"WireMock\"")) + .withRequestBody(containing("\"creator\": \"Tom Akehurst\"")) + .withRequestBody(containing("\"website\": \"wiremock.org\"")) + .willReturn(aResponse().withStatus(200))); + + InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("wiremock_intro.json"); + String jsonString = convertInputStreamToString(jsonInputStream); + StringEntity entity = new StringEntity(jsonString); + + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpPost request = new HttpPost("http://localhost:8080/baeldung/wiremock"); + request.addHeader("Content-Type", APPLICATION_JSON); + request.setEntity(entity); + HttpResponse response = httpClient.execute(request); + + verify(postRequestedFor(urlEqualTo(BAELDUNG_WIREMOCK_PATH)) + .withHeader("Content-Type", equalTo(APPLICATION_JSON))); + assertEquals(200, response.getStatusLine().getStatusCode()); + } + + @Test + public void givenJUnitManagedServer_whenNotUsingPriority_thenCorrect() throws IOException { + stubFor(get(urlPathMatching("/baeldung/.*")).willReturn(aResponse().withStatus(200))); + stubFor(get(urlPathEqualTo(BAELDUNG_WIREMOCK_PATH)).withHeader("Accept", matching("text/.*")).willReturn(aResponse().withStatus(503))); + + HttpResponse httpResponse = generateClientAndReceiveResponseForPriorityTests(); + + verify(getRequestedFor(urlEqualTo(BAELDUNG_WIREMOCK_PATH))); + assertEquals(503, httpResponse.getStatusLine().getStatusCode()); + } + + @Test + public void givenJUnitManagedServer_whenUsingPriority_thenCorrect() throws IOException { + stubFor(get(urlPathMatching("/baeldung/.*")).atPriority(1).willReturn(aResponse().withStatus(200))); + stubFor(get(urlPathEqualTo(BAELDUNG_WIREMOCK_PATH)).atPriority(2).withHeader("Accept", matching("text/.*")).willReturn(aResponse().withStatus(503))); + + HttpResponse httpResponse = generateClientAndReceiveResponseForPriorityTests(); + + verify(getRequestedFor(urlEqualTo(BAELDUNG_WIREMOCK_PATH))); + assertEquals(200, httpResponse.getStatusLine().getStatusCode()); + } + + private static String convertHttpResponseToString(HttpResponse httpResponse) throws IOException { + InputStream inputStream = httpResponse.getEntity().getContent(); + return convertInputStreamToString(inputStream); + } + + private static String convertInputStreamToString(InputStream inputStream) { + Scanner scanner = new Scanner(inputStream, "UTF-8"); + String string = scanner.useDelimiter("\\Z").next(); + scanner.close(); + return string; + } + + private HttpResponse generateClientAndReceiveResponseForPriorityTests() throws IOException { + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpGet request = new HttpGet("http://localhost:8080/baeldung/wiremock"); + request.addHeader("Accept", "text/xml"); + return httpClient.execute(request); + } +} \ No newline at end of file diff --git a/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/ProgrammaticallyManaged.java b/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/ProgrammaticallyManaged.java new file mode 100644 index 0000000000..ad7caa52b5 --- /dev/null +++ b/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/ProgrammaticallyManaged.java @@ -0,0 +1,54 @@ +package com.baeldung.rest.wiremock.introduction; + +import com.github.tomakehurst.wiremock.WireMockServer; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.junit.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Scanner; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.configureFor; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static com.github.tomakehurst.wiremock.client.WireMock.verify; +import static org.junit.Assert.assertEquals; + +public class ProgrammaticallyManaged { + + private static final String BAELDUNG_PATH = "/baeldung"; + + private WireMockServer wireMockServer = new WireMockServer(); + private CloseableHttpClient httpClient = HttpClients.createDefault(); + + @Test + public void givenProgrammaticallyManagedServer_whenUsingSimpleStubbing_thenCorrect() throws IOException { + wireMockServer.start(); + + configureFor("localhost", 8080); + stubFor(get(urlEqualTo(BAELDUNG_PATH)).willReturn(aResponse().withBody("Welcome to Baeldung!"))); + + HttpGet request = new HttpGet("http://localhost:8080/baeldung"); + HttpResponse httpResponse = httpClient.execute(request); + String stringResponse = convertResponseToString(httpResponse); + + verify(getRequestedFor(urlEqualTo(BAELDUNG_PATH))); + assertEquals("Welcome to Baeldung!", stringResponse); + + wireMockServer.stop(); + } + + private static String convertResponseToString(HttpResponse response) throws IOException { + InputStream responseStream = response.getEntity().getContent(); + Scanner scanner = new Scanner(responseStream, "UTF-8"); + String stringResponse = scanner.useDelimiter("\\Z").next(); + scanner.close(); + return stringResponse; + } +} \ No newline at end of file diff --git a/resteasy/.classpath b/resteasy/.classpath new file mode 100644 index 0000000000..3c88c332e3 --- /dev/null +++ b/resteasy/.classpath @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/resteasy/.project b/resteasy/.project new file mode 100644 index 0000000000..7303d59739 --- /dev/null +++ b/resteasy/.project @@ -0,0 +1,42 @@ + + + resteasy-tutorial + + + + + + org.eclipse.wst.jsdt.core.javascriptValidator + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + 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/resteasy/README.md b/resteasy/README.md new file mode 100644 index 0000000000..722f1dfe93 --- /dev/null +++ b/resteasy/README.md @@ -0,0 +1,8 @@ +========= + +## A Guide to RESTEasy + + +### Relevant Articles: +- [A Guide to RESTEasy](http://www.baeldung.com/resteasy-tutorial) +- [RESTEasy Client API](http://www.baeldung.com/resteasy-client-tutorial) diff --git a/resteasy/pom.xml b/resteasy/pom.xml new file mode 100644 index 0000000000..ec9e87b0d1 --- /dev/null +++ b/resteasy/pom.xml @@ -0,0 +1,77 @@ + + + 4.0.0 + + com.baeldung + resteasy-tutorial + 1.0 + war + + + 3.0.14.Final + + + + RestEasyTutorial + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + + + + org.jboss.resteasy + resteasy-servlet-initializer + ${resteasy.version} + + + + + org.jboss.resteasy + resteasy-client + ${resteasy.version} + + + + + + org.jboss.resteasy + resteasy-jaxb-provider + ${resteasy.version} + + + + org.jboss.resteasy + resteasy-jackson-provider + ${resteasy.version} + + + + + + junit + junit + 4.4 + + + + commons-io + commons-io + 2.4 + + + + + + \ No newline at end of file diff --git a/resteasy/src/main/java/com/baeldung/client/ServicesInterface.java b/resteasy/src/main/java/com/baeldung/client/ServicesInterface.java new file mode 100644 index 0000000000..19ad0c1ec3 --- /dev/null +++ b/resteasy/src/main/java/com/baeldung/client/ServicesInterface.java @@ -0,0 +1,36 @@ +package com.baeldung.client; + +import com.baeldung.model.Movie; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.util.List; + +@Path("/movies") +public interface ServicesInterface { + + @GET + @Path("/getinfo") + @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + Movie movieByImdbId(@QueryParam("imdbId") String imdbId); + + @GET + @Path("/listmovies") + @Produces({ "application/json" }) + List listMovies(); + + @POST + @Path("/addmovie") + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + Response addMovie(Movie movie); + + @PUT + @Path("/updatemovie") + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + Response updateMovie(Movie movie); + + @DELETE + @Path("/deletemovie") + Response deleteMovie(@QueryParam("imdbId") String imdbId); + +} diff --git a/resteasy/src/main/java/com/baeldung/model/Movie.java b/resteasy/src/main/java/com/baeldung/model/Movie.java new file mode 100644 index 0000000000..408f2144fb --- /dev/null +++ b/resteasy/src/main/java/com/baeldung/model/Movie.java @@ -0,0 +1,66 @@ +package com.baeldung.model; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlType; + +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "movie", propOrder = { "imdbId", "title" }) +public class Movie { + + protected String imdbId; + protected String title; + + public Movie(String imdbId, String title) { + this.imdbId = imdbId; + this.title = title; + } + + public Movie() {} + + public String getImdbId() { + return imdbId; + } + + public void setImdbId(String imdbId) { + this.imdbId = imdbId; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + + Movie movie = (Movie) o; + + if (imdbId != null ? !imdbId.equals(movie.imdbId) : movie.imdbId != null) + return false; + return title != null ? title.equals(movie.title) : movie.title == null; + + } + + @Override + public int hashCode() { + int result = imdbId != null ? imdbId.hashCode() : 0; + result = 31 * result + (title != null ? title.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "Movie{" + + "imdbId='" + imdbId + '\'' + + ", title='" + title + '\'' + + '}'; + } +} diff --git a/resteasy/src/main/java/com/baeldung/server/MovieCrudService.java b/resteasy/src/main/java/com/baeldung/server/MovieCrudService.java new file mode 100644 index 0000000000..b7f3215f3f --- /dev/null +++ b/resteasy/src/main/java/com/baeldung/server/MovieCrudService.java @@ -0,0 +1,83 @@ +package com.baeldung.server; + +import com.baeldung.model.Movie; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +@Path("/movies") +public class MovieCrudService { + + private Map inventory = new HashMap(); + + @GET + @Path("/getinfo") + @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + public Movie movieByImdbId(@QueryParam("imdbId") String imdbId) { + + System.out.println("*** Calling getinfo for a given ImdbID***"); + + if (inventory.containsKey(imdbId)) { + return inventory.get(imdbId); + } else + return null; + } + + @POST + @Path("/addmovie") + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + public Response addMovie(Movie movie) { + + System.out.println("*** Calling addMovie ***"); + + if (null != inventory.get(movie.getImdbId())) { + return Response.status(Response.Status.NOT_MODIFIED).entity("Movie is Already in the database.").build(); + } + + inventory.put(movie.getImdbId(), movie); + return Response.status(Response.Status.CREATED).build(); + } + + @PUT + @Path("/updatemovie") + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + public Response updateMovie(Movie movie) { + + System.out.println("*** Calling updateMovie ***"); + + if (null == inventory.get(movie.getImdbId())) { + return Response.status(Response.Status.NOT_MODIFIED).entity("Movie is not in the database.\nUnable to Update").build(); + } + + inventory.put(movie.getImdbId(), movie); + return Response.status(Response.Status.OK).build(); + } + + @DELETE + @Path("/deletemovie") + public Response deleteMovie(@QueryParam("imdbId") String imdbId) { + + System.out.println("*** Calling deleteMovie ***"); + + if (null == inventory.get(imdbId)) { + return Response.status(Response.Status.NOT_FOUND).entity("Movie is not in the database.\nUnable to Delete").build(); + } + + inventory.remove(imdbId); + return Response.status(Response.Status.OK).build(); + } + + @GET + @Path("/listmovies") + @Produces({ "application/json" }) + public List listMovies() { + + return inventory.values().stream().collect(Collectors.toCollection(ArrayList::new)); + } + +} diff --git a/resteasy/src/main/java/com/baeldung/server/RestEasyServices.java b/resteasy/src/main/java/com/baeldung/server/RestEasyServices.java new file mode 100644 index 0000000000..7726e49f38 --- /dev/null +++ b/resteasy/src/main/java/com/baeldung/server/RestEasyServices.java @@ -0,0 +1,32 @@ +package com.baeldung.server; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +@ApplicationPath("/rest") +public class RestEasyServices extends Application { + + private Set singletons = new HashSet(); + + public RestEasyServices() { + singletons.add(new MovieCrudService()); + } + + @Override + public Set getSingletons() { + return singletons; + } + + @Override + public Set> getClasses() { + return super.getClasses(); + } + + @Override + public Map getProperties() { + return super.getProperties(); + } +} diff --git a/resteasy/src/main/webapp/WEB-INF/classes/logback.xml b/resteasy/src/main/webapp/WEB-INF/classes/logback.xml new file mode 100644 index 0000000000..d94e9f71ab --- /dev/null +++ b/resteasy/src/main/webapp/WEB-INF/classes/logback.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/resteasy/src/main/webapp/WEB-INF/jboss-deployment-structure.xml b/resteasy/src/main/webapp/WEB-INF/jboss-deployment-structure.xml new file mode 100644 index 0000000000..cb258374a1 --- /dev/null +++ b/resteasy/src/main/webapp/WEB-INF/jboss-deployment-structure.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/resteasy/src/main/webapp/WEB-INF/jboss-web.xml b/resteasy/src/main/webapp/WEB-INF/jboss-web.xml new file mode 100644 index 0000000000..694bb71332 --- /dev/null +++ b/resteasy/src/main/webapp/WEB-INF/jboss-web.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/resteasy/src/main/webapp/WEB-INF/web.xml b/resteasy/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..d5f00293f4 --- /dev/null +++ b/resteasy/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,13 @@ + + + + RestEasy Example + + + resteasy.servlet.mapping.prefix + /rest + + + \ No newline at end of file diff --git a/resteasy/src/test/java/com/baeldung/server/RestEasyClientTest.java b/resteasy/src/test/java/com/baeldung/server/RestEasyClientTest.java new file mode 100644 index 0000000000..ef18b0f23f --- /dev/null +++ b/resteasy/src/test/java/com/baeldung/server/RestEasyClientTest.java @@ -0,0 +1,179 @@ +package com.baeldung.server; + +import com.baeldung.client.ServicesInterface; +import com.baeldung.model.Movie; +import org.apache.commons.io.IOUtils; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.codehaus.jackson.map.DeserializationConfig; +import org.codehaus.jackson.map.ObjectMapper; +import org.jboss.resteasy.client.jaxrs.ResteasyClient; +import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; +import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; +import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine; +import org.junit.Before; +import org.junit.Test; +import javax.naming.NamingException; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.text.SimpleDateFormat; +import java.util.List; +import java.util.Locale; + +public class RestEasyClientTest { + + public static final UriBuilder FULL_PATH = UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest"); + Movie transformerMovie = null; + Movie batmanMovie = null; + ObjectMapper jsonMapper = null; + + @Before + public void setup() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException { + + jsonMapper = new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); + jsonMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); + SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); + jsonMapper.setDateFormat(sdf); + + try (InputStream inputStream = new RestEasyClientTest().getClass().getResourceAsStream("./movies/transformer.json")) { + String transformerMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); + transformerMovie = jsonMapper.readValue(transformerMovieAsString, Movie.class); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Test is going to die ...", e); + } + + try (InputStream inputStream = new RestEasyClientTest().getClass().getResourceAsStream("./movies/batman.json")) { + String batmanMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); + batmanMovie = jsonMapper.readValue(batmanMovieAsString, Movie.class); + + } catch (Exception e) { + throw new RuntimeException("Test is going to die ...", e); + } + } + + @Test + public void testListAllMovies() { + + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(FULL_PATH); + ServicesInterface proxy = target.proxy(ServicesInterface.class); + + Response moviesResponse = proxy.addMovie(transformerMovie); + moviesResponse.close(); + moviesResponse = proxy.addMovie(batmanMovie); + moviesResponse.close(); + + List movies = proxy.listMovies(); + System.out.println(movies); + } + + @Test + public void testMovieByImdbId() { + + String transformerImdbId = "tt0418279"; + + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(FULL_PATH); + ServicesInterface proxy = target.proxy(ServicesInterface.class); + + Response moviesResponse = proxy.addMovie(transformerMovie); + moviesResponse.close(); + + Movie movies = proxy.movieByImdbId(transformerImdbId); + System.out.println(movies); + } + + @Test + public void testAddMovie() { + + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(FULL_PATH); + ServicesInterface proxy = target.proxy(ServicesInterface.class); + + Response moviesResponse = proxy.addMovie(batmanMovie); + moviesResponse.close(); + moviesResponse = proxy.addMovie(transformerMovie); + + if (moviesResponse.getStatus() != Response.Status.CREATED.getStatusCode()) { + System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); + } + + moviesResponse.close(); + System.out.println("Response Code: " + moviesResponse.getStatus()); + } + + @Test + public void testAddMovieMultiConnection() { + + PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); + CloseableHttpClient httpClient = HttpClients.custom() + .setConnectionManager(cm) + .build(); + ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient); + ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build(); + ResteasyWebTarget target = client.target(FULL_PATH); + ServicesInterface proxy = target.proxy(ServicesInterface.class); + + Response batmanResponse = proxy.addMovie(batmanMovie); + Response transformerResponse = proxy.addMovie(transformerMovie); + + if (batmanResponse.getStatus() != Response.Status.CREATED.getStatusCode()) { + System.out.println("Batman Movie creation Failed : HTTP error code : " + batmanResponse.getStatus()); + } + if (batmanResponse.getStatus() != Response.Status.CREATED.getStatusCode()) { + System.out.println("Batman Movie creation Failed : HTTP error code : " + batmanResponse.getStatus()); + } + + batmanResponse.close(); + transformerResponse.close(); + cm.close(); + + + + } + + @Test + public void testDeleteMovie() { + + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(FULL_PATH); + ServicesInterface proxy = target.proxy(ServicesInterface.class); + + Response moviesResponse = proxy.addMovie(batmanMovie); + moviesResponse.close(); + moviesResponse = proxy.deleteMovie(batmanMovie.getImdbId()); + + if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { + System.out.println(moviesResponse.readEntity(String.class)); + throw new RuntimeException("Failed : HTTP error code : " + moviesResponse.getStatus()); + } + + moviesResponse.close(); + System.out.println("Response Code: " + moviesResponse.getStatus()); + } + + @Test + public void testUpdateMovie() { + + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(FULL_PATH); + ServicesInterface proxy = target.proxy(ServicesInterface.class); + + Response moviesResponse = proxy.addMovie(batmanMovie); + moviesResponse.close(); + batmanMovie.setTitle("Batman Begins"); + moviesResponse = proxy.updateMovie(batmanMovie); + + if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { + System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); + } + + moviesResponse.close(); + System.out.println("Response Code: " + moviesResponse.getStatus()); + } + +} \ No newline at end of file diff --git a/resteasy/src/test/resources/com/baeldung/server/movies/batman.json b/resteasy/src/test/resources/com/baeldung/server/movies/batman.json new file mode 100644 index 0000000000..82aaaa8f40 --- /dev/null +++ b/resteasy/src/test/resources/com/baeldung/server/movies/batman.json @@ -0,0 +1,4 @@ +{ + "title": "Batman", + "imdbId": "tt0096895" +} \ No newline at end of file diff --git a/resteasy/src/test/resources/com/baeldung/server/movies/transformer.json b/resteasy/src/test/resources/com/baeldung/server/movies/transformer.json new file mode 100644 index 0000000000..634cefc73c --- /dev/null +++ b/resteasy/src/test/resources/com/baeldung/server/movies/transformer.json @@ -0,0 +1,4 @@ +{ + "title": "Transformers", + "imdbId": "tt0418279" +} \ No newline at end of file diff --git a/sandbox/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch b/sandbox/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch deleted file mode 100644 index 627021fb96..0000000000 --- a/sandbox/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/sandbox/.settings/.jsdtscope b/sandbox/.settings/.jsdtscope deleted file mode 100644 index 7b3f0c8b9f..0000000000 --- a/sandbox/.settings/.jsdtscope +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/sandbox/.settings/org.eclipse.jdt.core.prefs b/sandbox/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index b126d6476b..0000000000 --- a/sandbox/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,95 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignore -org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull -org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault -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.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 -org.eclipse.jdt.core.compiler.problem.comparingIdentical=warning -org.eclipse.jdt.core.compiler.problem.deadCode=warning -org.eclipse.jdt.core.compiler.problem.deprecation=warning -org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled -org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled -org.eclipse.jdt.core.compiler.problem.discouragedReference=warning -org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore -org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore -org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled -org.eclipse.jdt.core.compiler.problem.fieldHiding=error -org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning -org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning -org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning -org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts=disabled -org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning -org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=ignore -org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore -org.eclipse.jdt.core.compiler.problem.localVariableHiding=error -org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning -org.eclipse.jdt.core.compiler.problem.missingDefaultCase=ignore -org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore -org.eclipse.jdt.core.compiler.problem.missingEnumCaseDespiteDefault=disabled -org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=ignore -org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=ignore -org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled -org.eclipse.jdt.core.compiler.problem.missingSerialVersion=warning -org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignore -org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning -org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning -org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore -org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=error -org.eclipse.jdt.core.compiler.problem.nullReference=warning -org.eclipse.jdt.core.compiler.problem.nullSpecViolation=error -org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=warning -org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning -org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore -org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=ignore -org.eclipse.jdt.core.compiler.problem.potentialNullReference=ignore -org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=ignore -org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning -org.eclipse.jdt.core.compiler.problem.redundantNullAnnotation=warning -org.eclipse.jdt.core.compiler.problem.redundantNullCheck=ignore -org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=ignore -org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=ignore -org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore -org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=ignore -org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled -org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning -org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled -org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled -org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore -org.eclipse.jdt.core.compiler.problem.typeParameterHiding=error -org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=enabled -org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning -org.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning -org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore -org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning -org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore -org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=ignore -org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=ignore -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=enabled -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled -org.eclipse.jdt.core.compiler.problem.unusedImport=warning -org.eclipse.jdt.core.compiler.problem.unusedLabel=warning -org.eclipse.jdt.core.compiler.problem.unusedLocal=warning -org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=ignore -org.eclipse.jdt.core.compiler.problem.unusedParameter=ignore -org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference=enabled -org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled -org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled -org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning -org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning -org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning -org.eclipse.jdt.core.compiler.source=1.8 diff --git a/sandbox/.settings/org.eclipse.jdt.ui.prefs b/sandbox/.settings/org.eclipse.jdt.ui.prefs deleted file mode 100644 index 471e9b0d81..0000000000 --- a/sandbox/.settings/org.eclipse.jdt.ui.prefs +++ /dev/null @@ -1,55 +0,0 @@ -#Sat Jan 21 23:04:06 EET 2012 -eclipse.preferences.version=1 -editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true -sp_cleanup.add_default_serial_version_id=true -sp_cleanup.add_generated_serial_version_id=false -sp_cleanup.add_missing_annotations=true -sp_cleanup.add_missing_deprecated_annotations=true -sp_cleanup.add_missing_methods=false -sp_cleanup.add_missing_nls_tags=false -sp_cleanup.add_missing_override_annotations=true -sp_cleanup.add_missing_override_annotations_interface_methods=true -sp_cleanup.add_serial_version_id=false -sp_cleanup.always_use_blocks=true -sp_cleanup.always_use_parentheses_in_expressions=true -sp_cleanup.always_use_this_for_non_static_field_access=false -sp_cleanup.always_use_this_for_non_static_method_access=false -sp_cleanup.convert_to_enhanced_for_loop=true -sp_cleanup.correct_indentation=true -sp_cleanup.format_source_code=true -sp_cleanup.format_source_code_changes_only=true -sp_cleanup.make_local_variable_final=true -sp_cleanup.make_parameters_final=true -sp_cleanup.make_private_fields_final=false -sp_cleanup.make_type_abstract_if_missing_method=false -sp_cleanup.make_variable_declarations_final=true -sp_cleanup.never_use_blocks=false -sp_cleanup.never_use_parentheses_in_expressions=false -sp_cleanup.on_save_use_additional_actions=true -sp_cleanup.organize_imports=true -sp_cleanup.qualify_static_field_accesses_with_declaring_class=false -sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true -sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true -sp_cleanup.qualify_static_member_accesses_with_declaring_class=true -sp_cleanup.qualify_static_method_accesses_with_declaring_class=false -sp_cleanup.remove_private_constructors=true -sp_cleanup.remove_trailing_whitespaces=true -sp_cleanup.remove_trailing_whitespaces_all=true -sp_cleanup.remove_trailing_whitespaces_ignore_empty=false -sp_cleanup.remove_unnecessary_casts=true -sp_cleanup.remove_unnecessary_nls_tags=false -sp_cleanup.remove_unused_imports=true -sp_cleanup.remove_unused_local_variables=false -sp_cleanup.remove_unused_private_fields=true -sp_cleanup.remove_unused_private_members=false -sp_cleanup.remove_unused_private_methods=true -sp_cleanup.remove_unused_private_types=true -sp_cleanup.sort_members=false -sp_cleanup.sort_members_all=false -sp_cleanup.use_blocks=false -sp_cleanup.use_blocks_only_for_return_and_throw=false -sp_cleanup.use_parentheses_in_expressions=false -sp_cleanup.use_this_for_non_static_field_access=true -sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true -sp_cleanup.use_this_for_non_static_method_access=true -sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true diff --git a/sandbox/.settings/org.eclipse.m2e.core.prefs b/sandbox/.settings/org.eclipse.m2e.core.prefs deleted file mode 100644 index f897a7f1cb..0000000000 --- a/sandbox/.settings/org.eclipse.m2e.core.prefs +++ /dev/null @@ -1,4 +0,0 @@ -activeProfiles= -eclipse.preferences.version=1 -resolveWorkspaceProjects=true -version=1 diff --git a/sandbox/.settings/org.eclipse.m2e.wtp.prefs b/sandbox/.settings/org.eclipse.m2e.wtp.prefs deleted file mode 100644 index ef86089622..0000000000 --- a/sandbox/.settings/org.eclipse.m2e.wtp.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.m2e.wtp.enabledProjectSpecificPrefs=false diff --git a/sandbox/.settings/org.eclipse.wst.common.component b/sandbox/.settings/org.eclipse.wst.common.component deleted file mode 100644 index e98377cb0f..0000000000 --- a/sandbox/.settings/org.eclipse.wst.common.component +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/sandbox/.settings/org.eclipse.wst.common.project.facet.core.xml b/sandbox/.settings/org.eclipse.wst.common.project.facet.core.xml deleted file mode 100644 index f4ef8aa0a5..0000000000 --- a/sandbox/.settings/org.eclipse.wst.common.project.facet.core.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - diff --git a/sandbox/.settings/org.eclipse.wst.jsdt.ui.superType.container b/sandbox/.settings/org.eclipse.wst.jsdt.ui.superType.container deleted file mode 100644 index 3bd5d0a480..0000000000 --- a/sandbox/.settings/org.eclipse.wst.jsdt.ui.superType.container +++ /dev/null @@ -1 +0,0 @@ -org.eclipse.wst.jsdt.launching.baseBrowserLibrary \ No newline at end of file diff --git a/sandbox/.settings/org.eclipse.wst.jsdt.ui.superType.name b/sandbox/.settings/org.eclipse.wst.jsdt.ui.superType.name deleted file mode 100644 index 05bd71b6ec..0000000000 --- a/sandbox/.settings/org.eclipse.wst.jsdt.ui.superType.name +++ /dev/null @@ -1 +0,0 @@ -Window \ No newline at end of file diff --git a/sandbox/.settings/org.eclipse.wst.validation.prefs b/sandbox/.settings/org.eclipse.wst.validation.prefs deleted file mode 100644 index cacf5451ae..0000000000 --- a/sandbox/.settings/org.eclipse.wst.validation.prefs +++ /dev/null @@ -1,14 +0,0 @@ -DELEGATES_PREFERENCE=delegateValidatorList -USER_BUILD_PREFERENCE=enabledBuildValidatorListorg.eclipse.jst.j2ee.internal.classpathdep.ClasspathDependencyValidator; -USER_MANUAL_PREFERENCE=enabledManualValidatorListorg.eclipse.jst.j2ee.internal.classpathdep.ClasspathDependencyValidator; -USER_PREFERENCE=overrideGlobalPreferencestruedisableAllValidationfalseversion1.2.303.v201202090300 -eclipse.preferences.version=1 -override=true -suspend=false -vals/org.eclipse.jst.jsf.ui.JSFAppConfigValidator/global=FF01 -vals/org.eclipse.jst.jsp.core.JSPBatchValidator/global=FF01 -vals/org.eclipse.jst.jsp.core.JSPContentValidator/global=FF01 -vals/org.eclipse.jst.jsp.core.TLDValidator/global=FF01 -vals/org.eclipse.wst.dtd.core.dtdDTDValidator/global=FF01 -vals/org.eclipse.wst.jsdt.web.core.JsBatchValidator/global=TF02 -vf.version=3 diff --git a/sandbox/.settings/org.eclipse.wst.ws.service.policy.prefs b/sandbox/.settings/org.eclipse.wst.ws.service.policy.prefs deleted file mode 100644 index 9cfcabe16f..0000000000 --- a/sandbox/.settings/org.eclipse.wst.ws.service.policy.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.wst.ws.service.policy.projectEnabled=false diff --git a/sandbox/.springBeans b/sandbox/.springBeans deleted file mode 100644 index a79097f40d..0000000000 --- a/sandbox/.springBeans +++ /dev/null @@ -1,14 +0,0 @@ - - - 1 - - - - - - - src/main/webapp/WEB-INF/api-servlet.xml - - - - diff --git a/sandbox/README.md b/sandbox/README.md deleted file mode 100644 index 772681ad57..0000000000 --- a/sandbox/README.md +++ /dev/null @@ -1,9 +0,0 @@ -========= - -## Core Java Cookbooks and Examples - -### Relevant Articles: -- [Immutable ArrayList in Java](http://www.baeldung.com/java-immutable-list) -- [Java - Reading a Large File Efficiently](http://www.baeldung.com/java-read-lines-large-file) -- [Java InputStream to String](http://www.baeldung.com/convert-input-stream-to-string) - diff --git a/sandbox/pom.xml b/sandbox/pom.xml deleted file mode 100644 index 55cb9f11f2..0000000000 --- a/sandbox/pom.xml +++ /dev/null @@ -1,176 +0,0 @@ - - 4.0.0 - org.baeldung - sandbox - 0.1-SNAPSHOT - - sandbox - - - - - - - com.google.guava - guava - ${guava.version} - - - - org.apache.commons - commons-collections4 - 4.0 - - - - commons-io - commons-io - 2.4 - - - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - - - - - - - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - - - - - - 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} - - - - - - junit - junit-dep - ${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 - - - - - - core-java - - - src/main/resources - true - - - - - - - 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.0.3.RELEASE - 3.2.5.RELEASE - - - 4.3.10.Final - 5.1.35 - - - 2.4.4 - - - 1.7.12 - 1.1.3 - - - 5.1.3.Final - - - 16.0.1 - 3.4 - - - 1.3 - 4.11 - 1.10.19 - - 4.4.1 - 4.5 - - 2.4.1 - - - 3.3 - 2.6 - 2.18.1 - 2.7 - 1.4.14 - - - - \ No newline at end of file diff --git a/sandbox/src/main/webapp/WEB-INF/api-servlet.xml b/sandbox/src/main/webapp/WEB-INF/api-servlet.xml deleted file mode 100644 index a675fc6d95..0000000000 --- a/sandbox/src/main/webapp/WEB-INF/api-servlet.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/sandbox/src/main/webapp/WEB-INF/web.xml b/sandbox/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index 935beae648..0000000000 --- a/sandbox/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - - Spring MVC Application - - - - contextClass - - org.springframework.web.context.support.AnnotationConfigWebApplicationContext - - - - contextConfigLocation - org.baeldung.config - - - - org.springframework.web.context.ContextLoaderListener - - - - - api - org.springframework.web.servlet.DispatcherServlet - 1 - - - api - / - - - - - - - \ No newline at end of file diff --git a/sandbox/src/test/java/org/baeldung/codility/CodilityTest1.java b/sandbox/src/test/java/org/baeldung/codility/CodilityTest1.java deleted file mode 100644 index 8bd957c51a..0000000000 --- a/sandbox/src/test/java/org/baeldung/codility/CodilityTest1.java +++ /dev/null @@ -1,72 +0,0 @@ -package org.baeldung.codility; - -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Random; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -public class CodilityTest1 { - - @Before - public final void before() { - // - } - - // tests - - @Test - public final void whenSolutionIsCalculated1_thenCorrect() { - final int[] A = new int[] { 1, 4, 3, 3, 1, 2 }; - final int solution = solution(A); - Assert.assertEquals(4, solution); - } - - @Test - public final void whenSolutionIsCalculated2_thenCorrect() { - final int[] A = new int[] { 6, 4, 4, 6 }; - final int solution = solution(A); - Assert.assertEquals(-1, solution); - } - - @Test - public final void whenSolutionIsCalculated3_thenCorrect() { - final int[] A = new int[100000]; - final Random random = new Random(); - for (final int index : A) { - A[index] = random.nextInt(); - } - - final long start = System.currentTimeMillis(); - final int solution = solution(A); - final long end = System.currentTimeMillis(); - System.out.println("Time: " + (end - start)); - System.out.println(solution); - } - - // - - final int solution(final int elements[]) { - final Map collector = new LinkedHashMap(); - Integer currentValue = null; - for (final int element : elements) { - currentValue = collector.get(element); - if (currentValue == null) { - collector.put(element, 1); - } else if (currentValue >= 1) { - collector.put(element, ++currentValue); - } - } - - for (final Map.Entry entry : collector.entrySet()) { - if (entry.getValue() == 1) { - return entry.getKey(); - } - } - - return -1; - } - -} diff --git a/sandbox/src/test/java/org/baeldung/codility/CodilityTest2.java b/sandbox/src/test/java/org/baeldung/codility/CodilityTest2.java deleted file mode 100644 index d92e9001f9..0000000000 --- a/sandbox/src/test/java/org/baeldung/codility/CodilityTest2.java +++ /dev/null @@ -1,72 +0,0 @@ -package org.baeldung.codility; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.junit.Assert; -import org.junit.Test; - -public class CodilityTest2 { - - // tests - - @Test - public final void whenSolutionIsCalculated1_thenCorrect() { - final int solution = solution(955); - Assert.assertEquals(4, solution); - } - - @Test - public final void whenSolutionIsCalculated2_thenCorrect() { - final int solution = solution(102); - Assert.assertEquals(-1, solution); - } - - @Test - public final void whenSolutionIsCalculated3_thenCorrect() { - final int solution = solution(2); - Assert.assertEquals(-1, solution); - } - - @Test - public final void whenSolutionIsCalculated4_thenCorrect() { - final int solution = solution2("codilitycodilityco"); - Assert.assertEquals(8, solution); - } - - // - - public final int solution(final int decimal) { - final String binaryString = Integer.toBinaryString(decimal); - int lastPeriod = -1; - for (int period = 1; period < (binaryString.length() / 2 + 1); period++) { - final Matcher m = Pattern.compile("(\\S{" + period + ",})(?=.*?\\1)").matcher(binaryString); - final boolean found = m.find(); - if (found && m.groupCount() > 0) { - lastPeriod = period; - } - if (!found) { - break; - } - } - - return lastPeriod; - } - - public final int solution2(final String binaryString) { - int lastPeriod = -1; - for (int period = 1; period < (binaryString.length() / 2 + 1); period++) { - final Matcher m = Pattern.compile("(\\S{" + period + ",})(?=.*?\\1)").matcher(binaryString); - final boolean found = m.find(); - if (found && m.groupCount() > 0) { - lastPeriod = period; - } - if (!found) { - break; - } - } - - return lastPeriod; - } - -} diff --git a/sandbox/src/test/java/org/baeldung/codility/CodilityTest3.java b/sandbox/src/test/java/org/baeldung/codility/CodilityTest3.java deleted file mode 100644 index a7067be7b0..0000000000 --- a/sandbox/src/test/java/org/baeldung/codility/CodilityTest3.java +++ /dev/null @@ -1,207 +0,0 @@ -package org.baeldung.codility; - -import java.util.HashMap; -import java.util.Map; - -import org.junit.Assert; -import org.junit.Test; - -public class CodilityTest3 { - - // tests - - @Test - public final void whenSolutionIsCalculated1_thenCorrect() { - final int[] moves = new int[] { 1, 3, 2, 5, 4, 4, 6, 3, 2 }; - final int solution = solution(moves); - Assert.assertEquals(7, solution); - } - - // - - public int solution(final int[] moves) { - final World world = new World(); - int nextMove = 0; - for (final int move : moves) { - switch (nextMove) { - case 0: - if (world.moveNorth(move)) { - nextMove = 1; - break; - } else { - return world.movesCount; - } - case 1: - if (world.moveEast(move)) { - nextMove = 2; - break; - } else { - return world.movesCount; - } - case 2: - if (world.moveSouth(move)) { - nextMove = 3; - break; - } else { - return world.movesCount; - } - case 3: - if (world.moveWest(move)) { - nextMove = 0; - break; - } else { - return world.movesCount; - } - default: - throw new IllegalStateException(); - } - } - - return world.movesCount; - } - -} - -class World { - int movesCount = 1; - int currentX = 0; - int currentY = 0; - Map minXAtY; - Map maxXAtY; - Map minYAtX; - Map maxYAtX; - - public World() { - minXAtY = new HashMap<>(); - maxXAtY = new HashMap<>(); - minYAtX = new HashMap<>(); - maxYAtX = new HashMap<>(); - } - - final boolean moveNorth(final int steps) { - if (isMoveNorthValid(steps)) { - storeMoveNorth(steps); - movesCount++; - return true; - } - return false; - } - - final boolean moveEast(final int steps) { - if (isMoveEastValid(steps)) { - storeMoveEast(steps); - movesCount++; - return true; - } - return false; - } - - final boolean moveSouth(final int steps) { - if (isMoveSouthValid(steps)) { - storeMoveSouth(steps); - movesCount++; - return true; - } - return false; - } - - final boolean moveWest(final int steps) { - if (isMoveWestValid(steps)) { - storeMoveWest(steps); - movesCount++; - return true; - } - return false; - } - - // - - private boolean isMoveNorthValid(final int steps) { - int currentPosition = currentY; - for (int i = 1; i <= steps; i++) { - currentPosition += 1; - if (minXAtY.get(currentPosition) != null && minXAtY.get(currentPosition) > currentX) { - return false; - } - if (maxXAtY.get(currentPosition) != null && maxXAtY.get(currentPosition) < currentX) { - return false; - } - } - return true; - } - - private boolean isMoveEastValid(final int steps) { // - int currentPosition = currentX; - for (int i = 1; i <= steps; i++) { - currentPosition += 1; - if (minYAtX.get(currentPosition) != null && minYAtX.get(currentPosition) < currentY) { - return false; - } - if (maxYAtX.get(currentPosition) != null && maxYAtX.get(currentPosition) > currentY) { - return false; - } - } - return true; - } - - private boolean isMoveSouthValid(final int steps) { - int currentPosition = currentY; - for (int i = 1; i <= steps; i++) { - currentPosition -= 1; - if (minXAtY.get(currentPosition) != null && minXAtY.get(currentPosition) < currentX) { - return false; - } - if (maxXAtY.get(currentPosition) != null && maxXAtY.get(currentPosition) > currentX) { - return false; - } - } - return true; - } - - private boolean isMoveWestValid(final int steps) { - int currentPosition = currentX; - for (int i = 1; i <= steps; i++) { - currentPosition -= 1; - if (minYAtX.get(currentPosition) != null && minYAtX.get(currentPosition) > currentY) { - return false; - } - if (maxYAtX.get(currentPosition) != null && maxYAtX.get(currentPosition) < currentY) { - return false; - } - } - return true; - } - - private void storeMoveNorth(final int steps) { - currentY = currentY + steps; - final Integer currentMaxYAtThisLevel = maxYAtX.get(currentX); - if (currentMaxYAtThisLevel == null || currentMaxYAtThisLevel == null && currentMaxYAtThisLevel < currentY) { - maxYAtX.put(currentX, currentY); - } - } - - private void storeMoveEast(final int steps) { - currentX = currentX + steps; - final Integer currentMaxXAtThisLevel = maxXAtY.get(currentY); - if (currentMaxXAtThisLevel == null || currentMaxXAtThisLevel == null && currentMaxXAtThisLevel < currentX) { - maxXAtY.put(currentY, currentX); - } - } - - private void storeMoveSouth(final int steps) { - currentY = currentY - steps; - final Integer currentMinYAtThisLevel = minYAtX.get(currentX); - if (currentMinYAtThisLevel == null || currentMinYAtThisLevel == null && currentMinYAtThisLevel > currentY) { - minYAtX.put(currentX, currentY); - } - } - - private void storeMoveWest(final int steps) { - currentX = currentX - steps; - final Integer currentMinXAtThisLevel = minXAtY.get(currentY); - if (currentMinXAtThisLevel == null || currentMinXAtThisLevel == null && currentMinXAtThisLevel > currentX) { - minXAtY.put(currentY, currentX); - } - } - -} diff --git a/sandbox/src/test/resources/.gitignore b/sandbox/src/test/resources/.gitignore deleted file mode 100644 index 83c05e60c8..0000000000 --- a/sandbox/src/test/resources/.gitignore +++ /dev/null @@ -1,13 +0,0 @@ -*.class - -#folders# -/target -/neoDb* -/data -/src/main/webapp/WEB-INF/classes -*/META-INF/* - -# Packaged files # -*.jar -*.war -*.ear \ No newline at end of file diff --git a/spring-all/README.md b/spring-all/README.md index 4a3bd25077..47c947a414 100644 --- a/spring-all/README.md +++ b/spring-all/README.md @@ -4,6 +4,10 @@ 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 \ No newline at end of file +- [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 +- [Spring Profiles](http://www.baeldung.com/spring-profiles) +- [A Spring Custom Annotation for a Better DAO](http://www.baeldung.com/spring-annotation-bean-pre-processor) diff --git a/spring-all/pom.xml b/spring-all/pom.xml index 0551abe5a5..8ff09e5e17 100644 --- a/spring-all/pom.xml +++ b/spring-all/pom.xml @@ -1,6 +1,6 @@ 4.0.0 - org.baeldung + com.baeldung spring-all 0.1-SNAPSHOT @@ -127,8 +127,7 @@ junit - junit-dep - ${junit.version} + junit test @@ -164,7 +163,6 @@ org.apache.maven.plugins maven-compiler-plugin - ${maven-compiler-plugin.version} 1.8 1.8 @@ -174,7 +172,6 @@ org.apache.maven.plugins maven-war-plugin - ${maven-war-plugin.version} false @@ -183,7 +180,6 @@ org.apache.maven.plugins maven-surefire-plugin - ${maven-surefire-plugin.version} @@ -221,42 +217,42 @@ - 4.2.2.RELEASE - 4.0.2.RELEASE + 4.2.5.RELEASE + 4.0.4.RELEASE 3.20.0-GA 1.2 4.3.11.Final - 5.1.36 + 5.1.38 - 1.7.12 + 1.7.13 1.1.3 - 5.2.1.Final + 5.2.2.Final - 18.0 + 19.0 3.4 1.3 - 4.11 + 4.12 1.10.19 4.4.1 4.5 - 2.4.1 + 2.9.0 - 3.3 + 3.5.1 2.6 - 2.18.1 + 2.19.1 2.7 - 1.4.15 + 1.4.18 diff --git a/spring-all/src/main/java/org/baeldung/caching/config/CachingConfig.java b/spring-all/src/main/java/org/baeldung/caching/config/CachingConfig.java index 4153ec9636..c995bca68d 100644 --- a/spring-all/src/main/java/org/baeldung/caching/config/CachingConfig.java +++ b/spring-all/src/main/java/org/baeldung/caching/config/CachingConfig.java @@ -2,23 +2,19 @@ package org.baeldung.caching.config; import java.util.Arrays; -import org.baeldung.caching.example.CustomerDataService; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.concurrent.ConcurrentMapCache; import org.springframework.cache.support.SimpleCacheManager; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @EnableCaching +@ComponentScan("org.baeldung.caching.example") public class CachingConfig { - @Bean - public CustomerDataService customerDataService() { - return new CustomerDataService(); - } - @Bean public CacheManager cacheManager() { final SimpleCacheManager cacheManager = new SimpleCacheManager(); diff --git a/spring-all/src/main/java/org/baeldung/caching/example/AbstractService.java b/spring-all/src/main/java/org/baeldung/caching/example/AbstractService.java new file mode 100644 index 0000000000..38b5a5a3ec --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/example/AbstractService.java @@ -0,0 +1,76 @@ +package org.baeldung.caching.example; + +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.CachePut; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.cache.annotation.Caching; + +public abstract class AbstractService { + + // this method configuration is equivalent to xml configuration + @Cacheable(value = "addresses", key = "#customer.name") + public String getAddress(final Customer customer) { + return customer.getAddress(); + } + + /** + * The method returns the customer's address, + only it doesn't find it the cache- addresses and directory. + * + * @param customer the customer + * @return the address + */ + @Cacheable({ "addresses", "directory" }) + public String getAddress1(final Customer customer) { + return customer.getAddress(); + } + + /** + * The method returns the customer's address, + but refreshes all the entries in the cache to load new ones. + * + * @param customer the customer + * @return the address + */ + @CacheEvict(value = "addresses", allEntries = true) + public String getAddress2(final Customer customer) { + return customer.getAddress(); + } + + /** + * The method returns the customer's address, + but not before selectively evicting the cache as per specified paramters. + * + * @param customer the customer + * @return the address + */ + @Caching(evict = { @CacheEvict("addresses"), @CacheEvict(value = "directory", key = "#customer.name") }) + public String getAddress3(final Customer customer) { + return customer.getAddress(); + } + + /** + * The method uses the class level cache to look up for entries. + * + * @param customer the customer + * @return the address + */ + @Cacheable + // parameter not required as we have declared it using @CacheConfig + public String getAddress4(final Customer customer) { + return customer.getAddress(); + } + + /** + * The method selectively caches the results that meet the predefined criteria. + * + * @param customer the customer + * @return the address + */ + @CachePut(value = "addresses", condition = "#customer.name=='Tom'") + // @CachePut(value = "addresses", unless = "#result.length>64") + public String getAddress5(final Customer customer) { + return customer.getAddress(); + } + +} diff --git a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java index fe4de5d282..1c595057a8 100644 --- a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java +++ b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java @@ -1,7 +1,5 @@ package org.baeldung.caching.example; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; @@ -13,9 +11,6 @@ import org.springframework.stereotype.Component; @CacheConfig(cacheNames = { "addresses" }) public class CustomerDataService { - @Autowired - CacheManager cacheManager; - // this method configuration is equivalent to xml configuration @Cacheable(value = "addresses", key = "#customer.name") public String getAddress(final Customer customer) { diff --git a/spring-all/src/main/java/org/baeldung/caching/example/CustomerServiceWithParent.java b/spring-all/src/main/java/org/baeldung/caching/example/CustomerServiceWithParent.java new file mode 100644 index 0000000000..a5ded7daf9 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/example/CustomerServiceWithParent.java @@ -0,0 +1,10 @@ +package org.baeldung.caching.example; + +import org.springframework.cache.annotation.CacheConfig; +import org.springframework.stereotype.Component; + +@Component +@CacheConfig(cacheNames = { "addresses" }) +public class CustomerServiceWithParent extends AbstractService { + // +} diff --git a/spring-all/src/main/java/org/baeldung/customannotation/DataAccess.java b/spring-all/src/main/java/org/baeldung/customannotation/DataAccess.java index 11bc30a84a..9a8a493a6d 100644 --- a/spring-all/src/main/java/org/baeldung/customannotation/DataAccess.java +++ b/spring-all/src/main/java/org/baeldung/customannotation/DataAccess.java @@ -10,5 +10,5 @@ import java.lang.annotation.Target; @Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD }) @Documented public @interface DataAccess { - Classentity(); + Class entity(); } diff --git a/spring-all/src/main/java/org/baeldung/customannotation/DataAccessAnnotationProcessor.java b/spring-all/src/main/java/org/baeldung/customannotation/DataAccessAnnotationProcessor.java index 7902da746e..c792073745 100644 --- a/spring-all/src/main/java/org/baeldung/customannotation/DataAccessAnnotationProcessor.java +++ b/spring-all/src/main/java/org/baeldung/customannotation/DataAccessAnnotationProcessor.java @@ -19,15 +19,13 @@ public class DataAccessAnnotationProcessor implements BeanPostProcessor { } @Override - public Object postProcessBeforeInitialization(Object bean, String beanName) - throws BeansException { + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { scanDataAccessAnnotation(bean, beanName); return bean; } @Override - public Object postProcessAfterInitialization(Object bean, String beanName) - throws BeansException { + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; } diff --git a/spring-all/src/main/java/org/baeldung/customannotation/DataAccessFieldCallback.java b/spring-all/src/main/java/org/baeldung/customannotation/DataAccessFieldCallback.java index 16526fa56f..8cb62affc4 100644 --- a/spring-all/src/main/java/org/baeldung/customannotation/DataAccessFieldCallback.java +++ b/spring-all/src/main/java/org/baeldung/customannotation/DataAccessFieldCallback.java @@ -12,18 +12,14 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils.FieldCallback; - public final class DataAccessFieldCallback implements FieldCallback { private static Logger logger = LoggerFactory.getLogger(DataAccessFieldCallback.class); private static int AUTOWIRE_MODE = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME; - private static String ERROR_ENTITY_VALUE_NOT_SAME = "@DataAccess(entity) " - + "value should have same type with injected generic type."; - private static String WARN_NON_GENERIC_VALUE = "@DataAccess annotation assigned " - + "to raw (non-generic) declaration. This will make your code less type-safe."; - private static String ERROR_CREATE_INSTANCE = "Cannot create instance of " - + "type '{}' or instance creation is failed because: {}"; + private static String ERROR_ENTITY_VALUE_NOT_SAME = "@DataAccess(entity) " + "value should have same type with injected generic type."; + private static String WARN_NON_GENERIC_VALUE = "@DataAccess annotation assigned " + "to raw (non-generic) declaration. This will make your code less type-safe."; + private static String ERROR_CREATE_INSTANCE = "Cannot create instance of " + "type '{}' or instance creation is failed because: {}"; private ConfigurableListableBeanFactory configurableListableBeanFactory; private Object bean; @@ -34,15 +30,14 @@ public final class DataAccessFieldCallback implements FieldCallback { } @Override - public void doWith(final Field field) - throws IllegalArgumentException, IllegalAccessException { + public void doWith(final Field field) throws IllegalArgumentException, IllegalAccessException { if (!field.isAnnotationPresent(DataAccess.class)) { return; } ReflectionUtils.makeAccessible(field); final Type fieldGenericType = field.getGenericType(); - // In this example, get actual "GenericDAO' type. - final Class generic = field.getType(); + // In this example, get actual "GenericDAO' type. + final Class generic = field.getType(); final Class classValue = field.getDeclaredAnnotation(DataAccess.class).entity(); if (genericTypeIsValid(classValue, fieldGenericType)) { @@ -54,7 +49,6 @@ public final class DataAccessFieldCallback implements FieldCallback { } } - /** * For example, if user write: *
@@ -75,8 +69,6 @@ public final class DataAccessFieldCallback implements FieldCallback {
         }
     }
 
-
-
     public final Object getBeanInstance(final String beanName, final Class genericClass, final Class paramClass) {
         Object daoInstance = null;
         if (!configurableListableBeanFactory.containsBean(beanName)) {
@@ -90,7 +82,7 @@ public final class DataAccessFieldCallback implements FieldCallback {
                 logger.error(ERROR_CREATE_INSTANCE, genericClass.getTypeName(), e);
                 throw new RuntimeException(e);
             }
-            
+
             daoInstance = configurableListableBeanFactory.initializeBean(toRegister, beanName);
             configurableListableBeanFactory.autowireBeanProperties(daoInstance, AUTOWIRE_MODE, true);
             configurableListableBeanFactory.registerSingleton(beanName, daoInstance);
diff --git a/spring-all/src/main/java/org/baeldung/scopes/HelloMessageGenerator.java b/spring-all/src/main/java/org/baeldung/scopes/HelloMessageGenerator.java
new file mode 100644
index 0000000000..ae1c6157db
--- /dev/null
+++ b/spring-all/src/main/java/org/baeldung/scopes/HelloMessageGenerator.java
@@ -0,0 +1,15 @@
+package org.baeldung.scopes;
+
+public class HelloMessageGenerator {
+
+	private String message;
+
+	public String getMessage() {
+		return message;
+	}
+
+	public void setMessage(final String message) {
+		this.message = message;
+	}
+
+}
diff --git a/spring-all/src/main/java/org/baeldung/scopes/Person.java b/spring-all/src/main/java/org/baeldung/scopes/Person.java
new file mode 100644
index 0000000000..e6139c31dd
--- /dev/null
+++ b/spring-all/src/main/java/org/baeldung/scopes/Person.java
@@ -0,0 +1,27 @@
+package org.baeldung.scopes;
+
+public class Person {
+	private String name;
+	private int age;
+
+	public Person() {
+	}
+
+	public Person(final String name, final int age) {
+		this.name = name;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(final String name) {
+		this.name = name;
+	}
+
+	@Override
+	public String toString() {
+		return "Person [name=" + name + "]";
+	}
+
+}
diff --git a/spring-all/src/main/java/org/baeldung/scopes/ScopesController.java b/spring-all/src/main/java/org/baeldung/scopes/ScopesController.java
new file mode 100644
index 0000000000..bf733b75f9
--- /dev/null
+++ b/spring-all/src/main/java/org/baeldung/scopes/ScopesController.java
@@ -0,0 +1,31 @@
+package org.baeldung.scopes;
+
+import javax.annotation.Resource;
+
+import org.apache.log4j.Logger;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+@Controller
+public class ScopesController {
+	public static final Logger LOG = Logger.getLogger(ScopesController.class);
+
+	@Resource(name = "requestMessage")
+	HelloMessageGenerator requestMessage;
+
+	@Resource(name = "sessionMessage")
+	HelloMessageGenerator sessionMessage;
+
+	@RequestMapping("/scopes")
+	public String getScopes(final Model model) {
+		LOG.info("Request Message:" + requestMessage.getMessage());
+		LOG.info("Session Message" + sessionMessage.getMessage());
+		requestMessage.setMessage("Good morning!");
+		sessionMessage.setMessage("Good afternoon!");
+		model.addAttribute("requestMessage", requestMessage.getMessage());
+		model.addAttribute("sessionMessage", sessionMessage.getMessage());
+		return "scopesExample";
+	}
+
+}
diff --git a/spring-all/src/main/java/org/baeldung/spring/config/ScopesConfig.java b/spring-all/src/main/java/org/baeldung/spring/config/ScopesConfig.java
new file mode 100644
index 0000000000..5a9b266388
--- /dev/null
+++ b/spring-all/src/main/java/org/baeldung/spring/config/ScopesConfig.java
@@ -0,0 +1,57 @@
+package org.baeldung.spring.config;
+
+import org.baeldung.scopes.HelloMessageGenerator;
+import org.baeldung.scopes.Person;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Scope;
+import org.springframework.context.annotation.ScopedProxyMode;
+import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.servlet.config.annotation.EnableWebMvc;
+import org.springframework.web.servlet.view.JstlView;
+import org.springframework.web.servlet.view.UrlBasedViewResolver;
+
+@Configuration
+@ComponentScan("org.baeldung.scopes")
+@EnableWebMvc
+public class ScopesConfig {
+	@Bean
+	public UrlBasedViewResolver setupViewResolver() {
+		final UrlBasedViewResolver resolver = new UrlBasedViewResolver();
+		resolver.setPrefix("/WEB-INF/view/");
+		resolver.setSuffix(".jsp");
+		resolver.setViewClass(JstlView.class);
+		return resolver;
+	}
+
+	@Bean
+	@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
+	public HelloMessageGenerator requestMessage() {
+		return new HelloMessageGenerator();
+	}
+
+	@Bean
+	@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
+	public HelloMessageGenerator sessionMessage() {
+		return new HelloMessageGenerator();
+	}
+
+	@Bean
+	@Scope(value = WebApplicationContext.SCOPE_GLOBAL_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
+	public HelloMessageGenerator globalSessionMessage() {
+		return new HelloMessageGenerator();
+	}
+
+	@Bean
+	@Scope("prototype")
+	public Person personPrototype() {
+		return new Person();
+	}
+
+	@Bean
+	@Scope("singleton")
+	public Person personSingleton() {
+		return new Person();
+	}
+}
diff --git a/spring-all/src/main/resources/scopes.xml b/spring-all/src/main/resources/scopes.xml
new file mode 100644
index 0000000000..faecd727fa
--- /dev/null
+++ b/spring-all/src/main/resources/scopes.xml
@@ -0,0 +1,10 @@
+
+
+
+    
+    
+    
+    
+
diff --git a/spring-all/src/main/webapp/WEB-INF/view/scopesExample.jsp b/spring-all/src/main/webapp/WEB-INF/view/scopesExample.jsp
new file mode 100644
index 0000000000..7974cf0220
--- /dev/null
+++ b/spring-all/src/main/webapp/WEB-INF/view/scopesExample.jsp
@@ -0,0 +1,10 @@
+
+
+
+
+    

Bean Scopes Examples

+
+ Request Message: ${requestMessage }
+ Session Message: ${sessionMessage } + + \ No newline at end of file diff --git a/spring-all/src/test/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java b/spring-all/src/test/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java deleted file mode 100644 index a4a3733dd8..0000000000 --- a/spring-all/src/test/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.baeldung.caching.test; - -import org.baeldung.caching.config.CachingConfig; -import org.baeldung.caching.example.Customer; -import org.baeldung.caching.example.CustomerDataService; -import org.junit.Test; -import org.springframework.stereotype.Component; -import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; - -@Component -public class SpringCachingBehaviorTest { - - @Test - public void testCaching() { - @SuppressWarnings("resource") - final - // final ApplicationContext context = new ClassPathXmlApplicationContext("config.xml"); - AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); - context.register(CachingConfig.class); - context.refresh(); - final CustomerDataService service = context.getBean(CustomerDataService.class); - - final Customer cust = new Customer("Tom", "67-2, Downing Street, NY"); - service.getAddress(cust); - service.getAddress(cust); - - // fail("Unable to instantiate the CustomerDataService"); - } - -} diff --git a/spring-all/src/test/java/org/baeldung/caching/test/SpringCachingIntegrationTest.java b/spring-all/src/test/java/org/baeldung/caching/test/SpringCachingIntegrationTest.java new file mode 100644 index 0000000000..8c4ebaa7ec --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/caching/test/SpringCachingIntegrationTest.java @@ -0,0 +1,71 @@ +package org.baeldung.caching.test; + +import org.baeldung.caching.config.CachingConfig; +import org.baeldung.caching.example.Customer; +import org.baeldung.caching.example.CustomerDataService; +import org.baeldung.caching.example.CustomerServiceWithParent; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { CachingConfig.class }, loader = AnnotationConfigContextLoader.class) +public class SpringCachingIntegrationTest { + + @Autowired + private CustomerDataService service; + + @Autowired + private CustomerServiceWithParent serviceWithParent; + + // + + @Test + public void whenGettingAddress_thenCorrect() { + final Customer cust = new Customer("Tom", "67-2, Downing Street, NY"); + service.getAddress(cust); + service.getAddress(cust); + + service.getAddress1(cust); + service.getAddress1(cust); + + service.getAddress2(cust); + service.getAddress2(cust); + + service.getAddress3(cust); + service.getAddress3(cust); + + service.getAddress4(cust); + service.getAddress4(cust); + + service.getAddress5(cust); + service.getAddress5(cust); + } + + @Test + public void givenUsingServiceWithParent_whenGettingAddress_thenCorrect() { + final Customer cust = new Customer("Tom", "67-2, Downing Street, NY"); + + serviceWithParent.getAddress(cust); + serviceWithParent.getAddress(cust); + + serviceWithParent.getAddress1(cust); + serviceWithParent.getAddress1(cust); + + serviceWithParent.getAddress2(cust); + serviceWithParent.getAddress2(cust); + + serviceWithParent.getAddress3(cust); + serviceWithParent.getAddress3(cust); + + // serviceWithParent.getAddress4(cust); + // serviceWithParent.getAddress4(cust); + + serviceWithParent.getAddress5(cust); + serviceWithParent.getAddress5(cust); + } + +} diff --git a/spring-all/src/test/java/org/baeldung/customannotation/BeanWithGenericDAO.java b/spring-all/src/test/java/org/baeldung/customannotation/BeanWithGenericDAO.java index 32d4660f41..9ba915f296 100644 --- a/spring-all/src/test/java/org/baeldung/customannotation/BeanWithGenericDAO.java +++ b/spring-all/src/test/java/org/baeldung/customannotation/BeanWithGenericDAO.java @@ -5,10 +5,11 @@ import org.springframework.stereotype.Repository; @Repository public class BeanWithGenericDAO { - @DataAccess(entity=Person.class) + @DataAccess(entity = Person.class) private GenericDAO personGenericDAO; - public BeanWithGenericDAO() {} + public BeanWithGenericDAO() { + } public GenericDAO getPersonGenericDAO() { return personGenericDAO; diff --git a/spring-all/src/test/java/org/baeldung/customannotation/DataAccessFieldCallbackTest.java b/spring-all/src/test/java/org/baeldung/customannotation/DataAccessFieldCallbackTest.java index f025a3e00a..e47d03c961 100644 --- a/spring-all/src/test/java/org/baeldung/customannotation/DataAccessFieldCallbackTest.java +++ b/spring-all/src/test/java/org/baeldung/customannotation/DataAccessFieldCallbackTest.java @@ -15,7 +15,6 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { CustomAnnotationConfiguration.class }) public class DataAccessFieldCallbackTest { @@ -36,8 +35,7 @@ public class DataAccessFieldCallbackTest { } @Test - public void whenMethodGenericTypeIsValidCalled_thenReturnCorrectValue() - throws NoSuchFieldException, SecurityException { + public void whenMethodGenericTypeIsValidCalled_thenReturnCorrectValue() throws NoSuchFieldException, SecurityException { final DataAccessFieldCallback callback = new DataAccessFieldCallback(configurableListableBeanFactory, beanWithGenericDAO); final Type fieldType = BeanWithGenericDAO.class.getDeclaredField("personGenericDAO").getGenericType(); final boolean result = callback.genericTypeIsValid(Person.class, fieldType); diff --git a/spring-all/src/test/java/org/baeldung/scopes/ScopesTest.java b/spring-all/src/test/java/org/baeldung/scopes/ScopesTest.java new file mode 100644 index 0000000000..b1dd248c26 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/scopes/ScopesTest.java @@ -0,0 +1,43 @@ +package org.baeldung.scopes; + +import org.junit.Assert; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class ScopesTest { + + private static final String NAME = "John Smith"; + private static final String NAME_OTHER = "Anna Jones"; + + @Test + public void testScopeSingleton() { + final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("scopes.xml"); + + final Person personSingletonA = (Person) applicationContext.getBean("personSingleton"); + final Person personSingletonB = (Person) applicationContext.getBean("personSingleton"); + + personSingletonA.setName(NAME); + Assert.assertEquals(NAME, personSingletonB.getName()); + + ((AbstractApplicationContext) applicationContext).close(); + } + + @Test + public void testScopePrototype() { + final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("scopes.xml"); + + final Person personPrototypeA = (Person) applicationContext.getBean("personPrototype"); + final Person personPrototypeB = (Person) applicationContext.getBean("personPrototype"); + + personPrototypeA.setName(NAME); + personPrototypeB.setName(NAME_OTHER); + + Assert.assertEquals(NAME, personPrototypeA.getName()); + Assert.assertEquals(NAME_OTHER, personPrototypeB.getName()); + + ((AbstractApplicationContext) applicationContext).close(); + } + +} diff --git a/spring-apache-camel/.classpath b/spring-apache-camel/.classpath new file mode 100644 index 0000000000..698778fef3 --- /dev/null +++ b/spring-apache-camel/.classpath @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-apache-camel/.project b/spring-apache-camel/.project new file mode 100644 index 0000000000..7725877f6a --- /dev/null +++ b/spring-apache-camel/.project @@ -0,0 +1,29 @@ + + + spring-apache-camel + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + org.springframework.ide.eclipse.core.springbuilder + + + + + + org.springframework.ide.eclipse.core.springnature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/spring-apache-camel/README.md b/spring-apache-camel/README.md new file mode 100644 index 0000000000..4015760f7d --- /dev/null +++ b/spring-apache-camel/README.md @@ -0,0 +1,33 @@ + +

Configure and Use Apache Camel with Spring

+ +This article will demonstrate how to configure and use Apache Camel with Spring Framework. + +

Relevant Articles

+ + + +

Framework Versions:

+ +
    +
  • Spring 4.2.4
  • +
  • Apache Camel 2.16.1
  • +
+ +

Build and Run Application

+ +To build this application execute following maven command in ApacheCamelFileProcessor directory. + +mvn clean install + +To run this application you can either run our main class App from your IDE or you can execute following maven command: + +mvn exec:java -Dexec.mainClass="App" + +

Relevant Articles on Baeldung

+ diff --git a/spring-apache-camel/pom.xml b/spring-apache-camel/pom.xml new file mode 100644 index 0000000000..fbea9b779d --- /dev/null +++ b/spring-apache-camel/pom.xml @@ -0,0 +1,67 @@ + + 4.0.0 + org.apache.camel + spring-apache-camel + jar + 1.0-SNAPSHOT + spring-apache-camel + http://maven.apache.org + + + 2.16.1 + 4.2.4.RELEASE + 1.7 + 4.1 + + + + + + junit + junit + ${junit.version} + test + + + + org.apache.camel + camel-core + ${env.camel.version} + + + + org.apache.camel + camel-spring + ${env.camel.version} + + + + org.apache.camel + camel-stream + ${env.camel.version} + + + + org.springframework + spring-context + ${env.spring.version} + + + + + + + + maven-compiler-plugin + + true + true + ${java.version} + ${java.version} + ${java.version} + + + + + diff --git a/spring-apache-camel/src/main/java/com/baeldung/camel/main/App.java b/spring-apache-camel/src/main/java/com/baeldung/camel/main/App.java new file mode 100644 index 0000000000..ac0605a215 --- /dev/null +++ b/spring-apache-camel/src/main/java/com/baeldung/camel/main/App.java @@ -0,0 +1,12 @@ +package com.baeldung.camel.main; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class App { + public static void main(final String[] args) throws Exception { + ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context.xml"); + // Keep main thread alive for some time to let application finish processing the input files. + Thread.sleep(5000); + applicationContext.close(); + } +} \ No newline at end of file diff --git a/spring-apache-camel/src/main/java/com/baeldung/camel/processor/FileProcessor.java b/spring-apache-camel/src/main/java/com/baeldung/camel/processor/FileProcessor.java new file mode 100644 index 0000000000..971dd206cd --- /dev/null +++ b/spring-apache-camel/src/main/java/com/baeldung/camel/processor/FileProcessor.java @@ -0,0 +1,14 @@ +package com.baeldung.camel.processor; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; + +public class FileProcessor implements Processor { + + public void process(Exchange exchange) throws Exception { + String originalFileContent = exchange.getIn().getBody(String.class); + String upperCaseFileContent = originalFileContent.toUpperCase(); + exchange.getIn().setBody(upperCaseFileContent); + } + +} diff --git a/spring-apache-camel/src/main/resources/camel-context.xml b/spring-apache-camel/src/main/resources/camel-context.xml new file mode 100644 index 0000000000..0c10e0ecef --- /dev/null +++ b/spring-apache-camel/src/main/resources/camel-context.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + ${body.toLowerCase()} + + + + + + + + .......... File content conversion completed .......... + + + + + + + + + \ No newline at end of file diff --git a/spring-apache-camel/src/test/data/sampleInputFile/file.txt b/spring-apache-camel/src/test/data/sampleInputFile/file.txt new file mode 100644 index 0000000000..e057427864 --- /dev/null +++ b/spring-apache-camel/src/test/data/sampleInputFile/file.txt @@ -0,0 +1 @@ +This is data that will be processed by a Camel route! \ No newline at end of file diff --git a/spring-apache-camel/src/test/java/org/apache/camel/main/AppTest.java b/spring-apache-camel/src/test/java/org/apache/camel/main/AppTest.java new file mode 100644 index 0000000000..87b20369f3 --- /dev/null +++ b/spring-apache-camel/src/test/java/org/apache/camel/main/AppTest.java @@ -0,0 +1,117 @@ +package org.apache.camel.main; + +import com.baeldung.camel.main.App; +import junit.framework.TestCase; +import org.apache.camel.util.FileUtil; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.channels.FileChannel; +import java.nio.file.Files; +import java.nio.file.Paths; + +public class AppTest extends TestCase { + + private static final String FILE_NAME = "file.txt"; + private static final String SAMPLE_INPUT_DIR = "src/test/data/sampleInputFile/"; + private static final String TEST_INPUT_DIR = "src/test/data/input/"; + private static final String UPPERCASE_OUTPUT_DIR = "src/test/data/outputUpperCase/"; + private static final String LOWERCASE_OUTPUT_DIR = "src/test/data/outputLowerCase/"; + + @Before + public void setUp() throws Exception { + // Prepare input file for test + copySampleFileToInputDirectory(); + } + + @After + public void tearDown() throws Exception { + System.out.println("Deleting the test input and output files..."); + deleteFile(TEST_INPUT_DIR); + deleteFile(LOWERCASE_OUTPUT_DIR); + deleteFile(UPPERCASE_OUTPUT_DIR); + } + + @Test + public final void testMain() throws Exception { + App.main(null); + + String inputFileContent = readFileContent(SAMPLE_INPUT_DIR + FILE_NAME); + String outputUpperCase = readFileContent(UPPERCASE_OUTPUT_DIR + FILE_NAME); + String outputLowerCase = readFileContent(LOWERCASE_OUTPUT_DIR + FILE_NAME); + + System.out.println("Input File content = [" + inputFileContent + "]"); + System.out.println("UpperCaseOutput file content = [" + outputUpperCase + "]"); + System.out.println("LowerCaseOtput file content = [" + outputLowerCase + "]"); + + assertEquals(inputFileContent.toUpperCase(), outputUpperCase); + assertEquals(inputFileContent.toLowerCase(), outputLowerCase); + } + + private String readFileContent(String path) throws IOException { + byte[] encoded = Files.readAllBytes(Paths.get(path)); + return new String(encoded); + } + + private void deleteFile(String path) { + try { + FileUtil.removeDir(new File(path)); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Copy sample input file to input directory. + */ + private void copySampleFileToInputDirectory() { + File sourceFile = new File(SAMPLE_INPUT_DIR + FILE_NAME); + File destFile = new File(TEST_INPUT_DIR + FILE_NAME); + + if (!sourceFile.exists()) { + System.out.println("Sample input file not found at location = [" + SAMPLE_INPUT_DIR + FILE_NAME + "]. Please provide this file."); + } + + if (!destFile.exists()) { + try { + System.out.println("Creating input file = [" + TEST_INPUT_DIR + FILE_NAME + "]"); + + File destDir = new File(TEST_INPUT_DIR); + if (!destDir.exists()) { + destDir.mkdir(); + } + destFile.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + } + FileChannel source = null; + FileChannel destination = null; + + try { + source = new FileInputStream(sourceFile).getChannel(); + destination = new FileOutputStream(destFile).getChannel(); + if (destination != null && source != null) { + destination.transferFrom(source, 0, source.size()); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + source.close(); + } catch (Exception e) { + e.printStackTrace(); + } + try { + destination.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } +} diff --git a/spring-autowire/pom.xml b/spring-autowire/pom.xml new file mode 100644 index 0000000000..e28efdae61 --- /dev/null +++ b/spring-autowire/pom.xml @@ -0,0 +1,63 @@ + + 4.0.0 + + com.baeldung + spring-autowire + 0.0.1-SNAPSHOT + jar + + spring-autowire + http://maven.apache.org + + + UTF-8 + 4.2.5.RELEASE + 3.5.1 + 2.19.1 + + + + + junit + junit + 4.11 + + + org.springframework + spring-core + ${org.springframework.version} + + + org.springframework + spring-context + ${org.springframework.version} + + + org.springframework + spring-test + ${org.springframework.version} + test + + + + + spring-autowire + + + 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} + + + + diff --git a/spring-autowire/src/main/java/com/baeldung/autowire/sample/App.java b/spring-autowire/src/main/java/com/baeldung/autowire/sample/App.java new file mode 100644 index 0000000000..725a9b8406 --- /dev/null +++ b/spring-autowire/src/main/java/com/baeldung/autowire/sample/App.java @@ -0,0 +1,11 @@ +package com.baeldung.autowire.sample; + +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public class App { + public static void main(String[] args) { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class); + FooService fooService = ctx.getBean(FooService.class); + fooService.doStuff(); + } +} diff --git a/spring-autowire/src/main/java/com/baeldung/autowire/sample/AppConfig.java b/spring-autowire/src/main/java/com/baeldung/autowire/sample/AppConfig.java new file mode 100644 index 0000000000..f948e2bf8e --- /dev/null +++ b/spring-autowire/src/main/java/com/baeldung/autowire/sample/AppConfig.java @@ -0,0 +1,10 @@ +package com.baeldung.autowire.sample; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan("com.baeldung.autowire.sample") +public class AppConfig { + +} diff --git a/spring-autowire/src/main/java/com/baeldung/autowire/sample/Bar.java b/spring-autowire/src/main/java/com/baeldung/autowire/sample/Bar.java new file mode 100644 index 0000000000..7aa820adef --- /dev/null +++ b/spring-autowire/src/main/java/com/baeldung/autowire/sample/Bar.java @@ -0,0 +1,5 @@ +package com.baeldung.autowire.sample; + +public class Bar { + +} diff --git a/spring-autowire/src/main/java/com/baeldung/autowire/sample/BarFormatter.java b/spring-autowire/src/main/java/com/baeldung/autowire/sample/BarFormatter.java new file mode 100644 index 0000000000..fb704453fc --- /dev/null +++ b/spring-autowire/src/main/java/com/baeldung/autowire/sample/BarFormatter.java @@ -0,0 +1,13 @@ +package com.baeldung.autowire.sample; + +import org.springframework.stereotype.Component; + +@FormatterType("Bar") +@Component +public class BarFormatter implements Formatter { + + public String format() { + return "bar"; + } + +} diff --git a/spring-autowire/src/main/java/com/baeldung/autowire/sample/Foo.java b/spring-autowire/src/main/java/com/baeldung/autowire/sample/Foo.java new file mode 100644 index 0000000000..b587ab38b8 --- /dev/null +++ b/spring-autowire/src/main/java/com/baeldung/autowire/sample/Foo.java @@ -0,0 +1,5 @@ +package com.baeldung.autowire.sample; + +public class Foo { + +} diff --git a/spring-autowire/src/main/java/com/baeldung/autowire/sample/FooDAO.java b/spring-autowire/src/main/java/com/baeldung/autowire/sample/FooDAO.java new file mode 100644 index 0000000000..aec26202ab --- /dev/null +++ b/spring-autowire/src/main/java/com/baeldung/autowire/sample/FooDAO.java @@ -0,0 +1,5 @@ +package com.baeldung.autowire.sample; + +public class FooDAO { + +} diff --git a/spring-autowire/src/main/java/com/baeldung/autowire/sample/FooFormatter.java b/spring-autowire/src/main/java/com/baeldung/autowire/sample/FooFormatter.java new file mode 100644 index 0000000000..73966e9e43 --- /dev/null +++ b/spring-autowire/src/main/java/com/baeldung/autowire/sample/FooFormatter.java @@ -0,0 +1,13 @@ +package com.baeldung.autowire.sample; + +import org.springframework.stereotype.Component; + +@FormatterType("Foo") +@Component +public class FooFormatter implements Formatter { + + public String format() { + return "foo"; + } + +} diff --git a/spring-autowire/src/main/java/com/baeldung/autowire/sample/FooService.java b/spring-autowire/src/main/java/com/baeldung/autowire/sample/FooService.java new file mode 100644 index 0000000000..eccea4351a --- /dev/null +++ b/spring-autowire/src/main/java/com/baeldung/autowire/sample/FooService.java @@ -0,0 +1,17 @@ +package com.baeldung.autowire.sample; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class FooService { + + @Autowired + @FormatterType("Foo") + private Formatter formatter; + + public String doStuff(){ + return formatter.format(); + } + +} diff --git a/spring-autowire/src/main/java/com/baeldung/autowire/sample/Formatter.java b/spring-autowire/src/main/java/com/baeldung/autowire/sample/Formatter.java new file mode 100644 index 0000000000..83716d6e92 --- /dev/null +++ b/spring-autowire/src/main/java/com/baeldung/autowire/sample/Formatter.java @@ -0,0 +1,7 @@ +package com.baeldung.autowire.sample; + +public interface Formatter { + + String format(); + +} diff --git a/spring-autowire/src/main/java/com/baeldung/autowire/sample/FormatterType.java b/spring-autowire/src/main/java/com/baeldung/autowire/sample/FormatterType.java new file mode 100644 index 0000000000..7ffc308c9a --- /dev/null +++ b/spring-autowire/src/main/java/com/baeldung/autowire/sample/FormatterType.java @@ -0,0 +1,17 @@ +package com.baeldung.autowire.sample; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.beans.factory.annotation.Qualifier; + +@Qualifier +@Target({ElementType.FIELD, ElementType.METHOD,ElementType.TYPE, ElementType.PARAMETER}) +@Retention(RetentionPolicy.RUNTIME) +public @interface FormatterType { + + String value(); + +} diff --git a/spring-autowire/src/test/java/com/baeldung/autowire/sample/FooServiceTest.java b/spring-autowire/src/test/java/com/baeldung/autowire/sample/FooServiceTest.java new file mode 100644 index 0000000000..4607f527d9 --- /dev/null +++ b/spring-autowire/src/test/java/com/baeldung/autowire/sample/FooServiceTest.java @@ -0,0 +1,22 @@ +package com.baeldung.autowire.sample; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes=AppConfig.class, loader=AnnotationConfigContextLoader.class) +public class FooServiceTest { + + @Autowired + FooService fooService; + + @Test + public void whenFooFormatterType_thenReturnFoo(){ + Assert.assertEquals("foo", fooService.doStuff()); + } +} diff --git a/spring-batch/.classpath b/spring-batch/.classpath new file mode 100644 index 0000000000..e7ac9faf11 --- /dev/null +++ b/spring-batch/.classpath @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-batch/.project b/spring-batch/.project new file mode 100644 index 0000000000..0159a7237c --- /dev/null +++ b/spring-batch/.project @@ -0,0 +1,29 @@ + + + spring-batch + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + org.springframework.ide.eclipse.core.springbuilder + + + + + + org.springframework.ide.eclipse.core.springnature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/spring-batch/README.md b/spring-batch/README.md new file mode 100644 index 0000000000..953e652cea --- /dev/null +++ b/spring-batch/README.md @@ -0,0 +1,7 @@ +========= + +## Spring Batch + + +### Relevant Articles: +- [Introduction to Spring Batch](http://www.baeldung.com/introduction-to-spring-batch) diff --git a/spring-batch/pom.xml b/spring-batch/pom.xml new file mode 100644 index 0000000000..5538dd912f --- /dev/null +++ b/spring-batch/pom.xml @@ -0,0 +1,43 @@ + + 4.0.0 + + com.baeldung + spring-batch + 0.1-SNAPSHOT + jar + + spring-batch + http://maven.apache.org + + + UTF-8 + 4.2.0.RELEASE + 3.0.5.RELEASE + 3.8.11.2 + + + + + + org.xerial + sqlite-jdbc + ${sqlite.version} + + + org.springframework + spring-oxm + ${spring.version} + + + org.springframework + spring-jdbc + ${spring.version} + + + org.springframework.batch + spring-batch-core + ${spring.batch.version} + + + + diff --git a/spring-batch/src/main/java/org/baeldung/spring_batch_intro/App.java b/spring-batch/src/main/java/org/baeldung/spring_batch_intro/App.java new file mode 100644 index 0000000000..2ce4dae6e6 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/spring_batch_intro/App.java @@ -0,0 +1,32 @@ +package org.baeldung.spring_batch_intro; + +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public class App { + public static void main(final String[] args) { + // Spring Java config + final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + context.register(SpringConfig.class); + context.register(SpringBatchConfig.class); + context.refresh(); + + // Spring xml config + // ApplicationContext context = new ClassPathXmlApplicationContext("spring-batch.xml"); + + final JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); + final Job job = (Job) context.getBean("firstBatchJob"); + System.out.println("Starting the batch job"); + try { + final JobExecution execution = jobLauncher.run(job, new JobParameters()); + System.out.println("Job Status : " + execution.getStatus()); + System.out.println("Job succeeded"); + } catch (final Exception e) { + e.printStackTrace(); + System.out.println("Job failed"); + } + } +} \ No newline at end of file diff --git a/spring-batch/src/main/java/org/baeldung/spring_batch_intro/SpringBatchConfig.java b/spring-batch/src/main/java/org/baeldung/spring_batch_intro/SpringBatchConfig.java new file mode 100644 index 0000000000..9973005c7c --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/spring_batch_intro/SpringBatchConfig.java @@ -0,0 +1,88 @@ +package org.baeldung.spring_batch_intro; + +import java.net.MalformedURLException; +import java.text.ParseException; + +import org.baeldung.spring_batch_intro.model.Transaction; +import org.baeldung.spring_batch_intro.service.CustomItemProcessor; +import org.baeldung.spring_batch_intro.service.RecordFieldSetMapper; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.item.ItemProcessor; +import org.springframework.batch.item.ItemReader; +import org.springframework.batch.item.ItemWriter; +import org.springframework.batch.item.UnexpectedInputException; +import org.springframework.batch.item.file.FlatFileItemReader; +import org.springframework.batch.item.file.mapping.DefaultLineMapper; +import org.springframework.batch.item.file.transform.DelimitedLineTokenizer; +import org.springframework.batch.item.xml.StaxEventItemWriter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.core.io.Resource; +import org.springframework.oxm.Marshaller; +import org.springframework.oxm.jaxb.Jaxb2Marshaller; + +public class SpringBatchConfig { + @Autowired + private JobBuilderFactory jobs; + + @Autowired + private StepBuilderFactory steps; + + @Value("input/record.csv") + private Resource inputCsv; + + @Value("file:xml/output.xml") + private Resource outputXml; + + @Bean + public ItemReader itemReader() throws UnexpectedInputException, ParseException { + FlatFileItemReader reader = new FlatFileItemReader(); + DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer(); + String[] tokens = { "username", "userid", "transactiondate", "amount" }; + tokenizer.setNames(tokens); + reader.setResource(inputCsv); + DefaultLineMapper lineMapper = new DefaultLineMapper(); + lineMapper.setLineTokenizer(tokenizer); + lineMapper.setFieldSetMapper(new RecordFieldSetMapper()); + reader.setLinesToSkip(1); + reader.setLineMapper(lineMapper); + return reader; + } + + @Bean + public ItemProcessor itemProcessor() { + return new CustomItemProcessor(); + } + + @Bean + public ItemWriter itemWriter(Marshaller marshaller) throws MalformedURLException { + StaxEventItemWriter itemWriter = new StaxEventItemWriter(); + itemWriter.setMarshaller(marshaller); + itemWriter.setRootTagName("transactionRecord"); + itemWriter.setResource(outputXml); + return itemWriter; + } + + @Bean + public Marshaller marshaller() { + Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); + marshaller.setClassesToBeBound(new Class[] { Transaction.class }); + return marshaller; + } + + @Bean + protected Step step1(ItemReader reader, ItemProcessor processor, ItemWriter writer) { + return steps.get("step1"). chunk(10).reader(reader).processor(processor).writer(writer).build(); + } + + @Bean(name = "firstBatchJob") + public Job job(@Qualifier("step1") Step step1) { + return jobs.get("firstBatchJob").start(step1).build(); + } + +} diff --git a/spring-batch/src/main/java/org/baeldung/spring_batch_intro/SpringConfig.java b/spring-batch/src/main/java/org/baeldung/spring_batch_intro/SpringConfig.java new file mode 100644 index 0000000000..ed7d302047 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/spring_batch_intro/SpringConfig.java @@ -0,0 +1,78 @@ +package org.baeldung.spring_batch_intro; + +import java.net.MalformedURLException; + +import javax.sql.DataSource; + +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.core.launch.support.SimpleJobLauncher; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean; +import org.springframework.batch.support.transaction.ResourcelessTransactionManager; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.Resource; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.jdbc.datasource.init.DataSourceInitializer; +import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; +import org.springframework.transaction.PlatformTransactionManager; + +@Configuration +@EnableBatchProcessing +public class SpringConfig { + + @Value("org/springframework/batch/core/schema-drop-sqlite.sql") + private Resource dropReopsitoryTables; + + @Value("org/springframework/batch/core/schema-sqlite.sql") + private Resource dataReopsitorySchema; + + @Bean + public DataSource dataSource() { + DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName("org.sqlite.JDBC"); + dataSource.setUrl("jdbc:sqlite:repository.sqlite"); + return dataSource; + } + + @Bean + public DataSourceInitializer dataSourceInitializer(DataSource dataSource) throws MalformedURLException { + ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); + + databasePopulator.addScript(dropReopsitoryTables); + databasePopulator.addScript(dataReopsitorySchema); + databasePopulator.setIgnoreFailedDrops(true); + + DataSourceInitializer initializer = new DataSourceInitializer(); + initializer.setDataSource(dataSource); + initializer.setDatabasePopulator(databasePopulator); + + return initializer; + } + + private JobRepository getJobRepository() throws Exception { + JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); + factory.setDataSource(dataSource()); + factory.setTransactionManager(getTransactionManager()); + // JobRepositoryFactoryBean's methods Throws Generic Exception, + // it would have been better to have a specific one + factory.afterPropertiesSet(); + return (JobRepository) factory.getObject(); + } + + private PlatformTransactionManager getTransactionManager() { + return new ResourcelessTransactionManager(); + } + + public JobLauncher getJobLauncher() throws Exception { + SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); + // SimpleJobLauncher's methods Throws Generic Exception, + // it would have been better to have a specific one + jobLauncher.setJobRepository(getJobRepository()); + jobLauncher.afterPropertiesSet(); + return jobLauncher; + } + +} \ No newline at end of file diff --git a/spring-batch/src/main/java/org/baeldung/spring_batch_intro/model/Transaction.java b/spring-batch/src/main/java/org/baeldung/spring_batch_intro/model/Transaction.java new file mode 100644 index 0000000000..3b2b9610f2 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/spring_batch_intro/model/Transaction.java @@ -0,0 +1,54 @@ +package org.baeldung.spring_batch_intro.model; + +import java.util.Date; + +import javax.xml.bind.annotation.XmlRootElement; + +@SuppressWarnings("restriction") +@XmlRootElement(name = "transactionRecord") +public class Transaction { + private String username; + private int userId; + private Date transactionDate; + private double amount; + + /* getters and setters for the attributes */ + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public int getUserId() { + return userId; + } + + public void setUserId(int userId) { + this.userId = userId; + } + + public Date getTransactionDate() { + return transactionDate; + } + + public void setTransactionDate(Date transactionDate) { + this.transactionDate = transactionDate; + } + + public double getAmount() { + return amount; + } + + public void setAmount(double amount) { + this.amount = amount; + } + + @Override + public String toString() { + return "Transaction [username=" + username + ", userId=" + userId + ", transactionDate=" + transactionDate + ", amount=" + amount + "]"; + } + +} diff --git a/spring-batch/src/main/java/org/baeldung/spring_batch_intro/service/CustomItemProcessor.java b/spring-batch/src/main/java/org/baeldung/spring_batch_intro/service/CustomItemProcessor.java new file mode 100644 index 0000000000..ebee1d2802 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/spring_batch_intro/service/CustomItemProcessor.java @@ -0,0 +1,12 @@ +package org.baeldung.spring_batch_intro.service; + +import org.baeldung.spring_batch_intro.model.Transaction; +import org.springframework.batch.item.ItemProcessor; + +public class CustomItemProcessor implements ItemProcessor { + + public Transaction process(Transaction item) { + System.out.println("Processing..." + item); + return item; + } +} \ No newline at end of file diff --git a/spring-batch/src/main/java/org/baeldung/spring_batch_intro/service/RecordFieldSetMapper.java b/spring-batch/src/main/java/org/baeldung/spring_batch_intro/service/RecordFieldSetMapper.java new file mode 100644 index 0000000000..94f9e7d94e --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/spring_batch_intro/service/RecordFieldSetMapper.java @@ -0,0 +1,35 @@ +package org.baeldung.spring_batch_intro.service; + +import java.text.ParseException; +import java.text.SimpleDateFormat; + +import org.baeldung.spring_batch_intro.model.Transaction; +import org.springframework.batch.item.file.mapping.FieldSetMapper; +import org.springframework.batch.item.file.transform.FieldSet; +import org.springframework.validation.BindException; + +public class RecordFieldSetMapper implements FieldSetMapper { + + public Transaction mapFieldSet(FieldSet fieldSet) throws BindException { + + SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); + Transaction transaction = new Transaction(); + // you can either use the indices or custom names + // I personally prefer the custom names easy for debugging and + // validating the pipelines + transaction.setUsername(fieldSet.readString("username")); + transaction.setUserId(fieldSet.readInt("userid")); + transaction.setAmount(fieldSet.readDouble(3)); + // Converting the date + String dateString = fieldSet.readString(2); + try { + transaction.setTransactionDate(dateFormat.parse(dateString)); + } catch (ParseException e) { + e.printStackTrace(); + } + + return transaction; + + } + +} diff --git a/spring-batch/src/main/resources/input/record.csv b/spring-batch/src/main/resources/input/record.csv new file mode 100644 index 0000000000..e554becb2a --- /dev/null +++ b/spring-batch/src/main/resources/input/record.csv @@ -0,0 +1,4 @@ +username, user_id, transaction_date, transaction_amount +devendra, 1234, 31/10/2015, 10000 +john, 2134, 3/12/2015, 12321 +robin, 2134, 2/02/2015, 23411 \ No newline at end of file diff --git a/spring-batch/src/main/resources/spring-batch-intro.xml b/spring-batch/src/main/resources/spring-batch-intro.xml new file mode 100644 index 0000000000..93606d232f --- /dev/null +++ b/spring-batch/src/main/resources/spring-batch-intro.xml @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + org.baeldung.spring_batch_intro.model.Transaction + + + + + + + + + + + + + diff --git a/spring-batch/src/main/resources/spring.xml b/spring-batch/src/main/resources/spring.xml new file mode 100644 index 0000000000..dea261c5e6 --- /dev/null +++ b/spring-batch/src/main/resources/spring.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-batch/xml/output.xml b/spring-batch/xml/output.xml new file mode 100644 index 0000000000..acf4969341 --- /dev/null +++ b/spring-batch/xml/output.xml @@ -0,0 +1 @@ +10000.02015-10-31T00:00:00+05:301234devendra12321.02015-12-03T00:00:00+05:302134john23411.02015-02-02T00:00:00+05:302134robin \ No newline at end of file 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-boot/pom.xml b/spring-boot/pom.xml index 3a116a2974..d0a66197bf 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -1,6 +1,6 @@ 4.0.0 - org.baeldung + com.baeldung spring-boot 0.0.1-SNAPSHOT war @@ -11,7 +11,7 @@ org.springframework.boot spring-boot-starter-parent - 1.2.6.RELEASE + 1.3.3.RELEASE diff --git a/spring-data-cassandra/.classpath b/spring-data-cassandra/.classpath new file mode 100644 index 0000000000..698778fef3 --- /dev/null +++ b/spring-data-cassandra/.classpath @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-data-cassandra/.project b/spring-data-cassandra/.project new file mode 100644 index 0000000000..239fa4f002 --- /dev/null +++ b/spring-data-cassandra/.project @@ -0,0 +1,29 @@ + + + spring-data-cassandra + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + org.springframework.ide.eclipse.core.springbuilder + + + + + + org.springframework.ide.eclipse.core.springnature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/spring-data-cassandra/README.md b/spring-data-cassandra/README.md new file mode 100644 index 0000000000..456eefcf18 --- /dev/null +++ b/spring-data-cassandra/README.md @@ -0,0 +1,16 @@ +## Spring Data Cassandra + +### Relevant Articles: +- [Introduction to Spring Data Cassandra](http://www.baeldung.com/spring-data-cassandra-tutorial) +- [Using the CassandraTemplate from Spring Data](http://www.baeldung.com/spring-data-cassandratemplate-cqltemplate) + +### Build the Project with Tests Running +``` +mvn clean install +``` + +### Run Tests Directly +``` +mvn test +``` + diff --git a/spring-data-cassandra/pom.xml b/spring-data-cassandra/pom.xml index 13c5c18529..e5f8779942 100644 --- a/spring-data-cassandra/pom.xml +++ b/spring-data-cassandra/pom.xml @@ -2,7 +2,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - org.baeldung + com.baeldung spring-data-cassandra 0.0.1-SNAPSHOT jar @@ -11,8 +11,11 @@ UTF-8 - 1.3.1.RELEASE - 4.2.2.RELEASE + + 4.2.5.RELEASE + + 1.3.2.RELEASE + 4.11 1.7.12 1.1.3 diff --git a/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/config/CassandraConfig.java b/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/config/CassandraConfig.java index 5f2c4c6d47..2edd5551a5 100644 --- a/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/config/CassandraConfig.java +++ b/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/config/CassandraConfig.java @@ -17,8 +17,8 @@ import org.springframework.data.cassandra.repository.config.EnableCassandraRepos @PropertySource(value = { "classpath:cassandra.properties" }) @EnableCassandraRepositories(basePackages = "org.baeldung.spring.data.cassandra.repository") public class CassandraConfig extends AbstractCassandraConfiguration { - private static final Log LOGGER = LogFactory.getLog(CassandraConfig.class); + @Autowired private Environment environment; @@ -27,15 +27,17 @@ public class CassandraConfig extends AbstractCassandraConfiguration { return environment.getProperty("cassandra.keyspace"); } + @Override @Bean public CassandraClusterFactoryBean cluster() { - CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean(); + final CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean(); cluster.setContactPoints(environment.getProperty("cassandra.contactpoints")); cluster.setPort(Integer.parseInt(environment.getProperty("cassandra.port"))); LOGGER.info("Cluster created with contact points [" + environment.getProperty("cassandra.contactpoints") + "] " + "& port [" + Integer.parseInt(environment.getProperty("cassandra.port")) + "]."); return cluster; } + @Override @Bean public CassandraMappingContext cassandraMapping() throws ClassNotFoundException { return new BasicCassandraMappingContext(); diff --git a/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/model/Book.java b/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/model/Book.java index ebb619c456..a8ec81d6b5 100644 --- a/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/model/Book.java +++ b/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/model/Book.java @@ -1,28 +1,31 @@ package org.baeldung.spring.data.cassandra.model; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; + import org.springframework.cassandra.core.Ordering; import org.springframework.cassandra.core.PrimaryKeyType; import org.springframework.data.cassandra.mapping.Column; import org.springframework.data.cassandra.mapping.PrimaryKeyColumn; import org.springframework.data.cassandra.mapping.Table; -import java.util.HashSet; -import java.util.Set; -import java.util.UUID; - @Table public class Book { - @PrimaryKeyColumn(name = "isbn", ordinal = 2, type = PrimaryKeyType.CLUSTERED, ordering = Ordering.DESCENDING) + + @PrimaryKeyColumn(name = "id", ordinal = 0, type = PrimaryKeyType.CLUSTERED, ordering = Ordering.DESCENDING) private UUID id; - @PrimaryKeyColumn(name = "title", ordinal = 0, type = PrimaryKeyType.PARTITIONED) + + @PrimaryKeyColumn(name = "title", ordinal = 1, type = PrimaryKeyType.PARTITIONED) private String title; - @PrimaryKeyColumn(name = "publisher", ordinal = 1, type = PrimaryKeyType.PARTITIONED) + @PrimaryKeyColumn(name = "publisher", ordinal = 2, type = PrimaryKeyType.PARTITIONED) private String publisher; + @Column private Set tags = new HashSet<>(); - public Book(UUID id, String title, String publisher, Set tags) { + public Book(final UUID id, final String title, final String publisher, final Set tags) { this.id = id; this.title = title; this.publisher = publisher; @@ -45,19 +48,20 @@ public class Book { return tags; } - public void setId(UUID id) { + public void setId(final UUID id) { this.id = id; } - public void setTitle(String title) { + public void setTitle(final String title) { this.title = title; } - public void setPublisher(String publisher) { + public void setPublisher(final String publisher) { this.publisher = publisher; } - public void setTags(Set tags) { + public void setTags(final Set tags) { this.tags = tags; } + } diff --git a/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/repository/BookRepository.java b/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/repository/BookRepository.java index e37ae78b59..66d656ac3a 100644 --- a/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/repository/BookRepository.java +++ b/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/repository/BookRepository.java @@ -7,6 +7,8 @@ import org.springframework.stereotype.Repository; @Repository public interface BookRepository extends CassandraRepository { + @Query("select * from book where title = ?0 and publisher=?1") Iterable findByTitleAndPublisher(String title, String publisher); + } diff --git a/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/BookRepositoryIntegrationTest.java b/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/BookRepositoryIntegrationTest.java index 4b2893252a..8cbcdc3195 100644 --- a/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/BookRepositoryIntegrationTest.java +++ b/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/BookRepositoryIntegrationTest.java @@ -1,9 +1,11 @@ package org.baeldung.spring.data.cassandra.repository; -import com.datastax.driver.core.Cluster; -import com.datastax.driver.core.Session; -import com.datastax.driver.core.utils.UUIDs; -import com.google.common.collect.ImmutableSet; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import java.io.IOException; +import java.util.HashMap; + import org.apache.cassandra.exceptions.ConfigurationException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -11,7 +13,11 @@ import org.apache.thrift.transport.TTransportException; import org.baeldung.spring.data.cassandra.config.CassandraConfig; import org.baeldung.spring.data.cassandra.model.Book; import org.cassandraunit.utils.EmbeddedCassandraServerHelper; -import org.junit.*; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cassandra.core.cql.CqlIdentifier; @@ -19,16 +25,14 @@ import org.springframework.data.cassandra.core.CassandraAdminOperations; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import java.io.IOException; -import java.util.HashMap; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Session; +import com.datastax.driver.core.utils.UUIDs; +import com.google.common.collect.ImmutableSet; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = CassandraConfig.class) public class BookRepositoryIntegrationTest { - private static final Log LOGGER = LogFactory.getLog(BookRepositoryIntegrationTest.class); public static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS testKeySpace WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' };"; @@ -43,17 +47,18 @@ public class BookRepositoryIntegrationTest { @Autowired private CassandraAdminOperations adminTemplate; + // + @BeforeClass public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException { EmbeddedCassandraServerHelper.startEmbeddedCassandra(); - Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1") - .withPort(9142).build(); + final Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").withPort(9142).build(); LOGGER.info("Server Started at 127.0.0.1:9142... "); - Session session = cluster.connect(); + final Session session = cluster.connect(); session.execute(KEYSPACE_CREATION_QUERY); session.execute(KEYSPACE_ACTIVATE_QUERY); - Thread.sleep(5000); LOGGER.info("KeySpace created and activated."); + Thread.sleep(5000); } @Before @@ -63,54 +68,54 @@ public class BookRepositoryIntegrationTest { @Test public void whenSavingBook_thenAvailableOnRetrieval() { - Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", - "O'Reilly Media", ImmutableSet.of("Computer", "Software")); + final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); bookRepository.save(ImmutableSet.of(javaBook)); - Iterable books = bookRepository.findByTitleAndPublisher("Head First Java", "O'Reilly Media"); + final Iterable books = bookRepository.findByTitleAndPublisher("Head First Java", "O'Reilly Media"); assertEquals(javaBook.getId(), books.iterator().next().getId()); } @Test public void whenUpdatingBooks_thenAvailableOnRetrieval() { - Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); + final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); bookRepository.save(ImmutableSet.of(javaBook)); - Iterable books = bookRepository.findByTitleAndPublisher("Head First Java", "O'Reilly Media"); + final Iterable books = bookRepository.findByTitleAndPublisher("Head First Java", "O'Reilly Media"); javaBook.setTitle("Head First Java Second Edition"); bookRepository.save(ImmutableSet.of(javaBook)); - Iterable updateBooks = bookRepository.findByTitleAndPublisher("Head First Java Second Edition", "O'Reilly Media"); + final Iterable updateBooks = bookRepository.findByTitleAndPublisher("Head First Java Second Edition", "O'Reilly Media"); assertEquals(javaBook.getTitle(), updateBooks.iterator().next().getTitle()); } @Test(expected = java.util.NoSuchElementException.class) public void whenDeletingExistingBooks_thenNotAvailableOnRetrieval() { - Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); + final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); bookRepository.save(ImmutableSet.of(javaBook)); bookRepository.delete(javaBook); - Iterable books = bookRepository.findByTitleAndPublisher("Head First Java", "O'Reilly Media"); + final Iterable books = bookRepository.findByTitleAndPublisher("Head First Java", "O'Reilly Media"); assertNotEquals(javaBook.getId(), books.iterator().next().getId()); } @Test public void whenSavingBooks_thenAllShouldAvailableOnRetrieval() { - Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", - "O'Reilly Media", ImmutableSet.of("Computer", "Software")); - Book dPatternBook = new Book(UUIDs.timeBased(), "Head Design Patterns", - "O'Reilly Media", ImmutableSet.of("Computer", "Software")); + final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); + final Book dPatternBook = new Book(UUIDs.timeBased(), "Head Design Patterns", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); bookRepository.save(ImmutableSet.of(javaBook)); bookRepository.save(ImmutableSet.of(dPatternBook)); - Iterable books = bookRepository.findAll(); + final Iterable books = bookRepository.findAll(); int bookCount = 0; - for (Book book : books) bookCount++; + for (final Book book : books) { + bookCount++; + } assertEquals(bookCount, 2); } @After public void dropTable() { - adminTemplate.dropTable(CqlIdentifier.cqlId(DATA_TABLE_NAME)); + adminTemplate.dropTable(CqlIdentifier.cqlId(DATA_TABLE_NAME)); } @AfterClass public static void stopCassandraEmbedded() { EmbeddedCassandraServerHelper.cleanEmbeddedCassandra(); } + } diff --git a/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/CassandraTemplateIntegrationTest.java b/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/CassandraTemplateIntegrationTest.java new file mode 100644 index 0000000000..e331ac3cd4 --- /dev/null +++ b/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/CassandraTemplateIntegrationTest.java @@ -0,0 +1,154 @@ +package org.baeldung.spring.data.cassandra.repository; + +import static junit.framework.TestCase.assertNull; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import org.apache.cassandra.exceptions.ConfigurationException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.thrift.transport.TTransportException; +import org.baeldung.spring.data.cassandra.config.CassandraConfig; +import org.baeldung.spring.data.cassandra.model.Book; +import org.cassandraunit.utils.EmbeddedCassandraServerHelper; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cassandra.core.cql.CqlIdentifier; +import org.springframework.data.cassandra.core.CassandraAdminOperations; +import org.springframework.data.cassandra.core.CassandraOperations; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Session; +import com.datastax.driver.core.querybuilder.QueryBuilder; +import com.datastax.driver.core.querybuilder.Select; +import com.datastax.driver.core.utils.UUIDs; +import com.google.common.collect.ImmutableSet; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = CassandraConfig.class) +public class CassandraTemplateIntegrationTest { + private static final Log LOGGER = LogFactory.getLog(CassandraTemplateIntegrationTest.class); + + public static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS testKeySpace " + "WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' };"; + + public static final String KEYSPACE_ACTIVATE_QUERY = "USE testKeySpace;"; + + public static final String DATA_TABLE_NAME = "book"; + + @Autowired + private CassandraAdminOperations adminTemplate; + + @Autowired + private CassandraOperations cassandraTemplate; + + // + + @BeforeClass + public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException { + EmbeddedCassandraServerHelper.startEmbeddedCassandra(); + final Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").withPort(9142).build(); + LOGGER.info("Server Started at 127.0.0.1:9142... "); + final Session session = cluster.connect(); + session.execute(KEYSPACE_CREATION_QUERY); + session.execute(KEYSPACE_ACTIVATE_QUERY); + LOGGER.info("KeySpace created and activated."); + Thread.sleep(5000); + } + + @Before + public void createTable() throws InterruptedException, TTransportException, ConfigurationException, IOException { + adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap()); + } + + @Test + public void whenSavingBook_thenAvailableOnRetrieval() { + final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); + cassandraTemplate.insert(javaBook); + final Select select = QueryBuilder.select().from("book").where(QueryBuilder.eq("title", "Head First Java")).and(QueryBuilder.eq("publisher", "O'Reilly Media")).limit(10); + final Book retrievedBook = cassandraTemplate.selectOne(select, Book.class); + assertEquals(javaBook.getId(), retrievedBook.getId()); + } + + @Test + public void whenSavingBooks_thenAllAvailableOnRetrieval() { + final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); + final Book dPatternBook = new Book(UUIDs.timeBased(), "Head Design Patterns", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); + final List bookList = new ArrayList<>(); + bookList.add(javaBook); + bookList.add(dPatternBook); + cassandraTemplate.insert(bookList); + + final Select select = QueryBuilder.select().from("book").limit(10); + final List retrievedBooks = cassandraTemplate.select(select, Book.class); + assertThat(retrievedBooks.size(), is(2)); + assertEquals(javaBook.getId(), retrievedBooks.get(0).getId()); + assertEquals(dPatternBook.getId(), retrievedBooks.get(1).getId()); + } + + @Test + public void whenUpdatingBook_thenShouldUpdatedOnRetrieval() { + final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); + cassandraTemplate.insert(javaBook); + final Select select = QueryBuilder.select().from("book").limit(10); + final Book retrievedBook = cassandraTemplate.selectOne(select, Book.class); + retrievedBook.setTags(ImmutableSet.of("Java", "Programming")); + cassandraTemplate.update(retrievedBook); + final Book retrievedUpdatedBook = cassandraTemplate.selectOne(select, Book.class); + assertEquals(retrievedBook.getTags(), retrievedUpdatedBook.getTags()); + } + + @Test + public void whenDeletingASelectedBook_thenNotAvailableOnRetrieval() throws InterruptedException { + final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "OReilly Media", ImmutableSet.of("Computer", "Software")); + cassandraTemplate.insert(javaBook); + cassandraTemplate.delete(javaBook); + final Select select = QueryBuilder.select().from("book").limit(10); + final Book retrievedUpdatedBook = cassandraTemplate.selectOne(select, Book.class); + assertNull(retrievedUpdatedBook); + } + + @Test + public void whenDeletingAllBooks_thenNotAvailableOnRetrieval() { + final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); + final Book dPatternBook = new Book(UUIDs.timeBased(), "Head Design Patterns", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); + cassandraTemplate.insert(javaBook); + cassandraTemplate.insert(dPatternBook); + cassandraTemplate.deleteAll(Book.class); + final Select select = QueryBuilder.select().from("book").limit(10); + final Book retrievedUpdatedBook = cassandraTemplate.selectOne(select, Book.class); + assertNull(retrievedUpdatedBook); + } + + @Test + public void whenAddingBooks_thenCountShouldBeCorrectOnRetrieval() { + final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); + final Book dPatternBook = new Book(UUIDs.timeBased(), "Head Design Patterns", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); + cassandraTemplate.insert(javaBook); + cassandraTemplate.insert(dPatternBook); + final long bookCount = cassandraTemplate.count(Book.class); + assertEquals(2, bookCount); + } + + @After + public void dropTable() { + adminTemplate.dropTable(CqlIdentifier.cqlId(DATA_TABLE_NAME)); + } + + @AfterClass + public static void stopCassandraEmbedded() { + EmbeddedCassandraServerHelper.cleanEmbeddedCassandra(); + } +} diff --git a/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/CqlQueriesIntegrationTest.java b/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/CqlQueriesIntegrationTest.java new file mode 100644 index 0000000000..f7e42ae23b --- /dev/null +++ b/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/CqlQueriesIntegrationTest.java @@ -0,0 +1,124 @@ +package org.baeldung.spring.data.cassandra.repository; + +import static junit.framework.TestCase.assertEquals; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.UUID; + +import org.apache.cassandra.exceptions.ConfigurationException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.thrift.transport.TTransportException; +import org.baeldung.spring.data.cassandra.config.CassandraConfig; +import org.baeldung.spring.data.cassandra.model.Book; +import org.cassandraunit.utils.EmbeddedCassandraServerHelper; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cassandra.core.cql.CqlIdentifier; +import org.springframework.data.cassandra.core.CassandraAdminOperations; +import org.springframework.data.cassandra.core.CassandraOperations; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Session; +import com.datastax.driver.core.querybuilder.Insert; +import com.datastax.driver.core.querybuilder.QueryBuilder; +import com.datastax.driver.core.querybuilder.Select; +import com.datastax.driver.core.utils.UUIDs; +import com.google.common.collect.ImmutableSet; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = CassandraConfig.class) +public class CqlQueriesIntegrationTest { + private static final Log LOGGER = LogFactory.getLog(CqlQueriesIntegrationTest.class); + + public static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS testKeySpace " + "WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' };"; + + public static final String KEYSPACE_ACTIVATE_QUERY = "USE testKeySpace;"; + + public static final String DATA_TABLE_NAME = "book"; + + @Autowired + private CassandraAdminOperations adminTemplate; + + @Autowired + private CassandraOperations cassandraTemplate; + + // + + @BeforeClass + public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException { + EmbeddedCassandraServerHelper.startEmbeddedCassandra(25000); + final Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").withPort(9142).build(); + LOGGER.info("Server Started at 127.0.0.1:9142... "); + final Session session = cluster.connect(); + session.execute(KEYSPACE_CREATION_QUERY); + session.execute(KEYSPACE_ACTIVATE_QUERY); + LOGGER.info("KeySpace created and activated."); + Thread.sleep(5000); + } + + @Before + public void createTable() throws InterruptedException, TTransportException, ConfigurationException, IOException { + adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap()); + } + + @Test + public void whenSavingBook_thenAvailableOnRetrieval_usingQueryBuilder() { + final UUID uuid = UUIDs.timeBased(); + final Insert insert = QueryBuilder.insertInto(DATA_TABLE_NAME).value("id", uuid).value("title", "Head First Java").value("publisher", "OReilly Media").value("tags", ImmutableSet.of("Software")); + cassandraTemplate.execute(insert); + final Select select = QueryBuilder.select().from("book").limit(10); + final Book retrievedBook = cassandraTemplate.selectOne(select, Book.class); + assertEquals(uuid, retrievedBook.getId()); + } + + @Test + public void whenSavingBook_thenAvailableOnRetrieval_usingCQLStatements() { + final UUID uuid = UUIDs.timeBased(); + final String insertCql = "insert into book (id, title, publisher, tags) values " + "(" + uuid + ", 'Head First Java', 'OReilly Media', {'Software'})"; + cassandraTemplate.execute(insertCql); + final Select select = QueryBuilder.select().from("book").limit(10); + final Book retrievedBook = cassandraTemplate.selectOne(select, Book.class); + assertEquals(uuid, retrievedBook.getId()); + } + + @Test + public void whenSavingBook_thenAvailableOnRetrieval_usingPreparedStatements() throws InterruptedException { + final UUID uuid = UUIDs.timeBased(); + final String insertPreparedCql = "insert into book (id, title, publisher, tags) values (?, ?, ?, ?)"; + final List singleBookArgsList = new ArrayList<>(); + final List> bookList = new ArrayList<>(); + singleBookArgsList.add(uuid); + singleBookArgsList.add("Head First Java"); + singleBookArgsList.add("OReilly Media"); + singleBookArgsList.add(ImmutableSet.of("Software")); + bookList.add(singleBookArgsList); + cassandraTemplate.ingest(insertPreparedCql, bookList); + // This may not be required, just added to avoid any transient issues + Thread.sleep(5000); + final Select select = QueryBuilder.select().from("book"); + final Book retrievedBook = cassandraTemplate.selectOne(select, Book.class); + assertEquals(uuid, retrievedBook.getId()); + } + + @After + public void dropTable() { + adminTemplate.dropTable(CqlIdentifier.cqlId(DATA_TABLE_NAME)); + } + + @AfterClass + public static void stopCassandraEmbedded() { + EmbeddedCassandraServerHelper.cleanEmbeddedCassandra(); + } + +} diff --git a/spring-data-couchbase-2/.classpath b/spring-data-couchbase-2/.classpath new file mode 100644 index 0000000000..679a31b6da --- /dev/null +++ b/spring-data-couchbase-2/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/spring-data-couchbase-2/.project b/spring-data-couchbase-2/.project new file mode 100644 index 0000000000..1690ad8ce2 --- /dev/null +++ b/spring-data-couchbase-2/.project @@ -0,0 +1,30 @@ + + + spring-data-couchbase-2 + This project is a simple template for a jar utility using Spring. + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.springframework.ide.eclipse.core.springbuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.springframework.ide.eclipse.core.springnature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/spring-security-login-and-registration/.springBeans b/spring-data-couchbase-2/.springBeans similarity index 52% rename from spring-security-login-and-registration/.springBeans rename to spring-data-couchbase-2/.springBeans index 8096aa036b..0c014a97b6 100644 --- a/spring-security-login-and-registration/.springBeans +++ b/spring-data-couchbase-2/.springBeans @@ -1,15 +1,20 @@ 1 - + - - + + + true + false + + + diff --git a/spring-data-couchbase-2/README.md b/spring-data-couchbase-2/README.md new file mode 100644 index 0000000000..3ed226fb33 --- /dev/null +++ b/spring-data-couchbase-2/README.md @@ -0,0 +1,39 @@ +## Spring Data Couchbase Tutorial Project + +### Relevant Articles: +- [Spring Data Couchbase](http://www.baeldung.com/spring-data-couchbase) +- [Entity Validation, Query Consistency, and Optimistic Locking in Spring Data Couchbase](http://www.baeldung.com/entity-validation-locking-and-query-consistency-in-spring-data-couchbase) + +### Overview +This Maven project contains the Java code for Spring Data Couchbase +entities, repositories, and template-based services +as described in the tutorials, as well as a unit/integration test +for each service implementation. + +### Working with the Code +The project was developed and tested using Java 7 nad 8 in the Eclipse-based +Spring Source Toolkit (STS) and therefore should run fine in any +recent version of Eclipse or another IDE of your choice +that supports Java 7 or later. + +### Building the Project +You can also build the project using Maven outside of any IDE: +``` +mvn clean install +``` + +### Running the tests +The following test classes are in src/test/java in the package +org.baeldung.spring.data.couchbase.service: +- PersonServiceTest (abstract) +- PersonRepositoryTest (concrete) +- PersonTemplateServiceTest (concrete) +- StudentServiceTest (abstract) +- StudentRepositoryTest (concrete) +- StudentTemplateServiceTest (concrete) + +The concrete test classes may be run as JUnit tests from your IDE +or using the Maven command line: +``` +mvn test +``` diff --git a/spring-data-couchbase-2/pom.xml b/spring-data-couchbase-2/pom.xml new file mode 100644 index 0000000000..d24ef4aeaa --- /dev/null +++ b/spring-data-couchbase-2/pom.xml @@ -0,0 +1,105 @@ + + 4.0.0 + org.baeldung + spring-data-couchbase-2 + 0.1-SNAPSHOT + spring-data-couchbase-2 + jar + + + + + + org.springframework + spring-context + ${spring-framework.version} + + + org.springframework + spring-context-support + ${spring-framework.version} + + + org.springframework.data + spring-data-couchbase + ${spring-data-couchbase.version} + + + + + org.hibernate + hibernate-validator + ${hibernate-validator.version} + + + + joda-time + joda-time + ${joda-time.version} + + + + + org.slf4j + slf4j-api + ${org.slf4j.version} + compile + + + ch.qos.logback + logback-classic + ${logback.version} + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + + + + + org.springframework + spring-test + ${spring-framework.version} + test + + + junit + junit + ${junit.version} + test + + + + + + + maven-compiler-plugin + 2.3.2 + + 1.7 + 1.7 + + + + + + + 1.7 + UTF-8 + 4.2.4.RELEASE + 2.1.1.RELEASE + 5.2.4.Final + 2.9.2 + 1.1.3 + 1.7.12 + 4.11 + + + + diff --git a/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/MyCouchbaseConfig.java b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/MyCouchbaseConfig.java new file mode 100644 index 0000000000..8e6b971dbf --- /dev/null +++ b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/MyCouchbaseConfig.java @@ -0,0 +1,51 @@ +package org.baeldung.spring.data.couchbase; + +import java.util.Arrays; +import java.util.List; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration; +import org.springframework.data.couchbase.core.mapping.event.ValidatingCouchbaseEventListener; +import org.springframework.data.couchbase.core.query.Consistency; +import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories; +import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; + +@Configuration +@EnableCouchbaseRepositories(basePackages={"org.baeldung.spring.data.couchbase"}) +public class MyCouchbaseConfig extends AbstractCouchbaseConfiguration { + + public static final List NODE_LIST = Arrays.asList("localhost"); + public static final String BUCKET_NAME = "baeldung"; + public static final String BUCKET_PASSWORD = ""; + + @Override + protected List getBootstrapHosts() { + return NODE_LIST; + } + + @Override + protected String getBucketName() { + return BUCKET_NAME; + } + + @Override + protected String getBucketPassword() { + return BUCKET_PASSWORD; + } + + @Override + protected Consistency getDefaultConsistency() { + return Consistency.READ_YOUR_OWN_WRITES; + } + + @Bean + public LocalValidatorFactoryBean localValidatorFactoryBean() { + return new LocalValidatorFactoryBean(); + } + + @Bean + public ValidatingCouchbaseEventListener validatingCouchbaseEventListener() { + return new ValidatingCouchbaseEventListener(localValidatorFactoryBean()); + } +} diff --git a/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/model/Person.java b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/model/Person.java new file mode 100644 index 0000000000..9220e157ed --- /dev/null +++ b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/model/Person.java @@ -0,0 +1,87 @@ +package org.baeldung.spring.data.couchbase.model; + +import javax.validation.constraints.NotNull; + +import org.joda.time.DateTime; +import org.springframework.data.annotation.Id; +import org.springframework.data.couchbase.core.mapping.Document; + +import com.couchbase.client.java.repository.annotation.Field; + +@Document +public class Person { + + @Id + private String id; + @Field + @NotNull + private String firstName; + @Field + @NotNull + private String lastName; + @Field + @NotNull + private DateTime created; + @Field + private DateTime updated; + + public Person(String id, String firstName, String lastName) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + } + + public String getId() { + return id; + } + public void setId(String id) { + this.id = id; + } + public String getFirstName() { + return firstName; + } + public void setFirstName(String firstName) { + this.firstName = firstName; + } + public String getLastName() { + return lastName; + } + public void setLastName(String lastName) { + this.lastName = lastName; + } + public DateTime getCreated() { + return created; + } + public void setCreated(DateTime created) { + this.created = created; + } + public DateTime getUpdated() { + return updated; + } + public void setUpdated(DateTime updated) { + this.updated = updated; + } + + @Override + public int hashCode() { + int hash = 1; + if(id != null) { + hash = hash * 31 + id.hashCode(); + } + if(firstName != null) { + hash = hash * 31 + firstName.hashCode(); + } + if(lastName != null) { + hash = hash * 31 + lastName.hashCode(); + } + return hash; + } + + @Override + public boolean equals(Object obj) { + if((obj == null) || (obj.getClass() != this.getClass())) return false; + if(obj == this) return true; + Person other = (Person) obj; + return this.hashCode() == other.hashCode(); + } +} diff --git a/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/model/Student.java b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/model/Student.java new file mode 100644 index 0000000000..9c266c2c62 --- /dev/null +++ b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/model/Student.java @@ -0,0 +1,113 @@ +package org.baeldung.spring.data.couchbase.model; + +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Past; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; + +import org.joda.time.DateTime; +import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.Version; +import org.springframework.data.couchbase.core.mapping.Document; + +import com.couchbase.client.java.repository.annotation.Field; + +@Document +public class Student { + private static final String NAME_REGEX = "^[a-zA-Z .'-]+$"; + + @Id + private String id; + @Field + @NotNull + @Size(min=1, max=20) + @Pattern(regexp=NAME_REGEX) + private String firstName; + @Field + @NotNull + @Size(min=1, max=20) + @Pattern(regexp=NAME_REGEX) + private String lastName; + @Field + @Past + private DateTime dateOfBirth; + @Field + @NotNull + private DateTime created; + @Field + private DateTime updated; + @Version + private long version; + + public Student() {} + + public Student(String id, String firstName, String lastName, DateTime dateOfBirth) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + this.dateOfBirth = dateOfBirth; + } + + public String getId() { + return id; + } + public void setId(String id) { + this.id = id; + } + public String getFirstName() { + return firstName; + } + public void setFirstName(String firstName) { + this.firstName = firstName; + } + public String getLastName() { + return lastName; + } + public void setLastName(String lastName) { + this.lastName = lastName; + } + public DateTime getDateOfBirth() { + return dateOfBirth; + } + public void setDateOfBirth(DateTime dateOfBirth) { + this.dateOfBirth = dateOfBirth; + } + public DateTime getCreated() { + return created; + } + public void setCreated(DateTime created) { + this.created = created; + } + public DateTime getUpdated() { + return updated; + } + public void setUpdated(DateTime updated) { + this.updated = updated; + } + + @Override + public int hashCode() { + int hash = 1; + if(id != null) { + hash = hash * 31 + id.hashCode(); + } + if(firstName != null) { + hash = hash * 31 + firstName.hashCode(); + } + if(lastName != null) { + hash = hash * 31 + lastName.hashCode(); + } + if(dateOfBirth != null) { + hash = hash * 31 + dateOfBirth.hashCode(); + } + return hash; + } + + @Override + public boolean equals(Object obj) { + if((obj == null) || (obj.getClass() != this.getClass())) return false; + if(obj == this) return true; + Student other = (Student) obj; + return this.hashCode() == other.hashCode(); + } +} \ No newline at end of file diff --git a/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/repos/CustomStudentRepository.java b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/repos/CustomStudentRepository.java new file mode 100644 index 0000000000..9a5bf21492 --- /dev/null +++ b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/repos/CustomStudentRepository.java @@ -0,0 +1,9 @@ +package org.baeldung.spring.data.couchbase.repos; + +import java.util.List; + +import org.baeldung.spring.data.couchbase.model.Student; + +public interface CustomStudentRepository { + List findByFirstNameStartsWith(String s); +} diff --git a/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/repos/CustomStudentRepositoryImpl.java b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/repos/CustomStudentRepositoryImpl.java new file mode 100644 index 0000000000..66b672a4fd --- /dev/null +++ b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/repos/CustomStudentRepositoryImpl.java @@ -0,0 +1,25 @@ +package org.baeldung.spring.data.couchbase.repos; + +import java.util.List; + +import org.baeldung.spring.data.couchbase.model.Student; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.couchbase.core.CouchbaseTemplate; + +import com.couchbase.client.java.view.Stale; +import com.couchbase.client.java.view.ViewQuery; + +public class CustomStudentRepositoryImpl implements CustomStudentRepository { + + private static final String DESIGN_DOC = "student"; + + @Autowired + private CouchbaseTemplate template; + + public List findByFirstNameStartsWith(String s) { + return template.findByView(ViewQuery.from(DESIGN_DOC, "byFirstName") + .startKey(s) + .stale(Stale.FALSE), + Student.class); + } +} diff --git a/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/repos/PersonRepository.java b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/repos/PersonRepository.java new file mode 100644 index 0000000000..14b77759e3 --- /dev/null +++ b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/repos/PersonRepository.java @@ -0,0 +1,11 @@ +package org.baeldung.spring.data.couchbase.repos; + +import java.util.List; + +import org.baeldung.spring.data.couchbase.model.Person; +import org.springframework.data.repository.CrudRepository; + +public interface PersonRepository extends CrudRepository { + List findByFirstName(String firstName); + List findByLastName(String lastName); +} diff --git a/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/repos/StudentRepository.java b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/repos/StudentRepository.java new file mode 100644 index 0000000000..331ddc553d --- /dev/null +++ b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/repos/StudentRepository.java @@ -0,0 +1,11 @@ +package org.baeldung.spring.data.couchbase.repos; + +import java.util.List; + +import org.baeldung.spring.data.couchbase.model.Student; +import org.springframework.data.repository.CrudRepository; + +public interface StudentRepository extends CrudRepository, CustomStudentRepository { + List findByFirstName(String firstName); + List findByLastName(String lastName); +} diff --git a/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/service/PersonRepositoryService.java b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/service/PersonRepositoryService.java new file mode 100644 index 0000000000..90cc36780a --- /dev/null +++ b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/service/PersonRepositoryService.java @@ -0,0 +1,58 @@ +package org.baeldung.spring.data.couchbase.service; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.baeldung.spring.data.couchbase.model.Person; +import org.baeldung.spring.data.couchbase.repos.PersonRepository; +import org.joda.time.DateTime; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +@Qualifier("PersonRepositoryService") +public class PersonRepositoryService implements PersonService { + + private PersonRepository repo; + @Autowired + public void setPersonRepository(PersonRepository repo) { + this.repo = repo; + } + + public Person findOne(String id) { + return repo.findOne(id); + } + + public List findAll() { + List people = new ArrayList(); + Iterator it = repo.findAll().iterator(); + while(it.hasNext()) { + people.add(it.next()); + } + return people; + } + + public List findByFirstName(String firstName) { + return repo.findByFirstName(firstName); + } + + public List findByLastName(String lastName) { + return repo.findByLastName(lastName); + } + + public void create(Person person) { + person.setCreated(DateTime.now()); + repo.save(person); + } + + public void update(Person person) { + person.setUpdated(DateTime.now()); + repo.save(person); + } + + public void delete(Person person) { + repo.delete(person); + } +} diff --git a/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/service/PersonService.java b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/service/PersonService.java new file mode 100644 index 0000000000..a823908b01 --- /dev/null +++ b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/service/PersonService.java @@ -0,0 +1,22 @@ +package org.baeldung.spring.data.couchbase.service; + +import java.util.List; + +import org.baeldung.spring.data.couchbase.model.Person; + +public interface PersonService { + + Person findOne(String id); + + List findAll(); + + List findByFirstName(String firstName); + + List findByLastName(String lastName); + + void create(Person person); + + void update(Person person); + + void delete(Person person); +} diff --git a/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/service/PersonTemplateService.java b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/service/PersonTemplateService.java new file mode 100644 index 0000000000..45e9b90a19 --- /dev/null +++ b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/service/PersonTemplateService.java @@ -0,0 +1,55 @@ +package org.baeldung.spring.data.couchbase.service; + +import java.util.List; + +import org.baeldung.spring.data.couchbase.model.Person; +import org.joda.time.DateTime; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.data.couchbase.core.CouchbaseTemplate; +import org.springframework.stereotype.Service; + +import com.couchbase.client.java.view.ViewQuery; + +@Service +@Qualifier("PersonTemplateService") +public class PersonTemplateService implements PersonService { + + private static final String DESIGN_DOC = "person"; + + private CouchbaseTemplate template; + @Autowired + public void setCouchbaseTemplate(CouchbaseTemplate template) { + this.template = template; + } + + public Person findOne(String id) { + return template.findById(id, Person.class); + } + + public List findAll() { + return template.findByView(ViewQuery.from(DESIGN_DOC, "all"), Person.class); + } + + public List findByFirstName(String firstName) { + return template.findByView(ViewQuery.from(DESIGN_DOC, "byFirstName"), Person.class); + } + + public List findByLastName(String lastName) { + return template.findByView(ViewQuery.from(DESIGN_DOC, "byLastName"), Person.class); + } + + public void create(Person person) { + person.setCreated(DateTime.now()); + template.insert(person); + } + + public void update(Person person) { + person.setUpdated(DateTime.now()); + template.update(person); + } + + public void delete(Person person) { + template.remove(person); + } +} diff --git a/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/service/StudentRepositoryService.java b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/service/StudentRepositoryService.java new file mode 100644 index 0000000000..58304afc1c --- /dev/null +++ b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/service/StudentRepositoryService.java @@ -0,0 +1,58 @@ +package org.baeldung.spring.data.couchbase.service; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.baeldung.spring.data.couchbase.model.Student; +import org.baeldung.spring.data.couchbase.repos.StudentRepository; +import org.joda.time.DateTime; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +@Qualifier("StudentRepositoryService") +public class StudentRepositoryService implements StudentService { + + private StudentRepository repo; + @Autowired + public void setStudentRepository(StudentRepository repo) { + this.repo = repo; + } + + public Student findOne(String id) { + return repo.findOne(id); + } + + public List findAll() { + List people = new ArrayList(); + Iterator it = repo.findAll().iterator(); + while(it.hasNext()) { + people.add(it.next()); + } + return people; + } + + public List findByFirstName(String firstName) { + return repo.findByFirstName(firstName); + } + + public List findByLastName(String lastName) { + return repo.findByLastName(lastName); + } + + public void create(Student student) { + student.setCreated(DateTime.now()); + repo.save(student); + } + + public void update(Student student) { + student.setUpdated(DateTime.now()); + repo.save(student); + } + + public void delete(Student student) { + repo.delete(student); + } +} diff --git a/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/service/StudentService.java b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/service/StudentService.java new file mode 100644 index 0000000000..f483ef0fb6 --- /dev/null +++ b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/service/StudentService.java @@ -0,0 +1,22 @@ +package org.baeldung.spring.data.couchbase.service; + +import java.util.List; + +import org.baeldung.spring.data.couchbase.model.Student; + +public interface StudentService { + + Student findOne(String id); + + List findAll(); + + List findByFirstName(String firstName); + + List findByLastName(String lastName); + + void create(Student student); + + void update(Student student); + + void delete(Student student); +} diff --git a/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/service/StudentTemplateService.java b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/service/StudentTemplateService.java new file mode 100644 index 0000000000..c3808e0015 --- /dev/null +++ b/spring-data-couchbase-2/src/main/java/org/baeldung/spring/data/couchbase/service/StudentTemplateService.java @@ -0,0 +1,55 @@ +package org.baeldung.spring.data.couchbase.service; + +import java.util.List; + +import org.baeldung.spring.data.couchbase.model.Student; +import org.joda.time.DateTime; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.data.couchbase.core.CouchbaseTemplate; +import org.springframework.stereotype.Service; + +import com.couchbase.client.java.view.ViewQuery; + +@Service +@Qualifier("StudentTemplateService") +public class StudentTemplateService implements StudentService { + + private static final String DESIGN_DOC = "student"; + + private CouchbaseTemplate template; + @Autowired + public void setCouchbaseTemplate(CouchbaseTemplate template) { + this.template = template; + } + + public Student findOne(String id) { + return template.findById(id, Student.class); + } + + public List findAll() { + return template.findByView(ViewQuery.from(DESIGN_DOC, "all"), Student.class); + } + + public List findByFirstName(String firstName) { + return template.findByView(ViewQuery.from(DESIGN_DOC, "byFirstName"), Student.class); + } + + public List findByLastName(String lastName) { + return template.findByView(ViewQuery.from(DESIGN_DOC, "byLastName"), Student.class); + } + + public void create(Student student) { + student.setCreated(DateTime.now()); + template.insert(student); + } + + public void update(Student student) { + student.setUpdated(DateTime.now()); + template.update(student); + } + + public void delete(Student student) { + template.remove(student); + } +} diff --git a/spring-data-couchbase-2/src/main/resources/logback.xml b/spring-data-couchbase-2/src/main/resources/logback.xml new file mode 100644 index 0000000000..d9067fd1da --- /dev/null +++ b/spring-data-couchbase-2/src/main/resources/logback.xml @@ -0,0 +1,17 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-data-couchbase-2/src/site/site.xml b/spring-data-couchbase-2/src/site/site.xml new file mode 100644 index 0000000000..dda96feecd --- /dev/null +++ b/spring-data-couchbase-2/src/site/site.xml @@ -0,0 +1,25 @@ + + + + + Spring Sample: ${project.name} + index.html + + + + org.springframework.maven.skins + maven-spring-skin + 1.0.5 + + + + + + + + + + + + + diff --git a/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/IntegrationTest.java b/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/IntegrationTest.java new file mode 100644 index 0000000000..ce2daa92cd --- /dev/null +++ b/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/IntegrationTest.java @@ -0,0 +1,13 @@ +package org.baeldung.spring.data.couchbase; + +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestExecutionListeners; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { MyCouchbaseConfig.class, IntegrationTestConfig.class }) +@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class }) +public abstract class IntegrationTest { +} diff --git a/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/IntegrationTestConfig.java b/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/IntegrationTestConfig.java new file mode 100644 index 0000000000..6f040c34db --- /dev/null +++ b/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/IntegrationTestConfig.java @@ -0,0 +1,9 @@ +package org.baeldung.spring.data.couchbase; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan(basePackages = "org.baeldung.spring.data.couchbase") +public class IntegrationTestConfig { +} diff --git a/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/TestCouchbaseConfig.java b/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/TestCouchbaseConfig.java new file mode 100644 index 0000000000..c298ef0a61 --- /dev/null +++ b/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/TestCouchbaseConfig.java @@ -0,0 +1,17 @@ +package org.baeldung.spring.data.couchbase; + +import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter; +import org.springframework.data.couchbase.core.query.Consistency; + +public class TestCouchbaseConfig extends MyCouchbaseConfig { + + @Override + public String typeKey() { + return MappingCouchbaseConverter.TYPEKEY_SYNCGATEWAY_COMPATIBLE; + } + + @Override + public Consistency getDefaultConsistency() { + return Consistency.READ_YOUR_OWN_WRITES; + } +} diff --git a/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/service/PersonRepositoryServiceTest.java b/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/service/PersonRepositoryServiceTest.java new file mode 100644 index 0000000000..ce5cf7667d --- /dev/null +++ b/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/service/PersonRepositoryServiceTest.java @@ -0,0 +1,13 @@ +package org.baeldung.spring.data.couchbase.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; + +public class PersonRepositoryServiceTest extends PersonServiceTest { + + @Autowired + @Qualifier("PersonRepositoryService") + public void setPersonService(PersonService service) { + this.personService = service; + } +} diff --git a/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/service/PersonServiceTest.java b/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/service/PersonServiceTest.java new file mode 100644 index 0000000000..3fabf7a11e --- /dev/null +++ b/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/service/PersonServiceTest.java @@ -0,0 +1,125 @@ +package org.baeldung.spring.data.couchbase.service; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import org.baeldung.spring.data.couchbase.IntegrationTest; +import org.baeldung.spring.data.couchbase.MyCouchbaseConfig; +import org.baeldung.spring.data.couchbase.model.Person; +import org.joda.time.DateTime; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.couchbase.client.java.Bucket; +import com.couchbase.client.java.Cluster; +import com.couchbase.client.java.CouchbaseCluster; +import com.couchbase.client.java.document.JsonDocument; +import com.couchbase.client.java.document.json.JsonObject; + +public abstract class PersonServiceTest extends IntegrationTest { + + static final String typeField = "_class"; + static final String john = "John"; + static final String smith = "Smith"; + static final String johnSmithId = "person:" + john + ":" + smith; + static final Person johnSmith = new Person(johnSmithId, john, smith); + static final JsonObject jsonJohnSmith = JsonObject.empty().put(typeField, Person.class.getName()).put("firstName", john).put("lastName", smith).put("created", DateTime.now().getMillis()); + + static final String foo = "Foo"; + static final String bar = "Bar"; + static final String foobarId = "person:" + foo + ":" + bar; + static final Person foobar = new Person(foobarId, foo, bar); + static final JsonObject jsonFooBar = JsonObject.empty().put(typeField, Person.class.getName()).put("firstName", foo).put("lastName", bar).put("created", DateTime.now().getMillis()); + + PersonService personService; + + @BeforeClass + public static void setupBeforeClass() { + final Cluster cluster = CouchbaseCluster.create(MyCouchbaseConfig.NODE_LIST); + final Bucket bucket = cluster.openBucket(MyCouchbaseConfig.BUCKET_NAME, MyCouchbaseConfig.BUCKET_PASSWORD); + bucket.upsert(JsonDocument.create(johnSmithId, jsonJohnSmith)); + bucket.upsert(JsonDocument.create(foobarId, jsonFooBar)); + bucket.close(); + cluster.disconnect(); + } + + @Test + public void whenFindingPersonByJohnSmithId_thenReturnsJohnSmith() { + final Person actualPerson = personService.findOne(johnSmithId); + assertNotNull(actualPerson); + assertNotNull(actualPerson.getCreated()); + assertEquals(johnSmith, actualPerson); + } + + @Test + public void whenFindingAllPersons_thenReturnsTwoOrMorePersonsIncludingJohnSmithAndFooBar() { + final List resultList = personService.findAll(); + assertNotNull(resultList); + assertFalse(resultList.isEmpty()); + assertTrue(resultContains(resultList, johnSmith)); + assertTrue(resultContains(resultList, foobar)); + assertTrue(resultList.size() >= 2); + } + + @Test + public void whenFindingByFirstNameJohn_thenReturnsOnlyPersonsNamedJohn() { + final String expectedFirstName = john; + final List resultList = personService.findByFirstName(expectedFirstName); + assertNotNull(resultList); + assertFalse(resultList.isEmpty()); + assertTrue(allResultsContainExpectedFirstName(resultList, expectedFirstName)); + } + + @Test + public void whenFindingByLastNameSmith_thenReturnsOnlyPersonsNamedSmith() { + final String expectedLastName = smith; + final List resultList = personService.findByLastName(expectedLastName); + assertNotNull(resultList); + assertFalse(resultList.isEmpty()); + assertTrue(allResultsContainExpectedLastName(resultList, expectedLastName)); + } + + @Test + public void whenFindingByFirstNameJohn_thenReturnsOnePersonNamedJohn() { + final String expectedFirstName = john; + final List resultList = personService.findByFirstName(expectedFirstName); + assertTrue(resultList.size() == 1); + } + + private boolean resultContains(List resultList, Person person) { + boolean found = false; + for (final Person p : resultList) { + if (p.equals(person)) { + found = true; + break; + } + } + return found; + } + + private boolean allResultsContainExpectedFirstName(List resultList, String firstName) { + boolean found = false; + for (final Person p : resultList) { + if (p.getFirstName().equals(firstName)) { + found = true; + break; + } + } + return found; + } + + private boolean allResultsContainExpectedLastName(List resultList, String lastName) { + boolean found = false; + for (final Person p : resultList) { + if (p.getLastName().equals(lastName)) { + found = true; + break; + } + } + return found; + } +} diff --git a/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/service/PersonTemplateServiceTest.java b/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/service/PersonTemplateServiceTest.java new file mode 100644 index 0000000000..0238fa21fb --- /dev/null +++ b/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/service/PersonTemplateServiceTest.java @@ -0,0 +1,13 @@ +package org.baeldung.spring.data.couchbase.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; + +public class PersonTemplateServiceTest extends PersonServiceTest { + + @Autowired + @Qualifier("PersonTemplateService") + public void setPersonService(PersonService service) { + this.personService = service; + } +} diff --git a/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/service/StudentRepositoryServiceTest.java b/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/service/StudentRepositoryServiceTest.java new file mode 100644 index 0000000000..040453fd73 --- /dev/null +++ b/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/service/StudentRepositoryServiceTest.java @@ -0,0 +1,13 @@ +package org.baeldung.spring.data.couchbase.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; + +public class StudentRepositoryServiceTest extends StudentServiceTest { + + @Autowired + @Qualifier("StudentRepositoryService") + public void setStudentService(StudentService service) { + this.studentService = service; + } +} diff --git a/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/service/StudentServiceTest.java b/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/service/StudentServiceTest.java new file mode 100644 index 0000000000..79948cbedf --- /dev/null +++ b/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/service/StudentServiceTest.java @@ -0,0 +1,166 @@ +package org.baeldung.spring.data.couchbase.service; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import javax.validation.ConstraintViolationException; + +import org.baeldung.spring.data.couchbase.IntegrationTest; +import org.baeldung.spring.data.couchbase.MyCouchbaseConfig; +import org.baeldung.spring.data.couchbase.model.Student; +import org.joda.time.DateTime; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.couchbase.client.java.Bucket; +import com.couchbase.client.java.Cluster; +import com.couchbase.client.java.CouchbaseCluster; +import com.couchbase.client.java.document.JsonDocument; +import com.couchbase.client.java.document.json.JsonObject; + +public abstract class StudentServiceTest extends IntegrationTest { + + static final String typeField = "_class"; + static final String joe = "Joe"; + static final String college = "College"; + static final String joeCollegeId = "student:" + joe + ":" + college; + static final DateTime joeCollegeDob = DateTime.now().minusYears(21); + static final Student joeCollege = new Student(joeCollegeId, joe, college, joeCollegeDob); + static final JsonObject jsonJoeCollege = JsonObject.empty() + .put(typeField, Student.class.getName()) + .put("firstName", joe) + .put("lastName", college) + .put("created", DateTime.now().getMillis()) + .put("version", 1); + + static final String judy = "Judy"; + static final String jetson = "Jetson"; + static final String judyJetsonId = "student:" + judy + ":" + jetson; + static final DateTime judyJetsonDob = DateTime.now().minusYears(19).minusMonths(5).minusDays(3); + static final Student judyJetson = new Student(judyJetsonId, judy, jetson, judyJetsonDob); + static final JsonObject jsonJudyJetson = JsonObject.empty() + .put(typeField, Student.class.getName()) + .put("firstName", judy) + .put("lastName", jetson) + .put("created", DateTime.now().getMillis()) + .put("version", 1); + + StudentService studentService; + + @BeforeClass + public static void setupBeforeClass() { + Cluster cluster = CouchbaseCluster.create(MyCouchbaseConfig.NODE_LIST); + Bucket bucket = cluster.openBucket(MyCouchbaseConfig.BUCKET_NAME, MyCouchbaseConfig.BUCKET_PASSWORD); + bucket.upsert(JsonDocument.create(joeCollegeId, jsonJoeCollege)); + bucket.upsert(JsonDocument.create(judyJetsonId, jsonJudyJetson)); + bucket.close(); + cluster.disconnect(); + } + + @Test + public void whenCreatingStudent_thenDocumentIsPersisted() { + String firstName = "Eric"; + String lastName = "Stratton"; + DateTime dateOfBirth = DateTime.now().minusYears(25); + String id = "student:" + firstName + ":" + lastName; + Student expectedStudent = new Student(id, firstName, lastName, dateOfBirth); + studentService.create(expectedStudent); + Student actualStudent = studentService.findOne(id); + assertNotNull(actualStudent.getCreated()); + assertNotNull(actualStudent); + assertEquals(expectedStudent.getId(), actualStudent.getId()); + } + + @Test(expected=ConstraintViolationException.class) + public void whenCreatingStudentWithInvalidFirstName_thenConstraintViolationException() { + String firstName = "Er+ic"; + String lastName = "Stratton"; + DateTime dateOfBirth = DateTime.now().minusYears(25); + String id = "student:" + firstName + ":" + lastName; + Student student = new Student(id, firstName, lastName, dateOfBirth); + studentService.create(student); + } + + @Test(expected=ConstraintViolationException.class) + public void whenCreatingStudentWithFutureDob_thenConstraintViolationException() { + String firstName = "Jane"; + String lastName = "Doe"; + DateTime dateOfBirth = DateTime.now().plusDays(1); + String id = "student:" + firstName + ":" + lastName; + Student student = new Student(id, firstName, lastName, dateOfBirth); + studentService.create(student); + } + + @Test + public void whenFindingStudentByJohnSmithId_thenReturnsJohnSmith() { + Student actualStudent = studentService.findOne(joeCollegeId); + assertNotNull(actualStudent); + assertNotNull(actualStudent.getCreated()); + assertEquals(joeCollegeId, actualStudent.getId()); + } + + @Test + public void whenFindingAllStudents_thenReturnsTwoOrMoreStudentsIncludingJoeCollegeAndJudyJetson() { + List resultList = studentService.findAll(); + assertNotNull(resultList); + assertFalse(resultList.isEmpty()); + assertTrue(resultContains(resultList, joeCollege)); + assertTrue(resultContains(resultList, judyJetson)); + assertTrue(resultList.size() >= 2); + } + + @Test + public void whenFindingByFirstNameJohn_thenReturnsOnlyStudentsNamedJohn() { + String expectedFirstName = joe; + List resultList = studentService.findByFirstName(expectedFirstName); + assertNotNull(resultList); + assertFalse(resultList.isEmpty()); + assertTrue(allResultsContainExpectedFirstName(resultList, expectedFirstName)); + } + + @Test + public void whenFindingByLastNameSmith_thenReturnsOnlyStudentsNamedSmith() { + String expectedLastName = college; + List resultList = studentService.findByLastName(expectedLastName); + assertNotNull(resultList); + assertFalse(resultList.isEmpty()); + assertTrue(allResultsContainExpectedLastName(resultList, expectedLastName)); + } + + private boolean resultContains(List resultList, Student student) { + boolean found = false; + for(Student p : resultList) { + if(p.getId().equals(student.getId())) { + found = true; + break; + } + } + return found; + } + + private boolean allResultsContainExpectedFirstName(List resultList, String firstName) { + boolean found = false; + for(Student p : resultList) { + if(p.getFirstName().equals(firstName)) { + found = true; + break; + } + } + return found; + } + + private boolean allResultsContainExpectedLastName(List resultList, String lastName) { + boolean found = false; + for(Student p : resultList) { + if(p.getLastName().equals(lastName)) { + found = true; + break; + } + } + return found; + } +} diff --git a/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/service/StudentTemplateServiceTest.java b/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/service/StudentTemplateServiceTest.java new file mode 100644 index 0000000000..dd5be8e059 --- /dev/null +++ b/spring-data-couchbase-2/src/test/java/org/baeldung/spring/data/couchbase/service/StudentTemplateServiceTest.java @@ -0,0 +1,13 @@ +package org.baeldung.spring.data.couchbase.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; + +public class StudentTemplateServiceTest extends StudentServiceTest { + + @Autowired + @Qualifier("StudentTemplateService") + public void setStudentService(StudentService service) { + this.studentService = service; + } +} diff --git a/spring-data-couchbase-2/src/test/resources/logback.xml b/spring-data-couchbase-2/src/test/resources/logback.xml new file mode 100644 index 0000000000..d9067fd1da --- /dev/null +++ b/spring-data-couchbase-2/src/test/resources/logback.xml @@ -0,0 +1,17 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + \ No newline at end of file diff --git a/mockito/.classpath b/spring-data-couchbase-2b/.classpath similarity index 94% rename from mockito/.classpath rename to spring-data-couchbase-2b/.classpath index 8ebf6d9c31..450036fc00 100644 --- a/mockito/.classpath +++ b/spring-data-couchbase-2b/.classpath @@ -1,5 +1,10 @@ + + + + + @@ -12,12 +17,12 @@ - + - + @@ -27,10 +32,5 @@ - - - - - diff --git a/spring-data-couchbase-2b/.project b/spring-data-couchbase-2b/.project new file mode 100644 index 0000000000..1690ad8ce2 --- /dev/null +++ b/spring-data-couchbase-2b/.project @@ -0,0 +1,30 @@ + + + spring-data-couchbase-2 + This project is a simple template for a jar utility using Spring. + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.springframework.ide.eclipse.core.springbuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.springframework.ide.eclipse.core.springnature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/spring-data-couchbase-2b/.springBeans b/spring-data-couchbase-2b/.springBeans new file mode 100644 index 0000000000..0c014a97b6 --- /dev/null +++ b/spring-data-couchbase-2b/.springBeans @@ -0,0 +1,20 @@ + + + 1 + + + + + + + + + + + true + false + + + + + diff --git a/spring-data-couchbase-2b/README.md b/spring-data-couchbase-2b/README.md new file mode 100644 index 0000000000..262962f58a --- /dev/null +++ b/spring-data-couchbase-2b/README.md @@ -0,0 +1,36 @@ +## Spring Data Couchbase Tutorial Project + +### Relevant Articles: +- [Spring Data Couchbase](http://www.baeldung.com/spring-data-couchbase) +- [Entity Validation, Query Consistency, and Optimistic Locking in Spring Data Couchbase](http://www.baeldung.com/entity-validation-locking-and-query-consistency-in-spring-data-couchbase) + +### Overview +This Maven project contains the Java code for Spring Data Couchbase +entities, repositories, and repository-based services +as described in the tutorials, as well as a unit/integration test +for each service implementation. + +### Working with the Code +The project was developed and tested using Java 7 and 8 in the Eclipse-based +Spring Source Toolkit (STS) and therefore should run fine in any +recent version of Eclipse or another IDE of your choice +that supports Java 7 or later. + +### Building the Project +You can also build the project using Maven outside of any IDE: +``` +mvn clean install +``` + +### Running the tests +The following test classes are in src/test/java in the package +org.baeldung.spring.data.couchbase.service: +- CampusRepositoryServiceTest +- PersonRepositoryServiceTest +- StudentRepositoryServiceTest + +These may be run as JUnit tests from your IDE +or using the Maven command line: +``` +mvn test +``` diff --git a/spring-data-couchbase-2b/pom.xml b/spring-data-couchbase-2b/pom.xml new file mode 100644 index 0000000000..7d58f78ce5 --- /dev/null +++ b/spring-data-couchbase-2b/pom.xml @@ -0,0 +1,105 @@ + + 4.0.0 + org.baeldung + spring-data-couchbase-2b + 0.1-SNAPSHOT + spring-data-couchbase-2b + jar + + + + + + org.springframework + spring-context + ${spring-framework.version} + + + org.springframework + spring-context-support + ${spring-framework.version} + + + org.springframework.data + spring-data-couchbase + ${spring-data-couchbase.version} + + + + + org.hibernate + hibernate-validator + ${hibernate-validator.version} + + + + joda-time + joda-time + ${joda-time.version} + + + + + org.slf4j + slf4j-api + ${org.slf4j.version} + compile + + + ch.qos.logback + logback-classic + ${logback.version} + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + + + + + org.springframework + spring-test + ${spring-framework.version} + test + + + junit + junit + ${junit.version} + test + + + + + + + maven-compiler-plugin + 2.3.2 + + 1.7 + 1.7 + + + + + + + 1.7 + UTF-8 + 4.2.4.RELEASE + 2.1.1.RELEASE + 5.2.4.Final + 2.9.2 + 1.1.3 + 1.7.12 + 4.11 + + + + diff --git a/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/MyCouchbaseConfig.java b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/MyCouchbaseConfig.java new file mode 100644 index 0000000000..8eeda08455 --- /dev/null +++ b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/MyCouchbaseConfig.java @@ -0,0 +1,79 @@ +package org.baeldung.spring.data.couchbase; + +import java.util.Arrays; +import java.util.List; + +import org.baeldung.spring.data.couchbase.model.Campus; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration; +import org.springframework.data.couchbase.core.CouchbaseTemplate; +import org.springframework.data.couchbase.core.mapping.event.ValidatingCouchbaseEventListener; +import org.springframework.data.couchbase.core.query.Consistency; +import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories; +import org.springframework.data.couchbase.repository.config.RepositoryOperationsMapping; +import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; + +import com.couchbase.client.java.Bucket; + +@Configuration +@EnableCouchbaseRepositories(basePackages={"org.baeldung.spring.data.couchbase"}) +public class MyCouchbaseConfig extends AbstractCouchbaseConfiguration { + + public static final List NODE_LIST = Arrays.asList("localhost"); + public static final String DEFAULT_BUCKET_NAME = "baeldung"; + public static final String DEFAULT_BUCKET_PASSWORD = ""; + + @Override + protected List getBootstrapHosts() { + return NODE_LIST; + } + + @Override + protected String getBucketName() { + return DEFAULT_BUCKET_NAME; + } + + @Override + protected String getBucketPassword() { + return DEFAULT_BUCKET_PASSWORD; + } + + @Bean + public Bucket campusBucket() throws Exception { + return couchbaseCluster().openBucket("baeldung2", ""); + } + + @Bean + public CouchbaseTemplate campusTemplate() throws Exception { + CouchbaseTemplate template = new CouchbaseTemplate( + couchbaseClusterInfo(), campusBucket(), + mappingCouchbaseConverter(), translationService()); + template.setDefaultConsistency(getDefaultConsistency()); + return template; + } + + @Override + public void configureRepositoryOperationsMapping(RepositoryOperationsMapping baseMapping) { + try { + baseMapping.mapEntity(Campus.class, campusTemplate()); + } catch (Exception e) { + //custom Exception handling + } + } + + @Override + protected Consistency getDefaultConsistency() { + return Consistency.READ_YOUR_OWN_WRITES; + } + + @Bean + public LocalValidatorFactoryBean localValidatorFactoryBean() { + return new LocalValidatorFactoryBean(); + } + + @Bean + public ValidatingCouchbaseEventListener validatingCouchbaseEventListener() { + return new ValidatingCouchbaseEventListener(localValidatorFactoryBean()); + } +} diff --git a/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/model/Campus.java b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/model/Campus.java new file mode 100644 index 0000000000..201b14cf0b --- /dev/null +++ b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/model/Campus.java @@ -0,0 +1,102 @@ +package org.baeldung.spring.data.couchbase.model; + +import javax.validation.constraints.NotNull; + +import org.springframework.data.annotation.Id; +import org.springframework.data.couchbase.core.mapping.Document; +import org.springframework.data.geo.Point; + +import com.couchbase.client.java.repository.annotation.Field; + +@Document +public class Campus { + + @Id + private String id; + @Field + @NotNull + private String name; + @Field + @NotNull + private Point location; + + public String getId() { + return id; + } + public void setId(String id) { + this.id = id; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public Point getLocation() { + return location; + } + public void setLocation(Point location) { + this.location = location; + } + + @Override + public int hashCode() { + int hash = 1; + if(id != null) { + hash = hash * 31 + id.hashCode(); + } + if(name != null) { + hash = hash * 31 + name.hashCode(); + } + if(location != null) { + hash = hash * 31 + location.hashCode(); + } + return hash; + } + + @Override + public boolean equals(Object obj) { + if((obj == null) || (obj.getClass() != this.getClass())) return false; + if(obj == this) return true; + Campus other = (Campus) obj; + return this.hashCode() == other.hashCode(); + } + + @SuppressWarnings("unused") + private Campus() {} + + public Campus(Builder b) { + this.id = b.id; + this.name = b.name; + this.location = b.location; + } + + public static class Builder { + private String id; + private String name; + private Point location; + + public static Builder newInstance() { + return new Builder(); + } + + public Campus build() { + return new Campus(this); + } + + public Builder id(String id) { + this.id = id; + return this; + } + + public Builder name(String name) { + this.name = name; + return this; + } + + public Builder location(Point location) { + this.location = location; + return this; + } + } +} diff --git a/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/model/Person.java b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/model/Person.java new file mode 100644 index 0000000000..9220e157ed --- /dev/null +++ b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/model/Person.java @@ -0,0 +1,87 @@ +package org.baeldung.spring.data.couchbase.model; + +import javax.validation.constraints.NotNull; + +import org.joda.time.DateTime; +import org.springframework.data.annotation.Id; +import org.springframework.data.couchbase.core.mapping.Document; + +import com.couchbase.client.java.repository.annotation.Field; + +@Document +public class Person { + + @Id + private String id; + @Field + @NotNull + private String firstName; + @Field + @NotNull + private String lastName; + @Field + @NotNull + private DateTime created; + @Field + private DateTime updated; + + public Person(String id, String firstName, String lastName) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + } + + public String getId() { + return id; + } + public void setId(String id) { + this.id = id; + } + public String getFirstName() { + return firstName; + } + public void setFirstName(String firstName) { + this.firstName = firstName; + } + public String getLastName() { + return lastName; + } + public void setLastName(String lastName) { + this.lastName = lastName; + } + public DateTime getCreated() { + return created; + } + public void setCreated(DateTime created) { + this.created = created; + } + public DateTime getUpdated() { + return updated; + } + public void setUpdated(DateTime updated) { + this.updated = updated; + } + + @Override + public int hashCode() { + int hash = 1; + if(id != null) { + hash = hash * 31 + id.hashCode(); + } + if(firstName != null) { + hash = hash * 31 + firstName.hashCode(); + } + if(lastName != null) { + hash = hash * 31 + lastName.hashCode(); + } + return hash; + } + + @Override + public boolean equals(Object obj) { + if((obj == null) || (obj.getClass() != this.getClass())) return false; + if(obj == this) return true; + Person other = (Person) obj; + return this.hashCode() == other.hashCode(); + } +} diff --git a/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/model/Student.java b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/model/Student.java new file mode 100644 index 0000000000..9c266c2c62 --- /dev/null +++ b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/model/Student.java @@ -0,0 +1,113 @@ +package org.baeldung.spring.data.couchbase.model; + +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Past; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; + +import org.joda.time.DateTime; +import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.Version; +import org.springframework.data.couchbase.core.mapping.Document; + +import com.couchbase.client.java.repository.annotation.Field; + +@Document +public class Student { + private static final String NAME_REGEX = "^[a-zA-Z .'-]+$"; + + @Id + private String id; + @Field + @NotNull + @Size(min=1, max=20) + @Pattern(regexp=NAME_REGEX) + private String firstName; + @Field + @NotNull + @Size(min=1, max=20) + @Pattern(regexp=NAME_REGEX) + private String lastName; + @Field + @Past + private DateTime dateOfBirth; + @Field + @NotNull + private DateTime created; + @Field + private DateTime updated; + @Version + private long version; + + public Student() {} + + public Student(String id, String firstName, String lastName, DateTime dateOfBirth) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + this.dateOfBirth = dateOfBirth; + } + + public String getId() { + return id; + } + public void setId(String id) { + this.id = id; + } + public String getFirstName() { + return firstName; + } + public void setFirstName(String firstName) { + this.firstName = firstName; + } + public String getLastName() { + return lastName; + } + public void setLastName(String lastName) { + this.lastName = lastName; + } + public DateTime getDateOfBirth() { + return dateOfBirth; + } + public void setDateOfBirth(DateTime dateOfBirth) { + this.dateOfBirth = dateOfBirth; + } + public DateTime getCreated() { + return created; + } + public void setCreated(DateTime created) { + this.created = created; + } + public DateTime getUpdated() { + return updated; + } + public void setUpdated(DateTime updated) { + this.updated = updated; + } + + @Override + public int hashCode() { + int hash = 1; + if(id != null) { + hash = hash * 31 + id.hashCode(); + } + if(firstName != null) { + hash = hash * 31 + firstName.hashCode(); + } + if(lastName != null) { + hash = hash * 31 + lastName.hashCode(); + } + if(dateOfBirth != null) { + hash = hash * 31 + dateOfBirth.hashCode(); + } + return hash; + } + + @Override + public boolean equals(Object obj) { + if((obj == null) || (obj.getClass() != this.getClass())) return false; + if(obj == this) return true; + Student other = (Student) obj; + return this.hashCode() == other.hashCode(); + } +} \ No newline at end of file diff --git a/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/repos/CampusRepository.java b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/repos/CampusRepository.java new file mode 100644 index 0000000000..22b2fb2735 --- /dev/null +++ b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/repos/CampusRepository.java @@ -0,0 +1,19 @@ +package org.baeldung.spring.data.couchbase.repos; + +import java.util.Set; + +import org.baeldung.spring.data.couchbase.model.Campus; +import org.springframework.data.couchbase.core.query.Dimensional; +import org.springframework.data.couchbase.core.query.View; +import org.springframework.data.geo.Distance; +import org.springframework.data.geo.Point; +import org.springframework.data.repository.CrudRepository; + +public interface CampusRepository extends CrudRepository { + + @View(designDocument="campus", viewName="byName") + Set findByName(String name); + + @Dimensional(dimensions=2, designDocument="campus_spatial", spatialViewName="byLocation") + Set findByLocationNear(Point point, Distance distance); +} diff --git a/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/repos/PersonRepository.java b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/repos/PersonRepository.java new file mode 100644 index 0000000000..14b77759e3 --- /dev/null +++ b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/repos/PersonRepository.java @@ -0,0 +1,11 @@ +package org.baeldung.spring.data.couchbase.repos; + +import java.util.List; + +import org.baeldung.spring.data.couchbase.model.Person; +import org.springframework.data.repository.CrudRepository; + +public interface PersonRepository extends CrudRepository { + List findByFirstName(String firstName); + List findByLastName(String lastName); +} diff --git a/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/repos/StudentRepository.java b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/repos/StudentRepository.java new file mode 100644 index 0000000000..433964c872 --- /dev/null +++ b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/repos/StudentRepository.java @@ -0,0 +1,11 @@ +package org.baeldung.spring.data.couchbase.repos; + +import java.util.List; + +import org.baeldung.spring.data.couchbase.model.Student; +import org.springframework.data.repository.CrudRepository; + +public interface StudentRepository extends CrudRepository { + List findByFirstName(String firstName); + List findByLastName(String lastName); +} diff --git a/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/service/CampusRepositoryService.java b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/service/CampusRepositoryService.java new file mode 100644 index 0000000000..d12e59ba1f --- /dev/null +++ b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/service/CampusRepositoryService.java @@ -0,0 +1,54 @@ +package org.baeldung.spring.data.couchbase.service; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import org.baeldung.spring.data.couchbase.model.Campus; +import org.baeldung.spring.data.couchbase.repos.CampusRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.data.geo.Distance; +import org.springframework.data.geo.Point; +import org.springframework.stereotype.Service; + +@Service +@Qualifier("CampusRepositoryService") +public class CampusRepositoryService implements CampusService { + + private CampusRepository repo; + @Autowired + public void setCampusRepository(CampusRepository repo) { + this.repo = repo; + } + + @Override + public Campus find(String id) { + return repo.findOne(id); + } + + @Override + public Set findByName(String name) { + return repo.findByName(name); + } + + @Override + public Set findByLocationNear(Point point, Distance distance) { + return repo.findByLocationNear(point, distance); + } + + @Override + public Set findAll() { + Set campuses = new HashSet<>(); + Iterator it = repo.findAll().iterator(); + while(it.hasNext()) { + campuses.add(it.next()); + } + return campuses; + } + + @Override + public void save(Campus campus) { + repo.save(campus); + } +} diff --git a/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/service/CampusService.java b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/service/CampusService.java new file mode 100644 index 0000000000..e82c14cb87 --- /dev/null +++ b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/service/CampusService.java @@ -0,0 +1,20 @@ +package org.baeldung.spring.data.couchbase.service; + +import java.util.Set; + +import org.baeldung.spring.data.couchbase.model.Campus; +import org.springframework.data.geo.Distance; +import org.springframework.data.geo.Point; + +public interface CampusService { + + Campus find(String id); + + Set findByName(String name); + + Set findByLocationNear(Point point, Distance distance); + + Set findAll(); + + void save(Campus campus); +} diff --git a/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/service/PersonRepositoryService.java b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/service/PersonRepositoryService.java new file mode 100644 index 0000000000..90cc36780a --- /dev/null +++ b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/service/PersonRepositoryService.java @@ -0,0 +1,58 @@ +package org.baeldung.spring.data.couchbase.service; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.baeldung.spring.data.couchbase.model.Person; +import org.baeldung.spring.data.couchbase.repos.PersonRepository; +import org.joda.time.DateTime; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +@Qualifier("PersonRepositoryService") +public class PersonRepositoryService implements PersonService { + + private PersonRepository repo; + @Autowired + public void setPersonRepository(PersonRepository repo) { + this.repo = repo; + } + + public Person findOne(String id) { + return repo.findOne(id); + } + + public List findAll() { + List people = new ArrayList(); + Iterator it = repo.findAll().iterator(); + while(it.hasNext()) { + people.add(it.next()); + } + return people; + } + + public List findByFirstName(String firstName) { + return repo.findByFirstName(firstName); + } + + public List findByLastName(String lastName) { + return repo.findByLastName(lastName); + } + + public void create(Person person) { + person.setCreated(DateTime.now()); + repo.save(person); + } + + public void update(Person person) { + person.setUpdated(DateTime.now()); + repo.save(person); + } + + public void delete(Person person) { + repo.delete(person); + } +} diff --git a/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/service/PersonService.java b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/service/PersonService.java new file mode 100644 index 0000000000..a823908b01 --- /dev/null +++ b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/service/PersonService.java @@ -0,0 +1,22 @@ +package org.baeldung.spring.data.couchbase.service; + +import java.util.List; + +import org.baeldung.spring.data.couchbase.model.Person; + +public interface PersonService { + + Person findOne(String id); + + List findAll(); + + List findByFirstName(String firstName); + + List findByLastName(String lastName); + + void create(Person person); + + void update(Person person); + + void delete(Person person); +} diff --git a/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/service/StudentRepositoryService.java b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/service/StudentRepositoryService.java new file mode 100644 index 0000000000..58304afc1c --- /dev/null +++ b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/service/StudentRepositoryService.java @@ -0,0 +1,58 @@ +package org.baeldung.spring.data.couchbase.service; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.baeldung.spring.data.couchbase.model.Student; +import org.baeldung.spring.data.couchbase.repos.StudentRepository; +import org.joda.time.DateTime; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +@Qualifier("StudentRepositoryService") +public class StudentRepositoryService implements StudentService { + + private StudentRepository repo; + @Autowired + public void setStudentRepository(StudentRepository repo) { + this.repo = repo; + } + + public Student findOne(String id) { + return repo.findOne(id); + } + + public List findAll() { + List people = new ArrayList(); + Iterator it = repo.findAll().iterator(); + while(it.hasNext()) { + people.add(it.next()); + } + return people; + } + + public List findByFirstName(String firstName) { + return repo.findByFirstName(firstName); + } + + public List findByLastName(String lastName) { + return repo.findByLastName(lastName); + } + + public void create(Student student) { + student.setCreated(DateTime.now()); + repo.save(student); + } + + public void update(Student student) { + student.setUpdated(DateTime.now()); + repo.save(student); + } + + public void delete(Student student) { + repo.delete(student); + } +} diff --git a/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/service/StudentService.java b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/service/StudentService.java new file mode 100644 index 0000000000..f483ef0fb6 --- /dev/null +++ b/spring-data-couchbase-2b/src/main/java/org/baeldung/spring/data/couchbase/service/StudentService.java @@ -0,0 +1,22 @@ +package org.baeldung.spring.data.couchbase.service; + +import java.util.List; + +import org.baeldung.spring.data.couchbase.model.Student; + +public interface StudentService { + + Student findOne(String id); + + List findAll(); + + List findByFirstName(String firstName); + + List findByLastName(String lastName); + + void create(Student student); + + void update(Student student); + + void delete(Student student); +} diff --git a/spring-data-couchbase-2b/src/main/resources/logback.xml b/spring-data-couchbase-2b/src/main/resources/logback.xml new file mode 100644 index 0000000000..d9067fd1da --- /dev/null +++ b/spring-data-couchbase-2b/src/main/resources/logback.xml @@ -0,0 +1,17 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-data-couchbase-2b/src/site/site.xml b/spring-data-couchbase-2b/src/site/site.xml new file mode 100644 index 0000000000..dda96feecd --- /dev/null +++ b/spring-data-couchbase-2b/src/site/site.xml @@ -0,0 +1,25 @@ + + + + + Spring Sample: ${project.name} + index.html + + + + org.springframework.maven.skins + maven-spring-skin + 1.0.5 + + + + + + + + + + + + + diff --git a/spring-data-couchbase-2b/src/test/java/org/baeldung/spring/data/couchbase/IntegrationTest.java b/spring-data-couchbase-2b/src/test/java/org/baeldung/spring/data/couchbase/IntegrationTest.java new file mode 100644 index 0000000000..ce2daa92cd --- /dev/null +++ b/spring-data-couchbase-2b/src/test/java/org/baeldung/spring/data/couchbase/IntegrationTest.java @@ -0,0 +1,13 @@ +package org.baeldung.spring.data.couchbase; + +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestExecutionListeners; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { MyCouchbaseConfig.class, IntegrationTestConfig.class }) +@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class }) +public abstract class IntegrationTest { +} diff --git a/spring-data-couchbase-2b/src/test/java/org/baeldung/spring/data/couchbase/IntegrationTestConfig.java b/spring-data-couchbase-2b/src/test/java/org/baeldung/spring/data/couchbase/IntegrationTestConfig.java new file mode 100644 index 0000000000..6f040c34db --- /dev/null +++ b/spring-data-couchbase-2b/src/test/java/org/baeldung/spring/data/couchbase/IntegrationTestConfig.java @@ -0,0 +1,9 @@ +package org.baeldung.spring.data.couchbase; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan(basePackages = "org.baeldung.spring.data.couchbase") +public class IntegrationTestConfig { +} diff --git a/spring-data-couchbase-2b/src/test/java/org/baeldung/spring/data/couchbase/TestCouchbaseConfig.java b/spring-data-couchbase-2b/src/test/java/org/baeldung/spring/data/couchbase/TestCouchbaseConfig.java new file mode 100644 index 0000000000..c298ef0a61 --- /dev/null +++ b/spring-data-couchbase-2b/src/test/java/org/baeldung/spring/data/couchbase/TestCouchbaseConfig.java @@ -0,0 +1,17 @@ +package org.baeldung.spring.data.couchbase; + +import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter; +import org.springframework.data.couchbase.core.query.Consistency; + +public class TestCouchbaseConfig extends MyCouchbaseConfig { + + @Override + public String typeKey() { + return MappingCouchbaseConverter.TYPEKEY_SYNCGATEWAY_COMPATIBLE; + } + + @Override + public Consistency getDefaultConsistency() { + return Consistency.READ_YOUR_OWN_WRITES; + } +} diff --git a/spring-data-couchbase-2b/src/test/java/org/baeldung/spring/data/couchbase/service/CampusRepositoryServiceTest.java b/spring-data-couchbase-2b/src/test/java/org/baeldung/spring/data/couchbase/service/CampusRepositoryServiceTest.java new file mode 100644 index 0000000000..5a718f0807 --- /dev/null +++ b/spring-data-couchbase-2b/src/test/java/org/baeldung/spring/data/couchbase/service/CampusRepositoryServiceTest.java @@ -0,0 +1,135 @@ +package org.baeldung.spring.data.couchbase.service; + +import static org.junit.Assert.*; + +import java.util.Set; + +import javax.annotation.PostConstruct; + +import org.baeldung.spring.data.couchbase.IntegrationTest; +import org.baeldung.spring.data.couchbase.model.Campus; +import org.baeldung.spring.data.couchbase.repos.CampusRepository; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.data.geo.Distance; +import org.springframework.data.geo.Metrics; +import org.springframework.data.geo.Point; + +public class CampusRepositoryServiceTest extends IntegrationTest { + + @Autowired + @Qualifier("CampusRepositoryService") + private CampusRepositoryService campusService; + + @Autowired + private CampusRepository campusRepo; + + private final Campus Brown = Campus.Builder.newInstance() + .id("campus:Brown") + .name("Brown") + .location(new Point(71.4025, 51.8268)) + .build(); + + private final Campus Cornell = Campus.Builder.newInstance() + .id("campus:Cornell") + .name("Cornell") + .location(new Point(76.4833, 42.4459)) + .build(); + + private final Campus Columbia = Campus.Builder.newInstance() + .id("campus:Columbia") + .name("Columbia") + .location(new Point(73.9626, 40.8075)) + .build(); + + private final Campus Dartmouth = Campus.Builder.newInstance() + .id("campus:Dartmouth") + .name("Dartmouth") + .location(new Point(72.2887, 43.7044)) + .build(); + + private final Campus Harvard = Campus.Builder.newInstance() + .id("campus:Harvard") + .name("Harvard") + .location(new Point(71.1167, 42.3770)) + .build(); + + private final Campus Penn = Campus.Builder.newInstance() + .id("campus:Penn") + .name("Penn") + .location(new Point(75.1932, 39.9522)) + .build(); + + private final Campus Princeton = Campus.Builder.newInstance() + .id("campus:Princeton") + .name("Princeton") + .location(new Point(74.6514, 40.3340)) + .build(); + + private final Campus Yale = Campus.Builder.newInstance() + .id("campus:Yale") + .name("Yale") + .location(new Point(72.9223, 41.3163)) + .build(); + + private final Point Boston = new Point(71.0589, 42.3601); + private final Point NewYorkCity = new Point(74.0059, 40.7128); + + @PostConstruct + private void loadCampuses() throws Exception { + campusRepo.save(Brown); + campusRepo.save(Columbia); + campusRepo.save(Cornell); + campusRepo.save(Dartmouth); + campusRepo.save(Harvard); + campusRepo.save(Penn); + campusRepo.save(Princeton); + campusRepo.save(Yale); + } + + @Test + public final void givenNameHarvard_whenFindByName_thenReturnsHarvard() throws Exception { + Set campuses = campusService.findByName(Harvard.getName()); + assertNotNull(campuses); + assertFalse(campuses.isEmpty()); + assertTrue(campuses.size() == 1); + assertTrue(campuses.contains(Harvard)); + } + + @Test + public final void givenHarvardId_whenFind_thenReturnsHarvard() throws Exception { + Campus actual = campusService.find(Harvard.getId()); + assertNotNull(actual); + assertEquals(Harvard, actual); + } + + @Test + public final void whenFindAll_thenReturnsAll() throws Exception { + Set campuses = campusService.findAll(); + assertTrue(campuses.contains(Brown)); + assertTrue(campuses.contains(Columbia)); + assertTrue(campuses.contains(Cornell)); + assertTrue(campuses.contains(Dartmouth)); + assertTrue(campuses.contains(Harvard)); + assertTrue(campuses.contains(Penn)); + assertTrue(campuses.contains(Princeton)); + assertTrue(campuses.contains(Yale)); + } + + @Test + public final void whenFindByLocationNearBoston_thenResultContainsHarvard() throws Exception { + Set campuses = campusService.findByLocationNear(Boston, new Distance(1, Metrics.NEUTRAL)); + assertFalse(campuses.isEmpty()); + assertTrue(campuses.contains(Harvard)); + assertFalse(campuses.contains(Columbia)); + } + + @Test + public final void whenFindByLocationNearNewYorkCity_thenResultContainsColumbia() throws Exception { + Set campuses = campusService.findByLocationNear(NewYorkCity, new Distance(1, Metrics.NEUTRAL)); + assertFalse(campuses.isEmpty()); + assertTrue(campuses.contains(Columbia)); + assertFalse(campuses.contains(Harvard)); + } +} diff --git a/spring-data-couchbase-2b/src/test/java/org/baeldung/spring/data/couchbase/service/PersonRepositoryServiceTest.java b/spring-data-couchbase-2b/src/test/java/org/baeldung/spring/data/couchbase/service/PersonRepositoryServiceTest.java new file mode 100644 index 0000000000..84b6f75d56 --- /dev/null +++ b/spring-data-couchbase-2b/src/test/java/org/baeldung/spring/data/couchbase/service/PersonRepositoryServiceTest.java @@ -0,0 +1,130 @@ +package org.baeldung.spring.data.couchbase.service; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import org.baeldung.spring.data.couchbase.IntegrationTest; +import org.baeldung.spring.data.couchbase.MyCouchbaseConfig; +import org.baeldung.spring.data.couchbase.model.Person; +import org.joda.time.DateTime; +import org.junit.BeforeClass; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; + +import com.couchbase.client.java.Bucket; +import com.couchbase.client.java.Cluster; +import com.couchbase.client.java.CouchbaseCluster; +import com.couchbase.client.java.document.JsonDocument; +import com.couchbase.client.java.document.json.JsonObject; + +public class PersonRepositoryServiceTest extends IntegrationTest { + + private static final String typeField = "_class"; + private static final String john = "John"; + private static final String smith = "Smith"; + private static final String johnSmithId = "person:" + john + ":" + smith; + private static final Person johnSmith = new Person(johnSmithId, john, smith); + private static final JsonObject jsonJohnSmith = JsonObject.empty() + .put(typeField, Person.class.getName()) + .put("firstName", john) + .put("lastName", smith) + .put("created", DateTime.now().getMillis()); + + private static final String foo = "Foo"; + private static final String bar = "Bar"; + private static final String foobarId = "person:" + foo + ":" + bar; + private static final Person foobar = new Person(foobarId, foo, bar); + private static final JsonObject jsonFooBar = JsonObject.empty() + .put(typeField, Person.class.getName()) + .put("firstName", foo) + .put("lastName", bar) + .put("created", DateTime.now().getMillis()); + + @Autowired + @Qualifier("PersonRepositoryService") + private PersonService personService; + + @BeforeClass + public static void setupBeforeClass() { + Cluster cluster = CouchbaseCluster.create(MyCouchbaseConfig.NODE_LIST); + Bucket bucket = cluster.openBucket(MyCouchbaseConfig.DEFAULT_BUCKET_NAME, MyCouchbaseConfig.DEFAULT_BUCKET_PASSWORD); + bucket.upsert(JsonDocument.create(johnSmithId, jsonJohnSmith)); + bucket.upsert(JsonDocument.create(foobarId, jsonFooBar)); + bucket.close(); + cluster.disconnect(); + } + + @Test + public void whenFindingPersonByJohnSmithId_thenReturnsJohnSmith() { + Person actualPerson = personService.findOne(johnSmithId); + assertNotNull(actualPerson); + assertNotNull(actualPerson.getCreated()); + assertEquals(johnSmith, actualPerson); + } + + @Test + public void whenFindingAllPersons_thenReturnsTwoOrMorePersonsIncludingJohnSmithAndFooBar() { + List resultList = personService.findAll(); + assertNotNull(resultList); + assertFalse(resultList.isEmpty()); + assertTrue(resultContains(resultList, johnSmith)); + assertTrue(resultContains(resultList, foobar)); + assertTrue(resultList.size() >= 2); + } + + @Test + public void whenFindingByFirstNameJohn_thenReturnsOnlyPersonsNamedJohn() { + String expectedFirstName = john; + List resultList = personService.findByFirstName(expectedFirstName); + assertNotNull(resultList); + assertFalse(resultList.isEmpty()); + assertTrue(allResultsContainExpectedFirstName(resultList, expectedFirstName)); + } + + @Test + public void whenFindingByLastNameSmith_thenReturnsOnlyPersonsNamedSmith() { + String expectedLastName = smith; + List resultList = personService.findByLastName(expectedLastName); + assertNotNull(resultList); + assertFalse(resultList.isEmpty()); + assertTrue(allResultsContainExpectedLastName(resultList, expectedLastName)); + } + + private boolean resultContains(List resultList, Person person) { + boolean found = false; + for(Person p : resultList) { + if(p.equals(person)) { + found = true; + break; + } + } + return found; + } + + private boolean allResultsContainExpectedFirstName(List resultList, String firstName) { + boolean found = false; + for(Person p : resultList) { + if(p.getFirstName().equals(firstName)) { + found = true; + break; + } + } + return found; + } + + private boolean allResultsContainExpectedLastName(List resultList, String lastName) { + boolean found = false; + for(Person p : resultList) { + if(p.getLastName().equals(lastName)) { + found = true; + break; + } + } + return found; + } +} diff --git a/spring-data-couchbase-2b/src/test/java/org/baeldung/spring/data/couchbase/service/StudentRepositoryServiceTest.java b/spring-data-couchbase-2b/src/test/java/org/baeldung/spring/data/couchbase/service/StudentRepositoryServiceTest.java new file mode 100644 index 0000000000..166f01d754 --- /dev/null +++ b/spring-data-couchbase-2b/src/test/java/org/baeldung/spring/data/couchbase/service/StudentRepositoryServiceTest.java @@ -0,0 +1,170 @@ +package org.baeldung.spring.data.couchbase.service; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import javax.validation.ConstraintViolationException; + +import org.baeldung.spring.data.couchbase.IntegrationTest; +import org.baeldung.spring.data.couchbase.MyCouchbaseConfig; +import org.baeldung.spring.data.couchbase.model.Student; +import org.joda.time.DateTime; +import org.junit.BeforeClass; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; + +import com.couchbase.client.java.Bucket; +import com.couchbase.client.java.Cluster; +import com.couchbase.client.java.CouchbaseCluster; +import com.couchbase.client.java.document.JsonDocument; +import com.couchbase.client.java.document.json.JsonObject; + +public class StudentRepositoryServiceTest extends IntegrationTest { + + private static final String typeField = "_class"; + private static final String joe = "Joe"; + private static final String college = "College"; + private static final String joeCollegeId = "student:" + joe + ":" + college; + private static final DateTime joeCollegeDob = DateTime.now().minusYears(21); + private static final Student joeCollege = new Student(joeCollegeId, joe, college, joeCollegeDob); + private static final JsonObject jsonJoeCollege = JsonObject.empty() + .put(typeField, Student.class.getName()) + .put("firstName", joe) + .put("lastName", college) + .put("created", DateTime.now().getMillis()) + .put("version", 1); + + private static final String judy = "Judy"; + private static final String jetson = "Jetson"; + private static final String judyJetsonId = "student:" + judy + ":" + jetson; + private static final DateTime judyJetsonDob = DateTime.now().minusYears(19).minusMonths(5).minusDays(3); + private static final Student judyJetson = new Student(judyJetsonId, judy, jetson, judyJetsonDob); + private static final JsonObject jsonJudyJetson = JsonObject.empty() + .put(typeField, Student.class.getName()) + .put("firstName", judy) + .put("lastName", jetson) + .put("created", DateTime.now().getMillis()) + .put("version", 1); + + @Autowired + @Qualifier("StudentRepositoryService") + private StudentService studentService; + + @BeforeClass + public static void setupBeforeClass() { + Cluster cluster = CouchbaseCluster.create(MyCouchbaseConfig.NODE_LIST); + Bucket bucket = cluster.openBucket(MyCouchbaseConfig.DEFAULT_BUCKET_NAME, MyCouchbaseConfig.DEFAULT_BUCKET_PASSWORD); + bucket.upsert(JsonDocument.create(joeCollegeId, jsonJoeCollege)); + bucket.upsert(JsonDocument.create(judyJetsonId, jsonJudyJetson)); + bucket.close(); + cluster.disconnect(); + } + + @Test + public void whenCreatingStudent_thenDocumentIsPersisted() { + String firstName = "Eric"; + String lastName = "Stratton"; + DateTime dateOfBirth = DateTime.now().minusYears(25); + String id = "student:" + firstName + ":" + lastName; + Student expectedStudent = new Student(id, firstName, lastName, dateOfBirth); + studentService.create(expectedStudent); + Student actualStudent = studentService.findOne(id); + assertNotNull(actualStudent.getCreated()); + assertNotNull(actualStudent); + assertEquals(expectedStudent.getId(), actualStudent.getId()); + } + + @Test(expected=ConstraintViolationException.class) + public void whenCreatingStudentWithInvalidFirstName_thenConstraintViolationException() { + String firstName = "Er+ic"; + String lastName = "Stratton"; + DateTime dateOfBirth = DateTime.now().minusYears(25); + String id = "student:" + firstName + ":" + lastName; + Student student = new Student(id, firstName, lastName, dateOfBirth); + studentService.create(student); + } + + @Test(expected=ConstraintViolationException.class) + public void whenCreatingStudentWithFutureDob_thenConstraintViolationException() { + String firstName = "Jane"; + String lastName = "Doe"; + DateTime dateOfBirth = DateTime.now().plusDays(1); + String id = "student:" + firstName + ":" + lastName; + Student student = new Student(id, firstName, lastName, dateOfBirth); + studentService.create(student); + } + + @Test + public void whenFindingStudentByJohnSmithId_thenReturnsJohnSmith() { + Student actualStudent = studentService.findOne(joeCollegeId); + assertNotNull(actualStudent); + assertNotNull(actualStudent.getCreated()); + assertEquals(joeCollegeId, actualStudent.getId()); + } + + @Test + public void whenFindingAllStudents_thenReturnsTwoOrMoreStudentsIncludingJoeCollegeAndJudyJetson() { + List resultList = studentService.findAll(); + assertNotNull(resultList); + assertFalse(resultList.isEmpty()); + assertTrue(resultContains(resultList, joeCollege)); + assertTrue(resultContains(resultList, judyJetson)); + assertTrue(resultList.size() >= 2); + } + + @Test + public void whenFindingByFirstNameJohn_thenReturnsOnlyStudentsNamedJohn() { + String expectedFirstName = joe; + List resultList = studentService.findByFirstName(expectedFirstName); + assertNotNull(resultList); + assertFalse(resultList.isEmpty()); + assertTrue(allResultsContainExpectedFirstName(resultList, expectedFirstName)); + } + + @Test + public void whenFindingByLastNameSmith_thenReturnsOnlyStudentsNamedSmith() { + String expectedLastName = college; + List resultList = studentService.findByLastName(expectedLastName); + assertNotNull(resultList); + assertFalse(resultList.isEmpty()); + assertTrue(allResultsContainExpectedLastName(resultList, expectedLastName)); + } + + private boolean resultContains(List resultList, Student student) { + boolean found = false; + for(Student p : resultList) { + if(p.getId().equals(student.getId())) { + found = true; + break; + } + } + return found; + } + + private boolean allResultsContainExpectedFirstName(List resultList, String firstName) { + boolean found = false; + for(Student p : resultList) { + if(p.getFirstName().equals(firstName)) { + found = true; + break; + } + } + return found; + } + + private boolean allResultsContainExpectedLastName(List resultList, String lastName) { + boolean found = false; + for(Student p : resultList) { + if(p.getLastName().equals(lastName)) { + found = true; + break; + } + } + return found; + } +} diff --git a/spring-data-couchbase-2b/src/test/resources/logback.xml b/spring-data-couchbase-2b/src/test/resources/logback.xml new file mode 100644 index 0000000000..d9067fd1da --- /dev/null +++ b/spring-data-couchbase-2b/src/test/resources/logback.xml @@ -0,0 +1,17 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-data-elasticsearch/.classpath b/spring-data-elasticsearch/.classpath new file mode 100644 index 0000000000..6d7587a819 --- /dev/null +++ b/spring-data-elasticsearch/.classpath @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-data-elasticsearch/.project b/spring-data-elasticsearch/.project new file mode 100644 index 0000000000..09b9a781ed --- /dev/null +++ b/spring-data-elasticsearch/.project @@ -0,0 +1,29 @@ + + + spring-data-elasticsearch + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + org.springframework.ide.eclipse.core.springbuilder + + + + + + org.springframework.ide.eclipse.core.springnature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/spring-data-elasticsearch/README.md b/spring-data-elasticsearch/README.md new file mode 100644 index 0000000000..74d9e4f642 --- /dev/null +++ b/spring-data-elasticsearch/README.md @@ -0,0 +1,13 @@ +## Spring Data Elasticsearch +- [Introduction to Spring Data Elasticsearch](http://www.baeldung.com/spring-data-elasticsearch-tutorial) + +### Build the Project with Tests Running +``` +mvn clean install +``` + +### Run Tests Directly +``` +mvn test +``` + diff --git a/spring-data-elasticsearch/pom.xml b/spring-data-elasticsearch/pom.xml new file mode 100644 index 0000000000..3a6e330564 --- /dev/null +++ b/spring-data-elasticsearch/pom.xml @@ -0,0 +1,85 @@ + + 4.0.0 + + com.baeldung + spring-data-elasticsearch + 0.0.1-SNAPSHOT + jar + + spring-data-elasticsearch + + + UTF-8 + + 1.3.2.RELEASE + 4.2.5.RELEASE + + 4.11 + 1.7.12 + 1.1.3 + 2.0.1.RELEASE + + + + + org.springframework + spring-core + ${org.springframework.version} + + + junit + junit-dep + ${junit.version} + test + + + org.springframework + spring-test + ${org.springframework.version} + test + + + org.springframework.data + spring-data-elasticsearch + ${elasticsearch.version} + + net.java.dev.jna + jna + 4.1.0 + test + + + 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} + + + + + + maven-compiler-plugin + 2.3.2 + + 1.8 + 1.8 + + + + + diff --git a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java new file mode 100644 index 0000000000..f93999a1cc --- /dev/null +++ b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java @@ -0,0 +1,57 @@ +package com.baeldung.spring.data.es.config; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.elasticsearch.client.Client; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.node.NodeBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.elasticsearch.core.ElasticsearchOperations; +import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; +import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; + +@Configuration +@EnableElasticsearchRepositories(basePackages = "com.baeldung.spring.data.es.repository") +@ComponentScan(basePackages = { "com.baeldung.spring.data.es.service" }) +public class Config { + + @Value("${elasticsearch.home:/usr/local/Cellar/elasticsearch/2.3.2}") + private String elasticsearchHome; + + private static Logger logger = LoggerFactory.getLogger(Config.class); + + @Bean + public Client client() { + try { + final Path tmpDir = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")), "elasticsearch_data"); + + // @formatter:off + + final Settings.Builder elasticsearchSettings = + Settings.settingsBuilder().put("http.enabled", "false") + .put("path.data", tmpDir.toAbsolutePath().toString()) + .put("path.home", elasticsearchHome); + // @formatter:on + + logger.debug(tmpDir.toAbsolutePath().toString()); + + return new NodeBuilder().local(true).settings(elasticsearchSettings.build()).node().client(); + } catch (final IOException ioex) { + logger.error("Cannot create temp dir", ioex); + throw new RuntimeException(); + } + } + + @Bean + public ElasticsearchOperations elasticsearchTemplate() { + return new ElasticsearchTemplate(client()); + } +} diff --git a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/model/Article.java b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/model/Article.java new file mode 100644 index 0000000000..01330a6c9c --- /dev/null +++ b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/model/Article.java @@ -0,0 +1,74 @@ +package com.baeldung.spring.data.es.model; + +import static org.springframework.data.elasticsearch.annotations.FieldIndex.not_analyzed; +import static org.springframework.data.elasticsearch.annotations.FieldType.Nested; +import static org.springframework.data.elasticsearch.annotations.FieldType.String; + +import java.util.Arrays; +import java.util.List; + +import org.springframework.data.annotation.Id; +import org.springframework.data.elasticsearch.annotations.Document; +import org.springframework.data.elasticsearch.annotations.Field; +import org.springframework.data.elasticsearch.annotations.InnerField; +import org.springframework.data.elasticsearch.annotations.MultiField; + +@Document(indexName = "blog", type = "article") +public class Article { + + @Id + private String id; + + @MultiField(mainField = @Field(type = String), otherFields = { @InnerField(index = not_analyzed, suffix = "verbatim", type = String) }) + private String title; + + @Field(type = Nested) + private List authors; + + @Field(type = String, index = not_analyzed) + private String[] tags; + + public Article() { + } + + public Article(String title) { + this.title = title; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public List getAuthors() { + return authors; + } + + public void setAuthors(List authors) { + this.authors = authors; + } + + public String[] getTags() { + return tags; + } + + public void setTags(String... tags) { + this.tags = tags; + } + + @Override + public String toString() { + return "Article{" + "id='" + id + '\'' + ", title='" + title + '\'' + ", authors=" + authors + ", tags=" + Arrays.toString(tags) + '}'; + } +} diff --git a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/model/Author.java b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/model/Author.java new file mode 100644 index 0000000000..38f50e1614 --- /dev/null +++ b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/model/Author.java @@ -0,0 +1,26 @@ +package com.baeldung.spring.data.es.model; + +public class Author { + + private String name; + + public Author() { + } + + public Author(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "Author{" + "name='" + name + '\'' + '}'; + } +} diff --git a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/repository/ArticleRepository.java b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/repository/ArticleRepository.java new file mode 100644 index 0000000000..8aef865401 --- /dev/null +++ b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/repository/ArticleRepository.java @@ -0,0 +1,17 @@ +package com.baeldung.spring.data.es.repository; + +import com.baeldung.spring.data.es.model.Article; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.elasticsearch.annotations.Query; +import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ArticleRepository extends ElasticsearchRepository { + + Page
findByAuthorsName(String name, Pageable pageable); + + @Query("{\"bool\": {\"must\": [{\"match\": {\"authors.name\": \"?0\"}}]}}") + Page
findByAuthorsNameUsingCustomQuery(String name, Pageable pageable); +} diff --git a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleService.java b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleService.java new file mode 100644 index 0000000000..b5a8fde633 --- /dev/null +++ b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleService.java @@ -0,0 +1,21 @@ +package com.baeldung.spring.data.es.service; + +import com.baeldung.spring.data.es.model.Article; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; + +public interface ArticleService { + Article save(Article article); + + Article findOne(String id); + + Iterable
findAll(); + + Page
findByAuthorName(String name, Pageable pageable); + + Page
findByAuthorNameUsingCustomQuery(String name, Pageable pageable); + + long count(); + + void delete(Article article); +} diff --git a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleServiceImpl.java b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleServiceImpl.java new file mode 100644 index 0000000000..0ea922bdd3 --- /dev/null +++ b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleServiceImpl.java @@ -0,0 +1,54 @@ +package com.baeldung.spring.data.es.service; + +import com.baeldung.spring.data.es.repository.ArticleRepository; +import com.baeldung.spring.data.es.model.Article; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +@Service +public class ArticleServiceImpl implements ArticleService { + + private ArticleRepository articleRepository; + + @Autowired + public void setArticleRepository(ArticleRepository articleRepository) { + this.articleRepository = articleRepository; + } + + @Override + public Article save(Article article) { + return articleRepository.save(article); + } + + @Override + public Article findOne(String id) { + return articleRepository.findOne(id); + } + + @Override + public Iterable
findAll() { + return articleRepository.findAll(); + } + + @Override + public Page
findByAuthorName(String name, Pageable pageable) { + return articleRepository.findByAuthorsName(name, pageable); + } + + @Override + public Page
findByAuthorNameUsingCustomQuery(String name, Pageable pageable) { + return articleRepository.findByAuthorsNameUsingCustomQuery(name, pageable); + } + + @Override + public long count() { + return articleRepository.count(); + } + + @Override + public void delete(Article article) { + articleRepository.delete(article); + } +} diff --git a/spring-data-elasticsearch/src/main/resources/logback.xml b/spring-data-elasticsearch/src/main/resources/logback.xml new file mode 100644 index 0000000000..37a9e2edbf --- /dev/null +++ b/spring-data-elasticsearch/src/main/resources/logback.xml @@ -0,0 +1,20 @@ + + + + + elasticsearch - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryTest.java b/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryTest.java new file mode 100644 index 0000000000..1551d6442e --- /dev/null +++ b/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryTest.java @@ -0,0 +1,178 @@ +package com.baeldung.spring.data.es; + +import static java.util.Arrays.asList; +import static java.util.stream.Collectors.toList; +import static org.elasticsearch.index.query.MatchQueryBuilder.Operator.AND; +import static org.elasticsearch.index.query.QueryBuilders.boolQuery; +import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery; +import static org.elasticsearch.index.query.QueryBuilders.matchQuery; +import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery; +import static org.elasticsearch.index.query.QueryBuilders.nestedQuery; +import static org.elasticsearch.index.query.QueryBuilders.termQuery; +import static org.junit.Assert.assertEquals; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.client.Client; +import org.elasticsearch.common.unit.Fuzziness; +import org.elasticsearch.index.query.MultiMatchQueryBuilder; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.search.aggregations.Aggregation; +import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.aggregations.bucket.terms.StringTerms; +import org.elasticsearch.search.aggregations.bucket.terms.Terms; +import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; +import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; +import org.springframework.data.elasticsearch.core.query.SearchQuery; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.spring.data.es.config.Config; +import com.baeldung.spring.data.es.model.Article; +import com.baeldung.spring.data.es.model.Author; +import com.baeldung.spring.data.es.service.ArticleService; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { Config.class }, loader = AnnotationConfigContextLoader.class) +public class ElasticSearchQueryTest { + + @Autowired + private ElasticsearchTemplate elasticsearchTemplate; + + @Autowired + private ArticleService articleService; + + @Autowired + private Client client; + + private final Author johnSmith = new Author("John Smith"); + private final Author johnDoe = new Author("John Doe"); + + @Before + public void before() { + elasticsearchTemplate.deleteIndex(Article.class); + elasticsearchTemplate.createIndex(Article.class); + elasticsearchTemplate.putMapping(Article.class); + elasticsearchTemplate.refresh(Article.class); + + Article article = new Article("Spring Data Elasticsearch"); + article.setAuthors(asList(johnSmith, johnDoe)); + article.setTags("elasticsearch", "spring data"); + articleService.save(article); + + article = new Article("Search engines"); + article.setAuthors(asList(johnDoe)); + article.setTags("search engines", "tutorial"); + articleService.save(article); + + article = new Article("Second Article About Elasticsearch"); + article.setAuthors(asList(johnSmith)); + article.setTags("elasticsearch", "spring data"); + articleService.save(article); + + article = new Article("Elasticsearch Tutorial"); + article.setAuthors(asList(johnDoe)); + article.setTags("elasticsearch"); + articleService.save(article); + } + + @Test + public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() { + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(AND)).build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(1, articles.size()); + } + + @Test + public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() { + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions")).build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(1, articles.size()); + assertEquals("Search engines", articles.get(0).getTitle()); + } + + @Test + public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() { + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data")).build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(3, articles.size()); + } + + @Test + public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() { + SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")).build(); + List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(1, articles.size()); + + searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About")).build(); + articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(0, articles.size()); + } + + @Test + public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() { + final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith"))); + + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + + assertEquals(2, articles.size()); + } + + @Test + public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() { + final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("title"); + final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation).execute().actionGet(); + + final Map results = response.getAggregations().asMap(); + final StringTerms topTags = (StringTerms) results.get("top_tags"); + + final List keys = topTags.getBuckets().stream().map(b -> b.getKeyAsString()).collect(toList()); + Collections.sort(keys); + assertEquals(asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys); + } + + @Test + public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() { + final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags").order(Terms.Order.aggregation("_count", false)); + final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation).execute().actionGet(); + + final Map results = response.getAggregations().asMap(); + final StringTerms topTags = (StringTerms) results.get("top_tags"); + + final List keys = topTags.getBuckets().stream().map(b -> b.getKeyAsString()).collect(toList()); + assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys); + } + + @Test + public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() { + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(1, articles.size()); + } + + @Test + public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() { + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "spring date elasticserch").operator(AND).fuzziness(Fuzziness.ONE).prefixLength(3)).build(); + + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(1, articles.size()); + } + + @Test + public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() { + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(multiMatchQuery("tutorial").field("title").field("tags").type(MultiMatchQueryBuilder.Type.BEST_FIELDS)).build(); + + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(2, articles.size()); + } +} diff --git a/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchTest.java b/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchTest.java new file mode 100644 index 0000000000..e10b5f48d7 --- /dev/null +++ b/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchTest.java @@ -0,0 +1,132 @@ +package com.baeldung.spring.data.es; + +import static java.util.Arrays.asList; +import static org.elasticsearch.index.query.MatchQueryBuilder.Operator.AND; +import static org.elasticsearch.index.query.QueryBuilders.fuzzyQuery; +import static org.elasticsearch.index.query.QueryBuilders.matchQuery; +import static org.elasticsearch.index.query.QueryBuilders.regexpQuery; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; +import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; +import org.springframework.data.elasticsearch.core.query.SearchQuery; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.spring.data.es.config.Config; +import com.baeldung.spring.data.es.model.Article; +import com.baeldung.spring.data.es.model.Author; +import com.baeldung.spring.data.es.service.ArticleService; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { Config.class }, loader = AnnotationConfigContextLoader.class) +public class ElasticSearchTest { + + @Autowired + private ElasticsearchTemplate elasticsearchTemplate; + + @Autowired + private ArticleService articleService; + + private final Author johnSmith = new Author("John Smith"); + private final Author johnDoe = new Author("John Doe"); + + @Before + public void before() { + elasticsearchTemplate.deleteIndex(Article.class); + elasticsearchTemplate.createIndex(Article.class); + + Article article = new Article("Spring Data Elasticsearch"); + article.setAuthors(asList(johnSmith, johnDoe)); + articleService.save(article); + + article = new Article("Search engines"); + article.setAuthors(asList(johnDoe)); + articleService.save(article); + + article = new Article("Second Article About Elasticsearch"); + article.setAuthors(asList(johnSmith)); + articleService.save(article); + } + + @Test + public void givenArticleService_whenSaveArticle_thenIdIsAssigned() { + final List authors = asList(new Author("John Smith"), johnDoe); + + Article article = new Article("Making Search Elastic"); + article.setAuthors(authors); + + article = articleService.save(article); + assertNotNull(article.getId()); + } + + @Test + public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() { + + final Page
articleByAuthorName = articleService.findByAuthorName(johnSmith.getName(), new PageRequest(0, 10)); + assertEquals(2L, articleByAuthorName.getTotalElements()); + } + + @Test + public void givenCustomQuery_whenSearchByAuthorsName_thenArticleIsFound() { + + final Page
articleByAuthorName = articleService.findByAuthorNameUsingCustomQuery("John Smith", new PageRequest(0, 10)); + assertEquals(3L, articleByAuthorName.getTotalElements()); + } + + @Test + public void givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound() { + + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*")).build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + + assertEquals(1, articles.size()); + } + + @Test + public void givenSavedDoc_whenTitleUpdated_thenCouldFindByUpdatedTitle() { + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(fuzzyQuery("title", "serch")).build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + + assertEquals(1, articles.size()); + + final Article article = articles.get(0); + final String newTitle = "Getting started with Search Engines"; + article.setTitle(newTitle); + articleService.save(article); + + assertEquals(newTitle, articleService.findOne(article.getId()).getTitle()); + } + + @Test + public void givenSavedDoc_whenDelete_thenRemovedFromIndex() { + + final String articleTitle = "Spring Data Elasticsearch"; + + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(1, articles.size()); + final long count = articleService.count(); + + articleService.delete(articles.get(0)); + + assertEquals(count - 1, articleService.count()); + } + + @Test + public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() { + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(AND)).build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(1, articles.size()); + } +} diff --git a/spring-data-mongodb/.classpath b/spring-data-mongodb/.classpath new file mode 100644 index 0000000000..baf7c98131 --- /dev/null +++ b/spring-data-mongodb/.classpath @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-data-mongodb/.project b/spring-data-mongodb/.project new file mode 100644 index 0000000000..2e5a0d4bac --- /dev/null +++ b/spring-data-mongodb/.project @@ -0,0 +1,29 @@ + + + spring-data-mongodb + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.springframework.ide.eclipse.core.springbuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.springframework.ide.eclipse.core.springnature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/spring-data-mongodb/README.md b/spring-data-mongodb/README.md new file mode 100644 index 0000000000..d656bc897c --- /dev/null +++ b/spring-data-mongodb/README.md @@ -0,0 +1,11 @@ +========= + +## Spring Data MongoDB + + +### Relevant Articles: +- [A Guide to Queries in Spring Data MongoDB](http://www.baeldung.com/queries-in-spring-data-mongodb) +- [Spring Data MongoDB – Indexes, Annotations and Converters](http://www.baeldung.com/spring-data-mongodb-index-annotations-converter) +- [Custom Cascading in Spring Data MongoDB](http://www.baeldung.com/cascading-with-dbref-and-lifecycle-events-in-spring-data-mongodb) +- [GridFS in Spring Data MongoDB](http://www.baeldung.com/spring-data-mongodb-gridfs) +- [Introduction to Spring Data MongoDB](http://www.baeldung.com/spring-data-mongodb-tutorial) diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index 23ac353350..102344a3fa 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -1,7 +1,7 @@ 4.0.0 - org.baeldung + com.baeldung spring-data-mongodb 0.0.1-SNAPSHOT @@ -117,12 +117,14 @@ UTF-8 + + 4.2.5.RELEASE 1.7.1.RELEASE - 4.2.2.RELEASE + 1.3 4.11 - 2.4.1 + 2.9.0 3.6.6 1.1.3 diff --git a/spring-data-mongodb/src/main/java/org/baeldung/converter/UserWriterConverter.java b/spring-data-mongodb/src/main/java/org/baeldung/converter/UserWriterConverter.java index 54fa78e2d0..2dedda46ec 100644 --- a/spring-data-mongodb/src/main/java/org/baeldung/converter/UserWriterConverter.java +++ b/spring-data-mongodb/src/main/java/org/baeldung/converter/UserWriterConverter.java @@ -9,6 +9,7 @@ import com.mongodb.DBObject; @Component public class UserWriterConverter implements Converter { + @Override public DBObject convert(final User user) { final DBObject dbObject = new BasicDBObject(); @@ -22,4 +23,5 @@ public class UserWriterConverter implements Converter { dbObject.removeField("_class"); return dbObject; } + } diff --git a/spring-data-redis/.classpath b/spring-data-redis/.classpath new file mode 100644 index 0000000000..9ae7bca0fc --- /dev/null +++ b/spring-data-redis/.classpath @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-data-redis/.project b/spring-data-redis/.project new file mode 100644 index 0000000000..244dfe15fb --- /dev/null +++ b/spring-data-redis/.project @@ -0,0 +1,29 @@ + + + spring-data-redis + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + org.springframework.ide.eclipse.core.springbuilder + + + + + + org.springframework.ide.eclipse.core.springnature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/spring-data-redis/README.md b/spring-data-redis/README.md new file mode 100644 index 0000000000..89eae99f05 --- /dev/null +++ b/spring-data-redis/README.md @@ -0,0 +1,15 @@ +## Spring Data Redis + +### Relevant Articles: +- [Introduction to Spring Data Redis](http://www.baeldung.com/spring-data-redis-tutorial) + +### Build the Project with Tests Running +``` +mvn clean install +``` + +### Run Tests Directly +``` +mvn test +``` + diff --git a/spring-data-redis/pom.xml b/spring-data-redis/pom.xml new file mode 100644 index 0000000000..25686fca16 --- /dev/null +++ b/spring-data-redis/pom.xml @@ -0,0 +1,76 @@ + + + 4.0.0 + com.baeldung + spring-data-redis + 1.0 + jar + + + UTF-8 + 4.2.5.RELEASE + 1.6.2.RELEASE + 0.8.0 + + + + + org.springframework.data + spring-data-redis + ${spring-data-redis} + + + + cglib + cglib-nodep + 2.2 + + + + log4j + log4j + 1.2.16 + + + + redis.clients + jedis + 2.5.1 + jar + + + + org.springframework + spring-core + ${spring.version} + + + + org.springframework + spring-context + ${spring.version} + + + + junit + junit + 4.12 + test + + + + org.springframework + spring-test + ${spring.version} + test + + + + com.lordofthejars + nosqlunit-redis + ${nosqlunit.version} + + + + diff --git a/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java b/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java new file mode 100644 index 0000000000..4fd83a2bb6 --- /dev/null +++ b/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java @@ -0,0 +1,55 @@ +package com.baeldung.spring.data.redis.config; + +import com.baeldung.spring.data.redis.queue.MessagePublisher; +import com.baeldung.spring.data.redis.queue.RedisMessagePublisher; +import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.listener.ChannelTopic; +import org.springframework.data.redis.listener.RedisMessageListenerContainer; +import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; +import org.springframework.data.redis.serializer.GenericToStringSerializer; + +@Configuration +@ComponentScan("com.baeldung.spring.data.redis") +public class RedisConfig { + + @Bean + JedisConnectionFactory jedisConnectionFactory() { + return new JedisConnectionFactory(); + } + + @Bean + public RedisTemplate redisTemplate() { + final RedisTemplate template = new RedisTemplate(); + template.setConnectionFactory(jedisConnectionFactory()); + template.setValueSerializer(new GenericToStringSerializer(Object.class)); + return template; + } + + @Bean + MessageListenerAdapter messageListener() { + return new MessageListenerAdapter(new RedisMessageSubscriber()); + } + + @Bean + RedisMessageListenerContainer redisContainer() { + final RedisMessageListenerContainer container = new RedisMessageListenerContainer(); + container.setConnectionFactory(jedisConnectionFactory()); + container.addMessageListener(messageListener(), topic()); + return container; + } + + @Bean + MessagePublisher redisPublisher() { + return new RedisMessagePublisher(redisTemplate(), topic()); + } + + @Bean + ChannelTopic topic() { + return new ChannelTopic("pubsub:queue"); + } +} diff --git a/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/model/Student.java b/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/model/Student.java new file mode 100644 index 0000000000..10ba0f5ef4 --- /dev/null +++ b/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/model/Student.java @@ -0,0 +1,59 @@ +package com.baeldung.spring.data.redis.model; + +import java.io.Serializable; + +public class Student implements Serializable { + + public enum Gender { + MALE, FEMALE + } + + private String id; + private String name; + private Gender gender; + private int grade; + + public Student(String id, String name, Gender gender, int grade) { + this.id = id; + this.name = name; + this.gender = gender; + this.grade = grade; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Gender getGender() { + return gender; + } + + public void setGender(Gender gender) { + this.gender = gender; + } + + public int getGrade() { + return grade; + } + + public void setGrade(int grade) { + this.grade = grade; + } + + @Override + public String toString() { + return "Student{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", gender=" + gender + ", grade=" + grade + '}'; + } +} \ No newline at end of file diff --git a/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/queue/MessagePublisher.java b/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/queue/MessagePublisher.java new file mode 100644 index 0000000000..9a42545d6c --- /dev/null +++ b/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/queue/MessagePublisher.java @@ -0,0 +1,7 @@ +package com.baeldung.spring.data.redis.queue; + + +public interface MessagePublisher { + + void publish(final String message); +} diff --git a/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/queue/RedisMessagePublisher.java b/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/queue/RedisMessagePublisher.java new file mode 100644 index 0000000000..f4b3180a37 --- /dev/null +++ b/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/queue/RedisMessagePublisher.java @@ -0,0 +1,28 @@ +package com.baeldung.spring.data.redis.queue; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.listener.ChannelTopic; +import org.springframework.stereotype.Service; + +@Service +public class RedisMessagePublisher implements MessagePublisher { + + @Autowired + private RedisTemplate redisTemplate; + @Autowired + private ChannelTopic topic; + + public RedisMessagePublisher() { + } + + public RedisMessagePublisher(final RedisTemplate redisTemplate, + final ChannelTopic topic) { + this.redisTemplate = redisTemplate; + this.topic = topic; + } + + public void publish(final String message) { + redisTemplate.convertAndSend(topic.getTopic(), message); + } +} diff --git a/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/queue/RedisMessageSubscriber.java b/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/queue/RedisMessageSubscriber.java new file mode 100644 index 0000000000..849e1fb59f --- /dev/null +++ b/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/queue/RedisMessageSubscriber.java @@ -0,0 +1,19 @@ +package com.baeldung.spring.data.redis.queue; + +import org.springframework.data.redis.connection.Message; +import org.springframework.data.redis.connection.MessageListener; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +public class RedisMessageSubscriber implements MessageListener { + + public static List messageList = new ArrayList(); + + public void onMessage(final Message message, final byte[] pattern) { + messageList.add(message.toString()); + System.out.println("Message received: " + new String(message.getBody())); + } +} \ No newline at end of file diff --git a/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepository.java b/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepository.java new file mode 100644 index 0000000000..250c227f00 --- /dev/null +++ b/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepository.java @@ -0,0 +1,18 @@ +package com.baeldung.spring.data.redis.repo; + +import com.baeldung.spring.data.redis.model.Student; + +import java.util.Map; + +public interface StudentRepository { + + void saveStudent(Student person); + + void updateStudent(Student student); + + Student findStudent(String id); + + Map findAllStudents(); + + void deleteStudent(String id); +} diff --git a/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java b/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java new file mode 100644 index 0000000000..f13bef0f54 --- /dev/null +++ b/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java @@ -0,0 +1,49 @@ +package com.baeldung.spring.data.redis.repo; + +import com.baeldung.spring.data.redis.model.Student; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.HashOperations; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Repository; + +import javax.annotation.PostConstruct; +import java.util.Map; + +@Repository +public class StudentRepositoryImpl implements StudentRepository { + + private static final String KEY = "Student"; + + private RedisTemplate redisTemplate; + private HashOperations hashOperations; + + @Autowired + public StudentRepositoryImpl(RedisTemplate redisTemplate) { + this.redisTemplate = redisTemplate; + } + + @PostConstruct + private void init() { + hashOperations = redisTemplate.opsForHash(); + } + + public void saveStudent(final Student student) { + hashOperations.put(KEY, student.getId(), student); + } + + public void updateStudent(final Student student) { + hashOperations.put(KEY, student.getId(), student); + } + + public Student findStudent(final String id) { + return (Student) hashOperations.get(KEY, id); + } + + public Map findAllStudents() { + return hashOperations.entries(KEY); + } + + public void deleteStudent(final String id) { + hashOperations.delete(KEY, id); + } +} diff --git a/spring-data-redis/src/main/resources/logback.xml b/spring-data-redis/src/main/resources/logback.xml new file mode 100644 index 0000000000..215eeede64 --- /dev/null +++ b/spring-data-redis/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-redis/src/main/resources/test.png b/spring-data-redis/src/main/resources/test.png new file mode 100644 index 0000000000..c3b5e80276 Binary files /dev/null and b/spring-data-redis/src/main/resources/test.png differ diff --git a/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/RedisMessageListenerTest.java b/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/RedisMessageListenerTest.java new file mode 100644 index 0000000000..403cf990e0 --- /dev/null +++ b/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/RedisMessageListenerTest.java @@ -0,0 +1,30 @@ +package com.baeldung.spring.data.redis; + +import com.baeldung.spring.data.redis.config.RedisConfig; +import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber; +import com.baeldung.spring.data.redis.queue.RedisMessagePublisher; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.UUID; + +import static org.junit.Assert.assertTrue; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = RedisConfig.class) +public class RedisMessageListenerTest { + + @Autowired + private RedisMessagePublisher redisMessagePublisher; + + @Test + public void testOnMessage() throws Exception { + String message = "Message " + UUID.randomUUID(); + redisMessagePublisher.publish(message); + Thread.sleep(100); + assertTrue(RedisMessageSubscriber.messageList.get(0).contains(message)); + } +} \ No newline at end of file diff --git a/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/repo/StudentRepositoryTest.java b/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/repo/StudentRepositoryTest.java new file mode 100644 index 0000000000..c32dfc7670 --- /dev/null +++ b/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/repo/StudentRepositoryTest.java @@ -0,0 +1,59 @@ +package com.baeldung.spring.data.redis.repo; + +import com.baeldung.spring.data.redis.config.RedisConfig; +import com.baeldung.spring.data.redis.model.Student; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = RedisConfig.class) +public class StudentRepositoryTest { + + @Autowired + private StudentRepository studentRepository; + + @Test + public void whenSavingStudent_thenAvailableOnRetrieval() throws Exception { + final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); + studentRepository.saveStudent(student); + final Student retrievedStudent = studentRepository.findStudent(student.getId()); + assertEquals(student.getId(), retrievedStudent.getId()); + } + + @Test + public void whenUpdatingStudent_thenAvailableOnRetrieval() throws Exception { + final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); + studentRepository.saveStudent(student); + student.setName("Richard Watson"); + studentRepository.saveStudent(student); + final Student retrievedStudent = studentRepository.findStudent(student.getId()); + assertEquals(student.getName(), retrievedStudent.getName()); + } + + @Test + public void whenSavingStudents_thenAllShouldAvailableOnRetrieval() throws Exception { + final Student engStudent = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); + final Student medStudent = new Student("Med2015001", "Gareth Houston", Student.Gender.MALE, 2); + studentRepository.saveStudent(engStudent); + studentRepository.saveStudent(medStudent); + final Map retrievedStudent = studentRepository.findAllStudents(); + assertEquals(retrievedStudent.size(), 2); + } + + @Test + public void whenDeletingStudent_thenNotAvailableOnRetrieval() throws Exception { + final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1); + studentRepository.saveStudent(student); + studentRepository.deleteStudent(student.getId()); + final Student retrievedStudent = studentRepository.findStudent(student.getId()); + assertNull(retrievedStudent); + } +} \ No newline at end of file diff --git a/spring-data-rest/README.md b/spring-data-rest/README.md new file mode 100644 index 0000000000..7dc439206b --- /dev/null +++ b/spring-data-rest/README.md @@ -0,0 +1,14 @@ +###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. + +# Running the project +The application uses [Spring Boot](http://projects.spring.io/spring-boot/), so it is easy to run. You can start it any of a few ways: +* Run the `main` method from `SpringDataRestApplication` +* Use the Maven Spring Boot plugin: `mvn spring-boot:run` +* Package the application as a JAR and run it using `java -jar intro-spring-data-rest.jar` + +# Viewing the running application +To view the running application, visit [http://localhost:8080](http://localhost:8080) in your browser diff --git a/spring-data-rest/pom.xml b/spring-data-rest/pom.xml new file mode 100644 index 0000000000..f7f28aa9f1 --- /dev/null +++ b/spring-data-rest/pom.xml @@ -0,0 +1,63 @@ + + + 4.0.0 + + com.baeldung + intro-spring-data-rest + 1.0 + jar + + intro-spring-data-rest + Intro to Spring Data REST + + + org.springframework.boot + spring-boot-starter-parent + 1.3.3.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-data-rest + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + ${project.artifactId} + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/spring-data-rest/src/main/java/com/baeldung/SpringDataRestApplication.java b/spring-data-rest/src/main/java/com/baeldung/SpringDataRestApplication.java new file mode 100644 index 0000000000..6e8e62f52c --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/SpringDataRestApplication.java @@ -0,0 +1,11 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringDataRestApplication { + public static void main(String[] args) { + SpringApplication.run(SpringDataRestApplication.class, args); + } +} diff --git a/spring-data-rest/src/main/java/com/baeldung/UserRepository.java b/spring-data-rest/src/main/java/com/baeldung/UserRepository.java new file mode 100644 index 0000000000..ebbf0d49ab --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/UserRepository.java @@ -0,0 +1,12 @@ +package com.baeldung; + +import org.springframework.data.repository.PagingAndSortingRepository; +import org.springframework.data.repository.query.Param; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; + +import java.util.List; + +@RepositoryRestResource(collectionResourceRel = "users", path = "users") +public interface UserRepository extends PagingAndSortingRepository { + List findByName(@Param("name") String name); +} diff --git a/spring-data-rest/src/main/java/com/baeldung/WebsiteUser.java b/spring-data-rest/src/main/java/com/baeldung/WebsiteUser.java new file mode 100644 index 0000000000..a7a35a2573 --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/WebsiteUser.java @@ -0,0 +1,41 @@ +package com.baeldung; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class WebsiteUser { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + + private String name; + private String email; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } +} diff --git a/spring-data-rest/src/main/resources/application.properties b/spring-data-rest/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-exceptions/pom.xml b/spring-exceptions/pom.xml index f4c0acec85..554bb0c170 100644 --- a/spring-exceptions/pom.xml +++ b/spring-exceptions/pom.xml @@ -1,6 +1,6 @@ 4.0.0 - org.baeldung + com.baeldung spring-exceptions 0.1-SNAPSHOT @@ -105,7 +105,7 @@ junit - junit-dep + junit ${junit.version} test @@ -203,44 +203,44 @@ - 4.2.2.RELEASE - 4.0.2.RELEASE + 4.2.5.RELEASE + 4.0.4.RELEASE 3.20.0-GA 1.2 4.3.11.Final - 5.1.36 + 5.1.38 7.0.42 - 1.7.12 + 1.7.13 1.1.3 - 5.2.1.Final + 5.2.2.Final - 18.0 + 19.0 3.4 1.3 - 4.11 + 4.12 1.10.19 4.4.1 4.5 - 2.4.1 + 2.9.0 1.8.9 - 3.3 + 3.5.1 2.6 - 2.18.1 + 2.19.1 2.7 - 1.4.15 + 1.4.18 diff --git a/spring-exceptions/src/main/java/org/baeldung/persistence/dao/impl/ChildDao.java b/spring-exceptions/src/main/java/org/baeldung/persistence/dao/impl/ChildDao.java index 771da435c6..e068573c5c 100644 --- a/spring-exceptions/src/main/java/org/baeldung/persistence/dao/impl/ChildDao.java +++ b/spring-exceptions/src/main/java/org/baeldung/persistence/dao/impl/ChildDao.java @@ -8,7 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @Repository -public class ChildDao extends AbstractHibernateDaoimplements IChildDao { +public class ChildDao extends AbstractHibernateDao implements IChildDao { @Autowired private SessionFactory sessionFactory; diff --git a/spring-exceptions/src/main/java/org/baeldung/persistence/dao/impl/FooDao.java b/spring-exceptions/src/main/java/org/baeldung/persistence/dao/impl/FooDao.java index a5d995cec0..baf29c9ecd 100644 --- a/spring-exceptions/src/main/java/org/baeldung/persistence/dao/impl/FooDao.java +++ b/spring-exceptions/src/main/java/org/baeldung/persistence/dao/impl/FooDao.java @@ -8,7 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @Repository -public class FooDao extends AbstractHibernateDaoimplements IFooDao { +public class FooDao extends AbstractHibernateDao implements IFooDao { @Autowired private SessionFactory sessionFactory; diff --git a/spring-exceptions/src/main/java/org/baeldung/persistence/dao/impl/ParentDao.java b/spring-exceptions/src/main/java/org/baeldung/persistence/dao/impl/ParentDao.java index 207e01de58..5634137b63 100644 --- a/spring-exceptions/src/main/java/org/baeldung/persistence/dao/impl/ParentDao.java +++ b/spring-exceptions/src/main/java/org/baeldung/persistence/dao/impl/ParentDao.java @@ -8,7 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @Repository -public class ParentDao extends AbstractHibernateDaoimplements IParentDao { +public class ParentDao extends AbstractHibernateDao implements IParentDao { @Autowired private SessionFactory sessionFactory; diff --git a/spring-exceptions/src/main/java/org/baeldung/persistence/service/impl/ChildService.java b/spring-exceptions/src/main/java/org/baeldung/persistence/service/impl/ChildService.java index 987466643b..89597313ea 100644 --- a/spring-exceptions/src/main/java/org/baeldung/persistence/service/impl/ChildService.java +++ b/spring-exceptions/src/main/java/org/baeldung/persistence/service/impl/ChildService.java @@ -9,7 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service -public class ChildService extends AbstractServiceimplements IChildService { +public class ChildService extends AbstractService implements IChildService { @Autowired private IChildDao dao; diff --git a/spring-exceptions/src/main/java/org/baeldung/persistence/service/impl/FooService.java b/spring-exceptions/src/main/java/org/baeldung/persistence/service/impl/FooService.java index 382368bbd4..f0a4d7a649 100644 --- a/spring-exceptions/src/main/java/org/baeldung/persistence/service/impl/FooService.java +++ b/spring-exceptions/src/main/java/org/baeldung/persistence/service/impl/FooService.java @@ -9,7 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service -public class FooService extends AbstractServiceimplements IFooService { +public class FooService extends AbstractService implements IFooService { @Autowired private IFooDao dao; diff --git a/spring-exceptions/src/main/java/org/baeldung/persistence/service/impl/ParentService.java b/spring-exceptions/src/main/java/org/baeldung/persistence/service/impl/ParentService.java index e40ccfd2f8..97c44f4a2f 100644 --- a/spring-exceptions/src/main/java/org/baeldung/persistence/service/impl/ParentService.java +++ b/spring-exceptions/src/main/java/org/baeldung/persistence/service/impl/ParentService.java @@ -9,7 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service -public class ParentService extends AbstractServiceimplements IParentService { +public class ParentService extends AbstractService implements IParentService { @Autowired private IParentDao dao; diff --git a/spring-mvc-xml/.classpath b/spring-freemarker/.classpath similarity index 100% rename from spring-mvc-xml/.classpath rename to spring-freemarker/.classpath index 6b533711d3..5c3ac53820 100644 --- a/spring-mvc-xml/.classpath +++ b/spring-freemarker/.classpath @@ -6,32 +6,32 @@ - - - - - - + + + + + + + + + + + - - - - - diff --git a/spring-freemarker/.gitignore b/spring-freemarker/.gitignore new file mode 100644 index 0000000000..b83d22266a --- /dev/null +++ b/spring-freemarker/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/spring-security-oauth/spring-security-oauth-server/.project b/spring-freemarker/.project similarity index 96% rename from spring-security-oauth/spring-security-oauth-server/.project rename to spring-freemarker/.project index a66e7f1009..1d63e30744 100644 --- a/spring-security-oauth/spring-security-oauth-server/.project +++ b/spring-freemarker/.project @@ -1,6 +1,6 @@ - spring-security-oauth-server + spring4-freemarker-example @@ -21,12 +21,12 @@ - org.springframework.ide.eclipse.core.springbuilder + org.eclipse.wst.validation.validationbuilder - org.eclipse.wst.validation.validationbuilder + org.springframework.ide.eclipse.core.springbuilder @@ -37,9 +37,9 @@ + org.springframework.ide.eclipse.core.springnature org.eclipse.jem.workbench.JavaEMFNature org.eclipse.wst.common.modulecore.ModuleCoreNature - org.springframework.ide.eclipse.core.springnature org.eclipse.jdt.core.javanature org.eclipse.m2e.core.maven2Nature org.eclipse.wst.common.project.facet.core.nature diff --git a/spring-freemarker/README.md b/spring-freemarker/README.md new file mode 100644 index 0000000000..bd939d11e9 --- /dev/null +++ b/spring-freemarker/README.md @@ -0,0 +1,7 @@ +========= + +## Using FreeMarker in Spring MVC + + +### Relevant Articles: +- [Introduction to Using FreeMarker in Spring MVC](http://www.baeldung.com/freemarker-in-spring-mvc-tutorial) diff --git a/spring-freemarker/pom.xml b/spring-freemarker/pom.xml new file mode 100644 index 0000000000..9bbdbcecfb --- /dev/null +++ b/spring-freemarker/pom.xml @@ -0,0 +1,86 @@ + + 4.0.0 + com.freemarker.example + spring4-freemarker-example + war + 1.0-SNAPSHOT + Spring Freemarker Example + + + 1.8 + 4.2.4.RELEASE + 2.3.23 + 1.1.3 + 1.7.12 + 3.1.0 + false + + + + + + org.springframework + spring-webmvc + ${spring.version} + + + commons-logging + commons-logging + + + + + + org.springframework + spring-context-support + ${spring.version} + + + + + org.slf4j + jcl-over-slf4j + ${jcl.slf4j.version} + + + + ch.qos.logback + logback-classic + ${logback.version} + + + + + + javax.servlet + javax.servlet-api + ${servletapi.version} + provided + + + + + org.freemarker + freemarker + ${freemarker.version} + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + ${jdk.version} + ${jdk.version} + true + + + + + + \ No newline at end of file diff --git a/spring-freemarker/src/main/java/com/baeldung/freemarker/config/SpringWebConfig.java b/spring-freemarker/src/main/java/com/baeldung/freemarker/config/SpringWebConfig.java new file mode 100644 index 0000000000..77f0712aca --- /dev/null +++ b/spring-freemarker/src/main/java/com/baeldung/freemarker/config/SpringWebConfig.java @@ -0,0 +1,38 @@ +package com.baeldung.freemarker.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; +import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; + +@EnableWebMvc +@Configuration +@ComponentScan({"com.baeldung.freemarker"}) +public class SpringWebConfig extends WebMvcConfigurerAdapter { + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); + } + + @Bean + public FreeMarkerViewResolver freemarkerViewResolver() { + FreeMarkerViewResolver resolver = new FreeMarkerViewResolver(); + resolver.setCache(true); + resolver.setPrefix(""); + resolver.setSuffix(".ftl"); + return resolver; + } + + @Bean + public FreeMarkerConfigurer freemarkerConfig() { + FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer(); + freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/views/ftl/"); + return freeMarkerConfigurer; + } + +} \ No newline at end of file diff --git a/spring-freemarker/src/main/java/com/baeldung/freemarker/config/WebConfiguration.java b/spring-freemarker/src/main/java/com/baeldung/freemarker/config/WebConfiguration.java new file mode 100644 index 0000000000..3d5ab964e7 --- /dev/null +++ b/spring-freemarker/src/main/java/com/baeldung/freemarker/config/WebConfiguration.java @@ -0,0 +1,22 @@ +package com.baeldung.freemarker.config; + +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +public class WebConfiguration extends AbstractAnnotationConfigDispatcherServletInitializer { + + @Override + protected Class[] getServletConfigClasses() { + return new Class[] { SpringWebConfig.class }; + } + + @Override + protected String[] getServletMappings() { + return new String[] { "/" }; + } + + @Override + protected Class[] getRootConfigClasses() { + return null; + } + +} \ No newline at end of file diff --git a/spring-freemarker/src/main/java/com/baeldung/freemarker/controller/SpringController.java b/spring-freemarker/src/main/java/com/baeldung/freemarker/controller/SpringController.java new file mode 100644 index 0000000000..b911a4975b --- /dev/null +++ b/spring-freemarker/src/main/java/com/baeldung/freemarker/controller/SpringController.java @@ -0,0 +1,48 @@ +package com.baeldung.freemarker.controller; + +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import com.baeldung.freemarker.model.Car; + +@Controller +public class SpringController { + + private static List carList = new ArrayList(); + + @RequestMapping(value = "/", method = RequestMethod.GET) + public String home(Locale locale, Model model) { + return "redirect:/cars"; + } + + static { + carList.add(new Car("Honda", "Civic")); + carList.add(new Car("Toyota", "Camry")); + carList.add(new Car("Nissan", "Altima")); + } + + + @RequestMapping(value = "/cars", method = RequestMethod.GET) + public String init(@ModelAttribute("model") ModelMap model) { + model.addAttribute("carList", carList); + return "index"; + } + + @RequestMapping(value = "/add", method = RequestMethod.POST) + public String addCar(@ModelAttribute("car") Car car) { + if (null != car && null != car.getMake() && null != car.getModel() + && !car.getMake().isEmpty() && !car.getModel().isEmpty()) { + carList.add(car); + } + return "redirect:/cars"; + } + +} diff --git a/spring-freemarker/src/main/java/com/baeldung/freemarker/model/Car.java b/spring-freemarker/src/main/java/com/baeldung/freemarker/model/Car.java new file mode 100644 index 0000000000..e400d87608 --- /dev/null +++ b/spring-freemarker/src/main/java/com/baeldung/freemarker/model/Car.java @@ -0,0 +1,33 @@ +package com.baeldung.freemarker.model; + +public class Car { + + private String make; + private String model; + + public Car(){ + + } + + public Car(String make, String model) { + this.make = make; + this.model = model; + } + + public String getMake() { + return make; + } + + public void setMake(String make) { + this.make = make; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + +} diff --git a/spring-freemarker/src/main/resources/log4j.xml b/spring-freemarker/src/main/resources/log4j.xml new file mode 100644 index 0000000000..ae7d4b5ca4 --- /dev/null +++ b/spring-freemarker/src/main/resources/log4j.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-freemarker/src/main/webapp/WEB-INF/views/ftl/index.ftl b/spring-freemarker/src/main/webapp/WEB-INF/views/ftl/index.ftl new file mode 100644 index 0000000000..899c9b9271 --- /dev/null +++ b/spring-freemarker/src/main/webapp/WEB-INF/views/ftl/index.ftl @@ -0,0 +1,61 @@ + + FreeMarker Spring MVC Hello World + + + + + + +
+ +
+ Add Car + + Make :
+ Model:
+ + +
+ +
+
/>
+ + + + <#list model["carList"] as car> + + + + +
Make Model
${car.make} ${car.model}
+ + + + \ No newline at end of file diff --git a/spring-freemarker/src/test/resources/log4j.xml b/spring-freemarker/src/test/resources/log4j.xml new file mode 100644 index 0000000000..9b1538d31a --- /dev/null +++ b/spring-freemarker/src/test/resources/log4j.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-hibernate3/pom.xml b/spring-hibernate3/pom.xml index 29682a97c3..59053be596 100644 --- a/spring-hibernate3/pom.xml +++ b/spring-hibernate3/pom.xml @@ -1,6 +1,6 @@ 4.0.0 - org.baeldung + com.baeldung spring-hibernate3 0.1-SNAPSHOT @@ -73,7 +73,7 @@ junit - junit-dep + junit ${junit.version} test @@ -162,41 +162,41 @@ - 4.2.2.RELEASE - 4.0.2.RELEASE + 4.2.5.RELEASE + 4.0.4.RELEASE 3.20.0-GA 3.6.10.Final - 5.1.36 + 5.1.38 7.0.47 - 1.7.12 + 1.7.13 1.1.3 - 5.2.1.Final + 5.2.2.Final - 18.0 + 19.0 3.4 1.3 - 4.11 + 4.12 1.10.19 4.4.1 4.5 - 2.4.1 + 2.9.0 - 3.3 - 2.18.1 + 3.5.1 + 2.19.1 2.7 - 1.4.15 + 1.4.18 diff --git a/spring-hibernate3/src/main/java/org/baeldung/persistence/dao/FooDao.java b/spring-hibernate3/src/main/java/org/baeldung/persistence/dao/FooDao.java index cb8d7488ea..23de04169e 100644 --- a/spring-hibernate3/src/main/java/org/baeldung/persistence/dao/FooDao.java +++ b/spring-hibernate3/src/main/java/org/baeldung/persistence/dao/FooDao.java @@ -4,7 +4,7 @@ import org.baeldung.persistence.model.Foo; import org.springframework.stereotype.Repository; @Repository -public class FooDao extends AbstractHibernateDaoimplements IFooDao { +public class FooDao extends AbstractHibernateDao implements IFooDao { public FooDao() { super(); diff --git a/spring-hibernate4/README.md b/spring-hibernate4/README.md index 0d1ac1600e..4c0c6706d6 100644 --- a/spring-hibernate4/README.md +++ b/spring-hibernate4/README.md @@ -5,6 +5,9 @@ ### Relevant Articles: - [Hibernate 4 with Spring](http://www.baeldung.com/hibernate-4-spring) - [The DAO with Spring 3 and Hibernate](http://www.baeldung.com/2011/12/02/the-persistence-layer-with-spring-3-1-and-hibernate/) +- [Hibernate Pagination](http://www.baeldung.com/hibernate-pagination) +- [Sorting with Hibernate](http://www.baeldung.com/hibernate-sort) +- [Auditing with JPA, Hibernate, and Spring Data JPA](http://www.baeldung.com/database-auditing-jpa) ### Quick Start diff --git a/spring-hibernate4/pom.xml b/spring-hibernate4/pom.xml index 3b0e3b6a00..3652674614 100644 --- a/spring-hibernate4/pom.xml +++ b/spring-hibernate4/pom.xml @@ -1,6 +1,6 @@ 4.0.0 - org.baeldung + com.baeldung spring-hibernate4 0.1-SNAPSHOT @@ -15,6 +15,16 @@ spring-context ${org.springframework.version} + + org.springframework + spring-aspects + ${org.springframework.version} + + + org.springframework.security + spring-security-core + ${org.springframework.security.version} + @@ -23,21 +33,35 @@ spring-orm ${org.springframework.version} + + org.springframework.data + spring-data-jpa + ${org.springframework.data.version} + org.hibernate hibernate-core ${hibernate.version} - org.javassist - javassist - ${javassist.version} + org.hibernate + hibernate-envers + ${hibernate-envers.version} + + + javax.transaction + jta + ${jta.version} + + + javax.el + javax.el-api + ${el-api.version} mysql mysql-connector-java ${mysql-connector-java.version} - runtime @@ -53,6 +77,11 @@ hibernate-validator ${hibernate-validator.version} + + javax.el + javax.el-api + 2.2.5 + @@ -77,20 +106,35 @@ ${org.springframework.version} test - + junit - junit-dep + junit ${junit.version} test + + org.springframework.security + spring-security-test + ${org.springframework.security.version} + test + + org.hamcrest hamcrest-core ${org.hamcrest.version} test + + + junit + junit-dep + ${junit.version} + test + + org.hamcrest hamcrest-library @@ -169,24 +213,28 @@ - 4.2.2.RELEASE - 4.0.2.RELEASE - 3.20.0-GA - + 4.2.5.RELEASE + 4.0.4.RELEASE + 1.9.2.RELEASE + 3.20.0-GA + 4.3.11.Final - 5.1.36 - 7.0.42 + ${hibernate.version} + 5.1.38 + 8.0.30 + 1.1 + 2.2.4 - 1.7.12 + 1.7.13 1.1.3 - + - 5.2.1.Final + 5.2.2.Final - 18.0 + 19.0 3.4 @@ -197,14 +245,15 @@ 4.4.1 4.5 - 2.4.1 + 2.9.0 - 3.3 - 2.18.1 + 3.5.1 + 2.19.1 2.7 - 1.4.15 + 1.4.18 + - \ No newline at end of file + diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/IBarAuditableDao.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/IBarAuditableDao.java new file mode 100644 index 0000000000..182b493592 --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/IBarAuditableDao.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.dao; + +import com.baeldung.persistence.dao.common.IAuditOperations; +import com.baeldung.persistence.model.Bar; + +public interface IBarAuditableDao extends IBarDao, IAuditOperations { + // +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/IBarCrudRepository.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/IBarCrudRepository.java new file mode 100644 index 0000000000..4d7db64240 --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/IBarCrudRepository.java @@ -0,0 +1,10 @@ +package com.baeldung.persistence.dao; + +import java.io.Serializable; + +import com.baeldung.persistence.model.Bar; +import org.springframework.data.repository.CrudRepository; + +public interface IBarCrudRepository extends CrudRepository { + // +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/IBarDao.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/IBarDao.java new file mode 100644 index 0000000000..7896a2a84a --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/IBarDao.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.dao; + +import com.baeldung.persistence.dao.common.IOperations; +import com.baeldung.persistence.model.Bar; + +public interface IBarDao extends IOperations { + // +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/IChildDao.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/IChildDao.java new file mode 100644 index 0000000000..a55a0b0598 --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/IChildDao.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.dao; + +import com.baeldung.persistence.model.Child; +import com.baeldung.persistence.dao.common.IOperations; + +public interface IChildDao extends IOperations { + // +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/IFooAuditableDao.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/IFooAuditableDao.java new file mode 100644 index 0000000000..ddbb685988 --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/IFooAuditableDao.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.dao; + +import com.baeldung.persistence.dao.common.IAuditOperations; +import com.baeldung.persistence.model.Foo; + +public interface IFooAuditableDao extends IFooDao, IAuditOperations { + // +} \ No newline at end of file diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/IFooDao.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/IFooDao.java new file mode 100644 index 0000000000..0935772dbd --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/IFooDao.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.dao; + +import com.baeldung.persistence.model.Foo; +import com.baeldung.persistence.dao.common.IOperations; + +public interface IFooDao extends IOperations { + // +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/IParentDao.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/IParentDao.java new file mode 100644 index 0000000000..03680158bb --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/IParentDao.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.dao; + +import com.baeldung.persistence.model.Parent; +import com.baeldung.persistence.dao.common.IOperations; + +public interface IParentDao extends IOperations { + // +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/AbstractDao.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/AbstractDao.java new file mode 100644 index 0000000000..5a6c76a93a --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/AbstractDao.java @@ -0,0 +1,14 @@ +package com.baeldung.persistence.dao.common; + +import java.io.Serializable; + +import com.google.common.base.Preconditions; + +public abstract class AbstractDao implements IOperations { + + protected Class clazz; + + protected final void setClazz(final Class clazzToSet) { + clazz = Preconditions.checkNotNull(clazzToSet); + } +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/AbstractHibernateAuditableDao.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/AbstractHibernateAuditableDao.java new file mode 100644 index 0000000000..41184669ad --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/AbstractHibernateAuditableDao.java @@ -0,0 +1,37 @@ +package com.baeldung.persistence.dao.common; + +import java.io.Serializable; +import java.util.List; + +import org.hibernate.envers.AuditReader; +import org.hibernate.envers.AuditReaderFactory; +import org.hibernate.envers.query.AuditQuery; + +@SuppressWarnings("unchecked") +public class AbstractHibernateAuditableDao extends AbstractHibernateDao implements IAuditOperations { + + @Override + public List getEntitiesAtRevision(final Number revision) { + final AuditReader auditReader = AuditReaderFactory.get(getCurrentSession()); + final AuditQuery query = auditReader.createQuery().forEntitiesAtRevision(clazz, revision); + final List resultList = query.getResultList(); + return resultList; + } + + @Override + public List getEntitiesModifiedAtRevision(final Number revision) { + final AuditReader auditReader = AuditReaderFactory.get(getCurrentSession()); + final AuditQuery query = auditReader.createQuery().forEntitiesModifiedAtRevision(clazz, revision); + final List resultList = query.getResultList(); + return resultList; + } + + @Override + public List getRevisions() { + final AuditReader auditReader = AuditReaderFactory.get(getCurrentSession()); + final AuditQuery query = auditReader.createQuery().forRevisionsOfEntity(clazz, true, true); + final List resultList = query.getResultList(); + return resultList; + } + +} diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/common/AbstractHibernateDao.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/AbstractHibernateDao.java similarity index 62% rename from spring-hibernate4/src/main/java/org/baeldung/persistence/dao/common/AbstractHibernateDao.java rename to spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/AbstractHibernateDao.java index 65e57afcb3..f3ade67f80 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/common/AbstractHibernateDao.java +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/AbstractHibernateDao.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.dao.common; +package com.baeldung.persistence.dao.common; import java.io.Serializable; import java.util.List; @@ -10,55 +10,49 @@ import org.springframework.beans.factory.annotation.Autowired; import com.google.common.base.Preconditions; @SuppressWarnings("unchecked") -public abstract class AbstractHibernateDao implements IOperations { - private Class clazz; +public abstract class AbstractHibernateDao extends AbstractDao implements IOperations { @Autowired - private SessionFactory sessionFactory; + protected SessionFactory sessionFactory; // API - protected final void setClazz(final Class clazzToSet) { - clazz = Preconditions.checkNotNull(clazzToSet); - } - @Override - public final T findOne(final long id) { + public T findOne(final long id) { return (T) getCurrentSession().get(clazz, id); } @Override - public final List findAll() { + public List findAll() { return getCurrentSession().createQuery("from " + clazz.getName()).list(); } @Override - public final void create(final T entity) { + public void create(final T entity) { Preconditions.checkNotNull(entity); - // getCurrentSession().persist(entity); getCurrentSession().saveOrUpdate(entity); } @Override - public final T update(final T entity) { + public T update(final T entity) { Preconditions.checkNotNull(entity); return (T) getCurrentSession().merge(entity); } @Override - public final void delete(final T entity) { + public void delete(final T entity) { Preconditions.checkNotNull(entity); getCurrentSession().delete(entity); } @Override - public final void deleteById(final long entityId) { + public void deleteById(final long entityId) { final T entity = findOne(entityId); Preconditions.checkState(entity != null); delete(entity); } - protected final Session getCurrentSession() { + protected Session getCurrentSession() { return sessionFactory.getCurrentSession(); } diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/AbstractJpaDao.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/AbstractJpaDao.java new file mode 100644 index 0000000000..69f8e58c25 --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/AbstractJpaDao.java @@ -0,0 +1,56 @@ +package com.baeldung.persistence.dao.common; + +import java.io.Serializable; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.TypedQuery; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; + +public class AbstractJpaDao extends AbstractDao implements IOperations { + + @PersistenceContext + private EntityManager em; + + // API + + @Override + public T findOne(final long id) { + return em.find(clazz, Long.valueOf(id).intValue()); + } + + @Override + public List findAll() { + final CriteriaBuilder cb = em.getCriteriaBuilder(); + final CriteriaQuery cq = cb.createQuery(clazz); + final Root rootEntry = cq.from(clazz); + final CriteriaQuery all = cq.select(rootEntry); + final TypedQuery allQuery = em.createQuery(all); + return allQuery.getResultList(); + } + + @Override + public void create(final T entity) { + em.persist(entity); + } + + @Override + public T update(final T entity) { + em.merge(entity); + return entity; + } + + @Override + public void delete(final T entity) { + em.remove(entity); + } + + @Override + public void deleteById(final long entityId) { + delete(findOne(entityId)); + } + +} diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/common/GenericHibernateDao.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/GenericHibernateDao.java similarity index 76% rename from spring-hibernate4/src/main/java/org/baeldung/persistence/dao/common/GenericHibernateDao.java rename to spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/GenericHibernateDao.java index 018e367209..18b16fa033 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/common/GenericHibernateDao.java +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/GenericHibernateDao.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.dao.common; +package com.baeldung.persistence.dao.common; import java.io.Serializable; @@ -8,6 +8,6 @@ import org.springframework.stereotype.Repository; @Repository @Scope(BeanDefinition.SCOPE_PROTOTYPE) -public class GenericHibernateDao extends AbstractHibernateDaoimplements IGenericDao { +public class GenericHibernateDao extends AbstractHibernateDao implements IGenericDao { // } \ No newline at end of file diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/IAuditOperations.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/IAuditOperations.java new file mode 100644 index 0000000000..169d3fed72 --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/IAuditOperations.java @@ -0,0 +1,14 @@ +package com.baeldung.persistence.dao.common; + +import java.io.Serializable; +import java.util.List; + +public interface IAuditOperations { + + List getEntitiesAtRevision(Number revision); + + List getEntitiesModifiedAtRevision(Number revision); + + List getRevisions(); + +} diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/common/IGenericDao.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/IGenericDao.java similarity index 72% rename from spring-hibernate4/src/main/java/org/baeldung/persistence/dao/common/IGenericDao.java rename to spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/IGenericDao.java index b7876deea4..8d8af18394 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/common/IGenericDao.java +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/IGenericDao.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.dao.common; +package com.baeldung.persistence.dao.common; import java.io.Serializable; diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/common/IOperations.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/IOperations.java similarity index 87% rename from spring-hibernate4/src/main/java/org/baeldung/persistence/dao/common/IOperations.java rename to spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/IOperations.java index 1c84b06c85..4ef99221ab 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/common/IOperations.java +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/common/IOperations.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.dao.common; +package com.baeldung.persistence.dao.common; import java.io.Serializable; import java.util.List; diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/impl/BarAuditableDao.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/impl/BarAuditableDao.java new file mode 100644 index 0000000000..e12b6ae2da --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/impl/BarAuditableDao.java @@ -0,0 +1,28 @@ +package com.baeldung.persistence.dao.impl; + +import java.util.List; + +import com.baeldung.persistence.dao.IBarAuditableDao; +import com.baeldung.persistence.dao.common.AbstractHibernateAuditableDao; +import com.baeldung.persistence.model.Bar; + +public class BarAuditableDao extends AbstractHibernateAuditableDao implements IBarAuditableDao { + + public BarAuditableDao() { + super(); + + setClazz(Bar.class); + } + + // API + + @Override + public List getRevisions() { + final List resultList = super.getRevisions(); + for (final Bar bar : resultList) { + bar.getFooSet().size(); // force FooSet initialization + } + return resultList; + } + +} \ No newline at end of file diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/impl/BarDao.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/impl/BarDao.java new file mode 100644 index 0000000000..0ead802dc5 --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/impl/BarDao.java @@ -0,0 +1,19 @@ +package com.baeldung.persistence.dao.impl; + +import com.baeldung.persistence.dao.common.AbstractHibernateDao; +import com.baeldung.persistence.dao.IBarDao; +import com.baeldung.persistence.model.Bar; +import org.springframework.stereotype.Repository; + +@Repository +public class BarDao extends AbstractHibernateDao implements IBarDao { + + public BarDao() { + super(); + + setClazz(Bar.class); + } + + // API + +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/impl/BarJpaDao.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/impl/BarJpaDao.java new file mode 100644 index 0000000000..e0fa382d41 --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/impl/BarJpaDao.java @@ -0,0 +1,19 @@ +package com.baeldung.persistence.dao.impl; + +import com.baeldung.persistence.dao.IBarDao; +import com.baeldung.persistence.dao.common.AbstractJpaDao; +import com.baeldung.persistence.model.Bar; +import org.springframework.stereotype.Repository; + +@Repository +public class BarJpaDao extends AbstractJpaDao implements IBarDao { + + public BarJpaDao() { + super(); + + setClazz(Bar.class); + } + + // API + +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/impl/ChildDao.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/impl/ChildDao.java new file mode 100644 index 0000000000..b55da6e43a --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/impl/ChildDao.java @@ -0,0 +1,19 @@ +package com.baeldung.persistence.dao.impl; + +import com.baeldung.persistence.dao.common.AbstractHibernateDao; +import com.baeldung.persistence.model.Child; +import com.baeldung.persistence.dao.IChildDao; +import org.springframework.stereotype.Repository; + +@Repository +public class ChildDao extends AbstractHibernateDao implements IChildDao { + + public ChildDao() { + super(); + + setClazz(Child.class); + } + + // API + +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/impl/FooAuditableDao.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/impl/FooAuditableDao.java new file mode 100644 index 0000000000..05064c1478 --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/impl/FooAuditableDao.java @@ -0,0 +1,17 @@ +package com.baeldung.persistence.dao.impl; + +import com.baeldung.persistence.dao.common.AbstractHibernateAuditableDao; +import com.baeldung.persistence.model.Foo; +import com.baeldung.persistence.dao.IFooAuditableDao; + +public class FooAuditableDao extends AbstractHibernateAuditableDao implements IFooAuditableDao { + + public FooAuditableDao() { + super(); + + setClazz(Foo.class); + } + + // API + +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/impl/FooDao.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/impl/FooDao.java new file mode 100644 index 0000000000..787c449b1d --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/impl/FooDao.java @@ -0,0 +1,19 @@ +package com.baeldung.persistence.dao.impl; + +import com.baeldung.persistence.dao.common.AbstractHibernateDao; +import com.baeldung.persistence.dao.IFooDao; +import com.baeldung.persistence.model.Foo; +import org.springframework.stereotype.Repository; + +@Repository +public class FooDao extends AbstractHibernateDao implements IFooDao { + + public FooDao() { + super(); + + setClazz(Foo.class); + } + + // API + +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/impl/ParentDao.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/impl/ParentDao.java new file mode 100644 index 0000000000..4602b5f30e --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/dao/impl/ParentDao.java @@ -0,0 +1,19 @@ +package com.baeldung.persistence.dao.impl; + +import com.baeldung.persistence.dao.IParentDao; +import com.baeldung.persistence.dao.common.AbstractHibernateDao; +import com.baeldung.persistence.model.Parent; +import org.springframework.stereotype.Repository; + +@Repository +public class ParentDao extends AbstractHibernateDao implements IParentDao { + + public ParentDao() { + super(); + + setClazz(Parent.class); + } + + // API + +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/model/Bar.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/model/Bar.java new file mode 100644 index 0000000000..efc6367116 --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/model/Bar.java @@ -0,0 +1,242 @@ +package com.baeldung.persistence.model; + +import java.io.Serializable; +import java.util.Date; +import java.util.Set; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EntityListeners; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.NamedQuery; +import javax.persistence.OneToMany; +import javax.persistence.PrePersist; +import javax.persistence.PreRemove; +import javax.persistence.PreUpdate; + +import org.hibernate.annotations.OrderBy; +import org.hibernate.envers.Audited; +import org.jboss.logging.Logger; +import org.springframework.data.annotation.CreatedBy; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedBy; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import com.google.common.collect.Sets; + +@Entity +@NamedQuery(name = "Bar.findAll", query = "SELECT b FROM Bar b") +@Audited +@EntityListeners(AuditingEntityListener.class) +public class Bar implements Serializable { + + private static Logger logger = Logger.getLogger(Bar.class); + + public enum OPERATION { + INSERT, UPDATE, DELETE; + private String value; + + OPERATION() { + value = toString(); + } + + public String getValue() { + return value; + } + + public static OPERATION parse(final String value) { + OPERATION operation = null; + for (final OPERATION op : OPERATION.values()) { + if (op.getValue().equals(value)) { + operation = op; + break; + } + } + return operation; + } + }; + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "id") + private int id; + + @Column(name = "name") + private String name; + + @OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @OrderBy(clause = "NAME DESC") + // @NotAudited + private Set fooSet = Sets.newHashSet(); + + @Column(name = "operation") + private String operation; + + @Column(name = "timestamp") + private long timestamp; + + @Column(name = "created_date") + @CreatedDate + private long createdDate; + + @Column(name = "modified_date") + @LastModifiedDate + private long modifiedDate; + + @Column(name = "created_by") + @CreatedBy + private String createdBy; + + @Column(name = "modified_by") + @LastModifiedBy + private String modifiedBy; + + public Bar() { + super(); + } + + public Bar(final String name) { + super(); + + this.name = name; + } + + // API + + public Set getFooSet() { + return fooSet; + } + + public void setFooSet(final Set fooSet) { + this.fooSet = fooSet; + } + + public int getId() { + return id; + } + + public void setId(final int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public OPERATION getOperation() { + return OPERATION.parse(operation); + } + + public void setOperation(final OPERATION operation) { + this.operation = operation.getValue(); + } + + public long getTimestamp() { + return timestamp; + } + + public void setTimestamp(final long timestamp) { + this.timestamp = timestamp; + } + + public long getCreatedDate() { + return createdDate; + } + + public void setCreatedDate(final long createdDate) { + this.createdDate = createdDate; + } + + public long getModifiedDate() { + return modifiedDate; + } + + public void setModifiedDate(final long modifiedDate) { + this.modifiedDate = modifiedDate; + } + + public String getCreatedBy() { + return createdBy; + } + + public void setCreatedBy(final String createdBy) { + this.createdBy = createdBy; + } + + public String getModifiedBy() { + return modifiedBy; + } + + public void setModifiedBy(final String modifiedBy) { + this.modifiedBy = modifiedBy; + } + + public void setOperation(final String operation) { + this.operation = operation; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + final Bar other = (Bar) obj; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("Bar [name=").append(name).append("]"); + return builder.toString(); + } + + @PrePersist + public void onPrePersist() { + logger.info("@PrePersist"); + audit(OPERATION.INSERT); + } + + @PreUpdate + public void onPreUpdate() { + logger.info("@PreUpdate"); + audit(OPERATION.UPDATE); + } + + @PreRemove + public void onPreRemove() { + logger.info("@PreRemove"); + audit(OPERATION.DELETE); + } + + private void audit(final OPERATION operation) { + setOperation(operation); + setTimestamp((new Date()).getTime()); + } + +} diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Child.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/model/Child.java similarity index 95% rename from spring-hibernate4/src/main/java/org/baeldung/persistence/model/Child.java rename to spring-hibernate4/src/main/java/com/baeldung/persistence/model/Child.java index 4eec4cf1d3..19cfb2e237 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Child.java +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/model/Child.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.model; +package com.baeldung.persistence.model; import java.io.Serializable; diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/model/Foo.java similarity index 95% rename from spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java rename to spring-hibernate4/src/main/java/com/baeldung/persistence/model/Foo.java index 974535e058..6b48c1fa66 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/model/Foo.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.model; +package com.baeldung.persistence.model; import java.io.Serializable; @@ -12,7 +12,11 @@ import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; +import org.hibernate.envers.Audited; + @Entity +@Audited +// @Proxy(lazy = false) public class Foo implements Serializable { @Id diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Parent.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/model/Parent.java similarity index 96% rename from spring-hibernate4/src/main/java/org/baeldung/persistence/model/Parent.java rename to spring-hibernate4/src/main/java/com/baeldung/persistence/model/Parent.java index 19e405615d..fa6948990b 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Parent.java +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/model/Parent.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.model; +package com.baeldung.persistence.model; import java.io.Serializable; diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/service/IBarAuditableService.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/IBarAuditableService.java new file mode 100644 index 0000000000..33e5634d12 --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/IBarAuditableService.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.service; + +import com.baeldung.persistence.dao.common.IAuditOperations; +import com.baeldung.persistence.model.Bar; + +public interface IBarAuditableService extends IBarService, IAuditOperations { + +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/service/IBarService.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/IBarService.java new file mode 100644 index 0000000000..21185b5990 --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/IBarService.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.service; + +import com.baeldung.persistence.dao.common.IOperations; +import com.baeldung.persistence.model.Bar; + +public interface IBarService extends IOperations { + // +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/service/IChildService.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/IChildService.java new file mode 100644 index 0000000000..afe67a70c2 --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/IChildService.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.service; + +import com.baeldung.persistence.model.Child; +import com.baeldung.persistence.dao.common.IOperations; + +public interface IChildService extends IOperations { + // +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/service/IFooAuditableService.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/IFooAuditableService.java new file mode 100644 index 0000000000..b787e7fe91 --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/IFooAuditableService.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.service; + +import com.baeldung.persistence.dao.common.IAuditOperations; +import com.baeldung.persistence.model.Foo; + +public interface IFooAuditableService extends IFooService, IAuditOperations { + // +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/service/IFooService.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/IFooService.java new file mode 100644 index 0000000000..ffdb53964a --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/IFooService.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.service; + +import com.baeldung.persistence.model.Foo; +import com.baeldung.persistence.dao.common.IOperations; + +public interface IFooService extends IOperations { + // +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/service/IParentService.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/IParentService.java new file mode 100644 index 0000000000..f941416aac --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/IParentService.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.service; + +import com.baeldung.persistence.model.Parent; +import com.baeldung.persistence.dao.common.IOperations; + +public interface IParentService extends IOperations { + // +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/service/common/AbstractHibernateAuditableService.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/common/AbstractHibernateAuditableService.java new file mode 100644 index 0000000000..2695d7760a --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/common/AbstractHibernateAuditableService.java @@ -0,0 +1,30 @@ +package com.baeldung.persistence.service.common; + +import java.io.Serializable; +import java.util.List; + +import com.baeldung.persistence.dao.common.IAuditOperations; +import com.baeldung.persistence.dao.common.IOperations; +import org.springframework.transaction.annotation.Transactional; + +@Transactional(value = "hibernateTransactionManager") +public abstract class AbstractHibernateAuditableService extends AbstractHibernateService implements IOperations, IAuditOperations { + + @Override + public List getEntitiesAtRevision(final Number revision) { + return getAuditableDao().getEntitiesAtRevision(revision); + } + + @Override + public List getEntitiesModifiedAtRevision(final Number revision) { + return getAuditableDao().getEntitiesModifiedAtRevision(revision); + } + + @Override + public List getRevisions() { + return getAuditableDao().getRevisions(); + } + + abstract protected IAuditOperations getAuditableDao(); + +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/service/common/AbstractHibernateService.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/common/AbstractHibernateService.java new file mode 100644 index 0000000000..02b8ccf48b --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/common/AbstractHibernateService.java @@ -0,0 +1,42 @@ +package com.baeldung.persistence.service.common; + +import java.io.Serializable; +import java.util.List; + +import com.baeldung.persistence.dao.common.IOperations; +import org.springframework.transaction.annotation.Transactional; + +@Transactional(value = "hibernateTransactionManager") +public abstract class AbstractHibernateService extends AbstractService implements IOperations { + + @Override + public T findOne(final long id) { + return super.findOne(id); + } + + @Override + public List findAll() { + return super.findAll(); + } + + @Override + public void create(final T entity) { + super.create(entity); + } + + @Override + public T update(final T entity) { + return super.update(entity); + } + + @Override + public void delete(final T entity) { + super.delete(entity); + } + + @Override + public void deleteById(final long entityId) { + super.deleteById(entityId); + } + +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/service/common/AbstractJpaService.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/common/AbstractJpaService.java new file mode 100644 index 0000000000..a1c6fe9edf --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/common/AbstractJpaService.java @@ -0,0 +1,42 @@ +package com.baeldung.persistence.service.common; + +import java.io.Serializable; +import java.util.List; + +import com.baeldung.persistence.dao.common.IOperations; +import org.springframework.transaction.annotation.Transactional; + +@Transactional(value = "jpaTransactionManager") +public abstract class AbstractJpaService extends AbstractService implements IOperations { + + @Override + public T findOne(final long id) { + return super.findOne(id); + } + + @Override + public List findAll() { + return super.findAll(); + } + + @Override + public void create(final T entity) { + super.create(entity); + } + + @Override + public T update(final T entity) { + return super.update(entity); + } + + @Override + public void delete(final T entity) { + super.delete(entity); + } + + @Override + public void deleteById(final long entityId) { + super.deleteById(entityId); + } + +} diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/service/common/AbstractService.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/common/AbstractService.java similarity index 80% rename from spring-hibernate4/src/main/java/org/baeldung/persistence/service/common/AbstractService.java rename to spring-hibernate4/src/main/java/com/baeldung/persistence/service/common/AbstractService.java index 3b32bc3ebb..9b001b1fac 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/service/common/AbstractService.java +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/common/AbstractService.java @@ -1,12 +1,10 @@ -package org.baeldung.persistence.service.common; +package com.baeldung.persistence.service.common; import java.io.Serializable; import java.util.List; -import org.baeldung.persistence.dao.common.IOperations; -import org.springframework.transaction.annotation.Transactional; +import com.baeldung.persistence.dao.common.IOperations; -@Transactional public abstract class AbstractService implements IOperations { @Override diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/service/common/AbstractSpringDataJpaService.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/common/AbstractSpringDataJpaService.java new file mode 100644 index 0000000000..cef483e6bf --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/common/AbstractSpringDataJpaService.java @@ -0,0 +1,46 @@ +package com.baeldung.persistence.service.common; + +import java.io.Serializable; +import java.util.List; + +import com.baeldung.persistence.dao.common.IOperations; +import org.springframework.data.repository.CrudRepository; +import org.springframework.transaction.annotation.Transactional; + +import com.google.common.collect.Lists; + +@Transactional(value = "jpaTransactionManager") +public abstract class AbstractSpringDataJpaService implements IOperations { + + @Override + public T findOne(final long id) { + return getDao().findOne(Long.valueOf(id)); + } + + @Override + public List findAll() { + return Lists.newArrayList(getDao().findAll()); + } + + @Override + public void create(final T entity) { + getDao().save(entity); + } + + @Override + public T update(final T entity) { + return getDao().save(entity); + } + + @Override + public void delete(final T entity) { + getDao().delete(entity); + } + + @Override + public void deleteById(final long entityId) { + getDao().delete(Long.valueOf(entityId)); + } + + protected abstract CrudRepository getDao(); +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/BarAuditableService.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/BarAuditableService.java new file mode 100644 index 0000000000..d84c28caa5 --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/BarAuditableService.java @@ -0,0 +1,41 @@ +package com.baeldung.persistence.service.impl; + +import com.baeldung.persistence.dao.common.IAuditOperations; +import com.baeldung.persistence.service.common.AbstractHibernateAuditableService; +import com.baeldung.persistence.dao.IBarAuditableDao; +import com.baeldung.persistence.dao.IBarDao; +import com.baeldung.persistence.dao.common.IOperations; +import com.baeldung.persistence.model.Bar; +import com.baeldung.persistence.service.IBarAuditableService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +public class BarAuditableService extends AbstractHibernateAuditableService implements IBarAuditableService { + + @Autowired + @Qualifier("barHibernateDao") + private IBarDao dao; + + @Autowired + @Qualifier("barHibernateAuditableDao") + private IBarAuditableDao auditDao; + + public BarAuditableService() { + super(); + } + + // API + + @Override + protected IOperations getDao() { + return dao; + } + + @Override + protected IAuditOperations getAuditableDao() { + return auditDao; + } + +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/BarJpaService.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/BarJpaService.java new file mode 100644 index 0000000000..1c1b7a2274 --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/BarJpaService.java @@ -0,0 +1,30 @@ +package com.baeldung.persistence.service.impl; + +import com.baeldung.persistence.dao.IBarDao; +import com.baeldung.persistence.dao.common.IOperations; +import com.baeldung.persistence.model.Bar; +import com.baeldung.persistence.service.IBarService; +import com.baeldung.persistence.service.common.AbstractJpaService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +public class BarJpaService extends AbstractJpaService implements IBarService { + + @Autowired + @Qualifier("barJpaDao") + private IBarDao dao; + + public BarJpaService() { + super(); + } + + // API + + @Override + protected IOperations getDao() { + return dao; + } + +} \ No newline at end of file diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/BarService.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/BarService.java new file mode 100644 index 0000000000..32d1f919c5 --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/BarService.java @@ -0,0 +1,30 @@ +package com.baeldung.persistence.service.impl; + +import com.baeldung.persistence.dao.IBarDao; +import com.baeldung.persistence.dao.common.IOperations; +import com.baeldung.persistence.model.Bar; +import com.baeldung.persistence.service.IBarService; +import com.baeldung.persistence.service.common.AbstractHibernateService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +public class BarService extends AbstractHibernateService implements IBarService { + + @Autowired + @Qualifier("barHibernateDao") + private IBarDao dao; + + public BarService() { + super(); + } + + // API + + @Override + protected IOperations getDao() { + return dao; + } + +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/BarSpringDataJpaService.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/BarSpringDataJpaService.java new file mode 100644 index 0000000000..4a55d08a35 --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/BarSpringDataJpaService.java @@ -0,0 +1,26 @@ +package com.baeldung.persistence.service.impl; + +import java.io.Serializable; + +import com.baeldung.persistence.service.common.AbstractSpringDataJpaService; +import com.baeldung.persistence.dao.IBarCrudRepository; +import com.baeldung.persistence.model.Bar; +import com.baeldung.persistence.service.IBarService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.repository.CrudRepository; + +public class BarSpringDataJpaService extends AbstractSpringDataJpaService implements IBarService { + + @Autowired + private IBarCrudRepository dao; + + public BarSpringDataJpaService() { + super(); + } + + @Override + protected CrudRepository getDao() { + return dao; + } + +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/ChildService.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/ChildService.java new file mode 100644 index 0000000000..417fe2c49a --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/ChildService.java @@ -0,0 +1,28 @@ +package com.baeldung.persistence.service.impl; + +import com.baeldung.persistence.model.Child; +import com.baeldung.persistence.service.IChildService; +import com.baeldung.persistence.dao.IChildDao; +import com.baeldung.persistence.dao.common.IOperations; +import com.baeldung.persistence.service.common.AbstractHibernateService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class ChildService extends AbstractHibernateService implements IChildService { + + @Autowired + private IChildDao dao; + + public ChildService() { + super(); + } + + // API + + @Override + protected IOperations getDao() { + return dao; + } + +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/FooAuditableService.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/FooAuditableService.java new file mode 100644 index 0000000000..45ad315c42 --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/FooAuditableService.java @@ -0,0 +1,41 @@ +package com.baeldung.persistence.service.impl; + +import com.baeldung.persistence.dao.common.IAuditOperations; +import com.baeldung.persistence.service.IFooAuditableService; +import com.baeldung.persistence.service.common.AbstractHibernateAuditableService; +import com.baeldung.persistence.dao.IFooAuditableDao; +import com.baeldung.persistence.dao.IFooDao; +import com.baeldung.persistence.dao.common.IOperations; +import com.baeldung.persistence.model.Foo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +public class FooAuditableService extends AbstractHibernateAuditableService implements IFooAuditableService { + + @Autowired + @Qualifier("fooHibernateDao") + private IFooDao dao; + + @Autowired + @Qualifier("fooHibernateAuditableDao") + private IFooAuditableDao auditDao; + + public FooAuditableService() { + super(); + } + + // API + + @Override + protected IOperations getDao() { + return dao; + } + + @Override + protected IAuditOperations getAuditableDao() { + return auditDao; + } + +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/FooService.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/FooService.java new file mode 100644 index 0000000000..84cf018fee --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/FooService.java @@ -0,0 +1,30 @@ +package com.baeldung.persistence.service.impl; + +import com.baeldung.persistence.dao.IFooDao; +import com.baeldung.persistence.dao.common.IOperations; +import com.baeldung.persistence.model.Foo; +import com.baeldung.persistence.service.IFooService; +import com.baeldung.persistence.service.common.AbstractHibernateService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +public class FooService extends AbstractHibernateService implements IFooService { + + @Autowired + @Qualifier("fooHibernateDao") + private IFooDao dao; + + public FooService() { + super(); + } + + // API + + @Override + protected IOperations getDao() { + return dao; + } + +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/ParentService.java b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/ParentService.java new file mode 100644 index 0000000000..078acfc369 --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/persistence/service/impl/ParentService.java @@ -0,0 +1,28 @@ +package com.baeldung.persistence.service.impl; + +import com.baeldung.persistence.model.Parent; +import com.baeldung.persistence.service.IParentService; +import com.baeldung.persistence.dao.IParentDao; +import com.baeldung.persistence.dao.common.IOperations; +import com.baeldung.persistence.service.common.AbstractHibernateService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class ParentService extends AbstractHibernateService implements IParentService { + + @Autowired + private IParentDao dao; + + public ParentService() { + super(); + } + + // API + + @Override + protected IOperations getDao() { + return dao; + } + +} diff --git a/spring-hibernate4/src/main/java/com/baeldung/spring/PersistenceConfig.java b/spring-hibernate4/src/main/java/com/baeldung/spring/PersistenceConfig.java new file mode 100644 index 0000000000..35e80b81a5 --- /dev/null +++ b/spring-hibernate4/src/main/java/com/baeldung/spring/PersistenceConfig.java @@ -0,0 +1,179 @@ +package com.baeldung.spring; + +import java.util.Properties; + +import javax.sql.DataSource; + +import org.apache.tomcat.dbcp.dbcp2.BasicDataSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.orm.hibernate4.HibernateTransactionManager; +import org.springframework.orm.hibernate4.LocalSessionFactoryBean; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.JpaVendorAdapter; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import com.baeldung.persistence.dao.IBarAuditableDao; +import com.baeldung.persistence.dao.IBarDao; +import com.baeldung.persistence.dao.IFooAuditableDao; +import com.baeldung.persistence.dao.IFooDao; +import com.baeldung.persistence.dao.impl.BarAuditableDao; +import com.baeldung.persistence.dao.impl.BarDao; +import com.baeldung.persistence.dao.impl.BarJpaDao; +import com.baeldung.persistence.dao.impl.FooAuditableDao; +import com.baeldung.persistence.dao.impl.FooDao; +import com.baeldung.persistence.service.IBarAuditableService; +import com.baeldung.persistence.service.IBarService; +import com.baeldung.persistence.service.IFooAuditableService; +import com.baeldung.persistence.service.IFooService; +import com.baeldung.persistence.service.impl.BarAuditableService; +import com.baeldung.persistence.service.impl.BarJpaService; +import com.baeldung.persistence.service.impl.BarSpringDataJpaService; +import com.baeldung.persistence.service.impl.FooAuditableService; +import com.baeldung.persistence.service.impl.FooService; +import com.google.common.base.Preconditions; + +@Configuration +@EnableTransactionManagement +@EnableJpaRepositories(basePackages = { "com.baeldung.persistence" }, transactionManagerRef = "jpaTransactionManager") +@EnableJpaAuditing +@PropertySource({ "classpath:persistence-mysql.properties" }) +@ComponentScan({ "com.baeldung.persistence" }) +public class PersistenceConfig { + + @Autowired + private Environment env; + + public PersistenceConfig() { + super(); + } + + @Bean + public LocalSessionFactoryBean sessionFactory() { + final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); + sessionFactory.setDataSource(restDataSource()); + sessionFactory.setPackagesToScan(new String[] { "com.baeldung.persistence.model" }); + sessionFactory.setHibernateProperties(hibernateProperties()); + + return sessionFactory; + } + + @Bean + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { + final LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean(); + emf.setDataSource(restDataSource()); + emf.setPackagesToScan(new String[] { "com.baeldung.persistence.model" }); + + final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); + emf.setJpaVendorAdapter(vendorAdapter); + emf.setJpaProperties(hibernateProperties()); + + return emf; + } + + @Bean + public DataSource restDataSource() { + final BasicDataSource dataSource = new BasicDataSource(); + dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName"))); + dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url"))); + dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user"))); + dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass"))); + + return dataSource; + } + + @Bean + public PlatformTransactionManager hibernateTransactionManager() { + final HibernateTransactionManager transactionManager = new HibernateTransactionManager(); + transactionManager.setSessionFactory(sessionFactory().getObject()); + return transactionManager; + } + + @Bean + public PlatformTransactionManager jpaTransactionManager() { + final JpaTransactionManager transactionManager = new JpaTransactionManager(); + transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); + return transactionManager; + } + + @Bean + public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { + return new PersistenceExceptionTranslationPostProcessor(); + } + + @Bean + public IBarService barJpaService() { + return new BarJpaService(); + } + + @Bean + public IBarService barSpringDataJpaService() { + return new BarSpringDataJpaService(); + } + + @Bean + public IFooService fooHibernateService() { + return new FooService(); + } + + @Bean + public IBarAuditableService barHibernateAuditableService() { + return new BarAuditableService(); + } + + @Bean + public IFooAuditableService fooHibernateAuditableService() { + return new FooAuditableService(); + } + + @Bean + public IBarDao barJpaDao() { + return new BarJpaDao(); + } + + @Bean + public IBarDao barHibernateDao() { + return new BarDao(); + } + + @Bean + public IBarAuditableDao barHibernateAuditableDao() { + return new BarAuditableDao(); + } + + @Bean + public IFooDao fooHibernateDao() { + return new FooDao(); + } + + @Bean + public IFooAuditableDao fooHibernateAuditableDao() { + return new FooAuditableDao(); + } + + private final Properties hibernateProperties() { + final Properties hibernateProperties = new Properties(); + hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); + hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); + + hibernateProperties.setProperty("hibernate.show_sql", "true"); + // hibernateProperties.setProperty("hibernate.format_sql", "true"); + // hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true"); + + // Envers properties + hibernateProperties.setProperty("org.hibernate.envers.audit_table_suffix", env.getProperty("envers.audit_table_suffix")); + + return hibernateProperties; + } + +} \ No newline at end of file diff --git a/spring-hibernate4/src/main/java/org/baeldung/spring/PersistenceXmlConfig.java b/spring-hibernate4/src/main/java/com/baeldung/spring/PersistenceXmlConfig.java similarity index 83% rename from spring-hibernate4/src/main/java/org/baeldung/spring/PersistenceXmlConfig.java rename to spring-hibernate4/src/main/java/com/baeldung/spring/PersistenceXmlConfig.java index f5857ec425..9cbeb8e1f8 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/spring/PersistenceXmlConfig.java +++ b/spring-hibernate4/src/main/java/com/baeldung/spring/PersistenceXmlConfig.java @@ -1,4 +1,4 @@ -package org.baeldung.spring; +package com.baeldung.spring; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @@ -7,7 +7,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration @EnableTransactionManagement -@ComponentScan({ "org.baeldung.persistence.dao", "org.baeldung.persistence.service" }) +@ComponentScan({ "com.baeldung.persistence.dao", "com.baeldung.persistence.service" }) @ImportResource({ "classpath:hibernate4Config.xml" }) public class PersistenceXmlConfig { diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/IChildDao.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/IChildDao.java deleted file mode 100644 index 3bc0dc1fc4..0000000000 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/IChildDao.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.baeldung.persistence.dao; - -import org.baeldung.persistence.dao.common.IOperations; -import org.baeldung.persistence.model.Child; - -public interface IChildDao extends IOperations { - // -} diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/IFooDao.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/IFooDao.java deleted file mode 100644 index fc3928d8a6..0000000000 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/IFooDao.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.baeldung.persistence.dao; - -import org.baeldung.persistence.dao.common.IOperations; -import org.baeldung.persistence.model.Foo; - -public interface IFooDao extends IOperations { - // -} diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/IParentDao.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/IParentDao.java deleted file mode 100644 index 09158a4143..0000000000 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/IParentDao.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.baeldung.persistence.dao; - -import org.baeldung.persistence.dao.common.IOperations; -import org.baeldung.persistence.model.Parent; - -public interface IParentDao extends IOperations { - // -} diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/impl/ChildDao.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/impl/ChildDao.java deleted file mode 100644 index 80a4007633..0000000000 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/impl/ChildDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.baeldung.persistence.dao.impl; - -import org.baeldung.persistence.dao.IChildDao; -import org.baeldung.persistence.dao.common.AbstractHibernateDao; -import org.baeldung.persistence.model.Child; -import org.springframework.stereotype.Repository; - -@Repository -public class ChildDao extends AbstractHibernateDaoimplements IChildDao { - - public ChildDao() { - super(); - - setClazz(Child.class); - } - - // API - -} diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/impl/FooDao.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/impl/FooDao.java deleted file mode 100644 index eb3a66126c..0000000000 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/impl/FooDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.baeldung.persistence.dao.impl; - -import org.baeldung.persistence.dao.IFooDao; -import org.baeldung.persistence.dao.common.AbstractHibernateDao; -import org.baeldung.persistence.model.Foo; -import org.springframework.stereotype.Repository; - -@Repository -public class FooDao extends AbstractHibernateDaoimplements IFooDao { - - public FooDao() { - super(); - - setClazz(Foo.class); - } - - // API - -} diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/impl/ParentDao.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/impl/ParentDao.java deleted file mode 100644 index fca4c69338..0000000000 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/dao/impl/ParentDao.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.baeldung.persistence.dao.impl; - -import org.baeldung.persistence.dao.IParentDao; -import org.baeldung.persistence.dao.common.AbstractHibernateDao; -import org.baeldung.persistence.model.Parent; -import org.springframework.stereotype.Repository; - -@Repository -public class ParentDao extends AbstractHibernateDaoimplements IParentDao { - - public ParentDao() { - super(); - - setClazz(Parent.class); - } - - // API - -} diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java deleted file mode 100644 index 410ad79b02..0000000000 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java +++ /dev/null @@ -1,106 +0,0 @@ -package org.baeldung.persistence.model; - -import java.io.Serializable; -import java.util.Set; - -import javax.persistence.CascadeType; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.NamedQuery; -import javax.persistence.OneToMany; - -import org.hibernate.annotations.OrderBy; - -import com.google.common.collect.Sets; - -@Entity -@NamedQuery(name = "Bar.findAll", query = "SELECT b FROM Bar b") -public class Bar implements Serializable { - - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - @Column(name = "id") - private int id; - - @Column(name = "name") - private String name; - - @OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.LAZY) - @OrderBy(clause = "NAME DESC") - private Set fooSet = Sets.newHashSet(); - - public Bar() { - super(); - } - - public Bar(final String name) { - super(); - - this.name = name; - } - - // API - - public Set getFooSet() { - return fooSet; - } - - public void setFooSet(final Set fooSet) { - this.fooSet = fooSet; - } - - public int getId() { - return id; - } - - public void setId(final int id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(final String name) { - this.name = name; - } - - // - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((name == null) ? 0 : name.hashCode()); - return result; - } - - @Override - public boolean equals(final Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - final Bar other = (Bar) obj; - if (name == null) { - if (other.name != null) - return false; - } else if (!name.equals(other.name)) - return false; - return true; - } - - @Override - public String toString() { - final StringBuilder builder = new StringBuilder(); - builder.append("Bar [name=").append(name).append("]"); - return builder.toString(); - } - -} diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/service/IChildService.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/service/IChildService.java deleted file mode 100644 index c6c5e2cfb1..0000000000 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/service/IChildService.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.baeldung.persistence.service; - -import org.baeldung.persistence.dao.common.IOperations; -import org.baeldung.persistence.model.Child; - -public interface IChildService extends IOperations { - // -} diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/service/IFooService.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/service/IFooService.java deleted file mode 100644 index 8774320450..0000000000 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/service/IFooService.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.baeldung.persistence.service; - -import org.baeldung.persistence.dao.common.IOperations; -import org.baeldung.persistence.model.Foo; - -public interface IFooService extends IOperations { - // -} diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/service/IParentService.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/service/IParentService.java deleted file mode 100644 index 1782c281d2..0000000000 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/service/IParentService.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.baeldung.persistence.service; - -import org.baeldung.persistence.dao.common.IOperations; -import org.baeldung.persistence.model.Parent; - -public interface IParentService extends IOperations { - // -} diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/service/impl/ChildService.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/service/impl/ChildService.java deleted file mode 100644 index 71b1bc697e..0000000000 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/service/impl/ChildService.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.baeldung.persistence.service.impl; - -import org.baeldung.persistence.dao.IChildDao; -import org.baeldung.persistence.dao.common.IOperations; -import org.baeldung.persistence.model.Child; -import org.baeldung.persistence.service.IChildService; -import org.baeldung.persistence.service.common.AbstractService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -@Service -public class ChildService extends AbstractServiceimplements IChildService { - - @Autowired - private IChildDao dao; - - public ChildService() { - super(); - } - - // API - - @Override - protected IOperations getDao() { - return dao; - } - -} diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/service/impl/FooService.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/service/impl/FooService.java deleted file mode 100644 index 8a89153dd0..0000000000 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/service/impl/FooService.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.baeldung.persistence.service.impl; - -import org.baeldung.persistence.dao.IFooDao; -import org.baeldung.persistence.dao.common.IOperations; -import org.baeldung.persistence.model.Foo; -import org.baeldung.persistence.service.IFooService; -import org.baeldung.persistence.service.common.AbstractService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -@Service -public class FooService extends AbstractServiceimplements IFooService { - - @Autowired - private IFooDao dao; - - public FooService() { - super(); - } - - // API - - @Override - protected IOperations getDao() { - return dao; - } - -} diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/service/impl/ParentService.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/service/impl/ParentService.java deleted file mode 100644 index 1f9b602350..0000000000 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/service/impl/ParentService.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.baeldung.persistence.service.impl; - -import org.baeldung.persistence.dao.IParentDao; -import org.baeldung.persistence.dao.common.IOperations; -import org.baeldung.persistence.model.Parent; -import org.baeldung.persistence.service.IParentService; -import org.baeldung.persistence.service.common.AbstractService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -@Service -public class ParentService extends AbstractServiceimplements IParentService { - - @Autowired - private IParentDao dao; - - public ParentService() { - super(); - } - - // API - - @Override - protected IOperations getDao() { - return dao; - } - -} diff --git a/spring-hibernate4/src/main/java/org/baeldung/spring/PersistenceConfig.java b/spring-hibernate4/src/main/java/org/baeldung/spring/PersistenceConfig.java deleted file mode 100644 index 000c67d625..0000000000 --- a/spring-hibernate4/src/main/java/org/baeldung/spring/PersistenceConfig.java +++ /dev/null @@ -1,82 +0,0 @@ -package org.baeldung.spring; - -import java.util.Properties; - -import javax.sql.DataSource; - -import org.apache.tomcat.dbcp.dbcp.BasicDataSource; -import org.hibernate.SessionFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.core.env.Environment; -import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; -import org.springframework.orm.hibernate4.HibernateTransactionManager; -import org.springframework.orm.hibernate4.LocalSessionFactoryBean; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -import com.google.common.base.Preconditions; - -@Configuration -@EnableTransactionManagement -@PropertySource({ "classpath:persistence-mysql.properties" }) -@ComponentScan({ "org.baeldung.persistence" }) -public class PersistenceConfig { - - @Autowired - private Environment env; - - public PersistenceConfig() { - super(); - } - - @Bean - public LocalSessionFactoryBean sessionFactory() { - final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); - sessionFactory.setDataSource(restDataSource()); - sessionFactory.setPackagesToScan(new String[] { "org.baeldung.persistence.model" }); - sessionFactory.setHibernateProperties(hibernateProperties()); - - return sessionFactory; - } - - @Bean - public DataSource restDataSource() { - final BasicDataSource dataSource = new BasicDataSource(); - dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName"))); - dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url"))); - dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user"))); - dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass"))); - - return dataSource; - } - - @Bean - @Autowired - public HibernateTransactionManager transactionManager(final SessionFactory sessionFactory) { - final HibernateTransactionManager txManager = new HibernateTransactionManager(); - txManager.setSessionFactory(sessionFactory); - - return txManager; - } - - @Bean - public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { - return new PersistenceExceptionTranslationPostProcessor(); - } - - final Properties hibernateProperties() { - final Properties hibernateProperties = new Properties(); - hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); - hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); - - hibernateProperties.setProperty("hibernate.show_sql", "true"); - // hibernateProperties.setProperty("hibernate.format_sql", "true"); - // hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true"); - - return hibernateProperties; - } - -} \ No newline at end of file diff --git a/spring-hibernate4/src/main/resources/hibernate4Config.xml b/spring-hibernate4/src/main/resources/hibernate4Config.xml index aa419a19f7..ca507802cd 100644 --- a/spring-hibernate4/src/main/resources/hibernate4Config.xml +++ b/spring-hibernate4/src/main/resources/hibernate4Config.xml @@ -9,7 +9,7 @@ - + ${hibernate.hbm2ddl.auto} @@ -18,7 +18,7 @@ - + diff --git a/spring-hibernate4/src/main/resources/persistence-mysql.properties b/spring-hibernate4/src/main/resources/persistence-mysql.properties index 8263b0d9ac..f6b6ab6fca 100644 --- a/spring-hibernate4/src/main/resources/persistence-mysql.properties +++ b/spring-hibernate4/src/main/resources/persistence-mysql.properties @@ -8,3 +8,6 @@ jdbc.pass=tutorialmy5ql hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.show_sql=false hibernate.hbm2ddl.auto=create-drop + +# envers.X +envers.audit_table_suffix=_audit_log diff --git a/spring-hibernate4/src/main/resources/webSecurityConfig.xml b/spring-hibernate4/src/main/resources/webSecurityConfig.xml index 88af78dabc..d9423d31e7 100644 --- a/spring-hibernate4/src/main/resources/webSecurityConfig.xml +++ b/spring-hibernate4/src/main/resources/webSecurityConfig.xml @@ -5,7 +5,8 @@ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd" > - + + diff --git a/spring-hibernate4/src/test/java/com/baeldung/persistence/IntegrationTestSuite.java b/spring-hibernate4/src/test/java/com/baeldung/persistence/IntegrationTestSuite.java new file mode 100644 index 0000000000..f5c45a5d6f --- /dev/null +++ b/spring-hibernate4/src/test/java/com/baeldung/persistence/IntegrationTestSuite.java @@ -0,0 +1,25 @@ +package com.baeldung.persistence; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +import com.baeldung.persistence.audit.AuditTestSuite; +import com.baeldung.persistence.hibernate.FooPaginationPersistenceIntegrationTest; +import com.baeldung.persistence.hibernate.FooSortingPersistenceIntegrationTest; +import com.baeldung.persistence.service.FooServiceBasicPersistenceIntegrationTest; +import com.baeldung.persistence.service.FooServicePersistenceIntegrationTest; +import com.baeldung.persistence.service.ParentServicePersistenceIntegrationTest; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ // @formatter:off + AuditTestSuite.class + ,FooServiceBasicPersistenceIntegrationTest.class + ,FooPaginationPersistenceIntegrationTest.class + ,FooServicePersistenceIntegrationTest.class + ,ParentServicePersistenceIntegrationTest.class + ,FooSortingPersistenceIntegrationTest.class + +}) // @formatter:on +public class IntegrationTestSuite { + // +} diff --git a/spring-hibernate4/src/test/java/com/baeldung/persistence/audit/AuditTestSuite.java b/spring-hibernate4/src/test/java/com/baeldung/persistence/audit/AuditTestSuite.java new file mode 100644 index 0000000000..8a8a3445f6 --- /dev/null +++ b/spring-hibernate4/src/test/java/com/baeldung/persistence/audit/AuditTestSuite.java @@ -0,0 +1,14 @@ +package com.baeldung.persistence.audit; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ // @formatter:off + EnversFooBarAuditTest.class, + JPABarAuditTest.class, + SpringDataJPABarAuditTest.class +}) // @formatter:on +public class AuditTestSuite { + // +} \ No newline at end of file diff --git a/spring-hibernate4/src/test/java/com/baeldung/persistence/audit/EnversFooBarAuditTest.java b/spring-hibernate4/src/test/java/com/baeldung/persistence/audit/EnversFooBarAuditTest.java new file mode 100644 index 0000000000..6c13816b63 --- /dev/null +++ b/spring-hibernate4/src/test/java/com/baeldung/persistence/audit/EnversFooBarAuditTest.java @@ -0,0 +1,142 @@ +package com.baeldung.persistence.audit; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.List; + +import com.baeldung.persistence.model.Foo; +import com.baeldung.persistence.service.IBarAuditableService; +import com.baeldung.persistence.service.IFooAuditableService; +import com.baeldung.spring.PersistenceConfig; +import com.baeldung.persistence.model.Bar; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) +public class EnversFooBarAuditTest { + + private static Logger logger = LoggerFactory.getLogger(EnversFooBarAuditTest.class); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + logger.info("setUpBeforeClass()"); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + logger.info("tearDownAfterClass()"); + } + + @Autowired + @Qualifier("fooHibernateAuditableService") + private IFooAuditableService fooService; + + @Autowired + @Qualifier("barHibernateAuditableService") + private IBarAuditableService barService; + + @Autowired + private SessionFactory sessionFactory; + + private Session session; + + @Before + public void setUp() throws Exception { + logger.info("setUp()"); + makeRevisions(); + session = sessionFactory.openSession(); + } + + @After + public void tearDown() throws Exception { + logger.info("tearDown()"); + session.close(); + } + + private void makeRevisions() { + final Bar bar = rev1(); + rev2(bar); + rev3(bar); + rev4(bar); + } + + // REV #1: insert BAR & FOO1 + private Bar rev1() { + final Bar bar = new Bar("BAR"); + final Foo foo1 = new Foo("FOO1"); + foo1.setBar(bar); + fooService.create(foo1); + return bar; + } + + // REV #2: insert FOO2 & update BAR + private void rev2(final Bar bar) { + final Foo foo2 = new Foo("FOO2"); + foo2.setBar(bar); + fooService.create(foo2); + } + + // REV #3: update BAR + private void rev3(final Bar bar) { + + bar.setName("BAR1"); + barService.update(bar); + } + + // REV #4: insert FOO3 & update BAR + private void rev4(final Bar bar) { + + final Foo foo3 = new Foo("FOO3"); + foo3.setBar(bar); + fooService.create(foo3); + } + + @Test + public final void whenFooBarsModified_thenFooBarsAudited() { + + List barRevisionList; + List fooRevisionList; + + // test Bar revisions + + barRevisionList = barService.getRevisions(); + + assertNotNull(barRevisionList); + assertEquals(4, barRevisionList.size()); + + assertEquals("BAR", barRevisionList.get(0).getName()); + assertEquals("BAR", barRevisionList.get(1).getName()); + assertEquals("BAR1", barRevisionList.get(2).getName()); + assertEquals("BAR1", barRevisionList.get(3).getName()); + + assertEquals(1, barRevisionList.get(0).getFooSet().size()); + assertEquals(2, barRevisionList.get(1).getFooSet().size()); + assertEquals(2, barRevisionList.get(2).getFooSet().size()); + assertEquals(3, barRevisionList.get(3).getFooSet().size()); + + // test Foo revisions + + fooRevisionList = fooService.getRevisions(); + assertNotNull(fooRevisionList); + assertEquals(3, fooRevisionList.size()); + assertEquals("FOO1", fooRevisionList.get(0).getName()); + assertEquals("FOO2", fooRevisionList.get(1).getName()); + assertEquals("FOO3", fooRevisionList.get(2).getName()); + } + +} diff --git a/spring-hibernate4/src/test/java/com/baeldung/persistence/audit/JPABarAuditTest.java b/spring-hibernate4/src/test/java/com/baeldung/persistence/audit/JPABarAuditTest.java new file mode 100644 index 0000000000..1e4a10f61c --- /dev/null +++ b/spring-hibernate4/src/test/java/com/baeldung/persistence/audit/JPABarAuditTest.java @@ -0,0 +1,102 @@ +package com.baeldung.persistence.audit; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; + +import com.baeldung.persistence.model.Bar; +import com.baeldung.persistence.model.Bar.OPERATION; +import com.baeldung.persistence.service.IBarService; +import com.baeldung.spring.PersistenceConfig; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) +public class JPABarAuditTest { + + private static Logger logger = LoggerFactory.getLogger(JPABarAuditTest.class); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + logger.info("setUpBeforeClass()"); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + logger.info("tearDownAfterClass()"); + } + + @Autowired + @Qualifier("barJpaService") + private IBarService barService; + + @Autowired + private EntityManagerFactory entityManagerFactory; + + private EntityManager em; + + @Before + public void setUp() throws Exception { + logger.info("setUp()"); + em = entityManagerFactory.createEntityManager(); + } + + @After + public void tearDown() throws Exception { + logger.info("tearDown()"); + em.close(); + } + + @Test + public final void whenBarsModified_thenBarsAudited() { + + // insert BAR1 + Bar bar1 = new Bar("BAR1"); + barService.create(bar1); + + // update BAR1 + bar1.setName("BAR1a"); + barService.update(bar1); + + // insert BAR2 + Bar bar2 = new Bar("BAR2"); + barService.create(bar2); + + // update BAR1 + bar1.setName("BAR1b"); + barService.update(bar1); + + // get BAR1 and BAR2 from the DB and check the audit values + // detach instances from persistence context to make sure we fire db + em.detach(bar1); + em.detach(bar2); + bar1 = barService.findOne(bar1.getId()); + bar2 = barService.findOne(bar2.getId()); + + assertNotNull(bar1); + assertNotNull(bar2); + assertEquals(OPERATION.UPDATE, bar1.getOperation()); + assertEquals(OPERATION.INSERT, bar2.getOperation()); + assertTrue(bar1.getTimestamp() > bar2.getTimestamp()); + + barService.deleteById(bar1.getId()); + barService.deleteById(bar2.getId()); + + } + +} \ No newline at end of file diff --git a/spring-hibernate4/src/test/java/com/baeldung/persistence/audit/SpringDataJPABarAuditTest.java b/spring-hibernate4/src/test/java/com/baeldung/persistence/audit/SpringDataJPABarAuditTest.java new file mode 100644 index 0000000000..05c0e9fa83 --- /dev/null +++ b/spring-hibernate4/src/test/java/com/baeldung/persistence/audit/SpringDataJPABarAuditTest.java @@ -0,0 +1,76 @@ +package com.baeldung.persistence.audit; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; + +import com.baeldung.persistence.model.Bar; +import com.baeldung.persistence.service.IBarService; +import com.baeldung.spring.PersistenceConfig; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) +public class SpringDataJPABarAuditTest { + + private static Logger logger = LoggerFactory.getLogger(SpringDataJPABarAuditTest.class); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + logger.info("setUpBeforeClass()"); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + logger.info("tearDownAfterClass()"); + } + + @Autowired + @Qualifier("barSpringDataJpaService") + private IBarService barService; + + @Autowired + private EntityManagerFactory entityManagerFactory; + + private EntityManager em; + + @Before + public void setUp() throws Exception { + logger.info("setUp()"); + em = entityManagerFactory.createEntityManager(); + } + + @After + public void tearDown() throws Exception { + logger.info("tearDown()"); + em.close(); + } + + @Test + @WithMockUser(username = "tutorialuser") + public final void whenBarsModified_thenBarsAudited() { + Bar bar = new Bar("BAR1"); + barService.create(bar); + assertEquals(bar.getCreatedDate(), bar.getModifiedDate()); + assertEquals("tutorialuser", bar.getCreatedBy(), bar.getModifiedBy()); + bar.setName("BAR2"); + bar = barService.update(bar); + assertTrue(bar.getCreatedDate() < bar.getModifiedDate()); + assertEquals("tutorialuser", bar.getCreatedBy(), bar.getModifiedBy()); + } +} diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooFixtures.java b/spring-hibernate4/src/test/java/com/baeldung/persistence/hibernate/FooFixtures.java similarity index 95% rename from spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooFixtures.java rename to spring-hibernate4/src/test/java/com/baeldung/persistence/hibernate/FooFixtures.java index 8b16f9b605..da840dc027 100644 --- a/spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooFixtures.java +++ b/spring-hibernate4/src/test/java/com/baeldung/persistence/hibernate/FooFixtures.java @@ -1,9 +1,9 @@ -package org.baeldung.persistence.hibernate; +package com.baeldung.persistence.hibernate; import java.util.List; -import org.baeldung.persistence.model.Bar; -import org.baeldung.persistence.model.Foo; +import com.baeldung.persistence.model.Foo; +import com.baeldung.persistence.model.Bar; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooPaginationPersistenceIntegrationTest.java b/spring-hibernate4/src/test/java/com/baeldung/persistence/hibernate/FooPaginationPersistenceIntegrationTest.java similarity index 96% rename from spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooPaginationPersistenceIntegrationTest.java rename to spring-hibernate4/src/test/java/com/baeldung/persistence/hibernate/FooPaginationPersistenceIntegrationTest.java index 37e77ef139..20bbf72243 100644 --- a/spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooPaginationPersistenceIntegrationTest.java +++ b/spring-hibernate4/src/test/java/com/baeldung/persistence/hibernate/FooPaginationPersistenceIntegrationTest.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.hibernate; +package com.baeldung.persistence.hibernate; import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; import static org.hamcrest.Matchers.hasSize; @@ -7,9 +7,9 @@ import static org.junit.Assert.assertThat; import java.util.List; -import org.baeldung.persistence.model.Foo; -import org.baeldung.persistence.service.IFooService; -import org.baeldung.spring.PersistenceConfig; +import com.baeldung.persistence.model.Foo; +import com.baeldung.persistence.service.IFooService; +import com.baeldung.spring.PersistenceConfig; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.ScrollMode; diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooSortingPersistenceServiceTest.java b/spring-hibernate4/src/test/java/com/baeldung/persistence/hibernate/FooSortingPersistenceIntegrationTest.java similarity index 96% rename from spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooSortingPersistenceServiceTest.java rename to spring-hibernate4/src/test/java/com/baeldung/persistence/hibernate/FooSortingPersistenceIntegrationTest.java index 3e600816f5..0f76526960 100644 --- a/spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooSortingPersistenceServiceTest.java +++ b/spring-hibernate4/src/test/java/com/baeldung/persistence/hibernate/FooSortingPersistenceIntegrationTest.java @@ -1,13 +1,13 @@ -package org.baeldung.persistence.hibernate; +package com.baeldung.persistence.hibernate; import static org.junit.Assert.assertNull; import java.util.List; import java.util.Set; -import org.baeldung.persistence.model.Bar; -import org.baeldung.persistence.model.Foo; -import org.baeldung.spring.PersistenceConfig; +import com.baeldung.persistence.model.Bar; +import com.baeldung.persistence.model.Foo; +import com.baeldung.spring.PersistenceConfig; import org.hibernate.Criteria; import org.hibernate.NullPrecedence; import org.hibernate.Query; @@ -26,7 +26,7 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) @SuppressWarnings("unchecked") -public class FooSortingPersistenceServiceTest { +public class FooSortingPersistenceIntegrationTest { @Autowired private SessionFactory sessionFactory; diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java b/spring-hibernate4/src/test/java/com/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java similarity index 87% rename from spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java rename to spring-hibernate4/src/test/java/com/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java index 2865810c67..c77f5dfb95 100644 --- a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java +++ b/spring-hibernate4/src/test/java/com/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java @@ -1,10 +1,9 @@ -package org.baeldung.persistence.service; +package com.baeldung.persistence.service; import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; -import org.baeldung.persistence.model.Foo; -import org.baeldung.persistence.service.IFooService; -import org.baeldung.spring.PersistenceConfig; +import com.baeldung.persistence.model.Foo; +import com.baeldung.spring.PersistenceConfig; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.junit.After; diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java b/spring-hibernate4/src/test/java/com/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java similarity index 89% rename from spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java rename to spring-hibernate4/src/test/java/com/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java index 07c6ba3382..b82d4621ab 100644 --- a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java +++ b/spring-hibernate4/src/test/java/com/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java @@ -1,13 +1,14 @@ -package org.baeldung.persistence.service; +package com.baeldung.persistence.service; import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; -import org.baeldung.persistence.model.Foo; -import org.baeldung.spring.PersistenceConfig; +import com.baeldung.persistence.model.Foo; +import com.baeldung.spring.PersistenceConfig; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.dao.DataAccessException; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.InvalidDataAccessApiUsageException; @@ -20,6 +21,7 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader; public class FooServicePersistenceIntegrationTest { @Autowired + @Qualifier("fooHibernateService") private IFooService service; // tests diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/ParentServicePersistenceIntegrationTest.java b/spring-hibernate4/src/test/java/com/baeldung/persistence/service/ParentServicePersistenceIntegrationTest.java similarity index 92% rename from spring-hibernate4/src/test/java/org/baeldung/persistence/service/ParentServicePersistenceIntegrationTest.java rename to spring-hibernate4/src/test/java/com/baeldung/persistence/service/ParentServicePersistenceIntegrationTest.java index 3960aa79ea..9e8c4aba92 100644 --- a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/ParentServicePersistenceIntegrationTest.java +++ b/spring-hibernate4/src/test/java/com/baeldung/persistence/service/ParentServicePersistenceIntegrationTest.java @@ -1,8 +1,8 @@ -package org.baeldung.persistence.service; +package com.baeldung.persistence.service; -import org.baeldung.persistence.model.Child; -import org.baeldung.persistence.model.Parent; -import org.baeldung.spring.PersistenceConfig; +import com.baeldung.persistence.model.Child; +import com.baeldung.persistence.model.Parent; +import com.baeldung.spring.PersistenceConfig; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/IntegrationTestSuite.java b/spring-hibernate4/src/test/java/org/baeldung/persistence/IntegrationTestSuite.java deleted file mode 100644 index 42fcc28378..0000000000 --- a/spring-hibernate4/src/test/java/org/baeldung/persistence/IntegrationTestSuite.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.baeldung.persistence; - -import org.baeldung.persistence.hibernate.FooPaginationPersistenceIntegrationTest; -import org.baeldung.persistence.hibernate.FooSortingPersistenceServiceTest; -import org.baeldung.persistence.service.FooServiceBasicPersistenceIntegrationTest; -import org.baeldung.persistence.service.FooServicePersistenceIntegrationTest; -import org.baeldung.persistence.service.ParentServicePersistenceIntegrationTest; -import org.junit.runner.RunWith; -import org.junit.runners.Suite; - -@RunWith(Suite.class) -@Suite.SuiteClasses({ // @formatter:off - FooServiceBasicPersistenceIntegrationTest.class - ,FooPaginationPersistenceIntegrationTest.class - ,FooServicePersistenceIntegrationTest.class - ,ParentServicePersistenceIntegrationTest.class - ,FooSortingPersistenceServiceTest.class -}) // @formatter:on -public class IntegrationTestSuite { - // -} diff --git a/spring-jpa/README.md b/spring-jpa/README.md index 11df42ac52..f974c6e22c 100644 --- a/spring-jpa/README.md +++ b/spring-jpa/README.md @@ -7,3 +7,6 @@ - [Spring 3 and JPA with Hibernate](http://www.baeldung.com/2011/12/13/the-persistence-layer-with-spring-3-1-and-jpa/) - [Transactions with Spring 3 and JPA](http://www.baeldung.com/2011/12/26/transaction-configuration-with-jpa-and-spring-3-1/) - [The DAO with JPA and Spring](http://www.baeldung.com/spring-dao-jpa) +- [JPA Pagination](http://www.baeldung.com/jpa-pagination) +- [Sorting with JPA](http://www.baeldung.com/jpa-sort) +- [Spring JPA – Multiple Databases](http://www.baeldung.com/spring-data-jpa-multiple-databases) diff --git a/spring-jpa/pom.xml b/spring-jpa/pom.xml index f065fe1a81..25dd960435 100644 --- a/spring-jpa/pom.xml +++ b/spring-jpa/pom.xml @@ -1,7 +1,7 @@ 4.0.0 - org.baeldung + com.baeldung spring-jpa 0.1-SNAPSHOT @@ -58,6 +58,11 @@ hibernate-validator ${hibernate-validator.version} + + javax.el + javax.el-api + 2.2.5 + @@ -85,7 +90,7 @@ junit - junit-dep + junit ${junit.version} test @@ -174,41 +179,40 @@ - 4.2.2.RELEASE - 4.0.2.RELEASE + 4.2.5.RELEASE 3.20.0-GA 4.3.11.Final - 5.1.36 - 1.7.2.RELEASE + 5.1.38 + 1.8.2.RELEASE - 1.7.12 + 1.7.13 1.1.3 - 5.2.1.Final + 5.2.2.Final - 18.0 + 19.0 3.4 1.3 - 4.11 + 4.12 1.10.19 4.4.1 4.5 - 2.4.1 + 2.9.0 - 3.3 - 2.18.1 + 3.5.1 + 2.19.1 2.7 - 1.4.15 + 1.4.18 diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/dao/FooDao.java b/spring-jpa/src/main/java/org/baeldung/persistence/dao/FooDao.java index becd8d5f67..77978c5cf2 100644 --- a/spring-jpa/src/main/java/org/baeldung/persistence/dao/FooDao.java +++ b/spring-jpa/src/main/java/org/baeldung/persistence/dao/FooDao.java @@ -4,7 +4,7 @@ import org.baeldung.persistence.model.Foo; import org.springframework.stereotype.Repository; @Repository -public class FooDao extends AbstractJpaDAOimplements IFooDao { +public class FooDao extends AbstractJpaDAO implements IFooDao { public FooDao() { super(); diff --git a/spring-jpa/src/main/resources/webSecurityConfig.xml b/spring-jpa/src/main/resources/webSecurityConfig.xml deleted file mode 100644 index 88af78dabc..0000000000 --- a/spring-jpa/src/main/resources/webSecurityConfig.xml +++ /dev/null @@ -1,36 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/spring-jpa/src/test/java/org/baeldung/persistence/service/FooPaginationPersistenceIntegrationTest.java b/spring-jpa/src/test/java/org/baeldung/persistence/service/FooPaginationPersistenceIntegrationTest.java index 73c36190f9..091bec0ba0 100644 --- a/spring-jpa/src/test/java/org/baeldung/persistence/service/FooPaginationPersistenceIntegrationTest.java +++ b/spring-jpa/src/test/java/org/baeldung/persistence/service/FooPaginationPersistenceIntegrationTest.java @@ -88,11 +88,12 @@ public class FooPaginationPersistenceIntegrationTest { public final void givenEntitiesExist_whenRetrievingPage_thenCorrect() { final int pageSize = 10; - final Query queryIds = entityManager.createQuery("Select f.id from Foo f order by f.lastName"); + final Query queryIds = entityManager.createQuery("Select f.id from Foo f order by f.name"); final List fooIds = queryIds.getResultList(); - final Query query = entityManager.createQuery("Select f from Foo e whet f.id in :ids"); + final Query query = entityManager.createQuery("Select f from Foo as f where f.id in :ids"); query.setParameter("ids", fooIds.subList(0, pageSize)); + final List fooList = query.getResultList(); // Then @@ -129,13 +130,15 @@ public class FooPaginationPersistenceIntegrationTest { final Root from = criteriaQuery.from(Foo.class); final CriteriaQuery select = criteriaQuery.select(from); - final TypedQuery typedQuery = entityManager.createQuery(select); + TypedQuery typedQuery; while (pageNumber < count.intValue()) { + typedQuery = entityManager.createQuery(select); typedQuery.setFirstResult(pageNumber - 1); typedQuery.setMaxResults(pageSize); System.out.println("Current page: " + typedQuery.getResultList()); pageNumber += pageSize; } + } // UTIL diff --git a/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java b/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java index 96b3235f64..4c57865f74 100644 --- a/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java +++ b/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java @@ -36,7 +36,7 @@ public class FooServicePersistenceIntegrationTest { @Test(expected = DataIntegrityViolationException.class) public final void whenInvalidEntityIsCreated_thenDataException() { - service.create(new Foo()); + service.create(new Foo(randomAlphabetic(2048))); } @Test(expected = DataIntegrityViolationException.class) @@ -53,7 +53,7 @@ public class FooServicePersistenceIntegrationTest { @Test(expected = DataAccessException.class) public final void temp_whenInvalidEntityIsCreated_thenDataException() { - service.create(new Foo()); + service.create(new Foo(randomAlphabetic(2048))); } @Test diff --git a/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java b/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingIntegrationTest.java similarity index 95% rename from spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java rename to spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingIntegrationTest.java index c12dfda50c..3c9f509da5 100644 --- a/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java +++ b/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingIntegrationTest.java @@ -15,7 +15,6 @@ import org.baeldung.persistence.model.Bar; import org.baeldung.persistence.model.Foo; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; @@ -23,14 +22,11 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { PersistenceJPAConfig.class }, loader = AnnotationConfigContextLoader.class) @SuppressWarnings("unchecked") -public class FooServiceSortingTests { +public class FooServiceSortingIntegrationTest { @PersistenceContext private EntityManager entityManager; - @Autowired - private FooService service; - // tests @Test @@ -69,7 +65,10 @@ public class FooServiceSortingTests { final Query barJoinQuery = entityManager.createQuery(jql); final List fooList = barJoinQuery.getResultList(); for (final Foo foo : fooList) { - System.out.println("Name:" + foo.getName() + "-------BarId:" + foo.getBar().getId()); + System.out.println("Name:" + foo.getName()); + if (foo.getBar() != null) { + System.out.print("-------BarId:" + foo.getBar().getId()); + } } } diff --git a/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingWitNullsManualTest.java b/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingWitNullsManualIntegrationTest.java similarity index 93% rename from spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingWitNullsManualTest.java rename to spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingWitNullsManualIntegrationTest.java index 986e4e4a7d..040eee1c73 100644 --- a/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingWitNullsManualTest.java +++ b/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingWitNullsManualIntegrationTest.java @@ -20,7 +20,7 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { PersistenceJPAConfig.class }, loader = AnnotationConfigContextLoader.class) -public class FooServiceSortingWitNullsManualTest { +public class FooServiceSortingWitNullsManualIntegrationTest { @PersistenceContext private EntityManager entityManager; @@ -30,6 +30,7 @@ public class FooServiceSortingWitNullsManualTest { // tests + @SuppressWarnings("unchecked") @Test public final void whenSortingByStringNullLast_thenLastNull() { service.create(new Foo()); @@ -44,6 +45,7 @@ public class FooServiceSortingWitNullsManualTest { } } + @SuppressWarnings("unchecked") @Test public final void whenSortingByStringNullFirst_thenFirstNull() { service.create(new Foo()); diff --git a/spring-jpa/src/test/java/org/baeldung/persistence/service/JPAMultipleDBTest.java b/spring-jpa/src/test/java/org/baeldung/persistence/service/JpaMultipleDBIntegrationTest.java similarity index 94% rename from spring-jpa/src/test/java/org/baeldung/persistence/service/JPAMultipleDBTest.java rename to spring-jpa/src/test/java/org/baeldung/persistence/service/JpaMultipleDBIntegrationTest.java index 4c6b02ec3d..e036a4f3c1 100644 --- a/spring-jpa/src/test/java/org/baeldung/persistence/service/JPAMultipleDBTest.java +++ b/spring-jpa/src/test/java/org/baeldung/persistence/service/JpaMultipleDBIntegrationTest.java @@ -15,19 +15,20 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { UserConfig.class, ProductConfig.class }) -@TransactionConfiguration -public class JPAMultipleDBTest { +public class JpaMultipleDBIntegrationTest { + @Autowired private UserRepository userRepository; @Autowired private ProductRepository productRepository; + // tests + @Test @Transactional("userTransactionManager") public void whenCreatingUser_thenCreated() { diff --git a/spring-jpa/src/test/java/org/baeldung/persistence/service/PersistenceTestSuite.java b/spring-jpa/src/test/java/org/baeldung/persistence/service/PersistenceTestSuite.java index f6c41258d4..aa2dfb5293 100644 --- a/spring-jpa/src/test/java/org/baeldung/persistence/service/PersistenceTestSuite.java +++ b/spring-jpa/src/test/java/org/baeldung/persistence/service/PersistenceTestSuite.java @@ -7,9 +7,9 @@ import org.junit.runners.Suite; @Suite.SuiteClasses({ // @formatter:off FooPaginationPersistenceIntegrationTest.class ,FooServicePersistenceIntegrationTest.class - ,FooServiceSortingTests.class - // manual only - // ,FooServiceSortingWitNullsManualTest.class + ,FooServiceSortingIntegrationTest.class + ,JpaMultipleDBIntegrationTest.class + ,FooServiceSortingWitNullsManualIntegrationTest.class }) // @formatter:on public class PersistenceTestSuite { // diff --git a/spring-katharsis/README.md b/spring-katharsis/README.md new file mode 100644 index 0000000000..cf2a001760 --- /dev/null +++ b/spring-katharsis/README.md @@ -0,0 +1,9 @@ +========= + +## 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-katharsis/pom.xml b/spring-katharsis/pom.xml index 554ff4e656..892aaf24f1 100644 --- a/spring-katharsis/pom.xml +++ b/spring-katharsis/pom.xml @@ -31,7 +31,7 @@ io.katharsis katharsis-servlet - 1.0.0 + ${katharsis.version} @@ -45,7 +45,7 @@ com.jayway.restassured rest-assured - 2.4.0 + ${restassured.version} test @@ -60,6 +60,8 @@ 1.8 + 1.0.0 + 2.4.0 diff --git a/spring-katharsis/src/main/java/org/baeldung/persistence/model/User.java b/spring-katharsis/src/main/java/org/baeldung/persistence/model/User.java index 58a92002c8..67e4c6ae1d 100644 --- a/spring-katharsis/src/main/java/org/baeldung/persistence/model/User.java +++ b/spring-katharsis/src/main/java/org/baeldung/persistence/model/User.java @@ -30,7 +30,7 @@ public class User { private String email; @ManyToMany(fetch = FetchType.EAGER) - @JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id")) + @JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id") , inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id") ) @JsonApiToMany @JsonApiIncludeByDefault private Set roles; diff --git a/spring-mockito/.classpath b/spring-mockito/.classpath new file mode 100644 index 0000000000..6d7587a819 --- /dev/null +++ b/spring-mockito/.classpath @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/rest-testing/src/test/resources/.gitignore b/spring-mockito/.gitignore similarity index 100% rename from rest-testing/src/test/resources/.gitignore rename to spring-mockito/.gitignore diff --git a/rest-testing/.project b/spring-mockito/.project similarity index 78% rename from rest-testing/.project rename to spring-mockito/.project index 1dc9ce93fe..5f0e9cacbc 100644 --- a/rest-testing/.project +++ b/spring-mockito/.project @@ -1,22 +1,17 @@ - rest-testing + spring-mockito - - org.eclipse.jdt.core.javabuilder - - - org.eclipse.wst.common.project.facet.core.builder - org.eclipse.wst.validation.validationbuilder + org.eclipse.jdt.core.javabuilder @@ -25,10 +20,14 @@ + + org.springframework.ide.eclipse.core.springbuilder + + + - org.eclipse.jem.workbench.JavaEMFNature - org.eclipse.wst.common.modulecore.ModuleCoreNature + org.springframework.ide.eclipse.core.springnature org.eclipse.jdt.core.javanature org.eclipse.m2e.core.maven2Nature org.eclipse.wst.common.project.facet.core.nature diff --git a/spring-mockito/README.md b/spring-mockito/README.md new file mode 100644 index 0000000000..3ced7161fa --- /dev/null +++ b/spring-mockito/README.md @@ -0,0 +1,7 @@ +========= + +## Mockito Mocks into Spring Beans + + +### Relevant Articles: +- [Injecting Mockito Mocks into Spring Beans](http://www.baeldung.com/injecting-mocks-in-spring) diff --git a/spring-mockito/pom.xml b/spring-mockito/pom.xml new file mode 100644 index 0000000000..3dcca7aab7 --- /dev/null +++ b/spring-mockito/pom.xml @@ -0,0 +1,51 @@ + + + 4.0.0 + + com.baeldung + spring-mockito + 0.0.1-SNAPSHOT + jar + + spring-mockito + Injecting Mockito Mocks into Spring Beans + + + org.springframework.boot + spring-boot-starter-parent + 1.3.3.RELEASE + + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-test + test + + + org.mockito + mockito-all + 1.10.19 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + UTF-8 + 1.8 + + + diff --git a/spring-mockito/src/main/java/com/baeldung/MocksApplication.java b/spring-mockito/src/main/java/com/baeldung/MocksApplication.java new file mode 100644 index 0000000000..32be4c6a0a --- /dev/null +++ b/spring-mockito/src/main/java/com/baeldung/MocksApplication.java @@ -0,0 +1,11 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class MocksApplication { + public static void main(String[] args) { + SpringApplication.run(MocksApplication.class, args); + } +} diff --git a/spring-mockito/src/main/java/com/baeldung/NameService.java b/spring-mockito/src/main/java/com/baeldung/NameService.java new file mode 100644 index 0000000000..bc328cfd8a --- /dev/null +++ b/spring-mockito/src/main/java/com/baeldung/NameService.java @@ -0,0 +1,10 @@ +package com.baeldung; + +import org.springframework.stereotype.Service; + +@Service +public class NameService { + public String getUserName(String id) { + return "Real user name"; + } +} diff --git a/spring-mockito/src/main/java/com/baeldung/UserService.java b/spring-mockito/src/main/java/com/baeldung/UserService.java new file mode 100644 index 0000000000..fe7e35b9f8 --- /dev/null +++ b/spring-mockito/src/main/java/com/baeldung/UserService.java @@ -0,0 +1,19 @@ +package com.baeldung; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class UserService { + + private NameService nameService; + + @Autowired + public UserService(NameService nameService) { + this.nameService = nameService; + } + + public String getUserName(String id) { + return nameService.getUserName(id); + } +} diff --git a/spring-mockito/src/main/resources/application.properties b/spring-mockito/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-mockito/src/test/java/com/baeldung/NameServiceTestConfiguration.java b/spring-mockito/src/test/java/com/baeldung/NameServiceTestConfiguration.java new file mode 100644 index 0000000000..2c63ff6203 --- /dev/null +++ b/spring-mockito/src/test/java/com/baeldung/NameServiceTestConfiguration.java @@ -0,0 +1,17 @@ +package com.baeldung; + +import org.mockito.Mockito; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.context.annotation.Profile; + +@Profile("test") +@Configuration +public class NameServiceTestConfiguration { + @Bean + @Primary + public NameService nameService() { + return Mockito.mock(NameService.class); + } +} diff --git a/spring-mockito/src/test/java/com/baeldung/UserServiceTest.java b/spring-mockito/src/test/java/com/baeldung/UserServiceTest.java new file mode 100644 index 0000000000..631a8634be --- /dev/null +++ b/spring-mockito/src/test/java/com/baeldung/UserServiceTest.java @@ -0,0 +1,31 @@ +package com.baeldung; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@ActiveProfiles("test") +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = MocksApplication.class) +public class UserServiceTest { + + @Autowired + private UserService userService; + + @Autowired + private NameService nameService; + + @Test + public void whenUserIdIsProvided_thenRetrievedNameIsCorrect() { + Mockito.when(nameService.getUserName("SomeId")).thenReturn("Mock user name"); + + String testName = userService.getUserName("SomeId"); + + Assert.assertEquals("Mock user name", testName); + } +} \ No newline at end of file diff --git a/spring-mvc-java/.classpath b/spring-mvc-java/.classpath index 6b533711d3..a642d37ceb 100644 --- a/spring-mvc-java/.classpath +++ b/spring-mvc-java/.classpath @@ -1,37 +1,37 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-mvc-java/README.md b/spring-mvc-java/README.md index bf76c7e1d4..951d80033e 100644 --- a/spring-mvc-java/README.md +++ b/spring-mvc-java/README.md @@ -2,5 +2,11 @@ ## 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) +- [Introduction to Pointcut Expressions in Spring](http://www.baeldung.com/spring-aop-pointcut-tutorial) +- [Introduction to Advice Types in Spring](http://www.baeldung.com/spring-aop-advice-tutorial) +- [A Guide to the ViewResolver in Spring MVC](http://www.baeldung.com/spring-mvc-view-resolver-tutorial) diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index 772bbbb219..33d6306c5a 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -1,16 +1,12 @@ 4.0.0 - org.baeldung + com.baeldung spring-mvc-java 0.1-SNAPSHOT - spring-mvc-java war - - - org.springframework spring-web @@ -21,64 +17,82 @@ spring-webmvc ${org.springframework.version} - + + org.springframework + spring-websocket + ${org.springframework.version} + + + org.springframework + spring-messaging + ${org.springframework.version} + + + + com.fasterxml.jackson.core + jackson-core + 2.7.3 + + + com.fasterxml.jackson.core + jackson-databind + 2.7.3 + + - javax.servlet javax.servlet-api 3.0.1 provided - javax.servlet jstl 1.2 runtime - org.springframework spring-aop ${org.springframework.version} - org.aspectj aspectjrt ${aspectj.version} - org.aspectj aspectjweaver ${aspectj.version} - org.slf4j slf4j-api ${org.slf4j.version} - org.slf4j slf4j-log4j12 ${org.slf4j.version} - + + + commons-fileupload + commons-fileupload + 1.3.1 + + - junit - junit-dep + junit ${junit.version} test - org.hamcrest hamcrest-core @@ -91,21 +105,42 @@ ${org.hamcrest.version} test - org.mockito mockito-core ${mockito.version} test - org.springframework spring-test ${org.springframework.version} test - + + + org.thymeleaf + thymeleaf-spring4 + ${thymeleaf.version} + + + org.thymeleaf + thymeleaf + ${thymeleaf.version} + + + + + com.fasterxml.jackson.core + jackson-core + 2.1.2 + + + com.fasterxml.jackson.core + jackson-databind + 2.1.2 + + @@ -118,7 +153,6 @@ - org.apache.maven.plugins maven-compiler-plugin @@ -129,6 +163,11 @@ + + maven-resources-plugin + 2.7 + + org.apache.maven.plugins maven-war-plugin @@ -137,7 +176,6 @@ false - org.apache.maven.plugins maven-surefire-plugin @@ -151,13 +189,11 @@ - org.codehaus.cargo cargo-maven2-plugin ${cargo-maven2-plugin.version} - true jetty8x embedded @@ -172,50 +208,41 @@ - - - + - 4.2.2.RELEASE - 4.0.2.RELEASE - + 4.2.5.RELEASE + 4.0.4.RELEASE + 2.1.4.RELEASE 4.3.11.Final - 5.1.36 - + 5.1.38 - 1.7.12 + 1.7.13 1.1.3 - - 5.2.1.Final - + 5.2.2.Final - 18.0 + 19.0 3.4 - 1.3 - 4.11 + 4.12 1.10.19 - 4.4.1 4.5 - - 2.4.1 - + 2.9.0 - 3.3 + 3.5.1 2.6 - 2.18.1 + 2.19.1 2.7 - 1.4.15 - + 1.4.18 + 1.8.7 - + \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/org/baeldung/aop/LoggingAspect.java b/spring-mvc-java/src/main/java/com/baeldung/aop/LoggingAspect.java similarity index 82% rename from spring-mvc-java/src/main/java/org/baeldung/aop/LoggingAspect.java rename to spring-mvc-java/src/main/java/com/baeldung/aop/LoggingAspect.java index 72ac610abd..7ae37404be 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/aop/LoggingAspect.java +++ b/spring-mvc-java/src/main/java/com/baeldung/aop/LoggingAspect.java @@ -1,4 +1,4 @@ -package org.baeldung.aop; +package com.baeldung.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; @@ -24,13 +24,16 @@ public class LoggingAspect { }; @Pointcut("@target(org.springframework.stereotype.Repository)") - public void repositoryMethods() {} + public void repositoryMethods() { + } - @Pointcut("@annotation(org.baeldung.aop.annotations.Loggable)") - public void loggableMethods() {} + @Pointcut("@annotation(com.baeldung.aop.annotations.Loggable)") + public void loggableMethods() { + } - @Pointcut("@args(org.baeldung.aop.annotations.Entity)") - public void methodsAcceptingEntities() {} + @Pointcut("@args(com.baeldung.aop.annotations.Entity)") + public void methodsAcceptingEntities() { + } @Before("repositoryMethods()") public void logMethodCall(JoinPoint jp) { diff --git a/spring-mvc-java/src/main/java/org/baeldung/aop/PerformanceAspect.java b/spring-mvc-java/src/main/java/com/baeldung/aop/PerformanceAspect.java similarity index 92% rename from spring-mvc-java/src/main/java/org/baeldung/aop/PerformanceAspect.java rename to spring-mvc-java/src/main/java/com/baeldung/aop/PerformanceAspect.java index 57f5bc5edd..1f2076adff 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/aop/PerformanceAspect.java +++ b/spring-mvc-java/src/main/java/com/baeldung/aop/PerformanceAspect.java @@ -1,4 +1,4 @@ -package org.baeldung.aop; +package com.baeldung.aop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; @@ -16,7 +16,8 @@ public class PerformanceAspect { private static Logger logger = Logger.getLogger(PerformanceAspect.class.getName()); @Pointcut("within(@org.springframework.stereotype.Repository *)") - public void repositoryClassMethods() {} + public void repositoryClassMethods() { + } @Around("repositoryClassMethods()") public Object measureMethodExecutionTime(ProceedingJoinPoint pjp) throws Throwable { diff --git a/spring-mvc-java/src/main/java/org/baeldung/aop/PublishingAspect.java b/spring-mvc-java/src/main/java/com/baeldung/aop/PublishingAspect.java similarity index 82% rename from spring-mvc-java/src/main/java/org/baeldung/aop/PublishingAspect.java rename to spring-mvc-java/src/main/java/com/baeldung/aop/PublishingAspect.java index 20a10f4f6a..7791c63e7b 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/aop/PublishingAspect.java +++ b/spring-mvc-java/src/main/java/com/baeldung/aop/PublishingAspect.java @@ -1,10 +1,10 @@ -package org.baeldung.aop; +package com.baeldung.aop; +import com.baeldung.events.FooCreationEvent; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; -import org.baeldung.events.FooCreationEvent; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Component; @@ -21,13 +21,16 @@ public class PublishingAspect { } @Pointcut("@target(org.springframework.stereotype.Repository)") - public void repositoryMethods() {} + public void repositoryMethods() { + } @Pointcut("execution(* *..create*(Long,..))") - public void firstLongParamMethods() {} + public void firstLongParamMethods() { + } @Pointcut("repositoryMethods() && firstLongParamMethods()") - public void entityCreationMethods() {} + public void entityCreationMethods() { + } @AfterReturning(value = "entityCreationMethods()", returning = "entity") public void logMethodCall(JoinPoint jp, Object entity) throws Throwable { diff --git a/spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Entity.java b/spring-mvc-java/src/main/java/com/baeldung/aop/annotations/Entity.java similarity index 86% rename from spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Entity.java rename to spring-mvc-java/src/main/java/com/baeldung/aop/annotations/Entity.java index f964c3979e..61d91b0777 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Entity.java +++ b/spring-mvc-java/src/main/java/com/baeldung/aop/annotations/Entity.java @@ -1,4 +1,4 @@ -package org.baeldung.aop.annotations; +package com.baeldung.aop.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Loggable.java b/spring-mvc-java/src/main/java/com/baeldung/aop/annotations/Loggable.java similarity index 87% rename from spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Loggable.java rename to spring-mvc-java/src/main/java/com/baeldung/aop/annotations/Loggable.java index ef2863957f..92aa950e58 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/aop/annotations/Loggable.java +++ b/spring-mvc-java/src/main/java/com/baeldung/aop/annotations/Loggable.java @@ -1,4 +1,4 @@ -package org.baeldung.aop.annotations; +package com.baeldung.aop.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/spring-mvc-java/src/main/java/org/baeldung/dao/FooDao.java b/spring-mvc-java/src/main/java/com/baeldung/dao/FooDao.java similarity index 75% rename from spring-mvc-java/src/main/java/org/baeldung/dao/FooDao.java rename to spring-mvc-java/src/main/java/com/baeldung/dao/FooDao.java index f204440b2d..1d28b082ec 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/dao/FooDao.java +++ b/spring-mvc-java/src/main/java/com/baeldung/dao/FooDao.java @@ -1,7 +1,7 @@ -package org.baeldung.dao; +package com.baeldung.dao; -import org.baeldung.aop.annotations.Loggable; -import org.baeldung.model.Foo; +import com.baeldung.aop.annotations.Loggable; +import com.baeldung.model.Foo; import org.springframework.stereotype.Repository; @Repository diff --git a/spring-mvc-java/src/main/java/com/baeldung/dialect/CustomDialect.java b/spring-mvc-java/src/main/java/com/baeldung/dialect/CustomDialect.java new file mode 100644 index 0000000000..0c6a7c3ae0 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/dialect/CustomDialect.java @@ -0,0 +1,24 @@ +package com.baeldung.dialect; + +import java.util.HashSet; +import java.util.Set; + +import com.baeldung.processor.NameProcessor; +import org.thymeleaf.dialect.AbstractDialect; +import org.thymeleaf.processor.IProcessor; + +public class CustomDialect extends AbstractDialect { + + @Override + public String getPrefix() { + return "custom"; + } + + @Override + public Set getProcessors() { + final Set processors = new HashSet(); + processors.add(new NameProcessor()); + return processors; + } + +} diff --git a/spring-mvc-java/src/main/java/org/baeldung/events/FooCreationEvent.java b/spring-mvc-java/src/main/java/com/baeldung/events/FooCreationEvent.java similarity index 86% rename from spring-mvc-java/src/main/java/org/baeldung/events/FooCreationEvent.java rename to spring-mvc-java/src/main/java/com/baeldung/events/FooCreationEvent.java index af11f3a4be..5ea4b46c04 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/events/FooCreationEvent.java +++ b/spring-mvc-java/src/main/java/com/baeldung/events/FooCreationEvent.java @@ -1,4 +1,4 @@ -package org.baeldung.events; +package com.baeldung.events; import org.springframework.context.ApplicationEvent; diff --git a/spring-mvc-java/src/main/java/org/baeldung/events/FooCreationEventListener.java b/spring-mvc-java/src/main/java/com/baeldung/events/FooCreationEventListener.java similarity index 94% rename from spring-mvc-java/src/main/java/org/baeldung/events/FooCreationEventListener.java rename to spring-mvc-java/src/main/java/com/baeldung/events/FooCreationEventListener.java index 35dcfd2bc3..c0aa744bc1 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/events/FooCreationEventListener.java +++ b/spring-mvc-java/src/main/java/com/baeldung/events/FooCreationEventListener.java @@ -1,4 +1,4 @@ -package org.baeldung.events; +package com.baeldung.events; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; diff --git a/spring-mvc-java/src/main/java/com/baeldung/model/Company.java b/spring-mvc-java/src/main/java/com/baeldung/model/Company.java new file mode 100644 index 0000000000..558507268a --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/model/Company.java @@ -0,0 +1,38 @@ +package com.baeldung.model; + +public class Company { + + private long id; + private String name; + + public Company() { + super(); + } + + public Company(final long id, final String name) { + this.id = id; + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } + + @Override + public String toString() { + return "Company [id=" + id + ", name=" + name + "]"; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/model/Employee.java b/spring-mvc-java/src/main/java/com/baeldung/model/Employee.java new file mode 100644 index 0000000000..fb0a452219 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/model/Employee.java @@ -0,0 +1,61 @@ +package com.baeldung.model; + +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +public class Employee { + + private long id; + private String name; + private String contactNumber; + private String workingArea; + + public Employee() { + super(); + } + + public Employee(final long id, final String name, final String contactNumber, final String workingArea) { + this.id = id; + this.name = name; + this.contactNumber = contactNumber; + this.workingArea = workingArea; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } + + public String getContactNumber() { + return contactNumber; + } + + public void setContactNumber(final String contactNumber) { + this.contactNumber = contactNumber; + } + + public String getWorkingArea() { + return workingArea; + } + + public void setWorkingArea(final String workingArea) { + this.workingArea = workingArea; + } + + @Override + public String toString() { + return "Employee [id=" + id + ", name=" + name + ", contactNumber=" + contactNumber + ", workingArea=" + workingArea + "]"; + } + +} diff --git a/spring-mvc-java/src/main/java/org/baeldung/model/Foo.java b/spring-mvc-java/src/main/java/com/baeldung/model/Foo.java similarity index 54% rename from spring-mvc-java/src/main/java/org/baeldung/model/Foo.java rename to spring-mvc-java/src/main/java/com/baeldung/model/Foo.java index 0b1a553afc..01f5f43f60 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/model/Foo.java +++ b/spring-mvc-java/src/main/java/com/baeldung/model/Foo.java @@ -1,6 +1,6 @@ -package org.baeldung.model; +package com.baeldung.model; -import org.baeldung.aop.annotations.Entity; +import com.baeldung.aop.annotations.Entity; @Entity public class Foo { @@ -14,9 +14,6 @@ public class Foo { @Override public String toString() { - return "Foo{" + - "id=" + id + - ", name='" + name + '\'' + - '}'; + return "Foo{" + "id=" + id + ", name='" + name + '\'' + '}'; } } diff --git a/spring-mvc-java/src/main/java/com/baeldung/model/Message.java b/spring-mvc-java/src/main/java/com/baeldung/model/Message.java new file mode 100644 index 0000000000..c1f7f52215 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/model/Message.java @@ -0,0 +1,15 @@ +package com.baeldung.model; + +public class Message { + + private String from; + private String text; + + public String getText() { + return text; + } + + public String getFrom() { + return from; + } +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/model/OutputMessage.java b/spring-mvc-java/src/main/java/com/baeldung/model/OutputMessage.java new file mode 100644 index 0000000000..fc201ed016 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/model/OutputMessage.java @@ -0,0 +1,27 @@ +package com.baeldung.model; + +public class OutputMessage { + + private String from; + private String text; + private String time; + + public OutputMessage(final String from, final String text, final String time) { + + this.from = from; + this.text = text; + this.time = time; + } + + public String getText() { + return text; + } + + public String getTime() { + return time; + } + + public String getFrom() { + return from; + } +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/model/User.java b/spring-mvc-java/src/main/java/com/baeldung/model/User.java new file mode 100644 index 0000000000..dc4480c986 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/model/User.java @@ -0,0 +1,32 @@ +package com.baeldung.model; + +public class User { + private String firstname; + private String lastname; + private String emailId; + + public String getFirstname() { + return firstname; + } + + public void setFirstname(final String firstname) { + this.firstname = firstname; + } + + public String getLastname() { + return lastname; + } + + public void setLastname(final String lastname) { + this.lastname = lastname; + } + + public String getEmailId() { + return emailId; + } + + public void setEmailId(final String emailId) { + this.emailId = emailId; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/processor/NameProcessor.java b/spring-mvc-java/src/main/java/com/baeldung/processor/NameProcessor.java new file mode 100644 index 0000000000..9a7857198c --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/processor/NameProcessor.java @@ -0,0 +1,23 @@ +package com.baeldung.processor; + +import org.thymeleaf.Arguments; +import org.thymeleaf.dom.Element; +import org.thymeleaf.processor.attr.AbstractTextChildModifierAttrProcessor; + +public class NameProcessor extends AbstractTextChildModifierAttrProcessor { + + public NameProcessor() { + super("name"); + } + + @Override + protected String getText(final Arguments arguements, final Element elements, final String attributeName) { + return "Hello, " + elements.getAttributeValue(attributeName) + "!"; + } + + @Override + public int getPrecedence() { + return 1000; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ClientWebConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ClientWebConfig.java new file mode 100644 index 0000000000..1b30479685 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ClientWebConfig.java @@ -0,0 +1,93 @@ +package com.baeldung.spring.web.config; + +import java.util.HashSet; +import java.util.Set; + +import com.baeldung.dialect.CustomDialect; +import org.springframework.context.MessageSource; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Description; +import org.springframework.context.support.ResourceBundleMessageSource; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.InternalResourceViewResolver; +import org.springframework.web.servlet.view.JstlView; +import org.thymeleaf.dialect.IDialect; +import org.thymeleaf.spring4.SpringTemplateEngine; +import org.thymeleaf.spring4.view.ThymeleafViewResolver; +import org.thymeleaf.templateresolver.ServletContextTemplateResolver; + +@EnableWebMvc +@Configuration +public class ClientWebConfig extends WebMvcConfigurerAdapter { + + public ClientWebConfig() { + super(); + } + + // API + + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + super.addViewControllers(registry); + + registry.addViewController("/sample.html"); + } + + @Bean + public ViewResolver thymeleafViewResolver() { + final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); + viewResolver.setTemplateEngine(templateEngine()); + viewResolver.setOrder(1); + return viewResolver; + } + + @Bean + public ViewResolver viewResolver() { + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + bean.setViewClass(JstlView.class); + bean.setPrefix("/WEB-INF/view/"); + bean.setSuffix(".jsp"); + bean.setOrder(0); + return bean; + } + + @Bean + @Description("Thymeleaf template resolver serving HTML 5") + public ServletContextTemplateResolver templateResolver() { + final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); + templateResolver.setPrefix("/WEB-INF/templates/"); + templateResolver.setSuffix(".html"); + templateResolver.setTemplateMode("HTML5"); + return templateResolver; + } + + @Bean + @Description("Thymeleaf template engine with Spring integration") + public SpringTemplateEngine templateEngine() { + final SpringTemplateEngine templateEngine = new SpringTemplateEngine(); + templateEngine.setTemplateResolver(templateResolver()); + final Set dialects = new HashSet<>(); + dialects.add(new CustomDialect()); + templateEngine.setAdditionalDialects(dialects); + return templateEngine; + } + + @Bean + @Description("Spring message resolver") + public MessageSource messageSource() { + final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); + messageSource.setBasename("messages"); + return messageSource; + } + + @Override + public void addResourceHandlers(final ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); + } + +} \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ContentManagementWebConfig.java similarity index 60% rename from spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java rename to spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ContentManagementWebConfig.java index 945c1794fb..498105ded1 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ContentManagementWebConfig.java @@ -1,8 +1,10 @@ -package org.baeldung.spring.web.config; +package com.baeldung.spring.web.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.http.MediaType; import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @@ -11,29 +13,34 @@ import org.springframework.web.servlet.view.JstlView; @EnableWebMvc @Configuration -public class ClientWebConfig extends WebMvcConfigurerAdapter { +public class ContentManagementWebConfig extends WebMvcConfigurerAdapter { - public ClientWebConfig() { + public ContentManagementWebConfig() { super(); } // API + @Override + public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) { + configurer.favorPathExtension(false).favorParameter(true).parameterName("mediaType").ignoreAcceptHeader(true).useJaf(false).defaultContentType(MediaType.APPLICATION_JSON).mediaType("xml", MediaType.APPLICATION_XML).mediaType("json", + MediaType.APPLICATION_JSON); + } + @Override public void addViewControllers(final ViewControllerRegistry registry) { super.addViewControllers(registry); - registry.addViewController("/sample.html"); } @Bean public ViewResolver viewResolver() { final InternalResourceViewResolver bean = new InternalResourceViewResolver(); - bean.setViewClass(JstlView.class); bean.setPrefix("/WEB-INF/view/"); bean.setSuffix(".jsp"); - + bean.setOrder(0); return bean; } -} \ No newline at end of file + +} diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/MainWebAppInitializer.java similarity index 78% rename from spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java rename to spring-mvc-java/src/main/java/com/baeldung/spring/web/config/MainWebAppInitializer.java index 87502e2088..f428fc3223 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/MainWebAppInitializer.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/MainWebAppInitializer.java @@ -1,4 +1,4 @@ -package org.baeldung.spring.web.config; +package com.baeldung.spring.web.config; import java.util.Set; @@ -14,16 +14,18 @@ import org.springframework.web.servlet.DispatcherServlet; public class MainWebAppInitializer implements WebApplicationInitializer { + private static final String TMP_FOLDER = "/tmp"; + private static final int MAX_UPLOAD_SIZE = 5 * 1024 * 1024; // 5 MB + /** * Register and configure all Servlet container components necessary to power the web application. */ @Override public void onStartup(final ServletContext sc) throws ServletException { - System.out.println("MainWebAppInitializer.onStartup()"); // Create the 'root' Spring application context final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); - root.scan("org.baeldung.spring.web.config"); + root.scan("com.baeldung.spring.web.config"); // root.getEnvironment().setDefaultProfiles("embedded"); // Manages the lifecycle of the root application context @@ -32,6 +34,13 @@ public class MainWebAppInitializer implements WebApplicationInitializer { // Handles requests into the application final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext())); appServlet.setLoadOnStartup(1); + + // final MultipartConfigElement multipartConfigElement = new + // MultipartConfigElement(TMP_FOLDER, MAX_UPLOAD_SIZE, + // MAX_UPLOAD_SIZE * 2, MAX_UPLOAD_SIZE / 2); + // + // appServlet.setMultipartConfig(multipartConfigElement); + final Set mappingConflicts = appServlet.addMapping("/"); if (!mappingConflicts.isEmpty()) { throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278"); diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java new file mode 100644 index 0000000000..663b9cc4d2 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java @@ -0,0 +1,109 @@ +package com.baeldung.spring.web.config; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; +import org.springframework.http.MediaType; +import org.springframework.http.converter.ByteArrayHttpMessageConverter; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.web.multipart.commons.CommonsMultipartResolver; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.InternalResourceViewResolver; +import org.springframework.web.servlet.view.JstlView; +import org.springframework.web.servlet.view.ResourceBundleViewResolver; +import org.springframework.web.servlet.view.XmlViewResolver; +import org.springframework.web.util.UrlPathHelper; + +@Configuration +@EnableWebMvc +@ComponentScan("com.baeldung.web") +public class WebConfig extends WebMvcConfigurerAdapter { + + public WebConfig() { + super(); + } + + // @Bean + // public StandardServletMultipartResolver multipartResolver() { + // return new StandardServletMultipartResolver(); + // } + + @Bean(name = "multipartResolver") + public CommonsMultipartResolver multipartResolver() { + + final CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); + multipartResolver.setMaxUploadSize(100000); + + return multipartResolver; + } + + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + super.addViewControllers(registry); + registry.addViewController("/sample.html"); + } + + @Bean + public ViewResolver internalResourceViewResolver() { + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + bean.setViewClass(JstlView.class); + bean.setPrefix("/WEB-INF/view/"); + bean.setSuffix(".jsp"); + bean.setOrder(2); + return bean; + } + + @Bean + public ViewResolver xmlViewResolver() { + final XmlViewResolver bean = new XmlViewResolver(); + bean.setLocation(new ClassPathResource("views.xml")); + bean.setOrder(1); + return bean; + } + + @Bean + public ViewResolver resourceBundleViewResolver() { + final ResourceBundleViewResolver bean = new ResourceBundleViewResolver(); + bean.setBasename("views"); + bean.setOrder(0); + return bean; + } + + @Override + public void extendMessageConverters(final List> converters) { + converters.add(byteArrayHttpMessageConverter()); + } + + @Bean + public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() { + final ByteArrayHttpMessageConverter arrayHttpMessageConverter = new ByteArrayHttpMessageConverter(); + arrayHttpMessageConverter.setSupportedMediaTypes(getSupportedMediaTypes()); + + return arrayHttpMessageConverter; + } + + private List getSupportedMediaTypes() { + final List list = new ArrayList(); + list.add(MediaType.IMAGE_JPEG); + list.add(MediaType.IMAGE_PNG); + list.add(MediaType.APPLICATION_OCTET_STREAM); + + return list; + } + + @Override + public void configurePathMatch(final PathMatchConfigurer configurer) { + final UrlPathHelper urlPathHelper = new UrlPathHelper(); + urlPathHelper.setRemoveSemicolonContent(false); + + configurer.setUrlPathHelper(urlPathHelper); + } +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketConfig.java new file mode 100644 index 0000000000..6330041c60 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketConfig.java @@ -0,0 +1,24 @@ +package com.baeldung.spring.web.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.messaging.simp.config.MessageBrokerRegistry; +import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer; +import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; +import org.springframework.web.socket.config.annotation.StompEndpointRegistry; + +@Configuration +@EnableWebSocketMessageBroker +public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { + + @Override + public void configureMessageBroker(final MessageBrokerRegistry config) { + config.enableSimpleBroker("/topic"); + config.setApplicationDestinationPrefixes("/app"); + } + + @Override + public void registerStompEndpoints(final StompEndpointRegistry registry) { + registry.addEndpoint("/chat").withSockJS(); + } + +} \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/BeanA.java b/spring-mvc-java/src/main/java/com/baeldung/web/BeanA.java similarity index 89% rename from spring-mvc-java/src/main/java/org/baeldung/web/BeanA.java rename to spring-mvc-java/src/main/java/com/baeldung/web/BeanA.java index b6b6f49c16..79fac724f7 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/web/BeanA.java +++ b/spring-mvc-java/src/main/java/com/baeldung/web/BeanA.java @@ -1,4 +1,4 @@ -package org.baeldung.web; +package com.baeldung.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; diff --git a/spring-mvc-java/src/main/java/org/baeldung/web/BeanB.java b/spring-mvc-java/src/main/java/com/baeldung/web/BeanB.java similarity index 83% rename from spring-mvc-java/src/main/java/org/baeldung/web/BeanB.java rename to spring-mvc-java/src/main/java/com/baeldung/web/BeanB.java index 49e5af4ccb..05c9560a0c 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/web/BeanB.java +++ b/spring-mvc-java/src/main/java/com/baeldung/web/BeanB.java @@ -1,4 +1,4 @@ -package org.baeldung.web; +package com.baeldung.web; import org.springframework.stereotype.Component; diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/ChatController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/ChatController.java new file mode 100644 index 0000000000..f4bed1950b --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/ChatController.java @@ -0,0 +1,23 @@ +package com.baeldung.web.controller; + +import com.baeldung.model.Message; +import com.baeldung.model.OutputMessage; +import org.springframework.messaging.handler.annotation.MessageMapping; +import org.springframework.messaging.handler.annotation.SendTo; +import org.springframework.stereotype.Controller; + +import java.text.SimpleDateFormat; +import java.util.Date; + +@Controller +public class ChatController { + + @MessageMapping("/chat") + @SendTo("/topic/messages") + public OutputMessage send(final Message message) throws Exception { + + final String time = new SimpleDateFormat("HH:mm").format(new Date()); + return new OutputMessage(message.getFrom(), message.getText(), time); + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/CompanyController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/CompanyController.java new file mode 100644 index 0000000000..8228eafd5c --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/CompanyController.java @@ -0,0 +1,56 @@ +package com.baeldung.web.controller; + +import com.baeldung.model.Company; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; + +import java.util.HashMap; +import java.util.Map; + +@Controller +public class CompanyController { + + Map companyMap = new HashMap<>(); + + @RequestMapping(value = "/company", method = RequestMethod.GET) + public ModelAndView showForm() { + return new ModelAndView("companyHome", "company", new Company()); + } + + @RequestMapping(value = "/company/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET) + public @ResponseBody Company getCompanyById(@PathVariable final long Id) { + return companyMap.get(Id); + } + + @RequestMapping(value = "/addCompany", method = RequestMethod.POST) + public String submit(@ModelAttribute("company") final Company company, final BindingResult result, final ModelMap model) { + if (result.hasErrors()) { + return "error"; + } + model.addAttribute("name", company.getName()); + model.addAttribute("id", company.getId()); + + companyMap.put(company.getId(), company); + + return "companyView"; + } + + @RequestMapping(value = "/companyEmployee/{company}/employeeData/{employee}", method = RequestMethod.GET) + @ResponseBody + public ResponseEntity> getEmployeeDataFromCompany(@MatrixVariable(pathVar = "employee") final Map matrixVars) { + return new ResponseEntity<>(matrixVars, HttpStatus.OK); + } + + @RequestMapping(value = "/companyData/{company}/employeeData/{employee}", method = RequestMethod.GET) + @ResponseBody + public ResponseEntity> getCompanyName(@MatrixVariable(value = "name", pathVar = "company") final String name) { + final Map result = new HashMap(); + result.put("name", name); + return new ResponseEntity<>(result, HttpStatus.OK); + } +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/EmployeeController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/EmployeeController.java new file mode 100644 index 0000000000..fd5fa6bc27 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/EmployeeController.java @@ -0,0 +1,110 @@ +package com.baeldung.web.controller; + +import com.baeldung.model.Employee; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.ui.ModelMap; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; + +import java.util.*; + +@SessionAttributes("employees") +@Controller +@ControllerAdvice +public class EmployeeController { + + Map employeeMap = new HashMap<>(); + + @ModelAttribute("employees") + public void initEmployees() { + employeeMap.put(1L, new Employee(1L, "John", "223334411", "rh")); + employeeMap.put(2L, new Employee(2L, "Peter", "22001543", "informatics")); + employeeMap.put(3L, new Employee(3L, "Mike", "223334411", "admin")); + } + + @RequestMapping(value = "/employee", method = RequestMethod.GET) + public ModelAndView showForm() { + return new ModelAndView("employeeHome", "employee", new Employee()); + } + + @RequestMapping(value = "/employee/{Id}", produces = {"application/json", "application/xml"}, method = RequestMethod.GET) + public + @ResponseBody + Employee getEmployeeById(@PathVariable final long Id) { + return employeeMap.get(Id); + } + + @RequestMapping(value = "/addEmployee", method = RequestMethod.POST) + public String submit(@ModelAttribute("employee") final Employee employee, final BindingResult result, final ModelMap model) { + if (result.hasErrors()) { + return "error"; + } + model.addAttribute("name", employee.getName()); + model.addAttribute("contactNumber", employee.getContactNumber()); + model.addAttribute("workingArea", employee.getWorkingArea()); + model.addAttribute("id", employee.getId()); + + employeeMap.put(employee.getId(), employee); + + return "employeeView"; + } + + @ModelAttribute + public void addAttributes(final Model model) { + model.addAttribute("msg", "Welcome to the Netherlands!"); + } + + @RequestMapping(value = "/employees/{name}", method = RequestMethod.GET) + @ResponseBody + public ResponseEntity> getEmployeeByNameAndBeginContactNumber(@PathVariable final String name, @MatrixVariable final String beginContactNumber) { + final List employeesList = new ArrayList(); + for (final Map.Entry employeeEntry : employeeMap.entrySet()) { + final Employee employee = employeeEntry.getValue(); + if (employee.getName().equalsIgnoreCase(name) && employee.getContactNumber().startsWith(beginContactNumber)) { + employeesList.add(employee); + } + } + return new ResponseEntity<>(employeesList, HttpStatus.OK); + } + + @RequestMapping(value = "/employeesContacts/{contactNumber}", method = RequestMethod.GET) + @ResponseBody + public ResponseEntity> getEmployeeBycontactNumber(@MatrixVariable(required = true) final String contactNumber) { + final List employeesList = new ArrayList(); + for (final Map.Entry employeeEntry : employeeMap.entrySet()) { + final Employee employee = employeeEntry.getValue(); + if (employee.getContactNumber().equalsIgnoreCase(contactNumber)) { + employeesList.add(employee); + } + } + return new ResponseEntity<>(employeesList, HttpStatus.OK); + } + + @RequestMapping(value = "employeeData/{employee}", method = RequestMethod.GET) + @ResponseBody + public ResponseEntity> getEmployeeData(@MatrixVariable final Map matrixVars) { + return new ResponseEntity<>(matrixVars, HttpStatus.OK); + } + + @RequestMapping(value = "employeeArea/{workingArea}", method = RequestMethod.GET) + @ResponseBody + public ResponseEntity> getEmployeeByWorkingArea(@MatrixVariable final Map> matrixVars) { + final List employeesList = new ArrayList(); + final LinkedList workingArea = matrixVars.get("workingArea"); + for (final Map.Entry employeeEntry : employeeMap.entrySet()) { + final Employee employee = employeeEntry.getValue(); + for (final String area : workingArea) { + if (employee.getWorkingArea().equalsIgnoreCase(area)) { + employeesList.add(employee); + break; + } + } + } + return new ResponseEntity<>(employeesList, HttpStatus.OK); + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/FileUploadController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/FileUploadController.java new file mode 100644 index 0000000000..61bccb21aa --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/FileUploadController.java @@ -0,0 +1,32 @@ +package com.baeldung.web.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.multipart.MultipartFile; + +@Controller +public class FileUploadController { + + @RequestMapping(value = "/fileUpload", method = RequestMethod.GET) + public String displayForm() { + + return "fileUploadForm"; + } + + @RequestMapping(value = "/uploadFile", method = RequestMethod.POST) + public String submit(@RequestParam("file") final MultipartFile file, final ModelMap modelMap) { + + modelMap.addAttribute("file", file); + return "fileUploadView"; + } + + @RequestMapping(value = "/uploadMultiFile", method = RequestMethod.POST) + public String submit(@RequestParam("files") final MultipartFile[] files, final ModelMap modelMap) { + + modelMap.addAttribute("files", files); + return "fileUploadView"; + } +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/ImageController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/ImageController.java new file mode 100644 index 0000000000..5a8a491989 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/ImageController.java @@ -0,0 +1,27 @@ +package com.baeldung.web.controller; + +import java.io.IOException; +import java.io.InputStream; + +import javax.servlet.ServletContext; + +import org.apache.commons.io.IOUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class ImageController { + + @Autowired + ServletContext servletContext; + + @RequestMapping(value = "/image-byte-array", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE) + public @ResponseBody byte[] getImageAsByteArray() throws IOException { + final InputStream in = servletContext.getResourceAsStream("/WEB-INF/images/image-example.jpg"); + return IOUtils.toByteArray(in); + } +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/UserController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/UserController.java new file mode 100644 index 0000000000..fda159f204 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/UserController.java @@ -0,0 +1,31 @@ +package com.baeldung.web.controller; + +import com.baeldung.model.User; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +@Controller +@RequestMapping("/") +public class UserController { + + @RequestMapping(value = "/", method = RequestMethod.GET) + public String showForm(final Model model) { + final User user = new User(); + user.setFirstname("John"); + user.setLastname("Roy"); + user.setEmailId("John.Roy@gmail.com"); + model.addAttribute("user", user); + return "index"; + } + + @RequestMapping(value = "/processForm", method = RequestMethod.POST) + public String processForm(@ModelAttribute(value = "user") final User user, final Model model) { + // Insert User into DB + model.addAttribute("name", user.getFirstname() + " " + user.getLastname()); + return "hello"; + } + +} diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java deleted file mode 100644 index d60bcfe127..0000000000 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.baeldung.spring.web.config; - -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; - -@Configuration -@EnableWebMvc -@ComponentScan("org.baeldung.web") -public class WebConfig extends WebMvcConfigurerAdapter { - - public WebConfig() { - super(); - } - - // API - -} \ No newline at end of file diff --git a/spring-mvc-java/src/main/resources/org/baeldung/aop/beans.xml b/spring-mvc-java/src/main/resources/com/baeldung/aop/beans.xml similarity index 86% rename from spring-mvc-java/src/main/resources/org/baeldung/aop/beans.xml rename to spring-mvc-java/src/main/resources/com/baeldung/aop/beans.xml index 17c63e39e4..e6aa9d77c4 100644 --- a/spring-mvc-java/src/main/resources/org/baeldung/aop/beans.xml +++ b/spring-mvc-java/src/main/resources/com/baeldung/aop/beans.xml @@ -7,8 +7,8 @@ http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> - - + + diff --git a/spring-mvc-java/src/main/resources/messages_en.properties b/spring-mvc-java/src/main/resources/messages_en.properties new file mode 100644 index 0000000000..549024372b --- /dev/null +++ b/spring-mvc-java/src/main/resources/messages_en.properties @@ -0,0 +1 @@ +welcome.text=Hello \ No newline at end of file diff --git a/spring-mvc-java/src/main/resources/views.properties b/spring-mvc-java/src/main/resources/views.properties new file mode 100644 index 0000000000..95687cb62a --- /dev/null +++ b/spring-mvc-java/src/main/resources/views.properties @@ -0,0 +1,3 @@ +sample.(class)=org.springframework.web.servlet.view.JstlView +sample.url=/WEB-INF/view/sample.jsp + diff --git a/spring-mvc-java/src/main/resources/views.xml b/spring-mvc-java/src/main/resources/views.xml new file mode 100644 index 0000000000..83bca5293d --- /dev/null +++ b/spring-mvc-java/src/main/resources/views.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/images/image-example.jpg b/spring-mvc-java/src/main/webapp/WEB-INF/images/image-example.jpg new file mode 100644 index 0000000000..219abe530f Binary files /dev/null and b/spring-mvc-java/src/main/webapp/WEB-INF/images/image-example.jpg differ diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/templates/footer.html b/spring-mvc-java/src/main/webapp/WEB-INF/templates/footer.html new file mode 100644 index 0000000000..f72d553303 --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/templates/footer.html @@ -0,0 +1,6 @@ + + + +
© 2013 Footer
+ + \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/templates/hello.html b/spring-mvc-java/src/main/webapp/WEB-INF/templates/hello.html new file mode 100644 index 0000000000..1eddd85166 --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/templates/hello.html @@ -0,0 +1,14 @@ + + + + + + + Hi + John ! + Test +
© 2013 The Static + Templates
+ + \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/templates/index.html b/spring-mvc-java/src/main/webapp/WEB-INF/templates/index.html new file mode 100644 index 0000000000..9b4159c193 --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/templates/index.html @@ -0,0 +1,36 @@ + + + + +Thymeleaf Spring Example + + + Hello John! +

+
Please confirm your details
+

+
+ + + + + + + + + + + + + + + + +
Firstname :
Lastname :
EmailId :
+
+ + \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/companyHome.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/companyHome.jsp new file mode 100644 index 0000000000..38ef8d12ee --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/companyHome.jsp @@ -0,0 +1,31 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1" %> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> + + + + Form Example - Register a Company + + +

Welcome, Enter The Company Details

+ + + + + + + + + + + + + + +
Name
Id
+
+ + + + \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/companyView.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/companyView.jsp new file mode 100644 index 0000000000..8f34059b0a --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/companyView.jsp @@ -0,0 +1,20 @@ +<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> + + + Spring MVC Form Handling + + + +

Submitted Company Information

+ + + + + + + + + +
Name :${name}
ID :${id}
+ + \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/employeeHome.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/employeeHome.jsp new file mode 100644 index 0000000000..8a32fd12b6 --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/employeeHome.jsp @@ -0,0 +1,37 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> + + + +Form Example - Register an Employee + + +

Welcome, Enter The Employee Details

+ + + + + + + + + + + + + + + + + + + + + + +
Name
Id
Contact Number
Working Area
+
+ + + + \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/employeeView.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/employeeView.jsp new file mode 100644 index 0000000000..627a9d9ddb --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/employeeView.jsp @@ -0,0 +1,29 @@ +<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> + + +Spring MVC Form Handling + + + +

Submitted Employee Information

+

${msg}

+ + + + + + + + + + + + + + + + + +
Name :${name}
ID :${id}
Contact Number :${contactNumber}
Working Area :${workingArea}
+ + \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp new file mode 100644 index 0000000000..1414b824ff --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp @@ -0,0 +1,55 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> + + + + File Upload Example + + + + +

Enter The File to Upload (Single file)

+ + + + + + + + + + + +
Select a file to upload
+ +
+ +
+ +

Enter The Files to Upload (Multiple files)

+ + + + + + + + + + + + + + + + + + + +
Select a file to upload
Select a file to upload
Select a file to upload
+ +
+ + + + \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp new file mode 100644 index 0000000000..d6f748c6af --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp @@ -0,0 +1,36 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + + Spring MVC File Upload + + + +

Submitted File (Single)

+ + + + + + + + + +
OriginalFileName :${file.originalFilename}
Type :${file.contentType}
+
+ +

Submitted Files (Multiple)

+ + + + + + + + + + + +
OriginalFileName :${file.originalFilename}
Type :${file.contentType}
+ + \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/web_old.xml b/spring-mvc-java/src/main/webapp/WEB-INF/web_old.xml index 016369ad27..c8b38fae30 100644 --- a/spring-mvc-java/src/main/webapp/WEB-INF/web_old.xml +++ b/spring-mvc-java/src/main/webapp/WEB-INF/web_old.xml @@ -16,7 +16,7 @@ contextConfigLocation - org.baeldung.spring.web.config + com.baeldung.spring.web.config diff --git a/spring-mvc-java/src/main/webapp/resources/chat.html b/spring-mvc-java/src/main/webapp/resources/chat.html new file mode 100644 index 0000000000..92a54534fd --- /dev/null +++ b/spring-mvc-java/src/main/webapp/resources/chat.html @@ -0,0 +1,88 @@ + + + Chat WebSocket + + + + + + + + + + +
+ + +
+ +
+
+
+ + +
+
+
+ + +

+
+
+ + + \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/resources/js/sockjs-0.3.4.js b/spring-mvc-java/src/main/webapp/resources/js/sockjs-0.3.4.js new file mode 100644 index 0000000000..9f5b8f3deb --- /dev/null +++ b/spring-mvc-java/src/main/webapp/resources/js/sockjs-0.3.4.js @@ -0,0 +1,2378 @@ +/* SockJS client, version 0.3.4, http://sockjs.org, MIT License + +Copyright (c) 2011-2012 VMware, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +// JSON2 by Douglas Crockford (minified). +var JSON;JSON||(JSON={}),function(){function str(a,b){var c,d,e,f,g=gap,h,i=b[a];i&&typeof i=="object"&&typeof i.toJSON=="function"&&(i=i.toJSON(a)),typeof rep=="function"&&(i=rep.call(b,a,i));switch(typeof i){case"string":return quote(i);case"number":return isFinite(i)?String(i):"null";case"boolean":case"null":return String(i);case"object":if(!i)return"null";gap+=indent,h=[];if(Object.prototype.toString.apply(i)==="[object Array]"){f=i.length;for(c=0;c 1) { + this._listeners[eventType] = arr.slice(0, idx).concat( arr.slice(idx+1) ); + } else { + delete this._listeners[eventType]; + } + return; + } + return; +}; + +REventTarget.prototype.dispatchEvent = function (event) { + var t = event.type; + var args = Array.prototype.slice.call(arguments, 0); + if (this['on'+t]) { + this['on'+t].apply(this, args); + } + if (this._listeners && t in this._listeners) { + for(var i=0; i < this._listeners[t].length; i++) { + this._listeners[t][i].apply(this, args); + } + } +}; +// [*] End of lib/reventtarget.js + + +// [*] Including lib/simpleevent.js +/* + * ***** BEGIN LICENSE BLOCK ***** + * Copyright (c) 2011-2012 VMware, Inc. + * + * For the license see COPYING. + * ***** END LICENSE BLOCK ***** + */ + +var SimpleEvent = function(type, obj) { + this.type = type; + if (typeof obj !== 'undefined') { + for(var k in obj) { + if (!obj.hasOwnProperty(k)) continue; + this[k] = obj[k]; + } + } +}; + +SimpleEvent.prototype.toString = function() { + var r = []; + for(var k in this) { + if (!this.hasOwnProperty(k)) continue; + var v = this[k]; + if (typeof v === 'function') v = '[function]'; + r.push(k + '=' + v); + } + return 'SimpleEvent(' + r.join(', ') + ')'; +}; +// [*] End of lib/simpleevent.js + + +// [*] Including lib/eventemitter.js +/* + * ***** BEGIN LICENSE BLOCK ***** + * Copyright (c) 2011-2012 VMware, Inc. + * + * For the license see COPYING. + * ***** END LICENSE BLOCK ***** + */ + +var EventEmitter = function(events) { + var that = this; + that._events = events || []; + that._listeners = {}; +}; +EventEmitter.prototype.emit = function(type) { + var that = this; + that._verifyType(type); + if (that._nuked) return; + + var args = Array.prototype.slice.call(arguments, 1); + if (that['on'+type]) { + that['on'+type].apply(that, args); + } + if (type in that._listeners) { + for(var i = 0; i < that._listeners[type].length; i++) { + that._listeners[type][i].apply(that, args); + } + } +}; + +EventEmitter.prototype.on = function(type, callback) { + var that = this; + that._verifyType(type); + if (that._nuked) return; + + if (!(type in that._listeners)) { + that._listeners[type] = []; + } + that._listeners[type].push(callback); +}; + +EventEmitter.prototype._verifyType = function(type) { + var that = this; + if (utils.arrIndexOf(that._events, type) === -1) { + utils.log('Event ' + JSON.stringify(type) + + ' not listed ' + JSON.stringify(that._events) + + ' in ' + that); + } +}; + +EventEmitter.prototype.nuke = function() { + var that = this; + that._nuked = true; + for(var i=0; i= 3000 && code <= 4999); +}; + +// See: http://www.erg.abdn.ac.uk/~gerrit/dccp/notes/ccid2/rto_estimator/ +// and RFC 2988. +utils.countRTO = function (rtt) { + var rto; + if (rtt > 100) { + rto = 3 * rtt; // rto > 300msec + } else { + rto = rtt + 200; // 200msec < rto <= 300msec + } + return rto; +} + +utils.log = function() { + if (_window.console && console.log && console.log.apply) { + console.log.apply(console, arguments); + } +}; + +utils.bind = function(fun, that) { + if (fun.bind) { + return fun.bind(that); + } else { + return function() { + return fun.apply(that, arguments); + }; + } +}; + +utils.flatUrl = function(url) { + return url.indexOf('?') === -1 && url.indexOf('#') === -1; +}; + +utils.amendUrl = function(url) { + var dl = _document.location; + if (!url) { + throw new Error('Wrong url for SockJS'); + } + if (!utils.flatUrl(url)) { + throw new Error('Only basic urls are supported in SockJS'); + } + + // '//abc' --> 'http://abc' + if (url.indexOf('//') === 0) { + url = dl.protocol + url; + } + // '/abc' --> 'http://localhost:80/abc' + if (url.indexOf('/') === 0) { + url = dl.protocol + '//' + dl.host + url; + } + // strip trailing slashes + url = url.replace(/[/]+$/,''); + return url; +}; + +// IE doesn't support [].indexOf. +utils.arrIndexOf = function(arr, obj){ + for(var i=0; i < arr.length; i++){ + if(arr[i] === obj){ + return i; + } + } + return -1; +}; + +utils.arrSkip = function(arr, obj) { + var idx = utils.arrIndexOf(arr, obj); + if (idx === -1) { + return arr.slice(); + } else { + var dst = arr.slice(0, idx); + return dst.concat(arr.slice(idx+1)); + } +}; + +// Via: https://gist.github.com/1133122/2121c601c5549155483f50be3da5305e83b8c5df +utils.isArray = Array.isArray || function(value) { + return {}.toString.call(value).indexOf('Array') >= 0 +}; + +utils.delay = function(t, fun) { + if(typeof t === 'function') { + fun = t; + t = 0; + } + return setTimeout(fun, t); +}; + + +// Chars worth escaping, as defined by Douglas Crockford: +// https://github.com/douglascrockford/JSON-js/blob/47a9882cddeb1e8529e07af9736218075372b8ac/json2.js#L196 +var json_escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, + json_lookup = { +"\u0000":"\\u0000","\u0001":"\\u0001","\u0002":"\\u0002","\u0003":"\\u0003", +"\u0004":"\\u0004","\u0005":"\\u0005","\u0006":"\\u0006","\u0007":"\\u0007", +"\b":"\\b","\t":"\\t","\n":"\\n","\u000b":"\\u000b","\f":"\\f","\r":"\\r", +"\u000e":"\\u000e","\u000f":"\\u000f","\u0010":"\\u0010","\u0011":"\\u0011", +"\u0012":"\\u0012","\u0013":"\\u0013","\u0014":"\\u0014","\u0015":"\\u0015", +"\u0016":"\\u0016","\u0017":"\\u0017","\u0018":"\\u0018","\u0019":"\\u0019", +"\u001a":"\\u001a","\u001b":"\\u001b","\u001c":"\\u001c","\u001d":"\\u001d", +"\u001e":"\\u001e","\u001f":"\\u001f","\"":"\\\"","\\":"\\\\", +"\u007f":"\\u007f","\u0080":"\\u0080","\u0081":"\\u0081","\u0082":"\\u0082", +"\u0083":"\\u0083","\u0084":"\\u0084","\u0085":"\\u0085","\u0086":"\\u0086", +"\u0087":"\\u0087","\u0088":"\\u0088","\u0089":"\\u0089","\u008a":"\\u008a", +"\u008b":"\\u008b","\u008c":"\\u008c","\u008d":"\\u008d","\u008e":"\\u008e", +"\u008f":"\\u008f","\u0090":"\\u0090","\u0091":"\\u0091","\u0092":"\\u0092", +"\u0093":"\\u0093","\u0094":"\\u0094","\u0095":"\\u0095","\u0096":"\\u0096", +"\u0097":"\\u0097","\u0098":"\\u0098","\u0099":"\\u0099","\u009a":"\\u009a", +"\u009b":"\\u009b","\u009c":"\\u009c","\u009d":"\\u009d","\u009e":"\\u009e", +"\u009f":"\\u009f","\u00ad":"\\u00ad","\u0600":"\\u0600","\u0601":"\\u0601", +"\u0602":"\\u0602","\u0603":"\\u0603","\u0604":"\\u0604","\u070f":"\\u070f", +"\u17b4":"\\u17b4","\u17b5":"\\u17b5","\u200c":"\\u200c","\u200d":"\\u200d", +"\u200e":"\\u200e","\u200f":"\\u200f","\u2028":"\\u2028","\u2029":"\\u2029", +"\u202a":"\\u202a","\u202b":"\\u202b","\u202c":"\\u202c","\u202d":"\\u202d", +"\u202e":"\\u202e","\u202f":"\\u202f","\u2060":"\\u2060","\u2061":"\\u2061", +"\u2062":"\\u2062","\u2063":"\\u2063","\u2064":"\\u2064","\u2065":"\\u2065", +"\u2066":"\\u2066","\u2067":"\\u2067","\u2068":"\\u2068","\u2069":"\\u2069", +"\u206a":"\\u206a","\u206b":"\\u206b","\u206c":"\\u206c","\u206d":"\\u206d", +"\u206e":"\\u206e","\u206f":"\\u206f","\ufeff":"\\ufeff","\ufff0":"\\ufff0", +"\ufff1":"\\ufff1","\ufff2":"\\ufff2","\ufff3":"\\ufff3","\ufff4":"\\ufff4", +"\ufff5":"\\ufff5","\ufff6":"\\ufff6","\ufff7":"\\ufff7","\ufff8":"\\ufff8", +"\ufff9":"\\ufff9","\ufffa":"\\ufffa","\ufffb":"\\ufffb","\ufffc":"\\ufffc", +"\ufffd":"\\ufffd","\ufffe":"\\ufffe","\uffff":"\\uffff"}; + +// Some extra characters that Chrome gets wrong, and substitutes with +// something else on the wire. +var extra_escapable = /[\x00-\x1f\ud800-\udfff\ufffe\uffff\u0300-\u0333\u033d-\u0346\u034a-\u034c\u0350-\u0352\u0357-\u0358\u035c-\u0362\u0374\u037e\u0387\u0591-\u05af\u05c4\u0610-\u0617\u0653-\u0654\u0657-\u065b\u065d-\u065e\u06df-\u06e2\u06eb-\u06ec\u0730\u0732-\u0733\u0735-\u0736\u073a\u073d\u073f-\u0741\u0743\u0745\u0747\u07eb-\u07f1\u0951\u0958-\u095f\u09dc-\u09dd\u09df\u0a33\u0a36\u0a59-\u0a5b\u0a5e\u0b5c-\u0b5d\u0e38-\u0e39\u0f43\u0f4d\u0f52\u0f57\u0f5c\u0f69\u0f72-\u0f76\u0f78\u0f80-\u0f83\u0f93\u0f9d\u0fa2\u0fa7\u0fac\u0fb9\u1939-\u193a\u1a17\u1b6b\u1cda-\u1cdb\u1dc0-\u1dcf\u1dfc\u1dfe\u1f71\u1f73\u1f75\u1f77\u1f79\u1f7b\u1f7d\u1fbb\u1fbe\u1fc9\u1fcb\u1fd3\u1fdb\u1fe3\u1feb\u1fee-\u1fef\u1ff9\u1ffb\u1ffd\u2000-\u2001\u20d0-\u20d1\u20d4-\u20d7\u20e7-\u20e9\u2126\u212a-\u212b\u2329-\u232a\u2adc\u302b-\u302c\uaab2-\uaab3\uf900-\ufa0d\ufa10\ufa12\ufa15-\ufa1e\ufa20\ufa22\ufa25-\ufa26\ufa2a-\ufa2d\ufa30-\ufa6d\ufa70-\ufad9\ufb1d\ufb1f\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40-\ufb41\ufb43-\ufb44\ufb46-\ufb4e\ufff0-\uffff]/g, + extra_lookup; + +// JSON Quote string. Use native implementation when possible. +var JSONQuote = (JSON && JSON.stringify) || function(string) { + json_escapable.lastIndex = 0; + if (json_escapable.test(string)) { + string = string.replace(json_escapable, function(a) { + return json_lookup[a]; + }); + } + return '"' + string + '"'; +}; + +// This may be quite slow, so let's delay until user actually uses bad +// characters. +var unroll_lookup = function(escapable) { + var i; + var unrolled = {} + var c = [] + for(i=0; i<65536; i++) { + c.push( String.fromCharCode(i) ); + } + escapable.lastIndex = 0; + c.join('').replace(escapable, function (a) { + unrolled[ a ] = '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); + return ''; + }); + escapable.lastIndex = 0; + return unrolled; +}; + +// Quote string, also taking care of unicode characters that browsers +// often break. Especially, take care of unicode surrogates: +// http://en.wikipedia.org/wiki/Mapping_of_Unicode_characters#Surrogates +utils.quote = function(string) { + var quoted = JSONQuote(string); + + // In most cases this should be very fast and good enough. + extra_escapable.lastIndex = 0; + if(!extra_escapable.test(quoted)) { + return quoted; + } + + if(!extra_lookup) extra_lookup = unroll_lookup(extra_escapable); + + return quoted.replace(extra_escapable, function(a) { + return extra_lookup[a]; + }); +} + +var _all_protocols = ['websocket', + 'xdr-streaming', + 'xhr-streaming', + 'iframe-eventsource', + 'iframe-htmlfile', + 'xdr-polling', + 'xhr-polling', + 'iframe-xhr-polling', + 'jsonp-polling']; + +utils.probeProtocols = function() { + var probed = {}; + for(var i=0; i<_all_protocols.length; i++) { + var protocol = _all_protocols[i]; + // User can have a typo in protocol name. + probed[protocol] = SockJS[protocol] && + SockJS[protocol].enabled(); + } + return probed; +}; + +utils.detectProtocols = function(probed, protocols_whitelist, info) { + var pe = {}, + protocols = []; + if (!protocols_whitelist) protocols_whitelist = _all_protocols; + for(var i=0; i 0) { + maybe_push(protos); + } + } + } + + // 1. Websocket + if (info.websocket !== false) { + maybe_push(['websocket']); + } + + // 2. Streaming + if (pe['xhr-streaming'] && !info.null_origin) { + protocols.push('xhr-streaming'); + } else { + if (pe['xdr-streaming'] && !info.cookie_needed && !info.null_origin) { + protocols.push('xdr-streaming'); + } else { + maybe_push(['iframe-eventsource', + 'iframe-htmlfile']); + } + } + + // 3. Polling + if (pe['xhr-polling'] && !info.null_origin) { + protocols.push('xhr-polling'); + } else { + if (pe['xdr-polling'] && !info.cookie_needed && !info.null_origin) { + protocols.push('xdr-polling'); + } else { + maybe_push(['iframe-xhr-polling', + 'jsonp-polling']); + } + } + return protocols; +} +// [*] End of lib/utils.js + + +// [*] Including lib/dom.js +/* + * ***** BEGIN LICENSE BLOCK ***** + * Copyright (c) 2011-2012 VMware, Inc. + * + * For the license see COPYING. + * ***** END LICENSE BLOCK ***** + */ + +// May be used by htmlfile jsonp and transports. +var MPrefix = '_sockjs_global'; +utils.createHook = function() { + var window_id = 'a' + utils.random_string(8); + if (!(MPrefix in _window)) { + var map = {}; + _window[MPrefix] = function(window_id) { + if (!(window_id in map)) { + map[window_id] = { + id: window_id, + del: function() {delete map[window_id];} + }; + } + return map[window_id]; + } + } + return _window[MPrefix](window_id); +}; + + + +utils.attachMessage = function(listener) { + utils.attachEvent('message', listener); +}; +utils.attachEvent = function(event, listener) { + if (typeof _window.addEventListener !== 'undefined') { + _window.addEventListener(event, listener, false); + } else { + // IE quirks. + // According to: http://stevesouders.com/misc/test-postmessage.php + // the message gets delivered only to 'document', not 'window'. + _document.attachEvent("on" + event, listener); + // I get 'window' for ie8. + _window.attachEvent("on" + event, listener); + } +}; + +utils.detachMessage = function(listener) { + utils.detachEvent('message', listener); +}; +utils.detachEvent = function(event, listener) { + if (typeof _window.addEventListener !== 'undefined') { + _window.removeEventListener(event, listener, false); + } else { + _document.detachEvent("on" + event, listener); + _window.detachEvent("on" + event, listener); + } +}; + + +var on_unload = {}; +// Things registered after beforeunload are to be called immediately. +var after_unload = false; + +var trigger_unload_callbacks = function() { + for(var ref in on_unload) { + on_unload[ref](); + delete on_unload[ref]; + }; +}; + +var unload_triggered = function() { + if(after_unload) return; + after_unload = true; + trigger_unload_callbacks(); +}; + +// 'unload' alone is not reliable in opera within an iframe, but we +// can't use `beforeunload` as IE fires it on javascript: links. +utils.attachEvent('unload', unload_triggered); + +utils.unload_add = function(listener) { + var ref = utils.random_string(8); + on_unload[ref] = listener; + if (after_unload) { + utils.delay(trigger_unload_callbacks); + } + return ref; +}; +utils.unload_del = function(ref) { + if (ref in on_unload) + delete on_unload[ref]; +}; + + +utils.createIframe = function (iframe_url, error_callback) { + var iframe = _document.createElement('iframe'); + var tref, unload_ref; + var unattach = function() { + clearTimeout(tref); + // Explorer had problems with that. + try {iframe.onload = null;} catch (x) {} + iframe.onerror = null; + }; + var cleanup = function() { + if (iframe) { + unattach(); + // This timeout makes chrome fire onbeforeunload event + // within iframe. Without the timeout it goes straight to + // onunload. + setTimeout(function() { + if(iframe) { + iframe.parentNode.removeChild(iframe); + } + iframe = null; + }, 0); + utils.unload_del(unload_ref); + } + }; + var onerror = function(r) { + if (iframe) { + cleanup(); + error_callback(r); + } + }; + var post = function(msg, origin) { + try { + // When the iframe is not loaded, IE raises an exception + // on 'contentWindow'. + if (iframe && iframe.contentWindow) { + iframe.contentWindow.postMessage(msg, origin); + } + } catch (x) {}; + }; + + iframe.src = iframe_url; + iframe.style.display = 'none'; + iframe.style.position = 'absolute'; + iframe.onerror = function(){onerror('onerror');}; + iframe.onload = function() { + // `onload` is triggered before scripts on the iframe are + // executed. Give it few seconds to actually load stuff. + clearTimeout(tref); + tref = setTimeout(function(){onerror('onload timeout');}, 2000); + }; + _document.body.appendChild(iframe); + tref = setTimeout(function(){onerror('timeout');}, 15000); + unload_ref = utils.unload_add(cleanup); + return { + post: post, + cleanup: cleanup, + loaded: unattach + }; +}; + +utils.createHtmlfile = function (iframe_url, error_callback) { + var doc = new ActiveXObject('htmlfile'); + var tref, unload_ref; + var iframe; + var unattach = function() { + clearTimeout(tref); + }; + var cleanup = function() { + if (doc) { + unattach(); + utils.unload_del(unload_ref); + iframe.parentNode.removeChild(iframe); + iframe = doc = null; + CollectGarbage(); + } + }; + var onerror = function(r) { + if (doc) { + cleanup(); + error_callback(r); + } + }; + var post = function(msg, origin) { + try { + // When the iframe is not loaded, IE raises an exception + // on 'contentWindow'. + if (iframe && iframe.contentWindow) { + iframe.contentWindow.postMessage(msg, origin); + } + } catch (x) {}; + }; + + doc.open(); + doc.write('' + + 'document.domain="' + document.domain + '";' + + ''); + doc.close(); + doc.parentWindow[WPrefix] = _window[WPrefix]; + var c = doc.createElement('div'); + doc.body.appendChild(c); + iframe = doc.createElement('iframe'); + c.appendChild(iframe); + iframe.src = iframe_url; + tref = setTimeout(function(){onerror('timeout');}, 15000); + unload_ref = utils.unload_add(cleanup); + return { + post: post, + cleanup: cleanup, + loaded: unattach + }; +}; +// [*] End of lib/dom.js + + +// [*] Including lib/dom2.js +/* + * ***** BEGIN LICENSE BLOCK ***** + * Copyright (c) 2011-2012 VMware, Inc. + * + * For the license see COPYING. + * ***** END LICENSE BLOCK ***** + */ + +var AbstractXHRObject = function(){}; +AbstractXHRObject.prototype = new EventEmitter(['chunk', 'finish']); + +AbstractXHRObject.prototype._start = function(method, url, payload, opts) { + var that = this; + + try { + that.xhr = new XMLHttpRequest(); + } catch(x) {}; + + if (!that.xhr) { + try { + that.xhr = new _window.ActiveXObject('Microsoft.XMLHTTP'); + } catch(x) {}; + } + if (_window.ActiveXObject || _window.XDomainRequest) { + // IE8 caches even POSTs + url += ((url.indexOf('?') === -1) ? '?' : '&') + 't='+(+new Date); + } + + // Explorer tends to keep connection open, even after the + // tab gets closed: http://bugs.jquery.com/ticket/5280 + that.unload_ref = utils.unload_add(function(){that._cleanup(true);}); + try { + that.xhr.open(method, url, true); + } catch(e) { + // IE raises an exception on wrong port. + that.emit('finish', 0, ''); + that._cleanup(); + return; + }; + + if (!opts || !opts.no_credentials) { + // Mozilla docs says https://developer.mozilla.org/en/XMLHttpRequest : + // "This never affects same-site requests." + that.xhr.withCredentials = 'true'; + } + if (opts && opts.headers) { + for(var key in opts.headers) { + that.xhr.setRequestHeader(key, opts.headers[key]); + } + } + + that.xhr.onreadystatechange = function() { + if (that.xhr) { + var x = that.xhr; + switch (x.readyState) { + case 3: + // IE doesn't like peeking into responseText or status + // on Microsoft.XMLHTTP and readystate=3 + try { + var status = x.status; + var text = x.responseText; + } catch (x) {}; + // IE returns 1223 for 204: http://bugs.jquery.com/ticket/1450 + if (status === 1223) status = 204; + + // IE does return readystate == 3 for 404 answers. + if (text && text.length > 0) { + that.emit('chunk', status, text); + } + break; + case 4: + var status = x.status; + // IE returns 1223 for 204: http://bugs.jquery.com/ticket/1450 + if (status === 1223) status = 204; + + that.emit('finish', status, x.responseText); + that._cleanup(false); + break; + } + } + }; + that.xhr.send(payload); +}; + +AbstractXHRObject.prototype._cleanup = function(abort) { + var that = this; + if (!that.xhr) return; + utils.unload_del(that.unload_ref); + + // IE needs this field to be a function + that.xhr.onreadystatechange = function(){}; + + if (abort) { + try { + that.xhr.abort(); + } catch(x) {}; + } + that.unload_ref = that.xhr = null; +}; + +AbstractXHRObject.prototype.close = function() { + var that = this; + that.nuke(); + that._cleanup(true); +}; + +var XHRCorsObject = utils.XHRCorsObject = function() { + var that = this, args = arguments; + utils.delay(function(){that._start.apply(that, args);}); +}; +XHRCorsObject.prototype = new AbstractXHRObject(); + +var XHRLocalObject = utils.XHRLocalObject = function(method, url, payload) { + var that = this; + utils.delay(function(){ + that._start(method, url, payload, { + no_credentials: true + }); + }); +}; +XHRLocalObject.prototype = new AbstractXHRObject(); + + + +// References: +// http://ajaxian.com/archives/100-line-ajax-wrapper +// http://msdn.microsoft.com/en-us/library/cc288060(v=VS.85).aspx +var XDRObject = utils.XDRObject = function(method, url, payload) { + var that = this; + utils.delay(function(){that._start(method, url, payload);}); +}; +XDRObject.prototype = new EventEmitter(['chunk', 'finish']); +XDRObject.prototype._start = function(method, url, payload) { + var that = this; + var xdr = new XDomainRequest(); + // IE caches even POSTs + url += ((url.indexOf('?') === -1) ? '?' : '&') + 't='+(+new Date); + + var onerror = xdr.ontimeout = xdr.onerror = function() { + that.emit('finish', 0, ''); + that._cleanup(false); + }; + xdr.onprogress = function() { + that.emit('chunk', 200, xdr.responseText); + }; + xdr.onload = function() { + that.emit('finish', 200, xdr.responseText); + that._cleanup(false); + }; + that.xdr = xdr; + that.unload_ref = utils.unload_add(function(){that._cleanup(true);}); + try { + // Fails with AccessDenied if port number is bogus + that.xdr.open(method, url); + that.xdr.send(payload); + } catch(x) { + onerror(); + } +}; + +XDRObject.prototype._cleanup = function(abort) { + var that = this; + if (!that.xdr) return; + utils.unload_del(that.unload_ref); + + that.xdr.ontimeout = that.xdr.onerror = that.xdr.onprogress = + that.xdr.onload = null; + if (abort) { + try { + that.xdr.abort(); + } catch(x) {}; + } + that.unload_ref = that.xdr = null; +}; + +XDRObject.prototype.close = function() { + var that = this; + that.nuke(); + that._cleanup(true); +}; + +// 1. Is natively via XHR +// 2. Is natively via XDR +// 3. Nope, but postMessage is there so it should work via the Iframe. +// 4. Nope, sorry. +utils.isXHRCorsCapable = function() { + if (_window.XMLHttpRequest && 'withCredentials' in new XMLHttpRequest()) { + return 1; + } + // XDomainRequest doesn't work if page is served from file:// + if (_window.XDomainRequest && _document.domain) { + return 2; + } + if (IframeTransport.enabled()) { + return 3; + } + return 4; +}; +// [*] End of lib/dom2.js + + +// [*] Including lib/sockjs.js +/* + * ***** BEGIN LICENSE BLOCK ***** + * Copyright (c) 2011-2012 VMware, Inc. + * + * For the license see COPYING. + * ***** END LICENSE BLOCK ***** + */ + +var SockJS = function(url, dep_protocols_whitelist, options) { + if (this === _window) { + // makes `new` optional + return new SockJS(url, dep_protocols_whitelist, options); + } + + var that = this, protocols_whitelist; + that._options = {devel: false, debug: false, protocols_whitelist: [], + info: undefined, rtt: undefined}; + if (options) { + utils.objectExtend(that._options, options); + } + that._base_url = utils.amendUrl(url); + that._server = that._options.server || utils.random_number_string(1000); + if (that._options.protocols_whitelist && + that._options.protocols_whitelist.length) { + protocols_whitelist = that._options.protocols_whitelist; + } else { + // Deprecated API + if (typeof dep_protocols_whitelist === 'string' && + dep_protocols_whitelist.length > 0) { + protocols_whitelist = [dep_protocols_whitelist]; + } else if (utils.isArray(dep_protocols_whitelist)) { + protocols_whitelist = dep_protocols_whitelist + } else { + protocols_whitelist = null; + } + if (protocols_whitelist) { + that._debug('Deprecated API: Use "protocols_whitelist" option ' + + 'instead of supplying protocol list as a second ' + + 'parameter to SockJS constructor.'); + } + } + that._protocols = []; + that.protocol = null; + that.readyState = SockJS.CONNECTING; + that._ir = createInfoReceiver(that._base_url); + that._ir.onfinish = function(info, rtt) { + that._ir = null; + if (info) { + if (that._options.info) { + // Override if user supplies the option + info = utils.objectExtend(info, that._options.info); + } + if (that._options.rtt) { + rtt = that._options.rtt; + } + that._applyInfo(info, rtt, protocols_whitelist); + that._didClose(); + } else { + that._didClose(1002, 'Can\'t connect to server', true); + } + }; +}; +// Inheritance +SockJS.prototype = new REventTarget(); + +SockJS.version = "0.3.4"; + +SockJS.CONNECTING = 0; +SockJS.OPEN = 1; +SockJS.CLOSING = 2; +SockJS.CLOSED = 3; + +SockJS.prototype._debug = function() { + if (this._options.debug) + utils.log.apply(utils, arguments); +}; + +SockJS.prototype._dispatchOpen = function() { + var that = this; + if (that.readyState === SockJS.CONNECTING) { + if (that._transport_tref) { + clearTimeout(that._transport_tref); + that._transport_tref = null; + } + that.readyState = SockJS.OPEN; + that.dispatchEvent(new SimpleEvent("open")); + } else { + // The server might have been restarted, and lost track of our + // connection. + that._didClose(1006, "Server lost session"); + } +}; + +SockJS.prototype._dispatchMessage = function(data) { + var that = this; + if (that.readyState !== SockJS.OPEN) + return; + that.dispatchEvent(new SimpleEvent("message", {data: data})); +}; + +SockJS.prototype._dispatchHeartbeat = function(data) { + var that = this; + if (that.readyState !== SockJS.OPEN) + return; + that.dispatchEvent(new SimpleEvent('heartbeat', {})); +}; + +SockJS.prototype._didClose = function(code, reason, force) { + var that = this; + if (that.readyState !== SockJS.CONNECTING && + that.readyState !== SockJS.OPEN && + that.readyState !== SockJS.CLOSING) + throw new Error('INVALID_STATE_ERR'); + if (that._ir) { + that._ir.nuke(); + that._ir = null; + } + + if (that._transport) { + that._transport.doCleanup(); + that._transport = null; + } + + var close_event = new SimpleEvent("close", { + code: code, + reason: reason, + wasClean: utils.userSetCode(code)}); + + if (!utils.userSetCode(code) && + that.readyState === SockJS.CONNECTING && !force) { + if (that._try_next_protocol(close_event)) { + return; + } + close_event = new SimpleEvent("close", {code: 2000, + reason: "All transports failed", + wasClean: false, + last_event: close_event}); + } + that.readyState = SockJS.CLOSED; + + utils.delay(function() { + that.dispatchEvent(close_event); + }); +}; + +SockJS.prototype._didMessage = function(data) { + var that = this; + var type = data.slice(0, 1); + switch(type) { + case 'o': + that._dispatchOpen(); + break; + case 'a': + var payload = JSON.parse(data.slice(1) || '[]'); + for(var i=0; i < payload.length; i++){ + that._dispatchMessage(payload[i]); + } + break; + case 'm': + var payload = JSON.parse(data.slice(1) || 'null'); + that._dispatchMessage(payload); + break; + case 'c': + var payload = JSON.parse(data.slice(1) || '[]'); + that._didClose(payload[0], payload[1]); + break; + case 'h': + that._dispatchHeartbeat(); + break; + } +}; + +SockJS.prototype._try_next_protocol = function(close_event) { + var that = this; + if (that.protocol) { + that._debug('Closed transport:', that.protocol, ''+close_event); + that.protocol = null; + } + if (that._transport_tref) { + clearTimeout(that._transport_tref); + that._transport_tref = null; + } + + while(1) { + var protocol = that.protocol = that._protocols.shift(); + if (!protocol) { + return false; + } + // Some protocols require access to `body`, what if were in + // the `head`? + if (SockJS[protocol] && + SockJS[protocol].need_body === true && + (!_document.body || + (typeof _document.readyState !== 'undefined' + && _document.readyState !== 'complete'))) { + that._protocols.unshift(protocol); + that.protocol = 'waiting-for-load'; + utils.attachEvent('load', function(){ + that._try_next_protocol(); + }); + return true; + } + + if (!SockJS[protocol] || + !SockJS[protocol].enabled(that._options)) { + that._debug('Skipping transport:', protocol); + } else { + var roundTrips = SockJS[protocol].roundTrips || 1; + var to = ((that._options.rto || 0) * roundTrips) || 5000; + that._transport_tref = utils.delay(to, function() { + if (that.readyState === SockJS.CONNECTING) { + // I can't understand how it is possible to run + // this timer, when the state is CLOSED, but + // apparently in IE everythin is possible. + that._didClose(2007, "Transport timeouted"); + } + }); + + var connid = utils.random_string(8); + var trans_url = that._base_url + '/' + that._server + '/' + connid; + that._debug('Opening transport:', protocol, ' url:'+trans_url, + ' RTO:'+that._options.rto); + that._transport = new SockJS[protocol](that, trans_url, + that._base_url); + return true; + } + } +}; + +SockJS.prototype.close = function(code, reason) { + var that = this; + if (code && !utils.userSetCode(code)) + throw new Error("INVALID_ACCESS_ERR"); + if(that.readyState !== SockJS.CONNECTING && + that.readyState !== SockJS.OPEN) { + return false; + } + that.readyState = SockJS.CLOSING; + that._didClose(code || 1000, reason || "Normal closure"); + return true; +}; + +SockJS.prototype.send = function(data) { + var that = this; + if (that.readyState === SockJS.CONNECTING) + throw new Error('INVALID_STATE_ERR'); + if (that.readyState === SockJS.OPEN) { + that._transport.doSend(utils.quote('' + data)); + } + return true; +}; + +SockJS.prototype._applyInfo = function(info, rtt, protocols_whitelist) { + var that = this; + that._options.info = info; + that._options.rtt = rtt; + that._options.rto = utils.countRTO(rtt); + that._options.info.null_origin = !_document.domain; + var probed = utils.probeProtocols(); + that._protocols = utils.detectProtocols(probed, protocols_whitelist, info); +}; +// [*] End of lib/sockjs.js + + +// [*] Including lib/trans-websocket.js +/* + * ***** BEGIN LICENSE BLOCK ***** + * Copyright (c) 2011-2012 VMware, Inc. + * + * For the license see COPYING. + * ***** END LICENSE BLOCK ***** + */ + +var WebSocketTransport = SockJS.websocket = function(ri, trans_url) { + var that = this; + var url = trans_url + '/websocket'; + if (url.slice(0, 5) === 'https') { + url = 'wss' + url.slice(5); + } else { + url = 'ws' + url.slice(4); + } + that.ri = ri; + that.url = url; + var Constructor = _window.WebSocket || _window.MozWebSocket; + + that.ws = new Constructor(that.url); + that.ws.onmessage = function(e) { + that.ri._didMessage(e.data); + }; + // Firefox has an interesting bug. If a websocket connection is + // created after onunload, it stays alive even when user + // navigates away from the page. In such situation let's lie - + // let's not open the ws connection at all. See: + // https://github.com/sockjs/sockjs-client/issues/28 + // https://bugzilla.mozilla.org/show_bug.cgi?id=696085 + that.unload_ref = utils.unload_add(function(){that.ws.close()}); + that.ws.onclose = function() { + that.ri._didMessage(utils.closeFrame(1006, "WebSocket connection broken")); + }; +}; + +WebSocketTransport.prototype.doSend = function(data) { + this.ws.send('[' + data + ']'); +}; + +WebSocketTransport.prototype.doCleanup = function() { + var that = this; + var ws = that.ws; + if (ws) { + ws.onmessage = ws.onclose = null; + ws.close(); + utils.unload_del(that.unload_ref); + that.unload_ref = that.ri = that.ws = null; + } +}; + +WebSocketTransport.enabled = function() { + return !!(_window.WebSocket || _window.MozWebSocket); +}; + +// In theory, ws should require 1 round trip. But in chrome, this is +// not very stable over SSL. Most likely a ws connection requires a +// separate SSL connection, in which case 2 round trips are an +// absolute minumum. +WebSocketTransport.roundTrips = 2; +// [*] End of lib/trans-websocket.js + + +// [*] Including lib/trans-sender.js +/* + * ***** BEGIN LICENSE BLOCK ***** + * Copyright (c) 2011-2012 VMware, Inc. + * + * For the license see COPYING. + * ***** END LICENSE BLOCK ***** + */ + +var BufferedSender = function() {}; +BufferedSender.prototype.send_constructor = function(sender) { + var that = this; + that.send_buffer = []; + that.sender = sender; +}; +BufferedSender.prototype.doSend = function(message) { + var that = this; + that.send_buffer.push(message); + if (!that.send_stop) { + that.send_schedule(); + } +}; + +// For polling transports in a situation when in the message callback, +// new message is being send. If the sending connection was started +// before receiving one, it is possible to saturate the network and +// timeout due to the lack of receiving socket. To avoid that we delay +// sending messages by some small time, in order to let receiving +// connection be started beforehand. This is only a halfmeasure and +// does not fix the big problem, but it does make the tests go more +// stable on slow networks. +BufferedSender.prototype.send_schedule_wait = function() { + var that = this; + var tref; + that.send_stop = function() { + that.send_stop = null; + clearTimeout(tref); + }; + tref = utils.delay(25, function() { + that.send_stop = null; + that.send_schedule(); + }); +}; + +BufferedSender.prototype.send_schedule = function() { + var that = this; + if (that.send_buffer.length > 0) { + var payload = '[' + that.send_buffer.join(',') + ']'; + that.send_stop = that.sender(that.trans_url, payload, function(success, abort_reason) { + that.send_stop = null; + if (success === false) { + that.ri._didClose(1006, 'Sending error ' + abort_reason); + } else { + that.send_schedule_wait(); + } + }); + that.send_buffer = []; + } +}; + +BufferedSender.prototype.send_destructor = function() { + var that = this; + if (that._send_stop) { + that._send_stop(); + } + that._send_stop = null; +}; + +var jsonPGenericSender = function(url, payload, callback) { + var that = this; + + if (!('_send_form' in that)) { + var form = that._send_form = _document.createElement('form'); + var area = that._send_area = _document.createElement('textarea'); + area.name = 'd'; + form.style.display = 'none'; + form.style.position = 'absolute'; + form.method = 'POST'; + form.enctype = 'application/x-www-form-urlencoded'; + form.acceptCharset = "UTF-8"; + form.appendChild(area); + _document.body.appendChild(form); + } + var form = that._send_form; + var area = that._send_area; + var id = 'a' + utils.random_string(8); + form.target = id; + form.action = url + '/jsonp_send?i=' + id; + + var iframe; + try { + // ie6 dynamic iframes with target="" support (thanks Chris Lambacher) + iframe = _document.createElement('