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.EdenConstants;
011: import edu.iu.uis.eden.WorkflowServiceErrorImpl;
012: import edu.iu.uis.eden.exception.InvalidXmlException;
013: import edu.iu.uis.eden.services.IDocElement;
014: import edu.iu.uis.eden.services.InconsistentDocElementStateException;
015: import edu.iu.uis.eden.services.ServiceErrorConstants;
016: import edu.iu.uis.eden.util.XmlHelper;
017:
018: public class TestActionRequestedElement extends TestCase {
019: private ActionRequestedElement actionRequested;
020:
021: public TestActionRequestedElement(String s) {
022: super (s);
023: }
024:
025: protected void setUp() {
026: actionRequested = new ActionRequestedElement();
027: }
028:
029: protected void tearDown() {
030: }
031:
032: /**
033: * is this returning true when the object is empty
034: */
035: public void testIsEmpty() {
036: assertTrue("new object not returning true", actionRequested
037: .isEmpty());
038:
039: //set something and try again
040: actionRequested.setActionRequested("hello");
041: assertEquals("set ActionRequested returning true on isEmpty()",
042: false, actionRequested.isEmpty());
043: }
044:
045: /**
046: * loaded with an invalid actionrequested (on that's not in EdenConstants
047: * ActionRequested ActionRequestedElement should give a corrects error msg
048: */
049: public void testInvalidActionRequested() {
050: /* try catch added after api change surrounding all logic might not be
051: best approach */
052: try {
053: this .actionRequested.setActionRequested("Z");
054:
055: WorkflowServiceErrorImpl error = this .actionRequested
056: .validate();
057: assertEquals(
058: "Didn't give proper error for invalid actionrequested",
059: ServiceErrorConstants.ACTION_REQUESTED_INVALID,
060: error.getKey());
061:
062: /* load it with a valid and retest should be null */
063: this .actionRequested
064: .setActionRequested(EdenConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ);
065: error = this .actionRequested.validate();
066: assertNull(
067: "valid ActionRequested didn't return null on validation",
068: this .actionRequested.validate());
069: } catch (Exception ex) {
070: fail("threw exception validating");
071: }
072: }
073:
074: /**
075: * this is not a route control
076: */
077: public void testIsRouteControl() {
078: assertEquals(
079: "ActionRequested returning true on isRouteControl",
080: false, actionRequested.isRouteControl());
081: actionRequested.setRouteControl(true);
082: assertEquals(
083: "ActionRequested allowing true to set RouteControl",
084: false, actionRequested.isRouteControl());
085: }
086:
087: /**
088: * given a jdom element of the correct type it should load itself to the value
089: * of the given jdom element
090: *
091: * if the jdom element is null and allowBlanks true it should return
092: *
093: * if the jdom element is null and allowBlanks is false its should throw
094: * an InconsistentDocElementStateException
095: *
096: * if the element is of the incorrect type it should throw an
097: * InvalidXmlException
098: */
099: public void testLoadFromXMLContent() {
100: //test loading a normal date
101: String ackRequested = "A";
102: Element ackRequestedEl = new Element(actionRequested
103: .getElementName());
104: ackRequestedEl.setAttribute("value", ackRequested);
105:
106: try {
107: actionRequested.loadFromXMLContent(ackRequestedEl, false);
108: assertEquals(
109: "didn't properly load props from valid element",
110: ackRequested, actionRequested.getActionRequested());
111: } catch (Exception ex) {
112: fail("threw exception loading from valid element");
113: }
114:
115: //test implementation of interface
116: this .nonClassSpecificLoadFromXMLTests(actionRequested);
117: }
118:
119: /**
120: * is the correct constant coming back for the correct error
121: */
122: public void testValidate() {
123: //check an empty one
124: try {
125: WorkflowServiceErrorImpl error = actionRequested.validate();
126: assertEquals(
127: "validate returned the wrong constant on the DocElementError "
128: + "object",
129: ServiceErrorConstants.ACTION_REQUESTED_BLANK, error
130: .getKey());
131:
132: //this should pass
133: actionRequested.setActionRequested("A");
134: assertNull(
135: "validate on good ActionRequested didn't return null",
136: actionRequested.validate());
137: } catch (Exception ex) {
138: fail("threw exception validating");
139: }
140: }
141:
142: /**
143: * does it give back good xml
144: */
145: public void testGetXMLContent() {
146: Element xml = new Element(actionRequested.getElementName());
147: xml.setAttribute("value", "A");
148:
149: actionRequested.setActionRequested("a");
150: assertEquals("Didn't make XMLContent correctly", xml
151: .getAttributeValue("value"), actionRequested
152: .getXMLContent().getAttributeValue("value"));
153: }
154:
155: /**
156: * he should return caps regardless of what he's set w/
157: */
158: public void testGetActionRequested() {
159: actionRequested.setActionRequested("a");
160: assertEquals(
161: "GetActionRequested not properly capitalizing the "
162: + "actionRequested set with", "A",
163: actionRequested.getActionRequested());
164: }
165:
166: /**
167: * can this guy populate himself w/ xml he made.
168: */
169: public void testCanFeedOnOwnXML() {
170: String action = "A";
171: actionRequested.setActionRequested(action);
172:
173: Element ackReqEl = actionRequested.getXMLContent();
174:
175: try {
176: actionRequested.loadFromXMLContent(ackReqEl, false);
177: assertEquals(
178: "Didn't properly set properties from self made XML",
179: action, actionRequested.getActionRequested());
180: } catch (Exception ex) {
181: fail("threw exception loading from self generated xml");
182: }
183: }
184:
185: /**
186: * utility method that is not object specific
187: *
188: * @param docElement the docElement being tested
189: */
190: public void nonClassSpecificLoadFromXMLTests(IDocElement docElement) {
191: //give null allow blanks
192: try {
193: docElement.loadFromXMLContent(null, true);
194: } catch (Exception ex) {
195: fail("threw exception loading null element set to allow blanks");
196: }
197:
198: //give null dont allow blanks
199: try {
200: docElement.loadFromXMLContent(null, false);
201: fail("didn't throw InconsistentDocElementStateException "
202: + "loaded with null element allowBlanks set to false");
203: } catch (InconsistentDocElementStateException ex) {
204: } catch (InvalidXmlException ex) {
205: fail("didn't throw InconsistentDocElementStateException "
206: + "loaded with null element allowBlanks set to false");
207: }
208:
209: //give element of wrong type
210: try {
211: docElement.loadFromXMLContent(new Element("Imbad"), false);
212: fail("Didn't throw InvalidXmlException when loaded with "
213: + "element of the wrong type");
214: } catch (InconsistentDocElementStateException ex) {
215: fail("Didn't throw InvalidXmlException when loaded with "
216: + "element of the wrong type");
217: } catch (InvalidXmlException ex) {
218: }
219: }
220:
221: public Element makeStringElement(String xmlContent) {
222: Document doc = null;
223:
224: try {
225: doc = new Document(XmlHelper.buildJDocument(
226: new StringReader(xmlContent), false)
227: .getRootElement());
228: } catch (InvalidXmlException ex) {
229: fail("Generated invalid xml");
230: }
231:
232: return doc.getRootElement();
233: }
234: }
235:
236: /*
237: * Copyright 2003 The Trustees of Indiana University. All rights reserved.
238: *
239: * This file is part of the EDEN software package.
240: * For license information, see the LICENSE file in the top level directory
241: * of the EDEN source distribution.
242: */
|