View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.php
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.xml.export;
18  
19  import java.io.BufferedInputStream;
20  import java.io.ByteArrayInputStream;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.junit.Test;
26  import org.kuali.rice.kew.export.ExportDataSet;
27  import org.kuali.rice.kew.rule.bo.RuleAttribute;
28  import org.kuali.rice.kew.rule.bo.RuleTemplateAttribute;
29  import org.kuali.rice.kew.service.KEWServiceLocator;
30  
31  
32  public class RuleAttributeXmlExporterTest extends XmlExporterTestCase {
33      
34      /**
35       * This will test some rule attributes with routing and searching config.
36       */
37  	@Test public void testExport() throws Exception {
38          loadXmlFile("RuleAttributeExportConfig.xml");
39          assertExport();
40      }
41          
42      protected void assertExport() throws Exception {
43          // export all existing rule attributes
44          List oldRuleAttributes = KEWServiceLocator.getRuleAttributeService().findAll();
45          ExportDataSet dataSet = new ExportDataSet();
46          dataSet.getRuleAttributes().addAll(oldRuleAttributes);
47          byte[] xmlBytes = KEWServiceLocator.getXmlExporterService().export(dataSet);
48          assertTrue("XML should be non empty.", xmlBytes != null && xmlBytes.length > 0);
49          
50          // import the exported xml
51          loadXmlStream(new BufferedInputStream(new ByteArrayInputStream(xmlBytes)));
52          
53          List newRuleAttributes = KEWServiceLocator.getRuleAttributeService().findAll();
54          assertEquals("Should have same number of old and new RuleAttributes.", oldRuleAttributes.size(), newRuleAttributes.size());
55          for (Iterator iterator = oldRuleAttributes.iterator(); iterator.hasNext();) {
56              RuleAttribute oldRuleAttribute = (RuleAttribute) iterator.next();
57              boolean foundAttribute = false;
58              for (Iterator iterator2 = newRuleAttributes.iterator(); iterator2.hasNext();) {
59                  RuleAttribute newRuleAttribute = (RuleAttribute) iterator2.next();
60                  if (oldRuleAttribute.getName().equals(newRuleAttribute.getName())) {
61                      assertRuleAttributeExport(oldRuleAttribute, newRuleAttribute);
62                      foundAttribute = true;
63                  }
64              }
65              assertTrue("Could not locate the new rule attribute for name " + oldRuleAttribute.getName(), foundAttribute);
66          }
67      }
68      
69      private void assertRuleAttributeExport(RuleAttribute oldRuleAttribute, RuleAttribute newRuleAttribute) {
70          // ids should be the same because we don't version rule attributes, but thier version number should be different
71          assertEquals("Ids should be the same.", oldRuleAttribute.getRuleAttributeId(), newRuleAttribute.getRuleAttributeId());
72          assertFalse("Version numbers should be different.", oldRuleAttribute.getVersionNumber().equals(newRuleAttribute.getVersionNumber()));
73          assertEquals(oldRuleAttribute.getDescription(), newRuleAttribute.getDescription());
74          assertEquals(oldRuleAttribute.getName(), newRuleAttribute.getName());
75          assertEquals(oldRuleAttribute.getLabel(), newRuleAttribute.getLabel());
76          assertEquals(oldRuleAttribute.getType(), newRuleAttribute.getType());
77          assertEquals(StringUtils.deleteWhitespace(oldRuleAttribute.getXmlConfigData()), StringUtils.deleteWhitespace(newRuleAttribute.getXmlConfigData()));
78          assertRuleTemplateAttributes(oldRuleAttribute.getRuleTemplateAttributes(), newRuleAttribute.getRuleTemplateAttributes());
79      }
80      
81      private void assertRuleTemplateAttributes(List oldRuleTemplateAttributes, List newRuleTemplateAttributes) {
82          assertEquals(oldRuleTemplateAttributes.size(), newRuleTemplateAttributes.size());
83          for (Iterator iterator = oldRuleTemplateAttributes.iterator(); iterator.hasNext();) {
84              RuleTemplateAttribute oldAttribute = (RuleTemplateAttribute) iterator.next();
85              boolean foundAttribute = false;
86              for (Iterator iterator2 = oldRuleTemplateAttributes.iterator(); iterator2.hasNext();) {
87                  RuleTemplateAttribute newAttribute = (RuleTemplateAttribute) iterator2.next();
88                  if (oldAttribute.getRuleAttribute().getName().equals(newAttribute.getRuleAttribute().getName())) {
89                      assertEquals(oldAttribute.getRequired(), newAttribute.getRequired());
90                      foundAttribute = true;
91                  }
92              }
93              assertTrue("Could not locate new attribute.", foundAttribute);
94          }
95      }
96          
97  }