From 7ad18550f37128dbba5e5f90ea86aff525aa417d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20de=20Oliveira?= Date: Tue, 29 Jul 2014 18:14:38 -0300 Subject: [PATCH] Liferay Portal JSP Unit Test Examples --- .classpath | 29 ++ .gitignore | 2 + .settings/org.eclipse.jdt.core.prefs | 375 +++++++++++++++++ .settings/org.eclipse.jdt.ui.prefs | 56 +++ .settings/org.eclipse.m2e.core.prefs | 4 + README.md | 40 ++ liferay-portal-jsp-unit-test.launch | 26 ++ pom.xml | 67 +++ ...Blogs_view_entry_content_JSPTestSetUp.java | 114 ++++++ .../PortletBlogsAggregator_view_jsp_Test.java | 161 ++++++++ .../PortletBlogs_view_entry_Jsp_Test.java | 255 ++++++++++++ ...tletBlogs_view_entry_content_Jsp_Test.java | 124 ++++++ .../Discussion_page_jsp_TestSetUp.java | 33 ++ .../TaglibUIDiscussion_page_jsp_Test.java | 383 ++++++++++++++++++ .../taglib/ui/UserDisplayTagTestSetUp.java | 45 ++ .../liferay/taglib/util/IncludeTagSetUp.java | 33 ++ .../com/liferay/test/jsp/JSPTestEngine.java | 150 +++++++ .../com/liferay/test/jsp/ResponseContent.java | 44 ++ src/test/java/com/liferay/test/jsp/Strip.java | 50 +++ .../liferay/test/junit/StringLookingAt.java | 28 ++ .../test/portal/jsp/LiferayJSPTestSetUp.java | 311 ++++++++++++++ 21 files changed, 2330 insertions(+) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 .settings/org.eclipse.jdt.ui.prefs create mode 100644 .settings/org.eclipse.m2e.core.prefs create mode 100644 README.md create mode 100644 liferay-portal-jsp-unit-test.launch create mode 100644 pom.xml create mode 100644 src/test/java/com/liferay/jsp/portlet/blogs/Blogs_view_entry_content_JSPTestSetUp.java create mode 100644 src/test/java/com/liferay/jsp/portlet/blogs/PortletBlogsAggregator_view_jsp_Test.java create mode 100644 src/test/java/com/liferay/jsp/portlet/blogs/PortletBlogs_view_entry_Jsp_Test.java create mode 100644 src/test/java/com/liferay/jsp/portlet/blogs/PortletBlogs_view_entry_content_Jsp_Test.java create mode 100644 src/test/java/com/liferay/jsp/taglib/ui/discussion/Discussion_page_jsp_TestSetUp.java create mode 100644 src/test/java/com/liferay/jsp/taglib/ui/discussion/TaglibUIDiscussion_page_jsp_Test.java create mode 100644 src/test/java/com/liferay/taglib/ui/UserDisplayTagTestSetUp.java create mode 100644 src/test/java/com/liferay/taglib/util/IncludeTagSetUp.java create mode 100644 src/test/java/com/liferay/test/jsp/JSPTestEngine.java create mode 100644 src/test/java/com/liferay/test/jsp/ResponseContent.java create mode 100644 src/test/java/com/liferay/test/jsp/Strip.java create mode 100644 src/test/java/com/liferay/test/junit/StringLookingAt.java create mode 100644 src/test/java/com/liferay/test/portal/jsp/LiferayJSPTestSetUp.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..449d866 --- /dev/null +++ b/.classpath @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2e6fce6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/.project +/target diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..6041344 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,375 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.annotation.inheritNullAnnotations=disabled +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.compliance=1.7 +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=ignore +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=warning +org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore +org.eclipse.jdt.core.compiler.problem.localVariableHiding=ignore +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=ignore +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.nonnullParameterAnnotationDropped=warning +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.syntacticNullAnalysisForFields=disabled +org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore +org.eclipse.jdt.core.compiler.problem.typeParameterHiding=warning +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.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.formatter.align_type_members_on_columns=false +org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=36 +org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation=0 +org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant=20 +org.eclipse.jdt.core.formatter.alignment_for_arguments_in_explicit_constructor_call=5 +org.eclipse.jdt.core.formatter.alignment_for_arguments_in_method_invocation=36 +org.eclipse.jdt.core.formatter.alignment_for_arguments_in_qualified_allocation_expression=36 +org.eclipse.jdt.core.formatter.alignment_for_assignment=20 +org.eclipse.jdt.core.formatter.alignment_for_binary_expression=20 +org.eclipse.jdt.core.formatter.alignment_for_compact_if=20 +org.eclipse.jdt.core.formatter.alignment_for_conditional_expression=36 +org.eclipse.jdt.core.formatter.alignment_for_enum_constants=20 +org.eclipse.jdt.core.formatter.alignment_for_expressions_in_array_initializer=20 +org.eclipse.jdt.core.formatter.alignment_for_method_declaration=0 +org.eclipse.jdt.core.formatter.alignment_for_multiple_fields=16 +org.eclipse.jdt.core.formatter.alignment_for_parameters_in_constructor_declaration=36 +org.eclipse.jdt.core.formatter.alignment_for_parameters_in_method_declaration=36 +org.eclipse.jdt.core.formatter.alignment_for_resources_in_try=80 +org.eclipse.jdt.core.formatter.alignment_for_selector_in_method_invocation=5 +org.eclipse.jdt.core.formatter.alignment_for_superclass_in_type_declaration=36 +org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_enum_declaration=21 +org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_type_declaration=36 +org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_constructor_declaration=20 +org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_method_declaration=20 +org.eclipse.jdt.core.formatter.alignment_for_union_type_in_multicatch=16 +org.eclipse.jdt.core.formatter.blank_lines_after_imports=1 +org.eclipse.jdt.core.formatter.blank_lines_after_package=1 +org.eclipse.jdt.core.formatter.blank_lines_before_field=0 +org.eclipse.jdt.core.formatter.blank_lines_before_first_class_body_declaration=1 +org.eclipse.jdt.core.formatter.blank_lines_before_imports=1 +org.eclipse.jdt.core.formatter.blank_lines_before_member_type=1 +org.eclipse.jdt.core.formatter.blank_lines_before_method=1 +org.eclipse.jdt.core.formatter.blank_lines_before_new_chunk=0 +org.eclipse.jdt.core.formatter.blank_lines_before_package=1 +org.eclipse.jdt.core.formatter.blank_lines_between_import_groups=1 +org.eclipse.jdt.core.formatter.blank_lines_between_type_declarations=1 +org.eclipse.jdt.core.formatter.brace_position_for_annotation_type_declaration=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_anonymous_type_declaration=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_array_initializer=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_block=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_block_in_case=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_constructor_declaration=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_enum_constant=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_enum_declaration=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_method_declaration=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_switch=end_of_line +org.eclipse.jdt.core.formatter.brace_position_for_type_declaration=end_of_line +org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_block_comment=true +org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_javadoc_comment=true +org.eclipse.jdt.core.formatter.comment.format_block_comments=true +org.eclipse.jdt.core.formatter.comment.format_header=false +org.eclipse.jdt.core.formatter.comment.format_html=false +org.eclipse.jdt.core.formatter.comment.format_javadoc_comments=true +org.eclipse.jdt.core.formatter.comment.format_line_comments=true +org.eclipse.jdt.core.formatter.comment.format_source_code=true +org.eclipse.jdt.core.formatter.comment.indent_parameter_description=true +org.eclipse.jdt.core.formatter.comment.indent_root_tags=true +org.eclipse.jdt.core.formatter.comment.insert_new_line_before_root_tags=insert +org.eclipse.jdt.core.formatter.comment.insert_new_line_for_parameter=insert +org.eclipse.jdt.core.formatter.comment.line_length=80 +org.eclipse.jdt.core.formatter.comment.new_lines_at_block_boundaries=true +org.eclipse.jdt.core.formatter.comment.new_lines_at_javadoc_boundaries=true +org.eclipse.jdt.core.formatter.comment.preserve_white_space_between_code_and_line_comments=false +org.eclipse.jdt.core.formatter.compact_else_if=true +org.eclipse.jdt.core.formatter.continuation_indentation=4 +org.eclipse.jdt.core.formatter.continuation_indentation_for_array_initializer=4 +org.eclipse.jdt.core.formatter.disabling_tag=@formatter\:off +org.eclipse.jdt.core.formatter.enabling_tag=@formatter\:on +org.eclipse.jdt.core.formatter.format_guardian_clause_on_one_line=false +org.eclipse.jdt.core.formatter.format_line_comment_starting_on_first_column=true +org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_annotation_declaration_header=true +org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_enum_constant_header=true +org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_enum_declaration_header=true +org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_type_header=true +org.eclipse.jdt.core.formatter.indent_breaks_compare_to_cases=true +org.eclipse.jdt.core.formatter.indent_empty_lines=false +org.eclipse.jdt.core.formatter.indent_statements_compare_to_block=true +org.eclipse.jdt.core.formatter.indent_statements_compare_to_body=true +org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_cases=true +org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_switch=false +org.eclipse.jdt.core.formatter.indentation.size=4 +org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_field=insert +org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_local_variable=insert +org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_method=insert +org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_package=insert +org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_parameter=do not insert +org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_type=insert +org.eclipse.jdt.core.formatter.insert_new_line_after_label=do not insert +org.eclipse.jdt.core.formatter.insert_new_line_after_opening_brace_in_array_initializer=insert +org.eclipse.jdt.core.formatter.insert_new_line_at_end_of_file_if_missing=do not insert +org.eclipse.jdt.core.formatter.insert_new_line_before_catch_in_try_statement=insert +org.eclipse.jdt.core.formatter.insert_new_line_before_closing_brace_in_array_initializer=insert +org.eclipse.jdt.core.formatter.insert_new_line_before_else_in_if_statement=insert +org.eclipse.jdt.core.formatter.insert_new_line_before_finally_in_try_statement=insert +org.eclipse.jdt.core.formatter.insert_new_line_before_while_in_do_statement=insert +org.eclipse.jdt.core.formatter.insert_new_line_in_empty_annotation_declaration=insert +org.eclipse.jdt.core.formatter.insert_new_line_in_empty_anonymous_type_declaration=insert +org.eclipse.jdt.core.formatter.insert_new_line_in_empty_block=insert +org.eclipse.jdt.core.formatter.insert_new_line_in_empty_enum_constant=insert +org.eclipse.jdt.core.formatter.insert_new_line_in_empty_enum_declaration=insert +org.eclipse.jdt.core.formatter.insert_new_line_in_empty_method_body=insert +org.eclipse.jdt.core.formatter.insert_new_line_in_empty_type_declaration=insert +org.eclipse.jdt.core.formatter.insert_space_after_and_in_type_parameter=insert +org.eclipse.jdt.core.formatter.insert_space_after_assignment_operator=insert +org.eclipse.jdt.core.formatter.insert_space_after_at_in_annotation=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_at_in_annotation_type_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_binary_operator=insert +org.eclipse.jdt.core.formatter.insert_space_after_closing_angle_bracket_in_type_arguments=insert +org.eclipse.jdt.core.formatter.insert_space_after_closing_angle_bracket_in_type_parameters=insert +org.eclipse.jdt.core.formatter.insert_space_after_closing_brace_in_block=insert +org.eclipse.jdt.core.formatter.insert_space_after_closing_paren_in_cast=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_colon_in_assert=insert +org.eclipse.jdt.core.formatter.insert_space_after_colon_in_case=insert +org.eclipse.jdt.core.formatter.insert_space_after_colon_in_conditional=insert +org.eclipse.jdt.core.formatter.insert_space_after_colon_in_for=insert +org.eclipse.jdt.core.formatter.insert_space_after_colon_in_labeled_statement=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_allocation_expression=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_annotation=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_array_initializer=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_constructor_declaration_parameters=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_constructor_declaration_throws=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_enum_constant_arguments=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_enum_declarations=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_explicitconstructorcall_arguments=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_for_increments=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_for_inits=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_method_declaration_parameters=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_method_declaration_throws=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_method_invocation_arguments=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_multiple_field_declarations=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_multiple_local_declarations=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_parameterized_type_reference=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_superinterfaces=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_type_arguments=insert +org.eclipse.jdt.core.formatter.insert_space_after_comma_in_type_parameters=insert +org.eclipse.jdt.core.formatter.insert_space_after_ellipsis=insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_parameterized_type_reference=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_type_arguments=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_type_parameters=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_brace_in_array_initializer=insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_bracket_in_array_allocation_expression=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_bracket_in_array_reference=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_annotation=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_cast=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_catch=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_constructor_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_enum_constant=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_for=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_if=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_method_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_method_invocation=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_parenthesized_expression=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_switch=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_synchronized=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_try=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_while=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_postfix_operator=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_prefix_operator=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_question_in_conditional=insert +org.eclipse.jdt.core.formatter.insert_space_after_question_in_wildcard=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_semicolon_in_for=insert +org.eclipse.jdt.core.formatter.insert_space_after_semicolon_in_try_resources=insert +org.eclipse.jdt.core.formatter.insert_space_after_unary_operator=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_and_in_type_parameter=insert +org.eclipse.jdt.core.formatter.insert_space_before_assignment_operator=insert +org.eclipse.jdt.core.formatter.insert_space_before_at_in_annotation_type_declaration=insert +org.eclipse.jdt.core.formatter.insert_space_before_binary_operator=insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_parameterized_type_reference=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_type_arguments=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_type_parameters=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_brace_in_array_initializer=insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_bracket_in_array_allocation_expression=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_bracket_in_array_reference=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_annotation=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_cast=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_catch=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_constructor_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_enum_constant=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_for=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_if=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_method_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_method_invocation=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_parenthesized_expression=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_switch=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_synchronized=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_try=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_while=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_colon_in_assert=insert +org.eclipse.jdt.core.formatter.insert_space_before_colon_in_case=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_colon_in_conditional=insert +org.eclipse.jdt.core.formatter.insert_space_before_colon_in_default=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_colon_in_for=insert +org.eclipse.jdt.core.formatter.insert_space_before_colon_in_labeled_statement=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_allocation_expression=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_annotation=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_array_initializer=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_constructor_declaration_parameters=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_constructor_declaration_throws=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_enum_constant_arguments=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_enum_declarations=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_explicitconstructorcall_arguments=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_for_increments=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_for_inits=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_method_declaration_parameters=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_method_declaration_throws=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_method_invocation_arguments=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_multiple_field_declarations=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_multiple_local_declarations=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_parameterized_type_reference=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_superinterfaces=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_type_arguments=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_comma_in_type_parameters=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_ellipsis=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_parameterized_type_reference=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_arguments=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_parameters=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_annotation_type_declaration=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_anonymous_type_declaration=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_array_initializer=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_block=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_constructor_declaration=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_enum_constant=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_enum_declaration=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_method_declaration=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_switch=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_type_declaration=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_bracket_in_array_allocation_expression=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_bracket_in_array_reference=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_bracket_in_array_type_reference=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_annotation=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_annotation_type_member_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_catch=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_constructor_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_enum_constant=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_for=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_if=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_method_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_method_invocation=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_parenthesized_expression=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_switch=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_synchronized=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_try=insert +org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_while=insert +org.eclipse.jdt.core.formatter.insert_space_before_parenthesized_expression_in_return=insert +org.eclipse.jdt.core.formatter.insert_space_before_parenthesized_expression_in_throw=insert +org.eclipse.jdt.core.formatter.insert_space_before_postfix_operator=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_prefix_operator=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_question_in_conditional=insert +org.eclipse.jdt.core.formatter.insert_space_before_question_in_wildcard=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_semicolon=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_semicolon_in_for=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_semicolon_in_try_resources=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_unary_operator=do not insert +org.eclipse.jdt.core.formatter.insert_space_between_brackets_in_array_type_reference=do not insert +org.eclipse.jdt.core.formatter.insert_space_between_empty_braces_in_array_initializer=do not insert +org.eclipse.jdt.core.formatter.insert_space_between_empty_brackets_in_array_allocation_expression=do not insert +org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_annotation_type_member_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_constructor_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_enum_constant=do not insert +org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_declaration=do not insert +org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_invocation=do not insert +org.eclipse.jdt.core.formatter.join_lines_in_comments=true +org.eclipse.jdt.core.formatter.join_wrapped_lines=false +org.eclipse.jdt.core.formatter.keep_else_statement_on_same_line=false +org.eclipse.jdt.core.formatter.keep_empty_array_initializer_on_one_line=false +org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line=false +org.eclipse.jdt.core.formatter.keep_then_statement_on_same_line=false +org.eclipse.jdt.core.formatter.lineSplit=80 +org.eclipse.jdt.core.formatter.never_indent_block_comments_on_first_column=false +org.eclipse.jdt.core.formatter.never_indent_line_comments_on_first_column=false +org.eclipse.jdt.core.formatter.number_of_blank_lines_at_beginning_of_method_body=0 +org.eclipse.jdt.core.formatter.number_of_empty_lines_to_preserve=1 +org.eclipse.jdt.core.formatter.put_empty_statement_on_new_line=false +org.eclipse.jdt.core.formatter.tabulation.char=tab +org.eclipse.jdt.core.formatter.tabulation.size=4 +org.eclipse.jdt.core.formatter.use_on_off_tags=false +org.eclipse.jdt.core.formatter.use_tabs_only_for_leading_indentations=false +org.eclipse.jdt.core.formatter.wrap_before_binary_operator=false +org.eclipse.jdt.core.formatter.wrap_before_or_operator_multicatch=true +org.eclipse.jdt.core.formatter.wrap_outer_expressions_when_nested=true diff --git a/.settings/org.eclipse.jdt.ui.prefs b/.settings/org.eclipse.jdt.ui.prefs new file mode 100644 index 0000000..991e675 --- /dev/null +++ b/.settings/org.eclipse.jdt.ui.prefs @@ -0,0 +1,56 @@ +eclipse.preferences.version=1 +editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true +formatter_profile=_Liferay [plug-in] +formatter_settings_version=12 +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=false +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=false +sp_cleanup.correct_indentation=false +sp_cleanup.format_source_code=true +sp_cleanup.format_source_code_changes_only=false +sp_cleanup.make_local_variable_final=false +sp_cleanup.make_parameters_final=false +sp_cleanup.make_private_fields_final=true +sp_cleanup.make_type_abstract_if_missing_method=false +sp_cleanup.make_variable_declarations_final=false +sp_cleanup.never_use_blocks=false +sp_cleanup.never_use_parentheses_in_expressions=true +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=false +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=false +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=false +sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true +sp_cleanup.use_this_for_non_static_method_access=false +sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/README.md b/README.md new file mode 100644 index 0000000..7510be3 --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +Liferay Portal JSP Unit Test examples +===================================== + +How to test actual rendered HTML from your Portal JSP pages and taglibs, without the need to start a web container. + + +How to use +========== + +Your workspace must have a working "portal-master" project. + +Eclipse, "Import", "Existing Maven project". + +Run the tests using the `liferay-portal-jsp-unit-test` launch configuration. + + +Under the hood +============== + +If you ever need to configure a new Liferay Portal JSP Unit Test project from scratch. + +## Eclipse project, Java Build Path + +Required projects in the build path -> `portal-master` + +Libraries -> `portlet.jar - portal-master/lib/global` + +## Launch configuration + +Classpath, User Entries -> `Maven Dependencies` _before_ `liferay-portal-jsp-unit-test` + +Source, Source Lookup Path -> `portal-master` _before_ `Default` + + +Acknowledgements +================ + +Adapted from work by Günther Enthaler, who wrote the original _Jetty JSP unit test simple example_. + +https://github.com/genthaler/jetty-jsp-unit-test-simple/ diff --git a/liferay-portal-jsp-unit-test.launch b/liferay-portal-jsp-unit-test.launch new file mode 100644 index 0000000..29011a4 --- /dev/null +++ b/liferay-portal-jsp-unit-test.launch @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..09cc02b --- /dev/null +++ b/pom.xml @@ -0,0 +1,67 @@ + + 4.0.0 + com.liferay.arbo + liferay-portal-jsp-unit-test + 1.0-SNAPSHOT + Liferay Portal JSP Unit Test Examples + http://maven.apache.org + + + org.eclipse.jetty + jetty-jsp + ${jetty.version} + test + + + org.eclipse.jetty + test-jetty-servlet + ${jetty.version} + test + + + junit + junit + [3.8.1,) + test + + + org.powermock + + powermock-release-with-junit-mockito-dependencies + + 1.5.1 + pom + test + + + org.springframework + spring-test + 4.0.1.RELEASE + test + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.7 + 1.7 + + + + org.eclipse.jetty + ${jetty.version} + + false + + + + + + 8.1.15.v20140411 + + diff --git a/src/test/java/com/liferay/jsp/portlet/blogs/Blogs_view_entry_content_JSPTestSetUp.java b/src/test/java/com/liferay/jsp/portlet/blogs/Blogs_view_entry_content_JSPTestSetUp.java new file mode 100644 index 0000000..ab1558e --- /dev/null +++ b/src/test/java/com/liferay/jsp/portlet/blogs/Blogs_view_entry_content_JSPTestSetUp.java @@ -0,0 +1,114 @@ +package com.liferay.jsp.portlet.blogs; + +import static org.mockito.Mockito.mock; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; + +import java.util.Date; + +import javax.servlet.http.HttpServletRequest; + +import org.eclipse.jetty.testing.ServletTester; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.powermock.api.mockito.PowerMockito; + +import com.liferay.jsp.taglib.ui.discussion.Discussion_page_jsp_TestSetUp; +import com.liferay.portal.kernel.comment.CommentManager; +import com.liferay.portal.kernel.settings.SettingsFactory; +import com.liferay.portal.kernel.settings.SettingsFactoryUtil; +import com.liferay.portal.model.Layout; +import com.liferay.portal.security.permission.PermissionChecker; +import com.liferay.portal.util.test.RandomTestUtil; +import com.liferay.portlet.blogs.BlogsPortletInstanceSettings; +import com.liferay.portlet.blogs.BlogsSettings; +import com.liferay.portlet.blogs.model.BlogsEntry; +import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission; +import com.liferay.portlet.blogs.util.BlogsUtil; +import com.liferay.test.jsp.JSPTestEngine; + +class Blogs_view_entry_content_JSPTestSetUp { + + private JSPTestEngine engine; + + public Blogs_view_entry_content_JSPTestSetUp(JSPTestEngine engine) { + this.engine = engine; + } + + void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + setUpBlogsEntryPermission(); + + setupComments(); + + new Discussion_page_jsp_TestSetUp().setUp(); + } + + void setUpHttpServletRequest(HttpServletRequest request) { + request.setAttribute(com.liferay.portal.util.WebKeys.BLOGS_ENTRY, blogsEntry); + + request.setAttribute("view_entry_content.jsp-entry", blogsEntry); + + when(blogsEntry.isVisible()).thenReturn(true); + when(blogsEntry.getDisplayDate()).thenReturn(new Date()); + } + + void setUpBlogsSettings() { + ServletTester tester = engine.getServletTester(); + + // because NPE Long null to long + tester.setAttribute("scopeGroupId", RandomTestUtil.randomLong()); + + blogsSettings = mock(BlogsSettings.class); + + mockStatic(BlogsSettings.class, Mockito.CALLS_REAL_METHODS); + + PowerMockito.stub(PowerMockito.method(BlogsSettings.class, "getInstance", Long.TYPE)).toReturn(blogsSettings); + } + + void setUpBlogsPortletInstanceSettings() throws Exception { + // BlogsPortletInstanceSettings static initializer :-( + new SettingsFactoryUtil().setSettingsFactory(mock(SettingsFactory.class)); + + blogsPortletInstanceSettings = mock(BlogsPortletInstanceSettings.class); + + mockStatic(BlogsPortletInstanceSettings.class, Mockito.CALLS_REAL_METHODS); + + PowerMockito.stub( + PowerMockito.method( + BlogsPortletInstanceSettings.class, "getInstance", + Layout.class, String.class + )).toReturn(blogsPortletInstanceSettings); + + when(blogsPortletInstanceSettings.getDisplayStyle()).thenReturn(RandomTestUtil.randomString()); + when(blogsPortletInstanceSettings.isEnableComments()).thenReturn(true); + } + + private void setupComments() throws Exception { + mockStatic(BlogsUtil.class, Mockito.CALLS_REAL_METHODS); + PowerMockito.stub( + PowerMockito.method(BlogsUtil.class, "getCommentManager") + ).toReturn( + commentManager); + } + + private void setUpBlogsEntryPermission() { + mockStatic(BlogsEntryPermission.class, Mockito.CALLS_REAL_METHODS); + PowerMockito.stub(PowerMockito.method( + BlogsEntryPermission.class, "contains", + PermissionChecker.class, BlogsEntry.class, String.class + )).toReturn(true); + } + + private BlogsPortletInstanceSettings blogsPortletInstanceSettings; + + BlogsSettings blogsSettings; + + + public @Mock BlogsEntry blogsEntry; + + public @Mock CommentManager commentManager; + +} diff --git a/src/test/java/com/liferay/jsp/portlet/blogs/PortletBlogsAggregator_view_jsp_Test.java b/src/test/java/com/liferay/jsp/portlet/blogs/PortletBlogsAggregator_view_jsp_Test.java new file mode 100644 index 0000000..ced8848 --- /dev/null +++ b/src/test/java/com/liferay/jsp/portlet/blogs/PortletBlogsAggregator_view_jsp_Test.java @@ -0,0 +1,161 @@ + +package com.liferay.jsp.portlet.blogs; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.support.membermodification.MemberMatcher.method; +import static org.powermock.api.support.membermodification.MemberModifier.stub; + +import java.text.DateFormat; +import java.util.Collections; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; + +import javax.portlet.WindowState; +import javax.servlet.http.HttpServletRequest; + +import org.eclipse.jetty.testing.ServletTester; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import com.liferay.portal.kernel.exception.PortalException; +import com.liferay.portal.kernel.exception.SystemException; +import com.liferay.portal.kernel.settings.SettingsFactory; +import com.liferay.portal.kernel.settings.SettingsFactoryUtil; +import com.liferay.portal.kernel.util.FastDateFormatFactory; +import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil; +import com.liferay.portal.security.permission.PermissionChecker; +import com.liferay.portal.util.test.RandomTestUtil; +import com.liferay.portlet.PortletURLUtil; +import com.liferay.portlet.blogs.model.BlogsEntry; +import com.liferay.portlet.blogs.service.BlogsEntryService; +import com.liferay.portlet.blogs.service.BlogsEntryServiceUtil; +import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission; +import com.liferay.portlet.blogs.util.BlogsUtil; +import com.liferay.portlet.trash.util.Trash; +import com.liferay.portlet.trash.util.TrashUtil; +import com.liferay.test.jsp.JSPTestEngine; +import com.liferay.test.jsp.JSPTestEngine.HttpServletRequestPrepare; +import com.liferay.test.jsp.ResponseContent; +import com.liferay.test.portal.jsp.LiferayJSPTestSetUp; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ + PortletURLUtil.class, FastDateFormatFactoryUtil.class, + BlogsUtil.class, BlogsEntryPermission.class, BlogsEntryServiceUtil.class +}) +@PowerMockIgnore("javax.tools.*") +public class PortletBlogsAggregator_view_jsp_Test + implements HttpServletRequestPrepare { + + @Before + public void setUp() throws Exception { + engine.setResourceBase("/Users/arbo/git/liferay-portal/portal-web/docroot"); + engine.setURI("/html/portlet/blogs_aggregator/view.jsp"); + + MockitoAnnotations.initMocks(this); + + liferayJSP.setUp(); + + setupMocks(); + } + + @Test + public void testNoBlogs() throws Exception { + Mockito.when( + blogsEntryService.getGroupEntries( + Matchers.anyLong(), (Date)Matchers.any(), Matchers.anyInt(), + Matchers.anyInt()) + ).thenReturn(Collections. emptyList()); + + ResponseContent response = engine.execute(); + + response.assertContains("there-are-no-blogs"); + } + + protected void setupMocks() throws Exception, PortalException, + SystemException { + ServletTester tester = engine.getServletTester(); + + tester.setAttribute("scopeGroupId", RandomTestUtil.randomLong()); + + mockStatic(BlogsEntryPermission.class); + when( + BlogsEntryPermission.contains( + (PermissionChecker)any(), (BlogsEntry)any(), anyString())).thenReturn( + true); + + new SettingsFactoryUtil().setSettingsFactory(mock(SettingsFactory.class)); + + setUpFastDateFormatFactoryUtil(); + + when(entry.isVisible()).thenReturn(true); + when(entry.getDisplayDate()).thenReturn(new Date()); + + new TrashUtil().setTrash(trash); + + when(liferayJSP.portletPreferences.getValue("organizationId", "0")) + .thenReturn(String.valueOf(RandomTestUtil.randomLong())); + + when(liferayJSP.portletPreferences.getValue("selectionMethod", "users")) + .thenReturn("groups"); + + when(liferayJSP.liferayPortletRequest.getParameter("resetCur")) + .thenReturn("true"); + + when(liferayJSP.liferayPortletRequest.getWindowState()) + .thenReturn(WindowState.NORMAL); + + setUpBlogsEntryServiceUtil(); + + } + + protected void setUpFastDateFormatFactoryUtil() { + when( + fastDateFormatFactory.getDateTime( + (Locale)any(), (TimeZone)any())).thenReturn( + DateFormat.getInstance()); + new FastDateFormatFactoryUtil().setFastDateFormatFactory(fastDateFormatFactory); + } + + protected void setUpBlogsEntryServiceUtil() throws PortalException { + mockStatic(BlogsEntryServiceUtil.class, Mockito.CALLS_REAL_METHODS); + stub(method( + BlogsEntryServiceUtil.class, "getService")).toReturn( + blogsEntryService); + } + + @Override + public void prepare(HttpServletRequest request) { + liferayJSP.prepareRequest(request); + request.setAttribute("view_entry_content.jsp-entry", entry); + } + + JSPTestEngine engine = new JSPTestEngine(this); + + LiferayJSPTestSetUp liferayJSP = new LiferayJSPTestSetUp(engine); + + @Mock + BlogsEntry entry; + + @Mock + FastDateFormatFactory fastDateFormatFactory; + + @Mock + Trash trash; + + @Mock + BlogsEntryService blogsEntryService; +} diff --git a/src/test/java/com/liferay/jsp/portlet/blogs/PortletBlogs_view_entry_Jsp_Test.java b/src/test/java/com/liferay/jsp/portlet/blogs/PortletBlogs_view_entry_Jsp_Test.java new file mode 100644 index 0000000..bd824aa --- /dev/null +++ b/src/test/java/com/liferay/jsp/portlet/blogs/PortletBlogs_view_entry_Jsp_Test.java @@ -0,0 +1,255 @@ + +package com.liferay.jsp.portlet.blogs; + +import static org.mockito.Matchers.anyInt; +import static org.mockito.Matchers.anyLong; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.support.membermodification.MemberMatcher.method; +import static org.powermock.api.support.membermodification.MemberModifier.stub; + +import javax.servlet.http.HttpServletRequest; + +import org.eclipse.jetty.testing.ServletTester; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import com.liferay.portal.kernel.bean.BeanParamUtil; +import com.liferay.portal.kernel.exception.PortalException; +import com.liferay.portal.model.Company; +import com.liferay.portal.model.User; +import com.liferay.portal.security.permission.PermissionChecker; +import com.liferay.portal.service.SubscriptionLocalService; +import com.liferay.portal.service.SubscriptionLocalServiceUtil; +import com.liferay.portal.service.WorkflowDefinitionLinkLocalService; +import com.liferay.portal.service.WorkflowDefinitionLinkLocalServiceUtil; +import com.liferay.portal.theme.PortletDisplay; +import com.liferay.portal.util.SessionClicks; +import com.liferay.portal.util.test.RandomTestUtil; +import com.liferay.portlet.PortletURLUtil; +import com.liferay.portlet.asset.service.AssetCategoryServiceUtil; +import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil; +import com.liferay.portlet.asset.service.AssetEntryServiceUtil; +import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil; +import com.liferay.portlet.asset.service.AssetTagServiceUtil; +import com.liferay.portlet.asset.service.AssetVocabularyServiceUtil; +import com.liferay.portlet.blogs.BlogsPortletInstanceSettings; +import com.liferay.portlet.blogs.BlogsSettings; +import com.liferay.portlet.blogs.model.BlogsEntry; +import com.liferay.portlet.blogs.service.BlogsEntryLocalService; +import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil; +import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission; +import com.liferay.portlet.blogs.util.BlogsUtil; +import com.liferay.portlet.messageboards.model.MBMessage; +import com.liferay.portlet.messageboards.model.MBMessageDisplay; +import com.liferay.portlet.messageboards.model.MBThread; +import com.liferay.portlet.messageboards.service.MBMessageLocalService; +import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil; +import com.liferay.portlet.messageboards.service.permission.MBDiscussionPermission; +import com.liferay.portlet.trash.util.Trash; +import com.liferay.portlet.trash.util.TrashUtil; +import com.liferay.test.jsp.JSPTestEngine; +import com.liferay.test.jsp.JSPTestEngine.HttpServletRequestPrepare; +import com.liferay.test.jsp.ResponseContent; +import com.liferay.test.portal.jsp.LiferayJSPTestSetUp; + +@PowerMockIgnore("javax.tools.*") +@PrepareForTest({ + AssetEntryLocalServiceUtil.class, AssetEntryServiceUtil.class, + AssetTagLocalServiceUtil.class, AssetTagServiceUtil.class, + AssetVocabularyServiceUtil.class, + AssetCategoryServiceUtil.class, + PortletURLUtil.class, + BlogsUtil.class, BlogsPortletInstanceSettings.class, BlogsSettings.class, + BlogsEntryPermission.class, BlogsEntryLocalServiceUtil.class, + BeanParamUtil.class, SessionClicks.class, + MBMessageLocalServiceUtil.class, MBDiscussionPermission.class, + SubscriptionLocalServiceUtil.class, + WorkflowDefinitionLinkLocalServiceUtil.class +}) +@RunWith(PowerMockRunner.class) +public class PortletBlogs_view_entry_Jsp_Test + implements HttpServletRequestPrepare { + + @Before + public void setUp() throws Exception { + engine.setResourceBase("/Users/arbo/git/liferay-portal/portal-web/docroot"); + engine.setURI("/html/portlet/blogs/view_entry.jsp"); + + MockitoAnnotations.initMocks(this); + + liferayJSP.setUp(); + + blogsJSP.setUp(); + blogsJSP.setUpBlogsPortletInstanceSettings(); + blogsJSP.setUpBlogsSettings(); + + setupMocks(); + + setUpAssets(); + + setUpBlogsEntryLocalServiceUtil(); + + mockStatic(SessionClicks.class); + + setUpMBMessageLocalServiceUtil(); + + ServletTester tester = engine.getServletTester(); + tester.setAttribute("company", company); + tester.setAttribute("user", mock(User.class)); + liferayJSP.themeDisplay.setCompany(company); + + setUpSubscriptionLocalServiceUtil(); + + setUpWorkflowDefinitionLinkLocalServiceUtil(); + } + + @Test + public void testDiscussionTaglib() throws Exception { + engine.execute(); + } + + @Test + public void testInTrash() throws Exception { + when(trash.isInTrash(anyString(), anyLong())).thenReturn(true); + + ResponseContent response = engine.execute(); + + response.assertContains( + "commenting-is-disabled-because-this-entry-is-in-the-recycle-bin"); + } + + @Test + public void testBeTheFirst() throws Exception { + when( + mbMessageLocalService.getThreadMessagesCount(anyLong(), anyInt())) + .thenReturn(1); + + ResponseContent response = engine.execute(); + + response.assertContains("be-the-first"); + } + + private @Mock + MBMessageLocalService mbMessageLocalService; + + private @Mock + MBMessageDisplay mbMessageDisplay; + + private @Mock + SubscriptionLocalService subscriptionLocalService; + + @Mock + WorkflowDefinitionLinkLocalService workflowDefinitionLinkLocalService; + + @Mock + private Company company; + + private void setUpWorkflowDefinitionLinkLocalServiceUtil() { + mockStatic( + WorkflowDefinitionLinkLocalServiceUtil.class, + Mockito.CALLS_REAL_METHODS); + stub(method( + WorkflowDefinitionLinkLocalServiceUtil.class, "getService")).toReturn( + workflowDefinitionLinkLocalService); + } + + private void setUpSubscriptionLocalServiceUtil() { + mockStatic( + SubscriptionLocalServiceUtil.class, Mockito.CALLS_REAL_METHODS); + stub(method( + SubscriptionLocalServiceUtil.class, "getService")).toReturn( + subscriptionLocalService); + } + + protected void setUpMBMessageLocalServiceUtil() throws PortalException { + mockStatic(MBMessageLocalServiceUtil.class, Mockito.CALLS_REAL_METHODS); + stub(method( + MBMessageLocalServiceUtil.class, "getService")).toReturn( + mbMessageLocalService); + when( + mbMessageLocalService.getDiscussionMessageDisplay( + anyLong(), anyLong(), anyString(), + anyLong(), anyInt(), anyString())) + .thenReturn(mbMessageDisplay); + + when(mbMessageDisplay.getThread()).thenReturn(thread); + + when(mbMessageLocalService.getMessage(anyLong())).thenReturn( + rootMessage); + + mockStatic(MBDiscussionPermission.class, Mockito.CALLS_REAL_METHODS); + stub(method( + MBDiscussionPermission.class, "contains", + PermissionChecker.class, Long.TYPE, Long.TYPE, + String.class, Long.TYPE, Long.TYPE, String.class)).toReturn(true); + } + + protected void setUpBlogsEntryLocalServiceUtil() throws PortalException { + mockStatic(BlogsEntryLocalServiceUtil.class, Mockito.CALLS_REAL_METHODS); + stub(method( + BlogsEntryLocalServiceUtil.class, "getService")).toReturn( + blogsEntryLocalService); + when( + blogsEntryLocalService.getEntriesPrevAndNext(Mockito.anyLong())).thenReturn( + new BlogsEntry[3]); + } + + private void setUpAssets() { + mockStatic(AssetEntryLocalServiceUtil.class); + PowerMockito.mockStatic(AssetEntryServiceUtil.class); + PowerMockito.mockStatic(AssetTagLocalServiceUtil.class); + PowerMockito.mockStatic(AssetTagServiceUtil.class); + PowerMockito.mockStatic(AssetVocabularyServiceUtil.class); + PowerMockito.mockStatic(AssetCategoryServiceUtil.class); + } + + protected void setupMocks() throws Exception { + ServletTester tester = engine.getServletTester(); + + tester.setAttribute("portletDisplay", portletDisplay); + when(portletDisplay.getId()).thenReturn(RandomTestUtil.randomString()); + + PowerMockito.mockStatic(BeanParamUtil.class); + + new TrashUtil().setTrash(trash); + } + + @Override + public void prepare(HttpServletRequest request) { + liferayJSP.prepareRequest(request); + blogsJSP.setUpHttpServletRequest(request); + } + + JSPTestEngine engine = new JSPTestEngine(this); + + LiferayJSPTestSetUp liferayJSP = new LiferayJSPTestSetUp(engine); + + private Blogs_view_entry_content_JSPTestSetUp blogsJSP = + new Blogs_view_entry_content_JSPTestSetUp(engine); + + @Mock + PortletDisplay portletDisplay; + + @Mock + BlogsEntryLocalService blogsEntryLocalService; + + @Mock + MBThread thread; + + @Mock + MBMessage rootMessage; + + @Mock + private Trash trash; +} diff --git a/src/test/java/com/liferay/jsp/portlet/blogs/PortletBlogs_view_entry_content_Jsp_Test.java b/src/test/java/com/liferay/jsp/portlet/blogs/PortletBlogs_view_entry_content_Jsp_Test.java new file mode 100644 index 0000000..cc84161 --- /dev/null +++ b/src/test/java/com/liferay/jsp/portlet/blogs/PortletBlogs_view_entry_content_Jsp_Test.java @@ -0,0 +1,124 @@ + +package com.liferay.jsp.portlet.blogs; + +import static org.mockito.Matchers.anyLong; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.powermock.api.mockito.PowerMockito.when; + +import javax.servlet.http.HttpServletRequest; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockitoAnnotations; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import com.liferay.portal.kernel.portlet.LiferayPortletResponse; +import com.liferay.portal.kernel.util.JavaConstants; +import com.liferay.portal.util.test.RandomTestUtil; +import com.liferay.portlet.PortletURLUtil; +import com.liferay.portlet.asset.service.AssetCategoryServiceUtil; +import com.liferay.portlet.asset.service.AssetTagServiceUtil; +import com.liferay.portlet.asset.service.AssetVocabularyServiceUtil; +import com.liferay.portlet.blogs.BlogsPortletInstanceSettings; +import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission; +import com.liferay.portlet.blogs.util.BlogsUtil; +import com.liferay.portlet.trash.util.Trash; +import com.liferay.portlet.trash.util.TrashUtil; +import com.liferay.test.jsp.JSPTestEngine; +import com.liferay.test.jsp.JSPTestEngine.HttpServletRequestPrepare; +import com.liferay.test.jsp.ResponseContent; +import com.liferay.test.portal.jsp.LiferayJSPTestSetUp; + +@RunWith(PowerMockRunner.class) +@PrepareOnlyThisForTest({ + PortletURLUtil.class, BlogsUtil.class, + BlogsEntryPermission.class, BlogsPortletInstanceSettings.class, + AssetTagServiceUtil.class, + AssetVocabularyServiceUtil.class, AssetCategoryServiceUtil.class +}) +@PowerMockIgnore("javax.tools.*") +public class PortletBlogs_view_entry_content_Jsp_Test + implements HttpServletRequestPrepare { + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + engine.setResourceBase("/Users/arbo/git/liferay-portal/portal-web/docroot"); + engine.setURI("/html/portlet/blogs/view_entry_content.jsp"); + + liferayJSP.setUp(); + blogsJSP.setUp(); + blogsJSP.setUpBlogsPortletInstanceSettings(); + + setupMocks(); + + setUpAssets(); + } + + private void setUpAssets() { + PowerMockito.mockStatic(AssetVocabularyServiceUtil.class); + PowerMockito.mockStatic(AssetCategoryServiceUtil.class); + PowerMockito.mockStatic(AssetTagServiceUtil.class); + } + + @Test + public void test1Comment() throws Exception { + testCommentCount(1, "1 comment"); + } + + @Test + public void testNComments() throws Exception { + testCommentCount(142857, "142857 comments"); + } + + protected void testCommentCount(int commentCount, String rendered) + throws Exception { + when( + blogsJSP.commentManager.getCommentsCount( + anyString(), anyLong())).thenReturn(commentCount); + + ResponseContent response = engine.execute(); + + response.assertContains( + "" + + rendered + + ""); + } + + protected void setupMocks() throws Exception { + new TrashUtil().setTrash(mock(Trash.class)); + } + + @Override + public void prepare(HttpServletRequest request) { + blogsJSP.setUpHttpServletRequest(request); + + prepareTaglibs(request); + } + + private void prepareTaglibs(HttpServletRequest request) { + liferayJSP.prepareRequest(request); + + LiferayPortletResponse liferayPortletResponse = + liferayJSP.liferayPortletResponse; + + request.setAttribute( + JavaConstants.JAVAX_PORTLET_RESPONSE, liferayPortletResponse); + + when(liferayPortletResponse.getNamespace()).thenReturn( + RandomTestUtil.randomString()); + } + + JSPTestEngine engine = new JSPTestEngine(this); + + LiferayJSPTestSetUp liferayJSP = new LiferayJSPTestSetUp(engine); + + private Blogs_view_entry_content_JSPTestSetUp blogsJSP = + new Blogs_view_entry_content_JSPTestSetUp(engine); +} diff --git a/src/test/java/com/liferay/jsp/taglib/ui/discussion/Discussion_page_jsp_TestSetUp.java b/src/test/java/com/liferay/jsp/taglib/ui/discussion/Discussion_page_jsp_TestSetUp.java new file mode 100644 index 0000000..6d95130 --- /dev/null +++ b/src/test/java/com/liferay/jsp/taglib/ui/discussion/Discussion_page_jsp_TestSetUp.java @@ -0,0 +1,33 @@ +package com.liferay.jsp.taglib.ui.discussion; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.powermock.api.mockito.PowerMockito.when; + +import java.text.DateFormat; +import java.util.Locale; +import java.util.TimeZone; + +import com.liferay.portal.kernel.util.FastDateFormatFactory; +import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil; + +public class Discussion_page_jsp_TestSetUp { + + public void setUp() { + setUpFastDateFormatFactoryUtil(); + } + + private void setUpFastDateFormatFactoryUtil() { + FastDateFormatFactory fastDateFormatFactory = + mock(FastDateFormatFactory.class); + + when( + fastDateFormatFactory.getDateTime( + (Locale)any(), (TimeZone)any()) + ).thenReturn(DateFormat.getInstance()); + + new FastDateFormatFactoryUtil().setFastDateFormatFactory( + fastDateFormatFactory); + } + +} diff --git a/src/test/java/com/liferay/jsp/taglib/ui/discussion/TaglibUIDiscussion_page_jsp_Test.java b/src/test/java/com/liferay/jsp/taglib/ui/discussion/TaglibUIDiscussion_page_jsp_Test.java new file mode 100644 index 0000000..ead16cd --- /dev/null +++ b/src/test/java/com/liferay/jsp/taglib/ui/discussion/TaglibUIDiscussion_page_jsp_Test.java @@ -0,0 +1,383 @@ + +package com.liferay.jsp.taglib.ui.discussion; + +import static org.mockito.Matchers.anyInt; +import static org.mockito.Matchers.anyLong; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.support.membermodification.MemberMatcher.method; +import static org.powermock.api.support.membermodification.MemberModifier.stub; + +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.servlet.http.HttpServletRequest; + +import org.eclipse.jetty.testing.ServletTester; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import com.liferay.portal.kernel.exception.PortalException; +import com.liferay.portal.kernel.util.Http; +import com.liferay.portal.kernel.util.HttpUtil; +import com.liferay.portal.model.Company; +import com.liferay.portal.model.ModelHintsUtil; +import com.liferay.portal.model.User; +import com.liferay.portal.security.permission.PermissionChecker; +import com.liferay.portal.service.SubscriptionLocalService; +import com.liferay.portal.service.SubscriptionLocalServiceUtil; +import com.liferay.portal.service.UserLocalServiceUtil; +import com.liferay.portal.service.WorkflowDefinitionLinkLocalService; +import com.liferay.portal.service.WorkflowDefinitionLinkLocalServiceUtil; +import com.liferay.portal.theme.PortletDisplay; +import com.liferay.portal.util.SessionClicks; +import com.liferay.portal.util.test.RandomTestUtil; +import com.liferay.portlet.PortletURLUtil; +import com.liferay.portlet.asset.service.AssetCategoryServiceUtil; +import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil; +import com.liferay.portlet.asset.service.AssetEntryServiceUtil; +import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil; +import com.liferay.portlet.asset.service.AssetTagServiceUtil; +import com.liferay.portlet.asset.service.AssetVocabularyServiceUtil; +import com.liferay.portlet.blogs.BlogsPortletInstanceSettings; +import com.liferay.portlet.blogs.BlogsSettings; +import com.liferay.portlet.blogs.model.BlogsEntry; +import com.liferay.portlet.blogs.service.BlogsEntryLocalService; +import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil; +import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission; +import com.liferay.portlet.blogs.util.BlogsUtil; +import com.liferay.portlet.messageboards.model.MBMessage; +import com.liferay.portlet.messageboards.model.MBMessageDisplay; +import com.liferay.portlet.messageboards.model.MBThread; +import com.liferay.portlet.messageboards.service.MBMessageLocalService; +import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil; +import com.liferay.portlet.messageboards.service.permission.MBDiscussionPermission; +import com.liferay.portlet.ratings.service.RatingsEntryLocalService; +import com.liferay.portlet.ratings.service.RatingsEntryLocalServiceUtil; +import com.liferay.portlet.ratings.service.RatingsStatsLocalService; +import com.liferay.portlet.ratings.service.RatingsStatsLocalServiceUtil; +import com.liferay.portlet.trash.util.Trash; +import com.liferay.portlet.trash.util.TrashUtil; +import com.liferay.taglib.ui.UserDisplayTagTestSetUp; +import com.liferay.taglib.util.IncludeTagSetUp; +import com.liferay.test.jsp.JSPTestEngine; +import com.liferay.test.jsp.JSPTestEngine.HttpServletRequestPrepare; +import com.liferay.test.jsp.ResponseContent; +import com.liferay.test.portal.jsp.LiferayJSPTestSetUp; + +@PowerMockIgnore("javax.tools.*") +@PrepareForTest({ + AssetEntryLocalServiceUtil.class, AssetEntryServiceUtil.class, + AssetTagLocalServiceUtil.class, AssetTagServiceUtil.class, + AssetVocabularyServiceUtil.class, + AssetCategoryServiceUtil.class, + PortletURLUtil.class, + BlogsUtil.class, BlogsPortletInstanceSettings.class, BlogsSettings.class, + BlogsEntryPermission.class, BlogsEntryLocalServiceUtil.class, + SessionClicks.class, + MBMessageLocalServiceUtil.class, MBDiscussionPermission.class, + SubscriptionLocalServiceUtil.class, + RatingsEntryLocalServiceUtil.class, RatingsStatsLocalServiceUtil.class, + WorkflowDefinitionLinkLocalServiceUtil.class, + UserLocalServiceUtil.class, ModelHintsUtil.class +}) +@RunWith(PowerMockRunner.class) +public class TaglibUIDiscussion_page_jsp_Test + implements HttpServletRequestPrepare { + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + engine.setResourceBase("/Users/arbo/git/liferay-portal/portal-web/docroot"); + engine.setURI("/html/taglib/ui/discussion/page.jsp"); + + liferayJSP.setUp(); + + new Discussion_page_jsp_TestSetUp().setUp(); + + userDisplayTagTestSetUp = new UserDisplayTagTestSetUp(liferayJSP.user); + userDisplayTagTestSetUp.setUp(); + + new IncludeTagSetUp().setUp(); + + setupMocks(); + + setUpAssets(); + + setUpBlogsEntryLocalServiceUtil(); + + mockStatic(SessionClicks.class); + + setUpMBMessageLocalServiceUtil(); + + ServletTester tester = engine.getServletTester(); + tester.setAttribute("company", company); + tester.setAttribute("user", mock(User.class)); + liferayJSP.themeDisplay.setCompany(company); + + setUpSubscriptionLocalServiceUtil(); + + setUpWorkflowDefinitionLinkLocalServiceUtil(); + + setUpRatingsEntryLocalServiceUtil(); + setUpRatingsStatsLocalServiceUtil(); + } + + @Override + public void prepare(HttpServletRequest request) { + liferayJSP.prepareRequest(request); + userDisplayTagTestSetUp.setUpRequest(request); + } + + @Test + public void testDiscussionTaglib() throws Exception { + engine.execute(); + } + + @Test + public void testInTrash() throws Exception { + when(trash.isInTrash(anyString(), anyLong())).thenReturn(true); + + ResponseContent response = engine.execute(); + + response.assertContains( + "commenting-is-disabled-because-this-entry-is-in-the-recycle-bin"); + } + + @Test + public void testBeTheFirst() throws Exception { + when_getThreadMessagesCount(1); + + ResponseContent response = engine.execute(); + + response.assertContains("be-the-first"); + } + + @Test + public void testAddComment() throws Exception { + when_getThreadMessagesCount(2); + + ResponseContent response = engine.execute(); + + response.assertContains( + "add-comment"); + } + + @Test + public void testMessagesRendered() throws Exception { + MBMessage m1 = newMBMessage(), m2 = newMBMessage(); + + String body = RandomTestUtil.randomString(); + + when(m2.getBody()).thenReturn(body); + + List messages = Arrays.asList(m1, m2); + + int messagesCount = messages.size(); + when_getThreadMessagesCount(messagesCount); + + when(mbMessageLocalService.getThreadRepliesMessages( + anyLong(), anyInt(), anyInt(), anyInt())).thenReturn( + messages); + + ResponseContent response = engine.execute(); + + String randomNamespace = find_randomNamespace(response); + + response.assertContains(""); + + response.assertLookingAt(".*" + + Pattern.quote( + "" + + "" + + "delete")); + } + + protected String find_randomNamespace(ResponseContent response) { + String regex = + Pattern.quote( + ""); + + Matcher matcher = + Pattern.compile(regex) + .matcher(response.content()); + matcher.find(); + return matcher.group(1); + } + + protected MBMessage newMBMessage() { + MBMessage m = mock(MBMessage.class); + when(m.getModifiedDate()).thenReturn(new Date()); + return m; + } + + protected void when_getThreadMessagesCount(int value) { + when( + mbMessageLocalService.getThreadMessagesCount( + anyLong(), anyInt())).thenReturn(value); + } + + private @Mock + MBMessageLocalService mbMessageLocalService; + + private @Mock + MBMessageDisplay mbMessageDisplay; + + private @Mock + SubscriptionLocalService subscriptionLocalService; + + @Mock + RatingsEntryLocalService ratingsEntryLocalService; + + @Mock + RatingsStatsLocalService ratingsStatsLocalService; + + @Mock + WorkflowDefinitionLinkLocalService workflowDefinitionLinkLocalService; + + @Mock + private Company company; + + private void setUpWorkflowDefinitionLinkLocalServiceUtil() { + mockStatic( + WorkflowDefinitionLinkLocalServiceUtil.class, + Mockito.CALLS_REAL_METHODS); + stub(method( + WorkflowDefinitionLinkLocalServiceUtil.class, "getService")).toReturn( + workflowDefinitionLinkLocalService); + } + + private void setUpSubscriptionLocalServiceUtil() { + mockStatic( + SubscriptionLocalServiceUtil.class, Mockito.CALLS_REAL_METHODS); + stub(method( + SubscriptionLocalServiceUtil.class, "getService")).toReturn( + subscriptionLocalService); + } + + protected void setUpMBMessageLocalServiceUtil() throws PortalException { + mockStatic(MBMessageLocalServiceUtil.class, Mockito.CALLS_REAL_METHODS); + stub(method( + MBMessageLocalServiceUtil.class, "getService")).toReturn( + mbMessageLocalService); + when( + mbMessageLocalService.getDiscussionMessageDisplay( + anyLong(), anyLong(), anyString(), + anyLong(), anyInt(), anyString())) + .thenReturn(mbMessageDisplay); + + when(mbMessageDisplay.getThread()).thenReturn(thread); + + when(mbMessageLocalService.getMessage(anyLong())).thenReturn( + rootMessage); + + mockStatic(MBDiscussionPermission.class, Mockito.CALLS_REAL_METHODS); + stub(method( + MBDiscussionPermission.class, "contains", + PermissionChecker.class, Long.TYPE, Long.TYPE, + String.class, Long.TYPE, Long.TYPE, String.class)).toReturn(true); + } + + protected void setUpBlogsEntryLocalServiceUtil() throws PortalException { + mockStatic(BlogsEntryLocalServiceUtil.class, Mockito.CALLS_REAL_METHODS); + stub(method( + BlogsEntryLocalServiceUtil.class, "getService")).toReturn( + blogsEntryLocalService); + when( + blogsEntryLocalService.getEntriesPrevAndNext(Mockito.anyLong())).thenReturn( + new BlogsEntry[3]); + } + + private void setUpAssets() { + mockStatic(AssetEntryLocalServiceUtil.class); + mockStatic(AssetEntryServiceUtil.class); + mockStatic(AssetTagLocalServiceUtil.class); + mockStatic(AssetTagServiceUtil.class); + mockStatic(AssetVocabularyServiceUtil.class); + mockStatic(AssetCategoryServiceUtil.class); + } + + private void setUpRatingsStatsLocalServiceUtil() { + mockStatic( + RatingsStatsLocalServiceUtil.class, Mockito.CALLS_REAL_METHODS); + stub(method( + RatingsStatsLocalServiceUtil.class, "getService")).toReturn( + ratingsStatsLocalService); + } + + private void setUpRatingsEntryLocalServiceUtil() { + mockStatic( + RatingsEntryLocalServiceUtil.class, Mockito.CALLS_REAL_METHODS); + stub(method( + RatingsEntryLocalServiceUtil.class, "getService")).toReturn( + ratingsEntryLocalService); + } + + protected void setupMocks() throws Exception { + ServletTester tester = engine.getServletTester(); + + tester.setAttribute("portletDisplay", portletDisplay); + when(portletDisplay.getId()).thenReturn(RandomTestUtil.randomString()); + + new TrashUtil().setTrash(trash); + + new HttpUtil().setHttp(mock(Http.class)); + } + + JSPTestEngine engine = new JSPTestEngine(this); + + LiferayJSPTestSetUp liferayJSP = new LiferayJSPTestSetUp(engine); + + @Mock + PortletDisplay portletDisplay; + + @Mock + BlogsEntryLocalService blogsEntryLocalService; + + @Mock + MBThread thread; + + @Mock + MBMessage rootMessage; + + @Mock + private Trash trash; + + private UserDisplayTagTestSetUp userDisplayTagTestSetUp; + +} diff --git a/src/test/java/com/liferay/taglib/ui/UserDisplayTagTestSetUp.java b/src/test/java/com/liferay/taglib/ui/UserDisplayTagTestSetUp.java new file mode 100644 index 0000000..ebd599b --- /dev/null +++ b/src/test/java/com/liferay/taglib/ui/UserDisplayTagTestSetUp.java @@ -0,0 +1,45 @@ + +package com.liferay.taglib.ui; + +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.support.membermodification.MemberMatcher.method; +import static org.powermock.api.support.membermodification.MemberModifier.stub; + +import javax.servlet.http.HttpServletRequest; + +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +import com.liferay.portal.model.User; +import com.liferay.portal.service.UserLocalService; +import com.liferay.portal.service.UserLocalServiceUtil; + +public class UserDisplayTagTestSetUp { + + private User user; + + public UserDisplayTagTestSetUp(User user) { + this.user = user; + } + + public void setUp() { + MockitoAnnotations.initMocks(this); + setUpUserLocalServiceUtil(); + } + + public void setUpRequest(HttpServletRequest request) { + request.setAttribute("liferay-ui:user-display:user", user); + } + + private void setUpUserLocalServiceUtil() { + mockStatic(UserLocalServiceUtil.class, Mockito.CALLS_REAL_METHODS); + + stub( + method(UserLocalServiceUtil.class, "getService")).toReturn( + userLocalService); + } + + @Mock + UserLocalService userLocalService; +} diff --git a/src/test/java/com/liferay/taglib/util/IncludeTagSetUp.java b/src/test/java/com/liferay/taglib/util/IncludeTagSetUp.java new file mode 100644 index 0000000..9d131d8 --- /dev/null +++ b/src/test/java/com/liferay/taglib/util/IncludeTagSetUp.java @@ -0,0 +1,33 @@ + +package com.liferay.taglib.util; + +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.support.membermodification.MemberMatcher.method; +import static org.powermock.api.support.membermodification.MemberModifier.stub; + +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +import com.liferay.portal.model.ModelHints; +import com.liferay.portal.model.ModelHintsUtil; + +public class IncludeTagSetUp { + + public void setUp() { + MockitoAnnotations.initMocks(this); + + setUpModelHintsUtil(); + } + + private void setUpModelHintsUtil() { + mockStatic(ModelHintsUtil.class, Mockito.CALLS_REAL_METHODS); + + stub( + method(ModelHintsUtil.class, "getModelHints")).toReturn( + modelHints); + } + + @Mock + ModelHints modelHints; +} diff --git a/src/test/java/com/liferay/test/jsp/JSPTestEngine.java b/src/test/java/com/liferay/test/jsp/JSPTestEngine.java new file mode 100644 index 0000000..749c35e --- /dev/null +++ b/src/test/java/com/liferay/test/jsp/JSPTestEngine.java @@ -0,0 +1,150 @@ + +package com.liferay.test.jsp; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.Set; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.jasper.runtime.TldScanner; +import org.apache.jasper.servlet.JspServlet; +import org.eclipse.jetty.servlet.Holder; +import org.eclipse.jetty.servlet.ServletHandler; +import org.eclipse.jetty.servlet.ServletHolder; +import org.eclipse.jetty.testing.HttpTester; +import org.eclipse.jetty.testing.ServletTester; + +/** + * Troubleshooting: java.lang.NoClassDefFoundError: + * org/apache/tomcat/PeriodicEventListener Solution: Launch Configuration + * Classpath User Entries 1ST: Maven Dependencies 2ND: Eclipse project + */ +public class JSPTestEngine { + + ServletTester tester = newServletTester(); + HttpTester request = new HttpTester(); + HttpTester response = new HttpTester(); + + public interface HttpServletRequestPrepare { + + void prepare(HttpServletRequest request); + + } + + public JSPTestEngine(HttpServletRequestPrepare httpServletRequestPrepare) { + ServletHolder servlet = addJspServlet(httpServletRequestPrepare); + keepGenerated(servlet); + + request.setMethod("GET"); + request.setVersion("HTTP/1.0"); + } + + public void setResourceBase(String resourceBase) { + this.tester.setResourceBase(resourceBase); + } + + public void setURI(String uri) { + this.request.setURI(uri); + } + + public ServletTester getServletTester() { + return tester; + } + + public ResponseContent execute() throws Exception { + HttpTester response = goTester(); + + assertTrue(response.getMethod() == null); + assertEquals(200, response.getStatus()); + + String content = response.getContent(); + + return new ResponseContent(content); + } + + private HttpTester goTester() throws Exception { + tester.start(); + response.parse(tester.getResponses(request.generate())); + return response; + } + + private static ServletTester newServletTester() { + ServletTester tester = new ServletTester(); + workaroundMissingClassLoader(tester); + return tester; + } + + private static void workaroundMissingClassLoader(ServletTester tester) { + tester.setClassLoader(tester.getClass().getClassLoader()); + } + + private static void keepGenerated(ServletHolder servlet) { + servlet.setInitParameter("keepgenerated", "TRUE"); + servlet.setInitParameter("scratchdir", "./target/scratch"); + } + + private ServletHolder addJspServlet( + HttpServletRequestPrepare httpServletRequestPrepare) { + + ServletHandler servletHandler = tester.getContext().getServletHandler(); + + ViewJspServlet servlet = new ViewJspServlet(httpServletRequestPrepare); + + ServletHolder holder = + servletHandler.newServletHolder(Holder.Source.EMBEDDED); + holder.setServlet(servlet); + + servletHandler.addServletWithMapping(holder, "*.jsp"); + + return holder; + } + + static class ViewJspServlet extends JspServlet { + + private final HttpServletRequestPrepare httpServletRequestPrepare; + + public ViewJspServlet( + HttpServletRequestPrepare httpServletRequestPrepare) { + + this.httpServletRequestPrepare = httpServletRequestPrepare; + } + + @Override + public void service( + HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + httpServletRequestPrepare.prepare(request); + + super.service(request, response); + } + + } + + static { + try { + workaroundJettyTaglibJstlCoreSystemUris(); + } + catch (NoSuchFieldException e) { + throw new RuntimeException(e); + } + catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + private static void workaroundJettyTaglibJstlCoreSystemUris() + throws NoSuchFieldException, IllegalAccessException { + Field field = TldScanner.class.getDeclaredField("systemUris"); + field.setAccessible(true); + ((Set)field.get(null)).clear(); + field.setAccessible(false); + } + +} diff --git a/src/test/java/com/liferay/test/jsp/ResponseContent.java b/src/test/java/com/liferay/test/jsp/ResponseContent.java new file mode 100644 index 0000000..9581977 --- /dev/null +++ b/src/test/java/com/liferay/test/jsp/ResponseContent.java @@ -0,0 +1,44 @@ +package com.liferay.test.jsp; + +import static org.junit.Assert.assertThat; + +import org.hamcrest.core.StringContains; + +import com.liferay.test.junit.StringLookingAt; + +public class ResponseContent { + + public ResponseContent(String contentRaw) throws Exception { + this.strip = new Strip(contentRaw); + this.strip.strip(); + content = this.strip.trimmed(); + + dump(); + } + + public String content() { + return content; + } + + public void assertContains(String substring) { + assertThat( + content, StringContains.containsString(substring)); + } + + public void assertLookingAt(String regex) { + assertThat( + content, new StringLookingAt(regex)); + } + + private void dump() throws Exception { + if (SYSOUT_RESPONSE_CONTENT) + System.out.println(strip.stripped()); + } + + private String content; + + private Strip strip; + + private static final boolean SYSOUT_RESPONSE_CONTENT = true; + +} \ No newline at end of file diff --git a/src/test/java/com/liferay/test/jsp/Strip.java b/src/test/java/com/liferay/test/jsp/Strip.java new file mode 100644 index 0000000..1156bc3 --- /dev/null +++ b/src/test/java/com/liferay/test/jsp/Strip.java @@ -0,0 +1,50 @@ + +package com.liferay.test.jsp; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; + +import com.liferay.portal.kernel.util.StringUtil; + +public class Strip { + + private String content; + private StringWriter trimmed; + + public Strip(String content) { + this.content = content; + stripped = new StringWriter(content.length()); + trimmed = new StringWriter(content.length()); + } + + public void strip() throws IOException { + BufferedReader r = new BufferedReader(new StringReader(content)); + BufferedWriter strippedWriter = new BufferedWriter(stripped); + BufferedWriter trimmedWriter = new BufferedWriter(trimmed); + String line; + while ((line = r.readLine()) != null) { + String trim = StringUtil.trim(line); + if (!trim.isEmpty()) { + strippedWriter.write(line); + strippedWriter.newLine(); + trimmedWriter.write(trim); + } + } + strippedWriter.flush(); + trimmedWriter.flush(); + } + + private StringWriter stripped; + + public String stripped() { + return stripped.toString(); + } + + public String trimmed() { + return trimmed.toString(); + } + +} \ No newline at end of file diff --git a/src/test/java/com/liferay/test/junit/StringLookingAt.java b/src/test/java/com/liferay/test/junit/StringLookingAt.java new file mode 100644 index 0000000..69b2068 --- /dev/null +++ b/src/test/java/com/liferay/test/junit/StringLookingAt.java @@ -0,0 +1,28 @@ + +package com.liferay.test.junit; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.hamcrest.core.SubstringMatcher; + +public class StringLookingAt extends SubstringMatcher { + + public StringLookingAt(String substring) { + super(substring); + } + + @Override + protected boolean evalSubstringOf(String s) { + String regex = substring; + String input = s; + Pattern p = Pattern.compile(regex); + Matcher m = p.matcher(input); + return m.lookingAt(); + } + + @Override + protected String relationship() { + return "looking at"; + } +} \ No newline at end of file diff --git a/src/test/java/com/liferay/test/portal/jsp/LiferayJSPTestSetUp.java b/src/test/java/com/liferay/test/portal/jsp/LiferayJSPTestSetUp.java new file mode 100644 index 0000000..97abf52 --- /dev/null +++ b/src/test/java/com/liferay/test/portal/jsp/LiferayJSPTestSetUp.java @@ -0,0 +1,311 @@ + +package com.liferay.test.portal.jsp; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyLong; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.withSettings; +import static org.powermock.api.mockito.PowerMockito.mockStatic; + +import java.util.Locale; + +import javax.portlet.MimeResponse; +import javax.portlet.PortletPreferences; +import javax.portlet.PortletRequest; +import javax.portlet.PortletResponse; +import javax.portlet.PortletSession; +import javax.portlet.RenderRequest; +import javax.portlet.RenderResponse; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; + +import org.eclipse.jetty.testing.ServletTester; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.powermock.api.mockito.PowerMockito; +import org.springframework.mock.web.portlet.MockRenderResponse; + +import com.liferay.portal.kernel.bean.BeanProperties; +import com.liferay.portal.kernel.bean.BeanPropertiesUtil; +import com.liferay.portal.kernel.language.Language; +import com.liferay.portal.kernel.language.LanguageUtil; +import com.liferay.portal.kernel.language.UnicodeLanguage; +import com.liferay.portal.kernel.language.UnicodeLanguageUtil; +import com.liferay.portal.kernel.portlet.LiferayPortletConfig; +import com.liferay.portal.kernel.portlet.LiferayPortletRequest; +import com.liferay.portal.kernel.portlet.LiferayPortletResponse; +import com.liferay.portal.kernel.portlet.LiferayPortletURL; +import com.liferay.portal.kernel.servlet.DirectRequestDispatcherFactory; +import com.liferay.portal.kernel.servlet.DirectRequestDispatcherFactoryUtil; +import com.liferay.portal.kernel.util.FriendlyURLNormalizer; +import com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil; +import com.liferay.portal.kernel.util.HtmlUtil; +import com.liferay.portal.kernel.util.JavaConstants; +import com.liferay.portal.kernel.util.Props; +import com.liferay.portal.kernel.util.PropsUtil; +import com.liferay.portal.kernel.util.WebKeys; +import com.liferay.portal.model.Layout; +import com.liferay.portal.model.User; +import com.liferay.portal.theme.ThemeDisplay; +import com.liferay.portal.util.HtmlImpl; +import com.liferay.portal.util.Portal; +import com.liferay.portal.util.PortalUtil; +import com.liferay.portal.util.test.RandomTestUtil; +import com.liferay.portlet.PortletURLFactory; +import com.liferay.portlet.PortletURLFactoryUtil; +import com.liferay.portlet.PortletURLUtil; +import com.liferay.test.jsp.JSPTestEngine; + +public class LiferayJSPTestSetUp { + + public LiferayJSPTestSetUp(JSPTestEngine engine) { + this.engine = engine; + } + + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + themeDisplay = new ThemeDisplay(); + themeDisplay.setUser(user); + + liferayPortletRequest = mock(LiferayPortletRequest.class, + withSettings().extraInterfaces(RenderRequest.class)); + + liferayPortletResponse = mock(LiferayPortletResponse.class, + withSettings().extraInterfaces(RenderResponse.class)); + + ServletTester tester = engine.getServletTester(); + + tester.setAttribute("liferayPortletRequest", liferayPortletRequest); + tester.setAttribute("liferayPortletResponse", liferayPortletResponse); + tester.setAttribute("themeDisplay", themeDisplay); + tester.setAttribute("locale", Locale.US); + + tester.setAttribute("portletPreferences", portletPreferences); + + setUpPortletURLUtil(); + + tester.setAttribute("renderRequest", liferayPortletRequest); + tester.setAttribute("renderResponse", new MockRenderResponse()); + + setUpHtmlUtil(); + + setUpDirectRequestDispatcherFactoryUtil(); + + setUpPortalUtil(); + + setUpLanguageUtil(); + setUpUnicodeLanguageUtil(); + + setUpFriendlyURLNormalizerUtil(); + } + + protected void setUpPortalUtil() { + when(portal.getLiferayPortletResponse( + (PortletResponse)any())).thenReturn(liferayPortletResponse); + + when(portal.stripURLAnchor(anyString(), anyString())).then( + returnStripURLAnchor()); + + when(portal.generateRandomKey( + (HttpServletRequest)any(), anyString())).thenReturn( + RandomTestUtil.randomString()); + + new PortalUtil().setPortal(portal); + } + + private Answer returnStripURLAnchor() { + return new Answer() { + + @Override + public String[] answer(InvocationOnMock invocation) + throws Throwable { + + Object[] args = invocation.getArguments(); + + return new String[] { + String.valueOf(args[0]), + String.valueOf(args[1]) + }; + } + }; + } + + public void prepareRequest(HttpServletRequest request) { + preparePortletContextForTag(request); + + request.setAttribute( + PortletRequest.LIFECYCLE_PHASE, PortletRequest.RENDER_PHASE); + + when(liferayPortletRequest.getPreferences()).thenReturn( + portletPreferences); + when(liferayPortletRequest.getPortletSession()).thenReturn( + mock(PortletSession.class)); + + setUpLiferayPortletConfig(request); + + setUpBeanPropertiesUtil(); + + request.setAttribute(WebKeys.THEME_DISPLAY, themeDisplay); + themeDisplay.setLayout(mock(Layout.class)); + + request.setAttribute(WebKeys.CTX, request.getServletContext()); + } + + private void setUpBeanPropertiesUtil() { + BeanProperties beanProperties = mock(BeanProperties.class); + new BeanPropertiesUtil().setBeanProperties(beanProperties); + + when( + beanProperties.getStringSilent(any(), anyString())).then( + returnArgumentAsIs(1)); + when( + beanProperties.getStringSilent(any(), anyString(), anyString())).then( + returnArgumentAsIs(2)); + } + + protected void setUpLiferayPortletConfig(HttpServletRequest request) { + LiferayPortletConfig liferayPortletConfig = + mock(LiferayPortletConfig.class); + when(liferayPortletConfig.getPortletId()).thenReturn( + RandomTestUtil.randomString()); + request.setAttribute( + JavaConstants.JAVAX_PORTLET_CONFIG, liferayPortletConfig); + } + + /** + * Prevent: + * "Render response is null because this tag is not being called within the context of a portlet" + */ + protected void preparePortletContextForTag(HttpServletRequest request) { + request.setAttribute( + JavaConstants.JAVAX_PORTLET_REQUEST, liferayPortletRequest); + + when(liferayPortletResponse.createLiferayPortletURL( + anyLong(), anyString(), anyString())).thenReturn(portletURL); + } + + private void setUpLanguageUtil() { + Language language = mock(Language.class); + + when( + language.get((HttpServletRequest)any(), anyString())).then( + returnArgumentAsIs(1)); + + when( + language.get((Locale)any(), anyString())).then( + returnArgumentAsIs(1)); + + new LanguageUtil().setLanguage(language); + } + + private void setUpUnicodeLanguageUtil() { + UnicodeLanguage unicodeLanguage = mock(UnicodeLanguage.class); + new UnicodeLanguageUtil().setUnicodeLanguage(unicodeLanguage); + when( + unicodeLanguage.get((HttpServletRequest)any(), anyString())).then( + returnArgumentAsIs(1)); + } + + private Answer returnArgumentAsIs(final int index) { + return new Answer() { + + @Override + public String answer(InvocationOnMock invocation) throws Throwable { + return (String)invocation.getArguments()[index]; + } + }; + } + + protected void setUpDirectRequestDispatcherFactoryUtil() { + DirectRequestDispatcherFactory directRequestDispatcherFactory = + mock(DirectRequestDispatcherFactory.class); + + new DirectRequestDispatcherFactoryUtil().setDirectRequestDispatcherFactory(directRequestDispatcherFactory); + + when( + directRequestDispatcherFactory.getRequestDispatcher( + (ServletContext)any(), anyString())).then(returnItsOwn()); + } + + private Answer returnItsOwn() { + return new Answer() { + + @Override + public RequestDispatcher answer(InvocationOnMock invocation) + throws Throwable { + Object[] args = invocation.getArguments(); + ServletContext param = (ServletContext)args[0]; + String path = (String)args[1]; + return param.getRequestDispatcher(path); + } + + }; + } + + protected void setUpHtmlUtil() { + new HtmlUtil().setHtml(new HtmlImpl()); + } + + protected void setUpPortletURLUtil() { + PropsUtil.setProps(props); + + mockStatic(PortletURLUtil.class, Mockito.CALLS_REAL_METHODS); + + PowerMockito.stub(PowerMockito.method(PortletURLUtil.class, + "getCurrent", + LiferayPortletRequest.class, LiferayPortletResponse.class) + ).toReturn(portletURL); + + PowerMockito.stub(PowerMockito.method(PortletURLUtil.class, + "getCurrent", + PortletRequest.class, MimeResponse.class) + ).toReturn(portletURL); + + when(portletURLFactory.create( + (HttpServletRequest)any(), anyString(), + anyLong(), anyString())).thenReturn(portletURL); + + new PortletURLFactoryUtil().setPortletURLFactory(portletURLFactory); + } + + private void setUpFriendlyURLNormalizerUtil() { + FriendlyURLNormalizer friendlyURLNormalizer = + mock(FriendlyURLNormalizer.class); + when(friendlyURLNormalizer.normalize(anyString())).then( + returnArgumentAsIs(0)); + new FriendlyURLNormalizerUtil().setFriendlyURLNormalizer(friendlyURLNormalizer); + } + @Mock + LiferayPortletURL portletURL; + + @Mock + PortletURLFactory portletURLFactory; + + @Mock + Props props; + + @Mock + Portal portal; + + public LiferayPortletRequest liferayPortletRequest; + + public LiferayPortletResponse liferayPortletResponse; + + public ThemeDisplay themeDisplay; + + @Mock + public PortletPreferences portletPreferences; + + @Mock + public User user; + + private JSPTestEngine engine; + +}