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.office;
 
 
 
 import  net.officefloor.compile.impl.administrator.MockLoadAdministrator;
Tests loading the OfficeType.

Author(s):
Daniel Sagenschneider
 
 public class LoadOfficeTypeTest extends AbstractStructureTestCase {

Location of the Office.
 
 	private final String OFFICE_LOCATION = "OFFICE_LOCATION";
 
 	/*
 	 * (non-Javadoc)
 	 * 
 	 * @see junit.framework.TestCase#setUp()
 	 */
 	protected void setUp() throws Exception {
 		super.setUp();
 		MockOfficeSource.reset();
 	}

Ensure issue if fail to instantiate the OfficeSource.
 
 	public void testFailInstantiate() {
 
 		final RuntimeException failure = new RuntimeException(
 				"instantiate failure");
 
 		// Record failure to instantiate
 				"Failed to instantiate " + MockOfficeSource.class.getName()
 						+ " by default constructor"failure);
 
 		// Attempt to obtain specification
 		this.loadOfficeType(falsenull);
 	}

Ensure obtain the correct Office location.
 
 	public void testOfficeLocation() {
		this.loadOfficeType(truenew Loader() {
			public void sourceOffice(OfficeArchitect office,
					OfficeSourceContext contextthrows Exception {
				assertEquals("Incorrect office location",
			}
		});
	}

Ensures issue if fails to obtain the 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.loadOfficeType(falsenew Loader() {
			public void sourceOffice(OfficeArchitect office,
					OfficeSourceContext contextthrows Exception {
				context.getConfiguration(location);
			}
		});
	}

Ensure able to obtain a ConfigurationItem.
	public void testGetConfigurationItem() throws Exception {
		final String location = "LOCATION";
		final ConfigurationItem item = this.createMock(ConfigurationItem.class);
		// Record obtaining the configuration item
		// Obtain the configuration item
		this.loadOfficeType(truenew Loader() {
			public void sourceOffice(OfficeArchitect office,
					OfficeSourceContext contextthrows Exception {
				assertEquals("Incorrect configuation item"item,
						context.getConfiguration(location));
			}
		});
	}

Ensure issue if missing Property.
	public void testMissingProperty() {
		// Record missing property
		this.record_issue("Missing property 'missing' for OfficeSource "
		// Attempt to load office type
		this.loadOfficeType(falsenew Loader() {
			public void sourceOffice(OfficeArchitect office,
					OfficeSourceContext contextthrows Exception {
				context.getProperty("missing");
			}
		});
	}

Ensure able to get properties.
	public void testGetProperties() {
		// Attempt to load office type
		this.loadOfficeType(truenew Loader() {
			public void sourceOffice(OfficeArchitect office,
					OfficeSourceContext 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 ClassLoader.
	public void testGetClassLoader() {
		// Attempt to load office type
		this.loadOfficeType(truenew Loader() {
			public void sourceOffice(OfficeArchitect office,
					OfficeSourceContext contextthrows Exception {
				assertEquals("Incorrect class loader",
						context.getClassLoader());
			}
		});
	}

Ensure issue if fails to source the OfficeType.
	public void testFailSourceOfficeType() {
				"Fail source office type");
		// Record failure to source the office type
				"Failed to source OfficeType definition from OfficeSource "
MockOfficeSource.class.getName(), failure);
		// Attempt to load office type
		this.loadOfficeType(falsenew Loader() {
			public void sourceOffice(OfficeArchitect office,
					OfficeSourceContext contextthrows Exception {
				throw failure;
			}
		});
	}

Ensure issue if null OfficeManagedObjectType name.
	public void testNullManagedObjectName() {
		// Record null managed object name
		this.record_issue("Null name for managed object 0");
		// Attempt to load office type
		this.loadOfficeType(falsenew Loader() {
			public void sourceOffice(OfficeArchitect office,
					OfficeSourceContext contextthrows Exception {
				office.addOfficeObject(nullConnection.class.getName());
			}
		});
	}

Ensure issue if null OfficeManagedObjectType required type.
	public void testNullManagedObjectType() {
		// Record null required type
		this.record_issue("Null type for managed object 0 (name=MO)");
		// Attempt to load office type
		this.loadOfficeType(falsenew Loader() {
			public void sourceOffice(OfficeArchitect office,
					OfficeSourceContext contextthrows Exception {
				office.addOfficeObject("MO"null);
			}
		});
	}

Ensure get OfficeManagedObjectType not being administered (no extension interfaces).
	public void testManagedObjectType() {
		// Load office type with office floor managed object
		OfficeType officeType = this.loadOfficeType(truenew Loader() {
			public void sourceOffice(OfficeArchitect office,
					OfficeSourceContext contextthrows Exception {
				final String MANAGED_OBJECT_NAME = "MO";
				// Add the office object
				OfficeObject officeObject = office.addOfficeObject(
						MANAGED_OBJECT_NAMEConnection.class.getName());
				// Ensure the office object is correct
				assertEquals("Incorrect office object name",
						MANAGED_OBJECT_NAMEofficeObject.getOfficeObjectName());
				assertEquals("Incorrect dependent name"MANAGED_OBJECT_NAME,
						"Always no dependencies for Office object in sourcing Office",
						0, officeObject.getObjectDependencies().length);
				assertEquals("Incorrect administerable name",
						MANAGED_OBJECT_NAME,
			}
		});
		// Validate type
		assertEquals("Incorrect number of managed object types", 1,
				officeType.getOfficeManagedObjectTypes().length);
		OfficeManagedObjectType moType = officeType
		assertEquals("Incorrect name""MO",
		assertEquals("Incorrect type"Connection.class.getName(),
				moType.getObjectType());
		assertEquals("Should be no required extension interfaces", 0,
				moType.getExtensionInterfaces().length);
	}

Ensure get OfficeManagedObjectType being administered (has extension interfaces).
		// Load office type with administered office floor managed object
		OfficeType officeType = this.loadOfficeType(truenew Loader() {
			public void sourceOffice(OfficeArchitect office,
					OfficeSourceContext contextthrows Exception {
				OfficeObject mo = office.addOfficeObject("MO",
						.addAdministrator(office"ADMIN"XAResource.class,
			}
		});
		// Validate type
		assertEquals("Incorrect number of managed object types", 1,
				officeType.getOfficeManagedObjectTypes().length);
		OfficeManagedObjectType moType = officeType
		assertEquals("Incorrect name""MO",
		assertEquals("Incorrect type"Connection.class.getName(),
				moType.getObjectType());
		assertEquals("Incorrect number of extension interfaces", 1,
				moType.getExtensionInterfaces().length);
		String extensionInterface = moType.getExtensionInterfaces()[0];
		assertEquals("Incorrect extension interface",
				XAResource.class.getName(), extensionInterface);
	}

Ensure issue if null OfficeTeamType name.
	public void testNullTeamName() {
		// Record null required type
		this.record_issue("Null name for team 0");
		// Attempt to load office type
		this.loadOfficeType(falsenew Loader() {
			public void sourceOffice(OfficeArchitect office,
					OfficeSourceContext contextthrows Exception {
				office.addOfficeTeam(null);
			}
		});
	}

Ensure obtain the OfficeTeamType.
	public void testTeamType() {
		// Load office type
		OfficeType officeType = this.loadOfficeType(truenew Loader() {
			public void sourceOffice(OfficeArchitect office,
					OfficeSourceContext contextthrows Exception {
				office.addOfficeTeam("TEAM");
			}
		});
		// Validate type
		assertEquals("Incorrect number of teams", 1,
				officeType.getOfficeTeamTypes().length);
		OfficeTeamType team = officeType.getOfficeTeamTypes()[0];
		assertEquals("Incorrect team name""TEAM"team.getOfficeTeamName());
	}

Ensure obtain the OfficeInputType.
	public void testInputType() {
		// Load office type
		OfficeType officeType = this.loadOfficeType(truenew Loader() {
			public void sourceOffice(OfficeArchitect office,
					OfficeSourceContext contextthrows Exception {
				// Add section with an input
				LoadOfficeTypeTest.this.addSection(office"SECTION",
						new SectionMaker() {
							public void make(SectionMakerContext context) {
								context.getBuilder().addSectionInput("INPUT",
										String.class.getName());
							}
						});
			}
		});
		// Validate type
		assertEquals("Incorrect number of inputs", 1,
				officeType.getOfficeInputTypes().length);
		OfficeInputType input = officeType.getOfficeInputTypes()[0];
		assertEquals("Incorrect section name""SECTION",
		assertEquals("Incorrect input name""INPUT",
		assertEquals("Incorrect parameter type"String.class.getName(),
	}

Ensure can obtain the ManagedObjectType.
	public void testLoadManagedObjectType() {
		this.loadOfficeType(truenew Loader() {
			public void sourceOffice(OfficeArchitect architect,
					OfficeSourceContext contextthrows Exception {
				// Load the managed object type
				PropertyList properties = context.createPropertyList();
				properties.addProperty(
				ManagedObjectType<?> managedObjectType = context
								properties);
				// Ensure correct managed object type
				MockLoadManagedObject
						.assertManagedObjectType(managedObjectType);
			}
		});
	}

Ensure issue if fails to load the 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.loadOfficeType(falsenew Loader() {
			public void sourceOffice(OfficeArchitect architect,
					OfficeSourceContext contextthrows Exception {
				// Do not specify class causing failure to load type
				PropertyList properties = context.createPropertyList();
						ClassManagedObjectSource.class.getName(), properties);
				// Should not reach this point
				fail("Should not successfully load managed object type");
			}
		});
	}

Ensure can obtain the AdministratorType.
	public void testLoadAdministratorType() {
		this.loadOfficeType(truenew Loader() {
			public void sourceOffice(OfficeArchitect architect,
					OfficeSourceContext contextthrows Exception {
				// Load the managed object type
				PropertyList properties = context.createPropertyList();
				properties.addProperty(
						.setValue(MockLoadAdministrator.class.getName());
				AdministratorType<?, ?> administratorType = context
								properties);
				// Ensure correct administrator type
				MockLoadAdministrator
						.assertAdministratorType(administratorType);
			}
		});
	}

Ensure issue if fails to load the AdministratorType.
		// Ensure issue in not loading managed object type
				."loadAdministratorType",
				"Missing property 'class.name'");
		this.record_issue("Failure loading AdministratorType from source "
		// Fail to load the administrator type
		this.loadOfficeType(falsenew Loader() {
			public void sourceOffice(OfficeArchitect architect,
					OfficeSourceContext contextthrows Exception {
				// Do not specify class causing failure to load type
				PropertyList properties = context.createPropertyList();
						ClassAdministratorSource.class.getName(), properties);
				// Should not reach this point
				fail("Should not successfully load administrator type");
			}
		});
	}

Records an issue.

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

Records an issue.

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

Loads the OfficeType within the input Loader.

Parameters:
isExpectedToLoad Flag indicating if expecting to load the OfficeType.
loader Loader.
propertyNameValuePairs Property name value pairs.
Returns:
Loaded OfficeType.
	private OfficeType loadOfficeType(boolean isExpectedToLoadLoader loader,
			String... propertyNameValuePairs) {
		// Replay mock objects
		// Create the property list
		PropertyList propertyList = new PropertyListImpl();
		for (int i = 0; i < propertyNameValuePairs.lengthi += 2) {
			String name = propertyNameValuePairs[i];
			String value = propertyNameValuePairs[i + 1];
			propertyList.addProperty(name).setValue(value);
		}
		// Create the office loader and load the office
		OfficeFloorCompiler compiler = OfficeFloorCompiler
		compiler.setCompilerIssues(this.);
		OfficeLoader officeLoader = compiler.getOfficeLoader();
		OfficeType officeType = officeLoader.loadOfficeType(
				MockOfficeSource.classpropertyList);
		// Verify the mock objects
		// Ensure if should be loaded
		if (isExpectedToLoad) {
			assertNotNull("Expected to load the office type"officeType);
else {
			assertNull("Should not load the office type"officeType);
		}
		// Return the office type
		return officeType;
	}

Implemented to load the OfficeType.
	private interface Loader {

Implemented to load the OfficeType.

Parameters:
office OfficeArchitect.
context OfficeSourceContext.
Throws:
Exception If fails to source OfficeType.
				throws Exception;
	}

Mock OfficeSource for testing.
	public static class MockOfficeSource implements OfficeSource {

Loader to load the OfficeType.
		public static Loader loader;

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

Default constructor.
		public MockOfficeSource() {
			if ( != null) {
			}
		}
		/*
		 * ================ OfficeSource ======================================
		 */
			fail("Should not be invoked in obtaining office type");
			return null;
		}
		public void sourceOffice(OfficeArchitect officeArchitect,
				OfficeSourceContext contextthrows Exception {
			.sourceOffice(officeArchitectcontext);
		}
	}
New to GrepCode? Check out our FAQ X