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.impl.section;
 
 
 
 public class LoadSectionTypeTest extends OfficeFrameTestCase {

 
 	private final String SECTION_LOCATION = "SECTION";

 
 	private final ConfigurationContext configurationContext = this
 
 	private final CompilerIssues issues = this.createMock(CompilerIssues.class);
 
 	/*
 	 * (non-Javadoc)
 	 * 
 	 * @see junit.framework.TestCase#setUp()
 	 */
 	protected void setUp() throws Exception {
 		MockSectionSource.reset();
 	}

Ensure issue if fail to instantiate the net.officefloor.compile.spi.section.source.SectionSource.
 
 	public void testFailInstantiate() {
 
 		final RuntimeException failure = new RuntimeException(
 				"instantiate failure");
 
 		// Record failure to instantiate
				"Failed to instantiate " + MockSectionSource.class.getName()
" by default constructor"failure);
		// Attempt to obtain specification
		this.loadSectionType(falsenull);
	}

Ensure obtain the correct net.officefloor.compile.spi.office.OfficeSection location.
	public void testSectionLocation() {
		this.loadSectionType(truenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				assertEquals("Incorrect section location",
			}
		});
	}

Ensures issue if fails to obtain the net.officefloor.model.repository.ConfigurationItem.
	public void testFailGetConfigurationItem() throws Exception {
		final String location = "LOCATION";
		final IOException failure = new IOException(
				"Configuration Item failure");
		// Record failing to obtain the configuration item
				failure);
		this
				.record_issue("Failure obtaining configuration 'LOCATION'",
						failure);
		// Attempt to obtain the configuration item
		this.loadSectionType(falsenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				context.getConfiguration(location);
			}
		});
	}

	public void testGetConfigurationItem() throws Exception {
		final String location = "LOCATION";
		final ConfigurationItem item = this.createMock(ConfigurationItem.class);
		// Record obtaining the configuration item
				.getConfigurationItem(location), item);
		// Obtain the configuration item
		this.loadSectionType(truenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				assertEquals("Incorrect configuation item"itemcontext
						.getConfiguration(location));
			}
		});
	}

	public void testMissingProperty() {
		// Record missing property
		this.record_issue("Missing property 'missing' for SectionSource "
		// Attempt to load section type
		this.loadSectionType(falsenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				context.getProperty("missing");
			}
		});
	}

Ensure able to get properties.
	public void testGetProperties() {
		// Attempt to load section type
		this.loadSectionType(truenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				assertEquals("Ensure get defaulted property""DEFAULT",
						context.getProperty("missing""DEFAULT"));
				assertEquals("Ensure get property ONE""1"context
						.getProperty("ONE"));
				assertEquals("Ensure get property TWO""2"context
						.getProperty("TWO"));
				String[] names = context.getPropertyNames();
				assertEquals("Incorrect number of property names", 2,
						names.length);
				assertEquals("Incorrect property name 0""ONE"names[0]);
				assertEquals("Incorrect property name 1""TWO"names[1]);
				Properties properties = context.getProperties();
				assertEquals("Incorrect number of properties", 2, properties
						.size());
				assertEquals("Incorrect property ONE""1"properties
						.get("ONE"));
				assertEquals("Incorrect property TWO""2"properties
						.get("TWO"));
			}
		}, "ONE""1""TWO""2");
	}

Ensure able to get the java.lang.ClassLoader.
	public void testGetClassLoader() {
		// Attempt to load section type
		this.loadSectionType(truenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				assertEquals("Incorrect class loader",
			}
		});
	}

Ensure issue if fails to source the net.officefloor.compile.section.SectionType.
	public void testFailSourceSectionType() {
				"Fail source section type");
		// Record failure to source the section type
				"Failed to source SectionType definition from SectionSource "
MockSectionSource.class.getName(), failure);
		// Attempt to load section type
		this.loadSectionType(falsenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				throw failure;
			}
		});
	}

	public void testNullInputName() {
		// Record null input name
		this.record_issue("Null name for input 0");
		// Attempt to load section type
		this.loadSectionType(falsenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				section.addSectionInput(nullString.class.getName());
			}
		});
	}

Ensure can create input with a parameter type.
	public void testInputWithParameterType() {
		// Load section type
		SectionType type = this.loadSectionType(truenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				section.addSectionInput("INPUT"String.class.getName());
			}
		});
		// Ensure correct input
		assertEquals("Incorrect number of inputs", 1, type
		assertEquals("Incorrect input name""INPUT"type
		assertEquals("Incorrect parameter type"String.class.getName(), type
	}

Ensure can create input without a parameter type.
		// Load section type
		SectionType type = this.loadSectionType(truenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				section.addSectionInput("INPUT"null);
			}
		});
		// Ensure correct input
		assertEquals("Incorrect number of inputs", 1, type
		assertEquals("Incorrect input name""INPUT"type
		assertNull("Incorrect parameter type"type.getSectionInputTypes()[0]
	}

	public void testNullOutputName() {
		// Record null output name
		this.record_issue("Null name for output 0");
		// Attempt to load section type
		this.loadSectionType(falsenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				section.addSectionOutput(nullString.class.getName(), false);
			}
		});
	}

Ensure can create output with an argument type.
	public void testOutputWithArgumentType() {
		// Load section type
		SectionType type = this.loadSectionType(truenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				section.addSectionOutput("OUTPUT"Exception.class.getName(),
						true);
			}
		});
		// Ensure correct output
		assertEquals("Incorrect number of outputs", 1, type
		assertEquals("Incorrect output name""OUTPUT"type
		assertEquals("Incorrect argument type"Exception.class.getName(), type
		assertTrue("Incorrect is escalation only",
	}

Ensure can create output without an argument type.
		// Load section type
		SectionType type = this.loadSectionType(truenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				section.addSectionOutput("OUTPUT"nullfalse);
			}
		});
		// Ensure correct output
		assertEquals("Incorrect number of outputs", 1, type
		assertEquals("Incorrect output name""OUTPUT"type
		assertNull("Should be no argument type",
		assertFalse("Incorrect is escalation only"type
	}

	public void testNullObjectName() {
		// Record null object name
		this.record_issue("Null name for object 0");
		// Attempt to load section type
		this.loadSectionType(falsenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				section.addSectionObject(nullDouble.class.getName());
			}
		});
	}

	public void testNullObjectType() {
		// Record null object type
		this.record_issue("Null type for object 0 (name=OBJECT)");
		// Attempt to load section type
		this.loadSectionType(falsenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				section.addSectionObject("OBJECT"null);
			}
		});
	}

Ensure can create object with an object type.
	public void testObjectWithObjectType() {
		// Load section type
		SectionType type = this.loadSectionType(truenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				section.addSectionObject("OBJECT"Double.class.getName());
			}
		});
		// Ensure correct object
		assertEquals("Incorrect number of objects", 1, type
		assertEquals("Incorrect object name""OBJECT"type
		assertEquals("Incorrect object type"Double.class.getName(), type
	}

Ensure can load net.officefloor.compile.section.SectionType with no inputs, outputs or objects.
	public void testEmptySectionType() {
		// Load section type
		SectionType type = this.loadSectionType(truenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				// Load nothing
			}
		});
		// Ensure empty section type
		assertEquals("Incorrect number of inputs", 0, type
		assertEquals("Incorrect number of outputs", 0, type
		assertEquals("Incorrect number of objects", 0, type
	}

Ensure can load net.officefloor.compile.section.SectionType with multiple inputs, outputs or objects.
		// Load section type
		SectionType type = this.loadSectionType(truenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				// Inputs
				section.addSectionInput("INPUT_A"Integer.class.getName());
				section.addSectionInput("INPUT_B"String.class.getName());
				section.addSectionInput("INPUT_C"null);
				// Outputs
				section.addSectionOutput("OUTPUT_A"Exception.class.getName(),
						true);
				section.addSectionOutput("OUTPUT_B"Double.class.getName(),
						false);
				section.addSectionOutput("OUTPUT_C"nullfalse);
				// Objects
				section.addSectionObject("OBJECT_A"Object.class.getName());
				section
						.addSectionObject("OBJECT_B"Connection.class
			}
		});
		// Ensure section type correct
		assertList(new String[] { "getSectionInputName""getParameterType" },
						"INPUT_A"nullInteger.class.getName(), nullnull),
				new SectionInputNodeImpl("INPUT_B"nullString.class
						.getName(), nullnull), new SectionInputNodeImpl(
						"INPUT_C"nullnullnull));
		assertList(new String[] { "getSectionOutputName""getArgumentType",
				"isEscalationOnly" }, type.getSectionOutputTypes(),
				new SectionOutputNodeImpl("OUTPUT_A",
						Exception.class.getName(), truenullnullnull),
				new SectionOutputNodeImpl("OUTPUT_B"Double.class.getName(),
						falsenullnullnull), new SectionOutputNodeImpl(
						"OUTPUT_C"nullfalsenullnullnull));
		assertList(new String[] { "getSectionObjectName""getObjectType" },
						"OBJECT_A"Object.class.getName(), nullnullnull),
				new SectionObjectNodeImpl("OBJECT_B"Connection.class
						.getName(), nullnullnull));
	}

	public void testLoadWorkType() {
		// Load the section type which loads the work type
		this.loadSectionType(truenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				// Load the work type
				PropertyList properties = context.createPropertyList();
				properties
				WorkType<?> workType = context.loadWorkType(
						ClassWorkSource.class.getName(), properties);
				// Ensure correct work type
				MockLoadWork.assertWorkType(workType);
			}
		});
	}

Ensure issue if fails to load the net.officefloor.compile.work.WorkType.
	public void testFailLoadingWorkType() {
		// Ensure issue in not loading work type
				."loadWorkType",
				"Missing property 'class.name' for WorkSource "
		this.record_issue("Failure loading WorkType from source "
		// Fail to load the work type
		this.loadSectionType(falsenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				// Do not specify class causing failure to load work type
				PropertyList properties = context.createPropertyList();
						properties);
				// Should not reach this point
				fail("Should not successfully load work type");
			}
		});
	}

	public void testLoadManagedObjectType() {
		// Load the section type which loads the managed object type
		this.loadSectionType(truenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				// Load the managed object type
				PropertyList properties = context.createPropertyList();
				properties.addProperty(
				ManagedObjectType<?> managedObjectType = context
								.getName(), properties);
				// Ensure correct managed object type
				MockLoadManagedObject
						.assertManagedObjectType(managedObjectType);
			}
		});
	}

Ensure issue if fails to load the net.officefloor.compile.managedobject.ManagedObjectType.
		// Ensure issue in not loading managed object type
				."loadManagedObjectType",
				"Missing property 'class.name'");
		this.record_issue("Failure loading ManagedObjectType from source "
		// Fail to load the managed object type
		this.loadSectionType(falsenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				// Do not specify class causing failure to load type
				PropertyList properties = context.createPropertyList();
						.getName(), properties);
				// Should not reach this point
				fail("Should not successfully load managed object type");
			}
		});
	}

	public void testLoadSubSectionType() {
		// Load the section type which loads the sub section type
		this.loadSectionType(truenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				// Load the sub section type
				PropertyList properties = context.createPropertyList();
				SectionType sectionType = context.loadSectionType(
						MockLoadSection.class.getName(), properties);
				// Ensure correct section type
				MockLoadSection.assertSectionType(sectionType);
			}
		});
	}

Ensure issue if fails to load the net.officefloor.compile.section.SectionType.
	public void testIssueLoadingSectionType() {
		// Ensure issue in not loading managed object type
		this..addIssue(."FailLocation"nullnull,
				"Missing property 'missing' for SectionSource "
		this.record_issue("Failure loading SectionType from source "
		// Fail to load the managed object type
		this.loadSectionType(falsenew Loader() {
			public void sourceSection(SectionDesigner section,
					SectionSourceContext contextthrows Exception {
				// Do not specify class causing failure to load type
				PropertyList properties = context.createPropertyList();
						"FailLocation"properties);
			}
		});
	}
	public static class FailSectionSource extends AbstractSectionSource {
		protected void loadSpecification(SpecificationContext context) {
			// No specification
		}
		public void sourceSection(SectionDesigner designer,
				SectionSourceContext contextthrows Exception {
			// Obtain property causing failure
			context.getProperty("missing");
			// Ensure failed
			TestCase.fail("Should not successfully obtain a missing property");
		}
	}

Records an issue.

Parameters:
issueDescription Description of the issue.
	private void record_issue(String issueDescription) {
				nullissueDescription);
	}

Records an issue.

Parameters:
issueDescription Description of the issue.
cause Cause of the issue.
	private void record_issue(String issueDescriptionThrowable cause) {
				nullissueDescriptioncause);
	}

Loads the net.officefloor.compile.section.SectionType within the input LoadSectionTypeTest.Loader.

Parameters:
isExpectedToLoad Flag indicating if expecting to load the net.officefloor.compile.section.SectionType.
loader LoadSectionTypeTest.Loader.
propertyNameValuePairs net.officefloor.compile.properties.Property name value pairs.
Returns:
Loaded net.officefloor.compile.section.SectionType.
	private SectionType loadSectionType(boolean isExpectedToLoad,
			Loader loaderString... propertyNameValuePairs) {
		// Replay mock objects
		// Create the compiler
		OfficeFloorCompiler compiler = OfficeFloorCompiler
		// Create the property list
		PropertyList propertyList = compiler.createPropertyList();
		for (int i = 0; i < propertyNameValuePairs.lengthi += 2) {
			String name = propertyNameValuePairs[i];
			String value = propertyNameValuePairs[i + 1];
			propertyList.addProperty(name).setValue(value);
		}
		// Create the section loader and load the section
		compiler.setCompilerIssues(this.);
		SectionLoader sectionLoader = compiler.getSectionLoader();
		SectionType sectionType = sectionLoader.loadSectionType(
				MockSectionSource.classpropertyList);
		// Verify the mock objects
		// Ensure if should be loaded
		if (isExpectedToLoad) {
			assertNotNull("Expected to load the section type"sectionType);
else {
			assertNull("Should not load the section type"sectionType);
		}
		// Return the section type
		return sectionType;
	}

	private interface Loader {

				throws Exception;
	}

	public static class MockSectionSource implements SectionSource {

		public static Loader loader;

Failure in instantiating an instance.
Resets the state for the next test.
		public static void reset() {
			 = null;
		}

Default constructor.
		public MockSectionSource() {
			if ( != null) {
			}
		}
		/*
		 * ================ SectionSource ======================================
		 */
			fail("Should not be invoked in obtaining section type");
			return null;
		}
		public void sourceSection(SectionDesigner sectionBuilder,
				SectionSourceContext contextthrows Exception {
			.sourceSection(sectionBuildercontext);
		}
	}
New to GrepCode? Check out our FAQ X