001: package edu.iu.uis.eden.services.docelements;
002:
003: import java.util.Date;
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.IDocElement;
010: import edu.iu.uis.eden.services.InconsistentDocElementStateException;
011: import edu.iu.uis.eden.services.ServiceErrorConstants;
012:
013: public class TestDateRangeElement extends IDocInterfaceTestTemplate {
014: private DateRangeElement dateRange;
015:
016: public TestDateRangeElement(String s) {
017: super (s);
018: }
019:
020: protected void setUp() {
021: dateRange = new DateRangeElement();
022: }
023:
024: protected void tearDown() {
025: }
026:
027: public IDocElement getIDocElement() {
028: return dateRange;
029: }
030:
031: /**
032: * with no props loaded should be empty (i.e. instantiation)
033: *
034: * with any prop loaded it's not empty
035: */
036: public void testIsEmpty() {
037: assertTrue("Empty DateRangeElement returning false on isEmpty",
038: this .dateRange.isEmpty());
039:
040: //load it with a prop and retest
041: dateRange.setFromDate(new Date());
042: assertEquals("Loaded DateRange returned true on isEmpty",
043: false, dateRange.isEmpty());
044: }
045:
046: /**
047: * check that it's picking up missing kids and is not allowing
048: * a from date that is after a todate
049: */
050: public void testValidate() {
051: try {
052: //test blank
053: assertNotNull("empty Date Range is reporting valid",
054: dateRange.validate());
055:
056: //load with a date
057: WorkflowServiceErrorImpl error = dateRange.validate();
058: dateRange.setFromDate(new Date());
059: assertNotNull(
060: "Date Range with just from date set is reporting valid",
061: error);
062:
063: //is the error of the correct type
064: assertEquals("blank from date reporting wrong error",
065: ServiceErrorConstants.CHILDREN_IN_ERROR, error
066: .getKey());
067:
068: //just to date
069: dateRange.setFromDate(null);
070: dateRange.setToDate(new Date());
071: error = dateRange.validate();
072: assertNotNull(
073: "Date Range with just to date set is reporting valid",
074: error);
075:
076: //is the error of the correct type
077: assertEquals("blank from date reporting wrong error",
078: ServiceErrorConstants.CHILDREN_IN_ERROR, error
079: .getKey());
080:
081: //set both but to before from
082: dateRange.setToDate(new Date());
083: dateRange
084: .setFromDate(new Date(new Date().getTime() + 10000));
085: error = dateRange.validate();
086: assertNotNull(
087: "Date Range with to date set before from date is "
088: + "reporting valid", error);
089:
090: //is the error of the correct type
091: assertEquals("invalid date range reporting wrong error",
092: ServiceErrorConstants.DATE_RANGE_INVALID, error
093: .getKey());
094:
095: //test the valid scenary
096: dateRange.setToDate(new Date());
097: dateRange.setFromDate(new Date(
098: new Date().getTime() - 1000000));
099: assertNull("valid date range reporting error", dateRange
100: .validate());
101:
102: //no date can be before now let's try
103: dateRange
104: .setToDate(new Date(new Date().getTime() - 100000));
105: error = dateRange.validate();
106: assertEquals(
107: "to date before now returned incorrect to date",
108: ServiceErrorConstants.CHILDREN_IN_ERROR, error
109: .getKey());
110: } catch (Exception ex) {
111: fail("threw exception validating");
112: }
113: }
114:
115: /**
116: * loaded and unload does date range give back the correct xml
117: */
118: public void testGetXMLContent() {
119: //nothing loaded should give us null
120: super .testGetXMLContent();
121:
122: //load with a to Date should have a to Date wrapper
123: dateRange.setToDate(new Date());
124:
125: Element dateRangeEl = dateRange.getXMLContent();
126: Element toDateWrapper = dateRangeEl.getChild("toDate");
127: assertEquals(
128: "date range loaded with to date didn't return valid "
129: + "toDate element", "toDate", toDateWrapper
130: .getName());
131:
132: //load with from date and check for wrapper
133: dateRange.setFromDate(new Date());
134: dateRangeEl = dateRange.getXMLContent();
135:
136: Element fromDateWrapper = dateRangeEl.getChild("fromDate");
137: assertEquals(
138: "date range load with from date didn't return valid "
139: + "fromDate element", "fromDate",
140: fromDateWrapper.getName());
141: }
142:
143: /**
144: * given a root element with just a from date wrapper and just a
145: * toDate wrapper element we should get just those poperties
146: *
147: * if we give wrappers that are not what they should be we should
148: * get and invalidxmlexception
149: */
150: public void testLoadFromXMLContent() {
151: //make a root with just a toDate
152: Element element = new Element(dateRange.getElementName());
153: Element toDateWrapper = new Element("toDate");
154: Element nowDateWrapper = new Element("nowDate");
155: DateElement nowDate = new DateElement();
156: nowDate.setDate(new Date());
157: nowDateWrapper.addContent(nowDate.getXMLContent());
158:
159: DateElement toDate = new DateElement();
160: Date daDate = new Date();
161:
162: toDate.setDate(daDate);
163:
164: toDateWrapper.addContent(toDate.getXMLContent());
165: element.addContent(toDateWrapper);
166: element.addContent(nowDateWrapper);
167:
168: try {
169: dateRange.loadFromXMLContent(element, true);
170: assertEquals(
171: "didn't properly load props from valid element",
172: daDate.getTime(), dateRange.getToDate().getTime());
173: } catch (Exception ex) {
174: fail("threw exception loading valid element with toDate element");
175: }
176:
177: //load with but don't allow blanks
178: try {
179: dateRange.loadFromXMLContent(element, false);
180: fail("incomplete dateRange didn't throw "
181: + "InconsistentDocElementStateException when loading from "
182: + "incomplete xml");
183: } catch (InvalidXmlException ex) {
184: fail("incomplete dateRange didn't throw "
185: + "InconsistentDocElementStateException when loading from "
186: + "incomplete xml");
187: } catch (InconsistentDocElementStateException ex) {
188: }
189:
190: //make a root with just fromDate
191: element = new Element(dateRange.getElementName());
192:
193: Element fromDateWrapper = new Element("fromDate");
194: DateElement fromDate = new DateElement();
195: Date daFromDate = new Date();
196:
197: fromDate.setDate(daFromDate);
198:
199: fromDateWrapper.addContent(fromDate.getXMLContent());
200: element.addContent(nowDateWrapper.detach());
201: element.addContent(fromDateWrapper);
202:
203: try {
204: dateRange.loadFromXMLContent(element, true);
205: assertEquals("didn't properly load props from valid xml",
206: daFromDate.getTime(), dateRange.getFromDate()
207: .getTime());
208: } catch (Exception ex) {
209: fail("threw exception loading from valid element with fromDate element");
210: }
211:
212: //load but don't allow blanks
213: try {
214: dateRange.loadFromXMLContent(element, false);
215: fail("incomplete dateRange didn't throw "
216: + "InconsistentDocElementStateException when loading from "
217: + "incomplete xml");
218: } catch (InvalidXmlException ex) {
219: fail("incomplete dateRange didn't throw "
220: + "InconsistentDocElementStateException when loading from "
221: + "incomplete xml");
222: } catch (InconsistentDocElementStateException ex) {
223: }
224: }
225:
226: /**
227: * set the dateRange values, make xml, load a new dateRange with those values
228: * they should be the same.
229: */
230: public void testCanFeedOnOwnXML() {
231: Date fromDate = new Date(new Date().getTime() - 10000000);
232: Date toDate = new Date();
233:
234: dateRange.setFromDate(fromDate);
235: dateRange.setToDate(toDate);
236:
237: Element dateRangeEl = dateRange.getXMLContent();
238:
239: DateRangeElement aDateRange = new DateRangeElement();
240:
241: try {
242: aDateRange.loadFromXMLContent(dateRangeEl, false);
243: } catch (Exception ex) {
244: fail("threw exception loading from self generated element");
245: }
246:
247: assertEquals(
248: "didn't properly load props from self generated element",
249: toDate.getTime(), aDateRange.getToDate().getTime());
250:
251: assertEquals(
252: "didn't properly load props from self generated element",
253: fromDate.getTime(), aDateRange.getFromDate().getTime());
254: }
255: }
256:
257: /*
258: * Copyright 2003 The Trustees of Indiana University. All rights reserved.
259: *
260: * This file is part of the EDEN software package.
261: * For license information, see the LICENSE file in the top level directory
262: * of the EDEN source distribution.
263: */
|