Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * OfficeFloor - http://www.officefloor.net
   * Copyright (C) 2005-2011 Daniel Sagenschneider
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
  * This program 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.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 package net.officefloor.compile.test.section;
 
 
Utility class for testing a net.officefloor.compile.spi.section.source.SectionSource.

Author(s):
Daniel Sagenschneider
 
 public class SectionLoaderUtil {

 
 	public static final String SECTION_NAME = "TEST";

 
 	public static <S extends SectionSourcePropertyList validateSpecification(
 			Class<S> sectionSourceClassString... propertyNameLabels) {
 
 		// Load the specification
 				.loadSpecification(sectionSourceClass);
 
 		// Verify the properties
 		PropertyListUtil.validatePropertyNameLabels(propertyList,
 				propertyNameLabels);
 
 		// Return the property list
 		return propertyList;
 	}

Convenience method to obtain the class path location.

Parameters:
offsetClass Class indicating the package that the resource is within. Typically this will be the junit.framework.TestCase instance.
resourceName Name of the resource.
Returns:
Class path location of the resource.
	public static String getClassPathLocation(Class<?> offsetClass,
			String resourceName) {
		// Obtain the package location
		String packageLocation = offsetClass.getPackage().getName();
		packageLocation = packageLocation.replace('.''/');
		// Provide the class path location
		String classpathLocation = packageLocation + "/" + resourceName;
		return classpathLocation;
	}

	public static <S extends SectionSourceSectionDesigner createSectionDesigner(
			Class<S> sectionSourceClass) {
		NodeContext context = (NodeContextcompiler;
		return new SectionNodeImpl("TEST_LOCATION"context);
	}

Facade method to validate the net.officefloor.compile.section.SectionType and net.officefloor.compile.spi.office.OfficeSection.

Parameters:
designer net.officefloor.compile.spi.section.SectionDesigner containing the expected net.officefloor.compile.section.SectionType/net.officefloor.compile.spi.office.OfficeSection.
sectionSourceClass Class of the net.officefloor.compile.spi.section.source.SectionSource being tested.
offsetClass Object indicating the package that the resource is within. Typically this will be the junit.framework.TestCase instance.
resourceName Name of the resource. This is used with the offsetObject to determine the net.officefloor.compile.spi.office.OfficeSection location.
propertyNameValuePairs Listing of net.officefloor.compile.properties.Property name/value pairs.
	public static <S extends SectionSourcevoid validateSection(
			SectionDesigner designerClass<S> sectionSourceClass,
			Class<?> offsetClassString resourceName,
			String... propertyNameValuePairs) {
		// Obtain the section location
		String sectionLocation = getClassPathLocation(offsetClassresourceName);
		// Validate the section
		validateSection(designersectionSourceClasssectionLocation,
				propertyNameValuePairs);
	}

	public static <S extends SectionSourcevoid validateSection(
			SectionDesigner designerClass<S> sectionSourceClass,
			String sectionLocationString... propertyNameValuePairs) {
		// Validate the section type
		validateSectionType(designersectionSourceClasssectionLocation,
				propertyNameValuePairs);
		// Validate the office section
		validateOfficeSection(designersectionSourceClasssectionLocation,
				propertyNameValuePairs);
	}

Convenience method that validates the loaded net.officefloor.compile.section.SectionType against expected net.officefloor.compile.section.SectionType from the net.officefloor.compile.spi.section.SectionDesigner.

Parameters:
designer net.officefloor.compile.spi.section.SectionDesigner containing the expected net.officefloor.compile.section.SectionType.
sectionSourceClass Class of the net.officefloor.compile.spi.section.source.SectionSource being tested.
sectionLocation Location of the net.officefloor.compile.spi.office.OfficeSection.
propertyNameValuePairs Listing of net.officefloor.compile.properties.Property name/value pairs.
	public static <S extends SectionSourcevoid validateSectionType(
			SectionDesigner designerClass<S> sectionSourceClass,
			String sectionLocationString... propertyNameValuePairs) {
		// Cast to obtain expected section type
		if (!(designer instanceof SectionType)) {
			TestCase.fail("designer must be created from createSectionDesigner");
		}
		SectionType expectedSection = (SectionTypedesigner;
		// Load the actual section type
		SectionType actualSection = loadSectionType(sectionSourceClass,
				sectionLocationpropertyNameValuePairs);
		// Validate section inputs are as expected
		SectionInputType[] eInputs = expectedSection.getSectionInputTypes();
		SectionInputType[] aInputs = actualSection.getSectionInputTypes();
		TestCase.assertEquals("Incorrect number of inputs"eInputs.length,
				aInputs.length);
		for (int i = 0; i < eInputs.lengthi++) {
			SectionInputType eInput = eInputs[i];
			SectionInputType aInput = aInputs[i];
			TestCase.assertEquals("Incorrect name for input " + i,
			TestCase.assertEquals("Incorrect parameter type for input " + i,
					eInput.getParameterType(), aInput.getParameterType());
		}
		// Validate the section outputs are as expected
		SectionOutputType[] eOutputs = expectedSection.getSectionOutputTypes();
		SectionOutputType[] aOutputs = actualSection.getSectionOutputTypes();
		TestCase.assertEquals("Incorrect number of outputs"eOutputs.length,
				aOutputs.length);
		for (int i = 0; i < eOutputs.lengthi++) {
			SectionOutputType eOutput = eOutputs[i];
			SectionOutputType aOutput = aOutputs[i];
			TestCase.assertEquals("Incorrect name for output " + i,
			TestCase.assertEquals("Incorrect argument type for output " + i,
					eOutput.getArgumentType(), aOutput.getArgumentType());
			TestCase.assertEquals("Incorrect escalation only for output " + i,
					eOutput.isEscalationOnly(), aOutput.isEscalationOnly());
		}
		// Validate the section objects are as expected
		SectionObjectType[] eObjects = expectedSection.getSectionObjectTypes();
		SectionObjectType[] aObjects = actualSection.getSectionObjectTypes();
		TestCase.assertEquals("Incorrect number of objects"eObjects.length,
				aObjects.length);
		for (int i = 0; i < eObjects.lengthi++) {
			SectionObjectType eObject = eObjects[i];
			SectionObjectType aObject = aObjects[i];
			TestCase.assertEquals("Incorrect name for object " + i,
			TestCase.assertEquals("Incorrect object type for input " + i,
					eObject.getObjectType(), aObject.getObjectType());
		}
	}

	public static <S extends SectionSourcevoid validateOfficeSection(
			SectionDesigner designerClass<S> sectionSourceClass,
			String sectionLocationString... propertyNameValuePairs) {
		// Cast to obtain expected section type
		if (!(designer instanceof OfficeSection)) {
			TestCase.fail("designer must be created from createSectionDesigner");
		}
		OfficeSection eSection = (OfficeSectiondesigner;
		// Load the actual section type
				sectionSourceClasssectionLocationpropertyNameValuePairs);
		// Validate the office section
		TestCase.assertEquals("Incorrect section name",
		// Validate the office section inputs
		OfficeSectionInput[] eInputs = eSection.getOfficeSectionInputs();
		OfficeSectionInput[] aInputs = aSection.getOfficeSectionInputs();
		TestCase.assertEquals("Incorrect number of section inputs",
				eInputs.lengthaInputs.length);
		for (int i = 0; i < eInputs.lengthi++) {
			OfficeSectionInput eInput = eInputs[i];
			OfficeSectionInput aInput = aInputs[i];
			TestCase.assertEquals("Incorrect name for section input " + i,
			TestCase.assertEquals("Incorrect parameter type for section input "
ieInput.getParameterType(), aInput.getParameterType());
		}
		// Validate the office section outputs
		OfficeSectionOutput[] eOutputs = eSection.getOfficeSectionOutputs();
		OfficeSectionOutput[] aOutputs = aSection.getOfficeSectionOutputs();
		TestCase.assertEquals("Incorrect number of section outputs",
				eOutputs.lengthaOutputs.length);
		for (int i = 0; i < eOutputs.lengthi++) {
			OfficeSectionOutput eOutput = eOutputs[i];
			OfficeSectionOutput aOutput = aOutputs[i];
			TestCase.assertEquals("Incorrect name for section output " + i,
			TestCase.assertEquals("Incorrect argument type for section output "
ieOutput.getArgumentType(), aOutput.getArgumentType());
			TestCase.assertEquals(
					"Incorrect escalation only for section output " + i,
					eOutput.isEscalationOnly(), aOutput.isEscalationOnly());
		}
		// Validate the office section objects
		OfficeSectionObject[] eObjects = eSection.getOfficeSectionObjects();
		OfficeSectionObject[] aObjects = aSection.getOfficeSectionObjects();
		TestCase.assertEquals("Incorrect number of section objects",
				eObjects.lengthaObjects.length);
		for (int i = 0; i < eObjects.lengthi++) {
			OfficeSectionObject eObject = eObjects[i];
			OfficeSectionObject aObject = aObjects[i];
			TestCase.assertEquals("Incorrect name for section object " + i,
			TestCase.assertEquals("Incorrect object type for section object "
ieObject.getObjectType(), aObject.getObjectType());
		}
		// Validate remaining of the office section
		validateOfficeSubSection(nulleSectionaSection);
	}

	private static void validateOfficeSubSection(String subSectionName,
			OfficeSubSection eSectionOfficeSubSection aSection) {
		// Validate the office sub section
		TestCase.assertEquals("Incorrect section name (parent section="
subSectionName + ")"eSection.getOfficeSectionName(),
		// Determine this sub section name
		subSectionName = (subSectionName == null ? "" : subSectionName + ".")
eSection.getOfficeSectionName();
		// Validate the tasks
		OfficeTask[] eTasks = eSection.getOfficeTasks();
		OfficeTask[] aTasks = aSection.getOfficeTasks();
		TestCase.assertEquals("Incorrect number of tasks (section="
subSectionName + ")"eTasks.lengthaTasks.length);
		for (int i = 0; i < eTasks.lengthi++) {
			OfficeTask eTask = eTasks[i];
			OfficeTask aTask = aTasks[i];
			TestCase.assertEquals("Incorrect name for task " + i
" (sub section=" + subSectionName + ")",
			TestCase.assertNotNull("Must have team responsible for task " + i
" (sub section=" + subSectionName + ")",
			// Validate the dependencies
			ObjectDependency[] eDependencies = eTask.getObjectDependencies();
			ObjectDependency[] aDependencies = aTask.getObjectDependencies();
			TestCase.assertEquals(
					"Incorrect number of dependencies for task " + i
" (sub section=" + subSectionName + ", task="
eTask.getOfficeTaskName() + ")",
					eDependencies.lengthaDependencies.length);
			for (int j = 0; j < eDependencies.lengthj++) {
				ObjectDependency eDependency = eDependencies[j];
				ObjectDependency aDependency = aDependencies[j];
				TestCase.assertEquals(
						"Incorrect name for dependency " + j + " (sub section="
subSectionName + ", task="
eTask.getOfficeTaskName() + ")",
				// Do not check dependent as requires linking
			}
		}
		// Validate the managed object sources
		OfficeSectionManagedObjectSource[] eMoSources = eSection
		OfficeSectionManagedObjectSource[] aMoSources = aSection
		TestCase.assertEquals(
				"Incorrect number of managed object sources (sub section="
subSectionName + ")"eMoSources.length,
				aMoSources.length);
		for (int i = 0; i < eMoSources.lengthi++) {
			OfficeSectionManagedObjectSource eMoSource = eMoSources[i];
			OfficeSectionManagedObjectSource aMoSource = aMoSources[i];
			TestCase.assertEquals("Incorrect name for managed obect source "
i + " (sub section=" + subSectionName + ")",
			String managedObjectSourceName = eMoSource
			// Ensure load the managed object type to have list of teams
			// Validate the managed object source teams
			ManagedObjectTeam[] eTeams = eMoSource
			ManagedObjectTeam[] aTeams = aMoSource
			TestCase.assertEquals(
					"Incorrect number of teams for managed object source " + i
" (managed object source="
managedObjectSourceName + ", sub section="
subSectionName + ")"eTeams.length,
					aTeams.length);
			for (int j = 0; j < eTeams.lengthj++) {
				ManagedObjectTeam eTeam = eTeams[j];
				ManagedObjectTeam aTeam = aTeams[j];
				TestCase.assertEquals("Incorrect name for team " + j
" (managed object source=" + managedObjectSourceName
", sub section=" + subSectionName + ")",
			}
			// Validate the managed objects
			OfficeSectionManagedObject[] eMos = eMoSource
			OfficeSectionManagedObject[] aMos = eMoSource
			TestCase.assertEquals(
					"Incorrect number of managed objects (managed object source="
managedObjectSourceName + ", sub section="
subSectionName + ")"eMoSources.length,
					aMoSources.length);
			for (int j = 0; j < eMos.lengthj++) {
				TestCase.assertEquals("Incorrect name for managed object " + j
" (managed object source=" + managedObjectSourceName
", sub section=" + subSectionName + ")",
				TestCase.assertEquals(
						"Incorrect dependent name for managed object " + j
" (managed object source="
managedObjectSourceName + ", sub section="
subSectionName + ")",
				TestCase.assertEquals(
						"Incorrect administerable name for managed object " + i
" (managed object source="
managedObjectSourceName + ", sub section="
subSectionName + ")",
				String managedObjectName = eMo
				// Validate the managed object supported extension interfaces
				TestCase.assertEquals(
						"Incorrect number of supported extension interfaces for managed object "
j + " (managed object source="
managedObjectSourceName + ", sub section="
subSectionName + ")"eEis.length,
						aEis.length);
				for (int k = 0; k < eEis.lengthk++) {
					TestCase.assertEquals(
							"Incorrect class for extension interface " + k
" (managed object=" + managedObjectName
", managed object source="
managedObjectSourceName
", sub section=" + subSectionName + ")",
							eEis[k], aEis[k]);
				}
			}
		}
		// Validate the sub sections
		OfficeSubSection[] eSubSections = eSection.getOfficeSubSections();
		OfficeSubSection[] aSubSections = aSection.getOfficeSubSections();
		TestCase.assertEquals("Incorect number of sub sections (sub section="
subSectionName + ")"eSubSections.length,
				aSubSections.length);
		for (int i = 0; i < eSubSections.lengthi++) {
			OfficeSubSection eSubSection = eSubSections[i];
			OfficeSubSection aSubSection = aSubSections[i];
			TestCase.assertEquals("Incorrect name for sub section " + i
" (sub section=" + subSectionName + ")",
					eSubSection.getOfficeSectionName(),
					aSubSection.getOfficeSectionName());
		}
	}

	public static <S extends SectionSourceSectionType loadSectionType(
			Class<S> sectionSourceClassString sectionLocation,
			String... propertyNameValuePairs) {
		// Obtain the class loader
		ClassLoader classLoader = sectionSourceClass.getClassLoader();
				classLoader);
		try {
			// Load and return the section type
			return loadSectionType(sectionSourceClasssectionLocation,
					configurationContextclassLoaderpropertyNameValuePairs);
catch (Exception ex) {
			// Propagate as test case failure for tests not needing to handle
			StringWriter stackTrace = new StringWriter();
			ex.printStackTrace(new PrintWriter(stackTrace));
			TestCase.fail(stackTrace.toString());
			return null// fail will propagate failure
		}
	}

	public static <S extends SectionSourceSectionType loadSectionType(
			Class<S> sectionSourceClassString sectionLocation,
			ConfigurationContext configurationContextClassLoader classLoader,
			String... propertyNameValuePairsthrows Exception {
		// Load and return the section type
				sectionSourceClasssectionLocation,
				new PropertyListImpl(propertyNameValuePairs));
	}

	public static <S extends SectionSourceOfficeSection loadOfficeSection(
			String sectionNameClass<S> sectionSourceClass,
			String sectionLocationString... propertyNameValuePairs) {
		// Obtain the class loader
		ClassLoader classLoader = sectionSourceClass.getClassLoader();
				classLoader);
		// Load and return the office section
		return loadOfficeSection(sectionNamesectionSourceClass,
				sectionLocationconfigurationContextclassLoader,
				propertyNameValuePairs);
	}

	public static <S extends SectionSourceOfficeSection loadOfficeSection(
			String sectionNameClass<S> sectionSourceClass,
			String sectionLocationConfigurationContext configurationContext,
			ClassLoader classLoaderString... propertyNameValuePairs) {
		// Load and return the office section
				sectionNamesectionSourceClasssectionLocation,
				new PropertyListImpl(propertyNameValuePairs));
	}

		// Create the office floor compiler that fails on first issue
		OfficeFloorCompiler compiler = OfficeFloorCompiler
		return compiler;
	}

All access via static methods.
	private SectionLoaderUtil() {
	}
New to GrepCode? Check out our FAQ X