/**
 * File: AbstractPerlBasedTestCase.java
 * Created by: dstaines
 * Created on: Nov 13, 2009
 * CVS:  $$
 */
package org.ensembl.healthcheck.testcase;

import org.apache.commons.lang.StringUtils;
import org.ensembl.healthcheck.DatabaseRegistryEntry;

/**
 * <p>
 * Base class for invoking a perl script to carry out the test and parse the
 * output.
 * </p>
 * 
 * @author dstaines
 * 
 */
public abstract class AbstractPerlBasedTestCase extends
		AbstractShellBasedTestCase {

	public static final String PERLOPTS = "perlopts";
	public static final String PERL = "perl";

	protected String PERL5LIB = null;

	public String getPERL5LIB() {
		return PERL5LIB;
	}

	public void setPERL5LIB(String pERL5LIB) {
		PERL5LIB = pERL5LIB;
	}

	protected PerlScriptConfig config;

	public PerlScriptConfig getConfig() {
		if (config == null) {
			config = new PerlScriptConfig(System.getProperty(PERL),
					System.getProperty(PERLOPTS));
		}
		return config;
	}

	public void setConfig(PerlScriptConfig config) {
		this.config = config;
	}

	public AbstractPerlBasedTestCase() {
	}

	/**
	 * @return String perl script and relevant arguments to invoke with perl
	 *         binary and options from
	 *         {@link AbstractPerlBasedTestCase#getConfig()}
	 */
	protected abstract String getPerlScript(DatabaseRegistryEntry dbre,
			int speciesId);

	protected String createCommandLine(final DatabaseRegistryEntry dbre,
			int speciesId) {
		String commandLine = getPerlScript(dbre, speciesId);

		if (config != null) {

			if (!config.getPerlBinary().isEmpty()) {

				if (config.getPerlOptions().isEmpty()) {

					commandLine = config.getPerlBinary() + " " + commandLine;

				} else {

					commandLine = config.getPerlBinary() + " "
							+ config.getPerlOptions() + " " + commandLine;
				}
			}
		}
		return commandLine;
	}

	protected String[] environmentVarsToSet() {
		if (StringUtils.isEmpty(getPERL5LIB())) {
			return new String[] {};
		} else {
			return new String[] { "PERL5LIB=" + getPERL5LIB() };
		}
	}
}

@@ -1066,16 +1066,168 @@
 ase() %7B%0A
+%09%09// set PERL5LIB by default%0A%09%09String perl5Lib = System.getenv().get(%22PERL5LIB%22);%0A%09%09if (!StringUtils.isEmpty(perl5Lib)) %7B%0A%09%09%09setPERL5LIB(perl5Lib);%0A%09%09%7D%0A
 %09%7D%0A%0A%09/**
@@ -2021,91 +2021,8 @@
 ) %7B%0A
-%09%09if (StringUtils.isEmpty(getPERL5LIB())) %7B%0A%09%09%09return new String%5B%5D %7B%7D;%0A%09%09%7D else %7B%0A%09
 %09%09re
@@ -2072,17 +2072,13 @@
 IB() %7D;%0A
-%09%09%7D%0A
 %09%7D%0A%7D%0A

Set PERL5LIB by default using environment