Skip to content

Commit

Permalink
Liferay Portal JSP Unit Test Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
arboliveira committed Jul 30, 2014
0 parents commit 7ad1855
Show file tree
Hide file tree
Showing 21 changed files with 2,330 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/scratch"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry combineaccessrules="false" kind="src" path="/portal-master"/>
<classpathentry kind="lib" path="/portal-master/lib/global/portlet.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.project
/target
375 changes: 375 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions .settings/org.eclipse.jdt.ui.prefs
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions .settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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/
26 changes: 26 additions & 0 deletions liferay-portal-jsp-unit-test.launch
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.jdt.junit.launchconfig">
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/liferay-portal-jsp-unit-test"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="4"/>
</listAttribute>
<stringAttribute key="org.eclipse.debug.core.source_locator_id" value="org.eclipse.jdt.launching.sourceLocator.JavaSourceLookupDirector"/>
<stringAttribute key="org.eclipse.debug.core.source_locator_memento" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;sourceLookupDirector&gt;&#10;&lt;sourceContainers duplicates=&quot;false&quot;&gt;&#10;&lt;container memento=&quot;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; standalone=&amp;quot;no&amp;quot;?&amp;gt;&amp;#10;&amp;lt;javaProject name=&amp;quot;portal-master&amp;quot;/&amp;gt;&amp;#10;&quot; typeId=&quot;org.eclipse.jdt.launching.sourceContainer.javaProject&quot;/&gt;&#10;&lt;container memento=&quot;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; standalone=&amp;quot;no&amp;quot;?&amp;gt;&amp;#10;&amp;lt;default/&amp;gt;&amp;#10;&quot; typeId=&quot;org.eclipse.debug.core.containerType.default&quot;/&gt;&#10;&lt;/sourceContainers&gt;&#10;&lt;/sourceLookupDirector&gt;&#10;"/>
<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value="=liferay-portal-jsp-unit-test"/>
<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit4"/>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_START_ON_FIRST_THREAD" value="true"/>
<listAttribute key="org.eclipse.jdt.launching.CLASSPATH">
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;runtimeClasspathEntry containerPath=&quot;org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5&quot; path=&quot;1&quot; type=&quot;4&quot;/&gt;&#10;"/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;runtimeClasspathEntry containerPath=&quot;org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER&quot; path=&quot;3&quot; type=&quot;4&quot;/&gt;&#10;"/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;runtimeClasspathEntry path=&quot;3&quot; projectName=&quot;liferay-portal-jsp-unit-test&quot; type=&quot;1&quot;/&gt;&#10;"/>
</listAttribute>
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.m2e.launchconfig.classpathProvider"/>
<booleanAttribute key="org.eclipse.jdt.launching.DEFAULT_CLASSPATH" value="false"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value=""/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="liferay-portal-jsp-unit-test"/>
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.m2e.launchconfig.sourcepathProvider"/>
</launchConfiguration>
67 changes: 67 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.liferay.arbo</groupId>
<artifactId>liferay-portal-jsp-unit-test</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Liferay Portal JSP Unit Test Examples</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>${jetty.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>test-jetty-servlet</artifactId>
<version>${jetty.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>[3.8.1,)</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>
powermock-release-with-junit-mockito-dependencies
</artifactId>
<version>1.5.1</version>
<type>pom</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.0.1.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<version>${jetty.version}</version>
<configuration>
<useTestClasspath>false</useTestClasspath>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<jetty.version>8.1.15.v20140411</jetty.version>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -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;

}
Loading

0 comments on commit 7ad1855

Please sign in to comment.