From ab9969a393ac840eb89ebd8886c0ad000bc5d29f Mon Sep 17 00:00:00 2001 From: eugenp Date: Wed, 6 Nov 2013 15:10:49 +0200 Subject: [PATCH] mockito new tutorial --- mockito/.classpath | 36 ++++ ...e.wst.jsdt.core.javascriptValidator.launch | 7 + mockito/.gitignore | 13 ++ mockito/.project | 36 ++++ mockito/.settings/.jsdtscope | 5 + mockito/.settings/org.eclipse.jdt.core.prefs | 95 +++++++++ mockito/.settings/org.eclipse.jdt.ui.prefs | 55 +++++ mockito/.settings/org.eclipse.m2e.core.prefs | 4 + mockito/.settings/org.eclipse.m2e.wtp.prefs | 2 + .../org.eclipse.wst.common.component | 8 + ....eclipse.wst.common.project.facet.core.xml | 4 + ...rg.eclipse.wst.jsdt.ui.superType.container | 1 + .../org.eclipse.wst.jsdt.ui.superType.name | 1 + .../org.eclipse.wst.validation.prefs | 14 ++ .../org.eclipse.wst.ws.service.policy.prefs | 2 + mockito/.springBeans | 14 ++ mockito/README.md | 8 + mockito/pom.xml | 110 ++++++++++ mockito/src/main/resources/logback.xml | 20 ++ .../src/main/webapp/WEB-INF/api-servlet.xml | 6 + mockito/src/main/webapp/WEB-INF/web.xml | 42 ++++ .../guava/GuavaCollectionsExamplesTest.java | 189 ++++++++++++++++++ .../guava/GuavaFunctionalExamplesTest.java | 186 +++++++++++++++++ .../guava/GuavaOrderingExamplesTest.java | 179 +++++++++++++++++ .../hamcrest/HamcrestExamplesTest.java | 97 +++++++++ mockito/src/test/resources/.gitignore | 13 ++ 26 files changed, 1147 insertions(+) create mode 100644 mockito/.classpath create mode 100644 mockito/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch create mode 100644 mockito/.gitignore create mode 100644 mockito/.project create mode 100644 mockito/.settings/.jsdtscope create mode 100644 mockito/.settings/org.eclipse.jdt.core.prefs create mode 100644 mockito/.settings/org.eclipse.jdt.ui.prefs create mode 100644 mockito/.settings/org.eclipse.m2e.core.prefs create mode 100644 mockito/.settings/org.eclipse.m2e.wtp.prefs create mode 100644 mockito/.settings/org.eclipse.wst.common.component create mode 100644 mockito/.settings/org.eclipse.wst.common.project.facet.core.xml create mode 100644 mockito/.settings/org.eclipse.wst.jsdt.ui.superType.container create mode 100644 mockito/.settings/org.eclipse.wst.jsdt.ui.superType.name create mode 100644 mockito/.settings/org.eclipse.wst.validation.prefs create mode 100644 mockito/.settings/org.eclipse.wst.ws.service.policy.prefs create mode 100644 mockito/.springBeans create mode 100644 mockito/README.md create mode 100644 mockito/pom.xml create mode 100644 mockito/src/main/resources/logback.xml create mode 100644 mockito/src/main/webapp/WEB-INF/api-servlet.xml create mode 100644 mockito/src/main/webapp/WEB-INF/web.xml create mode 100644 mockito/src/test/java/org/baeldung/guava/GuavaCollectionsExamplesTest.java create mode 100644 mockito/src/test/java/org/baeldung/guava/GuavaFunctionalExamplesTest.java create mode 100644 mockito/src/test/java/org/baeldung/guava/GuavaOrderingExamplesTest.java create mode 100644 mockito/src/test/java/org/baeldung/hamcrest/HamcrestExamplesTest.java create mode 100644 mockito/src/test/resources/.gitignore diff --git a/mockito/.classpath b/mockito/.classpath new file mode 100644 index 0000000000..c370943e8c --- /dev/null +++ b/mockito/.classpath @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/mockito/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch b/mockito/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch new file mode 100644 index 0000000000..627021fb96 --- /dev/null +++ b/mockito/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch @@ -0,0 +1,7 @@ + + + + + + + diff --git a/mockito/.gitignore b/mockito/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/mockito/.gitignore @@ -0,0 +1,13 @@ +*.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/mockito/.project b/mockito/.project new file mode 100644 index 0000000000..1c6c0deddc --- /dev/null +++ b/mockito/.project @@ -0,0 +1,36 @@ + + + mockito + + + + + + 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 + + diff --git a/mockito/.settings/.jsdtscope b/mockito/.settings/.jsdtscope new file mode 100644 index 0000000000..7b3f0c8b9f --- /dev/null +++ b/mockito/.settings/.jsdtscope @@ -0,0 +1,5 @@ + + + + + diff --git a/mockito/.settings/org.eclipse.jdt.core.prefs b/mockito/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..723e5b1245 --- /dev/null +++ b/mockito/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,95 @@ +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.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +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.7 diff --git a/mockito/.settings/org.eclipse.jdt.ui.prefs b/mockito/.settings/org.eclipse.jdt.ui.prefs new file mode 100644 index 0000000000..471e9b0d81 --- /dev/null +++ b/mockito/.settings/org.eclipse.jdt.ui.prefs @@ -0,0 +1,55 @@ +#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 new file mode 100644 index 0000000000..f897a7f1cb --- /dev/null +++ b/mockito/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +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 new file mode 100644 index 0000000000..ef86089622 --- /dev/null +++ b/mockito/.settings/org.eclipse.m2e.wtp.prefs @@ -0,0 +1,2 @@ +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 new file mode 100644 index 0000000000..e98377cb0f --- /dev/null +++ b/mockito/.settings/org.eclipse.wst.common.component @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/mockito/.settings/org.eclipse.wst.common.project.facet.core.xml b/mockito/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000000..bc0009a455 --- /dev/null +++ b/mockito/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,4 @@ + + + + diff --git a/mockito/.settings/org.eclipse.wst.jsdt.ui.superType.container b/mockito/.settings/org.eclipse.wst.jsdt.ui.superType.container new file mode 100644 index 0000000000..3bd5d0a480 --- /dev/null +++ b/mockito/.settings/org.eclipse.wst.jsdt.ui.superType.container @@ -0,0 +1 @@ +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 new file mode 100644 index 0000000000..05bd71b6ec --- /dev/null +++ b/mockito/.settings/org.eclipse.wst.jsdt.ui.superType.name @@ -0,0 +1 @@ +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 new file mode 100644 index 0000000000..cacf5451ae --- /dev/null +++ b/mockito/.settings/org.eclipse.wst.validation.prefs @@ -0,0 +1,14 @@ +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 new file mode 100644 index 0000000000..9cfcabe16f --- /dev/null +++ b/mockito/.settings/org.eclipse.wst.ws.service.policy.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.wst.ws.service.policy.projectEnabled=false diff --git a/mockito/.springBeans b/mockito/.springBeans new file mode 100644 index 0000000000..a79097f40d --- /dev/null +++ b/mockito/.springBeans @@ -0,0 +1,14 @@ + + + 1 + + + + + + + src/main/webapp/WEB-INF/api-servlet.xml + + + + diff --git a/mockito/README.md b/mockito/README.md new file mode 100644 index 0000000000..d6d3ef275d --- /dev/null +++ b/mockito/README.md @@ -0,0 +1,8 @@ +========= + +## Guava Cookbooks and Examples + + +### Relevant Articles: +- [Guava Collections Cookbook](http://www.baeldung.com/guava-collections) + diff --git a/mockito/pom.xml b/mockito/pom.xml new file mode 100644 index 0000000000..e5e2c604ab --- /dev/null +++ b/mockito/pom.xml @@ -0,0 +1,110 @@ + + 4.0.0 + org.baeldung + spring-rest + 0.1-SNAPSHOT + + spring-rest + + + + + + + com.google.guava + guava + 15.0 + + + + + + + + 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 + + + + + + spring-rest + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + + + + + + + 3.2.4.RELEASE + 3.1.4.RELEASE + + + 4.2.4.Final + 5.1.26 + + + 1.7.5 + 1.0.11 + + + 5.0.1.Final + + + 15.0 + 3.1 + + + 1.3 + 4.11 + 1.9.5 + + 4.2.4 + 4.2.5 + + 1.8.1 + 1.8.9 + + + 1.4.3 + 2.16 + + + \ No newline at end of file diff --git a/mockito/src/main/resources/logback.xml b/mockito/src/main/resources/logback.xml new file mode 100644 index 0000000000..1146dade63 --- /dev/null +++ b/mockito/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/mockito/src/main/webapp/WEB-INF/api-servlet.xml b/mockito/src/main/webapp/WEB-INF/api-servlet.xml new file mode 100644 index 0000000000..5463820874 --- /dev/null +++ b/mockito/src/main/webapp/WEB-INF/api-servlet.xml @@ -0,0 +1,6 @@ + + + + \ 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 new file mode 100644 index 0000000000..48d4b8fe61 --- /dev/null +++ b/mockito/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,42 @@ + + + + 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/org/baeldung/guava/GuavaCollectionsExamplesTest.java b/mockito/src/test/java/org/baeldung/guava/GuavaCollectionsExamplesTest.java new file mode 100644 index 0000000000..2c137e6c80 --- /dev/null +++ b/mockito/src/test/java/org/baeldung/guava/GuavaCollectionsExamplesTest.java @@ -0,0 +1,189 @@ +package org.baeldung.guava; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Set; + +import org.junit.Test; + +import com.google.common.base.Function; +import com.google.common.base.Predicate; +import com.google.common.base.Predicates; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; + +@SuppressWarnings("unused") +public class GuavaCollectionsExamplesTest { + + // tests + + @Test + public final void whenDowncastingGenerifiedCollectionToNewGenerifiedCollection_thenCastIsOK() { + final class CastFunction implements Function { + @SuppressWarnings("unchecked") + @Override + public final T apply(final F from) { + return (T) from; + } + } + + final List originalList = Lists.newArrayList(); + final List selectedProducts = Lists.transform(originalList, new CastFunction()); + System.out.println(selectedProducts); + } + + @SuppressWarnings({ "unchecked" }) + @Test + public final void whenDowncastingGenerifiedCollectionToNewGenerifiedCollection_thenCastIsOK2() { + final List originalList = Lists.newArrayList(); + final List selectedProducts = (List) (List) originalList; + System.out.println(selectedProducts); + } + + @Test + public final void whenAddingAnIterableToACollection_thenAddedOK() { + final Iterable iter = Lists.newArrayList(); + final Collection collector = Lists.newArrayList(); + Iterables.addAll(collector, iter); + } + + // + + @Test + public final void whenCheckingIfCollectionContainsElementsByCustomMatch1_thenContains() { + final Iterable theCollection = Lists.newArrayList("a", "bc", "def"); + final boolean contains = Iterables.any(theCollection, new Predicate() { + @Override + public final boolean apply(final String input) { + return input.length() == 1; + } + }); + + assertTrue(contains); + } + + @Test + public final void whenCheckingIfCollectionContainsElementsByCustomMatch2_thenContains() { + final Set theCollection = Sets.newHashSet("a", "bc", "def"); + + final boolean contains = !Sets.filter(theCollection, new Predicate() { + @Override + public final boolean apply(final String input) { + return input.length() == 1; + } + }).isEmpty(); + + assertTrue(contains); + } + + @Test + public final void whenCheckingIfCollectionContainsElementsByCustomMatch3_thenContains() { + final Iterable theCollection = Sets.newHashSet("a", "bc", "def"); + + final boolean contains = Iterables.find(theCollection, new Predicate() { + @Override + public final boolean apply(final String input) { + return input.length() == 1; + } + }) != null; + + assertTrue(contains); + } + + // + + @Test(expected = NoSuchElementException.class) + public final void givenNoSearchResult_whenFindingElementInIterable_thenException() { + final Iterable theCollection = Sets.newHashSet("abcd", "efgh", "ijkl"); + + final String found = Iterables.find(theCollection, new Predicate() { + @Override + public final boolean apply(final String input) { + return input.length() == 1; + } + }); + + assertNull(found); + } + + @Test + public final void givenNoSearchResult_whenFindingElementInIterableWithSpecifiedReturn_thenNoException() { + final Iterable theCollection = Sets.newHashSet("abcd", "efgh", "ijkl"); + + final Predicate inputOfLengthOne = new Predicate() { + @Override + public final boolean apply(final String input) { + return input.length() == 1; + } + }; + final String found = Iterables.find(theCollection, inputOfLengthOne, null); + + assertNull(found); + } + + // purge of nulls + + @Test + public final void givenListContainsNulls_whenPurgedOfNulls_thenNoLongerContainsNulls() { + final List values = Lists.newArrayList("a", null, "b", "c"); + final Iterable withoutNulls = Iterables.filter(values, Predicates.notNull()); + System.out.println(withoutNulls); + } + + // immutable collections + + @Test + public final void whenCreatingImuutableCollections_thenNoExceptions() { + final ImmutableList immutableList = ImmutableList.of("a", "b", "c"); + final ImmutableSet immutableSet = ImmutableSet.of("a", "b", "c"); + final ImmutableMap imuttableMap = ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3"); + } + + @Test + public final void whenTransformingCollectionsToImmutable_thenNoExceptions() { + final List muttableList = Lists.newArrayList(); + final ImmutableList immutableList = ImmutableList.copyOf(muttableList); + + final Set muttableSet = Sets.newHashSet(); + final ImmutableSet immutableSet = ImmutableSet.copyOf(muttableSet); + + final Map muttableMap = Maps.newHashMap(); + final ImmutableMap imuttableMap = ImmutableMap.copyOf(muttableMap); + } + + @Test + public final void whenTransformingCollectionsToImmutableViaBuilders_thenNoExceptions() { + final List muttableList = Lists.newArrayList(); + final ImmutableList immutableList = ImmutableList. builder().addAll(muttableList).build(); + + final Set muttableSet = Sets.newHashSet(); + final ImmutableSet immutableSet = ImmutableSet. builder().addAll(muttableSet).build(); + + final Map muttableMap = Maps.newHashMap(); + final ImmutableMap imuttableMap = ImmutableMap. builder().putAll(muttableMap).build(); + } + + // unmodifiable + + @Test(expected = UnsupportedOperationException.class) + public final void givenUnmodifiableViewOverIterable_whenTryingToRemove_thenNotAllowed() { + final List numbers = Lists.newArrayList(1, 2, 3); + final Iterable unmodifiableIterable = Iterables.unmodifiableIterable(numbers); + final Iterator iterator = unmodifiableIterable.iterator(); + if (iterator.hasNext()) { + iterator.remove(); + } + } + +} diff --git a/mockito/src/test/java/org/baeldung/guava/GuavaFunctionalExamplesTest.java b/mockito/src/test/java/org/baeldung/guava/GuavaFunctionalExamplesTest.java new file mode 100644 index 0000000000..08091639f1 --- /dev/null +++ b/mockito/src/test/java/org/baeldung/guava/GuavaFunctionalExamplesTest.java @@ -0,0 +1,186 @@ +package org.baeldung.guava; + +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.lessThan; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.Test; + +import com.google.common.base.Function; +import com.google.common.base.Functions; +import com.google.common.base.Predicate; +import com.google.common.base.Predicates; +import com.google.common.collect.Collections2; +import com.google.common.collect.FluentIterable; +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Ordering; +import com.google.common.collect.Sets; + +public class GuavaFunctionalExamplesTest { + + // tests + + // predicates - filtering + + @Test + public final void whenFilteringNumbersAccordingToACondition_thenCorrectResults() { + final List numbers = Lists.newArrayList(1, 2, 3, 6, 8, 10, 34, 57, 89); + final Predicate acceptEvenNumber = new Predicate() { + @Override + public final boolean apply(final Integer number) { + return (number % 2) == 0; + } + }; + final List evenNumbers = Lists.newArrayList(Collections2.filter(numbers, acceptEvenNumber)); + + final Integer found = Collections.binarySearch(evenNumbers, 57); + assertThat(found, lessThan(0)); + } + + @Test + public final void givenCollectionContainsNulls_whenNullsAreFilteredOut_thenResultingCollectionsHasNoNulls() { + final List withNulls = Lists.newArrayList("a", "bc", null, "def"); + final Iterable withoutNuls = Iterables.filter(withNulls, Predicates.notNull()); + + assertTrue(Iterables.all(withoutNuls, Predicates.notNull())); + } + + // predicates - checking + + @Test + public final void givenEvenNumbers_whenCheckingIfAllSatisfyTheEvenPredicate_thenYes() { + final List evenNumbers = Lists.newArrayList(2, 6, 8, 10, 34, 90); + final Predicate acceptEvenNumber = new Predicate() { + @Override + public final boolean apply(final Integer number) { + return (number % 2) == 0; + } + }; + + assertTrue(Iterables.all(evenNumbers, acceptEvenNumber)); + } + + // negating a predicate + + @Test + public final void givenCollectionOfEvenNumbers_whenCheckingThatCollectionContainsNoOddNumber_thenTrue() { + final List evenNumbers = Lists.newArrayList(2, 6, 8, 10, 34, 90); + final Predicate acceptOddNumber = new Predicate() { + @Override + public final boolean apply(final Integer number) { + return (number % 2) != 0; + } + }; + + assertTrue(Iterables.all(evenNumbers, Predicates.not(acceptOddNumber))); + } + + // other predicates + + @Test + public final void when_thenCorrect() { + // CharMatcher.forPredicate(predicate) + } + + // functions + + @Test + public final void whenApplyingSimpleFunctionToInputs_thenCorrectlyTransformed() { + final List numbers = Lists.newArrayList(1, 2, 3); + final List numbersAsStrings = Lists.transform(numbers, Functions.toStringFunction()); + assertThat(numbersAsStrings, contains("1", "2", "3")); + } + + @Test + public final void whenUsingAnIntermediaryFunctionToOrder_thenCorerctlyOrderedInAlphabeticalOrder() { + final List numbersToSort = Arrays.asList(2, 1, 11, 100, 8, 14); + final Ordering ordering = Ordering.natural().onResultOf(Functions.toStringFunction()); + final List inAlphabeticalOrder = ordering.sortedCopy(numbersToSort); + + final List correctAlphabeticalOrder = Lists.newArrayList(1, 100, 11, 14, 2, 8); + assertThat(correctAlphabeticalOrder, equalTo(inAlphabeticalOrder)); + } + + @Test + public final void whenChainingPredicatesAndFunctions_thenCorrectResults() { + final List numbers = Arrays.asList(2, 1, 11, 100, 8, 14); + final Predicate acceptEvenNumber = new Predicate() { + @Override + public final boolean apply(final Integer number) { + return (number % 2) == 0; + } + }; + final Function powerOfTwo = new Function() { + @Override + public final Integer apply(final Integer input) { + return (int) Math.pow(input, 2); + } + }; + + final FluentIterable powerOfTwoOnlyForEvenNumbers = FluentIterable.from(numbers).filter(acceptEvenNumber).transform(powerOfTwo); + assertThat(powerOfTwoOnlyForEvenNumbers, contains(4, 10000, 64, 196)); + } + + @Test + public final void whenUsingFunctionComposition_thenCorrectResults() { + final List numbers = Arrays.asList(2, 3); + final Function powerOfTwo = new Function() { + @Override + public final Integer apply(final Integer input) { + return (int) Math.pow(input, 2); + } + }; + + final List result = Lists.transform(numbers, Functions.compose(powerOfTwo, powerOfTwo)); + assertThat(result, contains(16, 81)); + } + + // Set+Function => Map + + /** + * - see: http://code.google.com/p/guava-libraries/issues/detail?id=56 + */ + @Test + public final void whenMapIsBackedBySetAndFunction_thenCorrect() { + final Function powerOfTwo = new Function() { + @Override + public final Integer apply(final Integer input) { + return (int) Math.pow(input, 2); + } + }; + final Set lowNumbers = Sets.newHashSet(2, 3, 4); + + final Map numberToPowerOfTwoMuttable = Maps.asMap(lowNumbers, powerOfTwo); + final Map numberToPowerOfTwoImuttable = Maps.toMap(lowNumbers, powerOfTwo); + assertThat(numberToPowerOfTwoMuttable.get(2), equalTo(4)); + assertThat(numberToPowerOfTwoImuttable.get(2), equalTo(4)); + } + + // Predicate => Function + + @Test + public final void whenConvertingPredicateToFunction_thenCorrect() { + final List numbers = Lists.newArrayList(1, 2, 3, 6); + final Predicate acceptEvenNumber = new Predicate() { + @Override + public final boolean apply(final Integer number) { + return (number % 2) == 0; + } + }; + final Function isEventNumberFunction = Functions.forPredicate(acceptEvenNumber); + final List areNumbersEven = Lists.transform(numbers, isEventNumberFunction); + + assertThat(areNumbersEven, contains(false, true, false, true)); + } + +} diff --git a/mockito/src/test/java/org/baeldung/guava/GuavaOrderingExamplesTest.java b/mockito/src/test/java/org/baeldung/guava/GuavaOrderingExamplesTest.java new file mode 100644 index 0000000000..5821d19ba0 --- /dev/null +++ b/mockito/src/test/java/org/baeldung/guava/GuavaOrderingExamplesTest.java @@ -0,0 +1,179 @@ +package org.baeldung.guava; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.junit.Test; + +import com.google.common.base.Functions; +import com.google.common.collect.Lists; +import com.google.common.collect.Ordering; +import com.google.common.primitives.Ints; + +public class GuavaOrderingExamplesTest { + + private final class OrderingByLenght extends Ordering { + @Override + public final int compare(final String s1, final String s2) { + return Ints.compare(s1.length(), s2.length()); + } + } + + // tests + + // dealing with null + + @Test + public final void givenCollectionWithNulls_whenSortingWithNullsLast_thenNullsAreLast() { + final List toSort = Arrays.asList(3, 5, 4, null, 1, 2); + Collections.sort(toSort, Ordering.natural().nullsLast()); + assertThat(toSort.get(toSort.size() - 1), nullValue()); + } + + @Test + public final void givenCollectionWithNulls_whenSortingWithNullsFirst_thenNullsAreFirst() { + final List toSort = Arrays.asList(3, 5, 4, null, 1, 2); + Collections.sort(toSort, Ordering.natural().nullsFirst()); + assertThat(toSort.get(0), nullValue()); + } + + @Test + public final void whenCollectionIsSortedNullsLastReversed_thenNullAreFirst() { + final List toSort = Arrays.asList(3, 5, 4, null, 1, 2); + Collections.sort(toSort, Ordering.natural().nullsLast().reverse()); + assertThat(toSort.get(0), nullValue()); + } + + // natural ordering + + @Test + public final void whenSortingWithNaturalOrdering_thenCorectlySorted() { + final List toSort = Arrays.asList(3, 5, 4, 1, 2); + Collections.sort(toSort, Ordering.natural()); + + assertTrue(Ordering.natural().isOrdered(toSort)); + } + + // checking string ordering + + @Test + public final void givenCollectionContainsDuplicates_whenCheckingStringOrdering_thenNo() { + final List toSort = Arrays.asList(3, 5, 4, 2, 1, 2); + Collections.sort(toSort, Ordering.natural()); + + assertFalse(Ordering.natural().isStrictlyOrdered(toSort)); + } + + // custom - by length + + @Test + public final void givenCollectionIsSorted_whenUsingOrderingApiToCheckOrder_thenCheckCanBePerformed() { + final List toSort = Arrays.asList("zz", "aa", "b", "ccc"); + final Ordering byLength = new OrderingByLenght(); + Collections.sort(toSort, byLength); + + final Ordering expectedOrder = Ordering.explicit(Lists.newArrayList("b", "zz", "aa", "ccc")); + assertTrue(expectedOrder.isOrdered(toSort)); + } + + @Test + public final void whenSortingCollectionsOfStringsByLenght_thenCorrectlySorted() { + final List toSort = Arrays.asList("zz", "aa", "b", "ccc"); + final Ordering byLength = new OrderingByLenght(); + + Collections.sort(toSort, byLength); + + final Ordering expectedOrder = Ordering.explicit(Lists.newArrayList("b", "zz", "aa", "ccc")); + assertTrue(expectedOrder.isOrdered(toSort)); + } + + @Test + public final void whenSortingCollectionsOfStringsByLenghtWithSecondaryNaturalOrdering_thenCorrectlySorted() { + final List toSort = Arrays.asList("zz", "aa", "b", "ccc"); + final Ordering byLength = new OrderingByLenght(); + + Collections.sort(toSort, byLength.compound(Ordering.natural())); + + final Ordering expectedOrder = Ordering.explicit(Lists.newArrayList("b", "aa", "zz", "ccc")); + assertTrue(expectedOrder.isOrdered(toSort)); + } + + @Test + public final void whenSortingCollectionsWithComplexOrderingExample_thenCorrectlySorted() { + final List toSort = Arrays.asList("zz", "aa", null, "b", "ccc"); + + Collections.sort(toSort, new OrderingByLenght().reverse().compound(Ordering.natural()).nullsLast()); + System.out.println(toSort); + } + + // sorted copy + + @Test + public final void givenUnorderdList_whenRetrievingSortedCopy_thenSorted() { + final List toSort = Arrays.asList("aa", "b", "ccc"); + final List sortedCopy = new OrderingByLenght().sortedCopy(toSort); + + final Ordering expectedOrder = Ordering.explicit(Lists.newArrayList("b", "aa", "ccc")); + assertFalse(expectedOrder.isOrdered(toSort)); + assertTrue(expectedOrder.isOrdered(sortedCopy)); + } + + // to string + + @Test + public final void givenUnorderdList_whenUsingToStringForSortingObjects_thenSortedWithToString() { + final List toSort = Arrays.asList(1, 2, 11); + Collections.sort(toSort, Ordering.usingToString()); + + final Ordering expectedOrder = Ordering.explicit(Lists.newArrayList(1, 11, 2)); + assertTrue(expectedOrder.isOrdered(toSort)); + } + + // binary search + + @Test + public final void whenPerformingBinarySearch_thenFound() { + final List toSort = Arrays.asList(1, 2, 11); + Collections.sort(toSort, Ordering.usingToString()); + final int found = Ordering.usingToString().binarySearch(toSort, 2); + + System.out.println(found); + } + + // min/max without actually sorting + + @Test + public final void whenFindingTheMinimalElementWithoutSorting_thenFound() { + final List toSort = Arrays.asList(2, 1, 11, 100, 8, 14); + final int found = Ordering.natural().min(toSort); + assertThat(found, equalTo(1)); + } + + @Test + public final void whenFindingTheFirstFewElements_thenCorrect() { + final List toSort = Arrays.asList(2, 1, 11, 100, 8, 14); + final List leastOf = Ordering.natural().leastOf(toSort, 3); + final List expected = Lists.newArrayList(1, 2, 8); + assertThat(expected, equalTo(leastOf)); + } + + // order the results of a Function + + @Test + public final void givenListOfNumbers_whenRunningAToStringFunctionThenSorting_thenCorrect() { + final List toSort = Arrays.asList(2, 1, 11, 100, 8, 14); + final Ordering ordering = Ordering.natural().onResultOf(Functions.toStringFunction()); + final List sortedCopy = ordering.sortedCopy(toSort); + + final List expected = Lists.newArrayList(1, 100, 11, 14, 2, 8); + assertThat(expected, equalTo(sortedCopy)); + } + +} diff --git a/mockito/src/test/java/org/baeldung/hamcrest/HamcrestExamplesTest.java b/mockito/src/test/java/org/baeldung/hamcrest/HamcrestExamplesTest.java new file mode 100644 index 0000000000..a44ce766f0 --- /dev/null +++ b/mockito/src/test/java/org/baeldung/hamcrest/HamcrestExamplesTest.java @@ -0,0 +1,97 @@ +package org.baeldung.hamcrest; + +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.emptyArray; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.everyItem; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasItems; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertThat; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import org.hamcrest.Matchers; +import org.junit.Test; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; + +public class HamcrestExamplesTest { + + // tests + + @Test + public final void whenVerifyingSingleElementIsPartOfCollection_thenCorrect() { + final List collection = Lists.newArrayList("ab", "cd", "ef"); + assertThat(collection, hasItem("cd")); + assertThat(collection, not(hasItem("zz"))); + } + + @Test + public final void whenVerifyingMultipleElementsArePartOfCollection_thenCorrect1() { + final List collection = Lists.newArrayList("ab", "cd", "ef"); + assertThat(collection, hasItems("ef", "cd")); + } + + @Test + public final void whenVerifyingMultipleElementsArePartOfCollectionInStrictOrder_thenCorrect2() { + final List collection = Lists.newArrayList("ab", "cd", "ef"); + assertThat(collection, contains("ab", "cd", "ef")); + } + + @Test + public final void whenVerifyingMultipleElementsArePartOfCollectionInAnyOrder_thenCorrect2() { + final List collection = Lists.newArrayList("ab", "cd", "ef"); + assertThat(collection, containsInAnyOrder("cd", "ab", "ef")); + } + + @Test + public final void givenCollectionIsEmpty_whenChecking_thenEmpty() { + final List collection = Lists.newArrayList(); + assertThat(collection, empty()); + } + + @Test + public final void givenCollectionIsNotEmpty_whenChecking_thenNotEmpty() { + final List collection = Lists.newArrayList("a"); + assertThat(collection, not(empty())); + } + + @Test + public final void givenMapIsEmpty_whenChecking_thenEmpty() { + final Map collection = Maps.newHashMap(); + assertThat(collection, equalTo(Collections.EMPTY_MAP)); + } + + @Test + public final void givenArrayIsEmpty_whenChecking_thenEmpty() { + final String[] array = new String[] { "ab" }; + assertThat(array, not(emptyArray())); + } + + @Test + public final void whenCollectionSizeIsChecked_thenCorrect() { + final List collection = Lists.newArrayList("ab", "cd", "ef"); + assertThat(collection, hasSize(3)); + } + + @Test + public final void whenIterableSizeIsChecked_thenCorrect() { + final Iterable collection = Lists.newArrayList("ab", "cd", "ef"); + assertThat(collection, Matchers. iterableWithSize(3)); + } + + @Test + public final void whenCheckingConditionOverEachItem_thenCorrect() { + final List collection = Lists.newArrayList(15, 20, 25, 30); + assertThat(collection, everyItem(greaterThan(10))); + } + +} diff --git a/mockito/src/test/resources/.gitignore b/mockito/src/test/resources/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/mockito/src/test/resources/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file