001: package edu.iu.uis.eden.services.docelements;
002:
003: import java.io.StringReader;
004:
005: import junit.framework.TestCase;
006:
007: import org.jdom.Document;
008: import org.jdom.Element;
009:
010: import edu.iu.uis.eden.WorkflowServiceErrorImpl;
011: import edu.iu.uis.eden.exception.InvalidXmlException;
012: import edu.iu.uis.eden.services.IDocElement;
013: import edu.iu.uis.eden.services.InconsistentDocElementStateException;
014: import edu.iu.uis.eden.services.ServiceErrorConstants;
015: import edu.iu.uis.eden.util.XmlHelper;
016:
017: public class TestPriorityElement extends TestCase {
018: private PriorityElement priority;
019:
020: public TestPriorityElement(String s) {
021: super (s);
022: }
023:
024: protected void setUp() {
025: priority = new PriorityElement();
026: }
027:
028: protected void tearDown() {
029: }
030:
031: /**
032: * must be greater than 0
033: */
034: public void testValidate() {
035: try {
036: /* should get invalid priority type */
037: WorkflowServiceErrorImpl error = this .priority.validate();
038: assertEquals("did not return DocElementError with correct "
039: + "type", ServiceErrorConstants.PRIORITY_INVALID,
040: error.getKey());
041:
042: /* give it a valid value */
043: this .priority.setPriority(1);
044: assertNull("valid object didn't return null", priority
045: .validate());
046: } catch (Exception ex) {
047: fail("threw exception validating");
048: }
049: }
050:
051: /**
052: * given a jdom element of the correct type it should load itself to the value
053: * of the given jdom element
054: *
055: * if the jdom element is null and allowBlanks true it should return
056: *
057: * if the jdom element is null and allowBlanks is false its should throw
058: * an InconsistentDocElementStateException
059: *
060: * if the element is of the incorrect type it should throw an
061: * InvalidXmlException
062: */
063: public void testLoadFromXMLContent() {
064: Element priorityEl = new Element(priority.getElementName());
065: priorityEl.setAttribute("value", new Long(1).toString());
066:
067: try {
068: priority.loadFromXMLContent(priorityEl, false);
069: assertEquals("didn't properly load props from good xml", 1,
070: priority.getPriority());
071: } catch (Exception ex) {
072: fail("threw exception loading from good element");
073: }
074: }
075:
076: /**
077: * is not a routeControl
078: */
079: public void testIsRouteControl() {
080: //test default val
081: assertEquals("returning true on isRouteControl", false,
082: priority.isRouteControl());
083:
084: //set true and retest
085: priority.setRouteControl(true);
086: assertEquals("returning true on isRouteControl", false,
087: priority.isRouteControl());
088: }
089:
090: /**
091: * does PriorityElement render correct XML
092: */
093: public void testGetXMLContent() {
094: priority.setPriority(1);
095: assertNotNull("Loaded priority returned null on getXMLContent",
096: priority.getXMLContent());
097: }
098:
099: /**
100: * can this guy populate himself w/ xml he made.
101: */
102: public void testCanFeedOnOwnXML() {
103: priority.setPriority(1);
104:
105: Element xmlContent = priority.getXMLContent();
106:
107: PriorityElement testPriority = new PriorityElement();
108:
109: try {
110: testPriority.loadFromXMLContent(xmlContent, false);
111: assertEquals(
112: "didn't correctly load props from self generated xml",
113: 1, testPriority.getPriority());
114: } catch (Exception ex) {
115: fail("threw exception loading from self generated xml");
116: }
117: }
118:
119: /**
120: * utility method that is not object specific
121: *
122: * @param docElement the docElement being tested
123: */
124: public void nonClassSpecificLoadFromXMLTests(IDocElement docElement) {
125: //give null allow blanks
126: try {
127: docElement.loadFromXMLContent(null, true);
128: } catch (Exception ex) {
129: fail("threw exception loading null element set to allow blanks");
130: }
131:
132: //give null dont allow blanks
133: try {
134: docElement.loadFromXMLContent(null, false);
135: fail("didn't throw InconsistentDocElementStateException "
136: + "loaded with null element allowBlanks set to false");
137: } catch (InconsistentDocElementStateException ex) {
138: } catch (InvalidXmlException ex) {
139: fail("didn't throw InconsistentDocElementStateException "
140: + "loaded with null element allowBlanks set to false");
141: }
142:
143: //give element of wrong type
144: try {
145: docElement.loadFromXMLContent(new Element("Imbad"), false);
146: fail("Didn't throw InvalidXmlException when loaded with "
147: + "element of the wrong type");
148: } catch (InconsistentDocElementStateException ex) {
149: fail("Didn't throw InvalidXmlException when loaded with "
150: + "element of the wrong type");
151: } catch (InvalidXmlException ex) {
152: }
153: }
154:
155: public Element makeStringElement(String xmlContent) {
156: Document doc = null;
157:
158: try {
159: doc = new Document(XmlHelper.buildJDocument(
160: new StringReader(xmlContent), false)
161: .getRootElement());
162: } catch (InvalidXmlException ex) {
163: fail("Generated invalid xml");
164: }
165:
166: return doc.getRootElement();
167: }
168: }
169:
170: /*
171: * Copyright 2003 The Trustees of Indiana University. All rights reserved.
172: *
173: * This file is part of the EDEN software package.
174: * For license information, see the LICENSE file in the top level directory
175: * of the EDEN source distribution.
176: */
|