001: package edu.iu.uis.eden.services.docelements;
002:
003: import java.io.StringReader;
004: import java.text.SimpleDateFormat;
005: import java.util.Date;
006:
007: import junit.framework.TestCase;
008:
009: import org.jdom.Document;
010: import org.jdom.Element;
011:
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 TestDateElement extends TestCase {
019: private DateElement dateElement;
020:
021: public TestDateElement(String s) {
022: super (s);
023: }
024:
025: protected void setUp() {
026: this .dateElement = new DateElement();
027: }
028:
029: protected void tearDown() {
030: }
031:
032: /**
033: * test the xml content for to_date, from_date and date
034: */
035: public void testGetXMLContent() throws Exception {
036: SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
037: Date daDate = df.parse("11/05/2102");
038:
039: // String xmlDate = "<date value=\"" + daDate.toString() + "\" />";
040: DateElement date = new DateElement();
041:
042: //set to toDate
043: date.setDate(daDate);
044: assertNotNull("Returned null when loaded", date.getXMLContent());
045: }
046:
047: /**
048: * can this guy populate himself w/ xml he made.
049: */
050: public void testCanFeedOnOwnXML() {
051: //start w/ to date
052: DateElement date = new DateElement();
053:
054: // String daDate = "11/05/2102";
055: // date.setDate(new Date(daDate));
056: date.setDate(new Date());
057:
058: Element dateEl = date.getXMLContent();
059:
060: try {
061: date.loadFromXMLContent(dateEl, false);
062: assertNotNull(
063: "didn't properly load props from valid element",
064: date.getDate());
065: } catch (Exception ex) {
066: fail("threw exception loading from self generated xml");
067: }
068: }
069:
070: /**
071: * given a jdom element of the correct type it should load itself to the value
072: * of the given jdom element
073: *
074: * if the jdom element is null and allowBlanks true it should return
075: *
076: * if the jdom element is null and allowBlanks is false its should throw
077: * an InconsistentDocElementStateException
078: *
079: * if the element is of the incorrect type it should throw an
080: * InvalidXmlException
081: */
082: public void testLoadFromXMLContent() throws Exception {
083: //test loading a normal date
084: SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
085: Date daDate = df.parse("9/9/99");
086:
087: Element dateEl = new Element("date");
088: dateEl.setAttribute("value", new Long(daDate.getTime())
089: .toString());
090:
091: try {
092: dateElement.loadFromXMLContent(dateEl, false);
093: assertEquals(
094: "didn't properly load props from valid element",
095: daDate, dateElement.getDate());
096: } catch (Exception ex) {
097: fail("threw exception loading from valid element");
098: }
099:
100: //test implementation of interface
101: this .nonClassSpecificLoadFromXMLTests(dateElement);
102: }
103:
104: /**
105: * an object with a blank date should give a blank error specific to the
106: * type of date it is
107: *
108: * a loaded object w/ a bad date should give an invalid error specific to the
109: * type of date it is
110: */
111: public void testValidate() {
112: try {
113: //check for blank
114: assertEquals(
115: "gave an error object with an incorrect error constant",
116: ServiceErrorConstants.DATE_BLANK, dateElement
117: .validate().getKey());
118:
119: //make valid and check types should all be null
120: dateElement.setDate(new Date());
121: assertNull("valid date didn't return null on validate",
122: dateElement.validate());
123: } catch (Exception ex) {
124: fail("threw exception validating");
125: }
126: }
127:
128: /**
129: * this should not be a route control according to now business rules
130: */
131: public void testIsRouteControl() {
132: assertEquals("This should not be a routeControl", false,
133: this .dateElement.isRouteControl());
134: }
135:
136: /**
137: * utility method that is not object specific
138: *
139: * @param docElement the docElement being tested
140: */
141: public void nonClassSpecificLoadFromXMLTests(IDocElement docElement) {
142: //give null allow blanks
143: try {
144: docElement.loadFromXMLContent(null, true);
145: } catch (Exception ex) {
146: fail("threw exception loading null element set to allow blanks");
147: }
148:
149: //give null dont allow blanks
150: try {
151: docElement.loadFromXMLContent(null, false);
152: fail("didn't throw InconsistentDocElementStateException "
153: + "loaded with null element allowBlanks set to false");
154: } catch (InconsistentDocElementStateException ex) {
155: } catch (InvalidXmlException ex) {
156: fail("didn't throw InconsistentDocElementStateException "
157: + "loaded with null element allowBlanks set to false");
158: }
159:
160: //give element of wrong type
161: try {
162: docElement.loadFromXMLContent(new Element("Imbad"), false);
163: fail("Didn't throw InvalidXmlException when loaded with "
164: + "element of the wrong type");
165: } catch (InconsistentDocElementStateException ex) {
166: fail("Didn't throw InvalidXmlException when loaded with "
167: + "element of the wrong type");
168: } catch (InvalidXmlException ex) {
169: }
170: }
171:
172: public Element makeStringElement(String xmlContent) {
173: Document doc = null;
174:
175: try {
176: doc = new Document(XmlHelper.buildJDocument(
177: new StringReader(xmlContent), false)
178: .getRootElement());
179: } catch (InvalidXmlException ex) {
180: fail("Generated invalid xml");
181: }
182:
183: return doc.getRootElement();
184: }
185: }
186:
187: /*
188: * Copyright 2003 The Trustees of Indiana University. All rights reserved.
189: *
190: * This file is part of the EDEN software package.
191: * For license information, see the LICENSE file in the top level directory
192: * of the EDEN source distribution.
193: */
|