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.model.test.changes;
 
 
Abstract operations junit.framework.TestCase.

Author(s):
Daniel Sagenschneider
 
 public abstract class AbstractChangesTestCase<M extends Model, O> extends
net.officefloor.model.Model loaded for testing.
 
 	protected M model;

Operations.
 
 	protected O operations;

Flags if there is a specific setup file per test.
 
 	private boolean isSpecificSetupFilePerTest;

Initiate.
 
 		this. = false;
 	}

Initiate.

Parameters:
isSpecificSetupFilePerTest Flags if there is a specific setup file per test.
 
 	public AbstractChangesTestCase(boolean isSpecificSetupFilePerTest) {
 		this. = isSpecificSetupFilePerTest;
 	}
 
 	protected void setUp() throws Exception {
 
 		// Retrieve the setup model
 		String setupTestName = this.getSetupTestName();
 		this. = this.retrieveModel(setupTestNamenull);
 
 		// Create the model operations
 		this. = this.createModelOperations(this.);
 	}

 
 	protected abstract M retrieveModel(ConfigurationItem configurationItem)
 			throws Exception;

Creates the net.officefloor.model.Model operations.

Parameters:
model net.officefloor.model.Model to create operations for.
Returns:
net.officefloor.model.Model operations.
	protected abstract O createModelOperations(M model);

Obtains the extension for the file containing the net.officefloor.model.Model.

Returns:
Extension for the file containing the net.officefloor.model.Model.
	protected abstract String getModelFileExtension();

Allows particular tests of a junit.framework.TestCase to override using the default setup net.officefloor.model.repository.ConfigurationItem and use the specific test net.officefloor.model.repository.ConfigurationItem.
	protected void useTestSetupModel() {
		try {
			// Flag to use test specific setup model
			// re-setup the test
			this.setUp();
catch (Exception ex) {
			// Fail on failure (stops have to throw exception in tests)
			StringWriter msg = new StringWriter();
			fail("Failed to useTestSetupModel");
		}
	}

Obtains the test name for the setup net.officefloor.model.repository.ConfigurationItem.

Returns:
Test name for the setup net.officefloor.model.repository.ConfigurationItem.
	private String getSetupTestName() {
		return (this. ? this.getName() + "_" : "")
"setup";
	}

Asserts the net.officefloor.model.change.Change is correct.

Parameters:
change net.officefloor.model.change.Change to verify.
expectedTarget Expected target.
expectedChangeDescription Expected description of the net.officefloor.model.change.Change.
expectCanApply Expected if can apply the net.officefloor.model.change.Change. Should it be able to be applied, both the net.officefloor.model.change.Change.apply() and net.officefloor.model.change.Change.revert() will be also tested.
expectedConflictDescriptions Expected descriptions for the net.officefloor.model.change.Conflict instances on the net.officefloor.model.change.Change.
	protected <T> void assertChange(Change<T> change, T expectedTarget,
			String expectedChangeDescriptionboolean expectCanApply,
			String... expectedConflictDescriptions) {
		// Ensure details of change correct
		if (expectedTarget != null) {
			assertEquals("Incorrect target"expectedTargetchange.getTarget());
		}
		assertEquals("Incorrect change description"expectedChangeDescription,
		assertEquals("Incorrect number of conflicts",
				expectedConflictDescriptions.length,
				change.getConflicts().length);
		for (int i = 0; i < expectedConflictDescriptions.lengthi++) {
			assertEquals("Incorrect description for conflict " + i,
					expectedConflictDescriptions[i],
		}
		// Validate changes if can apply change
		if (expectCanApply) {
			// Should be no change until change is applied
			// Apply the change and validate results
			change.apply();
			// Revert the change and validate reverted back to setup
			change.revert();
			// Apply again for 'redo' functionality
			change.apply();
			// Revert change to have model in setup state for any further
			// testing
			change.revert();
		}
	}

Assets all the net.officefloor.model.change.Change instances result in a correct change.

Parameters:
changes net.officefloor.model.change.Change instances to verify.
	protected void assertChanges(Change<?>... changes) {
		// Apply the changes and verify the changes
		for (int i = 0; i < changes.lengthi++) {
			changes[i].apply();
		}
		// Revert the changes and verify reverted
		for (int i = (changes.length - 1); i >= 0; i--) {
			changes[i].revert();
		}
	}

Validates the net.officefloor.model.Model against the default net.officefloor.model.Model file for the test.
	protected void validateModel() {
		this.validateModel(null);
	}

Validates the net.officefloor.model.Model against the specific net.officefloor.model.Model file for the test.

Parameters:
specific Indicates the specific net.officefloor.model.Model file for the test.
	protected void validateModel(String specific) {
		this.validateModel(this.getName(), specific);
	}

Validates the net.officefloor.model.Model against the net.officefloor.model.Model setup for testing.

This is useful to test the revert functionality of a net.officefloor.model.change.Change.

	protected void validateAsSetupModel() {
		String setupTestName = this.getSetupTestName();
		this.validateModel(setupTestNamenull);
	}

Validates the net.officefloor.model.Model.

Parameters:
testName Name of the test.
specific Specific name for the test. May be null for the default net.officefloor.model.Model for the test.
	private void validateModel(String testNameString specific) {
		// Obtain the model
compareModel = this.retrieveModel(testNamespecific);
		try {
			// Ensure the models are the same
			assertGraph(compareModelthis.,
catch (Exception ex) {
			// Fail on failure (stops have to throw exception in tests)
			StringWriter msg = new StringWriter();
			fail("Failed to validate graph");
		}
	}

Retrieves the net.officefloor.model.Model for the test.

Parameters:
testName Name of the test.
specific Specific name for the test. May be null for the default net.officefloor.model.Model for the test.
Returns:
net.officefloor.model.Model.
	private M retrieveModel(String testNameString specific) {
		// Move the 'Test' to start of test case name
		String testCasePath = this.getClass().getSimpleName();
		testCasePath = this.getClass().getPackage().getName().replace('.''/')
"/Test"
testCasePath.substring(0,
						(testCasePath.length() - "Test".length()));
		// Construct the path to the model
		String testPath = testCasePath.replace('.''/') + "/" + testName;
		String modelPath = testPath + (specific == null ? "" : "/" + specific)
		try {
			// Obtain the configuration item to the model
			assertNotNull("Can not find model configuration: " + modelPath,
					item);
			// Return the retrieved model
			return this.retrieveModel(item);
catch (Exception ex) {
			// Fail on failure (stops have to throw exception in tests)
			StringWriter msg = new StringWriter();
			fail("Failed to retrieveModel: " + modelPath + "\n"
msg.toString());
			return null// fail will throw
		}
	}
New to GrepCode? Check out our FAQ X