001: /*******************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-impl/api-impl/src/test/org/sakaiproject/test/metaobj/StucturedObjectTest.java $
003: * $Id: StucturedObjectTest.java 19408 2006-12-11 23:52:11Z john.ellis@rsmart.com $
004: * **********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: ******************************************************************************/package org.sakaiproject.test.metaobj;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.jdom.output.Format;
025: import org.jdom.output.XMLOutputter;
026: import org.sakaiproject.metaobj.shared.mgt.FieldValueWrapperFactory;
027: import org.sakaiproject.metaobj.shared.mgt.StructuredArtifactValidationService;
028: import org.sakaiproject.metaobj.shared.model.ElementBean;
029: import org.sakaiproject.metaobj.shared.model.ElementListBean;
030: import org.sakaiproject.metaobj.shared.model.ValidationError;
031: import org.sakaiproject.metaobj.utils.xml.SchemaFactory;
032: import org.sakaiproject.metaobj.utils.xml.SchemaNode;
033: import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
034:
035: import java.io.ByteArrayOutputStream;
036: import java.io.IOException;
037: import java.util.Date;
038: import java.util.Iterator;
039: import java.util.List;
040:
041: /**
042: * Created by IntelliJ IDEA.
043: * User: John Ellis
044: * Date: Aug 19, 2005
045: * Time: 9:57:14 AM
046: * To change this template use File | Settings | File Templates.
047: */
048: public class StucturedObjectTest extends
049: AbstractDependencyInjectionSpringContextTests {
050:
051: private static final Log log = LogFactory
052: .getLog(StucturedObjectTest.class);
053:
054: private SchemaFactory schemaFactory;
055: private StructuredArtifactValidationService validator;
056:
057: protected String[] getConfigLocations() {
058: String[] configLocations = { "spring-beans-test.xml" };
059: return configLocations;
060: }
061:
062: protected void onSetUp() throws Exception {
063: super .onSetUp();
064: schemaFactory = SchemaFactory.getInstance();
065: validator = (StructuredArtifactValidationService) applicationContext
066: .getBean("org.sakaiproject.metaobj.shared.mgt.StructuredArtifactValidationService");
067: }
068:
069: public void testElementValidationGood() throws Exception {
070: ElementBean bean = getElementBean();
071:
072: bean.put("firstName", "John");
073: bean.put("middle", "d");
074: bean.put("lastName", "Ellis");
075:
076: bean.put("expires", new Date());
077:
078: ElementBean emails = (ElementBean) bean.get("emails");
079:
080: // can set a simple list (eachi item in list is a data node) with a string array
081: emails.put("email", new String[] {
082: "johnEllis@alumni.creighton.edu",
083: "john.ellis@rsmart.com" });
084:
085: ElementListBean phoneList = (ElementListBean) bean
086: .get("phoneNumber");
087:
088: // must create an element for each item in a complex list
089: ElementBean phone1 = phoneList.createBlank();
090: ElementBean phone2 = phoneList.createBlank();
091:
092: phone1.put("type", "Home");
093: phone1.put("number", "1234567890"); // i don't think i'll put my real number here :)
094: phone1.put("contact", "true");
095: phoneList.add(phone1);
096:
097: phone2.put("type", "Cell");
098: phone2.put("number", "987654321"); // i don't think i'll put my real number here :)
099: phoneList.add(phone2);
100:
101: List errors = validator.validate(bean);
102:
103: if (errors.size() == 0) {
104: log.error("No errors");
105: }
106:
107: for (Iterator i = errors.iterator(); i.hasNext();) {
108: ValidationError error = (ValidationError) i.next();
109: log.error("found error " + error.getDefaultMessage());
110: }
111:
112: dumpBean(bean);
113: }
114:
115: private void dumpBean(ElementBean bean) throws IOException {
116: XMLOutputter outputter = new XMLOutputter();
117: ByteArrayOutputStream os = new ByteArrayOutputStream();
118:
119: Format format = Format.getPrettyFormat();
120: outputter.setFormat(format);
121: outputter.output(bean.getBaseElement(), os);
122:
123: log.error(new String(os.toByteArray()));
124: }
125:
126: public void testElementValidationBad() throws Exception {
127: ElementBean bean = getElementBean();
128:
129: bean.put("firstName", "John");
130: bean.put("middle", "d");
131:
132: // no last name
133: // bean.put("lastName", "Ellis");
134:
135: bean.put("expires", new Date());
136:
137: ElementBean emails = (ElementBean) bean.get("emails");
138:
139: // can set a simple list (eachi item in list is a data node) with a string array
140: emails.put("email", new String[] {
141: "johnEllis@alumni.creighton.edu",
142: "john.ellisrsmart.com" }); // malformed email
143:
144: ElementListBean phoneList = (ElementListBean) bean
145: .get("phoneNumber");
146:
147: // must create an element for each item in a complex list
148: ElementBean phone1 = phoneList.createBlank();
149: ElementBean phone2 = phoneList.createBlank();
150:
151: phone1.put("type", "Home");
152: phone1.put("number", "1234567890"); // i don't think i'll put my real number here :)
153: phone1.put("contact", "true");
154: phoneList.add(phone1);
155:
156: phone2.put("type", "Something not in enum"); // not in enum
157: phone2.put("number", "987654321"); // i don't think i'll put my real number here :)
158: phoneList.add(phone2);
159:
160: List errors = validator.validate(bean);
161:
162: if (errors.size() == 0) {
163: log.error("No errors");
164: }
165:
166: for (Iterator i = errors.iterator(); i.hasNext();) {
167: ValidationError error = (ValidationError) i.next();
168: log.error("found error " + error.getDefaultMessage());
169: }
170:
171: dumpBean(bean);
172: }
173:
174: private ElementBean getElementBean() {
175: SchemaNode node = schemaFactory.getSchema(getClass()
176: .getResourceAsStream("/testSchema.xsd"));
177: ElementBean bean = new ElementBean("contactInfo", node
178: .getChild("contactInfo"), true);
179: ElementBean
180: .setWrapperFactory((FieldValueWrapperFactory) applicationContext
181: .getBean("fieldValueWrapperFactory"));
182: return bean;
183: }
184:
185: }
|