001: package edu.iu.uis.eden.services.docelements;
002:
003: import java.io.StringReader;
004:
005: import org.jdom.Document;
006: import org.jdom.Element;
007:
008: import edu.iu.uis.eden.exception.InvalidXmlException;
009: import edu.iu.uis.eden.services.IDocElement;
010: import edu.iu.uis.eden.services.InconsistentDocElementStateException;
011: import edu.iu.uis.eden.util.XmlHelper;
012:
013: public class TestActiveIndicatorElement extends
014: IDocInterfaceTestTemplate {
015: private ActiveIndicatorElement activeInd;
016:
017: public TestActiveIndicatorElement(String s) {
018: super (s);
019: }
020:
021: protected void setUp() {
022: activeInd = new ActiveIndicatorElement();
023: }
024:
025: protected void tearDown() {
026: }
027:
028: public IDocElement getIDocElement() {
029: return this .activeInd;
030: }
031:
032: /**
033: * true or false should be valid
034: */
035: public void testValidate() {
036: try {
037: assertNull("valid object didn't return null", activeInd
038: .validate());
039:
040: //set activeInd to true and recheck
041: assertNull("valid object didn't return null", activeInd
042: .validate());
043: } catch (Exception ex) {
044: fail("threw exception validating");
045: }
046: }
047:
048: /**
049: * should always be false
050: */
051: public void testIsRouteControl() {
052: assertEquals("ActiveIndicator returned true on isRouteControl",
053: false, activeInd.isRouteControl());
054:
055: //set routeControl to true and recheck;
056: activeInd.setRouteControl(true);
057: assertEquals("ActiveIndicator returned true on isRouteControl",
058: false, activeInd.isRouteControl());
059: }
060:
061: /**
062: * given a jdom element of the correct type it should load itself to the value
063: * of the given jdom element
064: *
065: * if the jdom element is null and allowBlanks true it should return
066: *
067: * if the jdom element is null and allowBlanks is false its should throw
068: * an InconsistentDocElementStateException
069: *
070: * if the element is of the incorrect type it should throw an
071: * InvalidXmlException
072: */
073: public void testLoadFromXMLContent() {
074: String XMLContentActive = "<active_ind value=\"Y\" />";
075: String XMLContentInActive = "<active_ind value=\"N\" />";
076:
077: try {
078: activeInd.loadFromXMLContent(this
079: .makeStringElement(XMLContentActive), false);
080: assertEquals("didn't properly load props from valid xml",
081: true, activeInd.getActiveIndicator());
082: } catch (Exception e) {
083: fail("threw exception loading from good xml content");
084: }
085:
086: //do same for inactive
087: try {
088: activeInd.loadFromXMLContent(this
089: .makeStringElement(XMLContentInActive), false);
090: assertEquals("didn't properly load props from valid xml",
091: false, activeInd.getActiveIndicator());
092: } catch (Exception e) {
093: fail("threw exception loading from good xml content");
094: }
095:
096: //test implementation of interface
097: this .nonClassSpecificLoadFromXMLTests(activeInd);
098: }
099:
100: /**
101: * does ActiveIndicator render the correct xml representation when set to
102: * true and false
103: */
104: public void testGetXMLContent() {
105: // String XMLContentActive = "<active_ind value=\"Y\" />";
106: // String XMLContentInActive = "<active_ind value=\"N\" />";
107: assertNotNull("Returned null Element when loaded", activeInd
108: .getXMLContent());
109: activeInd.setActiveIndicator(true);
110: assertNotNull("Returned null Element when loaded", activeInd
111: .getXMLContent());
112: }
113:
114: /**
115: * can this guy populate himself w/ xml he made.
116: */
117: public void testCanFeedOnOwnXML() {
118: activeInd.setActiveIndicator(true);
119:
120: Element activeIndEl = activeInd.getXMLContent();
121:
122: try {
123: activeInd.loadFromXMLContent(activeIndEl, false);
124: assertEquals(
125: "Didn't properly set properties from self made XML",
126: true, activeInd.getActiveIndicator());
127: } catch (Exception ex) {
128: fail("threw exception loading from self generated xml");
129: }
130:
131: //try w/ a false value
132: activeInd.setActiveIndicator(false);
133: activeIndEl = activeInd.getXMLContent();
134:
135: try {
136: activeInd.loadFromXMLContent(activeIndEl, false);
137: assertEquals(
138: "Didn't properly set properties from self made XML",
139: false, activeInd.getActiveIndicator());
140: } catch (Exception ex) {
141: fail("threw exception loading from self generated xml");
142: }
143: }
144:
145: /**
146: * utility method that is not object specific
147: *
148: * @param docElement the docElement being tested
149: */
150: public void nonClassSpecificLoadFromXMLTests(IDocElement docElement) {
151: //give null allow blanks
152: try {
153: docElement.loadFromXMLContent(null, true);
154: } catch (Exception ex) {
155: fail("threw exception loading null element set to allow blanks");
156: }
157:
158: //give null dont allow blanks
159: try {
160: docElement.loadFromXMLContent(null, false);
161: fail("didn't throw InconsistentDocElementStateException "
162: + "loaded with null element allowBlanks set to false");
163: } catch (InconsistentDocElementStateException ex) {
164: } catch (InvalidXmlException ex) {
165: fail("didn't throw InconsistentDocElementStateException "
166: + "loaded with null element allowBlanks set to false");
167: }
168:
169: //give element of wrong type
170: try {
171: docElement.loadFromXMLContent(new Element("Imbad"), false);
172: fail("Didn't throw InvalidXmlException when loaded with "
173: + "element of the wrong type");
174: } catch (InconsistentDocElementStateException ex) {
175: fail("Didn't throw InvalidXmlException when loaded with "
176: + "element of the wrong type");
177: } catch (InvalidXmlException ex) {
178: }
179: }
180:
181: public Element makeStringElement(String xmlContent) {
182: Document doc = null;
183:
184: try {
185: doc = new Document(XmlHelper.buildJDocument(
186: new StringReader(xmlContent), false)
187: .getRootElement());
188: } catch (InvalidXmlException ex) {
189: fail("Generated invalid xml");
190: }
191:
192: return doc.getRootElement();
193: }
194: }
195:
196: /*
197: * Copyright 2003 The Trustees of Indiana University. All rights reserved.
198: *
199: * This file is part of the EDEN software package.
200: * For license information, see the LICENSE file in the top level directory
201: * of the EDEN source distribution.
202: */
|