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.WorkflowServiceErrorImpl;
008: import edu.iu.uis.eden.exception.InvalidXmlException;
009: import edu.iu.uis.eden.services.InconsistentDocElementStateException;
010:
011: public class TestChartElement extends TestCase {
012: private ChartElement chartElement;
013:
014: public TestChartElement(String s) {
015: super (s);
016: }
017:
018: protected void setUp() {
019: chartElement = new ChartElement();
020: chartElement.setChart("BL");
021: chartElement.setRouteControl(true);
022: }
023:
024: protected void tearDown() {
025: }
026:
027: /**
028: * given a proper value does ChartElement make correct XML
029: */
030: public void testGetXMLContent() {
031: //for now just test not null we can do better later
032: chartElement.setChart("chart");
033: assertNotNull("Returned null when loaded", chartElement
034: .getXMLContent());
035: }
036:
037: /**
038: * given a jdom element of the correct type it should load itself to the value
039: * of the given jdom element
040: *
041: * if the jdom element is null and allowBlanks true it should return
042: *
043: * if the jdom element is null and allowBlanks is false its should throw
044: * an InconsistentDocElementStateException
045: *
046: * if the element is of the incorrect type it should throw an
047: * InvalidXmlException
048: */
049: public void testLoadFromXMLContent() {
050: //make a new chartElement with clear values
051: ChartElement chart = new ChartElement();
052:
053: //make a jdom docs with the content
054: Element chartRootEl = new Element(
055: "financial_chart_of_accounts_eok");
056: chartRootEl.setAttribute("route-control", "yes");
057:
058: Element chartEl = new Element("fin_coa_cd");
059: chartEl.setAttribute("value", "BL");
060: chartRootEl.addContent(chartEl);
061:
062: try {
063: chart.loadFromXMLContent(chartRootEl, false);
064: assertEquals("Didn't properly load from a valid element",
065: "BL", chart.getChart());
066: } catch (InvalidXmlException ex) {
067: fail("loadFromXMLContent threw an InvalidXmlException when using a "
068: + "good Element");
069: } catch (InconsistentDocElementStateException ex) {
070: fail("loadFromXMLContent threw an InconsistentDocElementStateException "
071: + "when using a good Element");
072: }
073:
074: //give a chart a bad element
075: chart = new ChartElement();
076: chartEl = new Element("imbad");
077: chartEl.setAttribute("value", "BL");
078:
079: try {
080: chart.loadFromXMLContent(chartEl, false);
081: fail("Didn't throw InvalidXMLContentExcecption when using a bad "
082: + "element");
083: } catch (InvalidXmlException ex) {
084: } catch (InconsistentDocElementStateException ex) {
085: fail("Didn't throw InvalidXMLContentExcecption when using a bad "
086: + "element, threw InconsistentDocElementStateException instead");
087: }
088:
089: //same thing but allow blanks
090: chart = new ChartElement();
091:
092: try {
093: chart.loadFromXMLContent(chartEl, true);
094: fail("Didn't throw InvalidXMLContentExcecption when using a bad "
095: + "element");
096: } catch (InvalidXmlException ex) {
097: } catch (InconsistentDocElementStateException ex) {
098: fail("Didn't throw InvalidXMLContentExcecption when using a bad "
099: + "element, threw InconsistentDocElementStateException instead");
100: }
101:
102: //pass null and allow blanks nothing should happen
103: chart = new ChartElement();
104:
105: try {
106: chart.loadFromXMLContent(null, true);
107: } catch (InvalidXmlException ex) {
108: fail("Threw InvalidXmlException when passed a "
109: + "null value and allowBlanks set to true");
110: } catch (InconsistentDocElementStateException ex) {
111: fail("Threw InconsistentDocElementStateException when passed a "
112: + "null value and allowBlanks set to true");
113: }
114:
115: //pass null and dont allow blanks
116: chart = new ChartElement();
117:
118: try {
119: chart.loadFromXMLContent(null, false);
120: fail("Didn't Throw InconsistentDocElementStateException when passed a "
121: + "null value and allowBlanks set to false");
122: } catch (InvalidXmlException ex) {
123: fail("Didn't Throw InconsistentDocElementStateException when passed a "
124: + "null value and allowBlanks set to false");
125: } catch (InconsistentDocElementStateException ex) {
126: }
127: }
128:
129: /**
130: * test that validate returns null when chart is set and that a
131: * DocElementError is returned when nothing is set
132: */
133: public void testValidate() {
134: try {
135: //validate the default chart should be true
136: assertNull(
137: "Valid ChartElement didn't return null from validate",
138: chartElement.validate());
139:
140: //make a new BAD ChartElement
141: ChartElement chart = new ChartElement();
142: WorkflowServiceErrorImpl error = (WorkflowServiceErrorImpl) chart
143: .validate();
144:
145: assertNotNull(
146: "Invalid CharElement returned null on validate",
147: error);
148: } catch (Exception ex) {
149: fail("threw exception validating");
150: }
151: }
152:
153: /**
154: * can this guy populate himself w/ xml he made.
155: */
156: public void testCanFeedOnOwnXML() {
157: ChartElement chart = new ChartElement();
158: chart.setChart("chart");
159:
160: Element chartEl = chart.getXMLContent();
161:
162: ChartElement aChartEl = new ChartElement();
163:
164: try {
165: aChartEl.loadFromXMLContent(chartEl, false);
166: } catch (InvalidXmlException ex) {
167: fail("XML generated invalid could not be loaded "
168: + "by itself");
169: } catch (InconsistentDocElementStateException ex) {
170: fail("XML generated invalid could not be loaded "
171: + "by itself");
172: }
173:
174: assertEquals("Could not find value from XML it generated",
175: chart.getChart(), aChartEl.getChart());
176: }
177:
178: /**
179: * this should be a route control according to now business rules
180: */
181: public void testIsRouteControl() {
182: assertEquals("This should be a routeControl", true,
183: this .chartElement.isRouteControl());
184: }
185: }
186:
187: /*
188: * Copyright 2003 The Trustees of Indiana University. All rights reserved.
189: *
190: * This file is part of the EDEN software package.
191: * For license information, see the LICENSE file in the top level directory
192: * of the EDEN source distribution.
193: */
|