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.plugin.section.clazz;
 
 import java.util.Map;
 
Tests the ClassSectionSource.

Author(s):
Daniel Sagenschneider
 
 public class ClassSectionSourceTest extends OfficeFrameTestCase {

Expected net.officefloor.compile.spi.section.SectionTask instances by name.

This is loaded via creation of the net.officefloor.compile.spi.section.SectionDesigner.

 
 	private final Map<StringSectionTaskexpectedTasks = new HashMap<StringSectionTask>();

ClassSectionSource used for testing. Allows for overriding with child implementation.
 
 	private Class<? extends ClassSectionSourceclassSectionSource = ClassSectionSource.class;

Ensure correct specification.
 
 	public void testSpecification() {
 		// No specification as uses location for class
 		SectionLoaderUtil.validateSpecification(ClassSectionSource.class);
 	}

 
 	public void testInput() {
 		// Create the expected section
 				MockInputSection.class"doInput");
 		expected.addSectionInput("doInput"null);
 
 		// Validate section
 		SectionLoaderUtil.validateSection(expectedClassSectionSource.class,
 	}

Section with only an input.
 
 	public static class MockInputSection {
 		public void doInput() {
 		}
 	}

	public void testOutput() {
		// Create the expected section
				MockOutputSection.class"doInput");
		expected.addSectionInput("doInput"null);
		expected.addSectionOutput("doOutput"nullfalse);
		// Validate section
		SectionLoaderUtil.validateSection(expectedClassSectionSource.class,
	}

Section with an output.
	public static class MockOutputSection {
		@NextTask("doOutput")
		public void doInput() {
		}
	}

	public void testFlowInterface() {
		// Create the expected section
				MockFlowInterfaceSection.class"doInput");
		expected.addSectionInput("doInput"null);
		expected.addSectionOutput("doOutput"nullfalse);
		// Validate section
		SectionLoaderUtil.validateSection(expectedClassSectionSource.class,
	}

	public static interface MockFlowInterface {
		void doOutput();
	}

	public static class MockFlowInterfaceSection {
		public void doInput(MockFlowInterface flows) {
		}
	}

Ensure can provide net.officefloor.compile.spi.section.SectionOutput for escalation.
	public void testEscalation() {
		// Create the expected section
				MockEscalationSection.class"doInput");
		expected.addSectionInput("doInput"null);
				SQLException.class.getName(), true);
		// Validate section
		SectionLoaderUtil.validateSection(expectedClassSectionSource.class,
	}

Section with an escalation.
	public static class MockEscalationSection {
		public void doInput() throws SQLException {
		}
	}

Ensure can provide parameter and argument types.
	public void testParameterArgument() {
		// Create the expected section
				MockParameterArgumentSection.class"doInput");
		this..get("doInput").getTaskObject("String");
		expected.addSectionInput("doInput"String.class.getName());
		expected.addSectionOutput("doOutput"Integer.class.getName(), false);
		// Validate section
		SectionLoaderUtil.validateSection(expectedClassSectionSource.class,
	}

Section with parameter and arguments.
	public static class MockParameterArgumentSection {
		@NextTask("doOutput")
		public Integer doInput(@Parameter String parameter) {
			return null;
		}
	}

	public void testObject() {
		// Create the expected section
				MockObjectSection.class"doInput");
		this..get("doInput").getTaskObject("Connection");
		expected.addSectionInput("doInput"null);
		// Validate section
		SectionLoaderUtil.validateSection(expectedClassSectionSource.class,
	}

Section with object.
	public static class MockObjectSection {
		public void doInput(Connection connection) {
		}
	}

	public void testDependency() {
		// Create the expected section
				MockDependencySection.class"doInput");
		expected.addSectionInput("doInput"null);
		// Validate section
		SectionLoaderUtil.validateSection(expectedClassSectionSource.class,
	}

	public static class MockDependencySection {
		public void doInput() {
		}
	}

Ensure able to handle changing the net.officefloor.frame.api.execute.Task name.
	public void testChangeTaskName() {
		// Override to change class name
		// Create the expected type
				MockInputSection.class"newName");
		expected.addSectionInput("newName"null);
		// Validate section
		SectionLoaderUtil.validateSection(expected,
	}

Section with only task.
	public static class MockChangeTaskNameSection {
		public void oldName() {
		}
	}

	public static class MockChangeTaskNameClassSectionSource extends
		protected String getTaskName(TaskType<?, ?, ?> taskType) {
			String taskTypeName = taskType.getTaskName();
			return ("oldName".equals(taskTypeName) ? "newName" : taskTypeName);
		}
	}

Ensure able to handle changing the net.officefloor.frame.api.execute.Task name along with keeping links working.
		// Override to change class name
		// Create the expected type
				MockInputSection.class"doInput""newName""finished");
		SectionTask doInput = this..get("doInput");
		doInput.getTaskObject("ReturnValue");
		doInput.getTaskObject("Boolean");
		SectionTask newName = this..get("newName");
		newName.getTaskObject("ReturnValue");
		newName.getTaskObject("String");
		newName.getTaskObject("Connection");
		SectionTask finished = this..get("finished");
		finished.getTaskObject("ReturnValue");
		// Inputs
		expected.addSectionInput("doInput"Boolean.class.getName());
		expected.addSectionInput("newName"String.class.getName());
		expected.addSectionInput("finished"null);
		// Outputs
		expected.addSectionOutput("externalFlow"nullfalse);
		expected.addSectionOutput("java.sql.SQLException",
				SQLException.class.getName(), true);
		// Objects
		// Validate section
		SectionLoaderUtil.validateSection(expected,
	}

Ensure able to handle changing the net.officefloor.frame.api.execute.Task name and continue to execute.
			throws Exception {
		final Connection connection = this.createMock(Connection.class);
		final ReturnValue returnValue = new ReturnValue();
		// Managed object internal, so must run to test
		AutoWireSection section = source.addSection("test",
		source.addObject(returnValueReturnValue.class);
		source.addObject(connectionConnection.class);
		source.link(section"externalFlow"section"finished");
		// Open OfficeFloor
		AutoWireOfficeFloor officeFloor = source.openOfficeFloor();
		try {
			// Run invoking flow
			officeFloor.invokeTask("test.WORK""doInput"new Boolean(true));
			assertEquals("Incorrect value on invoking flow",
					"doInput -> oldName(Flow) -> finished"returnValue.value);
			// Run using next task
			officeFloor.invokeTask("test.WORK""doInput"null);
			assertEquals("Incorrect value on next task",
					"doInput -> oldName(null) -> finished"returnValue.value);
finally {
			// Ensure closed
			officeFloor.closeOfficeFloor();
		}
	}

Mock net.officefloor.plugin.work.clazz.FlowInterface for linking to old method name even after net.officefloor.frame.api.execute.Task name change.
	public static interface MockChangeNameFlows {
		void oldName(String parameter);
	}

Section with only task.
	public static class MockChangeTaskNameWithLinksSection {
		// even with name change, should still link by method name
		@NextTask("oldName")
		public void doInput(MockChangeNameFlows flowReturnValue returnValue,
				@Parameter Boolean isInvokeFlow) {
			// Flag invoked
			returnValue.value = "doInput";
			// Determine if invoke flow
			if (isInvokeFlow == null ? false : isInvokeFlow.booleanValue()) {
				// Invoke the flow
				flow.oldName("Flow");
			}
		}
		@NextTask("externalFlow")
		public void oldName(ReturnValue returnValue,
				@Parameter String parameterConnection connection)
				throws SQLException {
			// Indicate invoked
			returnValue.value += " -> oldName(" + parameter + ")";
		}
		public void finished(ReturnValue returnValue) {
			returnValue.value += " -> finished";
		}
	}

Ensure can configure a ManagedObject.
	public void testManagedObject() throws Exception {
		// Managed object internal, so must run to test
		source.addSection("test"ClassSectionSource.class,
		// Open the OfficeFloor
		AutoWireOfficeFloor officeFloor = source.openOfficeFloor();
		try {
			// Run to ensure obtained message
			ReturnValue returnValue = new ReturnValue();
			officeFloor.invokeTask("test.WORK""doInput"returnValue);
			assertEquals("Incorrect value from managed object""test",
					returnValue.value);
finally {
			// Ensure closed
			officeFloor.closeOfficeFloor();
		}
	}

Allows returning a value from the net.officefloor.frame.api.manage.OfficeFloor.
	public static class ReturnValue {
		public String value = null;
	}

	public static class MockManagedObject {
		public String getMessage() {
			return "test";
		}
	}

Section with ManagedObject.
	public static class MockManagedObjectSection {
		@ManagedObject(source = ClassManagedObjectSource.class, properties = { @Property(name = ., valueClass = MockManagedObject.class) })
		public void doInput(@Parameter ReturnValue returnValue) {
			returnValue.value = this..getMessage();
		}
	}

Ensure can configure a ManagedObject with a dependency.
	public void testManagedObjectWithDependency() throws Exception {
		// Managed object internal, so must run to test
		source.addSection("test"ClassSectionSource.class,
		// Open OfficeFloor
		AutoWireOfficeFloor officeFloor = source.openOfficeFloor();
		try {
			// Run to ensure obtained message
			ReturnValue returnValue = new ReturnValue();
			officeFloor.invokeTask("test.WORK""doInput"returnValue);
			assertEquals("Incorrect value from managed object""test",
					returnValue.value);
finally {
			// Ensure closed
			officeFloor.closeOfficeFloor();
		}
	}

Mock ManagedObject with dependency.
	public static class MockManagedObjectWithDependency {
		public String getMessage() {
			return this..getMessage();
		}
	}

Section with ManagedObject with dependency.
		@ManagedObject(source = ClassManagedObjectSource.class, properties = { @Property(name = ., valueClass = MockManagedObject.class) })
		public void doInput(@Parameter ReturnValue returnValue) {
			returnValue.value = this..getMessage();
		}
	}

Ensure can internally invoke flows within the section.
	public void testInternalFlow() throws Exception {
		// Triggering flows, so must run to test
		source.addSection("test"ClassSectionSource.class,
		// Open OfficeFloor
		AutoWireOfficeFloor officeFloor = source.openOfficeFloor();
		try {
			// Run to ensure obtained message
			ReturnValue returnValue = new ReturnValue();
			officeFloor.invokeTask("test.WORK""doFirst"returnValue);
			assertEquals("Incorrect value from flow""one-two-three",
					returnValue.value);
finally {
			// Ensure closed
			officeFloor.closeOfficeFloor();
		}
	}

	public static interface MockInternalFlows {
		void doThird(ReturnValue returnValue);
	}

Section to undertake internal flows.
	public static class MockInternalFlowSection {
		@NextTask("doSecond")
		public ReturnValue doFirst(@Parameter ReturnValue returnValue) {
			returnValue.value = "one";
			return returnValue;
		}
		public void doSecond(@Parameter ReturnValue returnValue,
			returnValue.value = returnValue.value + "-two";
			flows.doThird(returnValue);
		}
		public void doThird(@Parameter ReturnValue returnValue) {
			returnValue.value = returnValue.value + "-three";
		}
	}

Ensure able to handle an escalation internally.
	public void testEscalationHandling() throws Exception {
		// Triggering flows, so must run to test
		ReturnValue returnValue = new ReturnValue();
		source.addObject(returnValueReturnValue.class);
		source.addSection("test"ClassSectionSource.class,
		// Open OfficeFloor
		AutoWireOfficeFloor officeFloor = source.openOfficeFloor();
		try {
			// Run to ensure obtained message
			officeFloor.invokeTask("test.WORK""triggerEscalation"null);
			assertEquals("Incorrect value from handling escalation""test",
					returnValue.value);
finally {
			// Ensure closed
			officeFloor.closeOfficeFloor();
		}
	}

Section to undertake handling of escalation.
	public static class MockEscalationHandlingSection {
		public void triggerEscalation() throws IOException {
			throw new IOException("test");
		}
		public void handleEscalation(@Parameter IOException escalation,
				ReturnValue value) {
			value.value = escalation.getMessage();
		}
	}

	public void testSubSection() throws Exception {
		// Triggering sub section, so must run to test
		source.addSection("test"ClassSectionSource.class,
		ReturnValue returnValue = new ReturnValue();
		source.addObject(returnValueReturnValue.class);
		// Open OfficeFloor
		AutoWireOfficeFloor officeFloor = source.openOfficeFloor();
		try {
			// Run to ensure obtained message
			officeFloor.invokeTask("test.WORK""doFirst"null);
			assertEquals("Incorrect value from sub section""sub section",
					returnValue.value);
finally {
			// Ensure closed
			officeFloor.closeOfficeFloor();
		}
	}

Mock SectionInterface for invoking a SubSection.
	@SectionInterface(source = ClassSectionSource.class, locationClass = MockSubSection.class, outputs = { @FlowLink(name = "output", method = "doLast") })
	public static interface MockSectionInterface {
	}

Section to invoke sub section.
	public static class MockSubSection {
		@NextTask("output")
		public void doSubSectionInput(ReturnValue returnValue) {
			returnValue.value = "sub";
		}
	}

Mock section containing the net.officefloor.compile.spi.section.SubSection.
	public static class MockInvokeSubSection {
		public void doFirst(MockSectionInterface subSection) {
			subSection.doSubSectionInput();
		}
		public void doLast(ReturnValue returnValue) {
			returnValue.value = returnValue.value + " section";
		}
	}

Creates the expected net.officefloor.compile.spi.section.SectionDesigner with pre-populated details.

Parameters:
sectionClass Section class.
taskNames Names of the net.officefloor.frame.api.execute.Task instances.
Returns:
net.officefloor.compile.spi.section.SectionDesigner.
	private SectionDesigner createSectionDesigner(Class<?> sectionClass,
			String... taskNames) {
		// Create the section designer
		SectionDesigner designer = SectionLoaderUtil
		SectionManagedObjectSource managedObjectSource = designer
		managedObjectSource.addProperty(
				sectionClass.getName());
		SectionWork work = designer.addSectionWork("WORK",
		for (String taskName : taskNames) {
			// Create the section task with the section object
			SectionTask task = work.addSectionTask(taskNametaskName);
			task.getTaskObject("OBJECT");
			// Register the expected task
			this..put(taskNametask);
		}
		// Return the section designer
		return designer;
	}
New to GrepCode? Check out our FAQ X