/* Copyright 2010 - 2014 by Brian Uri!
This file is part of DDMSence.
This library is free software; you can redistribute it and/or modify
it under the terms of version 3.0 of the GNU Lesser General Public
License as published by the Free Software Foundation.
This library 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with DDMSence. If not, see <http://www.gnu.org/licenses/>.
You can contact the author at ddmsence@urizone.net. The DDMSence
home page is located at http://ddmsence.urizone.net/
*/
package buri.ddmsence.ddms.resource;
import static org.junit.Assert.*;
import nu.xom.Element;
import org.junit.Test;
import buri.ddmsence.AbstractBaseTestCase;
import buri.ddmsence.ddms.InvalidDDMSException;
import buri.ddmsence.ddms.OutputFormat;
import buri.ddmsence.ddms.security.ism.SecurityAttributesTest;
import buri.ddmsence.util.DDMSVersion;
import buri.ddmsence.util.PropertyReader;
import buri.ddmsence.util.Util;
/**
* <p> Tests related to ddms:applicationSoftware elements </p>
*
* <p> Because a ddms:applicationSoftware is a local component, we cannot load a valid document from a unit test data
* file. We have to build the well-formed Element ourselves. </p>
*
* @author Brian Uri!
* @since 2.0.0
*/
public class ApplicationSoftwareTest extends AbstractBaseTestCase {
private static final String TEST_VALUE = "IRM Generator 2L-9";
/**
* Constructor
*/
public ApplicationSoftwareTest() {
super(null);
removeSupportedVersions("2.0 3.0 3.1");
}
/**
* Returns a fixture object for testing.
*/
public static Element getFixtureElement() {
try {
DDMSVersion version = DDMSVersion.getCurrentVersion();
Element element = Util.buildDDMSElement(ApplicationSoftware.getName(version), TEST_VALUE);
element.addNamespaceDeclaration(PropertyReader.getPrefix("ddms"), version.getNamespace());
element.addNamespaceDeclaration(PropertyReader.getPrefix("ism"), version.getIsmNamespace());
SecurityAttributesTest.getFixture().addTo(element);
return (element);
}
catch (InvalidDDMSException e) {
fail("Could not create fixture: " + e.getMessage());
}
return (null);
}
/**
* Returns a fixture object for testing.
*/
public static ApplicationSoftware getFixture() {
try {
return (new ApplicationSoftware(getFixtureElement()));
}
catch (InvalidDDMSException e) {
fail("Could not create fixture: " + e.getMessage());
}
return (null);
}
/**
* Attempts to build a component from a XOM element.
*
* @param element the element to build from
* @param message an expected error message. If empty, the constructor is expected to succeed.
*
* @return a valid object
*/
private ApplicationSoftware getInstance(Element element, String message) {
boolean expectFailure = !Util.isEmpty(message);
ApplicationSoftware component = null;
try {
component = new ApplicationSoftware(element);
checkConstructorSuccess(expectFailure);
}
catch (InvalidDDMSException e) {
checkConstructorFailure(expectFailure, e);
expectMessage(e, message);
}
return (component);
}
/**
* Helper method to create an object which is expected to be valid.
*
* @param builder the builder to commit
* @param message an expected error message. If empty, the constructor is expected to succeed.
*
* @return a valid object
*/
private ApplicationSoftware getInstance(ApplicationSoftware.Builder builder, String message) {
boolean expectFailure = !Util.isEmpty(message);
ApplicationSoftware component = null;
try {
component = builder.commit();
checkConstructorSuccess(expectFailure);
}
catch (InvalidDDMSException e) {
checkConstructorFailure(expectFailure, e);
expectMessage(e, message);
}
return (component);
}
/**
* Returns a builder, pre-populated with base data from the XML sample.
*
* This builder can then be modified to test various conditions.
*/
private ApplicationSoftware.Builder getBaseBuilder() {
ApplicationSoftware component = getInstance(getFixtureElement(), SUCCESS);
return (new ApplicationSoftware.Builder(component));
}
/**
* Returns the expected output for the test instance of this component
*/
private String getExpectedHTMLTextOutput(OutputFormat format) throws InvalidDDMSException {
Util.requireHTMLText(format);
StringBuffer text = new StringBuffer();
text.append(buildHTMLTextOutput(format, "applicationSoftware", TEST_VALUE));
text.append(buildHTMLTextOutput(format, "applicationSoftware.classification", "U"));
text.append(buildHTMLTextOutput(format, "applicationSoftware.ownerProducer", "USA"));
return (text.toString());
}
/**
* Returns the expected JSON output for this unit test
*/
private String getExpectedJSONOutput() {
StringBuffer json = new StringBuffer();
json.append("{\"applicationSoftware\":\"IRM Generator 2L-9\",");
json.append(SecurityAttributesTest.getBasicJSON()).append("}");
return (json.toString());
}
/**
* Returns the expected XML output for this unit test
*/
private String getExpectedXMLOutput() {
StringBuffer xml = new StringBuffer();
xml.append("<ddms:applicationSoftware ").append(getXmlnsDDMS()).append(" ").append(getXmlnsISM()).append(
" ism:classification=\"U\" ism:ownerProducer=\"USA\">");
xml.append(TEST_VALUE).append("</ddms:applicationSoftware>");
return (xml.toString());
}
@Test
public void testNameAndNamespace() throws InvalidDDMSException {
for (String sVersion : getSupportedVersions()) {
DDMSVersion version = DDMSVersion.setCurrentVersion(sVersion);
assertNameAndNamespace(getInstance(getFixtureElement(), SUCCESS), DEFAULT_DDMS_PREFIX,
ApplicationSoftware.getName(version));
getInstance(getWrongNameElementFixture(), WRONG_NAME_MESSAGE);
}
}
@Test
public void testConstructors() {
for (String sVersion : getSupportedVersions()) {
DDMSVersion.setCurrentVersion(sVersion);
// Element-based
getInstance(getFixtureElement(), SUCCESS);
// Data-based via Builder
getBaseBuilder();
}
}
@Test
public void testConstructorsMinimal() throws InvalidDDMSException {
for (String sVersion : getSupportedVersions()) {
DDMSVersion version = DDMSVersion.setCurrentVersion(sVersion);
// Element-based, No optional fields
Element element = Util.buildDDMSElement(ApplicationSoftware.getName(version), null);
SecurityAttributesTest.getFixture().addTo(element);
getInstance(element, SUCCESS);
// Data-based, No optional fields
ApplicationSoftware.Builder builder = getBaseBuilder();
builder.setValue(null);
getInstance(builder, SUCCESS);
}
}
@Test
public void testValidationErrors() throws InvalidDDMSException {
for (String sVersion : getSupportedVersions()) {
DDMSVersion.setCurrentVersion(sVersion);
// Bad security attributes
ApplicationSoftware.Builder builder = getBaseBuilder();
builder.setSecurityAttributes(null);
getInstance(builder, "classification must exist.");
}
}
@Test
public void testValidationWarnings() throws InvalidDDMSException {
for (String sVersion : getSupportedVersions()) {
DDMSVersion.setCurrentVersion(sVersion);
// No warnings
ApplicationSoftware component = getInstance(getFixtureElement(), SUCCESS);
assertEquals(0, component.getValidationWarnings().size());
// Completely empty
ApplicationSoftware.Builder builder = getBaseBuilder();
builder.setValue(null);
component = getInstance(builder, SUCCESS);
assertEquals(1, component.getValidationWarnings().size());
String text = "A ddms:applicationSoftware element was found with no value.";
String locator = "ddms:applicationSoftware";
assertWarningEquality(text, locator, component.getValidationWarnings().get(0));
}
}
@Test
public void testEquality() throws InvalidDDMSException {
for (String sVersion : getSupportedVersions()) {
DDMSVersion.setCurrentVersion(sVersion);
// Base equality
ApplicationSoftware elementComponent = getInstance(getFixtureElement(), SUCCESS);
ApplicationSoftware builderComponent = new ApplicationSoftware.Builder(elementComponent).commit();
assertEquals(elementComponent, builderComponent);
assertEquals(elementComponent.hashCode(), builderComponent.hashCode());
// Different values in each field
ApplicationSoftware.Builder builder = getBaseBuilder();
builder.setValue(DIFFERENT_VALUE);
assertFalse(elementComponent.equals(builder.commit()));
}
}
@Test
public void testVersionSpecific() throws InvalidDDMSException {
DDMSVersion.setCurrentVersion("2.0");
getInstance(getFixtureElement(), "The applicationSoftware element must not ");
}
@Test
public void testOutput() throws InvalidDDMSException {
for (String sVersion : getSupportedVersions()) {
DDMSVersion.setCurrentVersion(sVersion);
ApplicationSoftware elementComponent = getInstance(getFixtureElement(), SUCCESS);
assertEquals(getExpectedHTMLTextOutput(OutputFormat.HTML), elementComponent.toHTML());
assertEquals(getExpectedHTMLTextOutput(OutputFormat.TEXT), elementComponent.toText());
assertEquals(getExpectedXMLOutput(), elementComponent.toXML());
assertEquals(getExpectedJSONOutput(), elementComponent.toJSON());
assertValidJSON(elementComponent.toJSON());
}
}
@Test
public void testBuilderIsEmpty() throws InvalidDDMSException {
for (String sVersion : getSupportedVersions()) {
DDMSVersion.setCurrentVersion(sVersion);
ApplicationSoftware.Builder builder = new ApplicationSoftware.Builder();
assertNull(builder.commit());
assertTrue(builder.isEmpty());
builder.setValue(TEST_VALUE);
assertFalse(builder.isEmpty());
}
}
}