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.junit.Test;
025:
026: import edu.iu.uis.eden.KEWServiceLocator;
027: import edu.iu.uis.eden.export.ExportDataSet;
028: import edu.iu.uis.eden.export.ExportFormat;
029: import edu.iu.uis.eden.routetemplate.RuleTemplate;
030: import edu.iu.uis.eden.routetemplate.RuleTemplateAttribute;
031: import edu.iu.uis.eden.routetemplate.RuleTemplateOption;
032: import edu.iu.uis.eden.test.OldClearDatabaseLifecycle;
033:
034: public class RuleTemplateXmlExporterTest extends XmlExporterTestCase {
035:
036: @Test
037: public void testExport() throws Exception {
038: loadXmlFile("RuleTemplateExportConfig.xml");
039: assertExport();
040: }
041:
042: protected void assertExport() throws Exception {
043: // export all existing rule templates and their dependencies (rule attributes)
044: List oldRuleTemplates = KEWServiceLocator
045: .getRuleTemplateService().findAll();
046: ExportDataSet dataSet = new ExportDataSet(ExportFormat.XML);
047: dataSet.getRuleTemplates().addAll(oldRuleTemplates);
048: dataSet.getRuleAttributes().addAll(
049: KEWServiceLocator.getRuleAttributeService().findAll());
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: // now clear the tables
056: // TestUtilities.clearDatabase();
057: new OldClearDatabaseLifecycle().start();
058:
059: // import the exported xml
060: loadXmlStream(new BufferedInputStream(new ByteArrayInputStream(
061: xmlBytes)));
062:
063: List newRuleTemplates = KEWServiceLocator
064: .getRuleTemplateService().findAll();
065: assertEquals(
066: "Should have same number of old and new RuleTemplates.",
067: oldRuleTemplates.size(), newRuleTemplates.size());
068: for (Iterator iterator = oldRuleTemplates.iterator(); iterator
069: .hasNext();) {
070: RuleTemplate oldRuleTemplate = (RuleTemplate) iterator
071: .next();
072: boolean foundTemplate = false;
073: for (Iterator iterator2 = newRuleTemplates.iterator(); iterator2
074: .hasNext();) {
075: RuleTemplate newRuleTemplate = (RuleTemplate) iterator2
076: .next();
077: if (oldRuleTemplate.getName().equals(
078: newRuleTemplate.getName())) {
079: assertRuleTemplateExport(oldRuleTemplate,
080: newRuleTemplate);
081: foundTemplate = true;
082: }
083: }
084: assertTrue(
085: "Could not locate the new rule template for name "
086: + oldRuleTemplate.getName(), foundTemplate);
087: }
088: }
089:
090: private void assertRuleTemplateExport(RuleTemplate oldRuleTemplate,
091: RuleTemplate newRuleTemplate) {
092: assertFalse("Ids should be different.", oldRuleTemplate
093: .getRuleTemplateId().equals(
094: newRuleTemplate.getRuleTemplateId()));
095: assertEquals(oldRuleTemplate.getDescription(), newRuleTemplate
096: .getDescription());
097: assertEquals(oldRuleTemplate.getName(), newRuleTemplate
098: .getName());
099: if (oldRuleTemplate.getDelegationTemplate() != null) {
100: assertRuleTemplateExport(oldRuleTemplate
101: .getDelegationTemplate(), newRuleTemplate
102: .getDelegationTemplate());
103: } else {
104: assertNull(newRuleTemplate.getDelegationTemplate());
105: }
106: assertAttributes(oldRuleTemplate.getRuleTemplateAttributes(),
107: newRuleTemplate.getRuleTemplateAttributes());
108: assertOptions(oldRuleTemplate.getRuleTemplateOptions(),
109: newRuleTemplate.getRuleTemplateOptions());
110: }
111:
112: private void assertAttributes(List oldAttributes, List newAttributes) {
113: assertEquals(oldAttributes.size(), newAttributes.size());
114: for (Iterator iterator = oldAttributes.iterator(); iterator
115: .hasNext();) {
116: RuleTemplateAttribute oldAttribute = (RuleTemplateAttribute) iterator
117: .next();
118: boolean foundAttribute = false;
119: for (Iterator iterator2 = newAttributes.iterator(); iterator2
120: .hasNext();) {
121: RuleTemplateAttribute newAttribute = (RuleTemplateAttribute) iterator2
122: .next();
123: if (oldAttribute.getRuleAttribute().getName().equals(
124: newAttribute.getRuleAttribute().getName())) {
125: assertEquals(oldAttribute.getRequired(),
126: newAttribute.getRequired());
127: foundAttribute = true;
128: }
129: }
130: assertTrue("Could not locate old attribute with name '"
131: + oldAttribute.getRuleAttribute().getName()
132: + "' in new attributes list.", foundAttribute);
133: }
134: }
135:
136: private void assertOptions(List oldTemplateOptions,
137: List newTemplateOptions) {
138: assertEquals(oldTemplateOptions.size(), newTemplateOptions
139: .size());
140: for (Iterator iterator = oldTemplateOptions.iterator(); iterator
141: .hasNext();) {
142: RuleTemplateOption oldOption = (RuleTemplateOption) iterator
143: .next();
144: boolean foundOption = false;
145: for (Iterator iterator2 = newTemplateOptions.iterator(); iterator2
146: .hasNext();) {
147: RuleTemplateOption newOption = (RuleTemplateOption) iterator2
148: .next();
149: if (oldOption.getKey().equals(newOption.getKey())) {
150: assertEquals(oldOption.getValue(), newOption
151: .getValue());
152: foundOption = true;
153: }
154: }
155: assertTrue("Could not locate rule template option.",
156: foundOption);
157: }
158: }
159:
160: }
|