001: package edu.iu.uis.eden.services.docelements;
002:
003: import junit.framework.TestCase;
004:
005: import org.jdom.Element;
006:
007: import edu.iu.uis.eden.exception.InvalidXmlException;
008: import edu.iu.uis.eden.services.InconsistentDocElementStateException;
009:
010: public class TestOrgCodeElement extends TestCase {
011: private OrgCodeElement orgCode;
012:
013: public TestOrgCodeElement(String s) {
014: super (s);
015: }
016:
017: protected void setUp() {
018: orgCode = new OrgCodeElement();
019: orgCode.setOrgCode("UNIV");
020: }
021:
022: protected void tearDown() {
023: }
024:
025: /**
026: * given a jdom element of the correct type it should load itself to the value
027: * of the given jdom element
028: *
029: * if the jdom element is null and allowBlanks true it should return
030: *
031: * if the jdom element is null and allowBlanks is false its should throw
032: * an InconsistentDocElementStateException
033: *
034: * if the element is of the incorrect type it should throw an
035: * InvalidXmlException
036: */
037: public void testLoadFromXMLContent() {
038: //make a blank OrgCodeElement
039: OrgCodeElement orgCode = new OrgCodeElement();
040: String orgVal = "UNIV";
041:
042: //make a good element
043: Element goodEl = new Element(orgCode.getElementName());
044: goodEl.setAttribute("value", orgVal);
045:
046: try {
047: orgCode.loadFromXMLContent(goodEl, false);
048: assertEquals(
049: "didn't properly load props from good element",
050: orgVal, orgCode.getOrgCode());
051: } catch (Exception ex) {
052: fail("threw exception loading from good element");
053: }
054:
055: //give null allow blanks
056: try {
057: orgCode.loadFromXMLContent(null, true);
058: } catch (Exception ex) {
059: fail("threw exception loading null element set to allow blanks");
060: }
061:
062: //give null dont allow blanks
063: try {
064: orgCode.loadFromXMLContent(null, false);
065: fail("didn't throw InconsistentDocElementStateException "
066: + "loaded with null element allowBlanks set to false");
067: } catch (InconsistentDocElementStateException ex) {
068: } catch (InvalidXmlException ex) {
069: fail("didn't throw InconsistentDocElementStateException "
070: + "loaded with null element allowBlanks set to false");
071: }
072:
073: //give element of wrong type
074: try {
075: orgCode.loadFromXMLContent(new Element("Imbad"), false);
076: fail("Didn't throw InvalidXmlException when loaded with "
077: + "element of the wrong type");
078: } catch (InconsistentDocElementStateException ex) {
079: fail("Didn't throw InvalidXmlException when loaded with "
080: + "element of the wrong type");
081: } catch (InvalidXmlException ex) {
082: }
083: }
084:
085: /**
086: * if org is null or empty validate return DocElementError otherwise returns
087: * null
088: */
089: public void testValidate() {
090: try {
091: //validate the default OrgCodeElement should get null - he's good
092: assertNull(
093: "Good OrgCodeElement returned DocElementError on validation",
094: this .orgCode.validate());
095:
096: //make orgCode Evil
097: orgCode = new OrgCodeElement();
098: assertNotNull(
099: "Bad OrgCodeElement returned Null on validation expected "
100: + "a DocElementError", orgCode.validate());
101: } catch (Exception ex) {
102: fail("threw exception validating");
103: }
104: }
105:
106: /**
107: * if orgCode is null or blank empty String returned otherwise valid
108: * XML is returned.
109: */
110: public void testGetXMLContent() {
111: assertNotNull("Returned null when loaded", orgCode
112: .getXMLContent());
113: }
114:
115: /**
116: * can the object load from the same xml in generates and arrive at the same
117: * property value
118: */
119: public void testFeedFromOwnXML() {
120: Element xmlContent = orgCode.getXMLContent();
121:
122: OrgCodeElement aOrgCode = new OrgCodeElement();
123:
124: try {
125: aOrgCode.loadFromXMLContent(xmlContent, false);
126: } catch (Exception ex) {
127: fail("XML generated from OrgCodeElement invalid could not be loaded "
128: + "by itself");
129: }
130:
131: assertEquals(
132: "OrgCodeElement could not find value from XML it generated",
133: orgCode.getOrgCode(), aOrgCode.getOrgCode());
134: }
135:
136: /**
137: * Just inforcing a business rule that may not be so obvious
138: */
139: public void testRouteControlShouldbeFalse() {
140: assertEquals(
141: "OrgCodeElement is not a routeControl. "
142: + "UniversityOrganizationElement is this is a friendly warning.",
143: false, orgCode.isRouteControl());
144: }
145: }
146:
147: /*
148: * Copyright 2003 The Trustees of Indiana University. All rights reserved.
149: *
150: * This file is part of the EDEN software package.
151: * For license information, see the LICENSE file in the top level directory
152: * of the EDEN source distribution.
153: */
|