com.jalios.jcms.test
Class JcmsTestCase

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by com.jalios.jcms.test.JcmsTestCase
All Implemented Interfaces:
JcmsConstants, JaliosConstants, junit.framework.Test
Direct Known Subclasses:
FileParserTestCase

public abstract class JcmsTestCase
extends junit.framework.TestCase
implements JcmsConstants

Provides a super class for development of JCMS unit tests.
Extending this class will automatically provides a working JCMS during execution of all your unit tests.

The launched instance of JCMS will be using the following 3 files :

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, custom.prop and webapp.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 :

You can retrieve mock objects for testing using methods provided in this class : Convenient static "assert" methods not provided in the JUnit API are provided in this class.
By extending this class you can use any log4j logger starting with "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); 
      }
    }
  }
 

Since:
jcms-5.7.4
Version:
$Revision: 30666 $

Field Summary
protected static String A_VERY_LONG_TEXT
           
protected static Member admin
           
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 String[] langs
          Array of all channel languages (channel.getLanguageList())
 
Fields inherited from interface com.jalios.jcms.JcmsConstants
ADATE_SEARCH, ADMIN_NOTES_PROP, ADVANCED_TAB, ARCHIVES_DIR, ASCII_WIDTH, CATEGORY_TAB, CDATE_SEARCH, COMMON_ALARM, CONTENT_TAB, COOKIE_MAX_AGE, CRYPT_MD5, CRYPT_UNDEFINED, CRYPT_UNIX, 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_DEEP_COPY, OP_DEEP_DELETE, OP_DELETE, OP_MERGE, OP_UPDATE, 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, UDATE_SEARCH, UPDATE_RIGHT_TAB, UPLOAD_DIR, URL_REGEXP, WARNING_MSG, WEBAPP_PROP, WFEXPRESS_ALARM, WFREMINDER_ALARM, WORKFLOW_TAB, WORKFLOW_XML
 
Fields inherited from interface com.jalios.util.JaliosConstants
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 Summary
JcmsTestCase()
           
 
Method Summary
protected
<T> void
assertCmpEquals(Comparator<T> comparator, T o1, T o2)
           
protected
<T> void
assertCmpGreater(Comparator<T> comparator, T o1, T o2)
           
protected
<T> void
assertCmpLower(Comparator<T> comparator, T o1, T o2)
           
protected
<T> void
assertCmpOpposite(Comparator<T> comparator, T o1, T o2)
           
protected
<T> void
assertCmpSame(Comparator<T> c1, Comparator<T> c2, T o1, T o2)
           
protected  void assertDateRange(Date date, Date lowerDate, Date upperDate)
          Assert the given date d1 is in range {d2, d3}
protected  void assertDateRangeStrict(Date date, Date lowerDate, Date upperDate)
          Assert the given date date is in the range {lowerDate, upperDate}
protected  void assertIsEmpty(Object obj)
           
protected  void assertNotEmpty(Object obj)
           
protected  void assertNotSameContent(Date date1, Date date2)
           
protected  void assertNotSameContent(List<?> list1, List<?> list2)
           
protected  void assertNotSameContent(Map<?,?> map1, Map<?,?> map2)
           
protected  void assertNotSameContent(Object[] array1, Object[] array2)
           
protected  void assertNotSameContent(Set<?> set1, Set<?> set2)
           
static void assertObjectIsGarbageCollected(WeakReference<? extends 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(Date date1, Date date2)
           
protected  void assertSameContent(List<?> list1, List<?> list2)
           
protected  void assertSameContent(Map<?,?> map1, Map<?,?> map2)
           
protected  void assertSameContent(Object[] array1, Object[] array2)
           
protected  void assertSameContent(Set<?> set1, Set<?> set2)
           
protected  void assertStatusForbidden(ControllerStatus status)
           
protected  void assertStatusHasFailed(ControllerStatus status)
           
protected  void assertStatusHasFailed(String prop, ControllerStatus status)
           
protected  void assertStatusOK(ControllerStatus status)
           
protected  void beginTransaction()
           
protected  void commitTransaction()
           
protected  void deleteData(Data... array)
           
protected  void deleteData(String... ids)
           
protected  void deleteOnTearDown(Data data)
           
protected  Object deserialize(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  File getResourceFile(String name)
          Retrieves a new File instance pointing to the specified resource name.
protected  File serialize(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, String logPrefix)
           
protected  void waitForLuceneIndexation(Publication pub)
           
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, runTest, setName, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail, failNotEquals, failNotSame, failSame, format
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

friendlyURLFilter

protected static FriendlyURLFilter friendlyURLFilter
Instance of the FriendlyURLFilter being used for testing


channel

protected static Channel channel
Singleton used for testing


langs

protected static String[] langs
Array of all channel languages (channel.getLanguageList())


admin

protected static Member admin

defaultWorkspace

protected static Workspace defaultWorkspace

defaultGroup

protected static Group defaultGroup

A_VERY_LONG_TEXT

protected static String A_VERY_LONG_TEXT
Constructor Detail

JcmsTestCase

public JcmsTestCase()
Method Detail

setUp

protected void setUp()
              throws Exception
Overrides:
setUp in class junit.framework.TestCase
Throws:
Exception

tearDown

protected void tearDown()
                 throws Exception
Overrides:
tearDown in class junit.framework.TestCase
Throws:
Exception

getMockServletContext

protected javax.servlet.ServletContext getMockServletContext()
Convenient Method to return the fake servlet context used for this test.

See Also:
MockServletContext

getMockHttpSession

protected javax.servlet.http.HttpSession getMockHttpSession()
Convenient Method to return a new instance of a fake http session.

See Also:
HttpSession

getMockHttpServletRequest

protected javax.servlet.http.HttpServletRequest getMockHttpServletRequest()
Convenient Method to return a new instance of a fake http servlet request.

See Also:
MockHttpServletRequest

getMockHttpServletRequest

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

See Also:
MockHttpServletRequest

getMockHttpServletResponse

protected javax.servlet.http.HttpServletResponse getMockHttpServletResponse()
Convenient Method to return a new instance of a fake http servlet response.

See Also:
MockHttpServletResponse

getMockHttpServletResponse

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

See Also:
MockHttpServletResponse

getMockJcmsJspContext

protected JcmsJspContext getMockJcmsJspContext()
Retrieve a mock explorer JcmsJspContext.

Returns:
a new instance of JcmsJspContext
Since:
jcms-5.7.3

getMockJcmsJspContext

protected JcmsJspContext getMockJcmsJspContext(javax.servlet.http.HttpServletRequest request)
Retrieve a mock explorer JcmsJspContext based on the specified request

Parameters:
request - the mock request to use, if null a new mock request is created
Returns:
a new instance of JcmsJspContext
Since:
jcms-5.7.3

assertStatusOK

protected void assertStatusOK(ControllerStatus status)

assertStatusForbidden

protected void assertStatusForbidden(ControllerStatus status)

assertStatusHasFailed

protected void assertStatusHasFailed(ControllerStatus status)

assertStatusHasFailed

protected void assertStatusHasFailed(String prop,
                                     ControllerStatus status)

assertCmpGreater

protected <T> void assertCmpGreater(Comparator<T> comparator,
                                    T o1,
                                    T o2)

assertCmpLower

protected <T> void assertCmpLower(Comparator<T> comparator,
                                  T o1,
                                  T o2)

assertCmpEquals

protected <T> void assertCmpEquals(Comparator<T> comparator,
                                   T o1,
                                   T o2)

assertCmpOpposite

protected <T> void assertCmpOpposite(Comparator<T> comparator,
                                     T o1,
                                     T o2)

assertCmpSame

protected <T> void assertCmpSame(Comparator<T> c1,
                                 Comparator<T> c2,
                                 T o1,
                                 T o2)

assertSameContent

protected void assertSameContent(Object[] array1,
                                 Object[] array2)

assertNotSameContent

protected void assertNotSameContent(Object[] array1,
                                    Object[] array2)

assertSameContent

protected void assertSameContent(Set<?> set1,
                                 Set<?> set2)

assertNotSameContent

protected void assertNotSameContent(Set<?> set1,
                                    Set<?> set2)

assertSameContent

protected void assertSameContent(List<?> list1,
                                 List<?> list2)

assertNotSameContent

protected void assertNotSameContent(List<?> list1,
                                    List<?> list2)

assertSameContent

protected void assertSameContent(Map<?,?> map1,
                                 Map<?,?> map2)

assertNotSameContent

protected void assertNotSameContent(Map<?,?> map1,
                                    Map<?,?> map2)

assertSameContent

protected void assertSameContent(Date date1,
                                 Date date2)

assertNotSameContent

protected void assertNotSameContent(Date date1,
                                    Date date2)

assertIsEmpty

protected void assertIsEmpty(Object obj)

assertNotEmpty

protected void assertNotEmpty(Object obj)

assertDateRange

protected void assertDateRange(Date date,
                               Date lowerDate,
                               Date upperDate)
Assert the given date d1 is in range {d2, d3}

Parameters:
date - the date to be tested
lowerDate - the lower date limit (may be null)
upperDate - the upper date limit (may be null)

assertDateRangeStrict

protected void assertDateRangeStrict(Date date,
                                     Date lowerDate,
                                     Date upperDate)
Assert the given date date is in the range {lowerDate, upperDate}

Parameters:
date - the date to be tested
lowerDate - the lower date limit (may be null)
upperDate - the upper date limit (may be null)

assertObjectIsGarbageCollected

public static void assertObjectIsGarbageCollected(WeakReference<? extends Object> wRef,
                                                  long timeout,
                                                  boolean heapDump)
Check that the specified Object (referenced through a WeakReference) is garbage collected in the designated time. Otherwise throw an assertion failure.

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
  assertObjectIsGarbageCollected(pub1WeakRef, 10*MILLIS_IN_ONE_SECOND);
 

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.

Parameters:
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 debugging

getResourceFile

protected File getResourceFile(String name)
Retrieves a new File instance pointing to the specified resource name. Example :
   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());

Parameters:
name - the name of the resource, a '/'-separated path name that identifies the resource. e.g "com/company/jcms/MyFeature_testFile1.txt"

serialize

protected File serialize(Object obj)
                  throws Exception
Serialize the given object into a temp file and return this file.

Parameters:
obj - the Object to be serialized
Returns:
the file where the object has been serialized.
Throws:
Exception

deserialize

protected Object deserialize(File file)
                      throws Exception
Deserialize the given file into a new object which is returned.

Parameters:
file - the file
Returns:
the object
Throws:
Exception

beginTransaction

protected void beginTransaction()

commitTransaction

protected void commitTransaction()

deleteData

protected void deleteData(Data... array)

deleteData

protected void deleteData(String... ids)

deleteOnTearDown

protected void deleteOnTearDown(Data data)

sleep

protected void sleep(long millis)
Parameters:
millis - the length of time to sleep in milliseconds.

waitForAlarm

protected void waitForAlarm(long time,
                            AlarmManager alarmMgr,
                            boolean reallyWaitFor,
                            String logPrefix)

waitForLuceneIndexation

protected void waitForLuceneIndexation(Publication pub)


Copyright © 2001-2010 Jalios SA. All Rights Reserved.