001 /** 002 * Copyright 2005-2011 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.kew.xml.export; 017 018 import org.jdom.Document; 019 import org.junit.Test; 020 import org.kuali.rice.core.api.CoreApiServiceLocator; 021 import org.kuali.rice.coreservice.api.style.Style; 022 import org.kuali.rice.core.api.util.xml.XmlHelper; 023 import org.kuali.rice.core.api.util.xml.XmlJotter; 024 import org.kuali.rice.coreservice.impl.style.StyleBo; 025 import org.kuali.rice.coreservice.impl.style.StyleExportDataSet; 026 import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator; 027 import org.kuali.rice.test.BaselineTestCase; 028 029 import java.io.BufferedInputStream; 030 import java.io.ByteArrayInputStream; 031 import java.io.StringReader; 032 import java.util.Iterator; 033 import java.util.List; 034 035 import static org.junit.Assert.*; 036 037 038 /** 039 * Tests exporting Styles 040 * @author Kuali Rice Team (rice.collab@kuali.org) 041 */ 042 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE) 043 public class StyleXmlExporterTest extends XmlExporterTestCase { 044 045 public void testExportActionConfig() throws Exception { 046 // action config has no help entries 047 } 048 049 public void testExportEngineConfig() throws Exception { 050 // engine config has no help entries 051 } 052 053 /** 054 * This will test exporting some styles 055 */ 056 @Test public void testExport() throws Exception { 057 loadXmlFile("StyleExportConfig.xml"); 058 assertExport(); 059 } 060 061 protected void assertExport() throws Exception { 062 List<String> oldStyleNames = CoreServiceApiServiceLocator.getStyleService().getAllStyleNames(); 063 064 StyleExportDataSet dataSet = new StyleExportDataSet(); 065 for (String oldStyleName : oldStyleNames) { 066 Style oldStyle = CoreServiceApiServiceLocator.getStyleService().getStyle(oldStyleName); 067 assertNotNull(oldStyle); 068 dataSet.getStyles().add(StyleBo.from(oldStyle)); 069 } 070 071 byte[] xmlBytes = CoreApiServiceLocator.getXmlExporterService().export(dataSet.createExportDataSet()); 072 assertTrue("XML should be non empty.", xmlBytes != null && xmlBytes.length > 0); 073 // quick check to verify that not only is the XML non-empty, but that it might actually contain an attempt at an exported style 074 // (otherwise the XML could not contain any styles, and the test would pass with a false positive even though the export never 075 // exported anything) 076 assertTrue("XML does not contain exported style", new String(xmlBytes).contains("<styles ")); 077 assertTrue("XML does not contain exported style", new String(xmlBytes).contains("<style name=\"an_arbitrary_style\">")); 078 079 // import the exported xml 080 loadXmlStream(new BufferedInputStream(new ByteArrayInputStream(xmlBytes))); 081 082 List<String> newStyleNames = CoreServiceApiServiceLocator.getStyleService().getAllStyleNames(); 083 assertEquals("Should have same number of old and new Styles.", oldStyleNames.size(), newStyleNames.size()); 084 for (Iterator<String> iterator = oldStyleNames.iterator(); iterator.hasNext();) { 085 String oldStyleName = iterator.next(); 086 Style oldStyleEntry = CoreServiceApiServiceLocator.getStyleService().getStyle(oldStyleName); 087 assertNotNull(oldStyleEntry); 088 boolean foundAttribute = false; 089 for (String newStyleName : newStyleNames) { 090 if (oldStyleEntry.getName().equals(newStyleName)) { 091 Style newStyleEntry = CoreServiceApiServiceLocator.getStyleService().getStyle(newStyleName); 092 assertNotNull(newStyleEntry); 093 // NOTE: xmlns="http://www.w3.org/1999/xhtml" must be set on elements that contain HTML; exporter will automatically append an empty 094 // attribute, which will result in trivially unmatching content 095 assertEquals(canonicalize(oldStyleEntry.getXmlContent()), 096 canonicalize(newStyleEntry.getXmlContent())); 097 foundAttribute = true; 098 } 099 } 100 assertTrue("Could not locate the new style for name " + oldStyleEntry.getName(), foundAttribute); 101 } 102 } 103 104 private String canonicalize(String xml) throws Exception { 105 Document document = XmlHelper.buildJDocument(new StringReader(xml)); 106 return XmlJotter.jotDocument(document); 107 } 108 }