001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.xml.export;
018:
019: import java.io.BufferedInputStream;
020: import java.io.ByteArrayInputStream;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.apache.commons.lang.StringUtils;
025: import org.junit.Test;
026:
027: import edu.iu.uis.eden.KEWServiceLocator;
028: import edu.iu.uis.eden.export.ExportDataSet;
029: import edu.iu.uis.eden.export.ExportFormat;
030: import edu.iu.uis.eden.routetemplate.RuleAttribute;
031: import edu.iu.uis.eden.routetemplate.RuleTemplateAttribute;
032:
033: public class RuleAttributeXmlExporterTest extends XmlExporterTestCase {
034:
035: /**
036: * This will test some rule attributes with routing and searching config.
037: */
038: @Test
039: public void testExport() throws Exception {
040: loadXmlFile("RuleAttributeExportConfig.xml");
041: assertExport();
042: }
043:
044: protected void assertExport() throws Exception {
045: // export all existing rule attributes
046: List oldRuleAttributes = KEWServiceLocator
047: .getRuleAttributeService().findAll();
048: ExportDataSet dataSet = new ExportDataSet(ExportFormat.XML);
049: dataSet.getRuleAttributes().addAll(oldRuleAttributes);
050: byte[] xmlBytes = KEWServiceLocator.getXmlExporterService()
051: .export(ExportFormat.XML, dataSet);
052: assertTrue("XML should be non empty.", xmlBytes != null
053: && xmlBytes.length > 0);
054:
055: // import the exported xml
056: loadXmlStream(new BufferedInputStream(new ByteArrayInputStream(
057: xmlBytes)));
058:
059: List newRuleAttributes = KEWServiceLocator
060: .getRuleAttributeService().findAll();
061: assertEquals(
062: "Should have same number of old and new RuleAttributes.",
063: oldRuleAttributes.size(), newRuleAttributes.size());
064: for (Iterator iterator = oldRuleAttributes.iterator(); iterator
065: .hasNext();) {
066: RuleAttribute oldRuleAttribute = (RuleAttribute) iterator
067: .next();
068: boolean foundAttribute = false;
069: for (Iterator iterator2 = newRuleAttributes.iterator(); iterator2
070: .hasNext();) {
071: RuleAttribute newRuleAttribute = (RuleAttribute) iterator2
072: .next();
073: if (oldRuleAttribute.getName().equals(
074: newRuleAttribute.getName())) {
075: assertRuleAttributeExport(oldRuleAttribute,
076: newRuleAttribute);
077: foundAttribute = true;
078: }
079: }
080: assertTrue(
081: "Could not locate the new rule attribute for name "
082: + oldRuleAttribute.getName(),
083: foundAttribute);
084: }
085: }
086:
087: private void assertRuleAttributeExport(
088: RuleAttribute oldRuleAttribute,
089: RuleAttribute newRuleAttribute) {
090: // ids should be the same because we don't version rule attributes, but thier version number should be different
091: assertEquals("Ids should be the same.", oldRuleAttribute
092: .getRuleAttributeId(), newRuleAttribute
093: .getRuleAttributeId());
094: assertFalse("Version numbers should be different.",
095: oldRuleAttribute.getLockVerNbr().equals(
096: newRuleAttribute.getLockVerNbr()));
097: assertEquals(oldRuleAttribute.getDescription(),
098: newRuleAttribute.getDescription());
099: assertEquals(oldRuleAttribute.getName(), newRuleAttribute
100: .getName());
101: assertEquals(oldRuleAttribute.getLabel(), newRuleAttribute
102: .getLabel());
103: assertEquals(oldRuleAttribute.getType(), newRuleAttribute
104: .getType());
105: assertEquals(StringUtils.deleteWhitespace(oldRuleAttribute
106: .getXmlConfigData()), StringUtils
107: .deleteWhitespace(newRuleAttribute.getXmlConfigData()));
108: assertRuleTemplateAttributes(oldRuleAttribute
109: .getRuleTemplateAttributes(), newRuleAttribute
110: .getRuleTemplateAttributes());
111: }
112:
113: private void assertRuleTemplateAttributes(
114: List oldRuleTemplateAttributes,
115: List newRuleTemplateAttributes) {
116: assertEquals(oldRuleTemplateAttributes.size(),
117: newRuleTemplateAttributes.size());
118: for (Iterator iterator = oldRuleTemplateAttributes.iterator(); iterator
119: .hasNext();) {
120: RuleTemplateAttribute oldAttribute = (RuleTemplateAttribute) iterator
121: .next();
122: boolean foundAttribute = false;
123: for (Iterator iterator2 = oldRuleTemplateAttributes
124: .iterator(); iterator2.hasNext();) {
125: RuleTemplateAttribute newAttribute = (RuleTemplateAttribute) iterator2
126: .next();
127: if (oldAttribute.getRuleAttribute().getName().equals(
128: newAttribute.getRuleAttribute().getName())) {
129: assertEquals(oldAttribute.getRequired(),
130: newAttribute.getRequired());
131: foundAttribute = true;
132: }
133: }
134: assertTrue("Could not locate new attribute.",
135: foundAttribute);
136: }
137: }
138:
139: }
|