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.exception.InvalidXmlException;
009: import edu.iu.uis.eden.services.IDocElement;
010: import edu.iu.uis.eden.services.InconsistentDocElementStateException;
011:
012: public class TestHeaderElement extends TestCase {
013: private HeaderElement header;
014:
015: public TestHeaderElement(String s) {
016: super (s);
017: }
018:
019: protected void setUp() {
020: header = new HeaderElement();
021: }
022:
023: protected void tearDown() {
024: }
025:
026: /**
027: * this is not a routeControl
028: */
029: public void testIsRouteControl() {
030: assertEquals("Header is returning true on isRouteControl",
031: false, header.isRouteControl());
032:
033: //try and make it one
034: header.setRouteControl(true);
035: assertEquals("Header is returning true on isRouteControl",
036: false, header.isRouteControl());
037: }
038:
039: /**
040: * get null if empty.
041: *
042: * If keys and values are set verify in the xml
043: */
044: public void testGetXMLContent() {
045: assertNull("Empty header didn't return null on getXMLContent",
046: header.getXMLContent());
047:
048: String key = "key";
049: String value = "value";
050: header.setKeyValuePair(key, value);
051:
052: Element element = header.getXMLContent();
053: Element keyElement = element.getChild(key);
054: assertNotNull(
055: "didn't make an element for an existing key/value pair",
056: keyElement);
057: assertEquals("didn't properly put key/values in xml", value,
058: keyElement.getAttributeValue(value));
059:
060: //add another key/value pair test for it
061: String key2 = "key2";
062: String value2 = "value2";
063: header.setKeyValuePair(key2, value2);
064:
065: element = header.getXMLContent();
066: keyElement = element.getChild(key2);
067:
068: assertNotNull(
069: "didn't make an element for an existing key/value pair",
070: keyElement);
071: assertEquals("didn't properly put key/values in xml", value2,
072: keyElement.getAttributeValue(value));
073: }
074:
075: /**
076: * standard interface tests and loading correct value/key pairs
077: * from the element
078: */
079: public void testLoadFromXMLContent() {
080: Element rootEl = new Element(header.getElementName());
081:
082: //make a key/value element
083: Element keyValue1 = new Element("key1");
084: keyValue1.setAttribute("value", "value1");
085:
086: //make another key/value element
087: Element keyValue2 = new Element("key2");
088: keyValue2.setAttribute("value", "value2");
089:
090: //add them to the header element
091: rootEl.addContent(keyValue1);
092: rootEl.addContent(keyValue2);
093:
094: try {
095: header.loadFromXMLContent(rootEl, false);
096: } catch (Exception ex) {
097: fail("threw exception loading from valid element");
098: }
099:
100: assertEquals("Didn't properly load HashMap from elements",
101: "value1", header.getValueByKey("key1"));
102: assertEquals("Didn't properly load HashMap from elements",
103: "value2", header.getValueByKey("key2"));
104:
105: //test interface actions
106: this .nonClassSpecificLoadFromXMLTests(header);
107: }
108:
109: /**
110: * currently always valid
111: */
112: public void testValidate() {
113: try {
114: assertNull("didn't return null on valid (blank) object",
115: header.validate());
116:
117: //load and check for same result
118: header.setKeyValuePair("key", "value");
119: assertNull("didn't return null on valid object", header
120: .validate());
121: } catch (Exception ex) {
122: fail("threw exception validating");
123: }
124: }
125:
126: /**
127: * can this guy populate himself w/ xml he made.
128: */
129: public void testCanFeedOnOwnXML() {
130: String key1 = "key1";
131: String key2 = "key2";
132: String value1 = "value1";
133: String value2 = "value2";
134: header.setKeyValuePair(key1, value1);
135: header.setKeyValuePair(key2, value2);
136:
137: Element headerEl = header.getXMLContent();
138:
139: HeaderElement aHeader = new HeaderElement();
140:
141: try {
142: aHeader.loadFromXMLContent(headerEl, false);
143: assertEquals(
144: "Didn't properly set props from self generated element",
145: value1, header.getValueByKey(key1));
146: assertEquals(
147: "Didn't properly set props from self generated element",
148: value2, header.getValueByKey(key2));
149: } catch (Exception ex) {
150: fail("threw exception loading from self generated element");
151: }
152: }
153:
154: /**
155: * utility method that is not object specific
156: *
157: * @param docElement the docElement being tested
158: */
159: public void nonClassSpecificLoadFromXMLTests(IDocElement docElement) {
160: //give null allow blanks
161: try {
162: docElement.loadFromXMLContent(null, true);
163: } catch (Exception ex) {
164: fail("threw exception loading null element set to allow blanks");
165: }
166:
167: //give null dont allow blanks
168: try {
169: docElement.loadFromXMLContent(null, false);
170: fail("didn't throw InconsistentDocElementStateException "
171: + "loaded with null element allowBlanks set to false");
172: } catch (InconsistentDocElementStateException ex) {
173: } catch (InvalidXmlException ex) {
174: fail("didn't throw InconsistentDocElementStateException "
175: + "loaded with null element allowBlanks set to false");
176: }
177:
178: //give element of wrong type
179: try {
180: docElement.loadFromXMLContent(new Element("Imbad"), false);
181: fail("Didn't throw InvalidXmlException when loaded with "
182: + "element of the wrong type");
183: } catch (InconsistentDocElementStateException ex) {
184: fail("Didn't throw InvalidXmlException when loaded with "
185: + "element of the wrong type");
186: } catch (InvalidXmlException ex) {
187: }
188: }
189:
190: public String makeElementXMLString(Element element) {
191: XMLOutputter outputter = new XMLOutputter();
192:
193: return outputter.outputString(element);
194: }
195: }
196:
197: /*
198: * Copyright 2003 The Trustees of Indiana University. All rights reserved.
199: *
200: * This file is part of the EDEN software package.
201: * For license information, see the LICENSE file in the top level directory
202: * of the EDEN source distribution.
203: */
|