[opencms-dev] OpenCMS Unit Testing shell

Laurent Gauthier lgauthier at opnworks.com
Wed Oct 1 00:16:02 CEST 2003


Hello all,

We have written and used a class that extends com.opencms.core.CmsShell
and can be used for unit testing of Java classes that need to access
OpenCMS objects or an OpenCMS database. We have used it extensively to
perform unit testing on OpenCMS modules.

It really is pretty trivial to implement but still, we found it
extremely useful since we like to maintain and use a suite of Unit tests
for our Java applications and found nothing to allow us to do this out
of the box in the OpenCMS distribution.

If there is enough interest we could post it on the OpenCMS module
sandbox (if that is appropriate).

Laurent Gauthier
Mirasol Op'nWOrks

---------Java code after this line ---------------------

/*
 * Created on Sep 17, 2003
 *
 * (c) 2003, Mirasol Op'nWorks Inc.
 * web: http://www.opnworks.com
 * email: info at opnworks.com
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 *
 */
package com.opnworks.opencms.unittesting;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import source.org.apache.java.util.Configurations;
import source.org.apache.java.util.ExtendedProperties;

import com.opencms.boot.CmsBase;
import com.opencms.core.A_OpenCms;
import com.opencms.core.CmsException;
import com.opencms.core.CmsShell;
import com.opencms.core.I_CmsConstants;
import com.opencms.core.OpenCms;
import com.opencms.file.CmsObject;

/**
 * Implements a CmsShell as a Singleton for unit testing purposes
 * Example of use:
 * 		// Create a CMS shell for the suite of tests
 *		UnitTestingCmsShell.reset(); // Make sure we start fresh
 *		UnitTestingCmsShell.setUserName(USER_NAME); 
 *		UnitTestingCmsShell.setPassword(PASSWORD);
 *		shell = UnitTestingCmsShell.instance();
 * 
 * @author Laurent Gauthier
 */
public class UnitTestingCmsShell extends CmsShell implements
I_CmsConstants {
	
	private static String userName = "admin";
	private static String password = "admin";
	
	static private UnitTestingCmsShell _instance;
	
	protected OpenCms openCms;
	private CmsObject cmsObject; 

	//	Read properties file on startup
	 static {
		Properties properties = new Properties();
	 	try {
			InputStream is =
UnitTestingCmsShell.class.getResourceAsStream("shell.properties");
	 		properties.load(is);
	 		is.close();
	 		userName = properties.getProperty("user.name");
			password =
properties.getProperty("user.password");
	 	} catch (IOException e) {
	 		System.out.println("Error initializing the
class, properties not found." +
	 			e.getMessage());
	 	}
	 }
	 
	/**
	 * @return a CmsObject
	 */
	private CmsObject buildCmsObject() throws CmsException {
		CmsObject cms = new CmsObject();
		cms.loginUser(userName, password);
		
		openCms.initUser(cms, 
			null, 
			null, 
			C_USER_GUEST, 
			C_GROUP_GUEST, 
			C_PROJECT_ONLINE_ID, 
			null);
	
		return cms;
	}


	/**
	 * If this member is set to true the memory-logging is enabled.
	 */
	boolean logMemory = false;
	
	public static UnitTestingCmsShell instance() {

		if (null == _instance )
			_instance = new UnitTestingCmsShell();
		return _instance;
	}
	
	public CmsObject getCmsObject() {
		return cmsObject;
	}
	
	static public void reset() {

		try {
			if (_instance != null)
			_instance.destroy();
		} catch (CmsException e) {
			e.printStackTrace();
		}
		_instance = null;
	}
	
	/**
	 * 
	 */
	private UnitTestingCmsShell() {
		super();
	}
		
	protected void initialize() {        
		A_OpenCms.initVersion(this);
		
		// Make sure we use the proper encoding		
		System.setProperty("file.encoding", "ISO-8859-1");
		
	
CmsBase.setBasePath(System.getProperties().getProperty("user.dir"));
				
		try {
			String propsPath = "opencms.properties"; //
CmsBase.getPropertiesPath(false);
//			System.out.println("%%% props: " + propsPath);
			Configurations conf = new Configurations(new
ExtendedProperties(propsPath));
			openCms = new OpenCms(conf);
			cmsObject = new CmsObject();

			logMemory = conf.getBoolean("log.memory",
false);
			//log in default user.
			openCms.initUser(cmsObject, null, null,
C_USER_GUEST, C_GROUP_GUEST, C_PROJECT_ONLINE_ID, null);
		}
		catch(Exception exc) {
			try {
				this.destroy();
			} catch (CmsException e) {
				e.printStackTrace();
			}
			printException(exc);
		}
	}
	
	/**
	 * Destructor, should be called when the the class instance is
shut down.
	 */
	public void destroy() throws CmsException {
		openCms.destroy();
		_instance = null;
	}

	/**
	 * @param USER_NAME
	 */
	public static void setUserName(String aName) {
		userName = aName;		
	}

	/**
	 * @param PASSWORD
	 */
	public static void setPassword(String aPassword) {
		password = aPassword;		
	}
	/**
	 * @return
	 */
	public OpenCms getOpenCms() {
		return openCms;
	}

}




More information about the opencms-dev mailing list