All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.testtoolinterfaces.cmdline.CmdLineExecutionParser Maven / Gradle / Ivy

package org.testtoolinterfaces.cmdline;

import java.io.File;
import java.util.ArrayList;

import org.testtoolinterfaces.utils.RunTimeData;
import org.testtoolinterfaces.utils.RunTimeVariable;

public class CmdLineExecutionParser extends CmdLineAbstractParser
{
	public CmdLineExecutionParser( String anApplicationName )
	{
		super( anApplicationName );
		super.acceptCommand( EXECUTE,
			"Execute one or more tests, including preparation and restoration" );
		super.acceptCommand( VALIDATE,
			"Validate a Test File, a Test Group or a Test Case" );
		super.acceptCommand( PREPARE,
			"Do all preparational steps for a Test Group or Case" );
		super.acceptCommand(RESTORE,
			"Do all restorational steps for a Test Group or Case" );
		super.acceptCommand(INTERFACES,
			"Prints a list of supported interfaces" );
		super.acceptCommand(KEYWORDS,
			"Prints a list of supported keywords" );
		super.acceptCommand(KEYWORD_DETAILS,
			"Prints details of a keyword, such as description and parameters" );
		
		ArrayList testFileOptions = new ArrayList();
		testFileOptions.add( FILE );
		testFileOptions.add( TESTFILE );
		super.acceptOptions( testFileOptions, "Test Group File")
			.withRequiredArg().ofType( File.class );

		super.acceptOption(CONFIGFILE, "Individual Configuration File")
			.withRequiredArg().ofType( File.class );
		
		super.acceptOption(GLOBALCONFIGFILE, "Global Configuration File")
			.withRequiredArg().ofType( File.class );

		ArrayList testGroupOptions = new ArrayList();
		testGroupOptions.add( GROUP );
		testGroupOptions.add( TESTGROUP );
		super.acceptOptions( testGroupOptions, "Test Group ID")
			.withRequiredArg().ofType( String.class ).describedAs("ID");

		ArrayList testCaseOptions = new ArrayList();
		testCaseOptions.add( CASE );
		testCaseOptions.add( TESTCASE );
		super.acceptOptions( testCaseOptions, "Test Case ID")
			.withRequiredArg().ofType( String.class ).describedAs("ID");
	}

	@Override
	public void parse(RunTimeData aRtData, String... anArgs) throws ParameterException
	{
		File userHome = getUserHome(aRtData);
		File baseDir = getBaseDir(aRtData);

		File configDir = new File( baseDir, "config" );
		RunTimeVariable configDirVar = new RunTimeVariable(CONFIGDIR, configDir);
		aRtData.add(configDirVar);

		super.parse(aRtData, anArgs);

		String command = aRtData.getValueAsString(CmdLineParser.COMMAND);
		
		if ( ! command.equalsIgnoreCase( CmdLineParser.HELP ))
		{
			// Check if variables are used. Replace if so
			// Check that Files are absolute and exist. Extend if not
			// Set as File-object in RunTimeData
			setTestGroupFile(aRtData);
			setIndividualConfigFile(aRtData, userHome);
			setGlobalConfigFile(aRtData, baseDir, configDir);
		}
	}

	/**
	 * @param aRtData
	 * @throws ParameterException
	 */
	private void setTestGroupFile(RunTimeData aRtData) throws ParameterException
	{
		File tmpTestFile = aRtData.getValueAsFile(TESTFILE);
		if ( tmpTestFile != null )
		{
			String tmpTestFileName = tmpTestFile.getPath();
			File testFile = new File( aRtData.substituteVars(tmpTestFileName) );

			if ( ! testFile.exists() )
			{
				throw new ParameterException( TESTFILE + " cannot be found: " + tmpTestFileName );								
			}
			RunTimeVariable testFileVar = new RunTimeVariable(TESTFILE, testFile);
			aRtData.add(testFileVar);
			RunTimeVariable fileVar = new RunTimeVariable(FILE, testFile);
			aRtData.add(fileVar);
			RunTimeVariable testSuiteDirVar = new RunTimeVariable(TESTSUITEDIR, testFile.getParentFile());
			aRtData.add(testSuiteDirVar);
		}
	}

	/**
	 * @param aRtData
	 * @param aUserHome
	 * @throws ParameterException
	 */
	private void setIndividualConfigFile(RunTimeData aRtData, File aUserHome)
			throws ParameterException
	{
		File tmpConfigFile = aRtData.getValueAsFile(CONFIGFILE);
		if ( tmpConfigFile != null )
		{
			String individualConfigFileName = aRtData.substituteVars(tmpConfigFile.getPath());
			File individualConfigFile = new File( individualConfigFileName );

			if ( ! individualConfigFile.isAbsolute() )
			{
				individualConfigFile = new File( aUserHome, individualConfigFileName );
			}
			RunTimeVariable configFileVar = new RunTimeVariable(CONFIGFILE, individualConfigFile);
			aRtData.add(configFileVar);
		}
	}

	/**
	 * @param aRtData
	 * @param baseDir
	 * @param configDir
	 * @throws ParameterException
	 */
	private void setGlobalConfigFile(RunTimeData aRtData, File aBaseDir, File aConfigDir)
		throws ParameterException
	{
		File tmpGlobalConfigFile = aRtData.getValueAsFile(GLOBALCONFIGFILE);
		if ( tmpGlobalConfigFile != null )
		{
			String tmpGlobalConfigFilePath = tmpGlobalConfigFile.getPath();
			File globalConfigFile = new File( aRtData.substituteVars(tmpGlobalConfigFilePath) );

			if ( ! globalConfigFile.isAbsolute() )
			{
				globalConfigFile = new File( aConfigDir, tmpGlobalConfigFilePath );
				if ( ! globalConfigFile.exists() )
				{
					globalConfigFile = new File( aBaseDir, tmpGlobalConfigFilePath );
				}
			}
			if ( ! globalConfigFile.exists() )
			{
				throw new ParameterException( GLOBALCONFIGFILE + " cannot be found: " + tmpGlobalConfigFilePath );								
			}
			RunTimeVariable globalConfigFileVar = new RunTimeVariable(GLOBALCONFIGFILE, globalConfigFile);
			aRtData.add(globalConfigFileVar);
		}
	}

	/**
	 * @param aRtData
	 * @return
	 * @throws ParameterException
	 */
	private File getBaseDir(RunTimeData aRtData) throws ParameterException
	{
		File baseDir = aRtData.getValueAsFile(BASEDIR);
		if ( baseDir == null )
		{
			throw new Error( BASEDIR + " is not defined in RunTimeData" );			
		}
		if ( ! baseDir.isDirectory() )
		{
			throw new ParameterException( BASEDIR + " is not set or not valid: " + baseDir.getAbsolutePath() );			
		}
		return baseDir;
	}

	/**
	 * @param aRtData
	 * @return
	 * @throws ParameterException
	 */
	private File getUserHome(RunTimeData aRtData) throws ParameterException
	{
		File userHome = aRtData.getValueAsFile(USERHOME);
		if ( userHome == null )
		{
			throw new Error( USERHOME + " is not defined in RunTimeData" );			
		}
		if ( ! userHome.isDirectory() )
		{
			throw new ParameterException( USERHOME + " is not a valid directory: " + userHome.getAbsolutePath() );			
		}
		return userHome;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy