public abstract class JcmsTestCase extends junit.framework.TestCase implements JcmsConstants
The launched instance of JCMS will be using the following 3 files :
WEB-INF/data/store.xml.junit
: copy of WEB-INF/data/store.xml.release
if it exists, otherwise copy of WEB-INF/data/store.xml
WEB-INF/data/custom.prop.junit
: copy of WEB-INF/data/custom.prop.release
if it exists, otherwise copy of WEB-INF/data/custom.prop
Also, if file WEB-INF/data/junit.prop
exists, its content is appended to the junit file WEB-INF/data/custom.prop.junit
.
Warning!: Even though, store.xml
and custom.prop
are not modified by launching a test case using this class, beware that a real JCMS is being launched, thus some other legitimate file
might be modified (monitor.xml, logs, stats, lucene index, etc).
Some useful protected variables are provided to your subclass for easier Data manipulation in your test cases :
admin
: default administrator (channel.getDefaultAdmin()
)defaultWorkspace
: default workspace (channel.getDefaultWorkspace()
)defaultGroup
: default group (channel.getDefaultGroup()
)"unittests."
, the log message will be appendended to standard console.
Example :
public class MyFeatureTest extends com.jalios.jcms.test.JcmsTestCase { private static final Logger logger = Logger.getLogger(MyFeatureTest.class.getName()); Data testData = null; protected void setUp() throws Exception { super.setUp(); logger.info("Creating new article for MyFeature test..."); Article article = new Article(); article.setWorkspace(defaultWorkspace); article.setTitle("en", "Article Title en " + new Date()); article.performCreate(admin); testData = article } public void testMethod1() { HttpSession testSession = getMockHttpSession(); assertTrue(MyFeature.method1(getMockHttpServletRequest(testSession), testData)); assertFalse(MyFeature.method1(getMockHttpServletRequest(testSession), testData)); ... } protected void tearDown() throws Exception { super.tearDown(); if (testData != null) { logger.info("Deleting article created for MyFeature test..."); testData.performDelete(admin); } } }
Modifier and Type | Field and Description |
---|---|
protected static java.lang.String |
A_VERY_LONG_TEXT |
protected static Member |
admin |
protected static long |
ASYNC_TEST__ITERATION_WAIT_TIME |
protected static long |
ASYNC_TEST__MAX_ITERATION |
protected static long |
ASYNC_TEST__MAX_WAIT_TIME_BEFORE_FAILURE |
protected static Channel |
channel
Singleton used for testing
|
protected static Group |
defaultGroup |
protected static Workspace |
defaultWorkspace |
protected static FriendlyURLFilter |
friendlyURLFilter
Instance of the FriendlyURLFilter being used for testing
|
protected static boolean |
IS_JAVA7_OR_HIGHER |
protected static java.lang.String[] |
langs
Array of all channel languages (
channel.getLanguageList() ) |
ADATE_SEARCH, ADMIN_NOTES_PROP, ADVANCED_TAB, ARCHIVES_DIR, ASCII_WIDTH, CATEGORY_TAB, CDATE_SEARCH, COMMON_ALARM, CONTENT_TAB, COOKIE_MAX_AGE, CTRL_TOPIC_INTERNAL, CTRL_TOPIC_REF, CTRL_TOPIC_VALUE, CTRL_TOPIC_WRITE, CUSTOM_PROP, DOCCHOOSER_HEIGHT, DOCCHOOSER_WIDTH, DOCS_DIR, EDATE_SEARCH, EMAIL_REGEXP, ERROR_MSG, FORBIDDEN_FILE_ACCESS, FORBIDDEN_REDIRECT, FORCE_REDIRECT, ICON_ARCHIVE, ICON_LOCK, ICON_LOCK_STRONG, ICON_WARN, ICON_WH_BOOK_CLOSED, ICON_WH_BOOK_OPEN, INFORMATION_MSG, JALIOS_JUNIT_PROP, JCMS_CADDY, JCMS_MSG_LIST, JSYNC_DOWNLOAD_DIR, JSYNC_SYNC_ALARM, LOG_FILE, LOG_TOPIC_SECURITY, LOGGER_PROP, LOGGER_XMLPROP, MBR_PHOTO_DIR, MDATE_SEARCH, MONITOR_XML, OP_CREATE, OP_CREATE_STR, OP_DEEP_COPY, OP_DEEP_COPY_STR, OP_DEEP_DELETE, OP_DEEP_DELETE_STR, OP_DELETE, OP_DELETE_STR, OP_MERGE, OP_MERGE_STR, OP_UPDATE, OP_UPDATE_STR, PDATE_SEARCH, PHOTO_DIR, PHOTO_ICON, PHOTO_ICON_HEIGHT, PHOTO_ICON_WIDTH, PHOTO_LARGE, PHOTO_LARGE_HEIGHT, PHOTO_LARGE_WIDTH, PHOTO_NORMAL, PHOTO_NORMAL_HEIGHT, PHOTO_NORMAL_WIDTH, PHOTO_SMALL, PHOTO_SMALL_HEIGHT, PHOTO_SMALL_WIDTH, PHOTO_TINY, PHOTO_TINY_HEIGHT, PHOTO_TINY_WIDTH, PREVIOUS_TAB, PRINT_VIEW, PRIVATE_FILE_ACCESS, PUBLIC_FILE_ACCESS, READ_RIGHT_TAB, REVISION, SDATE_SEARCH, SEARCHENGINE_ALARM, SESSION_AUTHORIZED_FILENAMES_SET, STATS_REPORT_DIR, STATUS_PROP, STORE_XML, TEMPLATE_TAB, THUMBNAIL_LARGE_HEIGHT, THUMBNAIL_LARGE_WIDTH, THUMBNAIL_SMALL_HEIGHT, THUMBNAIL_SMALL_WIDTH, TYPES_ICON_ALT_PROP, TYPES_ICON_SUFFIX_PROP, TYPES_ICON_TITLE_PROP, TYPES_PREFIX_PROP, TYPES_THUMB_SUFFIX_PROP, UDATE_SEARCH, UPDATE_RIGHT_TAB, UPLOAD_DIR, URL_REGEXP, WARNING_MSG, WEBAPP_PROP, WFEXPRESS_ALARM, WFREMINDER_ALARM, WORKFLOW_TAB, WORKFLOW_XML
CRLF, MILLIS_IN_ONE_DAY, MILLIS_IN_ONE_HOUR, MILLIS_IN_ONE_MINUTE, MILLIS_IN_ONE_MONTH, MILLIS_IN_ONE_SECOND, MILLIS_IN_ONE_WEEK, MILLIS_IN_ONE_YEAR
Constructor and Description |
---|
JcmsTestCase() |
Modifier and Type | Method and Description |
---|---|
protected void |
assertAlert(Alert expectedAlert,
Alert actualAlert) |
protected void |
assertCanBeReadBy(Publication pub,
Group grp) |
protected void |
assertCanBeReadBy(Publication pub,
Member mbr) |
protected void |
assertCanBeReadBy(Publication pub,
Member mbr,
boolean searchInGroup) |
protected void |
assertCanDeleteOther(Member mbr,
Data data) |
protected void |
assertCannotBeReadBy(Publication pub,
Group grp,
RightInfo.Explanation restriction) |
protected void |
assertCannotBeReadBy(Publication pub,
Member mbr,
boolean searchInGroup,
RightInfo.Explanation explanation) |
protected void |
assertCannotBeReadBy(Publication pub,
Member mbr,
RightInfo.Explanation explanation) |
protected void |
assertCannotDeleteOther(Member mbr,
Data data,
RightInfo.Explanation explanation) |
protected void |
assertCannotUpdateOther(Member mbr,
Data data,
RightInfo.Explanation explanation) |
protected void |
assertCannotWorkOn(Member mbr,
Publication pub,
RightInfo.Explanation explanation) |
protected void |
assertCanUpdateOther(Member mbr,
Data data) |
protected void |
assertCanWorkOn(Member mbr,
Publication pub) |
protected <T> void |
assertCmpEquals(java.util.Comparator<T> comparator,
T o1,
T o2) |
protected <T> void |
assertCmpGreater(java.util.Comparator<T> comparator,
T o1,
T o2) |
protected <T> void |
assertCmpLower(java.util.Comparator<T> comparator,
T o1,
T o2) |
protected <T> void |
assertCmpOpposite(java.util.Comparator<T> comparator,
T o1,
T o2) |
protected <T> void |
assertCmpSame(java.util.Comparator<T> c1,
java.util.Comparator<T> c2,
T o1,
T o2) |
protected void |
assertDateRange(java.util.Date date,
java.util.Date lowerDate,
java.util.Date upperDate)
Assert the given date d1 is in range {d2, d3}
|
protected void |
assertDateRangeStrict(java.util.Date date,
java.util.Date lowerDate,
java.util.Date upperDate)
Assert the given date date is in the range {lowerDate, upperDate}
|
protected void |
assertIsEmpty(java.lang.Object obj) |
protected void |
assertNotEmpty(java.lang.Object obj) |
protected void |
assertNotSameContent(java.util.Date date1,
java.util.Date date2) |
protected void |
assertNotSameContent(java.util.List<?> list1,
java.util.List<?> list2) |
protected void |
assertNotSameContent(java.util.Map<?,?> map1,
java.util.Map<?,?> map2) |
protected void |
assertNotSameContent(java.lang.Object[] array1,
java.lang.Object[] array2) |
protected void |
assertNotSameContent(java.util.Set<?> set1,
java.util.Set<?> set2) |
static void |
assertObjectIsGarbageCollected(java.lang.ref.WeakReference<? extends java.lang.Object> wRef,
long timeout,
boolean heapDump)
Check that the specified Object (referenced through a WeakReference) is garbage collected
in the designated time.
|
protected void |
assertSameContent(java.util.Date date1,
java.util.Date date2) |
protected void |
assertSameContent(java.util.List<?> list1,
java.util.List<?> list2) |
protected void |
assertSameContent(java.util.Map<?,?> map1,
java.util.Map<?,?> map2) |
protected void |
assertSameContent(java.lang.Object[] array1,
java.lang.Object[] array2) |
protected void |
assertSameContent(java.util.Set<?> set1,
java.util.Set<?> set2) |
protected void |
assertStatusForbidden(ControllerStatus status) |
protected void |
assertStatusHasFailed(ControllerStatus status) |
protected void |
assertStatusHasFailed(java.lang.String prop,
ControllerStatus status) |
protected void |
assertStatusOK(ControllerStatus status) |
protected void |
beginTransaction() |
protected void |
commitTransaction() |
protected java.lang.String |
dataToDebugString(Data data) |
protected void |
deleteData(Data... array) |
protected void |
deleteData(java.lang.String... ids) |
protected void |
deleteOnTearDown(Data data) |
protected void |
deleteOnTearDown(Workspace ws,
java.util.Set<Data> initialDataSet)
Deletes on tear down the workspace, and all its Datas,
but not those contained in optional initialDataSet.
|
protected java.lang.Object |
deserialize(java.io.File file)
Deserialize the given file into a new object which is returned.
|
protected javax.servlet.http.HttpServletRequest |
getMockHttpServletRequest()
Convenient Method to return a new instance of a fake http servlet request.
|
protected javax.servlet.http.HttpServletRequest |
getMockHttpServletRequest(javax.servlet.http.HttpSession session)
Convenient Method to return a new instance of a fake http servlet request
using the specified mock session
|
protected javax.servlet.http.HttpServletResponse |
getMockHttpServletResponse()
Convenient Method to return a new instance of a fake http servlet response.
|
protected javax.servlet.http.HttpServletResponse |
getMockHttpServletResponse(javax.servlet.http.HttpSession session)
Convenient Method to return a new instance of a fake http servlet response
using the specified mock session
|
protected javax.servlet.http.HttpSession |
getMockHttpSession()
Convenient Method to return a new instance of a fake http session.
|
protected JcmsJspContext |
getMockJcmsJspContext()
Retrieve a mock explorer JcmsJspContext.
|
protected JcmsJspContext |
getMockJcmsJspContext(javax.servlet.http.HttpServletRequest request)
Retrieve a mock explorer JcmsJspContext based on the specified request
|
protected javax.servlet.ServletContext |
getMockServletContext()
Convenient Method to return the fake servlet context used for this test.
|
protected java.io.File |
getResourceFile(java.lang.String name)
Retrieves a new File instance pointing to the specified resource name.
|
protected void |
prepareWorkspaceDeletion(Workspace workspace)
Remove WkRole which are referenced in other Workspaces
to avoid unexpected WkRole deletion (due to Workspace.performDelete)
|
protected java.io.File |
serialize(java.lang.Object obj)
Serialize the given object into a temp file and return this file.
|
protected void |
setUp() |
protected void |
sleep(long millis) |
protected void |
tearDown() |
protected void |
waitForAlarm(long time,
AlarmManager alarmMgr,
boolean reallyWaitFor,
java.lang.String logPrefix) |
protected void |
waitForDBTimePrecision()
wait 1 ms if the DB support time with ms precision, otherwise 1 s
|
protected void |
waitForLuceneIndexation(java.io.File... files)
Wait for the specified file(s) (which must be in upload directory)
to be indexed in the lucene file index in its current last
modified date as returned by
File.lastModified() . |
protected void |
waitForLuceneIndexation(Publication... pubs)
Wait for the specified publication(s) to be indexed in all
the lucene publication index in its current version
as returned by
Publication.getVersionString() . |
protected static boolean IS_JAVA7_OR_HIGHER
protected static FriendlyURLFilter friendlyURLFilter
protected static Channel channel
protected static java.lang.String[] langs
channel.getLanguageList()
)protected static Member admin
protected static Workspace defaultWorkspace
protected static Group defaultGroup
protected static java.lang.String A_VERY_LONG_TEXT
protected static long ASYNC_TEST__MAX_WAIT_TIME_BEFORE_FAILURE
protected static long ASYNC_TEST__ITERATION_WAIT_TIME
protected static long ASYNC_TEST__MAX_ITERATION
protected void setUp() throws java.lang.Exception
java.lang.Exception
protected void tearDown() throws java.lang.Exception
java.lang.Exception
protected javax.servlet.ServletContext getMockServletContext()
MockServletContext
protected javax.servlet.http.HttpSession getMockHttpSession()
HttpSession
protected javax.servlet.http.HttpServletRequest getMockHttpServletRequest()
MockHttpServletRequest
protected javax.servlet.http.HttpServletRequest getMockHttpServletRequest(javax.servlet.http.HttpSession session)
session
- the sessionMockHttpServletRequest
protected javax.servlet.http.HttpServletResponse getMockHttpServletResponse()
MockHttpServletResponse
protected javax.servlet.http.HttpServletResponse getMockHttpServletResponse(javax.servlet.http.HttpSession session)
session
- the sessionMockHttpServletResponse
protected JcmsJspContext getMockJcmsJspContext()
protected JcmsJspContext getMockJcmsJspContext(javax.servlet.http.HttpServletRequest request)
request
- the mock request to use, if null a new mock request is createdprotected void assertStatusOK(ControllerStatus status)
protected void assertStatusForbidden(ControllerStatus status)
protected void assertStatusHasFailed(ControllerStatus status)
protected void assertStatusHasFailed(java.lang.String prop, ControllerStatus status)
protected void assertCanBeReadBy(Publication pub, Member mbr)
protected void assertCannotBeReadBy(Publication pub, Member mbr, RightInfo.Explanation explanation)
protected void assertCanBeReadBy(Publication pub, Member mbr, boolean searchInGroup)
protected void assertCannotBeReadBy(Publication pub, Member mbr, boolean searchInGroup, RightInfo.Explanation explanation)
protected void assertCanBeReadBy(Publication pub, Group grp)
protected void assertCannotBeReadBy(Publication pub, Group grp, RightInfo.Explanation restriction)
protected void assertCannotUpdateOther(Member mbr, Data data, RightInfo.Explanation explanation)
protected void assertCannotDeleteOther(Member mbr, Data data, RightInfo.Explanation explanation)
protected void assertCanWorkOn(Member mbr, Publication pub)
protected void assertCannotWorkOn(Member mbr, Publication pub, RightInfo.Explanation explanation)
protected <T> void assertCmpGreater(java.util.Comparator<T> comparator, T o1, T o2)
protected <T> void assertCmpLower(java.util.Comparator<T> comparator, T o1, T o2)
protected <T> void assertCmpEquals(java.util.Comparator<T> comparator, T o1, T o2)
protected <T> void assertCmpOpposite(java.util.Comparator<T> comparator, T o1, T o2)
protected <T> void assertCmpSame(java.util.Comparator<T> c1, java.util.Comparator<T> c2, T o1, T o2)
protected void assertSameContent(java.lang.Object[] array1, java.lang.Object[] array2)
protected void assertNotSameContent(java.lang.Object[] array1, java.lang.Object[] array2)
protected void assertSameContent(java.util.Set<?> set1, java.util.Set<?> set2)
protected void assertNotSameContent(java.util.Set<?> set1, java.util.Set<?> set2)
protected void assertSameContent(java.util.List<?> list1, java.util.List<?> list2)
protected void assertNotSameContent(java.util.List<?> list1, java.util.List<?> list2)
protected void assertSameContent(java.util.Map<?,?> map1, java.util.Map<?,?> map2)
protected void assertNotSameContent(java.util.Map<?,?> map1, java.util.Map<?,?> map2)
protected void assertSameContent(java.util.Date date1, java.util.Date date2)
protected void assertNotSameContent(java.util.Date date1, java.util.Date date2)
protected void assertIsEmpty(java.lang.Object obj)
protected void assertNotEmpty(java.lang.Object obj)
protected void assertDateRange(java.util.Date date, java.util.Date lowerDate, java.util.Date upperDate)
date
- the date to be testedlowerDate
- the lower date limit (may be null)upperDate
- the upper date limit (may be null)protected void assertDateRangeStrict(java.util.Date date, java.util.Date lowerDate, java.util.Date upperDate)
date
- the date to be testedlowerDate
- the lower date limit (may be null)upperDate
- the upper date limit (may be null)public static void assertObjectIsGarbageCollected(java.lang.ref.WeakReference<? extends java.lang.Object> wRef, long timeout, boolean heapDump)
IMPORTANT : in order for this method to work properly and success, you must get rid of all strong references of your object in the method callee. For example :
Publication pub1 = createSmallNews(admin, defaultWorkspace); pub1.performDelete(); // 1. Create WeakReference for the object WeakReference<Object> pub1WeakRef = new WeakReference<Object>(pub1); // 2. Remove strong reference for the object pub1 = null; // 3. Test object is garbage collected correctly, requesting a heap dump in case of failure assertObjectIsGarbageCollected(pub1WeakRef, 10*MILLIS_IN_ONE_SECOND, true);
Implementation note : this method will trigger a GC through System.gc()
every second until the timeout has been reached or the object is detected has garbage collected.
wRef
- the object WeakReference (required to prevent strong reference of object through callstack)timeout
- the timeout after which this method will consider (should be a multiple of 1000 ms with current implementation)heapDump
- perform HEAP dump on failure for easier debuggingprotected java.io.File getResourceFile(java.lang.String name)
File testFile1 = getResourceFile("com/company/jcms/MyFeature_testFile1.txt");Current implementation is to invoke class loader of current class :
return new File(getClass().getClassLoader().getResource(name).getFile());
name
- the name of the resource, a '/'-separated path name that
identifies the resource. e.g "com/company/jcms/MyFeature_testFile1.txt"
protected java.io.File serialize(java.lang.Object obj) throws java.lang.Exception
obj
- the Object to be serializedjava.lang.Exception
- if an error occursprotected java.lang.Object deserialize(java.io.File file) throws java.lang.Exception
file
- the filejava.lang.Exception
- if an error occursprotected void beginTransaction()
protected void commitTransaction()
protected void deleteData(Data... array)
protected void deleteData(java.lang.String... ids)
protected void prepareWorkspaceDeletion(Workspace workspace)
workspace
- the Workspace to be deleted.protected void deleteOnTearDown(Data data)
protected void deleteOnTearDown(Workspace ws, java.util.Set<Data> initialDataSet)
Set initialDataSet = channel.getAllDataSet();Then, the workspace is created, using :
Workspace workspace = new Workspace(); ... workspace.performCreate(admin);Then you might want to delete it on tear down, using :
deleteOnTearDown(workspace, initialDataSet);
Deleted datas will be :
ws
- the workspace to delete.initialDataSet
- a Set of Datas not to delete, since already
existing before the start of the testprotected void sleep(long millis)
millis
- the length of time to sleep in milliseconds.protected void waitForAlarm(long time, AlarmManager alarmMgr, boolean reallyWaitFor, java.lang.String logPrefix)
protected void waitForLuceneIndexation(Publication... pubs)
Publication.getVersionString()
.
For this method to work properly with DBData, it must be
invoked in a different transaction than the transaction
in which the create/update operation of the DBData was
performed. Indeed indexing is trigger by the
PublicationSearchEngineListener
during the
transaction commit, thus indexing will have been performed
only after transaction end.
If needed, several verification for the maximum duration
specified ASYNC_TEST__MAX_WAIT_TIME_BEFORE_FAILURE
.
pubs
- the publications to found in index.protected void waitForLuceneIndexation(java.io.File... files)
File.lastModified()
.
If needed, several verification for the maximum duration
specified ASYNC_TEST__MAX_WAIT_TIME_BEFORE_FAILURE
.
files
- the File(s) to found in file index.protected java.lang.String dataToDebugString(Data data)
data
- the data to get the debug string.protected void waitForDBTimePrecision()
Copyright © 2001-2010 Jalios SA. All Rights Reserved.