001: package edu.iu.uis.eden.services.docelements;
002:
003: import junit.framework.TestCase;
004:
005: import org.jdom.Element;
006: import org.jdom.output.XMLOutputter;
007:
008: import edu.iu.uis.eden.WorkflowServiceErrorImpl;
009: import edu.iu.uis.eden.exception.InvalidXmlException;
010: import edu.iu.uis.eden.services.IDocElement;
011: import edu.iu.uis.eden.services.InconsistentDocElementStateException;
012: import edu.iu.uis.eden.services.ServiceErrorConstants;
013:
014: public class TestUniversityOrganizationElement extends TestCase {
015: UniversityOrganizationElement univElement;
016:
017: public TestUniversityOrganizationElement(String s) {
018: super (s);
019: }
020:
021: protected void setUp() {
022: univElement = new UniversityOrganizationElement();
023: }
024:
025: protected void tearDown() {
026: }
027:
028: /**
029: * this is a route control
030: */
031: public void testIsRouteControl() {
032: assertTrue("UniversityOrganizationElement is a route control",
033: univElement.isRouteControl());
034:
035: //attempt to set it to false
036: univElement.setRouteControl(false);
037: assertTrue("UniversityOrganizationElement is a route control",
038: univElement.isRouteControl());
039: }
040:
041: /**
042: * test that this guy is returning an Element that contains good xml.
043: */
044: public void testGetXMLContent() {
045: String goodXML = "<university_organization_eok route-control=\"yes\">"
046: + "<financial_chart_of_accounts_eok route-control=\"yes\">"
047: + "<fin_coa_cd value=\"BL\" />"
048: + "</financial_chart_of_accounts_eok>"
049: + "<org_cd value=\"ACQU\" />"
050: + "</university_organization_eok>";
051:
052: univElement.setChart("BL");
053: univElement.setOrgCode("ACQU");
054: assertEquals("Didn't make element containing correct xml",
055: goodXML, this .makeElementXMLString(univElement
056: .getXMLContent()));
057: }
058:
059: /**
060: * if chart and org have been set we're valid if one hasn't we're not
061: */
062: public void testValidate() {
063: try {
064: //this should error
065: WorkflowServiceErrorImpl error = univElement.validate();
066: assertNotNull("invalid object returned null on validate",
067: error);
068:
069: univElement.setChart("BL");
070: error = univElement.validate();
071: assertNotNull("invalid object returned null on validate",
072: error);
073:
074: univElement.setOrgCode("ACQU");
075: assertNull("valid object didn't return null on validation",
076: univElement.validate());
077:
078: /* test a bad combo */
079: univElement.setChart("imbad");
080:
081: WorkflowServiceErrorImpl err = univElement.validate();
082: assertNotNull(
083: "invalid UniversityOrganizationElement didn't return "
084: + "an error object", err);
085:
086: /* test that the correct constant is coming back */
087: assertEquals(
088: "invalid UniversityOrganizationElement returned "
089: + "DocElementError of the wrong type",
090: ServiceErrorConstants.UNIVERSITY_ORGANIZATION_INVALID,
091: err.getKey());
092: } catch (Exception ex) {
093: ex.printStackTrace();
094: fail("threw exception validating");
095: }
096: }
097:
098: /**
099: * pass good xml this dudes way and see if the props are loaded correctly
100: * and the proper exceptions thrown
101: */
102: public void testLoadFromXMLContent() {
103: //make an element representative of just a chart in there
104: Element element = new Element(univElement.getElementName());
105: ChartElement chartElement = new ChartElement();
106: chartElement.setChart("BL");
107: element.addContent(chartElement.getXMLContent());
108:
109: //missing org add but allowblanks
110: try {
111: univElement.loadFromXMLContent(element, true);
112: assertEquals(
113: "didn't properly load props from a good element",
114: "BL", univElement.getChart());
115: } catch (Exception ex) {
116: fail("threw exception loading from good element but missing org");
117: }
118:
119: //try the same but missing chart
120: univElement = new UniversityOrganizationElement();
121: element = new Element(univElement.getElementName());
122:
123: OrgCodeElement orgCode = new OrgCodeElement();
124: orgCode.setOrgCode("BL");
125: element.addContent(orgCode.getXMLContent());
126:
127: try {
128: univElement.loadFromXMLContent(element, true);
129: assertEquals(
130: "didn't properly load props from a good element",
131: "BL", univElement.getOrgCode());
132: } catch (Exception ex) {
133: fail("threw exception loading from good element but missing chart");
134: }
135:
136: this .nonClassSpecificLoadFromXMLTests(univElement);
137: }
138:
139: /**
140: * can this guy populate himself w/ xml he made.
141: */
142: public void testCanFeedOnOwnXML() {
143: String chart = "BL";
144: String org = "BL";
145:
146: univElement.setChart("BL");
147: univElement.setOrgCode("BL");
148:
149: Element element = univElement.getXMLContent();
150: UniversityOrganizationElement aUnivElement = new UniversityOrganizationElement();
151:
152: try {
153: aUnivElement.loadFromXMLContent(element, false);
154: } catch (Exception ex) {
155: fail("threw exception loading from self generated xml");
156: }
157:
158: assertEquals("Didn't properly load chart from element", chart,
159: aUnivElement.getChart());
160: assertEquals("didn't properly load org from element", org,
161: aUnivElement.getChart());
162: }
163:
164: /**
165: * utility method that is not object specific
166: *
167: * @param docElement the docElement being tested
168: */
169: public void nonClassSpecificLoadFromXMLTests(IDocElement docElement) {
170: //give null allow blanks
171: try {
172: docElement.loadFromXMLContent(null, true);
173: } catch (Exception ex) {
174: fail("threw exception loading null element set to allow blanks");
175: }
176:
177: //give null dont allow blanks
178: try {
179: docElement.loadFromXMLContent(null, false);
180: fail("didn't throw InconsistentDocElementStateException "
181: + "loaded with null element allowBlanks set to false");
182: } catch (InconsistentDocElementStateException ex) {
183: } catch (InvalidXmlException ex) {
184: fail("didn't throw InconsistentDocElementStateException "
185: + "loaded with null element allowBlanks set to false");
186: }
187:
188: //give element of wrong type
189: try {
190: docElement.loadFromXMLContent(new Element("Imbad"), false);
191: fail("Didn't throw InvalidXmlException when loaded with "
192: + "element of the wrong type");
193: } catch (InconsistentDocElementStateException ex) {
194: fail("Didn't throw InvalidXmlException when loaded with "
195: + "element of the wrong type");
196: } catch (InvalidXmlException ex) {
197: }
198: }
199:
200: public String makeElementXMLString(Element element) {
201: XMLOutputter outputter = new XMLOutputter();
202:
203: return outputter.outputString(element);
204: }
205: }
206:
207: /*
208: * Copyright 2003 The Trustees of Indiana University. All rights reserved.
209: *
210: * This file is part of the EDEN software package.
211: * For license information, see the LICENSE file in the top level directory
212: * of the EDEN source distribution.
213: */
|