atg.nucleus
Class NucleusTestUtils

java.lang.Object
  extended by atg.nucleus.NucleusTestUtils

public class NucleusTestUtils
extends java.lang.Object

NucleusTestUtils

Version:
$Id: //test/UnitTests/base/main/src/Java/atg/nucleus/NucleusTestUtils.java#21 $ This class contains some utility methods to make it faster to write a unit test that needs to resolve componants against Nucleus.
Author:
adamb

Nested Class Summary
static class NucleusTestUtils.NucleusStartupOptions
          A class representing NucleusStartupOptions, used by startNucleusWithModules().
 
Field Summary
static java.lang.String ATG_DUST_TESTCONFIG
           
static java.lang.String CLASS_VERSION
          Class version string
static org.apache.log4j.Logger log
           
static boolean sRemoveTempAtgServerDirectories
          Whether or not to remove tempoary ATG server directories created by startNucleusWithModules().
 
Constructor Summary
NucleusTestUtils()
           
 
Method Summary
static void addComponent(atg.nucleus.Nucleus pNucleus, java.lang.String pComponentPath, java.lang.Object pComponent)
          Adds the given object, pComponent to Nucleus, pNucleus at the path given by pComponentPath.
static java.io.File createInitial(java.io.File pRoot, java.util.List pInitialServices)
          Creates an Initial.properties file pRoot The root directory of the configpath pInitialServices A list of initial services
static java.io.File createProperties(java.lang.String pComponentName, java.io.File pConfigDir, java.lang.String pClass, java.util.Properties pProps)
          Creates a .properties file
protected static java.io.File createTempServerDir()
          Create a temporary, empty server directory.
static int findFreePort()
          This method returns a free port number on the current machine.
static java.io.File getConfigpath(java.lang.Class pClassRelativeTo, java.lang.String pBaseConfigDirectory)
          A convenience method for returning the configpath for a test.
static java.io.File getConfigpath(java.lang.Class pClassRelativeTo, java.lang.String pBaseConfigDirectory, boolean pCreate)
          A convenience method for returning the configpath for a test.
static java.io.File getConfigpath(java.lang.String pBaseConfigDirectory)
          A convenience method for returning the configpath for a test.
static java.io.File getConfigpath(java.lang.String pBaseConfigDirectory, boolean pCreate)
          A convenience method for returning the configpath for a test.
static java.lang.String getGlobalTestConfig()
          Look up the global testconfig path.
static void setAbsoluteName(java.lang.String pName, atg.nucleus.GenericService pService)
          Allows the absoluteName of the given service to be explicitly defined.
static void shutdownNucleus(atg.nucleus.Nucleus pNucleus)
          Shutdown the specified Nucleus and try to delete the associated temporary server directory.
static atg.nucleus.Nucleus startNucleus(java.io.File configpath)
          Starts Nucleus using the given config directory
static atg.nucleus.Nucleus startNucleus(java.lang.String pSingleConfigpathEntry)
          Starts Nucleus using the given config directory
static atg.nucleus.Nucleus startNucleusWithModules(NucleusTestUtils.NucleusStartupOptions pOptions)
          This method starts nucleus with a config path calculated from the specified list of Dynamo modules ("DAS", "DPS", "DSS", "Publishing.base", etc).
static atg.nucleus.Nucleus startNucleusWithModules(java.lang.String[] pModules, java.lang.Class pClassRelativeTo, java.lang.String pInitialService)
          This method starts nucleus with a config path calculated from the specified list of Dynamo modules ("DAS", "DPS", "DSS", "Publishing.base", etc).
static atg.nucleus.Nucleus startNucleusWithModules(java.lang.String[] pModules, java.lang.Class pClassRelativeTo, java.lang.String pBaseConfigDirectory, java.lang.String pInitialService)
          This method starts nucleus with a config path calculated from the specified list of Dynamo modules ("DAS", "DPS", "DSS", "Publishing.base", etc).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLASS_VERSION

public static java.lang.String CLASS_VERSION
Class version string


log

public static final org.apache.log4j.Logger log

ATG_DUST_TESTCONFIG

public static final java.lang.String ATG_DUST_TESTCONFIG
See Also:
Constant Field Values

sRemoveTempAtgServerDirectories

public static boolean sRemoveTempAtgServerDirectories
Whether or not to remove tempoary ATG server directories created by startNucleusWithModules(). True by default, but can be set to false for debugging.

Constructor Detail

NucleusTestUtils

public NucleusTestUtils()
Method Detail

createInitial

public static java.io.File createInitial(java.io.File pRoot,
                                         java.util.List pInitialServices)
                                  throws java.io.IOException
Creates an Initial.properties file pRoot The root directory of the configpath pInitialServices A list of initial services

Parameters:
pRoot - The root of the config path entry.
pInitialServices - initial services list
Returns:
the create initial services properties file.
Throws:
java.io.IOException - if an error occurs

setAbsoluteName

public static void setAbsoluteName(java.lang.String pName,
                                   atg.nucleus.GenericService pService)
Allows the absoluteName of the given service to be explicitly defined. Normally this is determined by the object's location in the Nucleus hierarchy. For test items that are not really bound to Nucleus, it's convenient to just give it an absolute name rather than going through the whole configuration and binding process.

Parameters:
pName - The absolute name value to set
pService - The service whose absolute nameshould be set.

addComponent

public static void addComponent(atg.nucleus.Nucleus pNucleus,
                                java.lang.String pComponentPath,
                                java.lang.Object pComponent)
Adds the given object, pComponent to Nucleus, pNucleus at the path given by pComponentPath.

Parameters:
pNucleus - The Nucleus instance to which the component should be added
pComponentPath - the component path at which the component should be added
pComponent - the component instance to add

createProperties

public static java.io.File createProperties(java.lang.String pComponentName,
                                            java.io.File pConfigDir,
                                            java.lang.String pClass,
                                            java.util.Properties pProps)
                                     throws java.io.IOException
Creates a .properties file

Parameters:
pComponentName - Name of the component
pConfigDir - Name of the configuration directory. If null, will add to the current working directory.
pClass - The class of the component (used for $class property).
pProps - Other properties of the component.
Returns:
The created file.
Throws:
java.io.IOException

startNucleus

public static atg.nucleus.Nucleus startNucleus(java.io.File configpath)
Starts Nucleus using the given config directory

Parameters:
configpath - the config path directory entry to use as the entire config path.
Returns:
the started Nucleus

startNucleus

public static atg.nucleus.Nucleus startNucleus(java.lang.String pSingleConfigpathEntry)
Starts Nucleus using the given config directory

Parameters:
pSingleConfigpathEntry - the path name of the config path entry to specify.
Returns:
The started nucleus.

getConfigpath

public static java.io.File getConfigpath(java.lang.String pBaseConfigDirectory)
A convenience method for returning the configpath for a test. pConfigDirectory is the top level name to be used for the configpath. Returns a file in the "config/data" subdirectory of the passed in file.

Parameters:
pBaseConfigDirectory - the base configuration directory.
Returns:
The calculated configuration path.

getConfigpath

public static java.io.File getConfigpath(java.lang.String pBaseConfigDirectory,
                                         boolean pCreate)
A convenience method for returning the configpath for a test. pConfigDirectory is the top level name to be used for the configpath. Returns a file in the "data" subdirectory of the passed in file.

Parameters:
pBaseConfigDirectory - the base configuration directory.
pCreate - whether to create the config/data subdirectory if it does not exist.
Returns:
The calculated configuration path.

getConfigpath

public static java.io.File getConfigpath(java.lang.Class pClassRelativeTo,
                                         java.lang.String pBaseConfigDirectory)
A convenience method for returning the configpath for a test. pConfigDirectory is the top level name to be used for the configpath. Returns a file in the pBaseConfigDirectory (or pBaseConfigDirectory + "data") subdirectory of the the passed in class's location.

The directory location is calculated as (in psuedocode): (pClassRelativeTo's package location) + "/" + (pConfigDirectory or "data") + "/config" This method always creates the config/data subdirectory if it does not exist.

Parameters:
pClassRelativeTo - the class whose package the config/data (or pBaseConfigDirectory/data) should be relative in.
pBaseConfigDirectory - the base configuration directory If null, uses "config".
Returns:
The calculated configuration path.

getConfigpath

public static java.io.File getConfigpath(java.lang.Class pClassRelativeTo,
                                         java.lang.String pBaseConfigDirectory,
                                         boolean pCreate)
A convenience method for returning the configpath for a test. pConfigDirectory is the top level name to be used for the configpath. Returns a file in the pBaseConfigDirectory (or pBaseConfigDirectory + "data") subdirectory of the the passed in class's location.

The directory location is calculated as (in psuedocode): (pClassRelativeTo's package location) + "/" + (pConfigDirectory or "data") + "/config"

Parameters:
pClassRelativeTo - the class whose package the config/data (or pBaseConfigDirectory/data) should be relative in.
pBaseConfigDirectory - the base configuration directory If null, uses "config".
pCreate - whether to create the config/data subdirectory if it does not exist.
Returns:
The calculated configuration path.

getGlobalTestConfig

public static java.lang.String getGlobalTestConfig()
Look up the global testconfig path. This path is specified in either an

Returns:

startNucleusWithModules

public static atg.nucleus.Nucleus startNucleusWithModules(java.lang.String[] pModules,
                                                          java.lang.Class pClassRelativeTo,
                                                          java.lang.String pInitialService)
                                                   throws javax.servlet.ServletException
This method starts nucleus with a config path calculated from the specified list of Dynamo modules ("DAS", "DPS", "DSS", "Publishing.base", etc). Additionally adds a directory calculated relative to the location of pClassRelativeTo's package name from the classloader. The added config directory is calculated as (in psuedocode): (pClassRelativeTo's package location) + "/data/" + (pClassRelativeTo's simpleClassName) + "/config" and is only added if the directory exists.

You must specify a pInitialService parameter, which will be the initial service started by Nucleus (rather than the normally Initial component, which would do a full Nucleus component start-up).

This method also creates a temporary server directory, which is deleted when shutdownNucleus in invoked on the returned directory.

Note: If you need to start up a complete ATG instance, you should use DUST rather than a unit test.

Note: You may also wish to use a ConfigCreationFilter. You can either set a value for Nucleus.CREATION_FILTER_CLASS_PROPERTY_NAME ("atg.nucleus.Nucleus.creationFilterClass") as a DynamoEnv or System property, or set the creationFilter property in Nucleus.properties in your configuration. This allows on to block creation of referenced components without having to make additional properties file changes. Note 3: Nucleus's classReplacementMap can also be useful for replacing a component instance with a subclass.

Parameters:
pModules - the list of modules to use to calculate the Nucleus configuration path.
pClassRelativeTo - the class whose name and package will be used for the {packageName}/config/{ClassName}/data directory
pInitialService - the nucleus path of the Nucleus component to start-up. This is a required property to prevent accidental full start-up.
Returns:
the started Nucleus instance that should later be shut down with the shutdownNucleus method.
Throws:
javax.servlet.ServletException - if an error occurs

startNucleusWithModules

public static atg.nucleus.Nucleus startNucleusWithModules(java.lang.String[] pModules,
                                                          java.lang.Class pClassRelativeTo,
                                                          java.lang.String pBaseConfigDirectory,
                                                          java.lang.String pInitialService)
                                                   throws javax.servlet.ServletException
This method starts nucleus with a config path calculated from the specified list of Dynamo modules ("DAS", "DPS", "DSS", "Publishing.base", etc). Additionally adds a directory calculated relative to the location of pClassRelativeTo's package name from the classloader. The added config directory is calculated as (in psuedocode): (pClassRelativeTo's package location) + "/data/" + (pBaseConfigDirectory or "config") and is only added if the directory exists.

You must specify a pInitialService parameter, which will be the initial service started by Nucleus (rather than the normally Initial component, which would do a full Nucleus component start-up).

This method also creates a temporary server directory, which is deleted when shutdownNucleus in invoked on the returned directory.

Note: If you need to start up a complete ATG instance, you should use DUST rather than a unit test.

Note: You may also wish to use a ConfigCreationFilter. You can either set a value for Nucleus.CREATION_FILTER_CLASS_PROPERTY_NAME ("atg.nucleus.Nucleus.creationFilterClass") as a DynamoEnv or System property, or set the creationFilter property in Nucleus.properties in your configuration. This allows on to block creation of referenced components without having to make additional properties file changes. Note 3: Nucleus's classReplacementMap can also be useful for replacing a component instance with a subclass.

Parameters:
pModules - the list of modules to use to calculate the Nucleus configuration path.
pClassRelativeTo - the class whose package the config/data (or pBaseConfigDirectory/data) should be relative in.
pBaseConfigDirectory - the base configuration directory. If this parameter is non-null, the relative configuration subdirectory will be ("data/" + pBaseConfigDirectory) rather than "data/config".
pInitialService - the nucleus path of the Nucleus component to start-up. This is a required property to prevent accidental full start-up.
Returns:
the started Nucleus instance that should later be shut down with the shutdownNucleus method.
Throws:
javax.servlet.ServletException - if an error occurs

startNucleusWithModules

public static atg.nucleus.Nucleus startNucleusWithModules(NucleusTestUtils.NucleusStartupOptions pOptions)
                                                   throws javax.servlet.ServletException
This method starts nucleus with a config path calculated from the specified list of Dynamo modules ("DAS", "DPS", "DSS", "Publishing.base", etc). Additionally adds a directory calculated relative to the location of pClassRelativeTo's package name from the classloader. The added config directory is calculated as (in psuedocode): (pClassRelativeTo's package location) + "/data/" + (pBaseConfigDirectory or "config") and is only added if the directory exists.

You must specify a pInitialService parameter, which will be the initial service started by Nucleus (rather than the normally Initial component, which would do a full Nucleus component start-up).

This method also creates a temporary server directory, which is deleted when shutdownNucleus in invoked on the returned directory.

Note: If you need to start up a complete ATG instance, you should use DUST rather than a unit test.

Note: You may also wish to use a ConfigCreationFilter. You can either set a value for Nucleus.CREATION_FILTER_CLASS_PROPERTY_NAME ("atg.nucleus.Nucleus.creationFilterClass") as a DynamoEnv or System property, or set the creationFilter property in Nucleus.properties in your configuration. This allows on to block creation of referenced components without having to make additional properties file changes. Note 3: Nucleus's classReplacementMap can also be useful for replacing a component instance with a subclass. See NucleusStartupOptions for the effects of individual properties of pOptions.

Parameters:
pOptions - the startup Options for Nucleus.
Returns:
the started Nucleus instance that should later be shut down with the shutdownNucleus method.
Throws:
javax.servlet.ServletException - if an error occurs

shutdownNucleus

public static void shutdownNucleus(atg.nucleus.Nucleus pNucleus)
                            throws atg.nucleus.ServiceException,
                                   java.io.IOException
Shutdown the specified Nucleus and try to delete the associated temporary server directory. Typically used on a Nucleus created by startNucleusWithModules.

Parameters:
pNucleus - the nucleus instance to shut down.
Throws:
ServiceException - if an error occurs
java.io.IOException - if an error occurs (such as a failure to remove the temporary server directory).

createTempServerDir

protected static java.io.File createTempServerDir()
                                           throws java.io.IOException
Create a temporary, empty server directory. This is to satisfy Dynamo's need to have a server directory, yet not conflict if multiple tests are running at the same time against the same Dynamo instance. The directory name is generated by File.createTempFile.

Returns:
the created temporary server directory.
Throws:
java.io.IOException - if an error occurs

findFreePort

public static int findFreePort()
This method returns a free port number on the current machine. There is some chance that the port number could be taken by the time the caller actually gets around to using it. This method returns -9999 if it's not able to find a port.



Copyright © 2009. All Rights Reserved.